/[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.22 by sysadm, Sun Jun 15 04:43:33 2025 UTC Revision 1.43 by sysadm, Wed Oct 1 02:49:13 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "editor.h"  
17  #include "bbs.h"  #include "bbs.h"
18    #include "common.h"
19    #include "editor.h"
20  #include "io.h"  #include "io.h"
21  #include "log.h"  #include "log.h"
22  #include "common.h"  #include "login.h"
 #include "str_process.h"  
23  #include "memory_pool.h"  #include "memory_pool.h"
24    #include "str_process.h"
25  #include <stdlib.h>  #include <stdlib.h>
 #include <sys/param.h>  
 #include <strings.h>  
   
 #define _POSIX_C_SOURCE 200809L  
26  #include <string.h>  #include <string.h>
27    #include <sys/param.h>
28    
29  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
30  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000
# Line 96  EDITOR_DATA *editor_data_load(const char Line 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,
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 133  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';
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 215  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;
# Line 227  int editor_data_insert(EDITOR_DATA *p_ed Line 228  int editor_data_insert(EDITOR_DATA *p_ed
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 || p_editor_data->p_display_lines[display_line][i] > 127) // 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 277  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                  {                  {
264                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",  #ifdef _DEBUG
265                          //                p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                          log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
266                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
267    #endif
268    
269                          return -2;                          return -2;
270                  }                  }
271    
# 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);          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 371  int editor_data_insert(EDITOR_DATA *p_ed Line 358  int editor_data_insert(EDITOR_DATA *p_ed
358                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
359                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
360                          {                          {
361                                  // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);  #ifdef _DEBUG
362                                    log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
363    #endif
364    
365                                  // Terminate prior display line with \n, to avoid error on cleanup                                  // Terminate prior display line with \n, to avoid error on cleanup
366                                  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)
367                                  {                                  {
368                                          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);
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 386  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 422  int editor_data_insert(EDITOR_DATA *p_ed Line 430  int editor_data_insert(EDITOR_DATA *p_ed
430          // Prevent the last display line from being over-length          // Prevent the last display line from being over-length
431          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
432          {          {
433                  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);
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 435  int editor_data_insert(EDITOR_DATA *p_ed Line 444  int editor_data_insert(EDITOR_DATA *p_ed
444          return 0;          return 0;
445  }  }
446    
447  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long 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)
449  {  {
450            long display_line = *p_display_line;
451            long offset = *p_offset;
452          char *p_data_line = NULL;          char *p_data_line = NULL;
453          long len_data_line;          long len_data_line;
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          {          {
# Line 453  int editor_data_delete(EDITOR_DATA *p_ed Line 466  int editor_data_delete(EDITOR_DATA *p_ed
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 || p_editor_data->p_display_lines[display_line][i] > 127) // 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 504  int editor_data_delete(EDITOR_DATA *p_ed Line 504  int editor_data_delete(EDITOR_DATA *p_ed
504          {          {
505                  str_len = 1;                  str_len = 1;
506          }          }
507          else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK          else if (p_data_line[offset_data_line] & 0b10000000) // head of multi-byte character
508          {          {
509                  str_len = 2;                  str_len = 1;
510                    c = (p_data_line[offset_data_line] & 0b01110000) << 1;
511                    while (c & 0b10000000)
512                    {
513                            str_len++;
514                            c = (c & 0b01111111) << 1;
515                    }
516          }          }
517          else          else
518          {          {
# Line 562  int editor_data_delete(EDITOR_DATA *p_ed Line 568  int editor_data_delete(EDITOR_DATA *p_ed
568          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
569    
570          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
571          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, line_widths);
572    
573          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
574          {          {
575                  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];
576                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
577                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
578                          (i == 0                          (i == 0
579                                   ? p_data_line                                   ? p_data_line
# Line 584  int editor_data_delete(EDITOR_DATA *p_ed Line 591  int editor_data_delete(EDITOR_DATA *p_ed
591          if (*p_last_updated_line < last_display_line)          if (*p_last_updated_line < last_display_line)
592          {          {
593                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
594                  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++)
595                  {                  // {
596                          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];
597                          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];
598                  }                  //      p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j];
599                    // }
600                    memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
601                                    p_editor_data->p_display_lines + last_display_line + 1,
602                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
603                                            sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
604                    memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
605                                    p_editor_data->display_line_lengths + last_display_line + 1,
606                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
607                                            sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
608                    memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1,
609                                    p_editor_data->display_line_widths + last_display_line + 1,
610                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
611                                            sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
612    
613                  j = p_editor_data->display_line_total;                  j = p_editor_data->display_line_total;
614                  (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);
615                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);                  *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
616          }          }
617    
618            // Return real offset
619            *p_offset = offset;
620    
621          return str_len;          return str_len;
622  }  }
623    
# Line 604  static int editor_display_key_handler(in Line 627  static int editor_display_key_handler(in
627          {          {
628          case 0: // Set msg          case 0: // Set msg
629                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
630                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
631                    break;
632            case KEY_CSI:
633                    *p_key = KEY_ESC;
634                  break;                  break;
635          }          }
636    
# Line 618  int editor_display(EDITOR_DATA *p_editor Line 644  int editor_display(EDITOR_DATA *p_editor
644          EDITOR_CTX ctx;          EDITOR_CTX ctx;
645          int ch = 0;          int ch = 0;
646          char input_str[4];          char input_str[4];
647            char c;
648          int str_len = 0;          int str_len = 0;
649          int input_ok;          int input_ok;
650          const int screen_begin_row = 1;          const int screen_begin_row = 1;
# Line 645  int editor_display(EDITOR_DATA *p_editor Line 672  int editor_display(EDITOR_DATA *p_editor
672                  return ch;                  return ch;
673          }          }
674    
675          loop = 1;          for (loop = 1; !SYS_server_exit && loop;)
         while (!SYS_server_exit && loop)  
676          {          {
677                  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)
678                  {                  {
# Line 654  int editor_display(EDITOR_DATA *p_editor Line 680  int editor_display(EDITOR_DATA *p_editor
680    
681                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
682                                           "\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] "
683                                           "µÚ\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] "
684                                           "%s",                                           "%s",
685                                           row_pos, col_pos,                                           row_pos, col_pos,
686                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
687                                           key_insert ? "²åÈë" : "¸Äд",                                           key_insert ? "æ’å…¥" : "替æ¢",
688                                           ctx.msg);                                           ctx.msg);
689    
690                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
691                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
692                          {                          {
693                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 675  int editor_display(EDITOR_DATA *p_editor Line 701  int editor_display(EDITOR_DATA *p_editor
701                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
702                          iflush();                          iflush();
703    
704                          input_ok = 0;                          str_len = 0;
705                          while (!SYS_server_exit && !input_ok)                          ch = igetch_t(MAX_DELAY_TIME);
706                            while (!SYS_server_exit)
707                          {                          {
                                 ch = igetch_t(MAX_DELAY_TIME);  
                                 input_ok = 1;  
   
708                                  // extended key handler                                  // extended key handler
709                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
710                                  {                                  {
711                                          goto cleanup;                                          goto cleanup;
712                                  }                                  }
713    
714                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch < 256 && (ch & 0b10000000)) // head of multi-byte character
                                 {  
                                         input_str[str_len] = (char)(ch - 256);  
                                         str_len++;  
                                 }  
                                 else if (str_len > 0)  
715                                  {                                  {
                                         log_error("Received %d character over 127 followed by character less than 127\n", str_len);  
716                                          str_len = 0;                                          str_len = 0;
717                                            c = (char)(ch & 0b11110000);
718                                            while (c & 0b10000000)
719                                            {
720                                                    input_str[str_len] = (char)(ch - 256);
721                                                    str_len++;
722                                                    c = (c & 0b01111111) << 1;
723    
724                                                    if ((c & 0b10000000) == 0) // Input completed
725                                                    {
726                                                            break;
727                                                    }
728    
729                                                    // Expect additional bytes of input
730                                                    ch = igetch(100);                                                // 0.1 second
731                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
732                                                    {
733    #ifdef _DEBUG
734                                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
735    #endif
736                                                            str_len = 0;
737                                                            break;
738                                                    }
739                                            }
740                                  }                                  }
741    
742                                  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
743                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                // Special character
744                                  {                                  {
745                                            BBS_last_access_tm = time(NULL);
746    
747                                            // Refresh current action while user input
748                                            if (user_online_update(NULL) < 0)
749                                            {
750                                                    log_error("user_online_update(NULL) error\n");
751                                            }
752    
753                                          if (str_len == 0) // ch >= 32 && ch < 127                                          if (str_len == 0) // ch >= 32 && ch < 127
754                                          {                                          {
755                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
756                                                  str_len = 1;                                                  str_len = 1;
757                                          }                                          }
758    
                                         last_updated_line = line_current;  
759                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
760                                          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);
761                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
762                                          offset_out = offset_in;                                          offset_out = offset_in;
763    
764                                            last_updated_line = display_line_in;
765    
766                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
767                                          {                                          {
768                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
769                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line) < 0)
770                                                  {                                                  {
771                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
# Line 741  int editor_display(EDITOR_DATA *p_editor Line 791  int editor_display(EDITOR_DATA *p_editor
791                                                          clrtoeol();                                                          clrtoeol();
792                                                          for (i = 0; i < scroll_rows; i++)                                                          for (i = 0; i < scroll_rows; i++)
793                                                          {                                                          {
794                                                                  prints("\033[S"); // Scroll up 1 line                                                                  // prints("\033[S"); // Scroll up 1 line
795                                                                    prints("\n"); // Legacy Cterm only works with this line
796                                                          }                                                          }
797    
798                                                          output_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
# Line 756  int editor_display(EDITOR_DATA *p_editor Line 807  int editor_display(EDITOR_DATA *p_editor
807                                                  {                                                  {
808                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
809                                                  }                                                  }
810                                                  col_pos = offset_out + 1;  
811                                                    if (offset_out != offset_in)
812                                                    {
813                                                            if (display_line_out != display_line_in)
814                                                            {
815                                                                    col_pos = 1;
816                                                            }
817                                                            if (offset_out > 0)
818                                                            {
819                                                                    col_pos += (str_len == 1 ? 1 : 2);
820                                                            }
821                                                    }
822                                            }
823    
824                                            if (display_line_out != display_line_in) // Output on line change
825                                            {
826                                                    break;
827                                            }
828    
829                                            ch = igetch(0);
830                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
831                                            {
832                                                    break;
833                                          }                                          }
834    
835                                          str_len = 0;                                          str_len = 0;
# Line 764  int editor_display(EDITOR_DATA *p_editor Line 837  int editor_display(EDITOR_DATA *p_editor
837                                  }                                  }
838                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
839                                  {                                  {
840                                            BBS_last_access_tm = time(NULL);
841    
842                                            // Refresh current action while user input
843                                            if (user_online_update(NULL) < 0)
844                                            {
845                                                    log_error("user_online_update(NULL) error\n");
846                                            }
847    
848                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
849                                          {                                          {
850                                                  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
851                                                  {                                                  {
852                                                          input_ok = 0;                                                          break; // force output prior operation result if any
853                                                          continue;                                                  }
854    
855                                                    offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
856                                                                                               (int)col_pos - 1, &eol, &display_len, 0);
857                                                    if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
858                                                    {
859                                                            col_pos = display_len - 1;
860                                                    }
861                                                    else
862                                                    {
863                                                            col_pos = display_len;
864                                                  }                                                  }
865    
                                                 col_pos--;  
866                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
867                                                  {                                                  {
868                                                          row_pos--;                                                          row_pos--;
869                                                          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]);
870                                                  }                                                  }
871                                          }                                          }
872    
873                                          if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1,                                          display_line_in = line_current - output_current_row + row_pos;
874                                            offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
875                                            display_line_out = display_line_in;
876                                            offset_out = offset_in;
877    
878                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
879                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line)) < 0)
880                                          {                                          {
881                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
882                                          }                                          }
883                                          else                                          else
884                                          {                                          {
885                                                  if (ch == BACKSPACE)                                                  col_pos = display_len + 1; // Set col_pos to accurate pos
                                                 {  
                                                         for (i = 1; i < str_len; i++)  
                                                         {  
                                                                 col_pos--;  
                                                                 if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)  
                                                                 {  
                                                                         row_pos--;  
                                                                         col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);  
                                                                 }  
                                                         }  
                                                 }  
886    
887                                                  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));
888                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
# Line 827  int editor_display(EDITOR_DATA *p_editor Line 911  int editor_display(EDITOR_DATA *p_editor
911                                                  clrline(output_current_row, output_end_row);                                                  clrline(output_current_row, output_end_row);
912                                          }                                          }
913    
914                                            if (display_line_out != display_line_in) // Output on line change
915                                            {
916                                                    break;
917                                            }
918    
919                                            ch = igetch(0);
920                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
921                                            {
922                                                    break;
923                                            }
924    
925                                          str_len = 0;                                          str_len = 0;
926                                          continue;                                          continue;
927                                  }                                  }
928    
929                                    input_ok = 1;
930                                  switch (ch)                                  switch (ch)
931                                  {                                  {
932                                  case KEY_NULL:                                  case KEY_NULL:
933                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
934                                          goto cleanup;                                          goto cleanup;
935                                  case Ctrl('W'):                                  case Ctrl('W'):
936                                    case Ctrl('X'):
937                                          loop = 0;                                          loop = 0;
938                                          break;                                          break;
939                                  case Ctrl('S'): // Start of line                                  case Ctrl('S'): // Start of line
# Line 845  int editor_display(EDITOR_DATA *p_editor Line 942  int editor_display(EDITOR_DATA *p_editor
942                                          break;                                          break;
943                                  case Ctrl('E'): // End of line                                  case Ctrl('E'): // End of line
944                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
945                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
946                                          {                                          {
947                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
948                                                  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;
949                                                  break;                                                  break;
950                                          }                                          }
951                                          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]);
952                                          break;                                          break;
953                                  case Ctrl('T'): // Top of screen                                  case Ctrl('T'): // Top of screen
954                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
955                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
956                                          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]));
957                                          break;                                          break;
958                                  case Ctrl('B'): // Bottom of screen                                  case Ctrl('B'): // Bottom of screen
959                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
# Line 871  int editor_display(EDITOR_DATA *p_editor Line 968  int editor_display(EDITOR_DATA *p_editor
968                                          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
969                                          {                                          {
970                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
971                                                  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);
972                                          }                                          }
973                                          else                                          else
974                                          {                                          {
975                                                  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]));
976                                          }                                          }
977                                          break;                                          break;
978                                  case KEY_INS:                                  case KEY_INS:
# Line 897  int editor_display(EDITOR_DATA *p_editor Line 994  int editor_display(EDITOR_DATA *p_editor
994                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
995                                          {                                          {
996                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
997                                                  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;
998                                                  break;                                                  break;
999                                          }                                          }
1000                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
1001                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1002                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1003                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
1004                                          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;
1005                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1006                                          break;                                          break;
1007                                  case KEY_LEFT:                                  case KEY_LEFT:
1008                                          if (col_pos > 1)                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1009                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1010                                            if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
1011                                            {
1012                                                    col_pos = display_len - 1;
1013                                            }
1014                                            else
1015                                            {
1016                                                    col_pos = display_len;
1017                                            }
1018                                            if (col_pos >= 1)
1019                                          {                                          {
                                                 col_pos--;  
1020                                                  break;                                                  break;
1021                                          }                                          }
1022                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 918  int editor_display(EDITOR_DATA *p_editor Line 1024  int editor_display(EDITOR_DATA *p_editor
1024                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
1025                                          {                                          {
1026                                                  row_pos--;                                                  row_pos--;
1027                                                  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]));
1028                                                  break;                                                  break;
1029                                          }                                          }
1030                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 931  int editor_display(EDITOR_DATA *p_editor Line 1037  int editor_display(EDITOR_DATA *p_editor
1037                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
1038                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
1039                                          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
1040                                          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]));
1041                                          break;                                          break;
1042                                  case KEY_SPACE:                                  case KEY_SPACE:
1043                                          break;                                          break;
1044                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1045                                          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],
1046                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1047                                            if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&
1048                                                    p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8
1049                                            {
1050                                                    col_pos = display_len + 3;
1051                                            }
1052                                            else
1053                                            {
1054                                                    col_pos = display_len + 2;
1055                                            }
1056                                            if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1057                                          {                                          {
                                                 col_pos++;  
1058                                                  break;                                                  break;
1059                                          }                                          }
1060                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
# Line 946  int editor_display(EDITOR_DATA *p_editor Line 1062  int editor_display(EDITOR_DATA *p_editor
1062                                          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))
1063                                          {                                          {
1064                                                  row_pos++;                                                  row_pos++;
1065                                                  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]));
1066                                                  break;                                                  break;
1067                                          }                                          }
1068                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
1069                                          {                                          {
1070                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
1071                                                  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;
1072                                                  break;                                                  break;
1073                                          }                                          }
1074                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
1075                                          output_current_row = screen_row_total;                                          output_current_row = screen_row_total;
1076                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1077                                          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]));
1078                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1079                                          clrtoeol();                                          clrtoeol();
1080                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
1081                                            prints("\n"); // Legacy Cterm only works with this line
1082                                          break;                                          break;
1083                                  case KEY_PGUP:                                  case KEY_PGUP:
1084                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 975  int editor_display(EDITOR_DATA *p_editor Line 1092  int editor_display(EDITOR_DATA *p_editor
1092                                          }                                          }
1093                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1094                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1095                                          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]));
1096                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1097                                          break;                                          break;
1098                                  case KEY_PGDN:                                  case KEY_PGDN:
# Line 990  int editor_display(EDITOR_DATA *p_editor Line 1107  int editor_display(EDITOR_DATA *p_editor
1107                                          }                                          }
1108                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1109                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1110                                          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]));
1111                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1112                                          break;                                          break;
1113                                  case KEY_F1:                                  case KEY_F1:
# Line 1016  int editor_display(EDITOR_DATA *p_editor Line 1133  int editor_display(EDITOR_DATA *p_editor
1133                                          break;                                          break;
1134                                  }                                  }
1135    
1136                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(NULL);
1137    
1138                                    // Refresh current action while user input
1139                                    if (user_online_update(NULL) < 0)
1140                                    {
1141                                            log_error("user_online_update(NULL) error\n");
1142                                    }
1143    
1144                                  if (input_ok)                                  if (input_ok)
1145                                  {                                  {
1146                                          break;                                          break;
1147                                  }                                  }
1148    
1149                                    ch = igetch_t(MAX_DELAY_TIME);
1150                          }                          }
1151    
1152                          continue;                          continue;


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

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