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.3 2006/10/03 12:20:11 y0netan1 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.
32 #include <sys/types.h>
41 #include "screen.h" /* interface to screen package */
42 #include "layout.h" /* defines for screen position layout */
45 #include "top.local.h"
47 #include "machine.h" /* we should eliminate this!!! */
54 /* imported from screen.c */
55 extern int overstrike
;
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
;
62 #define lineindex(l) ((l)*display_width)
64 char *printable(char *str
);
66 /* things initialized by display_init and used thruout */
68 /* buffer of proc information lines for display updating */
69 char *screenbuf
= NULL
;
71 static const char **procstate_names
;
72 static const char **cpustate_names
;
73 static const char **memory_names
;
74 static const char **swap_names
;
76 static int num_procstates
;
77 static int num_cpustates
;
78 static int num_memory
;
81 static int *lprocstates
;
85 static int *cpustate_columns
;
86 static int cpustate_total_length
;
88 static enum { OFF
, ON
, ERASE
} header_status
= ON
;
90 static int string_count(const char **);
91 static void summary_format(char *str
, int *numbers
, const char **names
);
92 static void line_update(char *old
, char *new, int start
, int line
);
99 /* first, deallocate any previous buffer that may have been there */
100 if (screenbuf
!= NULL
)
105 /* calculate the current dimensions */
106 /* if operating in "dumb" mode, we only need one line */
107 xlines
= smart_terminal
? screen_length
- Header_lines
: 1;
111 /* we don't want more than MAX_COLS columns, since the machine-dependent
112 modules make static allocations based on MAX_COLS and we don't want
113 to run off the end of their buffers */
114 display_width
= screen_width
;
115 if (display_width
>= MAX_COLS
)
117 display_width
= MAX_COLS
- 1;
120 /* now, allocate space for the screen buffer */
121 screenbuf
= (char *)malloc(xlines
* display_width
);
122 if (screenbuf
== (char *)NULL
)
128 /* return number of lines available */
129 /* for dumb terminals, pretend like we can show any amount */
130 return(smart_terminal
? xlines
: Largest
);
134 display_init(struct statics
*statics
)
141 /* call resize to do the dirty work */
142 xlines
= display_resize();
144 /* only do the rest if we need to */
147 /* save pointers and allocate space for names */
148 procstate_names
= statics
->procstate_names
;
149 num_procstates
= string_count(procstate_names
);
150 lprocstates
= (int *)malloc(num_procstates
* sizeof(int));
152 cpustate_names
= statics
->cpustate_names
;
154 swap_names
= statics
->swap_names
;
155 num_swap
= string_count(swap_names
);
156 lswap
= (int *)malloc(num_swap
* sizeof(int));
157 num_cpustates
= string_count(cpustate_names
);
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 */
183 i_loadave(int mpid
, double *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
++)
208 u_loadave(int mpid
, double *avenrun
)
214 /* change screen only when value has really changed */
217 Move_to(x_lastpid
, y_lastpid
);
222 /* i remembers x coordinate to move to */
227 i
= x_loadave_nompid
;
230 /* move into position for load averages */
231 Move_to(i
, y_loadave
);
233 /* display new load averages */
234 /* we should optimize this and only display changes */
235 for (i
= 0; i
< 3; i
++)
244 i_timeofday(time_t *tod
)
247 * Display the current time.
248 * "ctime" always returns a string that looks like this:
250 * Sun Sep 16 01:03:52 1973
251 * 012345678901234567890123
254 * We want indices 11 thru 18 (length 8).
259 Move_to(screen_width
- 8, 0);
272 printf("%-8.8s\n", &(ctime(tod
)[11]));
276 static int ltotal
= 0;
277 static char procstates_buffer
[MAX_COLS
];
280 * *_procstates(total, brkdn, names) - print the process summary line
282 * Assumptions: cursor is at the beginning of the line on entry
287 i_procstates(int total
, int *brkdn
)
291 /* write current number of processes and remember the value */
292 printf("%d processes:", total
);
295 /* put out enough spaces to get to column 15 */
302 /* format and print the process state summary */
303 summary_format(procstates_buffer
, brkdn
, procstate_names
);
304 fputs(procstates_buffer
, stdout
);
306 /* save the numbers for next time */
307 memcpy(lprocstates
, brkdn
, num_procstates
* sizeof(int));
311 u_procstates(int total
, int *brkdn
)
313 static char new[MAX_COLS
];
316 /* update number of processes only if it has changed */
319 /* move and overwrite */
320 #if (x_procstate == 0)
321 Move_to(x_procstate
, y_procstate
);
323 /* cursor is already there...no motion needed */
324 /* assert(lastline == 1); */
328 /* if number of digits differs, rewrite the label */
329 if (digits(total
) != digits(ltotal
))
331 fputs(" processes:", stdout
);
332 /* put out enough spaces to get to column 15 */
338 /* cursor may end up right where we want it!!! */
345 /* see if any of the state numbers has changed */
346 if (memcmp(lprocstates
, brkdn
, num_procstates
* sizeof(int)) != 0)
348 /* format and update the line */
349 summary_format(new, brkdn
, procstate_names
);
350 line_update(procstates_buffer
, new, x_brkdn
, y_brkdn
);
351 memcpy(lprocstates
, brkdn
, num_procstates
* sizeof(int));
356 * *_cpustates(states, names) - print the cpu state percentages
358 * Assumptions: cursor is on the PREVIOUS line
362 i_cpustates(struct system_info
*si
)
367 const char *thisname
;
368 int *states
= si
->cpustates
;
371 /* print tag and bump lastline */
372 for (cpu
= 0; cpu
< n_cpus
; ++cpu
) {
374 printf("\nCPU%d states: ", cpu
);
376 printf("\nCPU states: ");
379 /* now walk thru the names and print the line */
380 names
= cpustate_names
;
382 while ((thisname
= *names
++) != NULL
)
384 if (*thisname
!= '\0')
386 /* retrieve the value and remember it */
388 /* if percentage is >= 1000, print it as 100% */
389 printf((value
>= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
390 i
++ == 0 ? "" : ", ",
399 z_cpustates(struct system_info
*si __unused
)
403 const char *thisname
;
406 /* show tag and bump lastline */
407 for (cpu
= 0; cpu
< n_cpus
; ++cpu
) {
409 printf("\nCPU%d states: ", cpu
);
411 printf("\nCPU states: ");
415 names
= cpustate_names
;
416 while ((thisname
= *names
++) != NULL
)
418 if (*thisname
!= '\0')
420 printf("%s %% %s", i
++ == 0 ? "" : ", ", thisname
);
427 * *_memory(stats) - print "Memory: " followed by the memory summary string
429 * Assumptions: cursor is on "lastline"
430 * for i_memory ONLY: cursor is on the previous line
433 char memory_buffer
[MAX_COLS
];
438 fputs("\nMem: ", stdout
);
441 /* format and print the memory summary */
442 summary_format(memory_buffer
, stats
, memory_names
);
443 fputs(memory_buffer
, stdout
);
449 static char new[MAX_COLS
];
451 /* format the new line */
452 summary_format(new, stats
, memory_names
);
453 line_update(memory_buffer
, new, x_mem
, y_mem
);
457 * *_swap(stats) - print "Swap: " followed by the swap summary string
459 * Assumptions: cursor is on "lastline"
460 * for i_swap ONLY: cursor is on the previous line
463 char swap_buffer
[MAX_COLS
];
468 fputs("\nSwap: ", stdout
);
471 /* format and print the swap summary */
472 summary_format(swap_buffer
, stats
, swap_names
);
473 fputs(swap_buffer
, stdout
);
479 static char new[MAX_COLS
];
481 /* format the new line */
482 summary_format(new, stats
, swap_names
);
483 line_update(swap_buffer
, new, x_swap
, y_swap
);
487 * *_message() - print the next pending message line, or erase the one
490 * Note that u_message is (currently) the same as i_message.
492 * Assumptions: lastline is consistent
496 * i_message is funny because it gets its message asynchronously (with
497 * respect to screen updates).
500 static char next_msg
[MAX_COLS
+ 5];
501 static int msglen
= 0;
502 /* Invariant: msglen is always the length of the message currently displayed
503 on the screen (even when next_msg doesn't contain that message). */
508 while (lastline
< y_message
)
513 if (next_msg
[0] != '\0')
515 dostandout(next_msg
);
516 msglen
= strlen(next_msg
);
521 (void) clear_eol(msglen
);
532 static int header_length
;
535 * *_header(text) - print the header for the process area
537 * Assumptions: cursor is on the previous line and lastline is consistent
543 header_length
= strlen(text
);
544 if (header_status
== ON
)
550 else if (header_status
== ERASE
)
558 u_header(char *text __unused
)
560 if (header_status
== ERASE
)
564 clear_eol(header_length
);
570 * *_process(line, thisline) - print one process line
572 * Assumptions: lastline is consistent
576 i_process(int line
, char *thisline
)
581 /* make sure we are on the correct line */
582 while (lastline
< y_procs
+ line
)
588 /* truncate the line to conform to our current screen width */
589 thisline
[display_width
] = '\0';
591 /* write the line out */
592 fputs(thisline
, stdout
);
594 /* copy it in to our buffer */
595 base
= smart_terminal
? screenbuf
+ lineindex(line
) : screenbuf
;
596 p
= strecpy(base
, thisline
);
598 /* zero fill the rest of it */
599 memzero(p
, display_width
- (p
- base
));
603 u_process(int xline
, char *xnewline
)
606 int screen_line
= xline
+ Header_lines
;
609 /* remember a pointer to the current line in the screen buffer */
610 bufferline
= &screenbuf
[lineindex(xline
)];
612 /* truncate the line to conform to our current screen width */
613 newline
[display_width
] = '\0';
615 /* is line higher than we went on the last display? */
616 if (xline
>= last_hi
)
618 /* yes, just ignore screenbuf and write it out directly */
619 /* get positioned on the correct line */
620 if (screen_line
- lastline
== 1)
627 Move_to(0, screen_line
);
628 lastline
= screen_line
;
631 /* now write the line */
632 fputs(xnewline
, stdout
);
634 /* copy it in to the buffer */
635 optr
= strecpy(bufferline
, xnewline
);
637 /* zero fill the rest of it */
638 memzero(optr
, display_width
- (optr
- bufferline
));
642 line_update(bufferline
, xnewline
, 0, xline
+ Header_lines
);
649 int screen_line
= hi
+ Header_lines
;
656 /* need to blank the remainder of the screen */
657 /* but only if there is any screen left below this line */
658 if (lastline
+ 1 < screen_length
)
660 /* efficiently move to the end of currently displayed info */
661 if (screen_line
- lastline
< 5)
663 while (lastline
< screen_line
)
671 Move_to(0, screen_line
);
672 lastline
= screen_line
;
677 /* we can do this the easy way */
678 putcap(clear_to_end
);
682 /* use clear_eol on each line */
684 while ((void) clear_eol(strlen(&screenbuf
[lineindex(i
++)])), i
< last_hi
)
693 /* move the cursor to a pleasant place */
694 Move_to(x_idlecursor
, y_idlecursor
);
695 lastline
= y_idlecursor
;
699 /* separate this display from the next with some vertical room */
700 fputs("\n\n", stdout
);
705 display_header(int t
)
711 else if (header_status
== ON
)
713 header_status
= ERASE
;
719 new_message(int type
, const char *msgfmt
, ...)
724 /* first, format the message */
725 va_start(va
, msgfmt
);
726 vsnprintf(next_msg
, sizeof(next_msg
), msgfmt
, va
);
731 /* message there already -- can we clear it? */
734 /* yes -- write it and clear to end */
735 i
= strlen(next_msg
);
736 if ((type
& MT_delayed
) == 0)
738 if (type
& MT_standout
)
739 dostandout(next_msg
);
741 fputs(next_msg
, stdout
);
742 (void) clear_eol(msglen
- i
);
750 if ((type
& MT_delayed
) == 0)
752 if (type
& MT_standout
)
753 dostandout(next_msg
);
755 fputs(next_msg
, stdout
);
756 msglen
= strlen(next_msg
);
765 if (clear_eol(msglen
) == 1)
772 readline(char *buffer
, int size
, int numeric
)
779 /* allow room for null terminator */
783 while ((fflush(stdout
), read(0, ptr
, 1) > 0))
785 /* newline means we are done */
786 if ((ch
= *ptr
) == '\n' || ch
== '\r')
791 /* handle special editing characters */
794 /* kill line -- account for overstriking */
800 /* return null string */
805 else if (ch
== ch_erase
)
807 /* erase previous character */
815 fputs("\b \b", stdout
);
820 /* check for character validity and buffer overflow */
821 else if (cnt
== size
|| (numeric
&& !isdigit(ch
)) ||
829 /* echo it and store it in the buffer */
840 /* all done -- null terminate the string */
843 /* account for the extra characters in the message area */
844 /* (if terminal overstrikes, remember the furthest they went) */
845 msglen
+= overstrike
? maxcnt
: cnt
;
847 /* return either inputted number or string length */
849 return(cnt
== 0 ? -1 : numeric
? atoi(buffer
) : cnt
);
852 /* internal support routines */
855 string_count(const char **pp
)
860 while (*pp
++ != NULL
)
868 summary_format(char *str
, int *numbers
, const char **names
)
872 const char *thisname
;
874 /* format each number followed by its string */
876 while ((thisname
= *names
++) != NULL
)
878 /* get the number to format */
881 /* display only non-zero numbers */
884 /* is this number in kilobytes? */
885 if (thisname
[0] == 'K')
887 /* yes: format it as a memory value */
888 p
= strecpy(p
, format_k(num
));
890 /* skip over the K, since it was included by format_k */
891 p
= strecpy(p
, thisname
+1);
895 p
= strecpy(p
, ltoa(num
));
896 p
= strecpy(p
, thisname
);
900 /* ignore negative numbers, but display corresponding string */
903 p
= strecpy(p
, thisname
);
907 /* if the last two characters in the string are ", ", delete them */
909 if (p
>= str
&& p
[0] == ',' && p
[1] == ' ')
916 line_update(char *old
, char *new, int start
, int line
)
920 register int newcol
= start
+ 1;
921 register int lastcol
= start
;
922 char cursor_on_line
= No
;
925 /* compare the two strings and only rewrite what has changed */
928 fprintf(debug
, "line_update, starting at %d\n", start
);
932 fputs("\n-\n", debug
);
935 /* start things off on the right foot */
936 /* this is to make sure the invariants get set up right */
937 if ((ch
= *new++) != *old
)
939 if (line
- lastline
== 1 && start
== 0)
945 Move_to(start
, line
);
947 cursor_on_line
= Yes
;
955 * main loop -- check each character. If the old and new aren't the
956 * same, then update the display. When the distance from the
957 * current cursor position to the new change is small enough,
958 * the characters that belong there are written to move the
962 * lastcol is the column where the cursor currently is sitting
963 * (always one beyond the end of the last mismatch).
965 do /* yes, a do...while */
967 if ((ch
= *new++) != *old
)
969 /* new character is different from old */
970 /* make sure the cursor is on top of this character */
971 diff
= newcol
- lastcol
;
974 /* some motion is required--figure out which is shorter */
975 if (diff
< 6 && cursor_on_line
)
977 /* overwrite old stuff--get it out of the old buffer */
978 printf("%.*s", diff
, ¤t
[lastcol
-start
]);
982 /* use cursor addressing */
983 Move_to(newcol
, line
);
984 cursor_on_line
= Yes
;
986 /* remember where the cursor is */
987 lastcol
= newcol
+ 1;
991 /* already there, update position */
995 /* write what we need to */
998 /* at the end--terminate with a clear-to-end-of-line */
999 (void) clear_eol(strlen(old
));
1003 /* write the new character */
1006 /* put the new character in the screen buffer */
1010 /* update working column and screen buffer pointer */
1014 } while (ch
!= '\0');
1016 /* zero out the rest of the line buffer -- MUST BE DONE! */
1017 diff
= display_width
- newcol
;
1023 /* remember where the current line is */
1031 * printable(str) - make the string pointed to by "str" into one that is
1032 * printable (i.e.: all ascii), by converting all non-printable
1033 * characters into '?'. Replacements are done in place and a pointer
1034 * to the original buffer is returned.
1038 printable(char *str
)
1044 while ((ch
= *ptr
) != '\0')
1056 i_uptime(struct timeval
*bt
, time_t *tod
)
1059 int days
, hrs
, mins
, secs
;
1061 if (bt
->tv_sec
!= -1) {
1062 uptime
= *tod
- bt
->tv_sec
;
1064 days
= uptime
/ 86400;
1066 hrs
= uptime
/ 3600;
1072 * Display the uptime.
1077 Move_to((screen_width
- 24) - (days
> 9 ? 1 : 0), 0);
1083 printf(" up %d+%02d:%02d:%02d", days
, hrs
, mins
, secs
);