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";
47 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
48 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
70 #define LPR_OPER "operator" /* group name of lpr operators */
74 * lpc -- line printer control program
77 #define MAX_CMDLINE 200
81 static char cmdline[MAX_CMDLINE];
83 static char *margv[MAX_MARGV];
86 int main(int _argc, char *_argv[]);
87 static void cmdscanner(void);
88 static struct cmd *getcmd(const char *_name);
89 static void intr(int _signo);
90 static void makeargv(void);
91 static int ingroup(const char *_grname);
94 main(int argc, char *argv[])
96 register struct cmd *c;
102 openlog("lpd", 0, LOG_LPR);
106 if (c == (struct cmd *)-1) {
107 printf("?Ambiguous command\n");
111 printf("?Invalid command\n");
114 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
115 ingroup(LPR_OPER) == 0) {
116 printf("?Privileged command\n");
119 if (c->c_generic != 0)
120 generic(c->c_generic, c->c_opts, c->c_handler,
123 (*c->c_handler)(argc, argv);
126 fromatty = isatty(fileno(stdin));
128 signal(SIGINT, intr);
135 intr(int signo __unused)
137 /* (the '__unused' is just to avoid a compile-time warning) */
153 register struct cmd *c;
155 static History *hist;
168 el = el_init("lpc", stdin, stdout, stderr);
169 hist = history_init();
170 history(hist, &he, H_SETSIZE, 100);
171 el_set(el, EL_HIST, history, hist);
172 el_set(el, EL_EDITOR, "emacs");
173 el_set(el, EL_PROMPT, lpc_prompt);
174 el_set(el, EL_SIGNAL, 1);
177 * EditLine init may call 'cgetset()' to set a
178 * capability-db meant for termcap (eg: to set
179 * terminal type 'xterm'). Reset that now, or
180 * that same db-information will be used for
181 * printcap (giving us an "xterm" printer, with
182 * all kinds of invalid capabilities...).
186 if ((bp = el_gets(el, &num)) == NULL || num == 0)
189 len = (num > MAX_CMDLINE - 1) ? MAX_CMDLINE - 1 : num;
190 memcpy(cmdline, bp, len);
192 history(hist, &he, H_ENTER, bp);
195 if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
197 if (cmdline[0] == 0 || cmdline[0] == '\n')
204 if (el != NULL && el_parse(el, margc, margv) != -1)
207 c = getcmd(margv[0]);
208 if (c == (struct cmd *)-1) {
209 printf("?Ambiguous command\n");
213 printf("?Invalid command\n");
216 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
217 ingroup(LPR_OPER) == 0) {
218 printf("?Privileged command\n");
223 * Two different commands might have the same generic rtn
224 * (eg: "clean" and "tclean"), and just use different
225 * handler routines for distinct command-setup. The handler
226 * routine might also be set on a generic routine for
227 * initial parameter processing.
229 if (c->c_generic != 0)
230 generic(c->c_generic, c->c_opts, c->c_handler,
233 (*c->c_handler)(margc, margv);
238 getcmd(const char *name)
240 register const char *p, *q;
241 register struct cmd *c, *found;
242 register int nmatches, longest;
247 for (c = cmdtab; (p = c->c_name); c++) {
248 for (q = name; *q == *p++; q++)
249 if (*q == 0) /* exact match? */
251 if (!*q) { /* the name was a prefix */
252 if (q - name > longest) {
256 } else if (q - name == longest)
261 return((struct cmd *)-1);
266 * Slice a string up into argc/argv.
272 register char **argp = margv;
276 for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) &&
277 n < MAX_MARGV - 1; n++) {
284 while (*cp != '\0' && !isspace(*cp))
293 #define HELPINDENT (sizeof ("directory"))
299 help(int argc, char *argv[])
301 register struct cmd *c;
304 register int i, j, w;
305 int columns, width = 0, lines;
307 printf("Commands may be abbreviated. Commands are:\n\n");
308 for (c = cmdtab; c->c_name; c++) {
309 int len = strlen(c->c_name);
314 width = (width + 8) &~ 7;
315 columns = 80 / width;
318 lines = (NCMDS + columns - 1) / columns;
319 for (i = 0; i < lines; i++) {
320 for (j = 0; j < columns; j++) {
321 c = cmdtab + j * lines + i;
323 printf("%s", c->c_name);
324 if (c + lines >= &cmdtab[NCMDS]) {
328 w = strlen(c->c_name);
341 if (c == (struct cmd *)-1)
342 printf("?Ambiguous help command %s\n", arg);
343 else if (c == (struct cmd *)0)
344 printf("?Invalid help command %s\n", arg);
346 printf("%-*s\t%s\n", (int) HELPINDENT,
347 c->c_name, c->c_help);
352 * return non-zero if the user is a member of the given group
355 ingroup(const char *grname)
357 static struct group *gptr=NULL;
358 static int ngroups = 0;
359 static long ngroups_max;
360 static gid_t *groups;
365 if ((gptr = getgrnam(grname)) == NULL) {
366 warnx("warning: unknown group '%s'", grname);
369 ngroups_max = sysconf(_SC_NGROUPS_MAX);
370 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
372 ngroups = getgroups(ngroups_max, groups);
377 for (i = 0; i < ngroups; i++)
378 if (gid == groups[i])
384 * Routine to get the information for a single printer (which will be
385 * called by the routines which implement individual commands).
386 * Note: This is for commands operating on a *single* printer.
389 setup_myprinter(char *pwanted, struct printer *pp, int sump_opts)
391 int cdres, cmdstatus;
394 cmdstatus = getprintcap(pwanted, pp);
397 fatal(pp, "%s", pcaperr(cmdstatus));
399 case PCAPERR_NOTFOUND:
400 printf("unknown printer %s\n", pwanted);
403 printf("warning: %s: unresolved tc= reference(s)", pwanted);
405 case PCAPERR_SUCCESS:
408 if ((sump_opts & SUMP_NOHEADER) == 0)
409 printf("%s:\n", pp->printer);
411 if (sump_opts & SUMP_CHDIR_SD) {
413 cdres = chdir(pp->spool_dir);
416 printf("\tcannot chdir to %s\n", pp->spool_dir);