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

Contents of /lbbs/src/editor.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.55 - (show annotations)
Sat Nov 8 08:21:31 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.54: +26 -22 lines
Content type: text/x-csrc
Support dynamic wide-character display width in str_process and editor related functions
Add dynamic / fixed wide-character display width selection

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

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