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