Annotation of src/usr.bin/ul/ul.c, Revision 1.18
1.18 ! deraadt 1: /* $OpenBSD: ul.c,v 1.17 2015/02/06 09:19:16 tedu Exp $ */
1.1 deraadt 2: /* $NetBSD: ul.c,v 1.3 1994/12/07 00:28:24 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.9 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
1.13 jaredy 33: #include <curses.h>
34: #include <err.h>
1.1 deraadt 35: #include <stdio.h>
1.13 jaredy 36: #include <stdlib.h>
1.2 deraadt 37: #include <string.h>
1.13 jaredy 38: #include <term.h>
1.6 deraadt 39: #include <unistd.h>
1.1 deraadt 40:
41: #define IESC '\033'
42: #define SO '\016'
43: #define SI '\017'
44: #define HFWD '9'
45: #define HREV '8'
46: #define FREV '7'
47: #define MAXBUF 512
48:
49: #define NORMAL 000
50: #define ALTSET 001 /* Reverse */
51: #define SUPERSC 002 /* Dim */
52: #define SUBSC 004 /* Dim | Ul */
53: #define UNDERL 010 /* Ul */
54: #define BOLD 020 /* Bold */
55:
56: int must_use_uc, must_overstrike;
57: char *CURS_UP, *CURS_RIGHT, *CURS_LEFT,
58: *ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
59: *ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
60:
61: struct CHAR {
62: char c_mode;
63: char c_char;
64: } ;
65:
66: struct CHAR obuf[MAXBUF];
67: int col, maxcol;
68: int mode;
69: int halfpos;
70: int upln;
71: int iflag;
72:
1.6 deraadt 73: int outchar(int);
74: void initcap(void);
75: void initbuf(void);
76: void mfilter(FILE *);
77: void reverse(void);
78: void fwd(void);
79: void flushln(void);
80: void msetmode(int);
81: void outc(int);
82: void overstrike(void);
83: void iattr(void);
84:
85: #define PRINT(s) \
86: do { \
87: if (s) \
88: tputs(s, 1, outchar); \
89: } while (0)
1.1 deraadt 90:
1.6 deraadt 91: int
1.10 deraadt 92: main(int argc, char *argv[])
1.1 deraadt 93: {
94: extern int optind;
95: extern char *optarg;
96: int c;
97: char *termtype;
98: FILE *f;
99: char termcap[1024];
100:
101: termtype = getenv("TERM");
102: if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
103: termtype = "lpr";
1.17 tedu 104: while ((c = getopt(argc, argv, "it:T:")) != -1)
105: switch (c) {
1.1 deraadt 106: case 't':
107: case 'T': /* for nroff compatibility */
1.18 ! deraadt 108: termtype = optarg;
1.1 deraadt 109: break;
110: case 'i':
111: iflag = 1;
112: break;
113:
114: default:
115: fprintf(stderr,
1.14 sobrado 116: "usage: %s [-i] [-t terminal] [file ...]\n",
117: argv[0]);
1.1 deraadt 118: exit(1);
119: }
120:
1.17 tedu 121: switch (tgetent(termcap, termtype)) {
1.1 deraadt 122: case 1:
123: break;
124: default:
1.13 jaredy 125: warnx("trouble reading termcap");
126: /* FALLTHROUGH */
1.1 deraadt 127: case 0:
128: /* No such terminal type - assume dumb */
1.8 deraadt 129: (void)strlcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:",
130: sizeof termcap);
1.1 deraadt 131: break;
132: }
133: initcap();
1.17 tedu 134: if ((tgetflag("os") && ENTER_BOLD == NULL ) ||
135: (tgetflag("ul") && ENTER_UNDERLINE == NULL && UNDER_CHAR == NULL))
136: must_overstrike = 1;
1.1 deraadt 137: initbuf();
138: if (optind == argc)
1.6 deraadt 139: mfilter(stdin);
1.1 deraadt 140: else for (; optind<argc; optind++) {
141: f = fopen(argv[optind],"r");
1.13 jaredy 142: if (f == NULL)
143: err(1, "%s", argv[optind]);
1.2 deraadt 144:
1.6 deraadt 145: mfilter(f);
1.2 deraadt 146: fclose(f);
1.1 deraadt 147: }
148: exit(0);
149: }
150:
1.6 deraadt 151: void
1.10 deraadt 152: mfilter(FILE *f)
1.1 deraadt 153: {
1.6 deraadt 154: int c;
1.1 deraadt 155:
1.5 markus 156: while ((c = getc(f)) != EOF && col < MAXBUF) switch(c) {
1.1 deraadt 157: case '\b':
158: if (col > 0)
159: col--;
160: continue;
161: case '\t':
162: col = (col+8) & ~07;
163: if (col > maxcol)
164: maxcol = col;
165: continue;
166: case '\r':
167: col = 0;
168: continue;
169: case SO:
170: mode |= ALTSET;
171: continue;
172: case SI:
173: mode &= ~ALTSET;
174: continue;
175: case IESC:
176: switch (c = getc(f)) {
177: case HREV:
178: if (halfpos == 0) {
179: mode |= SUPERSC;
180: halfpos--;
181: } else if (halfpos > 0) {
182: mode &= ~SUBSC;
183: halfpos--;
184: } else {
185: halfpos = 0;
186: reverse();
187: }
188: continue;
189: case HFWD:
190: if (halfpos == 0) {
191: mode |= SUBSC;
192: halfpos++;
193: } else if (halfpos < 0) {
194: mode &= ~SUPERSC;
195: halfpos++;
196: } else {
197: halfpos = 0;
198: fwd();
199: }
200: continue;
201: case FREV:
202: reverse();
203: continue;
204: default:
1.13 jaredy 205: errx(1, "0%o: unknown escape sequence", c);
206: /* NOTREACHED */
1.1 deraadt 207: }
208: continue;
209:
210: case '_':
211: if (obuf[col].c_char)
212: obuf[col].c_mode |= UNDERL | mode;
213: else
214: obuf[col].c_char = '_';
1.13 jaredy 215: /* FALLTHROUGH */
1.1 deraadt 216: case ' ':
217: col++;
218: if (col > maxcol)
219: maxcol = col;
220: continue;
221: case '\n':
222: flushln();
223: continue;
224: case '\f':
225: flushln();
226: putchar('\f');
227: continue;
228: default:
229: if (c < ' ') /* non printing */
230: continue;
231: if (obuf[col].c_char == '\0') {
232: obuf[col].c_char = c;
233: obuf[col].c_mode = mode;
234: } else if (obuf[col].c_char == '_') {
235: obuf[col].c_char = c;
236: obuf[col].c_mode |= UNDERL|mode;
237: } else if (obuf[col].c_char == c)
238: obuf[col].c_mode |= BOLD|mode;
239: else
240: obuf[col].c_mode = mode;
241: col++;
242: if (col > maxcol)
243: maxcol = col;
244: continue;
245: }
246: if (maxcol)
247: flushln();
248: }
249:
1.6 deraadt 250: void
1.10 deraadt 251: flushln(void)
1.1 deraadt 252: {
1.6 deraadt 253: int lastmode, i;
1.1 deraadt 254: int hadmodes = 0;
255:
256: lastmode = NORMAL;
1.17 tedu 257: for (i=0; i < maxcol; i++) {
1.1 deraadt 258: if (obuf[i].c_mode != lastmode) {
259: hadmodes++;
1.6 deraadt 260: msetmode(obuf[i].c_mode);
1.1 deraadt 261: lastmode = obuf[i].c_mode;
262: }
263: if (obuf[i].c_char == '\0') {
264: if (upln)
265: PRINT(CURS_RIGHT);
266: else
267: outc(' ');
268: } else
269: outc(obuf[i].c_char);
270: }
271: if (lastmode != NORMAL) {
1.6 deraadt 272: msetmode(0);
1.1 deraadt 273: }
274: if (must_overstrike && hadmodes)
275: overstrike();
276: putchar('\n');
277: if (iflag && hadmodes)
278: iattr();
279: (void)fflush(stdout);
280: if (upln)
281: upln--;
282: initbuf();
283: }
284:
285: /*
286: * For terminals that can overstrike, overstrike underlines and bolds.
287: * We don't do anything with halfline ups and downs, or Greek.
288: */
1.6 deraadt 289: void
1.10 deraadt 290: overstrike(void)
1.1 deraadt 291: {
1.7 mpech 292: int i;
1.13 jaredy 293: char *buf, *cp;
1.17 tedu 294: int hadbold = 0;
1.1 deraadt 295:
1.13 jaredy 296: if ((buf = malloc(maxcol + 1)) == NULL)
297: err(1, NULL);
298: cp = buf;
299:
1.1 deraadt 300: /* Set up overstrike buffer */
1.17 tedu 301: for (i = 0; i < maxcol; i++)
1.1 deraadt 302: switch (obuf[i].c_mode) {
303: case NORMAL:
304: default:
305: *cp++ = ' ';
306: break;
307: case UNDERL:
308: *cp++ = '_';
309: break;
310: case BOLD:
311: *cp++ = obuf[i].c_char;
312: hadbold=1;
313: break;
314: }
315: putchar('\r');
1.13 jaredy 316: while (cp > buf && *(cp - 1) == ' ')
317: cp--;
318: *cp = '\0';
319: for (cp = buf; *cp != '\0'; cp++)
1.1 deraadt 320: putchar(*cp);
321: if (hadbold) {
322: putchar('\r');
1.13 jaredy 323: for (cp = buf; *cp != '\0'; cp++)
1.1 deraadt 324: putchar(*cp=='_' ? ' ' : *cp);
325: putchar('\r');
1.13 jaredy 326: for (cp = buf; *cp != '\0'; cp++)
1.1 deraadt 327: putchar(*cp=='_' ? ' ' : *cp);
328: }
1.13 jaredy 329: free(buf);
1.1 deraadt 330: }
331:
1.6 deraadt 332: void
1.10 deraadt 333: iattr(void)
1.1 deraadt 334: {
1.7 mpech 335: int i;
1.13 jaredy 336: char *buf, *cp;
337:
338: if ((buf = malloc(maxcol + 1)) == NULL)
339: err(1, NULL);
340: cp = buf;
1.1 deraadt 341:
1.17 tedu 342: for (i=0; i < maxcol; i++)
1.1 deraadt 343: switch (obuf[i].c_mode) {
344: case NORMAL: *cp++ = ' '; break;
345: case ALTSET: *cp++ = 'g'; break;
346: case SUPERSC: *cp++ = '^'; break;
347: case SUBSC: *cp++ = 'v'; break;
348: case UNDERL: *cp++ = '_'; break;
349: case BOLD: *cp++ = '!'; break;
350: default: *cp++ = 'X'; break;
351: }
1.13 jaredy 352: while (cp > buf && *(cp - 1) == ' ')
353: cp--;
354: *cp = '\0';
355: for (cp = buf; *cp != '\0'; cp++)
1.1 deraadt 356: putchar(*cp);
1.13 jaredy 357: free(buf);
1.1 deraadt 358: putchar('\n');
359: }
360:
1.6 deraadt 361: void
1.10 deraadt 362: initbuf(void)
1.1 deraadt 363: {
1.17 tedu 364: bzero(obuf, sizeof (obuf)); /* depends on NORMAL == 0 */
1.1 deraadt 365: col = 0;
366: maxcol = 0;
367: mode &= ALTSET;
368: }
369:
1.6 deraadt 370: void
1.10 deraadt 371: fwd(void)
1.1 deraadt 372: {
1.6 deraadt 373: int oldcol, oldmax;
1.1 deraadt 374:
375: oldcol = col;
376: oldmax = maxcol;
377: flushln();
378: col = oldcol;
379: maxcol = oldmax;
380: }
381:
1.6 deraadt 382: void
1.10 deraadt 383: reverse(void)
1.1 deraadt 384: {
385: upln++;
386: fwd();
387: PRINT(CURS_UP);
388: PRINT(CURS_UP);
389: upln++;
390: }
391:
1.6 deraadt 392: void
1.10 deraadt 393: initcap(void)
1.1 deraadt 394: {
395: static char tcapbuf[512];
396: char *bp = tcapbuf;
397:
398: /* This nonsense attempts to work with both old and new termcap */
399: CURS_UP = tgetstr("up", &bp);
400: CURS_RIGHT = tgetstr("ri", &bp);
401: if (CURS_RIGHT == NULL)
402: CURS_RIGHT = tgetstr("nd", &bp);
403: CURS_LEFT = tgetstr("le", &bp);
404: if (CURS_LEFT == NULL)
405: CURS_LEFT = tgetstr("bc", &bp);
406: if (CURS_LEFT == NULL && tgetflag("bs"))
407: CURS_LEFT = "\b";
408:
409: ENTER_STANDOUT = tgetstr("so", &bp);
410: EXIT_STANDOUT = tgetstr("se", &bp);
411: ENTER_UNDERLINE = tgetstr("us", &bp);
412: EXIT_UNDERLINE = tgetstr("ue", &bp);
413: ENTER_DIM = tgetstr("mh", &bp);
414: ENTER_BOLD = tgetstr("md", &bp);
415: ENTER_REVERSE = tgetstr("mr", &bp);
416: EXIT_ATTRIBUTES = tgetstr("me", &bp);
417:
418: if (!ENTER_BOLD && ENTER_REVERSE)
419: ENTER_BOLD = ENTER_REVERSE;
420: if (!ENTER_BOLD && ENTER_STANDOUT)
421: ENTER_BOLD = ENTER_STANDOUT;
422: if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
423: ENTER_UNDERLINE = ENTER_STANDOUT;
424: EXIT_UNDERLINE = EXIT_STANDOUT;
425: }
426: if (!ENTER_DIM && ENTER_STANDOUT)
427: ENTER_DIM = ENTER_STANDOUT;
428: if (!ENTER_REVERSE && ENTER_STANDOUT)
429: ENTER_REVERSE = ENTER_STANDOUT;
430: if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
431: EXIT_ATTRIBUTES = EXIT_STANDOUT;
432:
433: /*
434: * Note that we use REVERSE for the alternate character set,
435: * not the as/ae capabilities. This is because we are modelling
436: * the model 37 teletype (since that's what nroff outputs) and
437: * the typical as/ae is more of a graphics set, not the greek
438: * letters the 37 has.
439: */
440:
441: UNDER_CHAR = tgetstr("uc", &bp);
442: must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
443: }
444:
1.6 deraadt 445: int
1.10 deraadt 446: outchar(int c)
1.1 deraadt 447: {
448: putchar(c & 0177);
1.6 deraadt 449: return (0);
1.1 deraadt 450: }
451:
452: static int curmode = 0;
453:
1.6 deraadt 454: void
1.10 deraadt 455: outc(int c)
1.1 deraadt 456: {
457: putchar(c);
458: if (must_use_uc && (curmode&UNDERL)) {
459: PRINT(CURS_LEFT);
460: PRINT(UNDER_CHAR);
461: }
462: }
463:
1.6 deraadt 464: void
1.10 deraadt 465: msetmode(int newmode)
1.1 deraadt 466: {
467: if (!iflag) {
468: if (curmode != NORMAL && newmode != NORMAL)
1.6 deraadt 469: msetmode(NORMAL);
1.1 deraadt 470: switch (newmode) {
471: case NORMAL:
472: switch(curmode) {
473: case NORMAL:
474: break;
475: case UNDERL:
476: PRINT(EXIT_UNDERLINE);
477: break;
478: default:
479: /* This includes standout */
480: PRINT(EXIT_ATTRIBUTES);
481: break;
482: }
483: break;
484: case ALTSET:
485: PRINT(ENTER_REVERSE);
486: break;
487: case SUPERSC:
488: /*
489: * This only works on a few terminals.
490: * It should be fixed.
491: */
492: PRINT(ENTER_UNDERLINE);
493: PRINT(ENTER_DIM);
494: break;
495: case SUBSC:
496: PRINT(ENTER_DIM);
497: break;
498: case UNDERL:
499: PRINT(ENTER_UNDERLINE);
500: break;
501: case BOLD:
502: PRINT(ENTER_BOLD);
503: break;
504: default:
505: /*
506: * We should have some provision here for multiple modes
507: * on at once. This will have to come later.
508: */
509: PRINT(ENTER_STANDOUT);
510: break;
511: }
512: }
513: curmode = newmode;
514: }