/[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.20 by sysadm, Fri Jun 13 15:50:21 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            char *p_data_line = NULL;
82          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES];
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    
         p_editor_data->data_line_total = 0;  
99          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);          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++)
# 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                    if (p_data_line == NULL)
188                    {
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            if (p_data_line != NULL)
201          {          {
202                  free(p_editor_data->p_data_lines[i]);                  memory_pool_free(p_mp_data_line, p_data_line);
                 p_editor_data->p_data_lines[i] = NULL;  
203          }          }
204    
205          free(p_editor_data);          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    
         memcpy(buf_insert, str, (size_t)str_len);  
         buf_insert[str_len] = '\0';  
         len_insert = str_len;  
   
230          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
231          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
232          {          {
233                  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
234                  {                  {
235                          i++;                          i++;
236                  }                  }
# Line 213  int editor_data_insert(EDITOR_DATA *p_ed Line 269  int editor_data_insert(EDITOR_DATA *p_ed
269          }          }
270    
271          // Split current data line if over-length          // Split current data line if over-length
272          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)
273          {          {
274                  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)
275                  {                  {
276                          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",
277                                            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);
278                          return -2;                          return -2;
279                  }                  }
280    
281                  // Allocate new data line                  // Allocate new data line
282                  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);
283                  if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)                  if (p_data_line == NULL)
284                  {                  {
285                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");                          log_error("memory_pool_alloc() error\n");
286                          return -2;                          return -2;
287                  }                  }
288    
289                  if (last_display_line > display_line)                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
290                  {                  {
291                          // 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++)  
292                          {                          {
293                                  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);  
294                          }                          }
                 }  
                 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;  
295    
296                          (p_editor_data->display_line_total)++;                          // Copy str to new data line
297                          last_display_line++;                          memcpy(p_data_line, str, (size_t)str_len);
                 }  
298    
299                  *p_last_updated_line = p_editor_data->display_line_total;                          // Copy rest part of current data line to new data line
300                  (p_editor_data->data_line_total)++;                          memcpy(p_data_line + str_len,
301                                       p_editor_data->p_display_lines[display_line] + offset,
302                                       (size_t)(len_data_line - offset_data_line));
303    
304                            p_data_line[str_len + len_data_line - offset_data_line] = '\0';
305    
306                            // Add line ending to current display line (data line)
307                            p_editor_data->p_display_lines[display_line][offset] = '\n';
308                            p_editor_data->p_display_lines[display_line][offset + 1] = '\0';
309                            p_editor_data->display_line_lengths[display_line] = offset + 1;
310    
311                  if (offset_data_line + str_len + 2 < MAX_EDITOR_DATA_LINE_LENGTH)                          *p_display_line = display_line + 1;
312                            *p_offset = str_len;
313                    }
314                    else
315                  {                  {
316                          // Copy rest part of current display line to insert buffer                          // Copy rest part of current data line to new data line
317                          memcpy(buf_insert,                          memcpy(p_data_line,
318                                     p_editor_data->p_display_lines[display_line] + offset,                                     p_editor_data->p_display_lines[display_line] + offset,
319                                     (size_t)(p_editor_data->display_line_lengths[display_line] - offset));                                     (size_t)(len_data_line - offset_data_line));
320                          len_insert = (p_editor_data->display_line_lengths[display_line] - offset);  
321                          buf_insert[len_insert] = '\0';                          p_data_line[len_data_line - offset_data_line] = '\0';
322    
323                          // Append str to current display line                          // Append str to current display line
324                          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 328  int editor_data_insert(EDITOR_DATA *p_ed
328                          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';
329                          p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;                          p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;
330    
331                          if (!done)                          *p_display_line = display_line;
332                          {                          *p_offset = offset + str_len;
                                 *p_display_line = display_line;  
                                 *p_offset = offset + str_len;  
                                 done = 1;  
                         }  
333                  }                  }
                 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';  
334    
335                          // 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;  
                 }  
336    
337                    // Set start display_line for spliting new data line
338                  display_line++;                  display_line++;
339                  offset = 0;  
340                    *p_last_updated_line = p_editor_data->display_line_total;
341            }
342            else // insert str into current data line at offset_data_line
343            {
344                    memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line));
345                    memcpy(p_data_line + offset_data_line, str, (size_t)str_len);
346                    p_data_line[len_data_line + str_len] = '\0';
347    
348                    // Set p_data_line to head of current display line
349                    p_data_line = p_editor_data->p_display_lines[display_line];
350                    split_line_total = last_display_line - display_line + 3;
351    
352                    *p_display_line = display_line;
353                    *p_offset = offset + str_len;
354          }          }
355    
356          for (i = display_line; len_insert > 0 && i <= last_display_line; i++)          // Split current data line since beginning of current display line
357            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
358    
359            for (i = 0; i < split_line_total; i++)
360          {          {
361                  len = split_line(buf_insert, SCREEN_COLS, &eol, &display_len_insert);                  if (display_line + i > last_display_line)
                 if (len != len_insert)  
362                  {                  {
363                          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
364                                            i, len, len_insert, buf_insert);                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
365                          return -3;                          {
366                                    // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
367                                    // Terminate prior display line with \n, to avoid error on cleanup
368                                    if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
369                                    {
370                                            len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len);
371                                            p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
372                                            p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
373                                            p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
374                                    }
375                                    if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
376                                    {
377                                            *p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1;
378                                    }
379                                    break;
380                            }
381                            for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
382                            {
383                                    p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
384                                    p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
385                            }
386                            last_display_line++;
387                            (p_editor_data->display_line_total)++;
388                  }                  }
389    
390                  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];
391                  buf_catenate[p_editor_data->display_line_lengths[i]] = '\0';                  p_editor_data->p_display_lines[display_line + i] =
392                            (i == 0
393                                     ? p_data_line
394                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
395    
396                  len = split_line(buf_catenate, SCREEN_COLS - display_len_insert, &eol, &display_len);                  if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
397                  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')
398                  {                  {
399                          offset -= len;                          break;
                         continue;  
400                  }                  }
401            }
402    
403            *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
404    
405                  // move \n to next display line if current line is full          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
406                  if (len > 0 && buf_catenate[len - 1] == '\n' && display_len + display_len_insert >= SCREEN_COLS)          {
407                    if (*p_display_line + 1 < p_editor_data->display_line_total)
408                    {
409                            *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
410                            (*p_display_line)++;
411                    }
412                    else if (*p_display_line + 1 >= MAX_EDITOR_DATA_LINES)
413                  {                  {
414                          len--;                          len = split_line(p_editor_data->p_display_lines[*p_display_line], SCREEN_COLS - 1, &eol, &display_len);
415                            p_editor_data->p_display_lines[*p_display_line][len] = '\0';
416                            p_editor_data->display_line_lengths[*p_display_line] = len;
417                            *p_offset = len;
418                  }                  }
419            }
420    
421            return 0;
422    }
423    
424    int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,
425                                               long *p_last_updated_line)
426    {
427            char *p_data_line = NULL;
428            long len_data_line;
429            long offset_data_line;
430            long last_display_line; // of data line
431            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
432            long split_line_total;
433            long i, j;
434            int str_len = 0;
435    
436            if (p_editor_data == NULL || p_last_updated_line == NULL)
437            {
438                    log_error("editor_data_op() error: NULL pointer\n");
439                    return -1;
440            }
441    
442                  memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)offset);          // Get accurate offset of first character of CJK at offset position
443                  memcpy(buf_catenate + offset, buf_insert, (size_t)len_insert);          for (i = 0; i < offset; i++)
444                  memcpy(buf_catenate + offset + len_insert, p_editor_data->p_display_lines[i] + offset, (size_t)(len - offset));          {
445                  len_catenate = len_insert + len;                  if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
446                  buf_catenate[len_catenate] = '\0';                  {
447                            i++;
448                    }
449            }
450            if (i > offset) // offset was skipped
451            {
452                    offset--;
453            }
454    
455                  len_insert = p_editor_data->display_line_lengths[i] - len;          // Get length of current data line
456                  if (len_insert > 0)          len_data_line = 0;
457            p_data_line = p_editor_data->p_display_lines[display_line];
458            for (i = display_line - 1; i >= 0; i--)
459            {
460                    if (p_editor_data->display_line_lengths[i] > 0 &&
461                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
462                  {                  {
463                          memcpy(buf_insert, p_editor_data->p_display_lines[i] + len, (size_t)len_insert);                          break;
                         buf_insert[len_insert] = '\0';  
464                  }                  }
465    
466                  memcpy(p_editor_data->p_display_lines[i], buf_catenate, (size_t)len_catenate);                  len_data_line += p_editor_data->display_line_lengths[i];
467                  p_editor_data->display_line_lengths[i] = len_catenate;                  p_data_line = p_editor_data->p_display_lines[i];
468            }
469            offset_data_line = len_data_line + offset;
470            last_display_line = p_editor_data->display_line_total - 1;
471            for (i = display_line; i < p_editor_data->display_line_total; i++)
472            {
473                    len_data_line += p_editor_data->display_line_lengths[i];
474    
475                  if (!done)                  if (p_editor_data->display_line_lengths[i] > 0 &&
476                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
477                  {                  {
478                          *p_display_line = i;                          last_display_line = i;
479                          *p_offset = offset + str_len;                          break;
                         done = 1;  
480                  }                  }
481            }
482    
483                  offset = 0;          if (offset_data_line >= len_data_line) // end-of-line
484            {
485                    return 0;
486          }          }
487    
488          *p_last_updated_line = MAX(i, *p_last_updated_line);          // Check str to be deleted
489            if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
490            {
491                    str_len = 1;
492            }
493            else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK
494            {
495                    str_len = 2;
496            }
497            else
498            {
499                    log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
500                                      display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
501                    str_len = 1;
502            }
503    
504          if (len_insert > 0)          // Current display line is (almost) empty
505            if (offset_data_line + str_len > len_data_line ||
506                    (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
507          {          {
508                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
509                  {                  {
510                          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;  
511                  }                  }
512    
513                  // Prepare one blank display line after last_display_line                  len_data_line = 0; // Next data line
514                  for (i = p_editor_data->display_line_total; i > last_display_line + 1; i--)                  last_display_line = p_editor_data->display_line_total - 1;
515                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
516                  {                  {
517                          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];
                         p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1];  
                 }  
                 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;  
518    
519                  (p_editor_data->display_line_total)++;                          if (p_editor_data->display_line_lengths[i] > 0 &&
520                  last_display_line++;                                  p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
521                            {
522                  // Fill data into blank display line                                  last_display_line = i;
523                  memcpy(p_editor_data->p_display_lines[last_display_line], buf_insert, (size_t)len_insert);                                  break;
524                  p_editor_data->p_display_lines[last_display_line][len_insert] = '\0';                          }
525                  p_editor_data->display_line_lengths[last_display_line] = len_insert;                  }
526    
527                  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
528                  {                  {
529                          *p_display_line = last_display_line;                          return 0;
                         *p_offset = str_len;  
                         done = 1;  
530                  }                  }
531    
532                  *p_last_updated_line = MAX(last_display_line, *p_last_updated_line);                  // Append next data line to current one
533                    memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
534                    p_data_line[offset_data_line + len_data_line] = '\0';
535    
536                    // Recycle next data line
537                    memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]);
538          }          }
539            else
540            {
541                    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));
542                    p_data_line[len_data_line - str_len] = '\0';
543                    len_data_line -= str_len;
544            }
545    
546            // Set p_data_line to head of current display line
547            p_data_line = p_editor_data->p_display_lines[display_line];
548            split_line_total = last_display_line - display_line + 2;
549    
550          if (done && *p_offset >= SCREEN_COLS)          // Split current data line since beginning of current display line
551            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
552    
553            for (i = 0; i < split_line_total; i++)
554          {          {
555                  (*p_display_line)++;                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
556                  *p_offset = 0;                  p_editor_data->p_display_lines[display_line + i] =
557                            (i == 0
558                                     ? p_data_line
559                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
560    
561                  if (*p_display_line >= p_editor_data->display_line_total)                  if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
562                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
563                  {                  {
564                          log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);                          break;
565                  }                  }
566          }          }
567    
568          return done;          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
 }  
569    
570  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,          if (*p_last_updated_line < last_display_line)
571                                             long *p_last_updated_line)          {
572  {                  // Remove redundant display line after last_display_line
573          return 0;                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
574                    {
575                            p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j];
576                            p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];
577                    }
578    
579                    j = p_editor_data->display_line_total;
580                    (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
581                    *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
582            }
583    
584            return str_len;
585  }  }
586    
587  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 590  static int editor_display_key_handler(in
590          {          {
591          case 0: // Set msg          case 0: // Set msg
592                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
593                                   "| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |");                                   "| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |");
594                  break;                  break;
595          }          }
596    
# Line 439  int editor_display(EDITOR_DATA *p_editor Line 603  int editor_display(EDITOR_DATA *p_editor
603          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
604          EDITOR_CTX ctx;          EDITOR_CTX ctx;
605          int ch = 0;          int ch = 0;
606          char insert_str[4];          char input_str[4];
607          int str_len = 0;          int str_len = 0;
608          int input_ok;          int input_ok;
         int screen_current_row;  
609          const int screen_begin_row = 1;          const int screen_begin_row = 1;
610          int screen_end_row = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
611          const int screen_row_total = screen_end_row - screen_begin_row + 1;          int output_current_row = screen_begin_row;
612            int output_end_row = SCREEN_ROWS - 1;
613          long int line_current = 0;          long int line_current = 0;
614          long int len;          long int len;
615          int loop;          int loop;
# Line 455  int editor_display(EDITOR_DATA *p_editor Line 619  int editor_display(EDITOR_DATA *p_editor
619          long display_line_out, offset_out;          long display_line_out, offset_out;
620          int scroll_rows;          int scroll_rows;
621          long last_updated_line = 0;          long last_updated_line = 0;
622          int insert = 1;          int key_insert = 1;
623          int i;          int i, j;
624            char *p_str;
625    
626          screen_current_row = screen_begin_row;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_row, SCREEN_ROWS);  
627    
628          // update msg_ext with extended key handler          // update msg_ext with extended key handler
629          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 634  int editor_display(EDITOR_DATA *p_editor
634          loop = 1;          loop = 1;
635          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
636          {          {
637                  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)
638                  {                  {
639                          ctx.line_cursor = line_current - screen_current_row + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
640    
641                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
642                                           "\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] "
643                                           "µÚ\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] "
644                                           "%s",                                           "%s",
645                                           row_pos, col_pos,                                           row_pos, col_pos,
646                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
647                                             key_insert ? "²åÈë" : "¸Äд",
648                                           ctx.msg);                                           ctx.msg);
649    
650                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);
# Line 510  int editor_display(EDITOR_DATA *p_editor Line 675  int editor_display(EDITOR_DATA *p_editor
675    
676                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch > 127 && ch <= 255) // GBK
677                                  {                                  {
678                                          insert_str[str_len] = (char)(ch - 256);                                          input_str[str_len] = (char)(ch - 256);
679                                          str_len++;                                          str_len++;
680                                  }                                  }
681                                  else                                  else
# Line 518  int editor_display(EDITOR_DATA *p_editor Line 683  int editor_display(EDITOR_DATA *p_editor
683                                          str_len = 0;                                          str_len = 0;
684                                  }                                  }
685    
686                                  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
687                                            ch == CR || ch == KEY_ESC)                                                                                       // Special character
688                                  {                                  {
689                                          if (str_len == 0)                                          if (str_len == 0)
690                                          {                                          {
691                                                  insert_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
692                                                  str_len = 1;                                                  str_len = 1;
693                                          }                                          }
694    
695                                          last_updated_line = line_current;                                          last_updated_line = line_current;
696                                          display_line_in = line_current - screen_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
697                                          offset_in = col_pos - 1;                                          offset_in = col_pos - 1;
698                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
699                                          offset_out = offset_in;                                          offset_out = offset_in;
700    
701                                            if (!key_insert) // overwrite
702                                            {
703                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
704                                                                                               &last_updated_line) < 0)
705                                                    {
706                                                            log_error("editor_data_delete() error\n");
707                                                    }
708                                            }
709    
710                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
711                                                                                     insert_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
712                                          {                                          {
713                                                  log_error("editor_data_insert(%s) error\n", insert_str);                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);
714                                                  str_len = 0;                                                  str_len = 0;
715                                          }                                          }
716                                          else                                          else
717                                          {                                          {
718                                                  str_len = 0;                                                  str_len = 0;
719    
720                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
721                                                  line_current -= (screen_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
722                                                  screen_current_row = (int)row_pos;                                                  output_current_row = (int)row_pos;
723    
724                                                  scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row));                                                  scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row));
725    
726                                                  if (scroll_rows > 0)                                                  if (scroll_rows > 0)
727                                                  {                                                  {
# Line 557  int editor_display(EDITOR_DATA *p_editor Line 732  int editor_display(EDITOR_DATA *p_editor
732                                                                  prints("\033[S"); // Scroll up 1 line                                                                  prints("\033[S"); // Scroll up 1 line
733                                                          }                                                          }
734    
735                                                          screen_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
736                                                          if (screen_current_row < screen_begin_row)                                                          if (output_current_row < screen_begin_row)
737                                                          {                                                          {
738                                                                  line_current += (screen_begin_row - screen_current_row);                                                                  line_current += (screen_begin_row - output_current_row);
739                                                                  screen_current_row = screen_begin_row;                                                                  output_current_row = screen_begin_row;
740                                                          }                                                          }
741                                                          row_pos = screen_end_row;                                                          row_pos = output_end_row;
742                                                  }                                                  }
743                                                  else // if (scroll_lines == 0)                                                  else // if (scroll_lines == 0)
744                                                  {                                                  {
745                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
746                                                  }                                                  }
747                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1;
   
                                                 continue;  
748                                          }                                          }
749    
750                                            continue;
751                                  }                                  }
752                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
753                                  {                                  {
754                                          last_updated_line = line_current;                                          if (ch == BACKSPACE)
755                                            {
756                                                    if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
757                                                    {
758                                                            input_ok = 0;
759                                                            continue;
760                                                    }
761    
762                                                    col_pos--;
763                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
764                                                    {
765                                                            row_pos--;
766                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
767                                                    }
768                                            }
769    
770                                          if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,                                          if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1,
771                                                                                     &last_updated_line) < 0)                                                                                                            &last_updated_line)) < 0)
772                                          {                                          {
773                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
774                                          }                                          }
775                                          else                                          else
776                                          {                                          {
777                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  if (ch == BACKSPACE)
778                                                    {
779                                                            for (i = 1; i < str_len; i++)
780                                                            {
781                                                                    col_pos--;
782                                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
783                                                                    {
784                                                                            row_pos--;
785                                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
786                                                                    }
787                                                            }
788                                                    }
789    
790                                                    output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
791                                                    line_current -= (output_current_row - row_pos);
792                                                    output_current_row = (int)row_pos;
793    
794                                                    if (output_current_row < screen_begin_row) // row_pos <= 0
795                                                    {
796                                                            output_current_row = screen_begin_row;
797                                                            row_pos = screen_begin_row;
798                                                            output_end_row = SCREEN_ROWS - 1;
799                                                    }
800    
801                                                    // Exceed end
802                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
803                                                            p_editor_data->display_line_total > screen_row_total)
804                                                    {
805                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
806                                                                                                    (p_editor_data->display_line_total - screen_row_total));
807    
808                                                            line_current = p_editor_data->display_line_total - screen_row_total;
809                                                            row_pos += scroll_rows;
810                                                            output_current_row = screen_begin_row;
811                                                            output_end_row = SCREEN_ROWS - 1;
812                                                    }
813    
814                                                    clrline(output_current_row, output_end_row);
815                                          }                                          }
816    
817                                          continue;                                          continue;
# Line 596  int editor_display(EDITOR_DATA *p_editor Line 822  int editor_display(EDITOR_DATA *p_editor
822                                  case KEY_NULL:                                  case KEY_NULL:
823                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
824                                          goto cleanup;                                          goto cleanup;
825                                  case Ctrl('C'):                                  case Ctrl('W'):
826                                          loop = 0;                                          loop = 0;
827                                          break;                                          break;
828                                    case Ctrl('S'): // Start of line
829                                  case KEY_CTRL_LEFT:                                  case KEY_CTRL_LEFT:
830                                          col_pos = 1;                                          col_pos = 1;
831                                          break;                                          break;
832                                    case Ctrl('E'): // End of line
833                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
834                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
835                                          break;                                          break;
836                                    case Ctrl('T'): // Top of screen
837                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
838                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
839                                          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]));
840                                          break;                                          break;
841                                    case Ctrl('B'): // Bottom of screen
842                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
843                                          row_pos = SCREEN_ROWS - 1;                                          if (p_editor_data->display_line_total < screen_row_total)
844                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          {
845                                                    row_pos = p_editor_data->display_line_total;
846                                            }
847                                            else
848                                            {
849                                                    row_pos = SCREEN_ROWS - 1;
850                                            }
851                                            if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
852                                            {
853                                                    // last display line does NOT have \n in the end
854                                                    col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1);
855                                            }
856                                            else
857                                            {
858                                                    col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
859                                            }
860                                          break;                                          break;
861                                  case KEY_INS:                                  case KEY_INS:
862                                          insert = !insert;                                          key_insert = !key_insert;
863                                          break;                                          break;
864                                  case KEY_HOME:                                  case KEY_HOME:
865                                          row_pos = 1;                                          row_pos = 1;
866                                          col_pos = 1;                                          col_pos = 1;
867                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
868                                          {                                          {
869                                                  break;                                                  break;
870                                          }                                          }
871                                          line_current = 0;                                          line_current = 0;
872                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
873                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
874                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
875                                          break;                                          break;
876                                  case KEY_END:                                  case KEY_END:
877                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
878                                          {                                          {
879                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
880                                                  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;
881                                                  break;                                                  break;
882                                          }                                          }
883                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
884                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
885                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
886                                          row_pos = screen_row_total;                                          row_pos = SCREEN_ROWS - 1;
887                                          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;
888                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
889                                          break;                                          break;
890                                  case KEY_LEFT:                                  case KEY_LEFT:
891                                          if (col_pos > 1)                                          if (col_pos > 1)
# Line 653  int editor_display(EDITOR_DATA *p_editor Line 898  int editor_display(EDITOR_DATA *p_editor
898                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
899                                          {                                          {
900                                                  row_pos--;                                                  row_pos--;
901                                                  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]));
902                                                  break;                                                  break;
903                                          }                                          }
904                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
905                                          {                                          {
906                                                  col_pos = 1;                                                  col_pos = 1;
907                                                  break;                                                  break;
908                                          }                                          }
909                                          line_current -= screen_current_row;                                          line_current -= output_current_row;
910                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
911                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
912                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
913                                          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
914                                          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:  
915                                          break;                                          break;
916                                  case KEY_SPACE:                                  case KEY_SPACE:
917                                          break;                                          break;
918                                  case KEY_RIGHT:                                  case KEY_RIGHT:
919                                          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])
920                                          {                                          {
921                                                  col_pos++;                                                  col_pos++;
922                                                  break;                                                  break;
# Line 683  int editor_display(EDITOR_DATA *p_editor Line 926  int editor_display(EDITOR_DATA *p_editor
926                                          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))
927                                          {                                          {
928                                                  row_pos++;                                                  row_pos++;
929                                                  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]));
930                                                  break;                                                  break;
931                                          }                                          }
932                                          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
933                                          {                                          {
934                                                  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
935                                                    col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1;
936                                                  break;                                                  break;
937                                          }                                          }
938                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
939                                          screen_current_row = screen_row_total;                                          output_current_row = screen_row_total;
940                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
941                                          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]));
942                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
943                                          clrtoeol();                                          clrtoeol();
944                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
945                                          break;                                          break;
946                                  case KEY_PGUP:                                  case KEY_PGUP:
947                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
948                                          {                                          {
949                                                  break;                                                  break;
950                                          }                                          }
951                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
952                                          if (line_current < 0)                                          if (line_current < 0)
953                                          {                                          {
954                                                  line_current = 0;                                                  line_current = 0;
955                                          }                                          }
956                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
957                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
958                                          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]));
959                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
960                                          break;                                          break;
961                                  case KEY_PGDN:                                  case KEY_PGDN:
962                                          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
963                                          {                                          {
964                                                  break;                                                  break;
965                                          }                                          }
966                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
967                                          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
968                                          {                                          {
969                                                  line_current = p_editor_data->display_line_total - screen_row_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
970                                          }                                          }
971                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
972                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
973                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]));                                          col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
974                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
975                                          break;                                          break;
976                                  case KEY_F1:                                  case KEY_F1:
977                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not reentrant
# Line 742  int editor_display(EDITOR_DATA *p_editor Line 984  int editor_display(EDITOR_DATA *p_editor
984                                          show_help = 1;                                          show_help = 1;
985                                  case KEY_F5:                                  case KEY_F5:
986                                          // Refresh after display help information                                          // Refresh after display help information
987                                          line_current -= (screen_current_row - screen_begin_row);                                          line_current -= (output_current_row - screen_begin_row);
988                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
989                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
990                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
991                                          break;                                          break;
992                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
993                                          break;                                          break;
# Line 755  int editor_display(EDITOR_DATA *p_editor Line 997  int editor_display(EDITOR_DATA *p_editor
997                                  }                                  }
998    
999                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(0);
1000    
1001                                    if (input_ok)
1002                                    {
1003                                            break;
1004                                    }
1005                          }                          }
1006    
1007                          continue;                          continue;
# Line 772  int editor_display(EDITOR_DATA *p_editor Line 1019  int editor_display(EDITOR_DATA *p_editor
1019                          len = 0;                          len = 0;
1020                  }                  }
1021    
1022                  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);
1023                  buffer[len] = '\0';                  // Replace '\033' with '*'
1024                    p_str = p_editor_data->p_display_lines[line_current];
1025                    for (i = 0, j = 0; i < len; i++)
1026                    {
1027                            if (p_str[i] == '\033')
1028                            {
1029                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1030                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1031                            }
1032                            else
1033                            {
1034                                    buffer[j] = p_str[i];
1035                                    j++;
1036                            }
1037                    }
1038                    buffer[j] = '\0';
1039    
1040                  moveto(screen_current_row, 0);                  moveto(output_current_row, 0);
1041                  clrtoeol();                  clrtoeol();
1042                  prints("%s", buffer);                  prints("%s", buffer);
1043                  line_current++;                  line_current++;
1044                  screen_current_row++;                  output_current_row++;
1045          }          }
1046    
1047  cleanup:  cleanup:


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

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