]>
git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/ul/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 static const char copyright
[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)ul.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid
[] =
45 "$FreeBSD: src/usr.bin/ul/ul.c,v 1.14 2005/05/21 09:55:09 ru Exp $";
67 #define ALTSET 001 /* Reverse */
68 #define SUPERSC 002 /* Dim */
69 #define SUBSC 004 /* Dim | Ul */
70 #define UNDERL 010 /* Ul */
71 #define BOLD 020 /* Bold */
73 int must_use_uc
, must_overstrike
;
75 *CURS_UP
, *CURS_RIGHT
, *CURS_LEFT
,
76 *ENTER_STANDOUT
, *EXIT_STANDOUT
, *ENTER_UNDERLINE
, *EXIT_UNDERLINE
,
77 *ENTER_DIM
, *ENTER_BOLD
, *ENTER_REVERSE
, *UNDER_CHAR
, *EXIT_ATTRIBUTES
;
82 int c_width
; /* width or -1 if multi-column char. filler */
85 struct CHAR obuf
[MAXBUF
];
92 static void usage(void);
100 void overstrike(void);
103 void outc(wint_t, int);
105 #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
108 main(int argc
, char **argv
)
111 const char *termtype
;
115 setlocale(LC_ALL
, "");
117 termtype
= getenv("TERM");
118 if (termtype
== NULL
|| (argv
[0] && argv
[0][0] == 'c' && !isatty(1)))
120 while ((c
=getopt(argc
, argv
, "it:T:")) != -1)
124 case 'T': /* for nroff compatibility */
134 switch(tgetent(termcap
, termtype
)) {
140 warnx("trouble reading termcap");
144 /* No such terminal type - assume dumb */
145 (void)strcpy(termcap
, "dumb:os:col#80:cr=^M:sf=^J:am:");
149 if ( (tgetflag("os") && ENTER_BOLD
==NULL
) ||
150 (tgetflag("ul") && ENTER_UNDERLINE
==NULL
&& UNDER_CHAR
==NULL
))
155 else for (; optind
<argc
&& argv
[optind
]; optind
++) {
156 f
= fopen(argv
[optind
],"r");
158 err(1, "%s", argv
[optind
]);
168 fprintf(stderr
, "usage: ul [-i] [-t terminal] [file ...]\n");
178 while ((c
= getwc(f
)) != WEOF
&& col
< MAXBUF
) switch(c
) {
204 switch (c
= getwc(f
)) {
210 } else if (halfpos
> 0) {
223 } else if (halfpos
< 0) {
237 errx(1, "unknown escape sequence in input: %o, %o", IESC
, c
);
242 if (obuf
[col
].c_char
|| obuf
[col
].c_width
< 0) {
243 while (col
> 0 && obuf
[col
].c_width
< 0)
245 w
= obuf
[col
].c_width
;
246 for (i
= 0; i
< w
; i
++)
247 obuf
[col
++].c_mode
|= UNDERL
| mode
;
252 obuf
[col
].c_char
= '_';
253 obuf
[col
].c_width
= 1;
271 if ((w
= wcwidth(c
)) <= 0) /* non printing */
273 if (obuf
[col
].c_char
== '\0') {
274 obuf
[col
].c_char
= c
;
275 for (i
= 0; i
< w
; i
++)
276 obuf
[col
+ i
].c_mode
= mode
;
277 obuf
[col
].c_width
= w
;
278 for (i
= 1; i
< w
; i
++)
279 obuf
[col
+ i
].c_width
= -1;
280 } else if (obuf
[col
].c_char
== '_') {
281 obuf
[col
].c_char
= c
;
282 for (i
= 0; i
< w
; i
++)
283 obuf
[col
+ i
].c_mode
|= UNDERL
|mode
;
284 obuf
[col
].c_width
= w
;
285 for (i
= 1; i
< w
; i
++)
286 obuf
[col
+ i
].c_width
= -1;
287 } else if (obuf
[col
].c_char
== c
) {
288 for (i
= 0; i
< w
; i
++)
289 obuf
[col
+ i
].c_mode
|= BOLD
|mode
;
291 w
= obuf
[col
].c_width
;
292 for (i
= 0; i
< w
; i
++)
293 obuf
[col
+ i
].c_mode
= mode
;
314 for (i
=0; i
<maxcol
; i
++) {
315 if (obuf
[i
].c_mode
!= lastmode
) {
317 setnewmode(obuf
[i
].c_mode
);
318 lastmode
= obuf
[i
].c_mode
;
320 if (obuf
[i
].c_char
== '\0') {
326 outc(obuf
[i
].c_char
, obuf
[i
].c_width
);
327 if (obuf
[i
].c_width
> 1)
328 i
+= obuf
[i
].c_width
- 1;
330 if (lastmode
!= NORMAL
) {
333 if (must_overstrike
&& hadmodes
)
336 if (iflag
&& hadmodes
)
338 (void)fflush(stdout
);
345 * For terminals that can overstrike, overstrike underlines and bolds.
346 * We don't do anything with halfline ups and downs, or Greek.
356 /* Set up overstrike buffer */
357 for (i
=0; i
<maxcol
; i
++)
358 switch (obuf
[i
].c_mode
) {
367 *cp
++ = obuf
[i
].c_char
;
368 if (obuf
[i
].c_width
> 1)
369 i
+= obuf
[i
].c_width
- 1;
374 for (*cp
=' '; *cp
==' '; cp
--)
376 for (cp
=lbuf
; *cp
; cp
++)
380 for (cp
=lbuf
; *cp
; cp
++)
381 putwchar(*cp
=='_' ? ' ' : *cp
);
383 for (cp
=lbuf
; *cp
; cp
++)
384 putwchar(*cp
=='_' ? ' ' : *cp
);
395 for (i
=0; i
<maxcol
; i
++)
396 switch (obuf
[i
].c_mode
) {
397 case NORMAL
: *cp
++ = ' '; break;
398 case ALTSET
: *cp
++ = 'g'; break;
399 case SUPERSC
: *cp
++ = '^'; break;
400 case SUBSC
: *cp
++ = 'v'; break;
401 case UNDERL
: *cp
++ = '_'; break;
402 case BOLD
: *cp
++ = '!'; break;
403 default: *cp
++ = 'X'; break;
405 for (*cp
=' '; *cp
==' '; cp
--)
407 for (cp
=lbuf
; *cp
; cp
++)
416 bzero((char *)obuf
, sizeof (obuf
)); /* depends on NORMAL == 0 */
447 static char tcapbuf
[512];
450 /* This nonsense attempts to work with both old and new termcap */
451 CURS_UP
= tgetstr("up", &bp
);
452 CURS_RIGHT
= tgetstr("ri", &bp
);
453 if (CURS_RIGHT
== NULL
)
454 CURS_RIGHT
= tgetstr("nd", &bp
);
455 CURS_LEFT
= tgetstr("le", &bp
);
456 if (CURS_LEFT
== NULL
)
457 CURS_LEFT
= tgetstr("bc", &bp
);
458 if (CURS_LEFT
== NULL
&& tgetflag("bs"))
461 ENTER_STANDOUT
= tgetstr("so", &bp
);
462 EXIT_STANDOUT
= tgetstr("se", &bp
);
463 ENTER_UNDERLINE
= tgetstr("us", &bp
);
464 EXIT_UNDERLINE
= tgetstr("ue", &bp
);
465 ENTER_DIM
= tgetstr("mh", &bp
);
466 ENTER_BOLD
= tgetstr("md", &bp
);
467 ENTER_REVERSE
= tgetstr("mr", &bp
);
468 EXIT_ATTRIBUTES
= tgetstr("me", &bp
);
470 if (!ENTER_BOLD
&& ENTER_REVERSE
)
471 ENTER_BOLD
= ENTER_REVERSE
;
472 if (!ENTER_BOLD
&& ENTER_STANDOUT
)
473 ENTER_BOLD
= ENTER_STANDOUT
;
474 if (!ENTER_UNDERLINE
&& ENTER_STANDOUT
) {
475 ENTER_UNDERLINE
= ENTER_STANDOUT
;
476 EXIT_UNDERLINE
= EXIT_STANDOUT
;
478 if (!ENTER_DIM
&& ENTER_STANDOUT
)
479 ENTER_DIM
= ENTER_STANDOUT
;
480 if (!ENTER_REVERSE
&& ENTER_STANDOUT
)
481 ENTER_REVERSE
= ENTER_STANDOUT
;
482 if (!EXIT_ATTRIBUTES
&& EXIT_STANDOUT
)
483 EXIT_ATTRIBUTES
= EXIT_STANDOUT
;
486 * Note that we use REVERSE for the alternate character set,
487 * not the as/ae capabilities. This is because we are modelling
488 * the model 37 teletype (since that's what nroff outputs) and
489 * the typical as/ae is more of a graphics set, not the greek
490 * letters the 37 has.
493 UNDER_CHAR
= tgetstr("uc", &bp
);
494 must_use_uc
= (UNDER_CHAR
&& !ENTER_UNDERLINE
);
500 return (putwchar(c
) != WEOF
? c
: EOF
);
503 static int curmode
= 0;
506 outc(wint_t c
, int width
)
511 if (must_use_uc
&& (curmode
&UNDERL
)) {
512 for (i
= 0; i
< width
; i
++)
514 for (i
= 0; i
< width
; i
++)
520 setnewmode(int newmode
)
523 if (curmode
!= NORMAL
&& newmode
!= NORMAL
)
531 PRINT(EXIT_UNDERLINE
);
534 /* This includes standout */
535 PRINT(EXIT_ATTRIBUTES
);
540 PRINT(ENTER_REVERSE
);
544 * This only works on a few terminals.
545 * It should be fixed.
547 PRINT(ENTER_UNDERLINE
);
554 PRINT(ENTER_UNDERLINE
);
561 * We should have some provision here for multiple modes
562 * on at once. This will have to come later.
564 PRINT(ENTER_STANDOUT
);