1 /* $NetBSD: lpc.c,v 1.24 2008/07/21 13:36:58 lukem Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
36 The Regents of the University of California. All rights reserved.");
38 static char sccsid
[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
40 __RCSID("$NetBSD: lpc.c,v 1.24 2008/07/21 13:36:58 lukem Exp $");
44 #include <sys/param.h>
63 #define LPR_OPER "operator" /* group name of lpr operators */
67 * lpc -- line printer control program
75 char *margv
[MAX_MARGV
];
85 static void cmdscanner(int);
86 static struct cmd
*getcmd(const char *);
87 static void intr(int);
88 static void makeargv(void);
89 static int ingroup(const char *);
90 int main(int, char *p
[]);
91 const char *prompt(void);
92 static int parse(char *, char *p
[], int);
95 main(int argc
, char *argv
[])
100 setprogname(argv
[0]);
101 openlog("lpd", 0, LOG_LPR
);
105 exit(!parse(*argv
, argv
, argc
));
107 fromatty
= isatty(fileno(stdin
));
108 top
= setjmp(toplevel
) == 0;
110 signal(SIGINT
, intr
);
118 parse(char *arg
, char **pargv
, int pargc
)
123 if (c
== (struct cmd
*)-1) {
124 printf("?Ambiguous command\n");
128 printf("?Invalid command\n");
131 if (c
->c_priv
&& getuid() && ingroup(LPR_OPER
) == 0) {
132 printf("?Privileged command\n");
135 (*c
->c_handler
)(pargc
, pargv
);
146 longjmp(toplevel
, 1);
166 hist
= history_init();
167 history(hist
, &he
, H_SETSIZE
, 100); /* 100 elt history buffer */
169 elptr
= el_init(getprogname(), stdin
, stdout
, stderr
);
170 el_set(elptr
, EL_EDITOR
, "emacs");
171 el_set(elptr
, EL_PROMPT
, prompt
);
172 el_set(elptr
, EL_HIST
, history
, hist
);
173 el_source(elptr
, NULL
);
178 if (((elline
= el_gets(elptr
, &scratch
)) != NULL
)
180 history(hist
, &he
, H_ENTER
, elline
);
181 cmdline
= strdup(elline
);
187 } while (margc
== 0);
190 if (!parse(cmdline
, margv
, margc
)) {
199 longjmp(toplevel
, 0);
203 getcmd(const char *name
)
206 struct cmd
*c
, *found
;
207 int nmatches
, longest
;
212 for (c
= cmdtab
; (p
= c
->c_name
) != NULL
; c
++) {
213 for (q
= name
; *q
== *p
++; q
++)
214 if (*q
== 0) /* exact match? */
216 if (!*q
) { /* the name was a prefix */
217 if (q
- name
> longest
) {
221 } else if (q
- name
== longest
)
226 return((struct cmd
*)-1);
231 * Slice a string up into argc/argv.
241 s
= strlen(cmdline
) + 1;
243 for (cp
= cmdline
; *cp
&& (size_t)(cp
- cmdline
) < s
&& n
< MAX_MARGV
; n
++) {
244 while (isspace((unsigned char)*cp
))
250 while (*cp
!= '\0' && !isspace((unsigned char)*cp
))
259 #define HELPINDENT (sizeof ("directory"))
265 help(int argc
, char *argv
[])
271 size_t columns
, width
= 0, lines
;
273 printf("Commands may be abbreviated. Commands are:\n\n");
274 for (c
= cmdtab
; c
->c_name
; c
++) {
275 size_t len
= strlen(c
->c_name
);
280 width
= (width
+ 8) &~ 7;
281 columns
= 80 / width
;
284 lines
= (NCMDS
+ columns
- 1) / columns
;
285 for (i
= 0; i
< lines
; i
++) {
286 for (j
= 0; j
< columns
; j
++) {
287 c
= cmdtab
+ j
* lines
+ i
;
288 printf("%s", c
->c_name
);
289 if (c
+ lines
>= &cmdtab
[NCMDS
- 1]) {
293 w
= strlen(c
->c_name
);
306 if (c
== (struct cmd
*)-1)
307 printf("?Ambiguous help command %s\n", arg
);
308 else if (c
== (struct cmd
*)0)
309 printf("?Invalid help command %s\n", arg
);
311 printf("%-*s\t%s\n", (int)HELPINDENT
,
312 c
->c_name
, c
->c_help
);
317 * return non-zero if the user is a member of the given group
320 ingroup(const char *grname
)
322 static struct group
*gptr
= NULL
;
323 static gid_t groups
[NGROUPS
];
329 if ((gptr
= getgrnam(grname
)) == NULL
) {
330 warnx("Warning: unknown group `%s'",
334 ngroups
= getgroups(NGROUPS
, groups
);
339 for (i
= 0; i
< ngroups
; i
++)
340 if (gid
== groups
[i
])