1 /* $NetBSD: ul.c,v 1.13 2008/07/21 14:19:27 lukem Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)ul.c 8.1 (Berkeley) 6/6/93";
42 __RCSID("$NetBSD: ul.c,v 1.13 2008/07/21 14:19:27 lukem Exp $");
60 #define ALTSET 001 /* Reverse */
61 #define SUPERSC 002 /* Dim */
62 #define SUBSC 004 /* Dim | Ul */
63 #define UNDERL 010 /* Ul */
64 #define BOLD 020 /* Bold */
67 int must_use_uc
, must_overstrike
;
68 const char *CURS_UP
, *CURS_RIGHT
, *CURS_LEFT
,
69 *ENTER_STANDOUT
, *EXIT_STANDOUT
, *ENTER_UNDERLINE
, *EXIT_UNDERLINE
,
70 *ENTER_DIM
, *ENTER_BOLD
, *ENTER_REVERSE
, *UNDER_CHAR
, *EXIT_ATTRIBUTES
;
77 struct CHAR obuf
[MAXBUF
];
84 int main
__P((int, char **));
85 void filter
__P((FILE *));
86 void flushln
__P((void));
88 void iattr
__P((void));
89 void initbuf
__P((void));
90 void initcap
__P((void));
92 int outchar
__P((int));
93 void overstrike
__P((void));
94 void reverse
__P((void));
95 void setulmode
__P((int));
98 #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
106 const char *termtype
;
109 termtype
= getenv("TERM");
110 if (termtype
== NULL
|| (argv
[0][0] == 'c' && !isatty(1)))
112 while ((c
=getopt(argc
, argv
, "it:T:")) != -1)
116 case 'T': /* for nroff compatibility */
125 "usage: %s [ -i ] [ -tTerm ] file...\n",
130 switch(t_getent(&info
, termtype
)) {
136 fprintf(stderr
,"trouble reading termcap");
137 /* fall through to ... */
140 /* No such terminal type - assume dumb */
141 if (t_setinfo(&info
, "dumb:os:col#80:cr=^M:sf=^J:am:") < 0) {
142 fprintf(stderr
, "t_setinfo failed, cannot continue\n");
149 if ( (t_getflag(info
, "os") && ENTER_BOLD
==NULL
) ||
150 (t_getflag(info
, "ul") && ENTER_UNDERLINE
==NULL
151 && UNDER_CHAR
==NULL
))
156 else for (; optind
<argc
; optind
++) {
157 f
= fopen(argv
[optind
],"r");
159 perror(argv
[optind
]);
175 while ((c
= getc(f
)) != EOF
) switch(c
) {
201 switch (c
= getc(f
)) {
207 } else if (halfpos
> 0) {
220 } else if (halfpos
< 0) {
235 "Unknown escape sequence in input: %o, %o\n",
242 if (obuf
[col
].c_char
)
243 obuf
[col
].c_mode
|= UNDERL
| mode
;
245 obuf
[col
].c_char
= '_';
262 if (c
< ' ') /* non printing */
264 if (obuf
[col
].c_char
== '\0') {
265 obuf
[col
].c_char
= c
;
266 obuf
[col
].c_mode
= mode
;
267 } else if (obuf
[col
].c_char
== '_') {
268 obuf
[col
].c_char
= c
;
269 obuf
[col
].c_mode
|= UNDERL
|mode
;
270 } else if (obuf
[col
].c_char
== c
)
271 obuf
[col
].c_mode
|= BOLD
|mode
;
273 obuf
[col
].c_mode
= mode
;
291 for (i
=0; i
<maxcol
; i
++) {
292 if (obuf
[i
].c_mode
!= lastmode
) {
294 setulmode(obuf
[i
].c_mode
);
295 lastmode
= obuf
[i
].c_mode
;
297 if (obuf
[i
].c_char
== '\0') {
305 outc(obuf
[i
].c_char
);
307 if (lastmode
!= NORMAL
) {
310 if (must_overstrike
&& hadmodes
)
313 if (iflag
&& hadmodes
)
315 (void)fflush(stdout
);
322 * For terminals that can overstrike, overstrike underlines and bolds.
323 * We don't do anything with halfline ups and downs, or Greek.
333 /* Set up overstrike buffer */
334 for (i
=0; i
<maxcol
; i
++)
335 switch (obuf
[i
].c_mode
) {
344 *cp
++ = obuf
[i
].c_char
;
349 for (*cp
=' '; *cp
==' '; cp
--)
351 for (cp
=lbuf
; *cp
; cp
++)
355 for (cp
=lbuf
; *cp
; cp
++)
356 putchar(*cp
=='_' ? ' ' : *cp
);
358 for (cp
=lbuf
; *cp
; cp
++)
359 putchar(*cp
=='_' ? ' ' : *cp
);
370 for (i
=0; i
<maxcol
; i
++)
371 switch (obuf
[i
].c_mode
) {
372 case NORMAL
: *cp
++ = ' '; break;
373 case ALTSET
: *cp
++ = 'g'; break;
374 case SUPERSC
: *cp
++ = '^'; break;
375 case SUBSC
: *cp
++ = 'v'; break;
376 case UNDERL
: *cp
++ = '_'; break;
377 case BOLD
: *cp
++ = '!'; break;
378 default: *cp
++ = 'X'; break;
380 for (*cp
=' '; *cp
==' '; cp
--)
382 for (cp
=lbuf
; *cp
; cp
++)
391 memset((char *)obuf
, 0, sizeof (obuf
)); /* depends on NORMAL == 0 */
422 /* This nonsense attempts to work with both old and new termcap */
423 CURS_UP
= t_agetstr(info
, "up");
424 CURS_RIGHT
= t_agetstr(info
, "ri");
425 if (CURS_RIGHT
== NULL
)
426 CURS_RIGHT
= t_agetstr(info
, "nd");
427 CURS_LEFT
= t_agetstr(info
, "le");
428 if (CURS_LEFT
== NULL
)
429 CURS_LEFT
= t_agetstr(info
, "bc");
430 if (CURS_LEFT
== NULL
&& t_getflag(info
, "bs"))
433 ENTER_STANDOUT
= t_agetstr(info
, "so");
434 EXIT_STANDOUT
= t_agetstr(info
, "se");
435 ENTER_UNDERLINE
= t_agetstr(info
, "us");
436 EXIT_UNDERLINE
= t_agetstr(info
, "ue");
437 ENTER_DIM
= t_agetstr(info
, "mh");
438 ENTER_BOLD
= t_agetstr(info
, "md");
439 ENTER_REVERSE
= t_agetstr(info
, "mr");
440 EXIT_ATTRIBUTES
= t_agetstr(info
, "me");
442 if (!ENTER_BOLD
&& ENTER_REVERSE
)
443 ENTER_BOLD
= ENTER_REVERSE
;
444 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
445 ENTER_BOLD
= ENTER_STANDOUT
;
446 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
447 ENTER_UNDERLINE
= ENTER_STANDOUT
;
448 EXIT_UNDERLINE
= EXIT_STANDOUT
;
450 if (!ENTER_DIM
&& ENTER_STANDOUT
)
451 ENTER_DIM
= ENTER_STANDOUT
;
452 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
453 ENTER_REVERSE
= ENTER_STANDOUT
;
454 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
455 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
458 * Note that we use REVERSE for the alternate character set,
459 * not the as/ae capabilities. This is because we are modelling
460 * the model 37 teletype (since that's what nroff outputs) and
461 * the typical as/ae is more of a graphics set, not the greek
462 * letters the 37 has.
465 UNDER_CHAR
= t_agetstr(info
, "uc");
466 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
473 return (putchar(c
& 0177));
476 static int curmode
= 0;
483 if (must_use_uc
&& (curmode
&UNDERL
)) {
494 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
502 PRINT(EXIT_UNDERLINE
);
505 /* This includes standout */
506 PRINT(EXIT_ATTRIBUTES
);
511 PRINT(ENTER_REVERSE
);
515 * This only works on a few terminals.
516 * It should be fixed.
518 PRINT(ENTER_UNDERLINE
);
525 PRINT(ENTER_UNDERLINE
);
532 * We should have some provision here for multiple modes
533 * on at once. This will have to come later.
535 PRINT(ENTER_STANDOUT
);