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