--- lbbs/src/screen.c 2025/05/03 13:41:21 1.28 +++ lbbs/src/screen.c 2025/05/06 05:12:29 1.35 @@ -104,15 +104,14 @@ void set_input_echo(int echo) static int _str_input(char *buffer, int buffer_length, int echo_mode) { - char buf[256], ch; - int c, offset = 0, len, loop = 1, i, hz = 0; + int c, offset = 0, i, hz = 0; for (i = 0; i < buffer_length && buffer[i] != '\0'; i++) { offset++; } - while (c = igetch_t(60)) + while ((c = igetch_t(60))) { if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR) { @@ -127,9 +126,19 @@ static int _str_input(char *buffer, int { 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; @@ -140,11 +149,19 @@ static int _str_input(char *buffer, int } if (c > 127 && c <= 255) { + if (!hz && offset + 2 > buffer_length) // No enough space for Chinese character + { + igetch(1); // Cleanup remaining input + outc('\a'); + iflush(); + continue; + } hz = (!hz); } if (offset >= buffer_length) { outc('\a'); + iflush(); continue; } buffer[offset++] = (char)c; @@ -164,27 +181,29 @@ static int _str_input(char *buffer, int } } - prints("\r\n"); - iflush(); - return offset; } int str_input(char *buffer, int buffer_length, 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, buffer_length, 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 len; + igetch(1); // Cleanup input buffer + moveto(row, col); prints(prompt); prints(buffer); @@ -199,7 +218,7 @@ int display_file(const char *filename) { char buffer[LINE_BUFFER_LEN]; FILE *fin; - int i; + size_t i; if ((fin = fopen(filename, "r")) == NULL) { @@ -226,15 +245,14 @@ int display_file(const char *filename) int display_file_ex(const char *filename, int begin_line, int wait) { char buffer[LINE_BUFFER_LEN]; - char temp[LINE_BUFFER_LEN]; int ch = 0; int input_ok, line, max_lines; - long int c_line_current = 0, c_line_total = 0; + long int c_line_current = 0; + long int c_line_total = 0; FILE *fin; - struct stat f_stat; long *p_line_offsets; - int len; - int percentile; + long int len; + long int percentile; int loop = 1; if ((fin = fopen(filename, "r")) == NULL) @@ -253,7 +271,7 @@ int display_file_ex(const char *filename while (loop) { - if (c_line_current >= c_line_total) + if (c_line_current >= c_line_total && c_line_total <= screen_rows - 2) { if (wait) { @@ -268,7 +286,7 @@ int display_file_ex(const char *filename break; } - if (line >= max_lines) + if (c_line_current >= c_line_total || line >= max_lines) { if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total) { @@ -276,13 +294,13 @@ int display_file_ex(const char *filename } else { - log_error("P100 reached\n"); percentile = 100; } moveto(screen_rows, 0); - prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m │ 结束 ← │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │ \033[m", - percentile); + prints("\033[1;44;32m%s (%d%%)%s\033[33m │ 结束 ← │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │ \033[m", + (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile, + (percentile < 10 ? " " : (percentile < 100 ? " " : ""))); iflush(); input_ok = 0; @@ -301,7 +319,7 @@ int display_file_ex(const char *filename 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 + // max_lines = screen_rows - 1; // Legacy Fterm only works with this line break; case KEY_DOWN: case CR: @@ -359,9 +377,7 @@ int display_file_ex(const char *filename 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); + display_file_ex(DATA_READ_HELP, begin_line, 1); // Refresh after display help information c_line_current -= (line - 1); @@ -385,7 +401,7 @@ int display_file_ex(const char *filename log_error("Error length exceeds buffer size: %d\n", len); len = LINE_BUFFER_LEN - 1; } - if (fgets(buffer, len + 1, fin) == NULL) + if (fgets(buffer, (int)len + 1, fin) == NULL) { log_error("Reach EOF\n"); break; @@ -407,7 +423,7 @@ int show_top(char *status) { char buffer[LINE_BUFFER_LEN]; - str_space(buffer, 20 - strlen(BBS_current_section_name)); + str_space(buffer, 20 - (int)strnlen(BBS_current_section_name, sizeof(BBS_current_section_name))); moveto(1, 0); clrtoeol(); @@ -422,13 +438,12 @@ int show_top(char *status) int show_bottom(char *msg) { char str_time[LINE_BUFFER_LEN]; - char str_time_onine[20]; char buffer[LINE_BUFFER_LEN]; time_t time_online; struct tm *tm_online; get_time_str(str_time, sizeof(str_time)); - str_space(buffer, 33 - strlen(BBS_username)); + str_space(buffer, 33 - (int)strnlen(BBS_username, sizeof(BBS_username))); time_online = time(0) - BBS_login_tm; tm_online = gmtime(&time_online); @@ -451,14 +466,12 @@ int show_active_board() char buffer[LINE_BUFFER_LEN]; FILE *fin; static int line; - int len; + unsigned int len; int end_of_line; - sprintf(filename, "%sdata/active_board.txt", app_home_dir); - clrline(3, 2 + ACTIVE_BOARD_HEIGHT); - if ((fin = fopen(filename, "r")) == NULL) + if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL) { log_error("Unable to open file %s\n", filename); return -1;