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.
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
;
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 cpustate_columns
= (int *)malloc(num_cpustates
* sizeof(int));
158 memory_names
= statics
->memory_names
;
159 num_memory
= string_count(memory_names
);
160 lmemory
= (int *)malloc(num_memory
* sizeof(int));
162 /* calculate starting columns where needed */
163 cpustate_total_length
= 0;
165 ip
= cpustate_columns
;
168 *ip
++ = cpustate_total_length
;
169 if ((i
= strlen(*pp
++)) > 0)
171 cpustate_total_length
+= i
+ 8;
176 /* return number of lines available */
180 i_loadave(mpid
, avenrun
)
188 /* i_loadave also clears the screen, since it is first */
191 /* mpid == -1 implies this system doesn't have an _mpid */
194 printf("last pid: %5d; ", mpid
);
197 printf("load averages");
199 for (i
= 0; i
< 3; i
++)
208 u_loadave(mpid
, avenrun
)
218 /* change screen only when value has really changed */
221 Move_to(x_lastpid
, y_lastpid
);
226 /* i remembers x coordinate to move to */
231 i
= x_loadave_nompid
;
234 /* move into position for load averages */
235 Move_to(i
, y_loadave
);
237 /* display new load averages */
238 /* we should optimize this and only display changes */
239 for (i
= 0; i
< 3; i
++)
253 * Display the current time.
254 * "ctime" always returns a string that looks like this:
256 * Sun Sep 16 01:03:52 1973
257 * 012345678901234567890123
260 * We want indices 11 thru 18 (length 8).
265 Move_to(screen_width
- 8, 0);
278 printf("%-8.8s\n", &(ctime(tod
)[11]));
282 static int ltotal
= 0;
283 static char procstates_buffer
[MAX_COLS
];
286 * *_procstates(total, brkdn, names) - print the process summary line
288 * Assumptions: cursor is at the beginning of the line on entry
292 i_procstates(total
, brkdn
)
300 /* write current number of processes and remember the value */
301 printf("%d processes:", total
);
304 /* put out enough spaces to get to column 15 */
311 /* format and print the process state summary */
312 summary_format(procstates_buffer
, brkdn
, procstate_names
);
313 fputs(procstates_buffer
, stdout
);
315 /* save the numbers for next time */
316 memcpy(lprocstates
, brkdn
, num_procstates
* sizeof(int));
319 u_procstates(total
, brkdn
)
325 static char new[MAX_COLS
];
328 /* update number of processes only if it has changed */
331 /* move and overwrite */
332 #if (x_procstate == 0)
333 Move_to(x_procstate
, y_procstate
);
335 /* cursor is already there...no motion needed */
336 /* assert(lastline == 1); */
340 /* if number of digits differs, rewrite the label */
341 if (digits(total
) != digits(ltotal
))
343 fputs(" processes:", stdout
);
344 /* put out enough spaces to get to column 15 */
350 /* cursor may end up right where we want it!!! */
357 /* see if any of the state numbers has changed */
358 if (memcmp(lprocstates
, brkdn
, num_procstates
* sizeof(int)) != 0)
360 /* format and update the line */
361 summary_format(new, brkdn
, procstate_names
);
362 line_update(procstates_buffer
, new, x_brkdn
, y_brkdn
);
363 memcpy(lprocstates
, brkdn
, num_procstates
* sizeof(int));
368 * *_cpustates(states, names) - print the cpu state percentages
370 * Assumptions: cursor is on the PREVIOUS line
374 i_cpustates(struct system_info
*si
)
378 register char **names
;
379 register char *thisname
;
380 int *states
= si
->cpustates
;
383 /* print tag and bump lastline */
384 for (cpu
= 0; cpu
< n_cpus
; ++cpu
) {
386 printf("\nCPU%d states: ", cpu
);
388 printf("\nCPU states: ");
391 /* now walk thru the names and print the line */
392 names
= cpustate_names
;
394 while ((thisname
= *names
++) != NULL
)
396 if (*thisname
!= '\0')
398 /* retrieve the value and remember it */
400 /* if percentage is >= 1000, print it as 100% */
401 printf((value
>= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
402 i
++ == 0 ? "" : ", ",
411 z_cpustates(struct system_info
*si
)
414 register char **names
;
415 register char *thisname
;
418 /* show tag and bump lastline */
419 for (cpu
= 0; cpu
< n_cpus
; ++cpu
) {
421 printf("\nCPU%d states: ", cpu
);
423 printf("\nCPU states: ");
427 names
= cpustate_names
;
428 while ((thisname
= *names
++) != NULL
)
430 if (*thisname
!= '\0')
432 printf("%s %% %s", i
++ == 0 ? "" : ", ", thisname
);
439 * *_memory(stats) - print "Memory: " followed by the memory summary string
441 * Assumptions: cursor is on "lastline"
442 * for i_memory ONLY: cursor is on the previous line
445 char memory_buffer
[MAX_COLS
];
452 fputs("\nMem: ", stdout
);
455 /* format and print the memory summary */
456 summary_format(memory_buffer
, stats
, memory_names
);
457 fputs(memory_buffer
, stdout
);
465 static char new[MAX_COLS
];
467 /* format the new line */
468 summary_format(new, stats
, memory_names
);
469 line_update(memory_buffer
, new, x_mem
, y_mem
);
473 * *_swap(stats) - print "Swap: " followed by the swap summary string
475 * Assumptions: cursor is on "lastline"
476 * for i_swap ONLY: cursor is on the previous line
479 char swap_buffer
[MAX_COLS
];
486 fputs("\nSwap: ", stdout
);
489 /* format and print the swap summary */
490 summary_format(swap_buffer
, stats
, swap_names
);
491 fputs(swap_buffer
, stdout
);
499 static char new[MAX_COLS
];
501 /* format the new line */
502 summary_format(new, stats
, swap_names
);
503 line_update(swap_buffer
, new, x_swap
, y_swap
);
507 * *_message() - print the next pending message line, or erase the one
510 * Note that u_message is (currently) the same as i_message.
512 * Assumptions: lastline is consistent
516 * i_message is funny because it gets its message asynchronously (with
517 * respect to screen updates).
520 static char next_msg
[MAX_COLS
+ 5];
521 static int msglen
= 0;
522 /* Invariant: msglen is always the length of the message currently displayed
523 on the screen (even when next_msg doesn't contain that message). */
528 while (lastline
< y_message
)
533 if (next_msg
[0] != '\0')
536 msglen
= strlen(next_msg
);
541 (void) clear_eol(msglen
);
552 static int header_length
;
555 * *_header(text) - print the header for the process area
557 * Assumptions: cursor is on the previous line and lastline is consistent
565 header_length
= strlen(text
);
566 if (header_status
== ON
)
572 else if (header_status
== ERASE
)
581 char *text
; /* ignored */
584 if (header_status
== ERASE
)
588 clear_eol(header_length
);
594 * *_process(line, thisline) - print one process line
596 * Assumptions: lastline is consistent
599 i_process(line
, thisline
)
608 /* make sure we are on the correct line */
609 while (lastline
< y_procs
+ line
)
615 /* truncate the line to conform to our current screen width */
616 thisline
[display_width
] = '\0';
618 /* write the line out */
619 fputs(thisline
, stdout
);
621 /* copy it in to our buffer */
622 base
= smart_terminal
? screenbuf
+ lineindex(line
) : screenbuf
;
623 p
= strecpy(base
, thisline
);
625 /* zero fill the rest of it */
626 memzero(p
, display_width
- (p
- base
));
629 u_process(line
, newline
)
636 register int screen_line
= line
+ Header_lines
;
637 register char *bufferline
;
639 /* remember a pointer to the current line in the screen buffer */
640 bufferline
= &screenbuf
[lineindex(line
)];
642 /* truncate the line to conform to our current screen width */
643 newline
[display_width
] = '\0';
645 /* is line higher than we went on the last display? */
648 /* yes, just ignore screenbuf and write it out directly */
649 /* get positioned on the correct line */
650 if (screen_line
- lastline
== 1)
657 Move_to(0, screen_line
);
658 lastline
= screen_line
;
661 /* now write the line */
662 fputs(newline
, stdout
);
664 /* copy it in to the buffer */
665 optr
= strecpy(bufferline
, newline
);
667 /* zero fill the rest of it */
668 memzero(optr
, display_width
- (optr
- bufferline
));
672 line_update(bufferline
, newline
, 0, line
+ Header_lines
);
681 register int screen_line
= hi
+ Header_lines
;
688 /* need to blank the remainder of the screen */
689 /* but only if there is any screen left below this line */
690 if (lastline
+ 1 < screen_length
)
692 /* efficiently move to the end of currently displayed info */
693 if (screen_line
- lastline
< 5)
695 while (lastline
< screen_line
)
703 Move_to(0, screen_line
);
704 lastline
= screen_line
;
709 /* we can do this the easy way */
710 putcap(clear_to_end
);
714 /* use clear_eol on each line */
716 while ((void) clear_eol(strlen(&screenbuf
[lineindex(i
++)])), i
< last_hi
)
725 /* move the cursor to a pleasant place */
726 Move_to(x_idlecursor
, y_idlecursor
);
727 lastline
= y_idlecursor
;
731 /* separate this display from the next with some vertical room */
732 fputs("\n\n", stdout
);
745 else if (header_status
== ON
)
747 header_status
= ERASE
;
752 new_message(type
, msgfmt
, a1
, a2
, a3
)
761 /* first, format the message */
762 (void) snprintf(next_msg
, sizeof(next_msg
), msgfmt
, a1
, a2
, a3
);
766 /* message there already -- can we clear it? */
769 /* yes -- write it and clear to end */
770 i
= strlen(next_msg
);
771 if ((type
& MT_delayed
) == 0)
773 type
& MT_standout
? standout(next_msg
) :
774 fputs(next_msg
, stdout
);
775 (void) clear_eol(msglen
- i
);
783 if ((type
& MT_delayed
) == 0)
785 type
& MT_standout
? standout(next_msg
) : fputs(next_msg
, stdout
);
786 msglen
= strlen(next_msg
);
795 if (clear_eol(msglen
) == 1)
801 readline(buffer
, size
, numeric
)
808 register char *ptr
= buffer
;
810 register char cnt
= 0;
811 register char maxcnt
= 0;
813 /* allow room for null terminator */
817 while ((fflush(stdout
), read(0, ptr
, 1) > 0))
819 /* newline means we are done */
820 if ((ch
= *ptr
) == '\n' || ch
== '\r')
825 /* handle special editing characters */
828 /* kill line -- account for overstriking */
834 /* return null string */
839 else if (ch
== ch_erase
)
841 /* erase previous character */
849 fputs("\b \b", stdout
);
854 /* check for character validity and buffer overflow */
855 else if (cnt
== size
|| (numeric
&& !isdigit(ch
)) ||
863 /* echo it and store it in the buffer */
874 /* all done -- null terminate the string */
877 /* account for the extra characters in the message area */
878 /* (if terminal overstrikes, remember the furthest they went) */
879 msglen
+= overstrike
? maxcnt
: cnt
;
881 /* return either inputted number or string length */
883 return(cnt
== 0 ? -1 : numeric
? atoi(buffer
) : cnt
);
886 /* internal support routines */
888 static int string_count(pp
)
896 while (*pp
++ != NULL
)
903 static void summary_format(str
, numbers
, names
)
907 register char **names
;
912 register char *thisname
;
913 register int useM
= No
;
915 /* format each number followed by its string */
917 while ((thisname
= *names
++) != NULL
)
919 /* get the number to format */
922 /* display only non-zero numbers */
925 /* is this number in kilobytes? */
926 if (thisname
[0] == 'K')
928 /* yes: format it as a memory value */
929 p
= strecpy(p
, format_k(num
));
931 /* skip over the K, since it was included by format_k */
932 p
= strecpy(p
, thisname
+1);
936 p
= strecpy(p
, itoa(num
));
937 p
= strecpy(p
, thisname
);
941 /* ignore negative numbers, but display corresponding string */
944 p
= strecpy(p
, thisname
);
948 /* if the last two characters in the string are ", ", delete them */
950 if (p
>= str
&& p
[0] == ',' && p
[1] == ' ')
956 static void line_update(old
, new, start
, line
)
966 register int newcol
= start
+ 1;
967 register int lastcol
= start
;
968 char cursor_on_line
= No
;
971 /* compare the two strings and only rewrite what has changed */
974 fprintf(debug
, "line_update, starting at %d\n", start
);
978 fputs("\n-\n", debug
);
981 /* start things off on the right foot */
982 /* this is to make sure the invariants get set up right */
983 if ((ch
= *new++) != *old
)
985 if (line
- lastline
== 1 && start
== 0)
991 Move_to(start
, line
);
993 cursor_on_line
= Yes
;
1001 * main loop -- check each character. If the old and new aren't the
1002 * same, then update the display. When the distance from the
1003 * current cursor position to the new change is small enough,
1004 * the characters that belong there are written to move the
1008 * lastcol is the column where the cursor currently is sitting
1009 * (always one beyond the end of the last mismatch).
1011 do /* yes, a do...while */
1013 if ((ch
= *new++) != *old
)
1015 /* new character is different from old */
1016 /* make sure the cursor is on top of this character */
1017 diff
= newcol
- lastcol
;
1020 /* some motion is required--figure out which is shorter */
1021 if (diff
< 6 && cursor_on_line
)
1023 /* overwrite old stuff--get it out of the old buffer */
1024 printf("%.*s", diff
, ¤t
[lastcol
-start
]);
1028 /* use cursor addressing */
1029 Move_to(newcol
, line
);
1030 cursor_on_line
= Yes
;
1032 /* remember where the cursor is */
1033 lastcol
= newcol
+ 1;
1037 /* already there, update position */
1041 /* write what we need to */
1044 /* at the end--terminate with a clear-to-end-of-line */
1045 (void) clear_eol(strlen(old
));
1049 /* write the new character */
1052 /* put the new character in the screen buffer */
1056 /* update working column and screen buffer pointer */
1060 } while (ch
!= '\0');
1062 /* zero out the rest of the line buffer -- MUST BE DONE! */
1063 diff
= display_width
- newcol
;
1069 /* remember where the current line is */
1077 * printable(str) - make the string pointed to by "str" into one that is
1078 * printable (i.e.: all ascii), by converting all non-printable
1079 * characters into '?'. Replacements are done in place and a pointer
1080 * to the original buffer is returned.
1083 char *printable(str
)
1092 while ((ch
= *ptr
) != '\0')
1110 int days
, hrs
, mins
, secs
;
1112 if (bt
->tv_sec
!= -1) {
1113 uptime
= *tod
- bt
->tv_sec
;
1115 days
= uptime
/ 86400;
1117 hrs
= uptime
/ 3600;
1123 * Display the uptime.
1128 Move_to((screen_width
- 24) - (days
> 9 ? 1 : 0), 0);
1134 printf(" up %d+%02d:%02d:%02d", days
, hrs
, mins
, secs
);