]>
diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/miscellany/less-177/screen.c
2 * Routines which deal with the characteristics of the terminal.
3 * Uses termcap to be as terminal-independent as possible.
5 * {{ Someday this should be rewritten to use curses. }}
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
20 #if !TERMIO && defined(TIOCGWINSZ)
21 #include <sys/ioctl.h>
24 * For the Unix PC (ATT 7300 & 3B1):
25 * Since WIOCGETD is defined in sys/window.h, we can't use that to decide
26 * whether to include sys/window.h. Use SIGPHONE from signal.h instead.
30 #include <sys/window.h>
34 #if NEED_PTEM_H && defined(TIOCGWINSZ)
36 * All this just to get struct winsize. Sigh.
38 #include <sys/types.h>
39 #include <sys/stream.h>
44 * Strings passed to tputs() to do various terminal functions.
47 *sc_pad
, /* Pad string */
48 *sc_home
, /* Cursor home */
49 *sc_addline
, /* Add line, scroll down following lines */
50 *sc_lower_left
, /* Cursor to last line, first column */
51 *sc_move
, /* General cursor positioning */
52 *sc_clear
, /* Clear screen */
53 *sc_eol_clear
, /* Clear to end of line */
54 *sc_s_in
, /* Enter standout (highlighted) mode */
55 *sc_s_out
, /* Exit standout mode */
56 *sc_u_in
, /* Enter underline mode */
57 *sc_u_out
, /* Exit underline mode */
58 *sc_b_in
, /* Enter bold mode */
59 *sc_b_out
, /* Exit bold mode */
60 *sc_bl_in
, /* Enter blink mode */
61 *sc_bl_out
, /* Exit blink mode */
62 *sc_visual_bell
, /* Visual bell (flash screen) sequence */
63 *sc_backspace
, /* Backspace cursor */
64 *sc_init
, /* Startup terminal initialization */
65 *sc_deinit
; /* Exit terminal de-initialization */
67 static int init_done
= 0;
69 public int auto_wrap
; /* Terminal does \r\n when write past margin */
70 public int ignaw
; /* Terminal ignores \n immediately after wrap */
71 public int erase_char
, kill_char
; /* The user's erase and line-kill chars */
72 public int sc_width
, sc_height
; /* Height & width of screen */
73 public int bo_s_width
, bo_e_width
; /* Printing width of boldface seq */
74 public int ul_s_width
, ul_e_width
; /* Printing width of underline seq */
75 public int so_s_width
, so_e_width
; /* Printing width of standout seq */
76 public int bl_s_width
, bl_e_width
; /* Printing width of blink seq */
78 static char *cheaper();
81 * These two variables are sometimes defined in,
82 * and needed by, the termcap library.
83 * It may be necessary on some systems to declare them extern here.
85 /*extern*/ short ospeed
; /* Terminal output baud rate */
86 /*extern*/ char PC
; /* Pad character */
88 extern int quiet
; /* If VERY_QUIET, use visual bell for bell */
89 extern int know_dumb
; /* Don't complain about a dumb terminal */
90 extern int back_scroll
;
92 extern char *tgetstr();
94 extern char *getenv();
98 * Change terminal to "raw mode", or restore to "normal" mode.
100 * 1. An outstanding read will complete on receipt of a single keystroke.
101 * 2. Input is not echoed.
102 * 3. On output, \n is mapped to \r\n.
103 * 4. \t is NOT expanded into spaces.
104 * 5. Signal-causing characters such as ctrl-C (interrupt),
105 * etc. are NOT disabled.
106 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
112 static int curr_on
= 0;
119 static struct termio save_term
;
124 * Get terminal modes.
126 ioctl(2, TCGETA
, &s
);
129 * Save modes and set certain variables dependent on modes.
132 ospeed
= s
.c_cflag
& CBAUD
;
133 erase_char
= s
.c_cc
[VERASE
];
134 kill_char
= s
.c_cc
[VKILL
];
137 * Set the modes to the way we want them.
139 s
.c_lflag
&= ~(ICANON
|ECHO
|ECHOE
|ECHOK
|ECHONL
);
140 s
.c_oflag
|= (OPOST
|ONLCR
|TAB3
);
141 s
.c_oflag
&= ~(OCRNL
|ONOCR
|ONLRET
);
147 * Restore saved modes.
151 ioctl(2, TCSETAW
, &s
);
156 static struct sgttyb save_term
;
161 * Get terminal modes.
163 ioctl(2, TIOCGETP
, &s
);
166 * Save modes and set certain variables dependent on modes.
169 ospeed
= s
.sg_ospeed
;
170 erase_char
= s
.sg_erase
;
171 kill_char
= s
.sg_kill
;
174 * Set the modes to the way we want them.
176 s
.sg_flags
|= CBREAK
;
177 s
.sg_flags
&= ~(ECHO
|XTABS
);
181 * Restore saved modes.
185 ioctl(2, TIOCSETN
, &s
);
199 * User knows this is a dumb terminal, so don't tell him.
204 error("WARNING: terminal cannot %s", &parg
);
208 * Get size of the output screen.
211 scrsize(p_height
, p_width
)
225 if (ioctl(2, TIOCGWINSZ
, &w
) == 0 && w
.ws_row
> 0)
226 *p_height
= w
.ws_row
;
230 if (ioctl(2, WIOCGETD
, &w
) == 0 && w
.uw_height
> 0)
231 *p_height
= w
.uw_height
/w
.uw_vs
;
235 if ((s
= getenv("LINES")) != NULL
)
238 *p_height
= tgetnum("li");
244 if (ioctl(2, TIOCGWINSZ
, &w
) == 0 && w
.ws_col
> 0)
248 if (ioctl(2, WIOCGETD
, &w
) == 0 && w
.uw_width
> 0)
249 *p_width
= w
.uw_width
/w
.uw_hs
;
253 if ((s
= getenv("COLUMNS")) != NULL
)
256 *p_width
= tgetnum("co");
263 * Get terminal capabilities via termcap.
269 register char *t1
, *t2
;
274 static char sbuf
[1024];
277 * Find out what kind of terminal this is.
279 if ((term
= getenv("TERM")) == NULL
)
281 if (tgetent(termbuf
, term
) <= 0)
282 strcpy(termbuf
, "dumb:hc:");
284 hard
= tgetflag("hc");
287 * Get size of the screen.
289 scrsize(&sc_height
, &sc_width
);
292 swindow
= sc_height
- 1;
294 auto_wrap
= tgetflag("am");
295 ignaw
= tgetflag("xn");
298 * Assumes termcap variable "sg" is the printing width of:
299 * the standout sequence, the end standout sequence,
300 * the underline sequence, the end underline sequence,
301 * the boldface sequence, and the end boldface sequence.
303 if ((so_s_width
= tgetnum("sg")) < 0)
305 so_e_width
= so_s_width
;
307 bo_s_width
= bo_e_width
= so_s_width
;
308 ul_s_width
= ul_e_width
= so_s_width
;
309 bl_s_width
= bl_e_width
= so_s_width
;
312 * Get various string-valued capabilities.
316 sc_pad
= tgetstr("pc", &sp
);
320 sc_init
= tgetstr("ti", &sp
);
324 sc_deinit
= tgetstr("te", &sp
);
325 if (sc_deinit
== NULL
)
328 sc_eol_clear
= tgetstr("ce", &sp
);
329 if (hard
|| sc_eol_clear
== NULL
|| *sc_eol_clear
== '\0')
331 cannot("clear to end of line");
335 sc_clear
= tgetstr("cl", &sp
);
336 if (hard
|| sc_clear
== NULL
|| *sc_clear
== '\0')
338 cannot("clear screen");
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_bl_in
= tgetstr("mb", &sp
);
382 if (hard
|| sc_bl_in
== NULL
)
385 sc_bl_out
= sc_s_out
;
388 sc_bl_out
= sc_b_out
;
391 sc_visual_bell
= tgetstr("vb", &sp
);
392 if (hard
|| sc_visual_bell
== NULL
)
399 sc_backspace
= tgetstr("bc", &sp
);
400 if (sc_backspace
== NULL
|| *sc_backspace
== '\0')
405 * Choose between using "ho" and "cm" ("home" and "cursor move")
406 * to move the cursor to the upper left corner of the screen.
408 t1
= tgetstr("ho", &sp
);
409 if (hard
|| t1
== NULL
)
411 if (*sc_move
== '\0')
415 strcpy(sp
, tgoto(sc_move
, 0, 0));
417 sp
+= strlen(sp
) + 1;
419 sc_home
= cheaper(t1
, t2
, "home cursor", "|\b^");
422 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
423 * to move the cursor to the lower left corner of the screen.
425 t1
= tgetstr("ll", &sp
);
426 if (hard
|| t1
== NULL
)
428 if (*sc_move
== '\0')
432 strcpy(sp
, tgoto(sc_move
, 0, sc_height
-1));
434 sp
+= strlen(sp
) + 1;
436 sc_lower_left
= cheaper(t1
, t2
,
437 "move cursor to lower left of screen", "\r");
440 * Choose between using "al" or "sr" ("add line" or "scroll reverse")
441 * to add a line at the top of the screen.
443 t1
= tgetstr("al", &sp
);
444 if (hard
|| t1
== NULL
)
446 t2
= tgetstr("sr", &sp
);
447 if (hard
|| t2
== NULL
)
449 sc_addline
= cheaper(t1
, t2
, "scroll backwards", "");
450 if (*sc_addline
== '\0')
453 * Force repaint on any backward movement.
460 * Return the cost of displaying a termcap string.
461 * We use the trick of calling tputs, but as a char printing function
462 * we give it inc_costcount, which just increments "costcount".
463 * This tells us how many chars would be printed by using this string.
464 * {{ Couldn't we just use strlen? }}
466 static int costcount
;
481 tputs(t
, sc_height
, inc_costcount
);
486 * Return the "best" of the two given termcap strings.
487 * The best, if both exist, is the one with the lower
488 * cost (see cost() function).
491 cheaper(t1
, t2
, doit
, def
)
496 if (*t1
== '\0' && *t2
== '\0')
505 if (cost(t1
) < cost(t2
))
512 * Below are the functions which perform all the
513 * terminal-specific screen manipulation.
518 * Initialize terminal
523 tputs(sc_init
, sc_height
, putchr
);
528 * Deinitialize terminal
535 tputs(sc_deinit
, sc_height
, putchr
);
540 * Home cursor (move to upper left corner of screen).
545 tputs(sc_home
, 1, putchr
);
549 * Add a blank line (called with cursor at home).
550 * Should scroll the display down.
555 tputs(sc_addline
, sc_height
, putchr
);
559 * Move cursor to lower left corner of screen.
564 tputs(sc_lower_left
, 1, putchr
);
568 * Ring the terminal bell.
573 if (quiet
== VERY_QUIET
)
580 * Output the "visual bell", if there is one.
585 if (*sc_visual_bell
== '\0')
587 tputs(sc_visual_bell
, sc_height
, putchr
);
596 tputs(sc_clear
, sc_height
, putchr
);
600 * Clear from the cursor to the end of the cursor's line.
601 * {{ This must not move the cursor. }}
606 tputs(sc_eol_clear
, 1, putchr
);
610 * Begin "standout" (bold, underline, or whatever).
615 tputs(sc_s_in
, 1, putchr
);
624 tputs(sc_s_out
, 1, putchr
);
628 * Begin "underline" (hopefully real underlining,
629 * otherwise whatever the terminal provides).
634 tputs(sc_u_in
, 1, putchr
);
643 tputs(sc_u_out
, 1, putchr
);
652 tputs(sc_b_in
, 1, putchr
);
661 tputs(sc_b_out
, 1, putchr
);
670 tputs(sc_bl_in
, 1, putchr
);
679 tputs(sc_bl_out
, 1, putchr
);
683 * Erase the character to the left of the cursor
684 * and move the cursor left.
690 * Try to erase the previous character by overstriking with a space.
692 tputs(sc_backspace
, 1, putchr
);
694 tputs(sc_backspace
, 1, putchr
);
698 * Output a plain backspace, without erasing the previous char.
703 tputs(sc_backspace
, 1, putchr
);