1 /*- 2 * Copyright (c) 1980, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 04/29/95"; 10 #endif /* not lint */ 11 12 #include <stdlib.h> 13 #include <unistd.h> 14 #include <signal.h> 15 #include <ctype.h> 16 #include <string.h> 17 #include "systat.h" 18 #include "extern.h" 19 20 void 21 command(cmd) 22 char *cmd; 23 { 24 register struct cmdtab *p; 25 register char *cp; 26 int interval, omask; 27 28 omask = sigblock(sigmask(SIGALRM)); 29 for (cp = cmd; *cp && !isspace(*cp); cp++) 30 ; 31 if (*cp) 32 *cp++ = '\0'; 33 if (*cmd == '\0') 34 return; 35 for (; *cp && isspace(*cp); cp++) 36 ; 37 if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0) 38 die(0); 39 if (strcmp(cmd, "load") == 0) { 40 load(); 41 goto done; 42 } 43 if (strcmp(cmd, "stop") == 0) { 44 alarm(0); 45 mvaddstr(CMDLINE, 0, "Refresh disabled."); 46 clrtoeol(); 47 goto done; 48 } 49 if (strcmp(cmd, "help") == 0) { 50 int col, len; 51 52 move(CMDLINE, col = 0); 53 for (p = cmdtab; p->c_name; p++) { 54 len = strlen(p->c_name); 55 if (col + len > COLS) 56 break; 57 addstr(p->c_name); col += len; 58 if (col + 1 < COLS) 59 addch(' '); 60 } 61 clrtoeol(); 62 goto done; 63 } 64 interval = atoi(cmd); 65 if (interval <= 0 && 66 (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) { 67 interval = *cp ? atoi(cp) : naptime; 68 if (interval <= 0) { 69 error("%d: bad interval.", interval); 70 goto done; 71 } 72 } 73 if (interval > 0) { 74 alarm(0); 75 naptime = interval; 76 display(0); 77 status(); 78 goto done; 79 } 80 p = lookup(cmd); 81 if (p == (struct cmdtab *)-1) { 82 error("%s: Ambiguous command.", cmd); 83 goto done; 84 } 85 if (p) { 86 if (curcmd == p) 87 goto done; 88 alarm(0); 89 (*curcmd->c_close)(wnd); 90 wnd = (*p->c_open)(); 91 if (wnd == 0) { 92 error("Couldn't open new display"); 93 wnd = (*curcmd->c_open)(); 94 if (wnd == 0) { 95 error("Couldn't change back to previous cmd"); 96 exit(1); 97 } 98 p = curcmd; 99 } 100 if ((p->c_flags & CF_INIT) == 0) { 101 if ((*p->c_init)()) 102 p->c_flags |= CF_INIT; 103 else 104 goto done; 105 } 106 curcmd = p; 107 labels(); 108 display(0); 109 status(); 110 goto done; 111 } 112 if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp)) 113 error("%s: Unknown command.", cmd); 114 done: 115 sigsetmask(omask); 116 } 117 118 struct cmdtab * 119 lookup(name) 120 register char *name; 121 { 122 register char *p, *q; 123 register struct cmdtab *c, *found; 124 register int nmatches, longest; 125 126 longest = 0; 127 nmatches = 0; 128 found = (struct cmdtab *) 0; 129 for (c = cmdtab; p = c->c_name; c++) { 130 for (q = name; *q == *p++; q++) 131 if (*q == 0) /* exact match? */ 132 return (c); 133 if (!*q) { /* the name was a prefix */ 134 if (q - name > longest) { 135 longest = q - name; 136 nmatches = 1; 137 found = c; 138 } else if (q - name == longest) 139 nmatches++; 140 } 141 } 142 if (nmatches != 1) 143 return ((struct cmdtab *)-1); 144 return (found); 145 } 146 147 void 148 status() 149 { 150 151 error("Showing %s, refresh every %d seconds.", 152 curcmd->c_name, naptime); 153 } 154 155 int 156 prefix(s1, s2) 157 register char *s1, *s2; 158 { 159 160 while (*s1 == *s2) { 161 if (*s1 == '\0') 162 return (1); 163 s1++, s2++; 164 } 165 return (*s1 == '\0'); 166 } 167