/[LeafOK_CVS]/lbbs/src/editor.c
ViewVC logotype

Diff of /lbbs/src/editor.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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


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

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