xref: /original-bsd/usr.bin/ul/ul.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)ul.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 
20 #define	IESC	'\033'
21 #define	SO	'\016'
22 #define	SI	'\017'
23 #define	HFWD	'9'
24 #define	HREV	'8'
25 #define	FREV	'7'
26 #define	MAXBUF	512
27 
28 #define	NORMAL	000
29 #define	ALTSET	001	/* Reverse */
30 #define	SUPERSC	002	/* Dim */
31 #define	SUBSC	004	/* Dim | Ul */
32 #define	UNDERL	010	/* Ul */
33 #define	BOLD	020	/* Bold */
34 
35 int	must_use_uc, must_overstrike;
36 char	*CURS_UP, *CURS_RIGHT, *CURS_LEFT,
37 	*ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
38 	*ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
39 
40 struct	CHAR	{
41 	char	c_mode;
42 	char	c_char;
43 } ;
44 
45 struct	CHAR	obuf[MAXBUF];
46 int	col, maxcol;
47 int	mode;
48 int	halfpos;
49 int	upln;
50 int	iflag;
51 
52 int	outchar();
53 #define	PRINT(s)	if (s == NULL) /* void */; else tputs(s, 1, outchar)
54 
55 main(argc, argv)
56 	int argc;
57 	char **argv;
58 {
59 	extern int optind;
60 	extern char *optarg;
61 	int c;
62 	char *termtype;
63 	FILE *f;
64 	char termcap[1024];
65 	char *getenv(), *strcpy();
66 
67 	termtype = getenv("TERM");
68 	if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
69 		termtype = "lpr";
70 	while ((c=getopt(argc, argv, "it:T:")) != EOF)
71 		switch(c) {
72 
73 		case 't':
74 		case 'T': /* for nroff compatibility */
75 				termtype = optarg;
76 			break;
77 		case 'i':
78 			iflag = 1;
79 			break;
80 
81 		default:
82 			fprintf(stderr,
83 				"usage: %s [ -i ] [ -tTerm ] file...\n",
84 				argv[0]);
85 			exit(1);
86 		}
87 
88 	switch(tgetent(termcap, termtype)) {
89 
90 	case 1:
91 		break;
92 
93 	default:
94 		fprintf(stderr,"trouble reading termcap");
95 		/* fall through to ... */
96 
97 	case 0:
98 		/* No such terminal type - assume dumb */
99 		(void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
100 		break;
101 	}
102 	initcap();
103 	if (    (tgetflag("os") && ENTER_BOLD==NULL ) ||
104 		(tgetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
105 			must_overstrike = 1;
106 	initbuf();
107 	if (optind == argc)
108 		filter(stdin);
109 	else for (; optind<argc; optind++) {
110 		f = fopen(argv[optind],"r");
111 		if (f == NULL) {
112 			perror(argv[optind]);
113 			exit(1);
114 		} else
115 			filter(f);
116 	}
117 	exit(0);
118 }
119 
120 filter(f)
121 	FILE *f;
122 {
123 	register c;
124 
125 	while ((c = getc(f)) != EOF) switch(c) {
126 
127 	case '\b':
128 		if (col > 0)
129 			col--;
130 		continue;
131 
132 	case '\t':
133 		col = (col+8) & ~07;
134 		if (col > maxcol)
135 			maxcol = col;
136 		continue;
137 
138 	case '\r':
139 		col = 0;
140 		continue;
141 
142 	case SO:
143 		mode |= ALTSET;
144 		continue;
145 
146 	case SI:
147 		mode &= ~ALTSET;
148 		continue;
149 
150 	case IESC:
151 		switch (c = getc(f)) {
152 
153 		case HREV:
154 			if (halfpos == 0) {
155 				mode |= SUPERSC;
156 				halfpos--;
157 			} else if (halfpos > 0) {
158 				mode &= ~SUBSC;
159 				halfpos--;
160 			} else {
161 				halfpos = 0;
162 				reverse();
163 			}
164 			continue;
165 
166 		case HFWD:
167 			if (halfpos == 0) {
168 				mode |= SUBSC;
169 				halfpos++;
170 			} else if (halfpos < 0) {
171 				mode &= ~SUPERSC;
172 				halfpos++;
173 			} else {
174 				halfpos = 0;
175 				fwd();
176 			}
177 			continue;
178 
179 		case FREV:
180 			reverse();
181 			continue;
182 
183 		default:
184 			fprintf(stderr,
185 				"Unknown escape sequence in input: %o, %o\n",
186 				IESC, c);
187 			exit(1);
188 		}
189 		continue;
190 
191 	case '_':
192 		if (obuf[col].c_char)
193 			obuf[col].c_mode |= UNDERL | mode;
194 		else
195 			obuf[col].c_char = '_';
196 	case ' ':
197 		col++;
198 		if (col > maxcol)
199 			maxcol = col;
200 		continue;
201 
202 	case '\n':
203 		flushln();
204 		continue;
205 
206 	case '\f':
207 		flushln();
208 		putchar('\f');
209 		continue;
210 
211 	default:
212 		if (c < ' ')	/* non printing */
213 			continue;
214 		if (obuf[col].c_char == '\0') {
215 			obuf[col].c_char = c;
216 			obuf[col].c_mode = mode;
217 		} else if (obuf[col].c_char == '_') {
218 			obuf[col].c_char = c;
219 			obuf[col].c_mode |= UNDERL|mode;
220 		} else if (obuf[col].c_char == c)
221 			obuf[col].c_mode |= BOLD|mode;
222 		else
223 			obuf[col].c_mode = mode;
224 		col++;
225 		if (col > maxcol)
226 			maxcol = col;
227 		continue;
228 	}
229 	if (maxcol)
230 		flushln();
231 }
232 
233 flushln()
234 {
235 	register lastmode;
236 	register i;
237 	int hadmodes = 0;
238 
239 	lastmode = NORMAL;
240 	for (i=0; i<maxcol; i++) {
241 		if (obuf[i].c_mode != lastmode) {
242 			hadmodes++;
243 			setmode(obuf[i].c_mode);
244 			lastmode = obuf[i].c_mode;
245 		}
246 		if (obuf[i].c_char == '\0') {
247 			if (upln)
248 				PRINT(CURS_RIGHT);
249 			else
250 				outc(' ');
251 		} else
252 			outc(obuf[i].c_char);
253 	}
254 	if (lastmode != NORMAL) {
255 		setmode(0);
256 	}
257 	if (must_overstrike && hadmodes)
258 		overstrike();
259 	putchar('\n');
260 	if (iflag && hadmodes)
261 		iattr();
262 	(void)fflush(stdout);
263 	if (upln)
264 		upln--;
265 	initbuf();
266 }
267 
268 /*
269  * For terminals that can overstrike, overstrike underlines and bolds.
270  * We don't do anything with halfline ups and downs, or Greek.
271  */
272 overstrike()
273 {
274 	register int i;
275 	char lbuf[256];
276 	register char *cp = lbuf;
277 	int hadbold=0;
278 
279 	/* Set up overstrike buffer */
280 	for (i=0; i<maxcol; i++)
281 		switch (obuf[i].c_mode) {
282 		case NORMAL:
283 		default:
284 			*cp++ = ' ';
285 			break;
286 		case UNDERL:
287 			*cp++ = '_';
288 			break;
289 		case BOLD:
290 			*cp++ = obuf[i].c_char;
291 			hadbold=1;
292 			break;
293 		}
294 	putchar('\r');
295 	for (*cp=' '; *cp==' '; cp--)
296 		*cp = 0;
297 	for (cp=lbuf; *cp; cp++)
298 		putchar(*cp);
299 	if (hadbold) {
300 		putchar('\r');
301 		for (cp=lbuf; *cp; cp++)
302 			putchar(*cp=='_' ? ' ' : *cp);
303 		putchar('\r');
304 		for (cp=lbuf; *cp; cp++)
305 			putchar(*cp=='_' ? ' ' : *cp);
306 	}
307 }
308 
309 iattr()
310 {
311 	register int i;
312 	char lbuf[256];
313 	register char *cp = lbuf;
314 
315 	for (i=0; i<maxcol; i++)
316 		switch (obuf[i].c_mode) {
317 		case NORMAL:	*cp++ = ' '; break;
318 		case ALTSET:	*cp++ = 'g'; break;
319 		case SUPERSC:	*cp++ = '^'; break;
320 		case SUBSC:	*cp++ = 'v'; break;
321 		case UNDERL:	*cp++ = '_'; break;
322 		case BOLD:	*cp++ = '!'; break;
323 		default:	*cp++ = 'X'; break;
324 		}
325 	for (*cp=' '; *cp==' '; cp--)
326 		*cp = 0;
327 	for (cp=lbuf; *cp; cp++)
328 		putchar(*cp);
329 	putchar('\n');
330 }
331 
332 initbuf()
333 {
334 
335 	bzero((char *)obuf, sizeof (obuf));	/* depends on NORMAL == 0 */
336 	col = 0;
337 	maxcol = 0;
338 	mode &= ALTSET;
339 }
340 
341 fwd()
342 {
343 	register oldcol, oldmax;
344 
345 	oldcol = col;
346 	oldmax = maxcol;
347 	flushln();
348 	col = oldcol;
349 	maxcol = oldmax;
350 }
351 
352 reverse()
353 {
354 	upln++;
355 	fwd();
356 	PRINT(CURS_UP);
357 	PRINT(CURS_UP);
358 	upln++;
359 }
360 
361 initcap()
362 {
363 	static char tcapbuf[512];
364 	char *bp = tcapbuf;
365 	char *getenv(), *tgetstr();
366 
367 	/* This nonsense attempts to work with both old and new termcap */
368 	CURS_UP =		tgetstr("up", &bp);
369 	CURS_RIGHT =		tgetstr("ri", &bp);
370 	if (CURS_RIGHT == NULL)
371 		CURS_RIGHT =	tgetstr("nd", &bp);
372 	CURS_LEFT =		tgetstr("le", &bp);
373 	if (CURS_LEFT == NULL)
374 		CURS_LEFT =	tgetstr("bc", &bp);
375 	if (CURS_LEFT == NULL && tgetflag("bs"))
376 		CURS_LEFT =	"\b";
377 
378 	ENTER_STANDOUT =	tgetstr("so", &bp);
379 	EXIT_STANDOUT =		tgetstr("se", &bp);
380 	ENTER_UNDERLINE =	tgetstr("us", &bp);
381 	EXIT_UNDERLINE =	tgetstr("ue", &bp);
382 	ENTER_DIM =		tgetstr("mh", &bp);
383 	ENTER_BOLD =		tgetstr("md", &bp);
384 	ENTER_REVERSE =		tgetstr("mr", &bp);
385 	EXIT_ATTRIBUTES =	tgetstr("me", &bp);
386 
387 	if (!ENTER_BOLD && ENTER_REVERSE)
388 		ENTER_BOLD = ENTER_REVERSE;
389 	if (!ENTER_BOLD && ENTER_STANDOUT)
390 		ENTER_BOLD = ENTER_STANDOUT;
391 	if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
392 		ENTER_UNDERLINE = ENTER_STANDOUT;
393 		EXIT_UNDERLINE = EXIT_STANDOUT;
394 	}
395 	if (!ENTER_DIM && ENTER_STANDOUT)
396 		ENTER_DIM = ENTER_STANDOUT;
397 	if (!ENTER_REVERSE && ENTER_STANDOUT)
398 		ENTER_REVERSE = ENTER_STANDOUT;
399 	if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
400 		EXIT_ATTRIBUTES = EXIT_STANDOUT;
401 
402 	/*
403 	 * Note that we use REVERSE for the alternate character set,
404 	 * not the as/ae capabilities.  This is because we are modelling
405 	 * the model 37 teletype (since that's what nroff outputs) and
406 	 * the typical as/ae is more of a graphics set, not the greek
407 	 * letters the 37 has.
408 	 */
409 
410 	UNDER_CHAR =		tgetstr("uc", &bp);
411 	must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
412 }
413 
414 outchar(c)
415 	int c;
416 {
417 	putchar(c & 0177);
418 }
419 
420 static int curmode = 0;
421 
422 outc(c)
423 	int c;
424 {
425 	putchar(c);
426 	if (must_use_uc && (curmode&UNDERL)) {
427 		PRINT(CURS_LEFT);
428 		PRINT(UNDER_CHAR);
429 	}
430 }
431 
432 setmode(newmode)
433 	int newmode;
434 {
435 	if (!iflag) {
436 		if (curmode != NORMAL && newmode != NORMAL)
437 			setmode(NORMAL);
438 		switch (newmode) {
439 		case NORMAL:
440 			switch(curmode) {
441 			case NORMAL:
442 				break;
443 			case UNDERL:
444 				PRINT(EXIT_UNDERLINE);
445 				break;
446 			default:
447 				/* This includes standout */
448 				PRINT(EXIT_ATTRIBUTES);
449 				break;
450 			}
451 			break;
452 		case ALTSET:
453 			PRINT(ENTER_REVERSE);
454 			break;
455 		case SUPERSC:
456 			/*
457 			 * This only works on a few terminals.
458 			 * It should be fixed.
459 			 */
460 			PRINT(ENTER_UNDERLINE);
461 			PRINT(ENTER_DIM);
462 			break;
463 		case SUBSC:
464 			PRINT(ENTER_DIM);
465 			break;
466 		case UNDERL:
467 			PRINT(ENTER_UNDERLINE);
468 			break;
469 		case BOLD:
470 			PRINT(ENTER_BOLD);
471 			break;
472 		default:
473 			/*
474 			 * We should have some provision here for multiple modes
475 			 * on at once.  This will have to come later.
476 			 */
477 			PRINT(ENTER_STANDOUT);
478 			break;
479 		}
480 	}
481 	curmode = newmode;
482 }
483