xref: /original-bsd/usr.sbin/timed/timedc/timedc.c (revision 907a1f7d)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)timedc.c	2.9 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 #include "timedc.h"
19 #include <signal.h>
20 #include <ctype.h>
21 #include <setjmp.h>
22 #include <syslog.h>
23 
24 int	top;
25 int	margc;
26 int	fromatty;
27 char	*margv[20];
28 char	cmdline[200];
29 jmp_buf	toplevel;
30 int	intr();
31 int priv_resources();
32 struct	cmd *getcmd();
33 
34 
35 main(argc, argv)
36 	char *argv[];
37 {
38 	register struct cmd *c;
39 
40 	openlog("timedc", LOG_ODELAY, LOG_AUTH);
41 
42 	/*
43 	 * security dictates!
44 	 */
45 	if (priv_resources() < 0) {
46 		fprintf(stderr, "Could not get privileged resources\n");
47 		exit(1);
48 	}
49 	(void) setuid(getuid());
50 
51 	if (--argc > 0) {
52 		c = getcmd(*++argv);
53 		if (c == (struct cmd *)-1) {
54 			printf("?Ambiguous command\n");
55 			exit(1);
56 		}
57 		if (c == 0) {
58 			printf("?Invalid command\n");
59 			exit(1);
60 		}
61 		if (c->c_priv && getuid()) {
62 			printf("?Privileged command\n");
63 			exit(1);
64 		}
65 		(*c->c_handler)(argc, argv);
66 		exit(0);
67 	}
68 	fromatty = isatty(fileno(stdin));
69 	top = setjmp(toplevel) == 0;
70 	if (top)
71 		(void) signal(SIGINT, intr);
72 	for (;;) {
73 		cmdscanner(top);
74 		top = 1;
75 	}
76 }
77 
78 intr()
79 {
80 	if (!fromatty)
81 		exit(0);
82 	longjmp(toplevel, 1);
83 }
84 
85 /*
86  * Command parser.
87  */
88 cmdscanner(top)
89 	int top;
90 {
91 	register struct cmd *c;
92 	extern int help();
93 
94 	if (!top)
95 		putchar('\n');
96 	for (;;) {
97 		if (fromatty) {
98 			printf("timedc> ");
99 			(void) fflush(stdout);
100 		}
101 		if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
102 			quit();
103 		if (cmdline[0] == 0)
104 			break;
105 		makeargv();
106 		c = getcmd(margv[0]);
107 		if (c == (struct cmd *)-1) {
108 			printf("?Ambiguous command\n");
109 			continue;
110 		}
111 		if (c == 0) {
112 			printf("?Invalid command\n");
113 			continue;
114 		}
115 		if (c->c_priv && getuid()) {
116 			printf("?Privileged command\n");
117 			continue;
118 		}
119 		(*c->c_handler)(margc, margv);
120 	}
121 	longjmp(toplevel, 0);
122 }
123 
124 struct cmd *
125 getcmd(name)
126 	register char *name;
127 {
128 	register char *p, *q;
129 	register struct cmd *c, *found;
130 	register int nmatches, longest;
131 	extern struct cmd cmdtab[];
132 	extern int NCMDS;
133 
134 	longest = 0;
135 	nmatches = 0;
136 	found = 0;
137 	for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
138 		p = c->c_name;
139 		for (q = name; *q == *p++; q++)
140 			if (*q == 0)		/* exact match? */
141 				return(c);
142 		if (!*q) {			/* the name was a prefix */
143 			if (q - name > longest) {
144 				longest = q - name;
145 				nmatches = 1;
146 				found = c;
147 			} else if (q - name == longest)
148 				nmatches++;
149 		}
150 	}
151 	if (nmatches > 1)
152 		return((struct cmd *)-1);
153 	return(found);
154 }
155 
156 /*
157  * Slice a string up into argc/argv.
158  */
159 makeargv()
160 {
161 	register char *cp;
162 	register char **argp = margv;
163 
164 	margc = 0;
165 	for (cp = cmdline; *cp;) {
166 		while (isspace(*cp))
167 			cp++;
168 		if (*cp == '\0')
169 			break;
170 		*argp++ = cp;
171 		margc += 1;
172 		while (*cp != '\0' && !isspace(*cp))
173 			cp++;
174 		if (*cp == '\0')
175 			break;
176 		*cp++ = '\0';
177 	}
178 	*argp++ = 0;
179 }
180 
181 #define HELPINDENT (sizeof ("directory"))
182 
183 /*
184  * Help command.
185  */
186 help(argc, argv)
187 	int argc;
188 	char *argv[];
189 {
190 	register struct cmd *c;
191 	extern struct cmd cmdtab[];
192 
193 	if (argc == 1) {
194 		register int i, j, w;
195 		int columns, width = 0, lines;
196 		extern int NCMDS;
197 
198 		printf("Commands may be abbreviated.  Commands are:\n\n");
199 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
200 			int len = strlen(c->c_name);
201 
202 			if (len > width)
203 				width = len;
204 		}
205 		width = (width + 8) &~ 7;
206 		columns = 80 / width;
207 		if (columns == 0)
208 			columns = 1;
209 		lines = (NCMDS + columns - 1) / columns;
210 		for (i = 0; i < lines; i++) {
211 			for (j = 0; j < columns; j++) {
212 				c = cmdtab + j * lines + i;
213 				printf("%s", c->c_name);
214 				if (c + lines >= &cmdtab[NCMDS]) {
215 					printf("\n");
216 					break;
217 				}
218 				w = strlen(c->c_name);
219 				while (w < width) {
220 					w = (w + 8) &~ 7;
221 					putchar('\t');
222 				}
223 			}
224 		}
225 		return;
226 	}
227 	while (--argc > 0) {
228 		register char *arg;
229 		arg = *++argv;
230 		c = getcmd(arg);
231 		if (c == (struct cmd *)-1)
232 			printf("?Ambiguous help command %s\n", arg);
233 		else if (c == (struct cmd *)0)
234 			printf("?Invalid help command %s\n", arg);
235 		else
236 			printf("%-*s\t%s\n", HELPINDENT,
237 				c->c_name, c->c_help);
238 	}
239 }
240