/* $NetBSD: ul.c,v 1.13 2008/07/21 14:19:27 lukem Exp $ */ /* * 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. * 3. 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 #ifndef lint __COPYRIGHT("@(#) Copyright (c) 1980, 1993\ The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)ul.c 8.1 (Berkeley) 6/6/93"; #endif __RCSID("$NetBSD: ul.c,v 1.13 2008/07/21 14:19:27 lukem Exp $"); #endif /* not lint */ #include #include #include #include #include #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 */ struct tinfo *info; 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)); #define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar) int 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) 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); } switch(t_getent(&info, termtype)) { case 1: break; default: fprintf(stderr,"trouble reading termcap"); /* fall through to ... */ case 0: /* No such terminal type - assume dumb */ if (t_setinfo(&info, "dumb:os:col#80:cr=^M:sf=^J:am:") < 0) { fprintf(stderr, "t_setinfo failed, cannot continue\n"); exit(1); } break; } initcap(); if ( (t_getflag(info, "os") && ENTER_BOLD==NULL ) || (t_getflag(info, "ul") && ENTER_UNDERLINE==NULL && UNDER_CHAR==NULL)) must_overstrike = 1; initbuf(); if (optind == argc) filter(stdin); else for (; optind 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 flushln() { int lastmode; int i; int hadmodes = 0; lastmode = NORMAL; for (i=0; i