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. 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: head/usr.bin/ul/ul.c 245093 2013-01-06 03:08:27Z andrew $
53 #define ALTSET 001 /* Reverse */
54 #define SUPERSC 002 /* Dim */
55 #define SUBSC 004 /* Dim | Ul */
56 #define UNDERL 010 /* Ul */
57 #define BOLD 020 /* Bold */
59 static int must_use_uc
, must_overstrike
;
61 *CURS_UP
, *CURS_RIGHT
, *CURS_LEFT
,
62 *ENTER_STANDOUT
, *EXIT_STANDOUT
, *ENTER_UNDERLINE
, *EXIT_UNDERLINE
,
63 *ENTER_DIM
, *ENTER_BOLD
, *ENTER_REVERSE
, *UNDER_CHAR
, *EXIT_ATTRIBUTES
;
68 int c_width
; /* width or -1 if multi-column char. filler */
71 static struct CHAR obuf
[MAXBUF
];
72 static int col
, maxcol
;
78 static void usage(void);
79 static void setnewmode(int);
80 static void initcap(void);
81 static void reverse(void);
82 static int outchar(int);
83 static void fwd(void);
84 static void initbuf(void);
85 static void iattr(void);
86 static void overstrike(void);
87 static void flushln(void);
88 static void filter(FILE *);
89 static void outc(wint_t, int);
91 #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
94 main(int argc
, char **argv
)
101 setlocale(LC_ALL
, "");
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");
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
= getwc(f
)) != WEOF
&& col
< MAXBUF
) switch(c
) {
190 switch (c
= getwc(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
|| obuf
[col
].c_width
< 0) {
229 while (col
> 0 && obuf
[col
].c_width
< 0)
231 w
= obuf
[col
].c_width
;
232 for (i
= 0; i
< w
; i
++)
233 obuf
[col
++].c_mode
|= UNDERL
| mode
;
238 obuf
[col
].c_char
= '_';
239 obuf
[col
].c_width
= 1;
257 if ((w
= wcwidth(c
)) <= 0) /* non printing */
259 if (obuf
[col
].c_char
== '\0') {
260 obuf
[col
].c_char
= c
;
261 for (i
= 0; i
< w
; i
++)
262 obuf
[col
+ i
].c_mode
= mode
;
263 obuf
[col
].c_width
= w
;
264 for (i
= 1; i
< w
; i
++)
265 obuf
[col
+ i
].c_width
= -1;
266 } else if (obuf
[col
].c_char
== '_') {
267 obuf
[col
].c_char
= c
;
268 for (i
= 0; i
< w
; i
++)
269 obuf
[col
+ i
].c_mode
|= UNDERL
|mode
;
270 obuf
[col
].c_width
= w
;
271 for (i
= 1; i
< w
; i
++)
272 obuf
[col
+ i
].c_width
= -1;
273 } else if ((wint_t)obuf
[col
].c_char
== c
) {
274 for (i
= 0; i
< w
; i
++)
275 obuf
[col
+ i
].c_mode
|= BOLD
|mode
;
277 w
= obuf
[col
].c_width
;
278 for (i
= 0; i
< w
; i
++)
279 obuf
[col
+ i
].c_mode
= mode
;
300 for (i
=0; i
<maxcol
; i
++) {
301 if (obuf
[i
].c_mode
!= lastmode
) {
303 setnewmode(obuf
[i
].c_mode
);
304 lastmode
= obuf
[i
].c_mode
;
306 if (obuf
[i
].c_char
== '\0') {
312 outc(obuf
[i
].c_char
, obuf
[i
].c_width
);
313 if (obuf
[i
].c_width
> 1)
314 i
+= obuf
[i
].c_width
- 1;
316 if (lastmode
!= NORMAL
) {
319 if (must_overstrike
&& hadmodes
)
322 if (iflag
&& hadmodes
)
324 (void)fflush(stdout
);
331 * For terminals that can overstrike, overstrike underlines and bolds.
332 * We don't do anything with halfline ups and downs, or Greek.
342 /* Set up overstrike buffer */
343 for (i
=0; i
<maxcol
; i
++)
344 switch (obuf
[i
].c_mode
) {
353 *cp
++ = obuf
[i
].c_char
;
354 if (obuf
[i
].c_width
> 1)
355 i
+= obuf
[i
].c_width
- 1;
360 for (*cp
=' '; *cp
==' '; cp
--)
362 for (cp
=lbuf
; *cp
; cp
++)
366 for (cp
=lbuf
; *cp
; cp
++)
367 putwchar(*cp
=='_' ? ' ' : *cp
);
369 for (cp
=lbuf
; *cp
; cp
++)
370 putwchar(*cp
=='_' ? ' ' : *cp
);
381 for (i
=0; i
<maxcol
; i
++)
382 switch (obuf
[i
].c_mode
) {
383 case NORMAL
: *cp
++ = ' '; break;
384 case ALTSET
: *cp
++ = 'g'; break;
385 case SUPERSC
: *cp
++ = '^'; break;
386 case SUBSC
: *cp
++ = 'v'; break;
387 case UNDERL
: *cp
++ = '_'; break;
388 case BOLD
: *cp
++ = '!'; break;
389 default: *cp
++ = 'X'; break;
391 for (*cp
=' '; *cp
==' '; cp
--)
393 for (cp
=lbuf
; *cp
; cp
++)
402 bzero((char *)obuf
, sizeof (obuf
)); /* depends on NORMAL == 0 */
433 static char tcapbuf
[512];
436 /* This nonsense attempts to work with both old and new termcap */
437 CURS_UP
= tgetstr("up", &bp
);
438 CURS_RIGHT
= tgetstr("ri", &bp
);
439 if (CURS_RIGHT
== NULL
)
440 CURS_RIGHT
= tgetstr("nd", &bp
);
441 CURS_LEFT
= tgetstr("le", &bp
);
442 if (CURS_LEFT
== NULL
)
443 CURS_LEFT
= tgetstr("bc", &bp
);
444 if (CURS_LEFT
== NULL
&& tgetflag("bs"))
447 ENTER_STANDOUT
= tgetstr("so", &bp
);
448 EXIT_STANDOUT
= tgetstr("se", &bp
);
449 ENTER_UNDERLINE
= tgetstr("us", &bp
);
450 EXIT_UNDERLINE
= tgetstr("ue", &bp
);
451 ENTER_DIM
= tgetstr("mh", &bp
);
452 ENTER_BOLD
= tgetstr("md", &bp
);
453 ENTER_REVERSE
= tgetstr("mr", &bp
);
454 EXIT_ATTRIBUTES
= tgetstr("me", &bp
);
456 if (!ENTER_BOLD
&& ENTER_REVERSE
)
457 ENTER_BOLD
= ENTER_REVERSE
;
458 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
459 ENTER_BOLD
= ENTER_STANDOUT
;
460 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
461 ENTER_UNDERLINE
= ENTER_STANDOUT
;
462 EXIT_UNDERLINE
= EXIT_STANDOUT
;
464 if (!ENTER_DIM
&& ENTER_STANDOUT
)
465 ENTER_DIM
= ENTER_STANDOUT
;
466 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
467 ENTER_REVERSE
= ENTER_STANDOUT
;
468 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
469 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
472 * Note that we use REVERSE for the alternate character set,
473 * not the as/ae capabilities. This is because we are modelling
474 * the model 37 teletype (since that's what nroff outputs) and
475 * the typical as/ae is more of a graphics set, not the greek
476 * letters the 37 has.
479 UNDER_CHAR
= tgetstr("uc", &bp
);
480 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
486 return (putwchar(c
) != WEOF
? c
: EOF
);
489 static int curmode
= 0;
492 outc(wint_t c
, int width
)
497 if (must_use_uc
&& (curmode
&UNDERL
)) {
498 for (i
= 0; i
< width
; i
++)
500 for (i
= 0; i
< width
; i
++)
506 setnewmode(int newmode
)
509 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
517 PRINT(EXIT_UNDERLINE
);
520 /* This includes standout */
521 PRINT(EXIT_ATTRIBUTES
);
526 PRINT(ENTER_REVERSE
);
530 * This only works on a few terminals.
531 * It should be fixed.
533 PRINT(ENTER_UNDERLINE
);
540 PRINT(ENTER_UNDERLINE
);
547 * We should have some provision here for multiple modes
548 * on at once. This will have to come later.
550 PRINT(ENTER_STANDOUT
);