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

Defined functions

cmdscanner defined in line 85; used 1 times
  • in line 70
getcmd defined in line 122; used 4 times
help defined in line 181; used 4 times
intr defined in line 75; used 2 times
main defined in line 39; never used
makeargv defined in line 154; used 1 times

Defined variables

cmdline defined in line 30; used 3 times
copyright defined in line 8; never used
fromatty defined in line 28; used 3 times
margc defined in line 31; used 3 times
margv defined in line 32; used 3 times
sccsid defined in line 14; never used
top defined in line 33; used 7 times
toplevel defined in line 37; used 3 times

Defined macros

HELPINDENT defined in line 176; used 1 times
Last modified: 1986-01-11
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1496
Valid CSS Valid XHTML 1.0 Strict