| 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> |
| 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 FILE_MMAP *p_file_mmap; |
|
int fd; |
|
|
struct stat sb; |
|
|
long *p_line_offsets; |
|
| 236 |
long int len; |
long int len; |
| 237 |
long int percentile; |
long int percentile; |
| 238 |
int loop = 1; |
int loop; |
|
|
|
|
if ((fd = open(filename, O_RDONLY)) < 0) |
|
|
{ |
|
|
log_error("open(%s) error (%d)\n", filename, errno); |
|
|
return -1; |
|
|
} |
|
| 239 |
|
|
| 240 |
if (fstat(fd, &sb) < 0) |
if ((p_file_mmap = get_file_mmap(filename)) == NULL) |
| 241 |
{ |
{ |
| 242 |
log_error("fstat(fd) error (%d)\n", errno); |
if (load_file_mmap(filename) < 0) |
| 243 |
return -1; |
{ |
| 244 |
} |
log_error("load_file_mmap(%s) error\n", filename); |
| 245 |
|
return KEY_NULL; |
| 246 |
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; |
|
|
} |
|
| 247 |
|
|
| 248 |
if (close(fd) < 0) |
if ((p_file_mmap = get_file_mmap(filename)) == NULL) |
| 249 |
{ |
{ |
| 250 |
log_error("close(fd) error (%d)\n", errno); |
log_error("get_file_mmap(%s) error\n", filename); |
| 251 |
return -1; |
return KEY_NULL; |
| 252 |
|
} |
| 253 |
} |
} |
| 254 |
|
|
|
p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES); |
|
|
|
|
|
c_line_total = split_data_lines(p_data, SCREEN_COLS, p_line_offsets, MAX_FILE_LINES); |
|
|
|
|
| 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 >= p_file_mmap->line_total && p_file_mmap->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 >= p_file_mmap->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) < p_file_mmap->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 / p_file_mmap->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 = p_file_mmap->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)) >= p_file_mmap->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) >= p_file_mmap->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 > p_file_mmap->line_total) // No enough lines to display |
| 366 |
{ |
{ |
| 367 |
c_line_current = c_line_total - (SCREEN_ROWS - 2); |
line_current = p_file_mmap->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_file_mmap->line_offsets[line_current + 1] - p_file_mmap->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_file_mmap->p_data + p_file_mmap->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: |
|
if (munmap(p_data, (size_t)sb.st_size) < 0) |
|
|
{ |
|
|
log_error("munmap() error (%d)\n", errno); |
|
|
} |
|
|
|
|
|
free(p_line_offsets); |
|
|
|
|
| 427 |
return ch; |
return ch; |
| 428 |
} |
} |
| 429 |
|
|