xref: /csrg-svn/usr.sbin/timed/timedc/timedc.c (revision 36246)
123670Sgusella /*
223670Sgusella  * Copyright (c) 1983 Regents of the University of California.
333123Sbostic  * All rights reserved.
433123Sbostic  *
533123Sbostic  * Redistribution and use in source and binary forms are permitted
634775Sbostic  * provided that the above copyright notice and this paragraph are
734775Sbostic  * duplicated in all such forms and that any documentation,
834775Sbostic  * advertising materials, and other materials related to such
934775Sbostic  * distribution and use acknowledge that the software was developed
1034775Sbostic  * by the University of California, Berkeley.  The name of the
1134775Sbostic  * University may not be used to endorse or promote products derived
1234775Sbostic  * from this software without specific prior written permission.
1334775Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434775Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534775Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1623670Sgusella  */
1723670Sgusella 
1823670Sgusella #ifndef lint
1923670Sgusella char copyright[] =
2023670Sgusella "@(#) Copyright (c) 1983 Regents of the University of California.\n\
2123670Sgusella  All rights reserved.\n";
2233123Sbostic #endif /* not lint */
2323670Sgusella 
2423670Sgusella #ifndef lint
25*36246Sbostic static char sccsid[] = "@(#)timedc.c	2.7 (Berkeley) 11/20/88";
2633123Sbostic #endif /* not lint */
2723670Sgusella 
2823670Sgusella #include "timedc.h"
2923670Sgusella #include <signal.h>
3023670Sgusella #include <ctype.h>
3123670Sgusella #include <setjmp.h>
3224908Sbloom #include <syslog.h>
3323670Sgusella 
3423670Sgusella int	top;
3523670Sgusella int	margc;
3623670Sgusella int	fromatty;
3723670Sgusella char	*margv[20];
3823670Sgusella char	cmdline[200];
3923670Sgusella jmp_buf	toplevel;
4023670Sgusella int	intr();
4123670Sgusella int priv_resources();
4223670Sgusella struct	cmd *getcmd();
4323670Sgusella 
4423670Sgusella 
4523670Sgusella main(argc, argv)
4623670Sgusella 	char *argv[];
4723670Sgusella {
4823670Sgusella 	register struct cmd *c;
4923670Sgusella 
5024872Seric 	openlog("timedc", LOG_ODELAY, LOG_AUTH);
5124872Seric 
5223670Sgusella 	/*
5323670Sgusella 	 * security dictates!
5423670Sgusella 	 */
5523670Sgusella 	if (priv_resources() < 0) {
5623670Sgusella 		fprintf(stderr, "Could not get priviledged resources\n");
5723670Sgusella 		exit(1);
5823670Sgusella 	}
5923670Sgusella 	(void) setuid(getuid());
6023670Sgusella 
6123670Sgusella 	if (--argc > 0) {
6223670Sgusella 		c = getcmd(*++argv);
6323670Sgusella 		if (c == (struct cmd *)-1) {
6423670Sgusella 			printf("?Ambiguous command\n");
6523670Sgusella 			exit(1);
6623670Sgusella 		}
6723670Sgusella 		if (c == 0) {
6823670Sgusella 			printf("?Invalid command\n");
6923670Sgusella 			exit(1);
7023670Sgusella 		}
7129348Sbloom 		if (c->c_priv && getuid()) {
7229348Sbloom 			printf("?Privileged command\n");
7329348Sbloom 			exit(1);
7429348Sbloom 		}
7523670Sgusella 		(*c->c_handler)(argc, argv);
7623670Sgusella 		exit(0);
7723670Sgusella 	}
7823670Sgusella 	fromatty = isatty(fileno(stdin));
7923670Sgusella 	top = setjmp(toplevel) == 0;
8023670Sgusella 	if (top)
8123670Sgusella 		(void) signal(SIGINT, intr);
8223670Sgusella 	for (;;) {
8323670Sgusella 		cmdscanner(top);
8423670Sgusella 		top = 1;
8523670Sgusella 	}
8623670Sgusella }
8723670Sgusella 
8823670Sgusella intr()
8923670Sgusella {
9023670Sgusella 	if (!fromatty)
9123670Sgusella 		exit(0);
9223670Sgusella 	longjmp(toplevel, 1);
9323670Sgusella }
9423670Sgusella 
9523670Sgusella /*
9623670Sgusella  * Command parser.
9723670Sgusella  */
9823670Sgusella cmdscanner(top)
9923670Sgusella 	int top;
10023670Sgusella {
10123670Sgusella 	register struct cmd *c;
10223670Sgusella 	extern int help();
10323670Sgusella 
10423670Sgusella 	if (!top)
10523670Sgusella 		putchar('\n');
10623670Sgusella 	for (;;) {
10723670Sgusella 		if (fromatty) {
10823670Sgusella 			printf("timedc> ");
10923670Sgusella 			(void) fflush(stdout);
11023670Sgusella 		}
111*36246Sbostic 		if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
11223670Sgusella 			quit();
11323670Sgusella 		if (cmdline[0] == 0)
11423670Sgusella 			break;
11523670Sgusella 		makeargv();
11623670Sgusella 		c = getcmd(margv[0]);
11723670Sgusella 		if (c == (struct cmd *)-1) {
11823670Sgusella 			printf("?Ambiguous command\n");
11923670Sgusella 			continue;
12023670Sgusella 		}
12123670Sgusella 		if (c == 0) {
12223670Sgusella 			printf("?Invalid command\n");
12323670Sgusella 			continue;
12423670Sgusella 		}
12523670Sgusella 		if (c->c_priv && getuid()) {
12623670Sgusella 			printf("?Privileged command\n");
12723670Sgusella 			continue;
12823670Sgusella 		}
12923670Sgusella 		(*c->c_handler)(margc, margv);
13023670Sgusella 	}
13123670Sgusella 	longjmp(toplevel, 0);
13223670Sgusella }
13323670Sgusella 
13423670Sgusella struct cmd *
13523670Sgusella getcmd(name)
13623670Sgusella 	register char *name;
13723670Sgusella {
13823670Sgusella 	register char *p, *q;
13923670Sgusella 	register struct cmd *c, *found;
14023670Sgusella 	register int nmatches, longest;
14133158Sbostic 	extern struct cmd cmdtab[];
14225961Sbloom 	extern int NCMDS;
14323670Sgusella 
14423670Sgusella 	longest = 0;
14523670Sgusella 	nmatches = 0;
14623670Sgusella 	found = 0;
14725961Sbloom 	for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
14825961Sbloom 		p = c->c_name;
14923670Sgusella 		for (q = name; *q == *p++; q++)
15023670Sgusella 			if (*q == 0)		/* exact match? */
15123670Sgusella 				return(c);
15223670Sgusella 		if (!*q) {			/* the name was a prefix */
15323670Sgusella 			if (q - name > longest) {
15423670Sgusella 				longest = q - name;
15523670Sgusella 				nmatches = 1;
15623670Sgusella 				found = c;
15723670Sgusella 			} else if (q - name == longest)
15823670Sgusella 				nmatches++;
15923670Sgusella 		}
16023670Sgusella 	}
16123670Sgusella 	if (nmatches > 1)
16223670Sgusella 		return((struct cmd *)-1);
16323670Sgusella 	return(found);
16423670Sgusella }
16523670Sgusella 
16623670Sgusella /*
16723670Sgusella  * Slice a string up into argc/argv.
16823670Sgusella  */
16923670Sgusella makeargv()
17023670Sgusella {
17123670Sgusella 	register char *cp;
17223670Sgusella 	register char **argp = margv;
17323670Sgusella 
17423670Sgusella 	margc = 0;
17523670Sgusella 	for (cp = cmdline; *cp;) {
17623670Sgusella 		while (isspace(*cp))
17723670Sgusella 			cp++;
17823670Sgusella 		if (*cp == '\0')
17923670Sgusella 			break;
18023670Sgusella 		*argp++ = cp;
18123670Sgusella 		margc += 1;
18223670Sgusella 		while (*cp != '\0' && !isspace(*cp))
18323670Sgusella 			cp++;
18423670Sgusella 		if (*cp == '\0')
18523670Sgusella 			break;
18623670Sgusella 		*cp++ = '\0';
18723670Sgusella 	}
18823670Sgusella 	*argp++ = 0;
18923670Sgusella }
19023670Sgusella 
19123670Sgusella #define HELPINDENT (sizeof ("directory"))
19223670Sgusella 
19323670Sgusella /*
19423670Sgusella  * Help command.
19523670Sgusella  */
19623670Sgusella help(argc, argv)
19723670Sgusella 	int argc;
19823670Sgusella 	char *argv[];
19923670Sgusella {
20023670Sgusella 	register struct cmd *c;
20133158Sbostic 	extern struct cmd cmdtab[];
20223670Sgusella 
20323670Sgusella 	if (argc == 1) {
20423670Sgusella 		register int i, j, w;
20523670Sgusella 		int columns, width = 0, lines;
20623670Sgusella 		extern int NCMDS;
20723670Sgusella 
20823670Sgusella 		printf("Commands may be abbreviated.  Commands are:\n\n");
20923670Sgusella 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
21023670Sgusella 			int len = strlen(c->c_name);
21123670Sgusella 
21223670Sgusella 			if (len > width)
21323670Sgusella 				width = len;
21423670Sgusella 		}
21523670Sgusella 		width = (width + 8) &~ 7;
21623670Sgusella 		columns = 80 / width;
21723670Sgusella 		if (columns == 0)
21823670Sgusella 			columns = 1;
21923670Sgusella 		lines = (NCMDS + columns - 1) / columns;
22023670Sgusella 		for (i = 0; i < lines; i++) {
22123670Sgusella 			for (j = 0; j < columns; j++) {
22223670Sgusella 				c = cmdtab + j * lines + i;
22323670Sgusella 				printf("%s", c->c_name);
22423670Sgusella 				if (c + lines >= &cmdtab[NCMDS]) {
22523670Sgusella 					printf("\n");
22623670Sgusella 					break;
22723670Sgusella 				}
22823670Sgusella 				w = strlen(c->c_name);
22923670Sgusella 				while (w < width) {
23023670Sgusella 					w = (w + 8) &~ 7;
23123670Sgusella 					putchar('\t');
23223670Sgusella 				}
23323670Sgusella 			}
23423670Sgusella 		}
23523670Sgusella 		return;
23623670Sgusella 	}
23723670Sgusella 	while (--argc > 0) {
23823670Sgusella 		register char *arg;
23923670Sgusella 		arg = *++argv;
24023670Sgusella 		c = getcmd(arg);
24123670Sgusella 		if (c == (struct cmd *)-1)
24223670Sgusella 			printf("?Ambiguous help command %s\n", arg);
24323670Sgusella 		else if (c == (struct cmd *)0)
24423670Sgusella 			printf("?Invalid help command %s\n", arg);
24523670Sgusella 		else
24623670Sgusella 			printf("%-*s\t%s\n", HELPINDENT,
24723670Sgusella 				c->c_name, c->c_help);
24823670Sgusella 	}
24923670Sgusella }
250