#include <sys/ioctl.h>
#include <err.h>
#include <term.h>
#include <termios.h>
#include "cmd.h"
#include "less.h"
#define DEFAULT_TERM "unknown"
static char
*sc_home,
*sc_addline,
*sc_lower_left,
*sc_return,
*sc_move,
*sc_clear,
*sc_eol_clear,
*sc_eos_clear,
*sc_s_in,
*sc_s_out,
*sc_u_in,
*sc_u_out,
*sc_b_in,
*sc_b_out,
*sc_bl_in,
*sc_bl_out,
*sc_visual_bell,
*sc_backspace,
*sc_s_keypad,
*sc_e_keypad,
*sc_init,
*sc_deinit;
static int init_done = 0;
int auto_wrap;
int ignaw;
int erase_char;
int erase2_char;
int kill_char;
int werase_char;
int sc_width, sc_height;
int bo_s_width, bo_e_width;
int ul_s_width, ul_e_width;
int so_s_width, so_e_width;
int bl_s_width, bl_e_width;
int can_goto_line;
int missing_cap = 0;
static int above_mem;
static int below_mem;
static int attrmode = AT_NORMAL;
extern int binattr;
static char *cheaper(char *, char *, char *);
static void tmodes(char *, char *, char **, char **, char *, char *);
extern int quiet;
extern int no_back_scroll;
extern int swindow;
extern int no_init;
extern int no_keypad;
extern int wscroll;
extern int screen_trashed;
extern int tty;
extern int top_scroll;
extern int oldbot;
extern int hilite_search;
void
raw_mode(int on)
{
static int curr_on = 0;
struct termios s;
static struct termios save_term;
static int saved_term = 0;
if (on == curr_on)
return;
erase2_char = '\b';
if (on) {
(void) tcgetattr(tty, &s);
if (!saved_term) {
save_term = s;
saved_term = 1;
}
erase_char = s.c_cc[VERASE];
#ifdef VERASE2
erase2_char = s.c_cc[VERASE2];
#endif
kill_char = s.c_cc[VKILL];
#ifdef VWERASE
werase_char = s.c_cc[VWERASE];
#endif
s.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL);
#ifndef TAB3
#define TAB3 0
#endif
s.c_oflag |= (TAB3 | OPOST | ONLCR);
s.c_oflag &= ~(OCRNL | ONOCR | ONLRET);
s.c_cc[VMIN] = 1;
s.c_cc[VTIME] = 0;
#ifdef VLNEXT
s.c_cc[VLNEXT] = 0;
#endif
#ifdef VDSUSP
s.c_cc[VDSUSP] = 0;
#endif
} else {
s = save_term;
}
(void) tcsetattr(tty, TCSASOFT | TCSADRAIN, &s);
curr_on = on;
}
static int hardcopy;
static void
scrsize(void)
{
int sys_height = 0, sys_width = 0, n;
struct winsize w;
char *s;
#define DEF_SC_WIDTH 80
#define DEF_SC_HEIGHT 24
if (ioctl(2, TIOCGWINSZ, &w) == 0) {
if (w.ws_row > 0)
sys_height = w.ws_row;
if (w.ws_col > 0)
sys_width = w.ws_col;
}
if (sys_height > 0)
sc_height = sys_height;
else if ((s = lgetenv("LINES")) != NULL)
sc_height = atoi(s);
else if (!hardcopy && (n = lines) > 0)
sc_height = n;
if (sc_height <= 0)
sc_height = DEF_SC_HEIGHT;
if (sys_width > 0)
sc_width = sys_width;
else if ((s = lgetenv("COLUMNS")) != NULL)
sc_width = atoi(s);
else if (!hardcopy && (n = columns) > 0)
sc_width = n;
if (sc_width <= 0)
sc_width = DEF_SC_WIDTH;
}
char *
special_key_str(int key)
{
char *s;
static char ctrlk[] = { CONTROL('K'), 0 };
if (hardcopy)
return (NULL);
switch (key) {
case SK_RIGHT_ARROW:
s = key_right;
break;
case SK_LEFT_ARROW:
s = key_left;
break;
case SK_UP_ARROW:
s = key_up;
break;
case SK_DOWN_ARROW:
s = key_down;
break;
case SK_PAGE_UP:
s = key_ppage;
break;
case SK_PAGE_DOWN:
s = key_npage;
break;
case SK_HOME:
s = key_home;
break;
case SK_END:
s = key_end;
break;
case SK_DELETE:
s = key_dc;
if (s == NULL) {
s = "\177\0";
}
break;
case SK_CONTROL_K:
s = ctrlk;
break;
default:
return (NULL);
}
return (s);
}
void
get_term(void)
{
char *t1, *t2;
char *term;
int err;
if ((term = lgetenv("TERM")) == NULL)
term = DEFAULT_TERM;
hardcopy = 0;
if (setupterm(term, 1, &err) < 0) {
if (err == 1)
hardcopy = 1;
else
errx(1, "%s: unknown terminal type", term);
}
if (hard_copy == 1)
hardcopy = 1;
scrsize();
pos_init();
auto_wrap = hardcopy ? 0 : auto_right_margin;
ignaw = hardcopy ? 0 : eat_newline_glitch;
above_mem = hardcopy ? 0 : memory_above;
below_mem = hardcopy ? 0 : memory_below;
if (hardcopy || (so_s_width = magic_cookie_glitch) < 0)
so_s_width = 0;
so_e_width = so_s_width;
bo_s_width = bo_e_width = so_s_width;
ul_s_width = ul_e_width = so_s_width;
bl_s_width = bl_e_width = so_s_width;
if (so_s_width > 0 || so_e_width > 0)
hilite_search = 0;
sc_s_keypad = keypad_xmit;
if (hardcopy || sc_s_keypad == NULL)
sc_s_keypad = "";
sc_e_keypad = keypad_local;
if (hardcopy || sc_e_keypad == NULL)
sc_e_keypad = "";
sc_init = enter_ca_mode;
if (hardcopy || sc_init == NULL)
sc_init = "";
sc_deinit = exit_ca_mode;
if (hardcopy || sc_deinit == NULL)
sc_deinit = "";
sc_eol_clear = clr_eol;
if (hardcopy || sc_eol_clear == NULL || *sc_eol_clear == '\0') {
missing_cap = 1;
sc_eol_clear = "";
}
sc_eos_clear = clr_eos;
if (below_mem &&
(hardcopy || sc_eos_clear == NULL || *sc_eos_clear == '\0')) {
missing_cap = 1;
sc_eos_clear = "";
}
sc_clear = clear_screen;
if (hardcopy || sc_clear == NULL || *sc_clear == '\0') {
missing_cap = 1;
sc_clear = "\n\n";
}
sc_move = cursor_address;
if (hardcopy || sc_move == NULL || *sc_move == '\0') {
sc_move = "";
can_goto_line = 0;
} else {
can_goto_line = 1;
}
tmodes(enter_standout_mode, exit_standout_mode, &sc_s_in, &sc_s_out,
"", "");
tmodes(enter_underline_mode, exit_underline_mode, &sc_u_in, &sc_u_out,
sc_s_in, sc_s_out);
tmodes(enter_bold_mode, exit_attribute_mode, &sc_b_in, &sc_b_out,
sc_s_in, sc_s_out);
tmodes(enter_blink_mode, exit_attribute_mode, &sc_bl_in, &sc_bl_out,
sc_s_in, sc_s_out);
sc_visual_bell = flash_screen;
if (hardcopy || sc_visual_bell == NULL)
sc_visual_bell = "";
sc_backspace = "\b";
t1 = cursor_home;
if (hardcopy || t1 == NULL)
t1 = "";
if (*sc_move == '\0') {
t2 = "";
} else {
t2 = estrdup(tparm(sc_move, 0, 0, 0, 0, 0, 0, 0, 0, 0));
}
sc_home = cheaper(t1, t2, "|\b^");
t1 = cursor_to_ll;
if (hardcopy || t1 == NULL)
t1 = "";
if (*sc_move == '\0') {
t2 = "";
} else {
t2 = estrdup(tparm(sc_move, sc_height-1,
0, 0, 0, 0, 0, 0, 0, 0));
}
sc_lower_left = cheaper(t1, t2, "\r");
sc_return = carriage_return;
if (hardcopy || sc_return == NULL)
sc_return = "\r";
t1 = insert_line;
if (hardcopy || t1 == NULL)
t1 = "";
t2 = scroll_reverse;
if (hardcopy || t2 == NULL)
t2 = "";
if (above_mem)
sc_addline = t1;
else
sc_addline = cheaper(t1, t2, "");
if (*sc_addline == '\0') {
no_back_scroll = 1;
}
}
static int costcount;
static int
inc_costcount(int c)
{
costcount++;
return (c);
}
static int
cost(char *t)
{
costcount = 0;
(void) tputs(t, sc_height, inc_costcount);
return (costcount);
}
static char *
cheaper(char *t1, char *t2, char *def)
{
if (*t1 == '\0' && *t2 == '\0') {
missing_cap = 1;
return (def);
}
if (*t1 == '\0')
return (t2);
if (*t2 == '\0')
return (t1);
if (cost(t1) < cost(t2))
return (t1);
return (t2);
}
static void
tmodes(char *incap, char *outcap, char **instr, char **outstr,
char *def_instr, char *def_outstr)
{
if (hardcopy) {
*instr = "";
*outstr = "";
return;
}
*instr = incap;
*outstr = outcap;
if (*instr == NULL) {
*instr = def_instr;
*outstr = def_outstr;
return;
}
if (*outstr == NULL)
*outstr = exit_attribute_mode;
if (*outstr == NULL)
*outstr = "";
}
void
init(void)
{
if (!no_init)
(void) tputs(sc_init, sc_height, putchr);
if (!no_keypad)
(void) tputs(sc_s_keypad, sc_height, putchr);
if (top_scroll) {
int i;
for (i = 1; i < sc_height; i++)
(void) putchr('\n');
} else
line_left();
init_done = 1;
}
void
deinit(void)
{
if (!init_done)
return;
if (!no_keypad)
(void) tputs(sc_e_keypad, sc_height, putchr);
if (!no_init)
(void) tputs(sc_deinit, sc_height, putchr);
init_done = 0;
}
void
home(void)
{
(void) tputs(sc_home, 1, putchr);
}
void
add_line(void)
{
(void) tputs(sc_addline, sc_height, putchr);
}
void
lower_left(void)
{
(void) tputs(sc_lower_left, 1, putchr);
}
void
line_left(void)
{
(void) tputs(sc_return, 1, putchr);
}
void
goto_line(int slinenum)
{
(void) tputs(tparm(sc_move, slinenum, 0, 0, 0, 0, 0, 0, 0, 0), 1,
putchr);
}
void
vbell(void)
{
if (*sc_visual_bell == '\0')
return;
(void) tputs(sc_visual_bell, sc_height, putchr);
}
static void
beep(void)
{
(void) putchr(CONTROL('G'));
}
void
ring_bell(void)
{
if (quiet == VERY_QUIET)
vbell();
else
beep();
}
void
do_clear(void)
{
(void) tputs(sc_clear, sc_height, putchr);
}
void
clear_eol(void)
{
(void) tputs(sc_eol_clear, 1, putchr);
}
static void
clear_eol_bot(void)
{
if (below_mem)
(void) tputs(sc_eos_clear, 1, putchr);
else
(void) tputs(sc_eol_clear, 1, putchr);
}
void
clear_bot(void)
{
if (oldbot)
lower_left();
else
line_left();
if (attrmode == AT_NORMAL)
clear_eol_bot();
else
{
int saved_attrmode = attrmode;
at_exit();
clear_eol_bot();
at_enter(saved_attrmode);
}
}
void
at_enter(int attr)
{
attr = apply_at_specials(attr);
if (attr & AT_UNDERLINE)
(void) tputs(sc_u_in, 1, putchr);
if (attr & AT_BOLD)
(void) tputs(sc_b_in, 1, putchr);
if (attr & AT_BLINK)
(void) tputs(sc_bl_in, 1, putchr);
if (attr & AT_STANDOUT)
(void) tputs(sc_s_in, 1, putchr);
attrmode = attr;
}
void
at_exit(void)
{
if (attrmode & AT_STANDOUT)
(void) tputs(sc_s_out, 1, putchr);
if (attrmode & AT_BLINK)
(void) tputs(sc_bl_out, 1, putchr);
if (attrmode & AT_BOLD)
(void) tputs(sc_b_out, 1, putchr);
if (attrmode & AT_UNDERLINE)
(void) tputs(sc_u_out, 1, putchr);
attrmode = AT_NORMAL;
}
void
at_switch(int attr)
{
int new_attrmode = apply_at_specials(attr);
int ignore_modes = AT_ANSI;
if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes)) {
at_exit();
at_enter(attr);
}
}
int
is_at_equiv(int attr1, int attr2)
{
attr1 = apply_at_specials(attr1);
attr2 = apply_at_specials(attr2);
return (attr1 == attr2);
}
int
apply_at_specials(int attr)
{
if (attr & AT_BINARY)
attr |= binattr;
if (attr & AT_HILITE)
attr |= AT_STANDOUT;
attr &= ~(AT_BINARY|AT_HILITE);
return (attr);
}
void
putbs(void)
{
(void) tputs(sc_backspace, 1, putchr);
}