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.2 2003/06/17 04:29:33 dillon 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 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 __P((void));
79 void setnewmode __P((int));
80 void initcap __P((void));
81 void reverse __P((void));
82 int outchar __P((int));
84 void initbuf __P((void));
85 void iattr __P((void));
86 void overstrike __P((void));
87 void flushln __P((void));
88 void filter __P((FILE *));
91 #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
103 termtype = getenv("TERM");
104 if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
106 while ((c=getopt(argc, argv, "it:T:")) != -1)
110 case 'T': /* for nroff compatibility */
120 switch(tgetent(termcap, termtype)) {
126 warnx("trouble reading termcap");
127 /* fall through to ... */
130 /* No such terminal type - assume dumb */
131 (void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
135 if ( (tgetflag("os") && ENTER_BOLD==NULL ) ||
136 (tgetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
141 else for (; optind<argc; optind++) {
142 f = fopen(argv[optind],"r");
144 err(1, "%s", argv[optind]);
154 fprintf(stderr, "usage: ul [-i] [-t terminal] file...\n");
164 while ((c = getc(f)) != EOF && col < MAXBUF) switch(c) {
190 switch (c = getc(f)) {
196 } else if (halfpos > 0) {
209 } else if (halfpos < 0) {
223 errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
228 if (obuf[col].c_char)
229 obuf[col].c_mode |= UNDERL | mode;
231 obuf[col].c_char = '_';
248 if (c < ' ') /* non printing */
250 if (obuf[col].c_char == '\0') {
251 obuf[col].c_char = c;
252 obuf[col].c_mode = mode;
253 } else if (obuf[col].c_char == '_') {
254 obuf[col].c_char = c;
255 obuf[col].c_mode |= UNDERL|mode;
256 } else if (obuf[col].c_char == c)
257 obuf[col].c_mode |= BOLD|mode;
259 obuf[col].c_mode = mode;
277 for (i=0; i<maxcol; i++) {
278 if (obuf[i].c_mode != lastmode) {
280 setnewmode(obuf[i].c_mode);
281 lastmode = obuf[i].c_mode;
283 if (obuf[i].c_char == '\0') {
289 outc(obuf[i].c_char);
291 if (lastmode != NORMAL) {
294 if (must_overstrike && hadmodes)
297 if (iflag && hadmodes)
299 (void)fflush(stdout);
306 * For terminals that can overstrike, overstrike underlines and bolds.
307 * We don't do anything with halfline ups and downs, or Greek.
314 register char *cp = lbuf;
317 /* Set up overstrike buffer */
318 for (i=0; i<maxcol; i++)
319 switch (obuf[i].c_mode) {
328 *cp++ = obuf[i].c_char;
333 for (*cp=' '; *cp==' '; cp--)
335 for (cp=lbuf; *cp; cp++)
339 for (cp=lbuf; *cp; cp++)
340 putchar(*cp=='_' ? ' ' : *cp);
342 for (cp=lbuf; *cp; cp++)
343 putchar(*cp=='_' ? ' ' : *cp);
352 register char *cp = lbuf;
354 for (i=0; i<maxcol; i++)
355 switch (obuf[i].c_mode) {
356 case NORMAL: *cp++ = ' '; break;
357 case ALTSET: *cp++ = 'g'; break;
358 case SUPERSC: *cp++ = '^'; break;
359 case SUBSC: *cp++ = 'v'; break;
360 case UNDERL: *cp++ = '_'; break;
361 case BOLD: *cp++ = '!'; break;
362 default: *cp++ = 'X'; break;
364 for (*cp=' '; *cp==' '; cp--)
366 for (cp=lbuf; *cp; cp++)
375 bzero((char *)obuf, sizeof (obuf)); /* depends on NORMAL == 0 */
384 register oldcol, oldmax;
406 static char tcapbuf[512];
409 /* This nonsense attempts to work with both old and new termcap */
410 CURS_UP = tgetstr("up", &bp);
411 CURS_RIGHT = tgetstr("ri", &bp);
412 if (CURS_RIGHT == NULL)
413 CURS_RIGHT = tgetstr("nd", &bp);
414 CURS_LEFT = tgetstr("le", &bp);
415 if (CURS_LEFT == NULL)
416 CURS_LEFT = tgetstr("bc", &bp);
417 if (CURS_LEFT == NULL && tgetflag("bs"))
420 ENTER_STANDOUT = tgetstr("so", &bp);
421 EXIT_STANDOUT = tgetstr("se", &bp);
422 ENTER_UNDERLINE = tgetstr("us", &bp);
423 EXIT_UNDERLINE = tgetstr("ue", &bp);
424 ENTER_DIM = tgetstr("mh", &bp);
425 ENTER_BOLD = tgetstr("md", &bp);
426 ENTER_REVERSE = tgetstr("mr", &bp);
427 EXIT_ATTRIBUTES = tgetstr("me", &bp);
429 if (!ENTER_BOLD && ENTER_REVERSE)
430 ENTER_BOLD = ENTER_REVERSE;
431 if (!ENTER_BOLD && ENTER_STANDOUT)
432 ENTER_BOLD = ENTER_STANDOUT;
433 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
434 ENTER_UNDERLINE = ENTER_STANDOUT;
435 EXIT_UNDERLINE = EXIT_STANDOUT;
437 if (!ENTER_DIM && ENTER_STANDOUT)
438 ENTER_DIM = ENTER_STANDOUT;
439 if (!ENTER_REVERSE && ENTER_STANDOUT)
440 ENTER_REVERSE = ENTER_STANDOUT;
441 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
442 EXIT_ATTRIBUTES = EXIT_STANDOUT;
445 * Note that we use REVERSE for the alternate character set,
446 * not the as/ae capabilities. This is because we are modelling
447 * the model 37 teletype (since that's what nroff outputs) and
448 * the typical as/ae is more of a graphics set, not the greek
449 * letters the 37 has.
452 UNDER_CHAR = tgetstr("uc", &bp);
453 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
460 return(putchar(c & 0177));
463 static int curmode = 0;
470 if (must_use_uc && (curmode&UNDERL)) {
481 if (curmode != NORMAL && newmode != NORMAL)
489 PRINT(EXIT_UNDERLINE);
492 /* This includes standout */
493 PRINT(EXIT_ATTRIBUTES);
498 PRINT(ENTER_REVERSE);
502 * This only works on a few terminals.
503 * It should be fixed.
505 PRINT(ENTER_UNDERLINE);
512 PRINT(ENTER_UNDERLINE);
519 * We should have some provision here for multiple modes
520 * on at once. This will have to come later.
522 PRINT(ENTER_STANDOUT);