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


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

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