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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)ul.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
64 #define ALTSET 001 /* Reverse */
65 #define SUPERSC 002 /* Dim */
66 #define SUBSC 004 /* Dim | Ul */
67 #define UNDERL 010 /* Ul */
68 #define BOLD 020 /* Bold */
70 int must_use_uc, must_overstrike;
71 char *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;
80 struct CHAR obuf[MAXBUF];
87 static void usage __P((void));
88 void setnewmode __P((int));
89 void initcap __P((void));
90 void reverse __P((void));
91 int outchar __P((int));
93 void initbuf __P((void));
94 void iattr __P((void));
95 void overstrike __P((void));
96 void flushln __P((void));
97 void filter __P((FILE *));
100 #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
112 termtype = getenv("TERM");
113 if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
115 while ((c=getopt(argc, argv, "it:T:")) != -1)
119 case 'T': /* for nroff compatibility */
129 switch(tgetent(termcap, termtype)) {
135 warnx("trouble reading termcap");
136 /* fall through to ... */
139 /* No such terminal type - assume dumb */
140 (void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
144 if ( (tgetflag("os") && ENTER_BOLD==NULL ) ||
145 (tgetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
150 else for (; optind<argc; optind++) {
151 f = fopen(argv[optind],"r");
153 err(1, "%s", argv[optind]);
163 fprintf(stderr, "usage: ul [-i] [-t terminal] file...\n");
173 while ((c = getc(f)) != EOF) switch(c) {
199 switch (c = getc(f)) {
205 } else if (halfpos > 0) {
218 } else if (halfpos < 0) {
232 errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
237 if (obuf[col].c_char)
238 obuf[col].c_mode |= UNDERL | mode;
240 obuf[col].c_char = '_';
257 if (c < ' ') /* non printing */
259 if (obuf[col].c_char == '\0') {
260 obuf[col].c_char = c;
261 obuf[col].c_mode = mode;
262 } else if (obuf[col].c_char == '_') {
263 obuf[col].c_char = c;
264 obuf[col].c_mode |= UNDERL|mode;
265 } else if (obuf[col].c_char == c)
266 obuf[col].c_mode |= BOLD|mode;
268 obuf[col].c_mode = mode;
286 for (i=0; i<maxcol; i++) {
287 if (obuf[i].c_mode != lastmode) {
289 setnewmode(obuf[i].c_mode);
290 lastmode = obuf[i].c_mode;
292 if (obuf[i].c_char == '\0') {
298 outc(obuf[i].c_char);
300 if (lastmode != NORMAL) {
303 if (must_overstrike && hadmodes)
306 if (iflag && hadmodes)
308 (void)fflush(stdout);
315 * For terminals that can overstrike, overstrike underlines and bolds.
316 * We don't do anything with halfline ups and downs, or Greek.
323 register char *cp = lbuf;
326 /* Set up overstrike buffer */
327 for (i=0; i<maxcol; i++)
328 switch (obuf[i].c_mode) {
337 *cp++ = obuf[i].c_char;
342 for (*cp=' '; *cp==' '; cp--)
344 for (cp=lbuf; *cp; cp++)
348 for (cp=lbuf; *cp; cp++)
349 putchar(*cp=='_' ? ' ' : *cp);
351 for (cp=lbuf; *cp; cp++)
352 putchar(*cp=='_' ? ' ' : *cp);
361 register char *cp = lbuf;
363 for (i=0; i<maxcol; i++)
364 switch (obuf[i].c_mode) {
365 case NORMAL: *cp++ = ' '; break;
366 case ALTSET: *cp++ = 'g'; break;
367 case SUPERSC: *cp++ = '^'; break;
368 case SUBSC: *cp++ = 'v'; break;
369 case UNDERL: *cp++ = '_'; break;
370 case BOLD: *cp++ = '!'; break;
371 default: *cp++ = 'X'; break;
373 for (*cp=' '; *cp==' '; cp--)
375 for (cp=lbuf; *cp; cp++)
384 bzero((char *)obuf, sizeof (obuf)); /* depends on NORMAL == 0 */
393 register oldcol, oldmax;
415 static char tcapbuf[512];
418 /* This nonsense attempts to work with both old and new termcap */
419 CURS_UP = tgetstr("up", &bp);
420 CURS_RIGHT = tgetstr("ri", &bp);
421 if (CURS_RIGHT == NULL)
422 CURS_RIGHT = tgetstr("nd", &bp);
423 CURS_LEFT = tgetstr("le", &bp);
424 if (CURS_LEFT == NULL)
425 CURS_LEFT = tgetstr("bc", &bp);
426 if (CURS_LEFT == NULL && tgetflag("bs"))
429 ENTER_STANDOUT = tgetstr("so", &bp);
430 EXIT_STANDOUT = tgetstr("se", &bp);
431 ENTER_UNDERLINE = tgetstr("us", &bp);
432 EXIT_UNDERLINE = tgetstr("ue", &bp);
433 ENTER_DIM = tgetstr("mh", &bp);
434 ENTER_BOLD = tgetstr("md", &bp);
435 ENTER_REVERSE = tgetstr("mr", &bp);
436 EXIT_ATTRIBUTES = tgetstr("me", &bp);
438 if (!ENTER_BOLD && ENTER_REVERSE)
439 ENTER_BOLD = ENTER_REVERSE;
440 if (!ENTER_BOLD && ENTER_STANDOUT)
441 ENTER_BOLD = ENTER_STANDOUT;
442 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
443 ENTER_UNDERLINE = ENTER_STANDOUT;
444 EXIT_UNDERLINE = EXIT_STANDOUT;
446 if (!ENTER_DIM && ENTER_STANDOUT)
447 ENTER_DIM = ENTER_STANDOUT;
448 if (!ENTER_REVERSE && ENTER_STANDOUT)
449 ENTER_REVERSE = ENTER_STANDOUT;
450 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
451 EXIT_ATTRIBUTES = EXIT_STANDOUT;
454 * Note that we use REVERSE for the alternate character set,
455 * not the as/ae capabilities. This is because we are modelling
456 * the model 37 teletype (since that's what nroff outputs) and
457 * the typical as/ae is more of a graphics set, not the greek
458 * letters the 37 has.
461 UNDER_CHAR = tgetstr("uc", &bp);
462 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
469 return(putchar(c & 0177));
472 static int curmode = 0;
479 if (must_use_uc && (curmode&UNDERL)) {
490 if (curmode != NORMAL && newmode != NORMAL)
498 PRINT(EXIT_UNDERLINE);
501 /* This includes standout */
502 PRINT(EXIT_ATTRIBUTES);
507 PRINT(ENTER_REVERSE);
511 * This only works on a few terminals.
512 * It should be fixed.
514 PRINT(ENTER_UNDERLINE);
521 PRINT(ENTER_UNDERLINE);
528 * We should have some provision here for multiple modes
529 * on at once. This will have to come later.
531 PRINT(ENTER_STANDOUT);