| 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 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "bbs.h" |
#include "bbs.h" |
| 10 |
#include "common.h" |
#include "common.h" |
| 21 |
#include <string.h> |
#include <string.h> |
| 22 |
#include <stdlib.h> |
#include <stdlib.h> |
| 23 |
#include <unistd.h> |
#include <unistd.h> |
| 24 |
|
#include <wchar.h> |
| 25 |
#include <sys/param.h> |
#include <sys/param.h> |
| 26 |
#include <sys/stat.h> |
#include <sys/stat.h> |
| 27 |
#include <sys/shm.h> |
#include <sys/shm.h> |
| 28 |
#include <sys/types.h> |
#include <sys/types.h> |
| 29 |
|
|
| 30 |
#define ACTIVE_BOARD_HEIGHT 8 |
const char CTRL_SEQ_CLR_LINE[] = "\033[K"; |
| 31 |
|
|
| 32 |
#define STR_TOP_LEFT_MAX_LEN 80 |
static const int ACTIVE_BOARD_HEIGHT = 8; |
|
#define STR_TOP_MIDDLE_MAX_LEN 40 |
|
|
#define STR_TOP_RIGHT_MAX_LEN 80 |
|
| 33 |
|
|
| 34 |
static const char *get_time_str(char *s, size_t len) |
static const int STR_TOP_LEFT_MAX_LEN = 80; |
| 35 |
|
static const int STR_TOP_MIDDLE_MAX_LEN = 40; |
| 36 |
|
static const int STR_TOP_RIGHT_MAX_LEN = 80; |
| 37 |
|
|
| 38 |
|
static size_t get_time_str(char *s, size_t len) |
| 39 |
{ |
{ |
|
static const char *weekday[] = { |
|
|
"天", "一", "二", "三", "四", "五", "六"}; |
|
| 40 |
time_t curtime; |
time_t curtime; |
| 41 |
struct tm local_tm; |
struct tm local_tm; |
| 42 |
|
|
| 43 |
time(&curtime); |
time(&curtime); |
| 44 |
localtime_r(&curtime, &local_tm); |
localtime_r(&curtime, &local_tm); |
| 45 |
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); |
|
| 46 |
|
|
| 47 |
return s; |
return j; |
| 48 |
} |
} |
| 49 |
|
|
| 50 |
void moveto(int row, int col) |
void moveto(int row, int col) |
| 130 |
iflush(); |
iflush(); |
| 131 |
} |
} |
| 132 |
|
|
| 133 |
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) |
| 134 |
{ |
{ |
| 135 |
int ch; |
int ch; |
| 136 |
int offset = 0; |
int offset = 0; |
| 137 |
int eol; |
int eol; |
| 138 |
int display_len; |
int display_len; |
| 139 |
char input_str[4]; |
char input_str[5]; |
| 140 |
int str_len = 0; |
int str_len = 0; |
| 141 |
|
wchar_t wcs[2]; |
| 142 |
char c; |
char c; |
| 143 |
|
|
| 144 |
buffer[buf_size - 1] = '\0'; |
buffer[buf_size - 1] = '\0'; |
| 148 |
|
|
| 149 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 150 |
{ |
{ |
| 151 |
ch = igetch_t(MIN(MAX_DELAY_TIME, 60)); |
ch = igetch_t(MIN(BBS_max_user_idle_time, 60)); |
| 152 |
|
|
| 153 |
if (ch == CR) |
if (ch == CR) |
| 154 |
{ |
{ |
| 169 |
offset--; |
offset--; |
| 170 |
if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8 |
if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8 |
| 171 |
{ |
{ |
| 172 |
while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000) |
while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0) |
| 173 |
{ |
{ |
| 174 |
offset--; |
offset--; |
| 175 |
} |
} |
| 190 |
else if ((ch & 0xff80) == 0x80) // head of multi-byte character |
else if ((ch & 0xff80) == 0x80) // head of multi-byte character |
| 191 |
{ |
{ |
| 192 |
str_len = 0; |
str_len = 0; |
| 193 |
c = (char)(ch & 0b11110000); |
c = (char)(ch & 0xf0); |
| 194 |
while (c & 0b10000000) |
while (c & 0x80) |
| 195 |
{ |
{ |
| 196 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 197 |
str_len++; |
str_len++; |
| 198 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 199 |
|
|
| 200 |
if ((c & 0b10000000) == 0) // Input completed |
if ((c & 0x80) == 0) // Input completed |
| 201 |
{ |
{ |
| 202 |
break; |
break; |
| 203 |
} |
} |
| 213 |
break; |
break; |
| 214 |
} |
} |
| 215 |
} |
} |
| 216 |
|
input_str[str_len] = '\0'; |
| 217 |
|
|
| 218 |
if (str_len == 0) // Incomplete input |
if (str_len == 0) // Incomplete input |
| 219 |
{ |
{ |
| 220 |
continue; |
continue; |
| 221 |
} |
} |
| 222 |
|
|
| 223 |
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) |
| 224 |
|
{ |
| 225 |
|
log_error("mbstowcs() error\n"); |
| 226 |
|
} |
| 227 |
|
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 |
| 228 |
{ |
{ |
| 229 |
outc('\a'); |
outc('\a'); |
| 230 |
iflush(); |
iflush(); |
| 280 |
return offset; |
return offset; |
| 281 |
} |
} |
| 282 |
|
|
| 283 |
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) |
| 284 |
{ |
{ |
| 285 |
int len; |
int len; |
| 286 |
|
|
| 292 |
iflush(); |
iflush(); |
| 293 |
|
|
| 294 |
return len; |
return len; |
| 295 |
}; |
} |
| 296 |
|
|
| 297 |
int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len) |
int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len) |
| 298 |
{ |
{ |
| 302 |
int offset = 0; |
int offset = 0; |
| 303 |
int eol; |
int eol; |
| 304 |
int display_len; |
int display_len; |
| 305 |
char input_str[4]; |
char input_str[5]; |
| 306 |
int str_len = 0; |
int str_len = 0; |
| 307 |
|
wchar_t wcs[2]; |
| 308 |
|
int wc_len; |
| 309 |
char c; |
char c; |
| 310 |
|
|
| 311 |
buffer[buf_size - 1] = '\0'; |
buffer[buf_size - 1] = '\0'; |
| 325 |
|
|
| 326 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 327 |
{ |
{ |
| 328 |
ch = igetch_t(MIN(MAX_DELAY_TIME, 60)); |
ch = igetch_t(MIN(BBS_max_user_idle_time, 60)); |
| 329 |
|
|
| 330 |
if (ch == CR) |
if (ch == CR) |
| 331 |
{ |
{ |
| 347 |
offset--; |
offset--; |
| 348 |
if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8 |
if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8 |
| 349 |
{ |
{ |
| 350 |
while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000) |
while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0) |
| 351 |
{ |
{ |
| 352 |
str_len++; |
str_len++; |
| 353 |
offset--; |
offset--; |
| 354 |
} |
} |
| 355 |
display_len--; |
|
| 356 |
col_cur--; |
if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1) |
| 357 |
|
{ |
| 358 |
|
log_error("mbstowcs() error\n"); |
| 359 |
|
} |
| 360 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 361 |
|
|
| 362 |
|
if (wc_len == 2) |
| 363 |
|
{ |
| 364 |
|
display_len--; |
| 365 |
|
col_cur--; |
| 366 |
|
} |
| 367 |
} |
} |
| 368 |
|
|
| 369 |
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)); |
| 387 |
if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character |
if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character |
| 388 |
{ |
{ |
| 389 |
str_len = 0; |
str_len = 0; |
| 390 |
c = (char)(buffer[offset] & 0b11110000); |
c = (char)(buffer[offset] & 0xf0); |
| 391 |
while (c & 0b10000000) |
while (c & 0x80) |
| 392 |
{ |
{ |
| 393 |
str_len++; |
str_len++; |
| 394 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 395 |
} |
} |
| 396 |
display_len--; |
display_len--; |
| 397 |
} |
} |
| 421 |
offset--; |
offset--; |
| 422 |
if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8 |
if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8 |
| 423 |
{ |
{ |
| 424 |
while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000) |
while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0) |
| 425 |
{ |
{ |
| 426 |
str_len++; |
str_len++; |
| 427 |
offset--; |
offset--; |
| 428 |
} |
} |
| 429 |
col_cur--; |
|
| 430 |
|
if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1) |
| 431 |
|
{ |
| 432 |
|
log_error("mbstowcs() error\n"); |
| 433 |
|
} |
| 434 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 435 |
|
|
| 436 |
|
if (wc_len == 2) |
| 437 |
|
{ |
| 438 |
|
col_cur--; |
| 439 |
|
} |
| 440 |
} |
} |
| 441 |
col_cur--; |
col_cur--; |
| 442 |
|
|
| 452 |
str_len = 0; |
str_len = 0; |
| 453 |
if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character |
if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character |
| 454 |
{ |
{ |
| 455 |
c = (char)(buffer[offset] & 0b11110000); |
c = (char)(buffer[offset] & 0xf0); |
| 456 |
while (c & 0b10000000) |
while (c & 0x80) |
| 457 |
{ |
{ |
| 458 |
str_len++; |
str_len++; |
| 459 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 460 |
|
} |
| 461 |
|
|
| 462 |
|
if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1) |
| 463 |
|
{ |
| 464 |
|
log_error("mbstowcs() error\n"); |
| 465 |
|
} |
| 466 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 467 |
|
|
| 468 |
|
if (wc_len == 2) |
| 469 |
|
{ |
| 470 |
|
col_cur++; |
| 471 |
} |
} |
|
col_cur++; |
|
| 472 |
} |
} |
| 473 |
else |
else |
| 474 |
{ |
{ |
| 514 |
else if ((ch & 0xff80) == 0x80) // head of multi-byte character |
else if ((ch & 0xff80) == 0x80) // head of multi-byte character |
| 515 |
{ |
{ |
| 516 |
str_len = 0; |
str_len = 0; |
| 517 |
c = (char)(ch & 0b11110000); |
c = (char)(ch & 0xf0); |
| 518 |
while (c & 0b10000000) |
while (c & 0x80) |
| 519 |
{ |
{ |
| 520 |
input_str[str_len] = (char)(ch - 256); |
input_str[str_len] = (char)(ch - 256); |
| 521 |
str_len++; |
str_len++; |
| 522 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 523 |
|
|
| 524 |
if ((c & 0b10000000) == 0) // Input completed |
if ((c & 0x80) == 0) // Input completed |
| 525 |
{ |
{ |
| 526 |
break; |
break; |
| 527 |
} |
} |
| 537 |
break; |
break; |
| 538 |
} |
} |
| 539 |
} |
} |
| 540 |
|
input_str[str_len] = '\0'; |
| 541 |
|
|
| 542 |
if (str_len == 0) // Incomplete input |
if (str_len == 0) // Incomplete input |
| 543 |
{ |
{ |
| 544 |
continue; |
continue; |
| 545 |
} |
} |
| 546 |
|
|
| 547 |
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) |
| 548 |
|
{ |
| 549 |
|
log_error("mbstowcs() error\n"); |
| 550 |
|
} |
| 551 |
|
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0])); |
| 552 |
|
|
| 553 |
|
if (len + str_len > buf_size - 1 || |
| 554 |
|
display_len + wc_len > max_display_len) // No enough space for Chinese character |
| 555 |
{ |
{ |
| 556 |
outc('\a'); |
outc('\a'); |
| 557 |
iflush(); |
iflush(); |
| 562 |
memcpy(buffer + offset, input_str, (size_t)str_len); |
memcpy(buffer + offset, input_str, (size_t)str_len); |
| 563 |
len += str_len; |
len += str_len; |
| 564 |
buffer[len] = '\0'; |
buffer[len] = '\0'; |
| 565 |
display_len += 2; |
display_len += wc_len; |
| 566 |
|
|
| 567 |
moveto(row, col_cur); |
moveto(row, col_cur); |
| 568 |
prints("%s", buffer + offset); |
prints("%s", buffer + offset); |
| 569 |
prints("%*s", max_display_len - display_len, ""); |
prints("%*s", max_display_len - display_len, ""); |
| 570 |
|
|
| 571 |
col_cur += 2; |
col_cur += wc_len; |
| 572 |
|
|
| 573 |
moveto(row, col_cur); |
moveto(row, col_cur); |
| 574 |
iflush(); |
iflush(); |
| 639 |
loop = 1; |
loop = 1; |
| 640 |
while (!SYS_server_exit && loop) |
while (!SYS_server_exit && loop) |
| 641 |
{ |
{ |
| 642 |
if (eof_exit > 0 && line_current >= display_line_total && display_line_total <= screen_row_total) |
if (eof_exit > 0 && line_current >= display_line_total) |
| 643 |
{ |
{ |
| 644 |
if (eof_exit == 1) |
if (eof_exit == 1) |
| 645 |
{ |
{ |
| 694 |
input_ok = 0; |
input_ok = 0; |
| 695 |
while (!SYS_server_exit && !input_ok) |
while (!SYS_server_exit && !input_ok) |
| 696 |
{ |
{ |
| 697 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(BBS_max_user_idle_time); |
| 698 |
input_ok = 1; |
input_ok = 1; |
| 699 |
|
|
| 700 |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 748 |
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 |
| 749 |
break; |
break; |
| 750 |
case CR: |
case CR: |
|
case KEY_SPACE: |
|
| 751 |
case KEY_DOWN: |
case KEY_DOWN: |
| 752 |
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 |
| 753 |
{ |
{ |
| 775 |
output_end_row = SCREEN_ROWS - 1; |
output_end_row = SCREEN_ROWS - 1; |
| 776 |
clrline(output_current_row, SCREEN_ROWS); |
clrline(output_current_row, SCREEN_ROWS); |
| 777 |
break; |
break; |
| 778 |
|
case KEY_SPACE: |
| 779 |
case KEY_PGDN: |
case KEY_PGDN: |
| 780 |
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 |
| 781 |
{ |
{ |
| 849 |
return ch; |
return ch; |
| 850 |
} |
} |
| 851 |
|
|
| 852 |
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) |
| 853 |
{ |
{ |
| 854 |
switch (*p_key) |
switch (*p_key) |
| 855 |
{ |
{ |
| 932 |
int show_bottom(const char *msg) |
int show_bottom(const char *msg) |
| 933 |
{ |
{ |
| 934 |
char str_time[LINE_BUFFER_LEN]; |
char str_time[LINE_BUFFER_LEN]; |
| 935 |
|
int len_str_time; |
| 936 |
time_t time_online; |
time_t time_online; |
| 937 |
struct tm *tm_online; |
struct tm *tm_online; |
| 938 |
char msg_f[LINE_BUFFER_LEN]; |
char msg_f[LINE_BUFFER_LEN]; |
| 939 |
int eol; |
int eol; |
| 940 |
int msg_len; |
int len_msg; |
| 941 |
int len; |
int len; |
| 942 |
int len_username; |
int len_username; |
| 943 |
char str_tm_online[LINE_BUFFER_LEN]; |
char str_tm_online[LINE_BUFFER_LEN]; |
| 944 |
|
int len_str_tm_online; |
| 945 |
|
|
| 946 |
get_time_str(str_time, sizeof(str_time)); |
len_str_time = (int)get_time_str(str_time, sizeof(str_time)); |
| 947 |
|
|
| 948 |
msg_f[0] = '\0'; |
msg_f[0] = '\0'; |
| 949 |
msg_len = 0; |
len_msg = 0; |
| 950 |
if (msg != NULL) |
if (msg != NULL) |
| 951 |
{ |
{ |
| 952 |
strncpy(msg_f, msg, sizeof(msg_f) - 1); |
strncpy(msg_f, msg, sizeof(msg_f) - 1); |
| 953 |
msg_f[sizeof(msg_f) - 1] = '\0'; |
msg_f[sizeof(msg_f) - 1] = '\0'; |
| 954 |
len = split_line(msg_f, 23, &eol, &msg_len, 1); |
len = split_line(msg_f, 23, &eol, &len_msg, 1); |
| 955 |
msg_f[len] = '\0'; |
msg_f[len] = '\0'; |
| 956 |
} |
} |
| 957 |
|
|
| 962 |
if (tm_online->tm_mday > 1) |
if (tm_online->tm_mday > 1) |
| 963 |
{ |
{ |
| 964 |
snprintf(str_tm_online, sizeof(str_tm_online), |
snprintf(str_tm_online, sizeof(str_tm_online), |
| 965 |
"\033[36m%2d\033[33m天\033[36m%2d\033[33m时", |
"\033[36m%d\033[33md \033[36m%d\033[33m:\033[36m%.2d", |
| 966 |
tm_online->tm_mday - 1, tm_online->tm_hour); |
tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min); |
| 967 |
} |
} |
| 968 |
else |
else |
| 969 |
{ |
{ |
| 970 |
snprintf(str_tm_online, sizeof(str_tm_online), |
snprintf(str_tm_online, sizeof(str_tm_online), |
| 971 |
"\033[36m%2d\033[33m时\033[36m%2d\033[33m分", |
"\033[36m%d\033[33m:\033[36m%.2d", |
| 972 |
tm_online->tm_hour, tm_online->tm_min); |
tm_online->tm_hour, tm_online->tm_min); |
| 973 |
} |
} |
| 974 |
|
len_str_tm_online = str_length(str_tm_online, 1); |
| 975 |
|
|
| 976 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 977 |
clrtoeol(); |
clrtoeol(); |
| 978 |
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", |
| 979 |
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, |
| 980 |
|
"", BBS_username, str_tm_online); |
| 981 |
|
|
| 982 |
return 0; |
return 0; |
| 983 |
} |
} |