Annotation of src/usr.bin/systat/cmds.c, Revision 1.12
1.12 ! millert 1: /* $OpenBSD: cmds.c,v 1.11 2002/06/18 00:46:47 deraadt Exp $ */
1.2 deraadt 2: /* $NetBSD: cmds.c,v 1.4 1996/05/10 23:16:32 thorpej Exp $ */
1.1 deraadt 3:
4: /*-
5: * Copyright (c) 1980, 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.12 ! millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: #if 0
35: static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/29/95";
36: #endif
1.12 ! millert 37: static char rcsid[] = "$OpenBSD: cmds.c,v 1.11 2002/06/18 00:46:47 deraadt Exp $";
1.1 deraadt 38: #endif /* not lint */
39:
40: #include <stdlib.h>
41: #include <unistd.h>
42: #include <signal.h>
43: #include <ctype.h>
44: #include <string.h>
45: #include "systat.h"
46: #include "extern.h"
47:
48: void
1.11 deraadt 49: command(char *cmd)
1.1 deraadt 50: {
1.10 deraadt 51: struct cmdtab *p;
52: char *cp;
1.6 millert 53: int interval;
54: sigset_t mask, omask;
1.1 deraadt 55:
1.6 millert 56: sigemptyset(&mask);
57: sigaddset(&mask, SIGALRM);
58: sigprocmask(SIG_BLOCK, &mask, &omask);
1.10 deraadt 59: for (cp = cmd; *cp && !isspace(*cp); cp++)
60: ;
61: if (*cp)
62: *cp++ = '\0';
1.1 deraadt 63: if (*cmd == '\0')
64: return;
65: for (; *cp && isspace(*cp); cp++)
66: ;
1.10 deraadt 67: if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0)
68: die();
1.1 deraadt 69: if (strcmp(cmd, "load") == 0) {
70: load();
71: goto done;
72: }
1.10 deraadt 73: if (strcmp(cmd, "stop") == 0) {
74: alarm(0);
75: mvaddstr(CMDLINE, 0, "Refresh disabled.");
76: clrtoeol();
1.1 deraadt 77: goto done;
1.10 deraadt 78: }
1.1 deraadt 79: if (strcmp(cmd, "help") == 0) {
80: int col, len;
81:
82: move(CMDLINE, col = 0);
83: for (p = cmdtab; p->c_name; p++) {
84: len = strlen(p->c_name);
85: if (col + len > COLS)
86: break;
87: addstr(p->c_name); col += len;
88: if (col + 1 < COLS)
89: addch(' ');
90: }
91: clrtoeol();
92: goto done;
93: }
94: interval = atoi(cmd);
1.10 deraadt 95: if (interval <= 0 &&
1.1 deraadt 96: (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
97: interval = *cp ? atoi(cp) : naptime;
1.10 deraadt 98: if (interval <= 0) {
1.1 deraadt 99: error("%d: bad interval.", interval);
100: goto done;
1.10 deraadt 101: }
1.1 deraadt 102: }
103: if (interval > 0) {
1.10 deraadt 104: alarm(0);
105: naptime = interval;
106: display();
107: status();
1.1 deraadt 108: goto done;
1.10 deraadt 109: }
1.1 deraadt 110: p = lookup(cmd);
111: if (p == (struct cmdtab *)-1) {
1.3 deraadt 112: error("%s: Ambiguous command.", cmd);
1.1 deraadt 113: goto done;
114: }
1.10 deraadt 115: if (p) {
116: if (curcmd == p)
1.1 deraadt 117: goto done;
1.10 deraadt 118: alarm(0);
1.1 deraadt 119: (*curcmd->c_close)(wnd);
120: wnd = (*p->c_open)();
121: if (wnd == 0) {
122: error("Couldn't open new display");
123: wnd = (*curcmd->c_open)();
124: if (wnd == 0) {
125: error("Couldn't change back to previous cmd");
126: exit(1);
127: }
128: p = curcmd;
129: }
130: if ((p->c_flags & CF_INIT) == 0) {
131: if ((*p->c_init)())
132: p->c_flags |= CF_INIT;
133: else
134: goto done;
135: }
1.10 deraadt 136: curcmd = p;
1.1 deraadt 137: labels();
1.10 deraadt 138: display();
139: status();
1.1 deraadt 140: goto done;
1.10 deraadt 141: }
1.3 deraadt 142: if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp))
143: error("%s: Unknown command.", cmd);
1.1 deraadt 144: done:
1.6 millert 145: sigprocmask(SIG_SETMASK, &omask, NULL);
1.1 deraadt 146: }
147:
148: struct cmdtab *
1.11 deraadt 149: lookup(char *name)
1.1 deraadt 150: {
1.7 mpech 151: char *p, *q;
152: struct cmdtab *c, *found;
153: int nmatches, longest;
1.1 deraadt 154:
155: longest = 0;
156: nmatches = 0;
157: found = (struct cmdtab *) 0;
1.5 millert 158: for (c = cmdtab; (p = c->c_name); c++) {
1.1 deraadt 159: for (q = name; *q == *p++; q++)
160: if (*q == 0) /* exact match? */
161: return (c);
162: if (!*q) { /* the name was a prefix */
163: if (q - name > longest) {
164: longest = q - name;
165: nmatches = 1;
166: found = c;
167: } else if (q - name == longest)
168: nmatches++;
169: }
170: }
1.3 deraadt 171: if (nmatches > 1)
1.1 deraadt 172: return ((struct cmdtab *)-1);
173: return (found);
174: }
175:
176: void
1.11 deraadt 177: status(void)
1.1 deraadt 178: {
179:
1.10 deraadt 180: error("Showing %s, refresh every %d seconds.",
181: curcmd->c_name, naptime);
1.1 deraadt 182: }
183:
184: int
1.11 deraadt 185: prefix(char *s1, char *s2)
1.1 deraadt 186: {
187:
1.10 deraadt 188: while (*s1 == *s2) {
189: if (*s1 == '\0')
190: return (1);
191: s1++, s2++;
192: }
193: return (*s1 == '\0');
1.1 deraadt 194: }