| 222 |
int len; |
int len; |
| 223 |
|
|
| 224 |
moveto(row, col); |
moveto(row, col); |
| 225 |
prints(prompt); |
prints("%s", prompt); |
| 226 |
prints(buffer); |
prints("%s", buffer); |
| 227 |
iflush(); |
iflush(); |
| 228 |
|
|
| 229 |
len = _str_input(buffer, buf_size, echo_mode); |
len = _str_input(buffer, buf_size, echo_mode); |
| 231 |
return len; |
return len; |
| 232 |
} |
} |
| 233 |
|
|
| 234 |
int display_data(const void *p_data, long line_total, const long *p_line_offsets, int begin_line, int wait) |
int display_data(const void *p_data, long line_total, const long *p_line_offsets, int begin_line, int eof_exit, |
| 235 |
|
display_data_key_handler key_handler, const char *help_filename) |
| 236 |
{ |
{ |
| 237 |
static int show_help = 1; |
static int show_help = 1; |
| 238 |
char buffer[LINE_BUFFER_LEN]; |
char buffer[LINE_BUFFER_LEN]; |
| 239 |
int ch = KEY_NULL; |
DISPLAY_CTX ctx; |
| 240 |
|
int ch = 0; |
| 241 |
int input_ok, line, max_lines; |
int input_ok, line, max_lines; |
| 242 |
long int line_current = 0; |
long int line_current = 0; |
| 243 |
long int len; |
long int len; |
| 249 |
line = begin_line; |
line = begin_line; |
| 250 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 251 |
|
|
| 252 |
|
// update msg_ext with extended key handler |
| 253 |
|
if (key_handler(&ch, &ctx) != 0) |
| 254 |
|
{ |
| 255 |
|
return ch; |
| 256 |
|
} |
| 257 |
|
|
| 258 |
loop = 1; |
loop = 1; |
| 259 |
while (!SYS_server_exit && loop) |
while (!SYS_server_exit && loop) |
| 260 |
{ |
{ |
| 261 |
if (line_current >= line_total && line_total <= SCREEN_ROWS - 2) |
if (eof_exit > 0 && line_current >= line_total && line_total <= SCREEN_ROWS - 2) |
| 262 |
{ |
{ |
| 263 |
if (wait) |
if (eof_exit == 1) |
| 264 |
{ |
{ |
| 265 |
ch = press_any_key(); |
ch = press_any_key(); |
| 266 |
} |
} |
| 267 |
else |
else // if (eof_exit == 2) |
| 268 |
{ |
{ |
| 269 |
iflush(); |
iflush(); |
| 270 |
} |
} |
| 275 |
|
|
| 276 |
if (line_current >= line_total || line >= max_lines) |
if (line_current >= line_total || line >= max_lines) |
| 277 |
{ |
{ |
| 278 |
|
ctx.reach_begin = (line_current < line ? 1 : 0); |
| 279 |
|
|
| 280 |
if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total) |
if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total) |
| 281 |
{ |
{ |
| 282 |
percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total; |
percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total; |
| 283 |
|
ctx.reach_end = 0; |
| 284 |
} |
} |
| 285 |
else |
else |
| 286 |
{ |
{ |
| 287 |
percentile = 100; |
percentile = 100; |
| 288 |
|
ctx.reach_end = 1; |
| 289 |
} |
} |
| 290 |
|
|
| 291 |
|
ctx.line_top = line_current - (line - 1) + 1; |
| 292 |
|
ctx.line_bottom = MIN(line_current - (line - 1) + (SCREEN_ROWS - 2), line_total); |
| 293 |
|
|
| 294 |
snprintf(buffer, sizeof(buffer), |
snprintf(buffer, sizeof(buffer), |
| 295 |
"\033[1;44;33m第\033[36m%ld\033[33m-\033[36m%ld\033[33m行 (\033[36m%ld%%\033[33m) │ " |
"\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s", |
| 296 |
"返回[\033[36m←\033[33m,\033[36mESC\033[33m] │ 移动[\033[36m↑\033[33m/\033[36m↓\033[33m/\033[36mPgUp\033[33m/\033[36mPgDn\033[33m] │ " |
ctx.line_top, |
| 297 |
"帮助[\033[36mh\033[33m] │", |
ctx.line_bottom, |
| 298 |
line_current - (line - 1) + 1, |
percentile, |
| 299 |
MIN(line_current - (line - 1) + (SCREEN_ROWS - 2), line_total), |
ctx.msg); |
|
percentile); |
|
| 300 |
|
|
| 301 |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
len = split_line(buffer, SCREEN_COLS, &eol, &display_len); |
| 302 |
for (;display_len < SCREEN_COLS; display_len++) |
for (; display_len < SCREEN_COLS; display_len++) |
| 303 |
{ |
{ |
| 304 |
buffer[len++] = ' '; |
buffer[len++] = ' '; |
| 305 |
} |
} |
| 307 |
strncat(buffer, "\033[m", sizeof(buffer) - 1 - strnlen(buffer, sizeof(buffer))); |
strncat(buffer, "\033[m", sizeof(buffer) - 1 - strnlen(buffer, sizeof(buffer))); |
| 308 |
|
|
| 309 |
moveto(SCREEN_ROWS, 0); |
moveto(SCREEN_ROWS, 0); |
| 310 |
prints(buffer); |
prints("%s", buffer); |
| 311 |
iflush(); |
iflush(); |
| 312 |
|
|
| 313 |
input_ok = 0; |
input_ok = 0; |
| 315 |
{ |
{ |
| 316 |
ch = igetch_t(MAX_DELAY_TIME); |
ch = igetch_t(MAX_DELAY_TIME); |
| 317 |
input_ok = 1; |
input_ok = 1; |
| 318 |
|
|
| 319 |
|
// extended key handler |
| 320 |
|
if (key_handler(&ch, &ctx) != 0) |
| 321 |
|
{ |
| 322 |
|
goto cleanup; |
| 323 |
|
} |
| 324 |
|
|
| 325 |
switch (ch) |
switch (ch) |
| 326 |
{ |
{ |
| 327 |
case KEY_NULL: |
case KEY_NULL: |
| 328 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 329 |
goto cleanup; |
goto cleanup; |
| 330 |
case KEY_HOME: |
case KEY_HOME: |
| 331 |
|
if (line_current - line < 0) // Reach begin |
| 332 |
|
{ |
| 333 |
|
break; |
| 334 |
|
} |
| 335 |
line_current = 0; |
line_current = 0; |
| 336 |
line = begin_line; |
line = begin_line; |
| 337 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 338 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 339 |
break; |
break; |
| 340 |
case KEY_END: |
case KEY_END: |
| 341 |
|
if (line_total < SCREEN_ROWS - 2) |
| 342 |
|
{ |
| 343 |
|
break; |
| 344 |
|
} |
| 345 |
line_current = line_total - (SCREEN_ROWS - 2); |
line_current = line_total - (SCREEN_ROWS - 2); |
| 346 |
line = begin_line; |
line = begin_line; |
| 347 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 348 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 349 |
break; |
break; |
| 350 |
case KEY_UP: |
case KEY_UP: |
| 351 |
if (line_current - line < 0) // Reach top |
if (line_current - line < 0) // Reach begin |
| 352 |
{ |
{ |
| 353 |
break; |
break; |
| 354 |
} |
} |
| 362 |
igetch_reset(); |
igetch_reset(); |
| 363 |
case KEY_SPACE: |
case KEY_SPACE: |
| 364 |
case KEY_DOWN: |
case KEY_DOWN: |
| 365 |
if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach bottom |
if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach end |
| 366 |
{ |
{ |
| 367 |
break; |
break; |
| 368 |
} |
} |
| 374 |
prints("\033[S"); // Scroll up 1 line |
prints("\033[S"); // Scroll up 1 line |
| 375 |
break; |
break; |
| 376 |
case KEY_PGUP: |
case KEY_PGUP: |
| 377 |
if (line_current - line < 0) // Reach top |
if (line_current - line < 0) // Reach begin |
| 378 |
{ |
{ |
| 379 |
break; |
break; |
| 380 |
} |
} |
| 388 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 389 |
break; |
break; |
| 390 |
case KEY_PGDN: |
case KEY_PGDN: |
| 391 |
if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach bottom |
if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach end |
| 392 |
{ |
{ |
| 393 |
break; |
break; |
| 394 |
} |
} |
| 406 |
loop = 0; |
loop = 0; |
| 407 |
break; |
break; |
| 408 |
case 'h': |
case 'h': |
| 409 |
if (!show_help) |
if (!show_help) // Not reentrant |
| 410 |
{ |
{ |
| 411 |
break; |
break; |
| 412 |
} |
} |
| 413 |
|
|
| 414 |
// Display help information |
// Display help information |
| 415 |
show_help = 0; |
show_help = 0; |
| 416 |
display_file(DATA_READ_HELP, begin_line, 1); |
display_file(help_filename, begin_line, 1); |
| 417 |
show_help = 1; |
show_help = 1; |
| 418 |
|
case KEY_F5: |
| 419 |
// Refresh after display help information |
// Refresh after display help information |
| 420 |
line_current -= (line - 1); |
line_current -= (line - 1); |
| 421 |
line = begin_line; |
line = begin_line; |
| 422 |
max_lines = SCREEN_ROWS - 1; |
max_lines = SCREEN_ROWS - 1; |
| 423 |
clrline(begin_line, SCREEN_ROWS); |
clrline(begin_line, SCREEN_ROWS); |
| 424 |
break; |
break; |
| 425 |
|
case 0: // Refresh bottom line |
| 426 |
|
break; |
| 427 |
default: |
default: |
| 428 |
input_ok = 0; |
input_ok = 0; |
| 429 |
break; |
break; |
| 438 |
len = p_line_offsets[line_current + 1] - p_line_offsets[line_current]; |
len = p_line_offsets[line_current + 1] - p_line_offsets[line_current]; |
| 439 |
if (len >= LINE_BUFFER_LEN) |
if (len >= LINE_BUFFER_LEN) |
| 440 |
{ |
{ |
| 441 |
log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n", |
log_error("Buffer overflow: len=%ld(%ld - %ld) line=%ld \n", |
| 442 |
len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); |
len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); |
| 443 |
len = LINE_BUFFER_LEN - 1; |
len = LINE_BUFFER_LEN - 1; |
| 444 |
} |
} |
| 445 |
|
else if (len < 0) |
| 446 |
|
{ |
| 447 |
|
log_error("Incorrect line offsets: len=%ld(%ld - %ld) line=%ld \n", |
| 448 |
|
len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current); |
| 449 |
|
len = 0; |
| 450 |
|
} |
| 451 |
|
|
| 452 |
memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len); |
memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len); |
| 453 |
buffer[len] = '\0'; |
buffer[len] = '\0'; |
| 463 |
return ch; |
return ch; |
| 464 |
} |
} |
| 465 |
|
|
| 466 |
int display_file(const char *filename, int begin_line, int wait) |
static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx) |
| 467 |
{ |
{ |
| 468 |
int ch = KEY_NULL; |
switch (*p_key) |
| 469 |
|
{ |
| 470 |
|
case 0: // Set msg |
| 471 |
|
snprintf(p_ctx->msg, sizeof(p_ctx->msg), |
| 472 |
|
"| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | " |
| 473 |
|
"移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | " |
| 474 |
|
"帮助[\033[32mh\033[33m] |"); |
| 475 |
|
break; |
| 476 |
|
} |
| 477 |
|
|
| 478 |
|
return 0; |
| 479 |
|
} |
| 480 |
|
|
| 481 |
|
int display_file(const char *filename, int begin_line, int eof_exit) |
| 482 |
|
{ |
| 483 |
|
int ret; |
| 484 |
const void *p_shm; |
const void *p_shm; |
| 485 |
size_t data_len; |
size_t data_len; |
| 486 |
long line_total; |
long line_total; |
| 493 |
return KEY_NULL; |
return KEY_NULL; |
| 494 |
} |
} |
| 495 |
|
|
| 496 |
ch = display_data(p_data, line_total, p_line_offsets, begin_line, wait); |
ret = display_data(p_data, line_total, p_line_offsets, begin_line, eof_exit, display_file_key_handler, DATA_READ_HELP); |
| 497 |
|
|
| 498 |
if (detach_file_shm(p_shm) < 0) |
if (detach_file_shm(p_shm) < 0) |
| 499 |
{ |
{ |
| 500 |
log_error("detach_file_shm(%s) error\n", filename); |
log_error("detach_file_shm(%s) error\n", filename); |
| 501 |
} |
} |
| 502 |
|
|
| 503 |
return ch; |
return ret; |
| 504 |
} |
} |
| 505 |
|
|
| 506 |
int show_top(const char *str_left, const char *str_middle, const char *str_right) |
int show_top(const char *str_left, const char *str_middle, const char *str_right) |