2 * Copyright (c) 1988 Mark Nudleman
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)screen.c 8.2 (Berkeley) 4/20/94";
40 * Routines which deal with the characteristics of the terminal.
41 * Uses termcap to be as terminal-independent as possible.
43 * {{ Someday this should be rewritten to use curses. }}
58 #include <sys/ioctl.h>
65 #include <sys/ioctl.h>
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.
72 #include <sys/signal.h>
74 #include <sys/window.h>
79 * Strings passed to tputs() to do various terminal functions.
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 */
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 bo_width, be_width; /* Printing width of boldface sequences */
106 int ul_width, ue_width; /* Printing width of underline sequences */
107 int so_width, se_width; /* Printing width of standout sequences */
115 * These two variables are sometimes defined in,
116 * and needed by, the termcap library.
117 * It may be necessary on some systems to declare them extern here.
119 /*extern*/ short ospeed; /* Terminal output baud rate */
120 /*extern*/ char PC; /* Pad character */
122 extern int back_scroll;
127 * Change terminal to "raw mode", or restore to "normal" mode.
129 * 1. An outstanding read will complete on receipt of a single keystroke.
130 * 2. Input is not echoed.
131 * 3. On output, \n is mapped to \r\n.
132 * 4. \t is NOT expanded into spaces.
133 * 5. Signal-causing characters such as ctrl-C (interrupt),
134 * etc. are NOT disabled.
135 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
140 #if TERMIO || TERMIOS
144 static struct termio save_term;
147 static struct termios save_term;
153 * Get terminal modes.
156 (void)ioctl(2, TCGETA, &s);
162 * Save modes and set certain variables dependent on modes.
166 ospeed = s.c_cflag & CBAUD;
168 /* more work needed here */
170 erase_char = s.c_cc[VERASE];
171 kill_char = s.c_cc[VKILL];
172 werase_char = s.c_cc[VWERASE];
175 * Set the modes to the way we want them.
177 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
178 s.c_oflag |= (OPOST|ONLCR|TAB3);
180 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
187 * Restore saved modes.
192 (void)ioctl(2, TCSETAW, &s);
194 tcsetattr(2, TCSADRAIN, &s);
199 static struct sgttyb save_term;
204 * Get terminal modes.
206 (void)ioctl(2, TIOCGETP, &s);
207 (void)ioctl(2, TIOCGLTC, &l);
210 * Save modes and set certain variables dependent on modes.
213 ospeed = s.sg_ospeed;
214 erase_char = s.sg_erase;
215 kill_char = s.sg_kill;
216 werase_char = l.t_werasc;
219 * Set the modes to the way we want them.
221 s.sg_flags |= CBREAK;
222 s.sg_flags &= ~(ECHO|XTABS);
226 * Restore saved modes.
230 (void)ioctl(2, TIOCSETN, &s);
235 * Get terminal capabilities via termcap.
250 static char sbuf[1024];
252 char *getenv(), *strcpy();
255 * Find out what kind of terminal this is.
257 if ((term = getenv("TERM")) == NULL)
259 if (tgetent(termbuf, term) <= 0)
260 (void)strcpy(termbuf, "dumb:co#80:hc:");
263 * Get size of the screen.
266 if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_row > 0)
267 sc_height = w.ws_row;
271 if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_height > 0)
272 sc_height = w.uw_height/w.uw_vs;
276 sc_height = tgetnum("li");
277 hard = (sc_height < 0 || tgetflag("hc"));
279 /* Oh no, this is a hardcopy terminal. */
284 if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
288 if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_width > 0)
289 sc_width = w.uw_width/w.uw_hs;
293 sc_width = tgetnum("co");
296 (void) setvari("_sc_height", (long) sc_height - 1);
297 (void) setvari("_sc_width", (long) sc_width);
299 auto_wrap = tgetflag("am");
300 ignaw = tgetflag("xn");
301 retain_below = tgetflag("db");
304 * Assumes termcap variable "sg" is the printing width of
305 * the standout sequence, the end standout sequence,
306 * the underline sequence, the end underline sequence,
307 * the boldface sequence, and the end boldface sequence.
309 if ((so_width = tgetnum("sg")) < 0)
311 be_width = bo_width = ue_width = ul_width = se_width = so_width;
314 * Get various string-valued capabilities.
318 sc_pad = tgetstr("pc", &sp);
322 sc_init = tgetstr("ti", &sp);
326 sc_deinit= tgetstr("te", &sp);
327 if (sc_deinit == NULL)
330 sc_eol_clear = tgetstr("ce", &sp);
331 if (hard || sc_eol_clear == NULL || *sc_eol_clear == '\0')
336 sc_clear = tgetstr("cl", &sp);
337 if (hard || sc_clear == NULL || *sc_clear == '\0')
342 sc_move = tgetstr("cm", &sp);
343 if (hard || sc_move == NULL || *sc_move == '\0')
346 * This is not an error here, because we don't
347 * always need sc_move.
348 * We need it only if we don't have home or lower-left.
353 sc_s_in = tgetstr("so", &sp);
354 if (hard || sc_s_in == NULL)
357 sc_s_out = tgetstr("se", &sp);
358 if (hard || sc_s_out == NULL)
361 sc_u_in = tgetstr("us", &sp);
362 if (hard || sc_u_in == NULL)
365 sc_u_out = tgetstr("ue", &sp);
366 if (hard || sc_u_out == NULL)
369 sc_b_in = tgetstr("md", &sp);
370 if (hard || sc_b_in == NULL)
376 sc_b_out = tgetstr("me", &sp);
377 if (hard || sc_b_out == NULL)
381 sc_home = tgetstr("ho", &sp);
382 if (hard || sc_home == NULL || *sc_home == '\0')
384 if (*sc_move == '\0')
387 * This last resort for sc_home is supposed to
388 * be an up-arrow suggesting moving to the
389 * top of the "virtual screen". (The one in
390 * your imagination as you try to use this on
391 * a hard copy terminal.)
398 * but we can use "move(0,0)".
400 (void)strcpy(sp, tgoto(sc_move, 0, 0));
402 sp += strlen(sp) + 1;
406 sc_lower_left = tgetstr("ll", &sp);
407 if (hard || sc_lower_left == NULL || *sc_lower_left == '\0')
409 if (*sc_move == '\0')
411 sc_lower_left = "\r";
415 * No "lower-left" string,
416 * but we can use "move(0,last-line)".
418 (void)strcpy(sp, tgoto(sc_move, 0, sc_height-1));
420 sp += strlen(sp) + 1;
425 * To add a line at top of screen and scroll the display down,
426 * we use "al" (add line) or "sr" (scroll reverse).
428 if ((sc_addline = tgetstr("al", &sp)) == NULL ||
430 sc_addline = tgetstr("sr", &sp);
432 if (hard || sc_addline == NULL || *sc_addline == '\0')
435 /* Force repaint on any backward movement */
443 sc_backspace = tgetstr("bc", &sp);
444 if (sc_backspace == NULL || *sc_backspace == '\0')
451 * Below are the functions which perform all the
452 * terminal-specific screen manipulation.
458 * Initialize terminal
462 tputs(sc_init, sc_height, putchr);
466 * Deinitialize terminal
470 tputs(sc_deinit, sc_height, putchr);
474 * Home cursor (move to upper left corner of screen).
478 tputs(sc_home, 1, putchr);
482 * Add a blank line (called with cursor at home).
483 * Should scroll the display down.
487 tputs(sc_addline, sc_height, putchr);
490 int short_file; /* if file less than a screen */
498 tputs(sc_lower_left, 1, putchr);
502 * Ring the terminal bell.
514 if (mode_flags & M_SO)
516 if (mode_flags & M_UL)
518 if (mode_flags & M_BO)
520 tputs(sc_clear, sc_height, putchr);
524 * Clear from the cursor to the end of the cursor's line.
525 * {{ This must not move the cursor. }}
529 if (mode_flags & M_SO)
531 if (mode_flags & M_UL)
533 if (mode_flags & M_BO)
535 tputs(sc_eol_clear, 1, putchr);
539 * Begin "standout" (bold, underline, or whatever).
543 tputs(sc_s_in, 1, putchr);
552 tputs(sc_s_out, 1, putchr);
557 * Begin "underline" (hopefully real underlining,
558 * otherwise whatever the terminal provides).
562 tputs(sc_u_in, 1, putchr);
571 tputs(sc_u_out, 1, putchr);
580 tputs(sc_b_in, 1, putchr);
589 tputs(sc_b_out, 1, putchr);
594 * Erase the character to the left of the cursor
595 * and move the cursor left.
600 * Try to erase the previous character by overstriking with a space.
602 tputs(sc_backspace, 1, putchr);
604 tputs(sc_backspace, 1, putchr);
608 * Output a plain backspace, without erasing the previous char.
612 tputs(sc_backspace, 1, putchr);