| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#include "editor.h" |
|
| 17 |
#include "bbs.h" |
#include "bbs.h" |
| 18 |
|
#include "common.h" |
| 19 |
|
#include "editor.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
#include "log.h" |
#include "log.h" |
| 22 |
#include "common.h" |
#include "memory_pool.h" |
| 23 |
#include "str_process.h" |
#include "str_process.h" |
| 24 |
#include <stdlib.h> |
#include <stdlib.h> |
| 25 |
|
#include <string.h> |
| 26 |
#include <sys/param.h> |
#include <sys/param.h> |
|
#include <strings.h> |
|
| 27 |
|
|
| 28 |
#define _POSIX_C_SOURCE 200809L |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
| 29 |
#include <string.h> |
#define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000 |
| 30 |
|
#define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1) |
| 31 |
|
|
| 32 |
|
static MEMORY_POOL *p_mp_data_line; |
| 33 |
|
static MEMORY_POOL *p_mp_editor_data; |
| 34 |
|
|
| 35 |
|
int editor_memory_pool_init(void) |
| 36 |
|
{ |
| 37 |
|
if (p_mp_data_line != NULL || p_mp_editor_data != NULL) |
| 38 |
|
{ |
| 39 |
|
log_error("Editor mem pool already initialized\n"); |
| 40 |
|
return -1; |
| 41 |
|
} |
| 42 |
|
|
| 43 |
|
p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT); |
| 44 |
|
if (p_mp_data_line == NULL) |
| 45 |
|
{ |
| 46 |
|
log_error("Memory pool init error\n"); |
| 47 |
|
return -2; |
| 48 |
|
} |
| 49 |
|
|
| 50 |
|
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
| 51 |
|
if (p_mp_data_line == NULL) |
| 52 |
|
{ |
| 53 |
|
log_error("Memory pool init error\n"); |
| 54 |
|
return -3; |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
return 0; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
void editor_memory_pool_cleanup(void) |
| 61 |
|
{ |
| 62 |
|
if (p_mp_data_line != NULL) |
| 63 |
|
{ |
| 64 |
|
memory_pool_cleanup(p_mp_data_line); |
| 65 |
|
p_mp_data_line = NULL; |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
if (p_mp_editor_data != NULL) |
| 69 |
|
{ |
| 70 |
|
memory_pool_cleanup(p_mp_editor_data); |
| 71 |
|
p_mp_editor_data = NULL; |
| 72 |
|
} |
| 73 |
|
} |
| 74 |
|
|
| 75 |
EDITOR_DATA *editor_data_load(const char *p_data) |
EDITOR_DATA *editor_data_load(const char *p_data) |
| 76 |
{ |
{ |
| 77 |
EDITOR_DATA *p_editor_data; |
EDITOR_DATA *p_editor_data; |
| 78 |
char *p_data_line = NULL; |
char *p_data_line = NULL; |
| 79 |
long line_offsets[MAX_EDITOR_DATA_LINES]; |
long line_offsets[MAX_EDITOR_DATA_LINES + 1]; |
| 80 |
long current_data_line_length = 0; |
long current_data_line_length = 0; |
| 81 |
long i, j; |
long i; |
| 82 |
|
|
| 83 |
if (p_data == NULL) |
if (p_data == NULL) |
| 84 |
{ |
{ |
| 86 |
return NULL; |
return NULL; |
| 87 |
} |
} |
| 88 |
|
|
| 89 |
p_editor_data = malloc(sizeof(EDITOR_DATA)); |
p_editor_data = memory_pool_alloc(p_mp_editor_data); |
| 90 |
if (p_editor_data == NULL) |
if (p_editor_data == NULL) |
| 91 |
{ |
{ |
| 92 |
log_error("malloc(EDITOR_DATA) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 93 |
return NULL; |
return NULL; |
| 94 |
} |
} |
| 95 |
|
|
| 96 |
p_editor_data->data_line_total = 0; |
p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES + 1, 0); |
|
p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES); |
|
| 97 |
|
|
| 98 |
for (i = 0; i < p_editor_data->display_line_total; i++) |
for (i = 0; i < p_editor_data->display_line_total; i++) |
| 99 |
{ |
{ |
| 103 |
current_data_line_length + p_editor_data->display_line_lengths[i] + 1 > MAX_EDITOR_DATA_LINE_LENGTH || |
current_data_line_length + p_editor_data->display_line_lengths[i] + 1 > MAX_EDITOR_DATA_LINE_LENGTH || |
| 104 |
(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')) |
| 105 |
{ |
{ |
|
if (p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES) |
|
|
{ |
|
|
log_error("Append line error, data_line_total(%ld) reach limit(%d)\n", p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES); |
|
|
return NULL; |
|
|
} |
|
|
|
|
| 106 |
// Allocate new data line |
// Allocate new data line |
| 107 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 108 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 109 |
{ |
{ |
| 110 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i); |
log_error("memory_pool_alloc() error: i = %d\n", i); |
| 111 |
// Cleanup |
// Cleanup |
| 112 |
for (j = p_editor_data->data_line_total - 1; j >= 0; j--) |
editor_data_cleanup(p_editor_data); |
|
{ |
|
|
free(p_editor_data->p_data_lines[j]); |
|
|
} |
|
|
free(p_editor_data); |
|
| 113 |
return NULL; |
return NULL; |
| 114 |
} |
} |
|
p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; |
|
|
(p_editor_data->data_line_total)++; |
|
| 115 |
|
|
| 116 |
p_editor_data->p_display_lines[i] = p_data_line; |
p_editor_data->p_display_lines[i] = p_data_line; |
| 117 |
current_data_line_length = 0; |
current_data_line_length = 0; |
| 123 |
|
|
| 124 |
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]); |
| 125 |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
| 126 |
|
|
| 127 |
|
// Trim \n from last line |
| 128 |
|
if (i + 1 == p_editor_data->display_line_total && |
| 129 |
|
p_editor_data->display_line_lengths[i] > 0 && |
| 130 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
| 131 |
|
{ |
| 132 |
|
p_editor_data->display_line_lengths[i]--; |
| 133 |
|
current_data_line_length--; |
| 134 |
|
} |
| 135 |
p_data_line[current_data_line_length] = '\0'; |
p_data_line[current_data_line_length] = '\0'; |
| 136 |
} |
} |
| 137 |
|
|
| 138 |
bzero(p_editor_data->p_data_lines + p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES - (size_t)p_editor_data->data_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); |
|
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); |
|
| 139 |
|
|
| 140 |
return p_editor_data; |
return p_editor_data; |
| 141 |
} |
} |
| 171 |
|
|
| 172 |
void editor_data_cleanup(EDITOR_DATA *p_editor_data) |
void editor_data_cleanup(EDITOR_DATA *p_editor_data) |
| 173 |
{ |
{ |
| 174 |
|
char *p_data_line = NULL; |
| 175 |
long i; |
long i; |
| 176 |
|
|
| 177 |
if (p_editor_data == NULL) |
if (p_editor_data == NULL) |
| 179 |
return; |
return; |
| 180 |
} |
} |
| 181 |
|
|
| 182 |
for (i = p_editor_data->data_line_total - 1; i >= 0; i--) |
for (i = 0; i < p_editor_data->display_line_total; i++) |
| 183 |
|
{ |
| 184 |
|
if (p_data_line == NULL) |
| 185 |
|
{ |
| 186 |
|
p_data_line = p_editor_data->p_display_lines[i]; |
| 187 |
|
} |
| 188 |
|
|
| 189 |
|
if (p_editor_data->display_line_lengths[i] > 0 && |
| 190 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
| 191 |
|
{ |
| 192 |
|
memory_pool_free(p_mp_data_line, p_data_line); |
| 193 |
|
p_data_line = NULL; |
| 194 |
|
} |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
if (p_data_line != NULL) |
| 198 |
{ |
{ |
| 199 |
free(p_editor_data->p_data_lines[i]); |
memory_pool_free(p_mp_data_line, p_data_line); |
|
p_editor_data->p_data_lines[i] = NULL; |
|
| 200 |
} |
} |
| 201 |
|
|
| 202 |
free(p_editor_data); |
memory_pool_free(p_mp_editor_data, p_editor_data); |
| 203 |
} |
} |
| 204 |
|
|
| 205 |
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, |
| 213 |
long last_display_line; // of data line |
long last_display_line; // of data line |
| 214 |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 215 |
long split_line_total; |
long split_line_total; |
| 216 |
long i, j; |
long i; |
| 217 |
|
int len; |
| 218 |
|
int eol; |
| 219 |
|
int display_len; |
| 220 |
|
|
| 221 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 222 |
{ |
{ |
| 224 |
return -1; |
return -1; |
| 225 |
} |
} |
| 226 |
|
|
| 227 |
|
// Validate str |
| 228 |
|
if ((str_len == 1 && str[0] <= 0) || |
| 229 |
|
(str_len == 2 && (str[0] >= 0 || str[1] >= 0))) |
| 230 |
|
{ |
| 231 |
|
log_error("Invalid input str, len=%d\n", str_len); |
| 232 |
|
return -2; |
| 233 |
|
} |
| 234 |
|
|
| 235 |
// Get accurate offset of first character of CJK at offset position |
// Get accurate offset of first character of CJK at offset position |
| 236 |
for (i = 0; i < offset; i++) |
for (i = 0; i < offset; i++) |
| 237 |
{ |
{ |
| 238 |
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 |
| 239 |
{ |
{ |
| 240 |
i++; |
i++; |
| 241 |
} |
} |
| 276 |
// Split current data line if over-length |
// Split current data line if over-length |
| 277 |
if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
| 278 |
{ |
{ |
| 279 |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES || p_editor_data->data_line_total >= MAX_EDITOR_DATA_LINES) |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
| 280 |
{ |
{ |
| 281 |
log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n", |
#ifdef _DEBUG |
| 282 |
p_editor_data->display_line_total, p_editor_data->data_line_total, MAX_EDITOR_DATA_LINES); |
log_error("Split line error, display_line_total(%ld) reach limit(%d)\n", |
| 283 |
|
p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
| 284 |
|
#endif |
| 285 |
|
|
| 286 |
return -2; |
return -2; |
| 287 |
} |
} |
| 288 |
|
|
| 289 |
// Allocate new data line |
// Allocate new data line |
| 290 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 291 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 292 |
{ |
{ |
| 293 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 294 |
return -2; |
return -2; |
| 295 |
} |
} |
|
p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; |
|
|
(p_editor_data->data_line_total)++; |
|
| 296 |
|
|
| 297 |
if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
| 298 |
{ |
{ |
| 362 |
} |
} |
| 363 |
|
|
| 364 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 365 |
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); |
| 366 |
|
|
| 367 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 368 |
{ |
{ |
| 371 |
// Insert blank display line after last_display_line |
// Insert blank display line after last_display_line |
| 372 |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
| 373 |
{ |
{ |
| 374 |
|
#ifdef _DEBUG |
| 375 |
log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
log_error("display_line_total over limit %d >= %d\n", p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
| 376 |
return -3; |
#endif |
| 377 |
} |
|
| 378 |
for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) |
// Terminate prior display line with \n, to avoid error on cleanup |
| 379 |
{ |
if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0) |
| 380 |
p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
{ |
| 381 |
p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; |
len = split_line(p_editor_data->p_display_lines[display_line + i - 1], SCREEN_COLS - 1, &eol, &display_len, 0); |
| 382 |
|
p_editor_data->p_display_lines[display_line + i - 1][len] = '\n'; |
| 383 |
|
p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0'; |
| 384 |
|
p_editor_data->display_line_lengths[display_line + i - 1] = len + 1; |
| 385 |
|
} |
| 386 |
|
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
| 387 |
|
{ |
| 388 |
|
*p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1; |
| 389 |
|
} |
| 390 |
|
break; |
| 391 |
} |
} |
| 392 |
|
|
| 393 |
|
// for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) |
| 394 |
|
// { |
| 395 |
|
// p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
| 396 |
|
// p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; |
| 397 |
|
// } |
| 398 |
|
memmove(p_editor_data->p_display_lines + last_display_line + 2, |
| 399 |
|
p_editor_data->p_display_lines + last_display_line + 1, |
| 400 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 401 |
|
sizeof(p_editor_data->p_display_lines[last_display_line + 1])); |
| 402 |
|
memmove(p_editor_data->display_line_lengths + last_display_line + 2, |
| 403 |
|
p_editor_data->display_line_lengths + last_display_line + 1, |
| 404 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 405 |
|
sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); |
| 406 |
|
|
| 407 |
last_display_line++; |
last_display_line++; |
| 408 |
|
*p_last_updated_line = p_editor_data->display_line_total; |
| 409 |
(p_editor_data->display_line_total)++; |
(p_editor_data->display_line_total)++; |
| 410 |
} |
} |
| 411 |
|
|
| 424 |
|
|
| 425 |
*p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line); |
*p_last_updated_line = MAX(display_line + MIN(i, split_line_total - 1), *p_last_updated_line); |
| 426 |
|
|
| 427 |
if (*p_offset > p_editor_data->display_line_lengths[*p_display_line] || |
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
|
(*p_offset > 0 && *p_offset == p_editor_data->display_line_lengths[*p_display_line] && |
|
|
p_editor_data->p_display_lines[*p_display_line][*p_offset - 1] == '\n')) |
|
| 428 |
{ |
{ |
| 429 |
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
if (*p_display_line + 1 < p_editor_data->display_line_total) |
| 430 |
(*p_display_line)++; |
{ |
| 431 |
|
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
| 432 |
|
(*p_display_line)++; |
| 433 |
|
} |
| 434 |
|
} |
| 435 |
|
|
| 436 |
if (*p_display_line >= p_editor_data->display_line_total) |
// Prevent the last display line from being over-length |
| 437 |
|
if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES) |
| 438 |
|
{ |
| 439 |
|
len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0); |
| 440 |
|
p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0'; |
| 441 |
|
p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len; |
| 442 |
|
if (*p_display_line + 1 >= p_editor_data->display_line_total) |
| 443 |
{ |
{ |
| 444 |
log_error("*p_display_line(%d) >= display_line_total(%d)\n", *p_display_line, p_editor_data->display_line_total); |
*p_offset = MIN(*p_offset, len); |
| 445 |
|
*p_display_line = p_editor_data->display_line_total - 1; |
| 446 |
} |
} |
| 447 |
} |
} |
| 448 |
|
|
| 449 |
return 0; |
return 0; |
| 450 |
} |
} |
| 451 |
|
|
| 452 |
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, |
| 453 |
long *p_last_updated_line) |
long *p_last_updated_line) |
| 454 |
{ |
{ |
| 455 |
|
long display_line = *p_display_line; |
| 456 |
|
long offset = *p_offset; |
| 457 |
char *p_data_line = NULL; |
char *p_data_line = NULL; |
| 458 |
long len_data_line; |
long len_data_line; |
| 459 |
long offset_data_line; |
long offset_data_line; |
| 472 |
// Get accurate offset of first character of CJK at offset position |
// Get accurate offset of first character of CJK at offset position |
| 473 |
for (i = 0; i < offset; i++) |
for (i = 0; i < offset; i++) |
| 474 |
{ |
{ |
| 475 |
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 |
| 476 |
{ |
{ |
| 477 |
i++; |
i++; |
| 478 |
} |
} |
| 510 |
} |
} |
| 511 |
} |
} |
| 512 |
|
|
| 513 |
|
if (offset_data_line >= len_data_line) // end-of-line |
| 514 |
|
{ |
| 515 |
|
return 0; |
| 516 |
|
} |
| 517 |
|
|
| 518 |
// Check str to be deleted |
// Check str to be deleted |
| 519 |
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) |
| 520 |
{ |
{ |
| 521 |
str_len = 1; |
str_len = 1; |
| 522 |
} |
} |
| 523 |
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 |
| 524 |
{ |
{ |
| 525 |
str_len = 2; |
str_len = 2; |
| 526 |
} |
} |
| 527 |
else |
else |
| 528 |
{ |
{ |
| 529 |
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", |
| 530 |
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]); |
|
| 531 |
str_len = 1; |
str_len = 1; |
| 532 |
} |
} |
| 533 |
|
|
| 537 |
{ |
{ |
| 538 |
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) |
| 539 |
{ |
{ |
|
log_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total); |
|
| 540 |
return 0; |
return 0; |
| 541 |
} |
} |
| 542 |
|
|
| 556 |
|
|
| 557 |
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 |
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 |
| 558 |
{ |
{ |
|
log_common("Debug: No enough buffer to merge with next data line: %ld > %ld\n", |
|
|
offset_data_line + len_data_line + 1, MAX_EDITOR_DATA_LINE_LENGTH); |
|
| 559 |
return 0; |
return 0; |
| 560 |
} |
} |
| 561 |
|
|
| 564 |
p_data_line[offset_data_line + len_data_line] = '\0'; |
p_data_line[offset_data_line + len_data_line] = '\0'; |
| 565 |
|
|
| 566 |
// Recycle next data line |
// Recycle next data line |
| 567 |
// TODO: 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]); |
| 568 |
} |
} |
| 569 |
else |
else |
| 570 |
{ |
{ |
| 578 |
split_line_total = last_display_line - display_line + 2; |
split_line_total = last_display_line - display_line + 2; |
| 579 |
|
|
| 580 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 581 |
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); |
| 582 |
|
|
| 583 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 584 |
{ |
{ |
| 597 |
|
|
| 598 |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
| 599 |
|
|
| 600 |
if (display_line + i < last_display_line) |
if (*p_last_updated_line < last_display_line) |
| 601 |
{ |
{ |
| 602 |
// Remove redundant display line after last_display_line |
// Remove redundant display line after last_display_line |
| 603 |
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++) |
| 604 |
{ |
// { |
| 605 |
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]; |
| 606 |
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]; |
| 607 |
} |
// } |
| 608 |
|
memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1, |
| 609 |
(p_editor_data->display_line_total) -= (last_display_line - (display_line + i)); |
p_editor_data->p_display_lines + last_display_line + 1, |
| 610 |
last_display_line = display_line + i; |
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 611 |
|
sizeof(p_editor_data->p_display_lines[last_display_line + 1])); |
| 612 |
*p_last_updated_line = p_editor_data->display_line_total - 1; |
memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1, |
| 613 |
|
p_editor_data->display_line_lengths + last_display_line + 1, |
| 614 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 615 |
|
sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); |
| 616 |
|
|
| 617 |
|
j = p_editor_data->display_line_total; |
| 618 |
|
(p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line); |
| 619 |
|
*p_last_updated_line = MAX(j - 1, *p_last_updated_line); |
| 620 |
} |
} |
| 621 |
|
|
| 622 |
|
// Return real offset |
| 623 |
|
*p_offset = offset; |
| 624 |
|
|
| 625 |
return str_len; |
return str_len; |
| 626 |
} |
} |
| 627 |
|
|
| 631 |
{ |
{ |
| 632 |
case 0: // Set msg |
case 0: // Set msg |
| 633 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 634 |
"| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |"); |
"| Í˳ö[\033[32mCtrl-W\033[33m] |"); |
| 635 |
|
break; |
| 636 |
|
case KEY_CSI: |
| 637 |
|
*p_key = KEY_ESC; |
| 638 |
break; |
break; |
| 639 |
} |
} |
| 640 |
|
|
| 650 |
char input_str[4]; |
char input_str[4]; |
| 651 |
int str_len = 0; |
int str_len = 0; |
| 652 |
int input_ok; |
int input_ok; |
|
int screen_current_row; |
|
| 653 |
const int screen_begin_row = 1; |
const int screen_begin_row = 1; |
| 654 |
int screen_end_row = SCREEN_ROWS - 1; |
const int screen_row_total = SCREEN_ROWS - screen_begin_row; |
| 655 |
const int screen_row_total = screen_end_row - screen_begin_row + 1; |
int output_current_row = screen_begin_row; |
| 656 |
|
int output_end_row = SCREEN_ROWS - 1; |
| 657 |
long int line_current = 0; |
long int line_current = 0; |
| 658 |
long int len; |
long int len; |
| 659 |
int loop; |
int loop; |
| 664 |
int scroll_rows; |
int scroll_rows; |
| 665 |
long last_updated_line = 0; |
long last_updated_line = 0; |
| 666 |
int key_insert = 1; |
int key_insert = 1; |
| 667 |
int i; |
int i, j; |
| 668 |
|
char *p_str; |
| 669 |
|
|
| 670 |
screen_current_row = screen_begin_row; |
clrline(output_current_row, SCREEN_ROWS); |
|
clrline(screen_begin_row, SCREEN_ROWS); |
|
| 671 |
|
|
| 672 |
// update msg_ext with extended key handler |
// update msg_ext with extended key handler |
| 673 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 678 |
loop = 1; |
loop = 1; |
| 679 |
while (!SYS_server_exit && loop) |
while (!SYS_server_exit && loop) |
| 680 |
{ |
{ |
| 681 |
if (line_current >= p_editor_data->display_line_total || screen_current_row > screen_end_row) |
if (line_current >= p_editor_data->display_line_total || output_current_row > output_end_row) |
| 682 |
{ |
{ |
| 683 |
ctx.line_cursor = line_current - screen_current_row + row_pos + 1; |
ctx.line_cursor = line_current - output_current_row + row_pos + 1; |
| 684 |
|
|
| 685 |
snprintf(buffer, sizeof(buffer), |
snprintf(buffer, sizeof(buffer), |
| 686 |
"\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] " |
"\033[1;44;33m[\033[32m%ld\033[33m;\033[32m%ld\033[33m] " |
| 688 |
"%s", |
"%s", |
| 689 |
row_pos, col_pos, |
row_pos, col_pos, |
| 690 |
ctx.line_cursor, p_editor_data->display_line_total, |
ctx.line_cursor, p_editor_data->display_line_total, |
| 691 |
key_insert ? "²åÈë" : "¸Äд", |
key_insert ? "²åÈë" : "Ìæ»»", |
| 692 |
ctx.msg); |
ctx.msg); |
| 693 |
|
|
| 694 |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1); |
| 695 |
for (; display_len < SCREEN_COLS; display_len++) |
for (; display_len < SCREEN_COLS; display_len++) |
| 696 |
{ |
{ |
| 697 |
buffer[len++] = ' '; |
buffer[len++] = ' '; |
| 705 |
moveto((int)row_pos, (int)col_pos); |
moveto((int)row_pos, (int)col_pos); |
| 706 |
iflush(); |
iflush(); |
| 707 |
|
|
| 708 |
input_ok = 0; |
str_len = 0; |
| 709 |
while (!SYS_server_exit && !input_ok) |
ch = igetch_t(MAX_DELAY_TIME); |
| 710 |
|
while (!SYS_server_exit) |
| 711 |
{ |
{ |
|
ch = igetch_t(MAX_DELAY_TIME); |
|
|
input_ok = 1; |
|
|
|
|
| 712 |
// extended key handler |
// extended key handler |
| 713 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 714 |
{ |
{ |
| 720 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 721 |
str_len++; |
str_len++; |
| 722 |
} |
} |
| 723 |
else |
else if (str_len > 0) |
| 724 |
{ |
{ |
| 725 |
|
log_error("Received %d character over 127 followed by character less than 127\n", str_len); |
| 726 |
str_len = 0; |
str_len = 0; |
| 727 |
} |
} |
| 728 |
|
|
| 729 |
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 |
| 730 |
|
ch == CR || ch == KEY_ESC) // Special character |
| 731 |
{ |
{ |
| 732 |
if (str_len == 0) |
BBS_last_access_tm = time(NULL); |
| 733 |
|
|
| 734 |
|
if (str_len == 0) // ch >= 32 && ch < 127 |
| 735 |
{ |
{ |
| 736 |
input_str[0] = (char)ch; |
input_str[0] = (char)ch; |
| 737 |
str_len = 1; |
str_len = 1; |
| 738 |
} |
} |
| 739 |
|
|
| 740 |
last_updated_line = line_current; |
display_line_in = line_current - output_current_row + row_pos; |
|
display_line_in = line_current - screen_current_row + row_pos; |
|
| 741 |
offset_in = col_pos - 1; |
offset_in = col_pos - 1; |
| 742 |
display_line_out = display_line_in; |
display_line_out = display_line_in; |
| 743 |
offset_out = offset_in; |
offset_out = offset_in; |
| 744 |
|
|
| 745 |
|
last_updated_line = display_line_in; |
| 746 |
|
|
| 747 |
if (!key_insert) // overwrite |
if (!key_insert) // overwrite |
| 748 |
{ |
{ |
| 749 |
if (editor_data_delete(p_editor_data, display_line_in, offset_in, |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 750 |
&last_updated_line) < 0) |
&last_updated_line) < 0) |
| 751 |
{ |
{ |
| 752 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 756 |
if (editor_data_insert(p_editor_data, &display_line_out, &offset_out, |
if (editor_data_insert(p_editor_data, &display_line_out, &offset_out, |
| 757 |
input_str, str_len, &last_updated_line) < 0) |
input_str, str_len, &last_updated_line) < 0) |
| 758 |
{ |
{ |
| 759 |
log_error("editor_data_insert(%s) error\n", input_str); |
log_error("editor_data_insert(str_len=%d) error\n", str_len); |
|
str_len = 0; |
|
| 760 |
} |
} |
| 761 |
else |
else |
| 762 |
{ |
{ |
| 763 |
str_len = 0; |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
| 764 |
|
line_current -= (output_current_row - row_pos); |
| 765 |
|
output_current_row = (int)row_pos; |
| 766 |
|
|
| 767 |
screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); |
scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row)); |
|
line_current -= (screen_current_row - row_pos); |
|
|
screen_current_row = (int)row_pos; |
|
|
|
|
|
scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row)); |
|
| 768 |
|
|
| 769 |
if (scroll_rows > 0) |
if (scroll_rows > 0) |
| 770 |
{ |
{ |
| 775 |
prints("\033[S"); // Scroll up 1 line |
prints("\033[S"); // Scroll up 1 line |
| 776 |
} |
} |
| 777 |
|
|
| 778 |
screen_current_row -= scroll_rows; |
output_current_row -= scroll_rows; |
| 779 |
if (screen_current_row < screen_begin_row) |
if (output_current_row < screen_begin_row) |
| 780 |
{ |
{ |
| 781 |
line_current += (screen_begin_row - screen_current_row); |
line_current += (screen_begin_row - output_current_row); |
| 782 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 783 |
} |
} |
| 784 |
row_pos = screen_end_row; |
row_pos = output_end_row; |
| 785 |
} |
} |
| 786 |
else // if (scroll_lines == 0) |
else // if (scroll_lines == 0) |
| 787 |
{ |
{ |
| 788 |
row_pos += (display_line_out - display_line_in); |
row_pos += (display_line_out - display_line_in); |
| 789 |
} |
} |
| 790 |
col_pos = offset_out + 1; |
col_pos = offset_out + 1; // Set col_pos to accurate pos |
| 791 |
|
} |
| 792 |
|
|
| 793 |
continue; |
if (display_line_out != display_line_in) // Output on line change |
| 794 |
|
{ |
| 795 |
|
break; |
| 796 |
} |
} |
| 797 |
|
|
| 798 |
|
ch = igetch(0); |
| 799 |
|
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input |
| 800 |
|
{ |
| 801 |
|
break; |
| 802 |
|
} |
| 803 |
|
|
| 804 |
|
str_len = 0; |
| 805 |
|
continue; |
| 806 |
} |
} |
| 807 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
| 808 |
{ |
{ |
| 809 |
|
BBS_last_access_tm = time(NULL); |
| 810 |
|
|
| 811 |
if (ch == BACKSPACE) |
if (ch == BACKSPACE) |
| 812 |
{ |
{ |
| 813 |
|
if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden |
| 814 |
|
{ |
| 815 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 816 |
|
continue; |
| 817 |
|
} |
| 818 |
|
|
| 819 |
col_pos--; |
col_pos--; |
| 820 |
if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0) |
if (col_pos > 1 && |
| 821 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0) // GBK |
| 822 |
|
{ |
| 823 |
|
col_pos--; |
| 824 |
|
} |
| 825 |
|
|
| 826 |
|
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
| 827 |
{ |
{ |
| 828 |
row_pos--; |
row_pos--; |
| 829 |
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 - output_current_row + row_pos]); |
| 830 |
} |
} |
| 831 |
} |
} |
| 832 |
|
|
| 833 |
if ((str_len = editor_data_delete(p_editor_data, line_current - screen_current_row + row_pos, col_pos - 1, |
display_line_in = line_current - output_current_row + row_pos; |
| 834 |
|
offset_in = col_pos - 1; |
| 835 |
|
display_line_out = display_line_in; |
| 836 |
|
offset_out = offset_in; |
| 837 |
|
|
| 838 |
|
if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 839 |
&last_updated_line)) < 0) |
&last_updated_line)) < 0) |
| 840 |
{ |
{ |
| 841 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 842 |
} |
} |
| 843 |
else |
else |
| 844 |
{ |
{ |
| 845 |
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 - screen_current_row + row_pos >= 0) |
|
|
{ |
|
|
row_pos--; |
|
|
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
|
|
} |
|
|
} |
|
|
} |
|
| 846 |
|
|
| 847 |
screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
| 848 |
line_current -= (screen_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 849 |
screen_current_row = (int)row_pos; |
output_current_row = (int)row_pos; |
| 850 |
|
|
| 851 |
if (screen_current_row < screen_begin_row) // row_pos <= 0 |
if (output_current_row < screen_begin_row) // row_pos <= 0 |
| 852 |
{ |
{ |
| 853 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 854 |
row_pos = screen_begin_row; |
row_pos = screen_begin_row; |
| 855 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 856 |
|
} |
| 857 |
|
|
| 858 |
|
// Exceed end |
| 859 |
|
if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total && |
| 860 |
|
p_editor_data->display_line_total > screen_row_total) |
| 861 |
|
{ |
| 862 |
|
scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) - |
| 863 |
|
(p_editor_data->display_line_total - screen_row_total)); |
| 864 |
|
|
| 865 |
|
line_current = p_editor_data->display_line_total - screen_row_total; |
| 866 |
|
row_pos += scroll_rows; |
| 867 |
|
output_current_row = screen_begin_row; |
| 868 |
|
output_end_row = SCREEN_ROWS - 1; |
| 869 |
} |
} |
| 870 |
|
|
| 871 |
|
clrline(output_current_row, output_end_row); |
| 872 |
|
} |
| 873 |
|
|
| 874 |
|
if (display_line_out != display_line_in) // Output on line change |
| 875 |
|
{ |
| 876 |
|
break; |
| 877 |
} |
} |
| 878 |
|
|
| 879 |
|
ch = igetch(0); |
| 880 |
|
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input |
| 881 |
|
{ |
| 882 |
|
break; |
| 883 |
|
} |
| 884 |
|
|
| 885 |
|
str_len = 0; |
| 886 |
continue; |
continue; |
| 887 |
} |
} |
| 888 |
|
|
| 889 |
|
input_ok = 1; |
| 890 |
switch (ch) |
switch (ch) |
| 891 |
{ |
{ |
| 892 |
case KEY_NULL: |
case KEY_NULL: |
| 893 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 894 |
goto cleanup; |
goto cleanup; |
| 895 |
case Ctrl('C'): |
case Ctrl('W'): |
| 896 |
loop = 0; |
loop = 0; |
| 897 |
break; |
break; |
| 898 |
|
case Ctrl('S'): // Start of line |
| 899 |
case KEY_CTRL_LEFT: |
case KEY_CTRL_LEFT: |
| 900 |
col_pos = 1; |
col_pos = 1; |
| 901 |
break; |
break; |
| 902 |
|
case Ctrl('E'): // End of line |
| 903 |
case KEY_CTRL_RIGHT: |
case KEY_CTRL_RIGHT: |
| 904 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
if (line_current - output_current_row + row_pos == p_editor_data->display_line_total - 1) // row_pos at end line |
| 905 |
|
{ |
| 906 |
|
// last display line does NOT have \n in the end |
| 907 |
|
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 908 |
|
break; |
| 909 |
|
} |
| 910 |
|
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]); |
| 911 |
break; |
break; |
| 912 |
|
case Ctrl('T'): // Top of screen |
| 913 |
case KEY_CTRL_UP: |
case KEY_CTRL_UP: |
| 914 |
row_pos = screen_begin_row; |
row_pos = screen_begin_row; |
| 915 |
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 - output_current_row + row_pos])); |
| 916 |
break; |
break; |
| 917 |
|
case Ctrl('B'): // Bottom of screen |
| 918 |
case KEY_CTRL_DOWN: |
case KEY_CTRL_DOWN: |
| 919 |
row_pos = SCREEN_ROWS - 1; |
if (p_editor_data->display_line_total < screen_row_total) |
| 920 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
{ |
| 921 |
|
row_pos = p_editor_data->display_line_total; |
| 922 |
|
} |
| 923 |
|
else |
| 924 |
|
{ |
| 925 |
|
row_pos = SCREEN_ROWS - 1; |
| 926 |
|
} |
| 927 |
|
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
| 928 |
|
{ |
| 929 |
|
// last display line does NOT have \n in the end |
| 930 |
|
col_pos = MIN(col_pos, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1); |
| 931 |
|
} |
| 932 |
|
else |
| 933 |
|
{ |
| 934 |
|
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - output_current_row + row_pos])); |
| 935 |
|
} |
| 936 |
break; |
break; |
| 937 |
case KEY_INS: |
case KEY_INS: |
| 938 |
key_insert = !key_insert; |
key_insert = !key_insert; |
| 940 |
case KEY_HOME: |
case KEY_HOME: |
| 941 |
row_pos = 1; |
row_pos = 1; |
| 942 |
col_pos = 1; |
col_pos = 1; |
| 943 |
if (line_current - screen_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |
| 944 |
{ |
{ |
| 945 |
break; |
break; |
| 946 |
} |
} |
| 947 |
line_current = 0; |
line_current = 0; |
| 948 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 949 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 950 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 951 |
break; |
break; |
| 952 |
case KEY_END: |
case KEY_END: |
| 953 |
if (p_editor_data->display_line_total < screen_row_total) |
if (p_editor_data->display_line_total < screen_row_total) |
| 954 |
{ |
{ |
| 955 |
row_pos = p_editor_data->display_line_total; |
row_pos = p_editor_data->display_line_total; |
| 956 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 957 |
break; |
break; |
| 958 |
} |
} |
| 959 |
line_current = p_editor_data->display_line_total - screen_row_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 960 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 961 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 962 |
row_pos = screen_row_total; |
row_pos = SCREEN_ROWS - 1; |
| 963 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 964 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 965 |
break; |
break; |
| 966 |
case KEY_LEFT: |
case KEY_LEFT: |
| 967 |
if (col_pos > 1) |
if (col_pos > 1) |
| 968 |
{ |
{ |
| 969 |
col_pos--; |
col_pos--; |
| 970 |
|
if (col_pos > 1 && |
| 971 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && |
| 972 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 2] < 0) // GBK |
| 973 |
|
{ |
| 974 |
|
col_pos--; |
| 975 |
|
} |
| 976 |
break; |
break; |
| 977 |
} |
} |
| 978 |
col_pos = SCREEN_COLS; // continue to KEY_UP |
col_pos = SCREEN_COLS; // continue to KEY_UP |
| 980 |
if (row_pos > screen_begin_row) |
if (row_pos > screen_begin_row) |
| 981 |
{ |
{ |
| 982 |
row_pos--; |
row_pos--; |
| 983 |
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 - output_current_row + row_pos])); |
| 984 |
break; |
break; |
| 985 |
} |
} |
| 986 |
if (line_current - screen_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |
| 987 |
{ |
{ |
| 988 |
col_pos = 1; |
col_pos = 1; |
| 989 |
break; |
break; |
| 990 |
} |
} |
| 991 |
line_current -= screen_current_row; |
line_current -= output_current_row; |
| 992 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 993 |
// screen_end_line = begin_line; |
// screen_end_line = begin_line; |
| 994 |
// prints("\033[T"); // Scroll down 1 line |
// prints("\033[T"); // Scroll down 1 line |
| 995 |
screen_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 |
| 996 |
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 - output_current_row + row_pos])); |
| 997 |
break; |
break; |
| 998 |
case KEY_SPACE: |
case KEY_SPACE: |
| 999 |
break; |
break; |
| 1000 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 1001 |
if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]) |
if (col_pos < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos]) |
| 1002 |
{ |
{ |
| 1003 |
|
if (p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos - 1] < 0 && |
| 1004 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][col_pos] < 0) // GBK |
| 1005 |
|
{ |
| 1006 |
|
col_pos++; |
| 1007 |
|
} |
| 1008 |
col_pos++; |
col_pos++; |
| 1009 |
break; |
break; |
| 1010 |
} |
} |
| 1013 |
if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total)) |
if (row_pos < MIN(screen_row_total, p_editor_data->display_line_total)) |
| 1014 |
{ |
{ |
| 1015 |
row_pos++; |
row_pos++; |
| 1016 |
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 - output_current_row + row_pos])); |
| 1017 |
break; |
break; |
| 1018 |
} |
} |
| 1019 |
if (line_current + (screen_row_total - (screen_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 |
| 1020 |
{ |
{ |
| 1021 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
// last display line does NOT have \n in the end |
| 1022 |
|
col_pos = p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] + 1; |
| 1023 |
break; |
break; |
| 1024 |
} |
} |
| 1025 |
line_current += (screen_row_total - (screen_current_row - screen_begin_row)); |
line_current += (screen_row_total - (output_current_row - screen_begin_row)); |
| 1026 |
screen_current_row = screen_row_total; |
output_current_row = screen_row_total; |
| 1027 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1028 |
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 - output_current_row + row_pos])); |
| 1029 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 1030 |
clrtoeol(); |
clrtoeol(); |
| 1031 |
prints("\033[S"); // Scroll up 1 line |
prints("\033[S"); // Scroll up 1 line |
| 1032 |
break; |
break; |
| 1033 |
case KEY_PGUP: |
case KEY_PGUP: |
| 1034 |
if (line_current - screen_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |
| 1035 |
{ |
{ |
| 1036 |
break; |
break; |
| 1037 |
} |
} |
| 1038 |
line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row)); |
line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row)); |
| 1039 |
if (line_current < 0) |
if (line_current < 0) |
| 1040 |
{ |
{ |
| 1041 |
line_current = 0; |
line_current = 0; |
| 1042 |
} |
} |
| 1043 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1044 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1045 |
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 - output_current_row + row_pos])); |
| 1046 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 1047 |
break; |
break; |
| 1048 |
case KEY_PGDN: |
case KEY_PGDN: |
| 1049 |
if (line_current + screen_row_total - (screen_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 |
| 1050 |
{ |
{ |
| 1051 |
break; |
break; |
| 1052 |
} |
} |
| 1053 |
line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row); |
line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row); |
| 1054 |
if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display |
if (line_current + screen_row_total > p_editor_data->display_line_total) // No enough lines to display |
| 1055 |
{ |
{ |
| 1056 |
line_current = p_editor_data->display_line_total - screen_row_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 1057 |
} |
} |
| 1058 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1059 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1060 |
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 - output_current_row + row_pos])); |
| 1061 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
|
break; |
|
|
case KEY_ESC: |
|
| 1062 |
break; |
break; |
| 1063 |
case KEY_F1: |
case KEY_F1: |
| 1064 |
if (!show_help) // Not reentrant |
if (!show_help) // Not reentrant |
| 1071 |
show_help = 1; |
show_help = 1; |
| 1072 |
case KEY_F5: |
case KEY_F5: |
| 1073 |
// Refresh after display help information |
// Refresh after display help information |
| 1074 |
line_current -= (screen_current_row - screen_begin_row); |
line_current -= (output_current_row - screen_begin_row); |
| 1075 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1076 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1077 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 1078 |
break; |
break; |
| 1079 |
case 0: // Refresh bottom line |
case 0: // Refresh bottom line |
| 1080 |
break; |
break; |
| 1083 |
break; |
break; |
| 1084 |
} |
} |
| 1085 |
|
|
| 1086 |
BBS_last_access_tm = time(0); |
BBS_last_access_tm = time(NULL); |
| 1087 |
|
|
| 1088 |
|
if (input_ok) |
| 1089 |
|
{ |
| 1090 |
|
break; |
| 1091 |
|
} |
| 1092 |
|
|
| 1093 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 1094 |
} |
} |
| 1095 |
|
|
| 1096 |
continue; |
continue; |
| 1108 |
len = 0; |
len = 0; |
| 1109 |
} |
} |
| 1110 |
|
|
| 1111 |
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); |
| 1112 |
buffer[len] = '\0'; |
// Replace '\033' with '*' |
| 1113 |
|
p_str = p_editor_data->p_display_lines[line_current]; |
| 1114 |
|
for (i = 0, j = 0; i < len; i++) |
| 1115 |
|
{ |
| 1116 |
|
if (p_str[i] == '\033') |
| 1117 |
|
{ |
| 1118 |
|
memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1); |
| 1119 |
|
j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1); |
| 1120 |
|
} |
| 1121 |
|
else |
| 1122 |
|
{ |
| 1123 |
|
buffer[j] = p_str[i]; |
| 1124 |
|
j++; |
| 1125 |
|
} |
| 1126 |
|
} |
| 1127 |
|
buffer[j] = '\0'; |
| 1128 |
|
|
| 1129 |
moveto(screen_current_row, 0); |
moveto(output_current_row, 0); |
| 1130 |
clrtoeol(); |
clrtoeol(); |
| 1131 |
prints("%s", buffer); |
prints("%s", buffer); |
| 1132 |
line_current++; |
line_current++; |
| 1133 |
screen_current_row++; |
output_current_row++; |
| 1134 |
} |
} |
| 1135 |
|
|
| 1136 |
cleanup: |
cleanup: |