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"
65 #include "fgetwc_or_err.h"
75 NORMAL_CHARSET
= 0, /* Must be zero, see initbuf() */
76 ALTERNATIVE_CHARSET
= 1 << 0, /* Reverse */
77 SUPERSCRIPT
= 1 << 1, /* Dim */
78 SUBSCRIPT
= 1 << 2, /* Dim | Ul */
79 UNDERLINE
= 1 << 3, /* Ul */
80 BOLD
= 1 << 4, /* Bold */
89 char *enter_underline
;
95 char *exit_attributes
;
119 static void __attribute__((__noreturn__
)) usage(void)
123 fputs(USAGE_HEADER
, out
);
124 fprintf(out
, _(" %s [options] [<file> ...]\n"), program_invocation_short_name
);
126 fputs(USAGE_SEPARATOR
, out
);
127 fputs(_("Do underlining.\n"), out
);
129 fputs(USAGE_OPTIONS
, out
);
130 fputs(_(" -t, -T, --terminal TERMINAL override the TERM environment variable\n"), out
);
131 fputs(_(" -i, --indicated underlining is indicated via a separate line\n"), out
);
132 fprintf(out
, USAGE_HELP_OPTIONS(30));
134 fprintf(out
, USAGE_MAN_TAIL("ul(1)"));
139 static void need_column(struct ul_ctl
*ctl
, size_t new_max
)
141 ctl
->max_column
= new_max
;
143 while (new_max
>= ctl
->buflen
) {
145 ctl
->buf
= xreallocarray(ctl
->buf
, ctl
->buflen
, sizeof(struct ul_char
));
149 static void set_column(struct ul_ctl
*ctl
, size_t column
)
151 ctl
->column
= column
;
153 if (ctl
->max_column
< ctl
->column
)
154 need_column(ctl
, ctl
->column
);
157 static void init_buffer(struct ul_ctl
*ctl
)
159 if (ctl
->buf
== NULL
) {
161 ctl
->buflen
= BUFSIZ
;
162 ctl
->buf
= xcalloc(ctl
->buflen
, sizeof(struct ul_char
));
164 /* assumes NORMAL_CHARSET == 0 */
165 memset(ctl
->buf
, 0, sizeof(struct ul_char
) * ctl
->max_column
);
169 ctl
->mode
&= ALTERNATIVE_CHARSET
;
172 static void init_term_caps(struct ul_ctl
*ctl
, struct term_caps
*const tcs
)
174 tcs
->curs_up
= tigetstr("cuu1");
175 tcs
->curs_right
= tigetstr("cuf1");
176 tcs
->curs_left
= tigetstr("cub1");
177 if (tcs
->curs_left
== NULL
)
178 tcs
->curs_left
= "\b";
180 tcs
->enter_standout
= tigetstr("smso");
181 tcs
->exit_standout
= tigetstr("rmso");
182 tcs
->enter_underline
= tigetstr("smul");
183 tcs
->exit_underline
= tigetstr("rmul");
184 tcs
->enter_dim
= tigetstr("dim");
185 tcs
->enter_bold
= tigetstr("bold");
186 tcs
->enter_reverse
= tigetstr("rev");
187 tcs
->exit_attributes
= tigetstr("sgr0");
189 if (!tcs
->enter_bold
&& tcs
->enter_reverse
)
190 tcs
->enter_bold
= tcs
->enter_reverse
;
192 if (!tcs
->enter_bold
&& tcs
->enter_standout
)
193 tcs
->enter_bold
= tcs
->enter_standout
;
195 if (!tcs
->enter_underline
&& tcs
->enter_standout
) {
196 tcs
->enter_underline
= tcs
->enter_standout
;
197 tcs
->exit_underline
= tcs
->exit_standout
;
200 if (!tcs
->enter_dim
&& tcs
->enter_standout
)
201 tcs
->enter_dim
= tcs
->enter_standout
;
203 if (!tcs
->enter_reverse
&& tcs
->enter_standout
)
204 tcs
->enter_reverse
= tcs
->enter_standout
;
206 if (!tcs
->exit_attributes
&& tcs
->exit_standout
)
207 tcs
->exit_attributes
= tcs
->exit_standout
;
210 * Note that we use REVERSE for the alternate character set,
211 * not the as/ae capabilities. This is because we are modeling
212 * the model 37 teletype (since that's what nroff outputs) and
213 * the typical as/ae is more of a graphics set, not the greek
214 * letters the 37 has.
216 tcs
->under_char
= tigetstr("uc");
217 ctl
->must_use_uc
= (tcs
->under_char
&& !tcs
->enter_underline
);
219 if ((tigetflag("os") && tcs
->enter_bold
== NULL
) ||
220 (tigetflag("ul") && tcs
->enter_underline
== NULL
221 && tcs
->under_char
== NULL
))
222 ctl
->must_overstrike
= 1;
225 static void sig_handler(int signo
__attribute__((__unused__
)))
230 static int ul_putwchar(int c
)
232 if (putwchar(c
) == WEOF
)
237 static void print_line(char *line
)
241 tputs(line
, STDOUT_FILENO
, ul_putwchar
);
244 static void ul_setmode(struct ul_ctl
*ctl
, struct term_caps
const *const tcs
,
247 if (!ctl
->indicated_opt
) {
248 if (ctl
->current_mode
!= NORMAL_CHARSET
&& new_mode
!= NORMAL_CHARSET
)
249 ul_setmode(ctl
, tcs
, NORMAL_CHARSET
);
253 switch (ctl
->current_mode
) {
257 print_line(tcs
->exit_underline
);
260 /* This includes standout */
261 print_line(tcs
->exit_attributes
);
265 case ALTERNATIVE_CHARSET
:
266 print_line(tcs
->enter_reverse
);
270 * This only works on a few terminals.
271 * It should be fixed.
273 print_line(tcs
->enter_underline
);
274 print_line(tcs
->enter_dim
);
277 print_line(tcs
->enter_dim
);
280 print_line(tcs
->enter_underline
);
283 print_line(tcs
->enter_bold
);
287 * We should have some provision here for multiple modes
288 * on at once. This will have to come later.
290 print_line(tcs
->enter_standout
);
294 ctl
->current_mode
= new_mode
;
297 static void indicate_attribute(struct ul_ctl
*ctl
)
300 wchar_t *buf
= xcalloc(ctl
->max_column
+ 1, sizeof(wchar_t));
303 for (i
= 0; i
< ctl
->max_column
; i
++) {
304 switch (ctl
->buf
[i
].c_mode
) {
305 case NORMAL_CHARSET
: *p
++ = ' '; break;
306 case ALTERNATIVE_CHARSET
: *p
++ = 'g'; break;
307 case SUPERSCRIPT
: *p
++ = '^'; break;
308 case SUBSCRIPT
: *p
++ = 'v'; break;
309 case UNDERLINE
: *p
++ = '_'; break;
310 case BOLD
: *p
++ = '!'; break;
311 default: *p
++ = 'X'; break;
315 for (*p
= ' '; *p
== ' '; p
--)
323 static void output_char(struct ul_ctl
*ctl
, struct term_caps
const *const tcs
,
329 if (ctl
->must_use_uc
&& (ctl
->current_mode
& UNDERLINE
)) {
330 for (i
= 0; i
< width
; i
++)
331 print_line(tcs
->curs_left
);
332 for (i
= 0; i
< width
; i
++)
333 print_line(tcs
->under_char
);
338 * For terminals that can overstrike, overstrike underlines and bolds.
339 * We don't do anything with halfline ups and downs, or Greek.
341 static void overstrike(struct ul_ctl
*ctl
)
344 wchar_t *buf
= xcalloc(ctl
->max_column
+ 1, sizeof(wchar_t));
348 /* Set up overstrike buffer */
349 for (i
= 0; i
< ctl
->max_column
; i
++) {
350 switch (ctl
->buf
[i
].c_mode
) {
359 *p
++ = ctl
->buf
[i
].c_char
;
360 if (1 < ctl
->buf
[i
].c_width
)
361 i
+= ctl
->buf
[i
].c_width
- 1;
368 for (*p
= ' '; *p
== ' '; p
--)
374 for (p
= buf
; *p
; p
++)
375 putwchar(*p
== '_' ? ' ' : *p
);
377 for (p
= buf
; *p
; p
++)
378 putwchar(*p
== '_' ? ' ' : *p
);
383 static void flush_line(struct ul_ctl
*ctl
, struct term_caps
const *const tcs
)
389 last_mode
= NORMAL_CHARSET
;
390 for (i
= 0; i
< ctl
->max_column
; i
++) {
391 if (ctl
->buf
[i
].c_mode
!= last_mode
) {
393 ul_setmode(ctl
, tcs
, ctl
->buf
[i
].c_mode
);
394 last_mode
= ctl
->buf
[i
].c_mode
;
396 if (ctl
->buf
[i
].c_char
== '\0') {
398 print_line(tcs
->curs_right
);
400 output_char(ctl
, tcs
, ' ', 1);
402 output_char(ctl
, tcs
, ctl
->buf
[i
].c_char
, ctl
->buf
[i
].c_width
);
403 if (1 < ctl
->buf
[i
].c_width
)
404 i
+= ctl
->buf
[i
].c_width
- 1;
406 if (last_mode
!= NORMAL_CHARSET
)
407 ul_setmode(ctl
, tcs
, NORMAL_CHARSET
);
408 if (ctl
->must_overstrike
&& had_mode
)
411 if (ctl
->indicated_opt
&& had_mode
)
412 indicate_attribute(ctl
);
419 static void forward(struct ul_ctl
*ctl
, struct term_caps
const *const tcs
)
421 int old_column
, old_maximum
;
423 old_column
= ctl
->column
;
424 old_maximum
= ctl
->max_column
;
425 flush_line(ctl
, tcs
);
426 set_column(ctl
, old_column
);
427 ctl
->max_column
= old_maximum
;
430 static void reverse(struct ul_ctl
*ctl
, struct term_caps
const *const tcs
)
434 print_line(tcs
->curs_up
);
435 print_line(tcs
->curs_up
);
439 static int handle_escape(struct ul_ctl
*ctl
, struct term_caps
const *const tcs
, FILE *f
)
443 switch (c
= fgetwc_or_err(f
)) {
445 if (0 < ctl
->half_position
) {
446 ctl
->mode
&= ~SUBSCRIPT
;
447 ctl
->half_position
--;
448 } else if (ctl
->half_position
== 0) {
449 ctl
->mode
|= SUPERSCRIPT
;
450 ctl
->half_position
--;
452 ctl
->half_position
= 0;
457 if (ctl
->half_position
< 0) {
458 ctl
->mode
&= ~SUPERSCRIPT
;
459 ctl
->half_position
++;
460 } else if (ctl
->half_position
== 0) {
461 ctl
->mode
|= SUBSCRIPT
;
462 ctl
->half_position
++;
464 ctl
->half_position
= 0;
478 static void filter(struct ul_ctl
*ctl
, struct term_caps
const *const tcs
, FILE *f
)
483 while ((c
= fgetwc_or_err(f
)) != WEOF
) {
486 set_column(ctl
, ctl
->column
&& 0 < ctl
->column
? ctl
->column
- 1 : 0);
489 set_column(ctl
, (ctl
->column
+ 8) & ~07);
495 ctl
->mode
|= ALTERNATIVE_CHARSET
;
498 ctl
->mode
&= ~ALTERNATIVE_CHARSET
;
501 if (handle_escape(ctl
, tcs
, f
)) {
502 c
= fgetwc_or_err(f
);
504 _("unknown escape sequence in input: %o, %o"), ESC
, c
);
508 if (ctl
->buf
[ctl
->column
].c_char
|| ctl
->buf
[ctl
->column
].c_width
< 0) {
509 while (ctl
->buf
[ctl
->column
].c_width
< 0 && 0 < ctl
->column
)
511 width
= ctl
->buf
[ctl
->column
].c_width
;
512 for (i
= 0; i
< width
; i
++)
513 ctl
->buf
[ctl
->column
++].c_mode
|= UNDERLINE
| ctl
->mode
;
514 set_column(ctl
, 0 < ctl
->column
? ctl
->column
: 0);
517 ctl
->buf
[ctl
->column
].c_char
= '_';
518 ctl
->buf
[ctl
->column
].c_width
= 1;
521 set_column(ctl
, ctl
->column
+ 1);
524 flush_line(ctl
, tcs
);
527 flush_line(ctl
, tcs
);
535 need_column(ctl
, ctl
->column
+ width
);
536 if (ctl
->buf
[ctl
->column
].c_char
== '\0') {
537 ctl
->buf
[ctl
->column
].c_char
= c
;
538 for (i
= 0; i
< width
; i
++)
539 ctl
->buf
[ctl
->column
+ i
].c_mode
= ctl
->mode
;
540 ctl
->buf
[ctl
->column
].c_width
= width
;
541 for (i
= 1; i
< width
; i
++)
542 ctl
->buf
[ctl
->column
+ i
].c_width
= -1;
543 } else if (ctl
->buf
[ctl
->column
].c_char
== '_') {
544 ctl
->buf
[ctl
->column
].c_char
= c
;
545 for (i
= 0; i
< width
; i
++)
546 ctl
->buf
[ctl
->column
+ i
].c_mode
|= UNDERLINE
| ctl
->mode
;
547 ctl
->buf
[ctl
->column
].c_width
= width
;
548 for (i
= 1; i
< width
; i
++)
549 ctl
->buf
[ctl
->column
+ i
].c_width
= -1;
550 } else if ((wint_t) ctl
->buf
[ctl
->column
].c_char
== c
) {
551 for (i
= 0; i
< width
; i
++)
552 ctl
->buf
[ctl
->column
+ i
].c_mode
|= BOLD
| ctl
->mode
;
554 width
= ctl
->buf
[ctl
->column
].c_width
;
555 for (i
= 0; i
< width
; i
++)
556 ctl
->buf
[ctl
->column
+ i
].c_mode
= ctl
->mode
;
558 set_column(ctl
, ctl
->column
+ width
);
563 flush_line(ctl
, tcs
);
566 int main(int argc
, char **argv
)
568 int c
, ret
, opt_terminal
= 0;
570 struct term_caps tcs
= { 0 };
571 struct ul_ctl ctl
= { .current_mode
= NORMAL_CHARSET
};
574 static const struct option longopts
[] = {
575 { "terminal", required_argument
, NULL
, 't' },
576 { "indicated", no_argument
, NULL
, 'i' },
577 { "version", no_argument
, NULL
, 'V' },
578 { "help", no_argument
, NULL
, 'h' },
582 setlocale(LC_ALL
, "");
583 bindtextdomain(PACKAGE
, LOCALEDIR
);
585 close_stdout_atexit();
587 signal(SIGINT
, sig_handler
);
588 signal(SIGTERM
, sig_handler
);
590 termtype
= getenv("TERM");
592 while ((c
= getopt_long(argc
, argv
, "it:T:Vh", longopts
, NULL
)) != -1) {
597 /* for nroff compatibility */
602 ctl
.indicated_opt
= 1;
606 print_version(EXIT_SUCCESS
);
610 errtryhelp(EXIT_FAILURE
);
614 setupterm(termtype
, STDOUT_FILENO
, &ret
);
619 warnx(_("trouble reading terminfo"));
623 warnx(_("terminal `%s' is not known, defaulting to `dumb'"),
625 setupterm("dumb", STDOUT_FILENO
, (int *)0);
629 init_term_caps(&ctl
, &tcs
);
633 filter(&ctl
, &tcs
, stdin
);
635 for (; optind
< argc
; optind
++) {
636 f
= fopen(argv
[optind
], "r");
638 err(EXIT_FAILURE
, _("cannot open %s"), argv
[optind
]);
639 filter(&ctl
, &tcs
, f
);
645 del_curterm(cur_term
);