| 27 |
#define _POSIX_C_SOURCE 200809L |
#define _POSIX_C_SOURCE 200809L |
| 28 |
#include <string.h> |
#include <string.h> |
| 29 |
|
|
| 30 |
|
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
| 31 |
|
|
| 32 |
EDITOR_DATA *editor_data_load(const char *p_data) |
EDITOR_DATA *editor_data_load(const char *p_data) |
| 33 |
{ |
{ |
| 34 |
EDITOR_DATA *p_editor_data; |
EDITOR_DATA *p_editor_data; |
| 171 |
// Get accurate offset of first character of CJK at offset position |
// Get accurate offset of first character of CJK at offset position |
| 172 |
for (i = 0; i < offset; i++) |
for (i = 0; i < offset; i++) |
| 173 |
{ |
{ |
| 174 |
if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK |
if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK |
| 175 |
{ |
{ |
| 176 |
i++; |
i++; |
| 177 |
} |
} |
| 370 |
// Get accurate offset of first character of CJK at offset position |
// Get accurate offset of first character of CJK at offset position |
| 371 |
for (i = 0; i < offset; i++) |
for (i = 0; i < offset; i++) |
| 372 |
{ |
{ |
| 373 |
if (p_editor_data->p_display_lines[display_line][i] < 0) // GBK |
if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK |
| 374 |
{ |
{ |
| 375 |
i++; |
i++; |
| 376 |
} |
} |
| 419 |
} |
} |
| 420 |
else |
else |
| 421 |
{ |
{ |
| 422 |
log_error("Some strange character at display_line %ld, offset %ld\n", display_line, offset); |
log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n", |
| 423 |
return -2; |
display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1], |
| 424 |
|
p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]); |
| 425 |
|
str_len = 1; |
| 426 |
} |
} |
| 427 |
|
|
| 428 |
// Current display line is (almost) empty |
// Current display line is (almost) empty |
| 429 |
if (offset_data_line + str_len > len_data_line || |
if (offset_data_line + str_len > len_data_line || |
| 430 |
(offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n')) |
(offset_data_line + str_len == len_data_line && p_data_line[offset_data_line] == '\n')) |
| 431 |
{ |
{ |
| 432 |
log_error("Nothing to be delete\n"); |
if (display_line + 1 >= p_editor_data->display_line_total) // No additional display line (data line) |
| 433 |
return 0; |
{ |
| 434 |
} |
log_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total); |
| 435 |
|
return 0; |
| 436 |
|
} |
| 437 |
|
|
| 438 |
|
len_data_line = 0; // Next data line |
| 439 |
|
last_display_line = p_editor_data->display_line_total - 1; |
| 440 |
|
for (i = display_line + 1; i < p_editor_data->display_line_total; i++) |
| 441 |
|
{ |
| 442 |
|
len_data_line += p_editor_data->display_line_lengths[i]; |
| 443 |
|
|
| 444 |
|
if (p_editor_data->display_line_lengths[i] > 0 && |
| 445 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') // reach end of current data line |
| 446 |
|
{ |
| 447 |
|
last_display_line = i; |
| 448 |
|
break; |
| 449 |
|
} |
| 450 |
|
} |
| 451 |
|
|
| 452 |
|
if (offset_data_line + len_data_line + 1 > MAX_EDITOR_DATA_LINE_LENGTH) // No enough buffer to merge current data line with next data line |
| 453 |
|
{ |
| 454 |
|
log_common("Debug: No enough buffer to merge with next data line: %ld > %ld\n", |
| 455 |
|
offset_data_line + len_data_line + 1, MAX_EDITOR_DATA_LINE_LENGTH); |
| 456 |
|
return 0; |
| 457 |
|
} |
| 458 |
|
|
| 459 |
memmove(p_data_line + offset_data_line, p_data_line + offset_data_line + str_len, (size_t)(len_data_line - offset_data_line - str_len)); |
// Append next data line to current one |
| 460 |
p_data_line[len_data_line - str_len] = '\0'; |
memcpy(p_data_line + offset_data_line, p_editor_data->p_display_lines[display_line + 1], (size_t)len_data_line); |
| 461 |
len_data_line -= str_len; |
p_data_line[offset_data_line + len_data_line] = '\0'; |
| 462 |
|
|
| 463 |
|
// Recycle next data line |
| 464 |
|
// TODO: free(p_editor_data->p_display_lines[display_line + 1]); |
| 465 |
|
} |
| 466 |
|
else |
| 467 |
|
{ |
| 468 |
|
memmove(p_data_line + offset_data_line, p_data_line + offset_data_line + str_len, (size_t)(len_data_line - offset_data_line - str_len)); |
| 469 |
|
p_data_line[len_data_line - str_len] = '\0'; |
| 470 |
|
len_data_line -= str_len; |
| 471 |
|
} |
| 472 |
|
|
| 473 |
// Set p_data_line to head of current display line |
// Set p_data_line to head of current display line |
| 474 |
p_data_line = p_editor_data->p_display_lines[display_line]; |
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 509 |
*p_last_updated_line = p_editor_data->display_line_total - 1; |
*p_last_updated_line = p_editor_data->display_line_total - 1; |
| 510 |
} |
} |
| 511 |
|
|
| 512 |
return 0; |
return str_len; |
| 513 |
} |
} |
| 514 |
|
|
| 515 |
static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx) |
static int editor_display_key_handler(int *p_key, EDITOR_CTX *p_ctx) |
| 548 |
int scroll_rows; |
int scroll_rows; |
| 549 |
long last_updated_line = 0; |
long last_updated_line = 0; |
| 550 |
int key_insert = 1; |
int key_insert = 1; |
| 551 |
int i; |
int i, j; |
| 552 |
|
char *p_str; |
| 553 |
|
|
| 554 |
screen_current_row = screen_begin_row; |
screen_current_row = screen_begin_row; |
| 555 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 591 |
iflush(); |
iflush(); |
| 592 |
|
|
| 593 |
input_ok = 0; |
input_ok = 0; |
| 594 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 595 |
while (!SYS_server_exit && !input_ok) |
while (!SYS_server_exit && !input_ok) |
| 596 |
{ |
{ |
|
ch = igetch_t(MAX_DELAY_TIME); |
|
|
input_ok = 1; |
|
|
|
|
| 597 |
// extended key handler |
// extended key handler |
| 598 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 599 |
{ |
{ |
| 610 |
str_len = 0; |
str_len = 0; |
| 611 |
} |
} |
| 612 |
|
|
| 613 |
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || ch == CR) // printable character or GBK |
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK |
| 614 |
|
ch == CR || ch == KEY_ESC) // Special character |
| 615 |
{ |
{ |
| 616 |
if (str_len == 0) |
if (str_len == 0) |
| 617 |
{ |
{ |
| 672 |
row_pos += (display_line_out - display_line_in); |
row_pos += (display_line_out - display_line_in); |
| 673 |
} |
} |
| 674 |
col_pos = offset_out + 1; |
col_pos = offset_out + 1; |
| 675 |
|
} |
| 676 |
|
|
| 677 |
continue; |
// Check whether there is additional input |
| 678 |
|
ch = igetch(0); |
| 679 |
|
if (ch == KEY_TIMEOUT) |
| 680 |
|
{ |
| 681 |
|
input_ok = 1; |
| 682 |
} |
} |
| 683 |
|
continue; |
| 684 |
} |
} |
| 685 |
else if (ch == KEY_DEL) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
| 686 |
{ |
{ |
| 687 |
last_updated_line = line_current; |
if (ch == BACKSPACE) |
| 688 |
|
{ |
| 689 |
|
col_pos--; |
| 690 |
|
if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0) |
| 691 |
|
{ |
| 692 |
|
row_pos--; |
| 693 |
|
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
| 694 |
|
} |
| 695 |
|
} |
| 696 |
|
|
| 697 |
if (editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1, |
if ((str_len = editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1, |
| 698 |
&last_updated_line) < 0) |
&last_updated_line)) < 0) |
| 699 |
{ |
{ |
| 700 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 701 |
} |
} |
| 702 |
else |
else |
| 703 |
{ |
{ |
| 704 |
|
if (ch == BACKSPACE) |
| 705 |
|
{ |
| 706 |
|
for (i = 1; i < str_len; i++) |
| 707 |
|
{ |
| 708 |
|
col_pos--; |
| 709 |
|
if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0) |
| 710 |
|
{ |
| 711 |
|
row_pos--; |
| 712 |
|
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
| 713 |
|
} |
| 714 |
|
} |
| 715 |
|
} |
| 716 |
|
|
| 717 |
screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); |
screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); |
| 718 |
line_current -= (screen_current_row - row_pos); |
line_current -= (screen_current_row - row_pos); |
| 719 |
screen_current_row = (int)row_pos; |
screen_current_row = (int)row_pos; |
| 720 |
|
|
| 721 |
|
if (screen_current_row < screen_begin_row) // row_pos <= 0 |
| 722 |
|
{ |
| 723 |
|
screen_current_row = screen_begin_row; |
| 724 |
|
row_pos = screen_begin_row; |
| 725 |
|
screen_end_row = SCREEN_ROWS - 1; |
| 726 |
|
} |
| 727 |
} |
} |
| 728 |
|
|
| 729 |
|
// Check whether there is additional input |
| 730 |
|
ch = igetch(0); |
| 731 |
|
if (ch == KEY_TIMEOUT) |
| 732 |
|
{ |
| 733 |
|
input_ok = 1; |
| 734 |
|
} |
| 735 |
continue; |
continue; |
| 736 |
} |
} |
| 737 |
|
|
| 738 |
|
input_ok = 1; |
| 739 |
switch (ch) |
switch (ch) |
| 740 |
{ |
{ |
| 741 |
case KEY_NULL: |
case KEY_NULL: |
| 744 |
case Ctrl('C'): |
case Ctrl('C'): |
| 745 |
loop = 0; |
loop = 0; |
| 746 |
break; |
break; |
| 747 |
|
case Ctrl('S'): // Start of line |
| 748 |
case KEY_CTRL_LEFT: |
case KEY_CTRL_LEFT: |
| 749 |
col_pos = 1; |
col_pos = 1; |
| 750 |
break; |
break; |
| 751 |
|
case Ctrl('E'): // End of line |
| 752 |
case KEY_CTRL_RIGHT: |
case KEY_CTRL_RIGHT: |
| 753 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
| 754 |
break; |
break; |
| 755 |
|
case Ctrl('T'): // Top of screen |
| 756 |
case KEY_CTRL_UP: |
case KEY_CTRL_UP: |
| 757 |
row_pos = screen_begin_row; |
row_pos = screen_begin_row; |
| 758 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
| 759 |
break; |
break; |
| 760 |
|
case Ctrl('B'): // Bottom of screen |
| 761 |
case KEY_CTRL_DOWN: |
case KEY_CTRL_DOWN: |
| 762 |
row_pos = SCREEN_ROWS - 1; |
row_pos = SCREEN_ROWS - 1; |
| 763 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
| 876 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
| 877 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(screen_begin_row, SCREEN_ROWS); |
| 878 |
break; |
break; |
|
case KEY_ESC: |
|
|
break; |
|
| 879 |
case KEY_F1: |
case KEY_F1: |
| 880 |
if (!show_help) // Not reentrant |
if (!show_help) // Not reentrant |
| 881 |
{ |
{ |
| 900 |
} |
} |
| 901 |
|
|
| 902 |
BBS_last_access_tm = time(0); |
BBS_last_access_tm = time(0); |
| 903 |
|
if (!input_ok) |
| 904 |
|
{ |
| 905 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 906 |
|
} |
| 907 |
} |
} |
| 908 |
|
|
| 909 |
continue; |
continue; |
| 921 |
len = 0; |
len = 0; |
| 922 |
} |
} |
| 923 |
|
|
| 924 |
memcpy(buffer, (const char *)p_editor_data->p_display_lines[line_current], (size_t)len); |
// memcpy(buffer, p_editor_data->p_display_lines[line_current], (size_t)len); |
| 925 |
buffer[len] = '\0'; |
// Replace '\033' with '*' |
| 926 |
|
p_str = p_editor_data->p_display_lines[line_current]; |
| 927 |
|
for (i = 0, j = 0; i < len; i++) |
| 928 |
|
{ |
| 929 |
|
if (p_str[i] == '\033') |
| 930 |
|
{ |
| 931 |
|
memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1); |
| 932 |
|
j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1); |
| 933 |
|
} |
| 934 |
|
else |
| 935 |
|
{ |
| 936 |
|
buffer[j] = p_str[i]; |
| 937 |
|
j++; |
| 938 |
|
} |
| 939 |
|
} |
| 940 |
|
buffer[j] = '\0'; |
| 941 |
|
|
| 942 |
moveto(screen_current_row, 0); |
moveto(screen_current_row, 0); |
| 943 |
clrtoeol(); |
clrtoeol(); |