| 20 |
#include "log.h" |
#include "log.h" |
| 21 |
#include "common.h" |
#include "common.h" |
| 22 |
#include "str_process.h" |
#include "str_process.h" |
| 23 |
|
#include "memory_pool.h" |
| 24 |
#include <stdlib.h> |
#include <stdlib.h> |
| 25 |
#include <sys/param.h> |
#include <sys/param.h> |
| 26 |
#include <strings.h> |
#include <strings.h> |
| 29 |
#include <string.h> |
#include <string.h> |
| 30 |
|
|
| 31 |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
| 32 |
|
#define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000 |
| 33 |
|
#define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1) |
| 34 |
|
|
| 35 |
|
static MEMORY_POOL *p_mp_data_line; |
| 36 |
|
static MEMORY_POOL *p_mp_editor_data; |
| 37 |
|
|
| 38 |
|
int editor_memory_pool_init(void) |
| 39 |
|
{ |
| 40 |
|
if (p_mp_data_line != NULL || p_mp_editor_data != NULL) |
| 41 |
|
{ |
| 42 |
|
log_error("Editor mem pool already initialized\n"); |
| 43 |
|
return -1; |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT); |
| 47 |
|
if (p_mp_data_line == NULL) |
| 48 |
|
{ |
| 49 |
|
log_error("Memory pool init error\n"); |
| 50 |
|
return -2; |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
| 54 |
|
if (p_mp_data_line == NULL) |
| 55 |
|
{ |
| 56 |
|
log_error("Memory pool init error\n"); |
| 57 |
|
return -3; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
return 0; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
void editor_memory_pool_cleanup(void) |
| 64 |
|
{ |
| 65 |
|
if (p_mp_data_line != NULL) |
| 66 |
|
{ |
| 67 |
|
memory_pool_cleanup(p_mp_data_line); |
| 68 |
|
p_mp_data_line = NULL; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
if (p_mp_editor_data != NULL) |
| 72 |
|
{ |
| 73 |
|
memory_pool_cleanup(p_mp_editor_data); |
| 74 |
|
p_mp_editor_data = NULL; |
| 75 |
|
} |
| 76 |
|
} |
| 77 |
|
|
| 78 |
EDITOR_DATA *editor_data_load(const char *p_data) |
EDITOR_DATA *editor_data_load(const char *p_data) |
| 79 |
{ |
{ |
| 89 |
return NULL; |
return NULL; |
| 90 |
} |
} |
| 91 |
|
|
| 92 |
p_editor_data = malloc(sizeof(EDITOR_DATA)); |
p_editor_data = memory_pool_alloc(p_mp_editor_data); |
| 93 |
if (p_editor_data == NULL) |
if (p_editor_data == NULL) |
| 94 |
{ |
{ |
| 95 |
log_error("malloc(EDITOR_DATA) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 96 |
return NULL; |
return NULL; |
| 97 |
} |
} |
| 98 |
|
|
| 107 |
(p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n')) |
(p_editor_data->display_line_lengths[i - 1] > 0 && p_data[line_offsets[i - 1] + p_editor_data->display_line_lengths[i - 1] - 1] == '\n')) |
| 108 |
{ |
{ |
| 109 |
// Allocate new data line |
// Allocate new data line |
| 110 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 111 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 112 |
{ |
{ |
| 113 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i); |
log_error("memory_pool_alloc() error: i = %d\n", i); |
| 114 |
// Cleanup |
// Cleanup |
| 115 |
editor_data_cleanup(p_editor_data); |
editor_data_cleanup(p_editor_data); |
| 116 |
return NULL; |
return NULL; |
| 126 |
|
|
| 127 |
memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); |
memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); |
| 128 |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
| 129 |
|
|
| 130 |
|
// Trim \n from last line |
| 131 |
|
if (i + 1 == p_editor_data->display_line_total && |
| 132 |
|
p_editor_data->display_line_lengths[i] > 0 && |
| 133 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
| 134 |
|
{ |
| 135 |
|
p_editor_data->display_line_lengths[i]--; |
| 136 |
|
current_data_line_length--; |
| 137 |
|
} |
| 138 |
p_data_line[current_data_line_length] = '\0'; |
p_data_line[current_data_line_length] = '\0'; |
| 139 |
} |
} |
| 140 |
|
|
| 192 |
if (p_editor_data->display_line_lengths[i] > 0 && |
if (p_editor_data->display_line_lengths[i] > 0 && |
| 193 |
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') |
| 194 |
{ |
{ |
| 195 |
free(p_data_line); |
memory_pool_free(p_mp_data_line, p_data_line); |
| 196 |
p_data_line = NULL; |
p_data_line = NULL; |
| 197 |
} |
} |
| 198 |
} |
} |
| 199 |
|
|
| 200 |
if (p_data_line != NULL) |
if (p_data_line != NULL) |
| 201 |
{ |
{ |
| 202 |
free(p_data_line); |
memory_pool_free(p_mp_data_line, p_data_line); |
| 203 |
} |
} |
| 204 |
|
|
| 205 |
free(p_editor_data); |
memory_pool_free(p_mp_editor_data, p_editor_data); |
| 206 |
} |
} |
| 207 |
|
|
| 208 |
int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
int editor_data_insert(EDITOR_DATA *p_editor_data, long *p_display_line, long *p_offset, |
| 279 |
} |
} |
| 280 |
|
|
| 281 |
// Allocate new data line |
// Allocate new data line |
| 282 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 283 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 284 |
{ |
{ |
| 285 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 286 |
return -2; |
return -2; |
| 287 |
} |
} |
| 288 |
|
|
| 404 |
|
|
| 405 |
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
| 406 |
{ |
{ |
| 407 |
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
if (*p_display_line + 1 < p_editor_data->display_line_total) |
|
|
|
|
if (*p_display_line + 1 >= p_editor_data->display_line_total) |
|
|
{ |
|
|
log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total); |
|
|
} |
|
|
else |
|
| 408 |
{ |
{ |
| 409 |
|
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
| 410 |
(*p_display_line)++; |
(*p_display_line)++; |
| 411 |
} |
} |
| 412 |
} |
} |
| 473 |
} |
} |
| 474 |
} |
} |
| 475 |
|
|
| 476 |
|
if (offset_data_line >= len_data_line) // end-of-line |
| 477 |
|
{ |
| 478 |
|
return 0; |
| 479 |
|
} |
| 480 |
|
|
| 481 |
// Check str to be deleted |
// Check str to be deleted |
| 482 |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
| 483 |
{ |
{ |
| 489 |
} |
} |
| 490 |
else |
else |
| 491 |
{ |
{ |
| 492 |
log_error("Some strange character at display_line %ld, offset %ld: %d %d %d %d\n", |
log_error("Some strange character at display_line %ld, offset %ld: %d %d\n", |
| 493 |
display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1], |
display_line, offset, p_data_line[offset_data_line], p_data_line[offset_data_line + 1]); |
|
p_data_line[offset_data_line + 2], p_data_line[offset_data_line + 3]); |
|
| 494 |
str_len = 1; |
str_len = 1; |
| 495 |
} |
} |
| 496 |
|
|
| 527 |
p_data_line[offset_data_line + len_data_line] = '\0'; |
p_data_line[offset_data_line + len_data_line] = '\0'; |
| 528 |
|
|
| 529 |
// Recycle next data line |
// Recycle next data line |
| 530 |
free(p_editor_data->p_display_lines[display_line + 1]); |
memory_pool_free(p_mp_data_line, p_editor_data->p_display_lines[display_line + 1]); |
| 531 |
} |
} |
| 532 |
else |
else |
| 533 |
{ |
{ |
| 560 |
|
|
| 561 |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
| 562 |
|
|
| 563 |
if (display_line + i < last_display_line) |
if (*p_last_updated_line < last_display_line) |
| 564 |
{ |
{ |
| 565 |
// Remove redundant display line after last_display_line |
// Remove redundant display line after last_display_line |
| 566 |
for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++) |
for (j = last_display_line + 1; j < p_editor_data->display_line_total; j++) |
| 567 |
{ |
{ |
| 568 |
p_editor_data->p_display_lines[j - (last_display_line - (display_line + i))] = p_editor_data->p_display_lines[j]; |
p_editor_data->p_display_lines[j - (last_display_line - *p_last_updated_line)] = p_editor_data->p_display_lines[j]; |
| 569 |
p_editor_data->display_line_lengths[j - (last_display_line - (display_line + i))] = p_editor_data->display_line_lengths[j]; |
p_editor_data->display_line_lengths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_lengths[j]; |
| 570 |
} |
} |
| 571 |
|
|
| 572 |
(p_editor_data->display_line_total) -= (last_display_line - (display_line + i)); |
j = p_editor_data->display_line_total; |
| 573 |
last_display_line = display_line + i; |
(p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line); |
| 574 |
|
*p_last_updated_line = MAX(j - 1, *p_last_updated_line); |
|
*p_last_updated_line = p_editor_data->display_line_total - 1; |
|
| 575 |
} |
} |
| 576 |
|
|
| 577 |
return str_len; |
return str_len; |
| 583 |
{ |
{ |
| 584 |
case 0: // Set msg |
case 0: // Set msg |
| 585 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 586 |
"| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |"); |
"| Í˳ö[\033[32mCtrl-W\033[33m] | °ïÖú[\033[32mh\033[33m] |"); |
| 587 |
break; |
break; |
| 588 |
} |
} |
| 589 |
|
|
| 746 |
{ |
{ |
| 747 |
if (ch == BACKSPACE) |
if (ch == BACKSPACE) |
| 748 |
{ |
{ |
| 749 |
|
if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden |
| 750 |
|
{ |
| 751 |
|
input_ok = 0; |
| 752 |
|
continue; |
| 753 |
|
} |
| 754 |
|
|
| 755 |
col_pos--; |
col_pos--; |
| 756 |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
| 757 |
{ |
{ |
| 802 |
row_pos += scroll_rows; |
row_pos += scroll_rows; |
| 803 |
output_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 804 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
|
clrline(output_current_row, SCREEN_ROWS); |
|
| 805 |
} |
} |
| 806 |
|
|
| 807 |
|
clrline(output_current_row, output_end_row); |
| 808 |
} |
} |
| 809 |
|
|
| 810 |
continue; |
continue; |
| 815 |
case KEY_NULL: |
case KEY_NULL: |
| 816 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 817 |
goto cleanup; |
goto cleanup; |
| 818 |
case Ctrl('C'): |
case Ctrl('W'): |
| 819 |
loop = 0; |
loop = 0; |
| 820 |
break; |
break; |
| 821 |
case Ctrl('S'): // Start of line |
case Ctrl('S'): // Start of line |
| 833 |
break; |
break; |
| 834 |
case Ctrl('B'): // Bottom of screen |
case Ctrl('B'): // Bottom of screen |
| 835 |
case KEY_CTRL_DOWN: |
case KEY_CTRL_DOWN: |
| 836 |
row_pos = SCREEN_ROWS - 1; |
if (p_editor_data->display_line_total < screen_row_total) |
| 837 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
{ |
| 838 |
|
row_pos = p_editor_data->display_line_total; |
| 839 |
|
} |
| 840 |
|
else |
| 841 |
|
{ |
| 842 |
|
row_pos = SCREEN_ROWS - 1; |
| 843 |
|
} |
| 844 |
|
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
| 845 |
|
{ |
| 846 |
|
// last display line does NOT have \n in the end |
| 847 |
|
col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1); |
| 848 |
|
} |
| 849 |
|
else |
| 850 |
|
{ |
| 851 |
|
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
| 852 |
|
} |
| 853 |
break; |
break; |
| 854 |
case KEY_INS: |
case KEY_INS: |
| 855 |
key_insert = !key_insert; |
key_insert = !key_insert; |
| 870 |
if (p_editor_data->display_line_total < screen_row_total) |
if (p_editor_data->display_line_total < screen_row_total) |
| 871 |
{ |
{ |
| 872 |
row_pos = p_editor_data->display_line_total; |
row_pos = p_editor_data->display_line_total; |
| 873 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 874 |
break; |
break; |
| 875 |
} |
} |
| 876 |
line_current = p_editor_data->display_line_total - screen_row_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 877 |
output_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 878 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 879 |
row_pos = SCREEN_ROWS - 1; |
row_pos = SCREEN_ROWS - 1; |
| 880 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 881 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 882 |
break; |
break; |
| 883 |
case KEY_LEFT: |
case KEY_LEFT: |
| 924 |
} |
} |
| 925 |
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
| 926 |
{ |
{ |
| 927 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
// last display line does NOT have \n in the end |
| 928 |
|
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 929 |
break; |
break; |
| 930 |
} |
} |
| 931 |
line_current += (screen_row_total - (output_current_row - screen_begin_row)); |
line_current += (screen_row_total - (output_current_row - screen_begin_row)); |