/[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.22 by sysadm, Sun Jun 15 04:43:33 2025 UTC Revision 1.30 by sysadm, Sat Jun 21 01:34:12 2025 UTC
# Line 22  Line 22 
22  #include "str_process.h"  #include "str_process.h"
23  #include "memory_pool.h"  #include "memory_pool.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 238  int editor_data_insert(EDITOR_DATA *p_ed Line 235  int editor_data_insert(EDITOR_DATA *p_ed
235          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
236          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
237          {          {
238                  if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK                  if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK
239                  {                  {
240                          i++;                          i++;
241                  }                  }
# 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 422  int editor_data_insert(EDITOR_DATA *p_ed Line 425  int editor_data_insert(EDITOR_DATA *p_ed
425          // Prevent the last display line from being over-length          // Prevent the last display line from being over-length
426          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
427          {          {
428                  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);
429                  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';
430                  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;
431                  if (*p_display_line + 1 >= p_editor_data->display_line_total)                  if (*p_display_line + 1 >= p_editor_data->display_line_total)
# Line 435  int editor_data_insert(EDITOR_DATA *p_ed Line 438  int editor_data_insert(EDITOR_DATA *p_ed
438          return 0;          return 0;
439  }  }
440    
441  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,  int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
442                                             long *p_last_updated_line)                                             long *p_last_updated_line)
443  {  {
444            long display_line = *p_display_line;
445            long offset = *p_offset;
446          char *p_data_line = NULL;          char *p_data_line = NULL;
447          long len_data_line;          long len_data_line;
448          long offset_data_line;          long offset_data_line;
# Line 456  int editor_data_delete(EDITOR_DATA *p_ed Line 461  int editor_data_delete(EDITOR_DATA *p_ed
461          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
462          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
463          {          {
464                  if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK                  if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK
465                  {                  {
466                          i++;                          i++;
467                  }                  }
# Line 504  int editor_data_delete(EDITOR_DATA *p_ed Line 509  int editor_data_delete(EDITOR_DATA *p_ed
509          {          {
510                  str_len = 1;                  str_len = 1;
511          }          }
512          else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK          else if (p_data_line[offset_data_line + 1] < 0) // GBK
513          {          {
514                  str_len = 2;                  str_len = 2;
515          }          }
# Line 562  int editor_data_delete(EDITOR_DATA *p_ed Line 567  int editor_data_delete(EDITOR_DATA *p_ed
567          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
568    
569          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
570          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);
571    
572          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
573          {          {
# Line 595  int editor_data_delete(EDITOR_DATA *p_ed Line 600  int editor_data_delete(EDITOR_DATA *p_ed
600                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
601          }          }
602    
603            // Return real offset
604            *p_offset = offset;
605    
606          return str_len;          return str_len;
607  }  }
608    
# Line 604  static int editor_display_key_handler(in Line 612  static int editor_display_key_handler(in
612          {          {
613          case 0: // Set msg          case 0: // Set msg
614                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
615                                   "| 退出[\033[32mCtrl-W\033[33m] | 帮助[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
616                    break;
617            case KEY_CSI:
618                    *p_key = KEY_ESC;
619                  break;                  break;
620          }          }
621    
# Line 658  int editor_display(EDITOR_DATA *p_editor Line 669  int editor_display(EDITOR_DATA *p_editor
669                                           "%s",                                           "%s",
670                                           row_pos, col_pos,                                           row_pos, col_pos,
671                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
672                                           key_insert ? "插入" : "改写",                                           key_insert ? "插入" : "替换",
673                                           ctx.msg);                                           ctx.msg);
674    
675                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
676                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
677                          {                          {
678                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 675  int editor_display(EDITOR_DATA *p_editor Line 686  int editor_display(EDITOR_DATA *p_editor
686                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
687                          iflush();                          iflush();
688    
689                            str_len = 0;
690                          input_ok = 0;                          input_ok = 0;
691                            ch = igetch_t(MAX_DELAY_TIME);
692                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
693                          {                          {
                                 ch = igetch_t(MAX_DELAY_TIME);  
                                 input_ok = 1;  
   
694                                  // extended key handler                                  // extended key handler
695                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
696                                  {                                  {
# Line 701  int editor_display(EDITOR_DATA *p_editor Line 711  int editor_display(EDITOR_DATA *p_editor
711                                  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
712                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
713                                  {                                  {
714                                            BBS_last_access_tm = time(NULL);
715    
716                                          if (str_len == 0) // ch >= 32 && ch < 127                                          if (str_len == 0) // ch >= 32 && ch < 127
717                                          {                                          {
718                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
# Line 715  int editor_display(EDITOR_DATA *p_editor Line 727  int editor_display(EDITOR_DATA *p_editor
727    
728                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
729                                          {                                          {
730                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
731                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line) < 0)
732                                                  {                                                  {
733                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
# Line 756  int editor_display(EDITOR_DATA *p_editor Line 768  int editor_display(EDITOR_DATA *p_editor
768                                                  {                                                  {
769                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
770                                                  }                                                  }
771                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
772                                            }
773    
774                                            if (display_line_out != display_line_in) // Output on line change
775                                            {
776                                                    break;
777                                            }
778    
779                                            ch = igetch(0);
780                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
781                                            {
782                                                    break;
783                                          }                                          }
784    
785                                          str_len = 0;                                          str_len = 0;
# Line 764  int editor_display(EDITOR_DATA *p_editor Line 787  int editor_display(EDITOR_DATA *p_editor
787                                  }                                  }
788                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
789                                  {                                  {
790                                            BBS_last_access_tm = time(NULL);
791    
792                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
793                                          {                                          {
794                                                  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
795                                                  {                                                  {
796                                                          input_ok = 0;                                                          ch = igetch_t(MAX_DELAY_TIME);
797                                                          continue;                                                          continue;
798                                                  }                                                  }
799    
800                                                  col_pos--;                                                  col_pos--;
801                                                    if (col_pos > 1 &&
802                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK
803                                                    {
804                                                            col_pos--;
805                                                    }
806    
807                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
808                                                  {                                                  {
809                                                          row_pos--;                                                          row_pos--;
# Line 780  int editor_display(EDITOR_DATA *p_editor Line 811  int editor_display(EDITOR_DATA *p_editor
811                                                  }                                                  }
812                                          }                                          }
813    
814                                          if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1,                                          display_line_in = line_current - output_current_row + row_pos;
815                                            offset_in = col_pos - 1;
816                                            display_line_out = display_line_in;
817                                            offset_out = offset_in;
818    
819                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
820                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line)) < 0)
821                                          {                                          {
822                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
823                                          }                                          }
824                                          else                                          else
825                                          {                                          {
826                                                  if (ch == BACKSPACE)                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
                                                 {  
                                                         for (i = 1; i < str_len; i++)  
                                                         {  
                                                                 col_pos--;  
                                                                 if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)  
                                                                 {  
                                                                         row_pos--;  
                                                                         col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);  
                                                                 }  
                                                         }  
                                                 }  
827    
828                                                  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));
829                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
# Line 827  int editor_display(EDITOR_DATA *p_editor Line 852  int editor_display(EDITOR_DATA *p_editor
852                                                  clrline(output_current_row, output_end_row);                                                  clrline(output_current_row, output_end_row);
853                                          }                                          }
854    
855                                            if (display_line_out != display_line_in) // Output on line change
856                                            {
857                                                    break;
858                                            }
859    
860                                            ch = igetch(0);
861                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
862                                            {
863                                                    break;
864                                            }
865    
866                                          str_len = 0;                                          str_len = 0;
867                                          continue;                                          continue;
868                                  }                                  }
869    
870                                    input_ok = 1;
871                                  switch (ch)                                  switch (ch)
872                                  {                                  {
873                                  case KEY_NULL:                                  case KEY_NULL:
# Line 845  int editor_display(EDITOR_DATA *p_editor Line 882  int editor_display(EDITOR_DATA *p_editor
882                                          break;                                          break;
883                                  case Ctrl('E'): // End of line                                  case Ctrl('E'): // End of line
884                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
885                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
886                                          {                                          {
887                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
888                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
# Line 911  int editor_display(EDITOR_DATA *p_editor Line 948  int editor_display(EDITOR_DATA *p_editor
948                                          if (col_pos > 1)                                          if (col_pos > 1)
949                                          {                                          {
950                                                  col_pos--;                                                  col_pos--;
951                                                    if (col_pos > 1 &&
952                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
953                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK
954                                                    {
955                                                            col_pos--;
956                                                    }
957                                                  break;                                                  break;
958                                          }                                          }
959                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 938  int editor_display(EDITOR_DATA *p_editor Line 981  int editor_display(EDITOR_DATA *p_editor
981                                  case KEY_RIGHT:                                  case KEY_RIGHT:
982                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
983                                          {                                          {
984                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
985                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK
986                                                    {
987                                                            col_pos++;
988                                                    }
989                                                  col_pos++;                                                  col_pos++;
990                                                  break;                                                  break;
991                                          }                                          }
# Line 949  int editor_display(EDITOR_DATA *p_editor Line 997  int editor_display(EDITOR_DATA *p_editor
997                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
998                                                  break;                                                  break;
999                                          }                                          }
1000                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
1001                                          {                                          {
1002                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
1003                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
# Line 1016  int editor_display(EDITOR_DATA *p_editor Line 1064  int editor_display(EDITOR_DATA *p_editor
1064                                          break;                                          break;
1065                                  }                                  }
1066    
1067                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(NULL);
1068    
1069                                  if (input_ok)                                  if (input_ok)
1070                                  {                                  {
1071                                          break;                                          break;
1072                                  }                                  }
1073    
1074                                    ch = igetch_t(MAX_DELAY_TIME);
1075                          }                          }
1076    
1077                          continue;                          continue;


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

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