/[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.43 by sysadm, Wed Oct 1 02:49:13 2025 UTC Revision 1.65 by sysadm, Sat Jan 3 10:27:14 2026 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-2026  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
# Line 24  Line 20 
20  #include "str_process.h"  #include "str_process.h"
21  #include <stdlib.h>  #include <stdlib.h>
22  #include <string.h>  #include <string.h>
23    #include <wchar.h>
24  #include <sys/param.h>  #include <sys/param.h>
25    
26  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  enum _editor_constant_t
27  #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000  {
28  #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)          EDITOR_MEM_POOL_LINE_PER_CHUNK = 1000,
29            EDITOR_MEM_POOL_CHUNK_LIMIT = (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1),
30    };
31    
32    static const char EDITOR_ESC_DISPLAY_STR[] = "\033[32m*\033[m";
33    
34  static MEMORY_POOL *p_mp_data_line;  static MEMORY_POOL *p_mp_data_line;
35  static MEMORY_POOL *p_mp_editor_data;  static MEMORY_POOL *p_mp_editor_data;
# Line 37  int editor_memory_pool_init(void) Line 38  int editor_memory_pool_init(void)
38  {  {
39          if (p_mp_data_line != NULL || p_mp_editor_data != NULL)          if (p_mp_data_line != NULL || p_mp_editor_data != NULL)
40          {          {
41                  log_error("Editor mem pool already initialized\n");                  log_error("Editor mem pool already initialized");
42                  return -1;                  return -1;
43          }          }
44    
45          p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT);          p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT);
46          if (p_mp_data_line == NULL)          if (p_mp_data_line == NULL)
47          {          {
48                  log_error("Memory pool init error\n");                  log_error("Memory pool init error");
49                  return -2;                  return -2;
50          }          }
51    
52          p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);          p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
53          if (p_mp_data_line == NULL)          if (p_mp_editor_data == NULL)
54          {          {
55                  log_error("Memory pool init error\n");                  log_error("Memory pool init error");
56                  return -3;                  return -3;
57          }          }
58    
# Line 80  EDITOR_DATA *editor_data_load(const char Line 81  EDITOR_DATA *editor_data_load(const char
81          long line_offsets[MAX_EDITOR_DATA_LINES + 1];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
82          long current_data_line_length = 0;          long current_data_line_length = 0;
83          long i;          long i;
84            int j;
85    
86          if (p_data == NULL)          if (p_data == NULL)
87          {          {
88                  log_error("editor_data_load() error: NULL pointer\n");                  log_error("NULL pointer error");
89                  return NULL;                  return NULL;
90          }          }
91    
92          p_editor_data = memory_pool_alloc(p_mp_editor_data);          p_editor_data = memory_pool_alloc(p_mp_editor_data);
93          if (p_editor_data == NULL)          if (p_editor_data == NULL)
94          {          {
95                  log_error("memory_pool_alloc() error\n");                  log_error("memory_pool_alloc() error");
96                  return NULL;                  return NULL;
97          }          }
98    
# Line 109  EDITOR_DATA *editor_data_load(const char Line 111  EDITOR_DATA *editor_data_load(const char
111                          p_data_line = memory_pool_alloc(p_mp_data_line);                          p_data_line = memory_pool_alloc(p_mp_data_line);
112                          if (p_data_line == NULL)                          if (p_data_line == NULL)
113                          {                          {
114                                  log_error("memory_pool_alloc() error: i = %d\n", i);                                  log_error("memory_pool_alloc() error: i = %d", i);
115                                  // Cleanup                                  // Cleanup
116                                  editor_data_cleanup(p_editor_data);                                  editor_data_cleanup(p_editor_data);
117                                  return NULL;                                  return NULL;
# Line 126  EDITOR_DATA *editor_data_load(const char Line 128  EDITOR_DATA *editor_data_load(const char
128                  memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]);                  memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]);
129                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
130    
131                    // Convert \t to single space
132                    for (j = 0; j < p_editor_data->display_line_lengths[i]; j++)
133                    {
134                            if (p_editor_data->p_display_lines[i][j] == '\t')
135                            {
136                                    p_editor_data->p_display_lines[i][j] = ' ';
137                            }
138                    }
139    
140                  // Trim \n from last line                  // Trim \n from last line
141                  if (i + 1 == p_editor_data->display_line_total &&                  if (i + 1 == p_editor_data->display_line_total &&
142                          p_editor_data->display_line_lengths[i] > 0 &&                          p_editor_data->display_line_lengths[i] > 0 &&
# Line 150  long editor_data_save(const EDITOR_DATA Line 161  long editor_data_save(const EDITOR_DATA
161    
162          if (p_editor_data == NULL || p_data == NULL)          if (p_editor_data == NULL || p_data == NULL)
163          {          {
164                  log_error("editor_data_save() error: NULL pointer\n");                  log_error("NULL pointer error");
165                  return -1;                  return -1;
166          }          }
167    
# Line 158  long editor_data_save(const EDITOR_DATA Line 169  long editor_data_save(const EDITOR_DATA
169          {          {
170                  if (current_pos + p_editor_data->display_line_lengths[i] + 1 > buf_len)                  if (current_pos + p_editor_data->display_line_lengths[i] + 1 > buf_len)
171                  {                  {
172                          log_error("Data buffer not longer enough %d > %d\n", current_pos + p_editor_data->display_line_lengths[i] + 1, buf_len);                          log_error("Data buffer not longer enough %d > %d", current_pos + p_editor_data->display_line_lengths[i] + 1, buf_len);
173                          p_data[current_pos] = '\0';                          p_data[current_pos] = '\0';
174                          return -2;                          return -2;
175                  }                  }
# Line 224  int editor_data_insert(EDITOR_DATA *p_ed Line 235  int editor_data_insert(EDITOR_DATA *p_ed
235    
236          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
237          {          {
238                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error");
239                  return -1;                  return -1;
240          }          }
241    
# Line 261  int editor_data_insert(EDITOR_DATA *p_ed Line 272  int editor_data_insert(EDITOR_DATA *p_ed
272          {          {
273                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
274                  {                  {
275  #ifdef _DEBUG                          log_debug("Split line error, display_line_total(%ld) reach limit(%d)",
                         log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",  
276                                            p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                                            p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
 #endif  
277    
278                          return -2;                          return -2;
279                  }                  }
# Line 273  int editor_data_insert(EDITOR_DATA *p_ed Line 282  int editor_data_insert(EDITOR_DATA *p_ed
282                  p_data_line = memory_pool_alloc(p_mp_data_line);                  p_data_line = memory_pool_alloc(p_mp_data_line);
283                  if (p_data_line == NULL)                  if (p_data_line == NULL)
284                  {                  {
285                          log_error("memory_pool_alloc() error\n");                          log_error("memory_pool_alloc() error");
286                          return -2;                          return -2;
287                  }                  }
288    
# Line 358  int editor_data_insert(EDITOR_DATA *p_ed Line 367  int editor_data_insert(EDITOR_DATA *p_ed
367                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
368                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
369                          {                          {
370  #ifdef _DEBUG                                  log_debug("display_line_total over limit %d >= %d", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
                                 log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);  
 #endif  
371    
372                                  // Terminate prior display line with \n, to avoid error on cleanup                                  // Terminate prior display line with \n, to avoid error on cleanup
373                                  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)
# Line 445  int editor_data_insert(EDITOR_DATA *p_ed Line 452  int editor_data_insert(EDITOR_DATA *p_ed
452  }  }
453    
454  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,
455                                             long *p_last_updated_line)                                             long *p_last_updated_line, int del_line)
456  {  {
457          long display_line = *p_display_line;          long display_line = *p_display_line;
458          long offset = *p_offset;          long offset = *p_offset;
# Line 462  int editor_data_delete(EDITOR_DATA *p_ed Line 469  int editor_data_delete(EDITOR_DATA *p_ed
469    
470          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
471          {          {
472                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error");
473                  return -1;                  return -1;
474          }          }
475    
# Line 500  int editor_data_delete(EDITOR_DATA *p_ed Line 507  int editor_data_delete(EDITOR_DATA *p_ed
507          }          }
508    
509          // Check str to be deleted          // Check str to be deleted
510          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (del_line)
511            {
512                    str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
513            }
514            else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
515          {          {
516                  str_len = 1;                  str_len = 1;
517          }          }
518          else if (p_data_line[offset_data_line] & 0b10000000) // head of multi-byte character          else if (p_data_line[offset_data_line] & 0x80) // head of multi-byte character
519          {          {
520                  str_len = 1;                  str_len = 1;
521                  c = (p_data_line[offset_data_line] & 0b01110000) << 1;                  c = (p_data_line[offset_data_line] & 0x70) << 1;
522                  while (c & 0b10000000)                  while (c & 0x80)
523                  {                  {
524                          str_len++;                          str_len++;
525                          c = (c & 0b01111111) << 1;                          c = (c & 0x7f) << 1;
526                  }                  }
527          }          }
528          else          else
529          {          {
530                  log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",                  log_error("Some strange character at display_line %ld, offset %ld: %d %d",
531                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
532                  str_len = 1;                  str_len = 1;
533          }          }
534    
535          // Current display line is (almost) empty          // Current display line is (almost) empty
536          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
537                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line &&
538                     p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
539          {          {
540                  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)
541                  {                  {
# Line 627  static int editor_display_key_handler(in Line 639  static int editor_display_key_handler(in
639          {          {
640          case 0: // Set msg          case 0: // Set msg
641                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
642                                   "| 退出[\033[32mCtrl-W\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]",
643                                     (UTF8_fixed_width ? "定宽" : "变宽"));
644                  break;                  break;
645          case KEY_CSI:          case KEY_CSI:
646                  *p_key = KEY_ESC;                  *p_key = KEY_ESC;
# Line 643  int editor_display(EDITOR_DATA *p_editor Line 656  int editor_display(EDITOR_DATA *p_editor
656          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
657          EDITOR_CTX ctx;          EDITOR_CTX ctx;
658          int ch = 0;          int ch = 0;
659          char input_str[4];          char input_str[5];
         char c;  
660          int str_len = 0;          int str_len = 0;
661            wchar_t wcs[2];
662            int wc_len;
663            char c;
664          int input_ok;          int input_ok;
665          const int screen_begin_row = 1;          const int screen_begin_row = 1;
666          const int screen_row_total = SCREEN_ROWS - screen_begin_row;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
# Line 663  int editor_display(EDITOR_DATA *p_editor Line 678  int editor_display(EDITOR_DATA *p_editor
678          int key_insert = 1;          int key_insert = 1;
679          int i, j;          int i, j;
680          char *p_str;          char *p_str;
681            int del_line;
682            int tab_width = 0;
683    
684          clrline(output_current_row, SCREEN_ROWS);          clrline(output_current_row, SCREEN_ROWS);
685    
# Line 701  int editor_display(EDITOR_DATA *p_editor Line 718  int editor_display(EDITOR_DATA *p_editor
718                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
719                          iflush();                          iflush();
720    
721                            tab_width = 0;
722                          str_len = 0;                          str_len = 0;
723                          ch = igetch_t(MAX_DELAY_TIME);                          ch = igetch_t(BBS_max_user_idle_time);
724                          while (!SYS_server_exit)                          while (!SYS_server_exit)
725                          {                          {
726                                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
727                                    {
728                                            BBS_last_access_tm = time(NULL);
729                                    }
730    
731                                  // extended key handler                                  // extended key handler
732                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
733                                  {                                  {
734                                          goto cleanup;                                          goto cleanup;
735                                  }                                  }
736    
737                                  if (ch < 256 && (ch & 0b10000000)) // head of multi-byte character                                  if (ch == '\t')
738                                    {
739                                            ch = ' ';
740                                            tab_width = TAB_SIZE - ((int)(col_pos - 1) % TAB_SIZE) - 1;
741                                    }
742    
743                                    if (ch < 256 && (ch & 0x80)) // head of multi-byte character
744                                  {                                  {
745                                          str_len = 0;                                          str_len = 0;
746                                          c = (char)(ch & 0b11110000);                                          c = (char)(ch & 0xf0);
747                                          while (c & 0b10000000)                                          while (c & 0x80)
748                                          {                                          {
749                                                  input_str[str_len] = (char)(ch - 256);                                                  input_str[str_len] = (char)(ch - 256);
750                                                  str_len++;                                                  str_len++;
751                                                  c = (c & 0b01111111) << 1;                                                  c = (c & 0x7f) << 1;
752    
753                                                  if ((c & 0b10000000) == 0) // Input completed                                                  if ((c & 0x80) == 0) // Input completed
754                                                  {                                                  {
755                                                          break;                                                          break;
756                                                  }                                                  }
# Line 730  int editor_display(EDITOR_DATA *p_editor Line 759  int editor_display(EDITOR_DATA *p_editor
759                                                  ch = igetch(100);                                                // 0.1 second                                                  ch = igetch(100);                                                // 0.1 second
760                                                  if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input                                                  if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
761                                                  {                                                  {
762  #ifdef _DEBUG                                                          log_debug("Ignore %d bytes of incomplete UTF8 character", str_len);
                                                         log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);  
 #endif  
763                                                          str_len = 0;                                                          str_len = 0;
764                                                          break;                                                          break;
765                                                  }                                                  }
766                                          }                                          }
767                                            input_str[str_len] = '\0';
768                                  }                                  }
769    
770                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character                                  if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character
771                                          ch == CR || ch == KEY_ESC)                                // Special character                                          ch == CR || ch == KEY_ESC)                                // Special character
772                                  {                                  {
                                         BBS_last_access_tm = time(NULL);  
   
773                                          // Refresh current action while user input                                          // Refresh current action while user input
774                                          if (user_online_update(NULL) < 0)                                          if (user_online_update(NULL) < 0)
775                                          {                                          {
776                                                  log_error("user_online_update(NULL) error\n");                                                  log_error("user_online_update(NULL) error");
777                                          }                                          }
778    
779                                          if (str_len == 0) // ch >= 32 && ch < 127                                          if (str_len == 0) // ch >= 32 && ch < 127
# Line 766  int editor_display(EDITOR_DATA *p_editor Line 792  int editor_display(EDITOR_DATA *p_editor
792                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
793                                          {                                          {
794                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
795                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line, 0) < 0)
796                                                  {                                                  {
797                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error");
798                                                  }                                                  }
799                                          }                                          }
800    
801                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
802                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
803                                          {                                          {
804                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);                                                  log_error("editor_data_insert(str_len=%d) error", str_len);
805                                          }                                          }
806                                          else                                          else
807                                          {                                          {
# Line 816  int editor_display(EDITOR_DATA *p_editor Line 842  int editor_display(EDITOR_DATA *p_editor
842                                                          }                                                          }
843                                                          if (offset_out > 0)                                                          if (offset_out > 0)
844                                                          {                                                          {
845                                                                  col_pos += (str_len == 1 ? 1 : 2);                                                                  if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
846                                                                    {
847                                                                            log_error("mbstowcs() error");
848                                                                    }
849                                                                    col_pos += (str_len == 1 ? 1 : (UTF8_fixed_width ? 2 : wcwidth(wcs[0])));
850                                                          }                                                          }
851                                                  }                                                  }
852                                          }                                          }
# Line 826  int editor_display(EDITOR_DATA *p_editor Line 856  int editor_display(EDITOR_DATA *p_editor
856                                                  break;                                                  break;
857                                          }                                          }
858    
859                                          ch = igetch(0);                                          if (ch == ' ' && tab_width > 0)
                                         if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input  
860                                          {                                          {
861                                                  break;                                                  tab_width--;
862                                            }
863                                            else
864                                            {
865                                                    ch = igetch(0);
866                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
867                                                    {
868                                                            break;
869                                                    }
870                                          }                                          }
871    
872                                          str_len = 0;                                          str_len = 0;
873                                          continue;                                          continue;
874                                  }                                  }
875                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
876                                  {                                  {
                                         BBS_last_access_tm = time(NULL);  
   
877                                          // Refresh current action while user input                                          // Refresh current action while user input
878                                          if (user_online_update(NULL) < 0)                                          if (user_online_update(NULL) < 0)
879                                          {                                          {
880                                                  log_error("user_online_update(NULL) error\n");                                                  log_error("user_online_update(NULL) error");
881                                          }                                          }
882    
883                                            del_line = 0;
884    
885                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
886                                          {                                          {
887                                                  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
# Line 854  int editor_display(EDITOR_DATA *p_editor Line 891  int editor_display(EDITOR_DATA *p_editor
891    
892                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],                                                  offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
893                                                                                             (int)col_pos - 1, &eol, &display_len, 0);                                                                                             (int)col_pos - 1, &eol, &display_len, 0);
894                                                  if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8                                                  col_pos = display_len;
                                                 {  
                                                         col_pos = display_len - 1;  
                                                 }  
                                                 else  
                                                 {  
                                                         col_pos = display_len;  
                                                 }  
   
895                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)                                                  if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
896                                                  {                                                  {
897                                                          row_pos--;                                                          row_pos--;
898                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
899                                                  }                                                  }
900                                          }                                          }
901                                            else if (ch == Ctrl('K'))
902                                            {
903                                                    del_line = 1;
904                                            }
905                                            else if (ch == Ctrl('Y'))
906                                            {
907                                                    col_pos = 1;
908                                                    del_line = 1;
909                                            }
910    
911                                          display_line_in = line_current - output_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
912                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
# Line 876  int editor_display(EDITOR_DATA *p_editor Line 914  int editor_display(EDITOR_DATA *p_editor
914                                          offset_out = offset_in;                                          offset_out = offset_in;
915    
916                                          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,
917                                                                                                            &last_updated_line)) < 0)                                                                                                            &last_updated_line, del_line)) < 0)
918                                          {                                          {
919                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error: %d", str_len);
920                                          }                                          }
921                                          else                                          else
922                                          {                                          {
# Line 930  int editor_display(EDITOR_DATA *p_editor Line 968  int editor_display(EDITOR_DATA *p_editor
968                                  switch (ch)                                  switch (ch)
969                                  {                                  {
970                                  case KEY_NULL:                                  case KEY_NULL:
971                                            log_debug("KEY_NULL");
972                                            goto cleanup;
973                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
974                                            log_debug("User input timeout");
975                                          goto cleanup;                                          goto cleanup;
976                                  case Ctrl('W'):                                  case Ctrl('W'):
977                                  case Ctrl('X'):                                  case Ctrl('X'):
# Line 1007  int editor_display(EDITOR_DATA *p_editor Line 1048  int editor_display(EDITOR_DATA *p_editor
1048                                  case KEY_LEFT:                                  case KEY_LEFT:
1049                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1050                                                                                     (int)col_pos - 1, &eol, &display_len, 0);                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1051                                          if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8                                          col_pos = display_len;
1052                                            if (offset_in > 0)
1053                                          {                                          {
1054                                                  col_pos = display_len - 1;                                                  str_len = 1;
1055                                          }                                                  offset_in--;
1056                                          else                                                  if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0 ||
1057                                          {                                                          p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] > 127) // UTF8
1058                                                  col_pos = display_len;                                                  {
1059                                                            while (offset_in > 0 &&
1060                                                                       (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xc0) != 0xc0)
1061                                                            {
1062                                                                    str_len++;
1063                                                                    offset_in--;
1064                                                            }
1065    
1066                                                            if (str_len > 4)
1067                                                            {
1068                                                                    log_error("Invalid UTF-8 data detected: str_len > 4");
1069                                                            }
1070    
1071                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1072                                                                    (size_t)-1)
1073                                                            {
1074                                                                    log_error("mbstowcs() error");
1075                                                            }
1076                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1077    
1078                                                            if (wc_len == 2)
1079                                                            {
1080                                                                    col_pos--;
1081                                                            }
1082                                                    }
1083                                          }                                          }
1084                                          if (col_pos >= 1)                                          if (col_pos >= 1)
1085                                          {                                          {
# Line 1039  int editor_display(EDITOR_DATA *p_editor Line 1105  int editor_display(EDITOR_DATA *p_editor
1105                                          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
1106                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[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]));
1107                                          break;                                          break;
                                 case KEY_SPACE:  
                                         break;  
1108                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1109                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1110                                                                                     (int)col_pos - 1, &eol, &display_len, 0);                                                                                     (int)col_pos - 1, &eol, &display_len, 0);
1111                                          if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&                                          col_pos = display_len + 2;
1112                                                  p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8                                          if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
                                         {  
                                                 col_pos = display_len + 3;  
                                         }  
                                         else  
1113                                          {                                          {
1114                                                  col_pos = display_len + 2;                                                  str_len = 0;
1115                                                    if ((p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0x80) ==
1116                                                            0x80) // head of multi-byte character
1117                                                    {
1118                                                            c = (char)(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xf0);
1119                                                            while (c & 0x80)
1120                                                            {
1121                                                                    str_len++;
1122                                                                    c = (c & 0x7f) << 1;
1123                                                            }
1124    
1125                                                            if (str_len > 4)
1126                                                            {
1127                                                                    log_error("Invalid UTF-8 data detected: str_len > 4");
1128                                                            }
1129    
1130                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1131                                                                    (size_t)-1)
1132                                                            {
1133                                                                    log_error("mbstowcs() error");
1134                                                            }
1135                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1136    
1137                                                            if (wc_len == 2)
1138                                                            {
1139                                                                    col_pos++;
1140                                                            }
1141                                                    }
1142                                                    else
1143                                                    {
1144                                                            str_len = 1;
1145                                                    }
1146                                                    offset_in += str_len;
1147                                          }                                          }
1148                                          if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])                                          if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1149                                          {                                          {
# Line 1110  int editor_display(EDITOR_DATA *p_editor Line 1202  int editor_display(EDITOR_DATA *p_editor
1202                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[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]));
1203                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1204                                          break;                                          break;
1205                                    case Ctrl('Q'):
1206                                  case KEY_F1:                                  case KEY_F1:
1207                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not re-entrant
1208                                          {                                          {
1209                                                  break;                                                  break;
1210                                          }                                          }
1211                                          // Display help information                                          // Display help information
1212                                          show_help = 0;                                          show_help = 0;
1213                                          display_file(DATA_READ_HELP, 1);                                          display_file(DATA_EDITOR_HELP, 1);
1214                                          show_help = 1;                                          show_help = 1;
1215                                  case KEY_F5:                                  case KEY_F5:
1216                                          // Refresh after display help information                                          // Refresh after display help information
# Line 1133  int editor_display(EDITOR_DATA *p_editor Line 1226  int editor_display(EDITOR_DATA *p_editor
1226                                          break;                                          break;
1227                                  }                                  }
1228    
                                 BBS_last_access_tm = time(NULL);  
   
1229                                  // Refresh current action while user input                                  // Refresh current action while user input
1230                                  if (user_online_update(NULL) < 0)                                  if (user_online_update(NULL) < 0)
1231                                  {                                  {
1232                                          log_error("user_online_update(NULL) error\n");                                          log_error("user_online_update(NULL) error");
1233                                  }                                  }
1234    
1235                                  if (input_ok)                                  if (input_ok)
# Line 1146  int editor_display(EDITOR_DATA *p_editor Line 1237  int editor_display(EDITOR_DATA *p_editor
1237                                          break;                                          break;
1238                                  }                                  }
1239    
1240                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(BBS_max_user_idle_time);
1241                          }                          }
1242    
1243                          continue;                          continue;
# Line 1155  int editor_display(EDITOR_DATA *p_editor Line 1246  int editor_display(EDITOR_DATA *p_editor
1246                  len = p_editor_data->display_line_lengths[line_current];                  len = p_editor_data->display_line_lengths[line_current];
1247                  if (len >= sizeof(buffer))                  if (len >= sizeof(buffer))
1248                  {                  {
1249                          log_error("Buffer overflow: len=%ld line=%ld \n", len, line_current);                          log_error("Buffer overflow: len=%ld line=%ld ", len, line_current);
1250                          len = sizeof(buffer) - 1;                          len = sizeof(buffer) - 1;
1251                  }                  }
1252                  else if (len < 0)                  else if (len < 0)
1253                  {                  {
1254                          log_error("Incorrect line offsets: len=%ld line=%ld \n", len, line_current);                          log_error("Incorrect line offsets: len=%ld line=%ld ", len, line_current);
1255                          len = 0;                          len = 0;
1256                  }                  }
1257    


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

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