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

Diff of /lbbs/src/editor.c

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

Revision 1.2 by sysadm, Mon Jun 9 15:41:09 2025 UTC Revision 1.18 by sysadm, Fri Jun 13 11:20:24 2025 UTC
# Line 20  Line 20 
20  #include "log.h"  #include "log.h"
21  #include "common.h"  #include "common.h"
22  #include "str_process.h"  #include "str_process.h"
23    #include "memory_pool.h"
24  #include <stdlib.h>  #include <stdlib.h>
25  #include <sys/param.h>  #include <sys/param.h>
26    #include <strings.h>
27    
28  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
29  #include <string.h>  #include <string.h>
30    
31    #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
32    #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000
33    #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)
34    
35    static MEMORY_POOL *p_mp_data_line;
36    static MEMORY_POOL *p_mp_editor_data;
37    
38    int editor_memory_pool_init(void)
39    {
40            if (p_mp_data_line != NULL || p_mp_editor_data != NULL)
41            {
42                    log_error("Editor mem pool already initialized\n");
43                    return -1;
44            }
45    
46            p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT);
47            if (p_mp_data_line == NULL)
48            {
49                    log_error("Memory pool init error\n");
50                    return -2;
51            }
52    
53            p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
54            if (p_mp_data_line == NULL)
55            {
56                    log_error("Memory pool init error\n");
57                    return -3;
58            }
59    
60            return 0;
61    }
62    
63    void editor_memory_pool_cleanup(void)
64    {
65            if (p_mp_data_line != NULL)
66            {
67                    memory_pool_cleanup(p_mp_data_line);
68                    p_mp_data_line = NULL;
69            }
70    
71            if (p_mp_editor_data != NULL)
72            {
73                    memory_pool_cleanup(p_mp_editor_data);
74                    p_mp_editor_data = NULL;
75            }
76    }
77    
78  EDITOR_DATA *editor_data_load(const char *p_data)  EDITOR_DATA *editor_data_load(const char *p_data)
79  {  {
80          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
81            char *p_data_line = NULL;
82          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES];
83          long current_data_line_length = 0;          long current_data_line_length = 0;
84          long i, j;          long i;
85    
86          if (p_data == NULL)          if (p_data == NULL)
87          {          {
# Line 39  EDITOR_DATA *editor_data_load(const char Line 89  EDITOR_DATA *editor_data_load(const char
89                  return NULL;                  return NULL;
90          }          }
91    
92          p_editor_data = malloc(sizeof(EDITOR_DATA));          p_editor_data = memory_pool_alloc(p_mp_editor_data);
93          if (p_editor_data == NULL)          if (p_editor_data == NULL)
94          {          {
95                  log_error("malloc(EDITOR_DATA) error: OOM\n");                  log_error("memory_pool_alloc() error\n");
96                  return NULL;                  return NULL;
97          }          }
98    
         p_editor_data->data_line_total = 0;  
99          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);
100    
101          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
# Line 57  EDITOR_DATA *editor_data_load(const char Line 106  EDITOR_DATA *editor_data_load(const char
106                          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 ||
107                          (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'))
108                  {                  {
                         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;  
                         }  
   
109                          // Allocate new data line                          // Allocate new data line
110                          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);
111                          if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)                          if (p_data_line == NULL)
112                          {                          {
113                                  log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);                                  log_error("memory_pool_alloc() error: i = %d\n", i);
114                                  // Cleanup                                  // Cleanup
115                                  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);  
116                                  return NULL;                                  return NULL;
117                          }                          }
118    
119                          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)++;  
120                          current_data_line_length = 0;                          current_data_line_length = 0;
121                  }                  }
122                  else                  else
# Line 88  EDITOR_DATA *editor_data_load(const char Line 126  EDITOR_DATA *editor_data_load(const char
126    
127                  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]);
128                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
129                  p_editor_data->p_data_lines[p_editor_data->data_line_total - 1][current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
130          }          }
131    
132            bzero(p_editor_data->p_display_lines + p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);
133    
134          return p_editor_data;          return p_editor_data;
135  }  }
136    
# Line 125  long editor_data_save(const EDITOR_DATA Line 165  long editor_data_save(const EDITOR_DATA
165    
166  void editor_data_cleanup(EDITOR_DATA *p_editor_data)  void editor_data_cleanup(EDITOR_DATA *p_editor_data)
167  {  {
168            char *p_data_line = NULL;
169          long i;          long i;
170    
171          if (p_editor_data == NULL)          if (p_editor_data == NULL)
# Line 132  void editor_data_cleanup(EDITOR_DATA *p_ Line 173  void editor_data_cleanup(EDITOR_DATA *p_
173                  return;                  return;
174          }          }
175    
176          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)          for (i = 0; i < p_editor_data->display_line_total; i++)
177            {
178                    if (p_data_line == NULL)
179                    {
180                            p_data_line = p_editor_data->p_display_lines[i];
181                    }
182    
183                    if (p_editor_data->display_line_lengths[i] > 0 &&
184                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
185                    {
186                            memory_pool_free(p_mp_data_line, p_data_line);
187                            p_data_line = NULL;
188                    }
189            }
190    
191            if (p_data_line != NULL)
192          {          {
193                  free(p_editor_data->p_data_lines[i]);                  memory_pool_free(p_mp_data_line, p_data_line);
                 p_editor_data->p_data_lines[i] = NULL;  
194          }          }
195    
196          free(p_editor_data);          memory_pool_free(p_mp_editor_data, p_editor_data);
197  }  }
198    
199  int editor_data_insert(EDITOR_DATA *p_editor_data, long display_line, long offset,  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
200                                             const char *str, int str_len, long *p_last_updated_line)                                             const char *str, int str_len, long *p_last_updated_line)
201  {  {
202          int len;          long display_line = *p_display_line;
203          int display_len;          long offset = *p_offset;
204          int eol;          char *p_data_line = NULL;
         char *p_data_line;  
205          long len_data_line;          long len_data_line;
206          long offset_data_line;          long offset_data_line;
207          long last_display_line; // of data line          long last_display_line; // of data line
208          char buf_insert[MAX_EDITOR_DATA_LINE_LENGTH];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
209          long len_insert;          long split_line_total;
210          int display_len_insert;          long i, j;
211          char buf_catenate[MAX_EDITOR_DATA_LINE_LENGTH];          int len;
212          long len_catenate;          int eol;
213          long i;          int display_len;
214    
215          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
216          {          {
# Line 164  int editor_data_insert(EDITOR_DATA *p_ed Line 218  int editor_data_insert(EDITOR_DATA *p_ed
218                  return -1;                  return -1;
219          }          }
220    
         memcpy(buf_insert, str, (size_t)str_len);  
         buf_insert[str_len] = '\0';  
         len_insert = str_len;  
   
221          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
222          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
223          {          {
224                  if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK                  if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
225                  {                  {
226                          i++;                          i++;
227                  }                  }
# Line 210  int editor_data_insert(EDITOR_DATA *p_ed Line 260  int editor_data_insert(EDITOR_DATA *p_ed
260          }          }
261    
262          // Split current data line if over-length          // Split current data line if over-length
263          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH)          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
264          {          {
265                  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)
266                  {                  {
267                          log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n",                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
268                                            p_editor_data->display_line_total, p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);                          //                p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
269                          return -2;                          return -2;
270                  }                  }
271    
272                  // Allocate new data line                  // Allocate new data line
273                  p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                  p_data_line = memory_pool_alloc(p_mp_data_line);
274                  if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)                  if (p_data_line == NULL)
275                  {                  {
276                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");                          log_error("memory_pool_alloc() error\n");
277                          return -2;                          return -2;
278                  }                  }
279    
280                  // Copy rest part of current data line since next display line to new data line                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
                 memcpy(p_editor_data->p_data_lines[p_editor_data->data_line_total],  
                            p_editor_data->p_display_lines[display_line + 1],  
                            (size_t)(len_data_line - (p_editor_data->p_display_lines[display_line + 1] - p_data_line)));  
                 p_editor_data->p_data_lines[p_editor_data->data_line_total]  
                                                                    [len_data_line - (p_editor_data->p_display_lines[display_line + 1] - p_data_line)] = '\0';  
   
                 p_data_line = p_editor_data->p_display_lines[display_line + 1];  
                 for (i = display_line + 1; i <= last_display_line; i++)  
281                  {                  {
282                          p_editor_data->p_display_lines[i] +=                          if (str[0] == CR)
283                                  (p_editor_data->p_data_lines[p_editor_data->data_line_total] - p_data_line);                          {
284                  }                                  str_len = 0;
285                            }
286    
287                  // Copy rest part of current display line to buffer                          // Copy str to new data line
288                  if (offset_data_line >= MAX_EDITOR_DATA_LINE_LENGTH / 2)                          memcpy(p_data_line, str, (size_t)str_len);
289                  {  
290                          memcpy(buf_insert + len_insert,                          // Copy rest part of current data line to new data line
291                                     p_editor_data->p_display_lines[display_line] + offset,                          memcpy(p_data_line + str_len,
                                    (size_t)(p_editor_data->display_line_lengths[display_line] - offset));  
                         len_insert += (p_editor_data->display_line_lengths[display_line] - offset);  
                 }  
                 else  
                 {  
                         memcpy(buf_insert,  
292                                     p_editor_data->p_display_lines[display_line] + offset,                                     p_editor_data->p_display_lines[display_line] + offset,
293                                     (size_t)(p_editor_data->display_line_lengths[display_line] - offset));                                     (size_t)(len_data_line - offset_data_line));
294                          len_insert = (p_editor_data->display_line_lengths[display_line] - offset);  
295                  }                          p_data_line[str_len + len_data_line - offset_data_line] = '\0';
                 buf_insert[len_insert] = '\0';  
296    
                 if (offset_data_line >= MAX_EDITOR_DATA_LINE_LENGTH / 2)  
                 {  
297                          // Add line ending to current display line (data line)                          // Add line ending to current display line (data line)
298                          p_editor_data->p_display_lines[display_line][offset] = '\n';                          p_editor_data->p_display_lines[display_line][offset] = '\n';
299                          p_editor_data->p_display_lines[display_line][offset + 1] = '\0';                          p_editor_data->p_display_lines[display_line][offset + 1] = '\0';
300                          p_editor_data->display_line_lengths[display_line] = offset + 1;                          p_editor_data->display_line_lengths[display_line] = offset + 1;
301    
302                            *p_display_line = display_line + 1;
303                            *p_offset = str_len;
304                  }                  }
305                  else                  else
306                  {                  {
307                            // Copy rest part of current data line to new data line
308                            memcpy(p_data_line,
309                                       p_editor_data->p_display_lines[display_line] + offset,
310                                       (size_t)(len_data_line - offset_data_line));
311    
312                            p_data_line[len_data_line - offset_data_line] = '\0';
313    
314                            // Append str to current display line
315                          memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);                          memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);
316    
317                          // Add line ending to current display line (data line)                          // Add line ending to current display line (data line)
318                          p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';                          p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';
319                          p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';                          p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';
320                          p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;                          p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;
321    
322                            *p_display_line = display_line;
323                            *p_offset = offset + str_len;
324                  }                  }
325    
326                    split_line_total = last_display_line - display_line + 3;
327    
328                    // Set start display_line for spliting new data line
329                  display_line++;                  display_line++;
                 offset = 0;  
330    
331                  *p_last_updated_line = p_editor_data->display_line_total;                  *p_last_updated_line = p_editor_data->display_line_total;
332            }
333            else // insert str into current data line at offset_data_line
334            {
335                    memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line));
336                    memcpy(p_data_line + offset_data_line, str, (size_t)str_len);
337                    p_data_line[len_data_line + str_len] = '\0';
338    
339                    // Set p_data_line to head of current display line
340                    p_data_line = p_editor_data->p_display_lines[display_line];
341                    split_line_total = last_display_line - display_line + 3;
342    
343                  last_display_line++;                  *p_display_line = display_line;
344                  (p_editor_data->display_line_total)++;                  *p_offset = offset + str_len;
                 (p_editor_data->data_line_total)++;  
345          }          }
346    
347          for (i = display_line; len_insert > 0 && i <= last_display_line; i++)          // Split current data line since beginning of current display line
348            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
349    
350            for (i = 0; i < split_line_total; i++)
351          {          {
352                  len = split_line(buf_insert, SCREEN_COLS, &eol, &display_len_insert);                  if (display_line + i > last_display_line)
                 if (len != len_insert)  
353                  {                  {
354                          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
355                                            i, len, len_insert, buf_insert);                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
356                          return -3;                          {
357                                    // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
358                                    // Terminate prior display line with \n, to avoid error on cleanup
359                                    if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
360                                    {
361                                            len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len);
362                                            p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
363                                            p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
364                                            p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
365                                    }
366                                    if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
367                                    {
368                                            *p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1;
369                                    }
370                                    break;
371                            }
372                            for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
373                            {
374                                    p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
375                                    p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
376                            }
377                            last_display_line++;
378                            (p_editor_data->display_line_total)++;
379                  }                  }
380    
381                  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];
382                  buf_catenate[p_editor_data->display_line_lengths[i]] = '\0';                  p_editor_data->p_display_lines[display_line + i] =
383                            (i == 0
384                                     ? p_data_line
385                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
386    
387                  len = split_line(buf_catenate, SCREEN_COLS - display_len_insert, &eol, &display_len);                  if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
388                  if (len < offset) // have no space to insert                          p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
389                  {                  {
390                          offset = 0;                          break;
                         continue; // retry at next display line  
391                  }                  }
392            }
393    
394            *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
395    
396                  // move \n to next display line if current line is full          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
397                  if (len > 0 && buf_catenate[len - 1] == '\n' && display_len + display_len_insert >= SCREEN_COLS)          {
398                    if (*p_display_line + 1 < p_editor_data->display_line_total)
399                  {                  {
400                          len--;                          *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
401                            (*p_display_line)++;
402                  }                  }
403            }
404    
405            return 0;
406    }
407    
408    int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,
409                                               long *p_last_updated_line)
410    {
411            char *p_data_line = NULL;
412            long len_data_line;
413            long offset_data_line;
414            long last_display_line; // of data line
415            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
416            long split_line_total;
417            long i, j;
418            int str_len = 0;
419    
420            if (p_editor_data == NULL || p_last_updated_line == NULL)
421            {
422                    log_error("editor_data_op() error: NULL pointer\n");
423                    return -1;
424            }
425    
426                  memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)offset);          // Get accurate offset of first character of CJK at offset position
427                  memcpy(buf_catenate + offset, buf_insert, (size_t)len_insert);          for (i = 0; i < offset; i++)
428                  memcpy(buf_catenate + offset + len_insert, p_editor_data->p_display_lines[i] + offset, (size_t)(len - offset));          {
429                  len_catenate = len_insert + len;                  if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
430                  buf_catenate[len_catenate] = '\0';                  {
431                            i++;
432                    }
433            }
434            if (i > offset) // offset was skipped
435            {
436                    offset--;
437            }
438    
439                  offset = 0;          // Get length of current data line
440                  len_insert = p_editor_data->display_line_lengths[i] - len;          len_data_line = 0;
441                  if (len_insert > 0)          p_data_line = p_editor_data->p_display_lines[display_line];
442            for (i = display_line - 1; i >= 0; i--)
443            {
444                    if (p_editor_data->display_line_lengths[i] > 0 &&
445                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
446                  {                  {
447                          memcpy(buf_insert, p_editor_data->p_display_lines[i] + len, (size_t)len_insert);                          break;
                         buf_insert[len_insert] = '\0';  
448                  }                  }
449    
450                  memcpy(p_editor_data->p_display_lines[i], buf_catenate, (size_t)len_catenate);                  len_data_line += p_editor_data->display_line_lengths[i];
451                  p_editor_data->display_line_lengths[i] = len_catenate;                  p_data_line = p_editor_data->p_display_lines[i];
452            }
453            offset_data_line = len_data_line + offset;
454            last_display_line = p_editor_data->display_line_total - 1;
455            for (i = display_line; i < p_editor_data->display_line_total; i++)
456            {
457                    len_data_line += p_editor_data->display_line_lengths[i];
458    
459                    if (p_editor_data->display_line_lengths[i] > 0 &&
460                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
461                    {
462                            last_display_line = i;
463                            break;
464                    }
465          }          }
466    
467          *p_last_updated_line = MAX(i, *p_last_updated_line);          if (offset_data_line >= len_data_line) // end-of-line
468            {
469                    return 0;
470            }
471    
472          if (len_insert > 0)          // Check str to be deleted
473            if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
474          {          {
475                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  str_len = 1;
476            }
477            else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK
478            {
479                    str_len = 2;
480            }
481            else
482            {
483                    log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
484                                      display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
485                    str_len = 1;
486            }
487    
488            // Current display line is (almost) empty
489            if (offset_data_line + str_len > len_data_line ||
490                    (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
491            {
492                    if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
493                  {                  {
494                          log_error("Append line error, display_line_total(%ld) reach limit(%d)\n",                          return 0;
                                           p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);  
                         return -2;  
495                  }                  }
496    
497                  // Prepare one blank display line after last_display_line                  len_data_line = 0; // Next data line
498                  for (i = p_editor_data->display_line_total; i > last_display_line + 1; i--)                  last_display_line = p_editor_data->display_line_total - 1;
499                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
500                  {                  {
501                          p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1];                          len_data_line += p_editor_data->display_line_lengths[i];
502                          p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1];  
503                            if (p_editor_data->display_line_lengths[i] > 0 &&
504                                    p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
505                            {
506                                    last_display_line = i;
507                                    break;
508                            }
509                  }                  }
                 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;  
510    
511                  (p_editor_data->display_line_total)++;                  if (offset_data_line + len_data_line + 1 > MAX_EDITOR_DATA_LINE_LENGTH) // No enough buffer to merge current data line with next data line
512                  last_display_line++;                  {
513                            return 0;
514                    }
515    
516                  // Fill data into blank display line                  // Append next data line to current one
517                  memcpy(p_editor_data->p_display_lines[last_display_line], buf_insert, (size_t)len_insert);                  memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
518                  p_editor_data->p_display_lines[last_display_line][len_insert] = '\0';                  p_data_line[offset_data_line + len_data_line] = '\0';
                 p_editor_data->display_line_lengths[last_display_line] = len_insert;  
519    
520                  *p_last_updated_line = MAX(last_display_line, *p_last_updated_line);                  // Recycle next data line
521                    memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]);
522            }
523            else
524            {
525                    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));
526                    p_data_line[len_data_line - str_len] = '\0';
527                    len_data_line -= str_len;
528          }          }
529    
530          return 0;          // Set p_data_line to head of current display line
531  }          p_data_line = p_editor_data->p_display_lines[display_line];
532            split_line_total = last_display_line - display_line + 2;
533    
534  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,          // Split current data line since beginning of current display line
535                                             long *p_last_updated_line)          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
536  {  
537          return 0;          for (i = 0; i < split_line_total; i++)
538            {
539                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
540                    p_editor_data->p_display_lines[display_line + i] =
541                            (i == 0
542                                     ? p_data_line
543                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
544    
545                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
546                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
547                    {
548                            break;
549                    }
550            }
551    
552            *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
553    
554            if (*p_last_updated_line < last_display_line)
555            {
556                    // Remove redundant display line after last_display_line
557                    for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
558                    {
559                            p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j];
560                            p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];
561                    }
562    
563                    j = p_editor_data->display_line_total;
564                    (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
565                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
566            }
567    
568            return str_len;
569  }  }
570    
571  static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)  static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)
# Line 376  static int editor_display_key_handler(in Line 574  static int editor_display_key_handler(in
574          {          {
575          case 0: // Set msg          case 0: // Set msg
576                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
577                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");
578                  break;                  break;
579          }          }
580    
# Line 389  int editor_display(EDITOR_DATA *p_editor Line 587  int editor_display(EDITOR_DATA *p_editor
587          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
588          EDITOR_CTX ctx;          EDITOR_CTX ctx;
589          int ch = 0;          int ch = 0;
590          char hz_ch[4];          char input_str[4];
591          int hz_len;          int str_len = 0;
592          int input_ok, screen_current_line;          int input_ok;
593          const int screen_begin_line = 1;          const int screen_begin_row = 1;
594          int screen_end_line = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
595          const int screen_line_total = screen_end_line - screen_begin_line + 1;          int output_current_row = screen_begin_row;
596            int output_end_row = SCREEN_ROWS - 1;
597          long int line_current = 0;          long int line_current = 0;
598          long int len;          long int len;
599          int loop;          int loop;
600          int eol, display_len;          int eol, display_len;
601          long row_pos = 1, col_pos = 1;          long row_pos = 1, col_pos = 1;
602            long display_line_in, offset_in;
603            long display_line_out, offset_out;
604            int scroll_rows;
605          long last_updated_line = 0;          long last_updated_line = 0;
606          int insert = 1;          int key_insert = 1;
607            int i, j;
608            char *p_str;
609    
610          screen_current_line = screen_begin_line;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_line, SCREEN_ROWS);  
611    
612          // update msg_ext with extended key handler          // update msg_ext with extended key handler
613          if (editor_display_key_handler(&ch, &ctx) != 0)          if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 415  int editor_display(EDITOR_DATA *p_editor Line 618  int editor_display(EDITOR_DATA *p_editor
618          loop = 1;          loop = 1;
619          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
620          {          {
621                  if (line_current >= p_editor_data->display_line_total || screen_current_line > screen_end_line)                  if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row)
622                  {                  {
623                          ctx.line_cursor = line_current - screen_current_line + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
624    
625                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
626                                           "\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] "
627                                           "µÚ\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] "
628                                           "%s",                                           "%s",
629                                           row_pos, col_pos,                                           row_pos, col_pos,
630                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
631                                             key_insert ? "²åÈë" : "¸Äд",
632                                           ctx.msg);                                           ctx.msg);
633    
634                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);
# Line 453  int editor_display(EDITOR_DATA *p_editor Line 657  int editor_display(EDITOR_DATA *p_editor
657                                          goto cleanup;                                          goto cleanup;
658                                  }                                  }
659    
660                                  if (ch >= 32 && ch < 127) // printable character                                  if (ch > 127 && ch <= 255) // GBK
661                                  {                                  {
662                                          last_updated_line = line_current;                                          input_str[str_len] = (char)(ch - 256);
663                                            str_len++;
664                                    }
665                                    else
666                                    {
667                                            str_len = 0;
668                                    }
669    
670                                          if (editor_data_insert(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1,                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK
671                                                                                     (const char *)&ch, 1, &last_updated_line) < 0)                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
672                                    {
673                                            if (str_len == 0)
674                                          {                                          {
675                                                  log_error("editor_data_op(INSERT ch) error\n");                                                  input_str[0] = (char)ch;
676                                                    str_len = 1;
677                                          }                                          }
                                         else  
                                         {  
                                                 screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current));  
                                                 line_current -= (screen_current_line - row_pos);  
                                                 screen_current_line = (int)row_pos;  
678    
679                                                  col_pos++;                                          last_updated_line = line_current;
680                                                  if (col_pos <= p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])                                          display_line_in = line_current - output_current_row + row_pos;
681                                            offset_in = col_pos - 1;
682                                            display_line_out = display_line_in;
683                                            offset_out = offset_in;
684    
685                                            if (!key_insert) // overwrite
686                                            {
687                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
688                                                                                               &last_updated_line) < 0)
689                                                  {                                                  {
690                                                          continue;                                                          log_error("editor_data_delete() error\n");
691                                                  }                                                  }
                                                 col_pos = 1;  
                                                 ch = KEY_DOWN;  
692                                          }                                          }
                                 }  
                                 else if (ch > 127 && ch <= 255) // CJK character  
                                 {  
                                         hz_ch[hz_len] = (char)(ch - 256);  
                                         hz_len++;  
693    
694                                          if (hz_len == 2) // GBK                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
695                                                                                       input_str, str_len, &last_updated_line) < 0)
696                                            {
697                                                    log_error("editor_data_insert(str_len=%d) error\n", str_len);
698                                                    str_len = 0;
699                                            }
700                                            else
701                                          {                                          {
702                                                  hz_len = 0;                                                  str_len = 0;
                                                 last_updated_line = line_current;  
703    
704                                                  if (editor_data_insert(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1,                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
705                                                                                             hz_ch, 2, &last_updated_line) < 0)                                                  line_current -= (output_current_row - row_pos);
706                                                  {                                                  output_current_row = (int)row_pos;
707                                                          log_error("editor_data_op(INSERT hz) error\n");  
708                                                  }                                                  scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row));
709                                                  else  
710                                                    if (scroll_rows > 0)
711                                                  {                                                  {
712                                                          screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current));                                                          moveto(SCREEN_ROWS, 0);
713                                                          line_current -= (screen_current_line - row_pos);                                                          clrtoeol();
714                                                          screen_current_line = (int)row_pos;                                                          for (i = 0; i < scroll_rows; i++)
715                                                            {
716                                                                    prints("\033[S"); // Scroll up 1 line
717                                                            }
718    
719                                                          col_pos += 2;                                                          output_current_row -= scroll_rows;
720                                                          if (col_pos <= p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])                                                          if (output_current_row < screen_begin_row)
721                                                          {                                                          {
722                                                                  continue;                                                                  line_current += (screen_begin_row - output_current_row);
723                                                                    output_current_row = screen_begin_row;
724                                                          }                                                          }
725                                                          col_pos = 1;                                                          row_pos = output_end_row;
726                                                          ch = KEY_DOWN;                                                  }
727                                                    else // if (scroll_lines == 0)
728                                                    {
729                                                            row_pos += (display_line_out - display_line_in);
730                                                  }                                                  }
731                                                    col_pos = offset_out + 1;
732                                          }                                          }
733    
734                                            continue;
735                                  }                                  }
736                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
737                                  {                                  {
738                                          last_updated_line = line_current;                                          if (ch == BACKSPACE)
739                                            {
740                                                    if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
741                                                    {
742                                                            input_ok = 0;
743                                                            continue;
744                                                    }
745    
746                                                    col_pos--;
747                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
748                                                    {
749                                                            row_pos--;
750                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
751                                                    }
752                                            }
753    
754                                          if (editor_data_delete(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1,                                          if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1,
755                                                                                     &last_updated_line) < 0)                                                                                                            &last_updated_line)) < 0)
756                                          {                                          {
757                                                  log_error("editor_data_op(DELETE) error\n");                                                  log_error("editor_data_delete() error\n");
758                                          }                                          }
759                                          else                                          else
760                                          {                                          {
761                                                  screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current));                                                  if (ch == BACKSPACE)
762                                                    {
763                                                            for (i = 1; i < str_len; i++)
764                                                            {
765                                                                    col_pos--;
766                                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
767                                                                    {
768                                                                            row_pos--;
769                                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
770                                                                    }
771                                                            }
772                                                    }
773    
774                                                    output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
775                                                    line_current -= (output_current_row - row_pos);
776                                                    output_current_row = (int)row_pos;
777    
778                                                    if (output_current_row < screen_begin_row) // row_pos <= 0
779                                                    {
780                                                            output_current_row = screen_begin_row;
781                                                            row_pos = screen_begin_row;
782                                                            output_end_row = SCREEN_ROWS - 1;
783                                                    }
784    
785                                                    // Exceed end
786                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
787                                                            p_editor_data->display_line_total > screen_row_total)
788                                                    {
789                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
790                                                                                                    (p_editor_data->display_line_total - screen_row_total));
791    
792                                                            line_current = p_editor_data->display_line_total - screen_row_total;
793                                                            row_pos += scroll_rows;
794                                                            output_current_row = screen_begin_row;
795                                                            output_end_row = SCREEN_ROWS - 1;
796                                                    }
797    
798                                                    clrline(output_current_row, output_end_row);
799                                          }                                          }
800    
801                                          continue;                                          continue;
802                                  }                                  }
803    
                                 hz_len = 0;  
   
804                                  switch (ch)                                  switch (ch)
805                                  {                                  {
806                                  case KEY_NULL:                                  case KEY_NULL:
807                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
808                                          goto cleanup;                                          goto cleanup;
809                                  case Ctrl('C'):                                  case Ctrl('W'):
810                                          loop = 0;                                          loop = 0;
811                                          break;                                          break;
812                                    case Ctrl('S'): // Start of line
813                                  case KEY_CTRL_LEFT:                                  case KEY_CTRL_LEFT:
814                                          col_pos = 1;                                          col_pos = 1;
815                                          break;                                          break;
816                                    case Ctrl('E'): // End of line
817                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
818                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
819                                          break;                                          break;
820                                    case Ctrl('T'): // Top of screen
821                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
822                                          row_pos = screen_begin_line;                                          row_pos = screen_begin_row;
823                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
824                                          break;                                          break;
825                                    case Ctrl('B'): // Bottom of screen
826                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
827                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
828                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          {
829                                                    row_pos = p_editor_data->display_line_total;
830                                            }
831                                            else
832                                            {
833                                                    row_pos = SCREEN_ROWS - 1;
834                                            }
835                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
836                                            {
837                                                    // last display line does NOT have \n in the end
838                                                    col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);
839                                            }
840                                            else
841                                            {
842                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
843                                            }
844                                          break;                                          break;
845                                  case KEY_INS:                                  case KEY_INS:
846                                          insert = !insert;                                          key_insert = !key_insert;
847                                          break;                                          break;
848                                  case KEY_HOME:                                  case KEY_HOME:
849                                          row_pos = 1;                                          row_pos = 1;
850                                          col_pos = 1;                                          col_pos = 1;
851                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
852                                          {                                          {
853                                                  break;                                                  break;
854                                          }                                          }
855                                          line_current = 0;                                          line_current = 0;
856                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
857                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
858                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
859                                          break;                                          break;
860                                  case KEY_END:                                  case KEY_END:
861                                          if (p_editor_data->display_line_total < screen_line_total)                                          if (p_editor_data->display_line_total < screen_row_total)
862                                          {                                          {
863                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
864                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
865                                                  break;                                                  break;
866                                          }                                          }
867                                          line_current = p_editor_data->display_line_total - screen_line_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
868                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
869                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
870                                          row_pos = screen_line_total;                                          row_pos = SCREEN_ROWS - 1;
871                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                          col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
872                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
873                                          break;                                          break;
874                                  case KEY_LEFT:                                  case KEY_LEFT:
875                                          if (col_pos > 1)                                          if (col_pos > 1)
# Line 586  int editor_display(EDITOR_DATA *p_editor Line 879  int editor_display(EDITOR_DATA *p_editor
879                                          }                                          }
880                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
881                                  case KEY_UP:                                  case KEY_UP:
882                                          if (row_pos > screen_begin_line)                                          if (row_pos > screen_begin_row)
883                                          {                                          {
884                                                  row_pos--;                                                  row_pos--;
885                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
886                                                  break;                                                  break;
887                                          }                                          }
888                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
889                                          {                                          {
890                                                  col_pos = 1;                                                  col_pos = 1;
891                                                  break;                                                  break;
892                                          }                                          }
893                                          line_current -= screen_current_line;                                          line_current -= output_current_row;
894                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
895                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
896                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
897                                          screen_end_line = SCREEN_ROWS - 1; // Legacy Fterm only works with this line                                          output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
898                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
                                         break;  
                                 case CR:  
899                                          break;                                          break;
900                                  case KEY_SPACE:                                  case KEY_SPACE:
901                                          break;                                          break;
902                                  case KEY_RIGHT:                                  case KEY_RIGHT:
903                                          if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
904                                          {                                          {
905                                                  col_pos++;                                                  col_pos++;
906                                                  break;                                                  break;
907                                          }                                          }
908                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
909                                  case KEY_DOWN:                                  case KEY_DOWN:
910                                          if (row_pos < MIN(screen_line_total, p_editor_data->display_line_total))                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))
911                                          {                                          {
912                                                  row_pos++;                                                  row_pos++;
913                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
914                                                  break;                                                  break;
915                                          }                                          }
916                                          if (line_current + (screen_line_total - (screen_current_line - screen_begin_line)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
917                                          {                                          {
918                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  // last display line does NOT have \n in the end
919                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
920                                                  break;                                                  break;
921                                          }                                          }
922                                          screen_current_line--;                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
923                                          screen_end_line = SCREEN_ROWS - 1;                                          output_current_row = screen_row_total;
924                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          output_end_row = SCREEN_ROWS - 1;
925                                            col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
926                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
927                                          clrtoeol();                                          clrtoeol();
928                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
929                                          break;                                          break;
930                                  case KEY_PGUP:                                  case KEY_PGUP:
931                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
932                                          {                                          {
933                                                  break;                                                  break;
934                                          }                                          }
935                                          line_current -= ((screen_line_total - 1) + (screen_current_line - screen_begin_line));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
936                                          if (line_current < 0)                                          if (line_current < 0)
937                                          {                                          {
938                                                  line_current = 0;                                                  line_current = 0;
939                                          }                                          }
940                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
941                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
942                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
943                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
944                                          break;                                          break;
945                                  case KEY_PGDN:                                  case KEY_PGDN:
946                                          if (line_current + screen_line_total - (screen_current_line - screen_begin_line) >= p_editor_data->display_line_total) // Reach end                                          if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= p_editor_data->display_line_total) // Reach end
947                                          {                                          {
948                                                  break;                                                  break;
949                                          }                                          }
950                                          line_current += (screen_line_total - 1) - (screen_current_line - screen_begin_line);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
951                                          if (line_current + screen_line_total > p_editor_data->display_line_total) // No enough lines to display                                          if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display
952                                          {                                          {
953                                                  line_current = p_editor_data->display_line_total - screen_line_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
954                                          }                                          }
955                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
956                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
957                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
958                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
959                                          break;                                          break;
960                                  case KEY_F1:                                  case KEY_F1:
961                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not reentrant
# Line 677  int editor_display(EDITOR_DATA *p_editor Line 968  int editor_display(EDITOR_DATA *p_editor
968                                          show_help = 1;                                          show_help = 1;
969                                  case KEY_F5:                                  case KEY_F5:
970                                          // Refresh after display help information                                          // Refresh after display help information
971                                          line_current -= (screen_current_line - screen_begin_line);                                          line_current -= (output_current_row - screen_begin_row);
972                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
973                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
974                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
975                                          break;                                          break;
976                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
977                                          break;                                          break;
# Line 690  int editor_display(EDITOR_DATA *p_editor Line 981  int editor_display(EDITOR_DATA *p_editor
981                                  }                                  }
982    
983                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(0);
984    
985                                    if (input_ok)
986                                    {
987                                            break;
988                                    }
989                          }                          }
990    
991                          continue;                          continue;
# Line 707  int editor_display(EDITOR_DATA *p_editor Line 1003  int editor_display(EDITOR_DATA *p_editor
1003                          len = 0;                          len = 0;
1004                  }                  }
1005    
1006                  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);
1007                  buffer[len] = '\0';                  // Replace '\033' with '*'
1008                    p_str = p_editor_data->p_display_lines[line_current];
1009                    for (i = 0, j = 0; i < len; i++)
1010                    {
1011                            if (p_str[i] == '\033')
1012                            {
1013                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1014                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1015                            }
1016                            else
1017                            {
1018                                    buffer[j] = p_str[i];
1019                                    j++;
1020                            }
1021                    }
1022                    buffer[j] = '\0';
1023    
1024                  moveto(screen_current_line, 0);                  moveto(output_current_row, 0);
1025                  clrtoeol();                  clrtoeol();
1026                  prints("%s", buffer);                  prints("%s", buffer);
1027                  line_current++;                  line_current++;
1028                  screen_current_line++;                  output_current_row++;
1029          }          }
1030    
1031  cleanup:  cleanup:


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

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