/[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.28 by sysadm, Tue Jun 17 13:17:04 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 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 + 1);          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 362  int editor_data_insert(EDITOR_DATA *p_ed Line 360  int editor_data_insert(EDITOR_DATA *p_ed
360          }          }
361    
362          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
363          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);
364    
365          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
366          {          {
# Line 375  int editor_data_insert(EDITOR_DATA *p_ed Line 373  int editor_data_insert(EDITOR_DATA *p_ed
373                                  // Terminate prior display line with \n, to avoid error on cleanup                                  // Terminate prior display line with \n, to avoid error on cleanup
374                                  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)
375                                  {                                  {
376                                          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);
377                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
378                                          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';
379                                          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 420  int editor_data_insert(EDITOR_DATA *p_ed
420          // Prevent the last display line from being over-length          // Prevent the last display line from being over-length
421          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
422          {          {
423                  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);
424                  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';
425                  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;
426                  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 433  int editor_data_insert(EDITOR_DATA *p_ed
433          return 0;          return 0;
434  }  }
435    
436  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,
437                                             long *p_last_updated_line)                                             long *p_last_updated_line)
438  {  {
439            long display_line = *p_display_line;
440            long offset = *p_offset;
441          char *p_data_line = NULL;          char *p_data_line = NULL;
442          long len_data_line;          long len_data_line;
443          long offset_data_line;          long offset_data_line;
# Line 456  int editor_data_delete(EDITOR_DATA *p_ed Line 456  int editor_data_delete(EDITOR_DATA *p_ed
456          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
457          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
458          {          {
459                  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
460                  {                  {
461                          i++;                          i++;
462                  }                  }
# Line 504  int editor_data_delete(EDITOR_DATA *p_ed Line 504  int editor_data_delete(EDITOR_DATA *p_ed
504          {          {
505                  str_len = 1;                  str_len = 1;
506          }          }
507          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
508          {          {
509                  str_len = 2;                  str_len = 2;
510          }          }
# Line 562  int editor_data_delete(EDITOR_DATA *p_ed Line 562  int editor_data_delete(EDITOR_DATA *p_ed
562          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
563    
564          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
565          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);
566    
567          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
568          {          {
# Line 595  int editor_data_delete(EDITOR_DATA *p_ed Line 595  int editor_data_delete(EDITOR_DATA *p_ed
595                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
596          }          }
597    
598            // Return real offset
599            *p_offset = offset;
600    
601          return str_len;          return str_len;
602  }  }
603    
# Line 604  static int editor_display_key_handler(in Line 607  static int editor_display_key_handler(in
607          {          {
608          case 0: // Set msg          case 0: // Set msg
609                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
610                                   "| 退出[\033[32mCtrl-W\033[33m] | 帮助[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
611                    break;
612            case KEY_CSI:
613                    *p_key = KEY_ESC;
614                  break;                  break;
615          }          }
616    
# Line 658  int editor_display(EDITOR_DATA *p_editor Line 664  int editor_display(EDITOR_DATA *p_editor
664                                           "%s",                                           "%s",
665                                           row_pos, col_pos,                                           row_pos, col_pos,
666                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
667                                           key_insert ? "插入" : "改写",                                           key_insert ? "插入" : "替换",
668                                           ctx.msg);                                           ctx.msg);
669    
670                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
671                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
672                          {                          {
673                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 675  int editor_display(EDITOR_DATA *p_editor Line 681  int editor_display(EDITOR_DATA *p_editor
681                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
682                          iflush();                          iflush();
683    
684                            str_len = 0;
685                          input_ok = 0;                          input_ok = 0;
686                            ch = igetch_t(MAX_DELAY_TIME);
687                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
688                          {                          {
                                 ch = igetch_t(MAX_DELAY_TIME);  
                                 input_ok = 1;  
   
689                                  // extended key handler                                  // extended key handler
690                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
691                                  {                                  {
# Line 701  int editor_display(EDITOR_DATA *p_editor Line 706  int editor_display(EDITOR_DATA *p_editor
706                                  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
707                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
708                                  {                                  {
709                                            BBS_last_access_tm = time(NULL);
710    
711                                          if (str_len == 0) // ch >= 32 && ch < 127                                          if (str_len == 0) // ch >= 32 && ch < 127
712                                          {                                          {
713                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
# Line 715  int editor_display(EDITOR_DATA *p_editor Line 722  int editor_display(EDITOR_DATA *p_editor
722    
723                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
724                                          {                                          {
725                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
726                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line) < 0)
727                                                  {                                                  {
728                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
# Line 756  int editor_display(EDITOR_DATA *p_editor Line 763  int editor_display(EDITOR_DATA *p_editor
763                                                  {                                                  {
764                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
765                                                  }                                                  }
766                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
767                                            }
768    
769                                            if (display_line_out != display_line_in) // Output on line change
770                                            {
771                                                    break;
772                                            }
773    
774                                            ch = igetch(0);
775                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
776                                            {
777                                                    break;
778                                          }                                          }
779    
780                                          str_len = 0;                                          str_len = 0;
# Line 764  int editor_display(EDITOR_DATA *p_editor Line 782  int editor_display(EDITOR_DATA *p_editor
782                                  }                                  }
783                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
784                                  {                                  {
785                                            BBS_last_access_tm = time(NULL);
786    
787                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
788                                          {                                          {
789                                                  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
790                                                  {                                                  {
791                                                          input_ok = 0;                                                          ch = igetch_t(MAX_DELAY_TIME);
792                                                          continue;                                                          continue;
793                                                  }                                                  }
794    
795                                                  col_pos--;                                                  col_pos--;
796                                                    if (col_pos > 1 &&
797                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK
798                                                    {
799                                                            col_pos--;
800                                                    }
801    
802                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
803                                                  {                                                  {
804                                                          row_pos--;                                                          row_pos--;
# Line 780  int editor_display(EDITOR_DATA *p_editor Line 806  int editor_display(EDITOR_DATA *p_editor
806                                                  }                                                  }
807                                          }                                          }
808    
809                                          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;
810                                            offset_in = col_pos - 1;
811                                            display_line_out = display_line_in;
812                                            offset_out = offset_in;
813    
814                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
815                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line)) < 0)
816                                          {                                          {
817                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
818                                          }                                          }
819                                          else                                          else
820                                          {                                          {
821                                                  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]);  
                                                                 }  
                                                         }  
                                                 }  
822    
823                                                  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));
824                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
# Line 827  int editor_display(EDITOR_DATA *p_editor Line 847  int editor_display(EDITOR_DATA *p_editor
847                                                  clrline(output_current_row, output_end_row);                                                  clrline(output_current_row, output_end_row);
848                                          }                                          }
849    
850                                            if (display_line_out != display_line_in) // Output on line change
851                                            {
852                                                    break;
853                                            }
854    
855                                            ch = igetch(0);
856                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
857                                            {
858                                                    break;
859                                            }
860    
861                                          str_len = 0;                                          str_len = 0;
862                                          continue;                                          continue;
863                                  }                                  }
864    
865                                    input_ok = 1;
866                                  switch (ch)                                  switch (ch)
867                                  {                                  {
868                                  case KEY_NULL:                                  case KEY_NULL:
# Line 845  int editor_display(EDITOR_DATA *p_editor Line 877  int editor_display(EDITOR_DATA *p_editor
877                                          break;                                          break;
878                                  case Ctrl('E'): // End of line                                  case Ctrl('E'): // End of line
879                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
880                                          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
881                                          {                                          {
882                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
883                                                  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 943  int editor_display(EDITOR_DATA *p_editor
943                                          if (col_pos > 1)                                          if (col_pos > 1)
944                                          {                                          {
945                                                  col_pos--;                                                  col_pos--;
946                                                    if (col_pos > 1 &&
947                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
948                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK
949                                                    {
950                                                            col_pos--;
951                                                    }
952                                                  break;                                                  break;
953                                          }                                          }
954                                          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 976  int editor_display(EDITOR_DATA *p_editor
976                                  case KEY_RIGHT:                                  case KEY_RIGHT:
977                                          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])
978                                          {                                          {
979                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
980                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK
981                                                    {
982                                                            col_pos++;
983                                                    }
984                                                  col_pos++;                                                  col_pos++;
985                                                  break;                                                  break;
986                                          }                                          }
# Line 949  int editor_display(EDITOR_DATA *p_editor Line 992  int editor_display(EDITOR_DATA *p_editor
992                                                  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]));
993                                                  break;                                                  break;
994                                          }                                          }
995                                          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
996                                          {                                          {
997                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
998                                                  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 1059  int editor_display(EDITOR_DATA *p_editor
1059                                          break;                                          break;
1060                                  }                                  }
1061    
1062                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(NULL);
1063    
1064                                  if (input_ok)                                  if (input_ok)
1065                                  {                                  {
1066                                          break;                                          break;
1067                                  }                                  }
1068    
1069                                    ch = igetch_t(MAX_DELAY_TIME);
1070                          }                          }
1071    
1072                          continue;                          continue;


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

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