| 19 |
#include "editor.h" |
#include "editor.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
#include "log.h" |
#include "log.h" |
| 22 |
|
#include "login.h" |
| 23 |
#include "memory_pool.h" |
#include "memory_pool.h" |
| 24 |
#include "str_process.h" |
#include "str_process.h" |
| 25 |
#include <stdlib.h> |
#include <stdlib.h> |
| 49 |
} |
} |
| 50 |
|
|
| 51 |
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
| 52 |
if (p_mp_data_line == NULL) |
if (p_mp_editor_data == NULL) |
| 53 |
{ |
{ |
| 54 |
log_error("Memory pool init error\n"); |
log_error("Memory pool init error\n"); |
| 55 |
return -3; |
return -3; |
| 83 |
|
|
| 84 |
if (p_data == NULL) |
if (p_data == NULL) |
| 85 |
{ |
{ |
| 86 |
log_error("editor_data_load() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 87 |
return NULL; |
return NULL; |
| 88 |
} |
} |
| 89 |
|
|
| 150 |
|
|
| 151 |
if (p_editor_data == NULL || p_data == NULL) |
if (p_editor_data == NULL || p_data == NULL) |
| 152 |
{ |
{ |
| 153 |
log_error("editor_data_save() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 154 |
return -1; |
return -1; |
| 155 |
} |
} |
| 156 |
|
|
| 224 |
|
|
| 225 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 226 |
{ |
{ |
| 227 |
log_error("editor_data_op() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 228 |
return -1; |
return -1; |
| 229 |
} |
} |
| 230 |
|
|
| 445 |
} |
} |
| 446 |
|
|
| 447 |
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, |
| 448 |
long *p_last_updated_line) |
long *p_last_updated_line, int del_line) |
| 449 |
{ |
{ |
| 450 |
long display_line = *p_display_line; |
long display_line = *p_display_line; |
| 451 |
long offset = *p_offset; |
long offset = *p_offset; |
| 462 |
|
|
| 463 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 464 |
{ |
{ |
| 465 |
log_error("editor_data_op() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 466 |
return -1; |
return -1; |
| 467 |
} |
} |
| 468 |
|
|
| 500 |
} |
} |
| 501 |
|
|
| 502 |
// Check str to be deleted |
// Check str to be deleted |
| 503 |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
if (del_line) |
| 504 |
|
{ |
| 505 |
|
str_len = (int)(p_editor_data->display_line_lengths[display_line] - offset); |
| 506 |
|
} |
| 507 |
|
else if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
| 508 |
{ |
{ |
| 509 |
str_len = 1; |
str_len = 1; |
| 510 |
} |
} |
| 511 |
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 |
| 512 |
{ |
{ |
| 513 |
str_len = 1; |
str_len = 1; |
| 514 |
c = (p_data_line[offset_data_line] & 0b01110000) << 1; |
c = (p_data_line[offset_data_line] & 0x70) << 1; |
| 515 |
while (c & 0b10000000) |
while (c & 0x80) |
| 516 |
{ |
{ |
| 517 |
str_len++; |
str_len++; |
| 518 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 519 |
} |
} |
| 520 |
} |
} |
| 521 |
else |
else |
| 527 |
|
|
| 528 |
// Current display line is (almost) empty |
// Current display line is (almost) empty |
| 529 |
if (offset_data_line + str_len > len_data_line || |
if (offset_data_line + str_len > len_data_line || |
| 530 |
(offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n')) |
(offset_data_line + str_len == len_data_line && |
| 531 |
|
p_data_line[del_line ? len_data_line - 1 : offset_data_line] == '\n')) |
| 532 |
{ |
{ |
| 533 |
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) |
| 534 |
{ |
{ |
| 668 |
int key_insert = 1; |
int key_insert = 1; |
| 669 |
int i, j; |
int i, j; |
| 670 |
char *p_str; |
char *p_str; |
| 671 |
|
int del_line; |
| 672 |
|
|
| 673 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 674 |
|
|
| 711 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(MAX_DELAY_TIME); |
| 712 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 713 |
{ |
{ |
| 714 |
|
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 715 |
|
{ |
| 716 |
|
BBS_last_access_tm = time(NULL); |
| 717 |
|
} |
| 718 |
|
|
| 719 |
// extended key handler |
// extended key handler |
| 720 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 721 |
{ |
{ |
| 722 |
goto cleanup; |
goto cleanup; |
| 723 |
} |
} |
| 724 |
|
|
| 725 |
if (ch < 256 && (ch & 0b10000000)) // head of multi-byte character |
if (ch < 256 && (ch & 0x80)) // head of multi-byte character |
| 726 |
{ |
{ |
| 727 |
str_len = 0; |
str_len = 0; |
| 728 |
c = (char)(ch & 0b11110000); |
c = (char)(ch & 0xf0); |
| 729 |
while (c & 0b10000000) |
while (c & 0x80) |
| 730 |
{ |
{ |
| 731 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 732 |
str_len++; |
str_len++; |
| 733 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 734 |
|
|
| 735 |
if ((c & 0b10000000) == 0) // Input completed |
if ((c & 0x80) == 0) // Input completed |
| 736 |
{ |
{ |
| 737 |
break; |
break; |
| 738 |
} |
} |
| 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 |
{ |
{ |
| 756 |
BBS_last_access_tm = time(NULL); |
// 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 |
if (str_len == 0) // ch >= 32 && ch < 127 |
| 763 |
{ |
{ |
| 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 |
} |
} |
| 823 |
{ |
{ |
| 824 |
col_pos = 1; |
col_pos = 1; |
| 825 |
} |
} |
| 826 |
if (ch != CR) |
if (offset_out > 0) |
| 827 |
{ |
{ |
| 828 |
col_pos += (str_len == 1 ? 1 : 2); |
col_pos += (str_len == 1 ? 1 : 2); |
| 829 |
} |
} |
| 844 |
str_len = 0; |
str_len = 0; |
| 845 |
continue; |
continue; |
| 846 |
} |
} |
| 847 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE || ch == Ctrl('K') || ch == Ctrl('Y')) // Del |
| 848 |
{ |
{ |
| 849 |
BBS_last_access_tm = time(NULL); |
// Refresh current action while user input |
| 850 |
|
if (user_online_update(NULL) < 0) |
| 851 |
|
{ |
| 852 |
|
log_error("user_online_update(NULL) error\n"); |
| 853 |
|
} |
| 854 |
|
|
| 855 |
|
del_line = 0; |
| 856 |
|
|
| 857 |
if (ch == BACKSPACE) |
if (ch == BACKSPACE) |
| 858 |
{ |
{ |
| 878 |
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]); |
| 879 |
} |
} |
| 880 |
} |
} |
| 881 |
|
else if (ch == Ctrl('K')) |
| 882 |
|
{ |
| 883 |
|
del_line = 1; |
| 884 |
|
} |
| 885 |
|
else if (ch == Ctrl('Y')) |
| 886 |
|
{ |
| 887 |
|
col_pos = 1; |
| 888 |
|
del_line = 1; |
| 889 |
|
} |
| 890 |
|
|
| 891 |
display_line_in = line_current - output_current_row + row_pos; |
display_line_in = line_current - output_current_row + row_pos; |
| 892 |
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); |
| 894 |
offset_out = offset_in; |
offset_out = offset_in; |
| 895 |
|
|
| 896 |
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, |
| 897 |
&last_updated_line)) < 0) |
&last_updated_line, del_line)) < 0) |
| 898 |
{ |
{ |
| 899 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error: %d\n", str_len); |
| 900 |
} |
} |
| 901 |
else |
else |
| 902 |
{ |
{ |
| 948 |
switch (ch) |
switch (ch) |
| 949 |
{ |
{ |
| 950 |
case KEY_NULL: |
case KEY_NULL: |
| 951 |
|
log_error("KEY_NULL\n"); |
| 952 |
|
goto cleanup; |
| 953 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 954 |
|
log_error("User input timeout\n"); |
| 955 |
goto cleanup; |
goto cleanup; |
| 956 |
case Ctrl('W'): |
case Ctrl('W'): |
| 957 |
|
case Ctrl('X'): |
| 958 |
loop = 0; |
loop = 0; |
| 959 |
break; |
break; |
| 960 |
case Ctrl('S'): // Start of line |
case Ctrl('S'): // Start of line |
| 1060 |
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 |
| 1061 |
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])); |
| 1062 |
break; |
break; |
|
case KEY_SPACE: |
|
|
break; |
|
| 1063 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 1064 |
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], |
| 1065 |
(int)col_pos - 1, &eol, &display_len, 0); |
(int)col_pos - 1, &eol, &display_len, 0); |
| 1129 |
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])); |
| 1130 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 1131 |
break; |
break; |
| 1132 |
|
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 |
|
|
| 1156 |
BBS_last_access_tm = time(NULL); |
// 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) |
if (input_ok) |
| 1163 |
{ |
{ |