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


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

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