/[LeafOK_CVS]/lbbs/src/editor.c
ViewVC logotype

Diff of /lbbs/src/editor.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.8 by sysadm, Wed Jun 11 08:00:51 2025 UTC Revision 1.51 by sysadm, Tue Nov 4 13:49:51 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                     editor.h  -  description  /*
3                                                           -------------------   * editor
4          copyright            : (C) 2004-2025 by Leaflet   *   - user interactive full-screen text editor
5          email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
 #include "editor.h"  
9  #include "bbs.h"  #include "bbs.h"
10    #include "common.h"
11    #include "editor.h"
12  #include "io.h"  #include "io.h"
13  #include "log.h"  #include "log.h"
14  #include "common.h"  #include "login.h"
15    #include "memory_pool.h"
16  #include "str_process.h"  #include "str_process.h"
17  #include <stdlib.h>  #include <stdlib.h>
18    #include <string.h>
19  #include <sys/param.h>  #include <sys/param.h>
 #include <strings.h>  
20    
21  #define _POSIX_C_SOURCE 200809L  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
22  #include <string.h>  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000
23    #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)
24    
25    static MEMORY_POOL *p_mp_data_line;
26    static MEMORY_POOL *p_mp_editor_data;
27    
28    int editor_memory_pool_init(void)
29    {
30            if (p_mp_data_line != NULL || p_mp_editor_data != NULL)
31            {
32                    log_error("Editor mem pool already initialized\n");
33                    return -1;
34            }
35    
36            p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT);
37            if (p_mp_data_line == NULL)
38            {
39                    log_error("Memory pool init error\n");
40                    return -2;
41            }
42    
43            p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
44            if (p_mp_editor_data == NULL)
45            {
46                    log_error("Memory pool init error\n");
47                    return -3;
48            }
49    
50            return 0;
51    }
52    
53    void editor_memory_pool_cleanup(void)
54    {
55            if (p_mp_data_line != NULL)
56            {
57                    memory_pool_cleanup(p_mp_data_line);
58                    p_mp_data_line = NULL;
59            }
60    
61            if (p_mp_editor_data != NULL)
62            {
63                    memory_pool_cleanup(p_mp_editor_data);
64                    p_mp_editor_data = NULL;
65            }
66    }
67    
68  EDITOR_DATA *editor_data_load(const char *p_data)  EDITOR_DATA *editor_data_load(const char *p_data)
69  {  {
70          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
71          char *p_data_line = NULL;          char *p_data_line = NULL;
72          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
73          long current_data_line_length = 0;          long current_data_line_length = 0;
74          long i, j;          long i;
75    
76          if (p_data == NULL)          if (p_data == NULL)
77          {          {
78                  log_error("editor_data_load() error: NULL pointer\n");                  log_error("NULL pointer error\n");
79                  return NULL;                  return NULL;
80          }          }
81    
82          p_editor_data = malloc(sizeof(EDITOR_DATA));          p_editor_data = memory_pool_alloc(p_mp_editor_data);
83          if (p_editor_data == NULL)          if (p_editor_data == NULL)
84          {          {
85                  log_error("malloc(EDITOR_DATA) error: OOM\n");                  log_error("memory_pool_alloc() error\n");
86                  return NULL;                  return NULL;
87          }          }
88    
89          p_editor_data->data_line_total = 0;          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1,
90          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);                                                                                                                   0, p_editor_data->display_line_widths);
91    
92          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
93          {          {
# Line 59  EDITOR_DATA *editor_data_load(const char Line 97  EDITOR_DATA *editor_data_load(const char
97                          current_data_line_length + p_editor_data->display_line_lengths[i] + 1 > MAX_EDITOR_DATA_LINE_LENGTH ||                          current_data_line_length + p_editor_data->display_line_lengths[i] + 1 > MAX_EDITOR_DATA_LINE_LENGTH ||
98                          (p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n'))                          (p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n'))
99                  {                  {
                         if (p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES)  
                         {  
                                 log_error("Append line error, data_line_total(%ld) reach limit(%d)\n", p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);  
                                 return NULL;  
                         }  
   
100                          // Allocate new data line                          // Allocate new data line
101                          p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                          p_data_line = memory_pool_alloc(p_mp_data_line);
102                          if (p_data_line == NULL)                          if (p_data_line == NULL)
103                          {                          {
104                                  log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);                                  log_error("memory_pool_alloc() error: i = %d\n", i);
105                                  // Cleanup                                  // Cleanup
106                                  for (j = p_editor_data->data_line_total - 1; j >= 0; j--)                                  editor_data_cleanup(p_editor_data);
                                 {  
                                         free(p_editor_data->p_data_lines[j]);  
                                 }  
                                 free(p_editor_data);  
107                                  return NULL;                                  return NULL;
108                          }                          }
                         p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                         (p_editor_data->data_line_total)++;  
109    
110                          p_editor_data->p_display_lines[i] = p_data_line;                          p_editor_data->p_display_lines[i] = p_data_line;
111                          current_data_line_length = 0;                          current_data_line_length = 0;
# Line 91  EDITOR_DATA *editor_data_load(const char Line 117  EDITOR_DATA *editor_data_load(const char
117    
118                  memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]);                  memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]);
119                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
120    
121                    // Trim \n from last line
122                    if (i + 1 == p_editor_data->display_line_total &&
123                            p_editor_data->display_line_lengths[i] > 0 &&
124                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
125                    {
126                            p_editor_data->display_line_lengths[i]--;
127                            p_editor_data->display_line_widths[i]--;
128                            current_data_line_length--;
129                    }
130                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
131          }          }
132    
133          bzero(p_editor_data->p_data_lines + p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->data_line_total);          memset(p_editor_data->p_display_lines + p_editor_data->display_line_total, 0, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);
         bzero(p_editor_data->p_display_lines + p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);  
134    
135          return p_editor_data;          return p_editor_data;
136  }  }
# Line 107  long editor_data_save(const EDITOR_DATA Line 142  long editor_data_save(const EDITOR_DATA
142    
143          if (p_editor_data == NULL || p_data == NULL)          if (p_editor_data == NULL || p_data == NULL)
144          {          {
145                  log_error("editor_data_save() error: NULL pointer\n");                  log_error("NULL pointer error\n");
146                  return -1;                  return -1;
147          }          }
148    
# Line 131  long editor_data_save(const EDITOR_DATA Line 166  long editor_data_save(const EDITOR_DATA
166    
167  void editor_data_cleanup(EDITOR_DATA *p_editor_data)  void editor_data_cleanup(EDITOR_DATA *p_editor_data)
168  {  {
169            char *p_data_line = NULL;
170          long i;          long i;
171    
172          if (p_editor_data == NULL)          if (p_editor_data == NULL)
# Line 138  void editor_data_cleanup(EDITOR_DATA *p_ Line 174  void editor_data_cleanup(EDITOR_DATA *p_
174                  return;                  return;
175          }          }
176    
177          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)          for (i = 0; i < p_editor_data->display_line_total; i++)
178            {
179                    if (p_data_line == NULL)
180                    {
181                            p_data_line = p_editor_data->p_display_lines[i];
182                    }
183    
184                    if (p_editor_data->display_line_lengths[i] > 0 &&
185                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
186                    {
187                            memory_pool_free(p_mp_data_line, p_data_line);
188                            p_data_line = NULL;
189                    }
190            }
191    
192            if (p_data_line != NULL)
193          {          {
194                  free(p_editor_data->p_data_lines[i]);                  memory_pool_free(p_mp_data_line, p_data_line);
                 p_editor_data->p_data_lines[i] = NULL;  
195          }          }
196    
197          free(p_editor_data);          memory_pool_free(p_mp_editor_data, p_editor_data);
198  }  }
199    
200  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
# Line 157  int editor_data_insert(EDITOR_DATA *p_ed Line 207  int editor_data_insert(EDITOR_DATA *p_ed
207          long offset_data_line;          long offset_data_line;
208          long last_display_line; // of data line          long last_display_line; // of data line
209          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
210            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
211          long split_line_total;          long split_line_total;
212          long i, j;          long i;
213            int len;
214            int eol;
215            int display_len;
216    
217          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
218          {          {
219                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
220                  return -1;                  return -1;
221          }          }
222    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
223          // Get length of current data line          // Get length of current data line
224          len_data_line = 0;          len_data_line = 0;
225          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 208  int editor_data_insert(EDITOR_DATA *p_ed Line 249  int editor_data_insert(EDITOR_DATA *p_ed
249          }          }
250    
251          // Split current data line if over-length          // Split current data line if over-length
252          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)          if (len_data_line + str_len + 2 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
253          {          {
254                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES || p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
255                  {                  {
256                          log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n",  #ifdef _DEBUG
257                                            p_editor_data->display_line_total, p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);                          log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
258                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
259    #endif
260    
261                          return -2;                          return -2;
262                  }                  }
263    
264                  // Allocate new data line                  // Allocate new data line
265                  p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                  p_data_line = memory_pool_alloc(p_mp_data_line);
266                  if (p_data_line == NULL)                  if (p_data_line == NULL)
267                  {                  {
268                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");                          log_error("memory_pool_alloc() error\n");
269                          return -2;                          return -2;
270                  }                  }
                 p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                 (p_editor_data->data_line_total)++;  
271    
272                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)                  if (offset_data_line + str_len + 2 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
273                  {                  {
274                          if (str[0] == CR)                          if (str[0] == CR)
275                          {                          {
# Line 273  int editor_data_insert(EDITOR_DATA *p_ed Line 315  int editor_data_insert(EDITOR_DATA *p_ed
315                          *p_offset = offset + str_len;                          *p_offset = offset + str_len;
316                  }                  }
317    
318                    // Update display width of current display line
319                    len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0);
320                    p_editor_data->display_line_widths[display_line] = display_len;
321    
322                  split_line_total = last_display_line - display_line + 3;                  split_line_total = last_display_line - display_line + 3;
323    
324                  // Set start display_line for spliting new data line                  // Set start display_line for spliting new data line
# Line 295  int editor_data_insert(EDITOR_DATA *p_ed Line 341  int editor_data_insert(EDITOR_DATA *p_ed
341          }          }
342    
343          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
344          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
345    
346          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
347          {          {
# Line 304  int editor_data_insert(EDITOR_DATA *p_ed Line 350  int editor_data_insert(EDITOR_DATA *p_ed
350                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
351                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
352                          {                          {
353    #ifdef _DEBUG
354                                  log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                                  log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
355                                  return -3;  #endif
356                          }  
357                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)                                  // Terminate prior display line with \n, to avoid error on cleanup
358                          {                                  if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
359                                  p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];                                  {
360                                  p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];                                          len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
361                                            p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
362                                            p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
363                                            p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
364                                            p_editor_data->display_line_widths[display_line + i - 1] = display_len;
365                                    }
366                                    if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
367                                    {
368                                            *p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1;
369                                    }
370                                    break;
371                          }                          }
372    
373                            // for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
374                            // {
375                            //      p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
376                            //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
377                            //      p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1];
378                            // }
379                            memmove(p_editor_data->p_display_lines + last_display_line + 2,
380                                            p_editor_data->p_display_lines + last_display_line + 1,
381                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
382                                                    sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
383                            memmove(p_editor_data->display_line_lengths + last_display_line + 2,
384                                            p_editor_data->display_line_lengths + last_display_line + 1,
385                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
386                                                    sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
387                            memmove(p_editor_data->display_line_widths + last_display_line + 2,
388                                            p_editor_data->display_line_widths + last_display_line + 1,
389                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
390                                                    sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
391    
392                          last_display_line++;                          last_display_line++;
393                            *p_last_updated_line = p_editor_data->display_line_total;
394                          (p_editor_data->display_line_total)++;                          (p_editor_data->display_line_total)++;
395                  }                  }
396    
397                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
398                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
399                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
400                          (i == 0                          (i == 0
401                                   ? p_data_line                                   ? p_data_line
# Line 331  int editor_data_insert(EDITOR_DATA *p_ed Line 410  int editor_data_insert(EDITOR_DATA *p_ed
410    
411          *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);          *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
412    
413          if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] ||          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
                 (*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] &&  
                  p_editor_data->p_display_lines[*p_display_line][*p_offset - 1] == '\n'))  
414          {          {
415                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                  if (*p_display_line + 1 < p_editor_data->display_line_total)
416                  (*p_display_line)++;                  {
417                            *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
418                            (*p_display_line)++;
419                    }
420            }
421    
422                  if (*p_display_line >= p_editor_data->display_line_total)          // Prevent the last display line from being over-length
423            if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
424            {
425                    len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
426                    p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
427                    p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
428                    p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len;
429                    if (*p_display_line + 1 >= p_editor_data->display_line_total)
430                  {                  {
431                          log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);                          *p_offset = MIN(*p_offset, len);
432                            *p_display_line = p_editor_data->display_line_total - 1;
433                  }                  }
434          }          }
435    
436          return 0;          return 0;
437  }  }
438    
439  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,  int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
440                                             long *p_last_updated_line)                                             long *p_last_updated_line, int del_line)
441  {  {
442            long display_line = *p_display_line;
443            long offset = *p_offset;
444          char *p_data_line = NULL;          char *p_data_line = NULL;
445          long len_data_line;          long len_data_line;
446          long offset_data_line;          long offset_data_line;
447          long last_display_line; // of data line          long last_display_line; // of data line
448          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
449            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
450          long split_line_total;          long split_line_total;
451          long i, j;          long i, j;
452          int str_len = 0;          int str_len = 0;
453            char c;
454    
455          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
456          {          {
457                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
458                  return -1;                  return -1;
459          }          }
460    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
461          // Get length of current data line          // Get length of current data line
462          len_data_line = 0;          len_data_line = 0;
463          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 406  int editor_data_delete(EDITOR_DATA *p_ed Line 486  int editor_data_delete(EDITOR_DATA *p_ed
486                  }                  }
487          }          }
488    
489            if (offset_data_line >= len_data_line) // end-of-line
490            {
491                    return 0;
492            }
493    
494          // Check str to be deleted          // Check str to be deleted
495          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (del_line)
496            {
497                    str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
498            }
499            else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
500          {          {
501                  str_len = 1;                  str_len = 1;
502          }          }
503          else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK          else if (p_data_line[offset_data_line] & 0x80) // head of multi-byte character
504          {          {
505                  str_len = 2;                  str_len = 1;
506                    c = (p_data_line[offset_data_line] & 0x70) << 1;
507                    while (c & 0x80)
508                    {
509                            str_len++;
510                            c = (c & 0x7f) << 1;
511                    }
512          }          }
513          else          else
514          {          {
515                  log_error("Some strange character at display_line %ld, offset %ld\n", display_line, offset);                  log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
516                  return -2;                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
517                    str_len = 1;
518          }          }
519    
520          // Current display line is (almost) empty          // Current display line is (almost) empty
521          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
522                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line &&
523                     p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
524          {          {
525                  log_error("Nothing to be delete\n");                  if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
526                  return 0;                  {
527          }                          return 0;
528                    }
529    
530                    len_data_line = 0; // Next data line
531                    last_display_line = p_editor_data->display_line_total - 1;
532                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
533                    {
534                            len_data_line += p_editor_data->display_line_lengths[i];
535    
536                            if (p_editor_data->display_line_lengths[i] > 0 &&
537                                    p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
538                            {
539                                    last_display_line = i;
540                                    break;
541                            }
542                    }
543    
544                    if (offset_data_line + len_data_line + 1 > MAX_EDITOR_DATA_LINE_LENGTH) // No enough buffer to merge current data line with next data line
545                    {
546                            return 0;
547                    }
548    
549          memmove(p_data_line + offset_data_line, p_data_line + offset_data_line + str_len, (size_t)(len_data_line - offset_data_line - str_len));                  // Append next data line to current one
550          p_data_line[len_data_line - str_len] = '\0';                  memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
551          len_data_line -= str_len;                  p_data_line[offset_data_line + len_data_line] = '\0';
552    
553                    // Recycle next data line
554                    memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]);
555            }
556            else
557            {
558                    memmove(p_data_line + offset_data_line, p_data_line + offset_data_line + str_len, (size_t)(len_data_line - offset_data_line - str_len));
559                    p_data_line[len_data_line - str_len] = '\0';
560                    len_data_line -= str_len;
561            }
562    
563          // Set p_data_line to head of current display line          // Set p_data_line to head of current display line
564          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
565          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
566    
567          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
568          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
569    
570          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
571          {          {
572                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
573                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
574                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
575                          (i == 0                          (i == 0
576                                   ? p_data_line                                   ? p_data_line
# Line 457  int editor_data_delete(EDITOR_DATA *p_ed Line 585  int editor_data_delete(EDITOR_DATA *p_ed
585    
586          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
587    
588          if (display_line + i < last_display_line)          if (*p_last_updated_line < last_display_line)
589          {          {
590                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
591                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)                  // for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
592                  {                  // {
593                          p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j];                  //      p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j];
594                          p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j];                  //      p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];
595                  }                  //      p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j];
596                    // }
597                  (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));                  memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
598                  last_display_line = display_line + i;                                  p_editor_data->p_display_lines + last_display_line + 1,
599                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
600                  *p_last_updated_line = p_editor_data->display_line_total - 1;                                          sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
601                    memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
602                                    p_editor_data->display_line_lengths + last_display_line + 1,
603                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
604                                            sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
605                    memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1,
606                                    p_editor_data->display_line_widths + last_display_line + 1,
607                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
608                                            sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
609    
610                    j = p_editor_data->display_line_total;
611                    (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
612                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
613          }          }
614    
615          return 0;          // Return real offset
616            *p_offset = offset;
617    
618            return str_len;
619  }  }
620    
621  static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)  static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)
# Line 481  static int editor_display_key_handler(in Line 624  static int editor_display_key_handler(in
624          {          {
625          case 0: // Set msg          case 0: // Set msg
626                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
627                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
628                    break;
629            case KEY_CSI:
630                    *p_key = KEY_ESC;
631                  break;                  break;
632          }          }
633    
# Line 495  int editor_display(EDITOR_DATA *p_editor Line 641  int editor_display(EDITOR_DATA *p_editor
641          EDITOR_CTX ctx;          EDITOR_CTX ctx;
642          int ch = 0;          int ch = 0;
643          char input_str[4];          char input_str[4];
644            char c;
645          int str_len = 0;          int str_len = 0;
646          int input_ok;          int input_ok;
         int screen_current_row;  
647          const int screen_begin_row = 1;          const int screen_begin_row = 1;
648          int screen_end_row = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
649          const int screen_row_total = screen_end_row - screen_begin_row + 1;          int output_current_row = screen_begin_row;
650            int output_end_row = SCREEN_ROWS - 1;
651          long int line_current = 0;          long int line_current = 0;
652          long int len;          long int len;
653          int loop;          int loop;
# Line 511  int editor_display(EDITOR_DATA *p_editor Line 658  int editor_display(EDITOR_DATA *p_editor
658          int scroll_rows;          int scroll_rows;
659          long last_updated_line = 0;          long last_updated_line = 0;
660          int key_insert = 1;          int key_insert = 1;
661          int i;          int i, j;
662            char *p_str;
663            int del_line;
664    
665          screen_current_row = screen_begin_row;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_row, SCREEN_ROWS);  
666    
667          // update msg_ext with extended key handler          // update msg_ext with extended key handler
668          if (editor_display_key_handler(&ch, &ctx) != 0)          if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 522  int editor_display(EDITOR_DATA *p_editor Line 670  int editor_display(EDITOR_DATA *p_editor
670                  return ch;                  return ch;
671          }          }
672    
673          loop = 1;          for (loop = 1; !SYS_server_exit && loop;)
         while (!SYS_server_exit && loop)  
674          {          {
675                  if (line_current >= p_editor_data->display_line_total || screen_current_row > screen_end_row)                  if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row)
676                  {                  {
677                          ctx.line_cursor = line_current - screen_current_row + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
678    
679                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
680                                           "\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] "                                           "\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] "
681                                           "µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ [\033[32m%s\033[33m] "                                           "第\033[32m%ld\033[33m/\033[32m%ld\033[33m行 [\033[32m%s\033[33m] "
682                                           "%s",                                           "%s",
683                                           row_pos, col_pos,                                           row_pos, col_pos,
684                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
685                                           key_insert ? "²åÈë" : "¸Äд",                                           key_insert ? "æ’å…¥" : "替æ¢",
686                                           ctx.msg);                                           ctx.msg);
687    
688                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
689                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
690                          {                          {
691                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 552  int editor_display(EDITOR_DATA *p_editor Line 699  int editor_display(EDITOR_DATA *p_editor
699                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
700                          iflush();                          iflush();
701    
702                          input_ok = 0;                          str_len = 0;
703                          while (!SYS_server_exit && !input_ok)                          ch = igetch_t(MAX_DELAY_TIME);
704                            while (!SYS_server_exit)
705                          {                          {
706                                  ch = igetch_t(MAX_DELAY_TIME);                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
707                                  input_ok = 1;                                  {
708                                            BBS_last_access_tm = time(NULL);
709                                    }
710    
711                                  // extended key handler                                  // extended key handler
712                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 564  int editor_display(EDITOR_DATA *p_editor Line 714  int editor_display(EDITOR_DATA *p_editor
714                                          goto cleanup;                                          goto cleanup;
715                                  }                                  }
716    
717                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch < 256 && (ch & 0x80)) // head of multi-byte character
                                 {  
                                         input_str[str_len] = (char)(ch - 256);  
                                         str_len++;  
                                 }  
                                 else  
718                                  {                                  {
719                                          str_len = 0;                                          str_len = 0;
720                                            c = (char)(ch & 0xf0);
721                                            while (c & 0x80)
722                                            {
723                                                    input_str[str_len] = (char)(ch - 256);
724                                                    str_len++;
725                                                    c = (c & 0x7f) << 1;
726    
727                                                    if ((c & 0x80) == 0) // Input completed
728                                                    {
729                                                            break;
730                                                    }
731    
732                                                    // Expect additional bytes of input
733                                                    ch = igetch(100);                                                // 0.1 second
734                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
735                                                    {
736    #ifdef _DEBUG
737                                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
738    #endif
739                                                            str_len = 0;
740                                                            break;
741                                                    }
742                                            }
743                                  }                                  }
744    
745                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || ch == CR) // printable character or GBK                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character
746                                            ch == CR || ch == KEY_ESC)                                // Special character
747                                  {                                  {
748                                          if (str_len == 0)                                          // Refresh current action while user input
749                                            if (user_online_update(NULL) < 0)
750                                            {
751                                                    log_error("user_online_update(NULL) error\n");
752                                            }
753    
754                                            if (str_len == 0) // ch >= 32 && ch < 127
755                                          {                                          {
756                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
757                                                  str_len = 1;                                                  str_len = 1;
758                                          }                                          }
759    
760                                          last_updated_line = line_current;                                          display_line_in = line_current - output_current_row + row_pos;
761                                          display_line_in = line_current - screen_current_row + row_pos;                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
                                         offset_in = col_pos - 1;  
762                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
763                                          offset_out = offset_in;                                          offset_out = offset_in;
764    
765                                            last_updated_line = display_line_in;
766    
767                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
768                                          {                                          {
769                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
770                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line, 0) < 0)
771                                                  {                                                  {
772                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
773                                                  }                                                  }
# Line 600  int editor_display(EDITOR_DATA *p_editor Line 776  int editor_display(EDITOR_DATA *p_editor
776                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
777                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
778                                          {                                          {
779                                                  log_error("editor_data_insert(%s) error\n", input_str);                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);
                                                 str_len = 0;  
780                                          }                                          }
781                                          else                                          else
782                                          {                                          {
783                                                  str_len = 0;                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
784                                                    line_current -= (output_current_row - row_pos);
785                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  output_current_row = (int)row_pos;
                                                 line_current -= (screen_current_row - row_pos);  
                                                 screen_current_row = (int)row_pos;  
786    
787                                                  scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row));                                                  scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row));
788    
789                                                  if (scroll_rows > 0)                                                  if (scroll_rows > 0)
790                                                  {                                                  {
# Line 619  int editor_display(EDITOR_DATA *p_editor Line 792  int editor_display(EDITOR_DATA *p_editor
792                                                          clrtoeol();                                                          clrtoeol();
793                                                          for (i = 0; i < scroll_rows; i++)                                                          for (i = 0; i < scroll_rows; i++)
794                                                          {                                                          {
795                                                                  prints("\033[S"); // Scroll up 1 line                                                                  // prints("\033[S"); // Scroll up 1 line
796                                                                    prints("\n"); // Legacy Cterm only works with this line
797                                                          }                                                          }
798    
799                                                          screen_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
800                                                          if (screen_current_row < screen_begin_row)                                                          if (output_current_row < screen_begin_row)
801                                                          {                                                          {
802                                                                  line_current += (screen_begin_row - screen_current_row);                                                                  line_current += (screen_begin_row - output_current_row);
803                                                                  screen_current_row = screen_begin_row;                                                                  output_current_row = screen_begin_row;
804                                                          }                                                          }
805                                                          row_pos = screen_end_row;                                                          row_pos = output_end_row;
806                                                  }                                                  }
807                                                  else // if (scroll_lines == 0)                                                  else // if (scroll_lines == 0)
808                                                  {                                                  {
809                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
810                                                  }                                                  }
                                                 col_pos = offset_out + 1;  
811    
812                                                  continue;                                                  if (offset_out != offset_in)
813                                                    {
814                                                            if (display_line_out != display_line_in)
815                                                            {
816                                                                    col_pos = 1;
817                                                            }
818                                                            if (offset_out > 0)
819                                                            {
820                                                                    col_pos += (str_len == 1 ? 1 : 2);
821                                                            }
822                                                    }
823                                          }                                          }
824    
825                                            if (display_line_out != display_line_in) // Output on line change
826                                            {
827                                                    break;
828                                            }
829    
830                                            ch = igetch(0);
831                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
832                                            {
833                                                    break;
834                                            }
835    
836                                            str_len = 0;
837                                            continue;
838                                  }                                  }
839                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
840                                  {                                  {
841                                          last_updated_line = line_current;                                          // Refresh current action while user input
842                                            if (user_online_update(NULL) < 0)
843                                            {
844                                                    log_error("user_online_update(NULL) error\n");
845                                            }
846    
847                                            del_line = 0;
848    
849                                            if (ch == BACKSPACE)
850                                            {
851                                                    if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
852                                                    {
853                                                            break; // force output prior operation result if any
854                                                    }
855    
856                                                    offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
857                                                                                               (int)col_pos - 1, &eol, &display_len, 0);
858                                                    if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
859                                                    {
860                                                            col_pos = display_len - 1;
861                                                    }
862                                                    else
863                                                    {
864                                                            col_pos = display_len;
865                                                    }
866    
867                                          if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
868                                                                                     &last_updated_line) < 0)                                                  {
869                                                            row_pos--;
870                                                            col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
871                                                    }
872                                            }
873                                            else if (ch == Ctrl('K'))
874                                          {                                          {
875                                                  log_error("editor_data_delete() error\n");                                                  del_line = 1;
876                                            }
877                                            else if (ch == Ctrl('Y'))
878                                            {
879                                                    col_pos = 1;
880                                                    del_line = 1;
881                                            }
882    
883                                            display_line_in = line_current - output_current_row + row_pos;
884                                            offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
885                                            display_line_out = display_line_in;
886                                            offset_out = offset_in;
887    
888                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
889                                                                                                              &last_updated_line, del_line)) < 0)
890                                            {
891                                                    log_error("editor_data_delete() error: %d\n", str_len);
892                                          }                                          }
893                                          else                                          else
894                                          {                                          {
895                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  col_pos = display_len + 1; // Set col_pos to accurate pos
896                                                  line_current -= (screen_current_row - row_pos);  
897                                                  screen_current_row = (int)row_pos;                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
898                                                    line_current -= (output_current_row - row_pos);
899                                                    output_current_row = (int)row_pos;
900    
901                                                    if (output_current_row < screen_begin_row) // row_pos <= 0
902                                                    {
903                                                            output_current_row = screen_begin_row;
904                                                            row_pos = screen_begin_row;
905                                                            output_end_row = SCREEN_ROWS - 1;
906                                                    }
907    
908                                                    // Exceed end
909                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
910                                                            p_editor_data->display_line_total > screen_row_total)
911                                                    {
912                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
913                                                                                                    (p_editor_data->display_line_total - screen_row_total));
914    
915                                                            line_current = p_editor_data->display_line_total - screen_row_total;
916                                                            row_pos += scroll_rows;
917                                                            output_current_row = screen_begin_row;
918                                                            output_end_row = SCREEN_ROWS - 1;
919                                                    }
920    
921                                                    clrline(output_current_row, output_end_row);
922                                            }
923    
924                                            if (display_line_out != display_line_in) // Output on line change
925                                            {
926                                                    break;
927                                            }
928    
929                                            ch = igetch(0);
930                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
931                                            {
932                                                    break;
933                                          }                                          }
934    
935                                            str_len = 0;
936                                          continue;                                          continue;
937                                  }                                  }
938    
939                                    input_ok = 1;
940                                  switch (ch)                                  switch (ch)
941                                  {                                  {
942                                  case KEY_NULL:                                  case KEY_NULL:
943                                            log_error("KEY_NULL\n");
944                                            goto cleanup;
945                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
946                                            log_error("User input timeout\n");
947                                          goto cleanup;                                          goto cleanup;
948                                  case Ctrl('C'):                                  case Ctrl('W'):
949                                    case Ctrl('X'):
950                                          loop = 0;                                          loop = 0;
951                                          break;                                          break;
952                                    case Ctrl('S'): // Start of line
953                                  case KEY_CTRL_LEFT:                                  case KEY_CTRL_LEFT:
954                                          col_pos = 1;                                          col_pos = 1;
955                                          break;                                          break;
956                                    case Ctrl('E'): // End of line
957                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
958                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
959                                            {
960                                                    // last display line does NOT have \n in the end
961                                                    col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
962                                                    break;
963                                            }
964                                            col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
965                                          break;                                          break;
966                                    case Ctrl('T'): // Top of screen
967                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
968                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
969                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
970                                          break;                                          break;
971                                    case Ctrl('B'): // Bottom of screen
972                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
973                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
974                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          {
975                                                    row_pos = p_editor_data->display_line_total;
976                                            }
977                                            else
978                                            {
979                                                    row_pos = SCREEN_ROWS - 1;
980                                            }
981                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
982                                            {
983                                                    // last display line does NOT have \n in the end
984                                                    col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1);
985                                            }
986                                            else
987                                            {
988                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
989                                            }
990                                          break;                                          break;
991                                  case KEY_INS:                                  case KEY_INS:
992                                          key_insert = !key_insert;                                          key_insert = !key_insert;
# Line 686  int editor_display(EDITOR_DATA *p_editor Line 994  int editor_display(EDITOR_DATA *p_editor
994                                  case KEY_HOME:                                  case KEY_HOME:
995                                          row_pos = 1;                                          row_pos = 1;
996                                          col_pos = 1;                                          col_pos = 1;
997                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
998                                          {                                          {
999                                                  break;                                                  break;
1000                                          }                                          }
1001                                          line_current = 0;                                          line_current = 0;
1002                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1003                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1004                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1005                                          break;                                          break;
1006                                  case KEY_END:                                  case KEY_END:
1007                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
1008                                          {                                          {
1009                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
1010                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1011                                                  break;                                                  break;
1012                                          }                                          }
1013                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
1014                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1015                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1016                                          row_pos = screen_row_total;                                          row_pos = SCREEN_ROWS - 1;
1017                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1018                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1019                                          break;                                          break;
1020                                  case KEY_LEFT:                                  case KEY_LEFT:
1021                                          if (col_pos > 1)                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1022                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1023                                            if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
1024                                            {
1025                                                    col_pos = display_len - 1;
1026                                            }
1027                                            else
1028                                            {
1029                                                    col_pos = display_len;
1030                                            }
1031                                            if (col_pos >= 1)
1032                                          {                                          {
                                                 col_pos--;  
1033                                                  break;                                                  break;
1034                                          }                                          }
1035                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 720  int editor_display(EDITOR_DATA *p_editor Line 1037  int editor_display(EDITOR_DATA *p_editor
1037                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
1038                                          {                                          {
1039                                                  row_pos--;                                                  row_pos--;
1040                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1041                                                  break;                                                  break;
1042                                          }                                          }
1043                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
1044                                          {                                          {
1045                                                  col_pos = 1;                                                  col_pos = 1;
1046                                                  break;                                                  break;
1047                                          }                                          }
1048                                          line_current -= screen_current_row;                                          line_current -= output_current_row;
1049                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1050                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
1051                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
1052                                          screen_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line                                          output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
1053                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
                                         break;  
                                 case KEY_SPACE:  
1054                                          break;                                          break;
1055                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1056                                          if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1057                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1058                                            if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&
1059                                                    p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8
1060                                            {
1061                                                    col_pos = display_len + 3;
1062                                            }
1063                                            else
1064                                            {
1065                                                    col_pos = display_len + 2;
1066                                            }
1067                                            if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1068                                          {                                          {
                                                 col_pos++;  
1069                                                  break;                                                  break;
1070                                          }                                          }
1071                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
# Line 748  int editor_display(EDITOR_DATA *p_editor Line 1073  int editor_display(EDITOR_DATA *p_editor
1073                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))
1074                                          {                                          {
1075                                                  row_pos++;                                                  row_pos++;
1076                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1077                                                  break;                                                  break;
1078                                          }                                          }
1079                                          if (line_current + (screen_row_total - (screen_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
1080                                          {                                          {
1081                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                  // last display line does NOT have \n in the end
1082                                                    col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1083                                                  break;                                                  break;
1084                                          }                                          }
1085                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
1086                                          screen_current_row = screen_row_total;                                          output_current_row = screen_row_total;
1087                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1088                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1089                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1090                                          clrtoeol();                                          clrtoeol();
1091                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
1092                                            prints("\n"); // Legacy Cterm only works with this line
1093                                          break;                                          break;
1094                                  case KEY_PGUP:                                  case KEY_PGUP:
1095                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
1096                                          {                                          {
1097                                                  break;                                                  break;
1098                                          }                                          }
1099                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
1100                                          if (line_current < 0)                                          if (line_current < 0)
1101                                          {                                          {
1102                                                  line_current = 0;                                                  line_current = 0;
1103                                          }                                          }
1104                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1105                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1106                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1107                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1108                                          break;                                          break;
1109                                  case KEY_PGDN:                                  case KEY_PGDN:
1110                                          if (line_current + screen_row_total - (screen_current_row - screen_begin_row) >= p_editor_data->display_line_total) // Reach end                                          if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= p_editor_data->display_line_total) // Reach end
1111                                          {                                          {
1112                                                  break;                                                  break;
1113                                          }                                          }
1114                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
1115                                          if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display                                          if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display
1116                                          {                                          {
1117                                                  line_current = p_editor_data->display_line_total - screen_row_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
1118                                          }                                          }
1119                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1120                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1121                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1122                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
1123                                          break;                                          break;
1124                                    case Ctrl('Q'):
1125                                  case KEY_F1:                                  case KEY_F1:
1126                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not re-entrant
1127                                          {                                          {
1128                                                  break;                                                  break;
1129                                          }                                          }
1130                                          // Display help information                                          // Display help information
1131                                          show_help = 0;                                          show_help = 0;
1132                                          display_file(DATA_READ_HELP, 1);                                          display_file(DATA_EDITOR_HELP, 1);
1133                                          show_help = 1;                                          show_help = 1;
1134                                  case KEY_F5:                                  case KEY_F5:
1135                                          // Refresh after display help information                                          // Refresh after display help information
1136                                          line_current -= (screen_current_row - screen_begin_row);                                          line_current -= (output_current_row - screen_begin_row);
1137                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1138                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1139                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1140                                          break;                                          break;
1141                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
1142                                          break;                                          break;
# Line 819  int editor_display(EDITOR_DATA *p_editor Line 1145  int editor_display(EDITOR_DATA *p_editor
1145                                          break;                                          break;
1146                                  }                                  }
1147    
1148                                  BBS_last_access_tm = time(0);                                  // Refresh current action while user input
1149                                    if (user_online_update(NULL) < 0)
1150                                    {
1151                                            log_error("user_online_update(NULL) error\n");
1152                                    }
1153    
1154                                    if (input_ok)
1155                                    {
1156                                            break;
1157                                    }
1158    
1159                                    ch = igetch_t(MAX_DELAY_TIME);
1160                          }                          }
1161    
1162                          continue;                          continue;
# Line 837  int editor_display(EDITOR_DATA *p_editor Line 1174  int editor_display(EDITOR_DATA *p_editor
1174                          len = 0;                          len = 0;
1175                  }                  }
1176    
1177                  memcpy(buffer, (const char *)p_editor_data->p_display_lines[line_current], (size_t)len);                  // memcpy(buffer, p_editor_data->p_display_lines[line_current], (size_t)len);
1178                  buffer[len] = '\0';                  // Replace '\033' with '*'
1179                    p_str = p_editor_data->p_display_lines[line_current];
1180                    for (i = 0, j = 0; i < len; i++)
1181                    {
1182                            if (p_str[i] == '\033')
1183                            {
1184                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1185                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1186                            }
1187                            else
1188                            {
1189                                    buffer[j] = p_str[i];
1190                                    j++;
1191                            }
1192                    }
1193                    buffer[j] = '\0';
1194    
1195                  moveto(screen_current_row, 0);                  moveto(output_current_row, 0);
1196                  clrtoeol();                  clrtoeol();
1197                  prints("%s", buffer);                  prints("%s", buffer);
1198                  line_current++;                  line_current++;
1199                  screen_current_row++;                  output_current_row++;
1200          }          }
1201    
1202  cleanup:  cleanup:


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1