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;
105 /* we don't want more than MAX_COLS columns, since the machine-dependent
106 modules make static allocations based on MAX_COLS and we don't want
107 to run off the end of their buffers */
108 display_width = screen_width;
109 if (display_width >= MAX_COLS)
111 display_width = MAX_COLS - 1;
114 /* now, allocate space for the screen buffer */
115 screenbuf = (char *)malloc(lines * display_width);
116 if (screenbuf == (char *)NULL)
122 /* return number of lines available */
123 /* for dumb terminals, pretend like we can show any amount */
124 return(smart_terminal ? lines : Largest);
127 int display_init(statics)
129 struct statics *statics;
137 /* call resize to do the dirty work */
138 lines = display_resize();
140 /* only do the rest if we need to */
143 /* save pointers and allocate space for names */
144 procstate_names = statics->procstate_names;
145 num_procstates = string_count(procstate_names);
146 lprocstates = (int *)malloc(num_procstates * sizeof(int));
148 cpustate_names = statics->cpustate_names;
150 swap_names = statics->swap_names;
151 num_swap = string_count(swap_names);
152 lswap = (int *)malloc(num_swap * sizeof(int));
153 num_cpustates = string_count(cpustate_names);
154 lcpustates = (int *)malloc(num_cpustates * sizeof(int));
155 cpustate_columns = (int *)malloc(num_cpustates * sizeof(int));
157 memory_names = statics->memory_names;
158 num_memory = string_count(memory_names);
159 lmemory = (int *)malloc(num_memory * sizeof(int));
161 /* calculate starting columns where needed */
162 cpustate_total_length = 0;
164 ip = cpustate_columns;
167 if ((i = strlen(*pp++)) > 0)
169 *ip++ = cpustate_total_length;
170 cpustate_total_length += i + 8;
175 /* return number of lines available */
179 i_loadave(mpid, avenrun)
187 /* i_loadave also clears the screen, since it is first */
190 /* mpid == -1 implies this system doesn't have an _mpid */
193 printf("last pid: %5d; ", mpid);
196 printf("load averages");
198 for (i = 0; i < 3; i++)
207 u_loadave(mpid, avenrun)
217 /* change screen only when value has really changed */
220 Move_to(x_lastpid, y_lastpid);
225 /* i remembers x coordinate to move to */
230 i = x_loadave_nompid;
233 /* move into position for load averages */
234 Move_to(i, y_loadave);
236 /* display new load averages */
237 /* we should optimize this and only display changes */
238 for (i = 0; i < 3; i++)
252 * Display the current time.
253 * "ctime" always returns a string that looks like this:
255 * Sun Sep 16 01:03:52 1973
256 * 012345678901234567890123
259 * We want indices 11 thru 18 (length 8).
264 Move_to(screen_width - 8, 0);
277 printf("%-8.8s\n", &(ctime(tod)[11]));
281 static int ltotal = 0;
282 static char procstates_buffer[128];
285 * *_procstates(total, brkdn, names) - print the process summary line
287 * Assumptions: cursor is at the beginning of the line on entry
291 i_procstates(total, brkdn)
299 /* write current number of processes and remember the value */
300 printf("%d processes:", total);
303 /* put out enough spaces to get to column 15 */
310 /* format and print the process state summary */
311 summary_format(procstates_buffer, brkdn, procstate_names);
312 fputs(procstates_buffer, stdout);
314 /* save the numbers for next time */
315 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
318 u_procstates(total, brkdn)
324 static char new[128];
327 /* update number of processes only if it has changed */
330 /* move and overwrite */
331 #if (x_procstate == 0)
332 Move_to(x_procstate, y_procstate);
334 /* cursor is already there...no motion needed */
335 /* assert(lastline == 1); */
339 /* if number of digits differs, rewrite the label */
340 if (digits(total) != digits(ltotal))
342 fputs(" processes:", stdout);
343 /* put out enough spaces to get to column 15 */
349 /* cursor may end up right where we want it!!! */
356 /* see if any of the state numbers has changed */
357 if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
359 /* format and update the line */
360 summary_format(new, brkdn, procstate_names);
361 line_update(procstates_buffer, new, x_brkdn, y_brkdn);
362 memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
367 * *_cpustates(states, names) - print the cpu state percentages
369 * Assumptions: cursor is on the PREVIOUS line
372 static int cpustates_column;
374 /* cpustates_tag() calculates the correct tag to use to label the line */
376 char *cpustates_tag()
381 static char *short_tag = "CPU: ";
382 static char *long_tag = "CPU states: ";
384 /* if length + strlen(long_tag) >= screen_width, then we have to
385 use the shorter tag (we subtract 2 to account for ": ") */
386 if (cpustate_total_length + (int)strlen(long_tag) - 2 >= screen_width)
395 /* set cpustates_column accordingly then return result */
396 cpustates_column = strlen(use);
402 register int *states;
407 register char **names = cpustate_names;
408 register char *thisname;
410 /* print tag and bump lastline */
411 printf("\n%s", cpustates_tag());
414 /* now walk thru the names and print the line */
415 while ((thisname = *names++) != NULL)
417 if (*thisname != '\0')
419 /* retrieve the value and remember it */
422 /* if percentage is >= 1000, print it as 100% */
423 printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
424 i++ == 0 ? "" : ", ",
430 /* copy over values into "last" array */
431 memcpy(lcpustates, states, num_cpustates * sizeof(int));
436 register int *states;
440 register char **names = cpustate_names;
441 register char *thisname;
445 Move_to(cpustates_column, y_cpustates);
446 lastline = y_cpustates;
448 colp = cpustate_columns;
450 /* we could be much more optimal about this */
451 while ((thisname = *names++) != NULL)
453 if (*thisname != '\0')
455 /* did the value change since last time? */
458 /* yes, move and change */
459 Move_to(cpustates_column + *colp, y_cpustates);
460 lastline = y_cpustates;
462 /* retrieve value and remember it */
465 /* if percentage is >= 1000, print it as 100% */
466 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
467 ((double)value)/10.);
469 /* remember it for next time */
474 /* increment and move on */
485 register char **names = cpustate_names;
486 register char *thisname;
489 /* show tag and bump lastline */
490 printf("\n%s", cpustates_tag());
493 while ((thisname = *names++) != NULL)
495 if (*thisname != '\0')
497 printf("%s %% %s", i++ == 0 ? "" : ", ", thisname);
501 /* fill the "last" array with all -1s, to insure correct updating */
511 * *_memory(stats) - print "Memory: " followed by the memory summary string
513 * Assumptions: cursor is on "lastline"
514 * for i_memory ONLY: cursor is on the previous line
517 char memory_buffer[MAX_COLS];
524 fputs("\nMem: ", stdout);
527 /* format and print the memory summary */
528 summary_format(memory_buffer, stats, memory_names);
529 fputs(memory_buffer, stdout);
537 static char new[MAX_COLS];
539 /* format the new line */
540 summary_format(new, stats, memory_names);
541 line_update(memory_buffer, new, x_mem, y_mem);
545 * *_swap(stats) - print "Swap: " followed by the swap summary string
547 * Assumptions: cursor is on "lastline"
548 * for i_swap ONLY: cursor is on the previous line
551 char swap_buffer[MAX_COLS];
558 fputs("\nSwap: ", stdout);
561 /* format and print the swap summary */
562 summary_format(swap_buffer, stats, swap_names);
563 fputs(swap_buffer, stdout);
571 static char new[MAX_COLS];
573 /* format the new line */
574 summary_format(new, stats, swap_names);
575 line_update(swap_buffer, new, x_swap, y_swap);
579 * *_message() - print the next pending message line, or erase the one
582 * Note that u_message is (currently) the same as i_message.
584 * Assumptions: lastline is consistent
588 * i_message is funny because it gets its message asynchronously (with
589 * respect to screen updates).
592 static char next_msg[MAX_COLS + 5];
593 static int msglen = 0;
594 /* Invariant: msglen is always the length of the message currently displayed
595 on the screen (even when next_msg doesn't contain that message). */
600 while (lastline < y_message)
605 if (next_msg[0] != '\0')
608 msglen = strlen(next_msg);
613 (void) clear_eol(msglen);
624 static int header_length;
627 * *_header(text) - print the header for the process area
629 * Assumptions: cursor is on the previous line and lastline is consistent
637 header_length = strlen(text);
638 if (header_status == ON)
644 else if (header_status == ERASE)
653 char *text; /* ignored */
656 if (header_status == ERASE)
660 clear_eol(header_length);
666 * *_process(line, thisline) - print one process line
668 * Assumptions: lastline is consistent
671 i_process(line, thisline)
680 /* make sure we are on the correct line */
681 while (lastline < y_procs + line)
687 /* truncate the line to conform to our current screen width */
688 thisline[display_width] = '\0';
690 /* write the line out */
691 fputs(thisline, stdout);
693 /* copy it in to our buffer */
694 base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
695 p = strecpy(base, thisline);
697 /* zero fill the rest of it */
698 memzero(p, display_width - (p - base));
701 u_process(line, newline)
708 register int screen_line = line + Header_lines;
709 register char *bufferline;
711 /* remember a pointer to the current line in the screen buffer */
712 bufferline = &screenbuf[lineindex(line)];
714 /* truncate the line to conform to our current screen width */
715 newline[display_width] = '\0';
717 /* is line higher than we went on the last display? */
720 /* yes, just ignore screenbuf and write it out directly */
721 /* get positioned on the correct line */
722 if (screen_line - lastline == 1)
729 Move_to(0, screen_line);
730 lastline = screen_line;
733 /* now write the line */
734 fputs(newline, stdout);
736 /* copy it in to the buffer */
737 optr = strecpy(bufferline, newline);
739 /* zero fill the rest of it */
740 memzero(optr, display_width - (optr - bufferline));
744 line_update(bufferline, newline, 0, line + Header_lines);
753 register int screen_line = hi + Header_lines;
760 /* need to blank the remainder of the screen */
761 /* but only if there is any screen left below this line */
762 if (lastline + 1 < screen_length)
764 /* efficiently move to the end of currently displayed info */
765 if (screen_line - lastline < 5)
767 while (lastline < screen_line)
775 Move_to(0, screen_line);
776 lastline = screen_line;
781 /* we can do this the easy way */
782 putcap(clear_to_end);
786 /* use clear_eol on each line */
788 while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
797 /* move the cursor to a pleasant place */
798 Move_to(x_idlecursor, y_idlecursor);
799 lastline = y_idlecursor;
803 /* separate this display from the next with some vertical room */
804 fputs("\n\n", stdout);
817 else if (header_status == ON)
819 header_status = ERASE;
824 new_message(type, msgfmt, a1, a2, a3)
833 /* first, format the message */
834 (void) snprintf(next_msg, sizeof(next_msg), msgfmt, a1, a2, a3);
838 /* message there already -- can we clear it? */
841 /* yes -- write it and clear to end */
842 i = strlen(next_msg);
843 if ((type & MT_delayed) == 0)
845 type & MT_standout ? standout(next_msg) :
846 fputs(next_msg, stdout);
847 (void) clear_eol(msglen - i);
855 if ((type & MT_delayed) == 0)
857 type & MT_standout ? standout(next_msg) : fputs(next_msg, stdout);
858 msglen = strlen(next_msg);
867 if (clear_eol(msglen) == 1)
873 readline(buffer, size, numeric)
880 register char *ptr = buffer;
882 register char cnt = 0;
883 register char maxcnt = 0;
885 /* allow room for null terminator */
889 while ((fflush(stdout), read(0, ptr, 1) > 0))
891 /* newline means we are done */
892 if ((ch = *ptr) == '\n' || ch == '\r')
897 /* handle special editing characters */
900 /* kill line -- account for overstriking */
906 /* return null string */
911 else if (ch == ch_erase)
913 /* erase previous character */
921 fputs("\b \b", stdout);
926 /* check for character validity and buffer overflow */
927 else if (cnt == size || (numeric && !isdigit(ch)) ||
935 /* echo it and store it in the buffer */
946 /* all done -- null terminate the string */
949 /* account for the extra characters in the message area */
950 /* (if terminal overstrikes, remember the furthest they went) */
951 msglen += overstrike ? maxcnt : cnt;
953 /* return either inputted number or string length */
955 return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
958 /* internal support routines */
960 static int string_count(pp)
968 while (*pp++ != NULL)
975 static void summary_format(str, numbers, names)
979 register char **names;
984 register char *thisname;
985 register int useM = No;
987 /* format each number followed by its string */
989 while ((thisname = *names++) != NULL)
991 /* get the number to format */
994 /* display only non-zero numbers */
997 /* is this number in kilobytes? */
998 if (thisname[0] == 'K')
1000 /* yes: format it as a memory value */
1001 p = strecpy(p, format_k(num));
1003 /* skip over the K, since it was included by format_k */
1004 p = strecpy(p, thisname+1);
1008 p = strecpy(p, itoa(num));
1009 p = strecpy(p, thisname);
1013 /* ignore negative numbers, but display corresponding string */
1016 p = strecpy(p, thisname);
1020 /* if the last two characters in the string are ", ", delete them */
1022 if (p >= str && p[0] == ',' && p[1] == ' ')
1028 static void line_update(old, new, start, line)
1038 register int newcol = start + 1;
1039 register int lastcol = start;
1040 char cursor_on_line = No;
1043 /* compare the two strings and only rewrite what has changed */
1046 fprintf(debug, "line_update, starting at %d\n", start);
1050 fputs("\n-\n", debug);
1053 /* start things off on the right foot */
1054 /* this is to make sure the invariants get set up right */
1055 if ((ch = *new++) != *old)
1057 if (line - lastline == 1 && start == 0)
1063 Move_to(start, line);
1065 cursor_on_line = Yes;
1073 * main loop -- check each character. If the old and new aren't the
1074 * same, then update the display. When the distance from the
1075 * current cursor position to the new change is small enough,
1076 * the characters that belong there are written to move the
1080 * lastcol is the column where the cursor currently is sitting
1081 * (always one beyond the end of the last mismatch).
1083 do /* yes, a do...while */
1085 if ((ch = *new++) != *old)
1087 /* new character is different from old */
1088 /* make sure the cursor is on top of this character */
1089 diff = newcol - lastcol;
1092 /* some motion is required--figure out which is shorter */
1093 if (diff < 6 && cursor_on_line)
1095 /* overwrite old stuff--get it out of the old buffer */
1096 printf("%.*s", diff, ¤t[lastcol-start]);
1100 /* use cursor addressing */
1101 Move_to(newcol, line);
1102 cursor_on_line = Yes;
1104 /* remember where the cursor is */
1105 lastcol = newcol + 1;
1109 /* already there, update position */
1113 /* write what we need to */
1116 /* at the end--terminate with a clear-to-end-of-line */
1117 (void) clear_eol(strlen(old));
1121 /* write the new character */
1124 /* put the new character in the screen buffer */
1128 /* update working column and screen buffer pointer */
1132 } while (ch != '\0');
1134 /* zero out the rest of the line buffer -- MUST BE DONE! */
1135 diff = display_width - newcol;
1141 /* remember where the current line is */
1149 * printable(str) - make the string pointed to by "str" into one that is
1150 * printable (i.e.: all ascii), by converting all non-printable
1151 * characters into '?'. Replacements are done in place and a pointer
1152 * to the original buffer is returned.
1155 char *printable(str)
1164 while ((ch = *ptr) != '\0')
1182 int days, hrs, mins, secs;
1184 if (bt->tv_sec != -1) {
1185 uptime = *tod - bt->tv_sec;
1187 days = uptime / 86400;
1189 hrs = uptime / 3600;
1195 * Display the uptime.
1200 Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1206 printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);