]>
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> to use terminfo instead
40 #include <unistd.h> /* for getopt(), isatty() */
41 #include <string.h> /* for bzero() */
42 #include <term.h> /* for setupterm() */
43 #include <stdlib.h> /* for getenv() */
47 void overstrike(void);
54 void setmode(int newmode
);
65 #define ALTSET 001 /* Reverse */
66 #define SUPERSC 002 /* Dim */
67 #define SUBSC 004 /* Dim | Ul */
68 #define UNDERL 010 /* Ul */
69 #define BOLD 020 /* Bold */
71 int must_use_uc
, must_overstrike
;
72 char *CURS_UP
, *CURS_RIGHT
, *CURS_LEFT
,
73 *ENTER_STANDOUT
, *EXIT_STANDOUT
, *ENTER_UNDERLINE
, *EXIT_UNDERLINE
,
74 *ENTER_DIM
, *ENTER_BOLD
, *ENTER_REVERSE
, *UNDER_CHAR
, *EXIT_ATTRIBUTES
;
81 struct CHAR obuf
[MAXBUF
];
88 #define PRINT(s) if (s == NULL) /* void */; else putp(s)
90 int main(int argc
, char **argv
)
98 termtype
= getenv("TERM");
99 if (termtype
== NULL
|| (argv
[0][0] == 'c' && !isatty(1)))
101 while ((c
=getopt(argc
, argv
, "it:T:")) != EOF
)
105 case 'T': /* for nroff compatibility */
114 "usage: %s [ -i ] [ -tTerm ] file...\n",
118 setupterm(termtype
, 1, &ret
);
125 fprintf(stderr
,"trouble reading terminfo");
126 /* fall through to ... */
129 /* No such terminal type - assume dumb */
130 setupterm("dumb", 1, (int *)0);
134 if ( (tigetflag("os") && ENTER_BOLD
==NULL
) ||
135 (tigetflag("ul") && ENTER_UNDERLINE
==NULL
&& UNDER_CHAR
==NULL
))
140 else for (; optind
<argc
; optind
++) {
141 f
= fopen(argv
[optind
],"r");
143 perror(argv
[optind
]);
155 while ((c
= getc(f
)) != EOF
) switch(c
) {
181 switch (c
= getc(f
)) {
187 } else if (halfpos
> 0) {
200 } else if (halfpos
< 0) {
215 "Unknown escape sequence in input: %o, %o\n",
222 if (obuf
[col
].c_char
)
223 obuf
[col
].c_mode
|= UNDERL
| mode
;
225 obuf
[col
].c_char
= '_';
242 if (c
< ' ') /* non printing */
244 if (obuf
[col
].c_char
== '\0') {
245 obuf
[col
].c_char
= c
;
246 obuf
[col
].c_mode
= mode
;
247 } else if (obuf
[col
].c_char
== '_') {
248 obuf
[col
].c_char
= c
;
249 obuf
[col
].c_mode
|= UNDERL
|mode
;
250 } else if (obuf
[col
].c_char
== c
)
251 obuf
[col
].c_mode
|= BOLD
|mode
;
253 obuf
[col
].c_mode
= mode
;
270 for (i
=0; i
<maxcol
; i
++) {
271 if (obuf
[i
].c_mode
!= lastmode
) {
273 setmode(obuf
[i
].c_mode
);
274 lastmode
= obuf
[i
].c_mode
;
276 if (obuf
[i
].c_char
== '\0') {
282 outc(obuf
[i
].c_char
);
284 if (lastmode
!= NORMAL
) {
287 if (must_overstrike
&& hadmodes
)
290 if (iflag
&& hadmodes
)
292 (void)fflush(stdout
);
299 * For terminals that can overstrike, overstrike underlines and bolds.
300 * We don't do anything with halfline ups and downs, or Greek.
306 register char *cp
= lbuf
;
309 /* Set up overstrike buffer */
310 for (i
=0; i
<maxcol
; i
++)
311 switch (obuf
[i
].c_mode
) {
320 *cp
++ = obuf
[i
].c_char
;
325 for (*cp
=' '; *cp
==' '; cp
--)
327 for (cp
=lbuf
; *cp
; cp
++)
331 for (cp
=lbuf
; *cp
; cp
++)
332 putchar(*cp
=='_' ? ' ' : *cp
);
334 for (cp
=lbuf
; *cp
; cp
++)
335 putchar(*cp
=='_' ? ' ' : *cp
);
343 register char *cp
= lbuf
;
345 for (i
=0; i
<maxcol
; i
++)
346 switch (obuf
[i
].c_mode
) {
347 case NORMAL
: *cp
++ = ' '; break;
348 case ALTSET
: *cp
++ = 'g'; break;
349 case SUPERSC
: *cp
++ = '^'; break;
350 case SUBSC
: *cp
++ = 'v'; break;
351 case UNDERL
: *cp
++ = '_'; break;
352 case BOLD
: *cp
++ = '!'; break;
353 default: *cp
++ = 'X'; break;
355 for (*cp
=' '; *cp
==' '; cp
--)
357 for (cp
=lbuf
; *cp
; cp
++)
365 bzero((char *)obuf
, sizeof (obuf
)); /* depends on NORMAL == 0 */
373 register oldcol
, oldmax
;
393 char *getenv(), *tigetstr();
395 CURS_UP
= tigetstr("cuu1");
396 CURS_RIGHT
= tigetstr("cuf1");
397 CURS_LEFT
= tigetstr("cub1");
398 if (CURS_LEFT
== NULL
)
401 ENTER_STANDOUT
= tigetstr("smso");
402 EXIT_STANDOUT
= tigetstr("rmso");
403 ENTER_UNDERLINE
= tigetstr("smul");
404 EXIT_UNDERLINE
= tigetstr("rmul");
405 ENTER_DIM
= tigetstr("dim");
406 ENTER_BOLD
= tigetstr("bold");
407 ENTER_REVERSE
= tigetstr("rev");
408 EXIT_ATTRIBUTES
= tigetstr("sgr0");
410 if (!ENTER_BOLD
&& ENTER_REVERSE
)
411 ENTER_BOLD
= ENTER_REVERSE
;
412 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
413 ENTER_BOLD
= ENTER_STANDOUT
;
414 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
415 ENTER_UNDERLINE
= ENTER_STANDOUT
;
416 EXIT_UNDERLINE
= EXIT_STANDOUT
;
418 if (!ENTER_DIM
&& ENTER_STANDOUT
)
419 ENTER_DIM
= ENTER_STANDOUT
;
420 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
421 ENTER_REVERSE
= ENTER_STANDOUT
;
422 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
423 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
426 * Note that we use REVERSE for the alternate character set,
427 * not the as/ae capabilities. This is because we are modelling
428 * the model 37 teletype (since that's what nroff outputs) and
429 * the typical as/ae is more of a graphics set, not the greek
430 * letters the 37 has.
433 UNDER_CHAR
= tigetstr("uc");
434 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
437 static int curmode
= 0;
442 if (must_use_uc
&& (curmode
&UNDERL
)) {
448 void setmode(int newmode
)
451 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
459 PRINT(EXIT_UNDERLINE
);
462 /* This includes standout */
463 PRINT(EXIT_ATTRIBUTES
);
468 PRINT(ENTER_REVERSE
);
472 * This only works on a few terminals.
473 * It should be fixed.
475 PRINT(ENTER_UNDERLINE
);
482 PRINT(ENTER_UNDERLINE
);
489 * We should have some provision here for multiple modes
490 * on at once. This will have to come later.
492 PRINT(ENTER_STANDOUT
);