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