/[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.37 by sysadm, Tue Jul 1 08:31:11 2025 UTC Revision 1.55 by sysadm, Sat Nov 8 08:21:31 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                     editor.h  -  description  /*
3                                                           -------------------   * editor
4          copyright            : (C) 2004-2025 by Leaflet   *   - user interactive full-screen text editor
5          email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
11  #include "editor.h"  #include "editor.h"
12  #include "io.h"  #include "io.h"
13  #include "log.h"  #include "log.h"
14    #include "login.h"
15  #include "memory_pool.h"  #include "memory_pool.h"
16  #include "str_process.h"  #include "str_process.h"
17  #include <stdlib.h>  #include <stdlib.h>
18  #include <string.h>  #include <string.h>
19    #include <wchar.h>
20  #include <sys/param.h>  #include <sys/param.h>
21    
22  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  enum _editor_constant_t
23  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000  {
24  #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)          EDITOR_MEM_POOL_LINE_PER_CHUNK = 1000,
25            EDITOR_MEM_POOL_CHUNK_LIMIT = (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1),
26    };
27    
28    static const char EDITOR_ESC_DISPLAY_STR[] = "\033[32m*\033[m";
29    
30  static MEMORY_POOL *p_mp_data_line;  static MEMORY_POOL *p_mp_data_line;
31  static MEMORY_POOL *p_mp_editor_data;  static MEMORY_POOL *p_mp_editor_data;
# Line 48  int editor_memory_pool_init(void) Line 46  int editor_memory_pool_init(void)
46          }          }
47    
48          p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);          p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
49          if (p_mp_data_line == NULL)          if (p_mp_editor_data == NULL)
50          {          {
51                  log_error("Memory pool init error\n");                  log_error("Memory pool init error\n");
52                  return -3;                  return -3;
# Line 82  EDITOR_DATA *editor_data_load(const char Line 80  EDITOR_DATA *editor_data_load(const char
80    
81          if (p_data == NULL)          if (p_data == NULL)
82          {          {
83                  log_error("editor_data_load() error: NULL pointer\n");                  log_error("NULL pointer error\n");
84                  return NULL;                  return NULL;
85          }          }
86    
# Line 93  EDITOR_DATA *editor_data_load(const char Line 91  EDITOR_DATA *editor_data_load(const char
91                  return NULL;                  return NULL;
92          }          }
93    
94          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,
95                                                                                                                     0, p_editor_data->display_line_widths);
96    
97          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
98          {          {
# Line 130  EDITOR_DATA *editor_data_load(const char Line 129  EDITOR_DATA *editor_data_load(const char
129                          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')
130                  {                  {
131                          p_editor_data->display_line_lengths[i]--;                          p_editor_data->display_line_lengths[i]--;
132                            p_editor_data->display_line_widths[i]--;
133                          current_data_line_length--;                          current_data_line_length--;
134                  }                  }
135                  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 147  long editor_data_save(const EDITOR_DATA
147    
148          if (p_editor_data == NULL || p_data == NULL)          if (p_editor_data == NULL || p_data == NULL)
149          {          {
150                  log_error("editor_data_save() error: NULL pointer\n");                  log_error("NULL pointer error\n");
151                  return -1;                  return -1;
152          }          }
153    
# Line 212  int editor_data_insert(EDITOR_DATA *p_ed Line 212  int editor_data_insert(EDITOR_DATA *p_ed
212          long offset_data_line;          long offset_data_line;
213          long last_display_line; // of data line          long last_display_line; // of data line
214          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
215            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
216          long split_line_total;          long split_line_total;
217          long i;          long i;
218          int len;          int len;
# Line 220  int editor_data_insert(EDITOR_DATA *p_ed Line 221  int editor_data_insert(EDITOR_DATA *p_ed
221    
222          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
223          {          {
224                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
225                  return -1;                  return -1;
226          }          }
227    
         // 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--;  
         }  
   
228          // Get length of current data line          // Get length of current data line
229          len_data_line = 0;          len_data_line = 0;
230          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 340  int editor_data_insert(EDITOR_DATA *p_ed Line 320  int editor_data_insert(EDITOR_DATA *p_ed
320                          *p_offset = offset + str_len;                          *p_offset = offset + str_len;
321                  }                  }
322    
323                    // Update display width of current display line
324                    len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0);
325                    p_editor_data->display_line_widths[display_line] = display_len;
326    
327                  split_line_total = last_display_line - display_line + 3;                  split_line_total = last_display_line - display_line + 3;
328    
329                  // 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 346  int editor_data_insert(EDITOR_DATA *p_ed
346          }          }
347    
348          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
349          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);
350    
351          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
352          {          {
# Line 382  int editor_data_insert(EDITOR_DATA *p_ed Line 366  int editor_data_insert(EDITOR_DATA *p_ed
366                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';                                          p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
367                                          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';
368                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;                                          p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
369                                            p_editor_data->display_line_widths[display_line + i - 1] = display_len;
370                                  }                                  }
371                                  if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])                                  if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
372                                  {                                  {
# Line 394  int editor_data_insert(EDITOR_DATA *p_ed Line 379  int editor_data_insert(EDITOR_DATA *p_ed
379                          // {                          // {
380                          //      p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];                          //      p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
381                          //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];                          //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
382                            //      p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1];
383                          // }                          // }
384                          memmove(p_editor_data->p_display_lines + last_display_line + 2,                          memmove(p_editor_data->p_display_lines + last_display_line + 2,
385                                          p_editor_data->p_display_lines + last_display_line + 1,                                          p_editor_data->p_display_lines + last_display_line + 1,
# Line 403  int editor_data_insert(EDITOR_DATA *p_ed Line 389  int editor_data_insert(EDITOR_DATA *p_ed
389                                          p_editor_data->display_line_lengths + last_display_line + 1,                                          p_editor_data->display_line_lengths + last_display_line + 1,
390                                          (size_t)(p_editor_data->display_line_total - last_display_line - 1) *                                          (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
391                                                  sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));                                                  sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
392                            memmove(p_editor_data->display_line_widths + last_display_line + 2,
393                                            p_editor_data->display_line_widths + last_display_line + 1,
394                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
395                                                    sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
396    
397                          last_display_line++;                          last_display_line++;
398                          *p_last_updated_line = p_editor_data->display_line_total;                          *p_last_updated_line = p_editor_data->display_line_total;
# Line 410  int editor_data_insert(EDITOR_DATA *p_ed Line 400  int editor_data_insert(EDITOR_DATA *p_ed
400                  }                  }
401    
402                  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];
403                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
404                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
405                          (i == 0                          (i == 0
406                                   ? p_data_line                                   ? p_data_line
# Line 439  int editor_data_insert(EDITOR_DATA *p_ed Line 430  int editor_data_insert(EDITOR_DATA *p_ed
430                  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);
431                  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';
432                  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;
433                    p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len;
434                  if (*p_display_line + 1 >= p_editor_data->display_line_total)                  if (*p_display_line + 1 >= p_editor_data->display_line_total)
435                  {                  {
436                          *p_offset = MIN(*p_offset, len);                          *p_offset = MIN(*p_offset, len);
# Line 450  int editor_data_insert(EDITOR_DATA *p_ed Line 442  int editor_data_insert(EDITOR_DATA *p_ed
442  }  }
443    
444  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,
445                                             long *p_last_updated_line)                                             long *p_last_updated_line, int del_line)
446  {  {
447          long display_line = *p_display_line;          long display_line = *p_display_line;
448          long offset = *p_offset;          long offset = *p_offset;
# Line 459  int editor_data_delete(EDITOR_DATA *p_ed Line 451  int editor_data_delete(EDITOR_DATA *p_ed
451          long offset_data_line;          long offset_data_line;
452          long last_display_line; // of data line          long last_display_line; // of data line
453          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
454            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
455          long split_line_total;          long split_line_total;
456          long i, j;          long i, j;
457          int str_len = 0;          int str_len = 0;
458            char c;
459    
460          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
461          {          {
462                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
463                  return -1;                  return -1;
464          }          }
465    
         // 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--;  
         }  
   
466          // Get length of current data line          // Get length of current data line
467          len_data_line = 0;          len_data_line = 0;
468          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 516  int editor_data_delete(EDITOR_DATA *p_ed Line 497  int editor_data_delete(EDITOR_DATA *p_ed
497          }          }
498    
499          // Check str to be deleted          // Check str to be deleted
500          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (del_line)
501            {
502                    str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
503            }
504            else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
505          {          {
506                  str_len = 1;                  str_len = 1;
507          }          }
508          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
509          {          {
510                  str_len = 2;                  str_len = 1;
511                    c = (p_data_line[offset_data_line] & 0x70) << 1;
512                    while (c & 0x80)
513                    {
514                            str_len++;
515                            c = (c & 0x7f) << 1;
516                    }
517          }          }
518          else          else
519          {          {
# Line 533  int editor_data_delete(EDITOR_DATA *p_ed Line 524  int editor_data_delete(EDITOR_DATA *p_ed
524    
525          // Current display line is (almost) empty          // Current display line is (almost) empty
526          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
527                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line &&
528                     p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
529          {          {
530                  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)
531                  {                  {
# Line 578  int editor_data_delete(EDITOR_DATA *p_ed Line 570  int editor_data_delete(EDITOR_DATA *p_ed
570          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
571    
572          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
573          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);
574    
575          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
576          {          {
577                  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];
578                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
579                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
580                          (i == 0                          (i == 0
581                                   ? p_data_line                                   ? p_data_line
# Line 604  int editor_data_delete(EDITOR_DATA *p_ed Line 597  int editor_data_delete(EDITOR_DATA *p_ed
597                  // {                  // {
598                  //      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];
599                  //      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];
600                    //      p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j];
601                  // }                  // }
602                  memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,                  memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
603                                  p_editor_data->p_display_lines + last_display_line + 1,                                  p_editor_data->p_display_lines + last_display_line + 1,
# Line 613  int editor_data_delete(EDITOR_DATA *p_ed Line 607  int editor_data_delete(EDITOR_DATA *p_ed
607                                  p_editor_data->display_line_lengths + last_display_line + 1,                                  p_editor_data->display_line_lengths + last_display_line + 1,
608                                  (size_t)(p_editor_data->display_line_total - last_display_line - 1) *                                  (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
609                                          sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));                                          sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
610                    memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1,
611                                    p_editor_data->display_line_widths + last_display_line + 1,
612                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
613                                            sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
614    
615                  j = p_editor_data->display_line_total;                  j = p_editor_data->display_line_total;
616                  (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 631  static int editor_display_key_handler(in Line 629  static int editor_display_key_handler(in
629          {          {
630          case 0: // Set msg          case 0: // Set msg
631                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
632                                   "| Í˳ö[\033[32mCtrl-W\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]",
633                                     (UTF8_fixed_width ? "定宽" : "å˜å®½"));
634                  break;                  break;
635          case KEY_CSI:          case KEY_CSI:
636                  *p_key = KEY_ESC;                  *p_key = KEY_ESC;
# Line 647  int editor_display(EDITOR_DATA *p_editor Line 646  int editor_display(EDITOR_DATA *p_editor
646          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
647          EDITOR_CTX ctx;          EDITOR_CTX ctx;
648          int ch = 0;          int ch = 0;
649          char input_str[4];          char input_str[5];
650            wchar_t wcs[2];
651            char c;
652          int str_len = 0;          int str_len = 0;
653          int input_ok;          int input_ok;
654          const int screen_begin_row = 1;          const int screen_begin_row = 1;
# Line 666  int editor_display(EDITOR_DATA *p_editor Line 667  int editor_display(EDITOR_DATA *p_editor
667          int key_insert = 1;          int key_insert = 1;
668          int i, j;          int i, j;
669          char *p_str;          char *p_str;
670            int del_line;
671    
672          clrline(output_current_row, SCREEN_ROWS);          clrline(output_current_row, SCREEN_ROWS);
673    
# Line 683  int editor_display(EDITOR_DATA *p_editor Line 685  int editor_display(EDITOR_DATA *p_editor
685    
686                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
687                                           "\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] "
688                                           "µÚ\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] "
689                                           "%s",                                           "%s",
690                                           row_pos, col_pos,                                           row_pos, col_pos,
691                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
692                                           key_insert ? "²åÈë" : "Ìæ»»",                                           key_insert ? "æ’å…¥" : "替æ¢",
693                                           ctx.msg);                                           ctx.msg);
694    
695                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
# Line 705  int editor_display(EDITOR_DATA *p_editor Line 707  int editor_display(EDITOR_DATA *p_editor
707                          iflush();                          iflush();
708    
709                          str_len = 0;                          str_len = 0;
710                          ch = igetch_t(MAX_DELAY_TIME);                          ch = igetch_t(BBS_max_user_idle_time);
711                          while (!SYS_server_exit)                          while (!SYS_server_exit)
712                          {                          {
713                                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
714                                    {
715                                            BBS_last_access_tm = time(NULL);
716                                    }
717    
718                                  // extended key handler                                  // extended key handler
719                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
720                                  {                                  {
721                                          goto cleanup;                                          goto cleanup;
722                                  }                                  }
723    
724                                  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)  
725                                  {                                  {
                                         log_error("Received %d character over 127 followed by character less than 127\n", str_len);  
726                                          str_len = 0;                                          str_len = 0;
727                                            c = (char)(ch & 0xf0);
728                                            while (c & 0x80)
729                                            {
730                                                    input_str[str_len] = (char)(ch - 256);
731                                                    str_len++;
732                                                    c = (c & 0x7f) << 1;
733    
734                                                    if ((c & 0x80) == 0) // Input completed
735                                                    {
736                                                            break;
737                                                    }
738    
739                                                    // Expect additional bytes of input
740                                                    ch = igetch(100);                                                // 0.1 second
741                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
742                                                    {
743    #ifdef _DEBUG
744                                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
745    #endif
746                                                            str_len = 0;
747                                                            break;
748                                                    }
749                                            }
750                                            input_str[str_len] = '\0';
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 737  int editor_display(EDITOR_DATA *p_editor Line 766  int editor_display(EDITOR_DATA *p_editor
766                                          }                                          }
767    
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    
# Line 746  int editor_display(EDITOR_DATA *p_editor Line 775  int editor_display(EDITOR_DATA *p_editor
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 787  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                                                                    if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
829                                                                    {
830                                                                            log_error("mbstowcs() error\n");
831                                                                    }
832                                                                    col_pos += (str_len == 1 ? 1 : (UTF8_fixed_width ? 2 : wcwidth(wcs[0])));
833                                                            }
834                                                    }
835                                          }                                          }
836    
837                                          if (display_line_out != display_line_in) // Output on line change                                          if (display_line_out != display_line_in) // Output on line change
# Line 804  int editor_display(EDITOR_DATA *p_editor Line 848  int editor_display(EDITOR_DATA *p_editor
848                                          str_len = 0;                                          str_len = 0;
849                                          continue;                                          continue;
850                                  }                                  }
851                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
852                                  {                                  {
853                                          BBS_last_access_tm = time(NULL);                                          // Refresh current action while user input
854                                            if (user_online_update(NULL) < 0)
855                                            {
856                                                    log_error("user_online_update(NULL) error\n");
857                                            }
858    
859                                            del_line = 0;
860    
861                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
862                                          {                                          {
# Line 815  int editor_display(EDITOR_DATA *p_editor Line 865  int editor_display(EDITOR_DATA *p_editor
865                                                          break; // force output prior operation result if any                                                          break; // force output prior operation result if any
866                                                  }                                                  }
867    
868                                                  col_pos--;                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
869                                                  if (col_pos > 1 &&                                                                                             (int)col_pos - 1, &eol, &display_len, 0);
870                                                          p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK                                                  col_pos = display_len;
                                                 {  
                                                         col_pos--;  
                                                 }  
   
871                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
872                                                  {                                                  {
873                                                          row_pos--;                                                          row_pos--;
874                                                          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]);
875                                                  }                                                  }
876                                          }                                          }
877                                            else if (ch == Ctrl('K'))
878                                            {
879                                                    del_line = 1;
880                                            }
881                                            else if (ch == Ctrl('Y'))
882                                            {
883                                                    col_pos = 1;
884                                                    del_line = 1;
885                                            }
886    
887                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
888                                          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);
889                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
890                                          offset_out = offset_in;                                          offset_out = offset_in;
891    
892                                          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,
893                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line, del_line)) < 0)
894                                          {                                          {
895                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error: %d\n", str_len);
896                                          }                                          }
897                                          else                                          else
898                                          {                                          {
899                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos                                                  col_pos = display_len + 1; // Set col_pos to accurate pos
900    
901                                                  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));
902                                                  line_current -= (output_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
# Line 889  int editor_display(EDITOR_DATA *p_editor Line 944  int editor_display(EDITOR_DATA *p_editor
944                                  switch (ch)                                  switch (ch)
945                                  {                                  {
946                                  case KEY_NULL:                                  case KEY_NULL:
947                                            log_error("KEY_NULL\n");
948                                            goto cleanup;
949                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
950                                            log_error("User input timeout\n");
951                                          goto cleanup;                                          goto cleanup;
952                                  case Ctrl('W'):                                  case Ctrl('W'):
953                                    case Ctrl('X'):
954                                          loop = 0;                                          loop = 0;
955                                          break;                                          break;
956                                  case Ctrl('S'): // Start of line                                  case Ctrl('S'): // Start of line
# Line 903  int editor_display(EDITOR_DATA *p_editor Line 962  int editor_display(EDITOR_DATA *p_editor
962                                          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
963                                          {                                          {
964                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
965                                                  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;
966                                                  break;                                                  break;
967                                          }                                          }
968                                          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]);
969                                          break;                                          break;
970                                  case Ctrl('T'): // Top of screen                                  case Ctrl('T'): // Top of screen
971                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
972                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
973                                          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]));
974                                          break;                                          break;
975                                  case Ctrl('B'): // Bottom of screen                                  case Ctrl('B'): // Bottom of screen
976                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
# Line 926  int editor_display(EDITOR_DATA *p_editor Line 985  int editor_display(EDITOR_DATA *p_editor
985                                          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
986                                          {                                          {
987                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
988                                                  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);
989                                          }                                          }
990                                          else                                          else
991                                          {                                          {
992                                                  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]));
993                                          }                                          }
994                                          break;                                          break;
995                                  case KEY_INS:                                  case KEY_INS:
# Line 952  int editor_display(EDITOR_DATA *p_editor Line 1011  int editor_display(EDITOR_DATA *p_editor
1011                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
1012                                          {                                          {
1013                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
1014                                                  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;
1015                                                  break;                                                  break;
1016                                          }                                          }
1017                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
1018                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1019                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1020                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
1021                                          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;
1022                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1023                                          break;                                          break;
1024                                  case KEY_LEFT:                                  case KEY_LEFT:
1025                                          if (col_pos > 1)                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1026                                          {                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1027                                                  col_pos--;                                          col_pos = display_len;
1028                                                  if (col_pos > 1 &&                                          if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
1029                                                          p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&                                          {
1030                                                          p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK                                                  split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1031                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1032                                                    if (display_len == col_pos - 2)
1033                                                  {                                                  {
1034                                                          col_pos--;                                                          col_pos--;
1035                                                  }                                                  }
1036                                            }
1037                                            if (col_pos >= 1)
1038                                            {
1039                                                  break;                                                  break;
1040                                          }                                          }
1041                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 979  int editor_display(EDITOR_DATA *p_editor Line 1043  int editor_display(EDITOR_DATA *p_editor
1043                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
1044                                          {                                          {
1045                                                  row_pos--;                                                  row_pos--;
1046                                                  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]));
1047                                                  break;                                                  break;
1048                                          }                                          }
1049                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 992  int editor_display(EDITOR_DATA *p_editor Line 1056  int editor_display(EDITOR_DATA *p_editor
1056                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
1057                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
1058                                          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
1059                                          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]));
                                         break;  
                                 case KEY_SPACE:  
1060                                          break;                                          break;
1061                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1062                                          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],
1063                                          {                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1064                                                  if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&                                          col_pos = display_len + 2;
1065                                                          p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK                                          if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&
1066                                                    p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8
1067                                            {
1068                                                    split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in,
1069                                                                       1, &eol, &display_len, 0);
1070                                                    if (display_len == 0)
1071                                                  {                                                  {
1072                                                          col_pos++;                                                          col_pos++;
1073                                                  }                                                  }
1074                                                  col_pos++;                                          }
1075                                            if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1076                                            {
1077                                                  break;                                                  break;
1078                                          }                                          }
1079                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
# Line 1012  int editor_display(EDITOR_DATA *p_editor Line 1081  int editor_display(EDITOR_DATA *p_editor
1081                                          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))
1082                                          {                                          {
1083                                                  row_pos++;                                                  row_pos++;
1084                                                  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]));
1085                                                  break;                                                  break;
1086                                          }                                          }
1087                                          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
1088                                          {                                          {
1089                                                  // last display line does NOT have \n in the end                                                  // last display line does NOT have \n in the end
1090                                                  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;
1091                                                  break;                                                  break;
1092                                          }                                          }
1093                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
1094                                          output_current_row = screen_row_total;                                          output_current_row = screen_row_total;
1095                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1096                                          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]));
1097                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1098                                          clrtoeol();                                          clrtoeol();
1099                                          // prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
# Line 1042  int editor_display(EDITOR_DATA *p_editor Line 1111  int editor_display(EDITOR_DATA *p_editor
1111                                          }                                          }
1112                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1113                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1114                                          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]));
1115                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1116                                          break;                                          break;
1117                                  case KEY_PGDN:                                  case KEY_PGDN:
# Line 1057  int editor_display(EDITOR_DATA *p_editor Line 1126  int editor_display(EDITOR_DATA *p_editor
1126                                          }                                          }
1127                                          output_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1128                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1129                                          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]));
1130                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1131                                          break;                                          break;
1132                                    case Ctrl('Q'):
1133                                  case KEY_F1:                                  case KEY_F1:
1134                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not re-entrant
1135                                          {                                          {
1136                                                  break;                                                  break;
1137                                          }                                          }
1138                                          // Display help information                                          // Display help information
1139                                          show_help = 0;                                          show_help = 0;
1140                                          display_file(DATA_READ_HELP, 1);                                          display_file(DATA_EDITOR_HELP, 1);
1141                                          show_help = 1;                                          show_help = 1;
1142                                  case KEY_F5:                                  case KEY_F5:
1143                                          // Refresh after display help information                                          // Refresh after display help information
# Line 1083  int editor_display(EDITOR_DATA *p_editor Line 1153  int editor_display(EDITOR_DATA *p_editor
1153                                          break;                                          break;
1154                                  }                                  }
1155    
1156                                  BBS_last_access_tm = time(NULL);                                  // Refresh current action while user input
1157                                    if (user_online_update(NULL) < 0)
1158                                    {
1159                                            log_error("user_online_update(NULL) error\n");
1160                                    }
1161    
1162                                  if (input_ok)                                  if (input_ok)
1163                                  {                                  {
1164                                          break;                                          break;
1165                                  }                                  }
1166    
1167                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(BBS_max_user_idle_time);
1168                          }                          }
1169    
1170                          continue;                          continue;


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

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