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

Contents of /lbbs/src/editor.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.52 - (show annotations)
Tue Nov 4 14:58:56 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.51: +1 -1 lines
Content type: text/x-csrc
Refine file header information comments

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * editor
4 * - user interactive full-screen text editor
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #include "bbs.h"
10 #include "common.h"
11 #include "editor.h"
12 #include "io.h"
13 #include "log.h"
14 #include "login.h"
15 #include "memory_pool.h"
16 #include "str_process.h"
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/param.h>
20
21 #define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m"
22 #define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000
23 #define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1)
24
25 static MEMORY_POOL *p_mp_data_line;
26 static MEMORY_POOL *p_mp_editor_data;
27
28 int editor_memory_pool_init(void)
29 {
30 if (p_mp_data_line != NULL || p_mp_editor_data != NULL)
31 {
32 log_error("Editor mem pool already initialized\n");
33 return -1;
34 }
35
36 p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT);
37 if (p_mp_data_line == NULL)
38 {
39 log_error("Memory pool init error\n");
40 return -2;
41 }
42
43 p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1);
44 if (p_mp_editor_data == NULL)
45 {
46 log_error("Memory pool init error\n");
47 return -3;
48 }
49
50 return 0;
51 }
52
53 void editor_memory_pool_cleanup(void)
54 {
55 if (p_mp_data_line != NULL)
56 {
57 memory_pool_cleanup(p_mp_data_line);
58 p_mp_data_line = NULL;
59 }
60
61 if (p_mp_editor_data != NULL)
62 {
63 memory_pool_cleanup(p_mp_editor_data);
64 p_mp_editor_data = NULL;
65 }
66 }
67
68 EDITOR_DATA *editor_data_load(const char *p_data)
69 {
70 EDITOR_DATA *p_editor_data;
71 char *p_data_line = NULL;
72 long line_offsets[MAX_EDITOR_DATA_LINES + 1];
73 long current_data_line_length = 0;
74 long i;
75
76 if (p_data == NULL)
77 {
78 log_error("NULL pointer error\n");
79 return NULL;
80 }
81
82 p_editor_data = memory_pool_alloc(p_mp_editor_data);
83 if (p_editor_data == NULL)
84 {
85 log_error("memory_pool_alloc() error\n");
86 return NULL;
87 }
88
89 p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1,
90 0, p_editor_data->display_line_widths);
91
92 for (i = 0; i < p_editor_data->display_line_total; i++)
93 {
94 p_editor_data->display_line_lengths[i] = line_offsets[i + 1] - line_offsets[i];
95
96 if (i == 0 ||
97 current_data_line_length + p_editor_data->display_line_lengths[i] + 1 > MAX_EDITOR_DATA_LINE_LENGTH ||
98 (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'))
99 {
100 // Allocate new data line
101 p_data_line = memory_pool_alloc(p_mp_data_line);
102 if (p_data_line == NULL)
103 {
104 log_error("memory_pool_alloc() error: i = %d\n", i);
105 // Cleanup
106 editor_data_cleanup(p_editor_data);
107 return NULL;
108 }
109
110 p_editor_data->p_display_lines[i] = p_data_line;
111 current_data_line_length = 0;
112 }
113 else
114 {
115 p_editor_data->p_display_lines[i] = p_editor_data->p_display_lines[i - 1] + p_editor_data->display_line_lengths[i - 1];
116 }
117
118 memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]);
119 current_data_line_length += p_editor_data->display_line_lengths[i];
120
121 // Trim \n from last line
122 if (i + 1 == p_editor_data->display_line_total &&
123 p_editor_data->display_line_lengths[i] > 0 &&
124 p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
125 {
126 p_editor_data->display_line_lengths[i]--;
127 p_editor_data->display_line_widths[i]--;
128 current_data_line_length--;
129 }
130 p_data_line[current_data_line_length] = '\0';
131 }
132
133 memset(p_editor_data->p_display_lines + p_editor_data->display_line_total, 0, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total);
134
135 return p_editor_data;
136 }
137
138 long editor_data_save(const EDITOR_DATA *p_editor_data, char *p_data, size_t buf_len)
139 {
140 long current_pos = 0;
141 long i;
142
143 if (p_editor_data == NULL || p_data == NULL)
144 {
145 log_error("NULL pointer error\n");
146 return -1;
147 }
148
149 for (i = 0; i < p_editor_data->display_line_total; i++)
150 {
151 if (current_pos + p_editor_data->display_line_lengths[i] + 1 > buf_len)
152 {
153 log_error("Data buffer not longer enough %d > %d\n", current_pos + p_editor_data->display_line_lengths[i] + 1, buf_len);
154 p_data[current_pos] = '\0';
155 return -2;
156 }
157
158 memcpy(p_data + current_pos, p_editor_data->p_display_lines[i], (size_t)p_editor_data->display_line_lengths[i]);
159 current_pos += p_editor_data->display_line_lengths[i];
160 }
161
162 p_data[current_pos] = '\0';
163
164 return current_pos;
165 }
166
167 void editor_data_cleanup(EDITOR_DATA *p_editor_data)
168 {
169 char *p_data_line = NULL;
170 long i;
171
172 if (p_editor_data == NULL)
173 {
174 return;
175 }
176
177 for (i = 0; i < p_editor_data->display_line_total; i++)
178 {
179 if (p_data_line == NULL)
180 {
181 p_data_line = p_editor_data->p_display_lines[i];
182 }
183
184 if (p_editor_data->display_line_lengths[i] > 0 &&
185 p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n')
186 {
187 memory_pool_free(p_mp_data_line, p_data_line);
188 p_data_line = NULL;
189 }
190 }
191
192 if (p_data_line != NULL)
193 {
194 memory_pool_free(p_mp_data_line, p_data_line);
195 }
196
197 memory_pool_free(p_mp_editor_data, p_editor_data);
198 }
199
200 int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
201 const char *str, int str_len, long *p_last_updated_line)
202 {
203 long display_line = *p_display_line;
204 long offset = *p_offset;
205 char *p_data_line = NULL;
206 long len_data_line;
207 long offset_data_line;
208 long last_display_line; // of data line
209 long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
210 int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
211 long split_line_total;
212 long i;
213 int len;
214 int eol;
215 int display_len;
216
217 if (p_editor_data == NULL || p_last_updated_line == NULL)
218 {
219 log_error("NULL pointer error\n");
220 return -1;
221 }
222
223 // Get length of current data line
224 len_data_line = 0;
225 p_data_line = p_editor_data->p_display_lines[display_line];
226 for (i = display_line - 1; i >= 0; i--)
227 {
228 if (p_editor_data->display_line_lengths[i] > 0 &&
229 p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
230 {
231 break;
232 }
233
234 len_data_line += p_editor_data->display_line_lengths[i];
235 p_data_line = p_editor_data->p_display_lines[i];
236 }
237 offset_data_line = len_data_line + offset;
238 last_display_line = p_editor_data->display_line_total - 1;
239 for (i = display_line; i < p_editor_data->display_line_total; i++)
240 {
241 len_data_line += p_editor_data->display_line_lengths[i];
242
243 if (p_editor_data->display_line_lengths[i] > 0 &&
244 p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
245 {
246 last_display_line = i;
247 break;
248 }
249 }
250
251 // Split current data line if over-length
252 if (len_data_line + str_len + 2 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
253 {
254 if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
255 {
256 #ifdef _DEBUG
257 log_error("Split line error, display_line_total(%ld) reach limit(%d)\n",
258 p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
259 #endif
260
261 return -2;
262 }
263
264 // Allocate new data line
265 p_data_line = memory_pool_alloc(p_mp_data_line);
266 if (p_data_line == NULL)
267 {
268 log_error("memory_pool_alloc() error\n");
269 return -2;
270 }
271
272 if (offset_data_line + str_len + 2 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR)
273 {
274 if (str[0] == CR)
275 {
276 str_len = 0;
277 }
278
279 // Copy str to new data line
280 memcpy(p_data_line, str, (size_t)str_len);
281
282 // Copy rest part of current data line to new data line
283 memcpy(p_data_line + str_len,
284 p_editor_data->p_display_lines[display_line] + offset,
285 (size_t)(len_data_line - offset_data_line));
286
287 p_data_line[str_len + len_data_line - offset_data_line] = '\0';
288
289 // Add line ending to current display line (data line)
290 p_editor_data->p_display_lines[display_line][offset] = '\n';
291 p_editor_data->p_display_lines[display_line][offset + 1] = '\0';
292 p_editor_data->display_line_lengths[display_line] = offset + 1;
293
294 *p_display_line = display_line + 1;
295 *p_offset = str_len;
296 }
297 else
298 {
299 // Copy rest part of current data line to new data line
300 memcpy(p_data_line,
301 p_editor_data->p_display_lines[display_line] + offset,
302 (size_t)(len_data_line - offset_data_line));
303
304 p_data_line[len_data_line - offset_data_line] = '\0';
305
306 // Append str to current display line
307 memcpy(p_editor_data->p_display_lines[display_line] + offset, str, (size_t)str_len);
308
309 // Add line ending to current display line (data line)
310 p_editor_data->p_display_lines[display_line][offset + str_len] = '\n';
311 p_editor_data->p_display_lines[display_line][offset + str_len + 1] = '\0';
312 p_editor_data->display_line_lengths[display_line] = offset + str_len + 1;
313
314 *p_display_line = display_line;
315 *p_offset = offset + str_len;
316 }
317
318 // Update display width of current display line
319 len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0);
320 p_editor_data->display_line_widths[display_line] = display_len;
321
322 split_line_total = last_display_line - display_line + 3;
323
324 // Set start display_line for spliting new data line
325 display_line++;
326
327 *p_last_updated_line = p_editor_data->display_line_total;
328 }
329 else // insert str into current data line at offset_data_line
330 {
331 memmove(p_data_line + offset_data_line + str_len, p_data_line + offset_data_line, (size_t)(len_data_line - offset_data_line));
332 memcpy(p_data_line + offset_data_line, str, (size_t)str_len);
333 p_data_line[len_data_line + str_len] = '\0';
334
335 // Set p_data_line to head of current display line
336 p_data_line = p_editor_data->p_display_lines[display_line];
337 split_line_total = last_display_line - display_line + 3;
338
339 *p_display_line = display_line;
340 *p_offset = offset + str_len;
341 }
342
343 // Split current data line since beginning of current display line
344 split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
345
346 for (i = 0; i < split_line_total; i++)
347 {
348 if (display_line + i > last_display_line)
349 {
350 // Insert blank display line after last_display_line
351 if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES)
352 {
353 #ifdef _DEBUG
354 log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES);
355 #endif
356
357 // Terminate prior display line with \n, to avoid error on cleanup
358 if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0)
359 {
360 len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
361 p_editor_data->p_display_lines[display_line + i - 1][len] = '\n';
362 p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0';
363 p_editor_data->display_line_lengths[display_line + i - 1] = len + 1;
364 p_editor_data->display_line_widths[display_line + i - 1] = display_len;
365 }
366 if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line])
367 {
368 *p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1;
369 }
370 break;
371 }
372
373 // for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--)
374 // {
375 // p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1];
376 // p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1];
377 // p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1];
378 // }
379 memmove(p_editor_data->p_display_lines + last_display_line + 2,
380 p_editor_data->p_display_lines + last_display_line + 1,
381 (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
382 sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
383 memmove(p_editor_data->display_line_lengths + last_display_line + 2,
384 p_editor_data->display_line_lengths + last_display_line + 1,
385 (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
386 sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
387 memmove(p_editor_data->display_line_widths + last_display_line + 2,
388 p_editor_data->display_line_widths + last_display_line + 1,
389 (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
390 sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
391
392 last_display_line++;
393 *p_last_updated_line = p_editor_data->display_line_total;
394 (p_editor_data->display_line_total)++;
395 }
396
397 p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
398 p_editor_data->display_line_widths[display_line + i] = line_widths[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 }
421
422 // Prevent the last display line from being over-length
423 if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES)
424 {
425 len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0);
426 p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0';
427 p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len;
428 p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len;
429 if (*p_display_line + 1 >= p_editor_data->display_line_total)
430 {
431 *p_offset = MIN(*p_offset, len);
432 *p_display_line = p_editor_data->display_line_total - 1;
433 }
434 }
435
436 return 0;
437 }
438
439 int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset,
440 long *p_last_updated_line, int del_line)
441 {
442 long display_line = *p_display_line;
443 long offset = *p_offset;
444 char *p_data_line = NULL;
445 long len_data_line;
446 long offset_data_line;
447 long last_display_line; // of data line
448 long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1];
449 int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1];
450 long split_line_total;
451 long i, j;
452 int str_len = 0;
453 char c;
454
455 if (p_editor_data == NULL || p_last_updated_line == NULL)
456 {
457 log_error("NULL pointer error\n");
458 return -1;
459 }
460
461 // Get length of current data line
462 len_data_line = 0;
463 p_data_line = p_editor_data->p_display_lines[display_line];
464 for (i = display_line - 1; i >= 0; i--)
465 {
466 if (p_editor_data->display_line_lengths[i] > 0 &&
467 p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of prior data line
468 {
469 break;
470 }
471
472 len_data_line += p_editor_data->display_line_lengths[i];
473 p_data_line = p_editor_data->p_display_lines[i];
474 }
475 offset_data_line = len_data_line + offset;
476 last_display_line = p_editor_data->display_line_total - 1;
477 for (i = display_line; i < p_editor_data->display_line_total; i++)
478 {
479 len_data_line += p_editor_data->display_line_lengths[i];
480
481 if (p_editor_data->display_line_lengths[i] > 0 &&
482 p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
483 {
484 last_display_line = i;
485 break;
486 }
487 }
488
489 if (offset_data_line >= len_data_line) // end-of-line
490 {
491 return 0;
492 }
493
494 // Check str to be deleted
495 if (del_line)
496 {
497 str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset);
498 }
499 else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127)
500 {
501 str_len = 1;
502 }
503 else if (p_data_line[offset_data_line] & 0x80) // head of multi-byte character
504 {
505 str_len = 1;
506 c = (p_data_line[offset_data_line] & 0x70) << 1;
507 while (c & 0x80)
508 {
509 str_len++;
510 c = (c & 0x7f) << 1;
511 }
512 }
513 else
514 {
515 log_error("Some strange character at display_line %ld, offset %ld: %d %d\n",
516 display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]);
517 str_len = 1;
518 }
519
520 // Current display line is (almost) empty
521 if (offset_data_line + str_len > len_data_line ||
522 (offset_data_line + str_len == len_data_line &&
523 p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n'))
524 {
525 if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line)
526 {
527 return 0;
528 }
529
530 len_data_line = 0; // Next data line
531 last_display_line = p_editor_data->display_line_total - 1;
532 for (i = display_line + 1; i < p_editor_data->display_line_total; i++)
533 {
534 len_data_line += p_editor_data->display_line_lengths[i];
535
536 if (p_editor_data->display_line_lengths[i] > 0 &&
537 p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line
538 {
539 last_display_line = i;
540 break;
541 }
542 }
543
544 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
545 {
546 return 0;
547 }
548
549 // Append next data line to current one
550 memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line);
551 p_data_line[offset_data_line + len_data_line] = '\0';
552
553 // Recycle next data line
554 memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]);
555 }
556 else
557 {
558 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));
559 p_data_line[len_data_line - str_len] = '\0';
560 len_data_line -= str_len;
561 }
562
563 // Set p_data_line to head of current display line
564 p_data_line = p_editor_data->p_display_lines[display_line];
565 split_line_total = last_display_line - display_line + 2;
566
567 // Split current data line since beginning of current display line
568 split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0, line_widths);
569
570 for (i = 0; i < split_line_total; i++)
571 {
572 p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i];
573 p_editor_data->display_line_widths[display_line + i] = line_widths[i];
574 p_editor_data->p_display_lines[display_line + i] =
575 (i == 0
576 ? p_data_line
577 : (p_editor_data->p_display_lines[display_line + i - 1] + p_editor_data->display_line_lengths[display_line + i - 1]));
578
579 if (p_editor_data->display_line_lengths[display_line + i] > 0 &&
580 p_editor_data->p_display_lines[display_line + i][p_editor_data->display_line_lengths[display_line + i] - 1] == '\n')
581 {
582 break;
583 }
584 }
585
586 *p_last_updated_line = display_line + MIN(i, split_line_total - 1);
587
588 if (*p_last_updated_line < last_display_line)
589 {
590 // Remove redundant display line after last_display_line
591 // for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++)
592 // {
593 // p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j];
594 // p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j];
595 // p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j];
596 // }
597 memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1,
598 p_editor_data->p_display_lines + last_display_line + 1,
599 (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
600 sizeof(p_editor_data->p_display_lines[last_display_line + 1]));
601 memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1,
602 p_editor_data->display_line_lengths + last_display_line + 1,
603 (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
604 sizeof(p_editor_data->display_line_lengths[last_display_line + 1]));
605 memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1,
606 p_editor_data->display_line_widths + last_display_line + 1,
607 (size_t)(p_editor_data->display_line_total - last_display_line - 1) *
608 sizeof(p_editor_data->display_line_widths[last_display_line + 1]));
609
610 j = p_editor_data->display_line_total;
611 (p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line);
612 *p_last_updated_line = MAX(j - 1, *p_last_updated_line);
613 }
614
615 // Return real offset
616 *p_offset = offset;
617
618 return str_len;
619 }
620
621 static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx)
622 {
623 switch (*p_key)
624 {
625 case 0: // Set msg
626 snprintf(p_ctx->msg, sizeof(p_ctx->msg),
627 "| 退出[\033[32mCtrl-W\033[33m] |");
628 break;
629 case KEY_CSI:
630 *p_key = KEY_ESC;
631 break;
632 }
633
634 return 0;
635 }
636
637 int editor_display(EDITOR_DATA *p_editor_data)
638 {
639 static int show_help = 1;
640 char buffer[MAX_EDITOR_DATA_LINE_LENGTH];
641 EDITOR_CTX ctx;
642 int ch = 0;
643 char input_str[4];
644 char c;
645 int str_len = 0;
646 int input_ok;
647 const int screen_begin_row = 1;
648 const int screen_row_total = SCREEN_ROWS - screen_begin_row;
649 int output_current_row = screen_begin_row;
650 int output_end_row = SCREEN_ROWS - 1;
651 long int line_current = 0;
652 long int len;
653 int loop;
654 int eol, display_len;
655 long row_pos = 1, col_pos = 1;
656 long display_line_in, offset_in;
657 long display_line_out, offset_out;
658 int scroll_rows;
659 long last_updated_line = 0;
660 int key_insert = 1;
661 int i, j;
662 char *p_str;
663 int del_line;
664
665 clrline(output_current_row, SCREEN_ROWS);
666
667 // update msg_ext with extended key handler
668 if (editor_display_key_handler(&ch, &ctx) != 0)
669 {
670 return ch;
671 }
672
673 for (loop = 1; !SYS_server_exit && loop;)
674 {
675 if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row)
676 {
677 ctx.line_cursor = line_current - output_current_row + row_pos + 1;
678
679 snprintf(buffer, sizeof(buffer),
680 "\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] "
681 "第\033[32m%ld\033[33m/\033[32m%ld\033[33m行 [\033[32m%s\033[33m] "
682 "%s",
683 row_pos, col_pos,
684 ctx.line_cursor, p_editor_data->display_line_total,
685 key_insert ? "插入" : "替换",
686 ctx.msg);
687
688 len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
689 for (; display_len < SCREEN_COLS; display_len++)
690 {
691 buffer[len++] = ' ';
692 }
693 buffer[len] = '\0';
694 strncat(buffer, "\033[m", sizeof(buffer) - 1 - strnlen(buffer, sizeof(buffer)));
695
696 moveto(SCREEN_ROWS, 0);
697 prints("%s", buffer);
698
699 moveto((int)row_pos, (int)col_pos);
700 iflush();
701
702 str_len = 0;
703 ch = igetch_t(MAX_DELAY_TIME);
704 while (!SYS_server_exit)
705 {
706 if (ch != KEY_NULL && ch != KEY_TIMEOUT)
707 {
708 BBS_last_access_tm = time(NULL);
709 }
710
711 // extended key handler
712 if (editor_display_key_handler(&ch, &ctx) != 0)
713 {
714 goto cleanup;
715 }
716
717 if (ch < 256 && (ch & 0x80)) // head of multi-byte character
718 {
719 str_len = 0;
720 c = (char)(ch & 0xf0);
721 while (c & 0x80)
722 {
723 input_str[str_len] = (char)(ch - 256);
724 str_len++;
725 c = (c & 0x7f) << 1;
726
727 if ((c & 0x80) == 0) // Input completed
728 {
729 break;
730 }
731
732 // Expect additional bytes of input
733 ch = igetch(100); // 0.1 second
734 if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
735 {
736 #ifdef _DEBUG
737 log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
738 #endif
739 str_len = 0;
740 break;
741 }
742 }
743 }
744
745 if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character
746 ch == CR || ch == KEY_ESC) // Special character
747 {
748 // Refresh current action while user input
749 if (user_online_update(NULL) < 0)
750 {
751 log_error("user_online_update(NULL) error\n");
752 }
753
754 if (str_len == 0) // ch >= 32 && ch < 127
755 {
756 input_str[0] = (char)ch;
757 str_len = 1;
758 }
759
760 display_line_in = line_current - output_current_row + row_pos;
761 offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
762 display_line_out = display_line_in;
763 offset_out = offset_in;
764
765 last_updated_line = display_line_in;
766
767 if (!key_insert) // overwrite
768 {
769 if (editor_data_delete(p_editor_data, &display_line_out, &offset_out,
770 &last_updated_line, 0) < 0)
771 {
772 log_error("editor_data_delete() error\n");
773 }
774 }
775
776 if (editor_data_insert(p_editor_data, &display_line_out, &offset_out,
777 input_str, str_len, &last_updated_line) < 0)
778 {
779 log_error("editor_data_insert(str_len=%d) error\n", str_len);
780 }
781 else
782 {
783 output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
784 line_current -= (output_current_row - row_pos);
785 output_current_row = (int)row_pos;
786
787 scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row));
788
789 if (scroll_rows > 0)
790 {
791 moveto(SCREEN_ROWS, 0);
792 clrtoeol();
793 for (i = 0; i < scroll_rows; i++)
794 {
795 // prints("\033[S"); // Scroll up 1 line
796 prints("\n"); // Legacy Cterm only works with this line
797 }
798
799 output_current_row -= scroll_rows;
800 if (output_current_row < screen_begin_row)
801 {
802 line_current += (screen_begin_row - output_current_row);
803 output_current_row = screen_begin_row;
804 }
805 row_pos = output_end_row;
806 }
807 else // if (scroll_lines == 0)
808 {
809 row_pos += (display_line_out - display_line_in);
810 }
811
812 if (offset_out != offset_in)
813 {
814 if (display_line_out != display_line_in)
815 {
816 col_pos = 1;
817 }
818 if (offset_out > 0)
819 {
820 col_pos += (str_len == 1 ? 1 : 2);
821 }
822 }
823 }
824
825 if (display_line_out != display_line_in) // Output on line change
826 {
827 break;
828 }
829
830 ch = igetch(0);
831 if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
832 {
833 break;
834 }
835
836 str_len = 0;
837 continue;
838 }
839 else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del
840 {
841 // Refresh current action while user input
842 if (user_online_update(NULL) < 0)
843 {
844 log_error("user_online_update(NULL) error\n");
845 }
846
847 del_line = 0;
848
849 if (ch == BACKSPACE)
850 {
851 if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden
852 {
853 break; // force output prior operation result if any
854 }
855
856 offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
857 (int)col_pos - 1, &eol, &display_len, 0);
858 if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
859 {
860 col_pos = display_len - 1;
861 }
862 else
863 {
864 col_pos = display_len;
865 }
866
867 if (col_pos < 1 && line_current - output_current_row + row_pos >= 0)
868 {
869 row_pos--;
870 col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
871 }
872 }
873 else if (ch == Ctrl('K'))
874 {
875 del_line = 1;
876 }
877 else if (ch == Ctrl('Y'))
878 {
879 col_pos = 1;
880 del_line = 1;
881 }
882
883 display_line_in = line_current - output_current_row + row_pos;
884 offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0);
885 display_line_out = display_line_in;
886 offset_out = offset_in;
887
888 if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out,
889 &last_updated_line, del_line)) < 0)
890 {
891 log_error("editor_data_delete() error: %d\n", str_len);
892 }
893 else
894 {
895 col_pos = display_len + 1; // Set col_pos to accurate pos
896
897 output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current));
898 line_current -= (output_current_row - row_pos);
899 output_current_row = (int)row_pos;
900
901 if (output_current_row < screen_begin_row) // row_pos <= 0
902 {
903 output_current_row = screen_begin_row;
904 row_pos = screen_begin_row;
905 output_end_row = SCREEN_ROWS - 1;
906 }
907
908 // Exceed end
909 if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total &&
910 p_editor_data->display_line_total > screen_row_total)
911 {
912 scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) -
913 (p_editor_data->display_line_total - screen_row_total));
914
915 line_current = p_editor_data->display_line_total - screen_row_total;
916 row_pos += scroll_rows;
917 output_current_row = screen_begin_row;
918 output_end_row = SCREEN_ROWS - 1;
919 }
920
921 clrline(output_current_row, output_end_row);
922 }
923
924 if (display_line_out != display_line_in) // Output on line change
925 {
926 break;
927 }
928
929 ch = igetch(0);
930 if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input
931 {
932 break;
933 }
934
935 str_len = 0;
936 continue;
937 }
938
939 input_ok = 1;
940 switch (ch)
941 {
942 case KEY_NULL:
943 log_error("KEY_NULL\n");
944 goto cleanup;
945 case KEY_TIMEOUT:
946 log_error("User input timeout\n");
947 goto cleanup;
948 case Ctrl('W'):
949 case Ctrl('X'):
950 loop = 0;
951 break;
952 case Ctrl('S'): // Start of line
953 case KEY_CTRL_LEFT:
954 col_pos = 1;
955 break;
956 case Ctrl('E'): // End of line
957 case KEY_CTRL_RIGHT:
958 if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
959 {
960 // last display line does NOT have \n in the end
961 col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
962 break;
963 }
964 col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]);
965 break;
966 case Ctrl('T'): // Top of screen
967 case KEY_CTRL_UP:
968 row_pos = screen_begin_row;
969 col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
970 break;
971 case Ctrl('B'): // Bottom of screen
972 case KEY_CTRL_DOWN:
973 if (p_editor_data->display_line_total < screen_row_total)
974 {
975 row_pos = p_editor_data->display_line_total;
976 }
977 else
978 {
979 row_pos = SCREEN_ROWS - 1;
980 }
981 if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end
982 {
983 // last display line does NOT have \n in the end
984 col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1);
985 }
986 else
987 {
988 col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
989 }
990 break;
991 case KEY_INS:
992 key_insert = !key_insert;
993 break;
994 case KEY_HOME:
995 row_pos = 1;
996 col_pos = 1;
997 if (line_current - output_current_row < 0) // Reach begin
998 {
999 break;
1000 }
1001 line_current = 0;
1002 output_current_row = screen_begin_row;
1003 output_end_row = SCREEN_ROWS - 1;
1004 clrline(output_current_row, SCREEN_ROWS);
1005 break;
1006 case KEY_END:
1007 if (p_editor_data->display_line_total < screen_row_total)
1008 {
1009 row_pos = p_editor_data->display_line_total;
1010 col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1011 break;
1012 }
1013 line_current = p_editor_data->display_line_total - screen_row_total;
1014 output_current_row = screen_begin_row;
1015 output_end_row = SCREEN_ROWS - 1;
1016 row_pos = SCREEN_ROWS - 1;
1017 col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1018 clrline(output_current_row, SCREEN_ROWS);
1019 break;
1020 case KEY_LEFT:
1021 offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1022 (int)col_pos - 1, &eol, &display_len, 0);
1023 if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8
1024 {
1025 col_pos = display_len - 1;
1026 }
1027 else
1028 {
1029 col_pos = display_len;
1030 }
1031 if (col_pos >= 1)
1032 {
1033 break;
1034 }
1035 col_pos = SCREEN_COLS; // continue to KEY_UP
1036 case KEY_UP:
1037 if (row_pos > screen_begin_row)
1038 {
1039 row_pos--;
1040 col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1041 break;
1042 }
1043 if (line_current - output_current_row < 0) // Reach begin
1044 {
1045 col_pos = 1;
1046 break;
1047 }
1048 line_current -= output_current_row;
1049 output_current_row = screen_begin_row;
1050 // screen_end_line = begin_line;
1051 // prints("\033[T"); // Scroll down 1 line
1052 output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
1053 col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1054 break;
1055 case KEY_RIGHT:
1056 offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos],
1057 (int)col_pos - 1, &eol, &display_len, 0);
1058 if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] &&
1059 p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8
1060 {
1061 col_pos = display_len + 3;
1062 }
1063 else
1064 {
1065 col_pos = display_len + 2;
1066 }
1067 if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos])
1068 {
1069 break;
1070 }
1071 col_pos = 1; // continue to KEY_DOWN
1072 case KEY_DOWN:
1073 if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total))
1074 {
1075 row_pos++;
1076 col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1077 break;
1078 }
1079 if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line
1080 {
1081 // last display line does NOT have \n in the end
1082 col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1;
1083 break;
1084 }
1085 line_current += (screen_row_total - (output_current_row - screen_begin_row));
1086 output_current_row = screen_row_total;
1087 output_end_row = SCREEN_ROWS - 1;
1088 col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1089 moveto(SCREEN_ROWS, 0);
1090 clrtoeol();
1091 // prints("\033[S"); // Scroll up 1 line
1092 prints("\n"); // Legacy Cterm only works with this line
1093 break;
1094 case KEY_PGUP:
1095 if (line_current - output_current_row < 0) // Reach begin
1096 {
1097 break;
1098 }
1099 line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
1100 if (line_current < 0)
1101 {
1102 line_current = 0;
1103 }
1104 output_current_row = screen_begin_row;
1105 output_end_row = SCREEN_ROWS - 1;
1106 col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1107 clrline(output_current_row, SCREEN_ROWS);
1108 break;
1109 case KEY_PGDN:
1110 if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= p_editor_data->display_line_total) // Reach end
1111 {
1112 break;
1113 }
1114 line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
1115 if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display
1116 {
1117 line_current = p_editor_data->display_line_total - screen_row_total;
1118 }
1119 output_current_row = screen_begin_row;
1120 output_end_row = SCREEN_ROWS - 1;
1121 col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]));
1122 clrline(output_current_row, SCREEN_ROWS);
1123 break;
1124 case Ctrl('Q'):
1125 case KEY_F1:
1126 if (!show_help) // Not re-entrant
1127 {
1128 break;
1129 }
1130 // Display help information
1131 show_help = 0;
1132 display_file(DATA_EDITOR_HELP, 1);
1133 show_help = 1;
1134 case KEY_F5:
1135 // Refresh after display help information
1136 line_current -= (output_current_row - screen_begin_row);
1137 output_current_row = screen_begin_row;
1138 output_end_row = SCREEN_ROWS - 1;
1139 clrline(output_current_row, SCREEN_ROWS);
1140 break;
1141 case 0: // Refresh bottom line
1142 break;
1143 default:
1144 input_ok = 0;
1145 break;
1146 }
1147
1148 // Refresh current action while user input
1149 if (user_online_update(NULL) < 0)
1150 {
1151 log_error("user_online_update(NULL) error\n");
1152 }
1153
1154 if (input_ok)
1155 {
1156 break;
1157 }
1158
1159 ch = igetch_t(MAX_DELAY_TIME);
1160 }
1161
1162 continue;
1163 }
1164
1165 len = p_editor_data->display_line_lengths[line_current];
1166 if (len >= sizeof(buffer))
1167 {
1168 log_error("Buffer overflow: len=%ld line=%ld \n", len, line_current);
1169 len = sizeof(buffer) - 1;
1170 }
1171 else if (len < 0)
1172 {
1173 log_error("Incorrect line offsets: len=%ld line=%ld \n", len, line_current);
1174 len = 0;
1175 }
1176
1177 // memcpy(buffer, p_editor_data->p_display_lines[line_current], (size_t)len);
1178 // Replace '\033' with '*'
1179 p_str = p_editor_data->p_display_lines[line_current];
1180 for (i = 0, j = 0; i < len; i++)
1181 {
1182 if (p_str[i] == '\033')
1183 {
1184 memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1185 j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1);
1186 }
1187 else
1188 {
1189 buffer[j] = p_str[i];
1190 j++;
1191 }
1192 }
1193 buffer[j] = '\0';
1194
1195 moveto(output_current_row, 0);
1196 clrtoeol();
1197 prints("%s", buffer);
1198 line_current++;
1199 output_current_row++;
1200 }
1201
1202 cleanup:
1203 return ch;
1204 }

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