]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/ul.c
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@misiek.eu.org>
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 bzero(), strcpy() */
46 #include <term.h> /* for setupterm() */
47 #include <stdlib.h> /* for getenv() */
48 #include <limits.h> /* for INT_MAX */
53 #ifdef ENABLE_WIDECHAR
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);
75 void setmode(int newmode
);
76 void setcol(int newcol
);
86 #define ALTSET 001 /* Reverse */
87 #define SUPERSC 002 /* Dim */
88 #define SUBSC 004 /* Dim | Ul */
89 #define UNDERL 010 /* Ul */
90 #define BOLD 020 /* Bold */
93 int must_use_uc
, must_overstrike
;
94 char *CURS_UP
, *CURS_RIGHT
, *CURS_LEFT
,
95 *ENTER_STANDOUT
, *EXIT_STANDOUT
, *ENTER_UNDERLINE
, *EXIT_UNDERLINE
,
96 *ENTER_DIM
, *ENTER_BOLD
, *ENTER_REVERSE
, *UNDER_CHAR
, *EXIT_ATTRIBUTES
;
104 int obuflen
; /* Tracks number of elements in obuf. */
111 #define PRINT(s) if (s == NULL) /* void */; else putwp(s)
113 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:")) != EOF
)
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
))
184 while ((c
= getwc(f
)) != WEOF
) switch(c
) {
191 setcol((col
+8) & ~07);
207 switch (c
= getwc(f
)) {
213 } else if (halfpos
> 0) {
226 } else if (halfpos
< 0) {
241 _("Unknown escape sequence in input: %o, %o\n"),
248 if (obuf
[col
].c_char
)
249 obuf
[col
].c_mode
|= UNDERL
| mode
;
251 obuf
[col
].c_char
= '_';
266 if (!iswprint(c
)) /* non printing */
268 if (obuf
[col
].c_char
== '\0') {
269 obuf
[col
].c_char
= c
;
270 obuf
[col
].c_mode
= mode
;
271 } else if (obuf
[col
].c_char
== '_') {
272 obuf
[col
].c_char
= c
;
273 obuf
[col
].c_mode
|= UNDERL
|mode
;
274 } else if (obuf
[col
].c_char
== c
)
275 obuf
[col
].c_mode
|= BOLD
|mode
;
277 obuf
[col
].c_mode
= mode
;
292 for (i
=0; i
<maxcol
; i
++) {
293 if (obuf
[i
].c_mode
!= lastmode
) {
295 setmode(obuf
[i
].c_mode
);
296 lastmode
= obuf
[i
].c_mode
;
298 if (obuf
[i
].c_char
== '\0') {
304 outc(obuf
[i
].c_char
);
306 if (lastmode
!= NORMAL
) {
309 if (must_overstrike
&& hadmodes
)
312 if (iflag
&& hadmodes
)
314 (void)fflush(stdout
);
321 * For terminals that can overstrike, overstrike underlines and bolds.
322 * We don't do anything with halfline ups and downs, or Greek.
328 register wchar_t *lbuf
= __builtin_alloca((maxcol
+1)*sizeof(wchar_t));
332 register wchar_t *cp
= lbuf
;
335 /* Set up overstrike buffer */
336 for (i
=0; i
<maxcol
; i
++)
337 switch (obuf
[i
].c_mode
) {
346 *cp
++ = obuf
[i
].c_char
;
351 for (*cp
=' '; *cp
==' '; cp
--)
353 for (cp
=lbuf
; *cp
; cp
++)
357 for (cp
=lbuf
; *cp
; cp
++)
358 putwchar(*cp
=='_' ? ' ' : *cp
);
360 for (cp
=lbuf
; *cp
; cp
++)
361 putwchar(*cp
=='_' ? ' ' : *cp
);
369 register char *lbuf
= __builtin_alloca((maxcol
+1)*sizeof(char));
373 register char *cp
= lbuf
;
375 for (i
=0; i
<maxcol
; i
++)
376 switch (obuf
[i
].c_mode
) {
377 case NORMAL
: *cp
++ = ' '; break;
378 case ALTSET
: *cp
++ = 'g'; break;
379 case SUPERSC
: *cp
++ = '^'; break;
380 case SUBSC
: *cp
++ = 'v'; break;
381 case UNDERL
: *cp
++ = '_'; break;
382 case BOLD
: *cp
++ = '!'; break;
383 default: *cp
++ = 'X'; break;
385 for (*cp
=' '; *cp
==' '; cp
--)
387 for (cp
=lbuf
; *cp
; cp
++)
394 if (obuf
== NULL
) { /* First time. */
396 obuf
= malloc(sizeof(struct CHAR
) * obuflen
);
398 fprintf(stderr
, _("Unable to allocate buffer.\n"));
403 /* assumes NORMAL == 0 */
404 bzero((char *)obuf
, sizeof(struct CHAR
) * obuflen
);
434 CURS_UP
= tigetstr("cuu1");
435 CURS_RIGHT
= tigetstr("cuf1");
436 CURS_LEFT
= tigetstr("cub1");
437 if (CURS_LEFT
== NULL
)
440 ENTER_STANDOUT
= tigetstr("smso");
441 EXIT_STANDOUT
= tigetstr("rmso");
442 ENTER_UNDERLINE
= tigetstr("smul");
443 EXIT_UNDERLINE
= tigetstr("rmul");
444 ENTER_DIM
= tigetstr("dim");
445 ENTER_BOLD
= tigetstr("bold");
446 ENTER_REVERSE
= tigetstr("rev");
447 EXIT_ATTRIBUTES
= tigetstr("sgr0");
449 if (!ENTER_BOLD
&& ENTER_REVERSE
)
450 ENTER_BOLD
= ENTER_REVERSE
;
451 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
452 ENTER_BOLD
= ENTER_STANDOUT
;
453 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
454 ENTER_UNDERLINE
= ENTER_STANDOUT
;
455 EXIT_UNDERLINE
= EXIT_STANDOUT
;
457 if (!ENTER_DIM
&& ENTER_STANDOUT
)
458 ENTER_DIM
= ENTER_STANDOUT
;
459 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
460 ENTER_REVERSE
= ENTER_STANDOUT
;
461 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
462 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
465 * Note that we use REVERSE for the alternate character set,
466 * not the as/ae capabilities. This is because we are modelling
467 * the model 37 teletype (since that's what nroff outputs) and
468 * the typical as/ae is more of a graphics set, not the greek
469 * letters the 37 has.
472 UNDER_CHAR
= tigetstr("uc");
473 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
476 static int curmode
= 0;
481 if (must_use_uc
&& (curmode
&UNDERL
)) {
487 void setmode(int newmode
)
490 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
498 PRINT(EXIT_UNDERLINE
);
501 /* This includes standout */
502 PRINT(EXIT_ATTRIBUTES
);
507 PRINT(ENTER_REVERSE
);
511 * This only works on a few terminals.
512 * It should be fixed.
514 PRINT(ENTER_UNDERLINE
);
521 PRINT(ENTER_UNDERLINE
);
528 * We should have some provision here for multiple modes
529 * on at once. This will have to come later.
531 PRINT(ENTER_STANDOUT
);
541 void setcol(int newcol
)
547 else if (col
> maxcol
) {
550 /* If col >= obuflen, expand obuf until obuflen > col. */
551 while (col
>= obuflen
) {
552 /* Paranoid check for obuflen == INT_MAX. */
553 if (obuflen
== INT_MAX
) {
555 _("Input line too long.\n"));
559 /* Similar paranoia: double only up to INT_MAX. */
560 obuflen
= ((INT_MAX
/ 2) < obuflen
)
564 /* Now we can try to expand obuf. */
565 obuf
= realloc(obuf
, sizeof(struct CHAR
) * obuflen
);
568 _("Out of memory when growing buffer.\n"));