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: #if defined(DOSCCS) && !defined(lint)
   8: char copyright[] =
   9: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  10:  All rights reserved.\n";
  11: 
  12: static char sccsid[] = "@(#)lpc.c	5.2.1 (2.11BSD GTE) 1/1/94";
  13: #endif
  14: 
  15: /*
  16:  * lpc -- line printer control program
  17:  */
  18: #include <stdio.h>
  19: #include <signal.h>
  20: #include <ctype.h>
  21: #include <setjmp.h>
  22: #include <syslog.h>
  23: 
  24: #include "lpc.h"
  25: 
  26: int fromatty;
  27: 
  28: char    cmdline[200];
  29: int margc;
  30: char    *margv[20];
  31: int top;
  32: int intr();
  33: struct  cmd *getcmd();
  34: 
  35: jmp_buf toplevel;
  36: 
  37: main(argc, argv)
  38:     char *argv[];
  39: {
  40:     register struct cmd *c;
  41:     extern char *name;
  42: 
  43:     name = argv[0];
  44:     openlog("lpd", 0, LOG_LPR);
  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: extern struct cmd cmdtab[];
  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->c_name; 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:                 if (c->c_name)
 208:                     printf("%s", c->c_name);
 209:                 if (c + lines >= &cmdtab[NCMDS]) {
 210:                     printf("\n");
 211:                     break;
 212:                 }
 213:                 w = strlen(c->c_name);
 214:                 while (w < width) {
 215:                     w = (w + 8) &~ 7;
 216:                     putchar('\t');
 217:                 }
 218:             }
 219:         }
 220:         return;
 221:     }
 222:     while (--argc > 0) {
 223:         register char *arg;
 224:         arg = *++argv;
 225:         c = getcmd(arg);
 226:         if (c == (struct cmd *)-1)
 227:             printf("?Ambiguous help command %s\n", arg);
 228:         else if (c == (struct cmd *)0)
 229:             printf("?Invalid help command %s\n", arg);
 230:         else
 231:             printf("%-*s\t%s\n", HELPINDENT,
 232:                 c->c_name, c->c_help);
 233:     }
 234: }

Defined functions

cmdscanner defined in line 85; used 1 times
  • in line 68
getcmd defined in line 122; used 4 times
help defined in line 181; used 4 times
intr defined in line 73; used 2 times
main defined in line 37; never used
makeargv defined in line 154; used 1 times

Defined variables

cmdline defined in line 28; used 3 times
copyright defined in line 8; never used
fromatty defined in line 26; used 3 times
margc defined in line 29; used 3 times
margv defined in line 30; used 3 times
sccsid defined in line 12; never used
top defined in line 31; used 7 times
toplevel defined in line 35; used 3 times

Defined macros

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