| 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 "login.h" |
| 23 |
|
#include "memory_pool.h" |
| 24 |
#include "str_process.h" |
#include "str_process.h" |
| 25 |
#include <stdlib.h> |
#include <stdlib.h> |
| 26 |
|
#include <string.h> |
| 27 |
#include <sys/param.h> |
#include <sys/param.h> |
|
#include <strings.h> |
|
| 28 |
|
|
| 29 |
#define _POSIX_C_SOURCE 200809L |
#define EDITOR_ESC_DISPLAY_STR "\033[32m*\033[m" |
| 30 |
#include <string.h> |
#define EDITOR_MEM_POOL_LINE_PER_CHUNK 1000 |
| 31 |
|
#define EDITOR_MEM_POOL_CHUNK_LIMIT (MAX_EDITOR_DATA_LINES / EDITOR_MEM_POOL_LINE_PER_CHUNK + 1) |
| 32 |
|
|
| 33 |
|
static MEMORY_POOL *p_mp_data_line; |
| 34 |
|
static MEMORY_POOL *p_mp_editor_data; |
| 35 |
|
|
| 36 |
|
int editor_memory_pool_init(void) |
| 37 |
|
{ |
| 38 |
|
if (p_mp_data_line != NULL || p_mp_editor_data != NULL) |
| 39 |
|
{ |
| 40 |
|
log_error("Editor mem pool already initialized\n"); |
| 41 |
|
return -1; |
| 42 |
|
} |
| 43 |
|
|
| 44 |
|
p_mp_data_line = memory_pool_init(MAX_EDITOR_DATA_LINE_LENGTH, EDITOR_MEM_POOL_LINE_PER_CHUNK, EDITOR_MEM_POOL_CHUNK_LIMIT); |
| 45 |
|
if (p_mp_data_line == NULL) |
| 46 |
|
{ |
| 47 |
|
log_error("Memory pool init error\n"); |
| 48 |
|
return -2; |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
p_mp_editor_data = memory_pool_init(sizeof(EDITOR_DATA), 1, 1); |
| 52 |
|
if (p_mp_editor_data == NULL) |
| 53 |
|
{ |
| 54 |
|
log_error("Memory pool init error\n"); |
| 55 |
|
return -3; |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
return 0; |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
void editor_memory_pool_cleanup(void) |
| 62 |
|
{ |
| 63 |
|
if (p_mp_data_line != NULL) |
| 64 |
|
{ |
| 65 |
|
memory_pool_cleanup(p_mp_data_line); |
| 66 |
|
p_mp_data_line = NULL; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
if (p_mp_editor_data != NULL) |
| 70 |
|
{ |
| 71 |
|
memory_pool_cleanup(p_mp_editor_data); |
| 72 |
|
p_mp_editor_data = NULL; |
| 73 |
|
} |
| 74 |
|
} |
| 75 |
|
|
| 76 |
EDITOR_DATA *editor_data_load(const char *p_data) |
EDITOR_DATA *editor_data_load(const char *p_data) |
| 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, j; |
long i; |
| 83 |
|
|
| 84 |
if (p_data == NULL) |
if (p_data == NULL) |
| 85 |
{ |
{ |
| 86 |
log_error("editor_data_load() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 87 |
return NULL; |
return NULL; |
| 88 |
} |
} |
| 89 |
|
|
| 90 |
p_editor_data = malloc(sizeof(EDITOR_DATA)); |
p_editor_data = memory_pool_alloc(p_mp_editor_data); |
| 91 |
if (p_editor_data == NULL) |
if (p_editor_data == NULL) |
| 92 |
{ |
{ |
| 93 |
log_error("malloc(EDITOR_DATA) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 94 |
return NULL; |
return NULL; |
| 95 |
} |
} |
| 96 |
|
|
| 97 |
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, |
| 98 |
p_editor_data->display_line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_EDITOR_DATA_LINES); |
0, p_editor_data->display_line_widths); |
| 99 |
|
|
| 100 |
for (i = 0; i < p_editor_data->display_line_total; i++) |
for (i = 0; i < p_editor_data->display_line_total; i++) |
| 101 |
{ |
{ |
| 105 |
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 || |
| 106 |
(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')) |
| 107 |
{ |
{ |
|
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; |
|
|
} |
|
|
|
|
| 108 |
// Allocate new data line |
// Allocate new data line |
| 109 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 110 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 111 |
{ |
{ |
| 112 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH * %d) error: OOM\n", i); |
log_error("memory_pool_alloc() error: i = %d\n", i); |
| 113 |
// Cleanup |
// Cleanup |
| 114 |
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); |
|
| 115 |
return NULL; |
return NULL; |
| 116 |
} |
} |
|
p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; |
|
|
(p_editor_data->data_line_total)++; |
|
| 117 |
|
|
| 118 |
p_editor_data->p_display_lines[i] = p_data_line; |
p_editor_data->p_display_lines[i] = p_data_line; |
| 119 |
current_data_line_length = 0; |
current_data_line_length = 0; |
| 125 |
|
|
| 126 |
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]); |
| 127 |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
current_data_line_length += p_editor_data->display_line_lengths[i]; |
| 128 |
|
|
| 129 |
|
// Trim \n from last line |
| 130 |
|
if (i + 1 == p_editor_data->display_line_total && |
| 131 |
|
p_editor_data->display_line_lengths[i] > 0 && |
| 132 |
|
p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') |
| 133 |
|
{ |
| 134 |
|
p_editor_data->display_line_lengths[i]--; |
| 135 |
|
p_editor_data->display_line_widths[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 |
|
|
| 141 |
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); |
|
| 142 |
|
|
| 143 |
return p_editor_data; |
return p_editor_data; |
| 144 |
} |
} |
| 150 |
|
|
| 151 |
if (p_editor_data == NULL || p_data == NULL) |
if (p_editor_data == NULL || p_data == NULL) |
| 152 |
{ |
{ |
| 153 |
log_error("editor_data_save() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 154 |
return -1; |
return -1; |
| 155 |
} |
} |
| 156 |
|
|
| 174 |
|
|
| 175 |
void editor_data_cleanup(EDITOR_DATA *p_editor_data) |
void editor_data_cleanup(EDITOR_DATA *p_editor_data) |
| 176 |
{ |
{ |
| 177 |
|
char *p_data_line = NULL; |
| 178 |
long i; |
long i; |
| 179 |
|
|
| 180 |
if (p_editor_data == NULL) |
if (p_editor_data == NULL) |
| 182 |
return; |
return; |
| 183 |
} |
} |
| 184 |
|
|
| 185 |
for (i = p_editor_data->data_line_total - 1; i >= 0; i--) |
for (i = 0; i < p_editor_data->display_line_total; i++) |
| 186 |
|
{ |
| 187 |
|
if (p_data_line == NULL) |
| 188 |
|
{ |
| 189 |
|
p_data_line = p_editor_data->p_display_lines[i]; |
| 190 |
|
} |
| 191 |
|
|
| 192 |
|
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') |
| 194 |
|
{ |
| 195 |
|
memory_pool_free(p_mp_data_line, p_data_line); |
| 196 |
|
p_data_line = NULL; |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
if (p_data_line != NULL) |
| 201 |
{ |
{ |
| 202 |
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; |
|
| 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, |
| 215 |
long offset_data_line; |
long offset_data_line; |
| 216 |
long last_display_line; // of data line |
long last_display_line; // of data line |
| 217 |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 218 |
|
int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 219 |
long split_line_total; |
long split_line_total; |
| 220 |
long i, j; |
long i; |
| 221 |
|
int len; |
| 222 |
|
int eol; |
| 223 |
|
int display_len; |
| 224 |
|
|
| 225 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 226 |
{ |
{ |
| 227 |
log_error("editor_data_op() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 228 |
return -1; |
return -1; |
| 229 |
} |
} |
| 230 |
|
|
|
// Get accurate offset of first character of CJK at offset position |
|
|
for (i = 0; i < offset; i++) |
|
|
{ |
|
|
if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK |
|
|
{ |
|
|
i++; |
|
|
} |
|
|
} |
|
|
if (i > offset) // offset was skipped |
|
|
{ |
|
|
offset--; |
|
|
} |
|
|
|
|
| 231 |
// Get length of current data line |
// Get length of current data line |
| 232 |
len_data_line = 0; |
len_data_line = 0; |
| 233 |
p_data_line = p_editor_data->p_display_lines[display_line]; |
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 257 |
} |
} |
| 258 |
|
|
| 259 |
// Split current data line if over-length |
// Split current data line if over-length |
| 260 |
if (len_data_line + str_len + 1 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
if (len_data_line + str_len + 2 > MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
| 261 |
{ |
{ |
| 262 |
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) |
| 263 |
{ |
{ |
| 264 |
log_error("Split line error, display_line_total(%ld) or data_line_total(%ld) reach limit(%d)\n", |
#ifdef _DEBUG |
| 265 |
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", |
| 266 |
|
p_editor_data->display_line_total, MAX_EDITOR_DATA_LINES); |
| 267 |
|
#endif |
| 268 |
|
|
| 269 |
return -2; |
return -2; |
| 270 |
} |
} |
| 271 |
|
|
| 272 |
// Allocate new data line |
// Allocate new data line |
| 273 |
p_data_line = malloc(MAX_EDITOR_DATA_LINE_LENGTH); |
p_data_line = memory_pool_alloc(p_mp_data_line); |
| 274 |
if (p_data_line == NULL) |
if (p_data_line == NULL) |
| 275 |
{ |
{ |
| 276 |
log_error("malloc(MAX_EDITOR_DATA_LINE_LENGTH) error: OOM\n"); |
log_error("memory_pool_alloc() error\n"); |
| 277 |
return -2; |
return -2; |
| 278 |
} |
} |
|
p_editor_data->p_data_lines[p_editor_data->data_line_total] = p_data_line; |
|
|
(p_editor_data->data_line_total)++; |
|
| 279 |
|
|
| 280 |
if (offset_data_line + str_len + 1 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
if (offset_data_line + str_len + 2 >= MAX_EDITOR_DATA_LINE_LENGTH || str[0] == CR) |
| 281 |
{ |
{ |
| 282 |
if (str[0] == CR) |
if (str[0] == CR) |
| 283 |
{ |
{ |
| 323 |
*p_offset = offset + str_len; |
*p_offset = offset + str_len; |
| 324 |
} |
} |
| 325 |
|
|
| 326 |
|
// Update display width of current display line |
| 327 |
|
len = split_line(p_editor_data->p_display_lines[display_line], SCREEN_COLS, &eol, &display_len, 0); |
| 328 |
|
p_editor_data->display_line_widths[display_line] = display_len; |
| 329 |
|
|
| 330 |
split_line_total = last_display_line - display_line + 3; |
split_line_total = last_display_line - display_line + 3; |
| 331 |
|
|
| 332 |
// Set start display_line for spliting new data line |
// Set start display_line for spliting new data line |
| 349 |
} |
} |
| 350 |
|
|
| 351 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 352 |
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, line_widths); |
| 353 |
|
|
| 354 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 355 |
{ |
{ |
| 358 |
// Insert blank display line after last_display_line |
// Insert blank display line after last_display_line |
| 359 |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
if (p_editor_data->display_line_total >= MAX_EDITOR_DATA_LINES) |
| 360 |
{ |
{ |
| 361 |
|
#ifdef _DEBUG |
| 362 |
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); |
| 363 |
return -3; |
#endif |
| 364 |
} |
|
| 365 |
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 |
| 366 |
{ |
if (display_line + i - 1 >= 0 && p_editor_data->display_line_lengths[display_line + i - 1] > 0) |
| 367 |
p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
{ |
| 368 |
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); |
| 369 |
|
p_editor_data->p_display_lines[display_line + i - 1][len] = '\n'; |
| 370 |
|
p_editor_data->p_display_lines[display_line + i - 1][len + 1] = '\0'; |
| 371 |
|
p_editor_data->display_line_lengths[display_line + i - 1] = len + 1; |
| 372 |
|
p_editor_data->display_line_widths[display_line + i - 1] = display_len; |
| 373 |
|
} |
| 374 |
|
if (*p_offset >= p_editor_data->display_line_lengths[*p_display_line]) |
| 375 |
|
{ |
| 376 |
|
*p_offset = p_editor_data->display_line_lengths[*p_display_line] - 1; |
| 377 |
|
} |
| 378 |
|
break; |
| 379 |
} |
} |
| 380 |
|
|
| 381 |
|
// for (j = p_editor_data->display_line_total; j > last_display_line + 1; j--) |
| 382 |
|
// { |
| 383 |
|
// p_editor_data->p_display_lines[j] = p_editor_data->p_display_lines[j - 1]; |
| 384 |
|
// p_editor_data->display_line_lengths[j] = p_editor_data->display_line_lengths[j - 1]; |
| 385 |
|
// p_editor_data->display_line_widths[j] = p_editor_data->display_line_widths[j - 1]; |
| 386 |
|
// } |
| 387 |
|
memmove(p_editor_data->p_display_lines + last_display_line + 2, |
| 388 |
|
p_editor_data->p_display_lines + last_display_line + 1, |
| 389 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 390 |
|
sizeof(p_editor_data->p_display_lines[last_display_line + 1])); |
| 391 |
|
memmove(p_editor_data->display_line_lengths + last_display_line + 2, |
| 392 |
|
p_editor_data->display_line_lengths + last_display_line + 1, |
| 393 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 394 |
|
sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); |
| 395 |
|
memmove(p_editor_data->display_line_widths + last_display_line + 2, |
| 396 |
|
p_editor_data->display_line_widths + last_display_line + 1, |
| 397 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 398 |
|
sizeof(p_editor_data->display_line_widths[last_display_line + 1])); |
| 399 |
|
|
| 400 |
last_display_line++; |
last_display_line++; |
| 401 |
|
*p_last_updated_line = p_editor_data->display_line_total; |
| 402 |
(p_editor_data->display_line_total)++; |
(p_editor_data->display_line_total)++; |
| 403 |
} |
} |
| 404 |
|
|
| 405 |
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
| 406 |
|
p_editor_data->display_line_widths[display_line + i] = line_widths[i]; |
| 407 |
p_editor_data->p_display_lines[display_line + i] = |
p_editor_data->p_display_lines[display_line + i] = |
| 408 |
(i == 0 |
(i == 0 |
| 409 |
? p_data_line |
? p_data_line |
| 418 |
|
|
| 419 |
*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); |
| 420 |
|
|
| 421 |
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')) |
|
| 422 |
{ |
{ |
| 423 |
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
if (*p_display_line + 1 < p_editor_data->display_line_total) |
| 424 |
(*p_display_line)++; |
{ |
| 425 |
|
*p_offset -= p_editor_data->display_line_lengths[*p_display_line]; |
| 426 |
|
(*p_display_line)++; |
| 427 |
|
} |
| 428 |
|
} |
| 429 |
|
|
| 430 |
if (*p_display_line >= p_editor_data->display_line_total) |
// Prevent the last display line from being over-length |
| 431 |
|
if (p_editor_data->display_line_total == MAX_EDITOR_DATA_LINES) |
| 432 |
|
{ |
| 433 |
|
len = split_line(p_editor_data->p_display_lines[p_editor_data->display_line_total - 1], SCREEN_COLS - 1, &eol, &display_len, 0); |
| 434 |
|
p_editor_data->p_display_lines[p_editor_data->display_line_total - 1][len] = '\0'; |
| 435 |
|
p_editor_data->display_line_lengths[p_editor_data->display_line_total - 1] = len; |
| 436 |
|
p_editor_data->display_line_widths[p_editor_data->display_line_total - 1] = display_len; |
| 437 |
|
if (*p_display_line + 1 >= p_editor_data->display_line_total) |
| 438 |
{ |
{ |
| 439 |
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); |
| 440 |
|
*p_display_line = p_editor_data->display_line_total - 1; |
| 441 |
} |
} |
| 442 |
} |
} |
| 443 |
|
|
| 444 |
return 0; |
return 0; |
| 445 |
} |
} |
| 446 |
|
|
| 447 |
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, |
| 448 |
long *p_last_updated_line) |
long *p_last_updated_line) |
| 449 |
{ |
{ |
| 450 |
|
long display_line = *p_display_line; |
| 451 |
|
long offset = *p_offset; |
| 452 |
char *p_data_line = NULL; |
char *p_data_line = NULL; |
| 453 |
long len_data_line; |
long len_data_line; |
| 454 |
long offset_data_line; |
long offset_data_line; |
| 455 |
long last_display_line; // of data line |
long last_display_line; // of data line |
| 456 |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
long line_offsets[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 457 |
|
int line_widths[MAX_EDITOR_DATA_LINE_LENGTH + 1]; |
| 458 |
long split_line_total; |
long split_line_total; |
| 459 |
long i, j; |
long i, j; |
| 460 |
int str_len = 0; |
int str_len = 0; |
| 461 |
|
char c; |
| 462 |
|
|
| 463 |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
if (p_editor_data == NULL || p_last_updated_line == NULL) |
| 464 |
{ |
{ |
| 465 |
log_error("editor_data_op() error: NULL pointer\n"); |
log_error("NULL pointer error\n"); |
| 466 |
return -1; |
return -1; |
| 467 |
} |
} |
| 468 |
|
|
|
// Get accurate offset of first character of CJK at offset position |
|
|
for (i = 0; i < offset; i++) |
|
|
{ |
|
|
if (p_editor_data->p_display_lines[display_line][i] < 0 || p_editor_data->p_display_lines[display_line][i] > 127) // GBK |
|
|
{ |
|
|
i++; |
|
|
} |
|
|
} |
|
|
if (i > offset) // offset was skipped |
|
|
{ |
|
|
offset--; |
|
|
} |
|
|
|
|
| 469 |
// Get length of current data line |
// Get length of current data line |
| 470 |
len_data_line = 0; |
len_data_line = 0; |
| 471 |
p_data_line = p_editor_data->p_display_lines[display_line]; |
p_data_line = p_editor_data->p_display_lines[display_line]; |
| 494 |
} |
} |
| 495 |
} |
} |
| 496 |
|
|
| 497 |
|
if (offset_data_line >= len_data_line) // end-of-line |
| 498 |
|
{ |
| 499 |
|
return 0; |
| 500 |
|
} |
| 501 |
|
|
| 502 |
// Check str to be deleted |
// Check str to be deleted |
| 503 |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
if (p_data_line[offset_data_line] > 0 && p_data_line[offset_data_line] < 127) |
| 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] & 0b10000000) // head of multi-byte character |
| 508 |
{ |
{ |
| 509 |
str_len = 2; |
str_len = 1; |
| 510 |
|
c = (p_data_line[offset_data_line] & 0b01110000) << 1; |
| 511 |
|
while (c & 0b10000000) |
| 512 |
|
{ |
| 513 |
|
str_len++; |
| 514 |
|
c = (c & 0b01111111) << 1; |
| 515 |
|
} |
| 516 |
} |
} |
| 517 |
else |
else |
| 518 |
{ |
{ |
| 519 |
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", |
| 520 |
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]); |
|
| 521 |
str_len = 1; |
str_len = 1; |
| 522 |
} |
} |
| 523 |
|
|
| 527 |
{ |
{ |
| 528 |
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) |
| 529 |
{ |
{ |
|
log_common("Debug: No additional display line: %ld + 1 >= %ld\n", display_line, p_editor_data->display_line_total); |
|
| 530 |
return 0; |
return 0; |
| 531 |
} |
} |
| 532 |
|
|
| 546 |
|
|
| 547 |
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 |
| 548 |
{ |
{ |
|
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); |
|
| 549 |
return 0; |
return 0; |
| 550 |
} |
} |
| 551 |
|
|
| 554 |
p_data_line[offset_data_line + len_data_line] = '\0'; |
p_data_line[offset_data_line + len_data_line] = '\0'; |
| 555 |
|
|
| 556 |
// Recycle next data line |
// Recycle next data line |
| 557 |
// 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]); |
| 558 |
} |
} |
| 559 |
else |
else |
| 560 |
{ |
{ |
| 568 |
split_line_total = last_display_line - display_line + 2; |
split_line_total = last_display_line - display_line + 2; |
| 569 |
|
|
| 570 |
// Split current data line since beginning of current display line |
// Split current data line since beginning of current display line |
| 571 |
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, line_widths); |
| 572 |
|
|
| 573 |
for (i = 0; i < split_line_total; i++) |
for (i = 0; i < split_line_total; i++) |
| 574 |
{ |
{ |
| 575 |
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
p_editor_data->display_line_lengths[display_line + i] = line_offsets[i + 1] - line_offsets[i]; |
| 576 |
|
p_editor_data->display_line_widths[display_line + i] = line_widths[i]; |
| 577 |
p_editor_data->p_display_lines[display_line + i] = |
p_editor_data->p_display_lines[display_line + i] = |
| 578 |
(i == 0 |
(i == 0 |
| 579 |
? p_data_line |
? p_data_line |
| 588 |
|
|
| 589 |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
*p_last_updated_line = display_line + MIN(i, split_line_total - 1); |
| 590 |
|
|
| 591 |
if (display_line + i < last_display_line) |
if (*p_last_updated_line < last_display_line) |
| 592 |
{ |
{ |
| 593 |
// Remove redundant display line after last_display_line |
// Remove redundant display line after last_display_line |
| 594 |
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++) |
| 595 |
{ |
// { |
| 596 |
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]; |
| 597 |
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]; |
| 598 |
} |
// p_editor_data->display_line_widths[j - (last_display_line - *p_last_updated_line)] = p_editor_data->display_line_widths[j]; |
| 599 |
|
// } |
| 600 |
(p_editor_data->display_line_total) -= (last_display_line - (display_line + i)); |
memmove(p_editor_data->p_display_lines + *p_last_updated_line + 1, |
| 601 |
last_display_line = display_line + i; |
p_editor_data->p_display_lines + last_display_line + 1, |
| 602 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 603 |
*p_last_updated_line = p_editor_data->display_line_total - 1; |
sizeof(p_editor_data->p_display_lines[last_display_line + 1])); |
| 604 |
|
memmove(p_editor_data->display_line_lengths + *p_last_updated_line + 1, |
| 605 |
|
p_editor_data->display_line_lengths + last_display_line + 1, |
| 606 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 607 |
|
sizeof(p_editor_data->display_line_lengths[last_display_line + 1])); |
| 608 |
|
memmove(p_editor_data->display_line_widths + *p_last_updated_line + 1, |
| 609 |
|
p_editor_data->display_line_widths + last_display_line + 1, |
| 610 |
|
(size_t)(p_editor_data->display_line_total - last_display_line - 1) * |
| 611 |
|
sizeof(p_editor_data->display_line_widths[last_display_line + 1])); |
| 612 |
|
|
| 613 |
|
j = p_editor_data->display_line_total; |
| 614 |
|
(p_editor_data->display_line_total) -= (last_display_line - *p_last_updated_line); |
| 615 |
|
*p_last_updated_line = MAX(j - 1, *p_last_updated_line); |
| 616 |
} |
} |
| 617 |
|
|
| 618 |
|
// Return real offset |
| 619 |
|
*p_offset = offset; |
| 620 |
|
|
| 621 |
return str_len; |
return str_len; |
| 622 |
} |
} |
| 623 |
|
|
| 627 |
{ |
{ |
| 628 |
case 0: // Set msg |
case 0: // Set msg |
| 629 |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 630 |
"| Í˳ö[\033[32mCtrl-C\033[33m] | °ïÖú[\033[32mh\033[33m] |"); |
"| 退出[\033[32mCtrl-W\033[33m] |"); |
| 631 |
|
break; |
| 632 |
|
case KEY_CSI: |
| 633 |
|
*p_key = KEY_ESC; |
| 634 |
break; |
break; |
| 635 |
} |
} |
| 636 |
|
|
| 644 |
EDITOR_CTX ctx; |
EDITOR_CTX ctx; |
| 645 |
int ch = 0; |
int ch = 0; |
| 646 |
char input_str[4]; |
char input_str[4]; |
| 647 |
|
char c; |
| 648 |
int str_len = 0; |
int str_len = 0; |
| 649 |
int input_ok; |
int input_ok; |
|
int screen_current_row; |
|
| 650 |
const int screen_begin_row = 1; |
const int screen_begin_row = 1; |
| 651 |
int screen_end_row = SCREEN_ROWS - 1; |
const int screen_row_total = SCREEN_ROWS - screen_begin_row; |
| 652 |
const int screen_row_total = screen_end_row - screen_begin_row + 1; |
int output_current_row = screen_begin_row; |
| 653 |
|
int output_end_row = SCREEN_ROWS - 1; |
| 654 |
long int line_current = 0; |
long int line_current = 0; |
| 655 |
long int len; |
long int len; |
| 656 |
int loop; |
int loop; |
| 661 |
int scroll_rows; |
int scroll_rows; |
| 662 |
long last_updated_line = 0; |
long last_updated_line = 0; |
| 663 |
int key_insert = 1; |
int key_insert = 1; |
| 664 |
int i; |
int i, j; |
| 665 |
|
char *p_str; |
| 666 |
|
|
| 667 |
screen_current_row = screen_begin_row; |
clrline(output_current_row, SCREEN_ROWS); |
|
clrline(screen_begin_row, SCREEN_ROWS); |
|
| 668 |
|
|
| 669 |
// update msg_ext with extended key handler |
// update msg_ext with extended key handler |
| 670 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 672 |
return ch; |
return ch; |
| 673 |
} |
} |
| 674 |
|
|
| 675 |
loop = 1; |
for (loop = 1; !SYS_server_exit && loop;) |
|
while (!SYS_server_exit && loop) |
|
| 676 |
{ |
{ |
| 677 |
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) |
| 678 |
{ |
{ |
| 679 |
ctx.line_cursor = line_current - screen_current_row + row_pos + 1; |
ctx.line_cursor = line_current - output_current_row + row_pos + 1; |
| 680 |
|
|
| 681 |
snprintf(buffer, sizeof(buffer), |
snprintf(buffer, sizeof(buffer), |
| 682 |
"\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] " |
| 683 |
"µÚ\033[32m%ld\033[33m/\033[32m%ld\033[33mÐÐ [\033[32m%s\033[33m] " |
"第\033[32m%ld\033[33m/\033[32m%ld\033[33m行 [\033[32m%s\033[33m] " |
| 684 |
"%s", |
"%s", |
| 685 |
row_pos, col_pos, |
row_pos, col_pos, |
| 686 |
ctx.line_cursor, p_editor_data->display_line_total, |
ctx.line_cursor, p_editor_data->display_line_total, |
| 687 |
key_insert ? "²åÈë" : "¸Äд", |
key_insert ? "æ’å…¥" : "替æ¢", |
| 688 |
ctx.msg); |
ctx.msg); |
| 689 |
|
|
| 690 |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1); |
| 691 |
for (; display_len < SCREEN_COLS; display_len++) |
for (; display_len < SCREEN_COLS; display_len++) |
| 692 |
{ |
{ |
| 693 |
buffer[len++] = ' '; |
buffer[len++] = ' '; |
| 701 |
moveto((int)row_pos, (int)col_pos); |
moveto((int)row_pos, (int)col_pos); |
| 702 |
iflush(); |
iflush(); |
| 703 |
|
|
| 704 |
input_ok = 0; |
str_len = 0; |
| 705 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(MAX_DELAY_TIME); |
| 706 |
while (!SYS_server_exit && !input_ok) |
while (!SYS_server_exit) |
| 707 |
{ |
{ |
| 708 |
|
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 709 |
|
{ |
| 710 |
|
BBS_last_access_tm = time(NULL); |
| 711 |
|
} |
| 712 |
|
|
| 713 |
// extended key handler |
// extended key handler |
| 714 |
if (editor_display_key_handler(&ch, &ctx) != 0) |
if (editor_display_key_handler(&ch, &ctx) != 0) |
| 715 |
{ |
{ |
| 716 |
goto cleanup; |
goto cleanup; |
| 717 |
} |
} |
| 718 |
|
|
| 719 |
if (ch > 127 && ch <= 255) // GBK |
if (ch < 256 && (ch & 0b10000000)) // head of multi-byte character |
|
{ |
|
|
input_str[str_len] = (char)(ch - 256); |
|
|
str_len++; |
|
|
} |
|
|
else |
|
| 720 |
{ |
{ |
| 721 |
str_len = 0; |
str_len = 0; |
| 722 |
|
c = (char)(ch & 0b11110000); |
| 723 |
|
while (c & 0b10000000) |
| 724 |
|
{ |
| 725 |
|
input_str[str_len] = (char)(ch - 256); |
| 726 |
|
str_len++; |
| 727 |
|
c = (c & 0b01111111) << 1; |
| 728 |
|
|
| 729 |
|
if ((c & 0b10000000) == 0) // Input completed |
| 730 |
|
{ |
| 731 |
|
break; |
| 732 |
|
} |
| 733 |
|
|
| 734 |
|
// Expect additional bytes of input |
| 735 |
|
ch = igetch(100); // 0.1 second |
| 736 |
|
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input |
| 737 |
|
{ |
| 738 |
|
#ifdef _DEBUG |
| 739 |
|
log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len); |
| 740 |
|
#endif |
| 741 |
|
str_len = 0; |
| 742 |
|
break; |
| 743 |
|
} |
| 744 |
|
} |
| 745 |
} |
} |
| 746 |
|
|
| 747 |
if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || ch == CR) // printable character or GBK |
if ((ch >= 32 && ch < 127) || str_len >= 2 || // Printable character or multi-byte character |
| 748 |
|
ch == CR || ch == KEY_ESC) // Special character |
| 749 |
{ |
{ |
| 750 |
if (str_len == 0) |
// Refresh current action while user input |
| 751 |
|
if (user_online_update(NULL) < 0) |
| 752 |
|
{ |
| 753 |
|
log_error("user_online_update(NULL) error\n"); |
| 754 |
|
} |
| 755 |
|
|
| 756 |
|
if (str_len == 0) // ch >= 32 && ch < 127 |
| 757 |
{ |
{ |
| 758 |
input_str[0] = (char)ch; |
input_str[0] = (char)ch; |
| 759 |
str_len = 1; |
str_len = 1; |
| 760 |
} |
} |
| 761 |
|
|
| 762 |
last_updated_line = line_current; |
display_line_in = line_current - output_current_row + row_pos; |
| 763 |
display_line_in = line_current - screen_current_row + row_pos; |
offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0); |
|
offset_in = col_pos - 1; |
|
| 764 |
display_line_out = display_line_in; |
display_line_out = display_line_in; |
| 765 |
offset_out = offset_in; |
offset_out = offset_in; |
| 766 |
|
|
| 767 |
|
last_updated_line = display_line_in; |
| 768 |
|
|
| 769 |
if (!key_insert) // overwrite |
if (!key_insert) // overwrite |
| 770 |
{ |
{ |
| 771 |
if (editor_data_delete(p_editor_data, display_line_in, offset_in, |
if (editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 772 |
&last_updated_line) < 0) |
&last_updated_line) < 0) |
| 773 |
{ |
{ |
| 774 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 778 |
if (editor_data_insert(p_editor_data, &display_line_out, &offset_out, |
if (editor_data_insert(p_editor_data, &display_line_out, &offset_out, |
| 779 |
input_str, str_len, &last_updated_line) < 0) |
input_str, str_len, &last_updated_line) < 0) |
| 780 |
{ |
{ |
| 781 |
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; |
|
| 782 |
} |
} |
| 783 |
else |
else |
| 784 |
{ |
{ |
| 785 |
str_len = 0; |
output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); |
| 786 |
|
line_current -= (output_current_row - row_pos); |
| 787 |
screen_end_row = MIN(SCREEN_ROWS - 1, screen_current_row + (int)(last_updated_line - line_current)); |
output_current_row = (int)row_pos; |
|
line_current -= (screen_current_row - row_pos); |
|
|
screen_current_row = (int)row_pos; |
|
| 788 |
|
|
| 789 |
scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (screen_end_row - screen_current_row)); |
scroll_rows = MAX(0, (int)(display_line_out - display_line_in) - (output_end_row - output_current_row)); |
| 790 |
|
|
| 791 |
if (scroll_rows > 0) |
if (scroll_rows > 0) |
| 792 |
{ |
{ |
| 794 |
clrtoeol(); |
clrtoeol(); |
| 795 |
for (i = 0; i < scroll_rows; i++) |
for (i = 0; i < scroll_rows; i++) |
| 796 |
{ |
{ |
| 797 |
prints("\033[S"); // Scroll up 1 line |
// prints("\033[S"); // Scroll up 1 line |
| 798 |
|
prints("\n"); // Legacy Cterm only works with this line |
| 799 |
} |
} |
| 800 |
|
|
| 801 |
screen_current_row -= scroll_rows; |
output_current_row -= scroll_rows; |
| 802 |
if (screen_current_row < screen_begin_row) |
if (output_current_row < screen_begin_row) |
| 803 |
{ |
{ |
| 804 |
line_current += (screen_begin_row - screen_current_row); |
line_current += (screen_begin_row - output_current_row); |
| 805 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 806 |
} |
} |
| 807 |
row_pos = screen_end_row; |
row_pos = output_end_row; |
| 808 |
} |
} |
| 809 |
else // if (scroll_lines == 0) |
else // if (scroll_lines == 0) |
| 810 |
{ |
{ |
| 811 |
row_pos += (display_line_out - display_line_in); |
row_pos += (display_line_out - display_line_in); |
| 812 |
} |
} |
| 813 |
col_pos = offset_out + 1; |
|
| 814 |
|
if (offset_out != offset_in) |
| 815 |
|
{ |
| 816 |
|
if (display_line_out != display_line_in) |
| 817 |
|
{ |
| 818 |
|
col_pos = 1; |
| 819 |
|
} |
| 820 |
|
if (offset_out > 0) |
| 821 |
|
{ |
| 822 |
|
col_pos += (str_len == 1 ? 1 : 2); |
| 823 |
|
} |
| 824 |
|
} |
| 825 |
|
} |
| 826 |
|
|
| 827 |
|
if (display_line_out != display_line_in) // Output on line change |
| 828 |
|
{ |
| 829 |
|
break; |
| 830 |
} |
} |
| 831 |
|
|
|
// Check whether there is additional input |
|
| 832 |
ch = igetch(0); |
ch = igetch(0); |
| 833 |
if (ch == KEY_TIMEOUT) |
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input |
| 834 |
{ |
{ |
| 835 |
input_ok = 1; |
break; |
| 836 |
} |
} |
| 837 |
|
|
| 838 |
|
str_len = 0; |
| 839 |
continue; |
continue; |
| 840 |
} |
} |
| 841 |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
else if (ch == KEY_DEL || ch == BACKSPACE) // Del |
| 842 |
{ |
{ |
| 843 |
|
// Refresh current action while user input |
| 844 |
|
if (user_online_update(NULL) < 0) |
| 845 |
|
{ |
| 846 |
|
log_error("user_online_update(NULL) error\n"); |
| 847 |
|
} |
| 848 |
|
|
| 849 |
if (ch == BACKSPACE) |
if (ch == BACKSPACE) |
| 850 |
{ |
{ |
| 851 |
col_pos--; |
if (line_current - output_current_row + row_pos <= 0 && col_pos <= 1) // Forbidden |
| 852 |
if (col_pos < 1 && line_current - screen_current_row + row_pos >= 0) |
{ |
| 853 |
|
break; // force output prior operation result if any |
| 854 |
|
} |
| 855 |
|
|
| 856 |
|
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
| 857 |
|
(int)col_pos - 1, &eol, &display_len, 0); |
| 858 |
|
if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 |
| 859 |
|
{ |
| 860 |
|
col_pos = display_len - 1; |
| 861 |
|
} |
| 862 |
|
else |
| 863 |
|
{ |
| 864 |
|
col_pos = display_len; |
| 865 |
|
} |
| 866 |
|
|
| 867 |
|
if (col_pos < 1 && line_current - output_current_row + row_pos >= 0) |
| 868 |
{ |
{ |
| 869 |
row_pos--; |
row_pos--; |
| 870 |
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_widths[line_current - output_current_row + row_pos]); |
| 871 |
} |
} |
| 872 |
} |
} |
| 873 |
|
|
| 874 |
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; |
| 875 |
|
offset_in = split_line(p_editor_data->p_display_lines[display_line_in], (int)col_pos - 1, &eol, &display_len, 0); |
| 876 |
|
display_line_out = display_line_in; |
| 877 |
|
offset_out = offset_in; |
| 878 |
|
|
| 879 |
|
if ((str_len = editor_data_delete(p_editor_data, &display_line_out, &offset_out, |
| 880 |
&last_updated_line)) < 0) |
&last_updated_line)) < 0) |
| 881 |
{ |
{ |
| 882 |
log_error("editor_data_delete() error\n"); |
log_error("editor_data_delete() error\n"); |
| 883 |
} |
} |
| 884 |
else |
else |
| 885 |
{ |
{ |
| 886 |
if (ch == BACKSPACE) |
col_pos = display_len + 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]); |
|
|
} |
|
|
} |
|
|
} |
|
| 887 |
|
|
| 888 |
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)); |
| 889 |
line_current -= (screen_current_row - row_pos); |
line_current -= (output_current_row - row_pos); |
| 890 |
screen_current_row = (int)row_pos; |
output_current_row = (int)row_pos; |
| 891 |
|
|
| 892 |
if (screen_current_row < screen_begin_row) // row_pos <= 0 |
if (output_current_row < screen_begin_row) // row_pos <= 0 |
| 893 |
{ |
{ |
| 894 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 895 |
row_pos = screen_begin_row; |
row_pos = screen_begin_row; |
| 896 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 897 |
} |
} |
| 898 |
|
|
| 899 |
|
// Exceed end |
| 900 |
|
if (line_current + (screen_row_total - output_current_row) >= p_editor_data->display_line_total && |
| 901 |
|
p_editor_data->display_line_total > screen_row_total) |
| 902 |
|
{ |
| 903 |
|
scroll_rows = (int)((line_current - (output_current_row - screen_begin_row)) - |
| 904 |
|
(p_editor_data->display_line_total - screen_row_total)); |
| 905 |
|
|
| 906 |
|
line_current = p_editor_data->display_line_total - screen_row_total; |
| 907 |
|
row_pos += scroll_rows; |
| 908 |
|
output_current_row = screen_begin_row; |
| 909 |
|
output_end_row = SCREEN_ROWS - 1; |
| 910 |
|
} |
| 911 |
|
|
| 912 |
|
clrline(output_current_row, output_end_row); |
| 913 |
|
} |
| 914 |
|
|
| 915 |
|
if (display_line_out != display_line_in) // Output on line change |
| 916 |
|
{ |
| 917 |
|
break; |
| 918 |
} |
} |
| 919 |
|
|
|
// Check whether there is additional input |
|
| 920 |
ch = igetch(0); |
ch = igetch(0); |
| 921 |
if (ch == KEY_TIMEOUT) |
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Output if no futher input |
| 922 |
{ |
{ |
| 923 |
input_ok = 1; |
break; |
| 924 |
} |
} |
| 925 |
|
|
| 926 |
|
str_len = 0; |
| 927 |
continue; |
continue; |
| 928 |
} |
} |
| 929 |
|
|
| 931 |
switch (ch) |
switch (ch) |
| 932 |
{ |
{ |
| 933 |
case KEY_NULL: |
case KEY_NULL: |
| 934 |
|
log_error("KEY_NULL\n"); |
| 935 |
|
goto cleanup; |
| 936 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 937 |
|
log_error("User input timeout\n"); |
| 938 |
goto cleanup; |
goto cleanup; |
| 939 |
case Ctrl('C'): |
case Ctrl('W'): |
| 940 |
|
case Ctrl('X'): |
| 941 |
loop = 0; |
loop = 0; |
| 942 |
break; |
break; |
| 943 |
|
case Ctrl('S'): // Start of line |
| 944 |
case KEY_CTRL_LEFT: |
case KEY_CTRL_LEFT: |
| 945 |
col_pos = 1; |
col_pos = 1; |
| 946 |
break; |
break; |
| 947 |
|
case Ctrl('E'): // End of line |
| 948 |
case KEY_CTRL_RIGHT: |
case KEY_CTRL_RIGHT: |
| 949 |
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 |
| 950 |
|
{ |
| 951 |
|
// last display line does NOT have \n in the end |
| 952 |
|
col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; |
| 953 |
|
break; |
| 954 |
|
} |
| 955 |
|
col_pos = MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos]); |
| 956 |
break; |
break; |
| 957 |
|
case Ctrl('T'): // Top of screen |
| 958 |
case KEY_CTRL_UP: |
case KEY_CTRL_UP: |
| 959 |
row_pos = screen_begin_row; |
row_pos = screen_begin_row; |
| 960 |
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_widths[line_current - output_current_row + row_pos])); |
| 961 |
break; |
break; |
| 962 |
|
case Ctrl('B'): // Bottom of screen |
| 963 |
case KEY_CTRL_DOWN: |
case KEY_CTRL_DOWN: |
| 964 |
row_pos = SCREEN_ROWS - 1; |
if (p_editor_data->display_line_total < screen_row_total) |
| 965 |
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos])); |
{ |
| 966 |
|
row_pos = p_editor_data->display_line_total; |
| 967 |
|
} |
| 968 |
|
else |
| 969 |
|
{ |
| 970 |
|
row_pos = SCREEN_ROWS - 1; |
| 971 |
|
} |
| 972 |
|
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= p_editor_data->display_line_total) // Reach end |
| 973 |
|
{ |
| 974 |
|
// last display line does NOT have \n in the end |
| 975 |
|
col_pos = MIN(col_pos, p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1); |
| 976 |
|
} |
| 977 |
|
else |
| 978 |
|
{ |
| 979 |
|
col_pos = MIN(col_pos, MAX(1, p_editor_data->display_line_widths[line_current - output_current_row + row_pos])); |
| 980 |
|
} |
| 981 |
break; |
break; |
| 982 |
case KEY_INS: |
case KEY_INS: |
| 983 |
key_insert = !key_insert; |
key_insert = !key_insert; |
| 985 |
case KEY_HOME: |
case KEY_HOME: |
| 986 |
row_pos = 1; |
row_pos = 1; |
| 987 |
col_pos = 1; |
col_pos = 1; |
| 988 |
if (line_current - screen_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |
| 989 |
{ |
{ |
| 990 |
break; |
break; |
| 991 |
} |
} |
| 992 |
line_current = 0; |
line_current = 0; |
| 993 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 994 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 995 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 996 |
break; |
break; |
| 997 |
case KEY_END: |
case KEY_END: |
| 998 |
if (p_editor_data->display_line_total < screen_row_total) |
if (p_editor_data->display_line_total < screen_row_total) |
| 999 |
{ |
{ |
| 1000 |
row_pos = p_editor_data->display_line_total; |
row_pos = p_editor_data->display_line_total; |
| 1001 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; |
| 1002 |
break; |
break; |
| 1003 |
} |
} |
| 1004 |
line_current = p_editor_data->display_line_total - screen_row_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 1005 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1006 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1007 |
row_pos = screen_row_total; |
row_pos = SCREEN_ROWS - 1; |
| 1008 |
col_pos = MAX(1, p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]); |
col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; |
| 1009 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 1010 |
break; |
break; |
| 1011 |
case KEY_LEFT: |
case KEY_LEFT: |
| 1012 |
if (col_pos > 1) |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
| 1013 |
|
(int)col_pos - 1, &eol, &display_len, 0); |
| 1014 |
|
if (offset_in >= 1 && p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in - 1] < 0) // UTF8 |
| 1015 |
|
{ |
| 1016 |
|
col_pos = display_len - 1; |
| 1017 |
|
} |
| 1018 |
|
else |
| 1019 |
|
{ |
| 1020 |
|
col_pos = display_len; |
| 1021 |
|
} |
| 1022 |
|
if (col_pos >= 1) |
| 1023 |
{ |
{ |
|
col_pos--; |
|
| 1024 |
break; |
break; |
| 1025 |
} |
} |
| 1026 |
col_pos = SCREEN_COLS; // continue to KEY_UP |
col_pos = SCREEN_COLS; // continue to KEY_UP |
| 1028 |
if (row_pos > screen_begin_row) |
if (row_pos > screen_begin_row) |
| 1029 |
{ |
{ |
| 1030 |
row_pos--; |
row_pos--; |
| 1031 |
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_widths[line_current - output_current_row + row_pos])); |
| 1032 |
break; |
break; |
| 1033 |
} |
} |
| 1034 |
if (line_current - screen_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |
| 1035 |
{ |
{ |
| 1036 |
col_pos = 1; |
col_pos = 1; |
| 1037 |
break; |
break; |
| 1038 |
} |
} |
| 1039 |
line_current -= screen_current_row; |
line_current -= output_current_row; |
| 1040 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1041 |
// screen_end_line = begin_line; |
// screen_end_line = begin_line; |
| 1042 |
// prints("\033[T"); // Scroll down 1 line |
// prints("\033[T"); // Scroll down 1 line |
| 1043 |
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 |
| 1044 |
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_widths[line_current - output_current_row + row_pos])); |
| 1045 |
break; |
break; |
| 1046 |
case KEY_SPACE: |
case KEY_SPACE: |
| 1047 |
break; |
break; |
| 1048 |
case KEY_RIGHT: |
case KEY_RIGHT: |
| 1049 |
if (col_pos < p_editor_data->display_line_lengths[line_current - screen_current_row + row_pos]) |
offset_in = split_line(p_editor_data->p_display_lines[line_current - output_current_row + row_pos], |
| 1050 |
|
(int)col_pos - 1, &eol, &display_len, 0); |
| 1051 |
|
if (offset_in < p_editor_data->display_line_lengths[line_current - output_current_row + row_pos] && |
| 1052 |
|
p_editor_data->p_display_lines[line_current - output_current_row + row_pos][offset_in] < 0) // UTF8 |
| 1053 |
|
{ |
| 1054 |
|
col_pos = display_len + 3; |
| 1055 |
|
} |
| 1056 |
|
else |
| 1057 |
|
{ |
| 1058 |
|
col_pos = display_len + 2; |
| 1059 |
|
} |
| 1060 |
|
if (col_pos <= p_editor_data->display_line_widths[line_current - output_current_row + row_pos]) |
| 1061 |
{ |
{ |
|
col_pos++; |
|
| 1062 |
break; |
break; |
| 1063 |
} |
} |
| 1064 |
col_pos = 1; // continue to KEY_DOWN |
col_pos = 1; // continue to KEY_DOWN |
| 1066 |
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)) |
| 1067 |
{ |
{ |
| 1068 |
row_pos++; |
row_pos++; |
| 1069 |
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_widths[line_current - output_current_row + row_pos])); |
| 1070 |
break; |
break; |
| 1071 |
} |
} |
| 1072 |
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 |
| 1073 |
{ |
{ |
| 1074 |
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 |
| 1075 |
|
col_pos = p_editor_data->display_line_widths[line_current - output_current_row + row_pos] + 1; |
| 1076 |
break; |
break; |
| 1077 |
} |
} |
| 1078 |
line_current += (screen_row_total - (screen_current_row - screen_begin_row)); |
line_current += (screen_row_total - (output_current_row - screen_begin_row)); |
| 1079 |
screen_current_row = screen_row_total; |
output_current_row = screen_row_total; |
| 1080 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1081 |
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_widths[line_current - output_current_row + row_pos])); |
| 1082 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 1083 |
clrtoeol(); |
clrtoeol(); |
| 1084 |
prints("\033[S"); // Scroll up 1 line |
// prints("\033[S"); // Scroll up 1 line |
| 1085 |
|
prints("\n"); // Legacy Cterm only works with this line |
| 1086 |
break; |
break; |
| 1087 |
case KEY_PGUP: |
case KEY_PGUP: |
| 1088 |
if (line_current - screen_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |
| 1089 |
{ |
{ |
| 1090 |
break; |
break; |
| 1091 |
} |
} |
| 1092 |
line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row)); |
line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row)); |
| 1093 |
if (line_current < 0) |
if (line_current < 0) |
| 1094 |
{ |
{ |
| 1095 |
line_current = 0; |
line_current = 0; |
| 1096 |
} |
} |
| 1097 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1098 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1099 |
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_widths[line_current - output_current_row + row_pos])); |
| 1100 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 1101 |
break; |
break; |
| 1102 |
case KEY_PGDN: |
case KEY_PGDN: |
| 1103 |
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 |
| 1104 |
{ |
{ |
| 1105 |
break; |
break; |
| 1106 |
} |
} |
| 1107 |
line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row); |
line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row); |
| 1108 |
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 |
| 1109 |
{ |
{ |
| 1110 |
line_current = p_editor_data->display_line_total - screen_row_total; |
line_current = p_editor_data->display_line_total - screen_row_total; |
| 1111 |
} |
} |
| 1112 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1113 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1114 |
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_widths[line_current - output_current_row + row_pos])); |
| 1115 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
|
break; |
|
|
case KEY_ESC: |
|
| 1116 |
break; |
break; |
| 1117 |
|
case Ctrl('Q'): |
| 1118 |
case KEY_F1: |
case KEY_F1: |
| 1119 |
if (!show_help) // Not reentrant |
if (!show_help) // Not reentrant |
| 1120 |
{ |
{ |
| 1126 |
show_help = 1; |
show_help = 1; |
| 1127 |
case KEY_F5: |
case KEY_F5: |
| 1128 |
// Refresh after display help information |
// Refresh after display help information |
| 1129 |
line_current -= (screen_current_row - screen_begin_row); |
line_current -= (output_current_row - screen_begin_row); |
| 1130 |
screen_current_row = screen_begin_row; |
output_current_row = screen_begin_row; |
| 1131 |
screen_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 1132 |
clrline(screen_begin_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 1133 |
break; |
break; |
| 1134 |
case 0: // Refresh bottom line |
case 0: // Refresh bottom line |
| 1135 |
break; |
break; |
| 1138 |
break; |
break; |
| 1139 |
} |
} |
| 1140 |
|
|
| 1141 |
BBS_last_access_tm = time(0); |
// Refresh current action while user input |
| 1142 |
if (!input_ok) |
if (user_online_update(NULL) < 0) |
| 1143 |
|
{ |
| 1144 |
|
log_error("user_online_update(NULL) error\n"); |
| 1145 |
|
} |
| 1146 |
|
|
| 1147 |
|
if (input_ok) |
| 1148 |
{ |
{ |
| 1149 |
ch = igetch_t(MAX_DELAY_TIME); |
break; |
| 1150 |
} |
} |
| 1151 |
|
|
| 1152 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 1153 |
} |
} |
| 1154 |
|
|
| 1155 |
continue; |
continue; |
| 1167 |
len = 0; |
len = 0; |
| 1168 |
} |
} |
| 1169 |
|
|
| 1170 |
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); |
| 1171 |
buffer[len] = '\0'; |
// Replace '\033' with '*' |
| 1172 |
|
p_str = p_editor_data->p_display_lines[line_current]; |
| 1173 |
|
for (i = 0, j = 0; i < len; i++) |
| 1174 |
|
{ |
| 1175 |
|
if (p_str[i] == '\033') |
| 1176 |
|
{ |
| 1177 |
|
memcpy(buffer + j, EDITOR_ESC_DISPLAY_STR, sizeof(EDITOR_ESC_DISPLAY_STR) - 1); |
| 1178 |
|
j += (int)(sizeof(EDITOR_ESC_DISPLAY_STR) - 1); |
| 1179 |
|
} |
| 1180 |
|
else |
| 1181 |
|
{ |
| 1182 |
|
buffer[j] = p_str[i]; |
| 1183 |
|
j++; |
| 1184 |
|
} |
| 1185 |
|
} |
| 1186 |
|
buffer[j] = '\0'; |
| 1187 |
|
|
| 1188 |
moveto(screen_current_row, 0); |
moveto(output_current_row, 0); |
| 1189 |
clrtoeol(); |
clrtoeol(); |
| 1190 |
prints("%s", buffer); |
prints("%s", buffer); |
| 1191 |
line_current++; |
line_current++; |
| 1192 |
screen_current_row++; |
output_current_row++; |
| 1193 |
} |
} |
| 1194 |
|
|
| 1195 |
cleanup: |
cleanup: |