1 /* 2 * Top users/processes display for Unix 3 * Version 3 4 * 5 * This program may be freely redistributed, 6 * but this entire comment MUST remain intact. 7 * 8 * Copyright (c) 1984, 1989, William LeFebvre, Rice University 9 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University 10 * 11 * $FreeBSD$ 12 */ 13 14 /* 15 * This file contains the routines that display information on the screen. 16 * Each section of the screen has two routines: one for initially writing 17 * all constant and dynamic text, and one for only updating the text that 18 * changes. The prefix "i_" is used on all the "initial" routines and the 19 * prefix "u_" is used for all the "updating" routines. 20 * 21 * ASSUMPTIONS: 22 * None of the "i_" routines use any of the termcap capabilities. 23 * In this way, those routines can be safely used on terminals that 24 * have minimal (or nonexistant) terminal capabilities. 25 * 26 * The routines are called in this order: *_loadave, i_timeofday, 27 * *_procstates, *_cpustates, *_memory, *_message, *_header, 28 * *_process, u_endscreen. 29 */ 30 31 #include <sys/cdefs.h> 32 #include <sys/resource.h> 33 #include <sys/time.h> 34 35 #include <assert.h> 36 #include <ctype.h> 37 #include <stdarg.h> 38 #include <stdbool.h> 39 #include <stdlib.h> 40 #include <stdio.h> 41 #include <string.h> 42 #include <termcap.h> 43 #include <time.h> 44 #include <unistd.h> 45 46 #include "screen.h" /* interface to screen package */ 47 #include "layout.h" /* defines for screen position layout */ 48 #include "display.h" 49 #include "top.h" 50 #include "machine.h" /* we should eliminate this!!! */ 51 #include "utils.h" 52 53 #ifdef DEBUG 54 FILE *debug; 55 #endif 56 57 static int lmpid = 0; 58 static int last_hi = 0; /* used in u_process and u_endscreen */ 59 static int lastline = 0; 60 static int display_width = MAX_COLS; 61 62 #define lineindex(l) ((l)*display_width) 63 64 65 /* things initialized by display_init and used thruout */ 66 67 /* buffer of proc information lines for display updating */ 68 static char *screenbuf = NULL; 69 70 static const char * const *procstate_names; 71 static const char * const *cpustate_names; 72 static const char * const *memory_names; 73 static const char * const *arc_names; 74 static const char * const *carc_names; 75 static const char * const *swap_names; 76 77 static int num_procstates; 78 static int num_cpustates; 79 static int num_memory; 80 static int num_swap; 81 82 static int *lprocstates; 83 static int *lcpustates; 84 static int *lmemory; 85 static int *lswap; 86 87 static int num_cpus; 88 static int *cpustate_columns; 89 static int cpustate_total_length; 90 static int cpustates_column; 91 92 static enum { OFF, ON, ERASE } header_status = ON; 93 94 static void summary_format(char *, int *, const char * const *); 95 static void line_update(char *, char *, int, int); 96 97 int x_lastpid = 10; 98 int y_lastpid = 0; 99 int x_loadave = 33; 100 int x_loadave_nompid = 15; 101 int y_loadave = 0; 102 int x_procstate = 0; 103 int y_procstate = 1; 104 int x_brkdn = 15; 105 int y_brkdn = 1; 106 int x_mem = 5; 107 int y_mem = 3; 108 int x_arc = 5; 109 int y_arc = 4; 110 int x_carc = 5; 111 int y_carc = 5; 112 int x_swap = 6; 113 int y_swap = 4; 114 int y_message = 5; 115 int x_header = 0; 116 int y_header = 6; 117 int x_idlecursor = 0; 118 int y_idlecursor = 5; 119 int y_procs = 7; 120 121 int y_cpustates = 2; 122 int Header_lines = 7; 123 124 int 125 display_resize(void) 126 { 127 int lines; 128 129 /* first, deallocate any previous buffer that may have been there */ 130 if (screenbuf != NULL) 131 { 132 free(screenbuf); 133 } 134 135 /* calculate the current dimensions */ 136 /* if operating in "dumb" mode, we only need one line */ 137 lines = smart_terminal ? screen_length - Header_lines : 1; 138 139 if (lines < 0) 140 lines = 0; 141 /* we don't want more than MAX_COLS columns, since the machine-dependent 142 modules make static allocations based on MAX_COLS and we don't want 143 to run off the end of their buffers */ 144 display_width = screen_width; 145 if (display_width >= MAX_COLS) 146 { 147 display_width = MAX_COLS - 1; 148 } 149 150 /* now, allocate space for the screen buffer */ 151 screenbuf = calloc(lines, display_width); 152 if (screenbuf == NULL) 153 { 154 /* oops! */ 155 return(-1); 156 } 157 158 /* return number of lines available */ 159 /* for dumb terminals, pretend like we can show any amount */ 160 return(smart_terminal ? lines : Largest); 161 } 162 163 int 164 display_updatecpus(struct statics *statics) 165 { 166 int lines; 167 int i; 168 169 /* call resize to do the dirty work */ 170 lines = display_resize(); 171 if (pcpu_stats) 172 num_cpus = statics->ncpus; 173 else 174 num_cpus = 1; 175 cpustates_column = 5; /* CPU: */ 176 if (num_cpus > 1) { 177 cpustates_column += 1 + digits(num_cpus); /* CPU #: */ 178 } 179 180 /* fill the "last" array with all -1s, to insure correct updating */ 181 for (i = 0; i < num_cpustates * num_cpus; ++i) { 182 lcpustates[i] = -1; 183 } 184 185 return(lines); 186 } 187 188 int 189 display_init(struct statics * statics) 190 { 191 int lines; 192 char **pp; 193 int *ip; 194 int i; 195 196 lines = display_updatecpus(statics); 197 198 /* only do the rest if we need to */ 199 if (lines > -1) 200 { 201 /* save pointers and allocate space for names */ 202 procstate_names = statics->procstate_names; 203 num_procstates = 8; 204 assert(num_procstates > 0); 205 lprocstates = calloc(num_procstates, sizeof(int)); 206 207 cpustate_names = statics->cpustate_names; 208 209 swap_names = statics->swap_names; 210 num_swap = 7; 211 assert(num_swap > 0); 212 lswap = calloc(num_swap, sizeof(int)); 213 num_cpustates = CPUSTATES; 214 assert(num_cpustates > 0); 215 lcpustates = calloc(num_cpustates * sizeof(int), statics->ncpus); 216 cpustate_columns = calloc(num_cpustates, sizeof(int)); 217 218 memory_names = statics->memory_names; 219 num_memory = 7; 220 assert(num_memory > 0); 221 lmemory = calloc(num_memory, sizeof(int)); 222 223 arc_names = statics->arc_names; 224 carc_names = statics->carc_names; 225 226 /* calculate starting columns where needed */ 227 cpustate_total_length = 0; 228 pp = cpustate_names; 229 ip = cpustate_columns; 230 while (*pp != NULL) 231 { 232 *ip++ = cpustate_total_length; 233 if ((i = strlen(*pp++)) > 0) 234 { 235 cpustate_total_length += i + 8; 236 } 237 } 238 } 239 240 /* return number of lines available */ 241 return(lines); 242 } 243 244 void 245 i_loadave(int mpid, double avenrun[]) 246 { 247 int i; 248 249 /* i_loadave also clears the screen, since it is first */ 250 top_clear(); 251 252 /* mpid == -1 implies this system doesn't have an _mpid */ 253 if (mpid != -1) 254 { 255 printf("last pid: %5d; ", mpid); 256 } 257 258 printf("load averages"); 259 260 for (i = 0; i < 3; i++) 261 { 262 printf("%c %5.2f", 263 i == 0 ? ':' : ',', 264 avenrun[i]); 265 } 266 lmpid = mpid; 267 } 268 269 void 270 u_loadave(int mpid, double *avenrun) 271 { 272 int i; 273 274 if (mpid != -1) 275 { 276 /* change screen only when value has really changed */ 277 if (mpid != lmpid) 278 { 279 Move_to(x_lastpid, y_lastpid); 280 printf("%5d", mpid); 281 lmpid = mpid; 282 } 283 284 /* i remembers x coordinate to move to */ 285 i = x_loadave; 286 } 287 else 288 { 289 i = x_loadave_nompid; 290 } 291 292 /* move into position for load averages */ 293 Move_to(i, y_loadave); 294 295 /* display new load averages */ 296 /* we should optimize this and only display changes */ 297 for (i = 0; i < 3; i++) 298 { 299 printf("%s%5.2f", 300 i == 0 ? "" : ", ", 301 avenrun[i]); 302 } 303 } 304 305 void 306 i_timeofday(time_t *tod) 307 { 308 /* 309 * Display the current time. 310 * "ctime" always returns a string that looks like this: 311 * 312 * Sun Sep 16 01:03:52 1973 313 * 012345678901234567890123 314 * 1 2 315 * 316 * We want indices 11 thru 18 (length 8). 317 */ 318 319 if (smart_terminal) 320 { 321 Move_to(screen_width - 8, 0); 322 } 323 else 324 { 325 fputs(" ", stdout); 326 } 327 #ifdef DEBUG 328 { 329 char *foo; 330 foo = ctime(tod); 331 fputs(foo, stdout); 332 } 333 #endif 334 printf("%-8.8s\n", &(ctime(tod)[11])); 335 lastline = 1; 336 } 337 338 static int ltotal = 0; 339 static char procstates_buffer[MAX_COLS]; 340 341 /* 342 * *_procstates(total, brkdn, names) - print the process summary line 343 * 344 * Assumptions: cursor is at the beginning of the line on entry 345 * lastline is valid 346 */ 347 348 void 349 i_procstates(int total, int *brkdn) 350 { 351 int i; 352 353 /* write current number of processes and remember the value */ 354 printf("%d %s:", total, (ps.thread) ? "threads" :"processes"); 355 ltotal = total; 356 357 /* put out enough spaces to get to column 15 */ 358 i = digits(total); 359 while (i++ < 4) 360 { 361 putchar(' '); 362 } 363 364 /* format and print the process state summary */ 365 summary_format(procstates_buffer, brkdn, procstate_names); 366 fputs(procstates_buffer, stdout); 367 368 /* save the numbers for next time */ 369 memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); 370 } 371 372 void 373 u_procstates(int total, int *brkdn) 374 { 375 static char new[MAX_COLS]; 376 int i; 377 378 /* update number of processes only if it has changed */ 379 if (ltotal != total) 380 { 381 /* move and overwrite */ 382 if (x_procstate == 0) { 383 Move_to(x_procstate, y_procstate); 384 } 385 else { 386 /* cursor is already there...no motion needed */ 387 assert(lastline == 1); 388 } 389 printf("%d", total); 390 391 /* if number of digits differs, rewrite the label */ 392 if (digits(total) != digits(ltotal)) 393 { 394 fputs(" processes:", stdout); 395 /* put out enough spaces to get to column 15 */ 396 i = digits(total); 397 while (i++ < 4) 398 { 399 putchar(' '); 400 } 401 /* cursor may end up right where we want it!!! */ 402 } 403 404 /* save new total */ 405 ltotal = total; 406 } 407 408 /* see if any of the state numbers has changed */ 409 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0) 410 { 411 /* format and update the line */ 412 summary_format(new, brkdn, procstate_names); 413 line_update(procstates_buffer, new, x_brkdn, y_brkdn); 414 memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); 415 } 416 } 417 418 void 419 i_cpustates(int *states) 420 { 421 int i = 0; 422 int value; 423 const char * const *names; 424 const char *thisname; 425 int cpu; 426 427 for (cpu = 0; cpu < num_cpus; cpu++) { 428 names = cpustate_names; 429 430 /* print tag and bump lastline */ 431 if (num_cpus == 1) 432 printf("\nCPU: "); 433 else { 434 value = printf("\nCPU %d: ", cpu); 435 while (value++ <= cpustates_column) 436 printf(" "); 437 } 438 lastline++; 439 440 /* now walk thru the names and print the line */ 441 while ((thisname = *names++) != NULL) 442 { 443 if (*thisname != '\0') 444 { 445 /* retrieve the value and remember it */ 446 value = *states++; 447 448 /* if percentage is >= 1000, print it as 100% */ 449 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"), 450 (i++ % num_cpustates) == 0 ? "" : ", ", 451 ((float)value)/10., 452 thisname); 453 } 454 } 455 } 456 457 /* copy over values into "last" array */ 458 memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus); 459 } 460 461 void 462 u_cpustates(int *states) 463 { 464 int value; 465 const char * const *names; 466 const char *thisname; 467 int *lp; 468 int *colp; 469 int cpu; 470 471 for (cpu = 0; cpu < num_cpus; cpu++) { 472 names = cpustate_names; 473 474 Move_to(cpustates_column, y_cpustates + cpu); 475 lastline = y_cpustates + cpu; 476 lp = lcpustates + (cpu * num_cpustates); 477 colp = cpustate_columns; 478 479 /* we could be much more optimal about this */ 480 while ((thisname = *names++) != NULL) 481 { 482 if (*thisname != '\0') 483 { 484 /* did the value change since last time? */ 485 if (*lp != *states) 486 { 487 /* yes, move and change */ 488 Move_to(cpustates_column + *colp, y_cpustates + cpu); 489 lastline = y_cpustates + cpu; 490 491 /* retrieve value and remember it */ 492 value = *states; 493 494 /* if percentage is >= 1000, print it as 100% */ 495 printf((value >= 1000 ? "%4.0f" : "%4.1f"), 496 ((double)value)/10.); 497 498 /* remember it for next time */ 499 *lp = value; 500 } 501 } 502 503 /* increment and move on */ 504 lp++; 505 states++; 506 colp++; 507 } 508 } 509 } 510 511 void 512 z_cpustates(void) 513 { 514 int i = 0; 515 const char **names; 516 char *thisname; 517 int cpu, value; 518 519 for (cpu = 0; cpu < num_cpus; cpu++) { 520 names = cpustate_names; 521 522 /* show tag and bump lastline */ 523 if (num_cpus == 1) 524 printf("\nCPU: "); 525 else { 526 value = printf("\nCPU %d: ", cpu); 527 while (value++ <= cpustates_column) 528 printf(" "); 529 } 530 lastline++; 531 532 while ((thisname = *names++) != NULL) 533 { 534 if (*thisname != '\0') 535 { 536 printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname); 537 } 538 } 539 } 540 541 /* fill the "last" array with all -1s, to insure correct updating */ 542 for (i = 0; i < num_cpustates * num_cpus; ++i) { 543 lcpustates[i] = -1; 544 } 545 } 546 547 /* 548 * *_memory(stats) - print "Memory: " followed by the memory summary string 549 * 550 * Assumptions: cursor is on "lastline" 551 * for i_memory ONLY: cursor is on the previous line 552 */ 553 554 static char memory_buffer[MAX_COLS]; 555 556 void 557 i_memory(int *stats) 558 { 559 fputs("\nMem: ", stdout); 560 lastline++; 561 562 /* format and print the memory summary */ 563 summary_format(memory_buffer, stats, memory_names); 564 fputs(memory_buffer, stdout); 565 } 566 567 void 568 u_memory(int *stats) 569 { 570 static char new[MAX_COLS]; 571 572 /* format the new line */ 573 summary_format(new, stats, memory_names); 574 line_update(memory_buffer, new, x_mem, y_mem); 575 } 576 577 /* 578 * *_arc(stats) - print "ARC: " followed by the ARC summary string 579 * 580 * Assumptions: cursor is on "lastline" 581 * for i_arc ONLY: cursor is on the previous line 582 */ 583 static char arc_buffer[MAX_COLS]; 584 585 void 586 i_arc(int *stats) 587 { 588 if (arc_names == NULL) 589 return; 590 591 fputs("\nARC: ", stdout); 592 lastline++; 593 594 /* format and print the memory summary */ 595 summary_format(arc_buffer, stats, arc_names); 596 fputs(arc_buffer, stdout); 597 } 598 599 void 600 u_arc(int *stats) 601 { 602 static char new[MAX_COLS]; 603 604 if (arc_names == NULL) 605 return; 606 607 /* format the new line */ 608 summary_format(new, stats, arc_names); 609 line_update(arc_buffer, new, x_arc, y_arc); 610 } 611 612 613 /* 614 * *_carc(stats) - print "Compressed ARC: " followed by the summary string 615 * 616 * Assumptions: cursor is on "lastline" 617 * for i_carc ONLY: cursor is on the previous line 618 */ 619 static char carc_buffer[MAX_COLS]; 620 621 void 622 i_carc(int *stats) 623 { 624 if (carc_names == NULL) 625 return; 626 627 fputs("\n ", stdout); 628 lastline++; 629 630 /* format and print the memory summary */ 631 summary_format(carc_buffer, stats, carc_names); 632 fputs(carc_buffer, stdout); 633 } 634 635 void 636 u_carc(int *stats) 637 { 638 static char new[MAX_COLS]; 639 640 if (carc_names == NULL) 641 return; 642 643 /* format the new line */ 644 summary_format(new, stats, carc_names); 645 line_update(carc_buffer, new, x_carc, y_carc); 646 } 647 648 /* 649 * *_swap(stats) - print "Swap: " followed by the swap summary string 650 * 651 * Assumptions: cursor is on "lastline" 652 * for i_swap ONLY: cursor is on the previous line 653 */ 654 655 static char swap_buffer[MAX_COLS]; 656 657 void 658 i_swap(int *stats) 659 { 660 fputs("\nSwap: ", stdout); 661 lastline++; 662 663 /* format and print the swap summary */ 664 summary_format(swap_buffer, stats, swap_names); 665 fputs(swap_buffer, stdout); 666 } 667 668 void 669 u_swap(int *stats) 670 { 671 static char new[MAX_COLS]; 672 673 /* format the new line */ 674 summary_format(new, stats, swap_names); 675 line_update(swap_buffer, new, x_swap, y_swap); 676 } 677 678 /* 679 * *_message() - print the next pending message line, or erase the one 680 * that is there. 681 * 682 * Note that u_message is (currently) the same as i_message. 683 * 684 * Assumptions: lastline is consistent 685 */ 686 687 /* 688 * i_message is funny because it gets its message asynchronously (with 689 * respect to screen updates). 690 */ 691 692 static char next_msg[MAX_COLS + 5]; 693 static int msglen = 0; 694 /* Invariant: msglen is always the length of the message currently displayed 695 on the screen (even when next_msg doesn't contain that message). */ 696 697 void 698 i_message(void) 699 { 700 701 while (lastline < y_message) 702 { 703 fputc('\n', stdout); 704 lastline++; 705 } 706 if (next_msg[0] != '\0') 707 { 708 top_standout(next_msg); 709 msglen = strlen(next_msg); 710 next_msg[0] = '\0'; 711 } 712 else if (msglen > 0) 713 { 714 (void) clear_eol(msglen); 715 msglen = 0; 716 } 717 } 718 719 void 720 u_message(void) 721 { 722 i_message(); 723 } 724 725 static int header_length; 726 727 /* 728 * Trim a header string to the current display width and return a newly 729 * allocated area with the trimmed header. 730 */ 731 732 const char * 733 trim_header(const char *text) 734 { 735 char *s; 736 int width; 737 738 s = NULL; 739 width = display_width; 740 header_length = strlen(text); 741 if (header_length >= width) { 742 s = strndup(text, width); 743 if (s == NULL) 744 return (NULL); 745 } 746 return (s); 747 } 748 749 /* 750 * *_header(text) - print the header for the process area 751 * 752 * Assumptions: cursor is on the previous line and lastline is consistent 753 */ 754 755 void 756 i_header(const char *text) 757 { 758 char *s; 759 760 s = trim_header(text); 761 if (s != NULL) 762 text = s; 763 764 if (header_status == ON) 765 { 766 putchar('\n'); 767 fputs(text, stdout); 768 lastline++; 769 } 770 else if (header_status == ERASE) 771 { 772 header_status = OFF; 773 } 774 free(s); 775 } 776 777 void 778 u_header(const char *text __unused) 779 { 780 781 if (header_status == ERASE) 782 { 783 putchar('\n'); 784 lastline++; 785 clear_eol(header_length); 786 header_status = OFF; 787 } 788 } 789 790 /* 791 * *_process(line, thisline) - print one process line 792 * 793 * Assumptions: lastline is consistent 794 */ 795 796 void 797 i_process(int line, char *thisline) 798 { 799 char *p; 800 char *base; 801 802 /* make sure we are on the correct line */ 803 while (lastline < y_procs + line) 804 { 805 putchar('\n'); 806 lastline++; 807 } 808 809 /* truncate the line to conform to our current screen width */ 810 thisline[display_width] = '\0'; 811 812 /* write the line out */ 813 fputs(thisline, stdout); 814 815 /* copy it in to our buffer */ 816 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf; 817 p = stpcpy(base, thisline); 818 819 /* zero fill the rest of it */ 820 memset(p, 0, display_width - (p - base)); 821 } 822 823 void 824 u_process(int line, char *newline) 825 { 826 char *optr; 827 int screen_line = line + Header_lines; 828 char *bufferline; 829 830 /* remember a pointer to the current line in the screen buffer */ 831 bufferline = &screenbuf[lineindex(line)]; 832 833 /* truncate the line to conform to our current screen width */ 834 newline[display_width] = '\0'; 835 836 /* is line higher than we went on the last display? */ 837 if (line >= last_hi) 838 { 839 /* yes, just ignore screenbuf and write it out directly */ 840 /* get positioned on the correct line */ 841 if (screen_line - lastline == 1) 842 { 843 putchar('\n'); 844 lastline++; 845 } 846 else 847 { 848 Move_to(0, screen_line); 849 lastline = screen_line; 850 } 851 852 /* now write the line */ 853 fputs(newline, stdout); 854 855 /* copy it in to the buffer */ 856 optr = stpcpy(bufferline, newline); 857 858 /* zero fill the rest of it */ 859 memset(optr, 0, display_width - (optr - bufferline)); 860 } 861 else 862 { 863 line_update(bufferline, newline, 0, line + Header_lines); 864 } 865 } 866 867 void 868 u_endscreen(int hi) 869 { 870 int screen_line = hi + Header_lines; 871 int i; 872 873 if (smart_terminal) 874 { 875 if (hi < last_hi) 876 { 877 /* need to blank the remainder of the screen */ 878 /* but only if there is any screen left below this line */ 879 if (lastline + 1 < screen_length) 880 { 881 /* efficiently move to the end of currently displayed info */ 882 if (screen_line - lastline < 5) 883 { 884 while (lastline < screen_line) 885 { 886 putchar('\n'); 887 lastline++; 888 } 889 } 890 else 891 { 892 Move_to(0, screen_line); 893 lastline = screen_line; 894 } 895 896 if (clear_to_end) 897 { 898 /* we can do this the easy way */ 899 putcap(clear_to_end); 900 } 901 else 902 { 903 /* use clear_eol on each line */ 904 i = hi; 905 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi) 906 { 907 putchar('\n'); 908 } 909 } 910 } 911 } 912 last_hi = hi; 913 914 /* move the cursor to a pleasant place */ 915 Move_to(x_idlecursor, y_idlecursor); 916 lastline = y_idlecursor; 917 } 918 else 919 { 920 /* separate this display from the next with some vertical room */ 921 fputs("\n\n", stdout); 922 } 923 } 924 925 void 926 display_header(int t) 927 { 928 929 if (t) 930 { 931 header_status = ON; 932 } 933 else if (header_status == ON) 934 { 935 header_status = ERASE; 936 } 937 } 938 939 void 940 new_message(int type, const char *msgfmt, ...) 941 { 942 va_list args; 943 size_t i; 944 945 va_start(args, msgfmt); 946 947 /* first, format the message */ 948 vsnprintf(next_msg, sizeof(next_msg), msgfmt, args); 949 950 va_end(args); 951 952 if (msglen > 0) 953 { 954 /* message there already -- can we clear it? */ 955 if (!overstrike) 956 { 957 /* yes -- write it and clear to end */ 958 i = strlen(next_msg); 959 if ((type & MT_delayed) == 0) 960 { 961 if (type & MT_standout) { 962 top_standout(next_msg); 963 } else { 964 fputs(next_msg, stdout); 965 } 966 clear_eol(msglen - i); 967 msglen = i; 968 next_msg[0] = '\0'; 969 } 970 } 971 } 972 else 973 { 974 if ((type & MT_delayed) == 0) 975 { 976 if (type & MT_standout) { 977 top_standout(next_msg); 978 } else { 979 fputs(next_msg, stdout); 980 } 981 msglen = strlen(next_msg); 982 next_msg[0] = '\0'; 983 } 984 } 985 } 986 987 void 988 clear_message(void) 989 { 990 if (clear_eol(msglen) == 1) 991 { 992 putchar('\r'); 993 } 994 } 995 996 int 997 readline(char *buffer, int size, int numeric) 998 { 999 char *ptr = buffer; 1000 char ch; 1001 char cnt = 0; 1002 char maxcnt = 0; 1003 1004 /* allow room for null terminator */ 1005 size -= 1; 1006 1007 /* read loop */ 1008 while ((fflush(stdout), read(0, ptr, 1) > 0)) 1009 { 1010 /* newline means we are done */ 1011 if ((ch = *ptr) == '\n' || ch == '\r') 1012 { 1013 break; 1014 } 1015 1016 /* handle special editing characters */ 1017 if (ch == ch_kill) 1018 { 1019 /* kill line -- account for overstriking */ 1020 if (overstrike) 1021 { 1022 msglen += maxcnt; 1023 } 1024 1025 /* return null string */ 1026 *buffer = '\0'; 1027 putchar('\r'); 1028 return(-1); 1029 } 1030 else if (ch == ch_erase) 1031 { 1032 /* erase previous character */ 1033 if (cnt <= 0) 1034 { 1035 /* none to erase! */ 1036 putchar('\7'); 1037 } 1038 else 1039 { 1040 fputs("\b \b", stdout); 1041 ptr--; 1042 cnt--; 1043 } 1044 } 1045 /* check for character validity and buffer overflow */ 1046 else if (cnt == size || (numeric && !isdigit(ch)) || 1047 !isprint(ch)) 1048 { 1049 /* not legal */ 1050 putchar('\7'); 1051 } 1052 else 1053 { 1054 /* echo it and store it in the buffer */ 1055 putchar(ch); 1056 ptr++; 1057 cnt++; 1058 if (cnt > maxcnt) 1059 { 1060 maxcnt = cnt; 1061 } 1062 } 1063 } 1064 1065 /* all done -- null terminate the string */ 1066 *ptr = '\0'; 1067 1068 /* account for the extra characters in the message area */ 1069 /* (if terminal overstrikes, remember the furthest they went) */ 1070 msglen += overstrike ? maxcnt : cnt; 1071 1072 /* return either inputted number or string length */ 1073 putchar('\r'); 1074 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt); 1075 } 1076 1077 /* internal support routines */ 1078 1079 static void 1080 summary_format(char *str, int *numbers, const char * const *names) 1081 { 1082 char *p; 1083 int num; 1084 const char *thisname; 1085 char rbuf[6]; 1086 1087 /* format each number followed by its string */ 1088 p = str; 1089 while ((thisname = *names++) != NULL) 1090 { 1091 /* get the number to format */ 1092 num = *numbers++; 1093 1094 /* display only non-zero numbers */ 1095 if (num > 0) 1096 { 1097 /* is this number in kilobytes? */ 1098 if (thisname[0] == 'K') 1099 { 1100 /* yes: format it as a memory value */ 1101 p = stpcpy(p, format_k(num)); 1102 1103 /* skip over the K, since it was included by format_k */ 1104 p = stpcpy(p, thisname+1); 1105 } 1106 /* is this number a ratio? */ 1107 else if (thisname[0] == ':') 1108 { 1109 (void) snprintf(rbuf, sizeof(rbuf), "%.2f", 1110 (float)*(numbers - 2) / (float)num); 1111 p = stpcpy(p, rbuf); 1112 p = stpcpy(p, thisname); 1113 } 1114 else 1115 { 1116 p = stpcpy(p, itoa(num)); 1117 p = stpcpy(p, thisname); 1118 } 1119 } 1120 1121 /* ignore negative numbers, but display corresponding string */ 1122 else if (num < 0) 1123 { 1124 p = stpcpy(p, thisname); 1125 } 1126 } 1127 1128 /* if the last two characters in the string are ", ", delete them */ 1129 p -= 2; 1130 if (p >= str && p[0] == ',' && p[1] == ' ') 1131 { 1132 *p = '\0'; 1133 } 1134 } 1135 1136 static void 1137 line_update(char *old, char *new, int start, int line) 1138 { 1139 int ch; 1140 int diff; 1141 int newcol = start + 1; 1142 int lastcol = start; 1143 char cursor_on_line = false; 1144 char *current; 1145 1146 /* compare the two strings and only rewrite what has changed */ 1147 current = old; 1148 #ifdef DEBUG 1149 fprintf(debug, "line_update, starting at %d\n", start); 1150 fputs(old, debug); 1151 fputc('\n', debug); 1152 fputs(new, debug); 1153 fputs("\n-\n", debug); 1154 #endif 1155 1156 /* start things off on the right foot */ 1157 /* this is to make sure the invariants get set up right */ 1158 if ((ch = *new++) != *old) 1159 { 1160 if (line - lastline == 1 && start == 0) 1161 { 1162 putchar('\n'); 1163 } 1164 else 1165 { 1166 Move_to(start, line); 1167 } 1168 cursor_on_line = true; 1169 putchar(ch); 1170 *old = ch; 1171 lastcol = 1; 1172 } 1173 old++; 1174 1175 /* 1176 * main loop -- check each character. If the old and new aren't the 1177 * same, then update the display. When the distance from the 1178 * current cursor position to the new change is small enough, 1179 * the characters that belong there are written to move the 1180 * cursor over. 1181 * 1182 * Invariants: 1183 * lastcol is the column where the cursor currently is sitting 1184 * (always one beyond the end of the last mismatch). 1185 */ 1186 do /* yes, a do...while */ 1187 { 1188 if ((ch = *new++) != *old) 1189 { 1190 /* new character is different from old */ 1191 /* make sure the cursor is on top of this character */ 1192 diff = newcol - lastcol; 1193 if (diff > 0) 1194 { 1195 /* some motion is required--figure out which is shorter */ 1196 if (diff < 6 && cursor_on_line) 1197 { 1198 /* overwrite old stuff--get it out of the old buffer */ 1199 printf("%.*s", diff, ¤t[lastcol-start]); 1200 } 1201 else 1202 { 1203 /* use cursor addressing */ 1204 Move_to(newcol, line); 1205 cursor_on_line = true; 1206 } 1207 /* remember where the cursor is */ 1208 lastcol = newcol + 1; 1209 } 1210 else 1211 { 1212 /* already there, update position */ 1213 lastcol++; 1214 } 1215 1216 /* write what we need to */ 1217 if (ch == '\0') 1218 { 1219 /* at the end--terminate with a clear-to-end-of-line */ 1220 (void) clear_eol(strlen(old)); 1221 } 1222 else 1223 { 1224 /* write the new character */ 1225 putchar(ch); 1226 } 1227 /* put the new character in the screen buffer */ 1228 *old = ch; 1229 } 1230 1231 /* update working column and screen buffer pointer */ 1232 newcol++; 1233 old++; 1234 1235 } while (ch != '\0'); 1236 1237 /* zero out the rest of the line buffer -- MUST BE DONE! */ 1238 diff = display_width - newcol; 1239 if (diff > 0) 1240 { 1241 memset(old, 0, diff); 1242 } 1243 1244 /* remember where the current line is */ 1245 if (cursor_on_line) 1246 { 1247 lastline = line; 1248 } 1249 } 1250 1251 /* 1252 * printable(str) - make the string pointed to by "str" into one that is 1253 * printable (i.e.: all ascii), by converting all non-printable 1254 * characters into '?'. Replacements are done in place and a pointer 1255 * to the original buffer is returned. 1256 */ 1257 1258 char * 1259 printable(char str[]) 1260 { 1261 char *ptr; 1262 char ch; 1263 1264 ptr = str; 1265 if (utf8flag) { 1266 while ((ch = *ptr) != '\0') { 1267 if (0x00 == (0x80 & ch)) { 1268 if (!isprint(ch)) { 1269 *ptr = '?'; 1270 } 1271 ++ptr; 1272 } else if (0xC0 == (0xE0 & ch)) { 1273 ++ptr; 1274 if ('\0' != *ptr) ++ptr; 1275 } else if (0xE0 == (0xF0 & ch)) { 1276 ++ptr; 1277 if ('\0' != *ptr) ++ptr; 1278 if ('\0' != *ptr) ++ptr; 1279 } else if (0xF0 == (0xF8 & ch)) { 1280 ++ptr; 1281 if ('\0' != *ptr) ++ptr; 1282 if ('\0' != *ptr) ++ptr; 1283 if ('\0' != *ptr) ++ptr; 1284 } else { 1285 *ptr = '?'; 1286 ++ptr; 1287 } 1288 } 1289 } else { 1290 while ((ch = *ptr) != '\0') { 1291 if (!isprint(ch)) { 1292 *ptr = '?'; 1293 } 1294 ptr++; 1295 } 1296 } 1297 return(str); 1298 } 1299 1300 void 1301 i_uptime(struct timeval *bt, time_t *tod) 1302 { 1303 time_t uptime; 1304 int days, hrs, mins, secs; 1305 1306 if (bt->tv_sec != -1) { 1307 uptime = *tod - bt->tv_sec; 1308 days = uptime / 86400; 1309 uptime %= 86400; 1310 hrs = uptime / 3600; 1311 uptime %= 3600; 1312 mins = uptime / 60; 1313 secs = uptime % 60; 1314 1315 /* 1316 * Display the uptime. 1317 */ 1318 1319 if (smart_terminal) 1320 { 1321 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0); 1322 } 1323 else 1324 { 1325 fputs(" ", stdout); 1326 } 1327 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs); 1328 } 1329 } 1330