file reorg, pathnames.h, paths.h; some reformatting
[unix-history] / usr / src / usr.bin / systat / main.c
index f9e2abc..296a3b8 100644 (file)
@@ -1,37 +1,32 @@
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)main.c     1.2 (Lucasfilm) %G%";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
 
 
-#include "systat.h"
+#ifndef lint
+static char sccsid[] = "@(#)main.c     5.4 (Berkeley) %G%";
+#endif not lint
 
 
-struct nlist nlst[] = {
-#define X_PROC          0
-        { "_proc" },
-#define X_NPROC         1
-        { "_nproc" },
-#define X_CCPU          2
-        { "_ccpu" },
-#define X_AVENRUN       3
-        { "_avenrun" },
-#define X_USRPTMAP      4
-        { "_Usrptmap" },
-#define X_USRPT         5
-        { "_usrpt" },
-#define X_NSWAP         6
-        { "_nswap" },
-#define X_SWAPMAP       7
-        { "_swapmap" },
-#define X_NSWAPMAP      8
-        { "_nswapmap" },
-#define X_DMMIN         9
-        { "_dmmin" },
-#define X_DMMAX         10
-        { "_dmmax" },
-#define X_NSWDEV        11
-        { "_nswdev" },
-#define        X_SWDEVT        12
-       { "_swdevt" },
-        { "" }
+#include "systat.h"
+#include <paths.h>
+
+static struct nlist nlst[] = {
+#define X_CCPU          0
+       { "_ccpu" },
+#define X_AVENRUN       1
+       { "_avenrun" },
+#define        X_HZ            2
+       { "_hz" },
+#define        X_PHZ           3
+       { "_phz" },
+       { "" }
 };
 
 int     kmem = -1;
 };
 
 int     kmem = -1;
@@ -42,295 +37,179 @@ int       naptime = 5;
 int     die();
 int     display();
 int     suspend();
 int     die();
 int     display();
 int     suspend();
+int    (*sigtstpdfl)();
 
 
-int     showpigs(), openpigs(), fetchpigs(), labelpigs();
-int     showswap(), fetchswap(), labelswap();
-int     showuser(), openuser(), fetchuser(), labeluser();
-int     shownet(), opennet(), fetchnet(), labelnet();
+double ccpu;
+int     dellave;
 
 
-struct  cmdtab {
-        char    *c_name;
-        int     (*c_refresh)();
-        int     (*c_open)();
-        int     (*c_fetch)();
-        int     (*c_label)();
-} cmdtab[] = {
-        { "pigs",       showpigs,       openpigs,       fetchpigs,
-          labelpigs },
-        { "swap",       showswap,       openpigs,       fetchswap,
-          labelswap },
-#ifdef notdef
-        { "user",       showuser,       openuser,       fetchuser,
-          labeluser },
-        { "net",        shownet,        opennet,        fetchnet,
-          labelnet },
-#endif
-        { "" }
-};
-struct  cmdtab *curcmd = &cmdtab[0];
+static WINDOW *wload;                  /* one line window for load average */
 
 main(argc, argv)
 
 main(argc, argv)
-        int argc;
-        char **argv;
+       int argc;
+       char **argv;
 {
 {
-        char ch, line[80];
 
        argc--, argv++;
        while (argc > 0) {
                if (argv[0][0] == '-') {
                        struct cmdtab *p;
 
 
        argc--, argv++;
        while (argc > 0) {
                if (argv[0][0] == '-') {
                        struct cmdtab *p;
 
-                       for (p = cmdtab; *p->c_name; p++)
-                               if (strcmp(p->c_name, &argv[0][1]) == 0)
-                                       break;
-                       if (*p->c_name == 0) {
+                       p = lookup(&argv[0][1]);
+                       if (p == (struct cmdtab *)-1) {
                                fprintf(stderr, "%s: unknown request\n",
                                    &argv[0][1]);
                                exit(1);
                        }
                        curcmd = p;
                } else {
                                fprintf(stderr, "%s: unknown request\n",
                                    &argv[0][1]);
                                exit(1);
                        }
                        curcmd = p;
                } else {
-                       naptime = atoi(argv[1]);
-                       if (naptime < 5)
+                       naptime = atoi(argv[0]);
+                       if (naptime <= 0)
                                naptime = 5;
                }
                argc--, argv++;
        }
                                naptime = 5;
                }
                argc--, argv++;
        }
-        nlist("/vmunix", nlst);
-        (*curcmd->c_open)();
-        signal(SIGINT, die);
-        signal(SIGQUIT, die);
-        signal(SIGTERM, die);
-
-        /* Initialize curses. */
-        initscr();
-        wnd = newwin(20, 70, 3, 5);
-
-#ifdef notdef
-        gethostname(hostname, sizeof (hostname));
-#endif
-        lseek(kmem, nlst[X_CCPU].n_value, 0);
-        read(kmem, &ccpu, sizeof (ccpu));
-        lccpu = log(ccpu);
-        (*curcmd->c_fetch)();
-        labels();
-
-        known[0].k_uid = -1;
-        strcpy(known[0].k_name, "<idle>");
-        numknown = 1;
-        dellave = 0.0;
-
-        signal(SIGALRM, display);
-        signal(SIGTSTP, suspend);
-        display();
-        noecho();
-        crmode();
-        for (;;) {
-                col = 0;
-                move(22, 0);
-                do {
-                        refresh();
-                        ch = getch() & 0177;
-                        if (ch == 0177 && ferror(stdin)) {
-                                clearerr(stdin);
-                                continue;
-                        }
-                        if (ch >= 'A' && ch <= 'Z')
-                                ch += 'a' - 'A';
-                        if (col == 0) {
-                                if (ch == CTRL(l)) {
-                                        wrefresh(curscr);
-                                        continue;
-                                }
-                                if (ch != ':')
-                                        continue;
-                                move(22, 0);
-                                clrtoeol();
-                        }
-                        if (ch == _tty.sg_erase && col > 0) {
-                                if (col == 1 && line[0] == ':')
-                                        continue;
-                                col--;
-                                goto doerase;
-                        }
-                        if (ch == CTRL(w) && col > 0) {
-                                while (--col >= 0 && isspace(line[col]))
-                                        ;
-                                col++;
-                                while (--col >= 0 && !isspace(line[col]))
-                                        if (col == 0 && line[0] == ':')
-                                                break;
-                                col++;
-                                goto doerase;
-                        }
-                        if (ch == _tty.sg_kill && col > 0) {
-                                col = 0;
-                                if (line[0] == ':')
-                                        col++;
-                doerase:
-                                move(22, col);
-                                clrtoeol();
-                                continue;
-                        }
-                        if (isprint(ch)) {
-                                line[col] = ch;
-                                mvaddch(22, col, ch);
-                                col++;
-                        }
-                } while (col == 0 || (ch != '\r' && ch != '\n'));
-                line[col] = '\0';
-                command(line + 1);
-        }
-}
-
-command(cmd)
-        char *cmd;
-{
-        register char *cp;
-        register struct cmdtab *p;
-        char *arg;
-
-        for (cp = cmd; *cp && !isspace(*cp); cp++)
-                ;
-        if (*cp)
-                *cp++ = '\0';
-        if (strcmp(cmd, "quit") == 0)
-                die();
-        if (strcmp(cmd, "status") == 0 || strcmp(cmd, "help") == 0) {
-                status();
-                return;
-        }
-       if (strcmp(cmd, "load") == 0) {
-               lseek(kmem, nlst[X_AVENRUN].n_value, L_SET);
-               read(kmem, &lave, sizeof (lave));
-               mvprintw(22, 0, "%4.1f", lave);
-               clrtoeol();
-               return;
+       nlist(_PATH_UNIX, nlst);
+       if (nlst[X_CCPU].n_type == 0) {
+               fprintf(stderr, "Couldn't namelist %s.\n", _PATH_UNIX);
+               exit(1);
        }
        }
-        for (p = cmdtab; *p->c_name; p++)
-                if (strcmp(cmd, p->c_name) == 0)
-                        break;
-        if (*p->c_name) {
-                if (curcmd == p)
-                        return;
-                alarm(0);
-                curcmd = p;
-               clear(); wclear(wnd);
-               labels();
-                display();
-                status();
-                return;
-        }
-        if (strcmp(cmd, "stop") == 0) {
-                alarm(0);
-                mvaddstr(22, 0, "Refresh disabled.");
-                clrtoeol();
-                return;
-        }
-        /* commands with arguments */
-        for (; *cp && isspace(*cp); cp++)
-                ;
-        if (strcmp(cmd, "start") == 0) {
-                int x;
-
-                if (*cp == '\0')
-                        x = naptime;
-                else
-                        x = atoi(cp);
-                if (x <= 0) {
-                        mvprintw(22, 0, "%d: bad interval.", x);
-                        clrtoeol();
-                        return;
-                }
-                alarm(0);
-                naptime = x;
-                display();
-                status();
-                return;
-        }
-       if (*cmd) {
-               mvprintw(22, 0, "%s: Unknown command.", cmd);
-               clrtoeol();
+       kmemf = _PATH_KMEM;
+       kmem = open(kmemf, O_RDONLY);
+       if (kmem < 0) {
+               perror(kmemf);
+               exit(1);
+       }
+       memf = _PATH_MEM;
+       mem = open(memf, O_RDONLY);
+       if (mem < 0) {
+               perror(memf);
+               exit(1);
+       }
+       swapf = _PATH_DRUM;
+       swap = open(swapf, O_RDONLY);
+       if (swap < 0) {
+               perror(swapf);
+               exit(1);
+       }
+       signal(SIGINT, die);
+       signal(SIGQUIT, die);
+       signal(SIGTERM, die);
+
+       /*
+        * Initialize display.  Load average appears in a one line
+        * window of its own.  Current command's display appears in
+        * an overlapping sub-window of stdscr configured by the display
+        * routines to minimize update work by curses.
+        */
+       initscr();
+       CMDLINE = LINES - 1;
+       wnd = (*curcmd->c_open)();
+       if (wnd == NULL) {
+               fprintf(stderr, "Couldn't initialize display.\n");
+               die();
+       }
+       wload = newwin(1, 0, 3, 20);
+       if (wload == NULL) {
+               fprintf(stderr, "Couldn't set up load average window.\n");
+               die();
        }
        }
-}
-
-status()
-{
-
-        mvprintw(22, 0, "Showing %s, refresh every %d seconds.",
-          curcmd->c_name, naptime);
-        clrtoeol();
-}
-
-suspend()
-{
-        int oldmask;
 
 
-       alarm(0);
-        move(22, 0);
-        refresh();
-        echo();
-        nocrmode();
-        signal(SIGTSTP, SIG_DFL);
-        oldmask = sigsetmask(0);
-        kill(getpid(), SIGTSTP);
-        sigsetmask(oldmask);
-        signal(SIGTSTP, suspend);
-        crmode();
-        noecho();
-        move(22, col);
-        wrefresh(curscr);
-       alarm(naptime);
+       gethostname(hostname, sizeof (hostname));
+       lseek(kmem, nlst[X_CCPU].n_value, L_SET);
+       read(kmem, &ccpu, sizeof (ccpu));
+       lccpu = log(ccpu);
+       hz = getw(nlst[X_HZ].n_value);
+       phz = getw(nlst[X_PHZ].n_value);
+       (*curcmd->c_init)();
+       curcmd->c_flags |= CF_INIT;
+       labels();
+
+       known[0].k_uid = -1;
+       known[0].k_name[0] = '\0';
+       numknown = 1;
+       procs[0].pid = -1;
+       strcpy(procs[0].cmd, "<idle>");
+       numprocs = 1;
+       dellave = 0.0;
+
+       signal(SIGALRM, display);
+       sigtstpdfl = signal(SIGTSTP, suspend);
+       display();
+       noecho();
+       crmode();
+       keyboard();
+       /*NOTREACHED*/
 }
 
 labels()
 {
 }
 
 labels()
 {
-
-        mvaddstr(2, 20,
-                "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
-        mvwaddstr(wnd, 0, 0, "Load Average");
-        (*curcmd->c_label)();
+       if (curcmd->c_flags & CF_LOADAV) {
+               mvaddstr(2, 20,
+                   "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
+               mvaddstr(3, 5, "Load Average");
+       }
+       (*curcmd->c_label)();
 #ifdef notdef
 #ifdef notdef
-        mvprintw(21, 25, "CPU usage on %s", hostname);
+       mvprintw(21, 25, "CPU usage on %s", hostname);
 #endif
 #endif
-        refresh();
+       refresh();
 }
 
 display()
 {
 }
 
 display()
 {
-        register int i, j;
+       register int i, j;
+
+       /* Get the load average over the last minute. */
+       lseek(kmem, nlst[X_AVENRUN].n_value, L_SET);
+       read(kmem, avenrun, sizeof (avenrun));
+       (*curcmd->c_fetch)();
+       if (curcmd->c_flags & CF_LOADAV) {
+               j = 5.0*avenrun[0] + 0.5;
+               dellave -= avenrun[0];
+               if (dellave >= 0.0)
+                       c = '<';
+               else {
+                       c = '>';
+                       dellave = -dellave;
+               }
+               if (dellave < 0.1)
+                       c = '|';
+               dellave = avenrun[0];
+               wmove(wload, 0, 0); wclrtoeol(wload);
+               for (i = (j > 50) ? 50 : j; i > 0; i--)
+                       waddch(wload, c);
+               if (j > 50)
+                       wprintw(wload, " %4.1f", avenrun[0]);
+       }
+       (*curcmd->c_refresh)();
+       if (curcmd->c_flags & CF_LOADAV)
+               wrefresh(wload);
+       wrefresh(wnd);
+       move(CMDLINE, col);
+       refresh();
+       alarm(naptime);
+}
+
+load()
+{
+       double  avenrun[3];
 
 
-        /* Get the load average over the last minute. */
-        lseek(kmem, nlst[X_AVENRUN].n_value, L_SET);
-        read(kmem, &lave, sizeof (lave));
-        (*curcmd->c_fetch)();
-        j = 5.0*lave + 0.5;
-        dellave -= lave;
-        if (dellave >= 0.0)
-                c = '<';
-        else {
-                c = '>';
-                dellave = -dellave;
-        }
-        if (dellave < 0.1)
-                c = '|';
-        dellave = lave;
-        wmove(wnd, 0, 15);
-        wclrtoeol(wnd);
-        for (i = (j > 50)? 50 : j; i > 0; i--)
-                waddch(wnd, c);
-        if (j > 50)
-                wprintw(wnd, " %4.1f", lave);
-        (*curcmd->c_refresh)();
-        wrefresh(wnd);
-        move(22, col);
-        refresh();
-        alarm(naptime);
+       lseek(kmem, nlst[X_AVENRUN].n_value, L_SET);
+       read(kmem, avenrun, sizeof (avenrun));
+       mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
+           avenrun[0], avenrun[1], avenrun[2]);
+       clrtoeol();
 }
 
 die()
 {
 }
 
 die()
 {
+       endwin();
+       exit(0);
+}
 
 
-        endwin();
-        exit(0);
+error(fmt, a1, a2, a3)
+{
+       mvprintw(CMDLINE, 0, fmt, a1, a2, a3);
+       clrtoeol();
+       refresh();
 }
 }