freebsd-dev/usr.bin/ul/ul.c

589 lines
12 KiB
C
Raw Normal View History

/*-
* SPDX-License-Identifier: BSD-3-Clause
*
1994-05-27 12:33:43 +00:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
2016-05-09 20:55:35 +00:00
* 3. Neither the name of the University nor the names of its contributors
1994-05-27 12:33:43 +00:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static const char copyright[] =
1994-05-27 12:33:43 +00:00
"@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
#if 0
1994-05-27 12:33:43 +00:00
static char sccsid[] = "@(#)ul.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
1999-08-28 01:08:13 +00:00
"$FreeBSD$";
1994-05-27 12:33:43 +00:00
#endif /* not lint */
#include <err.h>
#include <locale.h>
1994-05-27 12:33:43 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termcap.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
1994-05-27 12:33:43 +00:00
#define IESC '\033'
#define SO '\016'
#define SI '\017'
#define HFWD '9'
#define HREV '8'
#define FREV '7'
#define MAXBUF 512
#define NORMAL 000
#define ALTSET 001 /* Reverse */
#define SUPERSC 002 /* Dim */
#define SUBSC 004 /* Dim | Ul */
#define UNDERL 010 /* Ul */
#define BOLD 020 /* Bold */
2011-11-06 08:17:53 +00:00
static int must_use_uc, must_overstrike;
static const char
*CURS_UP, *CURS_RIGHT, *CURS_LEFT,
1994-05-27 12:33:43 +00:00
*ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
*ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
struct CHAR {
char c_mode;
wchar_t c_char;
int c_width; /* width or -1 if multi-column char. filler */
1994-05-27 12:33:43 +00:00
} ;
static struct CHAR sobuf[MAXBUF]; /* static output buffer */
static struct CHAR *obuf = sobuf;
static int buflen = MAXBUF;
2011-11-06 08:17:53 +00:00
static int col, maxcol;
static int mode;
static int halfpos;
static int upln;
static int iflag;
1994-05-27 12:33:43 +00:00
2002-03-22 01:42:45 +00:00
static void usage(void);
2011-11-06 08:17:53 +00:00
static void setnewmode(int);
static void initcap(void);
static void reverse(void);
static int outchar(int);
static void fwd(void);
static void initbuf(void);
static void iattr(void);
static void overstrike(void);
static void flushln(void);
static void filter(FILE *);
static void outc(wint_t, int);
1994-05-27 12:33:43 +00:00
#define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
1997-08-26 11:08:24 +00:00
int
main(int argc, char **argv)
1994-05-27 12:33:43 +00:00
{
int c;
const char *termtype;
1994-05-27 12:33:43 +00:00
FILE *f;
char termcap[1024];
setlocale(LC_ALL, "");
1994-05-27 12:33:43 +00:00
termtype = getenv("TERM");
if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
termtype = "lpr";
while ((c = getopt(argc, argv, "it:T:")) != -1)
switch (c) {
1994-05-27 12:33:43 +00:00
case 't':
case 'T': /* for nroff compatibility */
termtype = optarg;
1994-05-27 12:33:43 +00:00
break;
case 'i':
iflag = 1;
break;
default:
usage();
1994-05-27 12:33:43 +00:00
}
switch (tgetent(termcap, termtype)) {
1994-05-27 12:33:43 +00:00
case 1:
break;
default:
warnx("trouble reading termcap");
/* FALLTHROUGH */
1994-05-27 12:33:43 +00:00
case 0:
/* No such terminal type - assume dumb */
(void)strcpy(termcap, "dumb:os:col#80:cr=^M:sf=^J:am:");
break;
}
initcap();
if ((tgetflag("os") && ENTER_BOLD == NULL ) ||
(tgetflag("ul") && ENTER_UNDERLINE == NULL && UNDER_CHAR == NULL))
must_overstrike = 1;
1994-05-27 12:33:43 +00:00
initbuf();
if (optind == argc)
filter(stdin);
else for (; optind<argc; optind++) {
f = fopen(argv[optind],"r");
if (f == NULL)
err(1, "%s", argv[optind]);
else
1994-05-27 12:33:43 +00:00
filter(f);
}
if (obuf != sobuf) {
free(obuf);
}
1994-05-27 12:33:43 +00:00
exit(0);
}
static void
usage(void)
{
fprintf(stderr, "usage: ul [-i] [-t terminal] [file ...]\n");
exit(1);
}
2011-11-06 08:17:53 +00:00
static void
filter(FILE *f)
1994-05-27 12:33:43 +00:00
{
wint_t c;
int i, w;
int copy;
copy = 0;
while ((c = getwc(f)) != WEOF) {
if (col == buflen) {
if (obuf == sobuf) {
obuf = NULL;
copy = 1;
}
obuf = realloc(obuf, sizeof(*obuf) * 2 * buflen);
if (obuf == NULL) {
obuf = sobuf;
break;
} else if (copy) {
memcpy(obuf, sobuf, sizeof(*obuf) * buflen);
copy = 0;
1994-05-27 12:33:43 +00:00
}
bzero((char *)(obuf + buflen), sizeof(*obuf) * buflen);
buflen *= 2;
}
switch(c) {
case '\b':
if (col > 0)
col--;
1994-05-27 12:33:43 +00:00
continue;
case '\t':
col = (col+8) & ~07;
if (col > maxcol)
maxcol = col;
1994-05-27 12:33:43 +00:00
continue;
case '\r':
col = 0;
1994-05-27 12:33:43 +00:00
continue;
case SO:
mode |= ALTSET;
continue;
1994-05-27 12:33:43 +00:00
case SI:
mode &= ~ALTSET;
continue;
case IESC:
switch (c = getwc(f)) {
case HREV:
if (halfpos == 0) {
mode |= SUPERSC;
halfpos--;
} else if (halfpos > 0) {
mode &= ~SUBSC;
halfpos--;
} else {
halfpos = 0;
reverse();
}
continue;
case HFWD:
if (halfpos == 0) {
mode |= SUBSC;
halfpos++;
} else if (halfpos < 0) {
mode &= ~SUPERSC;
halfpos++;
} else {
halfpos = 0;
fwd();
}
continue;
case FREV:
reverse();
continue;
default:
errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
}
continue;
case '_':
if (obuf[col].c_char || obuf[col].c_width < 0) {
while (col > 0 && obuf[col].c_width < 0)
col--;
w = obuf[col].c_width;
for (i = 0; i < w; i++)
obuf[col++].c_mode |= UNDERL | mode;
if (col > maxcol)
maxcol = col;
continue;
}
obuf[col].c_char = '_';
obuf[col].c_width = 1;
/* FALLTHROUGH */
case ' ':
col++;
if (col > maxcol)
maxcol = col;
continue;
1994-05-27 12:33:43 +00:00
case '\n':
flushln();
continue;
1994-05-27 12:33:43 +00:00
case '\f':
flushln();
putwchar('\f');
continue;
1994-05-27 12:33:43 +00:00
default:
if ((w = wcwidth(c)) <= 0) /* non printing */
continue;
if (obuf[col].c_char == '\0') {
obuf[col].c_char = c;
for (i = 0; i < w; i++)
obuf[col + i].c_mode = mode;
obuf[col].c_width = w;
for (i = 1; i < w; i++)
obuf[col + i].c_width = -1;
} else if (obuf[col].c_char == '_') {
obuf[col].c_char = c;
for (i = 0; i < w; i++)
obuf[col + i].c_mode |= UNDERL|mode;
obuf[col].c_width = w;
for (i = 1; i < w; i++)
obuf[col + i].c_width = -1;
} else if ((wint_t)obuf[col].c_char == c) {
for (i = 0; i < w; i++)
obuf[col + i].c_mode |= BOLD|mode;
} else {
w = obuf[col].c_width;
for (i = 0; i < w; i++)
obuf[col + i].c_mode = mode;
}
col += w;
if (col > maxcol)
maxcol = col;
1994-05-27 12:33:43 +00:00
continue;
}
1994-05-27 12:33:43 +00:00
}
2004-07-30 10:58:06 +00:00
if (ferror(f))
err(1, NULL);
1994-05-27 12:33:43 +00:00
if (maxcol)
flushln();
}
2011-11-06 08:17:53 +00:00
static void
flushln(void)
1994-05-27 12:33:43 +00:00
{
int lastmode;
int i;
1994-05-27 12:33:43 +00:00
int hadmodes = 0;
lastmode = NORMAL;
for (i = 0; i < maxcol; i++) {
1994-05-27 12:33:43 +00:00
if (obuf[i].c_mode != lastmode) {
hadmodes++;
setnewmode(obuf[i].c_mode);
1994-05-27 12:33:43 +00:00
lastmode = obuf[i].c_mode;
}
if (obuf[i].c_char == '\0') {
if (upln)
PRINT(CURS_RIGHT);
else
outc(' ', 1);
1994-05-27 12:33:43 +00:00
} else
outc(obuf[i].c_char, obuf[i].c_width);
if (obuf[i].c_width > 1)
i += obuf[i].c_width - 1;
1994-05-27 12:33:43 +00:00
}
if (lastmode != NORMAL) {
setnewmode(0);
1994-05-27 12:33:43 +00:00
}
if (must_overstrike && hadmodes)
overstrike();
putwchar('\n');
1994-05-27 12:33:43 +00:00
if (iflag && hadmodes)
iattr();
(void)fflush(stdout);
if (upln)
upln--;
initbuf();
}
/*
* For terminals that can overstrike, overstrike underlines and bolds.
* We don't do anything with halfline ups and downs, or Greek.
*/
2011-11-06 08:17:53 +00:00
static void
overstrike(void)
1994-05-27 12:33:43 +00:00
{
int i;
wchar_t lbuf[256];
wchar_t *cp = lbuf;
1994-05-27 12:33:43 +00:00
int hadbold=0;
/* Set up overstrike buffer */
for (i=0; i<maxcol; i++)
switch (obuf[i].c_mode) {
case NORMAL:
default:
*cp++ = ' ';
break;
case UNDERL:
*cp++ = '_';
break;
case BOLD:
*cp++ = obuf[i].c_char;
if (obuf[i].c_width > 1)
i += obuf[i].c_width - 1;
1994-05-27 12:33:43 +00:00
hadbold=1;
break;
}
putwchar('\r');
1994-05-27 12:33:43 +00:00
for (*cp=' '; *cp==' '; cp--)
*cp = 0;
for (cp=lbuf; *cp; cp++)
putwchar(*cp);
1994-05-27 12:33:43 +00:00
if (hadbold) {
putwchar('\r');
1994-05-27 12:33:43 +00:00
for (cp=lbuf; *cp; cp++)
putwchar(*cp=='_' ? ' ' : *cp);
putwchar('\r');
1994-05-27 12:33:43 +00:00
for (cp=lbuf; *cp; cp++)
putwchar(*cp=='_' ? ' ' : *cp);
1994-05-27 12:33:43 +00:00
}
}
2011-11-06 08:17:53 +00:00
static void
iattr(void)
1994-05-27 12:33:43 +00:00
{
int i;
wchar_t lbuf[256];
wchar_t *cp = lbuf;
1994-05-27 12:33:43 +00:00
for (i=0; i<maxcol; i++)
switch (obuf[i].c_mode) {
case NORMAL: *cp++ = ' '; break;
case ALTSET: *cp++ = 'g'; break;
case SUPERSC: *cp++ = '^'; break;
case SUBSC: *cp++ = 'v'; break;
case UNDERL: *cp++ = '_'; break;
case BOLD: *cp++ = '!'; break;
default: *cp++ = 'X'; break;
}
for (*cp=' '; *cp==' '; cp--)
*cp = 0;
for (cp=lbuf; *cp; cp++)
putwchar(*cp);
putwchar('\n');
1994-05-27 12:33:43 +00:00
}
2011-11-06 08:17:53 +00:00
static void
initbuf(void)
1994-05-27 12:33:43 +00:00
{
bzero((char *)obuf, buflen * sizeof(*obuf)); /* depends on NORMAL == 0 */
1994-05-27 12:33:43 +00:00
col = 0;
maxcol = 0;
mode &= ALTSET;
}
2011-11-06 08:17:53 +00:00
static void
fwd(void)
1994-05-27 12:33:43 +00:00
{
int oldcol, oldmax;
1994-05-27 12:33:43 +00:00
oldcol = col;
oldmax = maxcol;
flushln();
col = oldcol;
maxcol = oldmax;
}
2011-11-06 08:17:53 +00:00
static void
reverse(void)
1994-05-27 12:33:43 +00:00
{
upln++;
fwd();
PRINT(CURS_UP);
PRINT(CURS_UP);
upln++;
}
2011-11-06 08:17:53 +00:00
static void
initcap(void)
1994-05-27 12:33:43 +00:00
{
static char tcapbuf[512];
char *bp = tcapbuf;
/* This nonsense attempts to work with both old and new termcap */
CURS_UP = tgetstr("up", &bp);
CURS_RIGHT = tgetstr("ri", &bp);
if (CURS_RIGHT == NULL)
CURS_RIGHT = tgetstr("nd", &bp);
CURS_LEFT = tgetstr("le", &bp);
if (CURS_LEFT == NULL)
CURS_LEFT = tgetstr("bc", &bp);
if (CURS_LEFT == NULL && tgetflag("bs"))
CURS_LEFT = "\b";
ENTER_STANDOUT = tgetstr("so", &bp);
EXIT_STANDOUT = tgetstr("se", &bp);
ENTER_UNDERLINE = tgetstr("us", &bp);
EXIT_UNDERLINE = tgetstr("ue", &bp);
ENTER_DIM = tgetstr("mh", &bp);
ENTER_BOLD = tgetstr("md", &bp);
ENTER_REVERSE = tgetstr("mr", &bp);
EXIT_ATTRIBUTES = tgetstr("me", &bp);
if (!ENTER_BOLD && ENTER_REVERSE)
ENTER_BOLD = ENTER_REVERSE;
if (!ENTER_BOLD && ENTER_STANDOUT)
ENTER_BOLD = ENTER_STANDOUT;
if (!ENTER_UNDERLINE && ENTER_STANDOUT) {
ENTER_UNDERLINE = ENTER_STANDOUT;
EXIT_UNDERLINE = EXIT_STANDOUT;
}
if (!ENTER_DIM && ENTER_STANDOUT)
ENTER_DIM = ENTER_STANDOUT;
if (!ENTER_REVERSE && ENTER_STANDOUT)
ENTER_REVERSE = ENTER_STANDOUT;
if (!EXIT_ATTRIBUTES && EXIT_STANDOUT)
EXIT_ATTRIBUTES = EXIT_STANDOUT;
1995-05-30 06:41:30 +00:00
1994-05-27 12:33:43 +00:00
/*
* Note that we use REVERSE for the alternate character set,
* not the as/ae capabilities. This is because we are modelling
* the model 37 teletype (since that's what nroff outputs) and
* the typical as/ae is more of a graphics set, not the greek
* letters the 37 has.
*/
UNDER_CHAR = tgetstr("uc", &bp);
must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
}
2011-11-06 08:17:53 +00:00
static int
outchar(int c)
1994-05-27 12:33:43 +00:00
{
return (putwchar(c) != WEOF ? c : EOF);
1994-05-27 12:33:43 +00:00
}
static int curmode = 0;
2011-11-06 08:17:53 +00:00
static void
outc(wint_t c, int width)
1994-05-27 12:33:43 +00:00
{
int i;
putwchar(c);
1994-05-27 12:33:43 +00:00
if (must_use_uc && (curmode&UNDERL)) {
for (i = 0; i < width; i++)
PRINT(CURS_LEFT);
for (i = 0; i < width; i++)
PRINT(UNDER_CHAR);
1994-05-27 12:33:43 +00:00
}
}
2011-11-06 08:17:53 +00:00
static void
setnewmode(int newmode)
1994-05-27 12:33:43 +00:00
{
if (!iflag) {
if (curmode != NORMAL && newmode != NORMAL)
setnewmode(NORMAL);
1994-05-27 12:33:43 +00:00
switch (newmode) {
case NORMAL:
switch(curmode) {
case NORMAL:
break;
case UNDERL:
PRINT(EXIT_UNDERLINE);
break;
default:
/* This includes standout */
PRINT(EXIT_ATTRIBUTES);
break;
}
break;
case ALTSET:
PRINT(ENTER_REVERSE);
break;
case SUPERSC:
/*
* This only works on a few terminals.
* It should be fixed.
*/
PRINT(ENTER_UNDERLINE);
PRINT(ENTER_DIM);
break;
case SUBSC:
PRINT(ENTER_DIM);
break;
case UNDERL:
PRINT(ENTER_UNDERLINE);
break;
case BOLD:
PRINT(ENTER_BOLD);
break;
default:
/*
* We should have some provision here for multiple modes
* on at once. This will have to come later.
*/
PRINT(ENTER_STANDOUT);
break;
}
}
curmode = newmode;
}