Use err(3), add usage() and prototypes.

Move setmode() to setnewmode() because of a conflict with setmode(2 or 3).
This commit is contained in:
Philippe Charnier 1997-08-20 11:01:59 +00:00
parent 3a19895e49
commit 575580b202
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=28454
2 changed files with 54 additions and 26 deletions

View File

@ -56,7 +56,7 @@ If the terminal is incapable of underlining, but is capable of
a standout mode then that is used instead.
If the terminal can overstrike,
or handles underlining automatically,
.Nm ul
.Nm
degenerates to
.Xr cat 1 .
If the terminal cannot underline, underlining is ignored.

View File

@ -32,16 +32,25 @@
*/
#ifndef lint
static char copyright[] =
static const char copyright[] =
"@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)ul.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termcap.h>
#include <unistd.h>
#define IESC '\033'
#define SO '\016'
@ -75,20 +84,30 @@ int halfpos;
int upln;
int iflag;
int outchar();
static void usage __P((void));
void setnewmode __P((int));
void initcap __P((void));
void reverse __P((void));
int outchar __P((int));
void fwd __P((void));
void initbuf __P((void));
void iattr __P((void));
void overstrike __P((void));
void flushln __P((void));
void filter __P((FILE *));
void outc __P((int));
#define PRINT(s) if (s == NULL) /* void */; else tputs(s, 1, outchar)
void
main(argc, argv)
int argc;
char **argv;
{
extern int optind;
extern char *optarg;
int c;
char *termtype;
FILE *f;
char termcap[1024];
char *getenv(), *strcpy();
termtype = getenv("TERM");
if (termtype == NULL || (argv[0][0] == 'c' && !isatty(1)))
@ -103,12 +122,8 @@ main(argc, argv)
case 'i':
iflag = 1;
break;
default:
fprintf(stderr,
"usage: %s [ -i ] [ -tTerm ] file...\n",
argv[0]);
exit(1);
usage();
}
switch(tgetent(termcap, termtype)) {
@ -117,7 +132,7 @@ main(argc, argv)
break;
default:
fprintf(stderr,"trouble reading termcap");
warnx("trouble reading termcap");
/* fall through to ... */
case 0:
@ -134,15 +149,22 @@ main(argc, argv)
filter(stdin);
else for (; optind<argc; optind++) {
f = fopen(argv[optind],"r");
if (f == NULL) {
perror(argv[optind]);
exit(1);
} else
if (f == NULL)
err(1, "%s", argv[optind]);
else
filter(f);
}
exit(0);
}
static void
usage()
{
fprintf(stderr, "usage: ul [-i] [-t terminal] file...\n");
exit(1);
}
void
filter(f)
FILE *f;
{
@ -207,10 +229,7 @@ filter(f)
continue;
default:
fprintf(stderr,
"Unknown escape sequence in input: %o, %o\n",
IESC, c);
exit(1);
errx(1, "unknown escape sequence in input: %o, %o", IESC, c);
}
continue;
@ -256,6 +275,7 @@ filter(f)
flushln();
}
void
flushln()
{
register lastmode;
@ -266,7 +286,7 @@ flushln()
for (i=0; i<maxcol; i++) {
if (obuf[i].c_mode != lastmode) {
hadmodes++;
setmode(obuf[i].c_mode);
setnewmode(obuf[i].c_mode);
lastmode = obuf[i].c_mode;
}
if (obuf[i].c_char == '\0') {
@ -278,7 +298,7 @@ flushln()
outc(obuf[i].c_char);
}
if (lastmode != NORMAL) {
setmode(0);
setnewmode(0);
}
if (must_overstrike && hadmodes)
overstrike();
@ -295,6 +315,7 @@ flushln()
* For terminals that can overstrike, overstrike underlines and bolds.
* We don't do anything with halfline ups and downs, or Greek.
*/
void
overstrike()
{
register int i;
@ -332,6 +353,7 @@ overstrike()
}
}
void
iattr()
{
register int i;
@ -355,6 +377,7 @@ iattr()
putchar('\n');
}
void
initbuf()
{
@ -364,6 +387,7 @@ initbuf()
mode &= ALTSET;
}
void
fwd()
{
register oldcol, oldmax;
@ -375,6 +399,7 @@ fwd()
maxcol = oldmax;
}
void
reverse()
{
upln++;
@ -384,11 +409,11 @@ reverse()
upln++;
}
void
initcap()
{
static char tcapbuf[512];
char *bp = tcapbuf;
char *getenv(), *tgetstr();
/* This nonsense attempts to work with both old and new termcap */
CURS_UP = tgetstr("up", &bp);
@ -437,14 +462,16 @@ initcap()
must_use_uc = (UNDER_CHAR && !ENTER_UNDERLINE);
}
int
outchar(c)
int c;
{
putchar(c & 0177);
return(putchar(c & 0177));
}
static int curmode = 0;
void
outc(c)
int c;
{
@ -455,12 +482,13 @@ outc(c)
}
}
setmode(newmode)
void
setnewmode(newmode)
int newmode;
{
if (!iflag) {
if (curmode != NORMAL && newmode != NORMAL)
setmode(NORMAL);
setnewmode(NORMAL);
switch (newmode) {
case NORMAL:
switch(curmode) {