Home | History | Annotate | Line # | Download | only in lpc
lpc.c revision 1.6
      1  1.6    mikel /*	$NetBSD: lpc.c,v 1.6 1997/07/10 05:34:48 mikel Exp $	*/
      2  1.1      cgd /*
      3  1.3      cgd  * Copyright (c) 1983, 1993
      4  1.3      cgd  *	The Regents of the University of California.  All rights reserved.
      5  1.3      cgd  *
      6  1.1      cgd  *
      7  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1      cgd  * modification, are permitted provided that the following conditions
      9  1.1      cgd  * are met:
     10  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1      cgd  *    must display the following acknowledgement:
     17  1.1      cgd  *	This product includes software developed by the University of
     18  1.1      cgd  *	California, Berkeley and its contributors.
     19  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1      cgd  *    may be used to endorse or promote products derived from this software
     21  1.1      cgd  *    without specific prior written permission.
     22  1.1      cgd  *
     23  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      cgd  * SUCH DAMAGE.
     34  1.1      cgd  */
     35  1.1      cgd 
     36  1.1      cgd #ifndef lint
     37  1.3      cgd static char copyright[] =
     38  1.3      cgd "@(#) Copyright (c) 1983, 1993\n\
     39  1.3      cgd 	The Regents of the University of California.  All rights reserved.\n";
     40  1.1      cgd #endif /* not lint */
     41  1.1      cgd 
     42  1.1      cgd #ifndef lint
     43  1.3      cgd static char sccsid[] = "@(#)lpc.c	8.1 (Berkeley) 6/6/93";
     44  1.1      cgd #endif /* not lint */
     45  1.1      cgd 
     46  1.3      cgd #include <sys/param.h>
     47  1.3      cgd 
     48  1.3      cgd #include <dirent.h>
     49  1.1      cgd #include <signal.h>
     50  1.1      cgd #include <setjmp.h>
     51  1.1      cgd #include <syslog.h>
     52  1.3      cgd #include <unistd.h>
     53  1.3      cgd #include <stdlib.h>
     54  1.3      cgd #include <stdio.h>
     55  1.3      cgd #include <ctype.h>
     56  1.3      cgd #include <string.h>
     57  1.3      cgd #include "lp.h"
     58  1.3      cgd #include "lpc.h"
     59  1.3      cgd #include "extern.h"
     60  1.1      cgd 
     61  1.3      cgd /*
     62  1.3      cgd  * lpc -- line printer control program
     63  1.3      cgd  */
     64  1.1      cgd 
     65  1.4  hpeyerl #define MAX_CMDLINE	200
     66  1.4  hpeyerl #define MAX_MARGV	20
     67  1.1      cgd int	fromatty;
     68  1.1      cgd 
     69  1.4  hpeyerl char	cmdline[MAX_CMDLINE];
     70  1.1      cgd int	margc;
     71  1.4  hpeyerl char	*margv[MAX_MARGV];
     72  1.1      cgd int	top;
     73  1.4  hpeyerl uid_t	uid, euid;
     74  1.1      cgd 
     75  1.1      cgd jmp_buf	toplevel;
     76  1.1      cgd 
     77  1.3      cgd static void		 cmdscanner __P((int));
     78  1.3      cgd static struct cmd	*getcmd __P((char *));
     79  1.3      cgd static void		 intr __P((int));
     80  1.3      cgd static void		 makeargv __P((void));
     81  1.3      cgd 
     82  1.3      cgd int
     83  1.1      cgd main(argc, argv)
     84  1.3      cgd 	int argc;
     85  1.1      cgd 	char *argv[];
     86  1.1      cgd {
     87  1.1      cgd 	register struct cmd *c;
     88  1.1      cgd 
     89  1.4  hpeyerl 	euid = geteuid();
     90  1.4  hpeyerl 	uid = getuid();
     91  1.4  hpeyerl 	seteuid(uid);
     92  1.1      cgd 	name = argv[0];
     93  1.1      cgd 	openlog("lpd", 0, LOG_LPR);
     94  1.1      cgd 
     95  1.1      cgd 	if (--argc > 0) {
     96  1.1      cgd 		c = getcmd(*++argv);
     97  1.1      cgd 		if (c == (struct cmd *)-1) {
     98  1.1      cgd 			printf("?Ambiguous command\n");
     99  1.1      cgd 			exit(1);
    100  1.1      cgd 		}
    101  1.1      cgd 		if (c == 0) {
    102  1.1      cgd 			printf("?Invalid command\n");
    103  1.1      cgd 			exit(1);
    104  1.1      cgd 		}
    105  1.1      cgd 		if (c->c_priv && getuid()) {
    106  1.1      cgd 			printf("?Privileged command\n");
    107  1.1      cgd 			exit(1);
    108  1.1      cgd 		}
    109  1.1      cgd 		(*c->c_handler)(argc, argv);
    110  1.1      cgd 		exit(0);
    111  1.1      cgd 	}
    112  1.1      cgd 	fromatty = isatty(fileno(stdin));
    113  1.1      cgd 	top = setjmp(toplevel) == 0;
    114  1.1      cgd 	if (top)
    115  1.1      cgd 		signal(SIGINT, intr);
    116  1.1      cgd 	for (;;) {
    117  1.1      cgd 		cmdscanner(top);
    118  1.1      cgd 		top = 1;
    119  1.1      cgd 	}
    120  1.1      cgd }
    121  1.1      cgd 
    122  1.3      cgd static void
    123  1.3      cgd intr(signo)
    124  1.3      cgd 	int signo;
    125  1.1      cgd {
    126  1.1      cgd 	if (!fromatty)
    127  1.1      cgd 		exit(0);
    128  1.1      cgd 	longjmp(toplevel, 1);
    129  1.1      cgd }
    130  1.1      cgd 
    131  1.1      cgd /*
    132  1.1      cgd  * Command parser.
    133  1.1      cgd  */
    134  1.3      cgd static void
    135  1.1      cgd cmdscanner(top)
    136  1.1      cgd 	int top;
    137  1.1      cgd {
    138  1.1      cgd 	register struct cmd *c;
    139  1.1      cgd 
    140  1.1      cgd 	if (!top)
    141  1.1      cgd 		putchar('\n');
    142  1.1      cgd 	for (;;) {
    143  1.1      cgd 		if (fromatty) {
    144  1.1      cgd 			printf("lpc> ");
    145  1.1      cgd 			fflush(stdout);
    146  1.1      cgd 		}
    147  1.4  hpeyerl 		if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
    148  1.3      cgd 			quit(0, NULL);
    149  1.1      cgd 		if (cmdline[0] == 0 || cmdline[0] == '\n')
    150  1.1      cgd 			break;
    151  1.1      cgd 		makeargv();
    152  1.1      cgd 		c = getcmd(margv[0]);
    153  1.1      cgd 		if (c == (struct cmd *)-1) {
    154  1.1      cgd 			printf("?Ambiguous command\n");
    155  1.1      cgd 			continue;
    156  1.1      cgd 		}
    157  1.1      cgd 		if (c == 0) {
    158  1.1      cgd 			printf("?Invalid command\n");
    159  1.1      cgd 			continue;
    160  1.1      cgd 		}
    161  1.1      cgd 		if (c->c_priv && getuid()) {
    162  1.1      cgd 			printf("?Privileged command\n");
    163  1.1      cgd 			continue;
    164  1.1      cgd 		}
    165  1.1      cgd 		(*c->c_handler)(margc, margv);
    166  1.1      cgd 	}
    167  1.1      cgd 	longjmp(toplevel, 0);
    168  1.1      cgd }
    169  1.1      cgd 
    170  1.1      cgd struct cmd *
    171  1.1      cgd getcmd(name)
    172  1.1      cgd 	register char *name;
    173  1.1      cgd {
    174  1.1      cgd 	register char *p, *q;
    175  1.1      cgd 	register struct cmd *c, *found;
    176  1.1      cgd 	register int nmatches, longest;
    177  1.1      cgd 
    178  1.1      cgd 	longest = 0;
    179  1.1      cgd 	nmatches = 0;
    180  1.1      cgd 	found = 0;
    181  1.6    mikel 	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
    182  1.1      cgd 		for (q = name; *q == *p++; q++)
    183  1.1      cgd 			if (*q == 0)		/* exact match? */
    184  1.1      cgd 				return(c);
    185  1.1      cgd 		if (!*q) {			/* the name was a prefix */
    186  1.1      cgd 			if (q - name > longest) {
    187  1.1      cgd 				longest = q - name;
    188  1.1      cgd 				nmatches = 1;
    189  1.1      cgd 				found = c;
    190  1.1      cgd 			} else if (q - name == longest)
    191  1.1      cgd 				nmatches++;
    192  1.1      cgd 		}
    193  1.1      cgd 	}
    194  1.1      cgd 	if (nmatches > 1)
    195  1.1      cgd 		return((struct cmd *)-1);
    196  1.1      cgd 	return(found);
    197  1.1      cgd }
    198  1.1      cgd 
    199  1.1      cgd /*
    200  1.1      cgd  * Slice a string up into argc/argv.
    201  1.1      cgd  */
    202  1.3      cgd static void
    203  1.1      cgd makeargv()
    204  1.1      cgd {
    205  1.1      cgd 	register char *cp;
    206  1.1      cgd 	register char **argp = margv;
    207  1.4  hpeyerl 	register int n = 0;
    208  1.1      cgd 
    209  1.1      cgd 	margc = 0;
    210  1.4  hpeyerl 	for (cp = cmdline; *cp && n < MAX_MARGV; n++) {
    211  1.1      cgd 		while (isspace(*cp))
    212  1.1      cgd 			cp++;
    213  1.1      cgd 		if (*cp == '\0')
    214  1.1      cgd 			break;
    215  1.1      cgd 		*argp++ = cp;
    216  1.1      cgd 		margc += 1;
    217  1.1      cgd 		while (*cp != '\0' && !isspace(*cp))
    218  1.1      cgd 			cp++;
    219  1.1      cgd 		if (*cp == '\0')
    220  1.1      cgd 			break;
    221  1.1      cgd 		*cp++ = '\0';
    222  1.1      cgd 	}
    223  1.1      cgd 	*argp++ = 0;
    224  1.1      cgd }
    225  1.1      cgd 
    226  1.1      cgd #define HELPINDENT (sizeof ("directory"))
    227  1.1      cgd 
    228  1.1      cgd /*
    229  1.1      cgd  * Help command.
    230  1.1      cgd  */
    231  1.3      cgd void
    232  1.1      cgd help(argc, argv)
    233  1.1      cgd 	int argc;
    234  1.1      cgd 	char *argv[];
    235  1.1      cgd {
    236  1.1      cgd 	register struct cmd *c;
    237  1.1      cgd 
    238  1.1      cgd 	if (argc == 1) {
    239  1.1      cgd 		register int i, j, w;
    240  1.1      cgd 		int columns, width = 0, lines;
    241  1.1      cgd 		extern int NCMDS;
    242  1.1      cgd 
    243  1.1      cgd 		printf("Commands may be abbreviated.  Commands are:\n\n");
    244  1.1      cgd 		for (c = cmdtab; c->c_name; c++) {
    245  1.1      cgd 			int len = strlen(c->c_name);
    246  1.1      cgd 
    247  1.1      cgd 			if (len > width)
    248  1.1      cgd 				width = len;
    249  1.1      cgd 		}
    250  1.1      cgd 		width = (width + 8) &~ 7;
    251  1.1      cgd 		columns = 80 / width;
    252  1.1      cgd 		if (columns == 0)
    253  1.1      cgd 			columns = 1;
    254  1.1      cgd 		lines = (NCMDS + columns - 1) / columns;
    255  1.1      cgd 		for (i = 0; i < lines; i++) {
    256  1.1      cgd 			for (j = 0; j < columns; j++) {
    257  1.1      cgd 				c = cmdtab + j * lines + i;
    258  1.1      cgd 				if (c->c_name)
    259  1.1      cgd 					printf("%s", c->c_name);
    260  1.1      cgd 				if (c + lines >= &cmdtab[NCMDS]) {
    261  1.1      cgd 					printf("\n");
    262  1.1      cgd 					break;
    263  1.1      cgd 				}
    264  1.1      cgd 				w = strlen(c->c_name);
    265  1.1      cgd 				while (w < width) {
    266  1.1      cgd 					w = (w + 8) &~ 7;
    267  1.1      cgd 					putchar('\t');
    268  1.1      cgd 				}
    269  1.1      cgd 			}
    270  1.1      cgd 		}
    271  1.1      cgd 		return;
    272  1.1      cgd 	}
    273  1.1      cgd 	while (--argc > 0) {
    274  1.1      cgd 		register char *arg;
    275  1.1      cgd 		arg = *++argv;
    276  1.1      cgd 		c = getcmd(arg);
    277  1.1      cgd 		if (c == (struct cmd *)-1)
    278  1.1      cgd 			printf("?Ambiguous help command %s\n", arg);
    279  1.1      cgd 		else if (c == (struct cmd *)0)
    280  1.1      cgd 			printf("?Invalid help command %s\n", arg);
    281  1.1      cgd 		else
    282  1.1      cgd 			printf("%-*s\t%s\n", HELPINDENT,
    283  1.1      cgd 				c->c_name, c->c_help);
    284  1.1      cgd 	}
    285  1.1      cgd }
    286