Home | History | Annotate | Line # | Download | only in more
      1  1.7       agc /*	$NetBSD: screen.c,v 1.7 2003/10/13 14:34:25 agc Exp $	*/
      2  1.3     perry 
      3  1.1       cjs /*
      4  1.7       agc  * Copyright (c) 1988 Mark Nudelman
      5  1.1       cjs  * Copyright (c) 1988, 1993
      6  1.1       cjs  *	The Regents of the University of California.  All rights reserved.
      7  1.1       cjs  *
      8  1.1       cjs  * Redistribution and use in source and binary forms, with or without
      9  1.1       cjs  * modification, are permitted provided that the following conditions
     10  1.1       cjs  * are met:
     11  1.1       cjs  * 1. Redistributions of source code must retain the above copyright
     12  1.1       cjs  *    notice, this list of conditions and the following disclaimer.
     13  1.1       cjs  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1       cjs  *    notice, this list of conditions and the following disclaimer in the
     15  1.1       cjs  *    documentation and/or other materials provided with the distribution.
     16  1.6       agc  * 3. Neither the name of the University nor the names of its contributors
     17  1.6       agc  *    may be used to endorse or promote products derived from this software
     18  1.6       agc  *    without specific prior written permission.
     19  1.6       agc  *
     20  1.6       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  1.6       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.6       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.6       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  1.6       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.6       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.6       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.6       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.6       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.6       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.6       agc  * SUCH DAMAGE.
     31  1.6       agc  */
     32  1.6       agc 
     33  1.4  christos #include <sys/cdefs.h>
     34  1.1       cjs #ifndef lint
     35  1.4  christos #if 0
     36  1.1       cjs static char sccsid[] = "@(#)screen.c	8.2 (Berkeley) 4/20/94";
     37  1.4  christos #else
     38  1.7       agc __RCSID("$NetBSD: screen.c,v 1.7 2003/10/13 14:34:25 agc Exp $");
     39  1.4  christos #endif
     40  1.1       cjs #endif /* not lint */
     41  1.1       cjs 
     42  1.1       cjs /*
     43  1.1       cjs  * Routines which deal with the characteristics of the terminal.
     44  1.1       cjs  * Uses termcap to be as terminal-independent as possible.
     45  1.1       cjs  *
     46  1.1       cjs  * {{ Someday this should be rewritten to use curses. }}
     47  1.1       cjs  */
     48  1.1       cjs 
     49  1.1       cjs #include <stdio.h>
     50  1.2       cjs #include <string.h>
     51  1.4  christos #include <stdlib.h>
     52  1.4  christos #include <unistd.h>
     53  1.4  christos 
     54  1.4  christos #include "less.h"
     55  1.4  christos #include "extern.h"
     56  1.1       cjs 
     57  1.1       cjs #define TERMIOS 1
     58  1.1       cjs 
     59  1.1       cjs #if TERMIO
     60  1.1       cjs #include <termio.h>
     61  1.1       cjs #else
     62  1.1       cjs #if TERMIOS
     63  1.1       cjs #include <termios.h>
     64  1.1       cjs #define TAB3 0
     65  1.1       cjs #include <sys/ioctl.h>
     66  1.1       cjs #else
     67  1.1       cjs #include <sgtty.h>
     68  1.1       cjs #endif
     69  1.1       cjs #endif
     70  1.4  christos #ifdef __NetBSD__
     71  1.4  christos #include <termcap.h>
     72  1.4  christos #endif
     73  1.1       cjs 
     74  1.1       cjs #ifdef TIOCGWINSZ
     75  1.1       cjs #include <sys/ioctl.h>
     76  1.1       cjs #else
     77  1.1       cjs /*
     78  1.1       cjs  * For the Unix PC (ATT 7300 & 3B1):
     79  1.1       cjs  * Since WIOCGETD is defined in sys/window.h, we can't use that to decide
     80  1.1       cjs  * whether to include sys/window.h.  Use SIGPHONE from sys/signal.h instead.
     81  1.1       cjs  */
     82  1.1       cjs #include <sys/signal.h>
     83  1.1       cjs #ifdef SIGPHONE
     84  1.1       cjs #include <sys/window.h>
     85  1.1       cjs #endif
     86  1.1       cjs #endif
     87  1.1       cjs 
     88  1.1       cjs /*
     89  1.1       cjs  * Strings passed to tputs() to do various terminal functions.
     90  1.1       cjs  */
     91  1.1       cjs static char
     92  1.1       cjs 	*sc_pad,		/* Pad string */
     93  1.1       cjs 	*sc_home,		/* Cursor home */
     94  1.1       cjs 	*sc_addline,		/* Add line, scroll down following lines */
     95  1.1       cjs 	*sc_lower_left,		/* Cursor to last line, first column */
     96  1.1       cjs 	*sc_move,		/* General cursor positioning */
     97  1.1       cjs 	*sc_clear,		/* Clear screen */
     98  1.1       cjs 	*sc_eol_clear,		/* Clear to end of line */
     99  1.1       cjs 	*sc_s_in,		/* Enter standout (highlighted) mode */
    100  1.1       cjs 	*sc_s_out,		/* Exit standout mode */
    101  1.1       cjs 	*sc_u_in,		/* Enter underline mode */
    102  1.1       cjs 	*sc_u_out,		/* Exit underline mode */
    103  1.1       cjs 	*sc_b_in,		/* Enter bold mode */
    104  1.1       cjs 	*sc_b_out,		/* Exit bold mode */
    105  1.1       cjs 	*sc_backspace,		/* Backspace cursor */
    106  1.1       cjs 	*sc_init,		/* Startup terminal initialization */
    107  1.1       cjs 	*sc_deinit;		/* Exit terminal de-intialization */
    108  1.1       cjs 
    109  1.1       cjs int auto_wrap;			/* Terminal does \r\n when write past margin */
    110  1.1       cjs int ignaw;			/* Terminal ignores \n immediately after wrap */
    111  1.1       cjs 				/* The user's erase and line-kill chars */
    112  1.1       cjs int retain_below;		/* Terminal retains text below the screen */
    113  1.1       cjs int erase_char, kill_char, werase_char;
    114  1.1       cjs int sc_width, sc_height = -1;	/* Height & width of screen */
    115  1.1       cjs int sc_window = -1;		/* window size for forward and backward */
    116  1.1       cjs int bo_width, be_width;		/* Printing width of boldface sequences */
    117  1.1       cjs int ul_width, ue_width;		/* Printing width of underline sequences */
    118  1.1       cjs int so_width, se_width;		/* Printing width of standout sequences */
    119  1.1       cjs 
    120  1.1       cjs /*
    121  1.1       cjs  * These two variables are sometimes defined in,
    122  1.1       cjs  * and needed by, the termcap library.
    123  1.1       cjs  * It may be necessary on some systems to declare them extern here.
    124  1.1       cjs  */
    125  1.1       cjs /*extern*/ short ospeed;	/* Terminal output baud rate */
    126  1.1       cjs /*extern*/ char PC;		/* Pad character */
    127  1.1       cjs 
    128  1.1       cjs /*
    129  1.1       cjs  * Change terminal to "raw mode", or restore to "normal" mode.
    130  1.1       cjs  * "Raw mode" means
    131  1.1       cjs  *	1. An outstanding read will complete on receipt of a single keystroke.
    132  1.1       cjs  *	2. Input is not echoed.
    133  1.1       cjs  *	3. On output, \n is mapped to \r\n.
    134  1.1       cjs  *	4. \t is NOT expanded into spaces.
    135  1.1       cjs  *	5. Signal-causing characters such as ctrl-C (interrupt),
    136  1.1       cjs  *	   etc. are NOT disabled.
    137  1.1       cjs  * It doesn't matter whether an input \n is mapped to \r, or vice versa.
    138  1.1       cjs  */
    139  1.4  christos void
    140  1.1       cjs raw_mode(on)
    141  1.1       cjs 	int on;
    142  1.1       cjs {
    143  1.1       cjs #if TERMIO || TERMIOS
    144  1.1       cjs 
    145  1.1       cjs #if TERMIO
    146  1.1       cjs 	struct termio s;
    147  1.1       cjs 	static struct termio save_term;
    148  1.1       cjs #else
    149  1.1       cjs 	struct termios s;
    150  1.1       cjs 	static struct termios save_term;
    151  1.1       cjs #endif
    152  1.1       cjs 
    153  1.1       cjs 	if (on)
    154  1.1       cjs 	{
    155  1.1       cjs 		/*
    156  1.1       cjs 		 * Get terminal modes.
    157  1.1       cjs 		 */
    158  1.1       cjs #if TERMIO
    159  1.1       cjs 		(void)ioctl(2, TCGETA, &s);
    160  1.1       cjs #else
    161  1.1       cjs 		tcgetattr(2, &s);
    162  1.1       cjs #endif
    163  1.1       cjs 
    164  1.1       cjs 		/*
    165  1.1       cjs 		 * Save modes and set certain variables dependent on modes.
    166  1.1       cjs 		 */
    167  1.1       cjs 		save_term = s;
    168  1.1       cjs #if TERMIO
    169  1.1       cjs 		ospeed = s.c_cflag & CBAUD;
    170  1.1       cjs #else
    171  1.1       cjs 		ospeed = cfgetospeed(&s);
    172  1.1       cjs #endif
    173  1.1       cjs 		erase_char = s.c_cc[VERASE];
    174  1.1       cjs 		kill_char = s.c_cc[VKILL];
    175  1.1       cjs 		werase_char = s.c_cc[VWERASE];
    176  1.1       cjs 
    177  1.1       cjs 		/*
    178  1.1       cjs 		 * Set the modes to the way we want them.
    179  1.1       cjs 		 */
    180  1.1       cjs 		s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
    181  1.1       cjs 		s.c_oflag |=  (OPOST|ONLCR|TAB3);
    182  1.1       cjs #if TERMIO
    183  1.1       cjs 		s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
    184  1.1       cjs #endif
    185  1.1       cjs 		s.c_cc[VMIN] = 1;
    186  1.1       cjs 		s.c_cc[VTIME] = 0;
    187  1.1       cjs 	} else
    188  1.1       cjs 	{
    189  1.1       cjs 		/*
    190  1.1       cjs 		 * Restore saved modes.
    191  1.1       cjs 		 */
    192  1.1       cjs 		s = save_term;
    193  1.1       cjs 	}
    194  1.1       cjs #if TERMIO
    195  1.1       cjs 	(void)ioctl(2, TCSETAW, &s);
    196  1.1       cjs #else
    197  1.1       cjs 	tcsetattr(2, TCSADRAIN, &s);
    198  1.1       cjs #endif
    199  1.1       cjs #else
    200  1.1       cjs 	struct sgttyb s;
    201  1.1       cjs 	struct ltchars l;
    202  1.1       cjs 	static struct sgttyb save_term;
    203  1.1       cjs 
    204  1.1       cjs 	if (on)
    205  1.1       cjs 	{
    206  1.1       cjs 		/*
    207  1.1       cjs 		 * Get terminal modes.
    208  1.1       cjs 		 */
    209  1.1       cjs 		(void)ioctl(2, TIOCGETP, &s);
    210  1.1       cjs 		(void)ioctl(2, TIOCGLTC, &l);
    211  1.1       cjs 
    212  1.1       cjs 		/*
    213  1.1       cjs 		 * Save modes and set certain variables dependent on modes.
    214  1.1       cjs 		 */
    215  1.1       cjs 		save_term = s;
    216  1.1       cjs 		ospeed = s.sg_ospeed;
    217  1.1       cjs 		erase_char = s.sg_erase;
    218  1.1       cjs 		kill_char = s.sg_kill;
    219  1.1       cjs 		werase_char = l.t_werasc;
    220  1.1       cjs 
    221  1.1       cjs 		/*
    222  1.1       cjs 		 * Set the modes to the way we want them.
    223  1.1       cjs 		 */
    224  1.1       cjs 		s.sg_flags |= CBREAK;
    225  1.1       cjs 		s.sg_flags &= ~(ECHO|XTABS);
    226  1.1       cjs 	} else
    227  1.1       cjs 	{
    228  1.1       cjs 		/*
    229  1.1       cjs 		 * Restore saved modes.
    230  1.1       cjs 		 */
    231  1.1       cjs 		s = save_term;
    232  1.1       cjs 	}
    233  1.1       cjs 	(void)ioctl(2, TIOCSETN, &s);
    234  1.1       cjs #endif
    235  1.1       cjs }
    236  1.1       cjs 
    237  1.1       cjs /*
    238  1.1       cjs  * Get terminal capabilities via termcap.
    239  1.1       cjs  */
    240  1.4  christos void
    241  1.1       cjs get_term()
    242  1.1       cjs {
    243  1.1       cjs 	char termbuf[2048];
    244  1.1       cjs 	char *sp;
    245  1.1       cjs 	char *term;
    246  1.1       cjs 	int hard;
    247  1.1       cjs #ifdef TIOCGWINSZ
    248  1.1       cjs 	struct winsize w;
    249  1.1       cjs #else
    250  1.1       cjs #ifdef WIOCGETD
    251  1.1       cjs 	struct uwdata w;
    252  1.1       cjs #endif
    253  1.1       cjs #endif
    254  1.1       cjs 	static char sbuf[1024];
    255  1.1       cjs 
    256  1.1       cjs 	/*
    257  1.1       cjs 	 * Find out what kind of terminal this is.
    258  1.1       cjs 	 */
    259  1.1       cjs  	if ((term = getenv("TERM")) == NULL)
    260  1.1       cjs  		term = "unknown";
    261  1.1       cjs  	if (tgetent(termbuf, term) <= 0)
    262  1.5    itojun  		(void)strlcpy(termbuf, "dumb:co#80:hc:", sizeof(termbuf));
    263  1.1       cjs 
    264  1.1       cjs 	/*
    265  1.1       cjs 	 * Get size of the screen.
    266  1.1       cjs 	 */
    267  1.1       cjs #ifdef TIOCGWINSZ
    268  1.1       cjs 	if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_row > 0)
    269  1.1       cjs 		sc_height = w.ws_row;
    270  1.1       cjs #else
    271  1.1       cjs #ifdef WIOCGETD
    272  1.1       cjs 	if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_height > 0)
    273  1.1       cjs 		sc_height = w.uw_height/w.uw_vs;
    274  1.1       cjs #endif
    275  1.1       cjs #endif
    276  1.1       cjs 	else
    277  1.1       cjs 		sc_height = tgetnum("li");
    278  1.1       cjs 	hard = (sc_height < 0 || tgetflag("hc"));
    279  1.1       cjs 	if (hard) {
    280  1.1       cjs 		/* Oh no, this is a hardcopy terminal. */
    281  1.1       cjs 		sc_height = 24;
    282  1.1       cjs 	}
    283  1.1       cjs 
    284  1.1       cjs #ifdef TIOCGWINSZ
    285  1.1       cjs  	if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
    286  1.1       cjs 		sc_width = w.ws_col;
    287  1.1       cjs 	else
    288  1.1       cjs #ifdef WIOCGETD
    289  1.1       cjs 	if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_width > 0)
    290  1.1       cjs 		sc_width = w.uw_width/w.uw_hs;
    291  1.1       cjs 	else
    292  1.1       cjs #endif
    293  1.1       cjs #endif
    294  1.1       cjs  		sc_width = tgetnum("co");
    295  1.1       cjs  	if (sc_width < 0)
    296  1.1       cjs   		sc_width = 80;
    297  1.1       cjs 
    298  1.1       cjs 	auto_wrap = tgetflag("am");
    299  1.1       cjs 	ignaw = tgetflag("xn");
    300  1.1       cjs 	retain_below = tgetflag("db");
    301  1.1       cjs 
    302  1.1       cjs 	/*
    303  1.1       cjs 	 * Assumes termcap variable "sg" is the printing width of
    304  1.1       cjs 	 * the standout sequence, the end standout sequence,
    305  1.1       cjs 	 * the underline sequence, the end underline sequence,
    306  1.1       cjs 	 * the boldface sequence, and the end boldface sequence.
    307  1.1       cjs 	 */
    308  1.1       cjs 	if ((so_width = tgetnum("sg")) < 0)
    309  1.1       cjs 		so_width = 0;
    310  1.1       cjs 	be_width = bo_width = ue_width = ul_width = se_width = so_width;
    311  1.1       cjs 
    312  1.1       cjs 	/*
    313  1.1       cjs 	 * Get various string-valued capabilities.
    314  1.1       cjs 	 */
    315  1.1       cjs 	sp = sbuf;
    316  1.1       cjs 
    317  1.1       cjs 	sc_pad = tgetstr("pc", &sp);
    318  1.1       cjs 	if (sc_pad != NULL)
    319  1.1       cjs 		PC = *sc_pad;
    320  1.1       cjs 
    321  1.1       cjs 	sc_init = tgetstr("ti", &sp);
    322  1.1       cjs 	if (sc_init == NULL)
    323  1.1       cjs 		sc_init = "";
    324  1.1       cjs 
    325  1.1       cjs 	sc_deinit= tgetstr("te", &sp);
    326  1.1       cjs 	if (sc_deinit == NULL)
    327  1.1       cjs 		sc_deinit = "";
    328  1.1       cjs 
    329  1.1       cjs 	sc_eol_clear = tgetstr("ce", &sp);
    330  1.1       cjs 	if (hard || sc_eol_clear == NULL || *sc_eol_clear == '\0')
    331  1.1       cjs 	{
    332  1.1       cjs 		sc_eol_clear = "";
    333  1.1       cjs 	}
    334  1.1       cjs 
    335  1.1       cjs 	sc_clear = tgetstr("cl", &sp);
    336  1.1       cjs 	if (hard || sc_clear == NULL || *sc_clear == '\0')
    337  1.1       cjs 	{
    338  1.1       cjs 		sc_clear = "\n\n";
    339  1.1       cjs 	}
    340  1.1       cjs 
    341  1.1       cjs 	sc_move = tgetstr("cm", &sp);
    342  1.1       cjs 	if (hard || sc_move == NULL || *sc_move == '\0')
    343  1.1       cjs 	{
    344  1.1       cjs 		/*
    345  1.1       cjs 		 * This is not an error here, because we don't
    346  1.1       cjs 		 * always need sc_move.
    347  1.1       cjs 		 * We need it only if we don't have home or lower-left.
    348  1.1       cjs 		 */
    349  1.1       cjs 		sc_move = "";
    350  1.1       cjs 	}
    351  1.1       cjs 
    352  1.1       cjs 	sc_s_in = tgetstr("so", &sp);
    353  1.1       cjs 	if (hard || sc_s_in == NULL)
    354  1.1       cjs 		sc_s_in = "";
    355  1.1       cjs 
    356  1.1       cjs 	sc_s_out = tgetstr("se", &sp);
    357  1.1       cjs 	if (hard || sc_s_out == NULL)
    358  1.1       cjs 		sc_s_out = "";
    359  1.1       cjs 
    360  1.1       cjs 	sc_u_in = tgetstr("us", &sp);
    361  1.1       cjs 	if (hard || sc_u_in == NULL)
    362  1.1       cjs 		sc_u_in = sc_s_in;
    363  1.1       cjs 
    364  1.1       cjs 	sc_u_out = tgetstr("ue", &sp);
    365  1.1       cjs 	if (hard || sc_u_out == NULL)
    366  1.1       cjs 		sc_u_out = sc_s_out;
    367  1.1       cjs 
    368  1.1       cjs 	sc_b_in = tgetstr("md", &sp);
    369  1.1       cjs 	if (hard || sc_b_in == NULL)
    370  1.1       cjs 	{
    371  1.1       cjs 		sc_b_in = sc_s_in;
    372  1.1       cjs 		sc_b_out = sc_s_out;
    373  1.1       cjs 	} else
    374  1.1       cjs 	{
    375  1.1       cjs 		sc_b_out = tgetstr("me", &sp);
    376  1.1       cjs 		if (hard || sc_b_out == NULL)
    377  1.1       cjs 			sc_b_out = "";
    378  1.1       cjs 	}
    379  1.1       cjs 
    380  1.1       cjs 	sc_home = tgetstr("ho", &sp);
    381  1.1       cjs 	if (hard || sc_home == NULL || *sc_home == '\0')
    382  1.1       cjs 	{
    383  1.1       cjs 		if (*sc_move == '\0')
    384  1.1       cjs 		{
    385  1.1       cjs 			/*
    386  1.1       cjs 			 * This last resort for sc_home is supposed to
    387  1.1       cjs 			 * be an up-arrow suggesting moving to the
    388  1.1       cjs 			 * top of the "virtual screen". (The one in
    389  1.1       cjs 			 * your imagination as you try to use this on
    390  1.1       cjs 			 * a hard copy terminal.)
    391  1.1       cjs 			 */
    392  1.1       cjs 			sc_home = "|\b^";
    393  1.1       cjs 		} else
    394  1.1       cjs 		{
    395  1.1       cjs 			/*
    396  1.1       cjs 			 * No "home" string,
    397  1.1       cjs 			 * but we can use "move(0,0)".
    398  1.1       cjs 			 */
    399  1.5    itojun 			(void)strlcpy(sp, tgoto(sc_move, 0, 0),
    400  1.5    itojun 			    sizeof(sbuf) - (sp - sbuf));
    401  1.1       cjs 			sc_home = sp;
    402  1.1       cjs 			sp += strlen(sp) + 1;
    403  1.1       cjs 		}
    404  1.1       cjs 	}
    405  1.1       cjs 
    406  1.1       cjs 	sc_lower_left = tgetstr("ll", &sp);
    407  1.1       cjs 	if (hard || sc_lower_left == NULL || *sc_lower_left == '\0')
    408  1.1       cjs 	{
    409  1.1       cjs 		if (*sc_move == '\0')
    410  1.1       cjs 		{
    411  1.1       cjs 			sc_lower_left = "\r";
    412  1.1       cjs 		} else
    413  1.1       cjs 		{
    414  1.1       cjs 			/*
    415  1.1       cjs 			 * No "lower-left" string,
    416  1.1       cjs 			 * but we can use "move(0,last-line)".
    417  1.1       cjs 			 */
    418  1.5    itojun 			(void)strlcpy(sp, tgoto(sc_move, 0, sc_height-1),
    419  1.5    itojun 			    sizeof(sbuf) - (sp - sbuf));
    420  1.1       cjs 			sc_lower_left = sp;
    421  1.1       cjs 			sp += strlen(sp) + 1;
    422  1.1       cjs 		}
    423  1.1       cjs 	}
    424  1.1       cjs 
    425  1.1       cjs 	/*
    426  1.1       cjs 	 * To add a line at top of screen and scroll the display down,
    427  1.1       cjs 	 * we use "al" (add line) or "sr" (scroll reverse).
    428  1.1       cjs 	 */
    429  1.1       cjs 	if ((sc_addline = tgetstr("al", &sp)) == NULL ||
    430  1.1       cjs 		 *sc_addline == '\0')
    431  1.1       cjs 		sc_addline = tgetstr("sr", &sp);
    432  1.1       cjs 
    433  1.1       cjs 	if (hard || sc_addline == NULL || *sc_addline == '\0')
    434  1.1       cjs 	{
    435  1.1       cjs 		sc_addline = "";
    436  1.1       cjs 		/* Force repaint on any backward movement */
    437  1.1       cjs 		back_scroll = 0;
    438  1.1       cjs 	}
    439  1.1       cjs 
    440  1.1       cjs 	if (tgetflag("bs"))
    441  1.1       cjs 		sc_backspace = "\b";
    442  1.1       cjs 	else
    443  1.1       cjs 	{
    444  1.1       cjs 		sc_backspace = tgetstr("bc", &sp);
    445  1.1       cjs 		if (sc_backspace == NULL || *sc_backspace == '\0')
    446  1.1       cjs 			sc_backspace = "\b";
    447  1.1       cjs 	}
    448  1.1       cjs }
    449  1.1       cjs 
    450  1.1       cjs 
    451  1.1       cjs /*
    452  1.1       cjs  * Below are the functions which perform all the
    453  1.1       cjs  * terminal-specific screen manipulation.
    454  1.1       cjs  */
    455  1.1       cjs 
    456  1.1       cjs /*
    457  1.1       cjs  * Initialize terminal
    458  1.1       cjs  */
    459  1.4  christos void
    460  1.1       cjs init()
    461  1.1       cjs {
    462  1.1       cjs 	tputs(sc_init, sc_height, putchr);
    463  1.1       cjs }
    464  1.1       cjs 
    465  1.1       cjs /*
    466  1.1       cjs  * Deinitialize terminal
    467  1.1       cjs  */
    468  1.4  christos void
    469  1.1       cjs deinit()
    470  1.1       cjs {
    471  1.1       cjs 	tputs(sc_deinit, sc_height, putchr);
    472  1.1       cjs }
    473  1.1       cjs 
    474  1.1       cjs /*
    475  1.1       cjs  * Home cursor (move to upper left corner of screen).
    476  1.1       cjs  */
    477  1.4  christos void
    478  1.1       cjs home()
    479  1.1       cjs {
    480  1.1       cjs 	tputs(sc_home, 1, putchr);
    481  1.1       cjs }
    482  1.1       cjs 
    483  1.1       cjs /*
    484  1.1       cjs  * Add a blank line (called with cursor at home).
    485  1.1       cjs  * Should scroll the display down.
    486  1.1       cjs  */
    487  1.4  christos void
    488  1.1       cjs add_line()
    489  1.1       cjs {
    490  1.1       cjs 	tputs(sc_addline, sc_height, putchr);
    491  1.1       cjs }
    492  1.1       cjs 
    493  1.1       cjs int short_file;				/* if file less than a screen */
    494  1.4  christos void
    495  1.1       cjs lower_left()
    496  1.1       cjs {
    497  1.1       cjs 	if (short_file) {
    498  1.1       cjs 		putchr('\r');
    499  1.1       cjs 		flush();
    500  1.1       cjs 	}
    501  1.1       cjs 	else
    502  1.1       cjs 		tputs(sc_lower_left, 1, putchr);
    503  1.1       cjs }
    504  1.1       cjs 
    505  1.1       cjs /*
    506  1.1       cjs  * Ring the terminal bell.
    507  1.1       cjs  */
    508  1.4  christos void
    509  1.1       cjs bell()
    510  1.1       cjs {
    511  1.1       cjs 	putchr('\7');
    512  1.1       cjs }
    513  1.1       cjs 
    514  1.1       cjs /*
    515  1.1       cjs  * Clear the screen.
    516  1.1       cjs  */
    517  1.4  christos void
    518  1.1       cjs clear()
    519  1.1       cjs {
    520  1.1       cjs 	tputs(sc_clear, sc_height, putchr);
    521  1.1       cjs }
    522  1.1       cjs 
    523  1.1       cjs /*
    524  1.1       cjs  * Clear from the cursor to the end of the cursor's line.
    525  1.1       cjs  * {{ This must not move the cursor. }}
    526  1.1       cjs  */
    527  1.4  christos void
    528  1.1       cjs clear_eol()
    529  1.1       cjs {
    530  1.1       cjs 	tputs(sc_eol_clear, 1, putchr);
    531  1.1       cjs }
    532  1.1       cjs 
    533  1.1       cjs /*
    534  1.1       cjs  * Begin "standout" (bold, underline, or whatever).
    535  1.1       cjs  */
    536  1.4  christos void
    537  1.1       cjs so_enter()
    538  1.1       cjs {
    539  1.1       cjs 	tputs(sc_s_in, 1, putchr);
    540  1.1       cjs }
    541  1.1       cjs 
    542  1.1       cjs /*
    543  1.1       cjs  * End "standout".
    544  1.1       cjs  */
    545  1.4  christos void
    546  1.1       cjs so_exit()
    547  1.1       cjs {
    548  1.1       cjs 	tputs(sc_s_out, 1, putchr);
    549  1.1       cjs }
    550  1.1       cjs 
    551  1.1       cjs /*
    552  1.1       cjs  * Begin "underline" (hopefully real underlining,
    553  1.1       cjs  * otherwise whatever the terminal provides).
    554  1.1       cjs  */
    555  1.4  christos void
    556  1.1       cjs ul_enter()
    557  1.1       cjs {
    558  1.1       cjs 	tputs(sc_u_in, 1, putchr);
    559  1.1       cjs }
    560  1.1       cjs 
    561  1.1       cjs /*
    562  1.1       cjs  * End "underline".
    563  1.1       cjs  */
    564  1.4  christos void
    565  1.1       cjs ul_exit()
    566  1.1       cjs {
    567  1.1       cjs 	tputs(sc_u_out, 1, putchr);
    568  1.1       cjs }
    569  1.1       cjs 
    570  1.1       cjs /*
    571  1.1       cjs  * Begin "bold"
    572  1.1       cjs  */
    573  1.4  christos void
    574  1.1       cjs bo_enter()
    575  1.1       cjs {
    576  1.1       cjs 	tputs(sc_b_in, 1, putchr);
    577  1.1       cjs }
    578  1.1       cjs 
    579  1.1       cjs /*
    580  1.1       cjs  * End "bold".
    581  1.1       cjs  */
    582  1.4  christos void
    583  1.1       cjs bo_exit()
    584  1.1       cjs {
    585  1.1       cjs 	tputs(sc_b_out, 1, putchr);
    586  1.1       cjs }
    587  1.1       cjs 
    588  1.1       cjs /*
    589  1.1       cjs  * Erase the character to the left of the cursor
    590  1.1       cjs  * and move the cursor left.
    591  1.1       cjs  */
    592  1.4  christos void
    593  1.1       cjs backspace()
    594  1.1       cjs {
    595  1.1       cjs 	/*
    596  1.1       cjs 	 * Try to erase the previous character by overstriking with a space.
    597  1.1       cjs 	 */
    598  1.1       cjs 	tputs(sc_backspace, 1, putchr);
    599  1.1       cjs 	putchr(' ');
    600  1.1       cjs 	tputs(sc_backspace, 1, putchr);
    601  1.1       cjs }
    602  1.1       cjs 
    603  1.1       cjs /*
    604  1.1       cjs  * Output a plain backspace, without erasing the previous char.
    605  1.1       cjs  */
    606  1.4  christos void
    607  1.1       cjs putbs()
    608  1.1       cjs {
    609  1.1       cjs 	tputs(sc_backspace, 1, putchr);
    610  1.1       cjs }
    611