| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
screen.c - description |
screen.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
begin : Mon Oct 18 2004 |
| 5 |
copyright : (C) 2004 by Leaflet |
copyright : (C) 2004 by Leaflet |
| 6 |
email : leaflet@leafok.com |
email : leaflet@leafok.com |
| 7 |
***************************************************************************/ |
***************************************************************************/ |
| 8 |
|
|
| 9 |
/*************************************************************************** |
/*************************************************************************** |
| 17 |
|
|
| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
|
#include "str_process.h" |
| 21 |
|
#include "log.h" |
| 22 |
#include "io.h" |
#include "io.h" |
| 23 |
|
#include "screen.h" |
| 24 |
|
#include <string.h> |
| 25 |
|
#include <ctype.h> |
| 26 |
#include <sys/types.h> |
#include <sys/types.h> |
| 27 |
#include <sys/stat.h> |
#include <sys/stat.h> |
| 28 |
#include <unistd.h> |
#include <unistd.h> |
| 29 |
|
#include <stdlib.h> |
| 30 |
|
|
| 31 |
#define ACTIVE_BOARD_HEIGHT 8 |
#define ACTIVE_BOARD_HEIGHT 8 |
| 32 |
|
|
| 33 |
int screen_lines = 24; |
int screen_rows = 24; |
| 34 |
|
int screen_cols = 80; |
| 35 |
|
|
| 36 |
void |
void moveto(int row, int col) |
|
moveto (int row, int col) |
|
| 37 |
{ |
{ |
| 38 |
if (row >= 0) |
if (row >= 0) |
| 39 |
{ |
{ |
| 40 |
prints ("\033[%d;%dH", row, col); |
prints("\033[%d;%dH", row, col); |
| 41 |
} |
} |
| 42 |
else |
else |
| 43 |
{ |
{ |
| 44 |
prints ("\r"); |
prints("\r"); |
| 45 |
} |
} |
|
iflush (); |
|
| 46 |
} |
} |
| 47 |
|
|
| 48 |
void |
void clrtoeol() |
|
clrtoeol () |
|
| 49 |
{ |
{ |
| 50 |
prints ("\033[K"); |
prints("\033[K"); |
|
iflush (); |
|
| 51 |
} |
} |
| 52 |
|
|
| 53 |
void |
void clrline(int line_begin, int line_end) |
|
clrline (int line_begin, int line_end) |
|
| 54 |
{ |
{ |
| 55 |
int i; |
int i; |
|
|
|
|
for (i = line_begin; i <= line_end; i++) |
|
|
{ |
|
|
moveto (i, 0); |
|
|
prints ("\033[K"); |
|
|
} |
|
| 56 |
|
|
| 57 |
iflush (); |
for (i = line_begin; i <= line_end; i++) |
| 58 |
|
{ |
| 59 |
|
moveto(i, 0); |
| 60 |
|
prints("\033[K"); |
| 61 |
|
} |
| 62 |
} |
} |
| 63 |
|
|
| 64 |
void |
void clrtobot(int line_begin) |
|
clrtobot (int line_begin) |
|
| 65 |
{ |
{ |
| 66 |
clrline (line_begin, screen_lines); |
moveto(line_begin, 0); |
| 67 |
moveto (line_begin, 0); |
prints("\033[J"); |
| 68 |
|
moveto(line_begin, 0); |
| 69 |
} |
} |
| 70 |
|
|
| 71 |
void |
void clearscr() |
|
clearscr () |
|
| 72 |
{ |
{ |
| 73 |
prints ("\33[2J"); |
prints("\033[2J"); |
| 74 |
moveto (0, 0); |
moveto(0, 0); |
|
iflush (); |
|
| 75 |
} |
} |
| 76 |
|
|
| 77 |
int |
int press_any_key() |
|
press_any_key () |
|
| 78 |
{ |
{ |
| 79 |
moveto (screen_lines, 0); |
moveto(screen_rows, 0); |
| 80 |
clrtoeol (); |
clrtoeol(); |
| 81 |
|
|
| 82 |
prints (" \033[1;33m按任意键继续...\033[0;37m"); |
prints(" \033[1;33m按任意键继续...\033[0;37m"); |
| 83 |
iflush (); |
iflush(); |
| 84 |
|
|
| 85 |
return igetch_t (60); |
return igetch_t(60); |
|
} |
|
|
|
|
|
void |
|
|
set_input_echo (int echo) |
|
|
{ |
|
|
if (echo) |
|
|
{ |
|
|
outc ('\x83'); // ASCII code 131 |
|
|
iflush (); |
|
|
} |
|
|
else |
|
|
{ |
|
|
// outc ('\x85'); // ASCII code 133 |
|
|
prints ("\xff\xfb\x01\xff\xfb\x03"); |
|
|
iflush (); |
|
|
igetch_t (60); |
|
|
igetch_t (60); |
|
|
} |
|
| 86 |
} |
} |
| 87 |
|
|
| 88 |
int |
void set_input_echo(int echo) |
|
str_input (char *buffer, int buffer_length, int echo_mode) |
|
| 89 |
{ |
{ |
| 90 |
char buf[256], ch; |
if (echo) |
|
int c, offset = 0, len, loop = 1, i, hz = 0; |
|
|
|
|
|
memset (buffer, '\0', buffer_length); |
|
|
|
|
|
while (c = igetch_t (60)) |
|
|
{ |
|
|
if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR) |
|
|
break; |
|
|
if (c == LF) |
|
|
continue; |
|
|
if (c == BACKSPACE) |
|
| 91 |
{ |
{ |
| 92 |
if (offset > 0) |
outc('\x83'); // ASCII code 131 |
| 93 |
{ |
iflush(); |
|
buffer[--offset] = '\0'; |
|
|
prints ("\b \b"); |
|
|
// clrtoeol (); |
|
|
iflush (); |
|
|
} |
|
|
continue; |
|
| 94 |
} |
} |
| 95 |
if (c > 255 || iscntrl (c)) |
else |
| 96 |
{ |
{ |
| 97 |
continue; |
// outc ('\x85'); // ASCII code 133 |
| 98 |
|
prints("\xff\xfb\x01\xff\xfb\x03"); |
| 99 |
|
iflush(); |
| 100 |
|
igetch(0); |
| 101 |
|
igetch(1); |
| 102 |
} |
} |
| 103 |
if (c > 127 && c <= 255) |
} |
| 104 |
{ |
|
| 105 |
hz = (!hz); |
static int _str_input(char *buffer, int buffer_length, int echo_mode) |
| 106 |
} |
{ |
| 107 |
if (offset >= buffer_length) |
char buf[256], ch; |
| 108 |
{ |
int c, offset = 0, len, loop = 1, i, hz = 0; |
| 109 |
outc ('\a'); |
|
| 110 |
continue; |
for (i = 0; i < buffer_length && buffer[i] != '\0'; i++) |
|
} |
|
|
buffer[offset++] = (char) c; |
|
|
buffer[offset] = '\0'; |
|
|
switch (echo_mode) |
|
| 111 |
{ |
{ |
| 112 |
case 0: |
offset++; |
|
outc ((char) c); |
|
|
break; |
|
|
case 1: |
|
|
outc ('*'); |
|
|
break; |
|
| 113 |
} |
} |
| 114 |
if (!hz) |
|
| 115 |
|
while (c = igetch_t(60)) |
| 116 |
{ |
{ |
| 117 |
iflush (); |
if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR) |
| 118 |
|
{ |
| 119 |
|
igetch(1); // Cleanup remaining '\n' in the buffer |
| 120 |
|
break; |
| 121 |
|
} |
| 122 |
|
if (c == LF) |
| 123 |
|
{ |
| 124 |
|
continue; |
| 125 |
|
} |
| 126 |
|
if (c == BACKSPACE) |
| 127 |
|
{ |
| 128 |
|
if (offset > 0) |
| 129 |
|
{ |
| 130 |
|
buffer[--offset] = '\0'; |
| 131 |
|
prints("\b \b"); |
| 132 |
|
// clrtoeol (); |
| 133 |
|
iflush(); |
| 134 |
|
} |
| 135 |
|
continue; |
| 136 |
|
} |
| 137 |
|
if (c > 255 || iscntrl(c)) |
| 138 |
|
{ |
| 139 |
|
continue; |
| 140 |
|
} |
| 141 |
|
if (c > 127 && c <= 255) |
| 142 |
|
{ |
| 143 |
|
hz = (!hz); |
| 144 |
|
} |
| 145 |
|
if (offset >= buffer_length) |
| 146 |
|
{ |
| 147 |
|
outc('\a'); |
| 148 |
|
continue; |
| 149 |
|
} |
| 150 |
|
buffer[offset++] = (char)c; |
| 151 |
|
buffer[offset] = '\0'; |
| 152 |
|
switch (echo_mode) |
| 153 |
|
{ |
| 154 |
|
case DOECHO: |
| 155 |
|
outc((char)c); |
| 156 |
|
break; |
| 157 |
|
case NOECHO: |
| 158 |
|
outc('*'); |
| 159 |
|
break; |
| 160 |
|
} |
| 161 |
|
if (!hz) |
| 162 |
|
{ |
| 163 |
|
iflush(); |
| 164 |
|
} |
| 165 |
} |
} |
|
} |
|
| 166 |
|
|
| 167 |
prints ("\r\n"); |
prints("\r\n"); |
| 168 |
iflush (); |
iflush(); |
| 169 |
|
|
| 170 |
return offset; |
return offset; |
| 171 |
} |
} |
| 172 |
|
|
| 173 |
int |
int str_input(char *buffer, int buffer_length, int echo_mode) |
| 174 |
get_data (int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode) |
{ |
| 175 |
|
int offset; |
| 176 |
|
|
| 177 |
|
memset(buffer, '\0', buffer_length); |
| 178 |
|
|
| 179 |
|
offset = _str_input(buffer, buffer_length, echo_mode); |
| 180 |
|
|
| 181 |
|
return offset; |
| 182 |
|
}; |
| 183 |
|
|
| 184 |
|
int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode) |
| 185 |
{ |
{ |
| 186 |
int len; |
int len; |
| 187 |
|
|
| 188 |
|
moveto(row, col); |
| 189 |
|
prints(prompt); |
| 190 |
|
prints(buffer); |
| 191 |
|
iflush(); |
| 192 |
|
|
| 193 |
moveto (row, col); |
len = _str_input(buffer, buffer_length, echo_mode); |
|
iflush (); |
|
|
prints (prompt); |
|
|
prints (buffer); |
|
|
iflush (); |
|
| 194 |
|
|
| 195 |
len = str_input (buffer, buffer_length, echo_mode); |
return len; |
|
|
|
|
return len; |
|
| 196 |
} |
} |
| 197 |
|
|
| 198 |
int |
int display_file(const char *filename) |
|
display_file (const char *filename) |
|
| 199 |
{ |
{ |
| 200 |
char buffer[260]; |
char buffer[LINE_BUFFER_LEN]; |
| 201 |
FILE *fin; |
FILE *fin; |
| 202 |
int i; |
int i; |
| 203 |
|
|
| 204 |
if ((fin = fopen (filename, "r")) == NULL) |
if ((fin = fopen(filename, "r")) == NULL) |
| 205 |
{ |
{ |
| 206 |
return -1; |
return -1; |
| 207 |
} |
} |
| 208 |
|
|
| 209 |
while (fgets (buffer, 255, fin)) |
while (fgets(buffer, sizeof(buffer) - 1, fin)) |
|
{ |
|
|
i = strlen (buffer); |
|
|
if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r') |
|
| 210 |
{ |
{ |
| 211 |
buffer[i - 1] = '\r'; |
i = strnlen(buffer, sizeof(buffer) - 1); |
| 212 |
buffer[i] = '\n'; |
if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r') |
| 213 |
buffer[i + 1] = '\0'; |
{ |
| 214 |
|
buffer[i - 1] = '\r'; |
| 215 |
|
buffer[i] = '\n'; |
| 216 |
|
buffer[i + 1] = '\0'; |
| 217 |
|
} |
| 218 |
|
prints(buffer); |
| 219 |
|
iflush(); |
| 220 |
} |
} |
| 221 |
prints (buffer); |
fclose(fin); |
|
iflush (); |
|
|
} |
|
|
fclose (fin); |
|
| 222 |
|
|
| 223 |
return 0; |
return 0; |
| 224 |
} |
} |
| 225 |
|
|
| 226 |
int |
int display_file_ex(const char *filename, int begin_line, int wait) |
|
display_file_ex (const char *filename, int begin_line, int wait) |
|
| 227 |
{ |
{ |
| 228 |
char buffer[260], temp[256]; |
char buffer[LINE_BUFFER_LEN]; |
| 229 |
int i, ch, input_ok; |
char temp[LINE_BUFFER_LEN]; |
| 230 |
long int line, c_line_begin = 0, c_line_total = 0; |
int ch = 0; |
| 231 |
long int f_line, f_size, f_offset; |
int input_ok, line, max_lines; |
| 232 |
FILE *fin; |
long int c_line_current = 0, c_line_total = 0; |
| 233 |
struct stat f_stat; |
FILE *fin; |
| 234 |
|
struct stat f_stat; |
| 235 |
clrline (begin_line, screen_lines); |
long *p_line_offsets; |
| 236 |
line = begin_line; |
int len; |
| 237 |
moveto (line, 0); |
int percentile; |
| 238 |
|
int loop = 1; |
| 239 |
|
|
| 240 |
if ((fin = fopen (filename, "r")) != NULL) |
if ((fin = fopen(filename, "r")) == NULL) |
|
{ |
|
|
if (fstat (fileno (fin), &f_stat) != 0) |
|
| 241 |
{ |
{ |
| 242 |
log_error ("Get file stat failed\n"); |
log_error("Unable to open file %s\n", filename); |
| 243 |
return -1; |
return -1; |
| 244 |
} |
} |
|
f_size = f_stat.st_size; |
|
| 245 |
|
|
| 246 |
while (fgets (buffer, 255, fin)) |
p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES); |
|
c_line_total++; |
|
|
rewind (fin); |
|
| 247 |
|
|
| 248 |
while (fgets (buffer, 255, fin)) |
c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES); |
|
{ |
|
|
if (line >= screen_lines) |
|
|
{ |
|
|
f_offset = ftell (fin); |
|
| 249 |
|
|
| 250 |
moveto (screen_lines, 0); |
clrline(begin_line, screen_rows); |
| 251 |
prints |
line = begin_line; |
| 252 |
("\033[1;44;32m下面还有喔 (%d%%)\033[33m │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │ \033[m", |
max_lines = screen_rows - 1; |
|
(f_offset - strlen (buffer)) * 100 / f_size); |
|
|
iflush (); |
|
| 253 |
|
|
| 254 |
input_ok = 0; |
while (loop) |
| 255 |
while (!input_ok) |
{ |
| 256 |
|
if (c_line_current >= c_line_total) |
| 257 |
{ |
{ |
| 258 |
ch = igetch_t (MAX_DELAY_TIME); |
if (wait) |
|
input_ok = 1; |
|
|
switch (ch) |
|
|
{ |
|
|
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++) |
|
| 259 |
{ |
{ |
| 260 |
if (fgets (buffer, 255, fin) == NULL) |
ch = press_any_key(); |
|
goto exit; |
|
| 261 |
} |
} |
| 262 |
break; |
else |
|
case KEY_PGUP: |
|
|
case Ctrl ('B'): |
|
|
if (c_line_begin > 0) |
|
|
c_line_begin -= (screen_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++) |
|
| 263 |
{ |
{ |
| 264 |
if (fgets (buffer, 255, fin) == NULL) |
iflush(); |
|
goto exit; |
|
| 265 |
} |
} |
| 266 |
break; |
|
| 267 |
case KEY_RIGHT: |
loop = 0; |
| 268 |
case KEY_PGDN: |
break; |
| 269 |
case Ctrl ('F'): |
} |
| 270 |
case KEY_SPACE: |
|
| 271 |
c_line_begin += (screen_lines - begin_line - 1); |
if (line >= max_lines) |
| 272 |
if (c_line_begin + (screen_lines - begin_line) > |
{ |
| 273 |
c_line_total) |
if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total) |
|
c_line_begin = |
|
|
c_line_total - (screen_lines - begin_line); |
|
|
rewind (fin); |
|
|
for (f_line = 0; f_line < c_line_begin; f_line++) |
|
| 274 |
{ |
{ |
| 275 |
if (fgets (buffer, 255, fin) == NULL) |
percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total; |
|
goto exit; |
|
| 276 |
} |
} |
| 277 |
break; |
else |
|
case KEY_NULL: |
|
|
case KEY_TIMEOUT: |
|
|
case KEY_LEFT: |
|
|
case 'q': |
|
|
case 'Q': |
|
|
goto exit; |
|
|
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++) |
|
| 278 |
{ |
{ |
| 279 |
if (fgets (buffer, 255, fin) == NULL) |
log_error("P100 reached\n"); |
| 280 |
goto exit; |
percentile = 100; |
| 281 |
} |
} |
|
break; |
|
|
default: |
|
|
input_ok = 0; |
|
|
break; |
|
|
} |
|
|
} |
|
| 282 |
|
|
| 283 |
clrline (begin_line, screen_lines); |
moveto(screen_rows, 0); |
| 284 |
line = begin_line; |
prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │ \033[m", |
| 285 |
moveto (line, 0); |
percentile); |
| 286 |
|
iflush(); |
| 287 |
|
|
| 288 |
continue; |
input_ok = 0; |
| 289 |
} |
while (!input_ok) |
| 290 |
|
{ |
| 291 |
|
ch = igetch_t(MAX_DELAY_TIME); |
| 292 |
|
input_ok = 1; |
| 293 |
|
switch (ch) |
| 294 |
|
{ |
| 295 |
|
case KEY_UP: |
| 296 |
|
if (c_line_current - line < 0) // Reach top |
| 297 |
|
{ |
| 298 |
|
break; |
| 299 |
|
} |
| 300 |
|
c_line_current -= line; |
| 301 |
|
line = begin_line; |
| 302 |
|
max_lines = begin_line + 1; |
| 303 |
|
prints("\033[T"); // Scroll down 1 line |
| 304 |
|
//max_lines = screen_rows - 1; // Legacy Fterm only works with this line |
| 305 |
|
break; |
| 306 |
|
case KEY_DOWN: |
| 307 |
|
case CR: |
| 308 |
|
if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom |
| 309 |
|
{ |
| 310 |
|
break; |
| 311 |
|
} |
| 312 |
|
c_line_current += ((screen_rows - 2) - (line - 1)); |
| 313 |
|
line = screen_rows - 2; |
| 314 |
|
max_lines = screen_rows - 1; |
| 315 |
|
moveto(screen_rows, 0); |
| 316 |
|
clrtoeol(); |
| 317 |
|
prints("\033[S"); // Scroll up 1 line |
| 318 |
|
break; |
| 319 |
|
case KEY_PGUP: |
| 320 |
|
case Ctrl('B'): |
| 321 |
|
if (c_line_current - line < 0) // Reach top |
| 322 |
|
{ |
| 323 |
|
break; |
| 324 |
|
} |
| 325 |
|
c_line_current -= ((screen_rows - 3) + (line - 1)); |
| 326 |
|
if (c_line_current < 0) |
| 327 |
|
{ |
| 328 |
|
c_line_current = 0; |
| 329 |
|
} |
| 330 |
|
line = begin_line; |
| 331 |
|
max_lines = screen_rows - 1; |
| 332 |
|
clrline(begin_line, screen_rows); |
| 333 |
|
break; |
| 334 |
|
case KEY_RIGHT: |
| 335 |
|
case KEY_PGDN: |
| 336 |
|
case Ctrl('F'): |
| 337 |
|
case KEY_SPACE: |
| 338 |
|
if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom |
| 339 |
|
{ |
| 340 |
|
break; |
| 341 |
|
} |
| 342 |
|
c_line_current += (screen_rows - 3) - (line - 1); |
| 343 |
|
if (c_line_current + screen_rows - 2 > c_line_total) // No enough lines to display |
| 344 |
|
{ |
| 345 |
|
c_line_current = c_line_total - (screen_rows - 2); |
| 346 |
|
} |
| 347 |
|
line = begin_line; |
| 348 |
|
max_lines = screen_rows - 1; |
| 349 |
|
clrline(begin_line, screen_rows); |
| 350 |
|
break; |
| 351 |
|
case KEY_NULL: |
| 352 |
|
case KEY_TIMEOUT: |
| 353 |
|
case KEY_LEFT: |
| 354 |
|
case 'q': |
| 355 |
|
case 'Q': |
| 356 |
|
loop = 0; |
| 357 |
|
break; |
| 358 |
|
case '?': |
| 359 |
|
case 'h': |
| 360 |
|
case 'H': |
| 361 |
|
// Display help information |
| 362 |
|
strcpy(temp, app_home_dir); |
| 363 |
|
strcat(temp, "data/read_help.txt"); |
| 364 |
|
display_file_ex(temp, begin_line, 1); |
| 365 |
|
|
| 366 |
|
// Refresh after display help information |
| 367 |
|
c_line_current -= (line - 1); |
| 368 |
|
line = begin_line; |
| 369 |
|
max_lines = screen_rows - 1; |
| 370 |
|
clrline(begin_line, screen_rows); |
| 371 |
|
break; |
| 372 |
|
default: |
| 373 |
|
input_ok = 0; |
| 374 |
|
break; |
| 375 |
|
} |
| 376 |
|
} |
| 377 |
|
|
| 378 |
i = strlen (buffer); |
continue; |
| 379 |
if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r') |
} |
|
{ |
|
|
buffer[i - 1] = '\r'; |
|
|
buffer[i] = '\n'; |
|
|
buffer[i + 1] = '\0'; |
|
|
} |
|
|
prints (buffer); |
|
|
iflush (); |
|
| 380 |
|
|
| 381 |
line++; |
fseek(fin, p_line_offsets[c_line_current], SEEK_SET); |
| 382 |
|
len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current]; |
| 383 |
|
if (len >= LINE_BUFFER_LEN) |
| 384 |
|
{ |
| 385 |
|
log_error("Error length exceeds buffer size: %d\n", len); |
| 386 |
|
len = LINE_BUFFER_LEN - 1; |
| 387 |
|
} |
| 388 |
|
if (fgets(buffer, len + 1, fin) == NULL) |
| 389 |
|
{ |
| 390 |
|
log_error("Reach EOF\n"); |
| 391 |
|
break; |
| 392 |
|
} |
| 393 |
|
moveto(line, 0); |
| 394 |
|
clrtoeol(); |
| 395 |
|
prints("%s", buffer); |
| 396 |
|
c_line_current++; |
| 397 |
|
line++; |
| 398 |
} |
} |
|
if (wait) |
|
|
ch = press_any_key (); |
|
|
else |
|
|
ch = 0; |
|
| 399 |
|
|
| 400 |
exit: |
free(p_line_offsets); |
| 401 |
fclose (fin); |
fclose(fin); |
| 402 |
|
|
| 403 |
return ch; |
return ch; |
|
} |
|
|
|
|
|
return -1; |
|
| 404 |
} |
} |
| 405 |
|
|
| 406 |
int |
int show_top(char *status) |
|
show_top (char *status) |
|
| 407 |
{ |
{ |
| 408 |
char buffer[256]; |
char buffer[LINE_BUFFER_LEN]; |
| 409 |
|
|
| 410 |
str_space (buffer, 20 - strlen (BBS_current_section_name)); |
str_space(buffer, 20 - strlen(BBS_current_section_name)); |
| 411 |
|
|
| 412 |
moveto (1, 0); |
moveto(1, 0); |
| 413 |
prints ("\033[1;44;33m%-20s \033[37m%20s" |
clrtoeol(); |
| 414 |
" %s\033[33m讨论区 [%s]\033[m", |
prints("\033[1;44;33m%-20s \033[37m%20s" |
| 415 |
status, BBS_name, buffer, BBS_current_section_name); |
" %s\033[33m讨论区 [%s]\033[m", |
| 416 |
iflush (); |
status, BBS_name, buffer, BBS_current_section_name); |
| 417 |
|
iflush(); |
| 418 |
|
|
| 419 |
return 0; |
return 0; |
| 420 |
} |
} |
| 421 |
|
|
| 422 |
int |
int show_bottom(char *msg) |
|
show_bottom (char *msg) |
|
| 423 |
{ |
{ |
| 424 |
char str_time[256], str_time_onine[20], buffer[256]; |
char str_time[LINE_BUFFER_LEN]; |
| 425 |
time_t time_online; |
char str_time_onine[20]; |
| 426 |
struct tm *tm_online; |
char buffer[LINE_BUFFER_LEN]; |
| 427 |
|
time_t time_online; |
| 428 |
|
struct tm *tm_online; |
| 429 |
|
|
| 430 |
get_time_str (str_time, 256); |
get_time_str(str_time, sizeof(str_time)); |
| 431 |
str_space (buffer, 33 - strlen (BBS_username)); |
str_space(buffer, 33 - strlen(BBS_username)); |
| 432 |
|
|
| 433 |
time_online = time (0) - BBS_login_tm; |
time_online = time(0) - BBS_login_tm; |
| 434 |
tm_online = gmtime (&time_online); |
tm_online = gmtime(&time_online); |
| 435 |
|
|
| 436 |
moveto (screen_lines, 0); |
moveto(screen_rows, 0); |
| 437 |
prints ("\033[1;44;33m[\033[36m%s\033[33m]" |
clrtoeol(); |
| 438 |
"%s帐号[\033[36m%s\033[33m]" |
prints("\033[1;44;33m[\033[36m%s\033[33m]" |
| 439 |
"[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m", |
"%s帐号[\033[36m%s\033[33m]" |
| 440 |
str_time, buffer, BBS_username, tm_online->tm_mday - 1, |
"[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m", |
| 441 |
tm_online->tm_hour, tm_online->tm_min); |
str_time, buffer, BBS_username, tm_online->tm_mday - 1, |
| 442 |
iflush (); |
tm_online->tm_hour, tm_online->tm_min); |
| 443 |
|
iflush(); |
| 444 |
|
|
| 445 |
return 0; |
return 0; |
| 446 |
} |
} |
| 447 |
|
|
| 448 |
int |
int show_active_board() |
|
show_active_board () |
|
| 449 |
{ |
{ |
| 450 |
char filename[256], buffer[260]; |
char filename[FILE_PATH_LEN]; |
| 451 |
FILE *fin; |
char buffer[LINE_BUFFER_LEN]; |
| 452 |
int i, j; |
FILE *fin; |
| 453 |
static long int line; |
static int line; |
| 454 |
|
int len; |
| 455 |
sprintf (filename, "%sdata/active_board.txt", app_home_dir); |
int end_of_line; |
| 456 |
|
|
| 457 |
clrline (3, 2 + ACTIVE_BOARD_HEIGHT); |
sprintf(filename, "%sdata/active_board.txt", app_home_dir); |
| 458 |
|
|
| 459 |
moveto (3, 0); |
clrline(3, 2 + ACTIVE_BOARD_HEIGHT); |
| 460 |
|
|
| 461 |
if ((fin = fopen (filename, "r")) != NULL) |
if ((fin = fopen(filename, "r")) == NULL) |
|
{ |
|
|
for (j = 0; j < line; j++) |
|
| 462 |
{ |
{ |
| 463 |
if (fgets (buffer, 255, fin) == NULL) |
log_error("Unable to open file %s\n", filename); |
| 464 |
{ |
return -1; |
|
line = 0; |
|
|
rewind (fin); |
|
|
break; |
|
|
} |
|
| 465 |
} |
} |
| 466 |
|
|
| 467 |
for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++) |
for (int i = 0; i < line; i++) |
| 468 |
{ |
{ |
| 469 |
if (fgets (buffer, 255, fin) == NULL) |
if (fgets(buffer, sizeof(buffer), fin) == NULL) |
|
{ |
|
|
line = 0; |
|
|
if (j == 0) |
|
| 470 |
{ |
{ |
| 471 |
rewind (fin); |
line = 0; |
| 472 |
if (fgets (buffer, 255, fin) == NULL) |
rewind(fin); |
| 473 |
{ |
break; |
|
break; |
|
|
} |
|
| 474 |
} |
} |
| 475 |
else |
} |
| 476 |
|
|
| 477 |
|
for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++) |
| 478 |
|
{ |
| 479 |
|
if (fgets(buffer, sizeof(buffer), fin) == NULL) |
| 480 |
{ |
{ |
| 481 |
break; |
line = 0; |
| 482 |
|
break; |
| 483 |
} |
} |
| 484 |
} |
line++; |
| 485 |
line++; |
len = split_line(buffer, screen_cols, &end_of_line); |
| 486 |
i = strlen (buffer); |
buffer[len] = '\0'; // Truncate over-length line |
| 487 |
if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r') |
moveto(3 + i, 0); |
| 488 |
{ |
prints("%s", buffer); |
|
buffer[i - 1] = '\r'; |
|
|
buffer[i] = '\n'; |
|
|
buffer[i + 1] = '\0'; |
|
|
} |
|
|
prints (buffer); |
|
|
iflush (); |
|
| 489 |
} |
} |
| 490 |
fclose (fin); |
iflush(); |
| 491 |
} |
|
| 492 |
|
fclose(fin); |
| 493 |
|
|
| 494 |
return 0; |
return 0; |
| 495 |
} |
} |