| 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" |
| 11 |
#include "editor.h" |
#include "editor.h" |
| 12 |
#include "io.h" |
#include "io.h" |
| 13 |
#include "log.h" |
#include "log.h" |
| 14 |
|
#include "login.h" |
| 15 |
#include "memory_pool.h" |
#include "memory_pool.h" |
| 16 |
#include "str_process.h" |
#include "str_process.h" |
| 17 |
#include <stdlib.h> |
#include <stdlib.h> |
| 41 |
} |
} |
| 42 |
|
|
| 43 |
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
| 44 |
if (p_mp_data_line == NULL) |
if (p_mp_editor_data == NULL) |
| 45 |
{ |
{ |
| 46 |
log_error("Memory pool init error\n"); |
log_error("Memory pool init error\n"); |
| 47 |
return -3; |
return -3; |
| 75 |
|
|
| 76 |
if (p_data == NULL) |
if (p_data == NULL) |
| 77 |
{ |
{ |
| 78 |
log_error("editor_data_load() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 79 |
return NULL; |
return NULL; |
| 80 |
} |
} |
| 81 |
|
|
| 142 |
|
|
| 143 |
if (p_editor_data == NULL || p_data == NULL) |
if (p_editor_data == NULL || p_data == NULL) |
| 144 |
{ |
{ |
| 145 |
log_error("editor_data_save() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 146 |
return -1; |
return -1; |
| 147 |
} |
} |
| 148 |
|
|
| 216 |
|
|
| 217 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 218 |
{ |
{ |
| 219 |
log_error("editor_data_op() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 220 |
return -1; |
return -1; |
| 221 |
} |
} |
| 222 |
|
|
| 437 |
} |
} |
| 438 |
|
|
| 439 |
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, |
| 440 |
long *p_last_updated_line) |
long *p_last_updated_line, int del_line) |
| 441 |
{ |
{ |
| 442 |
long display_line = *p_display_line; |
long display_line = *p_display_line; |
| 443 |
long offset = *p_offset; |
long offset = *p_offset; |
| 454 |
|
|
| 455 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 456 |
{ |
{ |
| 457 |
log_error("editor_data_op() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 458 |
return -1; |
return -1; |
| 459 |
} |
} |
| 460 |
|
|
| 492 |
} |
} |
| 493 |
|
|
| 494 |
// Check str to be deleted |
// Check str to be deleted |
| 495 |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
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; |
str_len = 1; |
| 502 |
} |
} |
| 503 |
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 |
| 504 |
{ |
{ |
| 505 |
str_len = 1; |
str_len = 1; |
| 506 |
c = (p_data_line[offset_data_line] & 0b01110000) << 1; |
c = (p_data_line[offset_data_line] & 0x70) << 1; |
| 507 |
while (c & 0b10000000) |
while (c & 0x80) |
| 508 |
{ |
{ |
| 509 |
str_len++; |
str_len++; |
| 510 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 511 |
} |
} |
| 512 |
} |
} |
| 513 |
else |
else |
| 519 |
|
|
| 520 |
// Current display line is (almost) empty |
// Current display line is (almost) empty |
| 521 |
if (offset_data_line + str_len > len_data_line || |
if (offset_data_line + str_len > len_data_line || |
| 522 |
(offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n')) |
(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) |
if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line) |
| 526 |
{ |
{ |
| 660 |
int key_insert = 1; |
int key_insert = 1; |
| 661 |
int i, j; |
int i, j; |
| 662 |
char *p_str; |
char *p_str; |
| 663 |
|
int del_line; |
| 664 |
|
|
| 665 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 666 |
|
|
| 700 |
iflush(); |
iflush(); |
| 701 |
|
|
| 702 |
str_len = 0; |
str_len = 0; |
| 703 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(BBS_max_user_idle_time); |
| 704 |
while (!SYS_server_exit) |
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 |
// extended key handler |
| 712 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 713 |
{ |
{ |
| 714 |
goto cleanup; |
goto cleanup; |
| 715 |
} |
} |
| 716 |
|
|
| 717 |
if (ch < 256 && (ch & 0b10000000)) // head of multi-byte character |
if (ch < 256 && (ch & 0x80)) // head of multi-byte character |
| 718 |
{ |
{ |
| 719 |
str_len = 0; |
str_len = 0; |
| 720 |
c = (char)(ch & 0b11110000); |
c = (char)(ch & 0xf0); |
| 721 |
while (c & 0b10000000) |
while (c & 0x80) |
| 722 |
{ |
{ |
| 723 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 724 |
str_len++; |
str_len++; |
| 725 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 726 |
|
|
| 727 |
if ((c & 0b10000000) == 0) // Input completed |
if ((c & 0x80) == 0) // Input completed |
| 728 |
{ |
{ |
| 729 |
break; |
break; |
| 730 |
} |
} |
| 745 |
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 |
| 746 |
ch == CR || ch == KEY_ESC) // Special character |
ch == CR || ch == KEY_ESC) // Special character |
| 747 |
{ |
{ |
| 748 |
BBS_last_access_tm = time(NULL); |
// 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 |
if (str_len == 0) // ch >= 32 && ch < 127 |
| 755 |
{ |
{ |
| 767 |
if (!key_insert) // overwrite |
if (!key_insert) // overwrite |
| 768 |
{ |
{ |
| 769 |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 770 |
&last_updated_line) < 0) |
&last_updated_line, 0) < 0) |
| 771 |
{ |
{ |
| 772 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 773 |
} |
} |
| 815 |
{ |
{ |
| 816 |
col_pos = 1; |
col_pos = 1; |
| 817 |
} |
} |
| 818 |
if (ch != CR) |
if (offset_out > 0) |
| 819 |
{ |
{ |
| 820 |
col_pos += (str_len == 1 ? 1 : 2); |
col_pos += (str_len == 1 ? 1 : 2); |
| 821 |
} |
} |
| 836 |
str_len = 0; |
str_len = 0; |
| 837 |
continue; |
continue; |
| 838 |
} |
} |
| 839 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del |
| 840 |
{ |
{ |
| 841 |
BBS_last_access_tm = time(NULL); |
// 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) |
if (ch == BACKSPACE) |
| 850 |
{ |
{ |
| 870 |
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]); |
| 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; |
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); |
offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0); |
| 886 |
offset_out = offset_in; |
offset_out = offset_in; |
| 887 |
|
|
| 888 |
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, |
| 889 |
&last_updated_line)) < 0) |
&last_updated_line, del_line)) < 0) |
| 890 |
{ |
{ |
| 891 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error: %d\n", str_len); |
| 892 |
} |
} |
| 893 |
else |
else |
| 894 |
{ |
{ |
| 940 |
switch (ch) |
switch (ch) |
| 941 |
{ |
{ |
| 942 |
case KEY_NULL: |
case KEY_NULL: |
| 943 |
|
log_error("KEY_NULL\n"); |
| 944 |
|
goto cleanup; |
| 945 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 946 |
|
log_error("User input timeout\n"); |
| 947 |
goto cleanup; |
goto cleanup; |
| 948 |
case Ctrl('W'): |
case Ctrl('W'): |
| 949 |
|
case Ctrl('X'): |
| 950 |
loop = 0; |
loop = 0; |
| 951 |
break; |
break; |
| 952 |
case Ctrl('S'): // Start of line |
case Ctrl('S'): // Start of line |
| 1052 |
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 |
| 1053 |
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])); |
| 1054 |
break; |
break; |
|
case KEY_SPACE: |
|
|
break; |
|
| 1055 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 1056 |
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], |
| 1057 |
(int)col_pos - 1, &eol, &display_len, 0); |
(int)col_pos - 1, &eol, &display_len, 0); |
| 1121 |
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])); |
| 1122 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 1123 |
break; |
break; |
| 1124 |
|
case Ctrl('Q'): |
| 1125 |
case KEY_F1: |
case KEY_F1: |
| 1126 |
if (!show_help) // Not reentrant |
if (!show_help) // Not re-entrant |
| 1127 |
{ |
{ |
| 1128 |
break; |
break; |
| 1129 |
} |
} |
| 1130 |
// Display help information |
// Display help information |
| 1131 |
show_help = 0; |
show_help = 0; |
| 1132 |
display_file(DATA_READ_HELP, 1); |
display_file(DATA_EDITOR_HELP, 1); |
| 1133 |
show_help = 1; |
show_help = 1; |
| 1134 |
case KEY_F5: |
case KEY_F5: |
| 1135 |
// Refresh after display help information |
// Refresh after display help information |
| 1145 |
break; |
break; |
| 1146 |
} |
} |
| 1147 |
|
|
| 1148 |
BBS_last_access_tm = time(NULL); |
// 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) |
if (input_ok) |
| 1155 |
{ |
{ |
| 1156 |
break; |
break; |
| 1157 |
} |
} |
| 1158 |
|
|
| 1159 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(BBS_max_user_idle_time); |
| 1160 |
} |
} |
| 1161 |
|
|
| 1162 |
continue; |
continue; |