From dd19ec55eafd6fed7012a5790902130b35be2ab5 Mon Sep 17 00:00:00 2001 From: Ben Gras Date: Tue, 28 Mar 2006 23:42:55 +0000 Subject: [PATCH] . have a user interface - q works! (use curses) also pressing anything else updates the display . interval is settable, default changed to 2sec . window size changes are detected and display is updated --- commands/simple/Makefile | 2 +- commands/simple/top.c | 69 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/commands/simple/Makefile b/commands/simple/Makefile index 63983d0d5..97299da3f 100755 --- a/commands/simple/Makefile +++ b/commands/simple/Makefile @@ -810,7 +810,7 @@ touch: touch.c @install -S 4kw $@ top: top.c - $(CCLD) -o $@ $? + $(CCLD) -o $@ $? -lcurses tr: tr.c $(CCLD) -o $@ $? diff --git a/commands/simple/top.c b/commands/simple/top.c index 03174784c..267570ff0 100644 --- a/commands/simple/top.c +++ b/commands/simple/top.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -15,10 +16,14 @@ #include #include #include +#include +#include #include #include #include +#include +#include #include #include @@ -29,10 +34,11 @@ #include "../../kernel/const.h" #include "../../kernel/proc.h" -char *Tclr_all; #define TC_BUFFER 1024 /* Size of termcap(3) buffer */ #define TC_STRINGS 200 /* Enough room for cm,cl,so,se */ +char *Tclr_all; + int print_memory(struct pm_mem_info *pmi) { int h; @@ -230,7 +236,6 @@ void showtop(int r) } - printf("%s", Tclr_all); lines += print_load(loads, NLOADS); @@ -270,16 +275,68 @@ void init(int *rows) if((v = tgetstr ("li", &s)) != NULL) sscanf(v, "%d", rows); if(*rows < 1) *rows = 24; + if(!initscr()) { + fprintf(stderr, "initscr() failed\n"); + exit(1); + } + cbreak(); + nl(); } +void sigwinch(int sig) { } + int main(int argc, char *argv[]) { - int r; + int r, c, s = 0, orig; + init(&r); - while(1) { - showtop(r); - sleep(5); + while((c=getopt(argc, argv, "s:")) != EOF) { + switch(c) { + case 's': + s = atoi(optarg); + break; + default: + fprintf(stderr, + "Usage: %s [-s]\n", argv[0]); + return 1; + } } + + if(s < 1) + s = 2; + + /* Catch window size changes so display is updated properly right away. */ + signal(SIGWINCH, sigwinch); + + while(1) { + fd_set fds; + int ns; + struct timeval tv; + showtop(r); + tv.tv_sec = s; + tv.tv_usec = 0; + + FD_ZERO(&fds); + FD_SET(STDIN_FILENO, &fds); + if((ns=select(STDIN_FILENO+1, &fds, NULL, NULL, &tv)) < 0 + && errno != EINTR) { + perror("select"); + sleep(1); + } + + if(ns > 0 && FD_ISSET(STDIN_FILENO, &fds)) { + char c; + if(read(STDIN_FILENO, &c, 1) == 1) { + switch(c) { + case 'q': + return 0; + break; + } + } + } + } + + return 0; }