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