]> Zhao Yanbai Git Server - minix.git/commitdiff
. have a user interface - q works! (use curses)
authorBen Gras <ben@minix3.org>
Tue, 28 Mar 2006 23:42:55 +0000 (23:42 +0000)
committerBen Gras <ben@minix3.org>
Tue, 28 Mar 2006 23:42:55 +0000 (23:42 +0000)
   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
commands/simple/top.c

index 63983d0d5bfdfec7112085b27b686b9eaeb0ddf8..97299da3fd50b1a56c125a507fb31986ddf9dbb0 100755 (executable)
@@ -810,7 +810,7 @@ touch:      touch.c
        @install -S 4kw $@
 
 top:   top.c
-       $(CCLD) -o $@ $?
+       $(CCLD) -o $@ $? -lcurses
 
 tr:    tr.c
        $(CCLD) -o $@ $?
index 03174784c85f9fd0f6e24f9b74b8c0a8216b45bc..267570ff054e1307b65fc274673c9b573b403677 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <stdio.h>
 #include <pwd.h>
+#include <curses.h>
 #include <timers.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <time.h>
 #include <string.h>
 #include <signal.h>
+#include <fcntl.h>
+#include <errno.h>
 
 #include <sys/ioc_tty.h>
 #include <sys/times.h>
 #include <sys/types.h>
+#include <sys/time.h>
+#include <sys/select.h>
 
 #include <minix/ipc.h>
 #include <minix/config.h>
 #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((c=getopt(argc, argv, "s:")) != EOF) {
+               switch(c) {
+                       case 's':
+                               s = atoi(optarg);
+                               break;
+                       default:
+                               fprintf(stderr,
+                                       "Usage: %s [-s<secdelay>]\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);
-               sleep(5);
+               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;
 }