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 * modified by Kars de Jong <jongk@cs.utwente.nl>
36 * to use terminfo instead of termcap.
37 * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
38 * added Native Language Support
39 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
40 * modified to work correctly in multi-byte locales
44 #include <unistd.h> /* for getopt(), isatty() */
45 #include <string.h> /* for memset(), strcpy() */
46 #include <term.h> /* for setupterm() */
47 #include <stdlib.h> /* for getenv() */
48 #include <limits.h> /* for INT_MAX */
49 #include <signal.h> /* for signal() */
57 #include "closestream.h"
60 /* Output an ASCII character as a wide character */
61 static int put1wc(int c
)
63 if (putwchar(c
) == WEOF
)
68 #define putwp(s) tputs(s, STDOUT_FILENO, put1wc)
70 #define putwp(s) putp(s)
73 static int handle_escape(FILE * f
);
74 static void filter(FILE *f
);
75 static void flushln(void);
76 static void overstrike(void);
77 static void iattr(void);
78 static void initbuf(void);
79 static void fwd(void);
80 static void reverse(void);
81 static void initinfo(void);
82 static void outc(wint_t c
, int width
);
83 static void xsetmode(int newmode
);
84 static void setcol(int newcol
);
85 static void needcol(int col
);
86 static void sig_handler(int signo
);
87 static void print_out(char *line
);
97 #define ALTSET 001 /* Reverse */
98 #define SUPERSC 002 /* Dim */
99 #define SUBSC 004 /* Dim | Ul */
100 #define UNDERL 010 /* Ul */
101 #define BOLD 020 /* Bold */
103 static int must_use_uc
, must_overstrike
;
104 static char *CURS_UP
,
123 static struct CHAR
*obuf
;
125 static int col
, maxcol
;
131 static void __attribute__((__noreturn__
))
134 fputs(USAGE_HEADER
, out
);
135 fprintf(out
, _(" %s [options] [<file> ...]\n"), program_invocation_short_name
);
137 fputs(USAGE_SEPARATOR
, out
);
138 fputs(_("Do underlining.\n"), out
);
140 fputs(USAGE_OPTIONS
, out
);
141 fputs(_(" -t, -T, --terminal TERMINAL override the TERM environment variable\n"), out
);
142 fputs(_(" -i, --indicated underlining is indicated via a separate line\n"), out
);
143 fputs(USAGE_SEPARATOR
, out
);
144 fputs(USAGE_HELP
, out
);
145 fputs(USAGE_VERSION
, out
);
147 fprintf(out
, USAGE_MAN_TAIL("ul(1)"));
149 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
152 int main(int argc
, char **argv
)
154 int c
, ret
, tflag
= 0;
158 static const struct option longopts
[] = {
159 { "terminal", required_argument
, NULL
, 't' },
160 { "indicated", no_argument
, NULL
, 'i' },
161 { "version", no_argument
, NULL
, 'V' },
162 { "help", no_argument
, NULL
, 'h' },
166 setlocale(LC_ALL
, "");
167 bindtextdomain(PACKAGE
, LOCALEDIR
);
169 atexit(close_stdout
);
171 signal(SIGINT
, sig_handler
);
172 signal(SIGTERM
, sig_handler
);
174 termtype
= getenv("TERM");
176 while ((c
= getopt_long(argc
, argv
, "it:T:Vh", longopts
, NULL
)) != -1)
181 /* for nroff compatibility */
189 printf(UTIL_LINUX_VERSION
);
194 errtryhelp(EXIT_FAILURE
);
196 setupterm(termtype
, STDOUT_FILENO
, &ret
);
203 warnx(_("trouble reading terminfo"));
204 /* fall through to ... */
208 warnx(_("terminal `%s' is not known, defaulting to `dumb'"),
210 setupterm("dumb", STDOUT_FILENO
, (int *)0);
214 if ((tigetflag("os") && ENTER_BOLD
==NULL
) ||
215 (tigetflag("ul") && ENTER_UNDERLINE
==NULL
&& UNDER_CHAR
==NULL
))
221 for (; optind
< argc
; optind
++) {
222 f
= fopen(argv
[optind
],"r");
224 err(EXIT_FAILURE
, _("cannot open %s"),
233 static int handle_escape(FILE * f
)
237 switch (c
= getwc(f
)) {
242 } else if (halfpos
> 0) {
254 } else if (halfpos
< 0) {
272 static void filter(FILE *f
)
277 while ((c
= getwc(f
)) != WEOF
) {
283 setcol((col
+ 8) & ~07);
295 if (handle_escape(f
)) {
298 _("unknown escape sequence in input: %o, %o"), IESC
, c
);
302 if (obuf
[col
].c_char
|| obuf
[col
].c_width
< 0) {
303 while (col
> 0 && obuf
[col
].c_width
< 0)
305 w
= obuf
[col
].c_width
;
306 for (i
= 0; i
< w
; i
++)
307 obuf
[col
++].c_mode
|= UNDERL
| mode
;
311 obuf
[col
].c_char
= '_';
312 obuf
[col
].c_width
= 1;
330 if (obuf
[col
].c_char
== '\0') {
331 obuf
[col
].c_char
= c
;
332 for (i
= 0; i
< w
; i
++)
333 obuf
[col
+ i
].c_mode
= mode
;
334 obuf
[col
].c_width
= w
;
335 for (i
= 1; i
< w
; i
++)
336 obuf
[col
+ i
].c_width
= -1;
337 } else if (obuf
[col
].c_char
== '_') {
338 obuf
[col
].c_char
= c
;
339 for (i
= 0; i
< w
; i
++)
340 obuf
[col
+ i
].c_mode
|= UNDERL
| mode
;
341 obuf
[col
].c_width
= w
;
342 for (i
= 1; i
< w
; i
++)
343 obuf
[col
+ i
].c_width
= -1;
344 } else if ((wint_t) obuf
[col
].c_char
== c
) {
345 for (i
= 0; i
< w
; i
++)
346 obuf
[col
+ i
].c_mode
|= BOLD
| mode
;
348 w
= obuf
[col
].c_width
;
349 for (i
= 0; i
< w
; i
++)
350 obuf
[col
+ i
].c_mode
= mode
;
360 static void flushln(void)
367 for (i
= 0; i
< maxcol
; i
++) {
368 if (obuf
[i
].c_mode
!= lastmode
) {
370 xsetmode(obuf
[i
].c_mode
);
371 lastmode
= obuf
[i
].c_mode
;
373 if (obuf
[i
].c_char
== '\0') {
375 print_out(CURS_RIGHT
);
379 outc(obuf
[i
].c_char
, obuf
[i
].c_width
);
380 if (obuf
[i
].c_width
> 1)
381 i
+= obuf
[i
].c_width
- 1;
383 if (lastmode
!= NORMAL
) {
386 if (must_overstrike
&& hadmodes
)
389 if (iflag
&& hadmodes
)
398 * For terminals that can overstrike, overstrike underlines and bolds.
399 * We don't do anything with halfline ups and downs, or Greek.
401 static void overstrike(void)
404 register wchar_t *lbuf
= xmalloc((maxcol
+ 1) * sizeof(wchar_t));
405 register wchar_t *cp
= lbuf
;
408 /* Set up overstrike buffer */
409 for (i
= 0; i
< maxcol
; i
++)
410 switch (obuf
[i
].c_mode
) {
419 *cp
++ = obuf
[i
].c_char
;
420 if (obuf
[i
].c_width
> 1)
421 i
+= obuf
[i
].c_width
- 1;
426 for (*cp
= ' '; *cp
== ' '; cp
--)
428 fputws(lbuf
, stdout
);
431 for (cp
= lbuf
; *cp
; cp
++)
432 putwchar(*cp
== '_' ? ' ' : *cp
);
434 for (cp
= lbuf
; *cp
; cp
++)
435 putwchar(*cp
== '_' ? ' ' : *cp
);
440 static void iattr(void)
443 register wchar_t *lbuf
= xmalloc((maxcol
+ 1) * sizeof(wchar_t));
444 register wchar_t *cp
= lbuf
;
446 for (i
= 0; i
< maxcol
; i
++)
447 switch (obuf
[i
].c_mode
) {
448 case NORMAL
: *cp
++ = ' '; break;
449 case ALTSET
: *cp
++ = 'g'; break;
450 case SUPERSC
: *cp
++ = '^'; break;
451 case SUBSC
: *cp
++ = 'v'; break;
452 case UNDERL
: *cp
++ = '_'; break;
453 case BOLD
: *cp
++ = '!'; break;
454 default: *cp
++ = 'X'; break;
456 for (*cp
= ' '; *cp
== ' '; cp
--)
458 fputws(lbuf
, stdout
);
463 static void initbuf(void)
468 obuf
= xcalloc(obuflen
, sizeof(struct CHAR
));
470 /* assumes NORMAL == 0 */
471 memset(obuf
, 0, sizeof(struct CHAR
) * maxcol
);
478 static void fwd(void)
489 static void reverse(void)
498 static void initinfo(void)
500 CURS_UP
= tigetstr("cuu1");
501 CURS_RIGHT
= tigetstr("cuf1");
502 CURS_LEFT
= tigetstr("cub1");
503 if (CURS_LEFT
== NULL
)
506 ENTER_STANDOUT
= tigetstr("smso");
507 EXIT_STANDOUT
= tigetstr("rmso");
508 ENTER_UNDERLINE
= tigetstr("smul");
509 EXIT_UNDERLINE
= tigetstr("rmul");
510 ENTER_DIM
= tigetstr("dim");
511 ENTER_BOLD
= tigetstr("bold");
512 ENTER_REVERSE
= tigetstr("rev");
513 EXIT_ATTRIBUTES
= tigetstr("sgr0");
515 if (!ENTER_BOLD
&& ENTER_REVERSE
)
516 ENTER_BOLD
= ENTER_REVERSE
;
517 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
518 ENTER_BOLD
= ENTER_STANDOUT
;
519 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
520 ENTER_UNDERLINE
= ENTER_STANDOUT
;
521 EXIT_UNDERLINE
= EXIT_STANDOUT
;
523 if (!ENTER_DIM
&& ENTER_STANDOUT
)
524 ENTER_DIM
= ENTER_STANDOUT
;
525 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
526 ENTER_REVERSE
= ENTER_STANDOUT
;
527 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
528 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
531 * Note that we use REVERSE for the alternate character set,
532 * not the as/ae capabilities. This is because we are modeling
533 * the model 37 teletype (since that's what nroff outputs) and
534 * the typical as/ae is more of a graphics set, not the greek
535 * letters the 37 has.
538 UNDER_CHAR
= tigetstr("uc");
539 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
542 static int curmode
= 0;
544 static void outc(wint_t c
, int width
) {
548 if (must_use_uc
&& (curmode
&UNDERL
)) {
549 for (i
= 0; i
< width
; i
++)
550 print_out(CURS_LEFT
);
551 for (i
= 0; i
< width
; i
++)
552 print_out(UNDER_CHAR
);
556 static void xsetmode(int newmode
)
559 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
567 print_out(EXIT_UNDERLINE
);
570 /* This includes standout */
571 print_out(EXIT_ATTRIBUTES
);
576 print_out(ENTER_REVERSE
);
580 * This only works on a few terminals.
581 * It should be fixed.
583 print_out(ENTER_UNDERLINE
);
584 print_out(ENTER_DIM
);
587 print_out(ENTER_DIM
);
590 print_out(ENTER_UNDERLINE
);
593 print_out(ENTER_BOLD
);
597 * We should have some provision here for multiple modes
598 * on at once. This will have to come later.
600 print_out(ENTER_STANDOUT
);
607 static void setcol(int newcol
) {
612 else if (col
> maxcol
)
616 static void needcol(int acol
) {
619 /* If col >= obuflen, expand obuf until obuflen > col. */
620 while (acol
>= obuflen
) {
621 /* Paranoid check for obuflen == INT_MAX. */
622 if (obuflen
== INT_MAX
)
623 errx(EXIT_FAILURE
, _("Input line too long."));
625 /* Similar paranoia: double only up to INT_MAX. */
626 if (obuflen
< (INT_MAX
/ 2))
631 /* Now we can try to expand obuf. */
632 obuf
= xrealloc(obuf
, sizeof(struct CHAR
) * obuflen
);
636 static void sig_handler(int signo
__attribute__ ((__unused__
)))
641 static void print_out(char *line
)