/[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.27 by sysadm, Tue Jun 17 02:06:48 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _POSIX_C_SOURCE 200809L
18    
19  #include "editor.h"  #include "editor.h"
20  #include "bbs.h"  #include "bbs.h"
21  #include "io.h"  #include "io.h"
# Line 24  Line 26 
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <sys/param.h>  #include <sys/param.h>
28  #include <strings.h>  #include <strings.h>
   
 #define _POSIX_C_SOURCE 200809L  
29  #include <string.h>  #include <string.h>
30    
31  #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 96  EDITOR_DATA *editor_data_load(const char
96                  return NULL;                  return NULL;
97          }          }
98    
99          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);
100    
101          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
102          {          {
# Line 138  EDITOR_DATA *editor_data_load(const char Line 138  EDITOR_DATA *editor_data_load(const char
138                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
139          }          }
140    
141          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);
142    
143          return p_editor_data;          return p_editor_data;
144  }  }
# 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 375  int editor_data_insert(EDITOR_DATA *p_ed Line 375  int editor_data_insert(EDITOR_DATA *p_ed
375                                  // Terminate prior display line with \n, to avoid error on cleanup                                  // Terminate prior display line with \n, to avoid error on cleanup
376                                  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)
377                                  {                                  {
378                                          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);
379                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
380                                          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';
381                                          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 422  int editor_data_insert(EDITOR_DATA *p_ed
422          // Prevent the last display line from being over-length          // Prevent the last display line from being over-length
423          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
424          {          {
425                  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);
426                  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';
427                  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;
428                  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 564  int editor_data_delete(EDITOR_DATA *p_ed
564          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
565    
566          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
567          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);
568    
569          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
570          {          {
# Line 609  static int editor_display_key_handler(in Line 609  static int editor_display_key_handler(in
609          {          {
610          case 0: // Set msg          case 0: // Set msg
611                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
612                                   "| 退出[\033[32mCtrl-W\033[33m] | 帮助[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
613                    break;
614            case KEY_CSI:
615                    *p_key = KEY_ESC;
616                  break;                  break;
617          }          }
618    
# Line 663  int editor_display(EDITOR_DATA *p_editor Line 666  int editor_display(EDITOR_DATA *p_editor
666                                           "%s",                                           "%s",
667                                           row_pos, col_pos,                                           row_pos, col_pos,
668                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
669                                           key_insert ? "插入" : "改写",                                           key_insert ? "插入" : "替换",
670                                           ctx.msg);                                           ctx.msg);
671    
672                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
673                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
674                          {                          {
675                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 680  int editor_display(EDITOR_DATA *p_editor Line 683  int editor_display(EDITOR_DATA *p_editor
683                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
684                          iflush();                          iflush();
685    
686                            str_len = 0;
687                          input_ok = 0;                          input_ok = 0;
688                            ch = igetch_t(MAX_DELAY_TIME);
689                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
690                          {                          {
                                 ch = igetch_t(MAX_DELAY_TIME);  
                                 input_ok = 1;  
   
691                                  // extended key handler                                  // extended key handler
692                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
693                                  {                                  {
# Line 706  int editor_display(EDITOR_DATA *p_editor Line 708  int editor_display(EDITOR_DATA *p_editor
708                                  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
709                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
710                                  {                                  {
711                                            BBS_last_access_tm = time(NULL);
712    
713                                          if (str_len == 0) // ch >= 32 && ch < 127                                          if (str_len == 0) // ch >= 32 && ch < 127
714                                          {                                          {
715                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
# Line 764  int editor_display(EDITOR_DATA *p_editor Line 768  int editor_display(EDITOR_DATA *p_editor
768                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
769                                          }                                          }
770    
771                                            if (display_line_out != display_line_in) // Output on line change
772                                            {
773                                                    break;
774                                            }
775    
776                                            ch = igetch(0);
777                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
778                                            {
779                                                    break;
780                                            }
781    
782                                          str_len = 0;                                          str_len = 0;
783                                          continue;                                          continue;
784                                  }                                  }
785                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
786                                  {                                  {
787                                            BBS_last_access_tm = time(NULL);
788    
789                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
790                                          {                                          {
791                                                  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
792                                                  {                                                  {
793                                                          input_ok = 0;                                                          ch = igetch_t(MAX_DELAY_TIME);
794                                                          continue;                                                          continue;
795                                                  }                                                  }
796    
# Line 832  int editor_display(EDITOR_DATA *p_editor Line 849  int editor_display(EDITOR_DATA *p_editor
849                                                  clrline(output_current_row, output_end_row);                                                  clrline(output_current_row, output_end_row);
850                                          }                                          }
851    
852                                            if (display_line_out != display_line_in) // Output on line change
853                                            {
854                                                    break;
855                                            }
856    
857                                            ch = igetch(0);
858                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
859                                            {
860                                                    break;
861                                            }
862    
863                                          str_len = 0;                                          str_len = 0;
864                                          continue;                                          continue;
865                                  }                                  }
866    
867                                    input_ok = 1;
868                                  switch (ch)                                  switch (ch)
869                                  {                                  {
870                                  case KEY_NULL:                                  case KEY_NULL:
# Line 1032  int editor_display(EDITOR_DATA *p_editor Line 1061  int editor_display(EDITOR_DATA *p_editor
1061                                          break;                                          break;
1062                                  }                                  }
1063    
1064                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(NULL);
1065    
1066                                  if (input_ok)                                  if (input_ok)
1067                                  {                                  {
1068                                          break;                                          break;
1069                                  }                                  }
1070    
1071                                    ch = igetch_t(MAX_DELAY_TIME);
1072                          }                          }
1073    
1074                          continue;                          continue;


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

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