Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | char copyright[] = | |
36 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ | |
37 | All rights reserved.\n"; | |
38 | #endif /* not lint */ | |
39 | ||
40 | #ifndef lint | |
41 | static char sccsid[] = "@(#)timedc.c 2.10 (Berkeley) 3/5/91"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | #include "timedc.h" | |
45 | #include <signal.h> | |
46 | #include <ctype.h> | |
47 | #include <setjmp.h> | |
48 | #include <syslog.h> | |
49 | ||
50 | int top; | |
51 | int margc; | |
52 | int fromatty; | |
53 | char *margv[20]; | |
54 | char cmdline[200]; | |
55 | jmp_buf toplevel; | |
56 | void intr(); | |
57 | int priv_resources(); | |
58 | struct cmd *getcmd(); | |
59 | ||
60 | ||
61 | main(argc, argv) | |
62 | char *argv[]; | |
63 | { | |
64 | register struct cmd *c; | |
65 | ||
66 | openlog("timedc", LOG_ODELAY, LOG_AUTH); | |
67 | ||
68 | /* | |
69 | * security dictates! | |
70 | */ | |
71 | if (priv_resources() < 0) { | |
72 | fprintf(stderr, "Could not get privileged resources\n"); | |
73 | exit(1); | |
74 | } | |
75 | (void) setuid(getuid()); | |
76 | ||
77 | if (--argc > 0) { | |
78 | c = getcmd(*++argv); | |
79 | if (c == (struct cmd *)-1) { | |
80 | printf("?Ambiguous command\n"); | |
81 | exit(1); | |
82 | } | |
83 | if (c == 0) { | |
84 | printf("?Invalid command\n"); | |
85 | exit(1); | |
86 | } | |
87 | if (c->c_priv && getuid()) { | |
88 | printf("?Privileged command\n"); | |
89 | exit(1); | |
90 | } | |
91 | (*c->c_handler)(argc, argv); | |
92 | exit(0); | |
93 | } | |
94 | fromatty = isatty(fileno(stdin)); | |
95 | top = setjmp(toplevel) == 0; | |
96 | if (top) | |
97 | (void) signal(SIGINT, intr); | |
98 | for (;;) { | |
99 | cmdscanner(top); | |
100 | top = 1; | |
101 | } | |
102 | } | |
103 | ||
104 | void | |
105 | intr() | |
106 | { | |
107 | if (!fromatty) | |
108 | exit(0); | |
109 | longjmp(toplevel, 1); | |
110 | } | |
111 | ||
112 | /* | |
113 | * Command parser. | |
114 | */ | |
115 | cmdscanner(top) | |
116 | int top; | |
117 | { | |
118 | register struct cmd *c; | |
119 | extern int help(); | |
120 | ||
121 | if (!top) | |
122 | putchar('\n'); | |
123 | for (;;) { | |
124 | if (fromatty) { | |
125 | printf("timedc> "); | |
126 | (void) fflush(stdout); | |
127 | } | |
128 | if (fgets(cmdline, sizeof(cmdline), stdin) == 0) | |
129 | quit(); | |
130 | if (cmdline[0] == 0) | |
131 | break; | |
132 | makeargv(); | |
133 | c = getcmd(margv[0]); | |
134 | if (c == (struct cmd *)-1) { | |
135 | printf("?Ambiguous command\n"); | |
136 | continue; | |
137 | } | |
138 | if (c == 0) { | |
139 | printf("?Invalid command\n"); | |
140 | continue; | |
141 | } | |
142 | if (c->c_priv && getuid()) { | |
143 | printf("?Privileged command\n"); | |
144 | continue; | |
145 | } | |
146 | (*c->c_handler)(margc, margv); | |
147 | } | |
148 | longjmp(toplevel, 0); | |
149 | } | |
150 | ||
151 | struct cmd * | |
152 | getcmd(name) | |
153 | register char *name; | |
154 | { | |
155 | register char *p, *q; | |
156 | register struct cmd *c, *found; | |
157 | register int nmatches, longest; | |
158 | extern struct cmd cmdtab[]; | |
159 | extern int NCMDS; | |
160 | ||
161 | longest = 0; | |
162 | nmatches = 0; | |
163 | found = 0; | |
b636e600 AS |
164 | if (name == NULL) |
165 | return NULL; | |
15637ed4 RG |
166 | for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { |
167 | p = c->c_name; | |
168 | for (q = name; *q == *p++; q++) | |
169 | if (*q == 0) /* exact match? */ | |
170 | return(c); | |
171 | if (!*q) { /* the name was a prefix */ | |
172 | if (q - name > longest) { | |
173 | longest = q - name; | |
174 | nmatches = 1; | |
175 | found = c; | |
176 | } else if (q - name == longest) | |
177 | nmatches++; | |
178 | } | |
179 | } | |
180 | if (nmatches > 1) | |
181 | return((struct cmd *)-1); | |
182 | return(found); | |
183 | } | |
184 | ||
185 | /* | |
186 | * Slice a string up into argc/argv. | |
187 | */ | |
188 | makeargv() | |
189 | { | |
190 | register char *cp; | |
191 | register char **argp = margv; | |
192 | ||
193 | margc = 0; | |
194 | for (cp = cmdline; *cp;) { | |
195 | while (isspace(*cp)) | |
196 | cp++; | |
197 | if (*cp == '\0') | |
198 | break; | |
199 | *argp++ = cp; | |
200 | margc += 1; | |
201 | while (*cp != '\0' && !isspace(*cp)) | |
202 | cp++; | |
203 | if (*cp == '\0') | |
204 | break; | |
205 | *cp++ = '\0'; | |
206 | } | |
207 | *argp++ = 0; | |
208 | } | |
209 | ||
210 | #define HELPINDENT (sizeof ("directory")) | |
211 | ||
212 | /* | |
213 | * Help command. | |
214 | */ | |
215 | help(argc, argv) | |
216 | int argc; | |
217 | char *argv[]; | |
218 | { | |
219 | register struct cmd *c; | |
220 | extern struct cmd cmdtab[]; | |
221 | ||
222 | if (argc == 1) { | |
223 | register int i, j, w; | |
224 | int columns, width = 0, lines; | |
225 | extern int NCMDS; | |
226 | ||
227 | printf("Commands may be abbreviated. Commands are:\n\n"); | |
228 | for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { | |
229 | int len = strlen(c->c_name); | |
230 | ||
231 | if (len > width) | |
232 | width = len; | |
233 | } | |
234 | width = (width + 8) &~ 7; | |
235 | columns = 80 / width; | |
236 | if (columns == 0) | |
237 | columns = 1; | |
238 | lines = (NCMDS + columns - 1) / columns; | |
239 | for (i = 0; i < lines; i++) { | |
240 | for (j = 0; j < columns; j++) { | |
241 | c = cmdtab + j * lines + i; | |
242 | printf("%s", c->c_name); | |
243 | if (c + lines >= &cmdtab[NCMDS]) { | |
244 | printf("\n"); | |
245 | break; | |
246 | } | |
247 | w = strlen(c->c_name); | |
248 | while (w < width) { | |
249 | w = (w + 8) &~ 7; | |
250 | putchar('\t'); | |
251 | } | |
252 | } | |
253 | } | |
254 | return; | |
255 | } | |
256 | while (--argc > 0) { | |
257 | register char *arg; | |
258 | arg = *++argv; | |
259 | c = getcmd(arg); | |
260 | if (c == (struct cmd *)-1) | |
261 | printf("?Ambiguous help command %s\n", arg); | |
262 | else if (c == (struct cmd *)0) | |
263 | printf("?Invalid help command %s\n", arg); | |
264 | else | |
265 | printf("%-*s\t%s\n", HELPINDENT, | |
266 | c->c_name, c->c_help); | |
267 | } | |
268 | } |