2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
45 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
68 #define LPR_OPER "operator" /* group name of lpr operators */
72 * lpc -- line printer control program
75 #define MAX_CMDLINE 200
79 static char cmdline[MAX_CMDLINE];
81 static char *margv[MAX_MARGV];
84 int main(int _argc, char *_argv[]);
85 static void cmdscanner(void);
86 static struct cmd *getcmd(const char *_name);
87 static void intr(int _signo);
88 static void makeargv(void);
89 static int ingroup(const char *_grname);
92 main(int argc, char *argv[])
94 register struct cmd *c;
100 openlog("lpd", 0, LOG_LPR);
104 if (c == (struct cmd *)-1) {
105 printf("?Ambiguous command\n");
109 printf("?Invalid command\n");
112 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
113 ingroup(LPR_OPER) == 0) {
114 printf("?Privileged command\n");
117 if (c->c_generic != NULL)
118 generic(c->c_generic, c->c_opts, c->c_handler,
121 (*c->c_handler)(argc, argv);
124 fromatty = isatty(fileno(stdin));
126 signal(SIGINT, intr);
133 intr(int signo __unused)
135 /* (the '__unused' is just to avoid a compile-time warning) */
151 register struct cmd *c;
153 static History *hist;
166 el = el_init("lpc", stdin, stdout, stderr);
167 hist = history_init();
168 history(hist, &he, H_SETSIZE, 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 = MIN(MAX_CMDLINE - 1, num);
188 memcpy(cmdline, bp, len);
190 history(hist, &he, H_ENTER, bp);
193 if (fgets(cmdline, MAX_CMDLINE, stdin) == NULL)
195 if (cmdline[0] == 0 || cmdline[0] == '\n')
202 if (el != NULL && el_parse(el, margc, (const char **)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 != NULL)
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 - 1; 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 long ngroups_max;
358 static gid_t *groups;
363 if ((gptr = getgrnam(grname)) == NULL) {
364 warnx("warning: unknown group '%s'", grname);
367 ngroups_max = sysconf(_SC_NGROUPS_MAX);
368 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
370 ngroups = getgroups(ngroups_max, groups);
375 for (i = 0; i < ngroups; i++)
376 if (gid == groups[i])
382 * Routine to get the information for a single printer (which will be
383 * called by the routines which implement individual commands).
384 * Note: This is for commands operating on a *single* printer.
387 setup_myprinter(char *pwanted, struct printer *pp, int sump_opts)
389 int cdres, cmdstatus;
392 cmdstatus = getprintcap(pwanted, pp);
395 fatal(pp, "%s", pcaperr(cmdstatus));
397 case PCAPERR_NOTFOUND:
398 printf("unknown printer %s\n", pwanted);
401 printf("warning: %s: unresolved tc= reference(s)", pwanted);
403 case PCAPERR_SUCCESS:
406 if ((sump_opts & SUMP_NOHEADER) == 0)
407 printf("%s:\n", pp->printer);
409 if (sump_opts & SUMP_CHDIR_SD) {
411 cdres = chdir(pp->spool_dir);
414 printf("\tcannot chdir to %s\n", pp->spool_dir);