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. 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
30 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
31 * @(#)lpc.c 8.3 (Berkeley) 4/28/95
32 * $FreeBSD: src/usr.sbin/lpr/lpc/lpc.c,v 1.13.2.11 2002/07/26 03:12:07 gad Exp $
35 #include <sys/param.h>
55 #define LPR_OPER "operator" /* group name of lpr operators */
59 * lpc -- line printer control program
62 #define MAX_CMDLINE 200
66 static char cmdline
[MAX_CMDLINE
];
68 static char *margv
[MAX_MARGV
];
71 static void cmdscanner(void);
72 static struct cmd
*getcmd(const char *_name
);
73 static void intr(int _signo
);
74 static void makeargv(void);
75 static int ingroup(const char *_grname
);
78 main(int argc
, char **argv
)
86 openlog("lpd", 0, LOG_LPR
);
90 if (c
== (struct cmd
*)-1) {
91 printf("?Ambiguous command\n");
95 printf("?Invalid command\n");
98 if ((c
->c_opts
& LPC_PRIVCMD
) && getuid() &&
99 ingroup(LPR_OPER
) == 0) {
100 printf("?Privileged command\n");
103 if (c
->c_generic
!= 0)
104 generic(c
->c_generic
, c
->c_opts
, c
->c_handler
,
107 (*c
->c_handler
)(argc
, argv
);
110 fromatty
= isatty(fileno(stdin
));
112 signal(SIGINT
, intr
);
119 intr(int signo __unused
)
121 /* (the '__unused' is just to avoid a compile-time warning) */
139 static History
*hist
;
152 el
= el_init("lpc", stdin
, stdout
, stderr
);
153 hist
= history_init();
154 history(hist
, &he
, H_SETSIZE
, 100);
155 el_set(el
, EL_HIST
, history
, hist
);
156 el_set(el
, EL_EDITOR
, "emacs");
157 el_set(el
, EL_PROMPT
, lpc_prompt
);
158 el_set(el
, EL_SIGNAL
, 1);
161 * EditLine init may call 'cgetset()' to set a
162 * capability-db meant for termcap (eg: to set
163 * terminal type 'xterm'). Reset that now, or
164 * that same db-information will be used for
165 * printcap (giving us an "xterm" printer, with
166 * all kinds of invalid capabilities...).
170 if ((bp
= el_gets(el
, &num
)) == NULL
|| num
== 0)
173 len
= (num
> MAX_CMDLINE
-1) ? MAX_CMDLINE
-1 : num
;
174 memcpy(cmdline
, bp
, len
);
176 history(hist
, &he
, H_ENTER
, bp
);
179 if (fgets(cmdline
, MAX_CMDLINE
, stdin
) == 0)
181 if (cmdline
[0] == 0 || cmdline
[0] == '\n')
188 if (el
!= NULL
&& el_parse(el
, margc
, (const char **)margv
) != -1)
191 c
= getcmd(margv
[0]);
192 if (c
== (struct cmd
*)-1) {
193 printf("?Ambiguous command\n");
197 printf("?Invalid command\n");
200 if ((c
->c_opts
& LPC_PRIVCMD
) && getuid() &&
201 ingroup(LPR_OPER
) == 0) {
202 printf("?Privileged command\n");
207 * Two different commands might have the same generic rtn
208 * (eg: "clean" and "tclean"), and just use different
209 * handler routines for distinct command-setup. The handler
210 * routine might also be set on a generic routine for
211 * initial parameter processing.
213 if (c
->c_generic
!= 0)
214 generic(c
->c_generic
, c
->c_opts
, c
->c_handler
,
217 (*c
->c_handler
)(margc
, margv
);
222 getcmd(const char *name
)
225 struct cmd
*c
, *found
;
226 int nmatches
, longest
;
231 for (c
= cmdtab
; (p
= c
->c_name
); c
++) {
232 for (q
= name
; *q
== *p
++; q
++)
233 if (*q
== 0) /* exact match? */
235 if (!*q
) { /* the name was a prefix */
236 if (q
- name
> longest
) {
240 } else if (q
- name
== longest
)
245 return((struct cmd
*)-1);
250 * Slice a string up into argc/argv.
260 for (cp
= cmdline
; *cp
&& (size_t)(cp
- cmdline
) < sizeof(cmdline
) &&
261 n
< MAX_MARGV
-1; n
++) {
268 while (*cp
!= '\0' && !isspace(*cp
))
277 #define HELPINDENT (sizeof ("directory"))
283 help(int argc
, char **argv
)
289 int columns
, width
= 0, lines
;
291 printf("Commands may be abbreviated. Commands are:\n\n");
292 for (c
= cmdtab
; c
->c_name
; c
++) {
293 int len
= strlen(c
->c_name
);
298 width
= (width
+ 8) &~ 7;
299 columns
= 80 / width
;
302 lines
= (NCMDS
+ columns
- 1) / columns
;
303 for (i
= 0; i
< lines
; i
++) {
304 for (j
= 0; j
< columns
; j
++) {
305 c
= cmdtab
+ j
* lines
+ i
;
307 printf("%s", c
->c_name
);
308 if (c
+ lines
>= &cmdtab
[NCMDS
]) {
312 w
= strlen(c
->c_name
);
326 if (c
== (struct cmd
*)-1)
327 printf("?Ambiguous help command %s\n", arg
);
329 printf("?Invalid help command %s\n", arg
);
331 printf("%-*s\t%s\n", (int) HELPINDENT
,
332 c
->c_name
, c
->c_help
);
337 * return non-zero if the user is a member of the given group
340 ingroup(const char *grname
)
342 static struct group
*gptr
=NULL
;
343 static int ngroups
= 0;
344 static gid_t groups
[NGROUPS
];
349 if ((gptr
= getgrnam(grname
)) == NULL
) {
350 warnx("warning: unknown group '%s'", grname
);
353 ngroups
= getgroups(NGROUPS
, groups
);
358 for (i
= 0; i
< ngroups
; i
++)
359 if (gid
== groups
[i
])
365 * Routine to get the information for a single printer (which will be
366 * called by the routines which implement individual commands).
367 * Note: This is for commands operating on a *single* printer.
370 setup_myprinter(char *pwanted
, struct printer
*pp
, int sump_opts
)
372 int cdres
, cmdstatus
;
375 cmdstatus
= getprintcap(pwanted
, pp
);
378 fatal(pp
, "%s", pcaperr(cmdstatus
));
380 case PCAPERR_NOTFOUND
:
381 printf("unknown printer %s\n", pwanted
);
384 printf("warning: %s: unresolved tc= reference(s)", pwanted
);
386 case PCAPERR_SUCCESS
:
389 if ((sump_opts
& SUMP_NOHEADER
) == 0)
390 printf("%s:\n", pp
->printer
);
392 if (sump_opts
& SUMP_CHDIR_SD
) {
394 cdres
= chdir(pp
->spool_dir
);
397 printf("\tcannot chdir to %s\n", pp
->spool_dir
);