/[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.23 by sysadm, Sun Jun 15 13:40:52 2025 UTC Revision 1.32 by sysadm, Fri Jun 27 10:14:47 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "editor.h"  
17  #include "bbs.h"  #include "bbs.h"
18    #include "common.h"
19    #include "editor.h"
20  #include "io.h"  #include "io.h"
21  #include "log.h"  #include "log.h"
 #include "common.h"  
 #include "str_process.h"  
22  #include "memory_pool.h"  #include "memory_pool.h"
23    #include "str_process.h"
24  #include <stdlib.h>  #include <stdlib.h>
 #include <sys/param.h>  
 #include <strings.h>  
   
 #define _POSIX_C_SOURCE 200809L  
25  #include <string.h>  #include <string.h>
26    #include <sys/param.h>
27    
28  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
29  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000
# Line 96  EDITOR_DATA *editor_data_load(const char Line 93  EDITOR_DATA *editor_data_load(const char
93                  return NULL;                  return NULL;
94          }          }
95    
96          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1);          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, 0);
97    
98          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
99          {          {
# Line 138  EDITOR_DATA *editor_data_load(const char Line 135  EDITOR_DATA *editor_data_load(const char
135                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
136          }          }
137    
138          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);          memset(p_editor_data->p_display_lines + p_editor_data->display_line_total, 0, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);
139    
140          return p_editor_data;          return p_editor_data;
141  }  }
# Line 216  int editor_data_insert(EDITOR_DATA *p_ed Line 213  int editor_data_insert(EDITOR_DATA *p_ed
213          long last_display_line; // of data line          long last_display_line; // of data line
214          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
215          long split_line_total;          long split_line_total;
216          long i, j;          long i;
217          int len;          int len;
218          int eol;          int eol;
219          int display_len;          int display_len;
# Line 281  int editor_data_insert(EDITOR_DATA *p_ed Line 278  int editor_data_insert(EDITOR_DATA *p_ed
278          {          {
279                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
280                  {                  {
281                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",  #ifdef _DEBUG
282                          //                p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                          log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
283                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
284    #endif
285    
286                          return -2;                          return -2;
287                  }                  }
288    
# Line 362  int editor_data_insert(EDITOR_DATA *p_ed Line 362  int editor_data_insert(EDITOR_DATA *p_ed
362          }          }
363    
364          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
365          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);
366    
367          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
368          {          {
# Line 371  int editor_data_insert(EDITOR_DATA *p_ed Line 371  int editor_data_insert(EDITOR_DATA *p_ed
371                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
372                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
373                          {                          {
374                                  // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);  #ifdef _DEBUG
375                                    log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
376    #endif
377    
378                                  // Terminate prior display line with \n, to avoid error on cleanup                                  // Terminate prior display line with \n, to avoid error on cleanup
379                                  if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)                                  if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
380                                  {                                  {
381                                          len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len);                                          len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
382                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
383                                          p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';                                          p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
384                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
# Line 386  int editor_data_insert(EDITOR_DATA *p_ed Line 389  int editor_data_insert(EDITOR_DATA *p_ed
389                                  }                                  }
390                                  break;                                  break;
391                          }                          }
392                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)  
393                          {                          // for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
394                                  p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];                          // {
395                                  p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];                          //      p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
396                          }                          //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
397                            // }
398                            memmove(p_editor_data->p_display_lines + last_display_line + 2,
399                                            p_editor_data->p_display_lines + last_display_line + 1,
400                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
401                                                    sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
402                            memmove(p_editor_data->display_line_lengths + last_display_line + 2,
403                                            p_editor_data->display_line_lengths + last_display_line + 1,
404                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
405                                                    sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
406    
407                          last_display_line++;                          last_display_line++;
408                            *p_last_updated_line = p_editor_data->display_line_total;
409                          (p_editor_data->display_line_total)++;                          (p_editor_data->display_line_total)++;
410                  }                  }
411    
# Line 422  int editor_data_insert(EDITOR_DATA *p_ed Line 436  int editor_data_insert(EDITOR_DATA *p_ed
436          // Prevent the last display line from being over-length          // Prevent the last display line from being over-length
437          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
438          {          {
439                  len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len);                  len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
440                  p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';                  p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
441                  p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;                  p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
442                  if (*p_display_line + 1 >= p_editor_data->display_line_total)                  if (*p_display_line + 1 >= p_editor_data->display_line_total)
# Line 564  int editor_data_delete(EDITOR_DATA *p_ed Line 578  int editor_data_delete(EDITOR_DATA *p_ed
578          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
579    
580          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
581          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);
582    
583          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
584          {          {
# Line 586  int editor_data_delete(EDITOR_DATA *p_ed Line 600  int editor_data_delete(EDITOR_DATA *p_ed
600          if (*p_last_updated_line < last_display_line)          if (*p_last_updated_line < last_display_line)
601          {          {
602                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
603                  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++)
604                  {                  // {
605                          p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = 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];
606                          p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = 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];
607                  }                  // }
608                    memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
609                                    p_editor_data->p_display_lines + last_display_line + 1,
610                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
611                                            sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
612                    memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
613                                    p_editor_data->display_line_lengths + last_display_line + 1,
614                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
615                                            sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
616    
617                  j = p_editor_data->display_line_total;                  j = p_editor_data->display_line_total;
618                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
# Line 609  static int editor_display_key_handler(in Line 631  static int editor_display_key_handler(in
631          {          {
632          case 0: // Set msg          case 0: // Set msg
633                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
634                                   "| 退出[\033[32mCtrl-W\033[33m] | 帮助[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
635                    break;
636            case KEY_CSI:
637                    *p_key = KEY_ESC;
638                  break;                  break;
639          }          }
640    
# Line 663  int editor_display(EDITOR_DATA *p_editor Line 688  int editor_display(EDITOR_DATA *p_editor
688                                           "%s",                                           "%s",
689                                           row_pos, col_pos,                                           row_pos, col_pos,
690                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
691                                           key_insert ? "插入" : "改写",                                           key_insert ? "插入" : "替换",
692                                           ctx.msg);                                           ctx.msg);
693    
694                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
695                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
696                          {                          {
697                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 680  int editor_display(EDITOR_DATA *p_editor Line 705  int editor_display(EDITOR_DATA *p_editor
705                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
706                          iflush();                          iflush();
707    
708                            str_len = 0;
709                          input_ok = 0;                          input_ok = 0;
710                            ch = igetch_t(MAX_DELAY_TIME);
711                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
712                          {                          {
                                 ch = igetch_t(MAX_DELAY_TIME);  
                                 input_ok = 1;  
   
713                                  // extended key handler                                  // extended key handler
714                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
715                                  {                                  {
# Line 706  int editor_display(EDITOR_DATA *p_editor Line 730  int editor_display(EDITOR_DATA *p_editor
730                                  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
731                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
732                                  {                                  {
733                                            BBS_last_access_tm = time(NULL);
734    
735                                          if (str_len == 0) // ch >= 32 && ch < 127                                          if (str_len == 0) // ch >= 32 && ch < 127
736                                          {                                          {
737                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
738                                                  str_len = 1;                                                  str_len = 1;
739                                          }                                          }
740    
                                         last_updated_line = line_current;  
741                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
742                                          offset_in = col_pos - 1;                                          offset_in = col_pos - 1;
743                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
744                                          offset_out = offset_in;                                          offset_out = offset_in;
745    
746                                            last_updated_line = display_line_in;
747    
748                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
749                                          {                                          {
750                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
# Line 764  int editor_display(EDITOR_DATA *p_editor Line 791  int editor_display(EDITOR_DATA *p_editor
791                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
792                                          }                                          }
793    
794                                            if (display_line_out != display_line_in) // Output on line change
795                                            {
796                                                    break;
797                                            }
798    
799                                            ch = igetch(0);
800                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
801                                            {
802                                                    break;
803                                            }
804    
805                                          str_len = 0;                                          str_len = 0;
806                                          continue;                                          continue;
807                                  }                                  }
808                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
809                                  {                                  {
810                                            BBS_last_access_tm = time(NULL);
811    
812                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
813                                          {                                          {
814                                                  if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden                                                  if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
815                                                  {                                                  {
816                                                          input_ok = 0;                                                          ch = igetch_t(MAX_DELAY_TIME);
817                                                          continue;                                                          continue;
818                                                  }                                                  }
819    
# Line 832  int editor_display(EDITOR_DATA *p_editor Line 872  int editor_display(EDITOR_DATA *p_editor
872                                                  clrline(output_current_row, output_end_row);                                                  clrline(output_current_row, output_end_row);
873                                          }                                          }
874    
875                                            if (display_line_out != display_line_in) // Output on line change
876                                            {
877                                                    break;
878                                            }
879    
880                                            ch = igetch(0);
881                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
882                                            {
883                                                    break;
884                                            }
885    
886                                          str_len = 0;                                          str_len = 0;
887                                          continue;                                          continue;
888                                  }                                  }
889    
890                                    input_ok = 1;
891                                  switch (ch)                                  switch (ch)
892                                  {                                  {
893                                  case KEY_NULL:                                  case KEY_NULL:
# Line 1032  int editor_display(EDITOR_DATA *p_editor Line 1084  int editor_display(EDITOR_DATA *p_editor
1084                                          break;                                          break;
1085                                  }                                  }
1086    
1087                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(NULL);
1088    
1089                                  if (input_ok)                                  if (input_ok)
1090                                  {                                  {
1091                                          break;                                          break;
1092                                  }                                  }
1093    
1094                                    ch = igetch_t(MAX_DELAY_TIME);
1095                          }                          }
1096    
1097                          continue;                          continue;


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

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