/[LeafOK_CVS]/lbbs/src/editor.c
ViewVC logotype

Diff of /lbbs/src/editor.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.23 by sysadm, Sun Jun 15 13:40:52 2025 UTC Revision 1.31 by sysadm, Sat Jun 21 02:15:18 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "editor.h"  
17  #include "bbs.h"  #include "bbs.h"
18    #include "common.h"
19    #include "editor.h"
20  #include "io.h"  #include "io.h"
21  #include "log.h"  #include "log.h"
 #include "common.h"  
 #include "str_process.h"  
22  #include "memory_pool.h"  #include "memory_pool.h"
23    #include "str_process.h"
24  #include <stdlib.h>  #include <stdlib.h>
 #include <sys/param.h>  
 #include <strings.h>  
   
 #define _POSIX_C_SOURCE 200809L  
25  #include <string.h>  #include <string.h>
26    #include <sys/param.h>
27    
28  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
29  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000
# Line 96  EDITOR_DATA *editor_data_load(const char Line 93  EDITOR_DATA *editor_data_load(const char
93                  return NULL;                  return NULL;
94          }          }
95    
96          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1);          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, 0);
97    
98          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
99          {          {
# Line 138  EDITOR_DATA *editor_data_load(const char Line 135  EDITOR_DATA *editor_data_load(const char
135                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
136          }          }
137    
138          bzero(p_editor_data->p_display_lines + p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);          memset(p_editor_data->p_display_lines + p_editor_data->display_line_total, 0, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);
139    
140          return p_editor_data;          return p_editor_data;
141  }  }
# Line 281  int editor_data_insert(EDITOR_DATA *p_ed Line 278  int editor_data_insert(EDITOR_DATA *p_ed
278          {          {
279                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
280                  {                  {
281                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",  #ifdef _DEBUG
282                          //                p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                          log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
283                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
284    #endif
285    
286                          return -2;                          return -2;
287                  }                  }
288    
# Line 362  int editor_data_insert(EDITOR_DATA *p_ed Line 362  int editor_data_insert(EDITOR_DATA *p_ed
362          }          }
363    
364          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
365          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);
366    
367          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
368          {          {
# Line 371  int editor_data_insert(EDITOR_DATA *p_ed Line 371  int editor_data_insert(EDITOR_DATA *p_ed
371                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
372                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
373                          {                          {
374                                  // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);  #ifdef _DEBUG
375                                    log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
376    #endif
377    
378                                  // Terminate prior display line with \n, to avoid error on cleanup                                  // Terminate prior display line with \n, to avoid error on cleanup
379                                  if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)                                  if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
380                                  {                                  {
381                                          len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len);                                          len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
382                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
383                                          p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';                                          p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
384                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
# Line 422  int editor_data_insert(EDITOR_DATA *p_ed Line 425  int editor_data_insert(EDITOR_DATA *p_ed
425          // Prevent the last display line from being over-length          // Prevent the last display line from being over-length
426          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
427          {          {
428                  len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len);                  len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
429                  p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';                  p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
430                  p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;                  p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
431                  if (*p_display_line + 1 >= p_editor_data->display_line_total)                  if (*p_display_line + 1 >= p_editor_data->display_line_total)
# Line 564  int editor_data_delete(EDITOR_DATA *p_ed Line 567  int editor_data_delete(EDITOR_DATA *p_ed
567          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
568    
569          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
570          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);
571    
572          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
573          {          {
# Line 609  static int editor_display_key_handler(in Line 612  static int editor_display_key_handler(in
612          {          {
613          case 0: // Set msg          case 0: // Set msg
614                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
615                                   "| 退出[\033[32mCtrl-W\033[33m] | 帮助[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
616                    break;
617            case KEY_CSI:
618                    *p_key = KEY_ESC;
619                  break;                  break;
620          }          }
621    
# Line 663  int editor_display(EDITOR_DATA *p_editor Line 669  int editor_display(EDITOR_DATA *p_editor
669                                           "%s",                                           "%s",
670                                           row_pos, col_pos,                                           row_pos, col_pos,
671                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
672                                           key_insert ? "插入" : "改写",                                           key_insert ? "插入" : "替换",
673                                           ctx.msg);                                           ctx.msg);
674    
675                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
676                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
677                          {                          {
678                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 680  int editor_display(EDITOR_DATA *p_editor Line 686  int editor_display(EDITOR_DATA *p_editor
686                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
687                          iflush();                          iflush();
688    
689                            str_len = 0;
690                          input_ok = 0;                          input_ok = 0;
691                            ch = igetch_t(MAX_DELAY_TIME);
692                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
693                          {                          {
                                 ch = igetch_t(MAX_DELAY_TIME);  
                                 input_ok = 1;  
   
694                                  // extended key handler                                  // extended key handler
695                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
696                                  {                                  {
# Line 706  int editor_display(EDITOR_DATA *p_editor Line 711  int editor_display(EDITOR_DATA *p_editor
711                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK
712                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
713                                  {                                  {
714                                            BBS_last_access_tm = time(NULL);
715    
716                                          if (str_len == 0) // ch >= 32 && ch < 127                                          if (str_len == 0) // ch >= 32 && ch < 127
717                                          {                                          {
718                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
# Line 764  int editor_display(EDITOR_DATA *p_editor Line 771  int editor_display(EDITOR_DATA *p_editor
771                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
772                                          }                                          }
773    
774                                            if (display_line_out != display_line_in) // Output on line change
775                                            {
776                                                    break;
777                                            }
778    
779                                            ch = igetch(0);
780                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
781                                            {
782                                                    break;
783                                            }
784    
785                                          str_len = 0;                                          str_len = 0;
786                                          continue;                                          continue;
787                                  }                                  }
788                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
789                                  {                                  {
790                                            BBS_last_access_tm = time(NULL);
791    
792                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
793                                          {                                          {
794                                                  if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden                                                  if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
795                                                  {                                                  {
796                                                          input_ok = 0;                                                          ch = igetch_t(MAX_DELAY_TIME);
797                                                          continue;                                                          continue;
798                                                  }                                                  }
799    
# Line 832  int editor_display(EDITOR_DATA *p_editor Line 852  int editor_display(EDITOR_DATA *p_editor
852                                                  clrline(output_current_row, output_end_row);                                                  clrline(output_current_row, output_end_row);
853                                          }                                          }
854    
855                                            if (display_line_out != display_line_in) // Output on line change
856                                            {
857                                                    break;
858                                            }
859    
860                                            ch = igetch(0);
861                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
862                                            {
863                                                    break;
864                                            }
865    
866                                          str_len = 0;                                          str_len = 0;
867                                          continue;                                          continue;
868                                  }                                  }
869    
870                                    input_ok = 1;
871                                  switch (ch)                                  switch (ch)
872                                  {                                  {
873                                  case KEY_NULL:                                  case KEY_NULL:
# Line 1032  int editor_display(EDITOR_DATA *p_editor Line 1064  int editor_display(EDITOR_DATA *p_editor
1064                                          break;                                          break;
1065                                  }                                  }
1066    
1067                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(NULL);
1068    
1069                                  if (input_ok)                                  if (input_ok)
1070                                  {                                  {
1071                                          break;                                          break;
1072                                  }                                  }
1073    
1074                                    ch = igetch_t(MAX_DELAY_TIME);
1075                          }                          }
1076    
1077                          continue;                          continue;


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

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