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 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1983, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
39 static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
43 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
66 #define LPR_OPER "operator" /* group name of lpr operators */
70 * lpc -- line printer control program
73 #define MAX_CMDLINE 200
77 static char cmdline[MAX_CMDLINE];
79 static char *margv[MAX_MARGV];
82 int main(int _argc, char *_argv[]);
83 static void cmdscanner(void);
84 static struct cmd *getcmd(const char *_name);
85 static void intr(int _signo);
86 static void makeargv(void);
87 static int ingroup(const char *_grname);
90 main(int argc, char *argv[])
92 register struct cmd *c;
98 openlog("lpd", 0, LOG_LPR);
102 if (c == (struct cmd *)-1) {
103 printf("?Ambiguous command\n");
107 printf("?Invalid command\n");
110 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
111 ingroup(LPR_OPER) == 0) {
112 printf("?Privileged command\n");
115 if (c->c_generic != 0)
116 generic(c->c_generic, c->c_opts, c->c_handler,
119 (*c->c_handler)(argc, argv);
122 fromatty = isatty(fileno(stdin));
124 signal(SIGINT, intr);
131 intr(int signo __unused)
133 /* (the '__unused' is just to avoid a compile-time warning) */
149 register struct cmd *c;
151 static History *hist;
164 el = el_init("lpc", stdin, stdout, stderr);
165 hist = history_init();
166 history(hist, &he, H_SETSIZE, 100);
167 el_set(el, EL_HIST, history, hist);
168 el_set(el, EL_EDITOR, "emacs");
169 el_set(el, EL_PROMPT, lpc_prompt);
170 el_set(el, EL_SIGNAL, 1);
173 * EditLine init may call 'cgetset()' to set a
174 * capability-db meant for termcap (eg: to set
175 * terminal type 'xterm'). Reset that now, or
176 * that same db-information will be used for
177 * printcap (giving us an "xterm" printer, with
178 * all kinds of invalid capabilities...).
182 if ((bp = el_gets(el, &num)) == NULL || num == 0)
185 len = (num > MAX_CMDLINE - 1) ? MAX_CMDLINE - 1 : num;
186 memcpy(cmdline, bp, len);
188 history(hist, &he, H_ENTER, bp);
191 if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
193 if (cmdline[0] == 0 || cmdline[0] == '\n')
200 if (el != NULL && el_parse(el, margc, margv) != -1)
203 c = getcmd(margv[0]);
204 if (c == (struct cmd *)-1) {
205 printf("?Ambiguous command\n");
209 printf("?Invalid command\n");
212 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
213 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_opts, c->c_handler,
229 (*c->c_handler)(margc, margv);
234 getcmd(const char *name)
236 register const char *p, *q;
237 register struct cmd *c, *found;
238 register int nmatches, longest;
243 for (c = cmdtab; (p = c->c_name); c++) {
244 for (q = name; *q == *p++; q++)
245 if (*q == 0) /* exact match? */
247 if (!*q) { /* the name was a prefix */
248 if (q - name > longest) {
252 } else if (q - name == longest)
257 return((struct cmd *)-1);
262 * Slice a string up into argc/argv.
268 register char **argp = margv;
272 for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) &&
273 n < MAX_MARGV - 1; n++) {
280 while (*cp != '\0' && !isspace(*cp))
289 #define HELPINDENT (sizeof ("directory"))
295 help(int argc, char *argv[])
297 register struct cmd *c;
300 register int i, j, w;
301 int columns, width = 0, lines;
303 printf("Commands may be abbreviated. Commands are:\n\n");
304 for (c = cmdtab; c->c_name; c++) {
305 int len = strlen(c->c_name);
310 width = (width + 8) &~ 7;
311 columns = 80 / width;
314 lines = (NCMDS + columns - 1) / columns;
315 for (i = 0; i < lines; i++) {
316 for (j = 0; j < columns; j++) {
317 c = cmdtab + j * lines + i;
319 printf("%s", c->c_name);
320 if (c + lines >= &cmdtab[NCMDS]) {
324 w = strlen(c->c_name);
337 if (c == (struct cmd *)-1)
338 printf("?Ambiguous help command %s\n", arg);
339 else if (c == (struct cmd *)0)
340 printf("?Invalid help command %s\n", arg);
342 printf("%-*s\t%s\n", (int) HELPINDENT,
343 c->c_name, c->c_help);
348 * return non-zero if the user is a member of the given group
351 ingroup(const char *grname)
353 static struct group *gptr=NULL;
354 static int ngroups = 0;
355 static long ngroups_max;
356 static gid_t *groups;
361 if ((gptr = getgrnam(grname)) == NULL) {
362 warnx("warning: unknown group '%s'", grname);
365 ngroups_max = sysconf(_SC_NGROUPS_MAX);
366 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
368 ngroups = getgroups(ngroups_max, groups);
373 for (i = 0; i < ngroups; i++)
374 if (gid == groups[i])
380 * Routine to get the information for a single printer (which will be
381 * called by the routines which implement individual commands).
382 * Note: This is for commands operating on a *single* printer.
385 setup_myprinter(char *pwanted, struct printer *pp, int sump_opts)
387 int cdres, cmdstatus;
390 cmdstatus = getprintcap(pwanted, pp);
393 fatal(pp, "%s", pcaperr(cmdstatus));
395 case PCAPERR_NOTFOUND:
396 printf("unknown printer %s\n", pwanted);
399 printf("warning: %s: unresolved tc= reference(s)", pwanted);
401 case PCAPERR_SUCCESS:
404 if ((sump_opts & SUMP_NOHEADER) == 0)
405 printf("%s:\n", pp->printer);
407 if (sump_opts & SUMP_CHDIR_SD) {
409 cdres = chdir(pp->spool_dir);
412 printf("\tcannot chdir to %s\n", pp->spool_dir);