/[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.15 by sysadm, Thu Jun 12 04:45:57 2025 UTC Revision 1.21 by sysadm, Sat Jun 14 11:15:46 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 28  Line 29 
29  #include <string.h>  #include <string.h>
30    
31  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  #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  {  {
# Line 43  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    
# Line 61  EDITOR_DATA *editor_data_load(const char Line 107  EDITOR_DATA *editor_data_load(const char
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                  {                  {
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                                  editor_data_cleanup(p_editor_data);                                  editor_data_cleanup(p_editor_data);
116                                  return NULL;                                  return NULL;
# Line 80  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    
130                    // Trim \n from last line
131                    if (i + 1 == p_editor_data->display_line_total &&
132                            p_editor_data->display_line_lengths[i] > 0 &&
133                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
134                    {
135                            p_editor_data->display_line_lengths[i]--;
136                            current_data_line_length--;
137                    }
138                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
139          }          }
140    
# Line 137  void editor_data_cleanup(EDITOR_DATA *p_ Line 192  void editor_data_cleanup(EDITOR_DATA *p_
192                  if (p_editor_data->display_line_lengths[i] > 0 &&                  if (p_editor_data->display_line_lengths[i] > 0 &&
193                          p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')                          p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
194                  {                  {
195                          free(p_data_line);                          memory_pool_free(p_mp_data_line, p_data_line);
196                          p_data_line = NULL;                          p_data_line = NULL;
197                  }                  }
198          }          }
199    
200          if (p_data_line != NULL)          if (p_data_line != NULL)
201          {          {
202                  free(p_data_line);                  memory_pool_free(p_mp_data_line, p_data_line);
203          }          }
204    
205          free(p_editor_data);          memory_pool_free(p_mp_editor_data, p_editor_data);
206  }  }
207    
208  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 172  int editor_data_insert(EDITOR_DATA *p_ed Line 227  int editor_data_insert(EDITOR_DATA *p_ed
227                  return -1;                  return -1;
228          }          }
229    
230            // Validate str
231            if ((str_len == 1 && str[0] <= 0) ||
232                    (str_len == 2 && (str[0] >= 0 || str[1] >= 0)))
233            {
234                    log_error("Invalid input str, len=%d\n", str_len);
235                    return -2;
236            }
237    
238          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
239          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
240          {          {
# Line 224  int editor_data_insert(EDITOR_DATA *p_ed Line 287  int editor_data_insert(EDITOR_DATA *p_ed
287                  }                  }
288    
289                  // Allocate new data line                  // Allocate new data line
290                  p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                  p_data_line = memory_pool_alloc(p_mp_data_line);
291                  if (p_data_line == NULL)                  if (p_data_line == NULL)
292                  {                  {
293                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");                          log_error("memory_pool_alloc() error\n");
294                          return -2;                          return -2;
295                  }                  }
296    
# Line 349  int editor_data_insert(EDITOR_DATA *p_ed Line 412  int editor_data_insert(EDITOR_DATA *p_ed
412    
413          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
414          {          {
415                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                  if (*p_display_line + 1 < p_editor_data->display_line_total)
   
                 if (*p_display_line + 1 >= p_editor_data->display_line_total)  
416                  {                  {
417                          log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);                          *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
418                            (*p_display_line)++;
419                  }                  }
420                  else                  else if (*p_display_line + 1 >= MAX_EDITOR_DATA_LINES)
421                  {                  {
422                          (*p_display_line)++;                          len = split_line(p_editor_data->p_display_lines[*p_display_line], SCREEN_COLS - 1, &eol, &display_len);
423                            p_editor_data->p_display_lines[*p_display_line][len] = '\0';
424                            p_editor_data->display_line_lengths[*p_display_line] = len;
425                            *p_offset = len;
426                  }                  }
427          }          }
428    
# Line 423  int editor_data_delete(EDITOR_DATA *p_ed Line 488  int editor_data_delete(EDITOR_DATA *p_ed
488                  }                  }
489          }          }
490    
491            if (offset_data_line >= len_data_line) // end-of-line
492            {
493                    return 0;
494            }
495    
496          // Check str to be deleted          // Check str to be deleted
497          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
498          {          {
# Line 434  int editor_data_delete(EDITOR_DATA *p_ed Line 504  int editor_data_delete(EDITOR_DATA *p_ed
504          }          }
505          else          else
506          {          {
507                  log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n",                  log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
508                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1],                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
                                   p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]);  
509                  str_len = 1;                  str_len = 1;
510          }          }
511    
# Line 473  int editor_data_delete(EDITOR_DATA *p_ed Line 542  int editor_data_delete(EDITOR_DATA *p_ed
542                  p_data_line[offset_data_line + len_data_line] = '\0';                  p_data_line[offset_data_line + len_data_line] = '\0';
543    
544                  // Recycle next data line                  // Recycle next data line
545                  free(p_editor_data->p_display_lines[display_line + 1]);                  memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]);
546          }          }
547          else          else
548          {          {
# Line 506  int editor_data_delete(EDITOR_DATA *p_ed Line 575  int editor_data_delete(EDITOR_DATA *p_ed
575    
576          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
577    
578          if (display_line + i < last_display_line)          if (*p_last_updated_line < last_display_line)
579          {          {
580                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
581                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
582                  {                  {
583                          p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j];                          p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j];
584                          p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j];                          p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];
585                  }                  }
586    
587                  (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));                  j = p_editor_data->display_line_total;
588                  last_display_line = display_line + i;                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
589                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
                 *p_last_updated_line = p_editor_data->display_line_total - 1;  
590          }          }
591    
592          return str_len;          return str_len;
# Line 530  static int editor_display_key_handler(in Line 598  static int editor_display_key_handler(in
598          {          {
599          case 0: // Set msg          case 0: // Set msg
600                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
601                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");
602                  break;                  break;
603          }          }
604    
# Line 618  int editor_display(EDITOR_DATA *p_editor Line 686  int editor_display(EDITOR_DATA *p_editor
686                                          input_str[str_len] = (char)(ch - 256);                                          input_str[str_len] = (char)(ch - 256);
687                                          str_len++;                                          str_len++;
688                                  }                                  }
689                                  else                                  else if (str_len > 0)
690                                  {                                  {
691                                            log_error("Received %d character over 127 followed by character less than 127\n", str_len);
692                                          str_len = 0;                                          str_len = 0;
693                                  }                                  }
694    
695                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK
696                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
697                                  {                                  {
698                                          if (str_len == 0)                                          if (str_len == 0) // ch >= 32 && ch < 127
699                                          {                                          {
700                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
701                                                  str_len = 1;                                                  str_len = 1;
# Line 651  int editor_display(EDITOR_DATA *p_editor Line 720  int editor_display(EDITOR_DATA *p_editor
720                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
721                                          {                                          {
722                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);
                                                 str_len = 0;  
723                                          }                                          }
724                                          else                                          else
725                                          {                                          {
                                                 str_len = 0;  
   
726                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
727                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
728                                                  output_current_row = (int)row_pos;                                                  output_current_row = (int)row_pos;
# Line 687  int editor_display(EDITOR_DATA *p_editor Line 753  int editor_display(EDITOR_DATA *p_editor
753                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1;
754                                          }                                          }
755    
756                                            str_len = 0;
757                                          continue;                                          continue;
758                                  }                                  }
759                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
760                                  {                                  {
761                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
762                                          {                                          {
763                                                    if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
764                                                    {
765                                                            input_ok = 0;
766                                                            continue;
767                                                    }
768    
769                                                  col_pos--;                                                  col_pos--;
770                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
771                                                  {                                                  {
# Line 743  int editor_display(EDITOR_DATA *p_editor Line 816  int editor_display(EDITOR_DATA *p_editor
816                                                          row_pos += scroll_rows;                                                          row_pos += scroll_rows;
817                                                          output_current_row = screen_begin_row;                                                          output_current_row = screen_begin_row;
818                                                          output_end_row = SCREEN_ROWS - 1;                                                          output_end_row = SCREEN_ROWS - 1;
                                                         clrline(output_current_row, SCREEN_ROWS);  
819                                                  }                                                  }
820    
821                                                    clrline(output_current_row, output_end_row);
822                                          }                                          }
823    
824                                            str_len = 0;
825                                          continue;                                          continue;
826                                  }                                  }
827    
# Line 755  int editor_display(EDITOR_DATA *p_editor Line 830  int editor_display(EDITOR_DATA *p_editor
830                                  case KEY_NULL:                                  case KEY_NULL:
831                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
832                                          goto cleanup;                                          goto cleanup;
833                                  case Ctrl('C'):                                  case Ctrl('W'):
834                                          loop = 0;                                          loop = 0;
835                                          break;                                          break;
836                                  case Ctrl('S'): // Start of line                                  case Ctrl('S'): // Start of line
# Line 773  int editor_display(EDITOR_DATA *p_editor Line 848  int editor_display(EDITOR_DATA *p_editor
848                                          break;                                          break;
849                                  case Ctrl('B'): // Bottom of screen                                  case Ctrl('B'): // Bottom of screen
850                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
851                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
852                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          {
853                                                    row_pos = p_editor_data->display_line_total;
854                                            }
855                                            else
856                                            {
857                                                    row_pos = SCREEN_ROWS - 1;
858                                            }
859                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
860                                            {
861                                                    // last display line does NOT have \n in the end
862                                                    col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);
863                                            }
864                                            else
865                                            {
866                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
867                                            }
868                                          break;                                          break;
869                                  case KEY_INS:                                  case KEY_INS:
870                                          key_insert = !key_insert;                                          key_insert = !key_insert;
# Line 795  int editor_display(EDITOR_DATA *p_editor Line 885  int editor_display(EDITOR_DATA *p_editor
885                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
886                                          {                                          {
887                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
888                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
889                                                  break;                                                  break;
890                                          }                                          }
891                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
892                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
893                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
894                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
895                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                          col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
896                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
897                                          break;                                          break;
898                                  case KEY_LEFT:                                  case KEY_LEFT:
# Line 849  int editor_display(EDITOR_DATA *p_editor Line 939  int editor_display(EDITOR_DATA *p_editor
939                                          }                                          }
940                                          if (line_current + (screen_row_total - (output_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
941                                          {                                          {
942                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                                  // last display line does NOT have \n in the end
943                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
944                                                  break;                                                  break;
945                                          }                                          }
946                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));


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

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