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__
))
137 " %s [options] [file...]\n"), program_invocation_short_name
);
141 " -t, --terminal TERMINAL override the TERM environment variable\n"
142 " -i, --indicated underlining is indicated via a separate line\n"
143 " -V, --version output version information and exit\n"
144 " -h, --help display this help and exit\n\n"));
146 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
149 int main(int argc
, char **argv
)
151 int c
, ret
, tflag
= 0;
155 static const struct option longopts
[] = {
156 { "terminal", required_argument
, 0, 't' },
157 { "indicated", no_argument
, 0, 'i' },
158 { "version", no_argument
, 0, 'V' },
159 { "help", no_argument
, 0, 'h' },
163 setlocale(LC_ALL
, "");
164 bindtextdomain(PACKAGE
, LOCALEDIR
);
166 atexit(close_stdout
);
168 signal(SIGINT
, sig_handler
);
169 signal(SIGTERM
, sig_handler
);
171 termtype
= getenv("TERM");
174 * FIXME: why terminal type is lpr when command begins with c and has
175 * no terminal? If this behavior can be explained please insert
176 * reference or remove the code. In case this truly is desired command
177 * behavior this should be mentioned in manual page.
179 if (termtype
== NULL
|| (argv
[0][0] == 'c' && !isatty(STDOUT_FILENO
)))
182 while ((c
= getopt_long(argc
, argv
, "it:T:Vh", longopts
, NULL
)) != -1)
187 /* for nroff compatibility */
195 printf(_("%s from %s\n"), program_invocation_short_name
,
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
)
292 setcol((col
+8) & ~07);
308 if(handle_escape(f
)) {
311 _("unknown escape sequence in input: %o, %o"),
317 if (obuf
[col
].c_char
|| obuf
[col
].c_width
< 0) {
318 while(col
> 0 && obuf
[col
].c_width
< 0)
320 w
= obuf
[col
].c_width
;
321 for (i
= 0; i
< w
; i
++)
322 obuf
[col
++].c_mode
|= UNDERL
| mode
;
326 obuf
[col
].c_char
= '_';
327 obuf
[col
].c_width
= 1;
348 if (obuf
[col
].c_char
== '\0') {
349 obuf
[col
].c_char
= c
;
350 for (i
= 0; i
< w
; i
++)
351 obuf
[col
+i
].c_mode
= mode
;
352 obuf
[col
].c_width
= w
;
353 for (i
= 1; i
< w
; i
++)
354 obuf
[col
+i
].c_width
= -1;
355 } else if (obuf
[col
].c_char
== '_') {
356 obuf
[col
].c_char
= c
;
357 for (i
= 0; i
< w
; i
++)
358 obuf
[col
+i
].c_mode
|= UNDERL
|mode
;
359 obuf
[col
].c_width
= w
;
360 for (i
= 1; i
< w
; i
++)
361 obuf
[col
+i
].c_width
= -1;
362 } else if ((wint_t) obuf
[col
].c_char
== c
) {
363 for (i
= 0; i
< w
; i
++)
364 obuf
[col
+i
].c_mode
|= BOLD
|mode
;
366 w
= obuf
[col
].c_width
;
367 for (i
= 0; i
< w
; i
++)
368 obuf
[col
+i
].c_mode
= mode
;
377 static void flushln(void)
384 for (i
= 0; i
< maxcol
; i
++) {
385 if (obuf
[i
].c_mode
!= lastmode
) {
387 setmode(obuf
[i
].c_mode
);
388 lastmode
= obuf
[i
].c_mode
;
390 if (obuf
[i
].c_char
== '\0') {
392 print_out(CURS_RIGHT
);
396 outc(obuf
[i
].c_char
, obuf
[i
].c_width
);
397 if (obuf
[i
].c_width
> 1)
398 i
+= obuf
[i
].c_width
- 1;
400 if (lastmode
!= NORMAL
) {
403 if (must_overstrike
&& hadmodes
)
406 if (iflag
&& hadmodes
)
415 * For terminals that can overstrike, overstrike underlines and bolds.
416 * We don't do anything with halfline ups and downs, or Greek.
418 static void overstrike(void)
422 register wchar_t *lbuf
= __builtin_alloca((maxcol
+ 1) * sizeof(wchar_t));
424 wchar_t lbuf
[BUFSIZ
];
426 register wchar_t *cp
= lbuf
;
429 /* Set up overstrike buffer */
430 for (i
= 0; i
< maxcol
; i
++)
431 switch (obuf
[i
].c_mode
) {
440 *cp
++ = obuf
[i
].c_char
;
441 if (obuf
[i
].c_width
> 1)
442 i
+= obuf
[i
].c_width
- 1;
447 for (*cp
= ' '; *cp
== ' '; cp
--)
449 for (cp
= lbuf
; *cp
; cp
++)
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 char *lbuf
= __builtin_alloca((maxcol
+1)*sizeof(char));
469 register char *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 for (cp
= lbuf
; *cp
; cp
++)
488 static void initbuf(void)
493 obuf
= xmalloc(sizeof(struct CHAR
) * obuflen
);
496 /* assumes NORMAL == 0 */
497 memset(obuf
, 0, sizeof(struct CHAR
) * obuflen
);
503 static void fwd(void)
514 static void reverse(void)
523 static void initinfo(void)
525 CURS_UP
= tigetstr("cuu1");
526 CURS_RIGHT
= tigetstr("cuf1");
527 CURS_LEFT
= tigetstr("cub1");
528 if (CURS_LEFT
== NULL
)
531 ENTER_STANDOUT
= tigetstr("smso");
532 EXIT_STANDOUT
= tigetstr("rmso");
533 ENTER_UNDERLINE
= tigetstr("smul");
534 EXIT_UNDERLINE
= tigetstr("rmul");
535 ENTER_DIM
= tigetstr("dim");
536 ENTER_BOLD
= tigetstr("bold");
537 ENTER_REVERSE
= tigetstr("rev");
538 EXIT_ATTRIBUTES
= tigetstr("sgr0");
540 if (!ENTER_BOLD
&& ENTER_REVERSE
)
541 ENTER_BOLD
= ENTER_REVERSE
;
542 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
543 ENTER_BOLD
= ENTER_STANDOUT
;
544 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
545 ENTER_UNDERLINE
= ENTER_STANDOUT
;
546 EXIT_UNDERLINE
= EXIT_STANDOUT
;
548 if (!ENTER_DIM
&& ENTER_STANDOUT
)
549 ENTER_DIM
= ENTER_STANDOUT
;
550 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
551 ENTER_REVERSE
= ENTER_STANDOUT
;
552 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
553 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
556 * Note that we use REVERSE for the alternate character set,
557 * not the as/ae capabilities. This is because we are modelling
558 * the model 37 teletype (since that's what nroff outputs) and
559 * the typical as/ae is more of a graphics set, not the greek
560 * letters the 37 has.
563 UNDER_CHAR
= tigetstr("uc");
564 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
567 static int curmode
= 0;
569 static void outc(wint_t c
, int width
) {
573 if (must_use_uc
&& (curmode
&UNDERL
)) {
574 for (i
= 0; i
< width
; i
++)
575 print_out(CURS_LEFT
);
576 for (i
= 0; i
< width
; i
++)
577 print_out(UNDER_CHAR
);
581 static void setmode(int newmode
)
584 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
592 print_out(EXIT_UNDERLINE
);
595 /* This includes standout */
596 print_out(EXIT_ATTRIBUTES
);
601 print_out(ENTER_REVERSE
);
605 * This only works on a few terminals.
606 * It should be fixed.
608 print_out(ENTER_UNDERLINE
);
609 print_out(ENTER_DIM
);
612 print_out(ENTER_DIM
);
615 print_out(ENTER_UNDERLINE
);
618 print_out(ENTER_BOLD
);
622 * We should have some provision here for multiple modes
623 * on at once. This will have to come later.
625 print_out(ENTER_STANDOUT
);
632 static void setcol(int newcol
) {
637 else if (col
> maxcol
)
641 static void needcol(int acol
) {
644 /* If col >= obuflen, expand obuf until obuflen > col. */
645 while (acol
>= obuflen
) {
646 /* Paranoid check for obuflen == INT_MAX. */
647 if (obuflen
== INT_MAX
)
648 errx(EXIT_FAILURE
, _("Input line too long."));
650 /* Similar paranoia: double only up to INT_MAX. */
651 if (obuflen
< (INT_MAX
/ 2))
656 /* Now we can try to expand obuf. */
657 obuf
= xrealloc(obuf
, sizeof(struct CHAR
) * obuflen
);
661 static void sig_handler(int signo
__attribute__ ((__unused__
)))
666 static void print_out(char *line
)