/[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.2 by sysadm, Mon Jun 9 15:41:09 2025 UTC Revision 1.22 by sysadm, Sun Jun 15 04:43:33 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>
27    
28  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
29  #include <string.h>  #include <string.h>
30    
31    #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
32    #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000
33    #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)
34    
35    static MEMORY_POOL *p_mp_data_line;
36    static MEMORY_POOL *p_mp_editor_data;
37    
38    int editor_memory_pool_init(void)
39    {
40            if (p_mp_data_line != NULL || p_mp_editor_data != NULL)
41            {
42                    log_error("Editor mem pool already initialized\n");
43                    return -1;
44            }
45    
46            p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT);
47            if (p_mp_data_line == NULL)
48            {
49                    log_error("Memory pool init error\n");
50                    return -2;
51            }
52    
53            p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
54            if (p_mp_data_line == NULL)
55            {
56                    log_error("Memory pool init error\n");
57                    return -3;
58            }
59    
60            return 0;
61    }
62    
63    void editor_memory_pool_cleanup(void)
64    {
65            if (p_mp_data_line != NULL)
66            {
67                    memory_pool_cleanup(p_mp_data_line);
68                    p_mp_data_line = NULL;
69            }
70    
71            if (p_mp_editor_data != NULL)
72            {
73                    memory_pool_cleanup(p_mp_editor_data);
74                    p_mp_editor_data = NULL;
75            }
76    }
77    
78  EDITOR_DATA *editor_data_load(const char *p_data)  EDITOR_DATA *editor_data_load(const char *p_data)
79  {  {
80          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
81          long line_offsets[MAX_EDITOR_DATA_LINES];          char *p_data_line = NULL;
82            long line_offsets[MAX_EDITOR_DATA_LINES + 1];
83          long current_data_line_length = 0;          long current_data_line_length = 0;
84          long i, j;          long i;
85    
86          if (p_data == NULL)          if (p_data == NULL)
87          {          {
# Line 39  EDITOR_DATA *editor_data_load(const char Line 89  EDITOR_DATA *editor_data_load(const char
89                  return NULL;                  return NULL;
90          }          }
91    
92          p_editor_data = malloc(sizeof(EDITOR_DATA));          p_editor_data = memory_pool_alloc(p_mp_editor_data);
93          if (p_editor_data == NULL)          if (p_editor_data == NULL)
94          {          {
95                  log_error("malloc(EDITOR_DATA) error: OOM\n");                  log_error("memory_pool_alloc() error\n");
96                  return NULL;                  return NULL;
97          }          }
98    
99          p_editor_data->data_line_total = 0;          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1);
         p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);  
100    
101          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
102          {          {
# Line 57  EDITOR_DATA *editor_data_load(const char Line 106  EDITOR_DATA *editor_data_load(const char
106                          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 ||
107                          (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'))
108                  {                  {
                         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;  
                         }  
   
109                          // Allocate new data line                          // Allocate new data line
110                          p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                          p_data_line = memory_pool_alloc(p_mp_data_line);
111                          if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)                          if (p_data_line == NULL)
112                          {                          {
113                                  log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);                                  log_error("memory_pool_alloc() error: i = %d\n", i);
114                                  // Cleanup                                  // Cleanup
115                                  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);  
116                                  return NULL;                                  return NULL;
117                          }                          }
118    
119                          p_editor_data->p_display_lines[i] = p_editor_data->p_data_lines[p_editor_data->data_line_total];                          p_editor_data->p_display_lines[i] = p_data_line;
                         (p_editor_data->data_line_total)++;  
120                          current_data_line_length = 0;                          current_data_line_length = 0;
121                  }                  }
122                  else                  else
# Line 88  EDITOR_DATA *editor_data_load(const char Line 126  EDITOR_DATA *editor_data_load(const char
126    
127                  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]);
128                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
129                  p_editor_data->p_data_lines[p_editor_data->data_line_total - 1][current_data_line_length] = '\0';  
130                    // Trim \n from last line
131                    if (i + 1 == p_editor_data->display_line_total &&
132                            p_editor_data->display_line_lengths[i] > 0 &&
133                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
134                    {
135                            p_editor_data->display_line_lengths[i]--;
136                            current_data_line_length--;
137                    }
138                    p_data_line[current_data_line_length] = '\0';
139          }          }
140    
141            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);
142    
143          return p_editor_data;          return p_editor_data;
144  }  }
145    
# Line 125  long editor_data_save(const EDITOR_DATA Line 174  long editor_data_save(const EDITOR_DATA
174    
175  void editor_data_cleanup(EDITOR_DATA *p_editor_data)  void editor_data_cleanup(EDITOR_DATA *p_editor_data)
176  {  {
177            char *p_data_line = NULL;
178          long i;          long i;
179    
180          if (p_editor_data == NULL)          if (p_editor_data == NULL)
# Line 132  void editor_data_cleanup(EDITOR_DATA *p_ Line 182  void editor_data_cleanup(EDITOR_DATA *p_
182                  return;                  return;
183          }          }
184    
185          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)          for (i = 0; i < p_editor_data->display_line_total; i++)
186          {          {
187                  free(p_editor_data->p_data_lines[i]);                  if (p_data_line == NULL)
188                  p_editor_data->p_data_lines[i] = NULL;                  {
189                            p_data_line = p_editor_data->p_display_lines[i];
190                    }
191    
192                    if (p_editor_data->display_line_lengths[i] > 0 &&
193                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
194                    {
195                            memory_pool_free(p_mp_data_line, p_data_line);
196                            p_data_line = NULL;
197                    }
198          }          }
199    
200          free(p_editor_data);          if (p_data_line != NULL)
201            {
202                    memory_pool_free(p_mp_data_line, p_data_line);
203            }
204    
205            memory_pool_free(p_mp_editor_data, p_editor_data);
206  }  }
207    
208  int editor_data_insert(EDITOR_DATA *p_editor_data, long display_line, long offset,  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
209                                             const char *str, int str_len, long *p_last_updated_line)                                             const char *str, int str_len, long *p_last_updated_line)
210  {  {
211          int len;          long display_line = *p_display_line;
212          int display_len;          long offset = *p_offset;
213          int eol;          char *p_data_line = NULL;
         char *p_data_line;  
214          long len_data_line;          long len_data_line;
215          long offset_data_line;          long offset_data_line;
216          long last_display_line; // of data line          long last_display_line; // of data line
217          char buf_insert[MAX_EDITOR_DATA_LINE_LENGTH];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
218          long len_insert;          long split_line_total;
219          int display_len_insert;          long i, j;
220          char buf_catenate[MAX_EDITOR_DATA_LINE_LENGTH];          int len;
221          long len_catenate;          int eol;
222          long i;          int display_len;
223    
224          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
225          {          {
# Line 164  int editor_data_insert(EDITOR_DATA *p_ed Line 227  int editor_data_insert(EDITOR_DATA *p_ed
227                  return -1;                  return -1;
228          }          }
229    
230          memcpy(buf_insert, str, (size_t)str_len);          // Validate str
231          buf_insert[str_len] = '\0';          if ((str_len == 1 && str[0] <= 0) ||
232          len_insert = str_len;                  (str_len == 2 && (str[0] >= 0 || str[1] >= 0)))
233            {
234                    log_error("Invalid input str, len=%d\n", str_len);
235                    return -2;
236            }
237    
238          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
239          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
240          {          {
241                  if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK                  if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
242                  {                  {
243                          i++;                          i++;
244                  }                  }
# Line 210  int editor_data_insert(EDITOR_DATA *p_ed Line 277  int editor_data_insert(EDITOR_DATA *p_ed
277          }          }
278    
279          // Split current data line if over-length          // Split current data line if over-length
280          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH)          if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
281          {          {
282                  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)
283                  {                  {
284                          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",
285                                            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);
286                          return -2;                          return -2;
287                  }                  }
288    
289                  // Allocate new data line                  // Allocate new data line
290                  p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                  p_data_line = memory_pool_alloc(p_mp_data_line);
291                  if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)                  if (p_data_line == NULL)
292                  {                  {
293                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");                          log_error("memory_pool_alloc() error\n");
294                          return -2;                          return -2;
295                  }                  }
296    
297                  // Copy rest part of current data line since next display line to new data line                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
                 memcpy(p_editor_data->p_data_lines[p_editor_data->data_line_total],  
                            p_editor_data->p_display_lines[display_line + 1],  
                            (size_t)(len_data_line - (p_editor_data->p_display_lines[display_line + 1] - p_data_line)));  
                 p_editor_data->p_data_lines[p_editor_data->data_line_total]  
                                                                    [len_data_line - (p_editor_data->p_display_lines[display_line + 1] - p_data_line)] = '\0';  
   
                 p_data_line = p_editor_data->p_display_lines[display_line + 1];  
                 for (i = display_line + 1; i <= last_display_line; i++)  
298                  {                  {
299                          p_editor_data->p_display_lines[i] +=                          if (str[0] == CR)
300                                  (p_editor_data->p_data_lines[p_editor_data->data_line_total] - p_data_line);                          {
301                  }                                  str_len = 0;
302                            }
303    
304                  // Copy rest part of current display line to buffer                          // Copy str to new data line
305                  if (offset_data_line >= MAX_EDITOR_DATA_LINE_LENGTH / 2)                          memcpy(p_data_line, str, (size_t)str_len);
306                  {  
307                          memcpy(buf_insert + len_insert,                          // Copy rest part of current data line to new data line
308                                     p_editor_data->p_display_lines[display_line] + offset,                          memcpy(p_data_line + str_len,
                                    (size_t)(p_editor_data->display_line_lengths[display_line] - offset));  
                         len_insert += (p_editor_data->display_line_lengths[display_line] - offset);  
                 }  
                 else  
                 {  
                         memcpy(buf_insert,  
309                                     p_editor_data->p_display_lines[display_line] + offset,                                     p_editor_data->p_display_lines[display_line] + offset,
310                                     (size_t)(p_editor_data->display_line_lengths[display_line] - offset));                                     (size_t)(len_data_line - offset_data_line));
311                          len_insert = (p_editor_data->display_line_lengths[display_line] - offset);  
312                  }                          p_data_line[str_len + len_data_line - offset_data_line] = '\0';
                 buf_insert[len_insert] = '\0';  
313    
                 if (offset_data_line >= MAX_EDITOR_DATA_LINE_LENGTH / 2)  
                 {  
314                          // Add line ending to current display line (data line)                          // Add line ending to current display line (data line)
315                          p_editor_data->p_display_lines[display_line][offset] = '\n';                          p_editor_data->p_display_lines[display_line][offset] = '\n';
316                          p_editor_data->p_display_lines[display_line][offset + 1] = '\0';                          p_editor_data->p_display_lines[display_line][offset + 1] = '\0';
317                          p_editor_data->display_line_lengths[display_line] = offset + 1;                          p_editor_data->display_line_lengths[display_line] = offset + 1;
318    
319                            *p_display_line = display_line + 1;
320                            *p_offset = str_len;
321                  }                  }
322                  else                  else
323                  {                  {
324                            // Copy rest part of current data line to new data line
325                            memcpy(p_data_line,
326                                       p_editor_data->p_display_lines[display_line] + offset,
327                                       (size_t)(len_data_line - offset_data_line));
328    
329                            p_data_line[len_data_line - offset_data_line] = '\0';
330    
331                            // Append str to current display line
332                          memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);                          memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);
333    
334                          // Add line ending to current display line (data line)                          // Add line ending to current display line (data line)
335                          p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';                          p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';
336                          p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';                          p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';
337                          p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;                          p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;
338    
339                            *p_display_line = display_line;
340                            *p_offset = offset + str_len;
341                  }                  }
342    
343                    split_line_total = last_display_line - display_line + 3;
344    
345                    // Set start display_line for spliting new data line
346                  display_line++;                  display_line++;
                 offset = 0;  
347    
348                  *p_last_updated_line = p_editor_data->display_line_total;                  *p_last_updated_line = p_editor_data->display_line_total;
349            }
350            else // insert str into current data line at offset_data_line
351            {
352                    memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line));
353                    memcpy(p_data_line + offset_data_line, str, (size_t)str_len);
354                    p_data_line[len_data_line + str_len] = '\0';
355    
356                    // Set p_data_line to head of current display line
357                    p_data_line = p_editor_data->p_display_lines[display_line];
358                    split_line_total = last_display_line - display_line + 3;
359    
360                  last_display_line++;                  *p_display_line = display_line;
361                  (p_editor_data->display_line_total)++;                  *p_offset = offset + str_len;
                 (p_editor_data->data_line_total)++;  
362          }          }
363    
364          for (i = display_line; len_insert > 0 && i <= last_display_line; i++)          // Split current data line since beginning of current display line
365            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
366    
367            for (i = 0; i < split_line_total; i++)
368          {          {
369                  len = split_line(buf_insert, SCREEN_COLS, &eol, &display_len_insert);                  if (display_line + i > last_display_line)
370                  if (len != len_insert)                  {
371                            // Insert blank display line after last_display_line
372                            if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
373                            {
374                                    // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
375                                    // Terminate prior display line with \n, to avoid error on cleanup
376                                    if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
377                                    {
378                                            len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len);
379                                            p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
380                                            p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
381                                            p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
382                                    }
383                                    if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
384                                    {
385                                            *p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1;
386                                    }
387                                    break;
388                            }
389                            for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
390                            {
391                                    p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
392                                    p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
393                            }
394                            last_display_line++;
395                            (p_editor_data->display_line_total)++;
396                    }
397    
398                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
399                    p_editor_data->p_display_lines[display_line + i] =
400                            (i == 0
401                                     ? p_data_line
402                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
403    
404                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
405                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
406                  {                  {
407                          log_error("buf_insert is truncated at display_line(%ld): len(%d) != len_insert(%d), buf_insert: %s\n",                          break;
                                           i, len, len_insert, buf_insert);  
                         return -3;  
408                  }                  }
409            }
410    
411                  memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)p_editor_data->display_line_lengths[i]);          *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
                 buf_catenate[p_editor_data->display_line_lengths[i]] = '\0';  
412    
413                  len = split_line(buf_catenate, SCREEN_COLS - display_len_insert, &eol, &display_len);          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
414                  if (len < offset) // have no space to insert          {
415                    if (*p_display_line + 1 < p_editor_data->display_line_total)
416                  {                  {
417                          offset = 0;                          *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
418                          continue; // retry at next display line                          (*p_display_line)++;
419                  }                  }
420            }
421    
422                  // move \n to next display line if current line is full          // Prevent the last display line from being over-length
423                  if (len > 0 && buf_catenate[len - 1] == '\n' && display_len + display_len_insert >= SCREEN_COLS)          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
424            {
425                    len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len);
426                    p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
427                    p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
428                    if (*p_display_line + 1 >= p_editor_data->display_line_total)
429                  {                  {
430                          len--;                          *p_offset = MIN(*p_offset, len);
431                            *p_display_line = p_editor_data->display_line_total - 1;
432                  }                  }
433            }
434    
435                  memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)offset);          return 0;
436                  memcpy(buf_catenate + offset, buf_insert, (size_t)len_insert);  }
                 memcpy(buf_catenate + offset + len_insert, p_editor_data->p_display_lines[i] + offset, (size_t)(len - offset));  
                 len_catenate = len_insert + len;  
                 buf_catenate[len_catenate] = '\0';  
437    
438                  offset = 0;  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,
439                  len_insert = p_editor_data->display_line_lengths[i] - len;                                             long *p_last_updated_line)
440                  if (len_insert > 0)  {
441            char *p_data_line = NULL;
442            long len_data_line;
443            long offset_data_line;
444            long last_display_line; // of data line
445            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
446            long split_line_total;
447            long i, j;
448            int str_len = 0;
449    
450            if (p_editor_data == NULL || p_last_updated_line == NULL)
451            {
452                    log_error("editor_data_op() error: NULL pointer\n");
453                    return -1;
454            }
455    
456            // Get accurate offset of first character of CJK at offset position
457            for (i = 0; i < offset; i++)
458            {
459                    if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
460                  {                  {
461                          memcpy(buf_insert, p_editor_data->p_display_lines[i] + len, (size_t)len_insert);                          i++;
462                          buf_insert[len_insert] = '\0';                  }
463            }
464            if (i > offset) // offset was skipped
465            {
466                    offset--;
467            }
468    
469            // Get length of current data line
470            len_data_line = 0;
471            p_data_line = p_editor_data->p_display_lines[display_line];
472            for (i = display_line - 1; i >= 0; i--)
473            {
474                    if (p_editor_data->display_line_lengths[i] > 0 &&
475                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
476                    {
477                            break;
478                  }                  }
479    
480                  memcpy(p_editor_data->p_display_lines[i], buf_catenate, (size_t)len_catenate);                  len_data_line += p_editor_data->display_line_lengths[i];
481                  p_editor_data->display_line_lengths[i] = len_catenate;                  p_data_line = p_editor_data->p_display_lines[i];
482          }          }
483            offset_data_line = len_data_line + offset;
484            last_display_line = p_editor_data->display_line_total - 1;
485            for (i = display_line; i < p_editor_data->display_line_total; i++)
486            {
487                    len_data_line += p_editor_data->display_line_lengths[i];
488    
489          *p_last_updated_line = MAX(i, *p_last_updated_line);                  if (p_editor_data->display_line_lengths[i] > 0 &&
490                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
491                    {
492                            last_display_line = i;
493                            break;
494                    }
495            }
496    
497          if (len_insert > 0)          if (offset_data_line >= len_data_line) // end-of-line
498          {          {
499                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  return 0;
500            }
501    
502            // Check str to be deleted
503            if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
504            {
505                    str_len = 1;
506            }
507            else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK
508            {
509                    str_len = 2;
510            }
511            else
512            {
513                    log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
514                                      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
519            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'))
521            {
522                    if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
523                  {                  {
524                          log_error("Append line error, display_line_total(%ld) reach limit(%d)\n",                          return 0;
                                           p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);  
                         return -2;  
525                  }                  }
526    
527                  // Prepare one blank display line after last_display_line                  len_data_line = 0; // Next data line
528                  for (i = p_editor_data->display_line_total; i > last_display_line + 1; i--)                  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                          p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1];                          len_data_line += p_editor_data->display_line_lengths[i];
532                          p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1];  
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                  }                  }
                 p_editor_data->p_display_lines[last_display_line + 1] =  
                         p_editor_data->p_display_lines[last_display_line] + p_editor_data->display_line_lengths[last_display_line];  
                 p_editor_data->display_line_lengths[last_display_line + 1] = 0;  
540    
541                  (p_editor_data->display_line_total)++;                  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                  last_display_line++;                  {
543                            return 0;
544                    }
545    
546                  // Fill data into blank display line                  // Append next data line to current one
547                  memcpy(p_editor_data->p_display_lines[last_display_line], buf_insert, (size_t)len_insert);                  memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
548                  p_editor_data->p_display_lines[last_display_line][len_insert] = '\0';                  p_data_line[offset_data_line + len_data_line] = '\0';
                 p_editor_data->display_line_lengths[last_display_line] = len_insert;  
549    
550                  *p_last_updated_line = MAX(last_display_line, *p_last_updated_line);                  // 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          return 0;          // Set p_data_line to head of current display line
561  }          p_data_line = p_editor_data->p_display_lines[display_line];
562            split_line_total = last_display_line - display_line + 2;
563    
564  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,          // Split current data line since beginning of current display line
565                                             long *p_last_updated_line)          split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
566  {  
567          return 0;          for (i = 0; i < split_line_total; i++)
568            {
569                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
570                    p_editor_data->p_display_lines[display_line + i] =
571                            (i == 0
572                                     ? p_data_line
573                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
574    
575                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
576                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
577                    {
578                            break;
579                    }
580            }
581    
582            *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
583    
584            if (*p_last_updated_line < last_display_line)
585            {
586                    // Remove redundant display line after last_display_line
587                    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 - *p_last_updated_line)] = p_editor_data->p_display_lines[j];
590                            p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];
591                    }
592    
593                    j = p_editor_data->display_line_total;
594                    (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);
596            }
597    
598            return str_len;
599  }  }
600    
601  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 376  static int editor_display_key_handler(in Line 604  static int editor_display_key_handler(in
604          {          {
605          case 0: // Set msg          case 0: // Set msg
606                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
607                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");
608                  break;                  break;
609          }          }
610    
# Line 389  int editor_display(EDITOR_DATA *p_editor Line 617  int editor_display(EDITOR_DATA *p_editor
617          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
618          EDITOR_CTX ctx;          EDITOR_CTX ctx;
619          int ch = 0;          int ch = 0;
620          char hz_ch[4];          char input_str[4];
621          int hz_len;          int str_len = 0;
622          int input_ok, screen_current_line;          int input_ok;
623          const int screen_begin_line = 1;          const int screen_begin_row = 1;
624          int screen_end_line = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
625          const int screen_line_total = screen_end_line - screen_begin_line + 1;          int output_current_row = screen_begin_row;
626            int output_end_row = SCREEN_ROWS - 1;
627          long int line_current = 0;          long int line_current = 0;
628          long int len;          long int len;
629          int loop;          int loop;
630          int eol, display_len;          int eol, display_len;
631          long row_pos = 1, col_pos = 1;          long row_pos = 1, col_pos = 1;
632            long display_line_in, offset_in;
633            long display_line_out, offset_out;
634            int scroll_rows;
635          long last_updated_line = 0;          long last_updated_line = 0;
636          int insert = 1;          int key_insert = 1;
637            int i, j;
638            char *p_str;
639    
640          screen_current_line = screen_begin_line;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_line, SCREEN_ROWS);  
641    
642          // update msg_ext with extended key handler          // update msg_ext with extended key handler
643          if (editor_display_key_handler(&ch, &ctx) != 0)          if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 415  int editor_display(EDITOR_DATA *p_editor Line 648  int editor_display(EDITOR_DATA *p_editor
648          loop = 1;          loop = 1;
649          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
650          {          {
651                  if (line_current >= p_editor_data->display_line_total || screen_current_line > screen_end_line)                  if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row)
652                  {                  {
653                          ctx.line_cursor = line_current - screen_current_line + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
654    
655                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
656                                           "\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] "
657                                           "µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ "                                           "µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ [\033[32m%s\033[33m] "
658                                           "%s",                                           "%s",
659                                           row_pos, col_pos,                                           row_pos, col_pos,
660                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
661                                             key_insert ? "²åÈë" : "¸Äд",
662                                           ctx.msg);                                           ctx.msg);
663    
664                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);
# Line 453  int editor_display(EDITOR_DATA *p_editor Line 687  int editor_display(EDITOR_DATA *p_editor
687                                          goto cleanup;                                          goto cleanup;
688                                  }                                  }
689    
690                                  if (ch >= 32 && ch < 127) // printable character                                  if (ch > 127 && ch <= 255) // GBK
691                                  {                                  {
692                                          last_updated_line = line_current;                                          input_str[str_len] = (char)(ch - 256);
693                                            str_len++;
694                                    }
695                                    else if (str_len > 0)
696                                    {
697                                            log_error("Received %d character over 127 followed by character less than 127\n", str_len);
698                                            str_len = 0;
699                                    }
700    
701                                          if (editor_data_insert(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1,                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK
702                                                                                     (const char *)&ch, 1, &last_updated_line) < 0)                                          ch == CR || ch == KEY_ESC)                                                                                       // Special character
703                                    {
704                                            if (str_len == 0) // ch >= 32 && ch < 127
705                                          {                                          {
706                                                  log_error("editor_data_op(INSERT ch) error\n");                                                  input_str[0] = (char)ch;
707                                                    str_len = 1;
708                                          }                                          }
                                         else  
                                         {  
                                                 screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current));  
                                                 line_current -= (screen_current_line - row_pos);  
                                                 screen_current_line = (int)row_pos;  
709    
710                                                  col_pos++;                                          last_updated_line = line_current;
711                                                  if (col_pos <= p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])                                          display_line_in = line_current - output_current_row + row_pos;
712                                            offset_in = col_pos - 1;
713                                            display_line_out = display_line_in;
714                                            offset_out = offset_in;
715    
716                                            if (!key_insert) // overwrite
717                                            {
718                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
719                                                                                               &last_updated_line) < 0)
720                                                  {                                                  {
721                                                          continue;                                                          log_error("editor_data_delete() error\n");
722                                                  }                                                  }
                                                 col_pos = 1;  
                                                 ch = KEY_DOWN;  
723                                          }                                          }
                                 }  
                                 else if (ch > 127 && ch <= 255) // CJK character  
                                 {  
                                         hz_ch[hz_len] = (char)(ch - 256);  
                                         hz_len++;  
724    
725                                          if (hz_len == 2) // GBK                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
726                                                                                       input_str, str_len, &last_updated_line) < 0)
727                                            {
728                                                    log_error("editor_data_insert(str_len=%d) error\n", str_len);
729                                            }
730                                            else
731                                          {                                          {
732                                                  hz_len = 0;                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
733                                                  last_updated_line = line_current;                                                  line_current -= (output_current_row - row_pos);
734                                                    output_current_row = (int)row_pos;
735    
736                                                  if (editor_data_insert(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1,                                                  scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row));
737                                                                                             hz_ch, 2, &last_updated_line) < 0)  
738                                                  {                                                  if (scroll_rows > 0)
                                                         log_error("editor_data_op(INSERT hz) error\n");  
                                                 }  
                                                 else  
739                                                  {                                                  {
740                                                          screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current));                                                          moveto(SCREEN_ROWS, 0);
741                                                          line_current -= (screen_current_line - row_pos);                                                          clrtoeol();
742                                                          screen_current_line = (int)row_pos;                                                          for (i = 0; i < scroll_rows; i++)
743                                                            {
744                                                                    prints("\033[S"); // Scroll up 1 line
745                                                            }
746    
747                                                          col_pos += 2;                                                          output_current_row -= scroll_rows;
748                                                          if (col_pos <= p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])                                                          if (output_current_row < screen_begin_row)
749                                                          {                                                          {
750                                                                  continue;                                                                  line_current += (screen_begin_row - output_current_row);
751                                                                    output_current_row = screen_begin_row;
752                                                          }                                                          }
753                                                          col_pos = 1;                                                          row_pos = output_end_row;
754                                                          ch = KEY_DOWN;                                                  }
755                                                    else // if (scroll_lines == 0)
756                                                    {
757                                                            row_pos += (display_line_out - display_line_in);
758                                                  }                                                  }
759                                                    col_pos = offset_out + 1;
760                                          }                                          }
761    
762                                            str_len = 0;
763                                            continue;
764                                  }                                  }
765                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
766                                  {                                  {
767                                          last_updated_line = line_current;                                          if (ch == BACKSPACE)
768                                            {
769                                                    if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
770                                                    {
771                                                            input_ok = 0;
772                                                            continue;
773                                                    }
774    
775                                                    col_pos--;
776                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
777                                                    {
778                                                            row_pos--;
779                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
780                                                    }
781                                            }
782    
783                                          if (editor_data_delete(p_editor_data, line_current - screen_current_line + row_pos, col_pos - 1,                                          if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1,
784                                                                                     &last_updated_line) < 0)                                                                                                            &last_updated_line)) < 0)
785                                          {                                          {
786                                                  log_error("editor_data_op(DELETE) error\n");                                                  log_error("editor_data_delete() error\n");
787                                          }                                          }
788                                          else                                          else
789                                          {                                          {
790                                                  screen_end_line = MIN(SCREEN_ROWS - 1, screen_current_line + (int)(last_updated_line - line_current));                                                  if (ch == BACKSPACE)
791                                                    {
792                                                            for (i = 1; i < str_len; i++)
793                                                            {
794                                                                    col_pos--;
795                                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
796                                                                    {
797                                                                            row_pos--;
798                                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
799                                                                    }
800                                                            }
801                                                    }
802    
803                                                    output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
804                                                    line_current -= (output_current_row - row_pos);
805                                                    output_current_row = (int)row_pos;
806    
807                                                    if (output_current_row < screen_begin_row) // row_pos <= 0
808                                                    {
809                                                            output_current_row = screen_begin_row;
810                                                            row_pos = screen_begin_row;
811                                                            output_end_row = SCREEN_ROWS - 1;
812                                                    }
813    
814                                                    // Exceed end
815                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
816                                                            p_editor_data->display_line_total > screen_row_total)
817                                                    {
818                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
819                                                                                                    (p_editor_data->display_line_total - screen_row_total));
820    
821                                                            line_current = p_editor_data->display_line_total - screen_row_total;
822                                                            row_pos += scroll_rows;
823                                                            output_current_row = screen_begin_row;
824                                                            output_end_row = SCREEN_ROWS - 1;
825                                                    }
826    
827                                                    clrline(output_current_row, output_end_row);
828                                          }                                          }
829    
830                                            str_len = 0;
831                                          continue;                                          continue;
832                                  }                                  }
833    
                                 hz_len = 0;  
   
834                                  switch (ch)                                  switch (ch)
835                                  {                                  {
836                                  case KEY_NULL:                                  case KEY_NULL:
837                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
838                                          goto cleanup;                                          goto cleanup;
839                                  case Ctrl('C'):                                  case Ctrl('W'):
840                                          loop = 0;                                          loop = 0;
841                                          break;                                          break;
842                                    case Ctrl('S'): // Start of line
843                                  case KEY_CTRL_LEFT:                                  case KEY_CTRL_LEFT:
844                                          col_pos = 1;                                          col_pos = 1;
845                                          break;                                          break;
846                                    case Ctrl('E'): // End of line
847                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
848                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
849                                            {
850                                                    // last display line does NOT have \n in the end
851                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
852                                                    break;
853                                            }
854                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
855                                          break;                                          break;
856                                    case Ctrl('T'): // Top of screen
857                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
858                                          row_pos = screen_begin_line;                                          row_pos = screen_begin_row;
859                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
860                                          break;                                          break;
861                                    case Ctrl('B'): // Bottom of screen
862                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
863                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
864                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          {
865                                                    row_pos = p_editor_data->display_line_total;
866                                            }
867                                            else
868                                            {
869                                                    row_pos = SCREEN_ROWS - 1;
870                                            }
871                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
872                                            {
873                                                    // last display line does NOT have \n in the end
874                                                    col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);
875                                            }
876                                            else
877                                            {
878                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
879                                            }
880                                          break;                                          break;
881                                  case KEY_INS:                                  case KEY_INS:
882                                          insert = !insert;                                          key_insert = !key_insert;
883                                          break;                                          break;
884                                  case KEY_HOME:                                  case KEY_HOME:
885                                          row_pos = 1;                                          row_pos = 1;
886                                          col_pos = 1;                                          col_pos = 1;
887                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
888                                          {                                          {
889                                                  break;                                                  break;
890                                          }                                          }
891                                          line_current = 0;                                          line_current = 0;
892                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
893                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
894                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
895                                          break;                                          break;
896                                  case KEY_END:                                  case KEY_END:
897                                          if (p_editor_data->display_line_total < screen_line_total)                                          if (p_editor_data->display_line_total < screen_row_total)
898                                          {                                          {
899                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
900                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
901                                                  break;                                                  break;
902                                          }                                          }
903                                          line_current = p_editor_data->display_line_total - screen_line_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
904                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
905                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
906                                          row_pos = screen_line_total;                                          row_pos = SCREEN_ROWS - 1;
907                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                          col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
908                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
909                                          break;                                          break;
910                                  case KEY_LEFT:                                  case KEY_LEFT:
911                                          if (col_pos > 1)                                          if (col_pos > 1)
# Line 586  int editor_display(EDITOR_DATA *p_editor Line 915  int editor_display(EDITOR_DATA *p_editor
915                                          }                                          }
916                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
917                                  case KEY_UP:                                  case KEY_UP:
918                                          if (row_pos > screen_begin_line)                                          if (row_pos > screen_begin_row)
919                                          {                                          {
920                                                  row_pos--;                                                  row_pos--;
921                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
922                                                  break;                                                  break;
923                                          }                                          }
924                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
925                                          {                                          {
926                                                  col_pos = 1;                                                  col_pos = 1;
927                                                  break;                                                  break;
928                                          }                                          }
929                                          line_current -= screen_current_line;                                          line_current -= output_current_row;
930                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
931                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
932                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
933                                          screen_end_line = SCREEN_ROWS - 1; // Legacy Fterm only works with this line                                          output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
934                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
                                         break;  
                                 case CR:  
935                                          break;                                          break;
936                                  case KEY_SPACE:                                  case KEY_SPACE:
937                                          break;                                          break;
938                                  case KEY_RIGHT:                                  case KEY_RIGHT:
939                                          if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos])                                          if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])
940                                          {                                          {
941                                                  col_pos++;                                                  col_pos++;
942                                                  break;                                                  break;
943                                          }                                          }
944                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
945                                  case KEY_DOWN:                                  case KEY_DOWN:
946                                          if (row_pos < MIN(screen_line_total, p_editor_data->display_line_total))                                          if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))
947                                          {                                          {
948                                                  row_pos++;                                                  row_pos++;
949                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                                  col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
950                                                  break;                                                  break;
951                                          }                                          }
952                                          if (line_current + (screen_line_total - (screen_current_line - screen_begin_line)) >= 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
953                                          {                                          {
954                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  // last display line does NOT have \n in the end
955                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
956                                                  break;                                                  break;
957                                          }                                          }
958                                          screen_current_line--;                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
959                                          screen_end_line = SCREEN_ROWS - 1;                                          output_current_row = screen_row_total;
960                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          output_end_row = SCREEN_ROWS - 1;
961                                            col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
962                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
963                                          clrtoeol();                                          clrtoeol();
964                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
965                                          break;                                          break;
966                                  case KEY_PGUP:                                  case KEY_PGUP:
967                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
968                                          {                                          {
969                                                  break;                                                  break;
970                                          }                                          }
971                                          line_current -= ((screen_line_total - 1) + (screen_current_line - screen_begin_line));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
972                                          if (line_current < 0)                                          if (line_current < 0)
973                                          {                                          {
974                                                  line_current = 0;                                                  line_current = 0;
975                                          }                                          }
976                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
977                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
978                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
979                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
980                                          break;                                          break;
981                                  case KEY_PGDN:                                  case KEY_PGDN:
982                                          if (line_current + screen_line_total - (screen_current_line - screen_begin_line) >= 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
983                                          {                                          {
984                                                  break;                                                  break;
985                                          }                                          }
986                                          line_current += (screen_line_total - 1) - (screen_current_line - screen_begin_line);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
987                                          if (line_current + screen_line_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
988                                          {                                          {
989                                                  line_current = p_editor_data->display_line_total - screen_line_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
990                                          }                                          }
991                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
992                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
993                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
994                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
995                                          break;                                          break;
996                                  case KEY_F1:                                  case KEY_F1:
997                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not reentrant
# Line 677  int editor_display(EDITOR_DATA *p_editor Line 1004  int editor_display(EDITOR_DATA *p_editor
1004                                          show_help = 1;                                          show_help = 1;
1005                                  case KEY_F5:                                  case KEY_F5:
1006                                          // Refresh after display help information                                          // Refresh after display help information
1007                                          line_current -= (screen_current_line - screen_begin_line);                                          line_current -= (output_current_row - screen_begin_row);
1008                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
1009                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1010                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1011                                          break;                                          break;
1012                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
1013                                          break;                                          break;
# Line 690  int editor_display(EDITOR_DATA *p_editor Line 1017  int editor_display(EDITOR_DATA *p_editor
1017                                  }                                  }
1018    
1019                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(0);
1020    
1021                                    if (input_ok)
1022                                    {
1023                                            break;
1024                                    }
1025                          }                          }
1026    
1027                          continue;                          continue;
# Line 707  int editor_display(EDITOR_DATA *p_editor Line 1039  int editor_display(EDITOR_DATA *p_editor
1039                          len = 0;                          len = 0;
1040                  }                  }
1041    
1042                  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);
1043                  buffer[len] = '\0';                  // Replace '\033' with '*'
1044                    p_str = p_editor_data->p_display_lines[line_current];
1045                    for (i = 0, j = 0; i < len; i++)
1046                    {
1047                            if (p_str[i] == '\033')
1048                            {
1049                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1050                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1051                            }
1052                            else
1053                            {
1054                                    buffer[j] = p_str[i];
1055                                    j++;
1056                            }
1057                    }
1058                    buffer[j] = '\0';
1059    
1060                  moveto(screen_current_line, 0);                  moveto(output_current_row, 0);
1061                  clrtoeol();                  clrtoeol();
1062                  prints("%s", buffer);                  prints("%s", buffer);
1063                  line_current++;                  line_current++;
1064                  screen_current_line++;                  output_current_row++;
1065          }          }
1066    
1067  cleanup:  cleanup:


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

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