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


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

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