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 <stdlib.h> /* for getenv() */
47 #include <limits.h> /* for INT_MAX */
48 #include <signal.h> /* for signal() */
52 #if defined(HAVE_NCURSESW_TERM_H)
53 # include <ncursesw/term.h>
54 #elif defined(HAVE_NCURSES_TERM_H)
55 # include <ncurses/term.h>
56 #elif defined(HAVE_TERM_H)
64 #include "closestream.h"
67 /* Output an ASCII character as a wide character */
68 static int put1wc(int c
)
70 if (putwchar(c
) == WEOF
)
75 #define putwp(s) tputs(s, STDOUT_FILENO, put1wc)
77 #define putwp(s) putp(s)
80 static int handle_escape(FILE * f
);
81 static void filter(FILE *f
);
82 static void flushln(void);
83 static void overstrike(void);
84 static void iattr(void);
85 static void initbuf(void);
86 static void fwd(void);
87 static void reverse(void);
88 static void initinfo(void);
89 static void outc(wint_t c
, int width
);
90 static void xsetmode(int newmode
);
91 static void setcol(int newcol
);
92 static void needcol(int col
);
93 static void sig_handler(int signo
);
94 static void print_out(char *line
);
104 #define ALTSET 001 /* Reverse */
105 #define SUPERSC 002 /* Dim */
106 #define SUBSC 004 /* Dim | Ul */
107 #define UNDERL 010 /* Ul */
108 #define BOLD 020 /* Bold */
110 static int must_use_uc
, must_overstrike
;
111 static char *CURS_UP
,
130 static struct CHAR
*obuf
;
132 static int col
, maxcol
;
138 static void __attribute__((__noreturn__
))
141 fputs(USAGE_HEADER
, out
);
142 fprintf(out
, _(" %s [options] [<file> ...]\n"), program_invocation_short_name
);
144 fputs(USAGE_SEPARATOR
, out
);
145 fputs(_("Do underlining.\n"), out
);
147 fputs(USAGE_OPTIONS
, out
);
148 fputs(_(" -t, -T, --terminal TERMINAL override the TERM environment variable\n"), out
);
149 fputs(_(" -i, --indicated underlining is indicated via a separate line\n"), out
);
150 fputs(USAGE_SEPARATOR
, out
);
151 fputs(USAGE_HELP
, out
);
152 fputs(USAGE_VERSION
, out
);
154 fprintf(out
, USAGE_MAN_TAIL("ul(1)"));
156 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
159 int main(int argc
, char **argv
)
161 int c
, ret
, tflag
= 0;
165 static const struct option longopts
[] = {
166 { "terminal", required_argument
, NULL
, 't' },
167 { "indicated", no_argument
, NULL
, 'i' },
168 { "version", no_argument
, NULL
, 'V' },
169 { "help", no_argument
, NULL
, 'h' },
173 setlocale(LC_ALL
, "");
174 bindtextdomain(PACKAGE
, LOCALEDIR
);
176 atexit(close_stdout
);
178 signal(SIGINT
, sig_handler
);
179 signal(SIGTERM
, sig_handler
);
181 termtype
= getenv("TERM");
183 while ((c
= getopt_long(argc
, argv
, "it:T:Vh", longopts
, NULL
)) != -1)
188 /* for nroff compatibility */
196 printf(UTIL_LINUX_VERSION
);
201 errtryhelp(EXIT_FAILURE
);
203 setupterm(termtype
, STDOUT_FILENO
, &ret
);
210 warnx(_("trouble reading terminfo"));
211 /* fall through to ... */
215 warnx(_("terminal `%s' is not known, defaulting to `dumb'"),
217 setupterm("dumb", STDOUT_FILENO
, (int *)0);
221 if ((tigetflag("os") && ENTER_BOLD
==NULL
) ||
222 (tigetflag("ul") && ENTER_UNDERLINE
==NULL
&& UNDER_CHAR
==NULL
))
228 for (; optind
< argc
; optind
++) {
229 f
= fopen(argv
[optind
],"r");
231 err(EXIT_FAILURE
, _("cannot open %s"),
240 static int handle_escape(FILE * f
)
244 switch (c
= getwc(f
)) {
249 } else if (halfpos
> 0) {
261 } else if (halfpos
< 0) {
279 static void filter(FILE *f
)
284 while ((c
= getwc(f
)) != WEOF
) {
290 setcol((col
+ 8) & ~07);
302 if (handle_escape(f
)) {
305 _("unknown escape sequence in input: %o, %o"), IESC
, c
);
309 if (obuf
[col
].c_char
|| obuf
[col
].c_width
< 0) {
310 while (col
> 0 && obuf
[col
].c_width
< 0)
312 w
= obuf
[col
].c_width
;
313 for (i
= 0; i
< w
; i
++)
314 obuf
[col
++].c_mode
|= UNDERL
| mode
;
318 obuf
[col
].c_char
= '_';
319 obuf
[col
].c_width
= 1;
337 if (obuf
[col
].c_char
== '\0') {
338 obuf
[col
].c_char
= c
;
339 for (i
= 0; i
< w
; i
++)
340 obuf
[col
+ i
].c_mode
= mode
;
341 obuf
[col
].c_width
= w
;
342 for (i
= 1; i
< w
; i
++)
343 obuf
[col
+ i
].c_width
= -1;
344 } else if (obuf
[col
].c_char
== '_') {
345 obuf
[col
].c_char
= c
;
346 for (i
= 0; i
< w
; i
++)
347 obuf
[col
+ i
].c_mode
|= UNDERL
| mode
;
348 obuf
[col
].c_width
= w
;
349 for (i
= 1; i
< w
; i
++)
350 obuf
[col
+ i
].c_width
= -1;
351 } else if ((wint_t) obuf
[col
].c_char
== c
) {
352 for (i
= 0; i
< w
; i
++)
353 obuf
[col
+ i
].c_mode
|= BOLD
| mode
;
355 w
= obuf
[col
].c_width
;
356 for (i
= 0; i
< w
; i
++)
357 obuf
[col
+ i
].c_mode
= mode
;
367 static void flushln(void)
374 for (i
= 0; i
< maxcol
; i
++) {
375 if (obuf
[i
].c_mode
!= lastmode
) {
377 xsetmode(obuf
[i
].c_mode
);
378 lastmode
= obuf
[i
].c_mode
;
380 if (obuf
[i
].c_char
== '\0') {
382 print_out(CURS_RIGHT
);
386 outc(obuf
[i
].c_char
, obuf
[i
].c_width
);
387 if (obuf
[i
].c_width
> 1)
388 i
+= obuf
[i
].c_width
- 1;
390 if (lastmode
!= NORMAL
) {
393 if (must_overstrike
&& hadmodes
)
396 if (iflag
&& hadmodes
)
405 * For terminals that can overstrike, overstrike underlines and bolds.
406 * We don't do anything with halfline ups and downs, or Greek.
408 static void overstrike(void)
411 register wchar_t *lbuf
= xmalloc((maxcol
+ 1) * sizeof(wchar_t));
412 register wchar_t *cp
= lbuf
;
415 /* Set up overstrike buffer */
416 for (i
= 0; i
< maxcol
; i
++)
417 switch (obuf
[i
].c_mode
) {
426 *cp
++ = obuf
[i
].c_char
;
427 if (obuf
[i
].c_width
> 1)
428 i
+= obuf
[i
].c_width
- 1;
433 for (*cp
= ' '; *cp
== ' '; cp
--)
435 fputws(lbuf
, stdout
);
438 for (cp
= lbuf
; *cp
; cp
++)
439 putwchar(*cp
== '_' ? ' ' : *cp
);
441 for (cp
= lbuf
; *cp
; cp
++)
442 putwchar(*cp
== '_' ? ' ' : *cp
);
447 static void iattr(void)
450 register wchar_t *lbuf
= xmalloc((maxcol
+ 1) * sizeof(wchar_t));
451 register wchar_t *cp
= lbuf
;
453 for (i
= 0; i
< maxcol
; i
++)
454 switch (obuf
[i
].c_mode
) {
455 case NORMAL
: *cp
++ = ' '; break;
456 case ALTSET
: *cp
++ = 'g'; break;
457 case SUPERSC
: *cp
++ = '^'; break;
458 case SUBSC
: *cp
++ = 'v'; break;
459 case UNDERL
: *cp
++ = '_'; break;
460 case BOLD
: *cp
++ = '!'; break;
461 default: *cp
++ = 'X'; break;
463 for (*cp
= ' '; *cp
== ' '; cp
--)
465 fputws(lbuf
, stdout
);
470 static void initbuf(void)
475 obuf
= xcalloc(obuflen
, sizeof(struct CHAR
));
477 /* assumes NORMAL == 0 */
478 memset(obuf
, 0, sizeof(struct CHAR
) * maxcol
);
485 static void fwd(void)
496 static void reverse(void)
505 static void initinfo(void)
507 CURS_UP
= tigetstr("cuu1");
508 CURS_RIGHT
= tigetstr("cuf1");
509 CURS_LEFT
= tigetstr("cub1");
510 if (CURS_LEFT
== NULL
)
513 ENTER_STANDOUT
= tigetstr("smso");
514 EXIT_STANDOUT
= tigetstr("rmso");
515 ENTER_UNDERLINE
= tigetstr("smul");
516 EXIT_UNDERLINE
= tigetstr("rmul");
517 ENTER_DIM
= tigetstr("dim");
518 ENTER_BOLD
= tigetstr("bold");
519 ENTER_REVERSE
= tigetstr("rev");
520 EXIT_ATTRIBUTES
= tigetstr("sgr0");
522 if (!ENTER_BOLD
&& ENTER_REVERSE
)
523 ENTER_BOLD
= ENTER_REVERSE
;
524 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
525 ENTER_BOLD
= ENTER_STANDOUT
;
526 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
527 ENTER_UNDERLINE
= ENTER_STANDOUT
;
528 EXIT_UNDERLINE
= EXIT_STANDOUT
;
530 if (!ENTER_DIM
&& ENTER_STANDOUT
)
531 ENTER_DIM
= ENTER_STANDOUT
;
532 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
533 ENTER_REVERSE
= ENTER_STANDOUT
;
534 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
535 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
538 * Note that we use REVERSE for the alternate character set,
539 * not the as/ae capabilities. This is because we are modeling
540 * the model 37 teletype (since that's what nroff outputs) and
541 * the typical as/ae is more of a graphics set, not the greek
542 * letters the 37 has.
545 UNDER_CHAR
= tigetstr("uc");
546 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
549 static int curmode
= 0;
551 static void outc(wint_t c
, int width
) {
555 if (must_use_uc
&& (curmode
&UNDERL
)) {
556 for (i
= 0; i
< width
; i
++)
557 print_out(CURS_LEFT
);
558 for (i
= 0; i
< width
; i
++)
559 print_out(UNDER_CHAR
);
563 static void xsetmode(int newmode
)
566 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
574 print_out(EXIT_UNDERLINE
);
577 /* This includes standout */
578 print_out(EXIT_ATTRIBUTES
);
583 print_out(ENTER_REVERSE
);
587 * This only works on a few terminals.
588 * It should be fixed.
590 print_out(ENTER_UNDERLINE
);
591 print_out(ENTER_DIM
);
594 print_out(ENTER_DIM
);
597 print_out(ENTER_UNDERLINE
);
600 print_out(ENTER_BOLD
);
604 * We should have some provision here for multiple modes
605 * on at once. This will have to come later.
607 print_out(ENTER_STANDOUT
);
614 static void setcol(int newcol
) {
619 else if (col
> maxcol
)
623 static void needcol(int acol
) {
626 /* If col >= obuflen, expand obuf until obuflen > col. */
627 while (acol
>= obuflen
) {
628 /* Paranoid check for obuflen == INT_MAX. */
629 if (obuflen
== INT_MAX
)
630 errx(EXIT_FAILURE
, _("Input line too long."));
632 /* Similar paranoia: double only up to INT_MAX. */
633 if (obuflen
< (INT_MAX
/ 2))
638 /* Now we can try to expand obuf. */
639 obuf
= xrealloc(obuf
, sizeof(struct CHAR
) * obuflen
);
643 static void sig_handler(int signo
__attribute__ ((__unused__
)))
648 static void print_out(char *line
)