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


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

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