Annotation of src/usr.bin/systat/cmds.c, Revision 1.15
1.15 ! tedu 1: /* $OpenBSD: cmds.c,v 1.14 2007/03/20 03:56:13 tedu 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.15 ! tedu 37: static char rcsid[] = "$OpenBSD: cmds.c,v 1.14 2007/03/20 03:56:13 tedu 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.15 ! tedu 53: double interval;
1.6 millert 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;
1.14 tedu 65: for (; isspace(*cp); cp++)
1.1 deraadt 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) {
1.13 deraadt 80: size_t len;
81: int column = 0;
1.1 deraadt 82:
1.13 deraadt 83: move(CMDLINE, column);
1.1 deraadt 84: for (p = cmdtab; p->c_name; p++) {
85: len = strlen(p->c_name);
1.13 deraadt 86: if (column + len > COLS)
1.1 deraadt 87: break;
1.13 deraadt 88: addstr(p->c_name);
89: column += len;
90: if (column + 1 < COLS)
1.1 deraadt 91: addch(' ');
92: }
93: clrtoeol();
94: goto done;
95: }
1.15 ! tedu 96: interval = strtod(cmd, NULL);
! 97: if (interval < 0.09 &&
1.1 deraadt 98: (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
1.15 ! tedu 99: interval = *cp ? strtod(cp, NULL) : naptime;
! 100: if (interval < 0.09) {
1.1 deraadt 101: error("%d: bad interval.", interval);
102: goto done;
1.10 deraadt 103: }
1.1 deraadt 104: }
1.15 ! tedu 105: if (interval >= 0.09) {
1.10 deraadt 106: alarm(0);
107: naptime = interval;
108: display();
109: status();
1.1 deraadt 110: goto done;
1.10 deraadt 111: }
1.1 deraadt 112: p = lookup(cmd);
113: if (p == (struct cmdtab *)-1) {
1.3 deraadt 114: error("%s: Ambiguous command.", cmd);
1.1 deraadt 115: goto done;
116: }
1.10 deraadt 117: if (p) {
118: if (curcmd == p)
1.1 deraadt 119: goto done;
1.10 deraadt 120: alarm(0);
1.1 deraadt 121: (*curcmd->c_close)(wnd);
122: wnd = (*p->c_open)();
123: if (wnd == 0) {
124: error("Couldn't open new display");
125: wnd = (*curcmd->c_open)();
126: if (wnd == 0) {
127: error("Couldn't change back to previous cmd");
128: exit(1);
129: }
130: p = curcmd;
131: }
132: if ((p->c_flags & CF_INIT) == 0) {
133: if ((*p->c_init)())
134: p->c_flags |= CF_INIT;
135: else
136: goto done;
137: }
1.10 deraadt 138: curcmd = p;
1.1 deraadt 139: labels();
1.10 deraadt 140: display();
141: status();
1.1 deraadt 142: goto done;
1.10 deraadt 143: }
1.3 deraadt 144: if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp))
145: error("%s: Unknown command.", cmd);
1.1 deraadt 146: done:
1.6 millert 147: sigprocmask(SIG_SETMASK, &omask, NULL);
1.1 deraadt 148: }
149:
150: struct cmdtab *
1.11 deraadt 151: lookup(char *name)
1.1 deraadt 152: {
1.7 mpech 153: char *p, *q;
154: struct cmdtab *c, *found;
155: int nmatches, longest;
1.1 deraadt 156:
157: longest = 0;
158: nmatches = 0;
159: found = (struct cmdtab *) 0;
1.5 millert 160: for (c = cmdtab; (p = c->c_name); c++) {
1.1 deraadt 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: }
1.3 deraadt 173: if (nmatches > 1)
1.1 deraadt 174: return ((struct cmdtab *)-1);
175: return (found);
176: }
177:
178: void
1.11 deraadt 179: status(void)
1.1 deraadt 180: {
181:
1.15 ! tedu 182: error("Showing %s, refresh every %f seconds.",
1.10 deraadt 183: curcmd->c_name, naptime);
1.1 deraadt 184: }
185:
186: int
1.11 deraadt 187: prefix(char *s1, char *s2)
1.1 deraadt 188: {
189:
1.10 deraadt 190: while (*s1 == *s2) {
191: if (*s1 == '\0')
192: return (1);
193: s1++, s2++;
194: }
195: return (*s1 == '\0');
1.1 deraadt 196: }