| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
editor.h - description |
/* |
| 3 |
------------------- |
* editor |
| 4 |
copyright : (C) 2004-2025 by Leaflet |
* - user interactive full-screen text editor |
| 5 |
email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "bbs.h" |
#include "bbs.h" |
| 10 |
#include "common.h" |
#include "common.h" |
| 16 |
#include "str_process.h" |
#include "str_process.h" |
| 17 |
#include <stdlib.h> |
#include <stdlib.h> |
| 18 |
#include <string.h> |
#include <string.h> |
| 19 |
|
#include <wchar.h> |
| 20 |
#include <sys/param.h> |
#include <sys/param.h> |
| 21 |
|
|
| 22 |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
enum _editor_constant_t |
| 23 |
#define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000 |
{ |
| 24 |
#define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1) |
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; |
static MEMORY_POOL *p_mp_data_line; |
| 31 |
static MEMORY_POOL *p_mp_editor_data; |
static MEMORY_POOL *p_mp_editor_data; |
| 46 |
} |
} |
| 47 |
|
|
| 48 |
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
| 49 |
if (p_mp_data_line == NULL) |
if (p_mp_editor_data == NULL) |
| 50 |
{ |
{ |
| 51 |
log_error("Memory pool init error\n"); |
log_error("Memory pool init error\n"); |
| 52 |
return -3; |
return -3; |
| 80 |
|
|
| 81 |
if (p_data == NULL) |
if (p_data == NULL) |
| 82 |
{ |
{ |
| 83 |
log_error("editor_data_load() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 84 |
return NULL; |
return NULL; |
| 85 |
} |
} |
| 86 |
|
|
| 147 |
|
|
| 148 |
if (p_editor_data == NULL || p_data == NULL) |
if (p_editor_data == NULL || p_data == NULL) |
| 149 |
{ |
{ |
| 150 |
log_error("editor_data_save() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 151 |
return -1; |
return -1; |
| 152 |
} |
} |
| 153 |
|
|
| 221 |
|
|
| 222 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 223 |
{ |
{ |
| 224 |
log_error("editor_data_op() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 225 |
return -1; |
return -1; |
| 226 |
} |
} |
| 227 |
|
|
| 442 |
} |
} |
| 443 |
|
|
| 444 |
int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
| 445 |
long *p_last_updated_line) |
long *p_last_updated_line, int del_line) |
| 446 |
{ |
{ |
| 447 |
long display_line = *p_display_line; |
long display_line = *p_display_line; |
| 448 |
long offset = *p_offset; |
long offset = *p_offset; |
| 459 |
|
|
| 460 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 461 |
{ |
{ |
| 462 |
log_error("editor_data_op() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 463 |
return -1; |
return -1; |
| 464 |
} |
} |
| 465 |
|
|
| 497 |
} |
} |
| 498 |
|
|
| 499 |
// Check str to be deleted |
// Check str to be deleted |
| 500 |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
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; |
str_len = 1; |
| 507 |
} |
} |
| 508 |
else if (p_data_line[offset_data_line] & 0b10000000) // head of multi-byte character |
else if (p_data_line[offset_data_line] & 0x80) // head of multi-byte character |
| 509 |
{ |
{ |
| 510 |
str_len = 1; |
str_len = 1; |
| 511 |
c = (p_data_line[offset_data_line] & 0b01110000) << 1; |
c = (p_data_line[offset_data_line] & 0x70) << 1; |
| 512 |
while (c & 0b10000000) |
while (c & 0x80) |
| 513 |
{ |
{ |
| 514 |
str_len++; |
str_len++; |
| 515 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 516 |
} |
} |
| 517 |
} |
} |
| 518 |
else |
else |
| 524 |
|
|
| 525 |
// Current display line is (almost) empty |
// Current display line is (almost) empty |
| 526 |
if (offset_data_line + str_len > len_data_line || |
if (offset_data_line + str_len > len_data_line || |
| 527 |
(offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n')) |
(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) |
if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line) |
| 531 |
{ |
{ |
| 629 |
{ |
{ |
| 630 |
case 0: // Set msg |
case 0: // Set msg |
| 631 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 632 |
"| 退出[\033[32mCtrl-W\033[33m] |"); |
"| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]", |
| 633 |
|
(UTF8_fixed_width ? "定宽" : "变宽")); |
| 634 |
break; |
break; |
| 635 |
case KEY_CSI: |
case KEY_CSI: |
| 636 |
*p_key = KEY_ESC; |
*p_key = KEY_ESC; |
| 646 |
char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; |
char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; |
| 647 |
EDITOR_CTX ctx; |
EDITOR_CTX ctx; |
| 648 |
int ch = 0; |
int ch = 0; |
| 649 |
char input_str[4]; |
char input_str[5]; |
| 650 |
|
wchar_t wcs[2]; |
| 651 |
char c; |
char c; |
| 652 |
int str_len = 0; |
int str_len = 0; |
| 653 |
int input_ok; |
int input_ok; |
| 667 |
int key_insert = 1; |
int key_insert = 1; |
| 668 |
int i, j; |
int i, j; |
| 669 |
char *p_str; |
char *p_str; |
| 670 |
|
int del_line; |
| 671 |
|
|
| 672 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 673 |
|
|
| 707 |
iflush(); |
iflush(); |
| 708 |
|
|
| 709 |
str_len = 0; |
str_len = 0; |
| 710 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(BBS_max_user_idle_time); |
| 711 |
while (!SYS_server_exit) |
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 |
// extended key handler |
| 719 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 720 |
{ |
{ |
| 721 |
goto cleanup; |
goto cleanup; |
| 722 |
} |
} |
| 723 |
|
|
| 724 |
if (ch < 256 && (ch & 0b10000000)) // head of multi-byte character |
if (ch < 256 && (ch & 0x80)) // head of multi-byte character |
| 725 |
{ |
{ |
| 726 |
str_len = 0; |
str_len = 0; |
| 727 |
c = (char)(ch & 0b11110000); |
c = (char)(ch & 0xf0); |
| 728 |
while (c & 0b10000000) |
while (c & 0x80) |
| 729 |
{ |
{ |
| 730 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 731 |
str_len++; |
str_len++; |
| 732 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 733 |
|
|
| 734 |
if ((c & 0b10000000) == 0) // Input completed |
if ((c & 0x80) == 0) // Input completed |
| 735 |
{ |
{ |
| 736 |
break; |
break; |
| 737 |
} |
} |
| 747 |
break; |
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 |
if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character |
| 754 |
ch == CR || ch == KEY_ESC) // Special character |
ch == CR || ch == KEY_ESC) // Special character |
| 755 |
{ |
{ |
|
BBS_last_access_tm = time(NULL); |
|
|
|
|
| 756 |
// Refresh current action while user input |
// Refresh current action while user input |
| 757 |
if (user_online_update(NULL) < 0) |
if (user_online_update(NULL) < 0) |
| 758 |
{ |
{ |
| 775 |
if (!key_insert) // overwrite |
if (!key_insert) // overwrite |
| 776 |
{ |
{ |
| 777 |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 778 |
&last_updated_line) < 0) |
&last_updated_line, 0) < 0) |
| 779 |
{ |
{ |
| 780 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 781 |
} |
} |
| 825 |
} |
} |
| 826 |
if (offset_out > 0) |
if (offset_out > 0) |
| 827 |
{ |
{ |
| 828 |
col_pos += (str_len == 1 ? 1 : 2); |
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 |
} |
} |
| 848 |
str_len = 0; |
str_len = 0; |
| 849 |
continue; |
continue; |
| 850 |
} |
} |
| 851 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del |
| 852 |
{ |
{ |
|
BBS_last_access_tm = time(NULL); |
|
|
|
|
| 853 |
// Refresh current action while user input |
// Refresh current action while user input |
| 854 |
if (user_online_update(NULL) < 0) |
if (user_online_update(NULL) < 0) |
| 855 |
{ |
{ |
| 856 |
log_error("user_online_update(NULL) error\n"); |
log_error("user_online_update(NULL) error\n"); |
| 857 |
} |
} |
| 858 |
|
|
| 859 |
|
del_line = 0; |
| 860 |
|
|
| 861 |
if (ch == BACKSPACE) |
if (ch == BACKSPACE) |
| 862 |
{ |
{ |
| 863 |
if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden |
if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden |
| 867 |
|
|
| 868 |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
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); |
(int)col_pos - 1, &eol, &display_len, 0); |
| 870 |
if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 |
col_pos = display_len; |
|
{ |
|
|
col_pos = display_len - 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
col_pos = display_len; |
|
|
} |
|
|
|
|
| 871 |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
| 872 |
{ |
{ |
| 873 |
row_pos--; |
row_pos--; |
| 874 |
col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]); |
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; |
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); |
offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0); |
| 890 |
offset_out = offset_in; |
offset_out = offset_in; |
| 891 |
|
|
| 892 |
if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 893 |
&last_updated_line)) < 0) |
&last_updated_line, del_line)) < 0) |
| 894 |
{ |
{ |
| 895 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error: %d\n", str_len); |
| 896 |
} |
} |
| 897 |
else |
else |
| 898 |
{ |
{ |
| 944 |
switch (ch) |
switch (ch) |
| 945 |
{ |
{ |
| 946 |
case KEY_NULL: |
case KEY_NULL: |
| 947 |
|
log_error("KEY_NULL\n"); |
| 948 |
|
goto cleanup; |
| 949 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 950 |
|
log_error("User input timeout\n"); |
| 951 |
goto cleanup; |
goto cleanup; |
| 952 |
case Ctrl('W'): |
case Ctrl('W'): |
| 953 |
case Ctrl('X'): |
case Ctrl('X'): |
| 1024 |
case KEY_LEFT: |
case KEY_LEFT: |
| 1025 |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
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); |
(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 |
if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 |
| 1029 |
{ |
{ |
| 1030 |
col_pos = display_len - 1; |
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 |
else |
if (display_len == col_pos - 2) |
| 1033 |
{ |
{ |
| 1034 |
col_pos = display_len; |
col_pos--; |
| 1035 |
|
} |
| 1036 |
} |
} |
| 1037 |
if (col_pos >= 1) |
if (col_pos >= 1) |
| 1038 |
{ |
{ |
| 1058 |
output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line |
output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line |
| 1059 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos])); |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos])); |
| 1060 |
break; |
break; |
|
case KEY_SPACE: |
|
|
break; |
|
| 1061 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 1062 |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
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); |
(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] && |
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 |
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8 |
| 1067 |
{ |
{ |
| 1068 |
col_pos = display_len + 3; |
split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, |
| 1069 |
} |
1, &eol, &display_len, 0); |
| 1070 |
else |
if (display_len == 0) |
| 1071 |
{ |
{ |
| 1072 |
col_pos = display_len + 2; |
col_pos++; |
| 1073 |
|
} |
| 1074 |
} |
} |
| 1075 |
if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos]) |
if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos]) |
| 1076 |
{ |
{ |
| 1131 |
break; |
break; |
| 1132 |
case Ctrl('Q'): |
case Ctrl('Q'): |
| 1133 |
case KEY_F1: |
case KEY_F1: |
| 1134 |
if (!show_help) // Not reentrant |
if (!show_help) // Not re-entrant |
| 1135 |
{ |
{ |
| 1136 |
break; |
break; |
| 1137 |
} |
} |
| 1138 |
// Display help information |
// Display help information |
| 1139 |
show_help = 0; |
show_help = 0; |
| 1140 |
display_file(DATA_READ_HELP, 1); |
display_file(DATA_EDITOR_HELP, 1); |
| 1141 |
show_help = 1; |
show_help = 1; |
| 1142 |
case KEY_F5: |
case KEY_F5: |
| 1143 |
// Refresh after display help information |
// Refresh after display help information |
| 1153 |
break; |
break; |
| 1154 |
} |
} |
| 1155 |
|
|
|
BBS_last_access_tm = time(NULL); |
|
|
|
|
| 1156 |
// Refresh current action while user input |
// Refresh current action while user input |
| 1157 |
if (user_online_update(NULL) < 0) |
if (user_online_update(NULL) < 0) |
| 1158 |
{ |
{ |
| 1164 |
break; |
break; |
| 1165 |
} |
} |
| 1166 |
|
|
| 1167 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(BBS_max_user_idle_time); |
| 1168 |
} |
} |
| 1169 |
|
|
| 1170 |
continue; |
continue; |