1 /* terminal.c -- controlling the terminal with termcap. */
3 /* Copyright (C) 1996-2017 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
29 #include "posixstat.h"
31 #if defined (HAVE_SYS_FILE_H)
32 # include <sys/file.h>
33 #endif /* HAVE_SYS_FILE_H */
35 #if defined (HAVE_UNISTD_H)
37 #endif /* HAVE_UNISTD_H */
39 #if defined (HAVE_STDLIB_H)
42 # include "ansi_stdlib.h"
43 #endif /* HAVE_STDLIB_H */
45 #if defined (HAVE_LOCALE_H)
51 /* System-specific feature definitions and include files. */
59 #if defined (HAVE_SYS_IOCTL_H)
60 # include <sys/ioctl.h> /* include for declaration of ioctl */
64 /* Some standard library routines. */
68 #include "rlprivate.h"
72 #if defined (__MINGW32__)
76 static void _win_get_screensize
PARAMS((int *, int *));
80 static void _emx_get_screensize
PARAMS((int *, int *));
83 /* If the calling application sets this to a non-zero value, readline will
84 use the $LINES and $COLUMNS environment variables to set its idea of the
85 window size before interrogating the kernel. */
86 int rl_prefer_env_winsize
= 0;
88 /* If this is non-zero, readline will set LINES and COLUMNS in the
89 environment when it handles SIGWINCH. */
90 int rl_change_environment
= 1;
92 /* **************************************************************** */
94 /* Terminal and Termcap */
96 /* **************************************************************** */
99 static char *term_buffer
= (char *)NULL
;
100 static char *term_string_buffer
= (char *)NULL
;
103 static int tcap_initialized
;
105 #if !defined (__linux__) && !defined (NCURSES_VERSION)
106 # if defined (__EMX__) || defined (NEED_EXTERN_PC)
108 # endif /* __EMX__ || NEED_EXTERN_PC */
110 #endif /* !__linux__ && !NCURSES_VERSION */
112 /* Some strings to control terminal actions. These are output by tputs (). */
113 char *_rl_term_clreol
;
114 char *_rl_term_clrpag
;
115 char *_rl_term_clrscroll
;
117 char *_rl_term_backspace
;
121 /* Non-zero if we determine that the terminal can do character insertion. */
122 int _rl_terminal_can_insert
= 0;
124 /* How to insert characters. */
131 /* How to delete characters. */
135 /* How to move forward a char, non-destructively */
136 char *_rl_term_forward_char
;
138 /* How to go up a line. */
141 /* A visible bell; char if the terminal can be made to flash the screen. */
142 static char *_rl_visible_bell
;
144 /* Non-zero means the terminal can auto-wrap lines. */
145 int _rl_term_autowrap
= -1;
147 /* Non-zero means that this terminal has a meta key. */
148 static int term_has_meta
;
150 /* The sequences to write to turn on and off the meta key, if this
152 static char *_rl_term_mm
;
153 static char *_rl_term_mo
;
155 /* The key sequences output by the arrow keys, if this terminal has any. */
156 static char *_rl_term_ku
;
157 static char *_rl_term_kd
;
158 static char *_rl_term_kr
;
159 static char *_rl_term_kl
;
161 /* How to initialize and reset the arrow keys, if this terminal has any. */
162 static char *_rl_term_ks
;
163 static char *_rl_term_ke
;
165 /* The key sequences sent by the Home and End keys, if any. */
166 static char *_rl_term_kh
;
167 static char *_rl_term_kH
;
168 static char *_rl_term_at7
; /* @7 */
171 static char *_rl_term_kD
;
174 static char *_rl_term_kI
;
177 static char *_rl_term_vs
; /* very visible */
178 static char *_rl_term_ve
; /* normal */
180 static void bind_termcap_arrow_keys
PARAMS((Keymap
));
182 /* Variables that hold the screen dimensions, used by the display code. */
183 int _rl_screenwidth
, _rl_screenheight
, _rl_screenchars
;
185 /* Non-zero means the user wants to enable the keypad. */
186 int _rl_enable_keypad
;
188 /* Non-zero means the user wants to enable a meta key. */
189 int _rl_enable_meta
= 1;
191 #if defined (__EMX__)
193 _emx_get_screensize (int *swp
, int *shp
)
206 #if defined (__MINGW32__)
208 _win_get_screensize (int *swp
, int *shp
)
211 CONSOLE_SCREEN_BUFFER_INFO scr
;
213 hConOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
214 if (hConOut
!= INVALID_HANDLE_VALUE
)
216 if (GetConsoleScreenBufferInfo (hConOut
, &scr
))
219 *shp
= scr
.srWindow
.Bottom
- scr
.srWindow
.Top
+ 1;
225 /* Get readline's idea of the screen size. TTY is a file descriptor open
226 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
227 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
228 non-null serve to check whether or not we have initialized termcap. */
230 _rl_get_screen_size (int tty
, int ignore_env
)
233 #if defined (TIOCGWINSZ)
234 struct winsize window_size
;
235 #endif /* TIOCGWINSZ */
239 #if defined (TIOCGWINSZ)
240 if (ioctl (tty
, TIOCGWINSZ
, &window_size
) == 0)
242 wc
= (int) window_size
.ws_col
;
243 wr
= (int) window_size
.ws_row
;
245 #endif /* TIOCGWINSZ */
247 #if defined (__EMX__)
248 _emx_get_screensize (&wc
, &wr
);
249 #elif defined (__MINGW32__)
250 _win_get_screensize (&wc
, &wr
);
253 if (ignore_env
|| rl_prefer_env_winsize
== 0)
255 _rl_screenwidth
= wc
;
256 _rl_screenheight
= wr
;
259 _rl_screenwidth
= _rl_screenheight
= -1;
261 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
262 is unset. If we prefer the environment, check it first before
263 assigning the value returned by the kernel. */
264 if (_rl_screenwidth
<= 0)
266 if (ignore_env
== 0 && (ss
= sh_get_env_value ("COLUMNS")))
267 _rl_screenwidth
= atoi (ss
);
269 if (_rl_screenwidth
<= 0)
270 _rl_screenwidth
= wc
;
272 #if defined (__DJGPP__)
273 if (_rl_screenwidth
<= 0)
274 _rl_screenwidth
= ScreenCols ();
276 if (_rl_screenwidth
<= 0 && term_string_buffer
)
277 _rl_screenwidth
= tgetnum ("co");
281 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
283 if (_rl_screenheight
<= 0)
285 if (ignore_env
== 0 && (ss
= sh_get_env_value ("LINES")))
286 _rl_screenheight
= atoi (ss
);
288 if (_rl_screenheight
<= 0)
289 _rl_screenheight
= wr
;
291 #if defined (__DJGPP__)
292 if (_rl_screenheight
<= 0)
293 _rl_screenheight
= ScreenRows ();
295 if (_rl_screenheight
<= 0 && term_string_buffer
)
296 _rl_screenheight
= tgetnum ("li");
300 /* If all else fails, default to 80x24 terminal. */
301 if (_rl_screenwidth
<= 1)
302 _rl_screenwidth
= 80;
304 if (_rl_screenheight
<= 0)
305 _rl_screenheight
= 24;
307 /* If we're being compiled as part of bash, set the environment
308 variables $LINES and $COLUMNS to new values. Otherwise, just
309 do a pair of putenv () or setenv () calls. */
310 if (rl_change_environment
)
311 sh_set_lines_and_columns (_rl_screenheight
, _rl_screenwidth
);
313 if (_rl_term_autowrap
== 0)
316 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
320 _rl_set_screen_size (int rows
, int cols
)
322 if (_rl_term_autowrap
== -1)
323 _rl_init_terminal_io (rl_terminal_name
);
326 _rl_screenheight
= rows
;
329 _rl_screenwidth
= cols
;
330 if (_rl_term_autowrap
== 0)
334 if (rows
> 0 || cols
> 0)
335 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
339 rl_set_screen_size (int rows
, int cols
)
341 _rl_set_screen_size (rows
, cols
);
345 rl_get_screen_size (int *rows
, int *cols
)
348 *rows
= _rl_screenheight
;
350 *cols
= _rl_screenwidth
;
354 rl_reset_screen_size (void)
356 _rl_get_screen_size (fileno (rl_instream
), 0);
360 _rl_sigwinch_resize_terminal (void)
362 _rl_get_screen_size (fileno (rl_instream
), 1);
366 rl_resize_terminal (void)
368 _rl_get_screen_size (fileno (rl_instream
), 1);
371 if (CUSTOM_REDISPLAY_FUNC ())
372 rl_forced_update_display ();
373 else if (RL_ISSTATE(RL_STATE_REDISPLAYING
) == 0)
374 _rl_redisplay_after_sigwinch ();
379 const char * const tc_var
;
383 /* This should be kept sorted, just in case we decide to change the
384 search algorithm to something smarter. */
385 static const struct _tc_string tc_strings
[] =
387 { "@7", &_rl_term_at7
},
388 { "DC", &_rl_term_DC
},
389 { "E3", &_rl_term_clrscroll
},
390 { "IC", &_rl_term_IC
},
391 { "ce", &_rl_term_clreol
},
392 { "cl", &_rl_term_clrpag
},
393 { "cr", &_rl_term_cr
},
394 { "dc", &_rl_term_dc
},
395 { "ei", &_rl_term_ei
},
396 { "ic", &_rl_term_ic
},
397 { "im", &_rl_term_im
},
398 { "kD", &_rl_term_kD
}, /* delete */
399 { "kH", &_rl_term_kH
}, /* home down ?? */
400 { "kI", &_rl_term_kI
}, /* insert */
401 { "kd", &_rl_term_kd
},
402 { "ke", &_rl_term_ke
}, /* end keypad mode */
403 { "kh", &_rl_term_kh
}, /* home */
404 { "kl", &_rl_term_kl
},
405 { "kr", &_rl_term_kr
},
406 { "ks", &_rl_term_ks
}, /* start keypad mode */
407 { "ku", &_rl_term_ku
},
408 { "le", &_rl_term_backspace
},
409 { "mm", &_rl_term_mm
},
410 { "mo", &_rl_term_mo
},
411 { "nd", &_rl_term_forward_char
},
412 { "pc", &_rl_term_pc
},
413 { "up", &_rl_term_up
},
414 { "vb", &_rl_visible_bell
},
415 { "vs", &_rl_term_vs
},
416 { "ve", &_rl_term_ve
},
419 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
421 /* Read the desired terminal capability strings into BP. The capabilities
422 are described in the TC_STRINGS table. */
424 get_term_capabilities (char **bp
)
426 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
429 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
430 *(tc_strings
[i
].tc_value
) = tgetstr ((char *)tc_strings
[i
].tc_var
, bp
);
432 tcap_initialized
= 1;
436 _rl_init_terminal_io (const char *terminal_name
)
440 int tty
, tgetent_ret
;
442 term
= terminal_name
? terminal_name
: sh_get_env_value ("TERM");
443 _rl_term_clrpag
= _rl_term_cr
= _rl_term_clreol
= _rl_term_clrscroll
= (char *)NULL
;
444 tty
= rl_instream
? fileno (rl_instream
) : 0;
450 _rl_term_im
= _rl_term_ei
= _rl_term_ic
= _rl_term_IC
= (char *)NULL
;
451 _rl_term_up
= _rl_term_dc
= _rl_term_DC
= _rl_visible_bell
= (char *)NULL
;
452 _rl_term_ku
= _rl_term_kd
= _rl_term_kl
= _rl_term_kr
= (char *)NULL
;
453 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
454 _rl_terminal_can_insert
= term_has_meta
= _rl_term_autowrap
= 0;
456 _rl_term_backspace
= (char *)NULL
;
457 _rl_term_goto
= _rl_term_pc
= _rl_term_ip
= (char *)NULL
;
458 _rl_term_ks
= _rl_term_ke
=_rl_term_vs
= _rl_term_ve
= (char *)NULL
;
459 _rl_term_kh
= _rl_term_kH
= _rl_term_at7
= _rl_term_kI
= (char *)NULL
;
460 #if defined(HACK_TERMCAP_MOTION)
461 _rl_term_forward_char
= (char *)NULL
;
464 _rl_get_screen_size (tty
, 0);
465 #else /* !__MSDOS__ */
466 /* I've separated this out for later work on not calling tgetent at all
467 if the calling application has supplied a custom redisplay function,
468 (and possibly if the application has supplied a custom input function). */
469 if (CUSTOM_REDISPLAY_FUNC())
475 if (term_string_buffer
== 0)
476 term_string_buffer
= (char *)xmalloc(2032);
478 if (term_buffer
== 0)
479 term_buffer
= (char *)xmalloc(4080);
481 buffer
= term_string_buffer
;
483 tgetent_ret
= tgetent (term_buffer
, term
);
486 if (tgetent_ret
<= 0)
488 FREE (term_string_buffer
);
490 buffer
= term_buffer
= term_string_buffer
= (char *)NULL
;
492 _rl_term_autowrap
= 0; /* used by _rl_get_screen_size */
494 /* Allow calling application to set default height and width, using
495 rl_set_screen_size */
496 if (_rl_screenwidth
<= 0 || _rl_screenheight
<= 0)
498 #if defined (__EMX__)
499 _emx_get_screensize (&_rl_screenwidth
, &_rl_screenheight
);
502 _rl_get_screen_size (tty
, 0);
503 #endif /* !__EMX__ */
507 if (_rl_screenwidth
<= 0 || _rl_screenheight
<= 0)
509 _rl_screenwidth
= 79;
510 _rl_screenheight
= 24;
513 /* Everything below here is used by the redisplay code (tputs). */
514 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
516 _rl_term_im
= _rl_term_ei
= _rl_term_ic
= _rl_term_IC
= (char *)NULL
;
517 _rl_term_up
= _rl_term_dc
= _rl_term_DC
= _rl_visible_bell
= (char *)NULL
;
518 _rl_term_ku
= _rl_term_kd
= _rl_term_kl
= _rl_term_kr
= (char *)NULL
;
519 _rl_term_kh
= _rl_term_kH
= _rl_term_kI
= _rl_term_kD
= (char *)NULL
;
520 _rl_term_ks
= _rl_term_ke
= _rl_term_at7
= (char *)NULL
;
521 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
522 _rl_term_ve
= _rl_term_vs
= (char *)NULL
;
523 _rl_term_forward_char
= (char *)NULL
;
524 _rl_terminal_can_insert
= term_has_meta
= 0;
526 /* Reasonable defaults for tgoto(). Readline currently only uses
527 tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
528 change that later... */
530 BC
= _rl_term_backspace
= "\b";
536 get_term_capabilities (&buffer
);
538 /* Set up the variables that the termcap library expects the application
540 PC
= _rl_term_pc
? *_rl_term_pc
: 0;
541 BC
= _rl_term_backspace
;
547 _rl_term_autowrap
= tgetflag ("am") && tgetflag ("xn");
549 /* Allow calling application to set default height and width, using
550 rl_set_screen_size */
551 if (_rl_screenwidth
<= 0 || _rl_screenheight
<= 0)
552 _rl_get_screen_size (tty
, 0);
554 /* "An application program can assume that the terminal can do
555 character insertion if *any one of* the capabilities `IC',
556 `im', `ic' or `ip' is provided." But we can't do anything if
557 only `ip' is provided, so... */
558 _rl_terminal_can_insert
= (_rl_term_IC
|| _rl_term_im
|| _rl_term_ic
);
560 /* Check to see if this terminal has a meta key and clear the capability
561 variables if there is none. */
562 term_has_meta
= tgetflag ("km") != 0;
563 if (term_has_meta
== 0)
564 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
565 #endif /* !__MSDOS__ */
567 /* Attempt to find and bind the arrow keys. Do not override already
568 bound keys in an overzealous attempt, however. */
570 bind_termcap_arrow_keys (emacs_standard_keymap
);
572 #if defined (VI_MODE)
573 bind_termcap_arrow_keys (vi_movement_keymap
);
574 bind_termcap_arrow_keys (vi_insertion_keymap
);
580 /* Bind the arrow key sequences from the termcap description in MAP. */
582 bind_termcap_arrow_keys (Keymap map
)
586 xkeymap
= _rl_keymap
;
589 rl_bind_keyseq_if_unbound (_rl_term_ku
, rl_get_previous_history
);
590 rl_bind_keyseq_if_unbound (_rl_term_kd
, rl_get_next_history
);
591 rl_bind_keyseq_if_unbound (_rl_term_kr
, rl_forward_char
);
592 rl_bind_keyseq_if_unbound (_rl_term_kl
, rl_backward_char
);
594 rl_bind_keyseq_if_unbound (_rl_term_kh
, rl_beg_of_line
); /* Home */
595 rl_bind_keyseq_if_unbound (_rl_term_at7
, rl_end_of_line
); /* End */
597 rl_bind_keyseq_if_unbound (_rl_term_kD
, rl_delete
);
598 rl_bind_keyseq_if_unbound (_rl_term_kI
, rl_overwrite_mode
); /* Insert */
600 _rl_keymap
= xkeymap
;
604 rl_get_termcap (const char *cap
)
608 if (tcap_initialized
== 0)
609 return ((char *)NULL
);
610 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
612 if (tc_strings
[i
].tc_var
[0] == cap
[0] && strcmp (tc_strings
[i
].tc_var
, cap
) == 0)
613 return *(tc_strings
[i
].tc_value
);
615 return ((char *)NULL
);
618 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
621 rl_reset_terminal (const char *terminal_name
)
623 _rl_screenwidth
= _rl_screenheight
= 0;
624 _rl_init_terminal_io (terminal_name
);
628 /* A function for the use of tputs () */
631 _rl_output_character_function (int c
)
633 putc (c
, _rl_out_stream
);
637 _rl_output_character_function (int c
)
639 return putc (c
, _rl_out_stream
);
643 /* Write COUNT characters from STRING to the output stream. */
645 _rl_output_some_chars (const char *string
, int count
)
647 fwrite (string
, 1, count
, _rl_out_stream
);
650 /* Move the cursor back. */
652 _rl_backspace (int count
)
657 if (_rl_term_backspace
)
658 for (i
= 0; i
< count
; i
++)
659 tputs (_rl_term_backspace
, 1, _rl_output_character_function
);
662 for (i
= 0; i
< count
; i
++)
663 putc ('\b', _rl_out_stream
);
667 /* Move to the start of the next line. */
671 #if defined (NEW_TTY_DRIVER) || defined (__MINT__)
673 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
674 #endif /* NEW_TTY_DRIVER || __MINT__ */
675 putc ('\n', _rl_out_stream
);
679 /* Ring the terminal bell. */
685 switch (_rl_bell_preference
)
691 if (_rl_visible_bell
)
696 tputs (_rl_visible_bell
, 1, _rl_output_character_function
);
702 fprintf (stderr
, "\007");
711 /* **************************************************************** */
713 /* Controlling the Meta Key and Keypad */
715 /* **************************************************************** */
717 static int enabled_meta
= 0; /* flag indicating we enabled meta mode */
720 _rl_enable_meta_key (void)
722 #if !defined (__DJGPP__)
723 if (term_has_meta
&& _rl_term_mm
)
725 tputs (_rl_term_mm
, 1, _rl_output_character_function
);
732 _rl_disable_meta_key (void)
734 #if !defined (__DJGPP__)
735 if (term_has_meta
&& _rl_term_mo
&& enabled_meta
)
737 tputs (_rl_term_mo
, 1, _rl_output_character_function
);
744 _rl_control_keypad (int on
)
746 #if !defined (__DJGPP__)
747 if (on
&& _rl_term_ks
)
748 tputs (_rl_term_ks
, 1, _rl_output_character_function
);
749 else if (!on
&& _rl_term_ke
)
750 tputs (_rl_term_ke
, 1, _rl_output_character_function
);
754 /* **************************************************************** */
756 /* Controlling the Cursor */
758 /* **************************************************************** */
760 /* Set the cursor appropriately depending on IM, which is one of the
761 insert modes (insert or overwrite). Insert mode gets the normal
762 cursor. Overwrite mode gets a very visible cursor. Only does
763 anything if we have both capabilities. */
765 _rl_set_cursor (int im
, int force
)
768 if (_rl_term_ve
&& _rl_term_vs
)
770 if (force
|| im
!= rl_insert_mode
)
772 if (im
== RL_IM_OVERWRITE
)
773 tputs (_rl_term_vs
, 1, _rl_output_character_function
);
775 tputs (_rl_term_ve
, 1, _rl_output_character_function
);