1: #ifndef lint
   2: static char *sccsid = "@(#)ul.c	4.1 (Berkeley) 10/1/80";
   3: #endif
   4: /*
   5:  * ul
   6:  */
   7: #include <stdio.h>
   8: 
   9: char    buf[BUFSIZ];
  10: char    isul[BUFSIZ];
  11: char    termcap[1024];
  12: char    ulbuf[BUFSIZ];
  13: char    *stul, *endul, *chul;
  14: char    *backspace;
  15: char    *termtype;
  16: int outc();
  17: char    *tgetstr();
  18: char    *getenv();
  19: 
  20: main(argc, argv)
  21:     int argc;
  22:     char **argv;
  23: {
  24:     register int i;
  25:     char *cp;
  26:     FILE *f;
  27: 
  28:     argc--, argv++;
  29:     termtype = getenv("TERM");
  30:     if (termtype == NULL)
  31:         termtype = "dumb";
  32:     while (argc > 0 && argv[0][0] == '-') {
  33:         switch(argv[0][1]) {
  34: 
  35:         case 't':
  36:         case 'T': /* for nroff compatibility */
  37:             if (argv[0][2])
  38:                 termtype = &argv[0][2];
  39:             else {
  40:                 termtype = argv[1];
  41:                 argc--;
  42:                 argv++;
  43:             }
  44:             break;
  45:         case 'i':
  46:             argc--, argv++;
  47:             iul(argc, argv);
  48:             exit(0);
  49: 
  50:         default:
  51:             printf("Usage: ul [ -i ] [ -tTerm ] file...\n");
  52:             exit(1);
  53:         }
  54:     }
  55:     switch(tgetent(termcap, termtype)) {
  56: 
  57:     case 1:
  58:         if (tgetflag("os"))
  59:             execv("/bin/cat",argv);
  60:         cp = ulbuf;
  61:         if ((backspace = tgetstr("bc",&cp)) == NULL)
  62:             backspace = "\b";
  63:         /*
  64: 		 * Handle terminals that have start underline/stop
  65: 		 * underline sequences, as well as those with
  66: 		 * underline char sequences (we assume the sequence
  67: 		 * moves the cursor forward one character).
  68: 		 * If we can't find underline sequences, we
  69: 		 * settle for standout sequences.
  70: 		 */
  71:         if ((chul=tgetstr("uc",&cp)) == NULL)
  72:             chul = "";
  73:         if ((stul=tgetstr("us",&cp)) == NULL && !tgetflag("ul") &&
  74:             (!*chul) && (stul=tgetstr("so",&cp)) == NULL)
  75:             stul = "";
  76:         if ((endul=tgetstr("ue",&cp)) == NULL && !tgetflag("ul") &&
  77:             (!*chul) && (endul=tgetstr("se",&cp)) == NULL)
  78:             endul = "";
  79:         if (chul==0&&stul==0&&endul==0&&tgetflag("ul"))
  80:             execv("/bin/cat",argv);
  81:         break;
  82: 
  83:     default:
  84:         fprintf(stderr,"trouble reading termcap");
  85:         /* fall through to ... */
  86: 
  87:     case 0:
  88:         /* No such terminal type - assume dumb */
  89:         stul = endul = chul = "";
  90:         break;
  91:     }
  92:     if (argc == 0)
  93:         filter(stdin);
  94:     else for (i=0; i<argc; i++) {
  95:         f = fopen(argv[i],"r");
  96:         if (f == NULL) {
  97:             perror(argv[i]);
  98:             exit(1);
  99:         } else
 100:             filter(f);
 101:     }
 102:     exit(0);
 103: }
 104: 
 105: filter(f)
 106: FILE *f;
 107: {
 108:     register int p, n;
 109:     register char c;
 110:     int state;
 111: 
 112:     n = 0;
 113:     for (;;) {
 114:         p = 0;
 115:         for (p=0; p<n; p++) {
 116:             buf[p] = '\0';
 117:             isul[p] = 0;
 118:         }
 119:         p = n = 0;
 120: 
 121:         for (;;) {
 122:             c = getc(f);
 123:             if (((int) c)==EOF)
 124:                 break;
 125:             if (c=='\b') {
 126:                 if (p > 0) {
 127:                     p--;
 128:                 }
 129:             } else if (c=='_' && isul[p]==0 && buf[p]) {
 130:                 isul[p] = 1;
 131:                 p++;
 132:             } else {
 133:                 if (buf[p] == '_')
 134:                     isul[p] = 1;
 135:                 buf[p] = c;
 136:                 p++;
 137:                 if (n < p)
 138:                     n = p;
 139:             }
 140:             if (c=='\n')
 141:                 break;
 142:         }
 143: 
 144:         state = 0;
 145:         for (p=0; p<n; p++) {
 146:             if (isul[p] != state)
 147:                 tputs(isul[p] ? stul : endul, 1, outc);
 148:             state = isul[p];
 149:             putchar(buf[p]);
 150:             if (isul[p] && *chul) {
 151:                 printf("%s",backspace);
 152:                 tputs(chul, 1, outc);
 153:             }
 154:         }
 155:         if (((int) c)==EOF) break;
 156:     }
 157: }
 158: 
 159: outc(c)
 160: char c;
 161: {
 162:     putchar(c);
 163: }
 164: 
 165: #define BACKSPACE 0
 166: #define QUOTE   0200
 167: 
 168: char    linebuf[BUFSIZ], genbuf[BUFSIZ];
 169: char    *strcpy();
 170: 
 171: iul(argc, argv)
 172:     register argc;
 173:     register char *argv[];
 174: {
 175:     register char *lp;
 176: 
 177:     do {
 178:         if (argc > 0) {
 179:             if (freopen(argv[0], "r", stdin) == NULL) {
 180:                 perror(argv[0]);
 181:                 exit(1);
 182:             }
 183:             argc--; argv++;
 184:         }
 185:         while (fgets(linebuf, sizeof linebuf, stdin) != 0) {
 186:             for (lp = linebuf; *lp; lp++)
 187:                 continue;
 188:             *--lp = 0;
 189:             doulg();
 190:             dographic();
 191:             if (genbuf[0])
 192:                 printf("\n%s", genbuf);
 193:             putchar('\n');
 194:             fflush(stdout);
 195:         }
 196:     } while (argc > 0);
 197:     exit(0);
 198: }
 199: 
 200: dographic()
 201: {
 202:     register char *lp;
 203:     register c;
 204: 
 205:     for (lp = linebuf; c = *lp++;) {
 206:         switch (c) {
 207:             case '\b':
 208:                 if (BACKSPACE == 0)
 209:                     c = '?';
 210:                 break;
 211:             default:
 212:                 if (c < ' ' || c == 0177)
 213:                     c = '?';
 214:                 break;
 215:             case '\t':
 216:                 break;
 217:         }
 218:         putchar(c);
 219:     }
 220: }
 221: 
 222: doulg()
 223: {
 224:     register char *lp, *gp;
 225:     char *maxgp;
 226:     register c;
 227:     int col;
 228: 
 229:     gp = genbuf;
 230:     *gp = 0;
 231:     maxgp = gp;
 232:     col = 0;
 233:     for (lp = linebuf; c = *lp++;) {
 234:         switch (c) {
 235:             case '\t':
 236:                 while ((col & 7) != 7) {
 237:                     *gp++ = ' ';
 238:                     if (gp >= &genbuf[BUFSIZ - 2])
 239:                         goto ovflo;
 240:                     col++;
 241:                 }
 242:                 break;
 243:             default:
 244:                 if (gp >= maxgp)
 245:                     break;
 246:                 c |= (*gp & QUOTE);
 247:                 break;
 248:             case '_':
 249:                 if (gp >= maxgp)
 250:                     c = QUOTE;
 251:                 else
 252:                     c = *gp | QUOTE;
 253:                 break;
 254:             case '\b':
 255:                 if (gp > genbuf) {
 256:                     gp--;
 257:                     col--;
 258:                 }
 259:                 continue;
 260:         }
 261:         if (gp >= &genbuf[BUFSIZ - 2]) {
 262: ovflo:
 263:             fprintf(stderr, "Line too long\n");
 264:             exit(1);
 265:         }
 266:         *gp++ = c;
 267:         if (gp > maxgp)
 268:             maxgp = gp;
 269:         col++;
 270:     }
 271:     *maxgp = 0;
 272:     strcpy(linebuf, genbuf);
 273:     for (lp = linebuf, gp = genbuf; c = *lp; gp++, lp++)
 274:         if (c & QUOTE) {
 275:             c &= 0177;
 276:             if (c == 0)
 277:                 *lp = '_', *gp = ' ';
 278:             else
 279:                 *lp = c, *gp = '-';
 280:         } else
 281:             *gp = ' ';
 282:     --gp;
 283:     while (gp >= genbuf && *gp == ' ')
 284:         --gp;
 285:     gp[1] = 0;
 286: }

Defined functions

dographic defined in line 200; used 1 times
doulg defined in line 222; used 1 times
filter defined in line 105; used 2 times
iul defined in line 171; used 1 times
  • in line 47
main defined in line 20; never used
outc defined in line 159; used 3 times

Defined variables

backspace defined in line 14; used 3 times
buf defined in line 9; used 5 times
chul defined in line 13; used 8 times
endul defined in line 13; used 6 times
genbuf defined in line 168; used 9 times
isul defined in line 10; used 8 times
linebuf defined in line 168; used 7 times
sccsid defined in line 2; never used
stul defined in line 13; used 6 times
termcap defined in line 11; used 1 times
  • in line 55
termtype defined in line 15; used 6 times
ulbuf defined in line 12; used 1 times
  • in line 60

Defined macros

BACKSPACE defined in line 165; used 1 times
QUOTE defined in line 166; used 4 times
Last modified: 1982-09-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 882
Valid CSS Valid XHTML 1.0 Strict