/[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.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 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 564  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 609  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 663  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 680  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 706  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 764  int editor_display(EDITOR_DATA *p_editor Line 766  int editor_display(EDITOR_DATA *p_editor
766                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos                                                  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;
781                                          continue;                                          continue;
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    
# Line 832  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 1032  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