1*22428Sdist /* 2*22428Sdist * Copyright (c) 1983 Regents of the University of California. 3*22428Sdist * All rights reserved. The Berkeley software License Agreement 4*22428Sdist * specifies the terms and conditions for redistribution. 5*22428Sdist */ 6*22428Sdist 712380Sralph #ifndef lint 8*22428Sdist char copyright[] = 9*22428Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 10*22428Sdist All rights reserved.\n"; 11*22428Sdist #endif not lint 1212380Sralph 13*22428Sdist #ifndef lint 14*22428Sdist static char sccsid[] = "@(#)lpc.c 5.1 (Berkeley) 06/06/85"; 15*22428Sdist #endif not lint 16*22428Sdist 1712380Sralph /* 1812380Sralph * lpc -- line printer control program 1912380Sralph */ 2012380Sralph #include <stdio.h> 2112380Sralph #include <signal.h> 2212380Sralph #include <ctype.h> 2312380Sralph #include <setjmp.h> 2412380Sralph 2512380Sralph #include "lpc.h" 2612380Sralph 2712380Sralph int fromatty; 2812380Sralph 2912380Sralph char cmdline[200]; 3012380Sralph int margc; 3112380Sralph char *margv[20]; 3212380Sralph int top; 3312380Sralph int intr(); 3412380Sralph struct cmd *getcmd(); 3512380Sralph 3612380Sralph jmp_buf toplevel; 3712380Sralph 3812380Sralph main(argc, argv) 3912380Sralph char *argv[]; 4012380Sralph { 4112380Sralph register struct cmd *c; 4212744Sralph extern char *name; 4312380Sralph 4412744Sralph name = argv[0]; 4512744Sralph 4612380Sralph if (--argc > 0) { 4712380Sralph c = getcmd(*++argv); 4812380Sralph if (c == (struct cmd *)-1) { 4912380Sralph printf("?Ambiguous command\n"); 5012380Sralph exit(1); 5112380Sralph } 5212380Sralph if (c == 0) { 5312380Sralph printf("?Invalid command\n"); 5412380Sralph exit(1); 5512380Sralph } 5612380Sralph if (c->c_priv && getuid()) { 5712380Sralph printf("?Privileged command\n"); 5812380Sralph exit(1); 5912380Sralph } 6012380Sralph (*c->c_handler)(argc, argv); 6112380Sralph exit(0); 6212380Sralph } 6312380Sralph fromatty = isatty(fileno(stdin)); 6412380Sralph top = setjmp(toplevel) == 0; 6512380Sralph if (top) 6613147Ssam signal(SIGINT, intr); 6712380Sralph for (;;) { 6812380Sralph cmdscanner(top); 6912380Sralph top = 1; 7012380Sralph } 7112380Sralph } 7212380Sralph 7312380Sralph intr() 7412380Sralph { 7512380Sralph if (!fromatty) 7612380Sralph exit(0); 7712380Sralph longjmp(toplevel, 1); 7812380Sralph } 7912380Sralph 8012380Sralph /* 8112380Sralph * Command parser. 8212380Sralph */ 8312380Sralph cmdscanner(top) 8412380Sralph int top; 8512380Sralph { 8612380Sralph register struct cmd *c; 8712380Sralph extern struct cmd cmdtab[]; 8812380Sralph extern int help(); 8912380Sralph 9012380Sralph if (!top) 9112380Sralph putchar('\n'); 9212380Sralph for (;;) { 9312380Sralph if (fromatty) { 9412380Sralph printf("lpc> "); 9512380Sralph fflush(stdout); 9612380Sralph } 9712380Sralph if (gets(cmdline) == 0) 9812380Sralph quit(); 9912380Sralph if (cmdline[0] == 0) 10012380Sralph break; 10112380Sralph makeargv(); 10212380Sralph c = getcmd(margv[0]); 10312380Sralph if (c == (struct cmd *)-1) { 10412380Sralph printf("?Ambiguous command\n"); 10512380Sralph continue; 10612380Sralph } 10712380Sralph if (c == 0) { 10812380Sralph printf("?Invalid command\n"); 10912380Sralph continue; 11012380Sralph } 11112380Sralph if (c->c_priv && getuid()) { 11212380Sralph printf("?Privileged command\n"); 11312380Sralph continue; 11412380Sralph } 11512380Sralph (*c->c_handler)(margc, margv); 11612380Sralph } 11712380Sralph longjmp(toplevel, 0); 11812380Sralph } 11912380Sralph 12012380Sralph struct cmd * 12112380Sralph getcmd(name) 12212380Sralph register char *name; 12312380Sralph { 12412380Sralph register char *p, *q; 12512380Sralph register struct cmd *c, *found; 12612380Sralph register int nmatches, longest; 12712380Sralph 12812380Sralph longest = 0; 12912380Sralph nmatches = 0; 13012380Sralph found = 0; 13112380Sralph for (c = cmdtab; p = c->c_name; c++) { 13212380Sralph for (q = name; *q == *p++; q++) 13312380Sralph if (*q == 0) /* exact match? */ 13412380Sralph return(c); 13512380Sralph if (!*q) { /* the name was a prefix */ 13612380Sralph if (q - name > longest) { 13712380Sralph longest = q - name; 13812380Sralph nmatches = 1; 13912380Sralph found = c; 14012380Sralph } else if (q - name == longest) 14112380Sralph nmatches++; 14212380Sralph } 14312380Sralph } 14412380Sralph if (nmatches > 1) 14512380Sralph return((struct cmd *)-1); 14612380Sralph return(found); 14712380Sralph } 14812380Sralph 14912380Sralph /* 15012380Sralph * Slice a string up into argc/argv. 15112380Sralph */ 15212380Sralph makeargv() 15312380Sralph { 15412380Sralph register char *cp; 15512380Sralph register char **argp = margv; 15612380Sralph 15712380Sralph margc = 0; 15812380Sralph for (cp = cmdline; *cp;) { 15912380Sralph while (isspace(*cp)) 16012380Sralph cp++; 16112380Sralph if (*cp == '\0') 16212380Sralph break; 16312380Sralph *argp++ = cp; 16412380Sralph margc += 1; 16512380Sralph while (*cp != '\0' && !isspace(*cp)) 16612380Sralph cp++; 16712380Sralph if (*cp == '\0') 16812380Sralph break; 16912380Sralph *cp++ = '\0'; 17012380Sralph } 17112380Sralph *argp++ = 0; 17212380Sralph } 17312380Sralph 17412380Sralph #define HELPINDENT (sizeof ("directory")) 17512380Sralph 17612380Sralph /* 17712380Sralph * Help command. 17812380Sralph */ 17912380Sralph help(argc, argv) 18012380Sralph int argc; 18112380Sralph char *argv[]; 18212380Sralph { 18312380Sralph register struct cmd *c; 18412380Sralph 18512380Sralph if (argc == 1) { 18612380Sralph register int i, j, w; 18712380Sralph int columns, width = 0, lines; 18812380Sralph extern int NCMDS; 18912380Sralph 19012380Sralph printf("Commands may be abbreviated. Commands are:\n\n"); 19112380Sralph for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 19212380Sralph int len = strlen(c->c_name); 19312380Sralph 19412380Sralph if (len > width) 19512380Sralph width = len; 19612380Sralph } 19712380Sralph width = (width + 8) &~ 7; 19812380Sralph columns = 80 / width; 19912380Sralph if (columns == 0) 20012380Sralph columns = 1; 20112380Sralph lines = (NCMDS + columns - 1) / columns; 20212380Sralph for (i = 0; i < lines; i++) { 20312380Sralph for (j = 0; j < columns; j++) { 20412380Sralph c = cmdtab + j * lines + i; 20512380Sralph printf("%s", c->c_name); 20612380Sralph if (c + lines >= &cmdtab[NCMDS]) { 20712380Sralph printf("\n"); 20812380Sralph break; 20912380Sralph } 21012380Sralph w = strlen(c->c_name); 21112380Sralph while (w < width) { 21212380Sralph w = (w + 8) &~ 7; 21312380Sralph putchar('\t'); 21412380Sralph } 21512380Sralph } 21612380Sralph } 21712380Sralph return; 21812380Sralph } 21912380Sralph while (--argc > 0) { 22012380Sralph register char *arg; 22112380Sralph arg = *++argv; 22212380Sralph c = getcmd(arg); 22312380Sralph if (c == (struct cmd *)-1) 22412380Sralph printf("?Ambiguous help command %s\n", arg); 22512380Sralph else if (c == (struct cmd *)0) 22612380Sralph printf("?Invalid help command %s\n", arg); 22712380Sralph else 22812380Sralph printf("%-*s\t%s\n", HELPINDENT, 22912380Sralph c->c_name, c->c_help); 23012380Sralph } 23112380Sralph } 232