| 24 |
#include <stdlib.h> |
#include <stdlib.h> |
| 25 |
#include <sys/param.h> |
#include <sys/param.h> |
| 26 |
#include <strings.h> |
#include <strings.h> |
|
|
|
|
#define _POSIX_C_SOURCE 200809L |
|
| 27 |
#include <string.h> |
#include <string.h> |
| 28 |
|
|
| 29 |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
| 77 |
{ |
{ |
| 78 |
EDITOR_DATA *p_editor_data; |
EDITOR_DATA *p_editor_data; |
| 79 |
char *p_data_line = NULL; |
char *p_data_line = NULL; |
| 80 |
long line_offsets[MAX_EDITOR_DATA_LINES]; |
long line_offsets[MAX_EDITOR_DATA_LINES + 1]; |
| 81 |
long current_data_line_length = 0; |
long current_data_line_length = 0; |
| 82 |
long i; |
long i; |
| 83 |
|
|
| 94 |
return NULL; |
return NULL; |
| 95 |
} |
} |
| 96 |
|
|
| 97 |
p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES); |
p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, 0); |
| 98 |
|
|
| 99 |
for (i = 0; i < p_editor_data->display_line_total; i++) |
for (i = 0; i < p_editor_data->display_line_total; i++) |
| 100 |
{ |
{ |
| 126 |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
| 127 |
|
|
| 128 |
// Trim \n from last line |
// Trim \n from last line |
| 129 |
if (i + 1 == p_editor_data->display_line_total && |
if (i + 1 == p_editor_data->display_line_total && |
| 130 |
p_editor_data->display_line_lengths[i] > 0 && |
p_editor_data->display_line_lengths[i] > 0 && |
| 131 |
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
| 132 |
{ |
{ |
| 133 |
p_editor_data->display_line_lengths[i]--; |
p_editor_data->display_line_lengths[i]--; |
| 136 |
p_data_line[current_data_line_length] = '\0'; |
p_data_line[current_data_line_length] = '\0'; |
| 137 |
} |
} |
| 138 |
|
|
| 139 |
bzero(p_editor_data->p_display_lines + p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->display_line_total); |
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); |
| 140 |
|
|
| 141 |
return p_editor_data; |
return p_editor_data; |
| 142 |
} |
} |
| 225 |
return -1; |
return -1; |
| 226 |
} |
} |
| 227 |
|
|
| 228 |
|
// Validate str |
| 229 |
|
if ((str_len == 1 && str[0] <= 0) || |
| 230 |
|
(str_len == 2 && (str[0] >= 0 || str[1] >= 0))) |
| 231 |
|
{ |
| 232 |
|
log_error("Invalid input str, len=%d\n", str_len); |
| 233 |
|
return -2; |
| 234 |
|
} |
| 235 |
|
|
| 236 |
// Get accurate offset of first character of CJK at offset position |
// Get accurate offset of first character of CJK at offset position |
| 237 |
for (i = 0; i < offset; i++) |
for (i = 0; i < offset; i++) |
| 238 |
{ |
{ |
| 239 |
if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK |
if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK |
| 240 |
{ |
{ |
| 241 |
i++; |
i++; |
| 242 |
} |
} |
| 360 |
} |
} |
| 361 |
|
|
| 362 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 363 |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0); |
| 364 |
|
|
| 365 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 366 |
{ |
{ |
| 373 |
// Terminate prior display line with \n, to avoid error on cleanup |
// Terminate prior display line with \n, to avoid error on cleanup |
| 374 |
if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0) |
if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0) |
| 375 |
{ |
{ |
| 376 |
len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len); |
len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0); |
| 377 |
p_editor_data->p_display_lines[display_line + i - 1][len] = '\n'; |
p_editor_data->p_display_lines[display_line + i - 1][len] = '\n'; |
| 378 |
p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0'; |
p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0'; |
| 379 |
p_editor_data->display_line_lengths[display_line + i - 1] = len + 1; |
p_editor_data->display_line_lengths[display_line + i - 1] = len + 1; |
| 417 |
} |
} |
| 418 |
} |
} |
| 419 |
|
|
| 420 |
|
// Prevent the last display line from being over-length |
| 421 |
|
if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES) |
| 422 |
|
{ |
| 423 |
|
len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0); |
| 424 |
|
p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0'; |
| 425 |
|
p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len; |
| 426 |
|
if (*p_display_line + 1 >= p_editor_data->display_line_total) |
| 427 |
|
{ |
| 428 |
|
*p_offset = MIN(*p_offset, len); |
| 429 |
|
*p_display_line = p_editor_data->display_line_total - 1; |
| 430 |
|
} |
| 431 |
|
} |
| 432 |
|
|
| 433 |
return 0; |
return 0; |
| 434 |
} |
} |
| 435 |
|
|
| 436 |
int editor_data_delete(EDITOR_DATA *p_editor_data, long display_line, long offset, |
int editor_data_delete(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
| 437 |
long *p_last_updated_line) |
long *p_last_updated_line) |
| 438 |
{ |
{ |
| 439 |
|
long display_line = *p_display_line; |
| 440 |
|
long offset = *p_offset; |
| 441 |
char *p_data_line = NULL; |
char *p_data_line = NULL; |
| 442 |
long len_data_line; |
long len_data_line; |
| 443 |
long offset_data_line; |
long offset_data_line; |
| 456 |
// Get accurate offset of first character of CJK at offset position |
// Get accurate offset of first character of CJK at offset position |
| 457 |
for (i = 0; i < offset; i++) |
for (i = 0; i < offset; i++) |
| 458 |
{ |
{ |
| 459 |
if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK |
if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK |
| 460 |
{ |
{ |
| 461 |
i++; |
i++; |
| 462 |
} |
} |
| 504 |
{ |
{ |
| 505 |
str_len = 1; |
str_len = 1; |
| 506 |
} |
} |
| 507 |
else if (p_data_line[offset_data_line + 1] < 0 || p_data_line[offset_data_line] > 127) // GBK |
else if (p_data_line[offset_data_line + 1] < 0) // GBK |
| 508 |
{ |
{ |
| 509 |
str_len = 2; |
str_len = 2; |
| 510 |
} |
} |
| 562 |
split_line_total = last_display_line - display_line + 2; |
split_line_total = last_display_line - display_line + 2; |
| 563 |
|
|
| 564 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 565 |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total); |
split_line_total = split_data_lines(p_data_line, SCREEN_COLS, line_offsets, split_line_total, 0); |
| 566 |
|
|
| 567 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 568 |
{ |
{ |
| 595 |
*p_last_updated_line = MAX(j - 1, *p_last_updated_line); |
*p_last_updated_line = MAX(j - 1, *p_last_updated_line); |
| 596 |
} |
} |
| 597 |
|
|
| 598 |
|
// Return real offset |
| 599 |
|
*p_offset = offset; |
| 600 |
|
|
| 601 |
return str_len; |
return str_len; |
| 602 |
} |
} |
| 603 |
|
|
| 607 |
{ |
{ |
| 608 |
case 0: // Set msg |
case 0: // Set msg |
| 609 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 610 |
"| 退出[\033[32mCtrl-W\033[33m] | 帮助[\033[32mh\033[33m] |"); |
"| 退出[\033[32mCtrl-W\033[33m] |"); |
| 611 |
|
break; |
| 612 |
|
case KEY_CSI: |
| 613 |
|
*p_key = KEY_ESC; |
| 614 |
break; |
break; |
| 615 |
} |
} |
| 616 |
|
|
| 664 |
"%s", |
"%s", |
| 665 |
row_pos, col_pos, |
row_pos, col_pos, |
| 666 |
ctx.line_cursor, p_editor_data->display_line_total, |
ctx.line_cursor, p_editor_data->display_line_total, |
| 667 |
key_insert ? "插入" : "改写", |
key_insert ? "插入" : "替换", |
| 668 |
ctx.msg); |
ctx.msg); |
| 669 |
|
|
| 670 |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1); |
| 671 |
for (; display_len < SCREEN_COLS; display_len++) |
for (; display_len < SCREEN_COLS; display_len++) |
| 672 |
{ |
{ |
| 673 |
buffer[len++] = ' '; |
buffer[len++] = ' '; |
| 681 |
moveto((int)row_pos, (int)col_pos); |
moveto((int)row_pos, (int)col_pos); |
| 682 |
iflush(); |
iflush(); |
| 683 |
|
|
| 684 |
|
str_len = 0; |
| 685 |
input_ok = 0; |
input_ok = 0; |
| 686 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 687 |
while (!SYS_server_exit && !input_ok) |
while (!SYS_server_exit && !input_ok) |
| 688 |
{ |
{ |
|
ch = igetch_t(MAX_DELAY_TIME); |
|
|
input_ok = 1; |
|
|
|
|
| 689 |
// extended key handler |
// extended key handler |
| 690 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 691 |
{ |
{ |
| 697 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 698 |
str_len++; |
str_len++; |
| 699 |
} |
} |
| 700 |
else |
else if (str_len > 0) |
| 701 |
{ |
{ |
| 702 |
|
log_error("Received %d character over 127 followed by character less than 127\n", str_len); |
| 703 |
str_len = 0; |
str_len = 0; |
| 704 |
} |
} |
| 705 |
|
|
| 706 |
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK |
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK |
| 707 |
ch == CR || ch == KEY_ESC) // Special character |
ch == CR || ch == KEY_ESC) // Special character |
| 708 |
{ |
{ |
| 709 |
if (str_len == 0) |
BBS_last_access_tm = time(NULL); |
| 710 |
|
|
| 711 |
|
if (str_len == 0) // ch >= 32 && ch < 127 |
| 712 |
{ |
{ |
| 713 |
input_str[0] = (char)ch; |
input_str[0] = (char)ch; |
| 714 |
str_len = 1; |
str_len = 1; |
| 722 |
|
|
| 723 |
if (!key_insert) // overwrite |
if (!key_insert) // overwrite |
| 724 |
{ |
{ |
| 725 |
if (editor_data_delete(p_editor_data, display_line_in, offset_in, |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 726 |
&last_updated_line) < 0) |
&last_updated_line) < 0) |
| 727 |
{ |
{ |
| 728 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 733 |
input_str, str_len, &last_updated_line) < 0) |
input_str, str_len, &last_updated_line) < 0) |
| 734 |
{ |
{ |
| 735 |
log_error("editor_data_insert(str_len=%d) error\n", str_len); |
log_error("editor_data_insert(str_len=%d) error\n", str_len); |
|
str_len = 0; |
|
| 736 |
} |
} |
| 737 |
else |
else |
| 738 |
{ |
{ |
|
str_len = 0; |
|
|
|
|
| 739 |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
| 740 |
line_current -= (output_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 741 |
output_current_row = (int)row_pos; |
output_current_row = (int)row_pos; |
| 763 |
{ |
{ |
| 764 |
row_pos += (display_line_out - display_line_in); |
row_pos += (display_line_out - display_line_in); |
| 765 |
} |
} |
| 766 |
col_pos = offset_out + 1; |
col_pos = offset_out + 1; // Set col_pos to accurate pos |
| 767 |
|
} |
| 768 |
|
|
| 769 |
|
if (display_line_out != display_line_in) // Output on line change |
| 770 |
|
{ |
| 771 |
|
break; |
| 772 |
|
} |
| 773 |
|
|
| 774 |
|
ch = igetch(0); |
| 775 |
|
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input |
| 776 |
|
{ |
| 777 |
|
break; |
| 778 |
} |
} |
| 779 |
|
|
| 780 |
|
str_len = 0; |
| 781 |
continue; |
continue; |
| 782 |
} |
} |
| 783 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
| 784 |
{ |
{ |
| 785 |
|
BBS_last_access_tm = time(NULL); |
| 786 |
|
|
| 787 |
if (ch == BACKSPACE) |
if (ch == BACKSPACE) |
| 788 |
{ |
{ |
| 789 |
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 |
| 790 |
{ |
{ |
| 791 |
input_ok = 0; |
ch = igetch_t(MAX_DELAY_TIME); |
| 792 |
continue; |
continue; |
| 793 |
} |
} |
| 794 |
|
|
| 795 |
col_pos--; |
col_pos--; |
| 796 |
|
if (col_pos > 1 && |
| 797 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK |
| 798 |
|
{ |
| 799 |
|
col_pos--; |
| 800 |
|
} |
| 801 |
|
|
| 802 |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
| 803 |
{ |
{ |
| 804 |
row_pos--; |
row_pos--; |
| 806 |
} |
} |
| 807 |
} |
} |
| 808 |
|
|
| 809 |
if ((str_len = editor_data_delete(p_editor_data, line_current - output_current_row + row_pos, col_pos - 1, |
display_line_in = line_current - output_current_row + row_pos; |
| 810 |
|
offset_in = col_pos - 1; |
| 811 |
|
display_line_out = display_line_in; |
| 812 |
|
offset_out = offset_in; |
| 813 |
|
|
| 814 |
|
if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 815 |
&last_updated_line)) < 0) |
&last_updated_line)) < 0) |
| 816 |
{ |
{ |
| 817 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 818 |
} |
} |
| 819 |
else |
else |
| 820 |
{ |
{ |
| 821 |
if (ch == BACKSPACE) |
col_pos = offset_out + 1; // Set col_pos to accurate pos |
|
{ |
|
|
for (i = 1; i < str_len; i++) |
|
|
{ |
|
|
col_pos--; |
|
|
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
|
|
{ |
|
|
row_pos--; |
|
|
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
|
|
} |
|
|
} |
|
|
} |
|
| 822 |
|
|
| 823 |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
| 824 |
line_current -= (output_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 847 |
clrline(output_current_row, output_end_row); |
clrline(output_current_row, output_end_row); |
| 848 |
} |
} |
| 849 |
|
|
| 850 |
|
if (display_line_out != display_line_in) // Output on line change |
| 851 |
|
{ |
| 852 |
|
break; |
| 853 |
|
} |
| 854 |
|
|
| 855 |
|
ch = igetch(0); |
| 856 |
|
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input |
| 857 |
|
{ |
| 858 |
|
break; |
| 859 |
|
} |
| 860 |
|
|
| 861 |
|
str_len = 0; |
| 862 |
continue; |
continue; |
| 863 |
} |
} |
| 864 |
|
|
| 865 |
|
input_ok = 1; |
| 866 |
switch (ch) |
switch (ch) |
| 867 |
{ |
{ |
| 868 |
case KEY_NULL: |
case KEY_NULL: |
| 877 |
break; |
break; |
| 878 |
case Ctrl('E'): // End of line |
case Ctrl('E'): // End of line |
| 879 |
case KEY_CTRL_RIGHT: |
case KEY_CTRL_RIGHT: |
| 880 |
|
if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line |
| 881 |
|
{ |
| 882 |
|
// last display line does NOT have \n in the end |
| 883 |
|
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 884 |
|
break; |
| 885 |
|
} |
| 886 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
| 887 |
break; |
break; |
| 888 |
case Ctrl('T'): // Top of screen |
case Ctrl('T'): // Top of screen |
| 943 |
if (col_pos > 1) |
if (col_pos > 1) |
| 944 |
{ |
{ |
| 945 |
col_pos--; |
col_pos--; |
| 946 |
|
if (col_pos > 1 && |
| 947 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && |
| 948 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK |
| 949 |
|
{ |
| 950 |
|
col_pos--; |
| 951 |
|
} |
| 952 |
break; |
break; |
| 953 |
} |
} |
| 954 |
col_pos = SCREEN_COLS; // continue to KEY_UP |
col_pos = SCREEN_COLS; // continue to KEY_UP |
| 976 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 977 |
if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]) |
if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]) |
| 978 |
{ |
{ |
| 979 |
|
if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && |
| 980 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK |
| 981 |
|
{ |
| 982 |
|
col_pos++; |
| 983 |
|
} |
| 984 |
col_pos++; |
col_pos++; |
| 985 |
break; |
break; |
| 986 |
} |
} |
| 992 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
| 993 |
break; |
break; |
| 994 |
} |
} |
| 995 |
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line |
| 996 |
{ |
{ |
| 997 |
// last display line does NOT have \n in the end |
// last display line does NOT have \n in the end |
| 998 |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 1059 |
break; |
break; |
| 1060 |
} |
} |
| 1061 |
|
|
| 1062 |
BBS_last_access_tm = time(0); |
BBS_last_access_tm = time(NULL); |
| 1063 |
|
|
| 1064 |
if (input_ok) |
if (input_ok) |
| 1065 |
{ |
{ |
| 1066 |
break; |
break; |
| 1067 |
} |
} |
| 1068 |
|
|
| 1069 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 1070 |
} |
} |
| 1071 |
|
|
| 1072 |
continue; |
continue; |