xref: /netbsd/distrib/utils/more/screen.c (revision 61470ee0)
1*61470ee0Sagc /*	$NetBSD: screen.c,v 1.7 2003/10/13 14:34:25 agc Exp $	*/
274b7d6e9Sagc 
374b7d6e9Sagc /*
4*61470ee0Sagc  * Copyright (c) 1988 Mark Nudelman
574b7d6e9Sagc  * Copyright (c) 1988, 1993
674b7d6e9Sagc  *	The Regents of the University of California.  All rights reserved.
774b7d6e9Sagc  *
874b7d6e9Sagc  * Redistribution and use in source and binary forms, with or without
974b7d6e9Sagc  * modification, are permitted provided that the following conditions
1074b7d6e9Sagc  * are met:
1174b7d6e9Sagc  * 1. Redistributions of source code must retain the above copyright
1274b7d6e9Sagc  *    notice, this list of conditions and the following disclaimer.
1374b7d6e9Sagc  * 2. Redistributions in binary form must reproduce the above copyright
1474b7d6e9Sagc  *    notice, this list of conditions and the following disclaimer in the
1574b7d6e9Sagc  *    documentation and/or other materials provided with the distribution.
1674b7d6e9Sagc  * 3. Neither the name of the University nor the names of its contributors
1774b7d6e9Sagc  *    may be used to endorse or promote products derived from this software
1874b7d6e9Sagc  *    without specific prior written permission.
1974b7d6e9Sagc  *
2074b7d6e9Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2174b7d6e9Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2274b7d6e9Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2374b7d6e9Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2474b7d6e9Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2574b7d6e9Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2674b7d6e9Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2774b7d6e9Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2874b7d6e9Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2974b7d6e9Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3074b7d6e9Sagc  * SUCH DAMAGE.
3174b7d6e9Sagc  */
323fe138c1Sperry 
33e3fbe7d2Schristos #include <sys/cdefs.h>
34963d5659Scjs #ifndef lint
35e3fbe7d2Schristos #if 0
36963d5659Scjs static char sccsid[] = "@(#)screen.c	8.2 (Berkeley) 4/20/94";
37e3fbe7d2Schristos #else
38*61470ee0Sagc __RCSID("$NetBSD: screen.c,v 1.7 2003/10/13 14:34:25 agc Exp $");
39e3fbe7d2Schristos #endif
40963d5659Scjs #endif /* not lint */
41963d5659Scjs 
42963d5659Scjs /*
43963d5659Scjs  * Routines which deal with the characteristics of the terminal.
44963d5659Scjs  * Uses termcap to be as terminal-independent as possible.
45963d5659Scjs  *
46963d5659Scjs  * {{ Someday this should be rewritten to use curses. }}
47963d5659Scjs  */
48963d5659Scjs 
49963d5659Scjs #include <stdio.h>
506a623f72Scjs #include <string.h>
51e3fbe7d2Schristos #include <stdlib.h>
52e3fbe7d2Schristos #include <unistd.h>
53e3fbe7d2Schristos 
54e3fbe7d2Schristos #include "less.h"
55e3fbe7d2Schristos #include "extern.h"
56963d5659Scjs 
57963d5659Scjs #define TERMIOS 1
58963d5659Scjs 
59963d5659Scjs #if TERMIO
60963d5659Scjs #include <termio.h>
61963d5659Scjs #else
62963d5659Scjs #if TERMIOS
63963d5659Scjs #include <termios.h>
64963d5659Scjs #define TAB3 0
65963d5659Scjs #include <sys/ioctl.h>
66963d5659Scjs #else
67963d5659Scjs #include <sgtty.h>
68963d5659Scjs #endif
69963d5659Scjs #endif
70e3fbe7d2Schristos #ifdef __NetBSD__
71e3fbe7d2Schristos #include <termcap.h>
72e3fbe7d2Schristos #endif
73963d5659Scjs 
74963d5659Scjs #ifdef TIOCGWINSZ
75963d5659Scjs #include <sys/ioctl.h>
76963d5659Scjs #else
77963d5659Scjs /*
78963d5659Scjs  * For the Unix PC (ATT 7300 & 3B1):
79963d5659Scjs  * Since WIOCGETD is defined in sys/window.h, we can't use that to decide
80963d5659Scjs  * whether to include sys/window.h.  Use SIGPHONE from sys/signal.h instead.
81963d5659Scjs  */
82963d5659Scjs #include <sys/signal.h>
83963d5659Scjs #ifdef SIGPHONE
84963d5659Scjs #include <sys/window.h>
85963d5659Scjs #endif
86963d5659Scjs #endif
87963d5659Scjs 
88963d5659Scjs /*
89963d5659Scjs  * Strings passed to tputs() to do various terminal functions.
90963d5659Scjs  */
91963d5659Scjs static char
92963d5659Scjs 	*sc_pad,		/* Pad string */
93963d5659Scjs 	*sc_home,		/* Cursor home */
94963d5659Scjs 	*sc_addline,		/* Add line, scroll down following lines */
95963d5659Scjs 	*sc_lower_left,		/* Cursor to last line, first column */
96963d5659Scjs 	*sc_move,		/* General cursor positioning */
97963d5659Scjs 	*sc_clear,		/* Clear screen */
98963d5659Scjs 	*sc_eol_clear,		/* Clear to end of line */
99963d5659Scjs 	*sc_s_in,		/* Enter standout (highlighted) mode */
100963d5659Scjs 	*sc_s_out,		/* Exit standout mode */
101963d5659Scjs 	*sc_u_in,		/* Enter underline mode */
102963d5659Scjs 	*sc_u_out,		/* Exit underline mode */
103963d5659Scjs 	*sc_b_in,		/* Enter bold mode */
104963d5659Scjs 	*sc_b_out,		/* Exit bold mode */
105963d5659Scjs 	*sc_backspace,		/* Backspace cursor */
106963d5659Scjs 	*sc_init,		/* Startup terminal initialization */
107963d5659Scjs 	*sc_deinit;		/* Exit terminal de-intialization */
108963d5659Scjs 
109963d5659Scjs int auto_wrap;			/* Terminal does \r\n when write past margin */
110963d5659Scjs int ignaw;			/* Terminal ignores \n immediately after wrap */
111963d5659Scjs 				/* The user's erase and line-kill chars */
112963d5659Scjs int retain_below;		/* Terminal retains text below the screen */
113963d5659Scjs int erase_char, kill_char, werase_char;
114963d5659Scjs int sc_width, sc_height = -1;	/* Height & width of screen */
115963d5659Scjs int sc_window = -1;		/* window size for forward and backward */
116963d5659Scjs int bo_width, be_width;		/* Printing width of boldface sequences */
117963d5659Scjs int ul_width, ue_width;		/* Printing width of underline sequences */
118963d5659Scjs int so_width, se_width;		/* Printing width of standout sequences */
119963d5659Scjs 
120963d5659Scjs /*
121963d5659Scjs  * These two variables are sometimes defined in,
122963d5659Scjs  * and needed by, the termcap library.
123963d5659Scjs  * It may be necessary on some systems to declare them extern here.
124963d5659Scjs  */
125963d5659Scjs /*extern*/ short ospeed;	/* Terminal output baud rate */
126963d5659Scjs /*extern*/ char PC;		/* Pad character */
127963d5659Scjs 
128963d5659Scjs /*
129963d5659Scjs  * Change terminal to "raw mode", or restore to "normal" mode.
130963d5659Scjs  * "Raw mode" means
131963d5659Scjs  *	1. An outstanding read will complete on receipt of a single keystroke.
132963d5659Scjs  *	2. Input is not echoed.
133963d5659Scjs  *	3. On output, \n is mapped to \r\n.
134963d5659Scjs  *	4. \t is NOT expanded into spaces.
135963d5659Scjs  *	5. Signal-causing characters such as ctrl-C (interrupt),
136963d5659Scjs  *	   etc. are NOT disabled.
137963d5659Scjs  * It doesn't matter whether an input \n is mapped to \r, or vice versa.
138963d5659Scjs  */
139e3fbe7d2Schristos void
raw_mode(on)140963d5659Scjs raw_mode(on)
141963d5659Scjs 	int on;
142963d5659Scjs {
143963d5659Scjs #if TERMIO || TERMIOS
144963d5659Scjs 
145963d5659Scjs #if TERMIO
146963d5659Scjs 	struct termio s;
147963d5659Scjs 	static struct termio save_term;
148963d5659Scjs #else
149963d5659Scjs 	struct termios s;
150963d5659Scjs 	static struct termios save_term;
151963d5659Scjs #endif
152963d5659Scjs 
153963d5659Scjs 	if (on)
154963d5659Scjs 	{
155963d5659Scjs 		/*
156963d5659Scjs 		 * Get terminal modes.
157963d5659Scjs 		 */
158963d5659Scjs #if TERMIO
159963d5659Scjs 		(void)ioctl(2, TCGETA, &s);
160963d5659Scjs #else
161963d5659Scjs 		tcgetattr(2, &s);
162963d5659Scjs #endif
163963d5659Scjs 
164963d5659Scjs 		/*
165963d5659Scjs 		 * Save modes and set certain variables dependent on modes.
166963d5659Scjs 		 */
167963d5659Scjs 		save_term = s;
168963d5659Scjs #if TERMIO
169963d5659Scjs 		ospeed = s.c_cflag & CBAUD;
170963d5659Scjs #else
171963d5659Scjs 		ospeed = cfgetospeed(&s);
172963d5659Scjs #endif
173963d5659Scjs 		erase_char = s.c_cc[VERASE];
174963d5659Scjs 		kill_char = s.c_cc[VKILL];
175963d5659Scjs 		werase_char = s.c_cc[VWERASE];
176963d5659Scjs 
177963d5659Scjs 		/*
178963d5659Scjs 		 * Set the modes to the way we want them.
179963d5659Scjs 		 */
180963d5659Scjs 		s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
181963d5659Scjs 		s.c_oflag |=  (OPOST|ONLCR|TAB3);
182963d5659Scjs #if TERMIO
183963d5659Scjs 		s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
184963d5659Scjs #endif
185963d5659Scjs 		s.c_cc[VMIN] = 1;
186963d5659Scjs 		s.c_cc[VTIME] = 0;
187963d5659Scjs 	} else
188963d5659Scjs 	{
189963d5659Scjs 		/*
190963d5659Scjs 		 * Restore saved modes.
191963d5659Scjs 		 */
192963d5659Scjs 		s = save_term;
193963d5659Scjs 	}
194963d5659Scjs #if TERMIO
195963d5659Scjs 	(void)ioctl(2, TCSETAW, &s);
196963d5659Scjs #else
197963d5659Scjs 	tcsetattr(2, TCSADRAIN, &s);
198963d5659Scjs #endif
199963d5659Scjs #else
200963d5659Scjs 	struct sgttyb s;
201963d5659Scjs 	struct ltchars l;
202963d5659Scjs 	static struct sgttyb save_term;
203963d5659Scjs 
204963d5659Scjs 	if (on)
205963d5659Scjs 	{
206963d5659Scjs 		/*
207963d5659Scjs 		 * Get terminal modes.
208963d5659Scjs 		 */
209963d5659Scjs 		(void)ioctl(2, TIOCGETP, &s);
210963d5659Scjs 		(void)ioctl(2, TIOCGLTC, &l);
211963d5659Scjs 
212963d5659Scjs 		/*
213963d5659Scjs 		 * Save modes and set certain variables dependent on modes.
214963d5659Scjs 		 */
215963d5659Scjs 		save_term = s;
216963d5659Scjs 		ospeed = s.sg_ospeed;
217963d5659Scjs 		erase_char = s.sg_erase;
218963d5659Scjs 		kill_char = s.sg_kill;
219963d5659Scjs 		werase_char = l.t_werasc;
220963d5659Scjs 
221963d5659Scjs 		/*
222963d5659Scjs 		 * Set the modes to the way we want them.
223963d5659Scjs 		 */
224963d5659Scjs 		s.sg_flags |= CBREAK;
225963d5659Scjs 		s.sg_flags &= ~(ECHO|XTABS);
226963d5659Scjs 	} else
227963d5659Scjs 	{
228963d5659Scjs 		/*
229963d5659Scjs 		 * Restore saved modes.
230963d5659Scjs 		 */
231963d5659Scjs 		s = save_term;
232963d5659Scjs 	}
233963d5659Scjs 	(void)ioctl(2, TIOCSETN, &s);
234963d5659Scjs #endif
235963d5659Scjs }
236963d5659Scjs 
237963d5659Scjs /*
238963d5659Scjs  * Get terminal capabilities via termcap.
239963d5659Scjs  */
240e3fbe7d2Schristos void
get_term()241963d5659Scjs get_term()
242963d5659Scjs {
243963d5659Scjs 	char termbuf[2048];
244963d5659Scjs 	char *sp;
245963d5659Scjs 	char *term;
246963d5659Scjs 	int hard;
247963d5659Scjs #ifdef TIOCGWINSZ
248963d5659Scjs 	struct winsize w;
249963d5659Scjs #else
250963d5659Scjs #ifdef WIOCGETD
251963d5659Scjs 	struct uwdata w;
252963d5659Scjs #endif
253963d5659Scjs #endif
254963d5659Scjs 	static char sbuf[1024];
255963d5659Scjs 
256963d5659Scjs 	/*
257963d5659Scjs 	 * Find out what kind of terminal this is.
258963d5659Scjs 	 */
259963d5659Scjs  	if ((term = getenv("TERM")) == NULL)
260963d5659Scjs  		term = "unknown";
261963d5659Scjs  	if (tgetent(termbuf, term) <= 0)
262cad152eeSitojun  		(void)strlcpy(termbuf, "dumb:co#80:hc:", sizeof(termbuf));
263963d5659Scjs 
264963d5659Scjs 	/*
265963d5659Scjs 	 * Get size of the screen.
266963d5659Scjs 	 */
267963d5659Scjs #ifdef TIOCGWINSZ
268963d5659Scjs 	if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_row > 0)
269963d5659Scjs 		sc_height = w.ws_row;
270963d5659Scjs #else
271963d5659Scjs #ifdef WIOCGETD
272963d5659Scjs 	if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_height > 0)
273963d5659Scjs 		sc_height = w.uw_height/w.uw_vs;
274963d5659Scjs #endif
275963d5659Scjs #endif
276963d5659Scjs 	else
277963d5659Scjs 		sc_height = tgetnum("li");
278963d5659Scjs 	hard = (sc_height < 0 || tgetflag("hc"));
279963d5659Scjs 	if (hard) {
280963d5659Scjs 		/* Oh no, this is a hardcopy terminal. */
281963d5659Scjs 		sc_height = 24;
282963d5659Scjs 	}
283963d5659Scjs 
284963d5659Scjs #ifdef TIOCGWINSZ
285963d5659Scjs  	if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
286963d5659Scjs 		sc_width = w.ws_col;
287963d5659Scjs 	else
288963d5659Scjs #ifdef WIOCGETD
289963d5659Scjs 	if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_width > 0)
290963d5659Scjs 		sc_width = w.uw_width/w.uw_hs;
291963d5659Scjs 	else
292963d5659Scjs #endif
293963d5659Scjs #endif
294963d5659Scjs  		sc_width = tgetnum("co");
295963d5659Scjs  	if (sc_width < 0)
296963d5659Scjs   		sc_width = 80;
297963d5659Scjs 
298963d5659Scjs 	auto_wrap = tgetflag("am");
299963d5659Scjs 	ignaw = tgetflag("xn");
300963d5659Scjs 	retain_below = tgetflag("db");
301963d5659Scjs 
302963d5659Scjs 	/*
303963d5659Scjs 	 * Assumes termcap variable "sg" is the printing width of
304963d5659Scjs 	 * the standout sequence, the end standout sequence,
305963d5659Scjs 	 * the underline sequence, the end underline sequence,
306963d5659Scjs 	 * the boldface sequence, and the end boldface sequence.
307963d5659Scjs 	 */
308963d5659Scjs 	if ((so_width = tgetnum("sg")) < 0)
309963d5659Scjs 		so_width = 0;
310963d5659Scjs 	be_width = bo_width = ue_width = ul_width = se_width = so_width;
311963d5659Scjs 
312963d5659Scjs 	/*
313963d5659Scjs 	 * Get various string-valued capabilities.
314963d5659Scjs 	 */
315963d5659Scjs 	sp = sbuf;
316963d5659Scjs 
317963d5659Scjs 	sc_pad = tgetstr("pc", &sp);
318963d5659Scjs 	if (sc_pad != NULL)
319963d5659Scjs 		PC = *sc_pad;
320963d5659Scjs 
321963d5659Scjs 	sc_init = tgetstr("ti", &sp);
322963d5659Scjs 	if (sc_init == NULL)
323963d5659Scjs 		sc_init = "";
324963d5659Scjs 
325963d5659Scjs 	sc_deinit= tgetstr("te", &sp);
326963d5659Scjs 	if (sc_deinit == NULL)
327963d5659Scjs 		sc_deinit = "";
328963d5659Scjs 
329963d5659Scjs 	sc_eol_clear = tgetstr("ce", &sp);
330963d5659Scjs 	if (hard || sc_eol_clear == NULL || *sc_eol_clear == '\0')
331963d5659Scjs 	{
332963d5659Scjs 		sc_eol_clear = "";
333963d5659Scjs 	}
334963d5659Scjs 
335963d5659Scjs 	sc_clear = tgetstr("cl", &sp);
336963d5659Scjs 	if (hard || sc_clear == NULL || *sc_clear == '\0')
337963d5659Scjs 	{
338963d5659Scjs 		sc_clear = "\n\n";
339963d5659Scjs 	}
340963d5659Scjs 
341963d5659Scjs 	sc_move = tgetstr("cm", &sp);
342963d5659Scjs 	if (hard || sc_move == NULL || *sc_move == '\0')
343963d5659Scjs 	{
344963d5659Scjs 		/*
345963d5659Scjs 		 * This is not an error here, because we don't
346963d5659Scjs 		 * always need sc_move.
347963d5659Scjs 		 * We need it only if we don't have home or lower-left.
348963d5659Scjs 		 */
349963d5659Scjs 		sc_move = "";
350963d5659Scjs 	}
351963d5659Scjs 
352963d5659Scjs 	sc_s_in = tgetstr("so", &sp);
353963d5659Scjs 	if (hard || sc_s_in == NULL)
354963d5659Scjs 		sc_s_in = "";
355963d5659Scjs 
356963d5659Scjs 	sc_s_out = tgetstr("se", &sp);
357963d5659Scjs 	if (hard || sc_s_out == NULL)
358963d5659Scjs 		sc_s_out = "";
359963d5659Scjs 
360963d5659Scjs 	sc_u_in = tgetstr("us", &sp);
361963d5659Scjs 	if (hard || sc_u_in == NULL)
362963d5659Scjs 		sc_u_in = sc_s_in;
363963d5659Scjs 
364963d5659Scjs 	sc_u_out = tgetstr("ue", &sp);
365963d5659Scjs 	if (hard || sc_u_out == NULL)
366963d5659Scjs 		sc_u_out = sc_s_out;
367963d5659Scjs 
368963d5659Scjs 	sc_b_in = tgetstr("md", &sp);
369963d5659Scjs 	if (hard || sc_b_in == NULL)
370963d5659Scjs 	{
371963d5659Scjs 		sc_b_in = sc_s_in;
372963d5659Scjs 		sc_b_out = sc_s_out;
373963d5659Scjs 	} else
374963d5659Scjs 	{
375963d5659Scjs 		sc_b_out = tgetstr("me", &sp);
376963d5659Scjs 		if (hard || sc_b_out == NULL)
377963d5659Scjs 			sc_b_out = "";
378963d5659Scjs 	}
379963d5659Scjs 
380963d5659Scjs 	sc_home = tgetstr("ho", &sp);
381963d5659Scjs 	if (hard || sc_home == NULL || *sc_home == '\0')
382963d5659Scjs 	{
383963d5659Scjs 		if (*sc_move == '\0')
384963d5659Scjs 		{
385963d5659Scjs 			/*
386963d5659Scjs 			 * This last resort for sc_home is supposed to
387963d5659Scjs 			 * be an up-arrow suggesting moving to the
388963d5659Scjs 			 * top of the "virtual screen". (The one in
389963d5659Scjs 			 * your imagination as you try to use this on
390963d5659Scjs 			 * a hard copy terminal.)
391963d5659Scjs 			 */
392963d5659Scjs 			sc_home = "|\b^";
393963d5659Scjs 		} else
394963d5659Scjs 		{
395963d5659Scjs 			/*
396963d5659Scjs 			 * No "home" string,
397963d5659Scjs 			 * but we can use "move(0,0)".
398963d5659Scjs 			 */
399cad152eeSitojun 			(void)strlcpy(sp, tgoto(sc_move, 0, 0),
400cad152eeSitojun 			    sizeof(sbuf) - (sp - sbuf));
401963d5659Scjs 			sc_home = sp;
402963d5659Scjs 			sp += strlen(sp) + 1;
403963d5659Scjs 		}
404963d5659Scjs 	}
405963d5659Scjs 
406963d5659Scjs 	sc_lower_left = tgetstr("ll", &sp);
407963d5659Scjs 	if (hard || sc_lower_left == NULL || *sc_lower_left == '\0')
408963d5659Scjs 	{
409963d5659Scjs 		if (*sc_move == '\0')
410963d5659Scjs 		{
411963d5659Scjs 			sc_lower_left = "\r";
412963d5659Scjs 		} else
413963d5659Scjs 		{
414963d5659Scjs 			/*
415963d5659Scjs 			 * No "lower-left" string,
416963d5659Scjs 			 * but we can use "move(0,last-line)".
417963d5659Scjs 			 */
418cad152eeSitojun 			(void)strlcpy(sp, tgoto(sc_move, 0, sc_height-1),
419cad152eeSitojun 			    sizeof(sbuf) - (sp - sbuf));
420963d5659Scjs 			sc_lower_left = sp;
421963d5659Scjs 			sp += strlen(sp) + 1;
422963d5659Scjs 		}
423963d5659Scjs 	}
424963d5659Scjs 
425963d5659Scjs 	/*
426963d5659Scjs 	 * To add a line at top of screen and scroll the display down,
427963d5659Scjs 	 * we use "al" (add line) or "sr" (scroll reverse).
428963d5659Scjs 	 */
429963d5659Scjs 	if ((sc_addline = tgetstr("al", &sp)) == NULL ||
430963d5659Scjs 		 *sc_addline == '\0')
431963d5659Scjs 		sc_addline = tgetstr("sr", &sp);
432963d5659Scjs 
433963d5659Scjs 	if (hard || sc_addline == NULL || *sc_addline == '\0')
434963d5659Scjs 	{
435963d5659Scjs 		sc_addline = "";
436963d5659Scjs 		/* Force repaint on any backward movement */
437963d5659Scjs 		back_scroll = 0;
438963d5659Scjs 	}
439963d5659Scjs 
440963d5659Scjs 	if (tgetflag("bs"))
441963d5659Scjs 		sc_backspace = "\b";
442963d5659Scjs 	else
443963d5659Scjs 	{
444963d5659Scjs 		sc_backspace = tgetstr("bc", &sp);
445963d5659Scjs 		if (sc_backspace == NULL || *sc_backspace == '\0')
446963d5659Scjs 			sc_backspace = "\b";
447963d5659Scjs 	}
448963d5659Scjs }
449963d5659Scjs 
450963d5659Scjs 
451963d5659Scjs /*
452963d5659Scjs  * Below are the functions which perform all the
453963d5659Scjs  * terminal-specific screen manipulation.
454963d5659Scjs  */
455963d5659Scjs 
456963d5659Scjs /*
457963d5659Scjs  * Initialize terminal
458963d5659Scjs  */
459e3fbe7d2Schristos void
init()460963d5659Scjs init()
461963d5659Scjs {
462963d5659Scjs 	tputs(sc_init, sc_height, putchr);
463963d5659Scjs }
464963d5659Scjs 
465963d5659Scjs /*
466963d5659Scjs  * Deinitialize terminal
467963d5659Scjs  */
468e3fbe7d2Schristos void
deinit()469963d5659Scjs deinit()
470963d5659Scjs {
471963d5659Scjs 	tputs(sc_deinit, sc_height, putchr);
472963d5659Scjs }
473963d5659Scjs 
474963d5659Scjs /*
475963d5659Scjs  * Home cursor (move to upper left corner of screen).
476963d5659Scjs  */
477e3fbe7d2Schristos void
home()478963d5659Scjs home()
479963d5659Scjs {
480963d5659Scjs 	tputs(sc_home, 1, putchr);
481963d5659Scjs }
482963d5659Scjs 
483963d5659Scjs /*
484963d5659Scjs  * Add a blank line (called with cursor at home).
485963d5659Scjs  * Should scroll the display down.
486963d5659Scjs  */
487e3fbe7d2Schristos void
add_line()488963d5659Scjs add_line()
489963d5659Scjs {
490963d5659Scjs 	tputs(sc_addline, sc_height, putchr);
491963d5659Scjs }
492963d5659Scjs 
493963d5659Scjs int short_file;				/* if file less than a screen */
494e3fbe7d2Schristos void
lower_left()495963d5659Scjs lower_left()
496963d5659Scjs {
497963d5659Scjs 	if (short_file) {
498963d5659Scjs 		putchr('\r');
499963d5659Scjs 		flush();
500963d5659Scjs 	}
501963d5659Scjs 	else
502963d5659Scjs 		tputs(sc_lower_left, 1, putchr);
503963d5659Scjs }
504963d5659Scjs 
505963d5659Scjs /*
506963d5659Scjs  * Ring the terminal bell.
507963d5659Scjs  */
508e3fbe7d2Schristos void
bell()509963d5659Scjs bell()
510963d5659Scjs {
511963d5659Scjs 	putchr('\7');
512963d5659Scjs }
513963d5659Scjs 
514963d5659Scjs /*
515963d5659Scjs  * Clear the screen.
516963d5659Scjs  */
517e3fbe7d2Schristos void
clear()518963d5659Scjs clear()
519963d5659Scjs {
520963d5659Scjs 	tputs(sc_clear, sc_height, putchr);
521963d5659Scjs }
522963d5659Scjs 
523963d5659Scjs /*
524963d5659Scjs  * Clear from the cursor to the end of the cursor's line.
525963d5659Scjs  * {{ This must not move the cursor. }}
526963d5659Scjs  */
527e3fbe7d2Schristos void
clear_eol()528963d5659Scjs clear_eol()
529963d5659Scjs {
530963d5659Scjs 	tputs(sc_eol_clear, 1, putchr);
531963d5659Scjs }
532963d5659Scjs 
533963d5659Scjs /*
534963d5659Scjs  * Begin "standout" (bold, underline, or whatever).
535963d5659Scjs  */
536e3fbe7d2Schristos void
so_enter()537963d5659Scjs so_enter()
538963d5659Scjs {
539963d5659Scjs 	tputs(sc_s_in, 1, putchr);
540963d5659Scjs }
541963d5659Scjs 
542963d5659Scjs /*
543963d5659Scjs  * End "standout".
544963d5659Scjs  */
545e3fbe7d2Schristos void
so_exit()546963d5659Scjs so_exit()
547963d5659Scjs {
548963d5659Scjs 	tputs(sc_s_out, 1, putchr);
549963d5659Scjs }
550963d5659Scjs 
551963d5659Scjs /*
552963d5659Scjs  * Begin "underline" (hopefully real underlining,
553963d5659Scjs  * otherwise whatever the terminal provides).
554963d5659Scjs  */
555e3fbe7d2Schristos void
ul_enter()556963d5659Scjs ul_enter()
557963d5659Scjs {
558963d5659Scjs 	tputs(sc_u_in, 1, putchr);
559963d5659Scjs }
560963d5659Scjs 
561963d5659Scjs /*
562963d5659Scjs  * End "underline".
563963d5659Scjs  */
564e3fbe7d2Schristos void
ul_exit()565963d5659Scjs ul_exit()
566963d5659Scjs {
567963d5659Scjs 	tputs(sc_u_out, 1, putchr);
568963d5659Scjs }
569963d5659Scjs 
570963d5659Scjs /*
571963d5659Scjs  * Begin "bold"
572963d5659Scjs  */
573e3fbe7d2Schristos void
bo_enter()574963d5659Scjs bo_enter()
575963d5659Scjs {
576963d5659Scjs 	tputs(sc_b_in, 1, putchr);
577963d5659Scjs }
578963d5659Scjs 
579963d5659Scjs /*
580963d5659Scjs  * End "bold".
581963d5659Scjs  */
582e3fbe7d2Schristos void
bo_exit()583963d5659Scjs bo_exit()
584963d5659Scjs {
585963d5659Scjs 	tputs(sc_b_out, 1, putchr);
586963d5659Scjs }
587963d5659Scjs 
588963d5659Scjs /*
589963d5659Scjs  * Erase the character to the left of the cursor
590963d5659Scjs  * and move the cursor left.
591963d5659Scjs  */
592e3fbe7d2Schristos void
backspace()593963d5659Scjs backspace()
594963d5659Scjs {
595963d5659Scjs 	/*
596963d5659Scjs 	 * Try to erase the previous character by overstriking with a space.
597963d5659Scjs 	 */
598963d5659Scjs 	tputs(sc_backspace, 1, putchr);
599963d5659Scjs 	putchr(' ');
600963d5659Scjs 	tputs(sc_backspace, 1, putchr);
601963d5659Scjs }
602963d5659Scjs 
603963d5659Scjs /*
604963d5659Scjs  * Output a plain backspace, without erasing the previous char.
605963d5659Scjs  */
606e3fbe7d2Schristos void
putbs()607963d5659Scjs putbs()
608963d5659Scjs {
609963d5659Scjs 	tputs(sc_backspace, 1, putchr);
610963d5659Scjs }
611