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. 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)
121 case 'T': /* for nroff compatibility */
131 switch (tgetent(termcap, termtype)) {
135 warnx("trouble reading termcap");
138 /* No such terminal type - assume dumb */
139 (void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
143 if ((tgetflag("os") && ENTER_BOLD == NULL ) ||
144 (tgetflag("ul") && ENTER_UNDERLINE == NULL && UNDER_CHAR == NULL))
149 else for (; optind<argc; optind++) {
150 f = fopen(argv[optind],"r");
152 err(1, "%s", argv[optind]);
165 fprintf(stderr, "usage: ul [-i] [-t terminal] [file ...]\n");
178 while ((c = getwc(f)) != WEOF) {
184 obuf = realloc(obuf, sizeof(*obuf) * 2 * buflen);
189 memcpy(obuf, sobuf, sizeof(*obuf) * buflen);
192 bzero((char *)(obuf + buflen), sizeof(*obuf) * buflen);
220 switch (c = getwc(f)) {
226 } else if (halfpos > 0) {
239 } else if (halfpos < 0) {
253 errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
258 if (obuf[col].c_char || obuf[col].c_width < 0) {
259 while (col > 0 && obuf[col].c_width < 0)
261 w = obuf[col].c_width;
262 for (i = 0; i < w; i++)
263 obuf[col++].c_mode |= UNDERL | mode;
268 obuf[col].c_char = '_';
269 obuf[col].c_width = 1;
287 if ((w = wcwidth(c)) <= 0) /* non printing */
289 if (obuf[col].c_char == '\0') {
290 obuf[col].c_char = c;
291 for (i = 0; i < w; i++)
292 obuf[col + i].c_mode = mode;
293 obuf[col].c_width = w;
294 for (i = 1; i < w; i++)
295 obuf[col + i].c_width = -1;
296 } else if (obuf[col].c_char == '_') {
297 obuf[col].c_char = c;
298 for (i = 0; i < w; i++)
299 obuf[col + i].c_mode |= UNDERL|mode;
300 obuf[col].c_width = w;
301 for (i = 1; i < w; i++)
302 obuf[col + i].c_width = -1;
303 } else if ((wint_t)obuf[col].c_char == c) {
304 for (i = 0; i < w; i++)
305 obuf[col + i].c_mode |= BOLD|mode;
307 w = obuf[col].c_width;
308 for (i = 0; i < w; i++)
309 obuf[col + i].c_mode = mode;
331 for (i = 0; i < maxcol; i++) {
332 if (obuf[i].c_mode != lastmode) {
334 setnewmode(obuf[i].c_mode);
335 lastmode = obuf[i].c_mode;
337 if (obuf[i].c_char == '\0') {
343 outc(obuf[i].c_char, obuf[i].c_width);
344 if (obuf[i].c_width > 1)
345 i += obuf[i].c_width - 1;
347 if (lastmode != NORMAL) {
350 if (must_overstrike && hadmodes)
353 if (iflag && hadmodes)
355 (void)fflush(stdout);
362 * For terminals that can overstrike, overstrike underlines and bolds.
363 * We don't do anything with halfline ups and downs, or Greek.
373 /* Set up overstrike buffer */
374 for (i=0; i<maxcol; i++)
375 switch (obuf[i].c_mode) {
384 *cp++ = obuf[i].c_char;
385 if (obuf[i].c_width > 1)
386 i += obuf[i].c_width - 1;
391 for (*cp=' '; *cp==' '; cp--)
393 for (cp=lbuf; *cp; cp++)
397 for (cp=lbuf; *cp; cp++)
398 putwchar(*cp=='_' ? ' ' : *cp);
400 for (cp=lbuf; *cp; cp++)
401 putwchar(*cp=='_' ? ' ' : *cp);
412 for (i=0; i<maxcol; i++)
413 switch (obuf[i].c_mode) {
414 case NORMAL: *cp++ = ' '; break;
415 case ALTSET: *cp++ = 'g'; break;
416 case SUPERSC: *cp++ = '^'; break;
417 case SUBSC: *cp++ = 'v'; break;
418 case UNDERL: *cp++ = '_'; break;
419 case BOLD: *cp++ = '!'; break;
420 default: *cp++ = 'X'; break;
422 for (*cp=' '; *cp==' '; cp--)
424 for (cp=lbuf; *cp; cp++)
433 bzero((char *)obuf, buflen * sizeof(*obuf)); /* depends on NORMAL == 0 */
464 static char tcapbuf[512];
467 /* This nonsense attempts to work with both old and new termcap */
468 CURS_UP = tgetstr("up", &bp);
469 CURS_RIGHT = tgetstr("ri", &bp);
470 if (CURS_RIGHT == NULL)
471 CURS_RIGHT = tgetstr("nd", &bp);
472 CURS_LEFT = tgetstr("le", &bp);
473 if (CURS_LEFT == NULL)
474 CURS_LEFT = tgetstr("bc", &bp);
475 if (CURS_LEFT == NULL && tgetflag("bs"))
478 ENTER_STANDOUT = tgetstr("so", &bp);
479 EXIT_STANDOUT = tgetstr("se", &bp);
480 ENTER_UNDERLINE = tgetstr("us", &bp);
481 EXIT_UNDERLINE = tgetstr("ue", &bp);
482 ENTER_DIM = tgetstr("mh", &bp);
483 ENTER_BOLD = tgetstr("md", &bp);
484 ENTER_REVERSE = tgetstr("mr", &bp);
485 EXIT_ATTRIBUTES = tgetstr("me", &bp);
487 if (!ENTER_BOLD && ENTER_REVERSE)
488 ENTER_BOLD = ENTER_REVERSE;
489 if (!ENTER_BOLD && ENTER_STANDOUT)
490 ENTER_BOLD = ENTER_STANDOUT;
491 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
492 ENTER_UNDERLINE = ENTER_STANDOUT;
493 EXIT_UNDERLINE = EXIT_STANDOUT;
495 if (!ENTER_DIM && ENTER_STANDOUT)
496 ENTER_DIM = ENTER_STANDOUT;
497 if (!ENTER_REVERSE && ENTER_STANDOUT)
498 ENTER_REVERSE = ENTER_STANDOUT;
499 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
500 EXIT_ATTRIBUTES = EXIT_STANDOUT;
503 * Note that we use REVERSE for the alternate character set,
504 * not the as/ae capabilities. This is because we are modelling
505 * the model 37 teletype (since that's what nroff outputs) and
506 * the typical as/ae is more of a graphics set, not the greek
507 * letters the 37 has.
510 UNDER_CHAR = tgetstr("uc", &bp);
511 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
517 return (putwchar(c) != WEOF ? c : EOF);
520 static int curmode = 0;
523 outc(wint_t c, int width)
528 if (must_use_uc && (curmode&UNDERL)) {
529 for (i = 0; i < width; i++)
531 for (i = 0; i < width; i++)
537 setnewmode(int newmode)
540 if (curmode != NORMAL && newmode != NORMAL)
548 PRINT(EXIT_UNDERLINE);
551 /* This includes standout */
552 PRINT(EXIT_ATTRIBUTES);
557 PRINT(ENTER_REVERSE);
561 * This only works on a few terminals.
562 * It should be fixed.
564 PRINT(ENTER_UNDERLINE);
571 PRINT(ENTER_UNDERLINE);
578 * We should have some provision here for multiple modes
579 * on at once. This will have to come later.
581 PRINT(ENTER_STANDOUT);