timedc.c revision 30642
1/*-
2 * Copyright (c) 1985, 1993
3 *	The Regents of the University of California.  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
35static const char copyright[] =
36"@(#) Copyright (c) 1985, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)timedc.c	8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45	"$Id$";
46#endif /* not lint */
47
48#include "timedc.h"
49#include <ctype.h>
50#include <err.h>
51#include <setjmp.h>
52#include <signal.h>
53#include <stdlib.h>
54#include <strings.h>
55#include <syslog.h>
56#include <unistd.h>
57
58int trace = 0;
59FILE *fd = 0;
60int	margc;
61int	fromatty;
62char	*margv[20];
63char	cmdline[200];
64jmp_buf	toplevel;
65static struct cmd *getcmd __P((char *));
66
67int
68main(argc, argv)
69	int argc;
70	char *argv[];
71{
72	register struct cmd *c;
73
74	openlog("timedc", LOG_ODELAY, LOG_AUTH);
75
76	/*
77	 * security dictates!
78	 */
79	if (priv_resources() < 0)
80		errx(1, "could not get privileged resources");
81	(void) setuid(getuid());
82
83	if (--argc > 0) {
84		c = getcmd(*++argv);
85		if (c == (struct cmd *)-1) {
86			printf("?Ambiguous command\n");
87			exit(1);
88		}
89		if (c == 0) {
90			printf("?Invalid command\n");
91			exit(1);
92		}
93		if (c->c_priv && getuid()) {
94			printf("?Privileged command\n");
95			exit(1);
96		}
97		(*c->c_handler)(argc, argv);
98		exit(0);
99	}
100
101	fromatty = isatty(fileno(stdin));
102	if (setjmp(toplevel))
103		putchar('\n');
104	(void) signal(SIGINT, intr);
105	for (;;) {
106		if (fromatty) {
107			printf("timedc> ");
108			(void) fflush(stdout);
109		}
110		if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
111			quit();
112		if (cmdline[0] == 0)
113			break;
114		makeargv();
115		if (margv[0] == 0)
116			continue;
117		c = getcmd(margv[0]);
118		if (c == (struct cmd *)-1) {
119			printf("?Ambiguous command\n");
120			continue;
121		}
122		if (c == 0) {
123			printf("?Invalid command\n");
124			continue;
125		}
126		if (c->c_priv && getuid()) {
127			printf("?Privileged command\n");
128			continue;
129		}
130		(*c->c_handler)(margc, margv);
131	}
132	return 0;
133}
134
135void
136intr(signo)
137	int signo;
138{
139	if (!fromatty)
140		exit(0);
141	longjmp(toplevel, 1);
142}
143
144
145static struct cmd *
146getcmd(name)
147	char *name;
148{
149	register char *p, *q;
150	register struct cmd *c, *found;
151	register int nmatches, longest;
152	extern int NCMDS;
153
154	longest = 0;
155	nmatches = 0;
156	found = 0;
157	for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
158		p = c->c_name;
159		for (q = name; *q == *p++; q++)
160			if (*q == 0)		/* exact match? */
161				return(c);
162		if (!*q) {			/* the name was a prefix */
163			if (q - name > longest) {
164				longest = q - name;
165				nmatches = 1;
166				found = c;
167			} else if (q - name == longest)
168				nmatches++;
169		}
170	}
171	if (nmatches > 1)
172		return((struct cmd *)-1);
173	return(found);
174}
175
176/*
177 * Slice a string up into argc/argv.
178 */
179void
180makeargv()
181{
182	register char *cp;
183	register char **argp = margv;
184
185	margc = 0;
186	for (cp = cmdline; *cp;) {
187		while (isspace(*cp))
188			cp++;
189		if (*cp == '\0')
190			break;
191		*argp++ = cp;
192		margc += 1;
193		while (*cp != '\0' && !isspace(*cp))
194			cp++;
195		if (*cp == '\0')
196			break;
197		*cp++ = '\0';
198	}
199	*argp++ = 0;
200}
201
202#define HELPINDENT (sizeof ("directory"))
203
204/*
205 * Help command.
206 */
207void
208help(argc, argv)
209	int argc;
210	char *argv[];
211{
212	register struct cmd *c;
213
214	if (argc == 1) {
215		register int i, j, w;
216		int columns, width = 0, lines;
217		extern int NCMDS;
218
219		printf("Commands may be abbreviated.  Commands are:\n\n");
220		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
221			int len = strlen(c->c_name);
222
223			if (len > width)
224				width = len;
225		}
226		width = (width + 8) &~ 7;
227		columns = 80 / width;
228		if (columns == 0)
229			columns = 1;
230		lines = (NCMDS + columns - 1) / columns;
231		for (i = 0; i < lines; i++) {
232			for (j = 0; j < columns; j++) {
233				c = cmdtab + j * lines + i;
234				printf("%s", c->c_name);
235				if (c + lines >= &cmdtab[NCMDS]) {
236					printf("\n");
237					break;
238				}
239				w = strlen(c->c_name);
240				while (w < width) {
241					w = (w + 8) &~ 7;
242					putchar('\t');
243				}
244			}
245		}
246		return;
247	}
248	while (--argc > 0) {
249		register char *arg;
250		arg = *++argv;
251		c = getcmd(arg);
252		if (c == (struct cmd *)-1)
253			printf("?Ambiguous help command %s\n", arg);
254		else if (c == (struct cmd *)0)
255			printf("?Invalid help command %s\n", arg);
256		else
257			printf("%-*s\t%s\n", (int)HELPINDENT,
258				c->c_name, c->c_help);
259	}
260}
261