/[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.8 by sysadm, Wed Jun 11 08:00:51 2025 UTC Revision 1.28 by sysadm, Tue Jun 17 13:17:04 2025 UTC
# Line 20  Line 20 
20  #include "log.h"  #include "log.h"
21  #include "common.h"  #include "common.h"
22  #include "str_process.h"  #include "str_process.h"
23    #include "memory_pool.h"
24  #include <stdlib.h>  #include <stdlib.h>
25  #include <sys/param.h>  #include <sys/param.h>
26  #include <strings.h>  #include <strings.h>
   
 #define _POSIX_C_SOURCE 200809L  
27  #include <string.h>  #include <string.h>
28    
29    #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
30    #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000
31    #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)
32    
33    static MEMORY_POOL *p_mp_data_line;
34    static MEMORY_POOL *p_mp_editor_data;
35    
36    int editor_memory_pool_init(void)
37    {
38            if (p_mp_data_line != NULL || p_mp_editor_data != NULL)
39            {
40                    log_error("Editor mem pool already initialized\n");
41                    return -1;
42            }
43    
44            p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT);
45            if (p_mp_data_line == NULL)
46            {
47                    log_error("Memory pool init error\n");
48                    return -2;
49            }
50    
51            p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
52            if (p_mp_data_line == NULL)
53            {
54                    log_error("Memory pool init error\n");
55                    return -3;
56            }
57    
58            return 0;
59    }
60    
61    void editor_memory_pool_cleanup(void)
62    {
63            if (p_mp_data_line != NULL)
64            {
65                    memory_pool_cleanup(p_mp_data_line);
66                    p_mp_data_line = NULL;
67            }
68    
69            if (p_mp_editor_data != NULL)
70            {
71                    memory_pool_cleanup(p_mp_editor_data);
72                    p_mp_editor_data = NULL;
73            }
74    }
75    
76  EDITOR_DATA *editor_data_load(const char *p_data)  EDITOR_DATA *editor_data_load(const char *p_data)
77  {  {
78          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
79          char *p_data_line = NULL;          char *p_data_line = NULL;
80          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES + 1];
81          long current_data_line_length = 0;          long current_data_line_length = 0;
82          long i, j;          long i;
83    
84          if (p_data == NULL)          if (p_data == NULL)
85          {          {
# Line 41  EDITOR_DATA *editor_data_load(const char Line 87  EDITOR_DATA *editor_data_load(const char
87                  return NULL;                  return NULL;
88          }          }
89    
90          p_editor_data = malloc(sizeof(EDITOR_DATA));          p_editor_data = memory_pool_alloc(p_mp_editor_data);
91          if (p_editor_data == NULL)          if (p_editor_data == NULL)
92          {          {
93                  log_error("malloc(EDITOR_DATA) error: OOM\n");                  log_error("memory_pool_alloc() error\n");
94                  return NULL;                  return NULL;
95          }          }
96    
97          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, 0);
         p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);  
98    
99          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
100          {          {
# Line 59  EDITOR_DATA *editor_data_load(const char Line 104  EDITOR_DATA *editor_data_load(const char
104                          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 ||
105                          (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'))
106                  {                  {
                         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;  
                         }  
   
107                          // Allocate new data line                          // Allocate new data line
108                          p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                          p_data_line = memory_pool_alloc(p_mp_data_line);
109                          if (p_data_line == NULL)                          if (p_data_line == NULL)
110                          {                          {
111                                  log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);                                  log_error("memory_pool_alloc() error: i = %d\n", i);
112                                  // Cleanup                                  // Cleanup
113                                  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);  
114                                  return NULL;                                  return NULL;
115                          }                          }
                         p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                         (p_editor_data->data_line_total)++;  
116    
117                          p_editor_data->p_display_lines[i] = p_data_line;                          p_editor_data->p_display_lines[i] = p_data_line;
118                          current_data_line_length = 0;                          current_data_line_length = 0;
# Line 91  EDITOR_DATA *editor_data_load(const char Line 124  EDITOR_DATA *editor_data_load(const char
124    
125                  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]);
126                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
127    
128                    // Trim \n from last line
129                    if (i + 1 == p_editor_data->display_line_total &&
130                            p_editor_data->display_line_lengths[i] > 0 &&
131                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
132                    {
133                            p_editor_data->display_line_lengths[i]--;
134                            current_data_line_length--;
135                    }
136                  p_data_line[current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
137          }          }
138    
139          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);  
140    
141          return p_editor_data;          return p_editor_data;
142  }  }
# Line 131  long editor_data_save(const EDITOR_DATA Line 172  long editor_data_save(const EDITOR_DATA
172    
173  void editor_data_cleanup(EDITOR_DATA *p_editor_data)  void editor_data_cleanup(EDITOR_DATA *p_editor_data)
174  {  {
175            char *p_data_line = NULL;
176          long i;          long i;
177    
178          if (p_editor_data == NULL)          if (p_editor_data == NULL)
# Line 138  void editor_data_cleanup(EDITOR_DATA *p_ Line 180  void editor_data_cleanup(EDITOR_DATA *p_
180                  return;                  return;
181          }          }
182    
183          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)          for (i = 0; i < p_editor_data->display_line_total; i++)
184            {
185                    if (p_data_line == NULL)
186                    {
187                            p_data_line = p_editor_data->p_display_lines[i];
188                    }
189    
190                    if (p_editor_data->display_line_lengths[i] > 0 &&
191                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
192                    {
193                            memory_pool_free(p_mp_data_line, p_data_line);
194                            p_data_line = NULL;
195                    }
196            }
197    
198            if (p_data_line != NULL)
199          {          {
200                  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;  
201          }          }
202    
203          free(p_editor_data);          memory_pool_free(p_mp_editor_data, p_editor_data);
204  }  }
205    
206  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
# Line 159  int editor_data_insert(EDITOR_DATA *p_ed Line 215  int editor_data_insert(EDITOR_DATA *p_ed
215          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
216          long split_line_total;          long split_line_total;
217          long i, j;          long i, j;
218            int len;
219            int eol;
220            int display_len;
221    
222          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
223          {          {
# Line 166  int editor_data_insert(EDITOR_DATA *p_ed Line 225  int editor_data_insert(EDITOR_DATA *p_ed
225                  return -1;                  return -1;
226          }          }
227    
228            // Validate str
229            if ((str_len == 1 && str[0] <= 0) ||
230                    (str_len == 2 && (str[0] >= 0 || str[1] >= 0)))
231            {
232                    log_error("Invalid input str, len=%d\n", str_len);
233                    return -2;
234            }
235    
236          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
237          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
238          {          {
# Line 210  int editor_data_insert(EDITOR_DATA *p_ed Line 277  int editor_data_insert(EDITOR_DATA *p_ed
277          // Split current data line if over-length          // Split current data line if over-length
278          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
279          {          {
280                  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)
281                  {                  {
282                          log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n",                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
283                                            p_editor_data->display_line_total, p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);                          //                p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
284                          return -2;                          return -2;
285                  }                  }
286    
287                  // Allocate new data line                  // Allocate new data line
288                  p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                  p_data_line = memory_pool_alloc(p_mp_data_line);
289                  if (p_data_line == NULL)                  if (p_data_line == NULL)
290                  {                  {
291                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");                          log_error("memory_pool_alloc() error\n");
292                          return -2;                          return -2;
293                  }                  }
                 p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line;  
                 (p_editor_data->data_line_total)++;  
294    
295                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
296                  {                  {
# Line 295  int editor_data_insert(EDITOR_DATA *p_ed Line 360  int editor_data_insert(EDITOR_DATA *p_ed
360          }          }
361    
362          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
363          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);
364    
365          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
366          {          {
# Line 304  int editor_data_insert(EDITOR_DATA *p_ed Line 369  int editor_data_insert(EDITOR_DATA *p_ed
369                          // Insert blank display line after last_display_line                          // Insert blank display line after last_display_line
370                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
371                          {                          {
372                                  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);
373                                  return -3;                                  // Terminate prior display line with \n, to avoid error on cleanup
374                                    if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
375                                    {
376                                            len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
377                                            p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
378                                            p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
379                                            p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
380                                    }
381                                    if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
382                                    {
383                                            *p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1;
384                                    }
385                                    break;
386                          }                          }
387                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)                          for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
388                          {                          {
# Line 331  int editor_data_insert(EDITOR_DATA *p_ed Line 408  int editor_data_insert(EDITOR_DATA *p_ed
408    
409          *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);
410    
411          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'))  
412          {          {
413                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];                  if (*p_display_line + 1 < p_editor_data->display_line_total)
414                  (*p_display_line)++;                  {
415                            *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
416                            (*p_display_line)++;
417                    }
418            }
419    
420                  if (*p_display_line >= p_editor_data->display_line_total)          // Prevent the last display line from being over-length
421            if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
422            {
423                    len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
424                    p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
425                    p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
426                    if (*p_display_line + 1 >= p_editor_data->display_line_total)
427                  {                  {
428                          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);
429                            *p_display_line = p_editor_data->display_line_total - 1;
430                  }                  }
431          }          }
432    
433          return 0;          return 0;
434  }  }
435    
436  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,
437                                             long *p_last_updated_line)                                             long *p_last_updated_line)
438  {  {
439            long display_line = *p_display_line;
440            long offset = *p_offset;
441          char *p_data_line = NULL;          char *p_data_line = NULL;
442          long len_data_line;          long len_data_line;
443          long offset_data_line;          long offset_data_line;
# Line 406  int editor_data_delete(EDITOR_DATA *p_ed Line 494  int editor_data_delete(EDITOR_DATA *p_ed
494                  }                  }
495          }          }
496    
497            if (offset_data_line >= len_data_line) // end-of-line
498            {
499                    return 0;
500            }
501    
502          // Check str to be deleted          // Check str to be deleted
503          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)          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 + 1] < 0) // GBK
508          {          {
509                  str_len = 2;                  str_len = 2;
510          }          }
511          else          else
512          {          {
513                  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",
514                  return -2;                                    display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
515                    str_len = 1;
516          }          }
517    
518          // Current display line is (almost) empty          // Current display line is (almost) empty
519          if (offset_data_line + str_len > len_data_line ||          if (offset_data_line + str_len > len_data_line ||
520                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))                  (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
521          {          {
522                  log_error("Nothing to be delete\n");                  if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
523                  return 0;                  {
524          }                          return 0;
525                    }
526    
527                    len_data_line = 0; // Next data line
528                    last_display_line = p_editor_data->display_line_total - 1;
529                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
530                    {
531                            len_data_line += p_editor_data->display_line_lengths[i];
532    
533                            if (p_editor_data->display_line_lengths[i] > 0 &&
534                                    p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
535                            {
536                                    last_display_line = i;
537                                    break;
538                            }
539                    }
540    
541                    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
542                    {
543                            return 0;
544                    }
545    
546          memmove(p_data_line + offset_data_line, p_data_line + offset_data_line + str_len, (size_t)(len_data_line - offset_data_line - str_len));                  // Append next data line to current one
547          p_data_line[len_data_line - str_len] = '\0';                  memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
548          len_data_line -= str_len;                  p_data_line[offset_data_line + len_data_line] = '\0';
549    
550                    // Recycle next data line
551                    memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]);
552            }
553            else
554            {
555                    memmove(p_data_line + offset_data_line, p_data_line + offset_data_line + str_len, (size_t)(len_data_line - offset_data_line - str_len));
556                    p_data_line[len_data_line - str_len] = '\0';
557                    len_data_line -= str_len;
558            }
559    
560          // Set p_data_line to head of current display line          // Set p_data_line to head of current display line
561          p_data_line = p_editor_data->p_display_lines[display_line];          p_data_line = p_editor_data->p_display_lines[display_line];
562          split_line_total = last_display_line - display_line + 2;          split_line_total = last_display_line - display_line + 2;
563    
564          // Split current data line since beginning of current display line          // Split current data line since beginning of current display line
565          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);
566    
567          for (i = 0; i < split_line_total; i++)          for (i = 0; i < split_line_total; i++)
568          {          {
# Line 457  int editor_data_delete(EDITOR_DATA *p_ed Line 581  int editor_data_delete(EDITOR_DATA *p_ed
581    
582          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
583    
584          if (display_line + i < last_display_line)          if (*p_last_updated_line < last_display_line)
585          {          {
586                  // Remove redundant display line after last_display_line                  // Remove redundant display line after last_display_line
587                  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++)
588                  {                  {
589                          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];
590                          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];
591                  }                  }
592    
593                  (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));                  j = p_editor_data->display_line_total;
594                  last_display_line = display_line + i;                  (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
595                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
                 *p_last_updated_line = p_editor_data->display_line_total - 1;  
596          }          }
597    
598          return 0;          // Return real offset
599            *p_offset = offset;
600    
601            return str_len;
602  }  }
603    
604  static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)  static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)
# Line 481  static int editor_display_key_handler(in Line 607  static int editor_display_key_handler(in
607          {          {
608          case 0: // Set msg          case 0: // Set msg
609                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
610                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] |");
611                    break;
612            case KEY_CSI:
613                    *p_key = KEY_ESC;
614                  break;                  break;
615          }          }
616    
# Line 497  int editor_display(EDITOR_DATA *p_editor Line 626  int editor_display(EDITOR_DATA *p_editor
626          char input_str[4];          char input_str[4];
627          int str_len = 0;          int str_len = 0;
628          int input_ok;          int input_ok;
         int screen_current_row;  
629          const int screen_begin_row = 1;          const int screen_begin_row = 1;
630          int screen_end_row = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
631          const int screen_row_total = screen_end_row - screen_begin_row + 1;          int output_current_row = screen_begin_row;
632            int output_end_row = SCREEN_ROWS - 1;
633          long int line_current = 0;          long int line_current = 0;
634          long int len;          long int len;
635          int loop;          int loop;
# Line 511  int editor_display(EDITOR_DATA *p_editor Line 640  int editor_display(EDITOR_DATA *p_editor
640          int scroll_rows;          int scroll_rows;
641          long last_updated_line = 0;          long last_updated_line = 0;
642          int key_insert = 1;          int key_insert = 1;
643          int i;          int i, j;
644            char *p_str;
645    
646          screen_current_row = screen_begin_row;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_row, SCREEN_ROWS);  
647    
648          // update msg_ext with extended key handler          // update msg_ext with extended key handler
649          if (editor_display_key_handler(&ch, &ctx) != 0)          if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 525  int editor_display(EDITOR_DATA *p_editor Line 654  int editor_display(EDITOR_DATA *p_editor
654          loop = 1;          loop = 1;
655          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
656          {          {
657                  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)
658                  {                  {
659                          ctx.line_cursor = line_current - screen_current_row + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
660    
661                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
662                                           "\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] "
# Line 535  int editor_display(EDITOR_DATA *p_editor Line 664  int editor_display(EDITOR_DATA *p_editor
664                                           "%s",                                           "%s",
665                                           row_pos, col_pos,                                           row_pos, col_pos,
666                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
667                                           key_insert ? "²åÈë" : "¸Äд",                                           key_insert ? "²åÈë" : "Ìæ»»",
668                                           ctx.msg);                                           ctx.msg);
669    
670                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
671                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
672                          {                          {
673                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 552  int editor_display(EDITOR_DATA *p_editor Line 681  int editor_display(EDITOR_DATA *p_editor
681                          moveto((int)row_pos, (int)col_pos);                          moveto((int)row_pos, (int)col_pos);
682                          iflush();                          iflush();
683    
684                            str_len = 0;
685                          input_ok = 0;                          input_ok = 0;
686                            ch = igetch_t(MAX_DELAY_TIME);
687                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
688                          {                          {
                                 ch = igetch_t(MAX_DELAY_TIME);  
                                 input_ok = 1;  
   
689                                  // extended key handler                                  // extended key handler
690                                  if (editor_display_key_handler(&ch, &ctx) != 0)                                  if (editor_display_key_handler(&ch, &ctx) != 0)
691                                  {                                  {
# Line 569  int editor_display(EDITOR_DATA *p_editor Line 697  int editor_display(EDITOR_DATA *p_editor
697                                          input_str[str_len] = (char)(ch - 256);                                          input_str[str_len] = (char)(ch - 256);
698                                          str_len++;                                          str_len++;
699                                  }                                  }
700                                  else                                  else if (str_len > 0)
701                                  {                                  {
702                                            log_error("Received %d character over 127 followed by character less than 127\n", str_len);
703                                          str_len = 0;                                          str_len = 0;
704                                  }                                  }
705    
706                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || ch == CR) // printable character or GBK                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK
707                                            ch == CR || ch == KEY_ESC)                                                                                       // Special character
708                                  {                                  {
709                                          if (str_len == 0)                                          BBS_last_access_tm = time(NULL);
710    
711                                            if (str_len == 0) // ch >= 32 && ch < 127
712                                          {                                          {
713                                                  input_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
714                                                  str_len = 1;                                                  str_len = 1;
715                                          }                                          }
716    
717                                          last_updated_line = line_current;                                          last_updated_line = line_current;
718                                          display_line_in = line_current - screen_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
719                                          offset_in = col_pos - 1;                                          offset_in = col_pos - 1;
720                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
721                                          offset_out = offset_in;                                          offset_out = offset_in;
722    
723                                          if (!key_insert) // overwrite                                          if (!key_insert) // overwrite
724                                          {                                          {
725                                                  if (editor_data_delete(p_editor_data, display_line_in, offset_in,                                                  if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
726                                                                                             &last_updated_line) < 0)                                                                                             &last_updated_line) < 0)
727                                                  {                                                  {
728                                                          log_error("editor_data_delete() error\n");                                                          log_error("editor_data_delete() error\n");
# Line 600  int editor_display(EDITOR_DATA *p_editor Line 732  int editor_display(EDITOR_DATA *p_editor
732                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
733                                                                                     input_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
734                                          {                                          {
735                                                  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;  
736                                          }                                          }
737                                          else                                          else
738                                          {                                          {
739                                                  str_len = 0;                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
740                                                    line_current -= (output_current_row - row_pos);
741                                                  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;  
742    
743                                                  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));
744    
745                                                  if (scroll_rows > 0)                                                  if (scroll_rows > 0)
746                                                  {                                                  {
# Line 622  int editor_display(EDITOR_DATA *p_editor Line 751  int editor_display(EDITOR_DATA *p_editor
751                                                                  prints("\033[S"); // Scroll up 1 line                                                                  prints("\033[S"); // Scroll up 1 line
752                                                          }                                                          }
753    
754                                                          screen_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
755                                                          if (screen_current_row < screen_begin_row)                                                          if (output_current_row < screen_begin_row)
756                                                          {                                                          {
757                                                                  line_current += (screen_begin_row - screen_current_row);                                                                  line_current += (screen_begin_row - output_current_row);
758                                                                  screen_current_row = screen_begin_row;                                                                  output_current_row = screen_begin_row;
759                                                          }                                                          }
760                                                          row_pos = screen_end_row;                                                          row_pos = output_end_row;
761                                                  }                                                  }
762                                                  else // if (scroll_lines == 0)                                                  else // if (scroll_lines == 0)
763                                                  {                                                  {
764                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
765                                                  }                                                  }
766                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
767                                            }
768    
769                                            if (display_line_out != display_line_in) // Output on line change
770                                            {
771                                                    break;
772                                            }
773    
774                                                  continue;                                          ch = igetch(0);
775                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
776                                            {
777                                                    break;
778                                          }                                          }
779    
780                                            str_len = 0;
781                                            continue;
782                                  }                                  }
783                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
784                                  {                                  {
785                                          last_updated_line = line_current;                                          BBS_last_access_tm = time(NULL);
786    
787                                            if (ch == BACKSPACE)
788                                            {
789                                                    if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
790                                                    {
791                                                            ch = igetch_t(MAX_DELAY_TIME);
792                                                            continue;
793                                                    }
794    
795                                                    col_pos--;
796                                                    if (col_pos > 1 &&
797                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK
798                                                    {
799                                                            col_pos--;
800                                                    }
801    
802                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
803                                                    {
804                                                            row_pos--;
805                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
806                                                    }
807                                            }
808    
809                                            display_line_in = line_current - output_current_row + row_pos;
810                                            offset_in = col_pos - 1;
811                                            display_line_out = display_line_in;
812                                            offset_out = offset_in;
813    
814                                          if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,                                          if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
815                                                                                     &last_updated_line) < 0)                                                                                                            &last_updated_line)) < 0)
816                                          {                                          {
817                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
818                                          }                                          }
819                                          else                                          else
820                                          {                                          {
821                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
822                                                  line_current -= (screen_current_row - row_pos);  
823                                                  screen_current_row = (int)row_pos;                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
824                                                    line_current -= (output_current_row - row_pos);
825                                                    output_current_row = (int)row_pos;
826    
827                                                    if (output_current_row < screen_begin_row) // row_pos <= 0
828                                                    {
829                                                            output_current_row = screen_begin_row;
830                                                            row_pos = screen_begin_row;
831                                                            output_end_row = SCREEN_ROWS - 1;
832                                                    }
833    
834                                                    // Exceed end
835                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
836                                                            p_editor_data->display_line_total > screen_row_total)
837                                                    {
838                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
839                                                                                                    (p_editor_data->display_line_total - screen_row_total));
840    
841                                                            line_current = p_editor_data->display_line_total - screen_row_total;
842                                                            row_pos += scroll_rows;
843                                                            output_current_row = screen_begin_row;
844                                                            output_end_row = SCREEN_ROWS - 1;
845                                                    }
846    
847                                                    clrline(output_current_row, output_end_row);
848                                            }
849    
850                                            if (display_line_out != display_line_in) // Output on line change
851                                            {
852                                                    break;
853                                          }                                          }
854    
855                                            ch = igetch(0);
856                                            if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
857                                            {
858                                                    break;
859                                            }
860    
861                                            str_len = 0;
862                                          continue;                                          continue;
863                                  }                                  }
864    
865                                    input_ok = 1;
866                                  switch (ch)                                  switch (ch)
867                                  {                                  {
868                                  case KEY_NULL:                                  case KEY_NULL:
869                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
870                                          goto cleanup;                                          goto cleanup;
871                                  case Ctrl('C'):                                  case Ctrl('W'):
872                                          loop = 0;                                          loop = 0;
873                                          break;                                          break;
874                                    case Ctrl('S'): // Start of line
875                                  case KEY_CTRL_LEFT:                                  case KEY_CTRL_LEFT:
876                                          col_pos = 1;                                          col_pos = 1;
877                                          break;                                          break;
878                                    case Ctrl('E'): // End of line
879                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
880                                          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
881                                            {
882                                                    // last display line does NOT have \n in the end
883                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
884                                                    break;
885                                            }
886                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
887                                          break;                                          break;
888                                    case Ctrl('T'): // Top of screen
889                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
890                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
891                                          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_lengths[line_current - output_current_row + row_pos]));
892                                          break;                                          break;
893                                    case Ctrl('B'): // Bottom of screen
894                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
895                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
896                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          {
897                                                    row_pos = p_editor_data->display_line_total;
898                                            }
899                                            else
900                                            {
901                                                    row_pos = SCREEN_ROWS - 1;
902                                            }
903                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
904                                            {
905                                                    // last display line does NOT have \n in the end
906                                                    col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);
907                                            }
908                                            else
909                                            {
910                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
911                                            }
912                                          break;                                          break;
913                                  case KEY_INS:                                  case KEY_INS:
914                                          key_insert = !key_insert;                                          key_insert = !key_insert;
# Line 686  int editor_display(EDITOR_DATA *p_editor Line 916  int editor_display(EDITOR_DATA *p_editor
916                                  case KEY_HOME:                                  case KEY_HOME:
917                                          row_pos = 1;                                          row_pos = 1;
918                                          col_pos = 1;                                          col_pos = 1;
919                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
920                                          {                                          {
921                                                  break;                                                  break;
922                                          }                                          }
923                                          line_current = 0;                                          line_current = 0;
924                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
925                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
926                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
927                                          break;                                          break;
928                                  case KEY_END:                                  case KEY_END:
929                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
930                                          {                                          {
931                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
932                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
933                                                  break;                                                  break;
934                                          }                                          }
935                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
936                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
937                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
938                                          row_pos = screen_row_total;                                          row_pos = SCREEN_ROWS - 1;
939                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
940                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
941                                          break;                                          break;
942                                  case KEY_LEFT:                                  case KEY_LEFT:
943                                          if (col_pos > 1)                                          if (col_pos > 1)
944                                          {                                          {
945                                                  col_pos--;                                                  col_pos--;
946                                                    if (col_pos > 1 &&
947                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
948                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK
949                                                    {
950                                                            col_pos--;
951                                                    }
952                                                  break;                                                  break;
953                                          }                                          }
954                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 720  int editor_display(EDITOR_DATA *p_editor Line 956  int editor_display(EDITOR_DATA *p_editor
956                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
957                                          {                                          {
958                                                  row_pos--;                                                  row_pos--;
959                                                  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_lengths[line_current - output_current_row + row_pos]));
960                                                  break;                                                  break;
961                                          }                                          }
962                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
963                                          {                                          {
964                                                  col_pos = 1;                                                  col_pos = 1;
965                                                  break;                                                  break;
966                                          }                                          }
967                                          line_current -= screen_current_row;                                          line_current -= output_current_row;
968                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
969                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
970                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
971                                          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
972                                          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_lengths[line_current - output_current_row + row_pos]));
973                                          break;                                          break;
974                                  case KEY_SPACE:                                  case KEY_SPACE:
975                                          break;                                          break;
976                                  case KEY_RIGHT:                                  case KEY_RIGHT:
977                                          if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
978                                          {                                          {
979                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
980                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK
981                                                    {
982                                                            col_pos++;
983                                                    }
984                                                  col_pos++;                                                  col_pos++;
985                                                  break;                                                  break;
986                                          }                                          }
# Line 748  int editor_display(EDITOR_DATA *p_editor Line 989  int editor_display(EDITOR_DATA *p_editor
989                                          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))
990                                          {                                          {
991                                                  row_pos++;                                                  row_pos++;
992                                                  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_lengths[line_current - output_current_row + row_pos]));
993                                                  break;                                                  break;
994                                          }                                          }
995                                          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
996                                          {                                          {
997                                                  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
998                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
999                                                  break;                                                  break;
1000                                          }                                          }
1001                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
1002                                          screen_current_row = screen_row_total;                                          output_current_row = screen_row_total;
1003                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1004                                          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_lengths[line_current - output_current_row + row_pos]));
1005                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
1006                                          clrtoeol();                                          clrtoeol();
1007                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
1008                                          break;                                          break;
1009                                  case KEY_PGUP:                                  case KEY_PGUP:
1010                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
1011                                          {                                          {
1012                                                  break;                                                  break;
1013                                          }                                          }
1014                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
1015                                          if (line_current < 0)                                          if (line_current < 0)
1016                                          {                                          {
1017                                                  line_current = 0;                                                  line_current = 0;
1018                                          }                                          }
1019                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1020                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1021                                          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_lengths[line_current - output_current_row + row_pos]));
1022                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1023                                          break;                                          break;
1024                                  case KEY_PGDN:                                  case KEY_PGDN:
1025                                          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
1026                                          {                                          {
1027                                                  break;                                                  break;
1028                                          }                                          }
1029                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
1030                                          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
1031                                          {                                          {
1032                                                  line_current = p_editor_data->display_line_total - screen_row_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
1033                                          }                                          }
1034                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1035                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1036                                          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_lengths[line_current - output_current_row + row_pos]));
1037                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
1038                                          break;                                          break;
1039                                  case KEY_F1:                                  case KEY_F1:
1040                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not reentrant
# Line 807  int editor_display(EDITOR_DATA *p_editor Line 1047  int editor_display(EDITOR_DATA *p_editor
1047                                          show_help = 1;                                          show_help = 1;
1048                                  case KEY_F5:                                  case KEY_F5:
1049                                          // Refresh after display help information                                          // Refresh after display help information
1050                                          line_current -= (screen_current_row - screen_begin_row);                                          line_current -= (output_current_row - screen_begin_row);
1051                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1052                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1053                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1054                                          break;                                          break;
1055                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
1056                                          break;                                          break;
# Line 819  int editor_display(EDITOR_DATA *p_editor Line 1059  int editor_display(EDITOR_DATA *p_editor
1059                                          break;                                          break;
1060                                  }                                  }
1061    
1062                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(NULL);
1063    
1064                                    if (input_ok)
1065                                    {
1066                                            break;
1067                                    }
1068    
1069                                    ch = igetch_t(MAX_DELAY_TIME);
1070                          }                          }
1071    
1072                          continue;                          continue;
# Line 837  int editor_display(EDITOR_DATA *p_editor Line 1084  int editor_display(EDITOR_DATA *p_editor
1084                          len = 0;                          len = 0;
1085                  }                  }
1086    
1087                  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);
1088                  buffer[len] = '\0';                  // Replace '\033' with '*'
1089                    p_str = p_editor_data->p_display_lines[line_current];
1090                    for (i = 0, j = 0; i < len; i++)
1091                    {
1092                            if (p_str[i] == '\033')
1093                            {
1094                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1095                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1096                            }
1097                            else
1098                            {
1099                                    buffer[j] = p_str[i];
1100                                    j++;
1101                            }
1102                    }
1103                    buffer[j] = '\0';
1104    
1105                  moveto(screen_current_row, 0);                  moveto(output_current_row, 0);
1106                  clrtoeol();                  clrtoeol();
1107                  prints("%s", buffer);                  prints("%s", buffer);
1108                  line_current++;                  line_current++;
1109                  screen_current_row++;                  output_current_row++;
1110          }          }
1111    
1112  cleanup:  cleanup:


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

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