1 /* $NetBSD: screen.c,v 1.7 2003/10/13 14:34:25 agc Exp $ */
2
3 /*
4 * Copyright (c) 1988 Mark Nudelman
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
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.
19 *
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.
31 */
32
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.7 2003/10/13 14:34:25 agc Exp $");
39 #endif
40 #endif /* not lint */
41
42 /*
43 * Routines which deal with the characteristics of the terminal.
44 * Uses termcap to be as terminal-independent as possible.
45 *
46 * {{ Someday this should be rewritten to use curses. }}
47 */
48
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53
54 #include "less.h"
55 #include "extern.h"
56
57 #define TERMIOS 1
58
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
73
74 #ifdef TIOCGWINSZ
75 #include <sys/ioctl.h>
76 #else
77 /*
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.
81 */
82 #include <sys/signal.h>
83 #ifdef SIGPHONE
84 #include <sys/window.h>
85 #endif
86 #endif
87
88 /*
89 * Strings passed to tputs() to do various terminal functions.
90 */
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 */
108
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 */
119
120 /*
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.
124 */
125 /*extern*/ short ospeed; /* Terminal output baud rate */
126 /*extern*/ char PC; /* Pad character */
127
128 /*
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.
138 */
139 void
raw_mode(on)140 raw_mode(on)
141 int on;
142 {
143 #if TERMIO || TERMIOS
144
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
152
153 if (on)
154 {
155 /*
156 * Get terminal modes.
157 */
158 #if TERMIO
159 (void)ioctl(2, TCGETA, &s);
160 #else
161 tcgetattr(2, &s);
162 #endif
163
164 /*
165 * Save modes and set certain variables dependent on modes.
166 */
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];
176
177 /*
178 * Set the modes to the way we want them.
179 */
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
188 {
189 /*
190 * Restore saved modes.
191 */
192 s = save_term;
193 }
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;
203
204 if (on)
205 {
206 /*
207 * Get terminal modes.
208 */
209 (void)ioctl(2, TIOCGETP, &s);
210 (void)ioctl(2, TIOCGLTC, &l);
211
212 /*
213 * Save modes and set certain variables dependent on modes.
214 */
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;
220
221 /*
222 * Set the modes to the way we want them.
223 */
224 s.sg_flags |= CBREAK;
225 s.sg_flags &= ~(ECHO|XTABS);
226 } else
227 {
228 /*
229 * Restore saved modes.
230 */
231 s = save_term;
232 }
233 (void)ioctl(2, TIOCSETN, &s);
234 #endif
235 }
236
237 /*
238 * Get terminal capabilities via termcap.
239 */
240 void
get_term()241 get_term()
242 {
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];
255
256 /*
257 * Find out what kind of terminal this is.
258 */
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));
263
264 /*
265 * Get size of the screen.
266 */
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;
282 }
283
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;
297
298 auto_wrap = tgetflag("am");
299 ignaw = tgetflag("xn");
300 retain_below = tgetflag("db");
301
302 /*
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.
307 */
308 if ((so_width = tgetnum("sg")) < 0)
309 so_width = 0;
310 be_width = bo_width = ue_width = ul_width = se_width = so_width;
311
312 /*
313 * Get various string-valued capabilities.
314 */
315 sp = sbuf;
316
317 sc_pad = tgetstr("pc", &sp);
318 if (sc_pad != NULL)
319 PC = *sc_pad;
320
321 sc_init = tgetstr("ti", &sp);
322 if (sc_init == NULL)
323 sc_init = "";
324
325 sc_deinit= tgetstr("te", &sp);
326 if (sc_deinit == NULL)
327 sc_deinit = "";
328
329 sc_eol_clear = tgetstr("ce", &sp);
330 if (hard || sc_eol_clear == NULL || *sc_eol_clear == '\0')
331 {
332 sc_eol_clear = "";
333 }
334
335 sc_clear = tgetstr("cl", &sp);
336 if (hard || sc_clear == NULL || *sc_clear == '\0')
337 {
338 sc_clear = "\n\n";
339 }
340
341 sc_move = tgetstr("cm", &sp);
342 if (hard || sc_move == NULL || *sc_move == '\0')
343 {
344 /*
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.
348 */
349 sc_move = "";
350 }
351
352 sc_s_in = tgetstr("so", &sp);
353 if (hard || sc_s_in == NULL)
354 sc_s_in = "";
355
356 sc_s_out = tgetstr("se", &sp);
357 if (hard || sc_s_out == NULL)
358 sc_s_out = "";
359
360 sc_u_in = tgetstr("us", &sp);
361 if (hard || sc_u_in == NULL)
362 sc_u_in = sc_s_in;
363
364 sc_u_out = tgetstr("ue", &sp);
365 if (hard || sc_u_out == NULL)
366 sc_u_out = sc_s_out;
367
368 sc_b_in = tgetstr("md", &sp);
369 if (hard || sc_b_in == NULL)
370 {
371 sc_b_in = sc_s_in;
372 sc_b_out = sc_s_out;
373 } else
374 {
375 sc_b_out = tgetstr("me", &sp);
376 if (hard || sc_b_out == NULL)
377 sc_b_out = "";
378 }
379
380 sc_home = tgetstr("ho", &sp);
381 if (hard || sc_home == NULL || *sc_home == '\0')
382 {
383 if (*sc_move == '\0')
384 {
385 /*
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.)
391 */
392 sc_home = "|\b^";
393 } else
394 {
395 /*
396 * No "home" string,
397 * but we can use "move(0,0)".
398 */
399 (void)strlcpy(sp, tgoto(sc_move, 0, 0),
400 sizeof(sbuf) - (sp - sbuf));
401 sc_home = sp;
402 sp += strlen(sp) + 1;
403 }
404 }
405
406 sc_lower_left = tgetstr("ll", &sp);
407 if (hard || sc_lower_left == NULL || *sc_lower_left == '\0')
408 {
409 if (*sc_move == '\0')
410 {
411 sc_lower_left = "\r";
412 } else
413 {
414 /*
415 * No "lower-left" string,
416 * but we can use "move(0,last-line)".
417 */
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;
422 }
423 }
424
425 /*
426 * To add a line at top of screen and scroll the display down,
427 * we use "al" (add line) or "sr" (scroll reverse).
428 */
429 if ((sc_addline = tgetstr("al", &sp)) == NULL ||
430 *sc_addline == '\0')
431 sc_addline = tgetstr("sr", &sp);
432
433 if (hard || sc_addline == NULL || *sc_addline == '\0')
434 {
435 sc_addline = "";
436 /* Force repaint on any backward movement */
437 back_scroll = 0;
438 }
439
440 if (tgetflag("bs"))
441 sc_backspace = "\b";
442 else
443 {
444 sc_backspace = tgetstr("bc", &sp);
445 if (sc_backspace == NULL || *sc_backspace == '\0')
446 sc_backspace = "\b";
447 }
448 }
449
450
451 /*
452 * Below are the functions which perform all the
453 * terminal-specific screen manipulation.
454 */
455
456 /*
457 * Initialize terminal
458 */
459 void
init()460 init()
461 {
462 tputs(sc_init, sc_height, putchr);
463 }
464
465 /*
466 * Deinitialize terminal
467 */
468 void
deinit()469 deinit()
470 {
471 tputs(sc_deinit, sc_height, putchr);
472 }
473
474 /*
475 * Home cursor (move to upper left corner of screen).
476 */
477 void
home()478 home()
479 {
480 tputs(sc_home, 1, putchr);
481 }
482
483 /*
484 * Add a blank line (called with cursor at home).
485 * Should scroll the display down.
486 */
487 void
add_line()488 add_line()
489 {
490 tputs(sc_addline, sc_height, putchr);
491 }
492
493 int short_file; /* if file less than a screen */
494 void
lower_left()495 lower_left()
496 {
497 if (short_file) {
498 putchr('\r');
499 flush();
500 }
501 else
502 tputs(sc_lower_left, 1, putchr);
503 }
504
505 /*
506 * Ring the terminal bell.
507 */
508 void
bell()509 bell()
510 {
511 putchr('\7');
512 }
513
514 /*
515 * Clear the screen.
516 */
517 void
clear()518 clear()
519 {
520 tputs(sc_clear, sc_height, putchr);
521 }
522
523 /*
524 * Clear from the cursor to the end of the cursor's line.
525 * {{ This must not move the cursor. }}
526 */
527 void
clear_eol()528 clear_eol()
529 {
530 tputs(sc_eol_clear, 1, putchr);
531 }
532
533 /*
534 * Begin "standout" (bold, underline, or whatever).
535 */
536 void
so_enter()537 so_enter()
538 {
539 tputs(sc_s_in, 1, putchr);
540 }
541
542 /*
543 * End "standout".
544 */
545 void
so_exit()546 so_exit()
547 {
548 tputs(sc_s_out, 1, putchr);
549 }
550
551 /*
552 * Begin "underline" (hopefully real underlining,
553 * otherwise whatever the terminal provides).
554 */
555 void
ul_enter()556 ul_enter()
557 {
558 tputs(sc_u_in, 1, putchr);
559 }
560
561 /*
562 * End "underline".
563 */
564 void
ul_exit()565 ul_exit()
566 {
567 tputs(sc_u_out, 1, putchr);
568 }
569
570 /*
571 * Begin "bold"
572 */
573 void
bo_enter()574 bo_enter()
575 {
576 tputs(sc_b_in, 1, putchr);
577 }
578
579 /*
580 * End "bold".
581 */
582 void
bo_exit()583 bo_exit()
584 {
585 tputs(sc_b_out, 1, putchr);
586 }
587
588 /*
589 * Erase the character to the left of the cursor
590 * and move the cursor left.
591 */
592 void
backspace()593 backspace()
594 {
595 /*
596 * Try to erase the previous character by overstriking with a space.
597 */
598 tputs(sc_backspace, 1, putchr);
599 putchr(' ');
600 tputs(sc_backspace, 1, putchr);
601 }
602
603 /*
604 * Output a plain backspace, without erasing the previous char.
605 */
606 void
putbs()607 putbs()
608 {
609 tputs(sc_backspace, 1, putchr);
610 }
611