]>
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);
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
)
123 setlocale(LC_ALL
, "");
124 bindtextdomain(PACKAGE
, LOCALEDIR
);
127 termtype
= getenv("TERM");
128 if (termtype
== NULL
|| (argv
[0][0] == 'c' && !isatty(1)))
130 while ((c
=getopt(argc
, argv
, "it:T:")) != EOF
)
134 case 'T': /* for nroff compatibility */
143 _("usage: %s [ -i ] [ -tTerm ] file...\n"),
147 setupterm(termtype
, 1, &ret
);
154 fprintf(stderr
,_("trouble reading terminfo"));
155 /* fall through to ... */
158 /* No such terminal type - assume dumb */
159 setupterm("dumb", 1, (int *)0);
163 if ( (tigetflag("os") && ENTER_BOLD
==NULL
) ||
164 (tigetflag("ul") && ENTER_UNDERLINE
==NULL
&& UNDER_CHAR
==NULL
))
169 else for (; optind
<argc
; optind
++) {
170 f
= fopen(argv
[optind
],"r");
172 perror(argv
[optind
]);
177 if (ferror(stdout
) || fclose(stdout
))
187 while ((c
= getwc(f
)) != WEOF
) switch(c
) {
194 setcol((col
+8) & ~07);
210 switch (c
= getwc(f
)) {
216 } else if (halfpos
> 0) {
229 } else if (halfpos
< 0) {
244 _("Unknown escape sequence in input: %o, %o\n"),
251 if (obuf
[col
].c_char
|| obuf
[col
].c_width
< 0) {
252 while(col
> 0 && obuf
[col
].c_width
< 0)
254 w
= obuf
[col
].c_width
;
255 for (i
= 0; i
< w
; i
++)
256 obuf
[col
++].c_mode
|= UNDERL
| mode
;
260 obuf
[col
].c_char
= '_';
261 obuf
[col
].c_width
= 1;
277 if (!iswprint(c
)) /* non printing */
281 if (obuf
[col
].c_char
== '\0') {
282 obuf
[col
].c_char
= c
;
283 for (i
= 0; i
< w
; i
++)
284 obuf
[col
+i
].c_mode
= mode
;
285 obuf
[col
].c_width
= w
;
286 for (i
= 1; i
< w
; i
++)
287 obuf
[col
+i
].c_width
= -1;
288 } else if (obuf
[col
].c_char
== '_') {
289 obuf
[col
].c_char
= c
;
290 for (i
= 0; i
< w
; i
++)
291 obuf
[col
+i
].c_mode
|= UNDERL
|mode
;
292 obuf
[col
].c_width
= w
;
293 for (i
= 1; i
< w
; i
++)
294 obuf
[col
+i
].c_width
= -1;
295 } else if (obuf
[col
].c_char
== c
) {
296 for (i
= 0; i
< w
; i
++)
297 obuf
[col
+i
].c_mode
|= BOLD
|mode
;
299 w
= obuf
[col
].c_width
;
300 for (i
= 0; i
< w
; i
++)
301 obuf
[col
+i
].c_mode
= mode
;
317 for (i
=0; i
<maxcol
; i
++) {
318 if (obuf
[i
].c_mode
!= lastmode
) {
320 setmode(obuf
[i
].c_mode
);
321 lastmode
= obuf
[i
].c_mode
;
323 if (obuf
[i
].c_char
== '\0') {
329 outc(obuf
[i
].c_char
, obuf
[i
].c_width
);
330 if (obuf
[i
].c_width
> 1)
331 i
+= obuf
[i
].c_width
-1;
333 if (lastmode
!= NORMAL
) {
336 if (must_overstrike
&& hadmodes
)
339 if (iflag
&& hadmodes
)
341 (void)fflush(stdout
);
348 * For terminals that can overstrike, overstrike underlines and bolds.
349 * We don't do anything with halfline ups and downs, or Greek.
351 void overstrike(void)
355 register wchar_t *lbuf
= __builtin_alloca((maxcol
+1)*sizeof(wchar_t));
359 register wchar_t *cp
= lbuf
;
362 /* Set up overstrike buffer */
363 for (i
=0; i
<maxcol
; i
++)
364 switch (obuf
[i
].c_mode
) {
373 *cp
++ = obuf
[i
].c_char
;
374 if (obuf
[i
].c_width
> 1)
375 i
+= obuf
[i
].c_width
- 1;
380 for (*cp
=' '; *cp
==' '; cp
--)
382 for (cp
=lbuf
; *cp
; cp
++)
386 for (cp
=lbuf
; *cp
; cp
++)
387 putwchar(*cp
=='_' ? ' ' : *cp
);
389 for (cp
=lbuf
; *cp
; cp
++)
390 putwchar(*cp
=='_' ? ' ' : *cp
);
398 register char *lbuf
= __builtin_alloca((maxcol
+1)*sizeof(char));
402 register char *cp
= lbuf
;
404 for (i
=0; i
<maxcol
; i
++)
405 switch (obuf
[i
].c_mode
) {
406 case NORMAL
: *cp
++ = ' '; break;
407 case ALTSET
: *cp
++ = 'g'; break;
408 case SUPERSC
: *cp
++ = '^'; break;
409 case SUBSC
: *cp
++ = 'v'; break;
410 case UNDERL
: *cp
++ = '_'; break;
411 case BOLD
: *cp
++ = '!'; break;
412 default: *cp
++ = 'X'; break;
414 for (*cp
=' '; *cp
==' '; cp
--)
416 for (cp
=lbuf
; *cp
; cp
++)
423 if (obuf
== NULL
) { /* First time. */
425 obuf
= malloc(sizeof(struct CHAR
) * obuflen
);
427 fprintf(stderr
, _("Unable to allocate buffer.\n"));
432 /* assumes NORMAL == 0 */
433 memset(obuf
, 0, sizeof(struct CHAR
) * obuflen
);
461 CURS_UP
= tigetstr("cuu1");
462 CURS_RIGHT
= tigetstr("cuf1");
463 CURS_LEFT
= tigetstr("cub1");
464 if (CURS_LEFT
== NULL
)
467 ENTER_STANDOUT
= tigetstr("smso");
468 EXIT_STANDOUT
= tigetstr("rmso");
469 ENTER_UNDERLINE
= tigetstr("smul");
470 EXIT_UNDERLINE
= tigetstr("rmul");
471 ENTER_DIM
= tigetstr("dim");
472 ENTER_BOLD
= tigetstr("bold");
473 ENTER_REVERSE
= tigetstr("rev");
474 EXIT_ATTRIBUTES
= tigetstr("sgr0");
476 if (!ENTER_BOLD
&& ENTER_REVERSE
)
477 ENTER_BOLD
= ENTER_REVERSE
;
478 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
479 ENTER_BOLD
= ENTER_STANDOUT
;
480 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
481 ENTER_UNDERLINE
= ENTER_STANDOUT
;
482 EXIT_UNDERLINE
= EXIT_STANDOUT
;
484 if (!ENTER_DIM
&& ENTER_STANDOUT
)
485 ENTER_DIM
= ENTER_STANDOUT
;
486 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
487 ENTER_REVERSE
= ENTER_STANDOUT
;
488 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
489 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
492 * Note that we use REVERSE for the alternate character set,
493 * not the as/ae capabilities. This is because we are modelling
494 * the model 37 teletype (since that's what nroff outputs) and
495 * the typical as/ae is more of a graphics set, not the greek
496 * letters the 37 has.
499 UNDER_CHAR
= tigetstr("uc");
500 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
503 static int curmode
= 0;
506 outc(wint_t c
, int width
) {
510 if (must_use_uc
&& (curmode
&UNDERL
)) {
511 for (i
=0; i
<width
; i
++)
513 for (i
=0; i
<width
; i
++)
518 void setmode(int newmode
)
521 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
529 PRINT(EXIT_UNDERLINE
);
532 /* This includes standout */
533 PRINT(EXIT_ATTRIBUTES
);
538 PRINT(ENTER_REVERSE
);
542 * This only works on a few terminals.
543 * It should be fixed.
545 PRINT(ENTER_UNDERLINE
);
552 PRINT(ENTER_UNDERLINE
);
559 * We should have some provision here for multiple modes
560 * on at once. This will have to come later.
562 PRINT(ENTER_STANDOUT
);
575 else if (col
> maxcol
)
583 /* If col >= obuflen, expand obuf until obuflen > col. */
584 while (col
>= obuflen
) {
585 /* Paranoid check for obuflen == INT_MAX. */
586 if (obuflen
== INT_MAX
) {
588 _("Input line too long.\n"));
592 /* Similar paranoia: double only up to INT_MAX. */
593 obuflen
= ((INT_MAX
/ 2) < obuflen
)
597 /* Now we can try to expand obuf. */
598 obuf
= realloc(obuf
, sizeof(struct CHAR
) * obuflen
);
601 _("Out of memory when growing buffer.\n"));