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
34 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
35 * @(#)lpc.c 8.3 (Berkeley) 4/28/95
36 * $FreeBSD: src/usr.sbin/lpr/lpc/lpc.c,v 1.13.2.11 2002/07/26 03:12:07 gad Exp $
37 * $DragonFly: src/usr.sbin/lpr/lpc/lpc.c,v 1.3 2003/11/04 17:03:11 drhodus Exp $
40 #include <sys/param.h>
60 #define LPR_OPER "operator" /* group name of lpr operators */
64 * lpc -- line printer control program
67 #define MAX_CMDLINE 200
71 static char cmdline[MAX_CMDLINE];
73 static char *margv[MAX_MARGV];
76 int main(int _argc, char *_argv[]);
77 static void cmdscanner(void);
78 static struct cmd *getcmd(const char *_name);
79 static void intr(int _signo);
80 static void makeargv(void);
81 static int ingroup(const char *_grname);
84 main(int argc, char *argv[])
86 register struct cmd *c;
92 openlog("lpd", 0, LOG_LPR);
96 if (c == (struct cmd *)-1) {
97 printf("?Ambiguous command\n");
101 printf("?Invalid command\n");
104 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
105 ingroup(LPR_OPER) == 0) {
106 printf("?Privileged command\n");
109 if (c->c_generic != 0)
110 generic(c->c_generic, c->c_opts, c->c_handler,
113 (*c->c_handler)(argc, argv);
116 fromatty = isatty(fileno(stdin));
118 signal(SIGINT, intr);
125 intr(int signo __unused)
127 /* (the '__unused' is just to avoid a compile-time warning) */
143 register struct cmd *c;
145 static History *hist;
157 el = el_init("lpc", stdin, stdout);
158 hist = history_init();
159 history(hist, H_EVENT, 100);
160 el_set(el, EL_HIST, history, hist);
161 el_set(el, EL_EDITOR, "emacs");
162 el_set(el, EL_PROMPT, lpc_prompt);
163 el_set(el, EL_SIGNAL, 1);
166 * EditLine init may call 'cgetset()' to set a
167 * capability-db meant for termcap (eg: to set
168 * terminal type 'xterm'). Reset that now, or
169 * that same db-information will be used for
170 * printcap (giving us an "xterm" printer, with
171 * all kinds of invalid capabilities...).
175 if ((bp = el_gets(el, &num)) == NULL || num == 0)
178 len = (num > MAX_CMDLINE -1) ? MAX_CMDLINE -1 : num;
179 memcpy(cmdline, bp, len);
181 history(hist, H_ENTER, bp);
184 if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
186 if (cmdline[0] == 0 || cmdline[0] == '\n')
193 if (el_parse(el, margc, margv) != -1)
196 c = getcmd(margv[0]);
197 if (c == (struct cmd *)-1) {
198 printf("?Ambiguous command\n");
202 printf("?Invalid command\n");
205 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
206 ingroup(LPR_OPER) == 0) {
207 printf("?Privileged command\n");
212 * Two different commands might have the same generic rtn
213 * (eg: "clean" and "tclean"), and just use different
214 * handler routines for distinct command-setup. The handler
215 * routine might also be set on a generic routine for
216 * initial parameter processing.
218 if (c->c_generic != 0)
219 generic(c->c_generic, c->c_opts, c->c_handler,
222 (*c->c_handler)(margc, margv);
227 getcmd(const char *name)
229 register const char *p, *q;
230 register struct cmd *c, *found;
231 register int nmatches, longest;
236 for (c = cmdtab; (p = c->c_name); c++) {
237 for (q = name; *q == *p++; q++)
238 if (*q == 0) /* exact match? */
240 if (!*q) { /* the name was a prefix */
241 if (q - name > longest) {
245 } else if (q - name == longest)
250 return((struct cmd *)-1);
255 * Slice a string up into argc/argv.
261 register char **argp = margv;
265 for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) &&
266 n < MAX_MARGV -1; n++) {
273 while (*cp != '\0' && !isspace(*cp))
282 #define HELPINDENT (sizeof ("directory"))
288 help(int argc, char *argv[])
290 register struct cmd *c;
293 register int i, j, w;
294 int columns, width = 0, lines;
296 printf("Commands may be abbreviated. Commands are:\n\n");
297 for (c = cmdtab; c->c_name; c++) {
298 int len = strlen(c->c_name);
303 width = (width + 8) &~ 7;
304 columns = 80 / width;
307 lines = (NCMDS + columns - 1) / columns;
308 for (i = 0; i < lines; i++) {
309 for (j = 0; j < columns; j++) {
310 c = cmdtab + j * lines + i;
312 printf("%s", c->c_name);
313 if (c + lines >= &cmdtab[NCMDS]) {
317 w = strlen(c->c_name);
330 if (c == (struct cmd *)-1)
331 printf("?Ambiguous help command %s\n", arg);
332 else if (c == (struct cmd *)0)
333 printf("?Invalid help command %s\n", arg);
335 printf("%-*s\t%s\n", (int) HELPINDENT,
336 c->c_name, c->c_help);
341 * return non-zero if the user is a member of the given group
344 ingroup(const char *grname)
346 static struct group *gptr=NULL;
347 static int ngroups = 0;
348 static gid_t groups[NGROUPS];
353 if ((gptr = getgrnam(grname)) == NULL) {
354 warnx("warning: unknown group '%s'", grname);
357 ngroups = getgroups(NGROUPS, groups);
362 for (i = 0; i < ngroups; i++)
363 if (gid == groups[i])
369 * Routine to get the information for a single printer (which will be
370 * called by the routines which implement individual commands).
371 * Note: This is for commands operating on a *single* printer.
374 setup_myprinter(char *pwanted, struct printer *pp, int sump_opts)
376 int cdres, cmdstatus;
379 cmdstatus = getprintcap(pwanted, pp);
382 fatal(pp, "%s", pcaperr(cmdstatus));
384 case PCAPERR_NOTFOUND:
385 printf("unknown printer %s\n", pwanted);
388 printf("warning: %s: unresolved tc= reference(s)", pwanted);
390 case PCAPERR_SUCCESS:
393 if ((sump_opts & SUMP_NOHEADER) == 0)
394 printf("%s:\n", pp->printer);
396 if (sump_opts & SUMP_CHDIR_SD) {
398 cdres = chdir(pp->spool_dir);
401 printf("\tcannot chdir to %s\n", pp->spool_dir);