/[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.31 by sysadm, Sat Jun 21 02:15:18 2025 UTC Revision 1.49 by sysadm, Sun Oct 19 07:08:29 2025 UTC
# Line 19  Line 19 
19  #include "editor.h"  #include "editor.h"
20  #include "io.h"  #include "io.h"
21  #include "log.h"  #include "log.h"
22    #include "login.h"
23  #include "memory_pool.h"  #include "memory_pool.h"
24  #include "str_process.h"  #include "str_process.h"
25  #include <stdlib.h>  #include <stdlib.h>
# Line 48  int editor_memory_pool_init(void) Line 49  int editor_memory_pool_init(void)
49          }          }
50    
51          p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);          p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
52          if (p_mp_data_line == NULL)          if (p_mp_editor_data == NULL)
53          {          {
54                  log_error("Memory pool init error\n");                  log_error("Memory pool init error\n");
55                  return -3;                  return -3;
# Line 82  EDITOR_DATA *editor_data_load(const char Line 83  EDITOR_DATA *editor_data_load(const char
83    
84          if (p_data == NULL)          if (p_data == NULL)
85          {          {
86                  log_error("editor_data_load() error: NULL pointer\n");                  log_error("NULL pointer error\n");
87                  return NULL;                  return NULL;
88          }          }
89    
# Line 93  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, 0);          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1,
98                                                                                                                     0, p_editor_data->display_line_widths);
99    
100          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
101          {          {
# Line 130  EDITOR_DATA *editor_data_load(const char Line 132  EDITOR_DATA *editor_data_load(const char
132                          p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')                          p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
133                  {                  {
134                          p_editor_data->display_line_lengths[i]--;                          p_editor_data->display_line_lengths[i]--;
135                            p_editor_data->display_line_widths[i]--;
136                          current_data_line_length--;                          current_data_line_length--;
137                  }                  }
138                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
# Line 147  long editor_data_save(const EDITOR_DATA Line 150  long editor_data_save(const EDITOR_DATA
150    
151          if (p_editor_data == NULL || p_data == NULL)          if (p_editor_data == NULL || p_data == NULL)
152          {          {
153                  log_error("editor_data_save() error: NULL pointer\n");                  log_error("NULL pointer error\n");
154                  return -1;                  return -1;
155          }          }
156    
# Line 212  int editor_data_insert(EDITOR_DATA *p_ed Line 215  int editor_data_insert(EDITOR_DATA *p_ed
215          long offset_data_line;          long offset_data_line;
216          long last_display_line; // of data line          long last_display_line; // of data line
217          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
218            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
219          long split_line_total;          long split_line_total;
220          long i, j;          long i;
221          int len;          int len;
222          int eol;          int eol;
223          int display_len;          int display_len;
224    
225          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
226          {          {
227                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
228                  return -1;                  return -1;
229          }          }
230    
         // Validate str  
         if ((str_len == 1 && str[0] <= 0) ||  
                 (str_len == 2 && (str[0] >= 0 || str[1] >= 0)))  
         {  
                 log_error("Invalid input str, len=%d\n", str_len);  
                 return -2;  
         }  
   
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
231          // Get length of current data line          // Get length of current data line
232          len_data_line = 0;          len_data_line = 0;
233          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 274  int editor_data_insert(EDITOR_DATA *p_ed Line 257  int editor_data_insert(EDITOR_DATA *p_ed
257          }          }
258    
259          // Split current data line if over-length          // Split current data line if over-length
260          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)          if (len_data_line + str_len + 2 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
261          {          {
262                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
263                  {                  {
# Line 294  int editor_data_insert(EDITOR_DATA *p_ed Line 277  int editor_data_insert(EDITOR_DATA *p_ed
277                          return -2;                          return -2;
278                  }                  }
279    
280                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)                  if (offset_data_line + str_len + 2 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
281                  {                  {
282                          if (str[0] == CR)                          if (str[0] == CR)
283                          {                          {
# Line 340  int editor_data_insert(EDITOR_DATA *p_ed Line 323  int editor_data_insert(EDITOR_DATA *p_ed
323                          *p_offset = offset + str_len;                          *p_offset = offset + str_len;
324                  }                  }
325    
326                    // Update display width of current display line
327                    len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0);
328                    p_editor_data->display_line_widths[display_line] = display_len;
329    
330                  split_line_total = last_display_line - display_line + 3;                  split_line_total = last_display_line - display_line + 3;
331    
332                  // Set start display_line for spliting new data line                  // Set start display_line for spliting new data line
# Line 362  int editor_data_insert(EDITOR_DATA *p_ed Line 349  int editor_data_insert(EDITOR_DATA *p_ed
349          }          }
350    
351          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
352          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
353    
354          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
355          {          {
# Line 382  int editor_data_insert(EDITOR_DATA *p_ed Line 369  int editor_data_insert(EDITOR_DATA *p_ed
369                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
370                                          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';
371                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
372                                            p_editor_data->display_line_widths[display_line + i - 1] = display_len;
373                                  }                                  }
374                                  if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])                                  if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
375                                  {                                  {
# Line 389  int editor_data_insert(EDITOR_DATA *p_ed Line 377  int editor_data_insert(EDITOR_DATA *p_ed
377                                  }                                  }
378                                  break;                                  break;
379                          }                          }
380                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)  
381                          {                          // for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
382                                  p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];                          // {
383                                  p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];                          //      p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
384                          }                          //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
385                            //      p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1];
386                            // }
387                            memmove(p_editor_data->p_display_lines + last_display_line + 2,
388                                            p_editor_data->p_display_lines + last_display_line + 1,
389                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
390                                                    sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
391                            memmove(p_editor_data->display_line_lengths + last_display_line + 2,
392                                            p_editor_data->display_line_lengths + last_display_line + 1,
393                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
394                                                    sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
395                            memmove(p_editor_data->display_line_widths + last_display_line + 2,
396                                            p_editor_data->display_line_widths + last_display_line + 1,
397                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
398                                                    sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
399    
400                          last_display_line++;                          last_display_line++;
401                            *p_last_updated_line = p_editor_data->display_line_total;
402                          (p_editor_data->display_line_total)++;                          (p_editor_data->display_line_total)++;
403                  }                  }
404    
405                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
406                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
407                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
408                          (i == 0                          (i == 0
409                                   ? p_data_line                                   ? p_data_line
# Line 428  int editor_data_insert(EDITOR_DATA *p_ed Line 433  int editor_data_insert(EDITOR_DATA *p_ed
433                  len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);                  len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
434                  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';
435                  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;
436                    p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len;
437                  if (*p_display_line + 1 >= p_editor_data->display_line_total)                  if (*p_display_line + 1 >= p_editor_data->display_line_total)
438                  {                  {
439                          *p_offset = MIN(*p_offset, len);                          *p_offset = MIN(*p_offset, len);
# Line 439  int editor_data_insert(EDITOR_DATA *p_ed Line 445  int editor_data_insert(EDITOR_DATA *p_ed
445  }  }
446    
447  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,
448                                             long *p_last_updated_line)                                             long *p_last_updated_line, int del_line)
449  {  {
450          long display_line = *p_display_line;          long display_line = *p_display_line;
451          long offset = *p_offset;          long offset = *p_offset;
# Line 448  int editor_data_delete(EDITOR_DATA *p_ed Line 454  int editor_data_delete(EDITOR_DATA *p_ed
454          long offset_data_line;          long offset_data_line;
455          long last_display_line; // of data line          long last_display_line; // of data line
456          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
457            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
458          long split_line_total;          long split_line_total;
459          long i, j;          long i, j;
460          int str_len = 0;          int str_len = 0;
461            char c;
462    
463          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
464          {          {
465                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
466                  return -1;                  return -1;
467          }          }
468    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
469          // Get length of current data line          // Get length of current data line
470          len_data_line = 0;          len_data_line = 0;
471          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 505  int editor_data_delete(EDITOR_DATA *p_ed Line 500  int editor_data_delete(EDITOR_DATA *p_ed
500          }          }
501    
502          // Check str to be deleted          // Check str to be deleted
503          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (del_line)
504            {
505                    str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
506            }
507            else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
508          {          {
509                  str_len = 1;                  str_len = 1;
510          }          }
511          else if (p_data_line[offset_data_line + 1] < 0) // GBK          else if (p_data_line[offset_data_line] & 0x80) // head of multi-byte character
512          {          {
513                  str_len = 2;                  str_len = 1;
514                    c = (p_data_line[offset_data_line] & 0x70) << 1;
515                    while (c & 0x80)
516                    {
517                            str_len++;
518                            c = (c & 0x7f) << 1;
519                    }
520          }          }
521          else          else
522          {          {
# Line 522  int editor_data_delete(EDITOR_DATA *p_ed Line 527  int editor_data_delete(EDITOR_DATA *p_ed
527    
528          // Current display line is (almost) empty          // Current display line is (almost) empty
529          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
530                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line &&
531                     p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
532          {          {
533                  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)
534                  {                  {
# Line 567  int editor_data_delete(EDITOR_DATA *p_ed Line 573  int editor_data_delete(EDITOR_DATA *p_ed
573          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
574    
575          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
576          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
577    
578          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
579          {          {
580                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
581                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
582                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
583                          (i == 0                          (i == 0
584                                   ? p_data_line                                   ? p_data_line
# Line 589  int editor_data_delete(EDITOR_DATA *p_ed Line 596  int editor_data_delete(EDITOR_DATA *p_ed
596          if (*p_last_updated_line < last_display_line)          if (*p_last_updated_line < last_display_line)
597          {          {
598                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
599                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)                  // for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
600                  {                  // {
601                          p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j];                  //      p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j];
602                          p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];                  //      p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];
603                  }                  //      p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j];
604                    // }
605                    memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
606                                    p_editor_data->p_display_lines + last_display_line + 1,
607                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
608                                            sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
609                    memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
610                                    p_editor_data->display_line_lengths + last_display_line + 1,
611                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
612                                            sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
613                    memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1,
614                                    p_editor_data->display_line_widths + last_display_line + 1,
615                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
616                                            sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
617    
618                  j = p_editor_data->display_line_total;                  j = p_editor_data->display_line_total;
619                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
# Line 612  static int editor_display_key_handler(in Line 632  static int editor_display_key_handler(in
632          {          {
633          case 0: // Set msg          case 0: // Set msg
634                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
635                                   "| Í˳ö[\033[32mCtrl-W\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
636                  break;                  break;
637          case KEY_CSI:          case KEY_CSI:
638                  *p_key = KEY_ESC;                  *p_key = KEY_ESC;
# Line 629  int editor_display(EDITOR_DATA *p_editor Line 649  int editor_display(EDITOR_DATA *p_editor
649          EDITOR_CTX ctx;          EDITOR_CTX ctx;
650          int ch = 0;          int ch = 0;
651          char input_str[4];          char input_str[4];
652            char c;
653          int str_len = 0;          int str_len = 0;
654          int input_ok;          int input_ok;
655          const int screen_begin_row = 1;          const int screen_begin_row = 1;
# Line 647  int editor_display(EDITOR_DATA *p_editor Line 668  int editor_display(EDITOR_DATA *p_editor
668          int key_insert = 1;          int key_insert = 1;
669          int i, j;          int i, j;
670          char *p_str;          char *p_str;
671            int del_line;
672    
673          clrline(output_current_row, SCREEN_ROWS);          clrline(output_current_row, SCREEN_ROWS);
674    
# Line 656  int editor_display(EDITOR_DATA *p_editor Line 678  int editor_display(EDITOR_DATA *p_editor
678                  return ch;                  return ch;
679          }          }
680    
681          loop = 1;          for (loop = 1; !SYS_server_exit && loop;)
         while (!SYS_server_exit && loop)  
682          {          {
683                  if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row)                  if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row)
684                  {                  {
# Line 665  int editor_display(EDITOR_DATA *p_editor Line 686  int editor_display(EDITOR_DATA *p_editor
686    
687                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
688                                           "\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] "                                           "\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] "
689                                           "µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ [\033[32m%s\033[33m] "                                           "第\033[32m%ld\033[33m/\033[32m%ld\033[33m行 [\033[32m%s\033[33m] "
690                                           "%s",                                           "%s",
691                                           row_pos, col_pos,                                           row_pos, col_pos,
692                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
693                                           key_insert ? "²åÈë" : "Ìæ»»",                                           key_insert ? "æ’å…¥" : "替æ¢",
694                                           ctx.msg);                                           ctx.msg);
695    
696                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
# Line 687  int editor_display(EDITOR_DATA *p_editor Line 708  int editor_display(EDITOR_DATA *p_editor
708                          iflush();                          iflush();
709    
710                          str_len = 0;                          str_len = 0;
                         input_ok = 0;  
711                          ch = igetch_t(MAX_DELAY_TIME);                          ch = igetch_t(MAX_DELAY_TIME);
712                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit)
713                          {                          {
714                                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
715                                    {
716                                            BBS_last_access_tm = time(NULL);
717                                    }
718    
719                                  // extended key handler                                  // extended key handler
720                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
721                                  {                                  {
722                                          goto cleanup;                                          goto cleanup;
723                                  }                                  }
724    
725                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch < 256 && (ch & 0x80)) // head of multi-byte character
                                 {  
                                         input_str[str_len] = (char)(ch - 256);  
                                         str_len++;  
                                 }  
                                 else if (str_len > 0)  
726                                  {                                  {
                                         log_error("Received %d character over 127 followed by character less than 127\n", str_len);  
727                                          str_len = 0;                                          str_len = 0;
728                                            c = (char)(ch & 0xf0);
729                                            while (c & 0x80)
730                                            {
731                                                    input_str[str_len] = (char)(ch - 256);
732                                                    str_len++;
733                                                    c = (c & 0x7f) << 1;
734    
735                                                    if ((c & 0x80) == 0) // Input completed
736                                                    {
737                                                            break;
738                                                    }
739    
740                                                    // Expect additional bytes of input
741                                                    ch = igetch(100);                                                // 0.1 second
742                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
743                                                    {
744    #ifdef _DEBUG
745                                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
746    #endif
747                                                            str_len = 0;
748                                                            break;
749                                                    }
750                                            }
751                                  }                                  }
752    
753                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character
754                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                // Special character
755                                  {                                  {
756                                          BBS_last_access_tm = time(NULL);                                          // Refresh current action while user input
757                                            if (user_online_update(NULL) < 0)
758                                            {
759                                                    log_error("user_online_update(NULL) error\n");
760                                            }
761    
762                                          if (str_len == 0) // ch >= 32 && ch < 127                                          if (str_len == 0) // ch >= 32 && ch < 127
763                                          {                                          {
# Line 719  int editor_display(EDITOR_DATA *p_editor Line 765  int editor_display(EDITOR_DATA *p_editor
765                                                  str_len = 1;                                                  str_len = 1;
766                                          }                                          }
767    
                                         last_updated_line = line_current;  
768                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
769                                          offset_in = col_pos - 1;                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
770                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
771                                          offset_out = offset_in;                                          offset_out = offset_in;
772    
773                                            last_updated_line = display_line_in;
774    
775                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
776                                          {                                          {
777                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
778                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line, 0) < 0)
779                                                  {                                                  {
780                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
781                                                  }                                                  }
# Line 753  int editor_display(EDITOR_DATA *p_editor Line 800  int editor_display(EDITOR_DATA *p_editor
800                                                          clrtoeol();                                                          clrtoeol();
801                                                          for (i = 0; i < scroll_rows; i++)                                                          for (i = 0; i < scroll_rows; i++)
802                                                          {                                                          {
803                                                                  prints("\033[S"); // Scroll up 1 line                                                                  // prints("\033[S"); // Scroll up 1 line
804                                                                    prints("\n"); // Legacy Cterm only works with this line
805                                                          }                                                          }
806    
807                                                          output_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
# Line 768  int editor_display(EDITOR_DATA *p_editor Line 816  int editor_display(EDITOR_DATA *p_editor
816                                                  {                                                  {
817                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
818                                                  }                                                  }
819                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos  
820                                                    if (offset_out != offset_in)
821                                                    {
822                                                            if (display_line_out != display_line_in)
823                                                            {
824                                                                    col_pos = 1;
825                                                            }
826                                                            if (offset_out > 0)
827                                                            {
828                                                                    col_pos += (str_len == 1 ? 1 : 2);
829                                                            }
830                                                    }
831                                          }                                          }
832    
833                                          if (display_line_out != display_line_in) // Output on line change                                          if (display_line_out != display_line_in) // Output on line change
# Line 785  int editor_display(EDITOR_DATA *p_editor Line 844  int editor_display(EDITOR_DATA *p_editor
844                                          str_len = 0;                                          str_len = 0;
845                                          continue;                                          continue;
846                                  }                                  }
847                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
848                                  {                                  {
849                                          BBS_last_access_tm = time(NULL);                                          // Refresh current action while user input
850                                            if (user_online_update(NULL) < 0)
851                                            {
852                                                    log_error("user_online_update(NULL) error\n");
853                                            }
854    
855                                            del_line = 0;
856    
857                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
858                                          {                                          {
859                                                  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
860                                                  {                                                  {
861                                                          ch = igetch_t(MAX_DELAY_TIME);                                                          break; // force output prior operation result if any
                                                         continue;  
862                                                  }                                                  }
863    
864                                                  col_pos--;                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
865                                                  if (col_pos > 1 &&                                                                                             (int)col_pos - 1, &eol, &display_len, 0);
866                                                          p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK                                                  if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
867                                                    {
868                                                            col_pos = display_len - 1;
869                                                    }
870                                                    else
871                                                  {                                                  {
872                                                          col_pos--;                                                          col_pos = display_len;
873                                                  }                                                  }
874    
875                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
876                                                  {                                                  {
877                                                          row_pos--;                                                          row_pos--;
878                                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
879                                                  }                                                  }
880                                          }                                          }
881                                            else if (ch == Ctrl('K'))
882                                            {
883                                                    del_line = 1;
884                                            }
885                                            else if (ch == Ctrl('Y'))
886                                            {
887                                                    col_pos = 1;
888                                                    del_line = 1;
889                                            }
890    
891                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
892                                          offset_in = col_pos - 1;                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
893                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
894                                          offset_out = offset_in;                                          offset_out = offset_in;
895    
896                                          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,
897                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line, del_line)) < 0)
898                                          {                                          {
899                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error: %d\n", str_len);
900                                          }                                          }
901                                          else                                          else
902                                          {                                          {
903                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos                                                  col_pos = display_len + 1; // Set col_pos to accurate pos
904    
905                                                  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));
906                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
# Line 871  int editor_display(EDITOR_DATA *p_editor Line 948  int editor_display(EDITOR_DATA *p_editor
948                                  switch (ch)                                  switch (ch)
949                                  {                                  {
950                                  case KEY_NULL:                                  case KEY_NULL:
951                                            log_error("KEY_NULL\n");
952                                            goto cleanup;
953                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
954                                            log_error("User input timeout\n");
955                                          goto cleanup;                                          goto cleanup;
956                                  case Ctrl('W'):                                  case Ctrl('W'):
957                                    case Ctrl('X'):
958                                          loop = 0;                                          loop = 0;
959                                          break;                                          break;
960                                  case Ctrl('S'): // Start of line                                  case Ctrl('S'): // Start of line
# Line 885  int editor_display(EDITOR_DATA *p_editor Line 966  int editor_display(EDITOR_DATA *p_editor
966                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
967                                          {                                          {
968                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
969                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
970                                                  break;                                                  break;
971                                          }                                          }
972                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
973                                          break;                                          break;
974                                  case Ctrl('T'): // Top of screen                                  case Ctrl('T'): // Top of screen
975                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
976                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
977                                          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_widths[line_current - output_current_row + row_pos]));
978                                          break;                                          break;
979                                  case Ctrl('B'): // Bottom of screen                                  case Ctrl('B'): // Bottom of screen
980                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
# Line 908  int editor_display(EDITOR_DATA *p_editor Line 989  int editor_display(EDITOR_DATA *p_editor
989                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
990                                          {                                          {
991                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
992                                                  col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);                                                  col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1);
993                                          }                                          }
994                                          else                                          else
995                                          {                                          {
996                                                  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_widths[line_current - output_current_row + row_pos]));
997                                          }                                          }
998                                          break;                                          break;
999                                  case KEY_INS:                                  case KEY_INS:
# Line 934  int editor_display(EDITOR_DATA *p_editor Line 1015  int editor_display(EDITOR_DATA *p_editor
1015                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
1016                                          {                                          {
1017                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
1018                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1019                                                  break;                                                  break;
1020                                          }                                          }
1021                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
1022                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1023                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1024                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
1025                                          col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                          col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1026                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1027                                          break;                                          break;
1028                                  case KEY_LEFT:                                  case KEY_LEFT:
1029                                          if (col_pos > 1)                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1030                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1031                                            if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
1032                                            {
1033                                                    col_pos = display_len - 1;
1034                                            }
1035                                            else
1036                                            {
1037                                                    col_pos = display_len;
1038                                            }
1039                                            if (col_pos >= 1)
1040                                          {                                          {
                                                 col_pos--;  
                                                 if (col_pos > 1 &&  
                                                         p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&  
                                                         p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK  
                                                 {  
                                                         col_pos--;  
                                                 }  
1041                                                  break;                                                  break;
1042                                          }                                          }
1043                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 961  int editor_display(EDITOR_DATA *p_editor Line 1045  int editor_display(EDITOR_DATA *p_editor
1045                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
1046                                          {                                          {
1047                                                  row_pos--;                                                  row_pos--;
1048                                                  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_widths[line_current - output_current_row + row_pos]));
1049                                                  break;                                                  break;
1050                                          }                                          }
1051                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 974  int editor_display(EDITOR_DATA *p_editor Line 1058  int editor_display(EDITOR_DATA *p_editor
1058                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
1059                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
1060                                          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
1061                                          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_widths[line_current - output_current_row + row_pos]));
1062                                          break;                                          break;
1063                                  case KEY_SPACE:                                  case KEY_SPACE:
1064                                          break;                                          break;
1065                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1066                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1067                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1068                                            if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&
1069                                                    p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8
1070                                            {
1071                                                    col_pos = display_len + 3;
1072                                            }
1073                                            else
1074                                            {
1075                                                    col_pos = display_len + 2;
1076                                            }
1077                                            if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1078                                          {                                          {
                                                 if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&  
                                                         p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK  
                                                 {  
                                                         col_pos++;  
                                                 }  
                                                 col_pos++;  
1079                                                  break;                                                  break;
1080                                          }                                          }
1081                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
# Line 994  int editor_display(EDITOR_DATA *p_editor Line 1083  int editor_display(EDITOR_DATA *p_editor
1083                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))
1084                                          {                                          {
1085                                                  row_pos++;                                                  row_pos++;
1086                                                  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_widths[line_current - output_current_row + row_pos]));
1087                                                  break;                                                  break;
1088                                          }                                          }
1089                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
1090                                          {                                          {
1091                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
1092                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1093                                                  break;                                                  break;
1094                                          }                                          }
1095                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
1096                                          output_current_row = screen_row_total;                                          output_current_row = screen_row_total;
1097                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1098                                          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_widths[line_current - output_current_row + row_pos]));
1099                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1100                                          clrtoeol();                                          clrtoeol();
1101                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
1102                                            prints("\n"); // Legacy Cterm only works with this line
1103                                          break;                                          break;
1104                                  case KEY_PGUP:                                  case KEY_PGUP:
1105                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 1023  int editor_display(EDITOR_DATA *p_editor Line 1113  int editor_display(EDITOR_DATA *p_editor
1113                                          }                                          }
1114                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1115                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1116                                          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_widths[line_current - output_current_row + row_pos]));
1117                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1118                                          break;                                          break;
1119                                  case KEY_PGDN:                                  case KEY_PGDN:
# Line 1038  int editor_display(EDITOR_DATA *p_editor Line 1128  int editor_display(EDITOR_DATA *p_editor
1128                                          }                                          }
1129                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1130                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1131                                          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_widths[line_current - output_current_row + row_pos]));
1132                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1133                                          break;                                          break;
1134                                    case Ctrl('Q'):
1135                                  case KEY_F1:                                  case KEY_F1:
1136                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not reentrant
1137                                          {                                          {
# Line 1064  int editor_display(EDITOR_DATA *p_editor Line 1155  int editor_display(EDITOR_DATA *p_editor
1155                                          break;                                          break;
1156                                  }                                  }
1157    
1158                                  BBS_last_access_tm = time(NULL);                                  // Refresh current action while user input
1159                                    if (user_online_update(NULL) < 0)
1160                                    {
1161                                            log_error("user_online_update(NULL) error\n");
1162                                    }
1163    
1164                                  if (input_ok)                                  if (input_ok)
1165                                  {                                  {


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

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