xref: /original-bsd/usr.bin/systat/cmds.c (revision ec7df300)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)cmds.c	5.1 (Berkeley) 05/30/85";
9 #endif not lint
10 
11 /*
12  * Command support.
13  */
14 
15 #include "systat.h"
16 #include <ctype.h>
17 
18 command(cmd)
19         char *cmd;
20 {
21         register char *cp;
22         register struct cmdtab *p;
23 	int interval, omask;
24         char *arg;
25 
26 	omask = sigblock(sigmask(SIGALRM));
27         for (cp = cmd; *cp && !isspace(*cp); cp++)
28                 ;
29         if (*cp)
30                 *cp++ = '\0';
31 	if (*cmd == '\0')
32 		return;
33 	for (; *cp && isspace(*cp); cp++)
34 		;
35         if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0)
36                 die();
37 	if (strcmp(cmd, "load") == 0) {
38 		load();
39 		goto done;
40 	}
41         if (strcmp(cmd, "stop") == 0) {
42                 alarm(0);
43                 mvaddstr(CMDLINE, 0, "Refresh disabled.");
44                 clrtoeol();
45 		goto done;
46         }
47 	if (strcmp(cmd, "help") == 0) {
48 		int col, len;
49 
50 		move(CMDLINE, col = 0);
51 		for (p = cmdtab; p->c_name; p++) {
52 			len = strlen(p->c_name);
53 			if (col + len > COLS)
54 				break;
55 			addstr(p->c_name); col += len;
56 			if (col + 1 < COLS)
57 				addch(' ');
58 		}
59 		clrtoeol();
60 		goto done;
61 	}
62 	interval = atoi(cmd);
63         if (interval <= 0 &&
64 	    (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
65 		interval = *cp ? atoi(cp) : naptime;
66                 if (interval <= 0) {
67 			error("%d: bad interval.", interval);
68 			goto done;
69                 }
70 	}
71 	if (interval > 0) {
72                 alarm(0);
73                 naptime = interval;
74                 display();
75                 status();
76 		goto done;
77         }
78 	p = lookup(cmd);
79 	if (p == (struct cmdtab *)-1) {
80 		error("%s: Ambiguous command.", cmd);
81 		goto done;
82 	}
83         if (p) {
84                 if (curcmd == p)
85 			goto done;
86                 alarm(0);
87 		(*curcmd->c_close)(wnd);
88 		wnd = (*p->c_open)();
89 		if (wnd == 0) {
90 			error("Couldn't open new display");
91 			wnd = (*curcmd->c_open)();
92 			if (wnd == 0) {
93 				error("Couldn't change back to previous cmd");
94 				exit(1);
95 			}
96 			p = curcmd;
97 		}
98                 curcmd = p;
99 		if ((p->c_flags & CF_INIT) == 0) {
100 			(*p->c_init)();
101 			p->c_flags |= CF_INIT;
102 		}
103 		labels();
104                 display();
105                 status();
106 		goto done;
107         }
108 	if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp))
109 		error("%s: Unknown command.", cmd);
110 done:
111 	sigsetmask(omask);
112 }
113 
114 struct cmdtab *
115 lookup(name)
116 	register char *name;
117 {
118 	register char *p, *q;
119 	register struct cmdtab *c, *found;
120 	register int nmatches, longest;
121 
122 	longest = 0;
123 	nmatches = 0;
124 	found = 0;
125 	for (c = cmdtab; p = c->c_name; c++) {
126 		for (q = name; *q == *p++; q++)
127 			if (*q == 0)		/* exact match? */
128 				return (c);
129 		if (!*q) {			/* the name was a prefix */
130 			if (q - name > longest) {
131 				longest = q - name;
132 				nmatches = 1;
133 				found = c;
134 			} else if (q - name == longest)
135 				nmatches++;
136 		}
137 	}
138 	if (nmatches > 1)
139 		return ((struct cmdtab *)-1);
140 	return (found);
141 }
142 
143 status()
144 {
145 
146         error("Showing %s, refresh every %d seconds.",
147           curcmd->c_name, naptime);
148 }
149 
150 suspend()
151 {
152         int oldmask;
153 
154 	alarm(0);
155         move(CMDLINE, 0);
156         refresh();
157         echo();
158         nocrmode();
159         signal(SIGTSTP, SIG_DFL);
160         oldmask = sigsetmask(0);
161         kill(getpid(), SIGTSTP);
162         sigsetmask(oldmask);
163         signal(SIGTSTP, suspend);
164         crmode();
165         noecho();
166         move(CMDLINE, col);
167         wrefresh(curscr);
168 	alarm(naptime);
169 }
170 
171 prefix(s1, s2)
172         register char *s1, *s2;
173 {
174 
175         while (*s1 == *s2) {
176                 if (*s1 == '\0')
177                         return (1);
178                 s1++, s2++;
179         }
180         return (*s1 == '\0');
181 }
182