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