/[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.48 by sysadm, Sat Oct 18 12:06:10 2025 UTC Revision 1.58 by sysadm, Mon Nov 10 07:23:16 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                     editor.h  -  description  /*
3                                                           -------------------   * editor
4          copyright            : (C) 2004-2025 by Leaflet   *   - user interactive full-screen text editor
5          email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
# Line 24  Line 16 
16  #include "str_process.h"  #include "str_process.h"
17  #include <stdlib.h>  #include <stdlib.h>
18  #include <string.h>  #include <string.h>
19    #include <wchar.h>
20  #include <sys/param.h>  #include <sys/param.h>
21    
22  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  enum _editor_constant_t
23  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000  {
24  #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)          EDITOR_MEM_POOL_LINE_PER_CHUNK = 1000,
25            EDITOR_MEM_POOL_CHUNK_LIMIT = (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1),
26            TAB_SIZE = 4,
27    };
28    
29    static const char EDITOR_ESC_DISPLAY_STR[] = "\033[32m*\033[m";
30    
31  static MEMORY_POOL *p_mp_data_line;  static MEMORY_POOL *p_mp_data_line;
32  static MEMORY_POOL *p_mp_editor_data;  static MEMORY_POOL *p_mp_editor_data;
# Line 80  EDITOR_DATA *editor_data_load(const char Line 78  EDITOR_DATA *editor_data_load(const char
78          long line_offsets[MAX_EDITOR_DATA_LINES + 1];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
79          long current_data_line_length = 0;          long current_data_line_length = 0;
80          long i;          long i;
81            int j;
82    
83          if (p_data == NULL)          if (p_data == NULL)
84          {          {
# Line 126  EDITOR_DATA *editor_data_load(const char Line 125  EDITOR_DATA *editor_data_load(const char
125                  memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]);                  memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]);
126                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
127    
128                    // Convert \t to single space
129                    for (j = 0; j < p_editor_data->display_line_lengths[i]; j++)
130                    {
131                            if (p_editor_data->p_display_lines[i][j] == '\t')
132                            {
133                                    p_editor_data->p_display_lines[i][j] = ' ';
134                            }
135                    }
136    
137                  // Trim \n from last line                  // Trim \n from last line
138                  if (i + 1 == p_editor_data->display_line_total &&                  if (i + 1 == p_editor_data->display_line_total &&
139                          p_editor_data->display_line_lengths[i] > 0 &&                          p_editor_data->display_line_lengths[i] > 0 &&
# Line 445  int editor_data_insert(EDITOR_DATA *p_ed Line 453  int editor_data_insert(EDITOR_DATA *p_ed
453  }  }
454    
455  int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,  int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
456                                             long *p_last_updated_line)                                             long *p_last_updated_line, int del_line)
457  {  {
458          long display_line = *p_display_line;          long display_line = *p_display_line;
459          long offset = *p_offset;          long offset = *p_offset;
# Line 500  int editor_data_delete(EDITOR_DATA *p_ed Line 508  int editor_data_delete(EDITOR_DATA *p_ed
508          }          }
509    
510          // Check str to be deleted          // Check str to be deleted
511          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (del_line)
512            {
513                    str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
514            }
515            else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
516          {          {
517                  str_len = 1;                  str_len = 1;
518          }          }
# Line 523  int editor_data_delete(EDITOR_DATA *p_ed Line 535  int editor_data_delete(EDITOR_DATA *p_ed
535    
536          // Current display line is (almost) empty          // Current display line is (almost) empty
537          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
538                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line &&
539                     p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
540          {          {
541                  if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)                  if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
542                  {                  {
# Line 627  static int editor_display_key_handler(in Line 640  static int editor_display_key_handler(in
640          {          {
641          case 0: // Set msg          case 0: // Set msg
642                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
643                                   "| 退出[\033[32mCtrl-W\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]",
644                                     (UTF8_fixed_width ? "定宽" : "变宽"));
645                  break;                  break;
646          case KEY_CSI:          case KEY_CSI:
647                  *p_key = KEY_ESC;                  *p_key = KEY_ESC;
# Line 643  int editor_display(EDITOR_DATA *p_editor Line 657  int editor_display(EDITOR_DATA *p_editor
657          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
658          EDITOR_CTX ctx;          EDITOR_CTX ctx;
659          int ch = 0;          int ch = 0;
660          char input_str[4];          char input_str[5];
         char c;  
661          int str_len = 0;          int str_len = 0;
662            wchar_t wcs[2];
663            int wc_len;
664            char c;
665          int input_ok;          int input_ok;
666          const int screen_begin_row = 1;          const int screen_begin_row = 1;
667          const int screen_row_total = SCREEN_ROWS - screen_begin_row;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
# Line 663  int editor_display(EDITOR_DATA *p_editor Line 679  int editor_display(EDITOR_DATA *p_editor
679          int key_insert = 1;          int key_insert = 1;
680          int i, j;          int i, j;
681          char *p_str;          char *p_str;
682            int del_line;
683            int tab_width = 0;
684    
685          clrline(output_current_row, SCREEN_ROWS);          clrline(output_current_row, SCREEN_ROWS);
686    
# Line 701  int editor_display(EDITOR_DATA *p_editor Line 719  int editor_display(EDITOR_DATA *p_editor
719                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
720                          iflush();                          iflush();
721    
722                            tab_width = 0;
723                          str_len = 0;                          str_len = 0;
724                          ch = igetch_t(MAX_DELAY_TIME);                          ch = igetch_t(BBS_max_user_idle_time);
725                          while (!SYS_server_exit)                          while (!SYS_server_exit)
726                          {                          {
727                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
# Line 716  int editor_display(EDITOR_DATA *p_editor Line 735  int editor_display(EDITOR_DATA *p_editor
735                                          goto cleanup;                                          goto cleanup;
736                                  }                                  }
737    
738                                    if (ch == '\t')
739                                    {
740                                            ch = ' ';
741                                            tab_width = TAB_SIZE - ((int)(col_pos - 1) % TAB_SIZE) - 1;
742                                    }
743    
744                                  if (ch < 256 && (ch & 0x80)) // head of multi-byte character                                  if (ch < 256 && (ch & 0x80)) // head of multi-byte character
745                                  {                                  {
746                                          str_len = 0;                                          str_len = 0;
# Line 742  int editor_display(EDITOR_DATA *p_editor Line 767  int editor_display(EDITOR_DATA *p_editor
767                                                          break;                                                          break;
768                                                  }                                                  }
769                                          }                                          }
770                                            input_str[str_len] = '\0';
771                                  }                                  }
772    
773                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character
# Line 769  int editor_display(EDITOR_DATA *p_editor Line 795  int editor_display(EDITOR_DATA *p_editor
795                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
796                                          {                                          {
797                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
798                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line, 0) < 0)
799                                                  {                                                  {
800                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
801                                                  }                                                  }
# Line 819  int editor_display(EDITOR_DATA *p_editor Line 845  int editor_display(EDITOR_DATA *p_editor
845                                                          }                                                          }
846                                                          if (offset_out > 0)                                                          if (offset_out > 0)
847                                                          {                                                          {
848                                                                  col_pos += (str_len == 1 ? 1 : 2);                                                                  if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
849                                                                    {
850                                                                            log_error("mbstowcs() error\n");
851                                                                    }
852                                                                    col_pos += (str_len == 1 ? 1 : (UTF8_fixed_width ? 2 : wcwidth(wcs[0])));
853                                                          }                                                          }
854                                                  }                                                  }
855                                          }                                          }
# Line 829  int editor_display(EDITOR_DATA *p_editor Line 859  int editor_display(EDITOR_DATA *p_editor
859                                                  break;                                                  break;
860                                          }                                          }
861    
862                                          ch = igetch(0);                                          if (ch == ' ' && tab_width > 0)
                                         if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input  
863                                          {                                          {
864                                                  break;                                                  tab_width--;
865                                            }
866                                            else
867                                            {
868                                                    ch = igetch(0);
869                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
870                                                    {
871                                                            break;
872                                                    }
873                                          }                                          }
874    
875                                          str_len = 0;                                          str_len = 0;
876                                          continue;                                          continue;
877                                  }                                  }
878                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
879                                  {                                  {
880                                          // Refresh current action while user input                                          // Refresh current action while user input
881                                          if (user_online_update(NULL) < 0)                                          if (user_online_update(NULL) < 0)
# Line 846  int editor_display(EDITOR_DATA *p_editor Line 883  int editor_display(EDITOR_DATA *p_editor
883                                                  log_error("user_online_update(NULL) error\n");                                                  log_error("user_online_update(NULL) error\n");
884                                          }                                          }
885    
886                                            del_line = 0;
887    
888                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
889                                          {                                          {
890                                                  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
# Line 855  int editor_display(EDITOR_DATA *p_editor Line 894  int editor_display(EDITOR_DATA *p_editor
894    
895                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
896                                                                                             (int)col_pos - 1, &eol, &display_len, 0);                                                                                             (int)col_pos - 1, &eol, &display_len, 0);
897                                                  if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8                                                  col_pos = display_len;
                                                 {  
                                                         col_pos = display_len - 1;  
                                                 }  
                                                 else  
                                                 {  
                                                         col_pos = display_len;  
                                                 }  
   
898                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
899                                                  {                                                  {
900                                                          row_pos--;                                                          row_pos--;
901                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
902                                                  }                                                  }
903                                          }                                          }
904                                            else if (ch == Ctrl('K'))
905                                            {
906                                                    del_line = 1;
907                                            }
908                                            else if (ch == Ctrl('Y'))
909                                            {
910                                                    col_pos = 1;
911                                                    del_line = 1;
912                                            }
913    
914                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
915                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
# Line 877  int editor_display(EDITOR_DATA *p_editor Line 917  int editor_display(EDITOR_DATA *p_editor
917                                          offset_out = offset_in;                                          offset_out = offset_in;
918    
919                                          if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,                                          if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
920                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line, del_line)) < 0)
921                                          {                                          {
922                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error: %d\n", str_len);
923                                          }                                          }
924                                          else                                          else
925                                          {                                          {
# Line 1011  int editor_display(EDITOR_DATA *p_editor Line 1051  int editor_display(EDITOR_DATA *p_editor
1051                                  case KEY_LEFT:                                  case KEY_LEFT:
1052                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1053                                                                                     (int)col_pos - 1, &eol, &display_len, 0);                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1054                                          if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8                                          col_pos = display_len;
1055                                          {                                          if (offset_in > 0)
                                                 col_pos = display_len - 1;  
                                         }  
                                         else  
1056                                          {                                          {
1057                                                  col_pos = display_len;                                                  str_len = 1;
1058                                                    offset_in--;
1059                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0 ||
1060                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] > 127) // UTF8
1061                                                    {
1062                                                            while (offset_in > 0 &&
1063                                                                       (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xc0) != 0xc0)
1064                                                            {
1065                                                                    str_len++;
1066                                                                    offset_in--;
1067                                                            }
1068    
1069                                                            if (str_len > 4)
1070                                                            {
1071                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1072                                                            }
1073    
1074                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1075                                                                    (size_t)-1)
1076                                                            {
1077                                                                    log_error("mbstowcs() error\n");
1078                                                            }
1079                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1080    
1081                                                            if (wc_len == 2)
1082                                                            {
1083                                                                    col_pos--;
1084                                                            }
1085                                                    }
1086                                          }                                          }
1087                                          if (col_pos >= 1)                                          if (col_pos >= 1)
1088                                          {                                          {
# Line 1043  int editor_display(EDITOR_DATA *p_editor Line 1108  int editor_display(EDITOR_DATA *p_editor
1108                                          output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line                                          output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
1109                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1110                                          break;                                          break;
                                 case KEY_SPACE:  
                                         break;  
1111                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1112                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1113                                                                                     (int)col_pos - 1, &eol, &display_len, 0);                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1114                                          if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&                                          col_pos = display_len + 2;
1115                                                  p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8                                          if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
1116                                          {                                          {
1117                                                  col_pos = display_len + 3;                                                  str_len = 0;
1118                                          }                                                  if ((p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0x80) ==
1119                                          else                                                          0x80) // head of multi-byte character
1120                                          {                                                  {
1121                                                  col_pos = display_len + 2;                                                          c = (char)(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xf0);
1122                                                            while (c & 0x80)
1123                                                            {
1124                                                                    str_len++;
1125                                                                    c = (c & 0x7f) << 1;
1126                                                            }
1127    
1128                                                            if (str_len > 4)
1129                                                            {
1130                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1131                                                            }
1132    
1133                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1134                                                                    (size_t)-1)
1135                                                            {
1136                                                                    log_error("mbstowcs() error\n");
1137                                                            }
1138                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1139    
1140                                                            if (wc_len == 2)
1141                                                            {
1142                                                                    col_pos++;
1143                                                            }
1144                                                    }
1145                                                    else
1146                                                    {
1147                                                            str_len = 1;
1148                                                    }
1149                                                    offset_in += str_len;
1150                                          }                                          }
1151                                          if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])                                          if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1152                                          {                                          {
# Line 1116  int editor_display(EDITOR_DATA *p_editor Line 1207  int editor_display(EDITOR_DATA *p_editor
1207                                          break;                                          break;
1208                                  case Ctrl('Q'):                                  case Ctrl('Q'):
1209                                  case KEY_F1:                                  case KEY_F1:
1210                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not re-entrant
1211                                          {                                          {
1212                                                  break;                                                  break;
1213                                          }                                          }
1214                                          // Display help information                                          // Display help information
1215                                          show_help = 0;                                          show_help = 0;
1216                                          display_file(DATA_READ_HELP, 1);                                          display_file(DATA_EDITOR_HELP, 1);
1217                                          show_help = 1;                                          show_help = 1;
1218                                  case KEY_F5:                                  case KEY_F5:
1219                                          // Refresh after display help information                                          // Refresh after display help information
# Line 1149  int editor_display(EDITOR_DATA *p_editor Line 1240  int editor_display(EDITOR_DATA *p_editor
1240                                          break;                                          break;
1241                                  }                                  }
1242    
1243                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(BBS_max_user_idle_time);
1244                          }                          }
1245    
1246                          continue;                          continue;


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

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