| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
screen.c - description |
/* |
| 3 |
------------------- |
* screen |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - advanced telnet-based user interactive input / output features |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "bbs.h" |
#include "bbs.h" |
| 14 |
#include "common.h" |
#include "common.h" |
| 25 |
#include <string.h> |
#include <string.h> |
| 26 |
#include <stdlib.h> |
#include <stdlib.h> |
| 27 |
#include <unistd.h> |
#include <unistd.h> |
| 28 |
|
#include <wchar.h> |
| 29 |
#include <sys/param.h> |
#include <sys/param.h> |
| 30 |
#include <sys/stat.h> |
#include <sys/stat.h> |
|
#include <sys/shm.h> |
|
| 31 |
#include <sys/types.h> |
#include <sys/types.h> |
| 32 |
|
|
| 33 |
#define ACTIVE_BOARD_HEIGHT 8 |
const char CTRL_SEQ_CLR_LINE[] = "\033[K"; |
| 34 |
|
|
| 35 |
|
static const int ACTIVE_BOARD_HEIGHT = 8; |
| 36 |
|
|
| 37 |
#define STR_TOP_LEFT_MAX_LEN 80 |
static const int STR_TOP_LEFT_MAX_LEN = 80; |
| 38 |
#define STR_TOP_MIDDLE_MAX_LEN 40 |
static const int STR_TOP_MIDDLE_MAX_LEN = 40; |
| 39 |
#define STR_TOP_RIGHT_MAX_LEN 80 |
static const int STR_TOP_RIGHT_MAX_LEN = 80; |
| 40 |
|
|
| 41 |
static const char *get_time_str(char *s, size_t len) |
static size_t get_time_str(char *s, size_t len) |
| 42 |
{ |
{ |
|
static const char *weekday[] = { |
|
|
"天", "一", "二", "三", "四", "五", "六"}; |
|
| 43 |
time_t curtime; |
time_t curtime; |
| 44 |
struct tm local_tm; |
struct tm local_tm; |
| 45 |
|
|
| 46 |
time(&curtime); |
time(&curtime); |
| 47 |
localtime_r(&curtime, &local_tm); |
localtime_r(&curtime, &local_tm); |
| 48 |
size_t j = strftime(s, len, "%b %d %H:%M 星期", &local_tm); |
size_t j = strftime(s, len, "%m/%d %H:%M %Z", &local_tm); |
|
|
|
|
if (j == 0 || j + strlen(weekday[local_tm.tm_wday]) + 1 > len) |
|
|
{ |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
strncat(s, weekday[local_tm.tm_wday], len - 1 - j); |
|
| 49 |
|
|
| 50 |
return s; |
return j; |
| 51 |
} |
} |
| 52 |
|
|
| 53 |
void moveto(int row, int col) |
void moveto(int row, int col) |
| 88 |
void clearscr() |
void clearscr() |
| 89 |
{ |
{ |
| 90 |
prints("\033[2J"); |
prints("\033[2J"); |
| 91 |
moveto(0, 0); |
moveto(1, 1); |
| 92 |
} |
} |
| 93 |
|
|
| 94 |
inline int press_any_key() |
inline int press_any_key() |
| 133 |
iflush(); |
iflush(); |
| 134 |
} |
} |
| 135 |
|
|
| 136 |
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, enum io_echo_t echo_mode) |
| 137 |
{ |
{ |
| 138 |
int ch; |
int ch; |
| 139 |
int offset = 0; |
int offset = 0; |
| 140 |
int eol; |
int eol; |
| 141 |
int display_len; |
int display_len; |
| 142 |
char input_str[4]; |
char input_str[5]; |
| 143 |
int str_len = 0; |
int str_len = 0; |
| 144 |
|
wchar_t wcs[2]; |
| 145 |
char c; |
char c; |
| 146 |
|
|
| 147 |
buffer[buf_size - 1] = '\0'; |
buffer[buf_size - 1] = '\0'; |
| 151 |
|
|
| 152 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 153 |
{ |
{ |
| 154 |
ch = igetch_t(MIN(MAX_DELAY_TIME, 60)); |
ch = igetch_t(MIN(BBS_max_user_idle_time, 60)); |
| 155 |
|
|
| 156 |
if (ch == CR) |
if (ch == CR) |
| 157 |
{ |
{ |
| 216 |
break; |
break; |
| 217 |
} |
} |
| 218 |
} |
} |
| 219 |
|
input_str[str_len] = '\0'; |
| 220 |
|
|
| 221 |
if (str_len == 0) // Incomplete input |
if (str_len == 0) // Incomplete input |
| 222 |
{ |
{ |
| 223 |
continue; |
continue; |
| 224 |
} |
} |
| 225 |
|
|
| 226 |
if (offset + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1) |
| 227 |
|
{ |
| 228 |
|
log_error("mbstowcs() error\n"); |
| 229 |
|
} |
| 230 |
|
if (offset + str_len > buf_size - 1 || display_len + (UTF8_fixed_width ? 2 : wcwidth(wcs[0])) > max_display_len) // No enough space for Chinese character |
| 231 |
{ |
{ |
| 232 |
outc('\a'); |
outc('\a'); |
| 233 |
iflush(); |
iflush(); |
| 283 |
return offset; |
return offset; |
| 284 |
} |
} |
| 285 |
|
|
| 286 |
int str_input(char *buffer, int buf_size, int echo_mode) |
int str_input(char *buffer, int buf_size, enum io_echo_t echo_mode) |
| 287 |
{ |
{ |
| 288 |
int len; |
int len; |
| 289 |
|
|
| 305 |
int offset = 0; |
int offset = 0; |
| 306 |
int eol; |
int eol; |
| 307 |
int display_len; |
int display_len; |
| 308 |
char input_str[4]; |
char input_str[5]; |
| 309 |
int str_len = 0; |
int str_len = 0; |
| 310 |
|
wchar_t wcs[2]; |
| 311 |
|
int wc_len; |
| 312 |
char c; |
char c; |
| 313 |
|
|
| 314 |
buffer[buf_size - 1] = '\0'; |
buffer[buf_size - 1] = '\0'; |
| 328 |
|
|
| 329 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 330 |
{ |
{ |
| 331 |
ch = igetch_t(MIN(MAX_DELAY_TIME, 60)); |
ch = igetch_t(MIN(BBS_max_user_idle_time, 60)); |
| 332 |
|
|
| 333 |
if (ch == CR) |
if (ch == CR) |
| 334 |
{ |
{ |
| 355 |
str_len++; |
str_len++; |
| 356 |
offset--; |
offset--; |
| 357 |
} |
} |
| 358 |
display_len--; |
|
| 359 |
col_cur--; |
if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1) |
| 360 |
|
{ |
| 361 |
|
log_error("mbstowcs() error\n"); |
| 362 |
|
} |
| 363 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 364 |
|
|
| 365 |
|
if (wc_len == 2) |
| 366 |
|
{ |
| 367 |
|
display_len--; |
| 368 |
|
col_cur--; |
| 369 |
|
} |
| 370 |
} |
} |
| 371 |
|
|
| 372 |
memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len)); |
memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len)); |
| 429 |
str_len++; |
str_len++; |
| 430 |
offset--; |
offset--; |
| 431 |
} |
} |
| 432 |
col_cur--; |
|
| 433 |
|
if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1) |
| 434 |
|
{ |
| 435 |
|
log_error("mbstowcs() error\n"); |
| 436 |
|
} |
| 437 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 438 |
|
|
| 439 |
|
if (wc_len == 2) |
| 440 |
|
{ |
| 441 |
|
col_cur--; |
| 442 |
|
} |
| 443 |
} |
} |
| 444 |
col_cur--; |
col_cur--; |
| 445 |
|
|
| 461 |
str_len++; |
str_len++; |
| 462 |
c = (c & 0x7f) << 1; |
c = (c & 0x7f) << 1; |
| 463 |
} |
} |
| 464 |
col_cur++; |
|
| 465 |
|
if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1) |
| 466 |
|
{ |
| 467 |
|
log_error("mbstowcs() error\n"); |
| 468 |
|
} |
| 469 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 470 |
|
|
| 471 |
|
if (wc_len == 2) |
| 472 |
|
{ |
| 473 |
|
col_cur++; |
| 474 |
|
} |
| 475 |
} |
} |
| 476 |
else |
else |
| 477 |
{ |
{ |
| 540 |
break; |
break; |
| 541 |
} |
} |
| 542 |
} |
} |
| 543 |
|
input_str[str_len] = '\0'; |
| 544 |
|
|
| 545 |
if (str_len == 0) // Incomplete input |
if (str_len == 0) // Incomplete input |
| 546 |
{ |
{ |
| 547 |
continue; |
continue; |
| 548 |
} |
} |
| 549 |
|
|
| 550 |
if (len + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1) |
| 551 |
|
{ |
| 552 |
|
log_error("mbstowcs() error\n"); |
| 553 |
|
} |
| 554 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 555 |
|
|
| 556 |
|
if (len + str_len > buf_size - 1 || |
| 557 |
|
display_len + wc_len > max_display_len) // No enough space for Chinese character |
| 558 |
{ |
{ |
| 559 |
outc('\a'); |
outc('\a'); |
| 560 |
iflush(); |
iflush(); |
| 565 |
memcpy(buffer + offset, input_str, (size_t)str_len); |
memcpy(buffer + offset, input_str, (size_t)str_len); |
| 566 |
len += str_len; |
len += str_len; |
| 567 |
buffer[len] = '\0'; |
buffer[len] = '\0'; |
| 568 |
display_len += 2; |
display_len += wc_len; |
| 569 |
|
|
| 570 |
moveto(row, col_cur); |
moveto(row, col_cur); |
| 571 |
prints("%s", buffer + offset); |
prints("%s", buffer + offset); |
| 572 |
prints("%*s", max_display_len - display_len, ""); |
prints("%*s", max_display_len - display_len, ""); |
| 573 |
|
|
| 574 |
col_cur += 2; |
col_cur += wc_len; |
| 575 |
|
|
| 576 |
moveto(row, col_cur); |
moveto(row, col_cur); |
| 577 |
iflush(); |
iflush(); |
| 642 |
loop = 1; |
loop = 1; |
| 643 |
while (!SYS_server_exit && loop) |
while (!SYS_server_exit && loop) |
| 644 |
{ |
{ |
| 645 |
if (eof_exit > 0 && line_current >= display_line_total && display_line_total <= screen_row_total) |
if (eof_exit > 0 && line_current >= display_line_total) |
| 646 |
{ |
{ |
| 647 |
if (eof_exit == 1) |
if (eof_exit == 1) |
| 648 |
{ |
{ |
| 697 |
input_ok = 0; |
input_ok = 0; |
| 698 |
while (!SYS_server_exit && !input_ok) |
while (!SYS_server_exit && !input_ok) |
| 699 |
{ |
{ |
| 700 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(BBS_max_user_idle_time); |
| 701 |
input_ok = 1; |
input_ok = 1; |
| 702 |
|
|
| 703 |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 704 |
{ |
{ |
| 705 |
BBS_last_access_tm = time(NULL); |
BBS_last_access_tm = time(NULL); |
| 706 |
|
|
| 707 |
|
// Refresh current action |
| 708 |
|
if (user_online_update(NULL) < 0) |
| 709 |
|
{ |
| 710 |
|
log_error("user_online_update(NULL) error\n"); |
| 711 |
|
} |
| 712 |
} |
} |
| 713 |
|
|
| 714 |
// extended key handler |
// extended key handler |
| 720 |
switch (ch) |
switch (ch) |
| 721 |
{ |
{ |
| 722 |
case KEY_NULL: |
case KEY_NULL: |
| 723 |
|
#ifdef _DEBUG |
| 724 |
log_error("KEY_NULL\n"); |
log_error("KEY_NULL\n"); |
| 725 |
|
#endif |
| 726 |
goto cleanup; |
goto cleanup; |
| 727 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 728 |
log_error("User input timeout\n"); |
log_error("User input timeout\n"); |
| 759 |
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 |
| 760 |
break; |
break; |
| 761 |
case CR: |
case CR: |
|
case KEY_SPACE: |
|
| 762 |
case KEY_DOWN: |
case KEY_DOWN: |
| 763 |
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 |
| 764 |
{ |
{ |
| 786 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 787 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 788 |
break; |
break; |
| 789 |
|
case KEY_SPACE: |
| 790 |
case KEY_PGDN: |
case KEY_PGDN: |
| 791 |
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 |
| 792 |
{ |
{ |
| 860 |
return ch; |
return ch; |
| 861 |
} |
} |
| 862 |
|
|
| 863 |
static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx) |
int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx) |
| 864 |
{ |
{ |
| 865 |
switch (*p_key) |
switch (*p_key) |
| 866 |
{ |
{ |
| 878 |
int display_file(const char *filename, int eof_exit) |
int display_file(const char *filename, int eof_exit) |
| 879 |
{ |
{ |
| 880 |
int ret; |
int ret; |
| 881 |
const void *p_shm; |
void *p_shm; |
| 882 |
size_t data_len; |
size_t data_len; |
| 883 |
long line_total; |
long line_total; |
| 884 |
const void *p_data; |
const void *p_data; |
| 943 |
int show_bottom(const char *msg) |
int show_bottom(const char *msg) |
| 944 |
{ |
{ |
| 945 |
char str_time[LINE_BUFFER_LEN]; |
char str_time[LINE_BUFFER_LEN]; |
| 946 |
|
int len_str_time; |
| 947 |
time_t time_online; |
time_t time_online; |
| 948 |
struct tm *tm_online; |
struct tm *tm_online; |
| 949 |
char msg_f[LINE_BUFFER_LEN]; |
char msg_f[LINE_BUFFER_LEN]; |
| 950 |
int eol; |
int eol; |
| 951 |
int msg_len; |
int len_msg; |
| 952 |
int len; |
int len; |
| 953 |
int len_username; |
int len_username; |
| 954 |
char str_tm_online[LINE_BUFFER_LEN]; |
char str_tm_online[LINE_BUFFER_LEN]; |
| 955 |
|
int len_str_tm_online; |
| 956 |
|
|
| 957 |
get_time_str(str_time, sizeof(str_time)); |
len_str_time = (int)get_time_str(str_time, sizeof(str_time)); |
| 958 |
|
|
| 959 |
msg_f[0] = '\0'; |
msg_f[0] = '\0'; |
| 960 |
msg_len = 0; |
len_msg = 0; |
| 961 |
if (msg != NULL) |
if (msg != NULL) |
| 962 |
{ |
{ |
| 963 |
strncpy(msg_f, msg, sizeof(msg_f) - 1); |
strncpy(msg_f, msg, sizeof(msg_f) - 1); |
| 964 |
msg_f[sizeof(msg_f) - 1] = '\0'; |
msg_f[sizeof(msg_f) - 1] = '\0'; |
| 965 |
len = split_line(msg_f, 23, &eol, &msg_len, 1); |
len = split_line(msg_f, 23, &eol, &len_msg, 1); |
| 966 |
msg_f[len] = '\0'; |
msg_f[len] = '\0'; |
| 967 |
} |
} |
| 968 |
|
|
| 973 |
if (tm_online->tm_mday > 1) |
if (tm_online->tm_mday > 1) |
| 974 |
{ |
{ |
| 975 |
snprintf(str_tm_online, sizeof(str_tm_online), |
snprintf(str_tm_online, sizeof(str_tm_online), |
| 976 |
"\033[36m%2d\033[33m天\033[36m%2d\033[33m时", |
"\033[36m%d\033[33md \033[36m%d\033[33m:\033[36m%.2d", |
| 977 |
tm_online->tm_mday - 1, tm_online->tm_hour); |
tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min); |
| 978 |
} |
} |
| 979 |
else |
else |
| 980 |
{ |
{ |
| 981 |
snprintf(str_tm_online, sizeof(str_tm_online), |
snprintf(str_tm_online, sizeof(str_tm_online), |
| 982 |
"\033[36m%2d\033[33m时\033[36m%2d\033[33m分", |
"\033[36m%d\033[33m:\033[36m%.2d", |
| 983 |
tm_online->tm_hour, tm_online->tm_min); |
tm_online->tm_hour, tm_online->tm_min); |
| 984 |
} |
} |
| 985 |
|
len_str_tm_online = str_length(str_tm_online, 1); |
| 986 |
|
|
| 987 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 988 |
clrtoeol(); |
clrtoeol(); |
| 989 |
prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m帐号[\033[36m%s\033[33m][%s\033[33m]\033[m", |
prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m用户[\033[36m%s\033[33m][%s\033[33m]\033[m", |
| 990 |
str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online); |
str_time, msg_f, 65 - len_str_time - len_msg - len_username - len_str_tm_online, |
| 991 |
|
"", BBS_username, str_tm_online); |
| 992 |
|
|
| 993 |
return 0; |
return 0; |
| 994 |
} |
} |