1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 char copyright[] = 9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 10 All rights reserved.\n"; 11 #endif not lint 12 13 #ifndef lint 14 static char sccsid[] = "@(#)lpc.c 5.1 (Berkeley) 06/06/85"; 15 #endif not lint 16 17 /* 18 * lpc -- line printer control program 19 */ 20 #include <stdio.h> 21 #include <signal.h> 22 #include <ctype.h> 23 #include <setjmp.h> 24 25 #include "lpc.h" 26 27 int fromatty; 28 29 char cmdline[200]; 30 int margc; 31 char *margv[20]; 32 int top; 33 int intr(); 34 struct cmd *getcmd(); 35 36 jmp_buf toplevel; 37 38 main(argc, argv) 39 char *argv[]; 40 { 41 register struct cmd *c; 42 extern char *name; 43 44 name = argv[0]; 45 46 if (--argc > 0) { 47 c = getcmd(*++argv); 48 if (c == (struct cmd *)-1) { 49 printf("?Ambiguous command\n"); 50 exit(1); 51 } 52 if (c == 0) { 53 printf("?Invalid command\n"); 54 exit(1); 55 } 56 if (c->c_priv && getuid()) { 57 printf("?Privileged command\n"); 58 exit(1); 59 } 60 (*c->c_handler)(argc, argv); 61 exit(0); 62 } 63 fromatty = isatty(fileno(stdin)); 64 top = setjmp(toplevel) == 0; 65 if (top) 66 signal(SIGINT, intr); 67 for (;;) { 68 cmdscanner(top); 69 top = 1; 70 } 71 } 72 73 intr() 74 { 75 if (!fromatty) 76 exit(0); 77 longjmp(toplevel, 1); 78 } 79 80 /* 81 * Command parser. 82 */ 83 cmdscanner(top) 84 int top; 85 { 86 register struct cmd *c; 87 extern struct cmd cmdtab[]; 88 extern int help(); 89 90 if (!top) 91 putchar('\n'); 92 for (;;) { 93 if (fromatty) { 94 printf("lpc> "); 95 fflush(stdout); 96 } 97 if (gets(cmdline) == 0) 98 quit(); 99 if (cmdline[0] == 0) 100 break; 101 makeargv(); 102 c = getcmd(margv[0]); 103 if (c == (struct cmd *)-1) { 104 printf("?Ambiguous command\n"); 105 continue; 106 } 107 if (c == 0) { 108 printf("?Invalid command\n"); 109 continue; 110 } 111 if (c->c_priv && getuid()) { 112 printf("?Privileged command\n"); 113 continue; 114 } 115 (*c->c_handler)(margc, margv); 116 } 117 longjmp(toplevel, 0); 118 } 119 120 struct cmd * 121 getcmd(name) 122 register char *name; 123 { 124 register char *p, *q; 125 register struct cmd *c, *found; 126 register int nmatches, longest; 127 128 longest = 0; 129 nmatches = 0; 130 found = 0; 131 for (c = cmdtab; p = c->c_name; c++) { 132 for (q = name; *q == *p++; q++) 133 if (*q == 0) /* exact match? */ 134 return(c); 135 if (!*q) { /* the name was a prefix */ 136 if (q - name > longest) { 137 longest = q - name; 138 nmatches = 1; 139 found = c; 140 } else if (q - name == longest) 141 nmatches++; 142 } 143 } 144 if (nmatches > 1) 145 return((struct cmd *)-1); 146 return(found); 147 } 148 149 /* 150 * Slice a string up into argc/argv. 151 */ 152 makeargv() 153 { 154 register char *cp; 155 register char **argp = margv; 156 157 margc = 0; 158 for (cp = cmdline; *cp;) { 159 while (isspace(*cp)) 160 cp++; 161 if (*cp == '\0') 162 break; 163 *argp++ = cp; 164 margc += 1; 165 while (*cp != '\0' && !isspace(*cp)) 166 cp++; 167 if (*cp == '\0') 168 break; 169 *cp++ = '\0'; 170 } 171 *argp++ = 0; 172 } 173 174 #define HELPINDENT (sizeof ("directory")) 175 176 /* 177 * Help command. 178 */ 179 help(argc, argv) 180 int argc; 181 char *argv[]; 182 { 183 register struct cmd *c; 184 185 if (argc == 1) { 186 register int i, j, w; 187 int columns, width = 0, lines; 188 extern int NCMDS; 189 190 printf("Commands may be abbreviated. Commands are:\n\n"); 191 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 192 int len = strlen(c->c_name); 193 194 if (len > width) 195 width = len; 196 } 197 width = (width + 8) &~ 7; 198 columns = 80 / width; 199 if (columns == 0) 200 columns = 1; 201 lines = (NCMDS + columns - 1) / columns; 202 for (i = 0; i < lines; i++) { 203 for (j = 0; j < columns; j++) { 204 c = cmdtab + j * lines + i; 205 printf("%s", c->c_name); 206 if (c + lines >= &cmdtab[NCMDS]) { 207 printf("\n"); 208 break; 209 } 210 w = strlen(c->c_name); 211 while (w < width) { 212 w = (w + 8) &~ 7; 213 putchar('\t'); 214 } 215 } 216 } 217 return; 218 } 219 while (--argc > 0) { 220 register char *arg; 221 arg = *++argv; 222 c = getcmd(arg); 223 if (c == (struct cmd *)-1) 224 printf("?Ambiguous help command %s\n", arg); 225 else if (c == (struct cmd *)0) 226 printf("?Invalid help command %s\n", arg); 227 else 228 printf("%-*s\t%s\n", HELPINDENT, 229 c->c_name, c->c_help); 230 } 231 } 232