| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
|
#include "screen.h" |
| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
#include "str_process.h" |
#include "str_process.h" |
| 21 |
#include "log.h" |
#include "log.h" |
| 22 |
#include "io.h" |
#include "io.h" |
| 23 |
#include "screen.h" |
#include "file_loader.h" |
| 24 |
#include <fcntl.h> |
#include <fcntl.h> |
| 25 |
#include <string.h> |
#include <string.h> |
| 26 |
#include <ctype.h> |
#include <ctype.h> |
| 30 |
#include <sys/types.h> |
#include <sys/types.h> |
| 31 |
#include <sys/stat.h> |
#include <sys/stat.h> |
| 32 |
#include <sys/param.h> |
#include <sys/param.h> |
| 33 |
#include <sys/mman.h> |
#include <sys/shm.h> |
| 34 |
|
|
| 35 |
#define ACTIVE_BOARD_HEIGHT 8 |
#define ACTIVE_BOARD_HEIGHT 8 |
| 36 |
|
|
| 229 |
{ |
{ |
| 230 |
static int show_help = 1; |
static int show_help = 1; |
| 231 |
char buffer[LINE_BUFFER_LEN]; |
char buffer[LINE_BUFFER_LEN]; |
| 232 |
void *p_data; |
int ch = KEY_NULL; |
|
int ch = 0; |
|
| 233 |
int input_ok, line, max_lines; |
int input_ok, line, max_lines; |
| 234 |
long int c_line_current = 0; |
long int line_current = 0; |
| 235 |
long int c_line_total = 0; |
const void *p_file_shm; |
| 236 |
int fd; |
const void *p_data; |
| 237 |
struct stat sb; |
size_t data_len; |
| 238 |
long *p_line_offsets; |
long line_total; |
| 239 |
|
const long *p_line_offsets; |
| 240 |
long int len; |
long int len; |
| 241 |
long int percentile; |
long int percentile; |
| 242 |
int loop = 1; |
int loop; |
|
|
|
|
if ((fd = open(filename, O_RDONLY)) < 0) |
|
|
{ |
|
|
log_error("open(%s) error (%d)\n", filename, errno); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
if (fstat(fd, &sb) < 0) |
|
|
{ |
|
|
log_error("fstat(fd) error (%d)\n", errno); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
p_data = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0L); |
|
|
if (p_data == MAP_FAILED) |
|
|
{ |
|
|
log_error("mmap() error (%d)\n", errno); |
|
|
return -2; |
|
|
} |
|
| 243 |
|
|
| 244 |
if (close(fd) < 0) |
if ((p_file_shm = get_file_shm(filename)) == NULL) |
| 245 |
{ |
{ |
| 246 |
log_error("close(fd) error (%d)\n", errno); |
log_error("get_file_shm(%s) error\n", filename); |
| 247 |
return -1; |
return KEY_NULL; |
| 248 |
} |
} |
| 249 |
|
|
| 250 |
p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES); |
data_len = *((size_t *)p_file_shm); |
| 251 |
|
line_total = *((long *)(p_file_shm + sizeof(size_t))); |
| 252 |
c_line_total = split_data_lines(p_data, SCREEN_COLS, p_line_offsets, MAX_FILE_LINES); |
p_data = p_file_shm + sizeof(data_len) + sizeof(line_total); |
| 253 |
|
p_line_offsets = p_data + data_len + 1; |
| 254 |
|
|
| 255 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 256 |
line = begin_line; |
line = begin_line; |
| 257 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 258 |
|
|
| 259 |
|
loop = 1; |
| 260 |
while (!SYS_server_exit && loop) |
while (!SYS_server_exit && loop) |
| 261 |
{ |
{ |
| 262 |
if (c_line_current >= c_line_total && c_line_total <= SCREEN_ROWS - 2) |
if (line_current >= line_total && line_total <= SCREEN_ROWS - 2) |
| 263 |
{ |
{ |
| 264 |
if (wait) |
if (wait) |
| 265 |
{ |
{ |
| 274 |
break; |
break; |
| 275 |
} |
} |
| 276 |
|
|
| 277 |
if (c_line_current >= c_line_total || line >= max_lines) |
if (line_current >= line_total || line >= max_lines) |
| 278 |
{ |
{ |
| 279 |
if (c_line_current - (line - 1) + (SCREEN_ROWS - 2) < c_line_total) |
if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total) |
| 280 |
{ |
{ |
| 281 |
percentile = (c_line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / c_line_total; |
percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total; |
| 282 |
} |
} |
| 283 |
else |
else |
| 284 |
{ |
{ |
| 302 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 303 |
goto cleanup; |
goto cleanup; |
| 304 |
case KEY_HOME: |
case KEY_HOME: |
| 305 |
c_line_current = 0; |
line_current = 0; |
| 306 |
line = begin_line; |
line = begin_line; |
| 307 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 308 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 309 |
break; |
break; |
| 310 |
case KEY_END: |
case KEY_END: |
| 311 |
c_line_current = c_line_total - (SCREEN_ROWS - 2); |
line_current = line_total - (SCREEN_ROWS - 2); |
| 312 |
line = begin_line; |
line = begin_line; |
| 313 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 314 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 315 |
break; |
break; |
| 316 |
case KEY_UP: |
case KEY_UP: |
| 317 |
if (c_line_current - line < 0) // Reach top |
if (line_current - line < 0) // Reach top |
| 318 |
{ |
{ |
| 319 |
break; |
break; |
| 320 |
} |
} |
| 321 |
c_line_current -= line; |
line_current -= line; |
| 322 |
line = begin_line; |
line = begin_line; |
| 323 |
// max_lines = begin_line + 1; |
// max_lines = begin_line + 1; |
| 324 |
// prints("\033[T"); // Scroll down 1 line |
// prints("\033[T"); // Scroll down 1 line |
| 327 |
case CR: |
case CR: |
| 328 |
igetch_reset(); |
igetch_reset(); |
| 329 |
case KEY_DOWN: |
case KEY_DOWN: |
| 330 |
if (c_line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= c_line_total) // Reach bottom |
if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach bottom |
| 331 |
{ |
{ |
| 332 |
break; |
break; |
| 333 |
} |
} |
| 334 |
c_line_current += ((SCREEN_ROWS - 2) - (line - 1)); |
line_current += ((SCREEN_ROWS - 2) - (line - 1)); |
| 335 |
line = SCREEN_ROWS - 2; |
line = SCREEN_ROWS - 2; |
| 336 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 337 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 340 |
break; |
break; |
| 341 |
case KEY_PGUP: |
case KEY_PGUP: |
| 342 |
case Ctrl('B'): |
case Ctrl('B'): |
| 343 |
if (c_line_current - line < 0) // Reach top |
if (line_current - line < 0) // Reach top |
| 344 |
{ |
{ |
| 345 |
break; |
break; |
| 346 |
} |
} |
| 347 |
c_line_current -= ((SCREEN_ROWS - 3) + (line - 1)); |
line_current -= ((SCREEN_ROWS - 3) + (line - 1)); |
| 348 |
if (c_line_current < 0) |
if (line_current < 0) |
| 349 |
{ |
{ |
| 350 |
c_line_current = 0; |
line_current = 0; |
| 351 |
} |
} |
| 352 |
line = begin_line; |
line = begin_line; |
| 353 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 357 |
case KEY_PGDN: |
case KEY_PGDN: |
| 358 |
case Ctrl('F'): |
case Ctrl('F'): |
| 359 |
case KEY_SPACE: |
case KEY_SPACE: |
| 360 |
if (c_line_current + (SCREEN_ROWS - 2) - (line - 1) >= c_line_total) // Reach bottom |
if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach bottom |
| 361 |
{ |
{ |
| 362 |
break; |
break; |
| 363 |
} |
} |
| 364 |
c_line_current += (SCREEN_ROWS - 3) - (line - 1); |
line_current += (SCREEN_ROWS - 3) - (line - 1); |
| 365 |
if (c_line_current + SCREEN_ROWS - 2 > c_line_total) // No enough lines to display |
if (line_current + SCREEN_ROWS - 2 > line_total) // No enough lines to display |
| 366 |
{ |
{ |
| 367 |
c_line_current = c_line_total - (SCREEN_ROWS - 2); |
line_current = line_total - (SCREEN_ROWS - 2); |
| 368 |
} |
} |
| 369 |
line = begin_line; |
line = begin_line; |
| 370 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 389 |
show_help = 1; |
show_help = 1; |
| 390 |
|
|
| 391 |
// Refresh after display help information |
// Refresh after display help information |
| 392 |
c_line_current -= (line - 1); |
line_current -= (line - 1); |
| 393 |
line = begin_line; |
line = begin_line; |
| 394 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 395 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 406 |
continue; |
continue; |
| 407 |
} |
} |
| 408 |
|
|
| 409 |
len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current]; |
len = p_line_offsets[line_current + 1] - p_line_offsets[line_current]; |
| 410 |
if (len >= LINE_BUFFER_LEN) |
if (len >= LINE_BUFFER_LEN) |
| 411 |
{ |
{ |
| 412 |
log_error("Error length exceeds buffer size: %d\n", len); |
log_error("Error length exceeds buffer size: %d\n", len); |
| 413 |
len = LINE_BUFFER_LEN - 1; |
len = LINE_BUFFER_LEN - 1; |
| 414 |
} |
} |
| 415 |
|
|
| 416 |
memcpy(buffer, (const char *)p_data + p_line_offsets[c_line_current], (size_t)len); |
memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len); |
| 417 |
buffer[len] = '\0'; |
buffer[len] = '\0'; |
| 418 |
|
|
| 419 |
moveto(line, 0); |
moveto(line, 0); |
| 420 |
clrtoeol(); |
clrtoeol(); |
| 421 |
prints("%s", buffer); |
prints("%s", buffer); |
| 422 |
c_line_current++; |
line_current++; |
| 423 |
line++; |
line++; |
| 424 |
} |
} |
| 425 |
|
|
| 426 |
cleanup: |
cleanup: |
| 427 |
if (munmap(p_data, (size_t)sb.st_size) < 0) |
if (shmdt(p_file_shm) == -1) |
| 428 |
{ |
{ |
| 429 |
log_error("munmap() error (%d)\n", errno); |
log_error("shmdt() error (%d)\n", errno); |
| 430 |
} |
} |
| 431 |
|
|
|
free(p_line_offsets); |
|
|
|
|
| 432 |
return ch; |
return ch; |
| 433 |
} |
} |
| 434 |
|
|
| 435 |
int show_top(char *status) |
int show_top(char *status) |
| 436 |
{ |
{ |
| 437 |
int end_of_line; |
int truncate; |
| 438 |
int display_len; |
int status_len; |
| 439 |
|
int section_name_len; |
| 440 |
int len; |
int len; |
| 441 |
|
|
| 442 |
char space1[LINE_BUFFER_LEN]; |
len = split_line(status, 20, &truncate, &status_len); |
| 443 |
char space2[LINE_BUFFER_LEN]; |
if (truncate) |
|
|
|
|
len = split_line(status, 20, &end_of_line, &display_len); |
|
|
if (end_of_line) |
|
| 444 |
{ |
{ |
| 445 |
status[len] = '\0'; |
status[len] = '\0'; |
| 446 |
} |
} |
|
str_space(space1, 31 - display_len); |
|
| 447 |
|
|
| 448 |
len = split_line(BBS_current_section_name, 20, &end_of_line, &display_len); |
len = split_line(BBS_current_section_name, 20, &truncate, §ion_name_len); |
| 449 |
if (end_of_line) |
if (truncate) |
| 450 |
{ |
{ |
| 451 |
status[len] = '\0'; |
status[len] = '\0'; |
| 452 |
} |
} |
|
str_space(space2, 30 - display_len); |
|
| 453 |
|
|
| 454 |
moveto(1, 0); |
moveto(1, 0); |
| 455 |
clrtoeol(); |
clrtoeol(); |
| 456 |
prints("\033[1;44;33m%s \033[37m%s%s%s\033[33m ÌÖÂÛÇø [%s]\033[m", |
prints("\033[1;44;33m%s \033[37m%*s%*s\033[33m ÌÖÂÛÇø [%s]\033[m", |
| 457 |
status, space1, BBS_name, space2, BBS_current_section_name); |
status, (39 - status_len), BBS_name, (30 - section_name_len), "", BBS_current_section_name); |
| 458 |
iflush(); |
iflush(); |
| 459 |
|
|
| 460 |
return 0; |
return 0; |