xref: /freebsd/usr.bin/systat/cmds.c (revision 9ff712b0f5e27b68efdb47d863ab7ef8ee243a5b)
1ff6c33c9SBruce Evans /*-
2ff6c33c9SBruce Evans  * Copyright (c) 1980, 1992, 1993
3ff6c33c9SBruce Evans  *	The Regents of the University of California.  All rights reserved.
4ff6c33c9SBruce Evans  *
5ff6c33c9SBruce Evans  * Redistribution and use in source and binary forms, with or without
6ff6c33c9SBruce Evans  * modification, are permitted provided that the following conditions
7ff6c33c9SBruce Evans  * are met:
8ff6c33c9SBruce Evans  * 1. Redistributions of source code must retain the above copyright
9ff6c33c9SBruce Evans  *    notice, this list of conditions and the following disclaimer.
10ff6c33c9SBruce Evans  * 2. Redistributions in binary form must reproduce the above copyright
11ff6c33c9SBruce Evans  *    notice, this list of conditions and the following disclaimer in the
12ff6c33c9SBruce Evans  *    documentation and/or other materials provided with the distribution.
13ff6c33c9SBruce Evans  * 3. All advertising materials mentioning features or use of this software
14ff6c33c9SBruce Evans  *    must display the following acknowledgement:
15ff6c33c9SBruce Evans  *	This product includes software developed by the University of
16ff6c33c9SBruce Evans  *	California, Berkeley and its contributors.
17ff6c33c9SBruce Evans  * 4. Neither the name of the University nor the names of its contributors
18ff6c33c9SBruce Evans  *    may be used to endorse or promote products derived from this software
19ff6c33c9SBruce Evans  *    without specific prior written permission.
20ff6c33c9SBruce Evans  *
21ff6c33c9SBruce Evans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22ff6c33c9SBruce Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ff6c33c9SBruce Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ff6c33c9SBruce Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25ff6c33c9SBruce Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ff6c33c9SBruce Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ff6c33c9SBruce Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ff6c33c9SBruce Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ff6c33c9SBruce Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ff6c33c9SBruce Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ff6c33c9SBruce Evans  * SUCH DAMAGE.
32ff6c33c9SBruce Evans  */
33ff6c33c9SBruce Evans 
349ff712b0SMark Murray #include <sys/cdefs.h>
35ff6c33c9SBruce Evans 
369ff712b0SMark Murray __FBSDID("$FreeBSD$");
379ff712b0SMark Murray 
389ff712b0SMark Murray #ifdef lint
399ff712b0SMark Murray static const char sccsid[] = "@(#)cmds.c	8.2 (Berkeley) 4/29/95";
409ff712b0SMark Murray #endif
419ff712b0SMark Murray 
42ff6c33c9SBruce Evans #include <ctype.h>
439ff712b0SMark Murray #include <signal.h>
449ff712b0SMark Murray #include <stdlib.h>
45ff6c33c9SBruce Evans #include <string.h>
469ff712b0SMark Murray #include <unistd.h>
479ff712b0SMark Murray 
48ff6c33c9SBruce Evans #include "systat.h"
49ff6c33c9SBruce Evans #include "extern.h"
50ff6c33c9SBruce Evans 
51ff6c33c9SBruce Evans void
52ff6c33c9SBruce Evans command(cmd)
539ff712b0SMark Murray         const char *cmd;
54ff6c33c9SBruce Evans {
559ff712b0SMark Murray         struct cmdtab *p;
569ff712b0SMark Murray         char *cp, *tmpstr, *tmpstr1;
57ff6c33c9SBruce Evans 	int interval, omask;
58ff6c33c9SBruce Evans 
599ff712b0SMark Murray 	tmpstr = tmpstr1 = strdup(cmd);
60ff6c33c9SBruce Evans 	omask = sigblock(sigmask(SIGALRM));
619ff712b0SMark Murray         for (cp = tmpstr1; *cp && !isspace(*cp); cp++)
62ff6c33c9SBruce Evans                 ;
63ff6c33c9SBruce Evans         if (*cp)
64ff6c33c9SBruce Evans                 *cp++ = '\0';
659ff712b0SMark Murray 	if (*tmpstr1 == '\0')
66ff6c33c9SBruce Evans 		return;
67ff6c33c9SBruce Evans 	for (; *cp && isspace(*cp); cp++)
68ff6c33c9SBruce Evans 		;
699ff712b0SMark Murray         if (strcmp(tmpstr1, "quit") == 0 || strcmp(tmpstr1, "q") == 0)
70ff6c33c9SBruce Evans                 die(0);
719ff712b0SMark Murray 	if (strcmp(tmpstr1, "load") == 0) {
72ff6c33c9SBruce Evans 		load();
73ff6c33c9SBruce Evans 		goto done;
74ff6c33c9SBruce Evans 	}
759ff712b0SMark Murray         if (strcmp(tmpstr1, "stop") == 0) {
76ff6c33c9SBruce Evans                 alarm(0);
77ff6c33c9SBruce Evans                 mvaddstr(CMDLINE, 0, "Refresh disabled.");
78ff6c33c9SBruce Evans                 clrtoeol();
79ff6c33c9SBruce Evans 		goto done;
80ff6c33c9SBruce Evans         }
819ff712b0SMark Murray 	if (strcmp(tmpstr1, "help") == 0) {
829ff712b0SMark Murray 		int _col, _len;
83ff6c33c9SBruce Evans 
849ff712b0SMark Murray 		move(CMDLINE, _col = 0);
85ff6c33c9SBruce Evans 		for (p = cmdtab; p->c_name; p++) {
869ff712b0SMark Murray 			_len = strlen(p->c_name);
879ff712b0SMark Murray 			if (_col + _len > COLS)
88ff6c33c9SBruce Evans 				break;
899ff712b0SMark Murray 			addstr(p->c_name); _col += _len;
909ff712b0SMark Murray 			if (_col + 1 < COLS)
91ff6c33c9SBruce Evans 				addch(' ');
92ff6c33c9SBruce Evans 		}
93ff6c33c9SBruce Evans 		clrtoeol();
94ff6c33c9SBruce Evans 		goto done;
95ff6c33c9SBruce Evans 	}
969ff712b0SMark Murray 	interval = atoi(tmpstr1);
97ff6c33c9SBruce Evans         if (interval <= 0 &&
989ff712b0SMark Murray 	    (strcmp(tmpstr1, "start") == 0 || strcmp(tmpstr1, "interval") == 0)) {
99ff6c33c9SBruce Evans 		interval = *cp ? atoi(cp) : naptime;
100ff6c33c9SBruce Evans                 if (interval <= 0) {
101ff6c33c9SBruce Evans 			error("%d: bad interval.", interval);
102ff6c33c9SBruce Evans 			goto done;
103ff6c33c9SBruce Evans                 }
104ff6c33c9SBruce Evans 	}
105ff6c33c9SBruce Evans 	if (interval > 0) {
106ff6c33c9SBruce Evans                 alarm(0);
107ff6c33c9SBruce Evans                 naptime = interval;
108ff6c33c9SBruce Evans                 display(0);
109ff6c33c9SBruce Evans                 status();
110ff6c33c9SBruce Evans 		goto done;
111ff6c33c9SBruce Evans         }
1129ff712b0SMark Murray 	p = lookup(tmpstr1);
113ff6c33c9SBruce Evans 	if (p == (struct cmdtab *)-1) {
1149ff712b0SMark Murray 		error("%s: Ambiguous command.", tmpstr1);
115ff6c33c9SBruce Evans 		goto done;
116ff6c33c9SBruce Evans 	}
117ff6c33c9SBruce Evans         if (p) {
118ff6c33c9SBruce Evans                 if (curcmd == p)
119ff6c33c9SBruce Evans 			goto done;
120ff6c33c9SBruce Evans                 alarm(0);
121ff6c33c9SBruce Evans 		(*curcmd->c_close)(wnd);
122ff6c33c9SBruce Evans 		wnd = (*p->c_open)();
123ff6c33c9SBruce Evans 		if (wnd == 0) {
124ff6c33c9SBruce Evans 			error("Couldn't open new display");
125ff6c33c9SBruce Evans 			wnd = (*curcmd->c_open)();
126ff6c33c9SBruce Evans 			if (wnd == 0) {
127ff6c33c9SBruce Evans 				error("Couldn't change back to previous cmd");
128ff6c33c9SBruce Evans 				exit(1);
129ff6c33c9SBruce Evans 			}
130ff6c33c9SBruce Evans 			p = curcmd;
131ff6c33c9SBruce Evans 		}
132ff6c33c9SBruce Evans 		if ((p->c_flags & CF_INIT) == 0) {
133ff6c33c9SBruce Evans 			if ((*p->c_init)())
134ff6c33c9SBruce Evans 				p->c_flags |= CF_INIT;
135ff6c33c9SBruce Evans 			else
136ff6c33c9SBruce Evans 				goto done;
137ff6c33c9SBruce Evans 		}
138ff6c33c9SBruce Evans                 curcmd = p;
139ff6c33c9SBruce Evans 		labels();
140ff6c33c9SBruce Evans                 display(0);
141ff6c33c9SBruce Evans                 status();
142ff6c33c9SBruce Evans 		goto done;
143ff6c33c9SBruce Evans         }
1449ff712b0SMark Murray 	if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(tmpstr1, cp))
1459ff712b0SMark Murray 		error("%s: Unknown command.", tmpstr1);
146ff6c33c9SBruce Evans done:
147ff6c33c9SBruce Evans 	sigsetmask(omask);
1489ff712b0SMark Murray 	free(tmpstr);
149ff6c33c9SBruce Evans }
150ff6c33c9SBruce Evans 
151ff6c33c9SBruce Evans struct cmdtab *
152ff6c33c9SBruce Evans lookup(name)
1539ff712b0SMark Murray 	const char *name;
154ff6c33c9SBruce Evans {
1559ff712b0SMark Murray 	const char *p, *q;
1569ff712b0SMark Murray 	struct cmdtab *ct, *found;
1579ff712b0SMark Murray 	int nmatches, longest;
158ff6c33c9SBruce Evans 
159ff6c33c9SBruce Evans 	longest = 0;
160ff6c33c9SBruce Evans 	nmatches = 0;
161ff6c33c9SBruce Evans 	found = (struct cmdtab *) 0;
1629ff712b0SMark Murray 	for (ct = cmdtab; (p = ct->c_name); ct++) {
163ff6c33c9SBruce Evans 		for (q = name; *q == *p++; q++)
164ff6c33c9SBruce Evans 			if (*q == 0)		/* exact match? */
1659ff712b0SMark Murray 				return (ct);
166ff6c33c9SBruce Evans 		if (!*q) {			/* the name was a prefix */
167ff6c33c9SBruce Evans 			if (q - name > longest) {
168ff6c33c9SBruce Evans 				longest = q - name;
169ff6c33c9SBruce Evans 				nmatches = 1;
1709ff712b0SMark Murray 				found = ct;
171ff6c33c9SBruce Evans 			} else if (q - name == longest)
172ff6c33c9SBruce Evans 				nmatches++;
173ff6c33c9SBruce Evans 		}
174ff6c33c9SBruce Evans 	}
175237f5336SBruce Evans 	if (nmatches > 1)
176ff6c33c9SBruce Evans 		return ((struct cmdtab *)-1);
177ff6c33c9SBruce Evans 	return (found);
178ff6c33c9SBruce Evans }
179ff6c33c9SBruce Evans 
180ff6c33c9SBruce Evans void
181ff6c33c9SBruce Evans status()
182ff6c33c9SBruce Evans {
183ff6c33c9SBruce Evans 
184ff6c33c9SBruce Evans         error("Showing %s, refresh every %d seconds.",
185ff6c33c9SBruce Evans           curcmd->c_name, naptime);
186ff6c33c9SBruce Evans }
187ff6c33c9SBruce Evans 
188ff6c33c9SBruce Evans int
189ff6c33c9SBruce Evans prefix(s1, s2)
1909ff712b0SMark Murray         const char *s1, *s2;
191ff6c33c9SBruce Evans {
192ff6c33c9SBruce Evans 
193ff6c33c9SBruce Evans         while (*s1 == *s2) {
194ff6c33c9SBruce Evans                 if (*s1 == '\0')
195ff6c33c9SBruce Evans                         return (1);
196ff6c33c9SBruce Evans                 s1++, s2++;
197ff6c33c9SBruce Evans         }
198ff6c33c9SBruce Evans         return (*s1 == '\0');
199ff6c33c9SBruce Evans }
200