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