cmds.c revision 1.5 1 /* $NetBSD: cmds.c,v 1.5 1996/12/13 19:26:18 scottr Exp $ */
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
40 static char rcsid[] = "$NetBSD: cmds.c,v 1.5 1996/12/13 19:26:18 scottr Exp $";
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;
58 sigset_t set;
59
60 sigemptyset(&set);
61 sigaddset(&set, SIGALRM);
62 sigprocmask(SIG_BLOCK, &set, NULL);
63 for (cp = cmd; *cp && !isspace(*cp); cp++)
64 ;
65 if (*cp)
66 *cp++ = '\0';
67 if (*cmd == '\0')
68 return;
69 for (; *cp && isspace(*cp); cp++)
70 ;
71 if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0)
72 die(0);
73 if (strcmp(cmd, "load") == 0) {
74 load();
75 goto done;
76 }
77 if (strcmp(cmd, "stop") == 0) {
78 alarm(0);
79 mvaddstr(CMDLINE, 0, "Refresh disabled.");
80 clrtoeol();
81 goto done;
82 }
83 if (strcmp(cmd, "help") == 0) {
84 int col, len;
85
86 move(CMDLINE, col = 0);
87 for (p = cmdtab; p->c_name; p++) {
88 len = strlen(p->c_name);
89 if (col + len > COLS)
90 break;
91 addstr(p->c_name); col += len;
92 if (col + 1 < COLS)
93 addch(' ');
94 }
95 clrtoeol();
96 goto done;
97 }
98 interval = atoi(cmd);
99 if (interval <= 0 &&
100 (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
101 interval = *cp ? atoi(cp) : naptime;
102 if (interval <= 0) {
103 error("%d: bad interval.", interval);
104 goto done;
105 }
106 }
107 if (interval > 0) {
108 alarm(0);
109 naptime = interval;
110 display(0);
111 status();
112 goto done;
113 }
114 p = lookup(cmd);
115 if (p == (struct cmdtab *)-1) {
116 /* if not a primary command, try a display specific one */
117 if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp))
118 error("%s: Ambiguous command.", cmd);
119 goto done;
120 }
121 if (p) {
122 if (curcmd == p)
123 goto done;
124 alarm(0);
125 (*curcmd->c_close)(wnd);
126 wnd = (*p->c_open)();
127 if (wnd == 0) {
128 error("Couldn't open new display");
129 wnd = (*curcmd->c_open)();
130 if (wnd == 0) {
131 error("Couldn't change back to previous cmd");
132 exit(1);
133 }
134 p = curcmd;
135 }
136 if ((p->c_flags & CF_INIT) == 0) {
137 if ((*p->c_init)())
138 p->c_flags |= CF_INIT;
139 else
140 goto done;
141 }
142 curcmd = p;
143 labels();
144 display(0);
145 status();
146 goto done;
147 }
148 done:
149 sigprocmask(SIG_UNBLOCK, &set, NULL);
150 }
151
152 struct cmdtab *
153 lookup(name)
154 register char *name;
155 {
156 register char *p, *q;
157 register struct cmdtab *c, *found;
158 register int nmatches, longest;
159
160 longest = 0;
161 nmatches = 0;
162 found = (struct cmdtab *) 0;
163 for (c = cmdtab; p = c->c_name; c++) {
164 for (q = name; *q == *p++; q++)
165 if (*q == 0) /* exact match? */
166 return (c);
167 if (!*q) { /* the name was a prefix */
168 if (q - name > longest) {
169 longest = q - name;
170 nmatches = 1;
171 found = c;
172 } else if (q - name == longest)
173 nmatches++;
174 }
175 }
176 if (nmatches != 1)
177 return ((struct cmdtab *)-1);
178 return (found);
179 }
180
181 void
182 status()
183 {
184 error("Showing %s, refresh every %d seconds.", curcmd->c_name, naptime);
185 }
186
187 int
188 prefix(s1, s2)
189 register char *s1, *s2;
190 {
191
192 while (*s1 == *s2) {
193 if (*s1 == '\0')
194 return (1);
195 s1++, s2++;
196 }
197 return (*s1 == '\0');
198 }
199