/[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.6 by sysadm, Wed Jun 11 05:40:09 2025 UTC Revision 1.17 by sysadm, Thu Jun 12 12:53:49 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>  #include <strings.h>
# Line 27  Line 28 
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;          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 41  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 59  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_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                          p_data_line = memory_pool_alloc(p_mp_data_line);
111                          if (p_data_line == 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                          }                          }
                         p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                         (p_editor_data->data_line_total)++;  
118    
119                          p_editor_data->p_display_lines[i] = p_data_line;                          p_editor_data->p_display_lines[i] = p_data_line;
120                          current_data_line_length = 0;                          current_data_line_length = 0;
# Line 94  EDITOR_DATA *editor_data_load(const char Line 129  EDITOR_DATA *editor_data_load(const char
129                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
130          }          }
131    
         bzero(p_editor_data->p_data_lines + p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->data_line_total);  
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);          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;
# Line 131  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 138  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                  free(p_editor_data->p_data_lines[i]);                  if (p_data_line == NULL)
179                  p_editor_data->p_data_lines[i] = NULL;                  {
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                    memory_pool_free(p_mp_data_line, p_data_line);
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 *p_display_line, long *p_offset,  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
# Line 159  int editor_data_insert(EDITOR_DATA *p_ed Line 208  int editor_data_insert(EDITOR_DATA *p_ed
208          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
209          long split_line_total;          long split_line_total;
210          long i, j;          long i, j;
211            int len;
212            int eol;
213            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 169  int editor_data_insert(EDITOR_DATA *p_ed Line 221  int editor_data_insert(EDITOR_DATA *p_ed
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 262  int editor_data_insert(EDITOR_DATA *p_ed
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 || str[0] == CR)          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_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                  p_data_line = memory_pool_alloc(p_mp_data_line);
274                  if (p_data_line == 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                  }                  }
                 p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                 (p_editor_data->data_line_total)++;  
279    
280                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
281                  {                  {
# Line 304  int editor_data_insert(EDITOR_DATA *p_ed Line 354  int editor_data_insert(EDITOR_DATA *p_ed
354                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
355                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
356                          {                          {
357                                  log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                                  // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
358                                  return -3;                                  // 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--)                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
373                          {                          {
# Line 329  int editor_data_insert(EDITOR_DATA *p_ed Line 391  int editor_data_insert(EDITOR_DATA *p_ed
391                  }                  }
392          }          }
393    
394          *p_last_updated_line = MAX(display_line + split_line_total - 1, *p_last_updated_line);          *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
395    
396          if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] ||          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
                 (*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] &&  
                  p_editor_data->p_display_lines[*p_display_line][*p_offset - 1] == '\n'))  
397          {          {
398                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
                 (*p_display_line)++;  
399    
400                  if (*p_display_line >= p_editor_data->display_line_total)                  if (*p_display_line + 1 >= p_editor_data->display_line_total)
401                  {                  {
402                          log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);                          log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);
403                  }                  }
404                    else
405                    {
406                            (*p_display_line)++;
407                    }
408          }          }
409    
410          return 0;          return 0;
# Line 350  int editor_data_insert(EDITOR_DATA *p_ed Line 413  int editor_data_insert(EDITOR_DATA *p_ed
413  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,
414                                             long *p_last_updated_line)                                             long *p_last_updated_line)
415  {  {
416          return 0;          char *p_data_line = NULL;
417            long len_data_line;
418            long offset_data_line;
419            long last_display_line; // of data line
420            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
421            long split_line_total;
422            long i, j;
423            int str_len = 0;
424    
425            if (p_editor_data == NULL || p_last_updated_line == NULL)
426            {
427                    log_error("editor_data_op() error: NULL pointer\n");
428                    return -1;
429            }
430    
431            // Get accurate offset of first character of CJK at offset position
432            for (i = 0; i < offset; i++)
433            {
434                    if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
435                    {
436                            i++;
437                    }
438            }
439            if (i > offset) // offset was skipped
440            {
441                    offset--;
442            }
443    
444            // Get length of current data line
445            len_data_line = 0;
446            p_data_line = p_editor_data->p_display_lines[display_line];
447            for (i = display_line - 1; i >= 0; i--)
448            {
449                    if (p_editor_data->display_line_lengths[i] > 0 &&
450                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
451                    {
452                            break;
453                    }
454    
455                    len_data_line += p_editor_data->display_line_lengths[i];
456                    p_data_line = p_editor_data->p_display_lines[i];
457            }
458            offset_data_line = len_data_line + offset;
459            last_display_line = p_editor_data->display_line_total - 1;
460            for (i = display_line; i < p_editor_data->display_line_total; i++)
461            {
462                    len_data_line += p_editor_data->display_line_lengths[i];
463    
464                    if (p_editor_data->display_line_lengths[i] > 0 &&
465                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
466                    {
467                            last_display_line = i;
468                            break;
469                    }
470            }
471    
472            // Check str to be deleted
473            if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
474            {
475                    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 %d %d\n",
484                                      display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1],
485                                      p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]);
486                    str_len = 1;
487            }
488    
489            // Current display line is (almost) empty
490            if (offset_data_line + str_len > len_data_line ||
491                    (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
492            {
493                    if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
494                    {
495                            return 0;
496                    }
497    
498                    len_data_line = 0; // Next data line
499                    last_display_line = p_editor_data->display_line_total - 1;
500                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
501                    {
502                            len_data_line += p_editor_data->display_line_lengths[i];
503    
504                            if (p_editor_data->display_line_lengths[i] > 0 &&
505                                    p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
506                            {
507                                    last_display_line = i;
508                                    break;
509                            }
510                    }
511    
512                    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
513                    {
514                            return 0;
515                    }
516    
517                    // Append next data line to current one
518                    memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
519                    p_data_line[offset_data_line + len_data_line] = '\0';
520    
521                    // Recycle next data line
522                    memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]);
523            }
524            else
525            {
526                    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));
527                    p_data_line[len_data_line - str_len] = '\0';
528                    len_data_line -= str_len;
529            }
530    
531            // Set p_data_line to head of current display line
532            p_data_line = p_editor_data->p_display_lines[display_line];
533            split_line_total = last_display_line - display_line + 2;
534    
535            // Split current data line since beginning of current display line
536            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
537    
538            for (i = 0; i < split_line_total; i++)
539            {
540                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
541                    p_editor_data->p_display_lines[display_line + i] =
542                            (i == 0
543                                     ? p_data_line
544                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
545    
546                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
547                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
548                    {
549                            break;
550                    }
551            }
552    
553            *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
554    
555            if (display_line + i < last_display_line)
556            {
557                    // Remove redundant display line after last_display_line
558                    for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
559                    {
560                            p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j];
561                            p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j];
562                    }
563    
564                    (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));
565                    last_display_line = display_line + i;
566    
567                    *p_last_updated_line = p_editor_data->display_line_total - 1;
568            }
569    
570            return str_len;
571  }  }
572    
573  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 359  static int editor_display_key_handler(in Line 576  static int editor_display_key_handler(in
576          {          {
577          case 0: // Set msg          case 0: // Set msg
578                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
579                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");
580                  break;                  break;
581          }          }
582    
# Line 375  int editor_display(EDITOR_DATA *p_editor Line 592  int editor_display(EDITOR_DATA *p_editor
592          char input_str[4];          char input_str[4];
593          int str_len = 0;          int str_len = 0;
594          int input_ok;          int input_ok;
         int screen_current_row;  
595          const int screen_begin_row = 1;          const int screen_begin_row = 1;
596          int screen_end_row = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
597          const int screen_row_total = screen_end_row - screen_begin_row + 1;          int output_current_row = screen_begin_row;
598            int output_end_row = SCREEN_ROWS - 1;
599          long int line_current = 0;          long int line_current = 0;
600          long int len;          long int len;
601          int loop;          int loop;
# Line 388  int editor_display(EDITOR_DATA *p_editor Line 605  int editor_display(EDITOR_DATA *p_editor
605          long display_line_out, offset_out;          long display_line_out, offset_out;
606          int scroll_rows;          int scroll_rows;
607          long last_updated_line = 0;          long last_updated_line = 0;
608          int insert = 1;          int key_insert = 1;
609          int i;          int i, j;
610            char *p_str;
611    
612          screen_current_row = screen_begin_row;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_row, SCREEN_ROWS);  
613    
614          // update msg_ext with extended key handler          // update msg_ext with extended key handler
615          if (editor_display_key_handler(&ch, &ctx) != 0)          if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 403  int editor_display(EDITOR_DATA *p_editor Line 620  int editor_display(EDITOR_DATA *p_editor
620          loop = 1;          loop = 1;
621          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
622          {          {
623                  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)
624                  {                  {
625                          ctx.line_cursor = line_current - screen_current_row + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
626    
627                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
628                                           "\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] "
629                                           "µÚ\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] "
630                                           "%s",                                           "%s",
631                                           row_pos, col_pos,                                           row_pos, col_pos,
632                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
633                                             key_insert ? "²åÈë" : "¸Äд",
634                                           ctx.msg);                                           ctx.msg);
635    
636                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);
# Line 451  int editor_display(EDITOR_DATA *p_editor Line 669  int editor_display(EDITOR_DATA *p_editor
669                                          str_len = 0;                                          str_len = 0;
670                                  }                                  }
671    
672                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || ch == CR) // printable character or GBK                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK
673                                            ch == CR || ch == KEY_ESC)                                                                                       // Special character
674                                  {                                  {
675                                          if (str_len == 0)                                          if (str_len == 0)
676                                          {                                          {
# Line 460  int editor_display(EDITOR_DATA *p_editor Line 679  int editor_display(EDITOR_DATA *p_editor
679                                          }                                          }
680    
681                                          last_updated_line = line_current;                                          last_updated_line = line_current;
682                                          display_line_in = line_current - screen_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
683                                          offset_in = col_pos - 1;                                          offset_in = col_pos - 1;
684                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
685                                          offset_out = offset_in;                                          offset_out = offset_in;
686    
687                                            if (!key_insert) // overwrite
688                                            {
689                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
690                                                                                               &last_updated_line) < 0)
691                                                    {
692                                                            log_error("editor_data_delete() error\n");
693                                                    }
694                                            }
695    
696                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
697                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
698                                          {                                          {
699                                                  log_error("editor_data_insert(%s) error\n", input_str);                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);
700                                                  str_len = 0;                                                  str_len = 0;
701                                          }                                          }
702                                          else                                          else
703                                          {                                          {
704                                                  str_len = 0;                                                  str_len = 0;
705    
706                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
707                                                  line_current -= (screen_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
708                                                  screen_current_row = (int)row_pos;                                                  output_current_row = (int)row_pos;
709    
710                                                  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));
711    
712                                                  if (scroll_rows > 0)                                                  if (scroll_rows > 0)
713                                                  {                                                  {
# Line 490  int editor_display(EDITOR_DATA *p_editor Line 718  int editor_display(EDITOR_DATA *p_editor
718                                                                  prints("\033[S"); // Scroll up 1 line                                                                  prints("\033[S"); // Scroll up 1 line
719                                                          }                                                          }
720    
721                                                          screen_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
722                                                          if (screen_current_row < screen_begin_row)                                                          if (output_current_row < screen_begin_row)
723                                                          {                                                          {
724                                                                  line_current += (screen_begin_row - screen_current_row);                                                                  line_current += (screen_begin_row - output_current_row);
725                                                                  screen_current_row = screen_begin_row;                                                                  output_current_row = screen_begin_row;
726                                                          }                                                          }
727                                                          row_pos = screen_end_row;                                                          row_pos = output_end_row;
728                                                  }                                                  }
729                                                  else // if (scroll_lines == 0)                                                  else // if (scroll_lines == 0)
730                                                  {                                                  {
731                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
732                                                  }                                                  }
733                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1;
   
                                                 continue;  
734                                          }                                          }
735    
736                                            continue;
737                                  }                                  }
738                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
739                                  {                                  {
740                                          last_updated_line = line_current;                                          if (ch == BACKSPACE)
741                                            {
742                                                    col_pos--;
743                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
744                                                    {
745                                                            row_pos--;
746                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
747                                                    }
748                                            }
749    
750                                          if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,                                          if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1,
751                                                                                     &last_updated_line) < 0)                                                                                                            &last_updated_line)) < 0)
752                                          {                                          {
753                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
754                                          }                                          }
755                                          else                                          else
756                                          {                                          {
757                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  if (ch == BACKSPACE)
758                                                    {
759                                                            for (i = 1; i < str_len; i++)
760                                                            {
761                                                                    col_pos--;
762                                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
763                                                                    {
764                                                                            row_pos--;
765                                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
766                                                                    }
767                                                            }
768                                                    }
769    
770                                                    output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
771                                                    line_current -= (output_current_row - row_pos);
772                                                    output_current_row = (int)row_pos;
773    
774                                                    if (output_current_row < screen_begin_row) // row_pos <= 0
775                                                    {
776                                                            output_current_row = screen_begin_row;
777                                                            row_pos = screen_begin_row;
778                                                            output_end_row = SCREEN_ROWS - 1;
779                                                    }
780    
781                                                    // Exceed end
782                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
783                                                            p_editor_data->display_line_total > screen_row_total)
784                                                    {
785                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
786                                                                                                    (p_editor_data->display_line_total - screen_row_total));
787    
788                                                            line_current = p_editor_data->display_line_total - screen_row_total;
789                                                            row_pos += scroll_rows;
790                                                            output_current_row = screen_begin_row;
791                                                            output_end_row = SCREEN_ROWS - 1;
792                                                            clrline(output_current_row, SCREEN_ROWS);
793                                                    }
794                                          }                                          }
795    
796                                          continue;                                          continue;
# Line 529  int editor_display(EDITOR_DATA *p_editor Line 801  int editor_display(EDITOR_DATA *p_editor
801                                  case KEY_NULL:                                  case KEY_NULL:
802                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
803                                          goto cleanup;                                          goto cleanup;
804                                  case Ctrl('C'):                                  case Ctrl('W'):
805                                          loop = 0;                                          loop = 0;
806                                          break;                                          break;
807                                    case Ctrl('S'): // Start of line
808                                  case KEY_CTRL_LEFT:                                  case KEY_CTRL_LEFT:
809                                          col_pos = 1;                                          col_pos = 1;
810                                          break;                                          break;
811                                    case Ctrl('E'): // End of line
812                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
813                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
814                                          break;                                          break;
815                                    case Ctrl('T'): // Top of screen
816                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
817                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
818                                          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_lengths[line_current - output_current_row + row_pos]));
819                                          break;                                          break;
820                                    case Ctrl('B'): // Bottom of screen
821                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
822                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
823                                          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_lengths[line_current - output_current_row + row_pos]));
824                                          break;                                          break;
825                                  case KEY_INS:                                  case KEY_INS:
826                                          insert = !insert;                                          key_insert = !key_insert;
827                                          break;                                          break;
828                                  case KEY_HOME:                                  case KEY_HOME:
829                                          row_pos = 1;                                          row_pos = 1;
830                                          col_pos = 1;                                          col_pos = 1;
831                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
832                                          {                                          {
833                                                  break;                                                  break;
834                                          }                                          }
835                                          line_current = 0;                                          line_current = 0;
836                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
837                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
838                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
839                                          break;                                          break;
840                                  case KEY_END:                                  case KEY_END:
841                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
842                                          {                                          {
843                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
844                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
845                                                  break;                                                  break;
846                                          }                                          }
847                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
848                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
849                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
850                                          row_pos = screen_row_total;                                          row_pos = SCREEN_ROWS - 1;
851                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
852                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
853                                          break;                                          break;
854                                  case KEY_LEFT:                                  case KEY_LEFT:
855                                          if (col_pos > 1)                                          if (col_pos > 1)
# Line 586  int editor_display(EDITOR_DATA *p_editor Line 862  int editor_display(EDITOR_DATA *p_editor
862                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
863                                          {                                          {
864                                                  row_pos--;                                                  row_pos--;
865                                                  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_lengths[line_current - output_current_row + row_pos]));
866                                                  break;                                                  break;
867                                          }                                          }
868                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
869                                          {                                          {
870                                                  col_pos = 1;                                                  col_pos = 1;
871                                                  break;                                                  break;
872                                          }                                          }
873                                          line_current -= screen_current_row;                                          line_current -= output_current_row;
874                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
875                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
876                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
877                                          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
878                                          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_lengths[line_current - output_current_row + row_pos]));
879                                          break;                                          break;
880                                  case KEY_SPACE:                                  case KEY_SPACE:
881                                          break;                                          break;
882                                  case KEY_RIGHT:                                  case KEY_RIGHT:
883                                          if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
884                                          {                                          {
885                                                  col_pos++;                                                  col_pos++;
886                                                  break;                                                  break;
# Line 614  int editor_display(EDITOR_DATA *p_editor Line 890  int editor_display(EDITOR_DATA *p_editor
890                                          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))
891                                          {                                          {
892                                                  row_pos++;                                                  row_pos++;
893                                                  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_lengths[line_current - output_current_row + row_pos]));
894                                                  break;                                                  break;
895                                          }                                          }
896                                          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
897                                          {                                          {
898                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
899                                                  break;                                                  break;
900                                          }                                          }
901                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
902                                          screen_current_row = screen_row_total;                                          output_current_row = screen_row_total;
903                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
904                                          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_lengths[line_current - output_current_row + row_pos]));
905                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
906                                          clrtoeol();                                          clrtoeol();
907                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
908                                          break;                                          break;
909                                  case KEY_PGUP:                                  case KEY_PGUP:
910                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
911                                          {                                          {
912                                                  break;                                                  break;
913                                          }                                          }
914                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
915                                          if (line_current < 0)                                          if (line_current < 0)
916                                          {                                          {
917                                                  line_current = 0;                                                  line_current = 0;
918                                          }                                          }
919                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
920                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
921                                          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_lengths[line_current - output_current_row + row_pos]));
922                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
923                                          break;                                          break;
924                                  case KEY_PGDN:                                  case KEY_PGDN:
925                                          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
926                                          {                                          {
927                                                  break;                                                  break;
928                                          }                                          }
929                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
930                                          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
931                                          {                                          {
932                                                  line_current = p_editor_data->display_line_total - screen_row_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
933                                          }                                          }
934                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
935                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
936                                          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_lengths[line_current - output_current_row + row_pos]));
937                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
938                                          break;                                          break;
939                                  case KEY_F1:                                  case KEY_F1:
940                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not reentrant
# Line 673  int editor_display(EDITOR_DATA *p_editor Line 947  int editor_display(EDITOR_DATA *p_editor
947                                          show_help = 1;                                          show_help = 1;
948                                  case KEY_F5:                                  case KEY_F5:
949                                          // Refresh after display help information                                          // Refresh after display help information
950                                          line_current -= (screen_current_row - screen_begin_row);                                          line_current -= (output_current_row - screen_begin_row);
951                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
952                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
953                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
954                                          break;                                          break;
955                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
956                                          break;                                          break;
# Line 686  int editor_display(EDITOR_DATA *p_editor Line 960  int editor_display(EDITOR_DATA *p_editor
960                                  }                                  }
961    
962                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(0);
963    
964                                    if (input_ok)
965                                    {
966                                            break;
967                                    }
968                          }                          }
969    
970                          continue;                          continue;
# Line 703  int editor_display(EDITOR_DATA *p_editor Line 982  int editor_display(EDITOR_DATA *p_editor
982                          len = 0;                          len = 0;
983                  }                  }
984    
985                  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);
986                  buffer[len] = '\0';                  // Replace '\033' with '*'
987                    p_str = p_editor_data->p_display_lines[line_current];
988                    for (i = 0, j = 0; i < len; i++)
989                    {
990                            if (p_str[i] == '\033')
991                            {
992                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
993                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
994                            }
995                            else
996                            {
997                                    buffer[j] = p_str[i];
998                                    j++;
999                            }
1000                    }
1001                    buffer[j] = '\0';
1002    
1003                  moveto(screen_current_row, 0);                  moveto(output_current_row, 0);
1004                  clrtoeol();                  clrtoeol();
1005                  prints("%s", buffer);                  prints("%s", buffer);
1006                  line_current++;                  line_current++;
1007                  screen_current_row++;                  output_current_row++;
1008          }          }
1009    
1010  cleanup:  cleanup:


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

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