/[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.3 by sysadm, Tue Jun 10 06:48:23 2025 UTC Revision 1.24 by sysadm, Mon Jun 16 01:36:56 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, 0);
         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 *p_display_line, long *p_offset,  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
# Line 146  int editor_data_insert(EDITOR_DATA *p_ed Line 210  int editor_data_insert(EDITOR_DATA *p_ed
210  {  {
211          long display_line = *p_display_line;          long display_line = *p_display_line;
212          long offset = *p_offset;          long offset = *p_offset;
213          int done = 0;          char *p_data_line = NULL;
         int len;  
         int display_len;  
         int eol;  
         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 167  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++)
# Line 213  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                  if (last_display_line > display_line)                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
298                  {                  {
299                          // Copy rest part of current data line (since next display line) to new data line                          if (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';  
   
                         // Relocate rest display lines (since next one) of current data line  
                         p_data_line = p_editor_data->p_display_lines[display_line + 1];  
                         for (i = display_line + 1; i <= last_display_line; i++)  
300                          {                          {
301                                  p_editor_data->p_display_lines[i] =                                  str_len = 0;
                                         p_editor_data->p_data_lines[p_editor_data->data_line_total] +  
                                         (p_editor_data->p_display_lines[i] - p_data_line);  
302                          }                          }
                 }  
                 else // last_display_line == display_line  
                 {  
                         // Insert blank display line pointing to new data line  
                         for (i = p_editor_data->display_line_total; i > display_line + 1; i--)  
                         {  
                                 p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1];  
                                 p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1];  
                         }  
                         p_editor_data->p_display_lines[display_line + 1] = p_editor_data->p_data_lines[p_editor_data->data_line_total];  
                         p_editor_data->display_line_lengths[display_line + 1] = 0;  
303    
304                          (p_editor_data->display_line_total)++;                          // Copy str to new data line
305                          last_display_line++;                          memcpy(p_data_line, str, (size_t)str_len);
                 }  
306    
307                  *p_last_updated_line = p_editor_data->display_line_total;                          // Copy rest part of current data line to new data line
308                  (p_editor_data->data_line_total)++;                          memcpy(p_data_line + str_len,
309                                       p_editor_data->p_display_lines[display_line] + offset,
310                                       (size_t)(len_data_line - offset_data_line));
311    
312                  if (offset_data_line + str_len + 2 < MAX_EDITOR_DATA_LINE_LENGTH)                          p_data_line[str_len + len_data_line - offset_data_line] = '\0';
313    
314                            // Add line ending to current display line (data line)
315                            p_editor_data->p_display_lines[display_line][offset] = '\n';
316                            p_editor_data->p_display_lines[display_line][offset + 1] = '\0';
317                            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
323                  {                  {
324                          // Copy rest part of current display line to insert buffer                          // Copy rest part of current data line to new data line
325                          memcpy(buf_insert,                          memcpy(p_data_line,
326                                     p_editor_data->p_display_lines[display_line] + offset,                                     p_editor_data->p_display_lines[display_line] + offset,
327                                     (size_t)(p_editor_data->display_line_lengths[display_line] - offset));                                     (size_t)(len_data_line - offset_data_line));
328                          len_insert = (p_editor_data->display_line_lengths[display_line] - offset);  
329                          buf_insert[len_insert] = '\0';                          p_data_line[len_data_line - offset_data_line] = '\0';
330    
331                          // Append str to current display line                          // 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);
# Line 283  int editor_data_insert(EDITOR_DATA *p_ed Line 336  int editor_data_insert(EDITOR_DATA *p_ed
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                          if (!done)                          *p_display_line = display_line;
340                          {                          *p_offset = offset + str_len;
                                 *p_display_line = display_line;  
                                 *p_offset = offset + str_len;  
                                 done = 1;  
                         }  
341                  }                  }
                 else  
                 {  
                         // Append rest part of current display line to insert buffer  
                         memcpy(buf_insert + len_insert,  
                                    p_editor_data->p_display_lines[display_line] + offset,  
                                    (size_t)(p_editor_data->display_line_lengths[display_line] - offset));  
                         len_insert += (p_editor_data->display_line_lengths[display_line] - offset);  
                         buf_insert[len_insert] = '\0';  
342    
343                          // Add line ending to current display line (data line)                  split_line_total = last_display_line - display_line + 3;
                         p_editor_data->p_display_lines[display_line][offset] = '\n';  
                         p_editor_data->p_display_lines[display_line][offset + 1] = '\0';  
                         p_editor_data->display_line_lengths[display_line] = offset + 1;  
                 }  
344    
345                    // Set start display_line for spliting new data line
346                  display_line++;                  display_line++;
347                  offset = 0;  
348                    *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          for (i = display_line; len_insert > 0 && i <= last_display_line; i++)                  *p_display_line = display_line;
361                    *p_offset = offset + str_len;
362            }
363    
364            // 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, 0);
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)
                 if (len != len_insert)  
370                  {                  {
371                          log_error("buf_insert is truncated at display_line(%ld): len(%d) != len_insert(%d), buf_insert: %s\n",                          // Insert blank display line after last_display_line
372                                            i, len, len_insert, buf_insert);                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
373                          return -3;                          {
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, 0);
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                  memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)p_editor_data->display_line_lengths[i]);                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
399                  buf_catenate[p_editor_data->display_line_lengths[i]] = '\0';                  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                  len = split_line(buf_catenate, SCREEN_COLS - display_len_insert, &eol, &display_len);                  if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
405                  if (len < offset) // offset out of current display line                          p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
406                  {                  {
407                          offset -= len;                          break;
                         continue;  
408                  }                  }
409            }
410    
411            *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
412    
413                  // move \n to next display line if current line is full          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
414                  if (len > 0 && buf_catenate[len - 1] == '\n' && display_len + display_len_insert >= SCREEN_COLS)          {
415                    if (*p_display_line + 1 < p_editor_data->display_line_total)
416                  {                  {
417                          len--;                          *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
418                            (*p_display_line)++;
419                  }                  }
420            }
421    
422                  memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)offset);          // Prevent the last display line from being over-length
423                  memcpy(buf_catenate + offset, buf_insert, (size_t)len_insert);          if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
424                  memcpy(buf_catenate + offset + len_insert, p_editor_data->p_display_lines[i] + offset, (size_t)(len - offset));          {
425                  len_catenate = len_insert + len;                  len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
426                  buf_catenate[len_catenate] = '\0';                  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                  len_insert = p_editor_data->display_line_lengths[i] - len;                  if (*p_display_line + 1 >= p_editor_data->display_line_total)
                 if (len_insert > 0)  
429                  {                  {
430                          memcpy(buf_insert, p_editor_data->p_display_lines[i] + len, (size_t)len_insert);                          *p_offset = MIN(*p_offset, len);
431                          buf_insert[len_insert] = '\0';                          *p_display_line = p_editor_data->display_line_total - 1;
432                  }                  }
433            }
434    
435                  memcpy(p_editor_data->p_display_lines[i], buf_catenate, (size_t)len_catenate);          return 0;
436                  p_editor_data->display_line_lengths[i] = len_catenate;  }
437    
438                  if (!done)  int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
439                                               long *p_last_updated_line)
440    {
441            long display_line = *p_display_line;
442            long offset = *p_offset;
443            char *p_data_line = NULL;
444            long len_data_line;
445            long offset_data_line;
446            long last_display_line; // of data line
447            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
448            long split_line_total;
449            long i, j;
450            int str_len = 0;
451    
452            if (p_editor_data == NULL || p_last_updated_line == NULL)
453            {
454                    log_error("editor_data_op() error: NULL pointer\n");
455                    return -1;
456            }
457    
458            // Get accurate offset of first character of CJK at offset position
459            for (i = 0; i < offset; i++)
460            {
461                    if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK
462                  {                  {
463                          *p_display_line = i;                          i++;
                         *p_offset = offset + str_len;  
                         done = 1;  
464                  }                  }
465            }
466                  offset = 0;          if (i > offset) // offset was skipped
467            {
468                    offset--;
469          }          }
470    
471          *p_last_updated_line = MAX(i, *p_last_updated_line);          // Get length of current data line
472            len_data_line = 0;
473            p_data_line = p_editor_data->p_display_lines[display_line];
474            for (i = display_line - 1; i >= 0; i--)
475            {
476                    if (p_editor_data->display_line_lengths[i] > 0 &&
477                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
478                    {
479                            break;
480                    }
481    
482          if (len_insert > 0)                  len_data_line += p_editor_data->display_line_lengths[i];
483                    p_data_line = p_editor_data->p_display_lines[i];
484            }
485            offset_data_line = len_data_line + offset;
486            last_display_line = p_editor_data->display_line_total - 1;
487            for (i = display_line; i < p_editor_data->display_line_total; i++)
488          {          {
489                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  len_data_line += p_editor_data->display_line_lengths[i];
490    
491                    if (p_editor_data->display_line_lengths[i] > 0 &&
492                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
493                  {                  {
494                          log_error("Append line error, display_line_total(%ld) reach limit(%d)\n",                          last_display_line = i;
495                                            p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                          break;
                         return -2;  
496                  }                  }
497            }
498    
499            if (offset_data_line >= len_data_line) // end-of-line
500            {
501                    return 0;
502            }
503    
504                  // Prepare one blank display line after last_display_line          // Check str to be deleted
505                  for (i = p_editor_data->display_line_total; i > last_display_line + 1; i--)          if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
506            {
507                    str_len = 1;
508            }
509            else if (p_data_line[offset_data_line + 1] < 0) // GBK
510            {
511                    str_len = 2;
512            }
513            else
514            {
515                    log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
516                                      display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
517                    str_len = 1;
518            }
519    
520            // Current display line is (almost) empty
521            if (offset_data_line + str_len > len_data_line ||
522                    (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
523            {
524                    if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
525                  {                  {
526                          p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1];                          return 0;
                         p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1];  
527                  }                  }
                 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;  
528    
529                  (p_editor_data->display_line_total)++;                  len_data_line = 0; // Next data line
530                  last_display_line++;                  last_display_line = p_editor_data->display_line_total - 1;
531                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
532                    {
533                            len_data_line += p_editor_data->display_line_lengths[i];
534    
535                  // Fill data into blank display line                          if (p_editor_data->display_line_lengths[i] > 0 &&
536                  memcpy(p_editor_data->p_display_lines[last_display_line], buf_insert, (size_t)len_insert);                                  p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
537                  p_editor_data->p_display_lines[last_display_line][len_insert] = '\0';                          {
538                  p_editor_data->display_line_lengths[last_display_line] = len_insert;                                  last_display_line = i;
539                                    break;
540                            }
541                    }
542    
543                  if (!done)                  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
544                  {                  {
545                          *p_display_line = last_display_line;                          return 0;
                         *p_offset = str_len;  
                         done = 1;  
546                  }                  }
547    
548                  *p_last_updated_line = MAX(last_display_line, *p_last_updated_line);                  // Append next data line to current one
549                    memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
550                    p_data_line[offset_data_line + len_data_line] = '\0';
551    
552                    // Recycle next data line
553                    memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]);
554            }
555            else
556            {
557                    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));
558                    p_data_line[len_data_line - str_len] = '\0';
559                    len_data_line -= str_len;
560          }          }
561    
562          if (done && *p_offset >= SCREEN_COLS)          // Set p_data_line to head of current display line
563            p_data_line = p_editor_data->p_display_lines[display_line];
564            split_line_total = last_display_line - display_line + 2;
565    
566            // Split current data line since beginning of current display line
567            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0);
568    
569            for (i = 0; i < split_line_total; i++)
570          {          {
571                  (*p_display_line)++;                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
572                  *p_offset = 0;                  p_editor_data->p_display_lines[display_line + i] =
573                            (i == 0
574                                     ? p_data_line
575                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
576    
577                  if (*p_display_line >= p_editor_data->display_line_total)                  if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
578                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
579                  {                  {
580                          log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);                          break;
581                  }                  }
582          }          }
583    
584          return done;          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
 }  
585    
586  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,          if (*p_last_updated_line < last_display_line)
587                                             long *p_last_updated_line)          {
588  {                  // Remove redundant display line after last_display_line
589          return 0;                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
590                    {
591                            p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j];
592                            p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];
593                    }
594    
595                    j = p_editor_data->display_line_total;
596                    (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
597                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
598            }
599    
600            // Return real offset
601            *p_offset = offset;
602    
603            return str_len;
604  }  }
605    
606  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 426  static int editor_display_key_handler(in Line 609  static int editor_display_key_handler(in
609          {          {
610          case 0: // Set msg          case 0: // Set msg
611                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
612                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");
613                    break;
614            case KEY_CSI:
615                    *p_key = KEY_ESC;
616                  break;                  break;
617          }          }
618    
# Line 439  int editor_display(EDITOR_DATA *p_editor Line 625  int editor_display(EDITOR_DATA *p_editor
625          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
626          EDITOR_CTX ctx;          EDITOR_CTX ctx;
627          int ch = 0;          int ch = 0;
628          char insert_str[4];          char input_str[4];
629          int str_len = 0;          int str_len = 0;
630          int input_ok;          int input_ok;
         int screen_current_row;  
631          const int screen_begin_row = 1;          const int screen_begin_row = 1;
632          int screen_end_row = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
633          const int screen_row_total = screen_end_row - screen_begin_row + 1;          int output_current_row = screen_begin_row;
634            int output_end_row = SCREEN_ROWS - 1;
635          long int line_current = 0;          long int line_current = 0;
636          long int len;          long int len;
637          int loop;          int loop;
# Line 455  int editor_display(EDITOR_DATA *p_editor Line 641  int editor_display(EDITOR_DATA *p_editor
641          long display_line_out, offset_out;          long display_line_out, offset_out;
642          int scroll_rows;          int scroll_rows;
643          long last_updated_line = 0;          long last_updated_line = 0;
644          int insert = 1;          int key_insert = 1;
645          int i;          int i, j;
646            char *p_str;
647    
648          screen_current_row = screen_begin_row;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_row, SCREEN_ROWS);  
649    
650          // update msg_ext with extended key handler          // update msg_ext with extended key handler
651          if (editor_display_key_handler(&ch, &ctx) != 0)          if (editor_display_key_handler(&ch, &ctx) != 0)
# Line 470  int editor_display(EDITOR_DATA *p_editor Line 656  int editor_display(EDITOR_DATA *p_editor
656          loop = 1;          loop = 1;
657          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
658          {          {
659                  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)
660                  {                  {
661                          ctx.line_cursor = line_current - screen_current_row + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
662    
663                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
664                                           "\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] "
665                                           "µÚ\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] "
666                                           "%s",                                           "%s",
667                                           row_pos, col_pos,                                           row_pos, col_pos,
668                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
669                                             key_insert ? "²åÈë" : "Ìæ»»",
670                                           ctx.msg);                                           ctx.msg);
671    
672                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
673                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
674                          {                          {
675                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 510  int editor_display(EDITOR_DATA *p_editor Line 697  int editor_display(EDITOR_DATA *p_editor
697    
698                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch > 127 && ch <= 255) // GBK
699                                  {                                  {
700                                          insert_str[str_len] = (char)(ch - 256);                                          input_str[str_len] = (char)(ch - 256);
701                                          str_len++;                                          str_len++;
702                                  }                                  }
703                                  else                                  else if (str_len > 0)
704                                  {                                  {
705                                            log_error("Received %d character over 127 followed by character less than 127\n", str_len);
706                                          str_len = 0;                                          str_len = 0;
707                                  }                                  }
708    
709                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2)) // printable character or GBK                                  if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK
710                                            ch == CR || ch == KEY_ESC)                                                                                       // Special character
711                                  {                                  {
712                                          if (str_len == 0)                                          if (str_len == 0) // ch >= 32 && ch < 127
713                                          {                                          {
714                                                  insert_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
715                                                  str_len = 1;                                                  str_len = 1;
716                                          }                                          }
717    
718                                          last_updated_line = line_current;                                          last_updated_line = line_current;
719                                          display_line_in = line_current - screen_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
720                                          offset_in = col_pos - 1;                                          offset_in = col_pos - 1;
721                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
722                                          offset_out = offset_in;                                          offset_out = offset_in;
723    
724                                            if (!key_insert) // overwrite
725                                            {
726                                                    if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
727                                                                                               &last_updated_line) < 0)
728                                                    {
729                                                            log_error("editor_data_delete() error\n");
730                                                    }
731                                            }
732    
733                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
734                                                                                     insert_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
735                                          {                                          {
736                                                  log_error("editor_data_insert(%s) error\n", insert_str);                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);
                                                 str_len = 0;  
737                                          }                                          }
738                                          else                                          else
739                                          {                                          {
740                                                  str_len = 0;                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
741                                                    line_current -= (output_current_row - row_pos);
742                                                    output_current_row = (int)row_pos;
743    
744                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row));
                                                 line_current -= (screen_current_row - row_pos);  
                                                 screen_current_row = (int)row_pos;  
   
                                                 scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row));  
745    
746                                                  if (scroll_rows > 0)                                                  if (scroll_rows > 0)
747                                                  {                                                  {
# Line 557  int editor_display(EDITOR_DATA *p_editor Line 752  int editor_display(EDITOR_DATA *p_editor
752                                                                  prints("\033[S"); // Scroll up 1 line                                                                  prints("\033[S"); // Scroll up 1 line
753                                                          }                                                          }
754    
755                                                          screen_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
756                                                          if (screen_current_row < screen_begin_row)                                                          if (output_current_row < screen_begin_row)
757                                                          {                                                          {
758                                                                  line_current += (screen_begin_row - screen_current_row);                                                                  line_current += (screen_begin_row - output_current_row);
759                                                                  screen_current_row = screen_begin_row;                                                                  output_current_row = screen_begin_row;
760                                                          }                                                          }
761                                                          row_pos = screen_end_row;                                                          row_pos = output_end_row;
762                                                  }                                                  }
763                                                  else // if (scroll_lines == 0)                                                  else // if (scroll_lines == 0)
764                                                  {                                                  {
765                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
766                                                  }                                                  }
767                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1; // Set col_pos to accurate pos
   
                                                 continue;  
768                                          }                                          }
769    
770                                            str_len = 0;
771                                            continue;
772                                  }                                  }
773                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
774                                  {                                  {
775                                          last_updated_line = line_current;                                          if (ch == BACKSPACE)
776                                            {
777                                                    if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
778                                                    {
779                                                            input_ok = 0;
780                                                            continue;
781                                                    }
782    
783                                                    col_pos--;
784                                                    if (col_pos > 1 &&
785                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK
786                                                    {
787                                                            col_pos--;
788                                                    }
789    
790                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
791                                                    {
792                                                            row_pos--;
793                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
794                                                    }
795                                            }
796    
797                                          if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,                                          display_line_in = line_current - output_current_row + row_pos;
798                                                                                     &last_updated_line) < 0)                                          offset_in = col_pos - 1;
799                                            display_line_out = display_line_in;
800                                            offset_out = offset_in;
801    
802                                            if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
803                                                                                                              &last_updated_line)) < 0)
804                                          {                                          {
805                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
806                                          }                                          }
807                                          else                                          else
808                                          {                                          {
809                                                  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
810    
811                                                    output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
812                                                    line_current -= (output_current_row - row_pos);
813                                                    output_current_row = (int)row_pos;
814    
815                                                    if (output_current_row < screen_begin_row) // row_pos <= 0
816                                                    {
817                                                            output_current_row = screen_begin_row;
818                                                            row_pos = screen_begin_row;
819                                                            output_end_row = SCREEN_ROWS - 1;
820                                                    }
821    
822                                                    // Exceed end
823                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
824                                                            p_editor_data->display_line_total > screen_row_total)
825                                                    {
826                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
827                                                                                                    (p_editor_data->display_line_total - screen_row_total));
828    
829                                                            line_current = p_editor_data->display_line_total - screen_row_total;
830                                                            row_pos += scroll_rows;
831                                                            output_current_row = screen_begin_row;
832                                                            output_end_row = SCREEN_ROWS - 1;
833                                                    }
834    
835                                                    clrline(output_current_row, output_end_row);
836                                          }                                          }
837    
838                                            str_len = 0;
839                                          continue;                                          continue;
840                                  }                                  }
841    
# Line 596  int editor_display(EDITOR_DATA *p_editor Line 844  int editor_display(EDITOR_DATA *p_editor
844                                  case KEY_NULL:                                  case KEY_NULL:
845                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
846                                          goto cleanup;                                          goto cleanup;
847                                  case Ctrl('C'):                                  case Ctrl('W'):
848                                          loop = 0;                                          loop = 0;
849                                          break;                                          break;
850                                    case Ctrl('S'): // Start of line
851                                  case KEY_CTRL_LEFT:                                  case KEY_CTRL_LEFT:
852                                          col_pos = 1;                                          col_pos = 1;
853                                          break;                                          break;
854                                    case Ctrl('E'): // End of line
855                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
856                                          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
857                                            {
858                                                    // last display line does NOT have \n in the end
859                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
860                                                    break;
861                                            }
862                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
863                                          break;                                          break;
864                                    case Ctrl('T'): // Top of screen
865                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
866                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
867                                          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]));
868                                          break;                                          break;
869                                    case Ctrl('B'): // Bottom of screen
870                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
871                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
872                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          {
873                                                    row_pos = p_editor_data->display_line_total;
874                                            }
875                                            else
876                                            {
877                                                    row_pos = SCREEN_ROWS - 1;
878                                            }
879                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
880                                            {
881                                                    // last display line does NOT have \n in the end
882                                                    col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);
883                                            }
884                                            else
885                                            {
886                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
887                                            }
888                                          break;                                          break;
889                                  case KEY_INS:                                  case KEY_INS:
890                                          insert = !insert;                                          key_insert = !key_insert;
891                                          break;                                          break;
892                                  case KEY_HOME:                                  case KEY_HOME:
893                                          row_pos = 1;                                          row_pos = 1;
894                                          col_pos = 1;                                          col_pos = 1;
895                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
896                                          {                                          {
897                                                  break;                                                  break;
898                                          }                                          }
899                                          line_current = 0;                                          line_current = 0;
900                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
901                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
902                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
903                                          break;                                          break;
904                                  case KEY_END:                                  case KEY_END:
905                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
906                                          {                                          {
907                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
908                                                  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;
909                                                  break;                                                  break;
910                                          }                                          }
911                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
912                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
913                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
914                                          row_pos = screen_row_total;                                          row_pos = SCREEN_ROWS - 1;
915                                          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;
916                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
917                                          break;                                          break;
918                                  case KEY_LEFT:                                  case KEY_LEFT:
919                                          if (col_pos > 1)                                          if (col_pos > 1)
920                                          {                                          {
921                                                  col_pos--;                                                  col_pos--;
922                                                    if (col_pos > 1 &&
923                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
924                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK
925                                                    {
926                                                            col_pos--;
927                                                    }
928                                                  break;                                                  break;
929                                          }                                          }
930                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
# Line 653  int editor_display(EDITOR_DATA *p_editor Line 932  int editor_display(EDITOR_DATA *p_editor
932                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
933                                          {                                          {
934                                                  row_pos--;                                                  row_pos--;
935                                                  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]));
936                                                  break;                                                  break;
937                                          }                                          }
938                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
939                                          {                                          {
940                                                  col_pos = 1;                                                  col_pos = 1;
941                                                  break;                                                  break;
942                                          }                                          }
943                                          line_current -= screen_current_row;                                          line_current -= output_current_row;
944                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
945                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
946                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
947                                          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
948                                          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]));
                                         break;  
                                 case CR:  
949                                          break;                                          break;
950                                  case KEY_SPACE:                                  case KEY_SPACE:
951                                          break;                                          break;
952                                  case KEY_RIGHT:                                  case KEY_RIGHT:
953                                          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])
954                                          {                                          {
955                                                    if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 &&
956                                                            p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK
957                                                    {
958                                                            col_pos++;
959                                                    }
960                                                  col_pos++;                                                  col_pos++;
961                                                  break;                                                  break;
962                                          }                                          }
# Line 683  int editor_display(EDITOR_DATA *p_editor Line 965  int editor_display(EDITOR_DATA *p_editor
965                                          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))
966                                          {                                          {
967                                                  row_pos++;                                                  row_pos++;
968                                                  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]));
969                                                  break;                                                  break;
970                                          }                                          }
971                                          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
972                                          {                                          {
973                                                  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
974                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
975                                                  break;                                                  break;
976                                          }                                          }
977                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
978                                          screen_current_row = screen_row_total;                                          output_current_row = screen_row_total;
979                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
980                                          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]));
981                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
982                                          clrtoeol();                                          clrtoeol();
983                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
984                                          break;                                          break;
985                                  case KEY_PGUP:                                  case KEY_PGUP:
986                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
987                                          {                                          {
988                                                  break;                                                  break;
989                                          }                                          }
990                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
991                                          if (line_current < 0)                                          if (line_current < 0)
992                                          {                                          {
993                                                  line_current = 0;                                                  line_current = 0;
994                                          }                                          }
995                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
996                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
997                                          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]));
998                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
999                                          break;                                          break;
1000                                  case KEY_PGDN:                                  case KEY_PGDN:
1001                                          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
1002                                          {                                          {
1003                                                  break;                                                  break;
1004                                          }                                          }
1005                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
1006                                          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
1007                                          {                                          {
1008                                                  line_current = p_editor_data->display_line_total - screen_row_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
1009                                          }                                          }
1010                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1011                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1012                                          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]));
1013                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
1014                                          break;                                          break;
1015                                  case KEY_F1:                                  case KEY_F1:
1016                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not reentrant
# Line 742  int editor_display(EDITOR_DATA *p_editor Line 1023  int editor_display(EDITOR_DATA *p_editor
1023                                          show_help = 1;                                          show_help = 1;
1024                                  case KEY_F5:                                  case KEY_F5:
1025                                          // Refresh after display help information                                          // Refresh after display help information
1026                                          line_current -= (screen_current_row - screen_begin_row);                                          line_current -= (output_current_row - screen_begin_row);
1027                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
1028                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
1029                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
1030                                          break;                                          break;
1031                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
1032                                          break;                                          break;
# Line 755  int editor_display(EDITOR_DATA *p_editor Line 1036  int editor_display(EDITOR_DATA *p_editor
1036                                  }                                  }
1037    
1038                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(0);
1039    
1040                                    if (input_ok)
1041                                    {
1042                                            break;
1043                                    }
1044                          }                          }
1045    
1046                          continue;                          continue;
# Line 772  int editor_display(EDITOR_DATA *p_editor Line 1058  int editor_display(EDITOR_DATA *p_editor
1058                          len = 0;                          len = 0;
1059                  }                  }
1060    
1061                  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);
1062                  buffer[len] = '\0';                  // Replace '\033' with '*'
1063                    p_str = p_editor_data->p_display_lines[line_current];
1064                    for (i = 0, j = 0; i < len; i++)
1065                    {
1066                            if (p_str[i] == '\033')
1067                            {
1068                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1069                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1070                            }
1071                            else
1072                            {
1073                                    buffer[j] = p_str[i];
1074                                    j++;
1075                            }
1076                    }
1077                    buffer[j] = '\0';
1078    
1079                  moveto(screen_current_row, 0);                  moveto(output_current_row, 0);
1080                  clrtoeol();                  clrtoeol();
1081                  prints("%s", buffer);                  prints("%s", buffer);
1082                  line_current++;                  line_current++;
1083                  screen_current_row++;                  output_current_row++;
1084          }          }
1085    
1086  cleanup:  cleanup:


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

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