2006-12-06 17:25:32 -06: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.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
/*
|
2006-12-06 17:25:41 -06:00
|
|
|
* modified by Kars de Jong <jongk@cs.utwente.nl>
|
|
|
|
* to use terminfo instead of termcap.
|
2013-01-26 10:07:51 -06:00
|
|
|
* 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
|
2006-12-06 17:25:41 -06:00
|
|
|
* added Native Language Support
|
|
|
|
* 1999-09-19 Bruno Haible <haible@clisp.cons.org>
|
|
|
|
* modified to work correctly in multi-byte locales
|
|
|
|
*/
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
#include <stdio.h>
|
2006-12-06 17:25:34 -06:00
|
|
|
#include <unistd.h> /* for getopt(), isatty() */
|
2009-08-17 00:27:39 -05:00
|
|
|
#include <string.h> /* for memset(), strcpy() */
|
2006-12-06 17:25:35 -06:00
|
|
|
#include <stdlib.h> /* for getenv() */
|
2006-12-06 17:25:37 -06:00
|
|
|
#include <limits.h> /* for INT_MAX */
|
2010-08-29 21:03:32 -05:00
|
|
|
#include <signal.h> /* for signal() */
|
2010-09-17 03:35:35 -05:00
|
|
|
#include <errno.h>
|
2011-04-30 06:06:21 -05:00
|
|
|
#include <getopt.h>
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2017-05-31 04:01:46 -05:00
|
|
|
#if defined(HAVE_NCURSESW_TERM_H)
|
|
|
|
# include <ncursesw/term.h>
|
2017-05-30 10:15:37 -05:00
|
|
|
#elif defined(HAVE_NCURSES_TERM_H)
|
|
|
|
# include <ncurses/term.h>
|
2017-05-31 04:01:46 -05:00
|
|
|
#elif defined(HAVE_TERM_H)
|
|
|
|
# include <term.h>
|
2017-05-30 10:15:37 -05:00
|
|
|
#endif
|
|
|
|
|
2010-10-22 10:25:46 -05:00
|
|
|
#include "nls.h"
|
|
|
|
#include "xalloc.h"
|
2006-12-06 17:25:41 -06:00
|
|
|
#include "widechar.h"
|
2011-01-25 15:44:52 -06:00
|
|
|
#include "c.h"
|
2012-04-04 12:39:15 -05:00
|
|
|
#include "closestream.h"
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2007-01-30 06:49:50 -06:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2011-04-30 06:06:24 -05:00
|
|
|
/* Output an ASCII character as a wide character */
|
|
|
|
static int put1wc(int c)
|
2006-12-06 17:25:41 -06:00
|
|
|
{
|
2011-04-30 06:06:24 -05:00
|
|
|
if (putwchar(c) == WEOF)
|
|
|
|
return EOF;
|
2020-04-19 00:16:22 -05:00
|
|
|
|
|
|
|
return c;
|
2006-12-06 17:25:41 -06:00
|
|
|
}
|
2020-06-29 15:21:50 -05:00
|
|
|
|
|
|
|
# define putwp(s) tputs(s, STDOUT_FILENO, put1wc)
|
2006-12-06 17:25:41 -06:00
|
|
|
#else
|
2020-06-29 15:21:50 -05:00
|
|
|
# define putwp(s) putp(s)
|
2006-12-06 17:25:41 -06:00
|
|
|
#endif
|
|
|
|
|
2006-12-06 17:25:32 -06:00
|
|
|
#define IESC '\033'
|
|
|
|
#define SO '\016'
|
|
|
|
#define SI '\017'
|
|
|
|
#define HFWD '9'
|
|
|
|
#define HREV '8'
|
|
|
|
#define FREV '7'
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
enum {
|
|
|
|
NORMAL = 0, /* Must be zero, see initbuf() */
|
|
|
|
ALTSET = 1 << 0, /* Reverse */
|
|
|
|
SUPERSC = 1 << 1, /* Dim */
|
|
|
|
SUBSC = 1 << 2, /* Dim | Ul */
|
|
|
|
UNDERL = 1 << 3, /* Ul */
|
|
|
|
BOLD = 1 << 4, /* Bold */
|
|
|
|
};
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-29 16:00:14 -05:00
|
|
|
struct term_caps {
|
|
|
|
char *curs_up;
|
|
|
|
char *curs_right;
|
|
|
|
char *curs_left;
|
|
|
|
char *enter_standout;
|
|
|
|
char *exit_standout;
|
|
|
|
char *enter_underline;
|
|
|
|
char *exit_underline;
|
|
|
|
char *enter_dim;
|
|
|
|
char *enter_bold;
|
|
|
|
char *enter_reverse;
|
|
|
|
char *under_char;
|
|
|
|
char *exit_attributes;
|
|
|
|
};
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
struct ul_char {
|
2006-12-06 17:25:41 -06:00
|
|
|
wchar_t c_char;
|
2006-12-06 17:25:44 -06:00
|
|
|
int c_width;
|
2020-06-30 16:17:01 -05:00
|
|
|
char c_mode;
|
2011-04-30 06:06:24 -05:00
|
|
|
};
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
struct ul_ctl {
|
|
|
|
int obuflen;
|
|
|
|
int col;
|
|
|
|
int maxcol;
|
|
|
|
int mode;
|
|
|
|
int halfpos;
|
|
|
|
int upln;
|
|
|
|
int curmode;
|
|
|
|
struct ul_char *obuf;
|
|
|
|
unsigned int
|
|
|
|
iflag:1,
|
|
|
|
must_use_uc:1,
|
|
|
|
must_overstrike:1;
|
|
|
|
};
|
2020-06-29 15:41:38 -05:00
|
|
|
|
2017-06-20 13:20:29 -05:00
|
|
|
static void __attribute__((__noreturn__)) usage(void)
|
2011-04-30 06:06:21 -05:00
|
|
|
{
|
2017-06-20 13:20:29 -05:00
|
|
|
FILE *out = stdout;
|
2020-06-29 15:21:50 -05:00
|
|
|
|
2013-04-05 05:43:51 -05:00
|
|
|
fputs(USAGE_HEADER, out);
|
|
|
|
fprintf(out, _(" %s [options] [<file> ...]\n"), program_invocation_short_name);
|
2011-04-30 06:06:21 -05:00
|
|
|
|
2014-12-22 15:57:17 -06:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(_("Do underlining.\n"), out);
|
|
|
|
|
|
|
|
fputs(USAGE_OPTIONS, out);
|
2013-04-05 05:43:51 -05:00
|
|
|
fputs(_(" -t, -T, --terminal TERMINAL override the TERM environment variable\n"), out);
|
|
|
|
fputs(_(" -i, --indicated underlining is indicated via a separate line\n"), out);
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
2017-06-29 08:52:16 -05:00
|
|
|
printf(USAGE_HELP_OPTIONS(30));
|
2013-04-05 05:43:51 -05:00
|
|
|
|
2017-06-29 08:52:16 -05:00
|
|
|
printf(USAGE_MAN_TAIL("ul(1)"));
|
2011-04-30 06:06:21 -05:00
|
|
|
|
2017-06-20 13:20:29 -05:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-04-30 06:06:21 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void needcol(struct ul_ctl *ctl, int acol)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->maxcol = acol;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
/* If col >= obuflen, expand obuf until obuflen > col. */
|
2020-06-30 16:17:01 -05:00
|
|
|
while (acol >= ctl->obuflen) {
|
2020-06-29 15:41:38 -05:00
|
|
|
/* Paranoid check for obuflen == INT_MAX. */
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->obuflen == INT_MAX)
|
2020-06-29 15:41:38 -05:00
|
|
|
errx(EXIT_FAILURE, _("Input line too long."));
|
2011-04-30 06:06:21 -05:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
/* Similar paranoia: double only up to INT_MAX. */
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->obuflen < (INT_MAX / 2))
|
|
|
|
ctl->obuflen *= 2;
|
2020-06-29 15:41:38 -05:00
|
|
|
else
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuflen = INT_MAX;
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
/* Now we can try to expand obuf. */
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuf = xrealloc(ctl->obuf, sizeof(struct ul_char) * ctl->obuflen);
|
2020-06-29 15:41:38 -05:00
|
|
|
}
|
|
|
|
}
|
2010-08-29 21:03:32 -05:00
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void setcol(struct ul_ctl *ctl, int newcol)
|
2020-06-29 15:41:38 -05:00
|
|
|
{
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->col = newcol;
|
2011-04-30 06:06:24 -05:00
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->col < 0)
|
|
|
|
ctl->col = 0;
|
|
|
|
else if (ctl->col > ctl->maxcol)
|
|
|
|
needcol(ctl, ctl->col);
|
2020-06-29 15:41:38 -05:00
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void initbuf(struct ul_ctl *ctl)
|
2020-06-29 15:41:38 -05:00
|
|
|
{
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->obuf == NULL) {
|
2020-06-29 15:41:38 -05:00
|
|
|
/* First time. */
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuflen = BUFSIZ;
|
|
|
|
ctl->obuf = xcalloc(ctl->obuflen, sizeof(struct ul_char));
|
2020-06-29 15:41:38 -05:00
|
|
|
} else
|
|
|
|
/* assumes NORMAL == 0 */
|
2020-06-30 16:17:01 -05:00
|
|
|
memset(ctl->obuf, 0, sizeof(struct ul_char) * ctl->maxcol);
|
2019-04-16 08:14:13 -05:00
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
setcol(ctl, 0);
|
|
|
|
ctl->maxcol = 0;
|
|
|
|
ctl->mode &= ALTSET;
|
2020-06-29 15:41:38 -05:00
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void initinfo(struct ul_ctl *ctl, struct term_caps *const tcs)
|
2020-06-29 15:41:38 -05:00
|
|
|
{
|
2020-06-29 16:00:14 -05:00
|
|
|
tcs->curs_up = tigetstr("cuu1");
|
|
|
|
tcs->curs_right = tigetstr("cuf1");
|
|
|
|
tcs->curs_left = tigetstr("cub1");
|
|
|
|
if (tcs->curs_left == NULL)
|
|
|
|
tcs->curs_left = "\b";
|
|
|
|
|
|
|
|
tcs->enter_standout = tigetstr("smso");
|
|
|
|
tcs->exit_standout = tigetstr("rmso");
|
|
|
|
tcs->enter_underline = tigetstr("smul");
|
|
|
|
tcs->exit_underline = tigetstr("rmul");
|
|
|
|
tcs->enter_dim = tigetstr("dim");
|
|
|
|
tcs->enter_bold = tigetstr("bold");
|
|
|
|
tcs->enter_reverse = tigetstr("rev");
|
|
|
|
tcs->exit_attributes = tigetstr("sgr0");
|
|
|
|
|
|
|
|
if (!tcs->enter_bold && tcs->enter_reverse)
|
|
|
|
tcs->enter_bold = tcs->enter_reverse;
|
|
|
|
|
|
|
|
if (!tcs->enter_bold && tcs->enter_standout)
|
|
|
|
tcs->enter_bold = tcs->enter_standout;
|
|
|
|
|
|
|
|
if (!tcs->enter_underline && tcs->enter_standout) {
|
|
|
|
tcs->enter_underline = tcs->enter_standout;
|
|
|
|
tcs->exit_underline = tcs->exit_standout;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2020-06-29 16:00:14 -05:00
|
|
|
|
|
|
|
if (!tcs->enter_dim && tcs->enter_standout)
|
|
|
|
tcs->enter_dim = tcs->enter_standout;
|
|
|
|
|
|
|
|
if (!tcs->enter_reverse && tcs->enter_standout)
|
|
|
|
tcs->enter_reverse = tcs->enter_standout;
|
|
|
|
|
|
|
|
if (!tcs->exit_attributes && tcs->exit_standout)
|
|
|
|
tcs->exit_attributes = tcs->exit_standout;
|
2020-06-29 15:21:50 -05:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
/*
|
|
|
|
* Note that we use REVERSE for the alternate character set,
|
|
|
|
* not the as/ae capabilities. This is because we are modeling
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-06-29 16:00:14 -05:00
|
|
|
tcs->under_char = tigetstr("uc");
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->must_use_uc = (tcs->under_char && !tcs->enter_underline);
|
2020-06-29 16:00:14 -05:00
|
|
|
|
|
|
|
if ((tigetflag("os") && tcs->enter_bold == NULL) ||
|
|
|
|
(tigetflag("ul") && tcs->enter_underline == NULL && tcs->under_char == NULL))
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->must_overstrike = 1;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
static void sig_handler(int signo __attribute__((__unused__)))
|
2011-04-30 06:06:26 -05:00
|
|
|
{
|
2020-06-29 15:41:38 -05:00
|
|
|
_exit(EXIT_SUCCESS);
|
|
|
|
}
|
2011-04-30 06:06:26 -05:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
static void print_out(char *line)
|
|
|
|
{
|
|
|
|
if (line == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
putwp(line);
|
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void xsetmode(struct ul_ctl *ctl, struct term_caps const *const tcs, int newmode)
|
2020-06-29 15:41:38 -05:00
|
|
|
{
|
2020-06-30 16:17:01 -05:00
|
|
|
if (!ctl->iflag) {
|
|
|
|
if (ctl->curmode != NORMAL && newmode != NORMAL)
|
|
|
|
xsetmode(ctl, tcs, NORMAL);
|
2020-06-29 15:41:38 -05:00
|
|
|
switch (newmode) {
|
|
|
|
case NORMAL:
|
2020-06-30 16:17:01 -05:00
|
|
|
switch (ctl->curmode) {
|
2020-06-29 15:41:38 -05:00
|
|
|
case NORMAL:
|
|
|
|
break;
|
|
|
|
case UNDERL:
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->exit_underline);
|
2020-06-29 15:41:38 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* This includes standout */
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->exit_attributes);
|
2020-06-29 15:41:38 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALTSET:
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->enter_reverse);
|
2020-06-29 15:41:38 -05:00
|
|
|
break;
|
|
|
|
case SUPERSC:
|
|
|
|
/*
|
|
|
|
* This only works on a few terminals.
|
|
|
|
* It should be fixed.
|
|
|
|
*/
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->enter_underline);
|
|
|
|
print_out(tcs->enter_dim);
|
2020-06-29 15:41:38 -05:00
|
|
|
break;
|
|
|
|
case SUBSC:
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->enter_dim);
|
2020-06-29 15:41:38 -05:00
|
|
|
break;
|
|
|
|
case UNDERL:
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->enter_underline);
|
2020-06-29 15:41:38 -05:00
|
|
|
break;
|
|
|
|
case BOLD:
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->enter_bold);
|
2020-06-29 15:41:38 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* We should have some provision here for multiple modes
|
|
|
|
* on at once. This will have to come later.
|
|
|
|
*/
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->enter_standout);
|
2020-06-29 15:41:38 -05:00
|
|
|
break;
|
2011-04-30 06:06:26 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->curmode = newmode;
|
2011-04-30 06:06:26 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void iattr(struct ul_ctl *ctl)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2020-06-29 15:41:38 -05:00
|
|
|
int i;
|
2020-06-30 16:17:01 -05:00
|
|
|
wchar_t *lbuf = xcalloc(ctl->maxcol + 1, sizeof(wchar_t));
|
2020-06-29 15:41:38 -05:00
|
|
|
wchar_t *cp = lbuf;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
for (i = 0; i < ctl->maxcol; i++)
|
|
|
|
switch (ctl->obuf[i].c_mode) {
|
2020-06-29 15:41:38 -05:00
|
|
|
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;
|
2006-12-06 17:25:44 -06:00
|
|
|
}
|
2020-06-29 15:41:38 -05:00
|
|
|
for (*cp = ' '; *cp == ' '; cp--)
|
|
|
|
*cp = 0;
|
|
|
|
fputws(lbuf, stdout);
|
|
|
|
putwchar('\n');
|
|
|
|
free(lbuf);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void outc(struct ul_ctl *ctl, struct term_caps const *const tcs, wint_t c, int width)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2006-12-06 17:25:37 -06:00
|
|
|
int i;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
putwchar(c);
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->must_use_uc && (ctl->curmode & UNDERL)) {
|
2020-06-29 15:41:38 -05:00
|
|
|
for (i = 0; i < width; i++)
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->curs_left);
|
2020-06-29 15:41:38 -05:00
|
|
|
for (i = 0; i < width; i++)
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->under_char);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For terminals that can overstrike, overstrike underlines and bolds.
|
|
|
|
* We don't do anything with halfline ups and downs, or Greek.
|
|
|
|
*/
|
2020-06-30 16:17:01 -05:00
|
|
|
static void overstrike(struct ul_ctl *ctl)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2020-06-29 15:21:50 -05:00
|
|
|
int i;
|
2020-06-30 16:17:01 -05:00
|
|
|
wchar_t *lbuf = xcalloc(ctl->maxcol + 1, sizeof(wchar_t));
|
2020-06-29 15:21:50 -05:00
|
|
|
wchar_t *cp = lbuf;
|
|
|
|
int hadbold = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
/* Set up overstrike buffer */
|
2020-06-30 16:17:01 -05:00
|
|
|
for (i = 0; i < ctl->maxcol; i++)
|
|
|
|
switch (ctl->obuf[i].c_mode) {
|
2006-12-06 17:25:32 -06:00
|
|
|
case NORMAL:
|
|
|
|
default:
|
|
|
|
*cp++ = ' ';
|
|
|
|
break;
|
|
|
|
case UNDERL:
|
|
|
|
*cp++ = '_';
|
|
|
|
break;
|
|
|
|
case BOLD:
|
2020-06-30 16:17:01 -05:00
|
|
|
*cp++ = ctl->obuf[i].c_char;
|
|
|
|
if (ctl->obuf[i].c_width > 1)
|
|
|
|
i += ctl->obuf[i].c_width - 1;
|
2020-06-29 15:21:50 -05:00
|
|
|
hadbold = 1;
|
2006-12-06 17:25:32 -06:00
|
|
|
break;
|
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
putwchar('\r');
|
2011-04-30 06:06:24 -05:00
|
|
|
for (*cp = ' '; *cp == ' '; cp--)
|
2006-12-06 17:25:32 -06:00
|
|
|
*cp = 0;
|
2013-07-07 14:54:47 -05:00
|
|
|
fputws(lbuf, stdout);
|
2006-12-06 17:25:32 -06:00
|
|
|
if (hadbold) {
|
2006-12-06 17:25:41 -06:00
|
|
|
putwchar('\r');
|
2011-04-30 06:06:24 -05:00
|
|
|
for (cp = lbuf; *cp; cp++)
|
|
|
|
putwchar(*cp == '_' ? ' ' : *cp);
|
2006-12-06 17:25:41 -06:00
|
|
|
putwchar('\r');
|
2011-04-30 06:06:24 -05:00
|
|
|
for (cp = lbuf; *cp; cp++)
|
|
|
|
putwchar(*cp == '_' ? ' ' : *cp);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2016-09-08 14:19:22 -05:00
|
|
|
free(lbuf);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void flushln(struct ul_ctl *ctl, struct term_caps const *const tcs)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2020-06-29 15:41:38 -05:00
|
|
|
int lastmode;
|
2020-06-29 15:21:50 -05:00
|
|
|
int i;
|
2020-06-29 15:41:38 -05:00
|
|
|
int hadmodes = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
lastmode = NORMAL;
|
2020-06-30 16:17:01 -05:00
|
|
|
for (i = 0; i < ctl->maxcol; i++) {
|
|
|
|
if (ctl->obuf[i].c_mode != lastmode) {
|
2020-06-29 15:41:38 -05:00
|
|
|
hadmodes++;
|
2020-06-30 16:17:01 -05:00
|
|
|
xsetmode(ctl, tcs, ctl->obuf[i].c_mode);
|
|
|
|
lastmode = ctl->obuf[i].c_mode;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->obuf[i].c_char == '\0') {
|
|
|
|
if (ctl->upln)
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->curs_right);
|
2020-06-29 15:41:38 -05:00
|
|
|
else
|
2020-06-30 16:17:01 -05:00
|
|
|
outc(ctl, tcs, ' ', 1);
|
2020-06-29 15:41:38 -05:00
|
|
|
} else
|
2020-06-30 16:17:01 -05:00
|
|
|
outc(ctl, tcs, ctl->obuf[i].c_char, ctl->obuf[i].c_width);
|
|
|
|
if (ctl->obuf[i].c_width > 1)
|
|
|
|
i += ctl->obuf[i].c_width - 1;
|
2020-06-29 15:41:38 -05:00
|
|
|
}
|
|
|
|
if (lastmode != NORMAL)
|
2020-06-30 16:17:01 -05:00
|
|
|
xsetmode(ctl, tcs, NORMAL);
|
|
|
|
if (ctl->must_overstrike && hadmodes)
|
|
|
|
overstrike(ctl);
|
2006-12-06 17:25:41 -06:00
|
|
|
putwchar('\n');
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->iflag && hadmodes)
|
|
|
|
iattr(ctl);
|
2020-06-29 15:41:38 -05:00
|
|
|
fflush(stdout);
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->upln)
|
|
|
|
ctl->upln--;
|
|
|
|
initbuf(ctl);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void fwd(struct ul_ctl *ctl, struct term_caps const *const tcs)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2006-12-06 17:25:37 -06:00
|
|
|
int oldcol, oldmax;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
oldcol = ctl->col;
|
|
|
|
oldmax = ctl->maxcol;
|
|
|
|
flushln(ctl, tcs);
|
|
|
|
setcol(ctl, oldcol);
|
|
|
|
ctl->maxcol = oldmax;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void reverse(struct ul_ctl *ctl, struct term_caps const *const tcs)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->upln++;
|
|
|
|
fwd(ctl, tcs);
|
2020-06-29 16:00:14 -05:00
|
|
|
print_out(tcs->curs_up);
|
|
|
|
print_out(tcs->curs_up);
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->upln++;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static int handle_escape(struct ul_ctl *ctl, struct term_caps const *const tcs, FILE *f)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2020-06-29 15:41:38 -05:00
|
|
|
wint_t c;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
switch (c = getwc(f)) {
|
|
|
|
case HREV:
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->halfpos == 0) {
|
|
|
|
ctl->mode |= SUPERSC;
|
|
|
|
ctl->halfpos--;
|
|
|
|
} else if (ctl->halfpos > 0) {
|
|
|
|
ctl->mode &= ~SUBSC;
|
|
|
|
ctl->halfpos--;
|
2020-06-29 15:41:38 -05:00
|
|
|
} else {
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->halfpos = 0;
|
|
|
|
reverse(ctl, tcs);
|
2020-06-29 15:41:38 -05:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case HFWD:
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->halfpos == 0) {
|
|
|
|
ctl->mode |= SUBSC;
|
|
|
|
ctl->halfpos++;
|
|
|
|
} else if (ctl->halfpos < 0) {
|
|
|
|
ctl->mode &= ~SUPERSC;
|
|
|
|
ctl->halfpos++;
|
2020-06-29 15:41:38 -05:00
|
|
|
} else {
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->halfpos = 0;
|
|
|
|
fwd(ctl, tcs);
|
2020-06-29 15:41:38 -05:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case FREV:
|
2020-06-30 16:17:01 -05:00
|
|
|
reverse(ctl, tcs);
|
2020-06-29 15:41:38 -05:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
/* unknown escape */
|
|
|
|
ungetwc(c, f);
|
|
|
|
return 1;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
static void filter(struct ul_ctl *ctl, struct term_caps const *const tcs, FILE *f)
|
2020-06-29 15:21:50 -05:00
|
|
|
{
|
2020-06-29 15:41:38 -05:00
|
|
|
wint_t c;
|
|
|
|
int i, w;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
while ((c = getwc(f)) != WEOF) {
|
|
|
|
switch (c) {
|
|
|
|
case '\b':
|
2020-06-30 16:17:01 -05:00
|
|
|
setcol(ctl, ctl->col - 1);
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
|
|
|
case '\t':
|
2020-06-30 16:17:01 -05:00
|
|
|
setcol(ctl, (ctl->col + 8) & ~07);
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
|
|
|
case '\r':
|
2020-06-30 16:17:01 -05:00
|
|
|
setcol(ctl, 0);
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
|
|
|
case SO:
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->mode |= ALTSET;
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
|
|
|
case SI:
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->mode &= ~ALTSET;
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
|
|
|
case IESC:
|
2020-06-30 16:17:01 -05:00
|
|
|
if (handle_escape(ctl, tcs, f)) {
|
2020-06-29 15:41:38 -05:00
|
|
|
c = getwc(f);
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
_("unknown escape sequence in input: %o, %o"), IESC, c);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
|
|
|
case '_':
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->obuf[ctl->col].c_char || ctl->obuf[ctl->col].c_width < 0) {
|
|
|
|
while (ctl->col > 0 && ctl->obuf[ctl->col].c_width < 0)
|
|
|
|
ctl->col--;
|
|
|
|
w = ctl->obuf[ctl->col].c_width;
|
2020-06-29 15:41:38 -05:00
|
|
|
for (i = 0; i < w; i++)
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuf[ctl->col++].c_mode |= UNDERL | ctl->mode;
|
|
|
|
setcol(ctl, ctl->col);
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuf[ctl->col].c_char = '_';
|
|
|
|
ctl->obuf[ctl->col].c_width = 1;
|
2020-06-29 15:41:38 -05:00
|
|
|
/* fallthrough */
|
|
|
|
case ' ':
|
2020-06-30 16:17:01 -05:00
|
|
|
setcol(ctl, ctl->col + 1);
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
|
|
|
case '\n':
|
2020-06-30 16:17:01 -05:00
|
|
|
flushln(ctl, tcs);
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
|
|
|
case '\f':
|
2020-06-30 16:17:01 -05:00
|
|
|
flushln(ctl, tcs);
|
2020-06-29 15:41:38 -05:00
|
|
|
putwchar('\f');
|
|
|
|
continue;
|
2006-12-06 17:25:32 -06:00
|
|
|
default:
|
2020-06-29 15:41:38 -05:00
|
|
|
if (!iswprint(c))
|
|
|
|
/* non printable */
|
|
|
|
continue;
|
|
|
|
w = wcwidth(c);
|
2020-06-30 16:17:01 -05:00
|
|
|
needcol(ctl, ctl->col + w);
|
|
|
|
if (ctl->obuf[ctl->col].c_char == '\0') {
|
|
|
|
ctl->obuf[ctl->col].c_char = c;
|
2020-06-29 15:41:38 -05:00
|
|
|
for (i = 0; i < w; i++)
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuf[ctl->col + i].c_mode = ctl->mode;
|
|
|
|
ctl->obuf[ctl->col].c_width = w;
|
2020-06-29 15:41:38 -05:00
|
|
|
for (i = 1; i < w; i++)
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuf[ctl->col + i].c_width = -1;
|
|
|
|
} else if (ctl->obuf[ctl->col].c_char == '_') {
|
|
|
|
ctl->obuf[ctl->col].c_char = c;
|
2020-06-29 15:41:38 -05:00
|
|
|
for (i = 0; i < w; i++)
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuf[ctl->col + i].c_mode |= UNDERL | ctl->mode;
|
|
|
|
ctl->obuf[ctl->col].c_width = w;
|
2020-06-29 15:41:38 -05:00
|
|
|
for (i = 1; i < w; i++)
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuf[ctl->col + i].c_width = -1;
|
|
|
|
} else if ((wint_t) ctl->obuf[ctl->col].c_char == c) {
|
2020-06-29 15:41:38 -05:00
|
|
|
for (i = 0; i < w; i++)
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuf[ctl->col + i].c_mode |= BOLD | ctl->mode;
|
2020-06-29 15:41:38 -05:00
|
|
|
} else {
|
2020-06-30 16:17:01 -05:00
|
|
|
w = ctl->obuf[ctl->col].c_width;
|
2020-06-29 15:41:38 -05:00
|
|
|
for (i = 0; i < w; i++)
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl->obuf[ctl->col + i].c_mode = ctl->mode;
|
2020-06-29 15:41:38 -05:00
|
|
|
}
|
2020-06-30 16:17:01 -05:00
|
|
|
setcol(ctl, ctl->col + w);
|
2020-06-29 15:41:38 -05:00
|
|
|
continue;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
}
|
2020-06-30 16:17:01 -05:00
|
|
|
if (ctl->maxcol)
|
|
|
|
flushln(ctl, tcs);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
int main(int argc, char **argv)
|
2020-06-29 15:21:50 -05:00
|
|
|
{
|
2020-06-29 15:41:38 -05:00
|
|
|
int c, ret, tflag = 0;
|
|
|
|
char *termtype;
|
2020-06-29 16:00:14 -05:00
|
|
|
struct term_caps tcs = { 0 };
|
2020-06-30 16:17:01 -05:00
|
|
|
struct ul_ctl ctl = { .curmode = NORMAL };
|
2020-06-29 15:41:38 -05:00
|
|
|
FILE *f;
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
static const struct option longopts[] = {
|
|
|
|
{ "terminal", required_argument, NULL, 't' },
|
|
|
|
{ "indicated", no_argument, NULL, 'i' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
close_stdout_atexit();
|
2006-12-06 17:25:44 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
signal(SIGINT, sig_handler);
|
|
|
|
signal(SIGTERM, sig_handler);
|
2006-12-06 17:25:44 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
termtype = getenv("TERM");
|
2006-12-06 17:25:44 -06:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
while ((c = getopt_long(argc, argv, "it:T:Vh", longopts, NULL)) != -1)
|
|
|
|
switch (c) {
|
2010-08-29 21:03:32 -05:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
case 't':
|
|
|
|
case 'T':
|
|
|
|
/* for nroff compatibility */
|
|
|
|
termtype = optarg;
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
|
|
|
case 'i':
|
2020-06-30 16:17:01 -05:00
|
|
|
ctl.iflag = 1;
|
2020-06-29 15:41:38 -05:00
|
|
|
break;
|
2010-08-29 21:03:32 -05:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
case 'V':
|
|
|
|
print_version(EXIT_SUCCESS);
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
default:
|
|
|
|
errtryhelp(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
setupterm(termtype, STDOUT_FILENO, &ret);
|
|
|
|
switch (ret) {
|
2011-04-30 06:06:20 -05:00
|
|
|
|
2020-06-29 15:41:38 -05:00
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
warnx(_("trouble reading terminfo"));
|
|
|
|
/* fallthrough */
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
if (tflag)
|
|
|
|
warnx(_("terminal `%s' is not known, defaulting to `dumb'"),
|
|
|
|
termtype);
|
|
|
|
setupterm("dumb", STDOUT_FILENO, (int *)0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-30 16:17:01 -05:00
|
|
|
initinfo(&ctl, &tcs);
|
|
|
|
initbuf(&ctl);
|
2020-06-29 15:41:38 -05:00
|
|
|
if (optind == argc)
|
2020-06-30 16:17:01 -05:00
|
|
|
filter(&ctl, &tcs, stdin);
|
2020-06-29 15:41:38 -05:00
|
|
|
else
|
|
|
|
for (; optind < argc; optind++) {
|
|
|
|
f = fopen(argv[optind], "r");
|
|
|
|
if (!f)
|
|
|
|
err(EXIT_FAILURE, _("cannot open %s"), argv[optind]);
|
2020-06-30 16:17:01 -05:00
|
|
|
filter(&ctl, &tcs, f);
|
2020-06-29 15:41:38 -05:00
|
|
|
fclose(f);
|
|
|
|
}
|
2020-06-30 16:17:01 -05:00
|
|
|
free(ctl.obuf);
|
2020-08-01 18:58:35 -05:00
|
|
|
del_curterm(cur_term);
|
2020-06-29 15:41:38 -05:00
|
|
|
return EXIT_SUCCESS;
|
2011-04-30 06:06:20 -05:00
|
|
|
}
|