--- lbbs/src/editor.c 2025/11/05 04:19:21 1.54 +++ lbbs/src/editor.c 2026/01/03 10:27:14 1.65 @@ -3,9 +3,13 @@ * editor * - user interactive full-screen text editor * - * Copyright (C) 2004-2025 Leaflet + * Copyright (C) 2004-2026 Leaflet */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "bbs.h" #include "common.h" #include "editor.h" @@ -16,6 +20,7 @@ #include "str_process.h" #include #include +#include #include enum _editor_constant_t @@ -33,21 +38,21 @@ int editor_memory_pool_init(void) { if (p_mp_data_line != NULL || p_mp_editor_data != NULL) { - log_error("Editor mem pool already initialized\n"); + log_error("Editor mem pool already initialized"); return -1; } p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT); if (p_mp_data_line == NULL) { - log_error("Memory pool init error\n"); + log_error("Memory pool init error"); return -2; } p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); if (p_mp_editor_data == NULL) { - log_error("Memory pool init error\n"); + log_error("Memory pool init error"); return -3; } @@ -76,17 +81,18 @@ EDITOR_DATA *editor_data_load(const char long line_offsets[MAX_EDITOR_DATA_LINES + 1]; long current_data_line_length = 0; long i; + int j; if (p_data == NULL) { - log_error("NULL pointer error\n"); + log_error("NULL pointer error"); return NULL; } p_editor_data = memory_pool_alloc(p_mp_editor_data); if (p_editor_data == NULL) { - log_error("memory_pool_alloc() error\n"); + log_error("memory_pool_alloc() error"); return NULL; } @@ -105,7 +111,7 @@ EDITOR_DATA *editor_data_load(const char p_data_line = memory_pool_alloc(p_mp_data_line); if (p_data_line == NULL) { - log_error("memory_pool_alloc() error: i = %d\n", i); + log_error("memory_pool_alloc() error: i = %d", i); // Cleanup editor_data_cleanup(p_editor_data); return NULL; @@ -122,6 +128,15 @@ EDITOR_DATA *editor_data_load(const char memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); current_data_line_length += p_editor_data->display_line_lengths[i]; + // Convert \t to single space + for (j = 0; j < p_editor_data->display_line_lengths[i]; j++) + { + if (p_editor_data->p_display_lines[i][j] == '\t') + { + p_editor_data->p_display_lines[i][j] = ' '; + } + } + // Trim \n from last line if (i + 1 == p_editor_data->display_line_total && p_editor_data->display_line_lengths[i] > 0 && @@ -146,7 +161,7 @@ long editor_data_save(const EDITOR_DATA if (p_editor_data == NULL || p_data == NULL) { - log_error("NULL pointer error\n"); + log_error("NULL pointer error"); return -1; } @@ -154,7 +169,7 @@ long editor_data_save(const EDITOR_DATA { if (current_pos + p_editor_data->display_line_lengths[i] + 1 > buf_len) { - log_error("Data buffer not longer enough %d > %d\n", current_pos + p_editor_data->display_line_lengths[i] + 1, buf_len); + log_error("Data buffer not longer enough %d > %d", current_pos + p_editor_data->display_line_lengths[i] + 1, buf_len); p_data[current_pos] = '\0'; return -2; } @@ -220,7 +235,7 @@ int editor_data_insert(EDITOR_DATA *p_ed if (p_editor_data == NULL || p_last_updated_line == NULL) { - log_error("NULL pointer error\n"); + log_error("NULL pointer error"); return -1; } @@ -257,10 +272,8 @@ int editor_data_insert(EDITOR_DATA *p_ed { if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) { -#ifdef _DEBUG - log_error("Split line error, display_line_total(%ld) reach limit(%d)\n", + log_debug("Split line error, display_line_total(%ld) reach limit(%d)", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); -#endif return -2; } @@ -269,7 +282,7 @@ int editor_data_insert(EDITOR_DATA *p_ed p_data_line = memory_pool_alloc(p_mp_data_line); if (p_data_line == NULL) { - log_error("memory_pool_alloc() error\n"); + log_error("memory_pool_alloc() error"); return -2; } @@ -354,9 +367,7 @@ int editor_data_insert(EDITOR_DATA *p_ed // Insert blank display line after last_display_line if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) { -#ifdef _DEBUG - log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); -#endif + log_debug("display_line_total over limit %d >= %d", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); // Terminate prior display line with \n, to avoid error on cleanup if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0) @@ -458,7 +469,7 @@ int editor_data_delete(EDITOR_DATA *p_ed if (p_editor_data == NULL || p_last_updated_line == NULL) { - log_error("NULL pointer error\n"); + log_error("NULL pointer error"); return -1; } @@ -516,7 +527,7 @@ int editor_data_delete(EDITOR_DATA *p_ed } else { - log_error("Some strange character at display_line %ld, offset %ld: %d %d\n", + log_error("Some strange character at display_line %ld, offset %ld: %d %d", display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]); str_len = 1; } @@ -628,7 +639,8 @@ static int editor_display_key_handler(in { case 0: // Set msg snprintf(p_ctx->msg, sizeof(p_ctx->msg), - "| 退出[\033[32mCtrl-W\033[33m] |"); + "| 退出[\033[32mCtrl-W\033[33m] | [\033[32m%s\033[33m]", + (UTF8_fixed_width ? "定宽" : "变宽")); break; case KEY_CSI: *p_key = KEY_ESC; @@ -644,9 +656,11 @@ int editor_display(EDITOR_DATA *p_editor char buffer[MAX_EDITOR_DATA_LINE_LENGTH]; EDITOR_CTX ctx; int ch = 0; - char input_str[4]; - char c; + char input_str[5]; int str_len = 0; + wchar_t wcs[2]; + int wc_len; + char c; int input_ok; const int screen_begin_row = 1; const int screen_row_total = SCREEN_ROWS - screen_begin_row; @@ -665,6 +679,7 @@ int editor_display(EDITOR_DATA *p_editor int i, j; char *p_str; int del_line; + int tab_width = 0; clrline(output_current_row, SCREEN_ROWS); @@ -703,6 +718,7 @@ int editor_display(EDITOR_DATA *p_editor moveto((int)row_pos, (int)col_pos); iflush(); + tab_width = 0; str_len = 0; ch = igetch_t(BBS_max_user_idle_time); while (!SYS_server_exit) @@ -718,6 +734,12 @@ int editor_display(EDITOR_DATA *p_editor goto cleanup; } + if (ch == '\t') + { + ch = ' '; + tab_width = TAB_SIZE - ((int)(col_pos - 1) % TAB_SIZE) - 1; + } + if (ch < 256 && (ch & 0x80)) // head of multi-byte character { str_len = 0; @@ -737,13 +759,12 @@ int editor_display(EDITOR_DATA *p_editor ch = igetch(100); // 0.1 second if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input { -#ifdef _DEBUG - log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len); -#endif + log_debug("Ignore %d bytes of incomplete UTF8 character", str_len); str_len = 0; break; } } + input_str[str_len] = '\0'; } if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character @@ -752,7 +773,7 @@ int editor_display(EDITOR_DATA *p_editor // Refresh current action while user input if (user_online_update(NULL) < 0) { - log_error("user_online_update(NULL) error\n"); + log_error("user_online_update(NULL) error"); } if (str_len == 0) // ch >= 32 && ch < 127 @@ -773,14 +794,14 @@ int editor_display(EDITOR_DATA *p_editor if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, &last_updated_line, 0) < 0) { - log_error("editor_data_delete() error\n"); + log_error("editor_data_delete() error"); } } if (editor_data_insert(p_editor_data, &display_line_out, &offset_out, input_str, str_len, &last_updated_line) < 0) { - log_error("editor_data_insert(str_len=%d) error\n", str_len); + log_error("editor_data_insert(str_len=%d) error", str_len); } else { @@ -821,7 +842,11 @@ int editor_display(EDITOR_DATA *p_editor } if (offset_out > 0) { - col_pos += (str_len == 1 ? 1 : 2); + if (mbstowcs(wcs, input_str, 1) == (size_t)-1) + { + log_error("mbstowcs() error"); + } + col_pos += (str_len == 1 ? 1 : (UTF8_fixed_width ? 2 : wcwidth(wcs[0]))); } } } @@ -831,10 +856,17 @@ int editor_display(EDITOR_DATA *p_editor break; } - ch = igetch(0); - if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input + if (ch == ' ' && tab_width > 0) { - break; + tab_width--; + } + else + { + ch = igetch(0); + if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input + { + break; + } } str_len = 0; @@ -845,7 +877,7 @@ int editor_display(EDITOR_DATA *p_editor // Refresh current action while user input if (user_online_update(NULL) < 0) { - log_error("user_online_update(NULL) error\n"); + log_error("user_online_update(NULL) error"); } del_line = 0; @@ -859,15 +891,7 @@ int editor_display(EDITOR_DATA *p_editor offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], (int)col_pos - 1, &eol, &display_len, 0); - 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 - 1; - } - else - { - col_pos = display_len; - } - + col_pos = display_len; if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) { row_pos--; @@ -892,7 +916,7 @@ int editor_display(EDITOR_DATA *p_editor if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, &last_updated_line, del_line)) < 0) { - log_error("editor_data_delete() error: %d\n", str_len); + log_error("editor_data_delete() error: %d", str_len); } else { @@ -944,10 +968,10 @@ int editor_display(EDITOR_DATA *p_editor switch (ch) { case KEY_NULL: - log_error("KEY_NULL\n"); + log_debug("KEY_NULL"); goto cleanup; case KEY_TIMEOUT: - log_error("User input timeout\n"); + log_debug("User input timeout"); goto cleanup; case Ctrl('W'): case Ctrl('X'): @@ -1024,13 +1048,38 @@ int editor_display(EDITOR_DATA *p_editor case KEY_LEFT: offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], (int)col_pos - 1, &eol, &display_len, 0); - 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 - 1; - } - else + col_pos = display_len; + if (offset_in > 0) { - col_pos = display_len; + str_len = 1; + offset_in--; + if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0 || + p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] > 127) // UTF8 + { + while (offset_in > 0 && + (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xc0) != 0xc0) + { + str_len++; + offset_in--; + } + + if (str_len > 4) + { + log_error("Invalid UTF-8 data detected: str_len > 4"); + } + + if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) == + (size_t)-1) + { + log_error("mbstowcs() error"); + } + wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); + + if (wc_len == 2) + { + col_pos--; + } + } } if (col_pos >= 1) { @@ -1059,14 +1108,42 @@ int editor_display(EDITOR_DATA *p_editor case KEY_RIGHT: offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], (int)col_pos - 1, &eol, &display_len, 0); - if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] && - p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8 - { - col_pos = display_len + 3; - } - else + col_pos = display_len + 2; + if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]) { - col_pos = display_len + 2; + str_len = 0; + if ((p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0x80) == + 0x80) // head of multi-byte character + { + c = (char)(p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] & 0xf0); + while (c & 0x80) + { + str_len++; + c = (c & 0x7f) << 1; + } + + if (str_len > 4) + { + log_error("Invalid UTF-8 data detected: str_len > 4"); + } + + if (mbstowcs(wcs, p_editor_data->p_display_lines[line_current - output_current_row + row_pos] + offset_in, 1) == + (size_t)-1) + { + log_error("mbstowcs() error"); + } + wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); + + if (wc_len == 2) + { + col_pos++; + } + } + else + { + str_len = 1; + } + offset_in += str_len; } if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos]) { @@ -1152,7 +1229,7 @@ int editor_display(EDITOR_DATA *p_editor // Refresh current action while user input if (user_online_update(NULL) < 0) { - log_error("user_online_update(NULL) error\n"); + log_error("user_online_update(NULL) error"); } if (input_ok) @@ -1169,12 +1246,12 @@ int editor_display(EDITOR_DATA *p_editor len = p_editor_data->display_line_lengths[line_current]; if (len >= sizeof(buffer)) { - log_error("Buffer overflow: len=%ld line=%ld \n", len, line_current); + log_error("Buffer overflow: len=%ld line=%ld ", len, line_current); len = sizeof(buffer) - 1; } else if (len < 0) { - log_error("Incorrect line offsets: len=%ld line=%ld \n", len, line_current); + log_error("Incorrect line offsets: len=%ld line=%ld ", len, line_current); len = 0; }