/[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.15 by sysadm, Thu Jun 12 04:45:57 2025 UTC
# Line 22  Line 22 
22  #include "str_process.h"  #include "str_process.h"
23  #include <stdlib.h>  #include <stdlib.h>
24  #include <sys/param.h>  #include <sys/param.h>
25    #include <strings.h>
26    
27  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
28  #include <string.h>  #include <string.h>
29    
30    #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
31    
32  EDITOR_DATA *editor_data_load(const char *p_data)  EDITOR_DATA *editor_data_load(const char *p_data)
33  {  {
34          EDITOR_DATA *p_editor_data;          EDITOR_DATA *p_editor_data;
35            char *p_data_line = NULL;
36          long line_offsets[MAX_EDITOR_DATA_LINES];          long line_offsets[MAX_EDITOR_DATA_LINES];
37          long current_data_line_length = 0;          long current_data_line_length = 0;
38          long i, j;          long i;
39    
40          if (p_data == NULL)          if (p_data == NULL)
41          {          {
# Line 46  EDITOR_DATA *editor_data_load(const char Line 50  EDITOR_DATA *editor_data_load(const char
50                  return NULL;                  return NULL;
51          }          }
52    
         p_editor_data->data_line_total = 0;  
53          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);          p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES);
54    
55          for (i = 0; i < p_editor_data->display_line_total; i++)          for (i = 0; i < p_editor_data->display_line_total; i++)
# Line 57  EDITOR_DATA *editor_data_load(const char Line 60  EDITOR_DATA *editor_data_load(const char
60                          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 ||
61                          (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'))
62                  {                  {
                         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;  
                         }  
   
63                          // Allocate new data line                          // Allocate new data line
64                          p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                          p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);
65                          if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)                          if (p_data_line == NULL)
66                          {                          {
67                                  log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);                                  log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i);
68                                  // Cleanup                                  // Cleanup
69                                  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);  
70                                  return NULL;                                  return NULL;
71                          }                          }
72    
73                          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)++;  
74                          current_data_line_length = 0;                          current_data_line_length = 0;
75                  }                  }
76                  else                  else
# Line 88  EDITOR_DATA *editor_data_load(const char Line 80  EDITOR_DATA *editor_data_load(const char
80    
81                  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]);
82                  current_data_line_length += p_editor_data->display_line_lengths[i];                  current_data_line_length += p_editor_data->display_line_lengths[i];
83                  p_editor_data->p_data_lines[p_editor_data->data_line_total - 1][current_data_line_length] = '\0';                  p_data_line[current_data_line_length] = '\0';
84          }          }
85    
86            bzero(p_editor_data->p_display_lines + p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);
87    
88          return p_editor_data;          return p_editor_data;
89  }  }
90    
# Line 125  long editor_data_save(const EDITOR_DATA Line 119  long editor_data_save(const EDITOR_DATA
119    
120  void editor_data_cleanup(EDITOR_DATA *p_editor_data)  void editor_data_cleanup(EDITOR_DATA *p_editor_data)
121  {  {
122            char *p_data_line = NULL;
123          long i;          long i;
124    
125          if (p_editor_data == NULL)          if (p_editor_data == NULL)
# Line 132  void editor_data_cleanup(EDITOR_DATA *p_ Line 127  void editor_data_cleanup(EDITOR_DATA *p_
127                  return;                  return;
128          }          }
129    
130          for (i = p_editor_data->data_line_total - 1; i >= 0; i--)          for (i = 0; i < p_editor_data->display_line_total; i++)
131          {          {
132                  free(p_editor_data->p_data_lines[i]);                  if (p_data_line == NULL)
133                  p_editor_data->p_data_lines[i] = NULL;                  {
134                            p_data_line = p_editor_data->p_display_lines[i];
135                    }
136    
137                    if (p_editor_data->display_line_lengths[i] > 0 &&
138                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
139                    {
140                            free(p_data_line);
141                            p_data_line = NULL;
142                    }
143            }
144    
145            if (p_data_line != NULL)
146            {
147                    free(p_data_line);
148          }          }
149    
150          free(p_editor_data);          free(p_editor_data);
# Line 146  int editor_data_insert(EDITOR_DATA *p_ed Line 155  int editor_data_insert(EDITOR_DATA *p_ed
155  {  {
156          long display_line = *p_display_line;          long display_line = *p_display_line;
157          long offset = *p_offset;          long offset = *p_offset;
158          int done = 0;          char *p_data_line = NULL;
         int len;  
         int display_len;  
         int eol;  
         char *p_data_line;  
159          long len_data_line;          long len_data_line;
160          long offset_data_line;          long offset_data_line;
161          long last_display_line; // of data line          long last_display_line; // of data line
162          char buf_insert[MAX_EDITOR_DATA_LINE_LENGTH];          long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
163          long len_insert;          long split_line_total;
164          int display_len_insert;          long i, j;
165          char buf_catenate[MAX_EDITOR_DATA_LINE_LENGTH];          int len;
166          long len_catenate;          int eol;
167          long i;          int display_len;
168    
169          if (p_editor_data == NULL || p_last_updated_line == NULL)          if (p_editor_data == NULL || p_last_updated_line == NULL)
170          {          {
# Line 167  int editor_data_insert(EDITOR_DATA *p_ed Line 172  int editor_data_insert(EDITOR_DATA *p_ed
172                  return -1;                  return -1;
173          }          }
174    
         memcpy(buf_insert, str, (size_t)str_len);  
         buf_insert[str_len] = '\0';  
         len_insert = str_len;  
   
175          // Get accurate offset of first character of CJK at offset position          // Get accurate offset of first character of CJK at offset position
176          for (i = 0; i < offset; i++)          for (i = 0; i < offset; i++)
177          {          {
178                  if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK                  if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
179                  {                  {
180                          i++;                          i++;
181                  }                  }
# Line 213  int editor_data_insert(EDITOR_DATA *p_ed Line 214  int editor_data_insert(EDITOR_DATA *p_ed
214          }          }
215    
216          // Split current data line if over-length          // Split current data line if over-length
217          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)
218          {          {
219                  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)
220                  {                  {
221                          log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n",                          // log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
222                                            p_editor_data->display_line_total, p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES);                          //                p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
223                          return -2;                          return -2;
224                  }                  }
225    
226                  // Allocate new data line                  // Allocate new data line
227                  p_editor_data->p_data_lines[p_editor_data->data_line_total] = malloc(MAX_EDITOR_DATA_LINE_LENGTH);                  p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);
228                  if (p_editor_data->p_data_lines[p_editor_data->data_line_total] == NULL)                  if (p_data_line == NULL)
229                  {                  {
230                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");                          log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");
231                          return -2;                          return -2;
232                  }                  }
233    
234                  if (last_display_line > display_line)                  if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
                 {  
                         // Copy rest part of current data line (since next display line) to new data line  
                         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++)  
                         {  
                                 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);  
                         }  
                 }  
                 else // last_display_line == display_line  
235                  {                  {
236                          // Insert blank display line pointing to new data line                          if (str[0] == CR)
                         for (i = p_editor_data->display_line_total; i > display_line + 1; i--)  
237                          {                          {
238                                  p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1];                                  str_len = 0;
                                 p_editor_data->display_line_lengths[i] = p_editor_data->display_line_lengths[i - 1];  
239                          }                          }
                         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;  
240    
241                          (p_editor_data->display_line_total)++;                          // Copy str to new data line
242                          last_display_line++;                          memcpy(p_data_line, str, (size_t)str_len);
                 }  
243    
244                  *p_last_updated_line = p_editor_data->display_line_total;                          // Copy rest part of current data line to new data line
245                  (p_editor_data->data_line_total)++;                          memcpy(p_data_line + str_len,
246                                       p_editor_data->p_display_lines[display_line] + offset,
247                                       (size_t)(len_data_line - offset_data_line));
248    
249                            p_data_line[str_len + len_data_line - offset_data_line] = '\0';
250    
251                  if (offset_data_line + str_len + 2 < MAX_EDITOR_DATA_LINE_LENGTH)                          // Add line ending to current display line (data line)
252                            p_editor_data->p_display_lines[display_line][offset] = '\n';
253                            p_editor_data->p_display_lines[display_line][offset + 1] = '\0';
254                            p_editor_data->display_line_lengths[display_line] = offset + 1;
255    
256                            *p_display_line = display_line + 1;
257                            *p_offset = str_len;
258                    }
259                    else
260                  {                  {
261                          // Copy rest part of current display line to insert buffer                          // Copy rest part of current data line to new data line
262                          memcpy(buf_insert,                          memcpy(p_data_line,
263                                     p_editor_data->p_display_lines[display_line] + offset,                                     p_editor_data->p_display_lines[display_line] + offset,
264                                     (size_t)(p_editor_data->display_line_lengths[display_line] - offset));                                     (size_t)(len_data_line - offset_data_line));
265                          len_insert = (p_editor_data->display_line_lengths[display_line] - offset);  
266                          buf_insert[len_insert] = '\0';                          p_data_line[len_data_line - offset_data_line] = '\0';
267    
268                          // Append str to current display line                          // Append str to current display line
269                          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 273  int editor_data_insert(EDITOR_DATA *p_ed
273                          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';
274                          p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;                          p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;
275    
276                          if (!done)                          *p_display_line = display_line;
277                          {                          *p_offset = offset + str_len;
                                 *p_display_line = display_line;  
                                 *p_offset = offset + str_len;  
                                 done = 1;  
                         }  
278                  }                  }
                 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';  
279    
280                          // 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;  
                 }  
281    
282                    // Set start display_line for spliting new data line
283                  display_line++;                  display_line++;
284                  offset = 0;  
285                    *p_last_updated_line = p_editor_data->display_line_total;
286          }          }
287            else // insert str into current data line at offset_data_line
288            {
289                    memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line));
290                    memcpy(p_data_line + offset_data_line, str, (size_t)str_len);
291                    p_data_line[len_data_line + str_len] = '\0';
292    
293                    // Set p_data_line to head of current display line
294                    p_data_line = p_editor_data->p_display_lines[display_line];
295                    split_line_total = last_display_line - display_line + 3;
296    
297                    *p_display_line = display_line;
298                    *p_offset = offset + str_len;
299            }
300    
301            // Split current data line since beginning of current display line
302            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
303    
304          for (i = display_line; len_insert > 0 && i <= last_display_line; i++)          for (i = 0; i < split_line_total; i++)
305          {          {
306                  len = split_line(buf_insert, SCREEN_COLS, &eol, &display_len_insert);                  if (display_line + i > last_display_line)
                 if (len != len_insert)  
307                  {                  {
308                          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
309                                            i, len, len_insert, buf_insert);                          if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
310                          return -3;                          {
311                                    // log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
312                                    // Terminate prior display line with \n, to avoid error on cleanup
313                                    if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
314                                    {
315                                            len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len);
316                                            p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
317                                            p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
318                                            p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
319                                    }
320                                    if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
321                                    {
322                                            *p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1;
323                                    }
324                                    break;
325                            }
326                            for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
327                            {
328                                    p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
329                                    p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
330                            }
331                            last_display_line++;
332                            (p_editor_data->display_line_total)++;
333                  }                  }
334    
335                  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];
336                  buf_catenate[p_editor_data->display_line_lengths[i]] = '\0';                  p_editor_data->p_display_lines[display_line + i] =
337                            (i == 0
338                                     ? p_data_line
339                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
340    
341                  len = split_line(buf_catenate, SCREEN_COLS - display_len_insert, &eol, &display_len);                  if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
342                  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')
343                  {                  {
344                          offset -= len;                          break;
                         continue;  
345                  }                  }
346            }
347    
348                  // 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);
                 if (len > 0 && buf_catenate[len - 1] == '\n' && display_len + display_len_insert >= SCREEN_COLS)  
                 {  
                         len--;  
                 }  
349    
350                  memcpy(buf_catenate, p_editor_data->p_display_lines[i], (size_t)offset);          if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
351                  memcpy(buf_catenate + offset, buf_insert, (size_t)len_insert);          {
352                  memcpy(buf_catenate + offset + len_insert, p_editor_data->p_display_lines[i] + offset, (size_t)(len - offset));                  *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
                 len_catenate = len_insert + len;  
                 buf_catenate[len_catenate] = '\0';  
353    
354                  len_insert = p_editor_data->display_line_lengths[i] - len;                  if (*p_display_line + 1 >= p_editor_data->display_line_total)
355                  if (len_insert > 0)                  {
356                            log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);
357                    }
358                    else
359                  {                  {
360                          memcpy(buf_insert, p_editor_data->p_display_lines[i] + len, (size_t)len_insert);                          (*p_display_line)++;
                         buf_insert[len_insert] = '\0';  
361                  }                  }
362            }
363    
364            return 0;
365    }
366    
367    int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,
368                                               long *p_last_updated_line)
369    {
370            char *p_data_line = NULL;
371            long len_data_line;
372            long offset_data_line;
373            long last_display_line; // of data line
374            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
375            long split_line_total;
376            long i, j;
377            int str_len = 0;
378    
379                  memcpy(p_editor_data->p_display_lines[i], buf_catenate, (size_t)len_catenate);          if (p_editor_data == NULL || p_last_updated_line == NULL)
380                  p_editor_data->display_line_lengths[i] = len_catenate;          {
381                    log_error("editor_data_op() error: NULL pointer\n");
382                    return -1;
383            }
384    
385                  if (!done)          // Get accurate offset of first character of CJK at offset position
386            for (i = 0; i < offset; i++)
387            {
388                    if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
389                  {                  {
390                          *p_display_line = i;                          i++;
                         *p_offset = offset + str_len;  
                         done = 1;  
391                  }                  }
392            }
393                  offset = 0;          if (i > offset) // offset was skipped
394            {
395                    offset--;
396          }          }
397    
398          *p_last_updated_line = MAX(i, *p_last_updated_line);          // Get length of current data line
399            len_data_line = 0;
400            p_data_line = p_editor_data->p_display_lines[display_line];
401            for (i = display_line - 1; i >= 0; i--)
402            {
403                    if (p_editor_data->display_line_lengths[i] > 0 &&
404                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
405                    {
406                            break;
407                    }
408    
409          if (len_insert > 0)                  len_data_line += p_editor_data->display_line_lengths[i];
410                    p_data_line = p_editor_data->p_display_lines[i];
411            }
412            offset_data_line = len_data_line + offset;
413            last_display_line = p_editor_data->display_line_total - 1;
414            for (i = display_line; i < p_editor_data->display_line_total; i++)
415          {          {
416                  if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)                  len_data_line += p_editor_data->display_line_lengths[i];
417    
418                    if (p_editor_data->display_line_lengths[i] > 0 &&
419                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
420                  {                  {
421                          log_error("Append line error, display_line_total(%ld) reach limit(%d)\n",                          last_display_line = i;
422                                            p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);                          break;
                         return -2;  
423                  }                  }
424            }
425    
426            // Check str to be deleted
427            if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
428            {
429                    str_len = 1;
430            }
431            else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK
432            {
433                    str_len = 2;
434            }
435            else
436            {
437                    log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n",
438                                      display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1],
439                                      p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]);
440                    str_len = 1;
441            }
442    
443                  // Prepare one blank display line after last_display_line          // Current display line is (almost) empty
444                  for (i = p_editor_data->display_line_total; i > last_display_line + 1; i--)          if (offset_data_line + str_len > len_data_line ||
445                    (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
446            {
447                    if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
448                  {                  {
449                          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];  
450                  }                  }
                 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;  
451    
452                  (p_editor_data->display_line_total)++;                  len_data_line = 0; // Next data line
453                  last_display_line++;                  last_display_line = p_editor_data->display_line_total - 1;
454                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
455                    {
456                            len_data_line += p_editor_data->display_line_lengths[i];
457    
458                  // Fill data into blank display line                          if (p_editor_data->display_line_lengths[i] > 0 &&
459                  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
460                  p_editor_data->p_display_lines[last_display_line][len_insert] = '\0';                          {
461                  p_editor_data->display_line_lengths[last_display_line] = len_insert;                                  last_display_line = i;
462                                    break;
463                            }
464                    }
465    
466                  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
467                  {                  {
468                          *p_display_line = last_display_line;                          return 0;
                         *p_offset = str_len;  
                         done = 1;  
469                  }                  }
470    
471                  *p_last_updated_line = MAX(last_display_line, *p_last_updated_line);                  // Append next data line to current one
472                    memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
473                    p_data_line[offset_data_line + len_data_line] = '\0';
474    
475                    // Recycle next data line
476                    free(p_editor_data->p_display_lines[display_line + 1]);
477            }
478            else
479            {
480                    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));
481                    p_data_line[len_data_line - str_len] = '\0';
482                    len_data_line -= str_len;
483          }          }
484    
485          if (done && *p_offset >= SCREEN_COLS)          // Set p_data_line to head of current display line
486            p_data_line = p_editor_data->p_display_lines[display_line];
487            split_line_total = last_display_line - display_line + 2;
488    
489            // Split current data line since beginning of current display line
490            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
491    
492            for (i = 0; i < split_line_total; i++)
493          {          {
494                  (*p_display_line)++;                  p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
495                  *p_offset = 0;                  p_editor_data->p_display_lines[display_line + i] =
496                            (i == 0
497                                     ? p_data_line
498                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
499    
500                  if (*p_display_line >= p_editor_data->display_line_total)                  if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
501                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
502                  {                  {
503                          log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);                          break;
504                  }                  }
505          }          }
506    
507          return done;          *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
 }  
508    
509  int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,          if (display_line + i < last_display_line)
510                                             long *p_last_updated_line)          {
511  {                  // Remove redundant display line after last_display_line
512          return 0;                  for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
513                    {
514                            p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j];
515                            p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j];
516                    }
517    
518                    (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));
519                    last_display_line = display_line + i;
520    
521                    *p_last_updated_line = p_editor_data->display_line_total - 1;
522            }
523    
524            return str_len;
525  }  }
526    
527  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 439  int editor_display(EDITOR_DATA *p_editor Line 543  int editor_display(EDITOR_DATA *p_editor
543          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
544          EDITOR_CTX ctx;          EDITOR_CTX ctx;
545          int ch = 0;          int ch = 0;
546          char insert_str[4];          char input_str[4];
547          int str_len = 0;          int str_len = 0;
548          int input_ok;          int input_ok;
         int screen_current_row;  
549          const int screen_begin_row = 1;          const int screen_begin_row = 1;
550          int screen_end_row = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
551          const int screen_row_total = screen_end_row - screen_begin_row + 1;          int output_current_row = screen_begin_row;
552            int output_end_row = SCREEN_ROWS - 1;
553          long int line_current = 0;          long int line_current = 0;
554          long int len;          long int len;
555          int loop;          int loop;
# Line 455  int editor_display(EDITOR_DATA *p_editor Line 559  int editor_display(EDITOR_DATA *p_editor
559          long display_line_out, offset_out;          long display_line_out, offset_out;
560          int scroll_rows;          int scroll_rows;
561          long last_updated_line = 0;          long last_updated_line = 0;
562          int insert = 1;          int key_insert = 1;
563          int i;          int i, j;
564            char *p_str;
565    
566          screen_current_row = screen_begin_row;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_row, SCREEN_ROWS);  
567    
568          // update msg_ext with extended key handler          // update msg_ext with extended key handler
569          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 574  int editor_display(EDITOR_DATA *p_editor
574          loop = 1;          loop = 1;
575          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
576          {          {
577                  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)
578                  {                  {
579                          ctx.line_cursor = line_current - screen_current_row + row_pos + 1;                          ctx.line_cursor = line_current - output_current_row + row_pos + 1;
580    
581                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
582                                           "\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] "
583                                           "µÚ\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] "
584                                           "%s",                                           "%s",
585                                           row_pos, col_pos,                                           row_pos, col_pos,
586                                           ctx.line_cursor, p_editor_data->display_line_total,                                           ctx.line_cursor, p_editor_data->display_line_total,
587                                             key_insert ? "²åÈë" : "¸Äд",
588                                           ctx.msg);                                           ctx.msg);
589    
590                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);
# Line 510  int editor_display(EDITOR_DATA *p_editor Line 615  int editor_display(EDITOR_DATA *p_editor
615    
616                                  if (ch > 127 && ch <= 255) // GBK                                  if (ch > 127 && ch <= 255) // GBK
617                                  {                                  {
618                                          insert_str[str_len] = (char)(ch - 256);                                          input_str[str_len] = (char)(ch - 256);
619                                          str_len++;                                          str_len++;
620                                  }                                  }
621                                  else                                  else
# Line 518  int editor_display(EDITOR_DATA *p_editor Line 623  int editor_display(EDITOR_DATA *p_editor
623                                          str_len = 0;                                          str_len = 0;
624                                  }                                  }
625    
626                                  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
627                                            ch == CR || ch == KEY_ESC)                                                                                       // Special character
628                                  {                                  {
629                                          if (str_len == 0)                                          if (str_len == 0)
630                                          {                                          {
631                                                  insert_str[0] = (char)ch;                                                  input_str[0] = (char)ch;
632                                                  str_len = 1;                                                  str_len = 1;
633                                          }                                          }
634    
635                                          last_updated_line = line_current;                                          last_updated_line = line_current;
636                                          display_line_in = line_current - screen_current_row + row_pos;                                          display_line_in = line_current - output_current_row + row_pos;
637                                          offset_in = col_pos - 1;                                          offset_in = col_pos - 1;
638                                          display_line_out = display_line_in;                                          display_line_out = display_line_in;
639                                          offset_out = offset_in;                                          offset_out = offset_in;
640    
641                                            if (!key_insert) // overwrite
642                                            {
643                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
644                                                                                               &last_updated_line) < 0)
645                                                    {
646                                                            log_error("editor_data_delete() error\n");
647                                                    }
648                                            }
649    
650                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,                                          if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
651                                                                                     insert_str, str_len, &last_updated_line) < 0)                                                                                     input_str, str_len, &last_updated_line) < 0)
652                                          {                                          {
653                                                  log_error("editor_data_insert(%s) error\n", insert_str);                                                  log_error("editor_data_insert(str_len=%d) error\n", str_len);
654                                                  str_len = 0;                                                  str_len = 0;
655                                          }                                          }
656                                          else                                          else
657                                          {                                          {
658                                                  str_len = 0;                                                  str_len = 0;
659    
660                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
661                                                  line_current -= (screen_current_row - row_pos);                                                  line_current -= (output_current_row - row_pos);
662                                                  screen_current_row = (int)row_pos;                                                  output_current_row = (int)row_pos;
663    
664                                                  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));
665    
666                                                  if (scroll_rows > 0)                                                  if (scroll_rows > 0)
667                                                  {                                                  {
# Line 557  int editor_display(EDITOR_DATA *p_editor Line 672  int editor_display(EDITOR_DATA *p_editor
672                                                                  prints("\033[S"); // Scroll up 1 line                                                                  prints("\033[S"); // Scroll up 1 line
673                                                          }                                                          }
674    
675                                                          screen_current_row -= scroll_rows;                                                          output_current_row -= scroll_rows;
676                                                          if (screen_current_row < screen_begin_row)                                                          if (output_current_row < screen_begin_row)
677                                                          {                                                          {
678                                                                  line_current += (screen_begin_row - screen_current_row);                                                                  line_current += (screen_begin_row - output_current_row);
679                                                                  screen_current_row = screen_begin_row;                                                                  output_current_row = screen_begin_row;
680                                                          }                                                          }
681                                                          row_pos = screen_end_row;                                                          row_pos = output_end_row;
682                                                  }                                                  }
683                                                  else // if (scroll_lines == 0)                                                  else // if (scroll_lines == 0)
684                                                  {                                                  {
685                                                          row_pos += (display_line_out - display_line_in);                                                          row_pos += (display_line_out - display_line_in);
686                                                  }                                                  }
687                                                  col_pos = offset_out + 1;                                                  col_pos = offset_out + 1;
   
                                                 continue;  
688                                          }                                          }
689    
690                                            continue;
691                                  }                                  }
692                                  else if (ch == KEY_DEL) // Del                                  else if (ch == KEY_DEL || ch == BACKSPACE) // Del
693                                  {                                  {
694                                          last_updated_line = line_current;                                          if (ch == BACKSPACE)
695                                            {
696                                                    col_pos--;
697                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
698                                                    {
699                                                            row_pos--;
700                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
701                                                    }
702                                            }
703    
704                                          if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1,                                          if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1,
705                                                                                     &last_updated_line) < 0)                                                                                                            &last_updated_line)) < 0)
706                                          {                                          {
707                                                  log_error("editor_data_delete() error\n");                                                  log_error("editor_data_delete() error\n");
708                                          }                                          }
709                                          else                                          else
710                                          {                                          {
711                                                  screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current));                                                  if (ch == BACKSPACE)
712                                                    {
713                                                            for (i = 1; i < str_len; i++)
714                                                            {
715                                                                    col_pos--;
716                                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
717                                                                    {
718                                                                            row_pos--;
719                                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
720                                                                    }
721                                                            }
722                                                    }
723    
724                                                    output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
725                                                    line_current -= (output_current_row - row_pos);
726                                                    output_current_row = (int)row_pos;
727    
728                                                    if (output_current_row < screen_begin_row) // row_pos <= 0
729                                                    {
730                                                            output_current_row = screen_begin_row;
731                                                            row_pos = screen_begin_row;
732                                                            output_end_row = SCREEN_ROWS - 1;
733                                                    }
734    
735                                                    // Exceed end
736                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
737                                                            p_editor_data->display_line_total > screen_row_total)
738                                                    {
739                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
740                                                                                                    (p_editor_data->display_line_total - screen_row_total));
741    
742                                                            line_current = p_editor_data->display_line_total - screen_row_total;
743                                                            row_pos += scroll_rows;
744                                                            output_current_row = screen_begin_row;
745                                                            output_end_row = SCREEN_ROWS - 1;
746                                                            clrline(output_current_row, SCREEN_ROWS);
747                                                    }
748                                          }                                          }
749    
750                                          continue;                                          continue;
# Line 599  int editor_display(EDITOR_DATA *p_editor Line 758  int editor_display(EDITOR_DATA *p_editor
758                                  case Ctrl('C'):                                  case Ctrl('C'):
759                                          loop = 0;                                          loop = 0;
760                                          break;                                          break;
761                                    case Ctrl('S'): // Start of line
762                                  case KEY_CTRL_LEFT:                                  case KEY_CTRL_LEFT:
763                                          col_pos = 1;                                          col_pos = 1;
764                                          break;                                          break;
765                                    case Ctrl('E'): // End of line
766                                  case KEY_CTRL_RIGHT:                                  case KEY_CTRL_RIGHT:
767                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
768                                          break;                                          break;
769                                    case Ctrl('T'): // Top of screen
770                                  case KEY_CTRL_UP:                                  case KEY_CTRL_UP:
771                                          row_pos = screen_begin_row;                                          row_pos = screen_begin_row;
772                                          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]));
773                                          break;                                          break;
774                                    case Ctrl('B'): // Bottom of screen
775                                  case KEY_CTRL_DOWN:                                  case KEY_CTRL_DOWN:
776                                          row_pos = SCREEN_ROWS - 1;                                          row_pos = SCREEN_ROWS - 1;
777                                          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]));
778                                          break;                                          break;
779                                  case KEY_INS:                                  case KEY_INS:
780                                          insert = !insert;                                          key_insert = !key_insert;
781                                          break;                                          break;
782                                  case KEY_HOME:                                  case KEY_HOME:
783                                          row_pos = 1;                                          row_pos = 1;
784                                          col_pos = 1;                                          col_pos = 1;
785                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
786                                          {                                          {
787                                                  break;                                                  break;
788                                          }                                          }
789                                          line_current = 0;                                          line_current = 0;
790                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
791                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
792                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
793                                          break;                                          break;
794                                  case KEY_END:                                  case KEY_END:
795                                          if (p_editor_data->display_line_total < screen_row_total)                                          if (p_editor_data->display_line_total < screen_row_total)
796                                          {                                          {
797                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
798                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
799                                                  break;                                                  break;
800                                          }                                          }
801                                          line_current = p_editor_data->display_line_total - screen_row_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
802                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
803                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
804                                          row_pos = screen_row_total;                                          row_pos = SCREEN_ROWS - 1;
805                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
806                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
807                                          break;                                          break;
808                                  case KEY_LEFT:                                  case KEY_LEFT:
809                                          if (col_pos > 1)                                          if (col_pos > 1)
# Line 653  int editor_display(EDITOR_DATA *p_editor Line 816  int editor_display(EDITOR_DATA *p_editor
816                                          if (row_pos > screen_begin_row)                                          if (row_pos > screen_begin_row)
817                                          {                                          {
818                                                  row_pos--;                                                  row_pos--;
819                                                  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]));
820                                                  break;                                                  break;
821                                          }                                          }
822                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
823                                          {                                          {
824                                                  col_pos = 1;                                                  col_pos = 1;
825                                                  break;                                                  break;
826                                          }                                          }
827                                          line_current -= screen_current_row;                                          line_current -= output_current_row;
828                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
829                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
830                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
831                                          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
832                                          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:  
833                                          break;                                          break;
834                                  case KEY_SPACE:                                  case KEY_SPACE:
835                                          break;                                          break;
836                                  case KEY_RIGHT:                                  case KEY_RIGHT:
837                                          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])
838                                          {                                          {
839                                                  col_pos++;                                                  col_pos++;
840                                                  break;                                                  break;
# Line 683  int editor_display(EDITOR_DATA *p_editor Line 844  int editor_display(EDITOR_DATA *p_editor
844                                          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))
845                                          {                                          {
846                                                  row_pos++;                                                  row_pos++;
847                                                  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]));
848                                                  break;                                                  break;
849                                          }                                          }
850                                          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
851                                          {                                          {
852                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]);                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
853                                                  break;                                                  break;
854                                          }                                          }
855                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
856                                          screen_current_row = screen_row_total;                                          output_current_row = screen_row_total;
857                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
858                                          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]));
859                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
860                                          clrtoeol();                                          clrtoeol();
861                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
862                                          break;                                          break;
863                                  case KEY_PGUP:                                  case KEY_PGUP:
864                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
865                                          {                                          {
866                                                  break;                                                  break;
867                                          }                                          }
868                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
869                                          if (line_current < 0)                                          if (line_current < 0)
870                                          {                                          {
871                                                  line_current = 0;                                                  line_current = 0;
872                                          }                                          }
873                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
874                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
875                                          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]));
876                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
877                                          break;                                          break;
878                                  case KEY_PGDN:                                  case KEY_PGDN:
879                                          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
880                                          {                                          {
881                                                  break;                                                  break;
882                                          }                                          }
883                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
884                                          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
885                                          {                                          {
886                                                  line_current = p_editor_data->display_line_total - screen_row_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
887                                          }                                          }
888                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
889                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
890                                          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]));
891                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
892                                          break;                                          break;
893                                  case KEY_F1:                                  case KEY_F1:
894                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not reentrant
# Line 742  int editor_display(EDITOR_DATA *p_editor Line 901  int editor_display(EDITOR_DATA *p_editor
901                                          show_help = 1;                                          show_help = 1;
902                                  case KEY_F5:                                  case KEY_F5:
903                                          // Refresh after display help information                                          // Refresh after display help information
904                                          line_current -= (screen_current_row - screen_begin_row);                                          line_current -= (output_current_row - screen_begin_row);
905                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
906                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
907                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
908                                          break;                                          break;
909                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
910                                          break;                                          break;
# Line 755  int editor_display(EDITOR_DATA *p_editor Line 914  int editor_display(EDITOR_DATA *p_editor
914                                  }                                  }
915    
916                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(0);
917    
918                                    if (input_ok)
919                                    {
920                                            break;
921                                    }
922                          }                          }
923    
924                          continue;                          continue;
# Line 772  int editor_display(EDITOR_DATA *p_editor Line 936  int editor_display(EDITOR_DATA *p_editor
936                          len = 0;                          len = 0;
937                  }                  }
938    
939                  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);
940                  buffer[len] = '\0';                  // Replace '\033' with '*'
941                    p_str = p_editor_data->p_display_lines[line_current];
942                    for (i = 0, j = 0; i < len; i++)
943                    {
944                            if (p_str[i] == '\033')
945                            {
946                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
947                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
948                            }
949                            else
950                            {
951                                    buffer[j] = p_str[i];
952                                    j++;
953                            }
954                    }
955                    buffer[j] = '\0';
956    
957                  moveto(screen_current_row, 0);                  moveto(output_current_row, 0);
958                  clrtoeol();                  clrtoeol();
959                  prints("%s", buffer);                  prints("%s", buffer);
960                  line_current++;                  line_current++;
961                  screen_current_row++;                  output_current_row++;
962          }          }
963    
964  cleanup:  cleanup:


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

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