2 * SPDX-License-Identifier: BSD-3-Clause
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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)ul.c 8.1 (Berkeley) 6/6/93";
42 static const char rcsid[] =
65 #define ALTSET 001 /* Reverse */
66 #define SUPERSC 002 /* Dim */
67 #define SUBSC 004 /* Dim | Ul */
68 #define UNDERL 010 /* Ul */
69 #define BOLD 020 /* Bold */
71 static int must_use_uc, must_overstrike;
73 *CURS_UP, *CURS_RIGHT, *CURS_LEFT,
74 *ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
75 *ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
80 int c_width; /* width or -1 if multi-column char. filler */
83 static struct CHAR sobuf[MAXBUF]; /* static output buffer */
84 static struct CHAR *obuf = sobuf;
85 static int buflen = MAXBUF;
86 static int col, maxcol;
92 static void usage(void);
93 static void setnewmode(int);
94 static void initcap(void);
95 static void reverse(void);
96 static int outchar(int);
97 static void fwd(void);
98 static void initbuf(void);
99 static void iattr(void);
100 static void overstrike(void);
101 static void flushln(void);
102 static void filter(FILE *);
103 static 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][0] == 'c' && !isatty(1)))
120 while ((c = getopt(argc, argv, "it:T:")) != -1)
123 case 'T': /* for nroff compatibility */
133 switch (tgetent(termcap, termtype)) {
137 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]);
167 fprintf(stderr, "usage: ul [-i] [-t terminal] [file ...]\n");
180 while ((c = getwc(f)) != WEOF) {
186 obuf = realloc(obuf, sizeof(*obuf) * 2 * buflen);
191 memcpy(obuf, sobuf, sizeof(*obuf) * buflen);
194 bzero((char *)(obuf + buflen), sizeof(*obuf) * buflen);
222 switch (c = getwc(f)) {
228 } else if (halfpos > 0) {
241 } else if (halfpos < 0) {
255 errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
260 if (obuf[col].c_char || obuf[col].c_width < 0) {
261 while (col > 0 && obuf[col].c_width < 0)
263 w = obuf[col].c_width;
264 for (i = 0; i < w; i++)
265 obuf[col++].c_mode |= UNDERL | mode;
270 obuf[col].c_char = '_';
271 obuf[col].c_width = 1;
289 if ((w = wcwidth(c)) <= 0) /* non printing */
291 if (obuf[col].c_char == '\0') {
292 obuf[col].c_char = c;
293 for (i = 0; i < w; i++)
294 obuf[col + i].c_mode = mode;
295 obuf[col].c_width = w;
296 for (i = 1; i < w; i++)
297 obuf[col + i].c_width = -1;
298 } else if (obuf[col].c_char == '_') {
299 obuf[col].c_char = c;
300 for (i = 0; i < w; i++)
301 obuf[col + i].c_mode |= UNDERL|mode;
302 obuf[col].c_width = w;
303 for (i = 1; i < w; i++)
304 obuf[col + i].c_width = -1;
305 } else if ((wint_t)obuf[col].c_char == c) {
306 for (i = 0; i < w; i++)
307 obuf[col + i].c_mode |= BOLD|mode;
309 w = obuf[col].c_width;
310 for (i = 0; i < w; i++)
311 obuf[col + i].c_mode = mode;
333 for (i = 0; i < maxcol; i++) {
334 if (obuf[i].c_mode != lastmode) {
336 setnewmode(obuf[i].c_mode);
337 lastmode = obuf[i].c_mode;
339 if (obuf[i].c_char == '\0') {
345 outc(obuf[i].c_char, obuf[i].c_width);
346 if (obuf[i].c_width > 1)
347 i += obuf[i].c_width - 1;
349 if (lastmode != NORMAL) {
352 if (must_overstrike && hadmodes)
355 if (iflag && hadmodes)
357 (void)fflush(stdout);
364 * For terminals that can overstrike, overstrike underlines and bolds.
365 * We don't do anything with halfline ups and downs, or Greek.
375 /* Set up overstrike buffer */
376 for (i=0; i<maxcol; i++)
377 switch (obuf[i].c_mode) {
386 *cp++ = obuf[i].c_char;
387 if (obuf[i].c_width > 1)
388 i += obuf[i].c_width - 1;
393 for (*cp=' '; *cp==' '; cp--)
395 for (cp=lbuf; *cp; cp++)
399 for (cp=lbuf; *cp; cp++)
400 putwchar(*cp=='_' ? ' ' : *cp);
402 for (cp=lbuf; *cp; cp++)
403 putwchar(*cp=='_' ? ' ' : *cp);
414 for (i=0; i<maxcol; i++)
415 switch (obuf[i].c_mode) {
416 case NORMAL: *cp++ = ' '; break;
417 case ALTSET: *cp++ = 'g'; break;
418 case SUPERSC: *cp++ = '^'; break;
419 case SUBSC: *cp++ = 'v'; break;
420 case UNDERL: *cp++ = '_'; break;
421 case BOLD: *cp++ = '!'; break;
422 default: *cp++ = 'X'; break;
424 for (*cp=' '; *cp==' '; cp--)
426 for (cp=lbuf; *cp; cp++)
435 bzero((char *)obuf, buflen * sizeof(*obuf)); /* depends on NORMAL == 0 */
466 static char tcapbuf[512];
469 /* This nonsense attempts to work with both old and new termcap */
470 CURS_UP = tgetstr("up", &bp);
471 CURS_RIGHT = tgetstr("ri", &bp);
472 if (CURS_RIGHT == NULL)
473 CURS_RIGHT = tgetstr("nd", &bp);
474 CURS_LEFT = tgetstr("le", &bp);
475 if (CURS_LEFT == NULL)
476 CURS_LEFT = tgetstr("bc", &bp);
477 if (CURS_LEFT == NULL && tgetflag("bs"))
480 ENTER_STANDOUT = tgetstr("so", &bp);
481 EXIT_STANDOUT = tgetstr("se", &bp);
482 ENTER_UNDERLINE = tgetstr("us", &bp);
483 EXIT_UNDERLINE = tgetstr("ue", &bp);
484 ENTER_DIM = tgetstr("mh", &bp);
485 ENTER_BOLD = tgetstr("md", &bp);
486 ENTER_REVERSE = tgetstr("mr", &bp);
487 EXIT_ATTRIBUTES = tgetstr("me", &bp);
489 if (!ENTER_BOLD && ENTER_REVERSE)
490 ENTER_BOLD = ENTER_REVERSE;
491 if (!ENTER_BOLD && ENTER_STANDOUT)
492 ENTER_BOLD = ENTER_STANDOUT;
493 if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
494 ENTER_UNDERLINE = ENTER_STANDOUT;
495 EXIT_UNDERLINE = EXIT_STANDOUT;
497 if (!ENTER_DIM && ENTER_STANDOUT)
498 ENTER_DIM = ENTER_STANDOUT;
499 if (!ENTER_REVERSE && ENTER_STANDOUT)
500 ENTER_REVERSE = ENTER_STANDOUT;
501 if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
502 EXIT_ATTRIBUTES = EXIT_STANDOUT;
505 * Note that we use REVERSE for the alternate character set,
506 * not the as/ae capabilities. This is because we are modelling
507 * the model 37 teletype (since that's what nroff outputs) and
508 * the typical as/ae is more of a graphics set, not the greek
509 * letters the 37 has.
512 UNDER_CHAR = tgetstr("uc", &bp);
513 must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
519 return (putwchar(c) != WEOF ? c : EOF);
522 static int curmode = 0;
525 outc(wint_t c, int width)
530 if (must_use_uc && (curmode&UNDERL)) {
531 for (i = 0; i < width; i++)
533 for (i = 0; i < width; i++)
539 setnewmode(int newmode)
542 if (curmode != NORMAL && newmode != NORMAL)
550 PRINT(EXIT_UNDERLINE);
553 /* This includes standout */
554 PRINT(EXIT_ATTRIBUTES);
559 PRINT(ENTER_REVERSE);
563 * This only works on a few terminals.
564 * It should be fixed.
566 PRINT(ENTER_UNDERLINE);
573 PRINT(ENTER_UNDERLINE);
580 * We should have some provision here for multiple modes
581 * on at once. This will have to come later.
583 PRINT(ENTER_STANDOUT);