Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / distrib / utils / more / screen.c
blobf831f1c1f155c2a99586e0ad9070e9783760b49d
1 /* $NetBSD: screen.c,v 1.6 2003/08/07 09:28:01 agc Exp $ */
3 /*
4 * Copyright (c) 1988 Mark Nudelman
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)screen.c 8.2 (Berkeley) 4/20/94";
37 #else
38 __RCSID("$NetBSD: screen.c,v 1.6 2003/08/07 09:28:01 agc Exp $");
39 #endif
40 #endif /* not lint */
43 * Routines which deal with the characteristics of the terminal.
44 * Uses termcap to be as terminal-independent as possible.
46 * {{ Someday this should be rewritten to use curses. }}
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
54 #include "less.h"
55 #include "extern.h"
57 #define TERMIOS 1
59 #if TERMIO
60 #include <termio.h>
61 #else
62 #if TERMIOS
63 #include <termios.h>
64 #define TAB3 0
65 #include <sys/ioctl.h>
66 #else
67 #include <sgtty.h>
68 #endif
69 #endif
70 #ifdef __NetBSD__
71 #include <termcap.h>
72 #endif
74 #ifdef TIOCGWINSZ
75 #include <sys/ioctl.h>
76 #else
78 * For the Unix PC (ATT 7300 & 3B1):
79 * Since WIOCGETD is defined in sys/window.h, we can't use that to decide
80 * whether to include sys/window.h. Use SIGPHONE from sys/signal.h instead.
82 #include <sys/signal.h>
83 #ifdef SIGPHONE
84 #include <sys/window.h>
85 #endif
86 #endif
89 * Strings passed to tputs() to do various terminal functions.
91 static char
92 *sc_pad, /* Pad string */
93 *sc_home, /* Cursor home */
94 *sc_addline, /* Add line, scroll down following lines */
95 *sc_lower_left, /* Cursor to last line, first column */
96 *sc_move, /* General cursor positioning */
97 *sc_clear, /* Clear screen */
98 *sc_eol_clear, /* Clear to end of line */
99 *sc_s_in, /* Enter standout (highlighted) mode */
100 *sc_s_out, /* Exit standout mode */
101 *sc_u_in, /* Enter underline mode */
102 *sc_u_out, /* Exit underline mode */
103 *sc_b_in, /* Enter bold mode */
104 *sc_b_out, /* Exit bold mode */
105 *sc_backspace, /* Backspace cursor */
106 *sc_init, /* Startup terminal initialization */
107 *sc_deinit; /* Exit terminal de-intialization */
109 int auto_wrap; /* Terminal does \r\n when write past margin */
110 int ignaw; /* Terminal ignores \n immediately after wrap */
111 /* The user's erase and line-kill chars */
112 int retain_below; /* Terminal retains text below the screen */
113 int erase_char, kill_char, werase_char;
114 int sc_width, sc_height = -1; /* Height & width of screen */
115 int sc_window = -1; /* window size for forward and backward */
116 int bo_width, be_width; /* Printing width of boldface sequences */
117 int ul_width, ue_width; /* Printing width of underline sequences */
118 int so_width, se_width; /* Printing width of standout sequences */
121 * These two variables are sometimes defined in,
122 * and needed by, the termcap library.
123 * It may be necessary on some systems to declare them extern here.
125 /*extern*/ short ospeed; /* Terminal output baud rate */
126 /*extern*/ char PC; /* Pad character */
129 * Change terminal to "raw mode", or restore to "normal" mode.
130 * "Raw mode" means
131 * 1. An outstanding read will complete on receipt of a single keystroke.
132 * 2. Input is not echoed.
133 * 3. On output, \n is mapped to \r\n.
134 * 4. \t is NOT expanded into spaces.
135 * 5. Signal-causing characters such as ctrl-C (interrupt),
136 * etc. are NOT disabled.
137 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
139 void
140 raw_mode(on)
141 int on;
143 #if TERMIO || TERMIOS
145 #if TERMIO
146 struct termio s;
147 static struct termio save_term;
148 #else
149 struct termios s;
150 static struct termios save_term;
151 #endif
153 if (on)
156 * Get terminal modes.
158 #if TERMIO
159 (void)ioctl(2, TCGETA, &s);
160 #else
161 tcgetattr(2, &s);
162 #endif
165 * Save modes and set certain variables dependent on modes.
167 save_term = s;
168 #if TERMIO
169 ospeed = s.c_cflag & CBAUD;
170 #else
171 ospeed = cfgetospeed(&s);
172 #endif
173 erase_char = s.c_cc[VERASE];
174 kill_char = s.c_cc[VKILL];
175 werase_char = s.c_cc[VWERASE];
178 * Set the modes to the way we want them.
180 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
181 s.c_oflag |= (OPOST|ONLCR|TAB3);
182 #if TERMIO
183 s.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
184 #endif
185 s.c_cc[VMIN] = 1;
186 s.c_cc[VTIME] = 0;
187 } else
190 * Restore saved modes.
192 s = save_term;
194 #if TERMIO
195 (void)ioctl(2, TCSETAW, &s);
196 #else
197 tcsetattr(2, TCSADRAIN, &s);
198 #endif
199 #else
200 struct sgttyb s;
201 struct ltchars l;
202 static struct sgttyb save_term;
204 if (on)
207 * Get terminal modes.
209 (void)ioctl(2, TIOCGETP, &s);
210 (void)ioctl(2, TIOCGLTC, &l);
213 * Save modes and set certain variables dependent on modes.
215 save_term = s;
216 ospeed = s.sg_ospeed;
217 erase_char = s.sg_erase;
218 kill_char = s.sg_kill;
219 werase_char = l.t_werasc;
222 * Set the modes to the way we want them.
224 s.sg_flags |= CBREAK;
225 s.sg_flags &= ~(ECHO|XTABS);
226 } else
229 * Restore saved modes.
231 s = save_term;
233 (void)ioctl(2, TIOCSETN, &s);
234 #endif
238 * Get terminal capabilities via termcap.
240 void
241 get_term()
243 char termbuf[2048];
244 char *sp;
245 char *term;
246 int hard;
247 #ifdef TIOCGWINSZ
248 struct winsize w;
249 #else
250 #ifdef WIOCGETD
251 struct uwdata w;
252 #endif
253 #endif
254 static char sbuf[1024];
257 * Find out what kind of terminal this is.
259 if ((term = getenv("TERM")) == NULL)
260 term = "unknown";
261 if (tgetent(termbuf, term) <= 0)
262 (void)strlcpy(termbuf, "dumb:co#80:hc:", sizeof(termbuf));
265 * Get size of the screen.
267 #ifdef TIOCGWINSZ
268 if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_row > 0)
269 sc_height = w.ws_row;
270 #else
271 #ifdef WIOCGETD
272 if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_height > 0)
273 sc_height = w.uw_height/w.uw_vs;
274 #endif
275 #endif
276 else
277 sc_height = tgetnum("li");
278 hard = (sc_height < 0 || tgetflag("hc"));
279 if (hard) {
280 /* Oh no, this is a hardcopy terminal. */
281 sc_height = 24;
284 #ifdef TIOCGWINSZ
285 if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
286 sc_width = w.ws_col;
287 else
288 #ifdef WIOCGETD
289 if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_width > 0)
290 sc_width = w.uw_width/w.uw_hs;
291 else
292 #endif
293 #endif
294 sc_width = tgetnum("co");
295 if (sc_width < 0)
296 sc_width = 80;
298 auto_wrap = tgetflag("am");
299 ignaw = tgetflag("xn");
300 retain_below = tgetflag("db");
303 * Assumes termcap variable "sg" is the printing width of
304 * the standout sequence, the end standout sequence,
305 * the underline sequence, the end underline sequence,
306 * the boldface sequence, and the end boldface sequence.
308 if ((so_width = tgetnum("sg")) < 0)
309 so_width = 0;
310 be_width = bo_width = ue_width = ul_width = se_width = so_width;
313 * Get various string-valued capabilities.
315 sp = sbuf;
317 sc_pad = tgetstr("pc", &sp);
318 if (sc_pad != NULL)
319 PC = *sc_pad;
321 sc_init = tgetstr("ti", &sp);
322 if (sc_init == NULL)
323 sc_init = "";
325 sc_deinit= tgetstr("te", &sp);
326 if (sc_deinit == NULL)
327 sc_deinit = "";
329 sc_eol_clear = tgetstr("ce", &sp);
330 if (hard || sc_eol_clear == NULL || *sc_eol_clear == '\0')
332 sc_eol_clear = "";
335 sc_clear = tgetstr("cl", &sp);
336 if (hard || sc_clear == NULL || *sc_clear == '\0')
338 sc_clear = "\n\n";
341 sc_move = tgetstr("cm", &sp);
342 if (hard || sc_move == NULL || *sc_move == '\0')
345 * This is not an error here, because we don't
346 * always need sc_move.
347 * We need it only if we don't have home or lower-left.
349 sc_move = "";
352 sc_s_in = tgetstr("so", &sp);
353 if (hard || sc_s_in == NULL)
354 sc_s_in = "";
356 sc_s_out = tgetstr("se", &sp);
357 if (hard || sc_s_out == NULL)
358 sc_s_out = "";
360 sc_u_in = tgetstr("us", &sp);
361 if (hard || sc_u_in == NULL)
362 sc_u_in = sc_s_in;
364 sc_u_out = tgetstr("ue", &sp);
365 if (hard || sc_u_out == NULL)
366 sc_u_out = sc_s_out;
368 sc_b_in = tgetstr("md", &sp);
369 if (hard || sc_b_in == NULL)
371 sc_b_in = sc_s_in;
372 sc_b_out = sc_s_out;
373 } else
375 sc_b_out = tgetstr("me", &sp);
376 if (hard || sc_b_out == NULL)
377 sc_b_out = "";
380 sc_home = tgetstr("ho", &sp);
381 if (hard || sc_home == NULL || *sc_home == '\0')
383 if (*sc_move == '\0')
386 * This last resort for sc_home is supposed to
387 * be an up-arrow suggesting moving to the
388 * top of the "virtual screen". (The one in
389 * your imagination as you try to use this on
390 * a hard copy terminal.)
392 sc_home = "|\b^";
393 } else
396 * No "home" string,
397 * but we can use "move(0,0)".
399 (void)strlcpy(sp, tgoto(sc_move, 0, 0),
400 sizeof(sbuf) - (sp - sbuf));
401 sc_home = sp;
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";
412 } else
415 * No "lower-left" string,
416 * but we can use "move(0,last-line)".
418 (void)strlcpy(sp, tgoto(sc_move, 0, sc_height-1),
419 sizeof(sbuf) - (sp - sbuf));
420 sc_lower_left = sp;
421 sp += strlen(sp) + 1;
426 * To add a line at top of screen and scroll the display down,
427 * we use "al" (add line) or "sr" (scroll reverse).
429 if ((sc_addline = tgetstr("al", &sp)) == NULL ||
430 *sc_addline == '\0')
431 sc_addline = tgetstr("sr", &sp);
433 if (hard || sc_addline == NULL || *sc_addline == '\0')
435 sc_addline = "";
436 /* Force repaint on any backward movement */
437 back_scroll = 0;
440 if (tgetflag("bs"))
441 sc_backspace = "\b";
442 else
444 sc_backspace = tgetstr("bc", &sp);
445 if (sc_backspace == NULL || *sc_backspace == '\0')
446 sc_backspace = "\b";
452 * Below are the functions which perform all the
453 * terminal-specific screen manipulation.
457 * Initialize terminal
459 void
460 init()
462 tputs(sc_init, sc_height, putchr);
466 * Deinitialize terminal
468 void
469 deinit()
471 tputs(sc_deinit, sc_height, putchr);
475 * Home cursor (move to upper left corner of screen).
477 void
478 home()
480 tputs(sc_home, 1, putchr);
484 * Add a blank line (called with cursor at home).
485 * Should scroll the display down.
487 void
488 add_line()
490 tputs(sc_addline, sc_height, putchr);
493 int short_file; /* if file less than a screen */
494 void
495 lower_left()
497 if (short_file) {
498 putchr('\r');
499 flush();
501 else
502 tputs(sc_lower_left, 1, putchr);
506 * Ring the terminal bell.
508 void
509 bell()
511 putchr('\7');
515 * Clear the screen.
517 void
518 clear()
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. }}
527 void
528 clear_eol()
530 tputs(sc_eol_clear, 1, putchr);
534 * Begin "standout" (bold, underline, or whatever).
536 void
537 so_enter()
539 tputs(sc_s_in, 1, putchr);
543 * End "standout".
545 void
546 so_exit()
548 tputs(sc_s_out, 1, putchr);
552 * Begin "underline" (hopefully real underlining,
553 * otherwise whatever the terminal provides).
555 void
556 ul_enter()
558 tputs(sc_u_in, 1, putchr);
562 * End "underline".
564 void
565 ul_exit()
567 tputs(sc_u_out, 1, putchr);
571 * Begin "bold"
573 void
574 bo_enter()
576 tputs(sc_b_in, 1, putchr);
580 * End "bold".
582 void
583 bo_exit()
585 tputs(sc_b_out, 1, putchr);
589 * Erase the character to the left of the cursor
590 * and move the cursor left.
592 void
593 backspace()
596 * Try to erase the previous character by overstriking with a space.
598 tputs(sc_backspace, 1, putchr);
599 putchr(' ');
600 tputs(sc_backspace, 1, putchr);
604 * Output a plain backspace, without erasing the previous char.
606 void
607 putbs()
609 tputs(sc_backspace, 1, putchr);