xref: /netbsd-src/usr.bin/systat/cmds.c (revision 8ff995f221c25ddfe01de917eaa76b6a5d2a82ec)
1*8ff995f2Schristos /*	$NetBSD: cmds.c,v 1.30 2016/10/22 22:02:55 christos Exp $	*/
22f3cb7aeSjtc 
312749a2aSjtc /*-
412749a2aSjtc  * Copyright (c) 1980, 1992, 1993
512749a2aSjtc  *	The Regents of the University of California.  All rights reserved.
612749a2aSjtc  *
712749a2aSjtc  * Redistribution and use in source and binary forms, with or without
812749a2aSjtc  * modification, are permitted provided that the following conditions
912749a2aSjtc  * are met:
1012749a2aSjtc  * 1. Redistributions of source code must retain the above copyright
1112749a2aSjtc  *    notice, this list of conditions and the following disclaimer.
1212749a2aSjtc  * 2. Redistributions in binary form must reproduce the above copyright
1312749a2aSjtc  *    notice, this list of conditions and the following disclaimer in the
1412749a2aSjtc  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1612749a2aSjtc  *    may be used to endorse or promote products derived from this software
1712749a2aSjtc  *    without specific prior written permission.
1812749a2aSjtc  *
1912749a2aSjtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2012749a2aSjtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2112749a2aSjtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2212749a2aSjtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2312749a2aSjtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2412749a2aSjtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2512749a2aSjtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2612749a2aSjtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2712749a2aSjtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2812749a2aSjtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2912749a2aSjtc  * SUCH DAMAGE.
3012749a2aSjtc  */
3112749a2aSjtc 
3259413993Smrg #include <sys/cdefs.h>
3312749a2aSjtc #ifndef lint
342f3cb7aeSjtc #if 0
35f1df59adSjtc static char sccsid[] = "@(#)cmds.c	8.2 (Berkeley) 4/29/95";
362f3cb7aeSjtc #endif
37*8ff995f2Schristos __RCSID("$NetBSD: cmds.c,v 1.30 2016/10/22 22:02:55 christos Exp $");
3812749a2aSjtc #endif /* not lint */
3912749a2aSjtc 
4012749a2aSjtc #include <ctype.h>
41828483a7Ssimonb #include <signal.h>
4212749a2aSjtc #include <string.h>
43828483a7Ssimonb #include <unistd.h>
44828483a7Ssimonb 
4512749a2aSjtc #include "systat.h"
4612749a2aSjtc #include "extern.h"
4712749a2aSjtc 
48fc391547Sad void	switch_mode(struct mode *p);
49099afa19Sjwise 
5012749a2aSjtc void
command(char * cmd)51fc391547Sad command(char *cmd)
5212749a2aSjtc {
5306f37661Sjwise 	struct command *c;
5406f37661Sjwise 	struct mode *p;
55b037ddd3Sjwise 	char *args;
5612749a2aSjtc 
576be16e19Sad 	if (cmd[0] == '\0')
586be16e19Sad 		return;
596be16e19Sad 
604e87b9d4Smycroft 	args = cmd;
614e87b9d4Smycroft 	cmd = strsep(&args, " \t");
6212749a2aSjtc 
634c0fbd4cSjwise 	if (curmode->c_commands) {
644c0fbd4cSjwise 		for (c = curmode->c_commands; c->c_name; c++) {
65ad285dbfSjwise 			if (strstr(c->c_name, cmd) == c->c_name) {
66b037ddd3Sjwise 				(c->c_cmd)(args);
674c0fbd4cSjwise 				goto done;
684c0fbd4cSjwise 			}
694c0fbd4cSjwise 		}
704c0fbd4cSjwise 	}
714c0fbd4cSjwise 
7206f37661Sjwise 	for (c = global_commands; c->c_name; c++) {
73ad285dbfSjwise 		if (strstr(c->c_name, cmd) == c->c_name) {
74b037ddd3Sjwise 			(c->c_cmd)(args);
7512749a2aSjtc 			goto done;
7612749a2aSjtc 		}
7706f37661Sjwise 	}
7806f37661Sjwise 
79099afa19Sjwise 	for (p = modes; p->c_name; p++) {
80ad285dbfSjwise 		if (strstr(p->c_name, cmd) == p->c_name) {
81099afa19Sjwise 			switch_mode(p);
8212749a2aSjtc 			goto done;
8312749a2aSjtc 		}
8412749a2aSjtc 	}
85099afa19Sjwise 
86cf09d2b1Sdsl 	if (isdigit((unsigned char)cmd[0])) {
87b037ddd3Sjwise 		global_interval(cmd);
88b037ddd3Sjwise 		goto done;
89b037ddd3Sjwise 	}
90b037ddd3Sjwise 
91099afa19Sjwise 	error("%s: Unknown command.", cmd);
92099afa19Sjwise done:
93eb8fd502Smycroft 	;
9412749a2aSjtc }
95099afa19Sjwise 
96099afa19Sjwise void
switch_mode(struct mode * p)97fc391547Sad switch_mode(struct mode *p)
98099afa19Sjwise {
99f70b9a57Sitojun 	int switchfail;
100e80cff47Shubertf 	struct mode *r;
101f70b9a57Sitojun 
102f70b9a57Sitojun 	switchfail = 0;
103e80cff47Shubertf 	r = p;
104f70b9a57Sitojun 
1056be16e19Sad 	if (curmode == p) {
1066be16e19Sad 		status();
107099afa19Sjwise 		return;
1086be16e19Sad 	}
109099afa19Sjwise 
11012749a2aSjtc 	alarm(0);
11106f37661Sjwise 	(*curmode->c_close)(wnd);
11212749a2aSjtc 	wnd = (*p->c_open)();
11312749a2aSjtc 	if (wnd == 0) {
11406f37661Sjwise 		wnd = (*curmode->c_open)();
11512749a2aSjtc 		if (wnd == 0) {
116099afa19Sjwise 			error("Couldn't change back to previous mode");
117850fb37fSjwise 			die(0);
11812749a2aSjtc 		}
119099afa19Sjwise 
12006f37661Sjwise 		p = curmode;
121f70b9a57Sitojun 		switchfail++;
12212749a2aSjtc 	}
123099afa19Sjwise 
12412749a2aSjtc 	if ((p->c_flags & CF_INIT) == 0) {
12512749a2aSjtc 		if ((*p->c_init)())
12612749a2aSjtc 			p->c_flags |= CF_INIT;
127f70b9a57Sitojun 		else {
128f70b9a57Sitojun 			(*p->c_close)(wnd);
129f70b9a57Sitojun 			wnd = (*curmode->c_open)();
130f70b9a57Sitojun 			p = curmode;
131f70b9a57Sitojun 			switchfail++;
132f70b9a57Sitojun 		}
13312749a2aSjtc 	}
134099afa19Sjwise 
13506f37661Sjwise 	curmode = p;
13612749a2aSjtc 	labels();
13712749a2aSjtc 	display(0);
138e80cff47Shubertf 	if (switchfail && !allflag)
139f70b9a57Sitojun 		error("Couldn't switch mode, back to %s", curmode->c_name);
140e80cff47Shubertf 	else {
141e80cff47Shubertf 		if (switchfail && allflag) {
142e80cff47Shubertf 			r++;
143e80cff47Shubertf 			switch_mode(r);
144e80cff47Shubertf 		} else {
14512749a2aSjtc 			status();
14612749a2aSjtc 		}
147e80cff47Shubertf 	}
148e80cff47Shubertf }
14912749a2aSjtc 
15012749a2aSjtc void
status(void)151fc391547Sad status(void)
15212749a2aSjtc {
153*8ff995f2Schristos 	error("Showing %s, refresh every %g seconds.", curmode->c_name, naptime);
15412749a2aSjtc }
155bd7662dcSscole 
156bd7662dcSscole int
prefix(const char * s1,const char * s2)157bd7662dcSscole prefix(const char *s1, const char *s2)
158bd7662dcSscole {
159bd7662dcSscole 
160bd7662dcSscole 	while (*s1 == *s2) {
161bd7662dcSscole 		if (*s1 == '\0')
162bd7662dcSscole 			return (1);
163bd7662dcSscole 		s1++, s2++;
164bd7662dcSscole 	}
165bd7662dcSscole 	return (*s1 == '\0');
166bd7662dcSscole }
167