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 static 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 static struct CHAR sobuf[MAXBUF]; /* static output buffer */
82 static struct CHAR *obuf = sobuf;
83 static int buflen = MAXBUF;
84 static int col, maxcol;
90 static void usage(void);
91 static void setnewmode(int);
92 static void initcap(void);
93 static void reverse(void);
94 static int outchar(int);
95 static void fwd(void);
96 static void initbuf(void);
97 static void iattr(void);
98 static void overstrike(void);
99 static void flushln(void);
100 static void filter(FILE *);
101 static void outc(wint_t, int);
103 #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
106 main(int argc, char **argv)
109 const char *termtype;
113 setlocale(LC_ALL, "");
115 termtype = getenv("TERM");
116 if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
118 while ((c=getopt(argc, argv, "it:T:")) != -1)
122 case 'T': /* for nroff compatibility */
132 switch(tgetent(termcap, termtype)) {
138 warnx("trouble reading termcap");
142 /* No such terminal type - assume dumb */
143 (void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
147 if ( (tgetflag("os") && ENTER_BOLD==NULL ) ||
148 (tgetflag("ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL))
153 else for (; optind<argc; optind++) {
154 f = fopen(argv[optind],"r");
156 err(1, "%s", argv[optind]);
169 fprintf(stderr, "usage: ul [-i] [-t terminal] [file ...]\n");
182 while ((c = getwc(f)) != WEOF) {
188 obuf = realloc(obuf, sizeof(*obuf) * 2 * buflen);
193 memcpy(obuf, sobuf, sizeof(*obuf) * buflen);
196 bzero((char *)(obuf + buflen), sizeof(*obuf) * buflen);
224 switch (c = getwc(f)) {
230 } else if (halfpos > 0) {
243 } else if (halfpos < 0) {
257 errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
262 if (obuf[col].c_char || obuf[col].c_width < 0) {
263 while (col > 0 && obuf[col].c_width < 0)
265 w = obuf[col].c_width;
266 for (i = 0; i < w; i++)
267 obuf[col++].c_mode |= UNDERL | mode;
272 obuf[col].c_char = '_';
273 obuf[col].c_width = 1;
291 if ((w = wcwidth(c)) <= 0) /* non printing */
293 if (obuf[col].c_char == '\0') {
294 obuf[col].c_char = c;
295 for (i = 0; i < w; i++)
296 obuf[col + i].c_mode = mode;
297 obuf[col].c_width = w;
298 for (i = 1; i < w; i++)
299 obuf[col + i].c_width = -1;
300 } else if (obuf[col].c_char == '_') {
301 obuf[col].c_char = c;
302 for (i = 0; i < w; i++)
303 obuf[col + i].c_mode |= UNDERL|mode;
304 obuf[col].c_width = w;
305 for (i = 1; i < w; i++)
306 obuf[col + i].c_width = -1;
307 } else if ((wint_t)obuf[col].c_char == c) {
308 for (i = 0; i < w; i++)
309 obuf[col + i].c_mode |= BOLD|mode;
311 w = obuf[col].c_width;
312 for (i = 0; i < w; i++)
313 obuf[col + i].c_mode = mode;
335 for (i=0; i<maxcol; i++) {
336 if (obuf[i].c_mode != lastmode) {
338 setnewmode(obuf[i].c_mode);
339 lastmode = obuf[i].c_mode;
341 if (obuf[i].c_char == '\0') {
347 outc(obuf[i].c_char, obuf[i].c_width);
348 if (obuf[i].c_width > 1)
349 i += obuf[i].c_width - 1;
351 if (lastmode != NORMAL) {
354 if (must_overstrike && hadmodes)
357 if (iflag && hadmodes)
359 (void)fflush(stdout);
366 * For terminals that can overstrike, overstrike underlines and bolds.
367 * We don't do anything with halfline ups and downs, or Greek.
377 /* Set up overstrike buffer */
378 for (i=0; i<maxcol; i++)
379 switch (obuf[i].c_mode) {
388 *cp++ = obuf[i].c_char;
389 if (obuf[i].c_width > 1)
390 i += obuf[i].c_width - 1;
395 for (*cp=' '; *cp==' '; cp--)
397 for (cp=lbuf; *cp; cp++)
401 for (cp=lbuf; *cp; cp++)
402 putwchar(*cp=='_' ? ' ' : *cp);
404 for (cp=lbuf; *cp; cp++)
405 putwchar(*cp=='_' ? ' ' : *cp);
416 for (i=0; i<maxcol; i++)
417 switch (obuf[i].c_mode) {
418 case NORMAL: *cp++ = ' '; break;
419 case ALTSET: *cp++ = 'g'; break;
420 case SUPERSC: *cp++ = '^'; break;
421 case SUBSC: *cp++ = 'v'; break;
422 case UNDERL: *cp++ = '_'; break;
423 case BOLD: *cp++ = '!'; break;
424 default: *cp++ = 'X'; break;
426 for (*cp=' '; *cp==' '; cp--)
428 for (cp=lbuf; *cp; cp++)
437 bzero((char *)obuf, buflen * sizeof(*obuf)); /* depends on NORMAL == 0 */
468 static char tcapbuf[512];
471 /* This nonsense attempts to work with both old and new termcap */
472 CURS_UP = tgetstr("up", &bp);
473 CURS_RIGHT = tgetstr("ri", &bp);
474 if (CURS_RIGHT == NULL)
475 CURS_RIGHT = tgetstr("nd", &bp);
476 CURS_LEFT = tgetstr("le", &bp);
477 if (CURS_LEFT == NULL)
478 CURS_LEFT = tgetstr("bc", &bp);
479 if (CURS_LEFT == NULL && tgetflag("bs"))
482 ENTER_STANDOUT = tgetstr("so", &bp);
483 EXIT_STANDOUT = tgetstr("se", &bp);
484 ENTER_UNDERLINE = tgetstr("us", &bp);
485 EXIT_UNDERLINE = tgetstr("ue", &bp);
486 ENTER_DIM = tgetstr("mh", &bp);
487 ENTER_BOLD = tgetstr("md", &bp);
488 ENTER_REVERSE = tgetstr("mr", &bp);
489 EXIT_ATTRIBUTES = tgetstr("me", &bp);
491 if (!ENTER_BOLD && ENTER_REVERSE)
492 ENTER_BOLD = ENTER_REVERSE;
493 if (!ENTER_BOLD && ENTER_STANDOUT)
494 ENTER_BOLD = ENTER_STANDOUT;
495 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
496 ENTER_UNDERLINE = ENTER_STANDOUT;
497 EXIT_UNDERLINE = EXIT_STANDOUT;
499 if (!ENTER_DIM && ENTER_STANDOUT)
500 ENTER_DIM = ENTER_STANDOUT;
501 if (!ENTER_REVERSE && ENTER_STANDOUT)
502 ENTER_REVERSE = ENTER_STANDOUT;
503 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
504 EXIT_ATTRIBUTES = EXIT_STANDOUT;
507 * Note that we use REVERSE for the alternate character set,
508 * not the as/ae capabilities. This is because we are modelling
509 * the model 37 teletype (since that's what nroff outputs) and
510 * the typical as/ae is more of a graphics set, not the greek
511 * letters the 37 has.
514 UNDER_CHAR = tgetstr("uc", &bp);
515 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
521 return (putwchar(c) != WEOF ? c : EOF);
524 static int curmode = 0;
527 outc(wint_t c, int width)
532 if (must_use_uc && (curmode&UNDERL)) {
533 for (i = 0; i < width; i++)
535 for (i = 0; i < width; i++)
541 setnewmode(int newmode)
544 if (curmode != NORMAL && newmode != NORMAL)
552 PRINT(EXIT_UNDERLINE);
555 /* This includes standout */
556 PRINT(EXIT_ATTRIBUTES);
561 PRINT(ENTER_REVERSE);
565 * This only works on a few terminals.
566 * It should be fixed.
568 PRINT(ENTER_UNDERLINE);
575 PRINT(ENTER_UNDERLINE);
582 * We should have some provision here for multiple modes
583 * on at once. This will have to come later.
585 PRINT(ENTER_STANDOUT);