2 * Copyright (C) 1984-2012 Mark Nudelman
3 * Modified for use with illumos by Garrett D'Amore.
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
13 * Routines which deal with the characteristics of the terminal.
14 * Uses termcap to be as terminal-independent as possible.
17 #include <sys/ioctl.h>
26 #define DEFAULT_TERM "unknown"
29 * Strings passed to tputs() to do various terminal functions.
32 *sc_home
, /* Cursor home */
33 *sc_addline
, /* Add line, scroll down following lines */
34 *sc_lower_left
, /* Cursor to last line, first column */
35 *sc_return
, /* Cursor to beginning of current line */
36 *sc_move
, /* General cursor positioning */
37 *sc_clear
, /* Clear screen */
38 *sc_eol_clear
, /* Clear to end of line */
39 *sc_eos_clear
, /* Clear to end of screen */
40 *sc_s_in
, /* Enter standout (highlighted) mode */
41 *sc_s_out
, /* Exit standout mode */
42 *sc_u_in
, /* Enter underline mode */
43 *sc_u_out
, /* Exit underline mode */
44 *sc_b_in
, /* Enter bold mode */
45 *sc_b_out
, /* Exit bold mode */
46 *sc_bl_in
, /* Enter blink mode */
47 *sc_bl_out
, /* Exit blink mode */
48 *sc_visual_bell
, /* Visual bell (flash screen) sequence */
49 *sc_backspace
, /* Backspace cursor */
50 *sc_s_keypad
, /* Start keypad mode */
51 *sc_e_keypad
, /* End keypad mode */
52 *sc_init
, /* Startup terminal initialization */
53 *sc_deinit
; /* Exit terminal de-initialization */
55 static int init_done
= 0;
57 int auto_wrap
; /* Terminal does \r\n when write past margin */
58 int ignaw
; /* Terminal ignores \n immediately after wrap */
59 int erase_char
; /* The user's erase char */
60 int erase2_char
; /* The user's other erase char */
61 int kill_char
; /* The user's line-kill char */
62 int werase_char
; /* The user's word-erase char */
63 int sc_width
, sc_height
; /* Height & width of screen */
64 int bo_s_width
, bo_e_width
; /* Printing width of boldface seq */
65 int ul_s_width
, ul_e_width
; /* Printing width of underline seq */
66 int so_s_width
, so_e_width
; /* Printing width of standout seq */
67 int bl_s_width
, bl_e_width
; /* Printing width of blink seq */
68 int can_goto_line
; /* Can move cursor to any line */
69 int missing_cap
= 0; /* Some capability is missing */
70 static int above_mem
; /* Memory retained above screen */
71 static int below_mem
; /* Memory retained below screen */
73 static int attrmode
= AT_NORMAL
;
76 static char *cheaper(char *, char *, char *);
77 static void tmodes(char *, char *, char **, char **, char *, char *);
79 extern int quiet
; /* If VERY_QUIET, use visual bell for bell */
80 extern int no_back_scroll
;
84 extern volatile sig_atomic_t sigs
;
86 extern int screen_trashed
;
88 extern int top_scroll
;
90 extern int hilite_search
;
93 * Change terminal to "raw mode", or restore to "normal" mode.
95 * 1. An outstanding read will complete on receipt of a single keystroke.
96 * 2. Input is not echoed.
97 * 3. On output, \n is mapped to \r\n.
98 * 4. \t is NOT expanded into spaces.
99 * 5. Signal-causing characters such as ctrl-C (interrupt),
100 * etc. are NOT disabled.
101 * It doesn't matter whether an input \n is mapped to \r, or vice versa.
106 static int curr_on
= 0;
108 static struct termios save_term
;
109 static int saved_term
= 0;
113 erase2_char
= '\b'; /* in case OS doesn't know about erase2 */
117 * Get terminal modes.
119 (void) tcgetattr(tty
, &s
);
122 * Save modes and set certain variables dependent on modes.
129 erase_char
= s
.c_cc
[VERASE
];
131 erase2_char
= s
.c_cc
[VERASE2
];
133 kill_char
= s
.c_cc
[VKILL
];
135 werase_char
= s
.c_cc
[VWERASE
];
139 * Set the modes to the way we want them.
141 s
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ECHOK
| ECHONL
);
144 #define TAB3 0 /* Its a lie, but TAB3 isn't defined by POSIX. */
146 s
.c_oflag
|= (TAB3
| OPOST
| ONLCR
);
147 s
.c_oflag
&= ~(OCRNL
| ONOCR
| ONLRET
);
158 * Restore saved modes.
162 (void) tcsetattr(tty
, TCSASOFT
| TCSADRAIN
, &s
);
167 * Some glue to prevent calling termcap functions if tgetent() failed.
172 * Get size of the output screen.
177 int sys_height
= 0, sys_width
= 0, n
;
181 #define DEF_SC_WIDTH 80
182 #define DEF_SC_HEIGHT 24
184 if (ioctl(2, TIOCGWINSZ
, &w
) == 0) {
186 sys_height
= w
.ws_row
;
188 sys_width
= w
.ws_col
;
192 sc_height
= sys_height
;
193 else if ((s
= lgetenv("LINES")) != NULL
)
195 else if (!hardcopy
&& (n
= lines
) > 0)
198 sc_height
= DEF_SC_HEIGHT
;
201 sc_width
= sys_width
;
202 else if ((s
= lgetenv("COLUMNS")) != NULL
)
204 else if (!hardcopy
&& (n
= columns
) > 0)
207 sc_width
= DEF_SC_WIDTH
;
211 * Return the characters actually input by a "special" key.
214 special_key_str(int key
)
217 static char ctrlk
[] = { CONTROL('K'), 0 };
263 * Get terminal capabilities via termcap.
273 * Find out what kind of terminal this is.
275 if ((term
= lgetenv("TERM")) == NULL
)
279 if (setupterm(term
, 1, &err
) < 0) {
283 errx(1, "%s: unknown terminal type", term
);
289 * Get size of the screen.
294 auto_wrap
= hardcopy
? 0 : auto_right_margin
;
295 ignaw
= hardcopy
? 0 : eat_newline_glitch
;
296 above_mem
= hardcopy
? 0 : memory_above
;
297 below_mem
= hardcopy
? 0 : memory_below
;
300 * Assumes termcap variable "sg" is the printing width of:
301 * the standout sequence, the end standout sequence,
302 * the underline sequence, the end underline sequence,
303 * the boldface sequence, and the end boldface sequence.
305 if (hardcopy
|| (so_s_width
= magic_cookie_glitch
) < 0)
307 so_e_width
= so_s_width
;
309 bo_s_width
= bo_e_width
= so_s_width
;
310 ul_s_width
= ul_e_width
= so_s_width
;
311 bl_s_width
= bl_e_width
= so_s_width
;
313 if (so_s_width
> 0 || so_e_width
> 0)
315 * Disable highlighting by default on magic cookie terminals.
316 * Turning on highlighting might change the displayed width
317 * of a line, causing the display to get messed up.
318 * The user can turn it back on with -g,
319 * but she won't like the results.
324 * Get various string-valued capabilities.
327 sc_s_keypad
= keypad_xmit
;
328 if (hardcopy
|| sc_s_keypad
== NULL
)
330 sc_e_keypad
= keypad_local
;
331 if (hardcopy
|| sc_e_keypad
== NULL
)
334 sc_init
= enter_ca_mode
;
335 if (hardcopy
|| sc_init
== NULL
)
338 sc_deinit
= exit_ca_mode
;
339 if (hardcopy
|| sc_deinit
== NULL
)
342 sc_eol_clear
= clr_eol
;
343 if (hardcopy
|| sc_eol_clear
== NULL
|| *sc_eol_clear
== '\0') {
348 sc_eos_clear
= clr_eos
;
350 (hardcopy
|| sc_eos_clear
== NULL
|| *sc_eos_clear
== '\0')) {
355 sc_clear
= clear_screen
;
356 if (hardcopy
|| sc_clear
== NULL
|| *sc_clear
== '\0') {
361 sc_move
= cursor_address
;
362 if (hardcopy
|| sc_move
== NULL
|| *sc_move
== '\0') {
364 * This is not an error here, because we don't
365 * always need sc_move.
366 * We need it only if we don't have home or lower-left.
374 tmodes(enter_standout_mode
, exit_standout_mode
, &sc_s_in
, &sc_s_out
,
376 tmodes(enter_underline_mode
, exit_underline_mode
, &sc_u_in
, &sc_u_out
,
378 tmodes(enter_bold_mode
, exit_attribute_mode
, &sc_b_in
, &sc_b_out
,
380 tmodes(enter_blink_mode
, exit_attribute_mode
, &sc_bl_in
, &sc_bl_out
,
383 sc_visual_bell
= flash_screen
;
384 if (hardcopy
|| sc_visual_bell
== NULL
)
390 * Choose between using "ho" and "cm" ("home" and "cursor move")
391 * to move the cursor to the upper left corner of the screen.
394 if (hardcopy
|| t1
== NULL
)
396 if (*sc_move
== '\0') {
399 t2
= estrdup(tparm(sc_move
, 0, 0, 0, 0, 0, 0, 0, 0, 0));
401 sc_home
= cheaper(t1
, t2
, "|\b^");
404 * Choose between using "ll" and "cm" ("lower left" and "cursor move")
405 * to move the cursor to the lower left corner of the screen.
408 if (hardcopy
|| t1
== NULL
)
410 if (*sc_move
== '\0') {
413 t2
= estrdup(tparm(sc_move
, sc_height
-1,
414 0, 0, 0, 0, 0, 0, 0, 0));
416 sc_lower_left
= cheaper(t1
, t2
, "\r");
419 * Get carriage return string.
421 sc_return
= carriage_return
;
422 if (hardcopy
|| sc_return
== NULL
)
426 * Choose between using insert_line or scroll_reverse
427 * to add a line at the top of the screen.
430 if (hardcopy
|| t1
== NULL
)
433 if (hardcopy
|| t2
== NULL
)
438 sc_addline
= cheaper(t1
, t2
, "");
439 if (*sc_addline
== '\0') {
441 * Force repaint on any backward movement.
448 * Return the cost of displaying a termcap string.
449 * We use the trick of calling tputs, but as a char printing function
450 * we give it inc_costcount, which just increments "costcount".
451 * This tells us how many chars would be printed by using this string.
452 * {{ Couldn't we just use strlen? }}
454 static int costcount
;
467 (void) tputs(t
, sc_height
, inc_costcount
);
472 * Return the "best" of the two given termcap strings.
473 * The best, if both exist, is the one with the lower
474 * cost (see cost() function).
477 cheaper(char *t1
, char *t2
, char *def
)
479 if (*t1
== '\0' && *t2
== '\0') {
487 if (cost(t1
) < cost(t2
))
493 tmodes(char *incap
, char *outcap
, char **instr
, char **outstr
,
494 char *def_instr
, char *def_outstr
)
505 if (*instr
== NULL
) {
508 *outstr
= def_outstr
;
513 /* No specific out capability; use exit_attribute_mode. */
514 *outstr
= exit_attribute_mode
;
516 /* Don't even have that, use an empty string */
521 * Below are the functions which perform all the
522 * terminal-specific screen manipulation.
526 * Initialize terminal
532 (void) tputs(sc_init
, sc_height
, putchr
);
534 (void) tputs(sc_s_keypad
, sc_height
, putchr
);
539 * This is nice to terminals with no alternate screen,
540 * but with saved scrolled-off-the-top lines. This way,
541 * no previous line is lost, but we start with a whole
544 for (i
= 1; i
< sc_height
; i
++)
552 * Deinitialize terminal
560 (void) tputs(sc_e_keypad
, sc_height
, putchr
);
562 (void) tputs(sc_deinit
, sc_height
, putchr
);
567 * Home cursor (move to upper left corner of screen).
572 (void) tputs(sc_home
, 1, putchr
);
576 * Add a blank line (called with cursor at home).
577 * Should scroll the display down.
582 (void) tputs(sc_addline
, sc_height
, putchr
);
586 * Move cursor to lower left corner of screen.
591 (void) tputs(sc_lower_left
, 1, putchr
);
595 * Move cursor to left position of current line.
600 (void) tputs(sc_return
, 1, putchr
);
604 * Goto a specific line on the screen.
607 goto_line(int slinenum
)
609 (void) tputs(tparm(sc_move
, slinenum
, 0, 0, 0, 0, 0, 0, 0, 0), 1,
614 * Output the "visual bell", if there is one.
619 if (*sc_visual_bell
== '\0')
621 (void) tputs(sc_visual_bell
, sc_height
, putchr
);
630 (void) putchr(CONTROL('G'));
634 * Ring the terminal bell.
639 if (quiet
== VERY_QUIET
)
651 (void) tputs(sc_clear
, sc_height
, putchr
);
655 * Clear from the cursor to the end of the cursor's line.
656 * {{ This must not move the cursor. }}
661 (void) tputs(sc_eol_clear
, 1, putchr
);
665 * Clear the current line.
666 * Clear the screen if there's off-screen memory below the display.
672 (void) tputs(sc_eos_clear
, 1, putchr
);
674 (void) tputs(sc_eol_clear
, 1, putchr
);
678 * Clear the bottom line of the display.
679 * Leave the cursor at the beginning of the bottom line.
685 * If we're in a non-normal attribute mode, temporarily exit
686 * the mode while we do the clear. Some terminals fill the
687 * cleared area with the current attribute.
694 if (attrmode
== AT_NORMAL
)
698 int saved_attrmode
= attrmode
;
702 at_enter(saved_attrmode
);
709 attr
= apply_at_specials(attr
);
711 /* The one with the most priority is last. */
712 if (attr
& AT_UNDERLINE
)
713 (void) tputs(sc_u_in
, 1, putchr
);
715 (void) tputs(sc_b_in
, 1, putchr
);
717 (void) tputs(sc_bl_in
, 1, putchr
);
718 if (attr
& AT_STANDOUT
)
719 (void) tputs(sc_s_in
, 1, putchr
);
727 /* Undo things in the reverse order we did them. */
728 if (attrmode
& AT_STANDOUT
)
729 (void) tputs(sc_s_out
, 1, putchr
);
730 if (attrmode
& AT_BLINK
)
731 (void) tputs(sc_bl_out
, 1, putchr
);
732 if (attrmode
& AT_BOLD
)
733 (void) tputs(sc_b_out
, 1, putchr
);
734 if (attrmode
& AT_UNDERLINE
)
735 (void) tputs(sc_u_out
, 1, putchr
);
737 attrmode
= AT_NORMAL
;
743 int new_attrmode
= apply_at_specials(attr
);
744 int ignore_modes
= AT_ANSI
;
746 if ((new_attrmode
& ~ignore_modes
) != (attrmode
& ~ignore_modes
)) {
753 is_at_equiv(int attr1
, int attr2
)
755 attr1
= apply_at_specials(attr1
);
756 attr2
= apply_at_specials(attr2
);
758 return (attr1
== attr2
);
762 apply_at_specials(int attr
)
764 if (attr
& AT_BINARY
)
766 if (attr
& AT_HILITE
)
768 attr
&= ~(AT_BINARY
|AT_HILITE
);
774 * Output a plain backspace, without erasing the previous char.
779 (void) tputs(sc_backspace
, 1, putchr
);