NetBSD/usr.bin/ul/ul.c

548 lines
11 KiB
C
Raw Normal View History

2000-05-28 14:27:13 +04:00
/* $NetBSD: ul.c,v 1.10 2000/05/28 10:27:13 blymn Exp $ */
1993-03-21 12:45:37 +03:00
/*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* 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.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)ul.c 8.1 (Berkeley) 6/6/93";
#endif
2000-05-28 14:27:13 +04:00
__RCSID("$NetBSD: ul.c,v 1.10 2000/05/28 10:27:13 blymn Exp $");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termcap.h>
#include <unistd.h>
1993-03-21 12:45:37 +03: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 */
2000-05-28 14:27:13 +04:00
struct tinfo *info;
1993-03-21 12:45:37 +03:00
int must_use_uc, must_overstrike;
char *CURS_UP, *CURS_RIGHT, *CURS_LEFT,
*ENTER_STANDOUT, *EXIT_STANDOUT, *ENTER_UNDERLINE, *EXIT_UNDERLINE,
*ENTER_DIM, *ENTER_BOLD, *ENTER_REVERSE, *UNDER_CHAR, *EXIT_ATTRIBUTES;
struct CHAR {
char c_mode;
char c_char;
} ;
struct CHAR obuf[MAXBUF];
int col, maxcol;
int mode;
int halfpos;
int upln;
int iflag;
int main __P((int, char **));
void filter __P((FILE *));
void flushln __P((void));
void fwd __P((void));
void iattr __P((void));
void initbuf __P((void));
void initcap __P((void));
void outc __P((int));
int outchar __P((int));
void overstrike __P((void));
void reverse __P((void));
void setulmode __P((int));
1993-03-21 12:45:37 +03:00
#define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
int
1993-03-21 12:45:37 +03:00
main(argc, argv)
int argc;
char **argv;
{
int c;
char *termtype;
FILE *f;
termtype = getenv("TERM");
if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
termtype = "lpr";
while ((c=getopt(argc, argv, "it:T:")) != -1)
1993-03-21 12:45:37 +03:00
switch(c) {
case 't':
case 'T': /* for nroff compatibility */
termtype = optarg;
break;
case 'i':
iflag = 1;
break;
default:
fprintf(stderr,
"usage: %s [ -i ] [ -tTerm ] file...\n",
argv[0]);
exit(1);
}
2000-05-28 14:27:13 +04:00
switch(t_getent(&info, termtype)) {
1993-03-21 12:45:37 +03:00
case 1:
break;
default:
fprintf(stderr,"trouble reading termcap");
/* fall through to ... */
case 0:
/* No such terminal type - assume dumb */
2000-05-28 14:27:13 +04:00
if (t_setinfo(&info, "dumb:os:col#80:cr=^M:sf=^J:am:") < 0) {
fprintf(stderr, "t_setinfo failed, cannot continue\n");
exit(1);
}
1993-03-21 12:45:37 +03:00
break;
}
initcap();
2000-05-28 14:27:13 +04:00
if ( (t_getflag(info, "os") && ENTER_BOLD==NULL ) ||
(t_getflag(info, "ul") && ENTER_UNDERLINE==NULL
&& UNDER_CHAR==NULL))
1993-03-21 12:45:37 +03:00
must_overstrike = 1;
initbuf();
if (optind == argc)
filter(stdin);
else for (; optind<argc; optind++) {
f = fopen(argv[optind],"r");
if (f == NULL) {
perror(argv[optind]);
exit(1);
}
filter(f);
fclose(f);
1993-03-21 12:45:37 +03:00
}
exit(0);
}
void
1993-03-21 12:45:37 +03:00
filter(f)
FILE *f;
{
int c;
1993-03-21 12:45:37 +03:00
while ((c = getc(f)) != EOF) switch(c) {
case '\b':
if (col > 0)
col--;
continue;
case '\t':
col = (col+8) & ~07;
if (col > maxcol)
maxcol = col;
continue;
case '\r':
col = 0;
continue;
case SO:
mode |= ALTSET;
continue;
case SI:
mode &= ~ALTSET;
continue;
case IESC:
switch (c = getc(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:
fprintf(stderr,
"Unknown escape sequence in input: %o, %o\n",
IESC, c);
exit(1);
}
continue;
case '_':
if (obuf[col].c_char)
obuf[col].c_mode |= UNDERL | mode;
else
obuf[col].c_char = '_';
case ' ':
col++;
if (col > maxcol)
maxcol = col;
continue;
case '\n':
flushln();
continue;
case '\f':
flushln();
putchar('\f');
continue;
default:
if (c < ' ') /* non printing */
continue;
if (obuf[col].c_char == '\0') {
obuf[col].c_char = c;
obuf[col].c_mode = mode;
} else if (obuf[col].c_char == '_') {
obuf[col].c_char = c;
obuf[col].c_mode |= UNDERL|mode;
} else if (obuf[col].c_char == c)
obuf[col].c_mode |= BOLD|mode;
else
obuf[col].c_mode = mode;
col++;
if (col > maxcol)
maxcol = col;
continue;
}
if (maxcol)
flushln();
}
void
1993-03-21 12:45:37 +03:00
flushln()
{
int lastmode;
int i;
1993-03-21 12:45:37 +03:00
int hadmodes = 0;
lastmode = NORMAL;
for (i=0; i<maxcol; i++) {
if (obuf[i].c_mode != lastmode) {
hadmodes++;
setulmode(obuf[i].c_mode);
1993-03-21 12:45:37 +03:00
lastmode = obuf[i].c_mode;
}
if (obuf[i].c_char == '\0') {
1998-12-20 02:21:24 +03:00
if (upln) {
1993-03-21 12:45:37 +03:00
PRINT(CURS_RIGHT);
1998-12-20 02:21:24 +03:00
}
else {
1993-03-21 12:45:37 +03:00
outc(' ');
1998-12-20 02:21:24 +03:00
}
1993-03-21 12:45:37 +03:00
} else
outc(obuf[i].c_char);
}
if (lastmode != NORMAL) {
setulmode(0);
1993-03-21 12:45:37 +03:00
}
if (must_overstrike && hadmodes)
overstrike();
putchar('\n');
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.
*/
void
1993-03-21 12:45:37 +03:00
overstrike()
{
int i;
1993-03-21 12:45:37 +03:00
char lbuf[256];
char *cp = lbuf;
1993-03-21 12:45:37 +03: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;
hadbold=1;
break;
}
putchar('\r');
for (*cp=' '; *cp==' '; cp--)
*cp = 0;
for (cp=lbuf; *cp; cp++)
putchar(*cp);
if (hadbold) {
putchar('\r');
for (cp=lbuf; *cp; cp++)
putchar(*cp=='_' ? ' ' : *cp);
putchar('\r');
for (cp=lbuf; *cp; cp++)
putchar(*cp=='_' ? ' ' : *cp);
}
}
void
1993-03-21 12:45:37 +03:00
iattr()
{
int i;
1993-03-21 12:45:37 +03:00
char lbuf[256];
char *cp = lbuf;
1993-03-21 12:45:37 +03: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++)
putchar(*cp);
putchar('\n');
}
void
1993-03-21 12:45:37 +03:00
initbuf()
{
memset((char *)obuf, 0, sizeof (obuf)); /* depends on NORMAL == 0 */
1993-03-21 12:45:37 +03:00
col = 0;
maxcol = 0;
mode &= ALTSET;
}
void
1993-03-21 12:45:37 +03:00
fwd()
{
int oldcol, oldmax;
1993-03-21 12:45:37 +03:00
oldcol = col;
oldmax = maxcol;
flushln();
col = oldcol;
maxcol = oldmax;
}
void
1993-03-21 12:45:37 +03:00
reverse()
{
upln++;
fwd();
PRINT(CURS_UP);
PRINT(CURS_UP);
upln++;
}
void
1993-03-21 12:45:37 +03:00
initcap()
{
2000-05-28 14:27:13 +04:00
static char *tcapbuf=NULL;
char *bp;
1993-03-21 12:45:37 +03:00
/* This nonsense attempts to work with both old and new termcap */
2000-05-28 14:27:13 +04:00
CURS_UP = t_agetstr(info, "up", &tcapbuf, &bp);
CURS_RIGHT = t_agetstr(info, "ri", &tcapbuf, &bp);
1993-03-21 12:45:37 +03:00
if (CURS_RIGHT == NULL)
2000-05-28 14:27:13 +04:00
CURS_RIGHT = t_agetstr(info, "nd", &tcapbuf, &bp);
CURS_LEFT = t_agetstr(info, "le", &tcapbuf, &bp);
1993-03-21 12:45:37 +03:00
if (CURS_LEFT == NULL)
2000-05-28 14:27:13 +04:00
CURS_LEFT = t_agetstr(info, "bc", &tcapbuf, &bp);
if (CURS_LEFT == NULL && t_getflag(info, "bs"))
1993-03-21 12:45:37 +03:00
CURS_LEFT = "\b";
2000-05-28 14:27:13 +04:00
ENTER_STANDOUT = t_agetstr(info, "so", &tcapbuf, &bp);
EXIT_STANDOUT = t_agetstr(info, "se", &tcapbuf, &bp);
ENTER_UNDERLINE = t_agetstr(info, "us", &tcapbuf, &bp);
EXIT_UNDERLINE = t_agetstr(info, "ue", &tcapbuf, &bp);
ENTER_DIM = t_agetstr(info, "mh", &tcapbuf, &bp);
ENTER_BOLD = t_agetstr(info, "md", &tcapbuf, &bp);
ENTER_REVERSE = t_agetstr(info, "mr", &tcapbuf, &bp);
EXIT_ATTRIBUTES = t_agetstr(info, "me", &tcapbuf, &bp);
1993-03-21 12:45:37 +03:00
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;
/*
* 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.
*/
2000-05-28 14:27:13 +04:00
UNDER_CHAR = t_agetstr(info, "uc", &tcapbuf, &bp);
1993-03-21 12:45:37 +03:00
must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
}
int
1993-03-21 12:45:37 +03:00
outchar(c)
int c;
{
return (putchar(c & 0177));
1993-03-21 12:45:37 +03:00
}
static int curmode = 0;
void
1993-03-21 12:45:37 +03:00
outc(c)
int c;
{
putchar(c);
if (must_use_uc && (curmode&UNDERL)) {
PRINT(CURS_LEFT);
PRINT(UNDER_CHAR);
}
}
void
setulmode(newmode)
1993-03-21 12:45:37 +03:00
int newmode;
{
if (!iflag) {
if (curmode != NORMAL && newmode != NORMAL)
setulmode(NORMAL);
1993-03-21 12:45:37 +03: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;
}