xref: /csrg-svn/usr.sbin/timed/timedc/timedc.c (revision 33123)
123670Sgusella /*
223670Sgusella  * Copyright (c) 1983 Regents of the University of California.
3*33123Sbostic  * All rights reserved.
4*33123Sbostic  *
5*33123Sbostic  * Redistribution and use in source and binary forms are permitted
6*33123Sbostic  * provided that this notice is preserved and that due credit is given
7*33123Sbostic  * to the University of California at Berkeley. The name of the University
8*33123Sbostic  * may not be used to endorse or promote products derived from this
9*33123Sbostic  * software without specific prior written permission. This software
10*33123Sbostic  * is provided ``as is'' without express or implied warranty.
1123670Sgusella  */
1223670Sgusella 
1323670Sgusella #ifndef lint
1423670Sgusella char copyright[] =
1523670Sgusella "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1623670Sgusella  All rights reserved.\n";
17*33123Sbostic #endif /* not lint */
1823670Sgusella 
1923670Sgusella #ifndef lint
20*33123Sbostic static char sccsid[] = "@(#)timedc.c	2.4 (Berkeley) 12/23/87";
21*33123Sbostic #endif /* not lint */
2223670Sgusella 
2323670Sgusella #include "timedc.h"
2423670Sgusella #include <signal.h>
2523670Sgusella #include <ctype.h>
2623670Sgusella #include <setjmp.h>
2724908Sbloom #include <syslog.h>
2823670Sgusella 
2923670Sgusella int	top;
3023670Sgusella int	margc;
3123670Sgusella int	fromatty;
3223670Sgusella char	*margv[20];
3323670Sgusella char	cmdline[200];
3423670Sgusella jmp_buf	toplevel;
3523670Sgusella int	intr();
3623670Sgusella int priv_resources();
3723670Sgusella struct	cmd *getcmd();
3823670Sgusella 
3923670Sgusella 
4023670Sgusella main(argc, argv)
4123670Sgusella 	char *argv[];
4223670Sgusella {
4323670Sgusella 	register struct cmd *c;
4423670Sgusella 
4524872Seric 	openlog("timedc", LOG_ODELAY, LOG_AUTH);
4624872Seric 
4723670Sgusella 	/*
4823670Sgusella 	 * security dictates!
4923670Sgusella 	 */
5023670Sgusella 	if (priv_resources() < 0) {
5123670Sgusella 		fprintf(stderr, "Could not get priviledged resources\n");
5223670Sgusella 		exit(1);
5323670Sgusella 	}
5423670Sgusella 	(void) setuid(getuid());
5523670Sgusella 
5623670Sgusella 	if (--argc > 0) {
5723670Sgusella 		c = getcmd(*++argv);
5823670Sgusella 		if (c == (struct cmd *)-1) {
5923670Sgusella 			printf("?Ambiguous command\n");
6023670Sgusella 			exit(1);
6123670Sgusella 		}
6223670Sgusella 		if (c == 0) {
6323670Sgusella 			printf("?Invalid command\n");
6423670Sgusella 			exit(1);
6523670Sgusella 		}
6629348Sbloom 		if (c->c_priv && getuid()) {
6729348Sbloom 			printf("?Privileged command\n");
6829348Sbloom 			exit(1);
6929348Sbloom 		}
7023670Sgusella 		(*c->c_handler)(argc, argv);
7123670Sgusella 		exit(0);
7223670Sgusella 	}
7323670Sgusella 	fromatty = isatty(fileno(stdin));
7423670Sgusella 	top = setjmp(toplevel) == 0;
7523670Sgusella 	if (top)
7623670Sgusella 		(void) signal(SIGINT, intr);
7723670Sgusella 	for (;;) {
7823670Sgusella 		cmdscanner(top);
7923670Sgusella 		top = 1;
8023670Sgusella 	}
8123670Sgusella }
8223670Sgusella 
8323670Sgusella intr()
8423670Sgusella {
8523670Sgusella 	if (!fromatty)
8623670Sgusella 		exit(0);
8723670Sgusella 	longjmp(toplevel, 1);
8823670Sgusella }
8923670Sgusella 
9023670Sgusella /*
9123670Sgusella  * Command parser.
9223670Sgusella  */
9323670Sgusella cmdscanner(top)
9423670Sgusella 	int top;
9523670Sgusella {
9623670Sgusella 	register struct cmd *c;
9723670Sgusella 	extern struct cmd cmdtab[];
9823670Sgusella 	extern int help();
9923670Sgusella 
10023670Sgusella 	if (!top)
10123670Sgusella 		putchar('\n');
10223670Sgusella 	for (;;) {
10323670Sgusella 		if (fromatty) {
10423670Sgusella 			printf("timedc> ");
10523670Sgusella 			(void) fflush(stdout);
10623670Sgusella 		}
10723670Sgusella 		if (gets(cmdline) == 0)
10823670Sgusella 			quit();
10923670Sgusella 		if (cmdline[0] == 0)
11023670Sgusella 			break;
11123670Sgusella 		makeargv();
11223670Sgusella 		c = getcmd(margv[0]);
11323670Sgusella 		if (c == (struct cmd *)-1) {
11423670Sgusella 			printf("?Ambiguous command\n");
11523670Sgusella 			continue;
11623670Sgusella 		}
11723670Sgusella 		if (c == 0) {
11823670Sgusella 			printf("?Invalid command\n");
11923670Sgusella 			continue;
12023670Sgusella 		}
12123670Sgusella 		if (c->c_priv && getuid()) {
12223670Sgusella 			printf("?Privileged command\n");
12323670Sgusella 			continue;
12423670Sgusella 		}
12523670Sgusella 		(*c->c_handler)(margc, margv);
12623670Sgusella 	}
12723670Sgusella 	longjmp(toplevel, 0);
12823670Sgusella }
12923670Sgusella 
13023670Sgusella struct cmd *
13123670Sgusella getcmd(name)
13223670Sgusella 	register char *name;
13323670Sgusella {
13423670Sgusella 	register char *p, *q;
13523670Sgusella 	register struct cmd *c, *found;
13623670Sgusella 	register int nmatches, longest;
13725961Sbloom 	extern int NCMDS;
13823670Sgusella 
13923670Sgusella 	longest = 0;
14023670Sgusella 	nmatches = 0;
14123670Sgusella 	found = 0;
14225961Sbloom 	for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
14325961Sbloom 		p = c->c_name;
14423670Sgusella 		for (q = name; *q == *p++; q++)
14523670Sgusella 			if (*q == 0)		/* exact match? */
14623670Sgusella 				return(c);
14723670Sgusella 		if (!*q) {			/* the name was a prefix */
14823670Sgusella 			if (q - name > longest) {
14923670Sgusella 				longest = q - name;
15023670Sgusella 				nmatches = 1;
15123670Sgusella 				found = c;
15223670Sgusella 			} else if (q - name == longest)
15323670Sgusella 				nmatches++;
15423670Sgusella 		}
15523670Sgusella 	}
15623670Sgusella 	if (nmatches > 1)
15723670Sgusella 		return((struct cmd *)-1);
15823670Sgusella 	return(found);
15923670Sgusella }
16023670Sgusella 
16123670Sgusella /*
16223670Sgusella  * Slice a string up into argc/argv.
16323670Sgusella  */
16423670Sgusella makeargv()
16523670Sgusella {
16623670Sgusella 	register char *cp;
16723670Sgusella 	register char **argp = margv;
16823670Sgusella 
16923670Sgusella 	margc = 0;
17023670Sgusella 	for (cp = cmdline; *cp;) {
17123670Sgusella 		while (isspace(*cp))
17223670Sgusella 			cp++;
17323670Sgusella 		if (*cp == '\0')
17423670Sgusella 			break;
17523670Sgusella 		*argp++ = cp;
17623670Sgusella 		margc += 1;
17723670Sgusella 		while (*cp != '\0' && !isspace(*cp))
17823670Sgusella 			cp++;
17923670Sgusella 		if (*cp == '\0')
18023670Sgusella 			break;
18123670Sgusella 		*cp++ = '\0';
18223670Sgusella 	}
18323670Sgusella 	*argp++ = 0;
18423670Sgusella }
18523670Sgusella 
18623670Sgusella #define HELPINDENT (sizeof ("directory"))
18723670Sgusella 
18823670Sgusella /*
18923670Sgusella  * Help command.
19023670Sgusella  */
19123670Sgusella help(argc, argv)
19223670Sgusella 	int argc;
19323670Sgusella 	char *argv[];
19423670Sgusella {
19523670Sgusella 	register struct cmd *c;
19623670Sgusella 
19723670Sgusella 	if (argc == 1) {
19823670Sgusella 		register int i, j, w;
19923670Sgusella 		int columns, width = 0, lines;
20023670Sgusella 		extern int NCMDS;
20123670Sgusella 
20223670Sgusella 		printf("Commands may be abbreviated.  Commands are:\n\n");
20323670Sgusella 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
20423670Sgusella 			int len = strlen(c->c_name);
20523670Sgusella 
20623670Sgusella 			if (len > width)
20723670Sgusella 				width = len;
20823670Sgusella 		}
20923670Sgusella 		width = (width + 8) &~ 7;
21023670Sgusella 		columns = 80 / width;
21123670Sgusella 		if (columns == 0)
21223670Sgusella 			columns = 1;
21323670Sgusella 		lines = (NCMDS + columns - 1) / columns;
21423670Sgusella 		for (i = 0; i < lines; i++) {
21523670Sgusella 			for (j = 0; j < columns; j++) {
21623670Sgusella 				c = cmdtab + j * lines + i;
21723670Sgusella 				printf("%s", c->c_name);
21823670Sgusella 				if (c + lines >= &cmdtab[NCMDS]) {
21923670Sgusella 					printf("\n");
22023670Sgusella 					break;
22123670Sgusella 				}
22223670Sgusella 				w = strlen(c->c_name);
22323670Sgusella 				while (w < width) {
22423670Sgusella 					w = (w + 8) &~ 7;
22523670Sgusella 					putchar('\t');
22623670Sgusella 				}
22723670Sgusella 			}
22823670Sgusella 		}
22923670Sgusella 		return;
23023670Sgusella 	}
23123670Sgusella 	while (--argc > 0) {
23223670Sgusella 		register char *arg;
23323670Sgusella 		arg = *++argv;
23423670Sgusella 		c = getcmd(arg);
23523670Sgusella 		if (c == (struct cmd *)-1)
23623670Sgusella 			printf("?Ambiguous help command %s\n", arg);
23723670Sgusella 		else if (c == (struct cmd *)0)
23823670Sgusella 			printf("?Invalid help command %s\n", arg);
23923670Sgusella 		else
24023670Sgusella 			printf("%-*s\t%s\n", HELPINDENT,
24123670Sgusella 				c->c_name, c->c_help);
24223670Sgusella 	}
24323670Sgusella }
244