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


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

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