xref: /freebsd/usr.bin/systat/cmds.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
1ff6c33c9SBruce Evans /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4ff6c33c9SBruce Evans  * Copyright (c) 1980, 1992, 1993
5ff6c33c9SBruce Evans  *	The Regents of the University of California.  All rights reserved.
6ff6c33c9SBruce Evans  *
7ff6c33c9SBruce Evans  * Redistribution and use in source and binary forms, with or without
8ff6c33c9SBruce Evans  * modification, are permitted provided that the following conditions
9ff6c33c9SBruce Evans  * are met:
10ff6c33c9SBruce Evans  * 1. Redistributions of source code must retain the above copyright
11ff6c33c9SBruce Evans  *    notice, this list of conditions and the following disclaimer.
12ff6c33c9SBruce Evans  * 2. Redistributions in binary form must reproduce the above copyright
13ff6c33c9SBruce Evans  *    notice, this list of conditions and the following disclaimer in the
14ff6c33c9SBruce Evans  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16ff6c33c9SBruce Evans  *    may be used to endorse or promote products derived from this software
17ff6c33c9SBruce Evans  *    without specific prior written permission.
18ff6c33c9SBruce Evans  *
19ff6c33c9SBruce Evans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20ff6c33c9SBruce Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ff6c33c9SBruce Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ff6c33c9SBruce Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23ff6c33c9SBruce Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ff6c33c9SBruce Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ff6c33c9SBruce Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ff6c33c9SBruce Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ff6c33c9SBruce Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ff6c33c9SBruce Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ff6c33c9SBruce Evans  * SUCH DAMAGE.
30ff6c33c9SBruce Evans  */
31ff6c33c9SBruce Evans 
329ff712b0SMark Murray 
339ff712b0SMark Murray 
348b3daf89SAlexander V. Chernikov #include <sys/param.h>
358b3daf89SAlexander V. Chernikov 
36ff6c33c9SBruce Evans #include <ctype.h>
379ff712b0SMark Murray #include <signal.h>
389ff712b0SMark Murray #include <stdlib.h>
39ff6c33c9SBruce Evans #include <string.h>
409ff712b0SMark Murray #include <unistd.h>
419ff712b0SMark Murray 
42ff6c33c9SBruce Evans #include "systat.h"
43ff6c33c9SBruce Evans #include "extern.h"
44ff6c33c9SBruce Evans 
45ff6c33c9SBruce Evans void
command(const char * cmd)4693b9f504SXin LI command(const char *cmd)
47ff6c33c9SBruce Evans {
489ff712b0SMark Murray 	struct cmdtab *p;
499ff712b0SMark Murray 	char *cp, *tmpstr, *tmpstr1;
508b3daf89SAlexander V. Chernikov 	double t;
51ff6c33c9SBruce Evans 
529ff712b0SMark Murray 	tmpstr = tmpstr1 = strdup(cmd);
539ff712b0SMark Murray 	for (cp = tmpstr1; *cp && !isspace(*cp); cp++)
54ff6c33c9SBruce Evans 		;
55ff6c33c9SBruce Evans 	if (*cp)
56ff6c33c9SBruce Evans 		*cp++ = '\0';
579ff712b0SMark Murray 	if (*tmpstr1 == '\0')
58*80cb4b11SXin LI 		goto done;
59ff6c33c9SBruce Evans 	for (; *cp && isspace(*cp); cp++)
60ff6c33c9SBruce Evans 		;
619ff712b0SMark Murray 	if (strcmp(tmpstr1, "quit") == 0 || strcmp(tmpstr1, "q") == 0)
62ff6c33c9SBruce Evans 		die(0);
639ff712b0SMark Murray 	if (strcmp(tmpstr1, "load") == 0) {
64ff6c33c9SBruce Evans 		load();
65ff6c33c9SBruce Evans 		goto done;
66ff6c33c9SBruce Evans 	}
679ff712b0SMark Murray 	if (strcmp(tmpstr1, "stop") == 0) {
688b3daf89SAlexander V. Chernikov 		delay = 0;
69ff6c33c9SBruce Evans 		mvaddstr(CMDLINE, 0, "Refresh disabled.");
70ff6c33c9SBruce Evans 		clrtoeol();
71ff6c33c9SBruce Evans 		goto done;
72ff6c33c9SBruce Evans 	}
739ff712b0SMark Murray 	if (strcmp(tmpstr1, "help") == 0) {
749ff712b0SMark Murray 		int _col, _len;
75ff6c33c9SBruce Evans 
769ff712b0SMark Murray 		move(CMDLINE, _col = 0);
77ff6c33c9SBruce Evans 		for (p = cmdtab; p->c_name; p++) {
789ff712b0SMark Murray 			_len = strlen(p->c_name);
799ff712b0SMark Murray 			if (_col + _len > COLS)
80ff6c33c9SBruce Evans 				break;
819ff712b0SMark Murray 			addstr(p->c_name); _col += _len;
829ff712b0SMark Murray 			if (_col + 1 < COLS)
83ff6c33c9SBruce Evans 				addch(' ');
84ff6c33c9SBruce Evans 		}
85ff6c33c9SBruce Evans 		clrtoeol();
86ff6c33c9SBruce Evans 		goto done;
87ff6c33c9SBruce Evans 	}
888b3daf89SAlexander V. Chernikov 	t = strtod(tmpstr1, NULL) * 1000000.0;
898b3daf89SAlexander V. Chernikov 	if (t > 0 && t < (double)UINT_MAX)
908b3daf89SAlexander V. Chernikov 		delay = (unsigned int)t;
918b3daf89SAlexander V. Chernikov 	if ((t <= 0 || t > (double)UINT_MAX) &&
928b3daf89SAlexander V. Chernikov 	    (strcmp(tmpstr1, "start") == 0 ||
938b3daf89SAlexander V. Chernikov 	    strcmp(tmpstr1, "interval") == 0)) {
948b3daf89SAlexander V. Chernikov 		if (*cp != '\0') {
958b3daf89SAlexander V. Chernikov 			t = strtod(cp, NULL) * 1000000.0;
968b3daf89SAlexander V. Chernikov 			if (t <= 0 || t >= (double)UINT_MAX) {
978b3daf89SAlexander V. Chernikov 				error("%d: bad interval.", (int)t);
98ff6c33c9SBruce Evans 				goto done;
99ff6c33c9SBruce Evans 			}
100ff6c33c9SBruce Evans 		}
1018b3daf89SAlexander V. Chernikov 	}
1028b3daf89SAlexander V. Chernikov 	if (t > 0) {
1038b3daf89SAlexander V. Chernikov 		delay = (unsigned int)t;
1048b3daf89SAlexander V. Chernikov 		display();
105ff6c33c9SBruce Evans 		status();
106ff6c33c9SBruce Evans 		goto done;
107ff6c33c9SBruce Evans 	}
1089ff712b0SMark Murray 	p = lookup(tmpstr1);
109ff6c33c9SBruce Evans 	if (p == (struct cmdtab *)-1) {
1109ff712b0SMark Murray 		error("%s: Ambiguous command.", tmpstr1);
111ff6c33c9SBruce Evans 		goto done;
112ff6c33c9SBruce Evans 	}
113ff6c33c9SBruce Evans 	if (p) {
114ff6c33c9SBruce Evans 		if (curcmd == p)
115ff6c33c9SBruce Evans 			goto done;
116ff6c33c9SBruce Evans 		(*curcmd->c_close)(wnd);
117319e2a24SPoul-Henning Kamp 		curcmd->c_flags &= ~CF_INIT;
118ff6c33c9SBruce Evans 		wnd = (*p->c_open)();
11961c2ed54SMarcelo Araujo 		if (wnd == NULL) {
120ff6c33c9SBruce Evans 			error("Couldn't open new display");
121ff6c33c9SBruce Evans 			wnd = (*curcmd->c_open)();
12261c2ed54SMarcelo Araujo 			if (wnd == NULL) {
123ff6c33c9SBruce Evans 				error("Couldn't change back to previous cmd");
124ff6c33c9SBruce Evans 				exit(1);
125ff6c33c9SBruce Evans 			}
126ff6c33c9SBruce Evans 			p = curcmd;
127ff6c33c9SBruce Evans 		}
128ff6c33c9SBruce Evans 		if ((p->c_flags & CF_INIT) == 0) {
129ff6c33c9SBruce Evans 			if ((*p->c_init)())
130ff6c33c9SBruce Evans 				p->c_flags |= CF_INIT;
131ff6c33c9SBruce Evans 			else
132ff6c33c9SBruce Evans 				goto done;
133ff6c33c9SBruce Evans 		}
134ff6c33c9SBruce Evans 		curcmd = p;
135ff6c33c9SBruce Evans 		labels();
1368b3daf89SAlexander V. Chernikov 		display();
137ff6c33c9SBruce Evans 		status();
138ff6c33c9SBruce Evans 		goto done;
139ff6c33c9SBruce Evans 	}
14061c2ed54SMarcelo Araujo 	if (curcmd->c_cmd == NULL || !(*curcmd->c_cmd)(tmpstr1, cp))
1419ff712b0SMark Murray 		error("%s: Unknown command.", tmpstr1);
142ff6c33c9SBruce Evans done:
1439ff712b0SMark Murray 	free(tmpstr);
144ff6c33c9SBruce Evans }
145ff6c33c9SBruce Evans 
146ff6c33c9SBruce Evans struct cmdtab *
lookup(const char * name)14793b9f504SXin LI lookup(const char *name)
148ff6c33c9SBruce Evans {
1499ff712b0SMark Murray 	const char *p, *q;
1509ff712b0SMark Murray 	struct cmdtab *ct, *found;
1519ff712b0SMark Murray 	int nmatches, longest;
152ff6c33c9SBruce Evans 
153ff6c33c9SBruce Evans 	longest = 0;
154ff6c33c9SBruce Evans 	nmatches = 0;
155ff6c33c9SBruce Evans 	found = (struct cmdtab *) 0;
1569ff712b0SMark Murray 	for (ct = cmdtab; (p = ct->c_name); ct++) {
157ff6c33c9SBruce Evans 		for (q = name; *q == *p++; q++)
158ff6c33c9SBruce Evans 			if (*q == 0)		/* exact match? */
1599ff712b0SMark Murray 				return (ct);
160ff6c33c9SBruce Evans 		if (!*q) {			/* the name was a prefix */
161ff6c33c9SBruce Evans 			if (q - name > longest) {
162ff6c33c9SBruce Evans 				longest = q - name;
163ff6c33c9SBruce Evans 				nmatches = 1;
1649ff712b0SMark Murray 				found = ct;
165ff6c33c9SBruce Evans 			} else if (q - name == longest)
166ff6c33c9SBruce Evans 				nmatches++;
167ff6c33c9SBruce Evans 		}
168ff6c33c9SBruce Evans 	}
169237f5336SBruce Evans 	if (nmatches > 1)
170ff6c33c9SBruce Evans 		return ((struct cmdtab *)-1);
171ff6c33c9SBruce Evans 	return (found);
172ff6c33c9SBruce Evans }
173ff6c33c9SBruce Evans 
174ff6c33c9SBruce Evans void
status(void)17593b9f504SXin LI status(void)
176ff6c33c9SBruce Evans {
177ff6c33c9SBruce Evans 
178ff6c33c9SBruce Evans 	error("Showing %s, refresh every %d seconds.",
1798b3daf89SAlexander V. Chernikov 	  curcmd->c_name, delay / 1000000);
180ff6c33c9SBruce Evans }
181ff6c33c9SBruce Evans 
182ff6c33c9SBruce Evans int
prefix(const char * s1,const char * s2)18393b9f504SXin LI prefix(const char *s1, const char *s2)
184ff6c33c9SBruce Evans {
185ff6c33c9SBruce Evans 
186ff6c33c9SBruce Evans 	while (*s1 == *s2) {
187ff6c33c9SBruce Evans 		if (*s1 == '\0')
188ff6c33c9SBruce Evans 			return (1);
189ff6c33c9SBruce Evans 		s1++, s2++;
190ff6c33c9SBruce Evans 	}
191ff6c33c9SBruce Evans 	return (*s1 == '\0');
192ff6c33c9SBruce Evans }
193