/[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.14 by sysadm, Thu Jun 12 03:16:35 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 54  EDITOR_DATA *editor_data_load(const char Line 57  EDITOR_DATA *editor_data_load(const char
57                  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];
58    
59                  if (i == 0 ||                  if (i == 0 ||
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                  {                  {
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;
74                          (p_editor_data->data_line_total)++;                          current_data_line_length = 0;
75                  }                  }
76                  else                  else
77                  {                  {
78                          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;  
79                  }                  }
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]);
                 p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i]] = '\0';  
   
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_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 120  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 127  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                    {
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);
151  }  }
152    
153  static int editor_display_key_handler(int *p_key, DISPLAY_CTX *p_ctx)  int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
154                                               const char *str, int str_len, long *p_last_updated_line)
155    {
156            long display_line = *p_display_line;
157            long offset = *p_offset;
158            char *p_data_line = NULL;
159            long len_data_line;
160            long offset_data_line;
161            long last_display_line; // of data line
162            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
163            long split_line_total;
164            long i, j;
165    
166            if (p_editor_data == NULL || p_last_updated_line == NULL)
167            {
168                    log_error("editor_data_op() error: NULL pointer\n");
169                    return -1;
170            }
171    
172            // Get accurate offset of first character of CJK at offset position
173            for (i = 0; i < offset; i++)
174            {
175                    if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
176                    {
177                            i++;
178                    }
179            }
180            if (i > offset) // offset was skipped
181            {
182                    offset--;
183            }
184    
185            // Get length of current data line
186            len_data_line = 0;
187            p_data_line = p_editor_data->p_display_lines[display_line];
188            for (i = display_line - 1; i >= 0; i--)
189            {
190                    if (p_editor_data->display_line_lengths[i] > 0 &&
191                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
192                    {
193                            break;
194                    }
195    
196                    len_data_line += p_editor_data->display_line_lengths[i];
197                    p_data_line = p_editor_data->p_display_lines[i];
198            }
199            offset_data_line = len_data_line + offset;
200            last_display_line = p_editor_data->display_line_total - 1;
201            for (i = display_line; i < p_editor_data->display_line_total; i++)
202            {
203                    len_data_line += p_editor_data->display_line_lengths[i];
204    
205                    if (p_editor_data->display_line_lengths[i] > 0 &&
206                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
207                    {
208                            last_display_line = i;
209                            break;
210                    }
211            }
212    
213            // Split current data line if over-length
214            if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
215            {
216                    if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
217                    {
218                            log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
219                                              p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
220                            return -2;
221                    }
222    
223                    // Allocate new data line
224                    p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH);
225                    if (p_data_line == NULL)
226                    {
227                            log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n");
228                            return -2;
229                    }
230    
231                    if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
232                    {
233                            if (str[0] == CR)
234                            {
235                                    str_len = 0;
236                            }
237    
238                            // Copy str to new data line
239                            memcpy(p_data_line, str, (size_t)str_len);
240    
241                            // Copy rest part of current data line to new data line
242                            memcpy(p_data_line + str_len,
243                                       p_editor_data->p_display_lines[display_line] + offset,
244                                       (size_t)(len_data_line - offset_data_line));
245    
246                            p_data_line[str_len + len_data_line - offset_data_line] = '\0';
247    
248                            // Add line ending to current display line (data line)
249                            p_editor_data->p_display_lines[display_line][offset] = '\n';
250                            p_editor_data->p_display_lines[display_line][offset + 1] = '\0';
251                            p_editor_data->display_line_lengths[display_line] = offset + 1;
252    
253                            *p_display_line = display_line + 1;
254                            *p_offset = str_len;
255                    }
256                    else
257                    {
258                            // Copy rest part of current data line to new data line
259                            memcpy(p_data_line,
260                                       p_editor_data->p_display_lines[display_line] + offset,
261                                       (size_t)(len_data_line - offset_data_line));
262    
263                            p_data_line[len_data_line - offset_data_line] = '\0';
264    
265                            // Append str to current display line
266                            memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);
267    
268                            // Add line ending to current display line (data line)
269                            p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';
270                            p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';
271                            p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;
272    
273                            *p_display_line = display_line;
274                            *p_offset = offset + str_len;
275                    }
276    
277                    split_line_total = last_display_line - display_line + 3;
278    
279                    // Set start display_line for spliting new data line
280                    display_line++;
281    
282                    *p_last_updated_line = p_editor_data->display_line_total;
283            }
284            else // insert str into current data line at offset_data_line
285            {
286                    memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line));
287                    memcpy(p_data_line + offset_data_line, str, (size_t)str_len);
288                    p_data_line[len_data_line + str_len] = '\0';
289    
290                    // Set p_data_line to head of current display line
291                    p_data_line = p_editor_data->p_display_lines[display_line];
292                    split_line_total = last_display_line - display_line + 3;
293    
294                    *p_display_line = display_line;
295                    *p_offset = offset + str_len;
296            }
297    
298            // Split current data line since beginning of current display line
299            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
300    
301            for (i = 0; i < split_line_total; i++)
302            {
303                    if (display_line + i > last_display_line)
304                    {
305                            // Insert blank display line after last_display_line
306                            if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
307                            {
308                                    log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
309                                    return -3;
310                            }
311                            for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
312                            {
313                                    p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
314                                    p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
315                            }
316                            last_display_line++;
317                            (p_editor_data->display_line_total)++;
318                    }
319    
320                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
321                    p_editor_data->p_display_lines[display_line + i] =
322                            (i == 0
323                                     ? p_data_line
324                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
325    
326                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
327                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
328                    {
329                            break;
330                    }
331            }
332    
333            *p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line);
334    
335            if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
336            {
337                    *p_offset -= p_editor_data->display_line_lengths[*p_display_line];
338                    (*p_display_line)++;
339    
340                    if (*p_display_line >= p_editor_data->display_line_total)
341                    {
342                            log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total);
343                    }
344            }
345    
346            return 0;
347    }
348    
349    int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset,
350                                               long *p_last_updated_line)
351    {
352            char *p_data_line = NULL;
353            long len_data_line;
354            long offset_data_line;
355            long last_display_line; // of data line
356            long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
357            long split_line_total;
358            long i, j;
359            int str_len = 0;
360    
361            if (p_editor_data == NULL || p_last_updated_line == NULL)
362            {
363                    log_error("editor_data_op() error: NULL pointer\n");
364                    return -1;
365            }
366    
367            // Get accurate offset of first character of CJK at offset position
368            for (i = 0; i < offset; i++)
369            {
370                    if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK
371                    {
372                            i++;
373                    }
374            }
375            if (i > offset) // offset was skipped
376            {
377                    offset--;
378            }
379    
380            // Get length of current data line
381            len_data_line = 0;
382            p_data_line = p_editor_data->p_display_lines[display_line];
383            for (i = display_line - 1; i >= 0; i--)
384            {
385                    if (p_editor_data->display_line_lengths[i] > 0 &&
386                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
387                    {
388                            break;
389                    }
390    
391                    len_data_line += p_editor_data->display_line_lengths[i];
392                    p_data_line = p_editor_data->p_display_lines[i];
393            }
394            offset_data_line = len_data_line + offset;
395            last_display_line = p_editor_data->display_line_total - 1;
396            for (i = display_line; i < p_editor_data->display_line_total; i++)
397            {
398                    len_data_line += p_editor_data->display_line_lengths[i];
399    
400                    if (p_editor_data->display_line_lengths[i] > 0 &&
401                            p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
402                    {
403                            last_display_line = i;
404                            break;
405                    }
406            }
407    
408            // Check str to be deleted
409            if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
410            {
411                    str_len = 1;
412            }
413            else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK
414            {
415                    str_len = 2;
416            }
417            else
418            {
419                    log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n",
420                                      display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1],
421                                      p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]);
422                    str_len = 1;
423            }
424    
425            // Current display line is (almost) empty
426            if (offset_data_line + str_len > len_data_line ||
427                    (offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n'))
428            {
429                    if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
430                    {
431                            return 0;
432                    }
433    
434                    len_data_line = 0; // Next data line
435                    last_display_line = p_editor_data->display_line_total - 1;
436                    for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
437                    {
438                            len_data_line += p_editor_data->display_line_lengths[i];
439    
440                            if (p_editor_data->display_line_lengths[i] > 0 &&
441                                    p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
442                            {
443                                    last_display_line = i;
444                                    break;
445                            }
446                    }
447    
448                    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
449                    {
450                            return 0;
451                    }
452    
453                    // Append next data line to current one
454                    memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
455                    p_data_line[offset_data_line + len_data_line] = '\0';
456    
457                    // Recycle next data line
458                    free(p_editor_data->p_display_lines[display_line + 1]);
459            }
460            else
461            {
462                    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));
463                    p_data_line[len_data_line - str_len] = '\0';
464                    len_data_line -= str_len;
465            }
466    
467            // Set p_data_line to head of current display line
468            p_data_line = p_editor_data->p_display_lines[display_line];
469            split_line_total = last_display_line - display_line + 2;
470    
471            // Split current data line since beginning of current display line
472            split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total);
473    
474            for (i = 0; i < split_line_total; i++)
475            {
476                    p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
477                    p_editor_data->p_display_lines[display_line + i] =
478                            (i == 0
479                                     ? p_data_line
480                                     : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
481    
482                    if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
483                            p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
484                    {
485                            break;
486                    }
487            }
488    
489            *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
490    
491            if (display_line + i < last_display_line)
492            {
493                    // Remove redundant display line after last_display_line
494                    for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
495                    {
496                            p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j];
497                            p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j];
498                    }
499    
500                    (p_editor_data->display_line_total) -= (last_display_line - (display_line + i));
501                    last_display_line = display_line + i;
502    
503                    *p_last_updated_line = p_editor_data->display_line_total - 1;
504            }
505    
506            return str_len;
507    }
508    
509    static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)
510  {  {
511          switch (*p_key)          switch (*p_key)
512          {          {
513          case 0: // Set msg          case 0: // Set msg
514                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
515                                   "| ·µ»Ø[\033[32m¡û\033[33m,\033[32mESC\033[33m] | "                                   "| Í˳ö[\033[32mCtrl-C\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] |");  
516                  break;                  break;
517          }          }
518    
# Line 154  int editor_display(EDITOR_DATA *p_editor Line 523  int editor_display(EDITOR_DATA *p_editor
523  {  {
524          static int show_help = 1;          static int show_help = 1;
525          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];          char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
526          DISPLAY_CTX ctx;          EDITOR_CTX ctx;
527          int ch = 0;          int ch = 0;
528          int input_ok, screen_current_line;          char input_str[4];
529          const int screen_begin_line = 1;          int str_len = 0;
530          int screen_end_line = SCREEN_ROWS - 1;          int input_ok;
531          const int screen_line_total = screen_end_line - screen_begin_line + 1;          const int screen_begin_row = 1;
532            const int screen_row_total = SCREEN_ROWS - screen_begin_row;
533            int output_current_row = screen_begin_row;
534            int output_end_row = SCREEN_ROWS - 1;
535          long int line_current = 0;          long int line_current = 0;
536          long int len;          long int len;
         long int percentile;  
537          int loop;          int loop;
538          int eol, display_len;          int eol, display_len;
539          long row_pos = 1, col_pos = 1;          long row_pos = 1, col_pos = 1;
540            long display_line_in, offset_in;
541            long display_line_out, offset_out;
542            int scroll_rows;
543            long last_updated_line = 0;
544            int key_insert = 1;
545            int i, j;
546            char *p_str;
547    
548          screen_current_line = screen_begin_line;          clrline(output_current_row, SCREEN_ROWS);
         clrline(screen_begin_line, SCREEN_ROWS);  
549    
550          // update msg_ext with extended key handler          // update msg_ext with extended key handler
551          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 556  int editor_display(EDITOR_DATA *p_editor
556          loop = 1;          loop = 1;
557          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
558          {          {
559                  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)
560                  {                  {
561                          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);  
562    
563                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
564                                           "\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] "
565                                           ctx.line_top,                                           "µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ [\033[32m%s\033[33m] "
566                                           ctx.line_bottom,                                           "%s",
567                                           percentile,                                           row_pos, col_pos,
568                                             ctx.line_cursor, p_editor_data->display_line_total,
569                                             key_insert ? "²åÈë" : "¸Äд",
570                                           ctx.msg);                                           ctx.msg);
571    
572                          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 595  int editor_display(EDITOR_DATA *p_editor
595                                          goto cleanup;                                          goto cleanup;
596                                  }                                  }
597    
598                                    if (ch > 127 && ch <= 255) // GBK
599                                    {
600                                            input_str[str_len] = (char)(ch - 256);
601                                            str_len++;
602                                    }
603                                    else
604                                    {
605                                            str_len = 0;
606                                    }
607    
608                                    if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK
609                                            ch == CR || ch == KEY_ESC)                                                                                       // Special character
610                                    {
611                                            if (str_len == 0)
612                                            {
613                                                    input_str[0] = (char)ch;
614                                                    str_len = 1;
615                                            }
616    
617                                            last_updated_line = line_current;
618                                            display_line_in = line_current - output_current_row + row_pos;
619                                            offset_in = col_pos - 1;
620                                            display_line_out = display_line_in;
621                                            offset_out = offset_in;
622    
623                                            if (!key_insert) // overwrite
624                                            {
625                                                    if (editor_data_delete(p_editor_data, display_line_in, offset_in,
626                                                                                               &last_updated_line) < 0)
627                                                    {
628                                                            log_error("editor_data_delete() error\n");
629                                                    }
630                                            }
631    
632                                            if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
633                                                                                       input_str, str_len, &last_updated_line) < 0)
634                                            {
635                                                    log_error("editor_data_insert(str_len=%d) error\n", str_len);
636                                                    str_len = 0;
637                                            }
638                                            else
639                                            {
640                                                    str_len = 0;
641    
642                                                    output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
643                                                    line_current -= (output_current_row - row_pos);
644                                                    output_current_row = (int)row_pos;
645    
646                                                    scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row));
647    
648                                                    if (scroll_rows > 0)
649                                                    {
650                                                            moveto(SCREEN_ROWS, 0);
651                                                            clrtoeol();
652                                                            for (i = 0; i < scroll_rows; i++)
653                                                            {
654                                                                    prints("\033[S"); // Scroll up 1 line
655                                                            }
656    
657                                                            output_current_row -= scroll_rows;
658                                                            if (output_current_row < screen_begin_row)
659                                                            {
660                                                                    line_current += (screen_begin_row - output_current_row);
661                                                                    output_current_row = screen_begin_row;
662                                                            }
663                                                            row_pos = output_end_row;
664                                                    }
665                                                    else // if (scroll_lines == 0)
666                                                    {
667                                                            row_pos += (display_line_out - display_line_in);
668                                                    }
669                                                    col_pos = offset_out + 1;
670                                            }
671    
672                                            continue;
673                                    }
674                                    else if (ch == KEY_DEL || ch == BACKSPACE) // Del
675                                    {
676                                            if (ch == BACKSPACE)
677                                            {
678                                                    col_pos--;
679                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
680                                                    {
681                                                            row_pos--;
682                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
683                                                    }
684                                            }
685    
686                                            if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1,
687                                                                                                              &last_updated_line)) < 0)
688                                            {
689                                                    log_error("editor_data_delete() error\n");
690                                            }
691                                            else
692                                            {
693                                                    if (ch == BACKSPACE)
694                                                    {
695                                                            for (i = 1; i < str_len; i++)
696                                                            {
697                                                                    col_pos--;
698                                                                    if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
699                                                                    {
700                                                                            row_pos--;
701                                                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
702                                                                    }
703                                                            }
704                                                    }
705    
706                                                    output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
707                                                    line_current -= (output_current_row - row_pos);
708                                                    output_current_row = (int)row_pos;
709    
710                                                    if (output_current_row < screen_begin_row) // row_pos <= 0
711                                                    {
712                                                            output_current_row = screen_begin_row;
713                                                            row_pos = screen_begin_row;
714                                                            output_end_row = SCREEN_ROWS - 1;
715                                                    }
716    
717                                                    // Exceed end
718                                                    if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
719                                                            p_editor_data->display_line_total > screen_row_total)
720                                                    {
721                                                            scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
722                                                                                                    (p_editor_data->display_line_total - screen_row_total));
723    
724                                                            line_current = p_editor_data->display_line_total - screen_row_total;
725                                                            row_pos += scroll_rows;
726                                                            output_current_row = screen_begin_row;
727                                                            output_end_row = SCREEN_ROWS - 1;
728                                                            clrline(output_current_row, SCREEN_ROWS);
729                                                    }
730                                            }
731    
732                                            continue;
733                                    }
734    
735                                  switch (ch)                                  switch (ch)
736                                  {                                  {
737                                  case KEY_NULL:                                  case KEY_NULL:
# Line 238  int editor_display(EDITOR_DATA *p_editor Line 740  int editor_display(EDITOR_DATA *p_editor
740                                  case Ctrl('C'):                                  case Ctrl('C'):
741                                          loop = 0;                                          loop = 0;
742                                          break;                                          break;
743                                  case Ctrl('H'):                                  case Ctrl('S'): // Start of line
744                                    case KEY_CTRL_LEFT:
745                                          col_pos = 1;                                          col_pos = 1;
746                                          break;                                          break;
747                                  case Ctrl('E'):                                  case Ctrl('E'): // End of line
748                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                  case KEY_CTRL_RIGHT:
749                                            col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
750                                            break;
751                                    case Ctrl('T'): // Top of screen
752                                    case KEY_CTRL_UP:
753                                            row_pos = screen_begin_row;
754                                            col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
755                                            break;
756                                    case Ctrl('B'): // Bottom of screen
757                                    case KEY_CTRL_DOWN:
758                                            row_pos = SCREEN_ROWS - 1;
759                                            col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]));
760                                            break;
761                                    case KEY_INS:
762                                            key_insert = !key_insert;
763                                          break;                                          break;
764                                  case KEY_HOME:                                  case KEY_HOME:
765                                          row_pos = 1;                                          row_pos = 1;
766                                          col_pos = 1;                                          col_pos = 1;
767                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
768                                          {                                          {
769                                                  break;                                                  break;
770                                          }                                          }
771                                          line_current = 0;                                          line_current = 0;
772                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
773                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
774                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
775                                          break;                                          break;
776                                  case KEY_END:                                  case KEY_END:
777                                          if (p_editor_data->display_line_total < screen_line_total)                                          if (p_editor_data->display_line_total < screen_row_total)
778                                          {                                          {
779                                                  row_pos = p_editor_data->display_line_total;                                                  row_pos = p_editor_data->display_line_total;
780                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
781                                                  break;                                                  break;
782                                          }                                          }
783                                          line_current = p_editor_data->display_line_total - screen_line_total;                                          line_current = p_editor_data->display_line_total - screen_row_total;
784                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
785                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
786                                          row_pos = screen_line_total;                                          row_pos = SCREEN_ROWS - 1;
787                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                          col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
788                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
789                                          break;                                          break;
790                                  case KEY_LEFT:                                  case KEY_LEFT:
791                                          if (col_pos > 1)                                          if (col_pos > 1)
# Line 278  int editor_display(EDITOR_DATA *p_editor Line 795  int editor_display(EDITOR_DATA *p_editor
795                                          }                                          }
796                                          col_pos = SCREEN_COLS; // continue to KEY_UP                                          col_pos = SCREEN_COLS; // continue to KEY_UP
797                                  case KEY_UP:                                  case KEY_UP:
798                                          if (row_pos > screen_begin_line)                                          if (row_pos > screen_begin_row)
799                                          {                                          {
800                                                  row_pos--;                                                  row_pos--;
801                                                  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]));
802                                                  break;                                                  break;
803                                          }                                          }
804                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
805                                          {                                          {
806                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  col_pos = 1;
807                                                  break;                                                  break;
808                                          }                                          }
809                                          line_current -= screen_current_line;                                          line_current -= output_current_row;
810                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
811                                          // screen_end_line = begin_line;                                          // screen_end_line = begin_line;
812                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
813                                          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
814                                          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:  
815                                          break;                                          break;
816                                  case KEY_SPACE:                                  case KEY_SPACE:
817                                          break;                                          break;
818                                  case KEY_RIGHT:                                  case KEY_RIGHT:
819                                          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])
820                                          {                                          {
821                                                  col_pos++;                                                  col_pos++;
822                                                  break;                                                  break;
823                                          }                                          }
824                                          col_pos = 1; // continue to KEY_DOWN                                          col_pos = 1; // continue to KEY_DOWN
825                                  case KEY_DOWN:                                  case KEY_DOWN:
826                                          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))
827                                          {                                          {
828                                                  row_pos++;                                                  row_pos++;
829                                                  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]));
830                                                  break;                                                  break;
831                                          }                                          }
832                                          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
833                                          {                                          {
834                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_line + row_pos]);                                                  col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]);
835                                                  break;                                                  break;
836                                          }                                          }
837                                          line_current += (screen_line_total - (screen_current_line - screen_begin_line));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
838                                          screen_current_line = screen_line_total;                                          output_current_row = screen_row_total;
839                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
840                                          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]));
841                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
842                                          clrtoeol();                                          clrtoeol();
843                                          prints("\033[S"); // Scroll up 1 line                                          prints("\033[S"); // Scroll up 1 line
844                                          break;                                          break;
845                                  case KEY_PGUP:                                  case KEY_PGUP:
846                                          if (line_current - screen_current_line < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
847                                          {                                          {
848                                                  break;                                                  break;
849                                          }                                          }
850                                          line_current -= ((screen_line_total - 1) + (screen_current_line - screen_begin_line));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
851                                          if (line_current < 0)                                          if (line_current < 0)
852                                          {                                          {
853                                                  line_current = 0;                                                  line_current = 0;
854                                          }                                          }
855                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
856                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
857                                          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]));
858                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
859                                          break;                                          break;
860                                  case KEY_PGDN:                                  case KEY_PGDN:
861                                          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
862                                          {                                          {
863                                                  break;                                                  break;
864                                          }                                          }
865                                          line_current += (screen_line_total - 1) - (screen_current_line - screen_begin_line);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
866                                          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
867                                          {                                          {
868                                                  line_current = p_editor_data->display_line_total - screen_line_total;                                                  line_current = p_editor_data->display_line_total - screen_row_total;
869                                          }                                          }
870                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
871                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
872                                          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]));
873                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_ESC:  
874                                          break;                                          break;
875                                  case KEY_F1:                                  case KEY_F1:
876                                          if (!show_help) // Not reentrant                                          if (!show_help) // Not reentrant
# Line 370  int editor_display(EDITOR_DATA *p_editor Line 883  int editor_display(EDITOR_DATA *p_editor
883                                          show_help = 1;                                          show_help = 1;
884                                  case KEY_F5:                                  case KEY_F5:
885                                          // Refresh after display help information                                          // Refresh after display help information
886                                          line_current -= (screen_current_line - screen_begin_line);                                          line_current -= (output_current_row - screen_begin_row);
887                                          screen_current_line = screen_begin_line;                                          output_current_row = screen_begin_row;
888                                          screen_end_line = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
889                                          clrline(screen_begin_line, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
890                                          break;                                          break;
891                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
892                                          break;                                          break;
# Line 383  int editor_display(EDITOR_DATA *p_editor Line 896  int editor_display(EDITOR_DATA *p_editor
896                                  }                                  }
897    
898                                  BBS_last_access_tm = time(0);                                  BBS_last_access_tm = time(0);
899    
900                                    if (input_ok)
901                                    {
902                                            break;
903                                    }
904                          }                          }
905    
906                          continue;                          continue;
# Line 400  int editor_display(EDITOR_DATA *p_editor Line 918  int editor_display(EDITOR_DATA *p_editor
918                          len = 0;                          len = 0;
919                  }                  }
920    
921                  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);
922                  buffer[len] = '\0';                  // Replace '\033' with '*'
923                    p_str = p_editor_data->p_display_lines[line_current];
924                    for (i = 0, j = 0; i < len; i++)
925                    {
926                            if (p_str[i] == '\033')
927                            {
928                                    memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
929                                    j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
930                            }
931                            else
932                            {
933                                    buffer[j] = p_str[i];
934                                    j++;
935                            }
936                    }
937                    buffer[j] = '\0';
938    
939                  moveto(screen_current_line, 0);                  moveto(output_current_row, 0);
940                  clrtoeol();                  clrtoeol();
941                  prints("%s", buffer);                  prints("%s", buffer);
942                  line_current++;                  line_current++;
943                  screen_current_line++;                  output_current_row++;
944          }          }
945    
946  cleanup:  cleanup:


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

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