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