1 /* $NetBSD: ul.c,v 1.16 2012/03/20 20:34:59 matt 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.16 2012/03/20 20:34:59 matt 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 */
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)
101 main(int argc
, char **argv
)
104 const char *termtype
;
107 termtype
= getenv("TERM");
108 if (termtype
== NULL
|| (argv
[0][0] == 'c' && !isatty(1)))
110 while ((c
=getopt(argc
, argv
, "it:T:")) != -1)
114 case 'T': /* for nroff compatibility */
123 "usage: %s [ -i ] [ -tTerm ] file...\n",
128 setupterm(termtype
, 0, NULL
);
129 if ((over_strike
&& enter_bold_mode
== NULL
) ||
130 (transparent_underline
&& enter_underline_mode
== NULL
&&
131 underline_char
== NULL
))
135 else for (; optind
<argc
; optind
++) {
136 f
= fopen(argv
[optind
],"r");
138 perror(argv
[optind
]);
153 while ((c
= getc(f
)) != EOF
) switch(c
) {
179 switch (c
= getc(f
)) {
185 } else if (halfpos
> 0) {
198 } else if (halfpos
< 0) {
213 "Unknown escape sequence in input: %o, %o\n",
220 if (obuf
[col
].c_char
)
221 obuf
[col
].c_mode
|= UNDERL
| mode
;
223 obuf
[col
].c_char
= '_';
240 if (c
< ' ') /* non printing */
242 if (obuf
[col
].c_char
== '\0') {
243 obuf
[col
].c_char
= c
;
244 obuf
[col
].c_mode
= mode
;
245 } else if (obuf
[col
].c_char
== '_') {
246 obuf
[col
].c_char
= c
;
247 obuf
[col
].c_mode
|= UNDERL
|mode
;
248 } else if (obuf
[col
].c_char
== c
)
249 obuf
[col
].c_mode
|= BOLD
|mode
;
251 obuf
[col
].c_mode
= mode
;
269 for (i
=0; i
<maxcol
; i
++) {
270 if (obuf
[i
].c_mode
!= lastmode
) {
272 setulmode(obuf
[i
].c_mode
);
273 lastmode
= obuf
[i
].c_mode
;
275 if (obuf
[i
].c_char
== '\0') {
283 outc(obuf
[i
].c_char
);
285 if (lastmode
!= NORMAL
) {
288 if (must_overstrike
&& hadmodes
)
291 if (iflag
&& hadmodes
)
293 (void)fflush(stdout
);
300 * For terminals that can overstrike, overstrike underlines and bolds.
301 * We don't do anything with halfline ups and downs, or Greek.
311 /* Set up overstrike buffer */
312 for (i
=0; i
<maxcol
; i
++)
313 switch (obuf
[i
].c_mode
) {
322 *cp
++ = obuf
[i
].c_char
;
327 for (*cp
=' '; *cp
==' '; cp
--)
329 for (cp
=lbuf
; *cp
; cp
++)
333 for (cp
=lbuf
; *cp
; cp
++)
334 putchar(*cp
=='_' ? ' ' : *cp
);
336 for (cp
=lbuf
; *cp
; cp
++)
337 putchar(*cp
=='_' ? ' ' : *cp
);
348 for (i
=0; i
<maxcol
; i
++)
349 switch (obuf
[i
].c_mode
) {
350 case NORMAL
: *cp
++ = ' '; break;
351 case ALTSET
: *cp
++ = 'g'; break;
352 case SUPERSC
: *cp
++ = '^'; break;
353 case SUBSC
: *cp
++ = 'v'; break;
354 case UNDERL
: *cp
++ = '_'; break;
355 case BOLD
: *cp
++ = '!'; break;
356 default: *cp
++ = 'X'; break;
358 for (*cp
=' '; *cp
==' '; cp
--)
360 for (cp
=lbuf
; *cp
; cp
++)
369 memset((char *)obuf
, 0, sizeof (obuf
)); /* depends on NORMAL == 0 */
400 return (putchar(c
& 0177));
403 static int curmode
= 0;
409 if (underline_char
&& !enter_underline_mode
&& (curmode
& UNDERL
)) {
414 PRINT(underline_char
);
419 setulmode(int newmode
)
422 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
430 if (enter_underline_mode
)
431 PRINT(exit_underline_mode
);
433 PRINT(exit_standout_mode
);
436 /* This includes standout */
437 if (exit_attribute_mode
)
438 PRINT(exit_attribute_mode
);
440 PRINT(exit_standout_mode
);
445 if (enter_reverse_mode
)
446 PRINT(enter_reverse_mode
);
448 PRINT(enter_standout_mode
);
452 * This only works on a few terminals.
453 * It should be fixed.
455 PRINT(enter_underline_mode
);
456 PRINT(enter_dim_mode
);
460 PRINT(enter_dim_mode
);
462 PRINT(enter_standout_mode
);
465 if (enter_underline_mode
)
466 PRINT(enter_underline_mode
);
468 PRINT(enter_standout_mode
);
472 PRINT(enter_bold_mode
);
474 PRINT(enter_reverse_mode
);
478 * We should have some provision here for multiple modes
479 * on at once. This will have to come later.
481 PRINT(enter_standout_mode
);