Commit | Line | Data |
---|---|---|
ba65e150 | 1 | /*- |
ea529f63 KB |
2 | * Copyright (c) 1980, 1992, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
ba65e150 | 4 | * |
1e3c68be | 5 | * %sccs.include.redist.c% |
07ed1e09 KM |
6 | */ |
7 | ||
612f3023 | 8 | #ifndef lint |
ea529f63 | 9 | static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) %G%"; |
ba65e150 | 10 | #endif /* not lint */ |
612f3023 | 11 | |
2dd98949 KB |
12 | #include <stdlib.h> |
13 | #include <unistd.h> | |
14 | #include <signal.h> | |
855ff304 | 15 | #include <ctype.h> |
2dd98949 KB |
16 | #include <string.h> |
17 | #include "systat.h" | |
18 | #include "extern.h" | |
612f3023 | 19 | |
2dd98949 | 20 | void |
612f3023 SL |
21 | command(cmd) |
22 | char *cmd; | |
23 | { | |
612f3023 | 24 | register struct cmdtab *p; |
2dd98949 | 25 | register char *cp; |
833d578b | 26 | int interval, omask; |
612f3023 | 27 | |
833d578b | 28 | omask = sigblock(sigmask(SIGALRM)); |
612f3023 SL |
29 | for (cp = cmd; *cp && !isspace(*cp); cp++) |
30 | ; | |
31 | if (*cp) | |
32 | *cp++ = '\0'; | |
acaaf0c4 SL |
33 | if (*cmd == '\0') |
34 | return; | |
855ff304 KM |
35 | for (; *cp && isspace(*cp); cp++) |
36 | ; | |
4fefc2e5 | 37 | if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0) |
2dd98949 | 38 | die(0); |
612f3023 SL |
39 | if (strcmp(cmd, "load") == 0) { |
40 | load(); | |
833d578b | 41 | goto done; |
612f3023 | 42 | } |
612f3023 SL |
43 | if (strcmp(cmd, "stop") == 0) { |
44 | alarm(0); | |
855ff304 | 45 | mvaddstr(CMDLINE, 0, "Refresh disabled."); |
612f3023 | 46 | clrtoeol(); |
833d578b | 47 | goto done; |
612f3023 | 48 | } |
9c0c7319 SL |
49 | if (strcmp(cmd, "help") == 0) { |
50 | int col, len; | |
612f3023 | 51 | |
9c0c7319 SL |
52 | move(CMDLINE, col = 0); |
53 | for (p = cmdtab; p->c_name; p++) { | |
54 | len = strlen(p->c_name); | |
55 | if (col + len > COLS) | |
56 | break; | |
57 | addstr(p->c_name); col += len; | |
58 | if (col + 1 < COLS) | |
59 | addch(' '); | |
60 | } | |
61 | clrtoeol(); | |
833d578b | 62 | goto done; |
9c0c7319 SL |
63 | } |
64 | interval = atoi(cmd); | |
65 | if (interval <= 0 && | |
66 | (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) { | |
67 | interval = *cp ? atoi(cp) : naptime; | |
68 | if (interval <= 0) { | |
69 | error("%d: bad interval.", interval); | |
833d578b | 70 | goto done; |
612f3023 | 71 | } |
9c0c7319 SL |
72 | } |
73 | if (interval > 0) { | |
612f3023 | 74 | alarm(0); |
9c0c7319 | 75 | naptime = interval; |
2dd98949 | 76 | display(0); |
612f3023 | 77 | status(); |
833d578b | 78 | goto done; |
612f3023 | 79 | } |
acaaf0c4 SL |
80 | p = lookup(cmd); |
81 | if (p == (struct cmdtab *)-1) { | |
82 | error("%s: Ambiguous command.", cmd); | |
833d578b | 83 | goto done; |
612f3023 | 84 | } |
acaaf0c4 SL |
85 | if (p) { |
86 | if (curcmd == p) | |
833d578b | 87 | goto done; |
acaaf0c4 SL |
88 | alarm(0); |
89 | (*curcmd->c_close)(wnd); | |
90 | wnd = (*p->c_open)(); | |
5d60dc8f SL |
91 | if (wnd == 0) { |
92 | error("Couldn't open new display"); | |
93 | wnd = (*curcmd->c_open)(); | |
94 | if (wnd == 0) { | |
95 | error("Couldn't change back to previous cmd"); | |
96 | exit(1); | |
97 | } | |
98 | p = curcmd; | |
99 | } | |
9c0c7319 | 100 | if ((p->c_flags & CF_INIT) == 0) { |
c0b7e584 JB |
101 | if ((*p->c_init)()) |
102 | p->c_flags |= CF_INIT; | |
103 | else | |
104 | goto done; | |
acaaf0c4 | 105 | } |
c0b7e584 | 106 | curcmd = p; |
acaaf0c4 | 107 | labels(); |
2dd98949 | 108 | display(0); |
acaaf0c4 | 109 | status(); |
833d578b | 110 | goto done; |
acaaf0c4 | 111 | } |
833d578b SL |
112 | if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp)) |
113 | error("%s: Unknown command.", cmd); | |
114 | done: | |
115 | sigsetmask(omask); | |
612f3023 SL |
116 | } |
117 | ||
118 | struct cmdtab * | |
4fefc2e5 | 119 | lookup(name) |
612f3023 SL |
120 | register char *name; |
121 | { | |
122 | register char *p, *q; | |
123 | register struct cmdtab *c, *found; | |
124 | register int nmatches, longest; | |
125 | ||
126 | longest = 0; | |
127 | nmatches = 0; | |
8b658b33 | 128 | found = (struct cmdtab *) 0; |
612f3023 SL |
129 | for (c = cmdtab; p = c->c_name; c++) { |
130 | for (q = name; *q == *p++; q++) | |
131 | if (*q == 0) /* exact match? */ | |
132 | return (c); | |
133 | if (!*q) { /* the name was a prefix */ | |
134 | if (q - name > longest) { | |
135 | longest = q - name; | |
136 | nmatches = 1; | |
137 | found = c; | |
138 | } else if (q - name == longest) | |
139 | nmatches++; | |
140 | } | |
141 | } | |
142 | if (nmatches > 1) | |
143 | return ((struct cmdtab *)-1); | |
144 | return (found); | |
145 | } | |
146 | ||
2dd98949 | 147 | void |
612f3023 SL |
148 | status() |
149 | { | |
150 | ||
855ff304 | 151 | error("Showing %s, refresh every %d seconds.", |
612f3023 | 152 | curcmd->c_name, naptime); |
612f3023 SL |
153 | } |
154 | ||
2dd98949 | 155 | int |
833d578b SL |
156 | prefix(s1, s2) |
157 | register char *s1, *s2; | |
158 | { | |
159 | ||
160 | while (*s1 == *s2) { | |
161 | if (*s1 == '\0') | |
162 | return (1); | |
163 | s1++, s2++; | |
164 | } | |
165 | return (*s1 == '\0'); | |
166 | } |