1 /* $OpenBSD: cmds.c,v 1.12 2003/06/03 02:56:17 millert Exp $ */
2 /* $NetBSD: cmds.c,v 1.4 1996/05/10 23:16:32 thorpej Exp $ */
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. Neither the name of the University nor the names of its contributors
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
37 static char rcsid[] = "$OpenBSD: cmds.c,v 1.12 2003/06/03 02:56:17 millert Exp $";
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
command(char * cmd)49 command(char *cmd)
50 {
51 struct cmdtab *p;
52 char *cp;
53 int interval;
54 sigset_t mask, omask;
55
56 sigemptyset(&mask);
57 sigaddset(&mask, SIGALRM);
58 sigprocmask(SIG_BLOCK, &mask, &omask);
59 for (cp = cmd; *cp && !isspace(*cp); cp++)
60 ;
61 if (*cp)
62 *cp++ = '\0';
63 if (*cmd == '\0')
64 return;
65 for (; *cp && isspace(*cp); cp++)
66 ;
67 if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0)
68 die();
69 if (strcmp(cmd, "load") == 0) {
70 load();
71 goto done;
72 }
73 if (strcmp(cmd, "stop") == 0) {
74 alarm(0);
75 mvaddstr(CMDLINE, 0, "Refresh disabled.");
76 clrtoeol();
77 goto done;
78 }
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);
95 if (interval <= 0 &&
96 (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
97 interval = *cp ? atoi(cp) : naptime;
98 if (interval <= 0) {
99 error("%d: bad interval.", interval);
100 goto done;
101 }
102 }
103 if (interval > 0) {
104 alarm(0);
105 naptime = interval;
106 display();
107 status();
108 goto done;
109 }
110 p = lookup(cmd);
111 if (p == (struct cmdtab *)-1) {
112 error("%s: Ambiguous command.", cmd);
113 goto done;
114 }
115 if (p) {
116 if (curcmd == p)
117 goto done;
118 alarm(0);
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 }
136 curcmd = p;
137 labels();
138 display();
139 status();
140 goto done;
141 }
142 if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp))
143 error("%s: Unknown command.", cmd);
144 done:
145 sigprocmask(SIG_SETMASK, &omask, NULL);
146 }
147
148 struct cmdtab *
lookup(char * name)149 lookup(char *name)
150 {
151 char *p, *q;
152 struct cmdtab *c, *found;
153 int nmatches, longest;
154
155 longest = 0;
156 nmatches = 0;
157 found = (struct cmdtab *) 0;
158 for (c = cmdtab; (p = c->c_name); c++) {
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 }
171 if (nmatches > 1)
172 return ((struct cmdtab *)-1);
173 return (found);
174 }
175
176 void
status(void)177 status(void)
178 {
179
180 error("Showing %s, refresh every %d seconds.",
181 curcmd->c_name, naptime);
182 }
183
184 int
prefix(char * s1,char * s2)185 prefix(char *s1, char *s2)
186 {
187
188 while (*s1 == *s2) {
189 if (*s1 == '\0')
190 return (1);
191 s1++, s2++;
192 }
193 return (*s1 == '\0');
194 }
195