let curses handle SIGTSTP
[unix-history] / usr / src / usr.bin / systat / cmds.c
index 93d1aa7..7ba41b1 100644 (file)
@@ -1,13 +1,18 @@
+/*
+ * 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[] = "@(#)cmds.c     1.4 (Lucasfilm) %G%";
-#endif
+static char sccsid[] = "@(#)cmds.c     5.2 (Berkeley) %G%";
+#endif not lint
 
 /*
  * Command support.
  */
 
 #include "systat.h"
 
 /*
  * Command support.
  */
 
 #include "systat.h"
-#include <signal.h>
 #include <ctype.h>
 
 command(cmd)
 #include <ctype.h>
 
 command(cmd)
@@ -15,8 +20,11 @@ command(cmd)
 {
         register char *cp;
         register struct cmdtab *p;
 {
         register char *cp;
         register struct cmdtab *p;
+       int interval, omask;
         char *arg;
         char *arg;
+       extern (*sigtstpdfl)();
 
 
+       omask = sigblock(sigmask(SIGALRM));
         for (cp = cmd; *cp && !isspace(*cp); cp++)
                 ;
         if (*cp)
         for (cp = cmd; *cp && !isspace(*cp); cp++)
                 ;
         if (*cp)
@@ -29,52 +37,79 @@ command(cmd)
                 die();
        if (strcmp(cmd, "load") == 0) {
                load();
                 die();
        if (strcmp(cmd, "load") == 0) {
                load();
-               return;
+               goto done;
        }
         if (strcmp(cmd, "stop") == 0) {
                 alarm(0);
                 mvaddstr(CMDLINE, 0, "Refresh disabled.");
                 clrtoeol();
        }
         if (strcmp(cmd, "stop") == 0) {
                 alarm(0);
                 mvaddstr(CMDLINE, 0, "Refresh disabled.");
                 clrtoeol();
-                return;
+               goto done;
         }
         }
-        if (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0) {
-                int x;
+       if (strcmp(cmd, "help") == 0) {
+               int col, len;
 
 
-               x = *cp ? atoi(cp) : naptime;
-                if (x <= 0) {
-                       error("%d: bad interval.", x);
-                        return;
+               move(CMDLINE, col = 0);
+               for (p = cmdtab; p->c_name; p++) {
+                       len = strlen(p->c_name);
+                       if (col + len > COLS)
+                               break;
+                       addstr(p->c_name); col += len;
+                       if (col + 1 < COLS)
+                               addch(' ');
+               }
+               clrtoeol();
+               goto done;
+       }
+       interval = atoi(cmd);
+        if (interval <= 0 &&
+           (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
+               interval = *cp ? atoi(cp) : naptime;
+                if (interval <= 0) {
+                       error("%d: bad interval.", interval);
+                       goto done;
                 }
                 }
+       }
+       if (interval > 0) {
                 alarm(0);
                 alarm(0);
-                naptime = x;
+                naptime = interval;
                 display();
                 status();
                 display();
                 status();
-                return;
+               goto done;
         }
        p = lookup(cmd);
        if (p == (struct cmdtab *)-1) {
                error("%s: Ambiguous command.", cmd);
         }
        p = lookup(cmd);
        if (p == (struct cmdtab *)-1) {
                error("%s: Ambiguous command.", cmd);
-               return;
+               goto done;
        }
         if (p) {
                 if (curcmd == p)
        }
         if (p) {
                 if (curcmd == p)
-                        return;
+                       goto done;
                 alarm(0);
                (*curcmd->c_close)(wnd);
                wnd = (*p->c_open)();
                 alarm(0);
                (*curcmd->c_close)(wnd);
                wnd = (*p->c_open)();
+               if (wnd == 0) {
+                       error("Couldn't open new display");
+                       wnd = (*curcmd->c_open)();
+                       if (wnd == 0) {
+                               error("Couldn't change back to previous cmd");
+                               exit(1);
+                       }
+                       p = curcmd;
+               }
                 curcmd = p;
                 curcmd = p;
-               if (p->c_flags == 0) {
+               if ((p->c_flags & CF_INIT) == 0) {
                        (*p->c_init)();
                        (*p->c_init)();
-                       p->c_flags = 1;
+                       p->c_flags |= CF_INIT;
                }
                labels();
                 display();
                 status();
                }
                labels();
                 display();
                 status();
-                return;
+               goto done;
         }
         }
-       if (curcmd->c_cmd && (*curcmd->c_cmd)(cmd, cp))
-               return;
-       error("%s: Unknown command.", cmd);
+       if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp))
+               error("%s: Unknown command.", cmd);
+done:
+       sigsetmask(omask);
 }
 
 struct cmdtab *
 }
 
 struct cmdtab *
@@ -122,7 +157,7 @@ suspend()
         refresh();
         echo();
         nocrmode();
         refresh();
         echo();
         nocrmode();
-        signal(SIGTSTP, SIG_DFL);
+        signal(SIGTSTP, sigtstpdfl);
         oldmask = sigsetmask(0);
         kill(getpid(), SIGTSTP);
         sigsetmask(oldmask);
         oldmask = sigsetmask(0);
         kill(getpid(), SIGTSTP);
         sigsetmask(oldmask);
@@ -130,6 +165,17 @@ suspend()
         crmode();
         noecho();
         move(CMDLINE, col);
         crmode();
         noecho();
         move(CMDLINE, col);
-        wrefresh(curscr);
        alarm(naptime);
 }
        alarm(naptime);
 }
+
+prefix(s1, s2)
+        register char *s1, *s2;
+{
+
+        while (*s1 == *s2) {
+                if (*s1 == '\0')
+                        return (1);
+                s1++, s2++;
+        }
+        return (*s1 == '\0');
+}