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