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
11 * $FreeBSD: src/contrib/top/display.c,v 1.4.6.3 2003/04/27 15:32:26 dwmalone Exp $
12 * $DragonFly: src/contrib/top/display.c,v 1.2 2003/06/17 04:24:07 dillon Exp $
16 * This file contains the routines that display information on the screen.
17 * Each section of the screen has two routines: one for initially writing
18 * all constant and dynamic text, and one for only updating the text that
19 * changes. The prefix "i_" is used on all the "initial" routines and the
20 * prefix "u_" is used for all the "updating" routines.
23 * None of the "i_" routines use any of the termcap capabilities.
24 * In this way, those routines can be safely used on terminals that
25 * have minimal (or nonexistant) terminal capabilities.
27 * The routines are called in this order: *_loadave, i_timeofday,
28 * *_procstates, *_cpustates, *_memory, *_message, *_header,
29 * *_process, u_endscreen.
37 #include "screen.h" /* interface to screen package */
38 #include "layout.h" /* defines for screen position layout */
41 #include "top.local.h"
43 #include "machine.h" /* we should eliminate this!!! */
50 /* imported from screen.c */
51 extern int overstrike
;
54 static int last_hi
= 0; /* used in u_process and u_endscreen */
55 static int lastline
= 0;
56 static int display_width
= MAX_COLS
;
58 #define lineindex(l) ((l)*display_width)
62 /* things initialized by display_init and used thruout */
64 /* buffer of proc information lines for display updating */
65 char *screenbuf
= NULL
;
67 static char **procstate_names
;
68 static char **cpustate_names
;
69 static char **memory_names
;
70 static char **swap_names
;
72 static int num_procstates
;
73 static int num_cpustates
;
74 static int num_memory
;
77 static int *lprocstates
;
78 static int *lcpustates
;
82 static int *cpustate_columns
;
83 static int cpustate_total_length
;
85 static enum { OFF
, ON
, ERASE
} header_status
= ON
;
87 static int string_count();
88 static void summary_format();
89 static void line_update();
96 /* first, deallocate any previous buffer that may have been there */
97 if (screenbuf
!= NULL
)
102 /* calculate the current dimensions */
103 /* if operating in "dumb" mode, we only need one line */
104 lines
= smart_terminal
? screen_length
- Header_lines
: 1;
108 /* we don't want more than MAX_COLS columns, since the machine-dependent
109 modules make static allocations based on MAX_COLS and we don't want
110 to run off the end of their buffers */
111 display_width
= screen_width
;
112 if (display_width
>= MAX_COLS
)
114 display_width
= MAX_COLS
- 1;
117 /* now, allocate space for the screen buffer */
118 screenbuf
= (char *)malloc(lines
* display_width
);
119 if (screenbuf
== (char *)NULL
)
125 /* return number of lines available */
126 /* for dumb terminals, pretend like we can show any amount */
127 return(smart_terminal
? lines
: Largest
);
130 int display_init(statics
)
132 struct statics
*statics
;
140 /* call resize to do the dirty work */
141 lines
= display_resize();
143 /* only do the rest if we need to */
146 /* save pointers and allocate space for names */
147 procstate_names
= statics
->procstate_names
;
148 num_procstates
= string_count(procstate_names
);
149 lprocstates
= (int *)malloc(num_procstates
* sizeof(int));
151 cpustate_names
= statics
->cpustate_names
;
153 swap_names
= statics
->swap_names
;
154 num_swap
= string_count(swap_names
);
155 lswap
= (int *)malloc(num_swap
* sizeof(int));
156 num_cpustates
= string_count(cpustate_names
);
157 lcpustates
= (int *)malloc(num_cpustates
* sizeof(int));
158 cpustate_columns
= (int *)malloc(num_cpustates
* sizeof(int));
160 memory_names
= statics
->memory_names
;
161 num_memory
= string_count(memory_names
);
162 lmemory
= (int *)malloc(num_memory
* sizeof(int));
164 /* calculate starting columns where needed */
165 cpustate_total_length
= 0;
167 ip
= cpustate_columns
;
170 *ip
++ = cpustate_total_length
;
171 if ((i
= strlen(*pp
++)) > 0)
173 cpustate_total_length
+= i
+ 8;
178 /* return number of lines available */
182 i_loadave(mpid
, avenrun
)
190 /* i_loadave also clears the screen, since it is first */
193 /* mpid == -1 implies this system doesn't have an _mpid */
196 printf("last pid: %5d; ", mpid
);
199 printf("load averages");
201 for (i
= 0; i
< 3; i
++)
210 u_loadave(mpid
, avenrun
)
220 /* change screen only when value has really changed */
223 Move_to(x_lastpid
, y_lastpid
);
228 /* i remembers x coordinate to move to */
233 i
= x_loadave_nompid
;
236 /* move into position for load averages */
237 Move_to(i
, y_loadave
);
239 /* display new load averages */
240 /* we should optimize this and only display changes */
241 for (i
= 0; i
< 3; i
++)
255 * Display the current time.
256 * "ctime" always returns a string that looks like this:
258 * Sun Sep 16 01:03:52 1973
259 * 012345678901234567890123
262 * We want indices 11 thru 18 (length 8).
267 Move_to(screen_width
- 8, 0);
280 printf("%-8.8s\n", &(ctime(tod
)[11]));
284 static int ltotal
= 0;
285 static char procstates_buffer
[MAX_COLS
];
288 * *_procstates(total, brkdn, names) - print the process summary line
290 * Assumptions: cursor is at the beginning of the line on entry
294 i_procstates(total
, brkdn
)
302 /* write current number of processes and remember the value */
303 printf("%d processes:", total
);
306 /* put out enough spaces to get to column 15 */
313 /* format and print the process state summary */
314 summary_format(procstates_buffer
, brkdn
, procstate_names
);
315 fputs(procstates_buffer
, stdout
);
317 /* save the numbers for next time */
318 memcpy(lprocstates
, brkdn
, num_procstates
* sizeof(int));
321 u_procstates(total
, brkdn
)
327 static char new[MAX_COLS
];
330 /* update number of processes only if it has changed */
333 /* move and overwrite */
334 #if (x_procstate == 0)
335 Move_to(x_procstate
, y_procstate
);
337 /* cursor is already there...no motion needed */
338 /* assert(lastline == 1); */
342 /* if number of digits differs, rewrite the label */
343 if (digits(total
) != digits(ltotal
))
345 fputs(" processes:", stdout
);
346 /* put out enough spaces to get to column 15 */
352 /* cursor may end up right where we want it!!! */
359 /* see if any of the state numbers has changed */
360 if (memcmp(lprocstates
, brkdn
, num_procstates
* sizeof(int)) != 0)
362 /* format and update the line */
363 summary_format(new, brkdn
, procstate_names
);
364 line_update(procstates_buffer
, new, x_brkdn
, y_brkdn
);
365 memcpy(lprocstates
, brkdn
, num_procstates
* sizeof(int));
370 * *_cpustates(states, names) - print the cpu state percentages
372 * Assumptions: cursor is on the PREVIOUS line
375 static int cpustates_column
;
377 /* cpustates_tag() calculates the correct tag to use to label the line */
379 char *cpustates_tag()
384 static char *short_tag
= "CPU: ";
385 static char *long_tag
= "CPU states: ";
387 /* if length + strlen(long_tag) >= screen_width, then we have to
388 use the shorter tag (we subtract 2 to account for ": ") */
389 if (cpustate_total_length
+ (int)strlen(long_tag
) - 2 >= screen_width
)
398 /* set cpustates_column accordingly then return result */
399 cpustates_column
= strlen(use
);
405 register int *states
;
410 register char **names
= cpustate_names
;
411 register char *thisname
;
413 /* print tag and bump lastline */
414 printf("\n%s", cpustates_tag());
417 /* now walk thru the names and print the line */
418 while ((thisname
= *names
++) != NULL
)
420 if (*thisname
!= '\0')
422 /* retrieve the value and remember it */
425 /* if percentage is >= 1000, print it as 100% */
426 printf((value
>= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
427 i
++ == 0 ? "" : ", ",
433 /* copy over values into "last" array */
434 memcpy(lcpustates
, states
, num_cpustates
* sizeof(int));
439 register int *states
;
443 register char **names
= cpustate_names
;
444 register char *thisname
;
448 Move_to(cpustates_column
, y_cpustates
);
449 lastline
= y_cpustates
;
451 colp
= cpustate_columns
;
453 /* we could be much more optimal about this */
454 while ((thisname
= *names
++) != NULL
)
456 if (*thisname
!= '\0')
458 /* did the value change since last time? */
461 /* yes, move and change */
462 Move_to(cpustates_column
+ *colp
, y_cpustates
);
463 lastline
= y_cpustates
;
465 /* retrieve value and remember it */
468 /* if percentage is >= 1000, print it as 100% */
469 printf((value
>= 1000 ? "%4.0f" : "%4.1f"),
470 ((double)value
)/10.);
472 /* remember it for next time */
477 /* increment and move on */
488 register char **names
= cpustate_names
;
489 register char *thisname
;
492 /* show tag and bump lastline */
493 printf("\n%s", cpustates_tag());
496 while ((thisname
= *names
++) != NULL
)
498 if (*thisname
!= '\0')
500 printf("%s %% %s", i
++ == 0 ? "" : ", ", thisname
);
504 /* fill the "last" array with all -1s, to insure correct updating */
514 * *_memory(stats) - print "Memory: " followed by the memory summary string
516 * Assumptions: cursor is on "lastline"
517 * for i_memory ONLY: cursor is on the previous line
520 char memory_buffer
[MAX_COLS
];
527 fputs("\nMem: ", stdout
);
530 /* format and print the memory summary */
531 summary_format(memory_buffer
, stats
, memory_names
);
532 fputs(memory_buffer
, stdout
);
540 static char new[MAX_COLS
];
542 /* format the new line */
543 summary_format(new, stats
, memory_names
);
544 line_update(memory_buffer
, new, x_mem
, y_mem
);
548 * *_swap(stats) - print "Swap: " followed by the swap summary string
550 * Assumptions: cursor is on "lastline"
551 * for i_swap ONLY: cursor is on the previous line
554 char swap_buffer
[MAX_COLS
];
561 fputs("\nSwap: ", stdout
);
564 /* format and print the swap summary */
565 summary_format(swap_buffer
, stats
, swap_names
);
566 fputs(swap_buffer
, stdout
);
574 static char new[MAX_COLS
];
576 /* format the new line */
577 summary_format(new, stats
, swap_names
);
578 line_update(swap_buffer
, new, x_swap
, y_swap
);
582 * *_message() - print the next pending message line, or erase the one
585 * Note that u_message is (currently) the same as i_message.
587 * Assumptions: lastline is consistent
591 * i_message is funny because it gets its message asynchronously (with
592 * respect to screen updates).
595 static char next_msg
[MAX_COLS
+ 5];
596 static int msglen
= 0;
597 /* Invariant: msglen is always the length of the message currently displayed
598 on the screen (even when next_msg doesn't contain that message). */
603 while (lastline
< y_message
)
608 if (next_msg
[0] != '\0')
611 msglen
= strlen(next_msg
);
616 (void) clear_eol(msglen
);
627 static int header_length
;
630 * *_header(text) - print the header for the process area
632 * Assumptions: cursor is on the previous line and lastline is consistent
640 header_length
= strlen(text
);
641 if (header_status
== ON
)
647 else if (header_status
== ERASE
)
656 char *text
; /* ignored */
659 if (header_status
== ERASE
)
663 clear_eol(header_length
);
669 * *_process(line, thisline) - print one process line
671 * Assumptions: lastline is consistent
674 i_process(line
, thisline
)
683 /* make sure we are on the correct line */
684 while (lastline
< y_procs
+ line
)
690 /* truncate the line to conform to our current screen width */
691 thisline
[display_width
] = '\0';
693 /* write the line out */
694 fputs(thisline
, stdout
);
696 /* copy it in to our buffer */
697 base
= smart_terminal
? screenbuf
+ lineindex(line
) : screenbuf
;
698 p
= strecpy(base
, thisline
);
700 /* zero fill the rest of it */
701 memzero(p
, display_width
- (p
- base
));
704 u_process(line
, newline
)
711 register int screen_line
= line
+ Header_lines
;
712 register char *bufferline
;
714 /* remember a pointer to the current line in the screen buffer */
715 bufferline
= &screenbuf
[lineindex(line
)];
717 /* truncate the line to conform to our current screen width */
718 newline
[display_width
] = '\0';
720 /* is line higher than we went on the last display? */
723 /* yes, just ignore screenbuf and write it out directly */
724 /* get positioned on the correct line */
725 if (screen_line
- lastline
== 1)
732 Move_to(0, screen_line
);
733 lastline
= screen_line
;
736 /* now write the line */
737 fputs(newline
, stdout
);
739 /* copy it in to the buffer */
740 optr
= strecpy(bufferline
, newline
);
742 /* zero fill the rest of it */
743 memzero(optr
, display_width
- (optr
- bufferline
));
747 line_update(bufferline
, newline
, 0, line
+ Header_lines
);
756 register int screen_line
= hi
+ Header_lines
;
763 /* need to blank the remainder of the screen */
764 /* but only if there is any screen left below this line */
765 if (lastline
+ 1 < screen_length
)
767 /* efficiently move to the end of currently displayed info */
768 if (screen_line
- lastline
< 5)
770 while (lastline
< screen_line
)
778 Move_to(0, screen_line
);
779 lastline
= screen_line
;
784 /* we can do this the easy way */
785 putcap(clear_to_end
);
789 /* use clear_eol on each line */
791 while ((void) clear_eol(strlen(&screenbuf
[lineindex(i
++)])), i
< last_hi
)
800 /* move the cursor to a pleasant place */
801 Move_to(x_idlecursor
, y_idlecursor
);
802 lastline
= y_idlecursor
;
806 /* separate this display from the next with some vertical room */
807 fputs("\n\n", stdout
);
820 else if (header_status
== ON
)
822 header_status
= ERASE
;
827 new_message(type
, msgfmt
, a1
, a2
, a3
)
836 /* first, format the message */
837 (void) snprintf(next_msg
, sizeof(next_msg
), msgfmt
, a1
, a2
, a3
);
841 /* message there already -- can we clear it? */
844 /* yes -- write it and clear to end */
845 i
= strlen(next_msg
);
846 if ((type
& MT_delayed
) == 0)
848 type
& MT_standout
? standout(next_msg
) :
849 fputs(next_msg
, stdout
);
850 (void) clear_eol(msglen
- i
);
858 if ((type
& MT_delayed
) == 0)
860 type
& MT_standout
? standout(next_msg
) : fputs(next_msg
, stdout
);
861 msglen
= strlen(next_msg
);
870 if (clear_eol(msglen
) == 1)
876 readline(buffer
, size
, numeric
)
883 register char *ptr
= buffer
;
885 register char cnt
= 0;
886 register char maxcnt
= 0;
888 /* allow room for null terminator */
892 while ((fflush(stdout
), read(0, ptr
, 1) > 0))
894 /* newline means we are done */
895 if ((ch
= *ptr
) == '\n' || ch
== '\r')
900 /* handle special editing characters */
903 /* kill line -- account for overstriking */
909 /* return null string */
914 else if (ch
== ch_erase
)
916 /* erase previous character */
924 fputs("\b \b", stdout
);
929 /* check for character validity and buffer overflow */
930 else if (cnt
== size
|| (numeric
&& !isdigit(ch
)) ||
938 /* echo it and store it in the buffer */
949 /* all done -- null terminate the string */
952 /* account for the extra characters in the message area */
953 /* (if terminal overstrikes, remember the furthest they went) */
954 msglen
+= overstrike
? maxcnt
: cnt
;
956 /* return either inputted number or string length */
958 return(cnt
== 0 ? -1 : numeric
? atoi(buffer
) : cnt
);
961 /* internal support routines */
963 static int string_count(pp
)
971 while (*pp
++ != NULL
)
978 static void summary_format(str
, numbers
, names
)
982 register char **names
;
987 register char *thisname
;
988 register int useM
= No
;
990 /* format each number followed by its string */
992 while ((thisname
= *names
++) != NULL
)
994 /* get the number to format */
997 /* display only non-zero numbers */
1000 /* is this number in kilobytes? */
1001 if (thisname
[0] == 'K')
1003 /* yes: format it as a memory value */
1004 p
= strecpy(p
, format_k(num
));
1006 /* skip over the K, since it was included by format_k */
1007 p
= strecpy(p
, thisname
+1);
1011 p
= strecpy(p
, itoa(num
));
1012 p
= strecpy(p
, thisname
);
1016 /* ignore negative numbers, but display corresponding string */
1019 p
= strecpy(p
, thisname
);
1023 /* if the last two characters in the string are ", ", delete them */
1025 if (p
>= str
&& p
[0] == ',' && p
[1] == ' ')
1031 static void line_update(old
, new, start
, line
)
1041 register int newcol
= start
+ 1;
1042 register int lastcol
= start
;
1043 char cursor_on_line
= No
;
1046 /* compare the two strings and only rewrite what has changed */
1049 fprintf(debug
, "line_update, starting at %d\n", start
);
1053 fputs("\n-\n", debug
);
1056 /* start things off on the right foot */
1057 /* this is to make sure the invariants get set up right */
1058 if ((ch
= *new++) != *old
)
1060 if (line
- lastline
== 1 && start
== 0)
1066 Move_to(start
, line
);
1068 cursor_on_line
= Yes
;
1076 * main loop -- check each character. If the old and new aren't the
1077 * same, then update the display. When the distance from the
1078 * current cursor position to the new change is small enough,
1079 * the characters that belong there are written to move the
1083 * lastcol is the column where the cursor currently is sitting
1084 * (always one beyond the end of the last mismatch).
1086 do /* yes, a do...while */
1088 if ((ch
= *new++) != *old
)
1090 /* new character is different from old */
1091 /* make sure the cursor is on top of this character */
1092 diff
= newcol
- lastcol
;
1095 /* some motion is required--figure out which is shorter */
1096 if (diff
< 6 && cursor_on_line
)
1098 /* overwrite old stuff--get it out of the old buffer */
1099 printf("%.*s", diff
, ¤t
[lastcol
-start
]);
1103 /* use cursor addressing */
1104 Move_to(newcol
, line
);
1105 cursor_on_line
= Yes
;
1107 /* remember where the cursor is */
1108 lastcol
= newcol
+ 1;
1112 /* already there, update position */
1116 /* write what we need to */
1119 /* at the end--terminate with a clear-to-end-of-line */
1120 (void) clear_eol(strlen(old
));
1124 /* write the new character */
1127 /* put the new character in the screen buffer */
1131 /* update working column and screen buffer pointer */
1135 } while (ch
!= '\0');
1137 /* zero out the rest of the line buffer -- MUST BE DONE! */
1138 diff
= display_width
- newcol
;
1144 /* remember where the current line is */
1152 * printable(str) - make the string pointed to by "str" into one that is
1153 * printable (i.e.: all ascii), by converting all non-printable
1154 * characters into '?'. Replacements are done in place and a pointer
1155 * to the original buffer is returned.
1158 char *printable(str
)
1167 while ((ch
= *ptr
) != '\0')
1185 int days
, hrs
, mins
, secs
;
1187 if (bt
->tv_sec
!= -1) {
1188 uptime
= *tod
- bt
->tv_sec
;
1190 days
= uptime
/ 86400;
1192 hrs
= uptime
/ 3600;
1198 * Display the uptime.
1203 Move_to((screen_width
- 24) - (days
> 9 ? 1 : 0), 0);
1209 printf(" up %d+%02d:%02d:%02d", days
, hrs
, mins
, secs
);