2 * Top users/processes display for Unix
5 * This program may be freely redistributed,
6 * but this entire comment MUST remain intact.
8 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
9 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
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.
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.
26 * The routines are called in this order: *_loadave, i_timeofday,
27 * *_procstates, *_cpustates, *_memory, *_message, *_header,
28 * *_process, u_endscreen.
36 #include "screen.h" /* interface to screen package */
37 #include "layout.h" /* defines for screen position layout */
40 #include "top.local.h"
42 #include "machine.h" /* we should eliminate this!!! */
49 /* imported from screen.c */
50 extern int overstrike;
53 static int last_hi = 0; /* used in u_process and u_endscreen */
54 static int lastline = 0;
55 static int display_width = MAX_COLS;
57 #define lineindex(l) ((l)*display_width)
61 /* things initialized by display_init and used thruout */
63 /* buffer of proc information lines for display updating */
64 char *screenbuf = NULL;
66 static char **procstate_names;
67 static char **cpustate_names;
68 static char **memory_names;
69 static char **swap_names;
71 static int num_procstates;
72 static int num_cpustates;
73 static int num_memory;
76 static int *lprocstates;
77 static int *lcpustates;
81 static int *cpustate_columns;
82 static int cpustate_total_length;
84 static enum { OFF, ON, ERASE } header_status = ON;
86 static int string_count();
87 static void summary_format();
88 static void line_update();
95 /* first, deallocate any previous buffer that may have been there */
96 if (screenbuf != NULL)
101 /* calculate the current dimensions */
102 /* if operating in "dumb" mode, we only need one line */
103 lines = smart_terminal ? screen_length - Header_lines : 1;
107 /* we don't want more than MAX_COLS columns, since the machine-dependent
108 modules make static allocations based on MAX_COLS and we don't want
109 to run off the end of their buffers */
110 display_width = screen_width;
111 if (display_width >= MAX_COLS)
113 display_width = MAX_COLS - 1;
116 /* now, allocate space for the screen buffer */
117 screenbuf = (char *)malloc(lines * display_width);
118 if (screenbuf == (char *)NULL)
124 /* return number of lines available */
125 /* for dumb terminals, pretend like we can show any amount */
126 return(smart_terminal ? lines : Largest);
129 int display_init(statics)
131 struct statics *statics;
139 /* call resize to do the dirty work */
140 lines = display_resize();
142 /* only do the rest if we need to */
145 /* save pointers and allocate space for names */
146 procstate_names = statics->procstate_names;
147 num_procstates = string_count(procstate_names);
148 lprocstates = (int *)malloc(num_procstates * sizeof(int));
150 cpustate_names = statics->cpustate_names;
152 swap_names = statics->swap_names;
153 num_swap = string_count(swap_names);
154 lswap = (int *)malloc(num_swap * sizeof(int));
155 num_cpustates = string_count(cpustate_names);
156 lcpustates = (int *)malloc(num_cpustates * sizeof(int));
157 cpustate_columns = (int *)malloc(num_cpustates * sizeof(int));
159 memory_names = statics->memory_names;
160 num_memory = string_count(memory_names);
161 lmemory = (int *)malloc(num_memory * sizeof(int));
163 /* calculate starting columns where needed */
164 cpustate_total_length = 0;
166 ip = cpustate_columns;
169 *ip++ = cpustate_total_length;
170 if ((i = strlen(*pp++)) > 0)
172 cpustate_total_length += i + 8;
177 /* return number of lines available */
181 i_loadave(mpid, avenrun)
189 /* i_loadave also clears the screen, since it is first */
192 /* mpid == -1 implies this system doesn't have an _mpid */
195 printf("last pid: %5d; ", mpid);
198 printf("load averages");
200 for (i = 0; i < 3; i++)
209 u_loadave(mpid, avenrun)
219 /* change screen only when value has really changed */
222 Move_to(x_lastpid, y_lastpid);
227 /* i remembers x coordinate to move to */
232 i = x_loadave_nompid;
235 /* move into position for load averages */
236 Move_to(i, y_loadave);
238 /* display new load averages */
239 /* we should optimize this and only display changes */
240 for (i = 0; i < 3; i++)
254 * Display the current time.
255 * "ctime" always returns a string that looks like this:
257 * Sun Sep 16 01:03:52 1973
258 * 012345678901234567890123
261 * We want indices 11 thru 18 (length 8).
266 Move_to(screen_width - 8, 0);
279 printf("%-8.8s\n", &(ctime(tod)[11]));
283 static int ltotal = 0;
284 static char procstates_buffer[MAX_COLS];
287 * *_procstates(total, brkdn, names) - print the process summary line
289 * Assumptions: cursor is at the beginning of the line on entry
293 i_procstates(total, brkdn)
301 /* write current number of processes and remember the value */
302 printf("%d processes:", total);
305 /* put out enough spaces to get to column 15 */
312 /* format and print the process state summary */
313 summary_format(procstates_buffer, brkdn, procstate_names);
314 fputs(procstates_buffer, stdout);
316 /* save the numbers for next time */
317 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
320 u_procstates(total, brkdn)
326 static char new[MAX_COLS];
329 /* update number of processes only if it has changed */
332 /* move and overwrite */
333 #if (x_procstate == 0)
334 Move_to(x_procstate, y_procstate);
336 /* cursor is already there...no motion needed */
337 /* assert(lastline == 1); */
341 /* if number of digits differs, rewrite the label */
342 if (digits(total) != digits(ltotal))
344 fputs(" processes:", stdout);
345 /* put out enough spaces to get to column 15 */
351 /* cursor may end up right where we want it!!! */
358 /* see if any of the state numbers has changed */
359 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
361 /* format and update the line */
362 summary_format(new, brkdn, procstate_names);
363 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
364 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
369 * *_cpustates(states, names) - print the cpu state percentages
371 * Assumptions: cursor is on the PREVIOUS line
374 static int cpustates_column;
376 /* cpustates_tag() calculates the correct tag to use to label the line */
378 char *cpustates_tag()
383 static char *short_tag = "CPU: ";
384 static char *long_tag = "CPU states: ";
386 /* if length + strlen(long_tag) >= screen_width, then we have to
387 use the shorter tag (we subtract 2 to account for ": ") */
388 if (cpustate_total_length + (int)strlen(long_tag) - 2 >= screen_width)
397 /* set cpustates_column accordingly then return result */
398 cpustates_column = strlen(use);
404 register int *states;
409 register char **names = cpustate_names;
410 register char *thisname;
412 /* print tag and bump lastline */
413 printf("\n%s", cpustates_tag());
416 /* now walk thru the names and print the line */
417 while ((thisname = *names++) != NULL)
419 if (*thisname != '\0')
421 /* retrieve the value and remember it */
424 /* if percentage is >= 1000, print it as 100% */
425 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
426 i++ == 0 ? "" : ", ",
432 /* copy over values into "last" array */
433 memcpy(lcpustates, states, num_cpustates * sizeof(int));
438 register int *states;
442 register char **names = cpustate_names;
443 register char *thisname;
447 Move_to(cpustates_column, y_cpustates);
448 lastline = y_cpustates;
450 colp = cpustate_columns;
452 /* we could be much more optimal about this */
453 while ((thisname = *names++) != NULL)
455 if (*thisname != '\0')
457 /* did the value change since last time? */
460 /* yes, move and change */
461 Move_to(cpustates_column + *colp, y_cpustates);
462 lastline = y_cpustates;
464 /* retrieve value and remember it */
467 /* if percentage is >= 1000, print it as 100% */
468 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
469 ((double)value)/10.);
471 /* remember it for next time */
476 /* increment and move on */
487 register char **names = cpustate_names;
488 register char *thisname;
491 /* show tag and bump lastline */
492 printf("\n%s", cpustates_tag());
495 while ((thisname = *names++) != NULL)
497 if (*thisname != '\0')
499 printf("%s %% %s", i++ == 0 ? "" : ", ", thisname);
503 /* fill the "last" array with all -1s, to insure correct updating */
513 * *_memory(stats) - print "Memory: " followed by the memory summary string
515 * Assumptions: cursor is on "lastline"
516 * for i_memory ONLY: cursor is on the previous line
519 char memory_buffer[MAX_COLS];
526 fputs("\nMem: ", stdout);
529 /* format and print the memory summary */
530 summary_format(memory_buffer, stats, memory_names);
531 fputs(memory_buffer, stdout);
539 static char new[MAX_COLS];
541 /* format the new line */
542 summary_format(new, stats, memory_names);
543 line_update(memory_buffer, new, x_mem, y_mem);
547 * *_swap(stats) - print "Swap: " followed by the swap summary string
549 * Assumptions: cursor is on "lastline"
550 * for i_swap ONLY: cursor is on the previous line
553 char swap_buffer[MAX_COLS];
560 fputs("\nSwap: ", stdout);
563 /* format and print the swap summary */
564 summary_format(swap_buffer, stats, swap_names);
565 fputs(swap_buffer, stdout);
573 static char new[MAX_COLS];
575 /* format the new line */
576 summary_format(new, stats, swap_names);
577 line_update(swap_buffer, new, x_swap, y_swap);
581 * *_message() - print the next pending message line, or erase the one
584 * Note that u_message is (currently) the same as i_message.
586 * Assumptions: lastline is consistent
590 * i_message is funny because it gets its message asynchronously (with
591 * respect to screen updates).
594 static char next_msg[MAX_COLS + 5];
595 static int msglen = 0;
596 /* Invariant: msglen is always the length of the message currently displayed
597 on the screen (even when next_msg doesn't contain that message). */
602 while (lastline < y_message)
607 if (next_msg[0] != '\0')
610 msglen = strlen(next_msg);
615 (void) clear_eol(msglen);
626 static int header_length;
629 * Trim a header string to the current display width and return a newly
630 * allocated area with the trimmed header.
643 width = display_width;
644 header_length = strlen(text);
645 if (header_length >= width) {
646 s = malloc((width + 1) * sizeof(char));
649 strncpy(s, text, width);
656 * *_header(text) - print the header for the process area
658 * Assumptions: cursor is on the previous line and lastline is consistent
668 s = trim_header(text);
672 if (header_status == ON)
678 else if (header_status == ERASE)
688 char *text; /* ignored */
692 if (header_status == ERASE)
696 clear_eol(header_length);
702 * *_process(line, thisline) - print one process line
704 * Assumptions: lastline is consistent
707 i_process(line, thisline)
716 /* make sure we are on the correct line */
717 while (lastline < y_procs + line)
723 /* truncate the line to conform to our current screen width */
724 thisline[display_width] = '\0';
726 /* write the line out */
727 fputs(thisline, stdout);
729 /* copy it in to our buffer */
730 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
731 p = strecpy(base, thisline);
733 /* zero fill the rest of it */
734 memzero(p, display_width - (p - base));
737 u_process(line, newline)
744 register int screen_line = line + Header_lines;
745 register char *bufferline;
747 /* remember a pointer to the current line in the screen buffer */
748 bufferline = &screenbuf[lineindex(line)];
750 /* truncate the line to conform to our current screen width */
751 newline[display_width] = '\0';
753 /* is line higher than we went on the last display? */
756 /* yes, just ignore screenbuf and write it out directly */
757 /* get positioned on the correct line */
758 if (screen_line - lastline == 1)
765 Move_to(0, screen_line);
766 lastline = screen_line;
769 /* now write the line */
770 fputs(newline, stdout);
772 /* copy it in to the buffer */
773 optr = strecpy(bufferline, newline);
775 /* zero fill the rest of it */
776 memzero(optr, display_width - (optr - bufferline));
780 line_update(bufferline, newline, 0, line + Header_lines);
789 register int screen_line = hi + Header_lines;
796 /* need to blank the remainder of the screen */
797 /* but only if there is any screen left below this line */
798 if (lastline + 1 < screen_length)
800 /* efficiently move to the end of currently displayed info */
801 if (screen_line - lastline < 5)
803 while (lastline < screen_line)
811 Move_to(0, screen_line);
812 lastline = screen_line;
817 /* we can do this the easy way */
818 putcap(clear_to_end);
822 /* use clear_eol on each line */
824 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
833 /* move the cursor to a pleasant place */
834 Move_to(x_idlecursor, y_idlecursor);
835 lastline = y_idlecursor;
839 /* separate this display from the next with some vertical room */
840 fputs("\n\n", stdout);
853 else if (header_status == ON)
855 header_status = ERASE;
860 new_message(type, msgfmt, a1, a2, a3)
869 /* first, format the message */
870 (void) snprintf(next_msg, sizeof(next_msg), msgfmt, a1, a2, a3);
874 /* message there already -- can we clear it? */
877 /* yes -- write it and clear to end */
878 i = strlen(next_msg);
879 if ((type & MT_delayed) == 0)
881 type & MT_standout ? standout(next_msg) :
882 fputs(next_msg, stdout);
883 (void) clear_eol(msglen - i);
891 if ((type & MT_delayed) == 0)
893 type & MT_standout ? standout(next_msg) : fputs(next_msg, stdout);
894 msglen = strlen(next_msg);
903 if (clear_eol(msglen) == 1)
909 readline(buffer, size, numeric)
916 register char *ptr = buffer;
918 register char cnt = 0;
919 register char maxcnt = 0;
921 /* allow room for null terminator */
925 while ((fflush(stdout), read(0, ptr, 1) > 0))
927 /* newline means we are done */
928 if ((ch = *ptr) == '\n' || ch == '\r')
933 /* handle special editing characters */
936 /* kill line -- account for overstriking */
942 /* return null string */
947 else if (ch == ch_erase)
949 /* erase previous character */
957 fputs("\b \b", stdout);
962 /* check for character validity and buffer overflow */
963 else if (cnt == size || (numeric && !isdigit(ch)) ||
971 /* echo it and store it in the buffer */
982 /* all done -- null terminate the string */
985 /* account for the extra characters in the message area */
986 /* (if terminal overstrikes, remember the furthest they went) */
987 msglen += overstrike ? maxcnt : cnt;
989 /* return either inputted number or string length */
991 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
994 /* internal support routines */
996 static int string_count(pp)
1004 while (*pp++ != NULL)
1011 static void summary_format(str, numbers, names)
1015 register char **names;
1020 register char *thisname;
1021 register int useM = No;
1023 /* format each number followed by its string */
1025 while ((thisname = *names++) != NULL)
1027 /* get the number to format */
1030 /* display only non-zero numbers */
1033 /* is this number in kilobytes? */
1034 if (thisname[0] == 'K')
1036 /* yes: format it as a memory value */
1037 p = strecpy(p, format_k(num));
1039 /* skip over the K, since it was included by format_k */
1040 p = strecpy(p, thisname+1);
1044 p = strecpy(p, itoa(num));
1045 p = strecpy(p, thisname);
1049 /* ignore negative numbers, but display corresponding string */
1052 p = strecpy(p, thisname);
1056 /* if the last two characters in the string are ", ", delete them */
1058 if (p >= str && p[0] == ',' && p[1] == ' ')
1064 static void line_update(old, new, start, line)
1074 register int newcol = start + 1;
1075 register int lastcol = start;
1076 char cursor_on_line = No;
1079 /* compare the two strings and only rewrite what has changed */
1082 fprintf(debug, "line_update, starting at %d\n", start);
1086 fputs("\n-\n", debug);
1089 /* start things off on the right foot */
1090 /* this is to make sure the invariants get set up right */
1091 if ((ch = *new++) != *old)
1093 if (line - lastline == 1 && start == 0)
1099 Move_to(start, line);
1101 cursor_on_line = Yes;
1109 * main loop -- check each character. If the old and new aren't the
1110 * same, then update the display. When the distance from the
1111 * current cursor position to the new change is small enough,
1112 * the characters that belong there are written to move the
1116 * lastcol is the column where the cursor currently is sitting
1117 * (always one beyond the end of the last mismatch).
1119 do /* yes, a do...while */
1121 if ((ch = *new++) != *old)
1123 /* new character is different from old */
1124 /* make sure the cursor is on top of this character */
1125 diff = newcol - lastcol;
1128 /* some motion is required--figure out which is shorter */
1129 if (diff < 6 && cursor_on_line)
1131 /* overwrite old stuff--get it out of the old buffer */
1132 printf("%.*s", diff, ¤t[lastcol-start]);
1136 /* use cursor addressing */
1137 Move_to(newcol, line);
1138 cursor_on_line = Yes;
1140 /* remember where the cursor is */
1141 lastcol = newcol + 1;
1145 /* already there, update position */
1149 /* write what we need to */
1152 /* at the end--terminate with a clear-to-end-of-line */
1153 (void) clear_eol(strlen(old));
1157 /* write the new character */
1160 /* put the new character in the screen buffer */
1164 /* update working column and screen buffer pointer */
1168 } while (ch != '\0');
1170 /* zero out the rest of the line buffer -- MUST BE DONE! */
1171 diff = display_width - newcol;
1177 /* remember where the current line is */
1185 * printable(str) - make the string pointed to by "str" into one that is
1186 * printable (i.e.: all ascii), by converting all non-printable
1187 * characters into '?'. Replacements are done in place and a pointer
1188 * to the original buffer is returned.
1191 char *printable(str)
1200 while ((ch = *ptr) != '\0')
1218 int days, hrs, mins, secs;
1220 if (bt->tv_sec != -1) {
1221 uptime = *tod - bt->tv_sec;
1223 days = uptime / 86400;
1225 hrs = uptime / 3600;
1231 * Display the uptime.
1236 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1242 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);