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