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 $
37 * $DragonFly: src/usr.sbin/lpr/lpc/lpc.c,v 1.7 2006/01/14 22:58:18 corecode Exp $
40 #include <sys/param.h>
60 #define LPR_OPER "operator" /* group name of lpr operators */
64 * lpc -- line printer control program
67 #define MAX_CMDLINE 200
71 static char cmdline
[MAX_CMDLINE
];
73 static char *margv
[MAX_MARGV
];
76 int main(int _argc
, char **_argv
);
77 static void cmdscanner(void);
78 static struct cmd
*getcmd(const char *_name
);
79 static void intr(int _signo
);
80 static void makeargv(void);
81 static int ingroup(const char *_grname
);
84 main(int argc
, char **argv
)
92 openlog("lpd", 0, LOG_LPR
);
96 if (c
== (struct cmd
*)-1) {
97 printf("?Ambiguous command\n");
101 printf("?Invalid command\n");
104 if ((c
->c_opts
& LPC_PRIVCMD
) && getuid() &&
105 ingroup(LPR_OPER
) == 0) {
106 printf("?Privileged command\n");
109 if (c
->c_generic
!= 0)
110 generic(c
->c_generic
, c
->c_opts
, c
->c_handler
,
113 (*c
->c_handler
)(argc
, argv
);
116 fromatty
= isatty(fileno(stdin
));
118 signal(SIGINT
, intr
);
125 intr(int signo __unused
)
127 /* (the '__unused' is just to avoid a compile-time warning) */
145 static History
*hist
;
158 el
= el_init("lpc", stdin
, stdout
, stderr
);
159 hist
= history_init();
160 history(hist
, &he
, H_SETSIZE
, 100);
161 el_set(el
, EL_HIST
, history
, hist
);
162 el_set(el
, EL_EDITOR
, "emacs");
163 el_set(el
, EL_PROMPT
, lpc_prompt
);
164 el_set(el
, EL_SIGNAL
, 1);
167 * EditLine init may call 'cgetset()' to set a
168 * capability-db meant for termcap (eg: to set
169 * terminal type 'xterm'). Reset that now, or
170 * that same db-information will be used for
171 * printcap (giving us an "xterm" printer, with
172 * all kinds of invalid capabilities...).
176 if ((bp
= el_gets(el
, &num
)) == NULL
|| num
== 0)
179 len
= (num
> MAX_CMDLINE
-1) ? MAX_CMDLINE
-1 : num
;
180 memcpy(cmdline
, bp
, len
);
182 history(hist
, &he
, H_ENTER
, bp
);
185 if (fgets(cmdline
, MAX_CMDLINE
, stdin
) == 0)
187 if (cmdline
[0] == 0 || cmdline
[0] == '\n')
194 if (el
!= NULL
&& el_parse(el
, margc
, (const char **)margv
) != -1)
197 c
= getcmd(margv
[0]);
198 if (c
== (struct cmd
*)-1) {
199 printf("?Ambiguous command\n");
203 printf("?Invalid command\n");
206 if ((c
->c_opts
& LPC_PRIVCMD
) && getuid() &&
207 ingroup(LPR_OPER
) == 0) {
208 printf("?Privileged command\n");
213 * Two different commands might have the same generic rtn
214 * (eg: "clean" and "tclean"), and just use different
215 * handler routines for distinct command-setup. The handler
216 * routine might also be set on a generic routine for
217 * initial parameter processing.
219 if (c
->c_generic
!= 0)
220 generic(c
->c_generic
, c
->c_opts
, c
->c_handler
,
223 (*c
->c_handler
)(margc
, margv
);
228 getcmd(const char *name
)
231 struct cmd
*c
, *found
;
232 int nmatches
, longest
;
237 for (c
= cmdtab
; (p
= c
->c_name
); c
++) {
238 for (q
= name
; *q
== *p
++; q
++)
239 if (*q
== 0) /* exact match? */
241 if (!*q
) { /* the name was a prefix */
242 if (q
- name
> longest
) {
246 } else if (q
- name
== longest
)
251 return((struct cmd
*)-1);
256 * Slice a string up into argc/argv.
266 for (cp
= cmdline
; *cp
&& (size_t)(cp
- cmdline
) < sizeof(cmdline
) &&
267 n
< MAX_MARGV
-1; n
++) {
274 while (*cp
!= '\0' && !isspace(*cp
))
283 #define HELPINDENT (sizeof ("directory"))
289 help(int argc
, char **argv
)
295 int columns
, width
= 0, lines
;
297 printf("Commands may be abbreviated. Commands are:\n\n");
298 for (c
= cmdtab
; c
->c_name
; c
++) {
299 int len
= strlen(c
->c_name
);
304 width
= (width
+ 8) &~ 7;
305 columns
= 80 / width
;
308 lines
= (NCMDS
+ columns
- 1) / columns
;
309 for (i
= 0; i
< lines
; i
++) {
310 for (j
= 0; j
< columns
; j
++) {
311 c
= cmdtab
+ j
* lines
+ i
;
313 printf("%s", c
->c_name
);
314 if (c
+ lines
>= &cmdtab
[NCMDS
]) {
318 w
= strlen(c
->c_name
);
332 if (c
== (struct cmd
*)-1)
333 printf("?Ambiguous help command %s\n", arg
);
335 printf("?Invalid help command %s\n", arg
);
337 printf("%-*s\t%s\n", (int) HELPINDENT
,
338 c
->c_name
, c
->c_help
);
343 * return non-zero if the user is a member of the given group
346 ingroup(const char *grname
)
348 static struct group
*gptr
=NULL
;
349 static int ngroups
= 0;
350 static gid_t groups
[NGROUPS
];
355 if ((gptr
= getgrnam(grname
)) == NULL
) {
356 warnx("warning: unknown group '%s'", grname
);
359 ngroups
= getgroups(NGROUPS
, groups
);
364 for (i
= 0; i
< ngroups
; i
++)
365 if (gid
== groups
[i
])
371 * Routine to get the information for a single printer (which will be
372 * called by the routines which implement individual commands).
373 * Note: This is for commands operating on a *single* printer.
376 setup_myprinter(char *pwanted
, struct printer
*pp
, int sump_opts
)
378 int cdres
, cmdstatus
;
381 cmdstatus
= getprintcap(pwanted
, pp
);
384 fatal(pp
, "%s", pcaperr(cmdstatus
));
386 case PCAPERR_NOTFOUND
:
387 printf("unknown printer %s\n", pwanted
);
390 printf("warning: %s: unresolved tc= reference(s)", pwanted
);
392 case PCAPERR_SUCCESS
:
395 if ((sump_opts
& SUMP_NOHEADER
) == 0)
396 printf("%s:\n", pp
->printer
);
398 if (sump_opts
& SUMP_CHDIR_SD
) {
400 cdres
= chdir(pp
->spool_dir
);
403 printf("\tcannot chdir to %s\n", pp
->spool_dir
);