/[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.13 by sysadm, Wed Jun 11 13:04:43 2025 UTC Revision 1.61 by sysadm, Wed Dec 17 03:47:00 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     */
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    
 #include "editor.h"  
13  #include "bbs.h"  #include "bbs.h"
14    #include "common.h"
15    #include "editor.h"
16  #include "io.h"  #include "io.h"
17  #include "log.h"  #include "log.h"
18  #include "common.h"  #include "login.h"
19    #include "memory_pool.h"
20  #include "str_process.h"  #include "str_process.h"
21  #include <stdlib.h>  #include <stdlib.h>
22    #include <string.h>
23    #include <wchar.h>
24  #include <sys/param.h>  #include <sys/param.h>
 #include <strings.h>  
25    
26  #define _POSIX_C_SOURCE 200809L  enum _editor_constant_t
27  #include <string.h>  {
28            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;
35    static MEMORY_POOL *p_mp_editor_data;
36    
37    int editor_memory_pool_init(void)
38    {
39            if (p_mp_data_line != NULL || p_mp_editor_data != NULL)
40            {
41                    log_error("Editor mem pool already initialized\n");
42                    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);
46            if (p_mp_data_line == NULL)
47            {
48                    log_error("Memory pool init error\n");
49                    return -2;
50            }
51    
52            p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
53            if (p_mp_editor_data == NULL)
54            {
55                    log_error("Memory pool init error\n");
56                    return -3;
57            }
58    
59            return 0;
60    }
61    
62  #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"  void editor_memory_pool_cleanup(void)
63    {
64            if (p_mp_data_line != NULL)
65            {
66                    memory_pool_cleanup(p_mp_data_line);
67                    p_mp_data_line = NULL;
68            }
69    
70            if (p_mp_editor_data != NULL)
71            {
72                    memory_pool_cleanup(p_mp_editor_data);
73                    p_mp_editor_data = NULL;
74            }
75    }
76    
77  EDITOR_DATA *editor_data_load(const char *p_data)  EDITOR_DATA *editor_data_load(const char *p_data)
78  {  {
79          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
80          char *p_data_line = NULL;          char *p_data_line = NULL;
81          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
82          long current_data_line_length = 0;          long current_data_line_length = 0;
83          long i, j;          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\n");
89                  return NULL;                  return NULL;
90          }          }
91    
92          p_editor_data = malloc(sizeof(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("malloc(EDITOR_DATA) error: OOM\n");                  log_error("memory_pool_alloc() error\n");
96                  return NULL;                  return NULL;
97          }          }
98    
99          p_editor_data->data_line_total = 0;          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1,
100          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);                                                                                                                   0, p_editor_data->display_line_widths);
101    
102          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
103          {          {
# Line 61  EDITOR_DATA *editor_data_load(const char Line 107  EDITOR_DATA *editor_data_load(const char
107                          current_data_line_length + p_editor_data->display_line_lengths[i] + 1 > MAX_EDITOR_DATA_LINE_LENGTH ||                          current_data_line_length + p_editor_data->display_line_lengths[i] + 1 > MAX_EDITOR_DATA_LINE_LENGTH ||
108                          (p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n'))                          (p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n'))
109                  {                  {
                         if (p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES)  
                         {  
                                 log_error("Append line error, data_line_total(%ld) reach limit(%d)\n", p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);  
                                 return NULL;  
                         }  
   
110                          // Allocate new data line                          // Allocate new data line
111                          p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                          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("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);                                  log_error("memory_pool_alloc() error: i = %d\n", i);
115                                  // Cleanup                                  // Cleanup
116                                  for (j = p_editor_data->data_line_total - 1; j >= 0; j--)                                  editor_data_cleanup(p_editor_data);
                                 {  
                                         free(p_editor_data->p_data_lines[j]);  
                                 }  
                                 free(p_editor_data);  
117                                  return NULL;                                  return NULL;
118                          }                          }
                         p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                         (p_editor_data->data_line_total)++;  
119    
120                          p_editor_data->p_display_lines[i] = p_data_line;                          p_editor_data->p_display_lines[i] = p_data_line;
121                          current_data_line_length = 0;                          current_data_line_length = 0;
# Line 93  EDITOR_DATA *editor_data_load(const char Line 127  EDITOR_DATA *editor_data_load(const char
127    
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
141                    if (i + 1 == p_editor_data->display_line_total &&
142                            p_editor_data->display_line_lengths[i] > 0 &&
143                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
144                    {
145                            p_editor_data->display_line_lengths[i]--;
146                            p_editor_data->display_line_widths[i]--;
147                            current_data_line_length--;
148                    }
149                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
150          }          }
151    
152          bzero(p_editor_data->p_data_lines + p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->data_line_total);          memset(p_editor_data->p_display_lines + p_editor_data->display_line_total, 0, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);
         bzero(p_editor_data->p_display_lines + p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);  
153    
154          return p_editor_data;          return p_editor_data;
155  }  }
# Line 109  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\n");
165                  return -1;                  return -1;
166          }          }
167    
# Line 133  long editor_data_save(const EDITOR_DATA Line 185  long editor_data_save(const EDITOR_DATA
185    
186  void editor_data_cleanup(EDITOR_DATA *p_editor_data)  void editor_data_cleanup(EDITOR_DATA *p_editor_data)
187  {  {
188            char *p_data_line = NULL;
189          long i;          long i;
190    
191          if (p_editor_data == NULL)          if (p_editor_data == NULL)
# Line 140  void editor_data_cleanup(EDITOR_DATA *p_ Line 193  void editor_data_cleanup(EDITOR_DATA *p_
193                  return;                  return;
194          }          }
195    
196          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)          for (i = 0; i < p_editor_data->display_line_total; i++)
197          {          {
198                  free(p_editor_data->p_data_lines[i]);                  if (p_data_line == NULL)
199                  p_editor_data->p_data_lines[i] = NULL;                  {
200                            p_data_line = p_editor_data->p_display_lines[i];
201                    }
202    
203                    if (p_editor_data->display_line_lengths[i] > 0 &&
204                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
205                    {
206                            memory_pool_free(p_mp_data_line, p_data_line);
207                            p_data_line = NULL;
208                    }
209            }
210    
211            if (p_data_line != NULL)
212            {
213                    memory_pool_free(p_mp_data_line, p_data_line);
214          }          }
215    
216          free(p_editor_data);          memory_pool_free(p_mp_editor_data, p_editor_data);
217  }  }
218    
219  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
# Line 159  int editor_data_insert(EDITOR_DATA *p_ed Line 226  int editor_data_insert(EDITOR_DATA *p_ed
226          long offset_data_line;          long offset_data_line;
227          long last_display_line; // of data line          long last_display_line; // of data line
228          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
229            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
230          long split_line_total;          long split_line_total;
231          long i, j;          long i;
232            int len;
233            int eol;
234            int display_len;
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\n");
239                  return -1;                  return -1;
240          }          }
241    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
242          // Get length of current data line          // Get length of current data line
243          len_data_line = 0;          len_data_line = 0;
244          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 210  int editor_data_insert(EDITOR_DATA *p_ed Line 268  int editor_data_insert(EDITOR_DATA *p_ed
268          }          }
269    
270          // Split current data line if over-length          // Split current data line if over-length
271          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)          if (len_data_line + str_len + 2 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
272          {          {
273                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES || p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES)                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
274                  {                  {
275                          log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n",  #ifdef _DEBUG
276                                            p_editor_data->display_line_total, p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);                          log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
277                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
278    #endif
279    
280                          return -2;                          return -2;
281                  }                  }
282    
283                  // Allocate new data line                  // Allocate new data line
284                  p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                  p_data_line = memory_pool_alloc(p_mp_data_line);
285                  if (p_data_line == NULL)                  if (p_data_line == NULL)
286                  {                  {
287                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");                          log_error("memory_pool_alloc() error\n");
288                          return -2;                          return -2;
289                  }                  }
                 p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                 (p_editor_data->data_line_total)++;  
290    
291                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)                  if (offset_data_line + str_len + 2 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
292                  {                  {
293                          if (str[0] == CR)                          if (str[0] == CR)
294                          {                          {
# Line 275  int editor_data_insert(EDITOR_DATA *p_ed Line 334  int editor_data_insert(EDITOR_DATA *p_ed
334                          *p_offset = offset + str_len;                          *p_offset = offset + str_len;
335                  }                  }
336    
337                    // Update display width of current display line
338                    len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0);
339                    p_editor_data->display_line_widths[display_line] = display_len;
340    
341                  split_line_total = last_display_line - display_line + 3;                  split_line_total = last_display_line - display_line + 3;
342    
343                  // Set start display_line for spliting new data line                  // Set start display_line for spliting new data line
# Line 297  int editor_data_insert(EDITOR_DATA *p_ed Line 360  int editor_data_insert(EDITOR_DATA *p_ed
360          }          }
361    
362          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
363          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
364    
365          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
366          {          {
# Line 306  int editor_data_insert(EDITOR_DATA *p_ed Line 369  int editor_data_insert(EDITOR_DATA *p_ed
369                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
370                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
371                          {                          {
372    #ifdef _DEBUG
373                                  log_error("display_line_total over limit %d >= %d\n", 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);
374                                  return -3;  #endif
375                          }  
376                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)                                  // Terminate prior display line with \n, to avoid error on cleanup
377                          {                                  if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
378                                  p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];                                  {
379                                  p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];                                          len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
380                                            p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
381                                            p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
382                                            p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
383                                            p_editor_data->display_line_widths[display_line + i - 1] = display_len;
384                                    }
385                                    if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
386                                    {
387                                            *p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1;
388                                    }
389                                    break;
390                          }                          }
391    
392                            // for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
393                            // {
394                            //      p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
395                            //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
396                            //      p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1];
397                            // }
398                            memmove(p_editor_data->p_display_lines + last_display_line + 2,
399                                            p_editor_data->p_display_lines + last_display_line + 1,
400                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
401                                                    sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
402                            memmove(p_editor_data->display_line_lengths + last_display_line + 2,
403                                            p_editor_data->display_line_lengths + last_display_line + 1,
404                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
405                                                    sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
406                            memmove(p_editor_data->display_line_widths + last_display_line + 2,
407                                            p_editor_data->display_line_widths + last_display_line + 1,
408                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
409                                                    sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
410    
411                          last_display_line++;                          last_display_line++;
412                            *p_last_updated_line = p_editor_data->display_line_total;
413                          (p_editor_data->display_line_total)++;                          (p_editor_data->display_line_total)++;
414                  }                  }
415    
416                  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];
417                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
418                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
419                          (i == 0                          (i == 0
420                                   ? p_data_line                                   ? p_data_line
# Line 333  int editor_data_insert(EDITOR_DATA *p_ed Line 429  int editor_data_insert(EDITOR_DATA *p_ed
429    
430          *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);          *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
431    
432          if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] ||          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
                 (*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] &&  
                  p_editor_data->p_display_lines[*p_display_line][*p_offset - 1] == '\n'))  
433          {          {
434                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                  if (*p_display_line + 1 < p_editor_data->display_line_total)
435                  (*p_display_line)++;                  {
436                            *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
437                            (*p_display_line)++;
438                    }
439            }
440    
441                  if (*p_display_line >= p_editor_data->display_line_total)          // Prevent the last display line from being over-length
442            if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
443            {
444                    len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
445                    p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
446                    p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
447                    p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len;
448                    if (*p_display_line + 1 >= p_editor_data->display_line_total)
449                  {                  {
450                          log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);                          *p_offset = MIN(*p_offset, len);
451                            *p_display_line = p_editor_data->display_line_total - 1;
452                  }                  }
453          }          }
454    
455          return 0;          return 0;
456  }  }
457    
458  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,  int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
459                                             long *p_last_updated_line)                                             long *p_last_updated_line, int del_line)
460  {  {
461            long display_line = *p_display_line;
462            long offset = *p_offset;
463          char *p_data_line = NULL;          char *p_data_line = NULL;
464          long len_data_line;          long len_data_line;
465          long offset_data_line;          long offset_data_line;
466          long last_display_line; // of data line          long last_display_line; // of data line
467          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
468            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
469          long split_line_total;          long split_line_total;
470          long i, j;          long i, j;
471          int str_len = 0;          int str_len = 0;
472            char c;
473    
474          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
475          {          {
476                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
477                  return -1;                  return -1;
478          }          }
479    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
480          // Get length of current data line          // Get length of current data line
481          len_data_line = 0;          len_data_line = 0;
482          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
# Line 408  int editor_data_delete(EDITOR_DATA *p_ed Line 505  int editor_data_delete(EDITOR_DATA *p_ed
505                  }                  }
506          }          }
507    
508            if (offset_data_line >= len_data_line) // end-of-line
509            {
510                    return 0;
511            }
512    
513          // Check str to be deleted          // Check str to be deleted
514          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (del_line)
515            {
516                    str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
517            }
518            else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
519          {          {
520                  str_len = 1;                  str_len = 1;
521          }          }
522          else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK          else if (p_data_line[offset_data_line] & 0x80) // head of multi-byte character
523          {          {
524                  str_len = 2;                  str_len = 1;
525                    c = (p_data_line[offset_data_line] & 0x70) << 1;
526                    while (c & 0x80)
527                    {
528                            str_len++;
529                            c = (c & 0x7f) << 1;
530                    }
531          }          }
532          else          else
533          {          {
534                  log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n",                  log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
535                                    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]);
                                   p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]);  
536                  str_len = 1;                  str_len = 1;
537          }          }
538    
539          // Current display line is (almost) empty          // Current display line is (almost) empty
540          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
541                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line &&
542                     p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
543          {          {
544                  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)
545                  {                  {
                         log_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total);  
546                          return 0;                          return 0;
547                  }                  }
548    
# Line 451  int editor_data_delete(EDITOR_DATA *p_ed Line 562  int editor_data_delete(EDITOR_DATA *p_ed
562    
563                  if (offset_data_line + len_data_line + 1 > MAX_EDITOR_DATA_LINE_LENGTH) // No enough buffer to merge current data line with next data line                  if (offset_data_line + len_data_line + 1 > MAX_EDITOR_DATA_LINE_LENGTH) // No enough buffer to merge current data line with next data line
564                  {                  {
                         log_common("Debug: No enough buffer to merge with next data line: %ld > %ld\n",  
                                            offset_data_line + len_data_line + 1, MAX_EDITOR_DATA_LINE_LENGTH);  
565                          return 0;                          return 0;
566                  }                  }
567    
# Line 461  int editor_data_delete(EDITOR_DATA *p_ed Line 570  int editor_data_delete(EDITOR_DATA *p_ed
570                  p_data_line[offset_data_line + len_data_line] = '\0';                  p_data_line[offset_data_line + len_data_line] = '\0';
571    
572                  // Recycle next data line                  // Recycle next data line
573                  // TODO: free(p_editor_data->p_display_lines[display_line + 1]);                  memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]);
574          }          }
575          else          else
576          {          {
# Line 475  int editor_data_delete(EDITOR_DATA *p_ed Line 584  int editor_data_delete(EDITOR_DATA *p_ed
584          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
585    
586          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
587          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
588    
589          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
590          {          {
591                  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];
592                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
593                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
594                          (i == 0                          (i == 0
595                                   ? p_data_line                                   ? p_data_line
# Line 494  int editor_data_delete(EDITOR_DATA *p_ed Line 604  int editor_data_delete(EDITOR_DATA *p_ed
604    
605          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
606    
607          if (display_line + i < last_display_line)          if (*p_last_updated_line < last_display_line)
608          {          {
609                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
610                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)                  // for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
611                  {                  // {
612                          p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = 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];
613                          p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = 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];
614                  }                  //      p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j];
615                    // }
616                  (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));                  memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
617                  last_display_line = display_line + i;                                  p_editor_data->p_display_lines + last_display_line + 1,
618                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
619                  *p_last_updated_line = p_editor_data->display_line_total - 1;                                          sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
620                    memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
621                                    p_editor_data->display_line_lengths + last_display_line + 1,
622                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
623                                            sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
624                    memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1,
625                                    p_editor_data->display_line_widths + last_display_line + 1,
626                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
627                                            sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
628    
629                    j = p_editor_data->display_line_total;
630                    (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
631                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
632          }          }
633    
634            // Return real offset
635            *p_offset = offset;
636    
637          return str_len;          return str_len;
638  }  }
639    
# Line 518  static int editor_display_key_handler(in Line 643  static int editor_display_key_handler(in
643          {          {
644          case 0: // Set msg          case 0: // Set msg
645                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
646                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]",
647                                     (UTF8_fixed_width ? "定宽" : "å˜å®½"));
648                    break;
649            case KEY_CSI:
650                    *p_key = KEY_ESC;
651                  break;                  break;
652          }          }
653    
# Line 531  int editor_display(EDITOR_DATA *p_editor Line 660  int editor_display(EDITOR_DATA *p_editor
660          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
661          EDITOR_CTX ctx;          EDITOR_CTX ctx;
662          int ch = 0;          int ch = 0;
663          char input_str[4];          char input_str[5];
664          int str_len = 0;          int str_len = 0;
665            wchar_t wcs[2];
666            int wc_len;
667            char c;
668          int input_ok;          int input_ok;
         int screen_current_row;  
669          const int screen_begin_row = 1;          const int screen_begin_row = 1;
670          int screen_end_row = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
671          const int screen_row_total = screen_end_row - screen_begin_row + 1;          int output_current_row = screen_begin_row;
672            int output_end_row = SCREEN_ROWS - 1;
673          long int line_current = 0;          long int line_current = 0;
674          long int len;          long int len;
675          int loop;          int loop;
# Line 550  int editor_display(EDITOR_DATA *p_editor Line 682  int editor_display(EDITOR_DATA *p_editor
682          int key_insert = 1;          int key_insert = 1;
683          int i, j;          int i, j;
684          char *p_str;          char *p_str;
685            int del_line;
686            int tab_width = 0;
687    
688          screen_current_row = screen_begin_row;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_row, SCREEN_ROWS);  
689    
690          // update msg_ext with extended key handler          // update msg_ext with extended key handler
691          if (editor_display_key_handler(&ch, &ctx) != 0)          if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 560  int editor_display(EDITOR_DATA *p_editor Line 693  int editor_display(EDITOR_DATA *p_editor
693                  return ch;                  return ch;
694          }          }
695    
696          loop = 1;          for (loop = 1; !SYS_server_exit && loop;)
         while (!SYS_server_exit && loop)  
697          {          {
698                  if (line_current >= p_editor_data->display_line_total || screen_current_row > screen_end_row)                  if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row)
699                  {                  {
700                          ctx.line_cursor = line_current - screen_current_row + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
701    
702                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
703                                           "\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] "
704                                           "µÚ\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] "
705                                           "%s",                                           "%s",
706                                           row_pos, col_pos,                                           row_pos, col_pos,
707                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
708                                           key_insert ? "²åÈë" : "¸Äд",                                           key_insert ? "æ’å…¥" : "替æ¢",
709                                           ctx.msg);                                           ctx.msg);
710    
711                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
712                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
713                          {                          {
714                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 590  int editor_display(EDITOR_DATA *p_editor Line 722  int editor_display(EDITOR_DATA *p_editor
722                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
723                          iflush();                          iflush();
724    
725                          input_ok = 0;                          tab_width = 0;
726                          ch = igetch_t(MAX_DELAY_TIME);                          str_len = 0;
727                          while (!SYS_server_exit && !input_ok)                          ch = igetch_t(BBS_max_user_idle_time);
728                            while (!SYS_server_exit)
729                          {                          {
730                                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
731                                    {
732                                            BBS_last_access_tm = time(NULL);
733                                    }
734    
735                                  // extended key handler                                  // extended key handler
736                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
737                                  {                                  {
738                                          goto cleanup;                                          goto cleanup;
739                                  }                                  }
740    
741                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch == '\t')
742                                  {                                  {
743                                          input_str[str_len] = (char)(ch - 256);                                          ch = ' ';
744                                          str_len++;                                          tab_width = TAB_SIZE - ((int)(col_pos - 1) % TAB_SIZE) - 1;
745                                  }                                  }
746                                  else  
747                                    if (ch < 256 && (ch & 0x80)) // head of multi-byte character
748                                  {                                  {
749                                          str_len = 0;                                          str_len = 0;
750                                            c = (char)(ch & 0xf0);
751                                            while (c & 0x80)
752                                            {
753                                                    input_str[str_len] = (char)(ch - 256);
754                                                    str_len++;
755                                                    c = (c & 0x7f) << 1;
756    
757                                                    if ((c & 0x80) == 0) // Input completed
758                                                    {
759                                                            break;
760                                                    }
761    
762                                                    // Expect additional bytes of input
763                                                    ch = igetch(100);                                                // 0.1 second
764                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
765                                                    {
766    #ifdef _DEBUG
767                                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
768    #endif
769                                                            str_len = 0;
770                                                            break;
771                                                    }
772                                            }
773                                            input_str[str_len] = '\0';
774                                  }                                  }
775    
776                                  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
777                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character                                          ch == CR || ch == KEY_ESC)                                // Special character
778                                  {                                  {
779                                          if (str_len == 0)                                          // Refresh current action while user input
780                                            if (user_online_update(NULL) < 0)
781                                            {
782                                                    log_error("user_online_update(NULL) error\n");
783                                            }
784    
785                                            if (str_len == 0) // ch >= 32 && ch < 127
786                                          {                                          {
787                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
788                                                  str_len = 1;                                                  str_len = 1;
789                                          }                                          }
790    
791                                          last_updated_line = line_current;                                          display_line_in = line_current - output_current_row + row_pos;
792                                          display_line_in = line_current - screen_current_row + row_pos;                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
                                         offset_in = col_pos - 1;  
793                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
794                                          offset_out = offset_in;                                          offset_out = offset_in;
795    
796                                            last_updated_line = display_line_in;
797    
798                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
799                                          {                                          {
800                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
801                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line, 0) < 0)
802                                                  {                                                  {
803                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
804                                                  }                                                  }
# Line 637  int editor_display(EDITOR_DATA *p_editor Line 807  int editor_display(EDITOR_DATA *p_editor
807                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
808                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
809                                          {                                          {
810                                                  log_error("editor_data_insert(%s) error\n", input_str);                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);
                                                 str_len = 0;  
811                                          }                                          }
812                                          else                                          else
813                                          {                                          {
814                                                  str_len = 0;                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
815                                                    line_current -= (output_current_row - row_pos);
816                                                    output_current_row = (int)row_pos;
817    
818                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row));
                                                 line_current -= (screen_current_row - row_pos);  
                                                 screen_current_row = (int)row_pos;  
   
                                                 scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row));  
819    
820                                                  if (scroll_rows > 0)                                                  if (scroll_rows > 0)
821                                                  {                                                  {
# Line 656  int editor_display(EDITOR_DATA *p_editor Line 823  int editor_display(EDITOR_DATA *p_editor
823                                                          clrtoeol();                                                          clrtoeol();
824                                                          for (i = 0; i < scroll_rows; i++)                                                          for (i = 0; i < scroll_rows; i++)
825                                                          {                                                          {
826                                                                  prints("\033[S"); // Scroll up 1 line                                                                  // prints("\033[S"); // Scroll up 1 line
827                                                                    prints("\n"); // Legacy Cterm only works with this line
828                                                          }                                                          }
829    
830                                                          screen_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
831                                                          if (screen_current_row < screen_begin_row)                                                          if (output_current_row < screen_begin_row)
832                                                          {                                                          {
833                                                                  line_current += (screen_begin_row - screen_current_row);                                                                  line_current += (screen_begin_row - output_current_row);
834                                                                  screen_current_row = screen_begin_row;                                                                  output_current_row = screen_begin_row;
835                                                          }                                                          }
836                                                          row_pos = screen_end_row;                                                          row_pos = output_end_row;
837                                                  }                                                  }
838                                                  else // if (scroll_lines == 0)                                                  else // if (scroll_lines == 0)
839                                                  {                                                  {
840                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
841                                                  }                                                  }
842                                                  col_pos = offset_out + 1;  
843                                                    if (offset_out != offset_in)
844                                                    {
845                                                            if (display_line_out != display_line_in)
846                                                            {
847                                                                    col_pos = 1;
848                                                            }
849                                                            if (offset_out > 0)
850                                                            {
851                                                                    if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
852                                                                    {
853                                                                            log_error("mbstowcs() error\n");
854                                                                    }
855                                                                    col_pos += (str_len == 1 ? 1 : (UTF8_fixed_width ? 2 : wcwidth(wcs[0])));
856                                                            }
857                                                    }
858                                          }                                          }
859    
860                                          // Check whether there is additional input                                          if (display_line_out != display_line_in) // Output on line change
                                         ch = igetch(0);  
                                         if (ch == KEY_TIMEOUT)  
861                                          {                                          {
862                                                  input_ok = 1;                                                  break;
863                                            }
864    
865                                            if (ch == ' ' && tab_width > 0)
866                                            {
867                                                    tab_width--;
868                                          }                                          }
869                                            else
870                                            {
871                                                    ch = igetch(0);
872                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
873                                                    {
874                                                            break;
875                                                    }
876                                            }
877    
878                                            str_len = 0;
879                                          continue;                                          continue;
880                                  }                                  }
881                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
882                                  {                                  {
883                                            // Refresh current action while user input
884                                            if (user_online_update(NULL) < 0)
885                                            {
886                                                    log_error("user_online_update(NULL) error\n");
887                                            }
888    
889                                            del_line = 0;
890    
891                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
892                                          {                                          {
893                                                  col_pos--;                                                  if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
894                                                  if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0)                                                  {
895                                                            break; // force output prior operation result if any
896                                                    }
897    
898                                                    offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
899                                                                                               (int)col_pos - 1, &eol, &display_len, 0);
900                                                    col_pos = display_len;
901                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
902                                                  {                                                  {
903                                                          row_pos--;                                                          row_pos--;
904                                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
905                                                  }                                                  }
906                                          }                                          }
907                                            else if (ch == Ctrl('K'))
908                                            {
909                                                    del_line = 1;
910                                            }
911                                            else if (ch == Ctrl('Y'))
912                                            {
913                                                    col_pos = 1;
914                                                    del_line = 1;
915                                            }
916    
917                                          if ((str_len = editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,                                          display_line_in = line_current - output_current_row + row_pos;
918                                                                                                            &last_updated_line)) < 0)                                          offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
919                                            display_line_out = display_line_in;
920                                            offset_out = offset_in;
921    
922                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
923                                                                                                              &last_updated_line, del_line)) < 0)
924                                          {                                          {
925                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error: %d\n", str_len);
926                                          }                                          }
927                                          else                                          else
928                                          {                                          {
929                                                  if (ch == BACKSPACE)                                                  col_pos = display_len + 1; // Set col_pos to accurate pos
                                                 {  
                                                         for (i = 1; i < str_len; i++)  
                                                         {  
                                                                 col_pos--;  
                                                                 if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0)  
                                                                 {  
                                                                         row_pos--;  
                                                                         col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);  
                                                                 }  
                                                         }  
                                                 }  
930    
931                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
932                                                  line_current -= (screen_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
933                                                  screen_current_row = (int)row_pos;                                                  output_current_row = (int)row_pos;
934    
935                                                  if (screen_current_row < screen_begin_row) // row_pos <= 0                                                  if (output_current_row < screen_begin_row) // row_pos <= 0
936                                                  {                                                  {
937                                                          screen_current_row = screen_begin_row;                                                          output_current_row = screen_begin_row;
938                                                          row_pos = screen_begin_row;                                                          row_pos = screen_begin_row;
939                                                          screen_end_row = SCREEN_ROWS - 1;                                                          output_end_row = SCREEN_ROWS - 1;
940                                                    }
941    
942                                                    // Exceed end
943                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
944                                                            p_editor_data->display_line_total > screen_row_total)
945                                                    {
946                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
947                                                                                                    (p_editor_data->display_line_total - screen_row_total));
948    
949                                                            line_current = p_editor_data->display_line_total - screen_row_total;
950                                                            row_pos += scroll_rows;
951                                                            output_current_row = screen_begin_row;
952                                                            output_end_row = SCREEN_ROWS - 1;
953                                                  }                                                  }
954    
955                                                    clrline(output_current_row, output_end_row);
956                                            }
957    
958                                            if (display_line_out != display_line_in) // Output on line change
959                                            {
960                                                    break;
961                                          }                                          }
962    
                                         // Check whether there is additional input  
963                                          ch = igetch(0);                                          ch = igetch(0);
964                                          if (ch == KEY_TIMEOUT)                                          if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
965                                          {                                          {
966                                                  input_ok = 1;                                                  break;
967                                          }                                          }
968    
969                                            str_len = 0;
970                                          continue;                                          continue;
971                                  }                                  }
972    
# Line 739  int editor_display(EDITOR_DATA *p_editor Line 974  int editor_display(EDITOR_DATA *p_editor
974                                  switch (ch)                                  switch (ch)
975                                  {                                  {
976                                  case KEY_NULL:                                  case KEY_NULL:
977    #ifdef _DEBUG
978                                            log_error("KEY_NULL\n");
979    #endif
980                                            goto cleanup;
981                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
982                                            log_error("User input timeout\n");
983                                          goto cleanup;                                          goto cleanup;
984                                  case Ctrl('C'):                                  case Ctrl('W'):
985                                    case Ctrl('X'):
986                                          loop = 0;                                          loop = 0;
987                                          break;                                          break;
988                                  case Ctrl('S'): // Start of line                                  case Ctrl('S'): // Start of line
# Line 750  int editor_display(EDITOR_DATA *p_editor Line 991  int editor_display(EDITOR_DATA *p_editor
991                                          break;                                          break;
992                                  case Ctrl('E'): // End of line                                  case Ctrl('E'): // End of line
993                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
994                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
995                                            {
996                                                    // last display line does NOT have \n in the end
997                                                    col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
998                                                    break;
999                                            }
1000                                            col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
1001                                          break;                                          break;
1002                                  case Ctrl('T'): // Top of screen                                  case Ctrl('T'): // Top of screen
1003                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
1004                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
1005                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1006                                          break;                                          break;
1007                                  case Ctrl('B'): // Bottom of screen                                  case Ctrl('B'): // Bottom of screen
1008                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
1009                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
1010                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          {
1011                                                    row_pos = p_editor_data->display_line_total;
1012                                            }
1013                                            else
1014                                            {
1015                                                    row_pos = SCREEN_ROWS - 1;
1016                                            }
1017                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
1018                                            {
1019                                                    // last display line does NOT have \n in the end
1020                                                    col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1);
1021                                            }
1022                                            else
1023                                            {
1024                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1025                                            }
1026                                          break;                                          break;
1027                                  case KEY_INS:                                  case KEY_INS:
1028                                          key_insert = !key_insert;                                          key_insert = !key_insert;
# Line 768  int editor_display(EDITOR_DATA *p_editor Line 1030  int editor_display(EDITOR_DATA *p_editor
1030                                  case KEY_HOME:                                  case KEY_HOME:
1031                                          row_pos = 1;                                          row_pos = 1;
1032                                          col_pos = 1;                                          col_pos = 1;
1033                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
1034                                          {                                          {
1035                                                  break;                                                  break;
1036                                          }                                          }
1037                                          line_current = 0;                                          line_current = 0;
1038                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1039                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1040                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1041                                          break;                                          break;
1042                                  case KEY_END:                                  case KEY_END:
1043                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
1044                                          {                                          {
1045                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
1046                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                  col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1047                                                  break;                                                  break;
1048                                          }                                          }
1049                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
1050                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1051                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1052                                          row_pos = screen_row_total;                                          row_pos = SCREEN_ROWS - 1;
1053                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1054                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1055                                          break;                                          break;
1056                                  case KEY_LEFT:                                  case KEY_LEFT:
1057                                          if (col_pos > 1)                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1058                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1059                                            col_pos = display_len;
1060                                            if (offset_in > 0)
1061                                            {
1062                                                    str_len = 1;
1063                                                    offset_in--;
1064                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0 ||
1065                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] > 127) // UTF8
1066                                                    {
1067                                                            while (offset_in > 0 &&
1068                                                                       (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xc0) != 0xc0)
1069                                                            {
1070                                                                    str_len++;
1071                                                                    offset_in--;
1072                                                            }
1073    
1074                                                            if (str_len > 4)
1075                                                            {
1076                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1077                                                            }
1078    
1079                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1080                                                                    (size_t)-1)
1081                                                            {
1082                                                                    log_error("mbstowcs() error\n");
1083                                                            }
1084                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1085    
1086                                                            if (wc_len == 2)
1087                                                            {
1088                                                                    col_pos--;
1089                                                            }
1090                                                    }
1091                                            }
1092                                            if (col_pos >= 1)
1093                                          {                                          {
                                                 col_pos--;  
1094                                                  break;                                                  break;
1095                                          }                                          }
1096                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 802  int editor_display(EDITOR_DATA *p_editor Line 1098  int editor_display(EDITOR_DATA *p_editor
1098                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
1099                                          {                                          {
1100                                                  row_pos--;                                                  row_pos--;
1101                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1102                                                  break;                                                  break;
1103                                          }                                          }
1104                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
1105                                          {                                          {
1106                                                  col_pos = 1;                                                  col_pos = 1;
1107                                                  break;                                                  break;
1108                                          }                                          }
1109                                          line_current -= screen_current_row;                                          line_current -= output_current_row;
1110                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1111                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
1112                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
1113                                          screen_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
1114                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_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:  
1115                                          break;                                          break;
1116                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1117                                          if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1118                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1119                                            col_pos = display_len + 2;
1120                                            if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
1121                                            {
1122                                                    str_len = 0;
1123                                                    if ((p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0x80) ==
1124                                                            0x80) // head of multi-byte character
1125                                                    {
1126                                                            c = (char)(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xf0);
1127                                                            while (c & 0x80)
1128                                                            {
1129                                                                    str_len++;
1130                                                                    c = (c & 0x7f) << 1;
1131                                                            }
1132    
1133                                                            if (str_len > 4)
1134                                                            {
1135                                                                    log_error("Invalid UTF-8 data detected: str_len > 4\n");
1136                                                            }
1137    
1138                                                            if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) ==
1139                                                                    (size_t)-1)
1140                                                            {
1141                                                                    log_error("mbstowcs() error\n");
1142                                                            }
1143                                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
1144    
1145                                                            if (wc_len == 2)
1146                                                            {
1147                                                                    col_pos++;
1148                                                            }
1149                                                    }
1150                                                    else
1151                                                    {
1152                                                            str_len = 1;
1153                                                    }
1154                                                    offset_in += str_len;
1155                                            }
1156                                            if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1157                                          {                                          {
                                                 col_pos++;  
1158                                                  break;                                                  break;
1159                                          }                                          }
1160                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
# Line 830  int editor_display(EDITOR_DATA *p_editor Line 1162  int editor_display(EDITOR_DATA *p_editor
1162                                          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))
1163                                          {                                          {
1164                                                  row_pos++;                                                  row_pos++;
1165                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1166                                                  break;                                                  break;
1167                                          }                                          }
1168                                          if (line_current + (screen_row_total - (screen_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end                                          if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
1169                                          {                                          {
1170                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                  // last display line does NOT have \n in the end
1171                                                    col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1172                                                  break;                                                  break;
1173                                          }                                          }
1174                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
1175                                          screen_current_row = screen_row_total;                                          output_current_row = screen_row_total;
1176                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1177                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1178                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1179                                          clrtoeol();                                          clrtoeol();
1180                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
1181                                            prints("\n"); // Legacy Cterm only works with this line
1182                                          break;                                          break;
1183                                  case KEY_PGUP:                                  case KEY_PGUP:
1184                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
1185                                          {                                          {
1186                                                  break;                                                  break;
1187                                          }                                          }
1188                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
1189                                          if (line_current < 0)                                          if (line_current < 0)
1190                                          {                                          {
1191                                                  line_current = 0;                                                  line_current = 0;
1192                                          }                                          }
1193                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1194                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1195                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1196                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1197                                          break;                                          break;
1198                                  case KEY_PGDN:                                  case KEY_PGDN:
1199                                          if (line_current + screen_row_total - (screen_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
1200                                          {                                          {
1201                                                  break;                                                  break;
1202                                          }                                          }
1203                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
1204                                          if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display                                          if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display
1205                                          {                                          {
1206                                                  line_current = p_editor_data->display_line_total - screen_row_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
1207                                          }                                          }
1208                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1209                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1210                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1211                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1212                                          break;                                          break;
1213                                    case Ctrl('Q'):
1214                                  case KEY_F1:                                  case KEY_F1:
1215                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not re-entrant
1216                                          {                                          {
1217                                                  break;                                                  break;
1218                                          }                                          }
1219                                          // Display help information                                          // Display help information
1220                                          show_help = 0;                                          show_help = 0;
1221                                          display_file(DATA_READ_HELP, 1);                                          display_file(DATA_EDITOR_HELP, 1);
1222                                          show_help = 1;                                          show_help = 1;
1223                                  case KEY_F5:                                  case KEY_F5:
1224                                          // Refresh after display help information                                          // Refresh after display help information
1225                                          line_current -= (screen_current_row - screen_begin_row);                                          line_current -= (output_current_row - screen_begin_row);
1226                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1227                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1228                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1229                                          break;                                          break;
1230                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
1231                                          break;                                          break;
# Line 899  int editor_display(EDITOR_DATA *p_editor Line 1234  int editor_display(EDITOR_DATA *p_editor
1234                                          break;                                          break;
1235                                  }                                  }
1236    
1237                                  BBS_last_access_tm = time(0);                                  // Refresh current action while user input
1238                                  if (!input_ok)                                  if (user_online_update(NULL) < 0)
1239                                  {                                  {
1240                                          ch = igetch_t(MAX_DELAY_TIME);                                          log_error("user_online_update(NULL) error\n");
1241                                  }                                  }
1242    
1243                                    if (input_ok)
1244                                    {
1245                                            break;
1246                                    }
1247    
1248                                    ch = igetch_t(BBS_max_user_idle_time);
1249                          }                          }
1250    
1251                          continue;                          continue;
# Line 939  int editor_display(EDITOR_DATA *p_editor Line 1281  int editor_display(EDITOR_DATA *p_editor
1281                  }                  }
1282                  buffer[j] = '\0';                  buffer[j] = '\0';
1283    
1284                  moveto(screen_current_row, 0);                  moveto(output_current_row, 0);
1285                  clrtoeol();                  clrtoeol();
1286                  prints("%s", buffer);                  prints("%s", buffer);
1287                  line_current++;                  line_current++;
1288                  screen_current_row++;                  output_current_row++;
1289          }          }
1290    
1291  cleanup:  cleanup:


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

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