* Routines which deal with the characteristics of the terminal.
* Uses termcap to be as terminal-independent as possible.
* {{ Someday this should be rewritten to use curses. }}
#if !TERMIO && defined(TIOCGWINSZ)
* For the Unix PC (ATT 7300 & 3B1):
* Since WIOCGETD is defined in sys/window.h, we can't use that to decide
* whether to include sys/window.h. Use SIGPHONE from signal.h instead.
#if NEED_PTEM_H && defined(TIOCGWINSZ)
* All this just to get struct winsize. Sigh.
* Strings passed to tputs() to do various terminal functions.
*sc_pad
, /* Pad string */
*sc_home
, /* Cursor home */
*sc_addline
, /* Add line, scroll down following lines */
*sc_lower_left
, /* Cursor to last line, first column */
*sc_move
, /* General cursor positioning */
*sc_clear
, /* Clear screen */
*sc_eol_clear
, /* Clear to end of line */
*sc_s_in
, /* Enter standout (highlighted) mode */
*sc_s_out
, /* Exit standout mode */
*sc_u_in
, /* Enter underline mode */
*sc_u_out
, /* Exit underline mode */
*sc_b_in
, /* Enter bold mode */
*sc_b_out
, /* Exit bold mode */
*sc_bl_in
, /* Enter blink mode */
*sc_bl_out
, /* Exit blink mode */
*sc_visual_bell
, /* Visual bell (flash screen) sequence */
*sc_backspace
, /* Backspace cursor */
*sc_init
, /* Startup terminal initialization */
*sc_deinit
; /* Exit terminal de-initialization */
static int init_done
= 0;
public int auto_wrap
; /* Terminal does \r\n when write past margin */
public int ignaw
; /* Terminal ignores \n immediately after wrap */
public int erase_char
, kill_char
; /* The user's erase and line-kill chars */
public int sc_width
, sc_height
; /* Height & width of screen */
public int bo_s_width
, bo_e_width
; /* Printing width of boldface seq */
public int ul_s_width
, ul_e_width
; /* Printing width of underline seq */
public int so_s_width
, so_e_width
; /* Printing width of standout seq */
public int bl_s_width
, bl_e_width
; /* Printing width of blink seq */
* These two variables are sometimes defined in,
* and needed by, the termcap library.
* It may be necessary on some systems to declare them extern here.
/*extern*/ short ospeed
; /* Terminal output baud rate */
/*extern*/ char PC
; /* Pad character */
extern int quiet
; /* If VERY_QUIET, use visual bell for bell */
extern int know_dumb
; /* Don't complain about a dumb terminal */
* Change terminal to "raw mode", or restore to "normal" mode.
* 1. An outstanding read will complete on receipt of a single keystroke.
* 2. Input is not echoed.
* 3. On output, \n is mapped to \r\n.
* 4. \t is NOT expanded into spaces.
* 5. Signal-causing characters such as ctrl-C (interrupt),
* It doesn't matter whether an input \n is mapped to \r, or vice versa.
static struct termio save_term
;
* Save modes and set certain variables dependent on modes.
ospeed
= s
.c_cflag
& CBAUD
;
erase_char
= s
.c_cc
[VERASE
];
kill_char
= s
.c_cc
[VKILL
];
* Set the modes to the way we want them.
s
.c_lflag
&= ~(ICANON
|ECHO
|ECHOE
|ECHOK
|ECHONL
);
s
.c_oflag
|= (OPOST
|ONLCR
|TAB3
);
s
.c_oflag
&= ~(OCRNL
|ONOCR
|ONLRET
);
static struct sgttyb save_term
;
* Save modes and set certain variables dependent on modes.
* Set the modes to the way we want them.
s
.sg_flags
&= ~(ECHO
|XTABS
);
* User knows this is a dumb terminal, so don't tell him.
error("WARNING: terminal cannot %s", &parg
);
* Get size of the output screen.
scrsize(p_height
, p_width
)
if (ioctl(2, TIOCGWINSZ
, &w
) == 0 && w
.ws_row
> 0)
if (ioctl(2, WIOCGETD
, &w
) == 0 && w
.uw_height
> 0)
*p_height
= w
.uw_height
/w
.uw_vs
;
if ((s
= getenv("LINES")) != NULL
)
*p_height
= tgetnum("li");
if (ioctl(2, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
if (ioctl(2, WIOCGETD
, &w
) == 0 && w
.uw_width
> 0)
*p_width
= w
.uw_width
/w
.uw_hs
;
if ((s
= getenv("COLUMNS")) != NULL
)
*p_width
= tgetnum("co");
* Get terminal capabilities via termcap.
* Find out what kind of terminal this is.
if ((term
= getenv("TERM")) == NULL
)
if (tgetent(termbuf
, term
) <= 0)
strcpy(termbuf
, "dumb:hc:");
* Get size of the screen.
scrsize(&sc_height
, &sc_width
);
auto_wrap
= tgetflag("am");
* Assumes termcap variable "sg" is the printing width of:
* the standout sequence, the end standout sequence,
* the underline sequence, the end underline sequence,
* the boldface sequence, and the end boldface sequence.
if ((so_s_width
= tgetnum("sg")) < 0)
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
;
* Get various string-valued capabilities.
sc_pad
= tgetstr("pc", &sp
);
sc_init
= tgetstr("ti", &sp
);
sc_deinit
= tgetstr("te", &sp
);
sc_eol_clear
= tgetstr("ce", &sp
);
if (hard
|| sc_eol_clear
== NULL
|| *sc_eol_clear
== '\0')
cannot("clear to end of line");
sc_clear
= tgetstr("cl", &sp
);
if (hard
|| sc_clear
== NULL
|| *sc_clear
== '\0')
sc_move
= tgetstr("cm", &sp
);
if (hard
|| sc_move
== NULL
|| *sc_move
== '\0')
* This is not an error here, because we don't
* We need it only if we don't have home or lower-left.
sc_s_in
= tgetstr("so", &sp
);
if (hard
|| sc_s_in
== NULL
)
sc_s_out
= tgetstr("se", &sp
);
if (hard
|| sc_s_out
== NULL
)
sc_u_in
= tgetstr("us", &sp
);
if (hard
|| sc_u_in
== NULL
)
sc_u_out
= tgetstr("ue", &sp
);
if (hard
|| sc_u_out
== NULL
)
sc_b_in
= tgetstr("md", &sp
);
if (hard
|| sc_b_in
== NULL
)
sc_b_out
= tgetstr("me", &sp
);
if (hard
|| sc_b_out
== NULL
)
sc_bl_in
= tgetstr("mb", &sp
);
if (hard
|| sc_bl_in
== NULL
)
sc_visual_bell
= tgetstr("vb", &sp
);
if (hard
|| sc_visual_bell
== NULL
)
sc_backspace
= tgetstr("bc", &sp
);
if (sc_backspace
== NULL
|| *sc_backspace
== '\0')
* Choose between using "ho" and "cm" ("home" and "cursor move")
* to move the cursor to the upper left corner of the screen.
strcpy(sp
, tgoto(sc_move
, 0, 0));
sc_home
= cheaper(t1
, t2
, "home cursor", "|\b^");
* Choose between using "ll" and "cm" ("lower left" and "cursor move")
* to move the cursor to the lower left corner of the screen.
strcpy(sp
, tgoto(sc_move
, 0, sc_height
-1));
sc_lower_left
= cheaper(t1
, t2
,
"move cursor to lower left of screen", "\r");
* Choose between using "al" or "sr" ("add line" or "scroll reverse")
* to add a line at the top of the screen.
sc_addline
= cheaper(t1
, t2
, "scroll backwards", "");
* Force repaint on any backward movement.
* Return the cost of displaying a termcap string.
* We use the trick of calling tputs, but as a char printing function
* we give it inc_costcount, which just increments "costcount".
* This tells us how many chars would be printed by using this string.
* {{ Couldn't we just use strlen? }}
tputs(t
, sc_height
, inc_costcount
);
* Return the "best" of the two given termcap strings.
* The best, if both exist, is the one with the lower
* cost (see cost() function).
cheaper(t1
, t2
, doit
, def
)
if (*t1
== '\0' && *t2
== '\0')
* Below are the functions which perform all the
* terminal-specific screen manipulation.
tputs(sc_init
, sc_height
, putchr
);
tputs(sc_deinit
, sc_height
, putchr
);
* Home cursor (move to upper left corner of screen).
tputs(sc_home
, 1, putchr
);
* Add a blank line (called with cursor at home).
* Should scroll the display down.
tputs(sc_addline
, sc_height
, putchr
);
* Move cursor to lower left corner of screen.
tputs(sc_lower_left
, 1, putchr
);
* Ring the terminal bell.
* Output the "visual bell", if there is one.
if (*sc_visual_bell
== '\0')
tputs(sc_visual_bell
, sc_height
, putchr
);
tputs(sc_clear
, sc_height
, putchr
);
* Clear from the cursor to the end of the cursor's line.
* {{ This must not move the cursor. }}
tputs(sc_eol_clear
, 1, putchr
);
* Begin "standout" (bold, underline, or whatever).
tputs(sc_s_in
, 1, putchr
);
tputs(sc_s_out
, 1, putchr
);
* Begin "underline" (hopefully real underlining,
* otherwise whatever the terminal provides).
tputs(sc_u_in
, 1, putchr
);
tputs(sc_u_out
, 1, putchr
);
tputs(sc_b_in
, 1, putchr
);
tputs(sc_b_out
, 1, putchr
);
tputs(sc_bl_in
, 1, putchr
);
tputs(sc_bl_out
, 1, putchr
);
* Erase the character to the left of the cursor
* and move the cursor left.
* Try to erase the previous character by overstriking with a space.
tputs(sc_backspace
, 1, putchr
);
tputs(sc_backspace
, 1, putchr
);
* Output a plain backspace, without erasing the previous char.
tputs(sc_backspace
, 1, putchr
);