cmds.c revision 1.12 1 /* $NetBSD: cmds.c,v 1.12 1999/12/16 04:49:32 jwise 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/29/95";
40 #endif
41 __RCSID("$NetBSD: cmds.c,v 1.12 1999/12/16 04:49:32 jwise Exp $");
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)
54 char *cmd;
55 {
56 struct command *c;
57 struct mode *p;
58 char *cp;
59 int interval;
60 sigset_t set;
61
62 sigemptyset(&set);
63 sigaddset(&set, SIGALRM);
64 sigprocmask(SIG_BLOCK, &set, NULL);
65 for (cp = cmd; *cp && !isspace((unsigned char)*cp); cp++)
66 ;
67 if (*cp)
68 *cp++ = '\0';
69 if (*cmd == '\0')
70 return;
71 for (; *cp && isspace((unsigned char)*cp); cp++)
72 ;
73
74 for (c = global_commands; c->c_name; c++) {
75 if (strcmp(cmd, c->c_name) == 0) {
76 (c->c_cmd)(cp);
77 goto done;
78 }
79 }
80
81 interval = atoi(cmd);
82 if (interval <= 0 &&
83 (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
84 interval = *cp ? atoi(cp) : naptime;
85 if (interval <= 0) {
86 error("%d: bad interval.", interval);
87 goto done;
88 }
89 }
90 if (interval > 0) {
91 alarm(0);
92 naptime = interval;
93 display(0);
94 status();
95 goto done;
96 }
97 p = lookup(cmd);
98 if (p == (struct mode *)-1) {
99 error("%s: Ambiguous command.", cmd);
100 goto done;
101 }
102 if (p) {
103 if (curmode == p)
104 goto done;
105 alarm(0);
106 (*curmode->c_close)(wnd);
107 wnd = (*p->c_open)();
108 if (wnd == 0) {
109 error("Couldn't open new display");
110 wnd = (*curmode->c_open)();
111 if (wnd == 0) {
112 error("Couldn't change back to previous cmd");
113 exit(1);
114 }
115 p = curmode;
116 }
117 if ((p->c_flags & CF_INIT) == 0) {
118 if ((*p->c_init)())
119 p->c_flags |= CF_INIT;
120 else
121 goto done;
122 }
123 curmode = p;
124 labels();
125 display(0);
126 status();
127 goto done;
128 }
129 if (curmode->c_cmd == 0 || !(*curmode->c_cmd)(cmd, cp))
130 error("%s: Unknown command.", cmd);
131 done:
132 sigprocmask(SIG_UNBLOCK, &set, NULL);
133 }
134
135 struct mode *
136 lookup(name)
137 char *name;
138 {
139 char *p, *q;
140 struct mode *c, *found;
141 int nmatches, longest;
142
143 longest = 0;
144 nmatches = 0;
145 found = (struct mode *) 0;
146 for (c = modes; (p = c->c_name); c++) {
147 for (q = name; *q == *p++; q++)
148 if (*q == 0) /* exact match? */
149 return (c);
150 if (!*q) { /* the name was a prefix */
151 if (q - name > longest) {
152 longest = q - name;
153 nmatches = 1;
154 found = c;
155 } else if (q - name == longest)
156 nmatches++;
157 }
158 }
159 if (nmatches > 1)
160 return ((struct mode *)-1);
161 return (found);
162 }
163
164 void
165 status()
166 {
167 error("Showing %s, refresh every %d seconds.", curmode->c_name, naptime);
168 }
169
170 /* case insensitive prefix comparison */
171 int
172 prefix(s1, s2)
173 char *s1, *s2;
174 {
175 char c1, c2;
176
177 while (1) {
178 c1 = *s1 >= 'A' && *s1 <= 'Z' ? *s1 + 'a' - 'A' : *s1;
179 c2 = *s2 >= 'A' && *s2 <= 'Z' ? *s2 + 'a' - 'A' : *s2;
180 if (c1 != c2)
181 break;
182 if (c1 == '\0')
183 return (1);
184 s1++, s2++;
185 }
186 return (*s1 == '\0');
187 }
188