2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)ul.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/ul/ul.c,v 1.6.2.1 2000/08/23 08:49:49 kris Exp $
36 * $DragonFly: src/usr.bin/ul/ul.c,v 1.4 2005/01/05 02:40:23 cpressey Exp $
55 #define ALTSET 001 /* Reverse */
56 #define SUPERSC 002 /* Dim */
57 #define SUBSC 004 /* Dim | Ul */
58 #define UNDERL 010 /* Ul */
59 #define BOLD 020 /* Bold */
61 int must_use_uc, must_overstrike;
62 const char *CURS_UP, *CURS_RIGHT, *CURS_LEFT,
63 *ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
64 *ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
71 struct CHAR obuf[MAXBUF];
78 static void usage(void);
86 void overstrike(void);
91 #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
94 main(int argc, char **argv)
101 termtype = getenv("TERM");
102 if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
104 while ((c=getopt(argc, argv, "it:T:")) != -1)
108 case 'T': /* for nroff compatibility */
118 switch(tgetent(termcap, termtype)) {
124 warnx("trouble reading termcap");
125 /* fall through to ... */
128 /* No such terminal type - assume dumb */
129 (void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
133 if ( (tgetflag("os") && ENTER_BOLD==NULL ) ||
134 (tgetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
139 else for (; optind<argc; optind++) {
140 f = fopen(argv[optind],"r");
142 err(1, "%s", argv[optind]);
152 fprintf(stderr, "usage: ul [-i] [-t terminal] file...\n");
161 while ((c = getc(f)) != EOF && col < MAXBUF) switch(c) {
187 switch (c = getc(f)) {
193 } else if (halfpos > 0) {
206 } else if (halfpos < 0) {
220 errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
225 if (obuf[col].c_char)
226 obuf[col].c_mode |= UNDERL | mode;
228 obuf[col].c_char = '_';
245 if (c < ' ') /* non printing */
247 if (obuf[col].c_char == '\0') {
248 obuf[col].c_char = c;
249 obuf[col].c_mode = mode;
250 } else if (obuf[col].c_char == '_') {
251 obuf[col].c_char = c;
252 obuf[col].c_mode |= UNDERL|mode;
253 } else if (obuf[col].c_char == c)
254 obuf[col].c_mode |= BOLD|mode;
256 obuf[col].c_mode = mode;
274 for (i=0; i<maxcol; i++) {
275 if (obuf[i].c_mode != lastmode) {
277 setnewmode(obuf[i].c_mode);
278 lastmode = obuf[i].c_mode;
280 if (obuf[i].c_char == '\0') {
286 outc(obuf[i].c_char);
288 if (lastmode != NORMAL) {
291 if (must_overstrike && hadmodes)
294 if (iflag && hadmodes)
296 (void)fflush(stdout);
303 * For terminals that can overstrike, overstrike underlines and bolds.
304 * We don't do anything with halfline ups and downs, or Greek.
314 /* Set up overstrike buffer */
315 for (i=0; i<maxcol; i++)
316 switch (obuf[i].c_mode) {
325 *cp++ = obuf[i].c_char;
330 for (*cp=' '; *cp==' '; cp--)
332 for (cp=lbuf; *cp; cp++)
336 for (cp=lbuf; *cp; cp++)
337 putchar(*cp=='_' ? ' ' : *cp);
339 for (cp=lbuf; *cp; cp++)
340 putchar(*cp=='_' ? ' ' : *cp);
351 for (i=0; i<maxcol; i++)
352 switch (obuf[i].c_mode) {
353 case NORMAL: *cp++ = ' '; break;
354 case ALTSET: *cp++ = 'g'; break;
355 case SUPERSC: *cp++ = '^'; break;
356 case SUBSC: *cp++ = 'v'; break;
357 case UNDERL: *cp++ = '_'; break;
358 case BOLD: *cp++ = '!'; break;
359 default: *cp++ = 'X'; break;
361 for (*cp=' '; *cp==' '; cp--)
363 for (cp=lbuf; *cp; cp++)
372 bzero((char *)obuf, sizeof (obuf)); /* depends on NORMAL == 0 */
403 static char tcapbuf[512];
406 /* This nonsense attempts to work with both old and new termcap */
407 CURS_UP = tgetstr("up", &bp);
408 CURS_RIGHT = tgetstr("ri", &bp);
409 if (CURS_RIGHT == NULL)
410 CURS_RIGHT = tgetstr("nd", &bp);
411 CURS_LEFT = tgetstr("le", &bp);
412 if (CURS_LEFT == NULL)
413 CURS_LEFT = tgetstr("bc", &bp);
414 if (CURS_LEFT == NULL && tgetflag("bs"))
417 ENTER_STANDOUT = tgetstr("so", &bp);
418 EXIT_STANDOUT = tgetstr("se", &bp);
419 ENTER_UNDERLINE = tgetstr("us", &bp);
420 EXIT_UNDERLINE = tgetstr("ue", &bp);
421 ENTER_DIM = tgetstr("mh", &bp);
422 ENTER_BOLD = tgetstr("md", &bp);
423 ENTER_REVERSE = tgetstr("mr", &bp);
424 EXIT_ATTRIBUTES = tgetstr("me", &bp);
426 if (!ENTER_BOLD && ENTER_REVERSE)
427 ENTER_BOLD = ENTER_REVERSE;
428 if (!ENTER_BOLD && ENTER_STANDOUT)
429 ENTER_BOLD = ENTER_STANDOUT;
430 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
431 ENTER_UNDERLINE = ENTER_STANDOUT;
432 EXIT_UNDERLINE = EXIT_STANDOUT;
434 if (!ENTER_DIM && ENTER_STANDOUT)
435 ENTER_DIM = ENTER_STANDOUT;
436 if (!ENTER_REVERSE && ENTER_STANDOUT)
437 ENTER_REVERSE = ENTER_STANDOUT;
438 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
439 EXIT_ATTRIBUTES = EXIT_STANDOUT;
442 * Note that we use REVERSE for the alternate character set,
443 * not the as/ae capabilities. This is because we are modelling
444 * the model 37 teletype (since that's what nroff outputs) and
445 * the typical as/ae is more of a graphics set, not the greek
446 * letters the 37 has.
449 UNDER_CHAR = tgetstr("uc", &bp);
450 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
456 return(putchar(c & 0177));
459 static int curmode = 0;
465 if (must_use_uc && (curmode&UNDERL)) {
472 setnewmode(int newmode)
475 if (curmode != NORMAL && newmode != NORMAL)
483 PRINT(EXIT_UNDERLINE);
486 /* This includes standout */
487 PRINT(EXIT_ATTRIBUTES);
492 PRINT(ENTER_REVERSE);
496 * This only works on a few terminals.
497 * It should be fixed.
499 PRINT(ENTER_UNDERLINE);
506 PRINT(ENTER_UNDERLINE);
513 * We should have some provision here for multiple modes
514 * on at once. This will have to come later.
516 PRINT(ENTER_STANDOUT);