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 */
54 static int put1wc(int c) /* Output an ASCII character as a wide character */
56 if (putwchar(c) == WEOF)
61 #define putwp(s) tputs(s,1,put1wc)
63 #define putwp(s) putp(s)
68 void overstrike(void);
74 void outc(wint_t c, int width);
75 void setmode(int newmode);
76 static void setcol(int newcol);
77 static void needcol(int col);
87 #define ALTSET 001 /* Reverse */
88 #define SUPERSC 002 /* Dim */
89 #define SUBSC 004 /* Dim | Ul */
90 #define UNDERL 010 /* Ul */
91 #define BOLD 020 /* Bold */
94 int must_use_uc, must_overstrike;
95 char *CURS_UP, *CURS_RIGHT, *CURS_LEFT,
96 *ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
97 *ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
106 int obuflen; /* Tracks number of elements in obuf. */
113 #define PRINT(s) if (s == NULL) /* void */; else putwp(s)
115 int main(int argc, char **argv)
121 setlocale(LC_ALL, "");
122 bindtextdomain(PACKAGE, LOCALEDIR);
125 termtype = getenv("TERM");
126 if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
128 while ((c = getopt(argc, argv, "it:T:")) != -1)
132 case 'T': /* for nroff compatibility */
141 _("usage: %s [ -i ] [ -tTerm ] file...\n"),
145 setupterm(termtype, 1, &ret);
152 fprintf(stderr,_("trouble reading terminfo"));
153 /* fall through to ... */
156 /* No such terminal type - assume dumb */
157 setupterm("dumb", 1, (int *)0);
161 if ( (tigetflag("os") && ENTER_BOLD==NULL ) ||
162 (tigetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
167 else for (; optind<argc; optind++) {
168 f = fopen(argv[optind],"r");
170 perror(argv[optind]);
175 if (ferror(stdout) || fclose(stdout))
185 while ((c = getwc(f)) != WEOF) switch(c) {
192 setcol((col+8) & ~07);
208 switch (c = getwc(f)) {
214 } else if (halfpos > 0) {
227 } else if (halfpos < 0) {
242 _("Unknown escape sequence in input: %o, %o\n"),
249 if (obuf[col].c_char || obuf[col].c_width < 0) {
250 while(col > 0 && obuf[col].c_width < 0)
252 w = obuf[col].c_width;
253 for (i = 0; i < w; i++)
254 obuf[col++].c_mode |= UNDERL | mode;
258 obuf[col].c_char = '_';
259 obuf[col].c_width = 1;
275 if (!iswprint(c)) /* non printing */
279 if (obuf[col].c_char == '\0') {
280 obuf[col].c_char = c;
281 for (i = 0; i < w; i++)
282 obuf[col+i].c_mode = mode;
283 obuf[col].c_width = w;
284 for (i = 1; i < w; i++)
285 obuf[col+i].c_width = -1;
286 } else if (obuf[col].c_char == '_') {
287 obuf[col].c_char = c;
288 for (i = 0; i < w; i++)
289 obuf[col+i].c_mode |= UNDERL|mode;
290 obuf[col].c_width = w;
291 for (i = 1; i < w; i++)
292 obuf[col+i].c_width = -1;
293 } else if (obuf[col].c_char == c) {
294 for (i = 0; i < w; i++)
295 obuf[col+i].c_mode |= BOLD|mode;
297 w = obuf[col].c_width;
298 for (i = 0; i < w; i++)
299 obuf[col+i].c_mode = mode;
315 for (i=0; i<maxcol; i++) {
316 if (obuf[i].c_mode != lastmode) {
318 setmode(obuf[i].c_mode);
319 lastmode = obuf[i].c_mode;
321 if (obuf[i].c_char == '\0') {
327 outc(obuf[i].c_char, obuf[i].c_width);
328 if (obuf[i].c_width > 1)
329 i += obuf[i].c_width -1;
331 if (lastmode != NORMAL) {
334 if (must_overstrike && hadmodes)
337 if (iflag && hadmodes)
339 (void)fflush(stdout);
346 * For terminals that can overstrike, overstrike underlines and bolds.
347 * We don't do anything with halfline ups and downs, or Greek.
349 void overstrike(void)
353 register wchar_t *lbuf = __builtin_alloca((maxcol+1)*sizeof(wchar_t));
357 register wchar_t *cp = lbuf;
360 /* Set up overstrike buffer */
361 for (i=0; i<maxcol; i++)
362 switch (obuf[i].c_mode) {
371 *cp++ = obuf[i].c_char;
372 if (obuf[i].c_width > 1)
373 i += obuf[i].c_width - 1;
378 for (*cp=' '; *cp==' '; cp--)
380 for (cp=lbuf; *cp; cp++)
384 for (cp=lbuf; *cp; cp++)
385 putwchar(*cp=='_' ? ' ' : *cp);
387 for (cp=lbuf; *cp; cp++)
388 putwchar(*cp=='_' ? ' ' : *cp);
396 register char *lbuf = __builtin_alloca((maxcol+1)*sizeof(char));
400 register char *cp = lbuf;
402 for (i=0; i<maxcol; i++)
403 switch (obuf[i].c_mode) {
404 case NORMAL: *cp++ = ' '; break;
405 case ALTSET: *cp++ = 'g'; break;
406 case SUPERSC: *cp++ = '^'; break;
407 case SUBSC: *cp++ = 'v'; break;
408 case UNDERL: *cp++ = '_'; break;
409 case BOLD: *cp++ = '!'; break;
410 default: *cp++ = 'X'; break;
412 for (*cp=' '; *cp==' '; cp--)
414 for (cp=lbuf; *cp; cp++)
421 if (obuf == NULL) { /* First time. */
423 obuf = malloc(sizeof(struct CHAR) * obuflen);
425 fprintf(stderr, _("Unable to allocate buffer.\n"));
430 /* assumes NORMAL == 0 */
431 memset(obuf, 0, sizeof(struct CHAR) * obuflen);
459 CURS_UP = tigetstr("cuu1");
460 CURS_RIGHT = tigetstr("cuf1");
461 CURS_LEFT = tigetstr("cub1");
462 if (CURS_LEFT == NULL)
465 ENTER_STANDOUT = tigetstr("smso");
466 EXIT_STANDOUT = tigetstr("rmso");
467 ENTER_UNDERLINE = tigetstr("smul");
468 EXIT_UNDERLINE = tigetstr("rmul");
469 ENTER_DIM = tigetstr("dim");
470 ENTER_BOLD = tigetstr("bold");
471 ENTER_REVERSE = tigetstr("rev");
472 EXIT_ATTRIBUTES = tigetstr("sgr0");
474 if (!ENTER_BOLD && ENTER_REVERSE)
475 ENTER_BOLD = ENTER_REVERSE;
476 if (!ENTER_BOLD && ENTER_STANDOUT)
477 ENTER_BOLD = ENTER_STANDOUT;
478 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
479 ENTER_UNDERLINE = ENTER_STANDOUT;
480 EXIT_UNDERLINE = EXIT_STANDOUT;
482 if (!ENTER_DIM && ENTER_STANDOUT)
483 ENTER_DIM = ENTER_STANDOUT;
484 if (!ENTER_REVERSE && ENTER_STANDOUT)
485 ENTER_REVERSE = ENTER_STANDOUT;
486 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
487 EXIT_ATTRIBUTES = EXIT_STANDOUT;
490 * Note that we use REVERSE for the alternate character set,
491 * not the as/ae capabilities. This is because we are modelling
492 * the model 37 teletype (since that's what nroff outputs) and
493 * the typical as/ae is more of a graphics set, not the greek
494 * letters the 37 has.
497 UNDER_CHAR = tigetstr("uc");
498 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
501 static int curmode = 0;
504 outc(wint_t c, int width) {
508 if (must_use_uc && (curmode&UNDERL)) {
509 for (i=0; i<width; i++)
511 for (i=0; i<width; i++)
516 void setmode(int newmode)
519 if (curmode != NORMAL && newmode != NORMAL)
527 PRINT(EXIT_UNDERLINE);
530 /* This includes standout */
531 PRINT(EXIT_ATTRIBUTES);
536 PRINT(ENTER_REVERSE);
540 * This only works on a few terminals.
541 * It should be fixed.
543 PRINT(ENTER_UNDERLINE);
550 PRINT(ENTER_UNDERLINE);
557 * We should have some provision here for multiple modes
558 * on at once. This will have to come later.
560 PRINT(ENTER_STANDOUT);
573 else if (col > maxcol)
581 /* If col >= obuflen, expand obuf until obuflen > col. */
582 while (col >= obuflen) {
583 /* Paranoid check for obuflen == INT_MAX. */
584 if (obuflen == INT_MAX) {
586 _("Input line too long.\n"));
590 /* Similar paranoia: double only up to INT_MAX. */
591 obuflen = ((INT_MAX / 2) < obuflen)
595 /* Now we can try to expand obuf. */
596 obuf = realloc(obuf, sizeof(struct CHAR) * obuflen);
599 _("Out of memory when growing buffer.\n"));