/[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.14 by sysadm, Thu Jun 12 03:16:35 2025 UTC Revision 1.18 by sysadm, Fri Jun 13 11:20:24 2025 UTC
# Line 20  Line 20 
20  #include "log.h"  #include "log.h"
21  #include "common.h"  #include "common.h"
22  #include "str_process.h"  #include "str_process.h"
23    #include "memory_pool.h"
24  #include <stdlib.h>  #include <stdlib.h>
25  #include <sys/param.h>  #include <sys/param.h>
26  #include <strings.h>  #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 137  void editor_data_cleanup(EDITOR_DATA *p_ Line 183  void editor_data_cleanup(EDITOR_DATA *p_
183                  if (p_editor_data->display_line_lengths[i] > 0 &&                  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')                          p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
185                  {                  {
186                          free(p_data_line);                          memory_pool_free(p_mp_data_line, p_data_line);
187                          p_data_line = NULL;                          p_data_line = NULL;
188                  }                  }
189          }          }
190    
191          if (p_data_line != NULL)          if (p_data_line != NULL)
192          {          {
193                  free(p_data_line);                  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 162  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 215  int editor_data_insert(EDITOR_DATA *p_ed Line 264  int editor_data_insert(EDITOR_DATA *p_ed
264          {          {
265                  if (p_editor_data->display_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) reach limit(%d)\n",                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
268                                            p_editor_data->display_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                  }                  }
279    
# Line 305  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 334  int editor_data_insert(EDITOR_DATA *p_ed Line 395  int editor_data_insert(EDITOR_DATA *p_ed
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])
397          {          {
398                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                  if (*p_display_line + 1 < p_editor_data->display_line_total)
                 (*p_display_line)++;  
   
                 if (*p_display_line >= p_editor_data->display_line_total)  
399                  {                  {
400                          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];
401                            (*p_display_line)++;
402                  }                  }
403          }          }
404    
# Line 405  int editor_data_delete(EDITOR_DATA *p_ed Line 464  int editor_data_delete(EDITOR_DATA *p_ed
464                  }                  }
465          }          }
466    
467            if (offset_data_line >= len_data_line) // end-of-line
468            {
469                    return 0;
470            }
471    
472          // Check str to be deleted          // Check str to be deleted
473          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)
474          {          {
# Line 416  int editor_data_delete(EDITOR_DATA *p_ed Line 480  int editor_data_delete(EDITOR_DATA *p_ed
480          }          }
481          else          else
482          {          {
483                  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",
484                                    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]);  
485                  str_len = 1;                  str_len = 1;
486          }          }
487    
# Line 455  int editor_data_delete(EDITOR_DATA *p_ed Line 518  int editor_data_delete(EDITOR_DATA *p_ed
518                  p_data_line[offset_data_line + len_data_line] = '\0';                  p_data_line[offset_data_line + len_data_line] = '\0';
519    
520                  // Recycle next data line                  // Recycle next data line
521                  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]);
522          }          }
523          else          else
524          {          {
# Line 488  int editor_data_delete(EDITOR_DATA *p_ed Line 551  int editor_data_delete(EDITOR_DATA *p_ed
551    
552          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
553    
554          if (display_line + i < last_display_line)          if (*p_last_updated_line < last_display_line)
555          {          {
556                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
557                  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++)
558                  {                  {
559                          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];
560                          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];
561                  }                  }
562    
563                  (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));                  j = p_editor_data->display_line_total;
564                  last_display_line = display_line + i;                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
565                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
                 *p_last_updated_line = p_editor_data->display_line_total - 1;  
566          }          }
567    
568          return str_len;          return str_len;
# Line 512  static int editor_display_key_handler(in Line 574  static int editor_display_key_handler(in
574          {          {
575          case 0: // Set msg          case 0: // Set msg
576                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
577                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");
578                  break;                  break;
579          }          }
580    
# Line 675  int editor_display(EDITOR_DATA *p_editor Line 737  int editor_display(EDITOR_DATA *p_editor
737                                  {                                  {
738                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
739                                          {                                          {
740                                                    if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
741                                                    {
742                                                            input_ok = 0;
743                                                            continue;
744                                                    }
745    
746                                                  col_pos--;                                                  col_pos--;
747                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
748                                                  {                                                  {
# Line 725  int editor_display(EDITOR_DATA *p_editor Line 793  int editor_display(EDITOR_DATA *p_editor
793                                                          row_pos += scroll_rows;                                                          row_pos += scroll_rows;
794                                                          output_current_row = screen_begin_row;                                                          output_current_row = screen_begin_row;
795                                                          output_end_row = SCREEN_ROWS - 1;                                                          output_end_row = SCREEN_ROWS - 1;
                                                         clrline(output_current_row, SCREEN_ROWS);  
796                                                  }                                                  }
797    
798                                                    clrline(output_current_row, output_end_row);
799                                          }                                          }
800    
801                                          continue;                                          continue;
# Line 737  int editor_display(EDITOR_DATA *p_editor Line 806  int editor_display(EDITOR_DATA *p_editor
806                                  case KEY_NULL:                                  case KEY_NULL:
807                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
808                                          goto cleanup;                                          goto cleanup;
809                                  case Ctrl('C'):                                  case Ctrl('W'):
810                                          loop = 0;                                          loop = 0;
811                                          break;                                          break;
812                                  case Ctrl('S'): // Start of line                                  case Ctrl('S'): // Start of line
# Line 755  int editor_display(EDITOR_DATA *p_editor Line 824  int editor_display(EDITOR_DATA *p_editor
824                                          break;                                          break;
825                                  case Ctrl('B'): // Bottom of screen                                  case Ctrl('B'): // Bottom of screen
826                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
827                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
828                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                          {
829                                                    row_pos = p_editor_data->display_line_total;
830                                            }
831                                            else
832                                            {
833                                                    row_pos = SCREEN_ROWS - 1;
834                                            }
835                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
836                                            {
837                                                    // last display line does NOT have \n in the end
838                                                    col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);
839                                            }
840                                            else
841                                            {
842                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
843                                            }
844                                          break;                                          break;
845                                  case KEY_INS:                                  case KEY_INS:
846                                          key_insert = !key_insert;                                          key_insert = !key_insert;
# Line 777  int editor_display(EDITOR_DATA *p_editor Line 861  int editor_display(EDITOR_DATA *p_editor
861                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
862                                          {                                          {
863                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
864                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
865                                                  break;                                                  break;
866                                          }                                          }
867                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
868                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
869                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
870                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
871                                          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;
872                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
873                                          break;                                          break;
874                                  case KEY_LEFT:                                  case KEY_LEFT:
# Line 831  int editor_display(EDITOR_DATA *p_editor Line 915  int editor_display(EDITOR_DATA *p_editor
915                                          }                                          }
916                                          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
917                                          {                                          {
918                                                  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
919                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
920                                                  break;                                                  break;
921                                          }                                          }
922                                          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