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 void usage(FILE *out
);
74 static int handle_escape(FILE * f
);
75 static void filter(FILE *f
);
76 static void flushln(void);
77 static void overstrike(void);
78 static void iattr(void);
79 static void initbuf(void);
80 static void fwd(void);
81 static void reverse(void);
82 static void initinfo(void);
83 static void outc(wint_t c
, int width
);
84 static void setmode(int newmode
);
85 static void setcol(int newcol
);
86 static void needcol(int col
);
87 static void sig_handler(int signo
);
88 static void print_out(char *line
);
98 #define ALTSET 001 /* Reverse */
99 #define SUPERSC 002 /* Dim */
100 #define SUBSC 004 /* Dim | Ul */
101 #define UNDERL 010 /* Ul */
102 #define BOLD 020 /* Bold */
104 int must_use_uc
, must_overstrike
;
132 static void __attribute__((__noreturn__
))
135 fputs(USAGE_HEADER
, out
);
136 fprintf(out
, _(" %s [options] [<file> ...]\n"), program_invocation_short_name
);
137 fputs(USAGE_OPTIONS
, out
);
139 fputs(_(" -t, -T, --terminal TERMINAL override the TERM environment variable\n"), out
);
140 fputs(_(" -i, --indicated underlining is indicated via a separate line\n"), out
);
141 fputs(USAGE_SEPARATOR
, out
);
142 fputs(USAGE_HELP
, out
);
143 fputs(USAGE_VERSION
, out
);
145 fprintf(out
, USAGE_MAN_TAIL("ul(1)"));
147 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
150 int main(int argc
, char **argv
)
152 int c
, ret
, tflag
= 0;
156 static const struct option longopts
[] = {
157 { "terminal", required_argument
, 0, 't' },
158 { "indicated", no_argument
, 0, 'i' },
159 { "version", no_argument
, 0, 'V' },
160 { "help", no_argument
, 0, 'h' },
164 setlocale(LC_ALL
, "");
165 bindtextdomain(PACKAGE
, LOCALEDIR
);
167 atexit(close_stdout
);
169 signal(SIGINT
, sig_handler
);
170 signal(SIGTERM
, sig_handler
);
172 termtype
= getenv("TERM");
175 * FIXME: why terminal type is lpr when command begins with c and has
176 * no terminal? If this behavior can be explained please insert
177 * reference or remove the code. In case this truly is desired command
178 * behavior this should be mentioned in manual page.
180 if (termtype
== NULL
|| (argv
[0][0] == 'c' && !isatty(STDOUT_FILENO
)))
183 while ((c
= getopt_long(argc
, argv
, "it:T:Vh", longopts
, NULL
)) != -1)
188 /* for nroff compatibility */
196 printf(_("%s from %s\n"), program_invocation_short_name
,
204 setupterm(termtype
, STDOUT_FILENO
, &ret
);
211 warnx(_("trouble reading terminfo"));
212 /* fall through to ... */
216 warnx(_("terminal `%s' is not known, defaulting to `dumb'"),
218 setupterm("dumb", STDOUT_FILENO
, (int *)0);
222 if ((tigetflag("os") && ENTER_BOLD
==NULL
) ||
223 (tigetflag("ul") && ENTER_UNDERLINE
==NULL
&& UNDER_CHAR
==NULL
))
229 for (; optind
< argc
; optind
++) {
230 f
= fopen(argv
[optind
],"r");
232 err(EXIT_FAILURE
, _("cannot open %s"),
241 static int handle_escape(FILE * f
)
245 switch (c
= getwc(f
)) {
250 } else if (halfpos
> 0) {
262 } else if (halfpos
< 0) {
280 static void filter(FILE *f
)
285 while ((c
= getwc(f
)) != WEOF
)
293 setcol((col
+8) & ~07);
309 if(handle_escape(f
)) {
312 _("unknown escape sequence in input: %o, %o"),
318 if (obuf
[col
].c_char
|| obuf
[col
].c_width
< 0) {
319 while(col
> 0 && obuf
[col
].c_width
< 0)
321 w
= obuf
[col
].c_width
;
322 for (i
= 0; i
< w
; i
++)
323 obuf
[col
++].c_mode
|= UNDERL
| mode
;
327 obuf
[col
].c_char
= '_';
328 obuf
[col
].c_width
= 1;
349 if (obuf
[col
].c_char
== '\0') {
350 obuf
[col
].c_char
= c
;
351 for (i
= 0; i
< w
; i
++)
352 obuf
[col
+i
].c_mode
= mode
;
353 obuf
[col
].c_width
= w
;
354 for (i
= 1; i
< w
; i
++)
355 obuf
[col
+i
].c_width
= -1;
356 } else if (obuf
[col
].c_char
== '_') {
357 obuf
[col
].c_char
= c
;
358 for (i
= 0; i
< w
; i
++)
359 obuf
[col
+i
].c_mode
|= UNDERL
|mode
;
360 obuf
[col
].c_width
= w
;
361 for (i
= 1; i
< w
; i
++)
362 obuf
[col
+i
].c_width
= -1;
363 } else if ((wint_t) obuf
[col
].c_char
== c
) {
364 for (i
= 0; i
< w
; i
++)
365 obuf
[col
+i
].c_mode
|= BOLD
|mode
;
367 w
= obuf
[col
].c_width
;
368 for (i
= 0; i
< w
; i
++)
369 obuf
[col
+i
].c_mode
= mode
;
378 static void flushln(void)
385 for (i
= 0; i
< maxcol
; i
++) {
386 if (obuf
[i
].c_mode
!= lastmode
) {
388 setmode(obuf
[i
].c_mode
);
389 lastmode
= obuf
[i
].c_mode
;
391 if (obuf
[i
].c_char
== '\0') {
393 print_out(CURS_RIGHT
);
397 outc(obuf
[i
].c_char
, obuf
[i
].c_width
);
398 if (obuf
[i
].c_width
> 1)
399 i
+= obuf
[i
].c_width
- 1;
401 if (lastmode
!= NORMAL
) {
404 if (must_overstrike
&& hadmodes
)
407 if (iflag
&& hadmodes
)
416 * For terminals that can overstrike, overstrike underlines and bolds.
417 * We don't do anything with halfline ups and downs, or Greek.
419 static void overstrike(void)
423 register wchar_t *lbuf
= __builtin_alloca((maxcol
+ 1) * sizeof(wchar_t));
425 wchar_t lbuf
[BUFSIZ
];
427 register wchar_t *cp
= lbuf
;
430 /* Set up overstrike buffer */
431 for (i
= 0; i
< maxcol
; i
++)
432 switch (obuf
[i
].c_mode
) {
441 *cp
++ = obuf
[i
].c_char
;
442 if (obuf
[i
].c_width
> 1)
443 i
+= obuf
[i
].c_width
- 1;
448 for (*cp
= ' '; *cp
== ' '; cp
--)
450 fputws(lbuf
, stdout
);
453 for (cp
= lbuf
; *cp
; cp
++)
454 putwchar(*cp
== '_' ? ' ' : *cp
);
456 for (cp
= lbuf
; *cp
; cp
++)
457 putwchar(*cp
== '_' ? ' ' : *cp
);
461 static void iattr(void)
465 register wchar_t *lbuf
= __builtin_alloca((maxcol
+1)*sizeof(wchar_t));
467 wchar_t lbuf
[BUFSIZ
];
469 register wchar_t *cp
= lbuf
;
471 for (i
= 0; i
< maxcol
; i
++)
472 switch (obuf
[i
].c_mode
) {
473 case NORMAL
: *cp
++ = ' '; break;
474 case ALTSET
: *cp
++ = 'g'; break;
475 case SUPERSC
: *cp
++ = '^'; break;
476 case SUBSC
: *cp
++ = 'v'; break;
477 case UNDERL
: *cp
++ = '_'; break;
478 case BOLD
: *cp
++ = '!'; break;
479 default: *cp
++ = 'X'; break;
481 for (*cp
= ' '; *cp
== ' '; cp
--)
483 fputws(lbuf
, stdout
);
487 static void initbuf(void)
492 obuf
= xcalloc(obuflen
, sizeof(struct CHAR
));
494 /* assumes NORMAL == 0 */
495 memset(obuf
, 0, sizeof(struct CHAR
) * maxcol
);
502 static void fwd(void)
513 static void reverse(void)
522 static void initinfo(void)
524 CURS_UP
= tigetstr("cuu1");
525 CURS_RIGHT
= tigetstr("cuf1");
526 CURS_LEFT
= tigetstr("cub1");
527 if (CURS_LEFT
== NULL
)
530 ENTER_STANDOUT
= tigetstr("smso");
531 EXIT_STANDOUT
= tigetstr("rmso");
532 ENTER_UNDERLINE
= tigetstr("smul");
533 EXIT_UNDERLINE
= tigetstr("rmul");
534 ENTER_DIM
= tigetstr("dim");
535 ENTER_BOLD
= tigetstr("bold");
536 ENTER_REVERSE
= tigetstr("rev");
537 EXIT_ATTRIBUTES
= tigetstr("sgr0");
539 if (!ENTER_BOLD
&& ENTER_REVERSE
)
540 ENTER_BOLD
= ENTER_REVERSE
;
541 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
542 ENTER_BOLD
= ENTER_STANDOUT
;
543 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
544 ENTER_UNDERLINE
= ENTER_STANDOUT
;
545 EXIT_UNDERLINE
= EXIT_STANDOUT
;
547 if (!ENTER_DIM
&& ENTER_STANDOUT
)
548 ENTER_DIM
= ENTER_STANDOUT
;
549 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
550 ENTER_REVERSE
= ENTER_STANDOUT
;
551 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
552 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
555 * Note that we use REVERSE for the alternate character set,
556 * not the as/ae capabilities. This is because we are modelling
557 * the model 37 teletype (since that's what nroff outputs) and
558 * the typical as/ae is more of a graphics set, not the greek
559 * letters the 37 has.
562 UNDER_CHAR
= tigetstr("uc");
563 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
566 static int curmode
= 0;
568 static void outc(wint_t c
, int width
) {
572 if (must_use_uc
&& (curmode
&UNDERL
)) {
573 for (i
= 0; i
< width
; i
++)
574 print_out(CURS_LEFT
);
575 for (i
= 0; i
< width
; i
++)
576 print_out(UNDER_CHAR
);
580 static void setmode(int newmode
)
583 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
591 print_out(EXIT_UNDERLINE
);
594 /* This includes standout */
595 print_out(EXIT_ATTRIBUTES
);
600 print_out(ENTER_REVERSE
);
604 * This only works on a few terminals.
605 * It should be fixed.
607 print_out(ENTER_UNDERLINE
);
608 print_out(ENTER_DIM
);
611 print_out(ENTER_DIM
);
614 print_out(ENTER_UNDERLINE
);
617 print_out(ENTER_BOLD
);
621 * We should have some provision here for multiple modes
622 * on at once. This will have to come later.
624 print_out(ENTER_STANDOUT
);
631 static void setcol(int newcol
) {
636 else if (col
> maxcol
)
640 static void needcol(int acol
) {
643 /* If col >= obuflen, expand obuf until obuflen > col. */
644 while (acol
>= obuflen
) {
645 /* Paranoid check for obuflen == INT_MAX. */
646 if (obuflen
== INT_MAX
)
647 errx(EXIT_FAILURE
, _("Input line too long."));
649 /* Similar paranoia: double only up to INT_MAX. */
650 if (obuflen
< (INT_MAX
/ 2))
655 /* Now we can try to expand obuf. */
656 obuf
= xrealloc(obuf
, sizeof(struct CHAR
) * obuflen
);
660 static void sig_handler(int signo
__attribute__ ((__unused__
)))
665 static void print_out(char *line
)