Must distinguish between "ambiguous" and "unknown" commands.
[unix-history] / usr / src / usr.bin / systat / cmds.c
CommitLineData
07ed1e09
KM
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
612f3023 7#ifndef lint
8b658b33 8static char sccsid[] = "@(#)cmds.c 5.5 (Berkeley) %G%";
07ed1e09 9#endif not lint
612f3023
SL
10
11/*
12 * Command support.
13 */
14
15#include "systat.h"
855ff304 16#include <ctype.h>
612f3023
SL
17
18command(cmd)
19 char *cmd;
20{
21 register char *cp;
22 register struct cmdtab *p;
833d578b 23 int interval, omask;
88e200ad 24 extern (*sigtstpdfl)();
612f3023 25
833d578b 26 omask = sigblock(sigmask(SIGALRM));
612f3023
SL
27 for (cp = cmd; *cp && !isspace(*cp); cp++)
28 ;
29 if (*cp)
30 *cp++ = '\0';
acaaf0c4
SL
31 if (*cmd == '\0')
32 return;
855ff304
KM
33 for (; *cp && isspace(*cp); cp++)
34 ;
4fefc2e5 35 if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0)
612f3023 36 die();
612f3023
SL
37 if (strcmp(cmd, "load") == 0) {
38 load();
833d578b 39 goto done;
612f3023 40 }
612f3023
SL
41 if (strcmp(cmd, "stop") == 0) {
42 alarm(0);
855ff304 43 mvaddstr(CMDLINE, 0, "Refresh disabled.");
612f3023 44 clrtoeol();
833d578b 45 goto done;
612f3023 46 }
9c0c7319
SL
47 if (strcmp(cmd, "help") == 0) {
48 int col, len;
612f3023 49
9c0c7319
SL
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();
833d578b 60 goto done;
9c0c7319
SL
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);
833d578b 68 goto done;
612f3023 69 }
9c0c7319
SL
70 }
71 if (interval > 0) {
612f3023 72 alarm(0);
9c0c7319 73 naptime = interval;
612f3023
SL
74 display();
75 status();
833d578b 76 goto done;
612f3023 77 }
acaaf0c4
SL
78 p = lookup(cmd);
79 if (p == (struct cmdtab *)-1) {
80 error("%s: Ambiguous command.", cmd);
833d578b 81 goto done;
612f3023 82 }
acaaf0c4
SL
83 if (p) {
84 if (curcmd == p)
833d578b 85 goto done;
acaaf0c4
SL
86 alarm(0);
87 (*curcmd->c_close)(wnd);
88 wnd = (*p->c_open)();
5d60dc8f
SL
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 }
9c0c7319 98 if ((p->c_flags & CF_INIT) == 0) {
c0b7e584
JB
99 if ((*p->c_init)())
100 p->c_flags |= CF_INIT;
101 else
102 goto done;
acaaf0c4 103 }
c0b7e584 104 curcmd = p;
acaaf0c4
SL
105 labels();
106 display();
107 status();
833d578b 108 goto done;
acaaf0c4 109 }
833d578b
SL
110 if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp))
111 error("%s: Unknown command.", cmd);
112done:
113 sigsetmask(omask);
612f3023
SL
114}
115
116struct cmdtab *
4fefc2e5 117lookup(name)
612f3023
SL
118 register char *name;
119{
120 register char *p, *q;
121 register struct cmdtab *c, *found;
122 register int nmatches, longest;
123
124 longest = 0;
125 nmatches = 0;
8b658b33 126 found = (struct cmdtab *) 0;
612f3023
SL
127 for (c = cmdtab; p = c->c_name; c++) {
128 for (q = name; *q == *p++; q++)
129 if (*q == 0) /* exact match? */
130 return (c);
131 if (!*q) { /* the name was a prefix */
132 if (q - name > longest) {
133 longest = q - name;
134 nmatches = 1;
135 found = c;
136 } else if (q - name == longest)
137 nmatches++;
138 }
139 }
140 if (nmatches > 1)
141 return ((struct cmdtab *)-1);
142 return (found);
143}
144
145status()
146{
147
855ff304 148 error("Showing %s, refresh every %d seconds.",
612f3023 149 curcmd->c_name, naptime);
612f3023
SL
150}
151
152suspend()
153{
154 int oldmask;
155
156 alarm(0);
855ff304 157 move(CMDLINE, 0);
612f3023
SL
158 refresh();
159 echo();
160 nocrmode();
88e200ad 161 signal(SIGTSTP, sigtstpdfl);
612f3023
SL
162 oldmask = sigsetmask(0);
163 kill(getpid(), SIGTSTP);
164 sigsetmask(oldmask);
165 signal(SIGTSTP, suspend);
166 crmode();
167 noecho();
855ff304 168 move(CMDLINE, col);
612f3023
SL
169 alarm(naptime);
170}
833d578b
SL
171
172prefix(s1, s2)
173 register char *s1, *s2;
174{
175
176 while (*s1 == *s2) {
177 if (*s1 == '\0')
178 return (1);
179 s1++, s2++;
180 }
181 return (*s1 == '\0');
182}