xref: /original-bsd/usr.sbin/lpr/lpc/lpc.c (revision f1656be1)
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 are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 char copyright[] =
15 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
16  All rights reserved.\n";
17 #endif /* not lint */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)lpc.c	5.5 (Berkeley) 05/05/88";
21 #endif /* not lint */
22 
23 /*
24  * lpc -- line printer control program
25  */
26 #include <stdio.h>
27 #include <signal.h>
28 #include <ctype.h>
29 #include <setjmp.h>
30 #include <syslog.h>
31 
32 #include "lpc.h"
33 
34 int	fromatty;
35 
36 char	cmdline[200];
37 int	margc;
38 char	*margv[20];
39 int	top;
40 int	intr();
41 struct	cmd *getcmd();
42 
43 jmp_buf	toplevel;
44 
45 main(argc, argv)
46 	char *argv[];
47 {
48 	register struct cmd *c;
49 	extern char *name;
50 
51 	name = argv[0];
52 	openlog("lpd", 0, LOG_LPR);
53 
54 	if (--argc > 0) {
55 		c = getcmd(*++argv);
56 		if (c == (struct cmd *)-1) {
57 			printf("?Ambiguous command\n");
58 			exit(1);
59 		}
60 		if (c == 0) {
61 			printf("?Invalid command\n");
62 			exit(1);
63 		}
64 		if (c->c_priv && getuid()) {
65 			printf("?Privileged command\n");
66 			exit(1);
67 		}
68 		(*c->c_handler)(argc, argv);
69 		exit(0);
70 	}
71 	fromatty = isatty(fileno(stdin));
72 	top = setjmp(toplevel) == 0;
73 	if (top)
74 		signal(SIGINT, intr);
75 	for (;;) {
76 		cmdscanner(top);
77 		top = 1;
78 	}
79 }
80 
81 intr()
82 {
83 	if (!fromatty)
84 		exit(0);
85 	longjmp(toplevel, 1);
86 }
87 
88 /*
89  * Command parser.
90  */
91 cmdscanner(top)
92 	int top;
93 {
94 	register struct cmd *c;
95 
96 	if (!top)
97 		putchar('\n');
98 	for (;;) {
99 		if (fromatty) {
100 			printf("lpc> ");
101 			fflush(stdout);
102 		}
103 		if (gets(cmdline) == 0)
104 			quit();
105 		if (cmdline[0] == 0)
106 			break;
107 		makeargv();
108 		c = getcmd(margv[0]);
109 		if (c == (struct cmd *)-1) {
110 			printf("?Ambiguous command\n");
111 			continue;
112 		}
113 		if (c == 0) {
114 			printf("?Invalid command\n");
115 			continue;
116 		}
117 		if (c->c_priv && getuid()) {
118 			printf("?Privileged command\n");
119 			continue;
120 		}
121 		(*c->c_handler)(margc, margv);
122 	}
123 	longjmp(toplevel, 0);
124 }
125 
126 extern struct cmd cmdtab[];
127 
128 struct cmd *
129 getcmd(name)
130 	register char *name;
131 {
132 	register char *p, *q;
133 	register struct cmd *c, *found;
134 	register int nmatches, longest;
135 
136 	longest = 0;
137 	nmatches = 0;
138 	found = 0;
139 	for (c = cmdtab; p = c->c_name; c++) {
140 		for (q = name; *q == *p++; q++)
141 			if (*q == 0)		/* exact match? */
142 				return(c);
143 		if (!*q) {			/* the name was a prefix */
144 			if (q - name > longest) {
145 				longest = q - name;
146 				nmatches = 1;
147 				found = c;
148 			} else if (q - name == longest)
149 				nmatches++;
150 		}
151 	}
152 	if (nmatches > 1)
153 		return((struct cmd *)-1);
154 	return(found);
155 }
156 
157 /*
158  * Slice a string up into argc/argv.
159  */
160 makeargv()
161 {
162 	register char *cp;
163 	register char **argp = margv;
164 
165 	margc = 0;
166 	for (cp = cmdline; *cp;) {
167 		while (isspace(*cp))
168 			cp++;
169 		if (*cp == '\0')
170 			break;
171 		*argp++ = cp;
172 		margc += 1;
173 		while (*cp != '\0' && !isspace(*cp))
174 			cp++;
175 		if (*cp == '\0')
176 			break;
177 		*cp++ = '\0';
178 	}
179 	*argp++ = 0;
180 }
181 
182 #define HELPINDENT (sizeof ("directory"))
183 
184 /*
185  * Help command.
186  */
187 help(argc, argv)
188 	int argc;
189 	char *argv[];
190 {
191 	register struct cmd *c;
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 - 1]; 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