--- lbbs/src/screen.c 2025/04/29 14:04:10 1.23 +++ lbbs/src/screen.c 2025/05/31 03:37:47 1.85 @@ -1,30 +1,44 @@ /*************************************************************************** screen.c - description ------------------- - begin : Mon Oct 18 2004 - copyright : (C) 2004 by Leaflet - email : leaflet@leafok.com + Copyright : (C) 2004-2025 by Leaflet + Email : leaflet@leafok.com ***************************************************************************/ /*************************************************************************** * * * 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 2 of the License, or * + * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ +#include "screen.h" #include "bbs.h" #include "common.h" +#include "str_process.h" +#include "log.h" #include "io.h" +#include "file_loader.h" +#include +#include +#include +#include +#include #include #include -#include +#include +#include + +#define _POSIX_C_SOURCE 200809L +#include #define ACTIVE_BOARD_HEIGHT 8 -int screen_lines = 24; +#define STR_TOP_LEFT_MAX_LEN 40 +#define STR_TOP_MIDDLE_MAX_LEN 20 +#define STR_TOP_RIGHT_MAX_LEN 20 void moveto(int row, int col) { @@ -36,13 +50,11 @@ void moveto(int row, int col) { prints("\r"); } - iflush(); } void clrtoeol() { - prints("\033[K"); - iflush(); + prints(CTRL_SEQ_CLR_LINE); } void clrline(int line_begin, int line_end) @@ -52,15 +64,14 @@ void clrline(int line_begin, int line_en for (i = line_begin; i <= line_end; i++) { moveto(i, 0); - prints("\033[K"); + prints(CTRL_SEQ_CLR_LINE); } - - iflush(); } void clrtobot(int line_begin) { - clrline(line_begin, screen_lines); + moveto(line_begin, 0); + prints("\033[J"); moveto(line_begin, 0); } @@ -68,18 +79,17 @@ void clearscr() { prints("\033[2J"); moveto(0, 0); - iflush(); } int press_any_key() { - moveto(screen_lines, 0); + moveto(SCREEN_ROWS, 0); clrtoeol(); prints(" \033[1;33m按任意键继续...\033[0;37m"); iflush(); - return igetch_t(60); + return igetch_t(MIN(MAX_DELAY_TIME, 60)); } void set_input_echo(int echo) @@ -94,57 +104,87 @@ void set_input_echo(int echo) // outc ('\x85'); // ASCII code 133 prints("\xff\xfb\x01\xff\xfb\x03"); iflush(); - igetch_t(60); - igetch_t(60); + igetch(0); + igetch_reset(); } } -int _str_input(char *buffer, int buffer_length, int echo_mode) +static int _str_input(char *buffer, int buf_size, int echo_mode) { - char buf[256], ch; - int c, offset = 0, len, loop = 1, i, hz = 0; + int c; + int offset = 0; + int hz = 0; - for (i = 0; i < buffer_length && buffer[i] != '\0'; i++) - { - offset++; - } + buffer[buf_size - 1] = '\0'; + for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++) + ; - while (c = igetch_t(60)) + igetch_reset(); + + while (!SYS_server_exit) { - if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR) + c = igetch_t(MIN(MAX_DELAY_TIME, 60)); + + if (c == CR) { + igetch_reset(); break; } - if (c == LF) + else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe + { + return -1; + } + else if (c == LF || c == '\0') { continue; } - if (c == BACKSPACE) + else if (c == BACKSPACE) { if (offset > 0) { - buffer[--offset] = '\0'; - prints("\b \b"); - // clrtoeol (); + offset--; + if (buffer[offset] < 0 || buffer[offset] > 127) + { + prints("\033[D \033[D"); + offset--; + if (offset < 0) // should not happen + { + log_error("Offset of buffer is negative\n"); + offset = 0; + } + } + buffer[offset] = '\0'; + prints("\033[D \033[D"); iflush(); } continue; } - if (c > 255 || iscntrl(c)) + else if (c > 255 || iscntrl(c)) { continue; } - if (c > 127 && c <= 255) + else if (c > 127 && c <= 255) { + if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character + { + igetch(0); // Ignore 1 character + outc('\a'); + iflush(); + continue; + } hz = (!hz); } - if (offset >= buffer_length) + + if (offset + 1 > buf_size - 1) { outc('\a'); + iflush(); continue; } + buffer[offset++] = (char)c; buffer[offset] = '\0'; + switch (echo_mode) { case DOECHO: @@ -160,323 +200,433 @@ int _str_input(char *buffer, int buffer_ } } - prints("\r\n"); - iflush(); - return offset; } -int str_input(char *buffer, int buffer_length, int echo_mode) +int str_input(char *buffer, int buf_size, int echo_mode) { - int offset; + int len; - memset(buffer, '\0', buffer_length); + buffer[0] = '\0'; - offset = _str_input(buffer, buffer_length, echo_mode); + len = _str_input(buffer, buf_size, echo_mode); - return offset; + prints("\r\n"); + iflush(); + + return len; }; -int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode) +int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int echo_mode) { int len; moveto(row, col); - iflush(); - prints(prompt); - prints(buffer); + prints("%s", prompt); + prints("%s", buffer); iflush(); - len = _str_input(buffer, buffer_length, echo_mode); + len = _str_input(buffer, buf_size, echo_mode); return len; } -int display_file(const char *filename) +int display_data(const void *p_data, long line_total, const long *p_line_offsets, int begin_line, int eof_exit, + display_data_key_handler key_handler, const char *help_filename) { - char buffer[260]; - FILE *fin; - int i; + static int show_help = 1; + char buffer[LINE_BUFFER_LEN]; + DISPLAY_CTX ctx; + int ch = 0; + int input_ok, line, max_lines; + long int line_current = 0; + long int len; + long int percentile; + int loop; + int eol, display_len; - if ((fin = fopen(filename, "r")) == NULL) + clrline(begin_line, SCREEN_ROWS); + line = begin_line; + max_lines = SCREEN_ROWS - 1; + + // update msg_ext with extended key handler + if (key_handler(&ch, &ctx) != 0) { - return -1; + return ch; } - while (fgets(buffer, 255, fin)) + loop = 1; + while (!SYS_server_exit && loop) { - i = strlen(buffer); - if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r') + if (eof_exit > 0 && line_current >= line_total && line_total <= SCREEN_ROWS - 2) { - buffer[i - 1] = '\r'; - buffer[i] = '\n'; - buffer[i + 1] = '\0'; + if (eof_exit == 1) + { + ch = press_any_key(); + } + else // if (eof_exit == 2) + { + iflush(); + } + + loop = 0; + break; } - prints(buffer); - iflush(); - } - fclose(fin); - return 0; -} + if (line_current >= line_total || line >= max_lines) + { + ctx.reach_begin = (line_current < line ? 1 : 0); -int display_file_ex(const char *filename, int begin_line, int wait) -{ - char buffer[260], temp[256]; - int i, ch, input_ok, max_lines; - long int line, c_line_begin = 0, c_line_total = 0; - long int f_line, f_size, f_offset; - FILE *fin; - struct stat f_stat; + if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total) + { + percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total; + ctx.reach_end = 0; + } + else + { + percentile = 100; + ctx.reach_end = 1; + } - max_lines = screen_lines - 1; - clrline(begin_line, screen_lines); - line = begin_line; - moveto(line, 0); + ctx.line_top = line_current - (line - 1) + 1; + ctx.line_bottom = MIN(line_current - (line - 1) + (SCREEN_ROWS - 2), line_total); - if ((fin = fopen(filename, "r")) != NULL) - { - if (fstat(fileno(fin), &f_stat) != 0) - { - log_error("Get file stat failed\n"); - return -1; - } - f_size = f_stat.st_size; + snprintf(buffer, sizeof(buffer), + "\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s", + ctx.line_top, + ctx.line_bottom, + percentile, + ctx.msg); - while (fgets(buffer, 255, fin)) - c_line_total++; - rewind(fin); + len = split_line(buffer, SCREEN_COLS, &eol, &display_len); + for (; display_len < SCREEN_COLS; display_len++) + { + buffer[len++] = ' '; + } + buffer[len] = '\0'; + strncat(buffer, "\033[m", sizeof(buffer) - 1 - strnlen(buffer, sizeof(buffer))); - while (fgets(buffer, 255, fin)) - { - if (line >= max_lines) + moveto(SCREEN_ROWS, 0); + prints("%s", buffer); + iflush(); + + input_ok = 0; + while (!SYS_server_exit && !input_ok) { - f_offset = ftell(fin); + ch = igetch_t(MAX_DELAY_TIME); + input_ok = 1; - moveto(screen_lines, 0); - prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m │ 结束 ← │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │ \033[m", - (f_offset - strlen(buffer)) * 100 / f_size); - iflush(); + // extended key handler + if (key_handler(&ch, &ctx) != 0) + { + goto cleanup; + } - input_ok = 0; - while (!input_ok) + switch (ch) { - ch = igetch_t(MAX_DELAY_TIME); - input_ok = 1; - switch (ch) + case KEY_NULL: + case KEY_TIMEOUT: + goto cleanup; + case KEY_HOME: + if (line_current - line < 0) // Reach begin { - case KEY_UP: - c_line_begin--; - if (c_line_begin >= 0) - { - rewind(fin); - for (f_line = 0; f_line < c_line_begin; f_line++) - { - if (fgets(buffer, 255, fin) == NULL) - goto exit; - } - } - else - { - goto exit; - } break; - case KEY_DOWN: - case CR: - c_line_begin++; - rewind(fin); - for (f_line = 0; f_line < c_line_begin; f_line++) - { - if (fgets(buffer, 255, fin) == NULL) - goto exit; - } + } + line_current = 0; + line = begin_line; + max_lines = SCREEN_ROWS - 1; + clrline(begin_line, SCREEN_ROWS); + break; + case KEY_END: + if (line_total < SCREEN_ROWS - 2) + { break; - case KEY_PGUP: - case Ctrl('B'): - if (c_line_begin > 0) - c_line_begin -= (max_lines - begin_line - 1); - else - goto exit; - if (c_line_begin < 0) - c_line_begin = 0; - rewind(fin); - for (f_line = 0; f_line < c_line_begin; f_line++) - { - if (fgets(buffer, 255, fin) == NULL) - goto exit; - } + } + line_current = line_total - (SCREEN_ROWS - 2); + line = begin_line; + max_lines = SCREEN_ROWS - 1; + clrline(begin_line, SCREEN_ROWS); + break; + case KEY_UP: + if (line_current - line < 0) // Reach begin + { break; - case KEY_RIGHT: - case KEY_PGDN: - case Ctrl('F'): - case KEY_SPACE: - c_line_begin += (max_lines - begin_line - 1); - if (c_line_begin + (max_lines - begin_line) > - c_line_total) - c_line_begin = - c_line_total - (max_lines - begin_line); - rewind(fin); - for (f_line = 0; f_line < c_line_begin; f_line++) - { - if (fgets(buffer, 255, fin) == NULL) - goto exit; - } + } + line_current -= line; + line = begin_line; + // max_lines = begin_line + 1; + // prints("\033[T"); // Scroll down 1 line + max_lines = SCREEN_ROWS - 1; // Legacy Fterm only works with this line + break; + case CR: + igetch_reset(); + case KEY_SPACE: + case KEY_DOWN: + if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach end + { break; - case KEY_NULL: - case KEY_TIMEOUT: - case KEY_LEFT: - case 'q': - case 'Q': - goto exit; + } + line_current += ((SCREEN_ROWS - 2) - (line - 1)); + line = SCREEN_ROWS - 2; + max_lines = SCREEN_ROWS - 1; + moveto(SCREEN_ROWS, 0); + clrtoeol(); + prints("\033[S"); // Scroll up 1 line + break; + case KEY_PGUP: + if (line_current - line < 0) // Reach begin + { break; - case '?': - case 'h': - case 'H': - // Display help information - strcpy(temp, app_home_dir); - strcat(temp, "data/read_help.txt"); - display_file_ex(temp, begin_line, 1); - - // Refresh after display help information - rewind(fin); - for (f_line = 0; f_line < c_line_begin; f_line++) - { - if (fgets(buffer, 255, fin) == NULL) - goto exit; - } + } + line_current -= ((SCREEN_ROWS - 3) + (line - 1)); + if (line_current < 0) + { + line_current = 0; + } + line = begin_line; + max_lines = SCREEN_ROWS - 1; + clrline(begin_line, SCREEN_ROWS); + break; + case KEY_PGDN: + if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach end + { break; - default: - input_ok = 0; + } + line_current += (SCREEN_ROWS - 3) - (line - 1); + if (line_current + SCREEN_ROWS - 2 > line_total) // No enough lines to display + { + line_current = line_total - (SCREEN_ROWS - 2); + } + line = begin_line; + max_lines = SCREEN_ROWS - 1; + clrline(begin_line, SCREEN_ROWS); + break; + case KEY_ESC: + case KEY_LEFT: + loop = 0; + break; + case 'h': + if (!show_help) // Not reentrant + { break; } - } - clrline(begin_line, screen_lines); - line = begin_line; - moveto(line, 0); + // Display help information + show_help = 0; + display_file(help_filename, begin_line, 1); + show_help = 1; + case KEY_F5: + // Refresh after display help information + line_current -= (line - 1); + line = begin_line; + max_lines = SCREEN_ROWS - 1; + clrline(begin_line, SCREEN_ROWS); + break; + case 0: // Refresh bottom line + break; + default: + input_ok = 0; + break; + } - continue; + BBS_last_access_tm = time(0); } - i = strlen(buffer); - if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r') - { - buffer[i - 1] = '\r'; - buffer[i] = '\n'; - buffer[i + 1] = '\0'; - } - prints(buffer); - iflush(); + continue; + } - line++; + len = p_line_offsets[line_current + 1] - p_line_offsets[line_current]; + if (len >= LINE_BUFFER_LEN) + { + log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n", + len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); + len = LINE_BUFFER_LEN - 1; } - if (wait) - ch = press_any_key(); - else - ch = 0; - exit: - fclose(fin); + memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len); + buffer[len] = '\0'; - return ch; + moveto(line, 0); + clrtoeol(); + prints("%s", buffer); + line_current++; + line++; + } + +cleanup: + return ch; +} + +static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx) +{ + switch (*p_key) + { + case 0: // Set msg + snprintf(p_ctx->msg, sizeof(p_ctx->msg), + "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ " + "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ " + "帮助[\033[32mh\033[33m] |"); + break; + } + + return 0; +} + +int display_file(const char *filename, int begin_line, int eof_exit) +{ + int ret; + const void *p_shm; + size_t data_len; + long line_total; + const void *p_data; + const long *p_line_offsets; + + if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL) + { + log_error("get_file_shm(%s) error\n", filename); + return KEY_NULL; + } + + ret = display_data(p_data, line_total, p_line_offsets, begin_line, eof_exit, display_file_key_handler, DATA_READ_HELP); + + if (detach_file_shm(p_shm) < 0) + { + log_error("detach_file_shm(%s) error\n", filename); } - return -1; + return ret; } -int show_top(char *status) +int show_top(const char *str_left, const char *str_middle, const char *str_right) { - char buffer[256]; + char str_left_f[STR_TOP_LEFT_MAX_LEN + 1]; + char str_middle_f[STR_TOP_MIDDLE_MAX_LEN + 1]; + char str_right_f[STR_TOP_RIGHT_MAX_LEN + 1]; + int str_left_len; + int str_middle_len; + int str_right_len; + int eol; + int len; - str_space(buffer, 20 - strlen(BBS_current_section_name)); + strncpy(str_left_f, str_left, sizeof(str_left_f) - 1); + str_left_f[sizeof(str_left_f) - 1] = '\0'; + len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN, &eol, &str_left_len); + str_left_f[len] = '\0'; + + strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1); + str_middle_f[sizeof(str_middle_f) - 1] = '\0'; + len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN, &eol, &str_middle_len); + str_middle_f[len] = '\0'; + + strncpy(str_right_f, str_right, sizeof(str_right_f) - 1); + str_right_f[sizeof(str_right_f) - 1] = '\0'; + len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN, &eol, &str_right_len); + str_right_f[len] = '\0'; moveto(1, 0); - prints("\033[1;44;33m%-20s \033[37m%20s" - " %s\033[33m讨论区 [%s]\033[m", - status, BBS_name, buffer, BBS_current_section_name); - iflush(); + clrtoeol(); + prints("\033[1;44;33m%s\033[37m%*s\033[33m%*s\033[m", + str_left_f, 44 - str_left_len, str_middle_f, 36, str_right_f); return 0; } -int show_bottom(char *msg) +int show_bottom(const char *msg) { - char str_time[256], str_time_onine[20], buffer[256]; + char str_time[LINE_BUFFER_LEN]; time_t time_online; struct tm *tm_online; + char msg_f[LINE_BUFFER_LEN]; + int eol; + int msg_len; + int len; + int len_username; - get_time_str(str_time, 256); - str_space(buffer, 33 - strlen(BBS_username)); + get_time_str(str_time, sizeof(str_time)); + + msg_f[0] = '\0'; + msg_len = 0; + if (msg != NULL) + { + strncpy(msg_f, msg, sizeof(msg_f) - 1); + msg_f[sizeof(msg_f) - 1] = '\0'; + len = split_line(msg_f, 23, &eol, &msg_len); + msg_f[len] = '\0'; + } + + len_username = (int)strnlen(BBS_username, sizeof(BBS_username)); time_online = time(0) - BBS_login_tm; tm_online = gmtime(&time_online); - moveto(screen_lines, 0); - prints("\033[1;44;33m[\033[36m%s\033[33m]" - "%s帐号[\033[36m%s\033[33m]" - "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m", - str_time, buffer, BBS_username, tm_online->tm_mday - 1, - tm_online->tm_hour, tm_online->tm_min); - iflush(); + moveto(SCREEN_ROWS, 0); + clrtoeol(); + prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m帐号[\033[36m%s\033[33m]" + "[\033[36m%1d\033[33m天\033[36m%2d\033[33m时\033[36m%2d\033[33m分]\033[m", + str_time, msg_f, 35 - msg_len - len_username, "", BBS_username, + tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min); return 0; } int show_active_board() { - char filename[256], buffer[260]; - FILE *fin; - int i, j; - static long int line; + static int line_current = 0; + static const void *p_shm = NULL; + static size_t data_len; + static long line_total; + static const void *p_data; + static const long *p_line_offsets; - sprintf(filename, "%sdata/active_board.txt", app_home_dir); + static time_t t_last_show = 0; + static int line_last = 0; - clrline(3, 2 + ACTIVE_BOARD_HEIGHT); + char buffer[LINE_BUFFER_LEN]; + long int len; - moveto(3, 0); + if (p_shm == NULL) + { + if ((p_shm = get_file_shm_readonly(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL) + { + log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD); + return KEY_NULL; + } + } - if ((fin = fopen(filename, "r")) != NULL) + if (time(0) - t_last_show >= 10) { - for (j = 0; j < line; j++) + line_last = line_current; + t_last_show = time(0); + } + else + { + line_current = line_last; + } + + clrline(2, 2 + ACTIVE_BOARD_HEIGHT); + + for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++) + { + len = p_line_offsets[line_current + 1] - p_line_offsets[line_current]; + if (len >= LINE_BUFFER_LEN) { - if (fgets(buffer, 255, fin) == NULL) - { - line = 0; - rewind(fin); - break; - } + log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n", + len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); + len = LINE_BUFFER_LEN - 1; } - for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++) + memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len); + buffer[len] = '\0'; + + moveto(3 + i, 0); + prints("%s", buffer); + + line_current++; + if (line_current >= line_total) { - if (fgets(buffer, 255, fin) == NULL) - { - line = 0; - if (j == 0) - { - rewind(fin); - if (fgets(buffer, 255, fin) == NULL) - { - break; - } - } - else - { - break; - } - } - line++; - i = strlen(buffer); - if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r') - { - buffer[i - 1] = '\r'; - buffer[i] = '\n'; - buffer[i + 1] = '\0'; - } - prints(buffer); - iflush(); + line_current = 0; + break; } - fclose(fin); } return 0;