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


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

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