2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1983, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
45 static const char rcsid[] =
49 #include <sys/param.h>
69 #define LPR_OPER "operator" /* group name of lpr operators */
73 * lpc -- line printer control program
76 #define MAX_CMDLINE 200
80 static char cmdline[MAX_CMDLINE];
82 static char *margv[MAX_MARGV];
85 int main(int _argc, char *_argv[]);
86 static void cmdscanner(void);
87 static struct cmd *getcmd(const char *_name);
88 static void intr(int _signo);
89 static void makeargv(void);
90 static int ingroup(const char *_grname);
93 main(int argc, char *argv[])
95 register struct cmd *c;
101 openlog("lpd", 0, LOG_LPR);
105 if (c == (struct cmd *)-1) {
106 printf("?Ambiguous command\n");
110 printf("?Invalid command\n");
113 if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
114 printf("?Privileged command\n");
117 if (c->c_generic != 0)
118 generic(c->c_generic, c->c_handler, argc, argv);
120 (*c->c_handler)(argc, argv);
123 fromatty = isatty(fileno(stdin));
125 signal(SIGINT, intr);
132 intr(int signo __unused)
134 /* (the '__unused' is just to avoid a compile-time warning) */
150 register struct cmd *c;
152 static History *hist;
165 el = el_init("lpc", stdin, stdout, stderr);
166 hist = history_init();
167 history(hist, &he, H_EVENT, 100);
168 el_set(el, EL_HIST, history, hist);
169 el_set(el, EL_EDITOR, "emacs");
170 el_set(el, EL_PROMPT, lpc_prompt);
171 el_set(el, EL_SIGNAL, 1);
174 * EditLine init may call 'cgetset()' to set a
175 * capability-db meant for termcap (eg: to set
176 * terminal type 'xterm'). Reset that now, or
177 * that same db-information will be used for
178 * printcap (giving us an "xterm" printer, with
179 * all kinds of invalid capabilities...).
183 if ((bp = el_gets(el, &num)) == NULL || num == 0)
186 len = (num > MAX_CMDLINE) ? MAX_CMDLINE : num;
187 memcpy(cmdline, bp, len);
189 history(hist, &he, H_ENTER, bp);
192 if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
194 if (cmdline[0] == 0 || cmdline[0] == '\n')
201 if (el_parse(el, margc, margv) != -1)
204 c = getcmd(margv[0]);
205 if (c == (struct cmd *)-1) {
206 printf("?Ambiguous command\n");
210 printf("?Invalid command\n");
213 if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
214 printf("?Privileged command\n");
219 * Two different commands might have the same generic rtn
220 * (eg: "clean" and "tclean"), and just use different
221 * handler routines for distinct command-setup. The handler
222 * routine might also be set on a generic routine for
223 * initial parameter processing.
225 if (c->c_generic != 0)
226 generic(c->c_generic, c->c_handler, margc, margv);
228 (*c->c_handler)(margc, margv);
233 getcmd(const char *name)
235 register const char *p, *q;
236 register struct cmd *c, *found;
237 register int nmatches, longest;
242 for (c = cmdtab; (p = c->c_name); c++) {
243 for (q = name; *q == *p++; q++)
244 if (*q == 0) /* exact match? */
246 if (!*q) { /* the name was a prefix */
247 if (q - name > longest) {
251 } else if (q - name == longest)
256 return((struct cmd *)-1);
261 * Slice a string up into argc/argv.
267 register char **argp = margv;
271 for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) &&
272 n < MAX_MARGV; n++) {
279 while (*cp != '\0' && !isspace(*cp))
288 #define HELPINDENT (sizeof ("directory"))
294 help(int argc, char *argv[])
296 register struct cmd *c;
299 register int i, j, w;
300 int columns, width = 0, lines;
302 printf("Commands may be abbreviated. Commands are:\n\n");
303 for (c = cmdtab; c->c_name; c++) {
304 int len = strlen(c->c_name);
309 width = (width + 8) &~ 7;
310 columns = 80 / width;
313 lines = (NCMDS + columns - 1) / columns;
314 for (i = 0; i < lines; i++) {
315 for (j = 0; j < columns; j++) {
316 c = cmdtab + j * lines + i;
318 printf("%s", c->c_name);
319 if (c + lines >= &cmdtab[NCMDS]) {
323 w = strlen(c->c_name);
336 if (c == (struct cmd *)-1)
337 printf("?Ambiguous help command %s\n", arg);
338 else if (c == (struct cmd *)0)
339 printf("?Invalid help command %s\n", arg);
341 printf("%-*s\t%s\n", (int) HELPINDENT,
342 c->c_name, c->c_help);
347 * return non-zero if the user is a member of the given group
350 ingroup(const char *grname)
352 static struct group *gptr=NULL;
353 static int ngroups = 0;
354 static gid_t groups[NGROUPS];
359 if ((gptr = getgrnam(grname)) == NULL) {
360 warnx("warning: unknown group '%s'", grname);
363 ngroups = getgroups(NGROUPS, groups);
368 for (i = 0; i < ngroups; i++)
369 if (gid == groups[i])