/[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.9 by sysadm, Wed Jun 11 10:44:33 2025 UTC Revision 1.54 by sysadm, Wed Nov 5 04:19:21 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 <sys/param.h>  #include <sys/param.h>
 #include <strings.h>  
20    
21  #define _POSIX_C_SOURCE 200809L  enum _editor_constant_t
22  #include <string.h>  {
23            EDITOR_MEM_POOL_LINE_PER_CHUNK = 1000,
24            EDITOR_MEM_POOL_CHUNK_LIMIT = (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1),
25    };
26    
27    static const char EDITOR_ESC_DISPLAY_STR[] = "\033[32m*\033[m";
28    
29    static MEMORY_POOL *p_mp_data_line;
30    static MEMORY_POOL *p_mp_editor_data;
31    
32    int editor_memory_pool_init(void)
33    {
34            if (p_mp_data_line != NULL || p_mp_editor_data != NULL)
35            {
36                    log_error("Editor mem pool already initialized\n");
37                    return -1;
38            }
39    
40            p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT);
41            if (p_mp_data_line == NULL)
42            {
43                    log_error("Memory pool init error\n");
44                    return -2;
45            }
46    
47            p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
48            if (p_mp_editor_data == NULL)
49            {
50                    log_error("Memory pool init error\n");
51                    return -3;
52            }
53    
54            return 0;
55    }
56    
57    void editor_memory_pool_cleanup(void)
58    {
59            if (p_mp_data_line != NULL)
60            {
61                    memory_pool_cleanup(p_mp_data_line);
62                    p_mp_data_line = NULL;
63            }
64    
65            if (p_mp_editor_data != NULL)
66            {
67                    memory_pool_cleanup(p_mp_editor_data);
68                    p_mp_editor_data = NULL;
69            }
70    }
71    
72  EDITOR_DATA *editor_data_load(const char *p_data)  EDITOR_DATA *editor_data_load(const char *p_data)
73  {  {
74          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
75          char *p_data_line = NULL;          char *p_data_line = NULL;
76          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
77          long current_data_line_length = 0;          long current_data_line_length = 0;
78          long i, j;          long i;
79    
80          if (p_data == NULL)          if (p_data == NULL)
81          {          {
82                  log_error("editor_data_load() error: NULL pointer\n");                  log_error("NULL pointer error\n");
83                  return NULL;                  return NULL;
84          }          }
85    
86          p_editor_data = malloc(sizeof(EDITOR_DATA));          p_editor_data = memory_pool_alloc(p_mp_editor_data);
87          if (p_editor_data == NULL)          if (p_editor_data == NULL)
88          {          {
89                  log_error("malloc(EDITOR_DATA) error: OOM\n");                  log_error("memory_pool_alloc() error\n");
90                  return NULL;                  return NULL;
91          }          }
92    
93          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,
94          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);
95    
96          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
97          {          {
# Line 59  EDITOR_DATA *editor_data_load(const char Line 101  EDITOR_DATA *editor_data_load(const char
101                          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 ||
102                          (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'))
103                  {                  {
                         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;  
                         }  
   
104                          // Allocate new data line                          // Allocate new data line
105                          p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                          p_data_line = memory_pool_alloc(p_mp_data_line);
106                          if (p_data_line == NULL)                          if (p_data_line == NULL)
107                          {                          {
108                                  log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);                                  log_error("memory_pool_alloc() error: i = %d\n", i);
109                                  // Cleanup                                  // Cleanup
110                                  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);  
111                                  return NULL;                                  return NULL;
112                          }                          }
                         p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                         (p_editor_data->data_line_total)++;  
113    
114                          p_editor_data->p_display_lines[i] = p_data_line;                          p_editor_data->p_display_lines[i] = p_data_line;
115                          current_data_line_length = 0;                          current_data_line_length = 0;
# Line 91  EDITOR_DATA *editor_data_load(const char Line 121  EDITOR_DATA *editor_data_load(const char
121    
122                  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]);
123                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
124    
125                    // Trim \n from last line
126                    if (i + 1 == p_editor_data->display_line_total &&
127                            p_editor_data->display_line_lengths[i] > 0 &&
128                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
129                    {
130                            p_editor_data->display_line_lengths[i]--;
131                            p_editor_data->display_line_widths[i]--;
132                            current_data_line_length--;
133                    }
134                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
135          }          }
136    
137          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);  
138    
139          return p_editor_data;          return p_editor_data;
140  }  }
# Line 107  long editor_data_save(const EDITOR_DATA Line 146  long editor_data_save(const EDITOR_DATA
146    
147          if (p_editor_data == NULL || p_data == NULL)          if (p_editor_data == NULL || p_data == NULL)
148          {          {
149                  log_error("editor_data_save() error: NULL pointer\n");                  log_error("NULL pointer error\n");
150                  return -1;                  return -1;
151          }          }
152    
# Line 131  long editor_data_save(const EDITOR_DATA Line 170  long editor_data_save(const EDITOR_DATA
170    
171  void editor_data_cleanup(EDITOR_DATA *p_editor_data)  void editor_data_cleanup(EDITOR_DATA *p_editor_data)
172  {  {
173            char *p_data_line = NULL;
174          long i;          long i;
175    
176          if (p_editor_data == NULL)          if (p_editor_data == NULL)
# Line 138  void editor_data_cleanup(EDITOR_DATA *p_ Line 178  void editor_data_cleanup(EDITOR_DATA *p_
178                  return;                  return;
179          }          }
180    
181          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)          for (i = 0; i < p_editor_data->display_line_total; i++)
182            {
183                    if (p_data_line == NULL)
184                    {
185                            p_data_line = p_editor_data->p_display_lines[i];
186                    }
187    
188                    if (p_editor_data->display_line_lengths[i] > 0 &&
189                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
190                    {
191                            memory_pool_free(p_mp_data_line, p_data_line);
192                            p_data_line = NULL;
193                    }
194            }
195    
196            if (p_data_line != NULL)
197          {          {
198                  free(p_editor_data->p_data_lines[i]);                  memory_pool_free(p_mp_data_line, p_data_line);
                 p_editor_data->p_data_lines[i] = NULL;  
199          }          }
200    
201          free(p_editor_data);          memory_pool_free(p_mp_editor_data, p_editor_data);
202  }  }
203    
204  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 211  int editor_data_insert(EDITOR_DATA *p_ed
211          long offset_data_line;          long offset_data_line;
212          long last_display_line; // of data line          long last_display_line; // of data line
213          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
214            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
215          long split_line_total;          long split_line_total;
216          long i, j;          long i;
217            int len;
218            int eol;
219            int display_len;
220    
221          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
222          {          {
223                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
224                  return -1;                  return -1;
225          }          }
226    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
227          // Get length of current data line          // Get length of current data line
228          len_data_line = 0;          len_data_line = 0;
229          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 253  int editor_data_insert(EDITOR_DATA *p_ed
253          }          }
254    
255          // Split current data line if over-length          // Split current data line if over-length
256          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)
257          {          {
258                  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)
259                  {                  {
260                          log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n",  #ifdef _DEBUG
261                                            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",
262                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
263    #endif
264    
265                          return -2;                          return -2;
266                  }                  }
267    
268                  // Allocate new data line                  // Allocate new data line
269                  p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                  p_data_line = memory_pool_alloc(p_mp_data_line);
270                  if (p_data_line == NULL)                  if (p_data_line == NULL)
271                  {                  {
272                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");                          log_error("memory_pool_alloc() error\n");
273                          return -2;                          return -2;
274                  }                  }
                 p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                 (p_editor_data->data_line_total)++;  
275    
276                  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)
277                  {                  {
278                          if (str[0] == CR)                          if (str[0] == CR)
279                          {                          {
# Line 273  int editor_data_insert(EDITOR_DATA *p_ed Line 319  int editor_data_insert(EDITOR_DATA *p_ed
319                          *p_offset = offset + str_len;                          *p_offset = offset + str_len;
320                  }                  }
321    
322                    // Update display width of current display line
323                    len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0);
324                    p_editor_data->display_line_widths[display_line] = display_len;
325    
326                  split_line_total = last_display_line - display_line + 3;                  split_line_total = last_display_line - display_line + 3;
327    
328                  // 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 345  int editor_data_insert(EDITOR_DATA *p_ed
345          }          }
346    
347          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
348          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);
349    
350          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
351          {          {
# Line 304  int editor_data_insert(EDITOR_DATA *p_ed Line 354  int editor_data_insert(EDITOR_DATA *p_ed
354                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
355                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
356                          {                          {
357    #ifdef _DEBUG
358                                  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);
359                                  return -3;  #endif
360                          }  
361                          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
362                          {                                  if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
363                                  p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];                                  {
364                                  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);
365                                            p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
366                                            p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
367                                            p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
368                                            p_editor_data->display_line_widths[display_line + i - 1] = display_len;
369                                    }
370                                    if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
371                                    {
372                                            *p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1;
373                                    }
374                                    break;
375                          }                          }
376    
377                            // for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
378                            // {
379                            //      p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
380                            //      p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
381                            //      p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1];
382                            // }
383                            memmove(p_editor_data->p_display_lines + last_display_line + 2,
384                                            p_editor_data->p_display_lines + last_display_line + 1,
385                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
386                                                    sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
387                            memmove(p_editor_data->display_line_lengths + last_display_line + 2,
388                                            p_editor_data->display_line_lengths + last_display_line + 1,
389                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
390                                                    sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
391                            memmove(p_editor_data->display_line_widths + last_display_line + 2,
392                                            p_editor_data->display_line_widths + last_display_line + 1,
393                                            (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
394                                                    sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
395    
396                          last_display_line++;                          last_display_line++;
397                            *p_last_updated_line = p_editor_data->display_line_total;
398                          (p_editor_data->display_line_total)++;                          (p_editor_data->display_line_total)++;
399                  }                  }
400    
401                  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];
402                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
403                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
404                          (i == 0                          (i == 0
405                                   ? p_data_line                                   ? p_data_line
# Line 331  int editor_data_insert(EDITOR_DATA *p_ed Line 414  int editor_data_insert(EDITOR_DATA *p_ed
414    
415          *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);
416    
417          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'))  
418          {          {
419                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                  if (*p_display_line + 1 < p_editor_data->display_line_total)
420                  (*p_display_line)++;                  {
421                            *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
422                            (*p_display_line)++;
423                    }
424            }
425    
426                  if (*p_display_line >= p_editor_data->display_line_total)          // Prevent the last display line from being over-length
427            if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
428            {
429                    len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
430                    p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
431                    p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
432                    p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len;
433                    if (*p_display_line + 1 >= p_editor_data->display_line_total)
434                  {                  {
435                          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);
436                            *p_display_line = p_editor_data->display_line_total - 1;
437                  }                  }
438          }          }
439    
440          return 0;          return 0;
441  }  }
442    
443  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,
444                                             long *p_last_updated_line)                                             long *p_last_updated_line, int del_line)
445  {  {
446            long display_line = *p_display_line;
447            long offset = *p_offset;
448          char *p_data_line = NULL;          char *p_data_line = NULL;
449          long len_data_line;          long len_data_line;
450          long offset_data_line;          long offset_data_line;
451          long last_display_line; // of data line          long last_display_line; // of data line
452          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
453            int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
454          long split_line_total;          long split_line_total;
455          long i, j;          long i, j;
456          int str_len = 0;          int str_len = 0;
457            char c;
458    
459          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
460          {          {
461                  log_error("editor_data_op() error: NULL pointer\n");                  log_error("NULL pointer error\n");
462                  return -1;                  return -1;
463          }          }
464    
         // Get accurate offset of first character of CJK at offset position  
         for (i = 0; i < offset; i++)  
         {  
                 if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK  
                 {  
                         i++;  
                 }  
         }  
         if (i > offset) // offset was skipped  
         {  
                 offset--;  
         }  
   
465          // Get length of current data line          // Get length of current data line
466          len_data_line = 0;          len_data_line = 0;
467          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 490  int editor_data_delete(EDITOR_DATA *p_ed
490                  }                  }
491          }          }
492    
493            if (offset_data_line >= len_data_line) // end-of-line
494            {
495                    return 0;
496            }
497    
498          // Check str to be deleted          // Check str to be deleted
499          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          if (del_line)
500            {
501                    str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
502            }
503            else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
504          {          {
505                  str_len = 1;                  str_len = 1;
506          }          }
507          else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK          else if (p_data_line[offset_data_line] & 0x80) // head of multi-byte character
508          {          {
509                  str_len = 2;                  str_len = 1;
510                    c = (p_data_line[offset_data_line] & 0x70) << 1;
511                    while (c & 0x80)
512                    {
513                            str_len++;
514                            c = (c & 0x7f) << 1;
515                    }
516          }          }
517          else          else
518          {          {
519                  log_error("Some strange character at display_line %ld, offset %ld\n", display_line, offset);                  log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
520                  return -2;                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
521                    str_len = 1;
522          }          }
523    
524          // Current display line is (almost) empty          // Current display line is (almost) empty
525          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
526                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line &&
527                     p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
528          {          {
529                  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)
530                  {                  {
                         log_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total);  
531                          return 0;                          return 0;
532                  }                  }
533    
# Line 447  int editor_data_delete(EDITOR_DATA *p_ed Line 547  int editor_data_delete(EDITOR_DATA *p_ed
547    
548                  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
549                  {                  {
                         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);  
550                          return 0;                          return 0;
551                  }                  }
552    
# Line 457  int editor_data_delete(EDITOR_DATA *p_ed Line 555  int editor_data_delete(EDITOR_DATA *p_ed
555                  p_data_line[offset_data_line + len_data_line] = '\0';                  p_data_line[offset_data_line + len_data_line] = '\0';
556    
557                  // Recycle next data line                  // Recycle next data line
558                  // 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]);
559          }          }
560          else          else
561          {          {
# Line 471  int editor_data_delete(EDITOR_DATA *p_ed Line 569  int editor_data_delete(EDITOR_DATA *p_ed
569          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
570    
571          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
572          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);
573    
574          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
575          {          {
576                  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];
577                    p_editor_data->display_line_widths[display_line + i] = line_widths[i];
578                  p_editor_data->p_display_lines[display_line + i] =                  p_editor_data->p_display_lines[display_line + i] =
579                          (i == 0                          (i == 0
580                                   ? p_data_line                                   ? p_data_line
# Line 490  int editor_data_delete(EDITOR_DATA *p_ed Line 589  int editor_data_delete(EDITOR_DATA *p_ed
589    
590          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
591    
592          if (display_line + i < last_display_line)          if (*p_last_updated_line < last_display_line)
593          {          {
594                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
595                  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++)
596                  {                  // {
597                          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];
598                          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];
599                  }                  //      p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j];
600                    // }
601                  (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));                  memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
602                  last_display_line = display_line + i;                                  p_editor_data->p_display_lines + last_display_line + 1,
603                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
604                  *p_last_updated_line = p_editor_data->display_line_total - 1;                                          sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
605                    memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
606                                    p_editor_data->display_line_lengths + last_display_line + 1,
607                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
608                                            sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
609                    memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1,
610                                    p_editor_data->display_line_widths + last_display_line + 1,
611                                    (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
612                                            sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
613    
614                    j = p_editor_data->display_line_total;
615                    (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
616                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
617          }          }
618    
619            // Return real offset
620            *p_offset = offset;
621    
622          return str_len;          return str_len;
623  }  }
624    
# Line 514  static int editor_display_key_handler(in Line 628  static int editor_display_key_handler(in
628          {          {
629          case 0: // Set msg          case 0: // Set msg
630                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
631                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| 退出[\033[32mCtrl-W\033[33m] |");
632                    break;
633            case KEY_CSI:
634                    *p_key = KEY_ESC;
635                  break;                  break;
636          }          }
637    
# Line 528  int editor_display(EDITOR_DATA *p_editor Line 645  int editor_display(EDITOR_DATA *p_editor
645          EDITOR_CTX ctx;          EDITOR_CTX ctx;
646          int ch = 0;          int ch = 0;
647          char input_str[4];          char input_str[4];
648            char c;
649          int str_len = 0;          int str_len = 0;
650          int input_ok;          int input_ok;
         int screen_current_row;  
651          const int screen_begin_row = 1;          const int screen_begin_row = 1;
652          int screen_end_row = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
653          const int screen_row_total = screen_end_row - screen_begin_row + 1;          int output_current_row = screen_begin_row;
654            int output_end_row = SCREEN_ROWS - 1;
655          long int line_current = 0;          long int line_current = 0;
656          long int len;          long int len;
657          int loop;          int loop;
# Line 544  int editor_display(EDITOR_DATA *p_editor Line 662  int editor_display(EDITOR_DATA *p_editor
662          int scroll_rows;          int scroll_rows;
663          long last_updated_line = 0;          long last_updated_line = 0;
664          int key_insert = 1;          int key_insert = 1;
665          int i;          int i, j;
666            char *p_str;
667            int del_line;
668    
669          screen_current_row = screen_begin_row;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_row, SCREEN_ROWS);  
670    
671          // update msg_ext with extended key handler          // update msg_ext with extended key handler
672          if (editor_display_key_handler(&ch, &ctx) != 0)          if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 555  int editor_display(EDITOR_DATA *p_editor Line 674  int editor_display(EDITOR_DATA *p_editor
674                  return ch;                  return ch;
675          }          }
676    
677          loop = 1;          for (loop = 1; !SYS_server_exit && loop;)
         while (!SYS_server_exit && loop)  
678          {          {
679                  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)
680                  {                  {
681                          ctx.line_cursor = line_current - screen_current_row + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
682    
683                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
684                                           "\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] "
685                                           "µÚ\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] "
686                                           "%s",                                           "%s",
687                                           row_pos, col_pos,                                           row_pos, col_pos,
688                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
689                                           key_insert ? "²åÈë" : "¸Äд",                                           key_insert ? "æ’å…¥" : "替æ¢",
690                                           ctx.msg);                                           ctx.msg);
691    
692                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
693                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
694                          {                          {
695                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 585  int editor_display(EDITOR_DATA *p_editor Line 703  int editor_display(EDITOR_DATA *p_editor
703                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
704                          iflush();                          iflush();
705    
706                          input_ok = 0;                          str_len = 0;
707                          while (!SYS_server_exit && !input_ok)                          ch = igetch_t(BBS_max_user_idle_time);
708                            while (!SYS_server_exit)
709                          {                          {
710                                  ch = igetch_t(MAX_DELAY_TIME);                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
711                                  input_ok = 1;                                  {
712                                            BBS_last_access_tm = time(NULL);
713                                    }
714    
715                                  // extended key handler                                  // extended key handler
716                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 597  int editor_display(EDITOR_DATA *p_editor Line 718  int editor_display(EDITOR_DATA *p_editor
718                                          goto cleanup;                                          goto cleanup;
719                                  }                                  }
720    
721                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch < 256 && (ch & 0x80)) // head of multi-byte character
                                 {  
                                         input_str[str_len] = (char)(ch - 256);  
                                         str_len++;  
                                 }  
                                 else  
722                                  {                                  {
723                                          str_len = 0;                                          str_len = 0;
724                                            c = (char)(ch & 0xf0);
725                                            while (c & 0x80)
726                                            {
727                                                    input_str[str_len] = (char)(ch - 256);
728                                                    str_len++;
729                                                    c = (c & 0x7f) << 1;
730    
731                                                    if ((c & 0x80) == 0) // Input completed
732                                                    {
733                                                            break;
734                                                    }
735    
736                                                    // Expect additional bytes of input
737                                                    ch = igetch(100);                                                // 0.1 second
738                                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
739                                                    {
740    #ifdef _DEBUG
741                                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
742    #endif
743                                                            str_len = 0;
744                                                            break;
745                                                    }
746                                            }
747                                  }                                  }
748    
749                                  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
750                                            ch == CR || ch == KEY_ESC)                                // Special character
751                                  {                                  {
752                                          if (str_len == 0)                                          // Refresh current action while user input
753                                            if (user_online_update(NULL) < 0)
754                                            {
755                                                    log_error("user_online_update(NULL) error\n");
756                                            }
757    
758                                            if (str_len == 0) // ch >= 32 && ch < 127
759                                          {                                          {
760                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
761                                                  str_len = 1;                                                  str_len = 1;
762                                          }                                          }
763    
764                                          last_updated_line = line_current;                                          display_line_in = line_current - output_current_row + row_pos;
765                                          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;  
766                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
767                                          offset_out = offset_in;                                          offset_out = offset_in;
768    
769                                            last_updated_line = display_line_in;
770    
771                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
772                                          {                                          {
773                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
774                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line, 0) < 0)
775                                                  {                                                  {
776                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
777                                                  }                                                  }
# Line 633  int editor_display(EDITOR_DATA *p_editor Line 780  int editor_display(EDITOR_DATA *p_editor
780                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
781                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
782                                          {                                          {
783                                                  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;  
784                                          }                                          }
785                                          else                                          else
786                                          {                                          {
787                                                  str_len = 0;                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
788                                                    line_current -= (output_current_row - row_pos);
789                                                  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;  
790    
791                                                  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));
792    
793                                                  if (scroll_rows > 0)                                                  if (scroll_rows > 0)
794                                                  {                                                  {
# Line 652  int editor_display(EDITOR_DATA *p_editor Line 796  int editor_display(EDITOR_DATA *p_editor
796                                                          clrtoeol();                                                          clrtoeol();
797                                                          for (i = 0; i < scroll_rows; i++)                                                          for (i = 0; i < scroll_rows; i++)
798                                                          {                                                          {
799                                                                  prints("\033[S"); // Scroll up 1 line                                                                  // prints("\033[S"); // Scroll up 1 line
800                                                                    prints("\n"); // Legacy Cterm only works with this line
801                                                          }                                                          }
802    
803                                                          screen_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
804                                                          if (screen_current_row < screen_begin_row)                                                          if (output_current_row < screen_begin_row)
805                                                          {                                                          {
806                                                                  line_current += (screen_begin_row - screen_current_row);                                                                  line_current += (screen_begin_row - output_current_row);
807                                                                  screen_current_row = screen_begin_row;                                                                  output_current_row = screen_begin_row;
808                                                          }                                                          }
809                                                          row_pos = screen_end_row;                                                          row_pos = output_end_row;
810                                                  }                                                  }
811                                                  else // if (scroll_lines == 0)                                                  else // if (scroll_lines == 0)
812                                                  {                                                  {
813                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
814                                                  }                                                  }
                                                 col_pos = offset_out + 1;  
815    
816                                                  continue;                                                  if (offset_out != offset_in)
817                                                    {
818                                                            if (display_line_out != display_line_in)
819                                                            {
820                                                                    col_pos = 1;
821                                                            }
822                                                            if (offset_out > 0)
823                                                            {
824                                                                    col_pos += (str_len == 1 ? 1 : 2);
825                                                            }
826                                                    }
827                                            }
828    
829                                            if (display_line_out != display_line_in) // Output on line change
830                                            {
831                                                    break;
832                                            }
833    
834                                            ch = igetch(0);
835                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
836                                            {
837                                                    break;
838                                          }                                          }
839    
840                                            str_len = 0;
841                                            continue;
842                                  }                                  }
843                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
844                                  {                                  {
845                                            // Refresh current action while user input
846                                            if (user_online_update(NULL) < 0)
847                                            {
848                                                    log_error("user_online_update(NULL) error\n");
849                                            }
850    
851                                            del_line = 0;
852    
853                                          if (ch == BACKSPACE)                                          if (ch == BACKSPACE)
854                                          {                                          {
855                                                  col_pos--;                                                  if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
856                                                  if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0)                                                  {
857                                                            break; // force output prior operation result if any
858                                                    }
859    
860                                                    offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
861                                                                                               (int)col_pos - 1, &eol, &display_len, 0);
862                                                    if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
863                                                    {
864                                                            col_pos = display_len - 1;
865                                                    }
866                                                    else
867                                                    {
868                                                            col_pos = display_len;
869                                                    }
870    
871                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
872                                                  {                                                  {
873                                                          row_pos--;                                                          row_pos--;
874                                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                          col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
875                                                  }                                                  }
876                                          }                                          }
877                                            else if (ch == Ctrl('K'))
878                                            {
879                                                    del_line = 1;
880                                            }
881                                            else if (ch == Ctrl('Y'))
882                                            {
883                                                    col_pos = 1;
884                                                    del_line = 1;
885                                            }
886    
887                                          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;
888                                                                                                            &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);
889                                            display_line_out = display_line_in;
890                                            offset_out = offset_in;
891    
892                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
893                                                                                                              &last_updated_line, del_line)) < 0)
894                                          {                                          {
895                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error: %d\n", str_len);
896                                          }                                          }
897                                          else                                          else
898                                          {                                          {
899                                                  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]);  
                                                                 }  
                                                         }  
                                                 }  
900    
901                                                  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));
902                                                  line_current -= (screen_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
903                                                  screen_current_row = (int)row_pos;                                                  output_current_row = (int)row_pos;
904    
905                                                  if (screen_current_row < screen_begin_row) // row_pos <= 0                                                  if (output_current_row < screen_begin_row) // row_pos <= 0
906                                                  {                                                  {
907                                                          screen_current_row = screen_begin_row;                                                          output_current_row = screen_begin_row;
908                                                          row_pos = screen_begin_row;                                                          row_pos = screen_begin_row;
909                                                          screen_end_row = SCREEN_ROWS - 1;                                                          output_end_row = SCREEN_ROWS - 1;
910                                                    }
911    
912                                                    // Exceed end
913                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
914                                                            p_editor_data->display_line_total > screen_row_total)
915                                                    {
916                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
917                                                                                                    (p_editor_data->display_line_total - screen_row_total));
918    
919                                                            line_current = p_editor_data->display_line_total - screen_row_total;
920                                                            row_pos += scroll_rows;
921                                                            output_current_row = screen_begin_row;
922                                                            output_end_row = SCREEN_ROWS - 1;
923                                                  }                                                  }
924    
925                                                    clrline(output_current_row, output_end_row);
926                                            }
927    
928                                            if (display_line_out != display_line_in) // Output on line change
929                                            {
930                                                    break;
931                                            }
932    
933                                            ch = igetch(0);
934                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
935                                            {
936                                                    break;
937                                          }                                          }
938    
939                                            str_len = 0;
940                                          continue;                                          continue;
941                                  }                                  }
942    
943                                    input_ok = 1;
944                                  switch (ch)                                  switch (ch)
945                                  {                                  {
946                                  case KEY_NULL:                                  case KEY_NULL:
947                                            log_error("KEY_NULL\n");
948                                            goto cleanup;
949                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
950                                            log_error("User input timeout\n");
951                                          goto cleanup;                                          goto cleanup;
952                                  case Ctrl('C'):                                  case Ctrl('W'):
953                                    case Ctrl('X'):
954                                          loop = 0;                                          loop = 0;
955                                          break;                                          break;
956                                    case Ctrl('S'): // Start of line
957                                  case KEY_CTRL_LEFT:                                  case KEY_CTRL_LEFT:
958                                          col_pos = 1;                                          col_pos = 1;
959                                          break;                                          break;
960                                    case Ctrl('E'): // End of line
961                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
962                                          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
963                                            {
964                                                    // last display line does NOT have \n in the end
965                                                    col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
966                                                    break;
967                                            }
968                                            col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
969                                          break;                                          break;
970                                    case Ctrl('T'): // Top of screen
971                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
972                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
973                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - 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]));
974                                          break;                                          break;
975                                    case Ctrl('B'): // Bottom of screen
976                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
977                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
978                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          {
979                                                    row_pos = p_editor_data->display_line_total;
980                                            }
981                                            else
982                                            {
983                                                    row_pos = SCREEN_ROWS - 1;
984                                            }
985                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
986                                            {
987                                                    // last display line does NOT have \n in the end
988                                                    col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1);
989                                            }
990                                            else
991                                            {
992                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
993                                            }
994                                          break;                                          break;
995                                  case KEY_INS:                                  case KEY_INS:
996                                          key_insert = !key_insert;                                          key_insert = !key_insert;
# Line 747  int editor_display(EDITOR_DATA *p_editor Line 998  int editor_display(EDITOR_DATA *p_editor
998                                  case KEY_HOME:                                  case KEY_HOME:
999                                          row_pos = 1;                                          row_pos = 1;
1000                                          col_pos = 1;                                          col_pos = 1;
1001                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
1002                                          {                                          {
1003                                                  break;                                                  break;
1004                                          }                                          }
1005                                          line_current = 0;                                          line_current = 0;
1006                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1007                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1008                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1009                                          break;                                          break;
1010                                  case KEY_END:                                  case KEY_END:
1011                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
1012                                          {                                          {
1013                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
1014                                                  col_pos = 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;
1015                                                  break;                                                  break;
1016                                          }                                          }
1017                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
1018                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1019                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1020                                          row_pos = screen_row_total;                                          row_pos = SCREEN_ROWS - 1;
1021                                          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;
1022                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1023                                          break;                                          break;
1024                                  case KEY_LEFT:                                  case KEY_LEFT:
1025                                          if (col_pos > 1)                                          offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1026                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1027                                            if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
1028                                            {
1029                                                    col_pos = display_len - 1;
1030                                            }
1031                                            else
1032                                            {
1033                                                    col_pos = display_len;
1034                                            }
1035                                            if (col_pos >= 1)
1036                                          {                                          {
                                                 col_pos--;  
1037                                                  break;                                                  break;
1038                                          }                                          }
1039                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 781  int editor_display(EDITOR_DATA *p_editor Line 1041  int editor_display(EDITOR_DATA *p_editor
1041                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
1042                                          {                                          {
1043                                                  row_pos--;                                                  row_pos--;
1044                                                  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]));
1045                                                  break;                                                  break;
1046                                          }                                          }
1047                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
1048                                          {                                          {
1049                                                  col_pos = 1;                                                  col_pos = 1;
1050                                                  break;                                                  break;
1051                                          }                                          }
1052                                          line_current -= screen_current_row;                                          line_current -= output_current_row;
1053                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1054                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
1055                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
1056                                          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
1057                                          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:  
1058                                          break;                                          break;
1059                                  case KEY_RIGHT:                                  case KEY_RIGHT:
1060                                          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],
1061                                                                                       (int)col_pos - 1, &eol, &display_len, 0);
1062                                            if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&
1063                                                    p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8
1064                                            {
1065                                                    col_pos = display_len + 3;
1066                                            }
1067                                            else
1068                                            {
1069                                                    col_pos = display_len + 2;
1070                                            }
1071                                            if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1072                                          {                                          {
                                                 col_pos++;  
1073                                                  break;                                                  break;
1074                                          }                                          }
1075                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
# Line 809  int editor_display(EDITOR_DATA *p_editor Line 1077  int editor_display(EDITOR_DATA *p_editor
1077                                          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))
1078                                          {                                          {
1079                                                  row_pos++;                                                  row_pos++;
1080                                                  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]));
1081                                                  break;                                                  break;
1082                                          }                                          }
1083                                          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
1084                                          {                                          {
1085                                                  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
1086                                                    col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1087                                                  break;                                                  break;
1088                                          }                                          }
1089                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
1090                                          screen_current_row = screen_row_total;                                          output_current_row = screen_row_total;
1091                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1092                                          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]));
1093                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1094                                          clrtoeol();                                          clrtoeol();
1095                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
1096                                            prints("\n"); // Legacy Cterm only works with this line
1097                                          break;                                          break;
1098                                  case KEY_PGUP:                                  case KEY_PGUP:
1099                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
1100                                          {                                          {
1101                                                  break;                                                  break;
1102                                          }                                          }
1103                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
1104                                          if (line_current < 0)                                          if (line_current < 0)
1105                                          {                                          {
1106                                                  line_current = 0;                                                  line_current = 0;
1107                                          }                                          }
1108                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1109                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1110                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - 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]));
1111                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1112                                          break;                                          break;
1113                                  case KEY_PGDN:                                  case KEY_PGDN:
1114                                          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
1115                                          {                                          {
1116                                                  break;                                                  break;
1117                                          }                                          }
1118                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
1119                                          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
1120                                          {                                          {
1121                                                  line_current = p_editor_data->display_line_total - screen_row_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
1122                                          }                                          }
1123                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1124                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1125                                          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]));
1126                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
1127                                          break;                                          break;
1128                                    case Ctrl('Q'):
1129                                  case KEY_F1:                                  case KEY_F1:
1130                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not re-entrant
1131                                          {                                          {
1132                                                  break;                                                  break;
1133                                          }                                          }
1134                                          // Display help information                                          // Display help information
1135                                          show_help = 0;                                          show_help = 0;
1136                                          display_file(DATA_READ_HELP, 1);                                          display_file(DATA_EDITOR_HELP, 1);
1137                                          show_help = 1;                                          show_help = 1;
1138                                  case KEY_F5:                                  case KEY_F5:
1139                                          // Refresh after display help information                                          // Refresh after display help information
1140                                          line_current -= (screen_current_row - screen_begin_row);                                          line_current -= (output_current_row - screen_begin_row);
1141                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1142                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1143                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1144                                          break;                                          break;
1145                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
1146                                          break;                                          break;
# Line 880  int editor_display(EDITOR_DATA *p_editor Line 1149  int editor_display(EDITOR_DATA *p_editor
1149                                          break;                                          break;
1150                                  }                                  }
1151    
1152                                  BBS_last_access_tm = time(0);                                  // Refresh current action while user input
1153                                    if (user_online_update(NULL) < 0)
1154                                    {
1155                                            log_error("user_online_update(NULL) error\n");
1156                                    }
1157    
1158                                    if (input_ok)
1159                                    {
1160                                            break;
1161                                    }
1162    
1163                                    ch = igetch_t(BBS_max_user_idle_time);
1164                          }                          }
1165    
1166                          continue;                          continue;
# Line 898  int editor_display(EDITOR_DATA *p_editor Line 1178  int editor_display(EDITOR_DATA *p_editor
1178                          len = 0;                          len = 0;
1179                  }                  }
1180    
1181                  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);
1182                  buffer[len] = '\0';                  // Replace '\033' with '*'
1183                    p_str = p_editor_data->p_display_lines[line_current];
1184                    for (i = 0, j = 0; i < len; i++)
1185                    {
1186                            if (p_str[i] == '\033')
1187                            {
1188                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1189                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1190                            }
1191                            else
1192                            {
1193                                    buffer[j] = p_str[i];
1194                                    j++;
1195                            }
1196                    }
1197                    buffer[j] = '\0';
1198    
1199                  moveto(screen_current_row, 0);                  moveto(output_current_row, 0);
1200                  clrtoeol();                  clrtoeol();
1201                  prints("%s", buffer);                  prints("%s", buffer);
1202                  line_current++;                  line_current++;
1203                  screen_current_row++;                  output_current_row++;
1204          }          }
1205    
1206  cleanup:  cleanup:


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

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