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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)ul.c 8.1 (Berkeley) 6/6/93";
40 static const char rcsid[] =
63 #define ALTSET 001 /* Reverse */
64 #define SUPERSC 002 /* Dim */
65 #define SUBSC 004 /* Dim | Ul */
66 #define UNDERL 010 /* Ul */
67 #define BOLD 020 /* Bold */
69 int must_use_uc, must_overstrike;
71 *CURS_UP, *CURS_RIGHT, *CURS_LEFT,
72 *ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
73 *ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
78 int c_width; /* width or -1 if multi-column char. filler */
81 struct CHAR obuf[MAXBUF];
88 static void usage(void);
96 void overstrike(void);
99 void outc(wint_t, int);
101 #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
104 main(int argc, char **argv)
107 const char *termtype;
111 setlocale(LC_ALL, "");
113 termtype = getenv("TERM");
114 if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
116 while ((c=getopt(argc, argv, "it:T:")) != -1)
120 case 'T': /* for nroff compatibility */
130 switch(tgetent(termcap, termtype)) {
136 warnx("trouble reading termcap");
140 /* No such terminal type - assume dumb */
141 (void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
145 if ( (tgetflag("os") && ENTER_BOLD==NULL ) ||
146 (tgetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
151 else for (; optind<argc; optind++) {
152 f = fopen(argv[optind],"r");
154 err(1, "%s", argv[optind]);
164 fprintf(stderr, "usage: ul [-i] [-t terminal] [file ...]\n");
174 while ((c = getwc(f)) != WEOF && col < MAXBUF) switch(c) {
200 switch (c = getwc(f)) {
206 } else if (halfpos > 0) {
219 } else if (halfpos < 0) {
233 errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
238 if (obuf[col].c_char || obuf[col].c_width < 0) {
239 while (col > 0 && obuf[col].c_width < 0)
241 w = obuf[col].c_width;
242 for (i = 0; i < w; i++)
243 obuf[col++].c_mode |= UNDERL | mode;
248 obuf[col].c_char = '_';
249 obuf[col].c_width = 1;
267 if ((w = wcwidth(c)) <= 0) /* non printing */
269 if (obuf[col].c_char == '\0') {
270 obuf[col].c_char = c;
271 for (i = 0; i < w; i++)
272 obuf[col + i].c_mode = mode;
273 obuf[col].c_width = w;
274 for (i = 1; i < w; i++)
275 obuf[col + i].c_width = -1;
276 } else if (obuf[col].c_char == '_') {
277 obuf[col].c_char = c;
278 for (i = 0; i < w; i++)
279 obuf[col + i].c_mode |= UNDERL|mode;
280 obuf[col].c_width = w;
281 for (i = 1; i < w; i++)
282 obuf[col + i].c_width = -1;
283 } else if (obuf[col].c_char == c) {
284 for (i = 0; i < w; i++)
285 obuf[col + i].c_mode |= BOLD|mode;
287 w = obuf[col].c_width;
288 for (i = 0; i < w; i++)
289 obuf[col + i].c_mode = mode;
310 for (i=0; i<maxcol; i++) {
311 if (obuf[i].c_mode != lastmode) {
313 setnewmode(obuf[i].c_mode);
314 lastmode = obuf[i].c_mode;
316 if (obuf[i].c_char == '\0') {
322 outc(obuf[i].c_char, obuf[i].c_width);
323 if (obuf[i].c_width > 1)
324 i += obuf[i].c_width - 1;
326 if (lastmode != NORMAL) {
329 if (must_overstrike && hadmodes)
332 if (iflag && hadmodes)
334 (void)fflush(stdout);
341 * For terminals that can overstrike, overstrike underlines and bolds.
342 * We don't do anything with halfline ups and downs, or Greek.
352 /* Set up overstrike buffer */
353 for (i=0; i<maxcol; i++)
354 switch (obuf[i].c_mode) {
363 *cp++ = obuf[i].c_char;
364 if (obuf[i].c_width > 1)
365 i += obuf[i].c_width - 1;
370 for (*cp=' '; *cp==' '; cp--)
372 for (cp=lbuf; *cp; cp++)
376 for (cp=lbuf; *cp; cp++)
377 putwchar(*cp=='_' ? ' ' : *cp);
379 for (cp=lbuf; *cp; cp++)
380 putwchar(*cp=='_' ? ' ' : *cp);
391 for (i=0; i<maxcol; i++)
392 switch (obuf[i].c_mode) {
393 case NORMAL: *cp++ = ' '; break;
394 case ALTSET: *cp++ = 'g'; break;
395 case SUPERSC: *cp++ = '^'; break;
396 case SUBSC: *cp++ = 'v'; break;
397 case UNDERL: *cp++ = '_'; break;
398 case BOLD: *cp++ = '!'; break;
399 default: *cp++ = 'X'; break;
401 for (*cp=' '; *cp==' '; cp--)
403 for (cp=lbuf; *cp; cp++)
412 bzero((char *)obuf, sizeof (obuf)); /* depends on NORMAL == 0 */
443 static char tcapbuf[512];
446 /* This nonsense attempts to work with both old and new termcap */
447 CURS_UP = tgetstr("up", &bp);
448 CURS_RIGHT = tgetstr("ri", &bp);
449 if (CURS_RIGHT == NULL)
450 CURS_RIGHT = tgetstr("nd", &bp);
451 CURS_LEFT = tgetstr("le", &bp);
452 if (CURS_LEFT == NULL)
453 CURS_LEFT = tgetstr("bc", &bp);
454 if (CURS_LEFT == NULL && tgetflag("bs"))
457 ENTER_STANDOUT = tgetstr("so", &bp);
458 EXIT_STANDOUT = tgetstr("se", &bp);
459 ENTER_UNDERLINE = tgetstr("us", &bp);
460 EXIT_UNDERLINE = tgetstr("ue", &bp);
461 ENTER_DIM = tgetstr("mh", &bp);
462 ENTER_BOLD = tgetstr("md", &bp);
463 ENTER_REVERSE = tgetstr("mr", &bp);
464 EXIT_ATTRIBUTES = tgetstr("me", &bp);
466 if (!ENTER_BOLD && ENTER_REVERSE)
467 ENTER_BOLD = ENTER_REVERSE;
468 if (!ENTER_BOLD && ENTER_STANDOUT)
469 ENTER_BOLD = ENTER_STANDOUT;
470 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
471 ENTER_UNDERLINE = ENTER_STANDOUT;
472 EXIT_UNDERLINE = EXIT_STANDOUT;
474 if (!ENTER_DIM && ENTER_STANDOUT)
475 ENTER_DIM = ENTER_STANDOUT;
476 if (!ENTER_REVERSE && ENTER_STANDOUT)
477 ENTER_REVERSE = ENTER_STANDOUT;
478 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
479 EXIT_ATTRIBUTES = EXIT_STANDOUT;
482 * Note that we use REVERSE for the alternate character set,
483 * not the as/ae capabilities. This is because we are modelling
484 * the model 37 teletype (since that's what nroff outputs) and
485 * the typical as/ae is more of a graphics set, not the greek
486 * letters the 37 has.
489 UNDER_CHAR = tgetstr("uc", &bp);
490 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
496 return (putwchar(c) != WEOF ? c : EOF);
499 static int curmode = 0;
502 outc(wint_t c, int width)
507 if (must_use_uc && (curmode&UNDERL)) {
508 for (i = 0; i < width; i++)
510 for (i = 0; i < width; i++)
516 setnewmode(int newmode)
519 if (curmode != NORMAL && newmode != NORMAL)
527 PRINT(EXIT_UNDERLINE);
530 /* This includes standout */
531 PRINT(EXIT_ATTRIBUTES);
536 PRINT(ENTER_REVERSE);
540 * This only works on a few terminals.
541 * It should be fixed.
543 PRINT(ENTER_UNDERLINE);
550 PRINT(ENTER_UNDERLINE);
557 * We should have some provision here for multiple modes
558 * on at once. This will have to come later.
560 PRINT(ENTER_STANDOUT);