/[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.21 by sysadm, Sat Jun 14 11:15:46 2025 UTC Revision 1.29 by sysadm, Tue Jun 17 13:25:49 2025 UTC
# Line 24  Line 24 
24  #include <stdlib.h>  #include <stdlib.h>
25  #include <sys/param.h>  #include <sys/param.h>
26  #include <strings.h>  #include <strings.h>
   
 #define _POSIX_C_SOURCE 200809L  
27  #include <string.h>  #include <string.h>
28    
29  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
# Line 79  EDITOR_DATA *editor_data_load(const char Line 77  EDITOR_DATA *editor_data_load(const char
77  {  {
78          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
79          char *p_data_line = NULL;          char *p_data_line = NULL;
80          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
81          long current_data_line_length = 0;          long current_data_line_length = 0;
82          long i;          long i;
83    
# Line 96  EDITOR_DATA *editor_data_load(const char Line 94  EDITOR_DATA *editor_data_load(const char
94                  return NULL;                  return NULL;
95          }          }
96    
97          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, 0);
98    
99          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
100          {          {
# Line 138  EDITOR_DATA *editor_data_load(const char Line 136  EDITOR_DATA *editor_data_load(const char
136                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
137          }          }
138    
139          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);
140    
141          return p_editor_data;          return p_editor_data;
142  }  }
# Line 238  int editor_data_insert(EDITOR_DATA *p_ed Line 236  int editor_data_insert(EDITOR_DATA *p_ed
236          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
237          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
238          {          {
239                  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
240                  {                  {
241                          i++;                          i++;
242                  }                  }
# Line 281  int editor_data_insert(EDITOR_DATA *p_ed Line 279  int editor_data_insert(EDITOR_DATA *p_ed
279          {          {
280                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
281                  {                  {
282                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",  #ifdef _DEBUG
283                          //                p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                          log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
284                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
285    #endif
286    
287                          return -2;                          return -2;
288                  }                  }
289    
# Line 362  int editor_data_insert(EDITOR_DATA *p_ed Line 363  int editor_data_insert(EDITOR_DATA *p_ed
363          }          }
364    
365          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
366          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);
367    
368          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
369          {          {
# Line 371  int editor_data_insert(EDITOR_DATA *p_ed Line 372  int editor_data_insert(EDITOR_DATA *p_ed
372                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
373                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
374                          {                          {
375                                  // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);  #ifdef _DEBUG
376                                    log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
377    #endif
378    
379                                  // Terminate prior display line with \n, to avoid error on cleanup                                  // Terminate prior display line with \n, to avoid error on cleanup
380                                  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)
381                                  {                                  {
382                                          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);
383                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
384                                          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';
385                                          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 417  int editor_data_insert(EDITOR_DATA *p_ed Line 421  int editor_data_insert(EDITOR_DATA *p_ed
421                          *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                          *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
422                          (*p_display_line)++;                          (*p_display_line)++;
423                  }                  }
424                  else if (*p_display_line + 1 >= MAX_EDITOR_DATA_LINES)          }
425    
426            // Prevent the last display line from being over-length
427            if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
428            {
429                    len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
430                    p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
431                    p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
432                    if (*p_display_line + 1 >= p_editor_data->display_line_total)
433                  {                  {
434                          len = split_line(p_editor_data->p_display_lines[*p_display_line], SCREEN_COLS - 1, &eol, &display_len);                          *p_offset = MIN(*p_offset, len);
435                          p_editor_data->p_display_lines[*p_display_line][len] = '\0';                          *p_display_line = p_editor_data->display_line_total - 1;
                         p_editor_data->display_line_lengths[*p_display_line] = len;  
                         *p_offset = len;  
436                  }                  }
437          }          }
438    
439          return 0;          return 0;
440  }  }
441    
442  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,
443                                             long *p_last_updated_line)                                             long *p_last_updated_line)
444  {  {
445            long display_line = *p_display_line;
446            long offset = *p_offset;
447          char *p_data_line = NULL;          char *p_data_line = NULL;
448          long len_data_line;          long len_data_line;
449          long offset_data_line;          long offset_data_line;
# Line 450  int editor_data_delete(EDITOR_DATA *p_ed Line 462  int editor_data_delete(EDITOR_DATA *p_ed
462          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
463          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
464          {          {
465                  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
466                  {                  {
467                          i++;                          i++;
468                  }                  }
# Line 498  int editor_data_delete(EDITOR_DATA *p_ed Line 510  int editor_data_delete(EDITOR_DATA *p_ed
510          {          {
511                  str_len = 1;                  str_len = 1;
512          }          }
513          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
514          {          {
515                  str_len = 2;                  str_len = 2;
516          }          }
# Line 556  int editor_data_delete(EDITOR_DATA *p_ed Line 568  int editor_data_delete(EDITOR_DATA *p_ed
568          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
569    
570          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
571          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);
572    
573          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
574          {          {
# Line 589  int editor_data_delete(EDITOR_DATA *p_ed Line 601  int editor_data_delete(EDITOR_DATA *p_ed
601                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
602          }          }
603    
604            // Return real offset
605            *p_offset = offset;
606    
607          return str_len;          return str_len;
608  }  }
609    
# Line 598  static int editor_display_key_handler(in Line 613  static int editor_display_key_handler(in
613          {          {
614          case 0: // Set msg          case 0: // Set msg
615                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
616                                   "| 退出[\033[32mCtrl-W\033[33m] | 帮助[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
617                    break;
618            case KEY_CSI:
619                    *p_key = KEY_ESC;
620                  break;                  break;
621          }          }
622    
# Line 652  int editor_display(EDITOR_DATA *p_editor Line 670  int editor_display(EDITOR_DATA *p_editor
670                                           "%s",                                           "%s",
671                                           row_pos, col_pos,                                           row_pos, col_pos,
672                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
673                                           key_insert ? "插入" : "改写",                                           key_insert ? "插入" : "替换",
674                                           ctx.msg);                                           ctx.msg);
675    
676                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
677                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
678                          {                          {
679                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 669  int editor_display(EDITOR_DATA *p_editor Line 687  int editor_display(EDITOR_DATA *p_editor
687                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
688                          iflush();                          iflush();
689    
690                            str_len = 0;
691                          input_ok = 0;                          input_ok = 0;
692                            ch = igetch_t(MAX_DELAY_TIME);
693                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
694                          {                          {
                                 ch = igetch_t(MAX_DELAY_TIME);  
                                 input_ok = 1;  
   
695                                  // extended key handler                                  // extended key handler
696                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
697                                  {                                  {
# Line 695  int editor_display(EDITOR_DATA *p_editor Line 712  int editor_display(EDITOR_DATA *p_editor
712                                  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
713                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
714                                  {                                  {
715                                            BBS_last_access_tm = time(NULL);
716    
717                                          if (str_len == 0) // ch >= 32 && ch < 127                                          if (str_len == 0) // ch >= 32 && ch < 127
718                                          {                                          {
719                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
# Line 709  int editor_display(EDITOR_DATA *p_editor Line 728  int editor_display(EDITOR_DATA *p_editor
728    
729                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
730                                          {                                          {
731                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
732                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line) < 0)
733                                                  {                                                  {
734                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
# Line 750  int editor_display(EDITOR_DATA *p_editor Line 769  int editor_display(EDITOR_DATA *p_editor
769                                                  {                                                  {
770                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
771                                                  }                                                  }
772                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
773                                            }
774    
775                                            if (display_line_out != display_line_in) // Output on line change
776                                            {
777                                                    break;
778                                            }
779    
780                                            ch = igetch(0);
781                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
782                                            {
783                                                    break;
784                                          }                                          }
785    
786                                          str_len = 0;                                          str_len = 0;
# Line 758  int editor_display(EDITOR_DATA *p_editor Line 788  int editor_display(EDITOR_DATA *p_editor
788                                  }                                  }
789                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
790                                  {                                  {
791                                            BBS_last_access_tm = time(NULL);
792    
793                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
794                                          {                                          {
795                                                  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
796                                                  {                                                  {
797                                                          input_ok = 0;                                                          ch = igetch_t(MAX_DELAY_TIME);
798                                                          continue;                                                          continue;
799                                                  }                                                  }
800    
801                                                  col_pos--;                                                  col_pos--;
802                                                    if (col_pos > 1 &&
803                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK
804                                                    {
805                                                            col_pos--;
806                                                    }
807    
808                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
809                                                  {                                                  {
810                                                          row_pos--;                                                          row_pos--;
# Line 774  int editor_display(EDITOR_DATA *p_editor Line 812  int editor_display(EDITOR_DATA *p_editor
812                                                  }                                                  }
813                                          }                                          }
814    
815                                          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;
816                                            offset_in = col_pos - 1;
817                                            display_line_out = display_line_in;
818                                            offset_out = offset_in;
819    
820                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
821                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line)) < 0)
822                                          {                                          {
823                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
824                                          }                                          }
825                                          else                                          else
826                                          {                                          {
827                                                  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]);  
                                                                 }  
                                                         }  
                                                 }  
828    
829                                                  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));
830                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
# Line 821  int editor_display(EDITOR_DATA *p_editor Line 853  int editor_display(EDITOR_DATA *p_editor
853                                                  clrline(output_current_row, output_end_row);                                                  clrline(output_current_row, output_end_row);
854                                          }                                          }
855    
856                                            if (display_line_out != display_line_in) // Output on line change
857                                            {
858                                                    break;
859                                            }
860    
861                                            ch = igetch(0);
862                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
863                                            {
864                                                    break;
865                                            }
866    
867                                          str_len = 0;                                          str_len = 0;
868                                          continue;                                          continue;
869                                  }                                  }
870    
871                                    input_ok = 1;
872                                  switch (ch)                                  switch (ch)
873                                  {                                  {
874                                  case KEY_NULL:                                  case KEY_NULL:
# Line 839  int editor_display(EDITOR_DATA *p_editor Line 883  int editor_display(EDITOR_DATA *p_editor
883                                          break;                                          break;
884                                  case Ctrl('E'): // End of line                                  case Ctrl('E'): // End of line
885                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
886                                            if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
887                                            {
888                                                    // last display line does NOT have \n in the end
889                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
890                                                    break;
891                                            }
892                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
893                                          break;                                          break;
894                                  case Ctrl('T'): // Top of screen                                  case Ctrl('T'): // Top of screen
# Line 899  int editor_display(EDITOR_DATA *p_editor Line 949  int editor_display(EDITOR_DATA *p_editor
949                                          if (col_pos > 1)                                          if (col_pos > 1)
950                                          {                                          {
951                                                  col_pos--;                                                  col_pos--;
952                                                    if (col_pos > 1 &&
953                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
954                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK
955                                                    {
956                                                            col_pos--;
957                                                    }
958                                                  break;                                                  break;
959                                          }                                          }
960                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 926  int editor_display(EDITOR_DATA *p_editor Line 982  int editor_display(EDITOR_DATA *p_editor
982                                  case KEY_RIGHT:                                  case KEY_RIGHT:
983                                          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])
984                                          {                                          {
985                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
986                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK
987                                                    {
988                                                            col_pos++;
989                                                    }
990                                                  col_pos++;                                                  col_pos++;
991                                                  break;                                                  break;
992                                          }                                          }
# Line 937  int editor_display(EDITOR_DATA *p_editor Line 998  int editor_display(EDITOR_DATA *p_editor
998                                                  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]));
999                                                  break;                                                  break;
1000                                          }                                          }
1001                                          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
1002                                          {                                          {
1003                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
1004                                                  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 1004  int editor_display(EDITOR_DATA *p_editor Line 1065  int editor_display(EDITOR_DATA *p_editor
1065                                          break;                                          break;
1066                                  }                                  }
1067    
1068                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(NULL);
1069    
1070                                  if (input_ok)                                  if (input_ok)
1071                                  {                                  {
1072                                          break;                                          break;
1073                                  }                                  }
1074    
1075                                    ch = igetch_t(MAX_DELAY_TIME);
1076                          }                          }
1077    
1078                          continue;                          continue;


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

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