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


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

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