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