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[] =
46 "$FreeBSD: src/usr.sbin/lpr/lpc/lpc.c,v 1.13.2.11 2002/07/26 03:12:07 gad Exp $";
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_opts & LPC_PRIVCMD) && getuid() &&
114 ingroup(LPR_OPER) == 0) {
115 printf("?Privileged command\n");
118 if (c->c_generic != 0)
119 generic(c->c_generic, c->c_opts, c->c_handler,
122 (*c->c_handler)(argc, argv);
125 fromatty = isatty(fileno(stdin));
127 signal(SIGINT, intr);
134 intr(int signo __unused)
136 /* (the '__unused' is just to avoid a compile-time warning) */
152 register struct cmd *c;
154 static History *hist;
166 el = el_init("lpc", stdin, stdout);
167 hist = history_init();
168 history(hist, H_EVENT, 100);
169 el_set(el, EL_HIST, history, hist);
170 el_set(el, EL_EDITOR, "emacs");
171 el_set(el, EL_PROMPT, lpc_prompt);
172 el_set(el, EL_SIGNAL, 1);
175 * EditLine init may call 'cgetset()' to set a
176 * capability-db meant for termcap (eg: to set
177 * terminal type 'xterm'). Reset that now, or
178 * that same db-information will be used for
179 * printcap (giving us an "xterm" printer, with
180 * all kinds of invalid capabilities...).
184 if ((bp = el_gets(el, &num)) == NULL || num == 0)
187 len = (num > MAX_CMDLINE) ? MAX_CMDLINE : num;
188 memcpy(cmdline, bp, len);
190 history(hist, H_ENTER, bp);
193 if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
195 if (cmdline[0] == 0 || cmdline[0] == '\n')
202 if (el_parse(el, margc, margv) != -1)
205 c = getcmd(margv[0]);
206 if (c == (struct cmd *)-1) {
207 printf("?Ambiguous command\n");
211 printf("?Invalid command\n");
214 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
215 ingroup(LPR_OPER) == 0) {
216 printf("?Privileged command\n");
221 * Two different commands might have the same generic rtn
222 * (eg: "clean" and "tclean"), and just use different
223 * handler routines for distinct command-setup. The handler
224 * routine might also be set on a generic routine for
225 * initial parameter processing.
227 if (c->c_generic != 0)
228 generic(c->c_generic, c->c_opts, c->c_handler,
231 (*c->c_handler)(margc, margv);
236 getcmd(const char *name)
238 register const char *p, *q;
239 register struct cmd *c, *found;
240 register int nmatches, longest;
245 for (c = cmdtab; (p = c->c_name); c++) {
246 for (q = name; *q == *p++; q++)
247 if (*q == 0) /* exact match? */
249 if (!*q) { /* the name was a prefix */
250 if (q - name > longest) {
254 } else if (q - name == longest)
259 return((struct cmd *)-1);
264 * Slice a string up into argc/argv.
270 register char **argp = margv;
274 for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) &&
275 n < MAX_MARGV; n++) {
282 while (*cp != '\0' && !isspace(*cp))
291 #define HELPINDENT (sizeof ("directory"))
297 help(int argc, char *argv[])
299 register struct cmd *c;
302 register int i, j, w;
303 int columns, width = 0, lines;
305 printf("Commands may be abbreviated. Commands are:\n\n");
306 for (c = cmdtab; c->c_name; c++) {
307 int len = strlen(c->c_name);
312 width = (width + 8) &~ 7;
313 columns = 80 / width;
316 lines = (NCMDS + columns - 1) / columns;
317 for (i = 0; i < lines; i++) {
318 for (j = 0; j < columns; j++) {
319 c = cmdtab + j * lines + i;
321 printf("%s", c->c_name);
322 if (c + lines >= &cmdtab[NCMDS]) {
326 w = strlen(c->c_name);
339 if (c == (struct cmd *)-1)
340 printf("?Ambiguous help command %s\n", arg);
341 else if (c == (struct cmd *)0)
342 printf("?Invalid help command %s\n", arg);
344 printf("%-*s\t%s\n", (int) HELPINDENT,
345 c->c_name, c->c_help);
350 * return non-zero if the user is a member of the given group
353 ingroup(const char *grname)
355 static struct group *gptr=NULL;
356 static int ngroups = 0;
357 static gid_t groups[NGROUPS];
362 if ((gptr = getgrnam(grname)) == NULL) {
363 warnx("warning: unknown group '%s'", grname);
366 ngroups = getgroups(NGROUPS, groups);
371 for (i = 0; i < ngroups; i++)
372 if (gid == groups[i])
378 * Routine to get the information for a single printer (which will be
379 * called by the routines which implement individual commands).
380 * Note: This is for commands operating on a *single* printer.
383 setup_myprinter(char *pwanted, struct printer *pp, int sump_opts)
385 int cdres, cmdstatus;
388 cmdstatus = getprintcap(pwanted, pp);
391 fatal(pp, "%s", pcaperr(cmdstatus));
393 case PCAPERR_NOTFOUND:
394 printf("unknown printer %s\n", pwanted);
397 printf("warning: %s: unresolved tc= reference(s)", pwanted);
399 case PCAPERR_SUCCESS:
402 if ((sump_opts & SUMP_NOHEADER) == 0)
403 printf("%s:\n", pp->printer);
405 if (sump_opts & SUMP_CHDIR_SD) {
407 cdres = chdir(pp->spool_dir);
410 printf("\tcannot chdir to %s\n", pp->spool_dir);