Changeset View
Changeset View
Standalone View
Standalone View
head/usr.bin/top/display.c
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | |||||
#ifdef DEBUG | #ifdef DEBUG | ||||
FILE *debug; | FILE *debug; | ||||
#endif | #endif | ||||
static int lmpid = 0; | static int lmpid = 0; | ||||
static int last_hi = 0; /* used in u_process and u_endscreen */ | static int last_hi = 0; /* used in u_process and u_endscreen */ | ||||
static int lastline = 0; | static int lastline = 0; | ||||
static int display_width = MAX_COLS; | |||||
#define lineindex(l) ((l)*display_width) | #define lineindex(l) ((l)*screen_width) | ||||
/* things initialized by display_init and used thruout */ | /* things initialized by display_init and used thruout */ | ||||
/* buffer of proc information lines for display updating */ | /* buffer of proc information lines for display updating */ | ||||
static char *screenbuf = NULL; | static char *screenbuf = NULL; | ||||
static const char * const *procstate_names; | static const char * const *procstate_names; | ||||
Show All 18 Lines | |||||
static int cpustate_total_length; | static int cpustate_total_length; | ||||
static int cpustates_column; | static int cpustates_column; | ||||
static enum { OFF, ON, ERASE } header_status = ON; | static enum { OFF, ON, ERASE } header_status = ON; | ||||
static void summary_format(char *, int *, const char * const *); | static void summary_format(char *, int *, const char * const *); | ||||
static void line_update(char *, char *, int, int); | static void line_update(char *, char *, int, int); | ||||
static int setup_buffer_bufsiz = 0; | |||||
static char * setup_buffer(char *, int); | |||||
int x_lastpid = 10; | int x_lastpid = 10; | ||||
int y_lastpid = 0; | int y_lastpid = 0; | ||||
int x_loadave = 33; | int x_loadave = 33; | ||||
int x_loadave_nompid = 15; | int x_loadave_nompid = 15; | ||||
int y_loadave = 0; | int y_loadave = 0; | ||||
int x_procstate = 0; | int x_procstate = 0; | ||||
int y_procstate = 1; | int y_procstate = 1; | ||||
int x_brkdn = 15; | int x_brkdn = 15; | ||||
Show All 28 Lines | free(screenbuf); | ||||
} | } | ||||
/* calculate the current dimensions */ | /* calculate the current dimensions */ | ||||
/* if operating in "dumb" mode, we only need one line */ | /* if operating in "dumb" mode, we only need one line */ | ||||
lines = smart_terminal ? screen_length - Header_lines : 1; | lines = smart_terminal ? screen_length - Header_lines : 1; | ||||
if (lines < 0) | if (lines < 0) | ||||
lines = 0; | lines = 0; | ||||
/* we don't want more than MAX_COLS columns, since the machine-dependent | |||||
modules make static allocations based on MAX_COLS and we don't want | |||||
to run off the end of their buffers */ | |||||
display_width = screen_width; | |||||
if (display_width >= MAX_COLS) | |||||
{ | |||||
display_width = MAX_COLS - 1; | |||||
} | |||||
/* now, allocate space for the screen buffer */ | /* now, allocate space for the screen buffer */ | ||||
screenbuf = calloc(lines, display_width); | screenbuf = calloc(lines, screen_width); | ||||
if (screenbuf == NULL) | if (screenbuf == NULL) | ||||
{ | { | ||||
/* oops! */ | /* oops! */ | ||||
return(-1); | return(-1); | ||||
} | } | ||||
/* return number of lines available */ | /* return number of lines available */ | ||||
/* for dumb terminals, pretend like we can show any amount */ | /* for dumb terminals, pretend like we can show any amount */ | ||||
▲ Show 20 Lines • Show All 171 Lines • ▼ Show 20 Lines | #ifdef DEBUG | ||||
fputs(foo, stdout); | fputs(foo, stdout); | ||||
} | } | ||||
#endif | #endif | ||||
printf("%-8.8s\n", &(ctime(tod)[11])); | printf("%-8.8s\n", &(ctime(tod)[11])); | ||||
lastline = 1; | lastline = 1; | ||||
} | } | ||||
static int ltotal = 0; | static int ltotal = 0; | ||||
static char procstates_buffer[MAX_COLS]; | static char *procstates_buffer = NULL; | ||||
/* | /* | ||||
* *_procstates(total, brkdn, names) - print the process summary line | * *_procstates(total, brkdn, names) - print the process summary line | ||||
* | * | ||||
* Assumptions: cursor is at the beginning of the line on entry | * Assumptions: cursor is at the beginning of the line on entry | ||||
* lastline is valid | * lastline is valid | ||||
*/ | */ | ||||
void | void | ||||
i_procstates(int total, int *brkdn) | i_procstates(int total, int *brkdn) | ||||
{ | { | ||||
int i; | int i; | ||||
procstates_buffer = setup_buffer(procstates_buffer, 0); | |||||
/* write current number of processes and remember the value */ | /* write current number of processes and remember the value */ | ||||
printf("%d %s:", total, (ps.thread) ? "threads" :"processes"); | printf("%d %s:", total, (ps.thread) ? "threads" :"processes"); | ||||
ltotal = total; | ltotal = total; | ||||
/* put out enough spaces to get to column 15 */ | /* put out enough spaces to get to column 15 */ | ||||
i = digits(total); | i = digits(total); | ||||
while (i++ < 4) | while (i++ < 4) | ||||
{ | { | ||||
putchar(' '); | putchar(' '); | ||||
} | } | ||||
/* format and print the process state summary */ | /* format and print the process state summary */ | ||||
summary_format(procstates_buffer, brkdn, procstate_names); | summary_format(procstates_buffer, brkdn, procstate_names); | ||||
fputs(procstates_buffer, stdout); | fputs(procstates_buffer, stdout); | ||||
/* save the numbers for next time */ | /* save the numbers for next time */ | ||||
memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); | memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); | ||||
} | } | ||||
void | void | ||||
u_procstates(int total, int *brkdn) | u_procstates(int total, int *brkdn) | ||||
{ | { | ||||
static char new[MAX_COLS]; | static char *new = NULL; | ||||
int i; | int i; | ||||
new = setup_buffer(new, 0); | |||||
/* update number of processes only if it has changed */ | /* update number of processes only if it has changed */ | ||||
if (ltotal != total) | if (ltotal != total) | ||||
{ | { | ||||
/* move and overwrite */ | /* move and overwrite */ | ||||
if (x_procstate == 0) { | if (x_procstate == 0) { | ||||
Move_to(x_procstate, y_procstate); | Move_to(x_procstate, y_procstate); | ||||
} | } | ||||
else { | else { | ||||
▲ Show 20 Lines • Show All 160 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* *_memory(stats) - print "Memory: " followed by the memory summary string | * *_memory(stats) - print "Memory: " followed by the memory summary string | ||||
* | * | ||||
* Assumptions: cursor is on "lastline" | * Assumptions: cursor is on "lastline" | ||||
* for i_memory ONLY: cursor is on the previous line | * for i_memory ONLY: cursor is on the previous line | ||||
*/ | */ | ||||
static char memory_buffer[MAX_COLS]; | static char *memory_buffer = NULL; | ||||
void | void | ||||
i_memory(int *stats) | i_memory(int *stats) | ||||
{ | { | ||||
memory_buffer = setup_buffer(memory_buffer, 0); | |||||
fputs("\nMem: ", stdout); | fputs("\nMem: ", stdout); | ||||
lastline++; | lastline++; | ||||
/* format and print the memory summary */ | /* format and print the memory summary */ | ||||
summary_format(memory_buffer, stats, memory_names); | summary_format(memory_buffer, stats, memory_names); | ||||
fputs(memory_buffer, stdout); | fputs(memory_buffer, stdout); | ||||
} | } | ||||
void | void | ||||
u_memory(int *stats) | u_memory(int *stats) | ||||
{ | { | ||||
static char new[MAX_COLS]; | static char *new = NULL; | ||||
new = setup_buffer(new, 0); | |||||
/* format the new line */ | /* format the new line */ | ||||
summary_format(new, stats, memory_names); | summary_format(new, stats, memory_names); | ||||
line_update(memory_buffer, new, x_mem, y_mem); | line_update(memory_buffer, new, x_mem, y_mem); | ||||
} | } | ||||
/* | /* | ||||
* *_arc(stats) - print "ARC: " followed by the ARC summary string | * *_arc(stats) - print "ARC: " followed by the ARC summary string | ||||
* | * | ||||
* Assumptions: cursor is on "lastline" | * Assumptions: cursor is on "lastline" | ||||
* for i_arc ONLY: cursor is on the previous line | * for i_arc ONLY: cursor is on the previous line | ||||
*/ | */ | ||||
static char arc_buffer[MAX_COLS]; | static char *arc_buffer = NULL; | ||||
void | void | ||||
i_arc(int *stats) | i_arc(int *stats) | ||||
{ | { | ||||
arc_buffer = setup_buffer(arc_buffer, 0); | |||||
if (arc_names == NULL) | if (arc_names == NULL) | ||||
return; | return; | ||||
fputs("\nARC: ", stdout); | fputs("\nARC: ", stdout); | ||||
lastline++; | lastline++; | ||||
/* format and print the memory summary */ | /* format and print the memory summary */ | ||||
summary_format(arc_buffer, stats, arc_names); | summary_format(arc_buffer, stats, arc_names); | ||||
fputs(arc_buffer, stdout); | fputs(arc_buffer, stdout); | ||||
} | } | ||||
void | void | ||||
u_arc(int *stats) | u_arc(int *stats) | ||||
{ | { | ||||
static char new[MAX_COLS]; | static char *new = NULL; | ||||
new = setup_buffer(new, 0); | |||||
if (arc_names == NULL) | if (arc_names == NULL) | ||||
return; | return; | ||||
/* format the new line */ | /* format the new line */ | ||||
summary_format(new, stats, arc_names); | summary_format(new, stats, arc_names); | ||||
line_update(arc_buffer, new, x_arc, y_arc); | line_update(arc_buffer, new, x_arc, y_arc); | ||||
} | } | ||||
/* | /* | ||||
* *_carc(stats) - print "Compressed ARC: " followed by the summary string | * *_carc(stats) - print "Compressed ARC: " followed by the summary string | ||||
* | * | ||||
* Assumptions: cursor is on "lastline" | * Assumptions: cursor is on "lastline" | ||||
* for i_carc ONLY: cursor is on the previous line | * for i_carc ONLY: cursor is on the previous line | ||||
*/ | */ | ||||
static char carc_buffer[MAX_COLS]; | static char *carc_buffer = NULL; | ||||
void | void | ||||
i_carc(int *stats) | i_carc(int *stats) | ||||
{ | { | ||||
carc_buffer = setup_buffer(carc_buffer, 0); | |||||
if (carc_names == NULL) | if (carc_names == NULL) | ||||
return; | return; | ||||
fputs("\n ", stdout); | fputs("\n ", stdout); | ||||
lastline++; | lastline++; | ||||
/* format and print the memory summary */ | /* format and print the memory summary */ | ||||
summary_format(carc_buffer, stats, carc_names); | summary_format(carc_buffer, stats, carc_names); | ||||
fputs(carc_buffer, stdout); | fputs(carc_buffer, stdout); | ||||
} | } | ||||
void | void | ||||
u_carc(int *stats) | u_carc(int *stats) | ||||
{ | { | ||||
static char new[MAX_COLS]; | static char *new = NULL; | ||||
new = setup_buffer(new, 0); | |||||
if (carc_names == NULL) | if (carc_names == NULL) | ||||
return; | return; | ||||
/* format the new line */ | /* format the new line */ | ||||
summary_format(new, stats, carc_names); | summary_format(new, stats, carc_names); | ||||
line_update(carc_buffer, new, x_carc, y_carc); | line_update(carc_buffer, new, x_carc, y_carc); | ||||
} | } | ||||
/* | /* | ||||
* *_swap(stats) - print "Swap: " followed by the swap summary string | * *_swap(stats) - print "Swap: " followed by the swap summary string | ||||
* | * | ||||
* Assumptions: cursor is on "lastline" | * Assumptions: cursor is on "lastline" | ||||
* for i_swap ONLY: cursor is on the previous line | * for i_swap ONLY: cursor is on the previous line | ||||
*/ | */ | ||||
static char swap_buffer[MAX_COLS]; | static char *swap_buffer = NULL; | ||||
void | void | ||||
i_swap(int *stats) | i_swap(int *stats) | ||||
{ | { | ||||
swap_buffer = setup_buffer(swap_buffer, 0); | |||||
fputs("\nSwap: ", stdout); | fputs("\nSwap: ", stdout); | ||||
lastline++; | lastline++; | ||||
/* format and print the swap summary */ | /* format and print the swap summary */ | ||||
summary_format(swap_buffer, stats, swap_names); | summary_format(swap_buffer, stats, swap_names); | ||||
fputs(swap_buffer, stdout); | fputs(swap_buffer, stdout); | ||||
} | } | ||||
void | void | ||||
u_swap(int *stats) | u_swap(int *stats) | ||||
{ | { | ||||
static char new[MAX_COLS]; | static char *new = NULL; | ||||
new = setup_buffer(new, 0); | |||||
/* format the new line */ | /* format the new line */ | ||||
summary_format(new, stats, swap_names); | summary_format(new, stats, swap_names); | ||||
line_update(swap_buffer, new, x_swap, y_swap); | line_update(swap_buffer, new, x_swap, y_swap); | ||||
} | } | ||||
/* | /* | ||||
* *_message() - print the next pending message line, or erase the one | * *_message() - print the next pending message line, or erase the one | ||||
* that is there. | * that is there. | ||||
* | * | ||||
* Note that u_message is (currently) the same as i_message. | * Note that u_message is (currently) the same as i_message. | ||||
* | * | ||||
* Assumptions: lastline is consistent | * Assumptions: lastline is consistent | ||||
*/ | */ | ||||
/* | /* | ||||
* i_message is funny because it gets its message asynchronously (with | * i_message is funny because it gets its message asynchronously (with | ||||
* respect to screen updates). | * respect to screen updates). | ||||
*/ | */ | ||||
static char next_msg[MAX_COLS + 5]; | static char *next_msg = NULL; | ||||
static int msglen = 0; | static int msglen = 0; | ||||
/* Invariant: msglen is always the length of the message currently displayed | /* Invariant: msglen is always the length of the message currently displayed | ||||
on the screen (even when next_msg doesn't contain that message). */ | on the screen (even when next_msg doesn't contain that message). */ | ||||
void | void | ||||
i_message(void) | i_message(void) | ||||
{ | { | ||||
next_msg = setup_buffer(next_msg, 5); | |||||
while (lastline < y_message) | while (lastline < y_message) | ||||
{ | { | ||||
fputc('\n', stdout); | fputc('\n', stdout); | ||||
lastline++; | lastline++; | ||||
} | } | ||||
if (next_msg[0] != '\0') | if (next_msg[0] != '\0') | ||||
{ | { | ||||
Show All 23 Lines | |||||
const char * | const char * | ||||
trim_header(const char *text) | trim_header(const char *text) | ||||
{ | { | ||||
char *s; | char *s; | ||||
int width; | int width; | ||||
s = NULL; | s = NULL; | ||||
width = display_width; | width = screen_width; | ||||
header_length = strlen(text); | header_length = strlen(text); | ||||
if (header_length >= width) { | if (header_length >= width) { | ||||
s = strndup(text, width); | s = strndup(text, width); | ||||
if (s == NULL) | if (s == NULL) | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
return (s); | return (s); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | i_process(int line, char *thisline) | ||||
/* make sure we are on the correct line */ | /* make sure we are on the correct line */ | ||||
while (lastline < y_procs + line) | while (lastline < y_procs + line) | ||||
{ | { | ||||
putchar('\n'); | putchar('\n'); | ||||
lastline++; | lastline++; | ||||
} | } | ||||
/* truncate the line to conform to our current screen width */ | /* truncate the line to conform to our current screen width */ | ||||
thisline[display_width] = '\0'; | thisline[screen_width] = '\0'; | ||||
/* write the line out */ | /* write the line out */ | ||||
fputs(thisline, stdout); | fputs(thisline, stdout); | ||||
/* copy it in to our buffer */ | /* copy it in to our buffer */ | ||||
base = smart_terminal ? screenbuf + lineindex(line) : screenbuf; | base = smart_terminal ? screenbuf + lineindex(line) : screenbuf; | ||||
p = stpcpy(base, thisline); | p = stpcpy(base, thisline); | ||||
/* zero fill the rest of it */ | /* zero fill the rest of it */ | ||||
memset(p, 0, display_width - (p - base)); | memset(p, 0, screen_width - (p - base)); | ||||
} | } | ||||
void | void | ||||
u_process(int line, char *newline) | u_process(int line, char *newline) | ||||
{ | { | ||||
char *optr; | char *optr; | ||||
int screen_line = line + Header_lines; | int screen_line = line + Header_lines; | ||||
char *bufferline; | char *bufferline; | ||||
/* remember a pointer to the current line in the screen buffer */ | /* remember a pointer to the current line in the screen buffer */ | ||||
bufferline = &screenbuf[lineindex(line)]; | bufferline = &screenbuf[lineindex(line)]; | ||||
/* truncate the line to conform to our current screen width */ | /* truncate the line to conform to our current screen width */ | ||||
newline[display_width] = '\0'; | newline[screen_width] = '\0'; | ||||
/* is line higher than we went on the last display? */ | /* is line higher than we went on the last display? */ | ||||
if (line >= last_hi) | if (line >= last_hi) | ||||
{ | { | ||||
/* yes, just ignore screenbuf and write it out directly */ | /* yes, just ignore screenbuf and write it out directly */ | ||||
/* get positioned on the correct line */ | /* get positioned on the correct line */ | ||||
if (screen_line - lastline == 1) | if (screen_line - lastline == 1) | ||||
{ | { | ||||
putchar('\n'); | putchar('\n'); | ||||
lastline++; | lastline++; | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
Move_to(0, screen_line); | Move_to(0, screen_line); | ||||
lastline = screen_line; | lastline = screen_line; | ||||
} | } | ||||
/* now write the line */ | /* now write the line */ | ||||
fputs(newline, stdout); | fputs(newline, stdout); | ||||
/* copy it in to the buffer */ | /* copy it in to the buffer */ | ||||
optr = stpcpy(bufferline, newline); | optr = stpcpy(bufferline, newline); | ||||
/* zero fill the rest of it */ | /* zero fill the rest of it */ | ||||
memset(optr, 0, display_width - (optr - bufferline)); | memset(optr, 0, screen_width - (optr - bufferline)); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
line_update(bufferline, newline, 0, line + Header_lines); | line_update(bufferline, newline, 0, line + Header_lines); | ||||
} | } | ||||
} | } | ||||
void | void | ||||
▲ Show 20 Lines • Show All 362 Lines • ▼ Show 20 Lines | #endif | ||||
/* update working column and screen buffer pointer */ | /* update working column and screen buffer pointer */ | ||||
newcol++; | newcol++; | ||||
old++; | old++; | ||||
} while (ch != '\0'); | } while (ch != '\0'); | ||||
/* zero out the rest of the line buffer -- MUST BE DONE! */ | /* zero out the rest of the line buffer -- MUST BE DONE! */ | ||||
diff = display_width - newcol; | diff = screen_width - newcol; | ||||
if (diff > 0) | if (diff > 0) | ||||
{ | { | ||||
memset(old, 0, diff); | memset(old, 0, diff); | ||||
} | } | ||||
/* remember where the current line is */ | /* remember where the current line is */ | ||||
if (cursor_on_line) | if (cursor_on_line) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | if (smart_terminal) | ||||
Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0); | Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
fputs(" ", stdout); | fputs(" ", stdout); | ||||
} | } | ||||
printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs); | printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs); | ||||
} | } | ||||
} | |||||
static char * | |||||
setup_buffer(char *buffer, int addlen) | |||||
{ | |||||
char *b = NULL; | |||||
if (NULL == buffer) { | |||||
setup_buffer_bufsiz = screen_width; | |||||
b = calloc(setup_buffer_bufsiz + addlen, sizeof(char)); | |||||
} else { | |||||
if (screen_width > setup_buffer_bufsiz) { | |||||
setup_buffer_bufsiz = screen_width; | |||||
free(buffer); | |||||
b = calloc(setup_buffer_bufsiz + addlen, | |||||
sizeof(char)); | |||||
} else { | |||||
b = buffer; | |||||
} | |||||
} | |||||
if (NULL == b) { | |||||
fprintf(stderr, "%s: can't allocate sufficient memory\n", | |||||
myname); | |||||
exit(4); | |||||
} | |||||
return b; | |||||
} | } |