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