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() */
59 /* Output an ASCII character as a wide character */
60 static int put1wc(int c
)
62 if (putwchar(c
) == WEOF
)
67 #define putwp(s) tputs(s, STDOUT_FILENO, put1wc)
69 #define putwp(s) putp(s)
72 static void usage(FILE *out
);
73 static void filter(FILE *f
);
74 static void flushln(void);
75 static void overstrike(void);
76 static void iattr(void);
77 static void initbuf(void);
78 static void fwd(void);
79 static void reverse(void);
80 static void initinfo(void);
81 static void outc(wint_t c
, int width
);
82 static void setmode(int newmode
);
83 static void setcol(int newcol
);
84 static void needcol(int col
);
85 static void sig_handler(int signo
);
86 static void print_out(char *line
);
96 #define ALTSET 001 /* Reverse */
97 #define SUPERSC 002 /* Dim */
98 #define SUBSC 004 /* Dim | Ul */
99 #define UNDERL 010 /* Ul */
100 #define BOLD 020 /* Bold */
102 int must_use_uc
, must_overstrike
;
130 static void __attribute__((__noreturn__
))
135 " %s [options] [file]\n"), program_invocation_short_name
);
139 " -t, --terminal TERMINAL overwrite TERM environment variable\n"
140 " -i, --indicated Underlining is indicated by a separate line\n"
141 " -V, --version output version information and exit\n"
142 " -h, --help display this help and exit\n\n"));
144 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
147 int main(int argc
, char **argv
)
153 static const struct option longopts
[] = {
154 { "terminal", required_argument
, 0, 't' },
155 { "indicated", no_argument
, 0, 'i' },
156 { "version", no_argument
, 0, 'V' },
157 { "help", no_argument
, 0, 'h' },
161 setlocale(LC_ALL
, "");
162 bindtextdomain(PACKAGE
, LOCALEDIR
);
165 signal(SIGINT
, sig_handler
);
166 signal(SIGTERM
, sig_handler
);
168 termtype
= getenv("TERM");
171 * FIXME: why terminal type is lpr when command begins with c and has
172 * no terminal? If this behavior can be explained please insert
173 * refrence or remove the code. In case this truly is desired command
174 * behavior this should be mentioned in manual page.
176 if (termtype
== NULL
|| (argv
[0][0] == 'c' && !isatty(STDOUT_FILENO
)))
179 while ((c
= getopt_long(argc
, argv
, "it:T:Vh", longopts
, NULL
)) != -1)
184 /* for nroff compatibility */
191 printf(_("%s from %s\n"), program_invocation_short_name
,
193 return(EXIT_SUCCESS
);
200 setupterm(termtype
, STDOUT_FILENO
, &ret
);
207 warnx(_("trouble reading terminfo"));
208 /* fall through to ... */
211 /* No such terminal type - assume dumb */
212 setupterm("dumb", STDOUT_FILENO
, (int *)0);
216 if ((tigetflag("os") && ENTER_BOLD
==NULL
) ||
217 (tigetflag("ul") && ENTER_UNDERLINE
==NULL
&& UNDER_CHAR
==NULL
))
223 for (; optind
< argc
; optind
++) {
224 f
= fopen(argv
[optind
],"r");
226 err(EXIT_FAILURE
, _("%s: open failed"),
231 if (ferror(stdout
) || fclose(stdout
))
238 static void filter(FILE *f
)
243 while ((c
= getwc(f
)) != WEOF
)
251 setcol((col
+8) & ~07);
267 switch (c
= getwc(f
)) {
273 } else if (halfpos
> 0) {
286 } else if (halfpos
< 0) {
301 _("unknown escape sequence in input: %o, %o"),
308 if (obuf
[col
].c_char
|| obuf
[col
].c_width
< 0) {
309 while(col
> 0 && obuf
[col
].c_width
< 0)
311 w
= obuf
[col
].c_width
;
312 for (i
= 0; i
< w
; i
++)
313 obuf
[col
++].c_mode
|= UNDERL
| mode
;
317 obuf
[col
].c_char
= '_';
318 obuf
[col
].c_width
= 1;
339 if (obuf
[col
].c_char
== '\0') {
340 obuf
[col
].c_char
= c
;
341 for (i
= 0; i
< w
; i
++)
342 obuf
[col
+i
].c_mode
= mode
;
343 obuf
[col
].c_width
= w
;
344 for (i
= 1; i
< w
; i
++)
345 obuf
[col
+i
].c_width
= -1;
346 } else if (obuf
[col
].c_char
== '_') {
347 obuf
[col
].c_char
= c
;
348 for (i
= 0; i
< w
; i
++)
349 obuf
[col
+i
].c_mode
|= UNDERL
|mode
;
350 obuf
[col
].c_width
= w
;
351 for (i
= 1; i
< w
; i
++)
352 obuf
[col
+i
].c_width
= -1;
353 } else if ((wint_t) obuf
[col
].c_char
== c
) {
354 for (i
= 0; i
< w
; i
++)
355 obuf
[col
+i
].c_mode
|= BOLD
|mode
;
357 w
= obuf
[col
].c_width
;
358 for (i
= 0; i
< w
; i
++)
359 obuf
[col
+i
].c_mode
= mode
;
368 static void flushln(void)
375 for (i
= 0; i
< maxcol
; i
++) {
376 if (obuf
[i
].c_mode
!= lastmode
) {
378 setmode(obuf
[i
].c_mode
);
379 lastmode
= obuf
[i
].c_mode
;
381 if (obuf
[i
].c_char
== '\0') {
383 print_out(CURS_RIGHT
);
387 outc(obuf
[i
].c_char
, obuf
[i
].c_width
);
388 if (obuf
[i
].c_width
> 1)
389 i
+= obuf
[i
].c_width
- 1;
391 if (lastmode
!= NORMAL
) {
394 if (must_overstrike
&& hadmodes
)
397 if (iflag
&& hadmodes
)
406 * For terminals that can overstrike, overstrike underlines and bolds.
407 * We don't do anything with halfline ups and downs, or Greek.
409 static void overstrike(void)
413 register wchar_t *lbuf
= __builtin_alloca((maxcol
+ 1) * sizeof(wchar_t));
415 wchar_t lbuf
[BUFSIZ
];
417 register wchar_t *cp
= lbuf
;
420 /* Set up overstrike buffer */
421 for (i
= 0; i
< maxcol
; i
++)
422 switch (obuf
[i
].c_mode
) {
431 *cp
++ = obuf
[i
].c_char
;
432 if (obuf
[i
].c_width
> 1)
433 i
+= obuf
[i
].c_width
- 1;
438 for (*cp
= ' '; *cp
== ' '; cp
--)
440 for (cp
= lbuf
; *cp
; cp
++)
444 for (cp
= lbuf
; *cp
; cp
++)
445 putwchar(*cp
== '_' ? ' ' : *cp
);
447 for (cp
= lbuf
; *cp
; cp
++)
448 putwchar(*cp
== '_' ? ' ' : *cp
);
452 static void iattr(void)
456 register char *lbuf
= __builtin_alloca((maxcol
+1)*sizeof(char));
460 register char *cp
= lbuf
;
462 for (i
= 0; i
< maxcol
; i
++)
463 switch (obuf
[i
].c_mode
) {
464 case NORMAL
: *cp
++ = ' '; break;
465 case ALTSET
: *cp
++ = 'g'; break;
466 case SUPERSC
: *cp
++ = '^'; break;
467 case SUBSC
: *cp
++ = 'v'; break;
468 case UNDERL
: *cp
++ = '_'; break;
469 case BOLD
: *cp
++ = '!'; break;
470 default: *cp
++ = 'X'; break;
472 for (*cp
= ' '; *cp
== ' '; cp
--)
474 for (cp
= lbuf
; *cp
; cp
++)
479 static void initbuf(void)
484 obuf
= xmalloc(sizeof(struct CHAR
) * obuflen
);
487 /* assumes NORMAL == 0 */
488 memset(obuf
, 0, sizeof(struct CHAR
) * obuflen
);
494 static void fwd(void)
505 static void reverse(void)
514 static void initinfo(void)
516 CURS_UP
= tigetstr("cuu1");
517 CURS_RIGHT
= tigetstr("cuf1");
518 CURS_LEFT
= tigetstr("cub1");
519 if (CURS_LEFT
== NULL
)
522 ENTER_STANDOUT
= tigetstr("smso");
523 EXIT_STANDOUT
= tigetstr("rmso");
524 ENTER_UNDERLINE
= tigetstr("smul");
525 EXIT_UNDERLINE
= tigetstr("rmul");
526 ENTER_DIM
= tigetstr("dim");
527 ENTER_BOLD
= tigetstr("bold");
528 ENTER_REVERSE
= tigetstr("rev");
529 EXIT_ATTRIBUTES
= tigetstr("sgr0");
531 if (!ENTER_BOLD
&& ENTER_REVERSE
)
532 ENTER_BOLD
= ENTER_REVERSE
;
533 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
534 ENTER_BOLD
= ENTER_STANDOUT
;
535 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
536 ENTER_UNDERLINE
= ENTER_STANDOUT
;
537 EXIT_UNDERLINE
= EXIT_STANDOUT
;
539 if (!ENTER_DIM
&& ENTER_STANDOUT
)
540 ENTER_DIM
= ENTER_STANDOUT
;
541 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
542 ENTER_REVERSE
= ENTER_STANDOUT
;
543 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
544 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
547 * Note that we use REVERSE for the alternate character set,
548 * not the as/ae capabilities. This is because we are modelling
549 * the model 37 teletype (since that's what nroff outputs) and
550 * the typical as/ae is more of a graphics set, not the greek
551 * letters the 37 has.
554 UNDER_CHAR
= tigetstr("uc");
555 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
558 static int curmode
= 0;
560 static void outc(wint_t c
, int width
) {
564 if (must_use_uc
&& (curmode
&UNDERL
)) {
565 for (i
= 0; i
< width
; i
++)
566 print_out(CURS_LEFT
);
567 for (i
= 0; i
< width
; i
++)
568 print_out(UNDER_CHAR
);
572 static void setmode(int newmode
)
575 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
583 print_out(EXIT_UNDERLINE
);
586 /* This includes standout */
587 print_out(EXIT_ATTRIBUTES
);
592 print_out(ENTER_REVERSE
);
596 * This only works on a few terminals.
597 * It should be fixed.
599 print_out(ENTER_UNDERLINE
);
600 print_out(ENTER_DIM
);
603 print_out(ENTER_DIM
);
606 print_out(ENTER_UNDERLINE
);
609 print_out(ENTER_BOLD
);
613 * We should have some provision here for multiple modes
614 * on at once. This will have to come later.
616 print_out(ENTER_STANDOUT
);
623 static void setcol(int newcol
) {
628 else if (col
> maxcol
)
632 static void needcol(int col
) {
635 /* If col >= obuflen, expand obuf until obuflen > col. */
636 while (col
>= obuflen
) {
637 /* Paranoid check for obuflen == INT_MAX. */
638 if (obuflen
== INT_MAX
)
639 errx(EXIT_FAILURE
, _("Input line too long."));
641 /* Similar paranoia: double only up to INT_MAX. */
642 if (obuflen
< (INT_MAX
/ 2))
647 /* Now we can try to expand obuf. */
648 obuf
= xrealloc(obuf
, sizeof(struct CHAR
) * obuflen
);
652 static void sig_handler(int signo
__attribute__ ((__unused__
)))
657 static void print_out(char *line
)