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 $
39 #include <sys/param.h>
59 #define LPR_OPER "operator" /* group name of lpr operators */
63 * lpc -- line printer control program
66 #define MAX_CMDLINE 200
70 static char cmdline
[MAX_CMDLINE
];
72 static char *margv
[MAX_MARGV
];
75 static void cmdscanner(void);
76 static struct cmd
*getcmd(const char *_name
);
77 static void intr(int _signo
);
78 static void makeargv(void);
79 static int ingroup(const char *_grname
);
82 main(int argc
, char **argv
)
90 openlog("lpd", 0, LOG_LPR
);
94 if (c
== (struct cmd
*)-1) {
95 printf("?Ambiguous command\n");
99 printf("?Invalid command\n");
102 if ((c
->c_opts
& LPC_PRIVCMD
) && getuid() &&
103 ingroup(LPR_OPER
) == 0) {
104 printf("?Privileged command\n");
107 if (c
->c_generic
!= 0)
108 generic(c
->c_generic
, c
->c_opts
, c
->c_handler
,
111 (*c
->c_handler
)(argc
, argv
);
114 fromatty
= isatty(fileno(stdin
));
116 signal(SIGINT
, intr
);
123 intr(int signo __unused
)
125 /* (the '__unused' is just to avoid a compile-time warning) */
143 static History
*hist
;
156 el
= el_init("lpc", stdin
, stdout
, stderr
);
157 hist
= history_init();
158 history(hist
, &he
, H_SETSIZE
, 100);
159 el_set(el
, EL_HIST
, history
, hist
);
160 el_set(el
, EL_EDITOR
, "emacs");
161 el_set(el
, EL_PROMPT
, lpc_prompt
);
162 el_set(el
, EL_SIGNAL
, 1);
165 * EditLine init may call 'cgetset()' to set a
166 * capability-db meant for termcap (eg: to set
167 * terminal type 'xterm'). Reset that now, or
168 * that same db-information will be used for
169 * printcap (giving us an "xterm" printer, with
170 * all kinds of invalid capabilities...).
174 if ((bp
= el_gets(el
, &num
)) == NULL
|| num
== 0)
177 len
= (num
> MAX_CMDLINE
-1) ? MAX_CMDLINE
-1 : num
;
178 memcpy(cmdline
, bp
, len
);
180 history(hist
, &he
, H_ENTER
, bp
);
183 if (fgets(cmdline
, MAX_CMDLINE
, stdin
) == 0)
185 if (cmdline
[0] == 0 || cmdline
[0] == '\n')
192 if (el
!= NULL
&& el_parse(el
, margc
, (const char **)margv
) != -1)
195 c
= getcmd(margv
[0]);
196 if (c
== (struct cmd
*)-1) {
197 printf("?Ambiguous command\n");
201 printf("?Invalid command\n");
204 if ((c
->c_opts
& LPC_PRIVCMD
) && getuid() &&
205 ingroup(LPR_OPER
) == 0) {
206 printf("?Privileged command\n");
211 * Two different commands might have the same generic rtn
212 * (eg: "clean" and "tclean"), and just use different
213 * handler routines for distinct command-setup. The handler
214 * routine might also be set on a generic routine for
215 * initial parameter processing.
217 if (c
->c_generic
!= 0)
218 generic(c
->c_generic
, c
->c_opts
, c
->c_handler
,
221 (*c
->c_handler
)(margc
, margv
);
226 getcmd(const char *name
)
229 struct cmd
*c
, *found
;
230 int nmatches
, longest
;
235 for (c
= cmdtab
; (p
= c
->c_name
); c
++) {
236 for (q
= name
; *q
== *p
++; q
++)
237 if (*q
== 0) /* exact match? */
239 if (!*q
) { /* the name was a prefix */
240 if (q
- name
> longest
) {
244 } else if (q
- name
== longest
)
249 return((struct cmd
*)-1);
254 * Slice a string up into argc/argv.
264 for (cp
= cmdline
; *cp
&& (size_t)(cp
- cmdline
) < sizeof(cmdline
) &&
265 n
< MAX_MARGV
-1; n
++) {
272 while (*cp
!= '\0' && !isspace(*cp
))
281 #define HELPINDENT (sizeof ("directory"))
287 help(int argc
, char **argv
)
293 int columns
, width
= 0, lines
;
295 printf("Commands may be abbreviated. Commands are:\n\n");
296 for (c
= cmdtab
; c
->c_name
; c
++) {
297 int len
= strlen(c
->c_name
);
302 width
= (width
+ 8) &~ 7;
303 columns
= 80 / width
;
306 lines
= (NCMDS
+ columns
- 1) / columns
;
307 for (i
= 0; i
< lines
; i
++) {
308 for (j
= 0; j
< columns
; j
++) {
309 c
= cmdtab
+ j
* lines
+ i
;
311 printf("%s", c
->c_name
);
312 if (c
+ lines
>= &cmdtab
[NCMDS
]) {
316 w
= strlen(c
->c_name
);
330 if (c
== (struct cmd
*)-1)
331 printf("?Ambiguous help command %s\n", arg
);
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
);