| 38 |
|
|
| 39 |
#define STR_TOP_LEFT_MAX_LEN 80 |
#define STR_TOP_LEFT_MAX_LEN 80 |
| 40 |
#define STR_TOP_MIDDLE_MAX_LEN 40 |
#define STR_TOP_MIDDLE_MAX_LEN 40 |
| 41 |
#define STR_TOP_RIGHT_MAX_LEN 40 |
#define STR_TOP_RIGHT_MAX_LEN 80 |
| 42 |
|
|
| 43 |
|
static const char *get_time_str(char *s, size_t len) |
| 44 |
|
{ |
| 45 |
|
static const char *weekday[] = { |
| 46 |
|
"天", "一", "二", "三", "四", "五", "六"}; |
| 47 |
|
time_t curtime; |
| 48 |
|
struct tm local_tm; |
| 49 |
|
|
| 50 |
|
time(&curtime); |
| 51 |
|
localtime_r(&curtime, &local_tm); |
| 52 |
|
size_t j = strftime(s, len, "%b %d %H:%M 星期", &local_tm); |
| 53 |
|
|
| 54 |
|
if (j == 0 || j + strlen(weekday[local_tm.tm_wday]) + 1 > len) |
| 55 |
|
{ |
| 56 |
|
return NULL; |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
strncat(s, weekday[local_tm.tm_wday], len - 1 - j); |
| 60 |
|
|
| 61 |
|
return s; |
| 62 |
|
} |
| 63 |
|
|
| 64 |
void moveto(int row, int col) |
void moveto(int row, int col) |
| 65 |
{ |
{ |
| 102 |
moveto(0, 0); |
moveto(0, 0); |
| 103 |
} |
} |
| 104 |
|
|
| 105 |
int press_any_key() |
inline int press_any_key() |
| 106 |
|
{ |
| 107 |
|
return press_any_key_ex(" \033[1;33m按任意键继续...\033[m"); |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
int press_any_key_ex(const char *msg) |
| 111 |
{ |
{ |
| 112 |
|
int ch = 0; |
| 113 |
|
int wait_seconds = 60; |
| 114 |
|
int duration = 0; |
| 115 |
|
time_t t_begin = time(NULL); |
| 116 |
|
|
| 117 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 118 |
clrtoeol(); |
clrtoeol(); |
| 119 |
|
|
| 120 |
prints(" \033[1;33m按任意键继续...\033[0;37m"); |
prints(msg); |
| 121 |
iflush(); |
iflush(); |
| 122 |
|
|
| 123 |
return igetch_t(MIN(MAX_DELAY_TIME, 60)); |
do |
| 124 |
|
{ |
| 125 |
|
ch = igetch_t(wait_seconds - duration); |
| 126 |
|
duration = (int)(time(NULL) - t_begin); |
| 127 |
|
} while (!SYS_server_exit && ch == 0 && duration < 60); |
| 128 |
|
|
| 129 |
|
return ch; |
| 130 |
} |
} |
| 131 |
|
|
| 132 |
void set_input_echo(int echo) |
void set_input_echo(int echo) |
| 134 |
if (echo) |
if (echo) |
| 135 |
{ |
{ |
| 136 |
outc('\x83'); // ASCII code 131 |
outc('\x83'); // ASCII code 131 |
|
iflush(); |
|
| 137 |
} |
} |
| 138 |
else |
else |
| 139 |
{ |
{ |
| 140 |
// outc ('\x85'); // ASCII code 133 |
// outc ('\x85'); // ASCII code 133 |
| 141 |
prints("\xff\xfb\x01\xff\xfb\x03"); |
prints("\xff\xfb\x01\xff\xfb\x03"); |
|
iflush(); |
|
|
igetch(0); |
|
|
igetch_reset(); |
|
| 142 |
} |
} |
| 143 |
|
iflush(); |
| 144 |
} |
} |
| 145 |
|
|
| 146 |
static int _str_input(char *buffer, int buf_size, int max_display_len, int echo_mode) |
static int _str_input(char *buffer, int buf_size, int max_display_len, int echo_mode) |
| 164 |
|
|
| 165 |
if (ch == CR) |
if (ch == CR) |
| 166 |
{ |
{ |
|
igetch_reset(); |
|
| 167 |
break; |
break; |
| 168 |
} |
} |
| 169 |
else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe |
else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe |
| 301 |
return len; |
return len; |
| 302 |
}; |
}; |
| 303 |
|
|
| 304 |
int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len, int echo_mode) |
int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len) |
| 305 |
{ |
{ |
| 306 |
int len; |
int len = 0; |
| 307 |
|
int col_cur = 0; |
| 308 |
|
int ch; |
| 309 |
|
int offset = 0; |
| 310 |
|
int eol; |
| 311 |
|
int display_len; |
| 312 |
|
char input_str[4]; |
| 313 |
|
int str_len = 0; |
| 314 |
|
char c; |
| 315 |
|
|
| 316 |
|
buffer[buf_size - 1] = '\0'; |
| 317 |
|
offset = split_line(buffer, max_display_len, &eol, &display_len, 0); |
| 318 |
|
buffer[offset] = '\0'; |
| 319 |
|
len = offset; |
| 320 |
|
col_cur = col + str_length(prompt, 1) + display_len; |
| 321 |
|
|
| 322 |
moveto(row, col); |
moveto(row, col); |
| 323 |
prints("%s", prompt); |
prints("%s", prompt); |
| 324 |
prints("%s", buffer); |
prints("%s", buffer); |
| 325 |
|
prints("%*s", max_display_len - display_len, ""); |
| 326 |
|
moveto(row, col_cur); |
| 327 |
iflush(); |
iflush(); |
| 328 |
|
|
| 329 |
len = _str_input(buffer, buf_size, max_display_len, echo_mode); |
igetch_reset(); |
| 330 |
|
|
| 331 |
|
while (!SYS_server_exit) |
| 332 |
|
{ |
| 333 |
|
ch = igetch_t(MIN(MAX_DELAY_TIME, 60)); |
| 334 |
|
|
| 335 |
|
if (ch == CR) |
| 336 |
|
{ |
| 337 |
|
break; |
| 338 |
|
} |
| 339 |
|
else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe |
| 340 |
|
{ |
| 341 |
|
return -1; |
| 342 |
|
} |
| 343 |
|
else if (ch == LF || ch == '\0') |
| 344 |
|
{ |
| 345 |
|
continue; |
| 346 |
|
} |
| 347 |
|
else if (ch == BACKSPACE) |
| 348 |
|
{ |
| 349 |
|
if (offset > 0) |
| 350 |
|
{ |
| 351 |
|
str_len = 1; |
| 352 |
|
offset--; |
| 353 |
|
if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8 |
| 354 |
|
{ |
| 355 |
|
while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000) |
| 356 |
|
{ |
| 357 |
|
str_len++; |
| 358 |
|
offset--; |
| 359 |
|
} |
| 360 |
|
display_len--; |
| 361 |
|
col_cur--; |
| 362 |
|
} |
| 363 |
|
|
| 364 |
|
memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len)); |
| 365 |
|
len -= str_len; |
| 366 |
|
buffer[len] = '\0'; |
| 367 |
|
display_len--; |
| 368 |
|
col_cur--; |
| 369 |
|
|
| 370 |
|
moveto(row, col_cur); |
| 371 |
|
prints("%s", buffer + offset); |
| 372 |
|
prints("%*s", max_display_len - display_len, ""); |
| 373 |
|
moveto(row, col_cur); |
| 374 |
|
iflush(); |
| 375 |
|
} |
| 376 |
|
continue; |
| 377 |
|
} |
| 378 |
|
else if (ch == KEY_DEL) |
| 379 |
|
{ |
| 380 |
|
if (offset < len) |
| 381 |
|
{ |
| 382 |
|
if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character |
| 383 |
|
{ |
| 384 |
|
str_len = 0; |
| 385 |
|
c = (char)(buffer[offset] & 0b11110000); |
| 386 |
|
while (c & 0b10000000) |
| 387 |
|
{ |
| 388 |
|
str_len++; |
| 389 |
|
c = (c & 0b01111111) << 1; |
| 390 |
|
} |
| 391 |
|
display_len--; |
| 392 |
|
} |
| 393 |
|
else |
| 394 |
|
{ |
| 395 |
|
str_len = 1; |
| 396 |
|
} |
| 397 |
|
|
| 398 |
|
memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len)); |
| 399 |
|
len -= str_len; |
| 400 |
|
buffer[len] = '\0'; |
| 401 |
|
display_len--; |
| 402 |
|
|
| 403 |
|
moveto(row, col_cur); |
| 404 |
|
prints("%s", buffer + offset); |
| 405 |
|
prints("%*s", max_display_len - display_len, ""); |
| 406 |
|
moveto(row, col_cur); |
| 407 |
|
iflush(); |
| 408 |
|
} |
| 409 |
|
continue; |
| 410 |
|
} |
| 411 |
|
else if (ch == KEY_LEFT) |
| 412 |
|
{ |
| 413 |
|
if (offset > 0) |
| 414 |
|
{ |
| 415 |
|
str_len = 1; |
| 416 |
|
offset--; |
| 417 |
|
if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8 |
| 418 |
|
{ |
| 419 |
|
while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000) |
| 420 |
|
{ |
| 421 |
|
str_len++; |
| 422 |
|
offset--; |
| 423 |
|
} |
| 424 |
|
col_cur--; |
| 425 |
|
} |
| 426 |
|
col_cur--; |
| 427 |
|
|
| 428 |
|
moveto(row, col_cur); |
| 429 |
|
iflush(); |
| 430 |
|
} |
| 431 |
|
continue; |
| 432 |
|
} |
| 433 |
|
else if (ch == KEY_RIGHT) |
| 434 |
|
{ |
| 435 |
|
if (offset < len) |
| 436 |
|
{ |
| 437 |
|
str_len = 0; |
| 438 |
|
if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character |
| 439 |
|
{ |
| 440 |
|
c = (char)(buffer[offset] & 0b11110000); |
| 441 |
|
while (c & 0b10000000) |
| 442 |
|
{ |
| 443 |
|
str_len++; |
| 444 |
|
c = (c & 0b01111111) << 1; |
| 445 |
|
} |
| 446 |
|
col_cur++; |
| 447 |
|
} |
| 448 |
|
else |
| 449 |
|
{ |
| 450 |
|
str_len = 1; |
| 451 |
|
} |
| 452 |
|
|
| 453 |
|
col_cur++; |
| 454 |
|
offset += str_len; |
| 455 |
|
|
| 456 |
|
moveto(row, col_cur); |
| 457 |
|
iflush(); |
| 458 |
|
} |
| 459 |
|
continue; |
| 460 |
|
} |
| 461 |
|
else if (ch == KEY_HOME || ch == KEY_UP) |
| 462 |
|
{ |
| 463 |
|
if (offset > 0) |
| 464 |
|
{ |
| 465 |
|
offset = 0; |
| 466 |
|
col_cur = col + str_length(prompt, 1); |
| 467 |
|
|
| 468 |
|
moveto(row, col_cur); |
| 469 |
|
iflush(); |
| 470 |
|
} |
| 471 |
|
continue; |
| 472 |
|
} |
| 473 |
|
else if (ch == KEY_END || ch == KEY_DOWN) |
| 474 |
|
{ |
| 475 |
|
if (offset < len) |
| 476 |
|
{ |
| 477 |
|
offset = len; |
| 478 |
|
col_cur = col + str_length(prompt, 1) + display_len; |
| 479 |
|
|
| 480 |
|
moveto(row, col_cur); |
| 481 |
|
iflush(); |
| 482 |
|
} |
| 483 |
|
continue; |
| 484 |
|
} |
| 485 |
|
else if (ch > 255 || iscntrl(ch)) |
| 486 |
|
{ |
| 487 |
|
continue; |
| 488 |
|
} |
| 489 |
|
else if ((ch & 0xff80) == 0x80) // head of multi-byte character |
| 490 |
|
{ |
| 491 |
|
str_len = 0; |
| 492 |
|
c = (char)(ch & 0b11110000); |
| 493 |
|
while (c & 0b10000000) |
| 494 |
|
{ |
| 495 |
|
input_str[str_len] = (char)(ch - 256); |
| 496 |
|
str_len++; |
| 497 |
|
c = (c & 0b01111111) << 1; |
| 498 |
|
|
| 499 |
|
if ((c & 0b10000000) == 0) // Input completed |
| 500 |
|
{ |
| 501 |
|
break; |
| 502 |
|
} |
| 503 |
|
|
| 504 |
|
// Expect additional bytes of input |
| 505 |
|
ch = igetch(100); // 0.1 second |
| 506 |
|
if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input |
| 507 |
|
{ |
| 508 |
|
#ifdef _DEBUG |
| 509 |
|
log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len); |
| 510 |
|
#endif |
| 511 |
|
str_len = 0; |
| 512 |
|
break; |
| 513 |
|
} |
| 514 |
|
} |
| 515 |
|
|
| 516 |
|
if (str_len == 0) // Incomplete input |
| 517 |
|
{ |
| 518 |
|
continue; |
| 519 |
|
} |
| 520 |
|
|
| 521 |
|
if (len + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character |
| 522 |
|
{ |
| 523 |
|
outc('\a'); |
| 524 |
|
iflush(); |
| 525 |
|
continue; |
| 526 |
|
} |
| 527 |
|
|
| 528 |
|
memmove(buffer + offset + str_len, buffer + offset, (size_t)(len - offset)); |
| 529 |
|
memcpy(buffer + offset, input_str, (size_t)str_len); |
| 530 |
|
len += str_len; |
| 531 |
|
buffer[len] = '\0'; |
| 532 |
|
display_len += 2; |
| 533 |
|
|
| 534 |
|
moveto(row, col_cur); |
| 535 |
|
prints("%s", buffer + offset); |
| 536 |
|
prints("%*s", max_display_len - display_len, ""); |
| 537 |
|
|
| 538 |
|
col_cur += 2; |
| 539 |
|
|
| 540 |
|
moveto(row, col_cur); |
| 541 |
|
iflush(); |
| 542 |
|
|
| 543 |
|
offset += str_len; |
| 544 |
|
} |
| 545 |
|
else if (ch >= 32 && ch < 127) // Printable character |
| 546 |
|
{ |
| 547 |
|
if (len + 1 > buf_size - 1 || display_len + 1 > max_display_len) |
| 548 |
|
{ |
| 549 |
|
outc('\a'); |
| 550 |
|
iflush(); |
| 551 |
|
continue; |
| 552 |
|
} |
| 553 |
|
|
| 554 |
|
memmove(buffer + offset + 1, buffer + offset, (size_t)(len - offset)); |
| 555 |
|
buffer[offset] = (char)ch; |
| 556 |
|
len++; |
| 557 |
|
buffer[len] = '\0'; |
| 558 |
|
display_len++; |
| 559 |
|
|
| 560 |
|
moveto(row, col_cur); |
| 561 |
|
prints("%s", buffer + offset); |
| 562 |
|
prints("%*s", max_display_len - display_len, ""); |
| 563 |
|
|
| 564 |
|
col_cur++; |
| 565 |
|
|
| 566 |
|
moveto(row, col_cur); |
| 567 |
|
iflush(); |
| 568 |
|
|
| 569 |
|
offset++; |
| 570 |
|
} |
| 571 |
|
else // Invalid character |
| 572 |
|
{ |
| 573 |
|
continue; |
| 574 |
|
} |
| 575 |
|
} |
| 576 |
|
|
| 577 |
return len; |
return len; |
| 578 |
} |
} |
| 664 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(MAX_DELAY_TIME); |
| 665 |
input_ok = 1; |
input_ok = 1; |
| 666 |
|
|
| 667 |
|
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 668 |
|
{ |
| 669 |
|
BBS_last_access_tm = time(NULL); |
| 670 |
|
} |
| 671 |
|
|
| 672 |
// extended key handler |
// extended key handler |
| 673 |
if (key_handler(&ch, &ctx) != 0) |
if (key_handler(&ch, &ctx) != 0) |
| 674 |
{ |
{ |
| 678 |
switch (ch) |
switch (ch) |
| 679 |
{ |
{ |
| 680 |
case KEY_NULL: |
case KEY_NULL: |
| 681 |
|
log_error("KEY_NULL\n"); |
| 682 |
|
goto cleanup; |
| 683 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 684 |
|
log_error("User input timeout\n"); |
| 685 |
goto cleanup; |
goto cleanup; |
| 686 |
case KEY_HOME: |
case KEY_HOME: |
| 687 |
if (line_current - output_current_row < 0) // Reach begin |
if (line_current - output_current_row < 0) // Reach begin |
| 715 |
output_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 |
| 716 |
break; |
break; |
| 717 |
case CR: |
case CR: |
|
igetch_reset(); |
|
| 718 |
case KEY_SPACE: |
case KEY_SPACE: |
| 719 |
case KEY_DOWN: |
case KEY_DOWN: |
| 720 |
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end |
if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end |
| 783 |
input_ok = 0; |
input_ok = 0; |
| 784 |
break; |
break; |
| 785 |
} |
} |
|
|
|
|
BBS_last_access_tm = time(NULL); |
|
| 786 |
} |
} |
| 787 |
|
|
| 788 |
continue; |
continue; |