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 * 4. 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.
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
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)ul.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/ul/ul.c,v 1.6.2.1 2000/08/23 08:49:49 kris Exp $
32 * $DragonFly: src/usr.bin/ul/ul.c,v 1.4 2005/01/05 02:40:23 cpressey Exp $
51 #define ALTSET 001 /* Reverse */
52 #define SUPERSC 002 /* Dim */
53 #define SUBSC 004 /* Dim | Ul */
54 #define UNDERL 010 /* Ul */
55 #define BOLD 020 /* Bold */
57 int must_use_uc, must_overstrike;
58 const char *CURS_UP, *CURS_RIGHT, *CURS_LEFT,
59 *ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
60 *ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
67 struct CHAR obuf[MAXBUF];
74 static void usage(void);
82 void overstrike(void);
87 #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
90 main(int argc, char **argv)
97 termtype = getenv("TERM");
98 if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
100 while ((c=getopt(argc, argv, "it:T:")) != -1)
104 case 'T': /* for nroff compatibility */
114 switch(tgetent(termcap, termtype)) {
120 warnx("trouble reading termcap");
121 /* fall through to ... */
124 /* No such terminal type - assume dumb */
125 (void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
129 if ( (tgetflag("os") && ENTER_BOLD==NULL ) ||
130 (tgetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
135 else for (; optind<argc; optind++) {
136 f = fopen(argv[optind],"r");
138 err(1, "%s", argv[optind]);
148 fprintf(stderr, "usage: ul [-i] [-t terminal] file...\n");
157 while ((c = getc(f)) != EOF && col < MAXBUF) switch(c) {
183 switch (c = getc(f)) {
189 } else if (halfpos > 0) {
202 } else if (halfpos < 0) {
216 errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
221 if (obuf[col].c_char)
222 obuf[col].c_mode |= UNDERL | mode;
224 obuf[col].c_char = '_';
241 if (c < ' ') /* non printing */
243 if (obuf[col].c_char == '\0') {
244 obuf[col].c_char = c;
245 obuf[col].c_mode = mode;
246 } else if (obuf[col].c_char == '_') {
247 obuf[col].c_char = c;
248 obuf[col].c_mode |= UNDERL|mode;
249 } else if (obuf[col].c_char == c)
250 obuf[col].c_mode |= BOLD|mode;
252 obuf[col].c_mode = mode;
270 for (i=0; i<maxcol; i++) {
271 if (obuf[i].c_mode != lastmode) {
273 setnewmode(obuf[i].c_mode);
274 lastmode = obuf[i].c_mode;
276 if (obuf[i].c_char == '\0') {
282 outc(obuf[i].c_char);
284 if (lastmode != NORMAL) {
287 if (must_overstrike && hadmodes)
290 if (iflag && hadmodes)
292 (void)fflush(stdout);
299 * For terminals that can overstrike, overstrike underlines and bolds.
300 * We don't do anything with halfline ups and downs, or Greek.
310 /* Set up overstrike buffer */
311 for (i=0; i<maxcol; i++)
312 switch (obuf[i].c_mode) {
321 *cp++ = obuf[i].c_char;
326 for (*cp=' '; *cp==' '; cp--)
328 for (cp=lbuf; *cp; cp++)
332 for (cp=lbuf; *cp; cp++)
333 putchar(*cp=='_' ? ' ' : *cp);
335 for (cp=lbuf; *cp; cp++)
336 putchar(*cp=='_' ? ' ' : *cp);
347 for (i=0; i<maxcol; i++)
348 switch (obuf[i].c_mode) {
349 case NORMAL: *cp++ = ' '; break;
350 case ALTSET: *cp++ = 'g'; break;
351 case SUPERSC: *cp++ = '^'; break;
352 case SUBSC: *cp++ = 'v'; break;
353 case UNDERL: *cp++ = '_'; break;
354 case BOLD: *cp++ = '!'; break;
355 default: *cp++ = 'X'; break;
357 for (*cp=' '; *cp==' '; cp--)
359 for (cp=lbuf; *cp; cp++)
368 bzero((char *)obuf, sizeof (obuf)); /* depends on NORMAL == 0 */
399 static char tcapbuf[512];
402 /* This nonsense attempts to work with both old and new termcap */
403 CURS_UP = tgetstr("up", &bp);
404 CURS_RIGHT = tgetstr("ri", &bp);
405 if (CURS_RIGHT == NULL)
406 CURS_RIGHT = tgetstr("nd", &bp);
407 CURS_LEFT = tgetstr("le", &bp);
408 if (CURS_LEFT == NULL)
409 CURS_LEFT = tgetstr("bc", &bp);
410 if (CURS_LEFT == NULL && tgetflag("bs"))
413 ENTER_STANDOUT = tgetstr("so", &bp);
414 EXIT_STANDOUT = tgetstr("se", &bp);
415 ENTER_UNDERLINE = tgetstr("us", &bp);
416 EXIT_UNDERLINE = tgetstr("ue", &bp);
417 ENTER_DIM = tgetstr("mh", &bp);
418 ENTER_BOLD = tgetstr("md", &bp);
419 ENTER_REVERSE = tgetstr("mr", &bp);
420 EXIT_ATTRIBUTES = tgetstr("me", &bp);
422 if (!ENTER_BOLD && ENTER_REVERSE)
423 ENTER_BOLD = ENTER_REVERSE;
424 if (!ENTER_BOLD && ENTER_STANDOUT)
425 ENTER_BOLD = ENTER_STANDOUT;
426 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
427 ENTER_UNDERLINE = ENTER_STANDOUT;
428 EXIT_UNDERLINE = EXIT_STANDOUT;
430 if (!ENTER_DIM && ENTER_STANDOUT)
431 ENTER_DIM = ENTER_STANDOUT;
432 if (!ENTER_REVERSE && ENTER_STANDOUT)
433 ENTER_REVERSE = ENTER_STANDOUT;
434 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
435 EXIT_ATTRIBUTES = EXIT_STANDOUT;
438 * Note that we use REVERSE for the alternate character set,
439 * not the as/ae capabilities. This is because we are modelling
440 * the model 37 teletype (since that's what nroff outputs) and
441 * the typical as/ae is more of a graphics set, not the greek
442 * letters the 37 has.
445 UNDER_CHAR = tgetstr("uc", &bp);
446 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
452 return(putchar(c & 0177));
455 static int curmode = 0;
461 if (must_use_uc && (curmode&UNDERL)) {
468 setnewmode(int newmode)
471 if (curmode != NORMAL && newmode != NORMAL)
479 PRINT(EXIT_UNDERLINE);
482 /* This includes standout */
483 PRINT(EXIT_ATTRIBUTES);
488 PRINT(ENTER_REVERSE);
492 * This only works on a few terminals.
493 * It should be fixed.
495 PRINT(ENTER_UNDERLINE);
502 PRINT(ENTER_UNDERLINE);
509 * We should have some provision here for multiple modes
510 * on at once. This will have to come later.
512 PRINT(ENTER_STANDOUT);