1 /* terminal.c -- controlling the terminal with termcap. */
3 /* Copyright (C) 1996-2009 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. */
54 #if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ)
55 # include <sys/ioctl.h>
56 #endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */
65 /* Some standard library routines. */
69 #include "rlprivate.h"
73 #if defined (__MINGW32__)
77 static void _win_get_screensize PARAMS((int *, int *));
81 static void _emx_get_screensize PARAMS((int *, int *));
84 /* If the calling application sets this to a non-zero value, readline will
85 use the $LINES and $COLUMNS environment variables to set its idea of the
86 window size before interrogating the kernel. */
87 int rl_prefer_env_winsize = 0;
89 /* If this is non-zero, readline will set LINES and COLUMNS in the
90 environment when it handles SIGWINCH. */
91 int rl_change_environment = 1;
93 /* **************************************************************** */
95 /* Terminal and Termcap */
97 /* **************************************************************** */
100 static char *term_buffer = (char *)NULL;
101 static char *term_string_buffer = (char *)NULL;
104 static int tcap_initialized;
106 #if !defined (__linux__) && !defined (NCURSES_VERSION)
107 # if defined (__EMX__) || defined (NEED_EXTERN_PC)
109 # endif /* __EMX__ || NEED_EXTERN_PC */
111 #endif /* !__linux__ && !NCURSES_VERSION */
113 /* Some strings to control terminal actions. These are output by tputs (). */
114 char *_rl_term_clreol;
115 char *_rl_term_clrpag;
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 char *_rl_term_forward_char;
137 /* How to go up a line. */
140 /* A visible bell; char if the terminal can be made to flash the screen. */
141 static char *_rl_visible_bell;
143 /* Non-zero means the terminal can auto-wrap lines. */
144 int _rl_term_autowrap = -1;
146 /* Non-zero means that this terminal has a meta key. */
147 static int term_has_meta;
149 /* The sequences to write to turn on and off the meta key, if this
151 static char *_rl_term_mm;
152 static char *_rl_term_mo;
154 /* The key sequences output by the arrow keys, if this terminal has any. */
155 static char *_rl_term_ku;
156 static char *_rl_term_kd;
157 static char *_rl_term_kr;
158 static char *_rl_term_kl;
160 /* How to initialize and reset the arrow keys, if this terminal has any. */
161 static char *_rl_term_ks;
162 static char *_rl_term_ke;
164 /* The key sequences sent by the Home and End keys, if any. */
165 static char *_rl_term_kh;
166 static char *_rl_term_kH;
167 static char *_rl_term_at7; /* @7 */
170 static char *_rl_term_kD;
173 static char *_rl_term_kI;
176 static char *_rl_term_vs; /* very visible */
177 static char *_rl_term_ve; /* normal */
179 static void bind_termcap_arrow_keys PARAMS((Keymap));
181 /* Variables that hold the screen dimensions, used by the display code. */
182 int _rl_screenwidth, _rl_screenheight, _rl_screenchars;
184 /* Non-zero means the user wants to enable the keypad. */
185 int _rl_enable_keypad;
187 /* Non-zero means the user wants to enable a meta key. */
188 int _rl_enable_meta = 1;
190 #if defined (__EMX__)
192 _emx_get_screensize (swp, shp)
206 #if defined (__MINGW32__)
208 _win_get_screensize (swp, shp)
212 CONSOLE_SCREEN_BUFFER_INFO scr;
214 hConOut = GetStdHandle (STD_OUTPUT_HANDLE);
215 if (hConOut != INVALID_HANDLE_VALUE)
217 if (GetConsoleScreenBufferInfo (hConOut, &scr))
220 *shp = scr.srWindow.Bottom - scr.srWindow.Top + 1;
226 /* Get readline's idea of the screen size. TTY is a file descriptor open
227 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
228 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
229 non-null serve to check whether or not we have initialized termcap. */
231 _rl_get_screen_size (tty, ignore_env)
235 #if defined (TIOCGWINSZ)
236 struct winsize window_size;
237 #endif /* TIOCGWINSZ */
241 #if defined (TIOCGWINSZ)
242 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
244 wc = (int) window_size.ws_col;
245 wr = (int) window_size.ws_row;
247 #endif /* TIOCGWINSZ */
249 #if defined (__EMX__)
250 _emx_get_screensize (&wc, &wr);
251 #elif defined (__MINGW32__)
252 _win_get_screensize (&wc, &wr);
255 if (ignore_env || rl_prefer_env_winsize == 0)
257 _rl_screenwidth = wc;
258 _rl_screenheight = wr;
261 _rl_screenwidth = _rl_screenheight = -1;
263 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
264 is unset. If we prefer the environment, check it first before
265 assigning the value returned by the kernel. */
266 if (_rl_screenwidth <= 0)
268 if (ignore_env == 0 && (ss = sh_get_env_value ("COLUMNS")))
269 _rl_screenwidth = atoi (ss);
271 if (_rl_screenwidth <= 0)
272 _rl_screenwidth = wc;
274 #if defined (__DJGPP__)
275 if (_rl_screenwidth <= 0)
276 _rl_screenwidth = ScreenCols ();
278 if (_rl_screenwidth <= 0 && term_string_buffer)
279 _rl_screenwidth = tgetnum ("co");
283 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
285 if (_rl_screenheight <= 0)
287 if (ignore_env == 0 && (ss = sh_get_env_value ("LINES")))
288 _rl_screenheight = atoi (ss);
290 if (_rl_screenheight <= 0)
291 _rl_screenheight = wr;
293 #if defined (__DJGPP__)
294 if (_rl_screenheight <= 0)
295 _rl_screenheight = ScreenRows ();
297 if (_rl_screenheight <= 0 && term_string_buffer)
298 _rl_screenheight = tgetnum ("li");
302 /* If all else fails, default to 80x24 terminal. */
303 if (_rl_screenwidth <= 1)
304 _rl_screenwidth = 80;
306 if (_rl_screenheight <= 0)
307 _rl_screenheight = 24;
309 /* If we're being compiled as part of bash, set the environment
310 variables $LINES and $COLUMNS to new values. Otherwise, just
311 do a pair of putenv () or setenv () calls. */
312 if (rl_change_environment)
313 sh_set_lines_and_columns (_rl_screenheight, _rl_screenwidth);
315 if (_rl_term_autowrap == 0)
318 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
322 _rl_set_screen_size (rows, cols)
325 if (_rl_term_autowrap == -1)
326 _rl_init_terminal_io (rl_terminal_name);
329 _rl_screenheight = rows;
332 _rl_screenwidth = cols;
333 if (_rl_term_autowrap == 0)
337 if (rows > 0 || cols > 0)
338 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
342 rl_set_screen_size (rows, cols)
345 _rl_set_screen_size (rows, cols);
349 rl_get_screen_size (rows, cols)
353 *rows = _rl_screenheight;
355 *cols = _rl_screenwidth;
359 rl_reset_screen_size ()
361 _rl_get_screen_size (fileno (rl_instream), 0);
365 _rl_sigwinch_resize_terminal ()
367 _rl_get_screen_size (fileno (rl_instream), 1);
371 rl_resize_terminal ()
373 _rl_get_screen_size (fileno (rl_instream), 1);
376 if (CUSTOM_REDISPLAY_FUNC ())
377 rl_forced_update_display ();
378 else if (RL_ISSTATE(RL_STATE_REDISPLAYING) == 0)
379 _rl_redisplay_after_sigwinch ();
384 const char * const tc_var;
388 /* This should be kept sorted, just in case we decide to change the
389 search algorithm to something smarter. */
390 static const struct _tc_string tc_strings[] =
392 { "@7", &_rl_term_at7 },
393 { "DC", &_rl_term_DC },
394 { "IC", &_rl_term_IC },
395 { "ce", &_rl_term_clreol },
396 { "cl", &_rl_term_clrpag },
397 { "cr", &_rl_term_cr },
398 { "dc", &_rl_term_dc },
399 { "ei", &_rl_term_ei },
400 { "ic", &_rl_term_ic },
401 { "im", &_rl_term_im },
402 { "kD", &_rl_term_kD }, /* delete */
403 { "kH", &_rl_term_kH }, /* home down ?? */
404 { "kI", &_rl_term_kI }, /* insert */
405 { "kd", &_rl_term_kd },
406 { "ke", &_rl_term_ke }, /* end keypad mode */
407 { "kh", &_rl_term_kh }, /* home */
408 { "kl", &_rl_term_kl },
409 { "kr", &_rl_term_kr },
410 { "ks", &_rl_term_ks }, /* start keypad mode */
411 { "ku", &_rl_term_ku },
412 { "le", &_rl_term_backspace },
413 { "mm", &_rl_term_mm },
414 { "mo", &_rl_term_mo },
415 { "nd", &_rl_term_forward_char },
416 { "pc", &_rl_term_pc },
417 { "up", &_rl_term_up },
418 { "vb", &_rl_visible_bell },
419 { "vs", &_rl_term_vs },
420 { "ve", &_rl_term_ve },
423 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
425 /* Read the desired terminal capability strings into BP. The capabilities
426 are described in the TC_STRINGS table. */
428 get_term_capabilities (bp)
431 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
434 for (i = 0; i < NUM_TC_STRINGS; i++)
435 *(tc_strings[i].tc_value) = tgetstr ((char *)tc_strings[i].tc_var, bp);
437 tcap_initialized = 1;
441 _rl_init_terminal_io (terminal_name)
442 const char *terminal_name;
446 int tty, tgetent_ret;
448 term = terminal_name ? terminal_name : sh_get_env_value ("TERM");
449 _rl_term_clrpag = _rl_term_cr = _rl_term_clreol = (char *)NULL;
450 tty = rl_instream ? fileno (rl_instream) : 0;
456 _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
457 _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
458 _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
459 _rl_term_mm = _rl_term_mo = (char *)NULL;
460 _rl_terminal_can_insert = term_has_meta = _rl_term_autowrap = 0;
462 _rl_term_clreol = _rl_term_clrpag = _rl_term_backspace = (char *)NULL;
463 _rl_term_goto = _rl_term_pc = _rl_term_ip = (char *)NULL;
464 _rl_term_ks = _rl_term_ke =_rl_term_vs = _rl_term_ve = (char *)NULL;
465 _rl_term_kh = _rl_term_kH = _rl_term_at7 = _rl_term_kI = (char *)NULL;
466 #if defined(HACK_TERMCAP_MOTION)
467 _rl_term_forward_char = (char *)NULL;
470 _rl_get_screen_size (tty, 0);
471 #else /* !__MSDOS__ */
472 /* I've separated this out for later work on not calling tgetent at all
473 if the calling application has supplied a custom redisplay function,
474 (and possibly if the application has supplied a custom input function). */
475 if (CUSTOM_REDISPLAY_FUNC())
481 if (term_string_buffer == 0)
482 term_string_buffer = (char *)xmalloc(2032);
484 if (term_buffer == 0)
485 term_buffer = (char *)xmalloc(4080);
487 buffer = term_string_buffer;
489 tgetent_ret = tgetent (term_buffer, term);
492 if (tgetent_ret <= 0)
494 FREE (term_string_buffer);
496 buffer = term_buffer = term_string_buffer = (char *)NULL;
498 _rl_term_autowrap = 0; /* used by _rl_get_screen_size */
500 /* Allow calling application to set default height and width, using
501 rl_set_screen_size */
502 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
504 #if defined (__EMX__)
505 _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
508 _rl_get_screen_size (tty, 0);
509 #endif /* !__EMX__ */
513 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
515 _rl_screenwidth = 79;
516 _rl_screenheight = 24;
519 /* Everything below here is used by the redisplay code (tputs). */
520 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
522 _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
523 _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
524 _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
525 _rl_term_kh = _rl_term_kH = _rl_term_kI = _rl_term_kD = (char *)NULL;
526 _rl_term_ks = _rl_term_ke = _rl_term_at7 = (char *)NULL;
527 _rl_term_mm = _rl_term_mo = (char *)NULL;
528 _rl_term_ve = _rl_term_vs = (char *)NULL;
529 _rl_term_forward_char = (char *)NULL;
530 _rl_terminal_can_insert = term_has_meta = 0;
532 /* Reasonable defaults for tgoto(). Readline currently only uses
533 tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
534 change that later... */
536 BC = _rl_term_backspace = "\b";
542 get_term_capabilities (&buffer);
544 /* Set up the variables that the termcap library expects the application
546 PC = _rl_term_pc ? *_rl_term_pc : 0;
547 BC = _rl_term_backspace;
553 _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
555 /* Allow calling application to set default height and width, using
556 rl_set_screen_size */
557 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
558 _rl_get_screen_size (tty, 0);
560 /* "An application program can assume that the terminal can do
561 character insertion if *any one of* the capabilities `IC',
562 `im', `ic' or `ip' is provided." But we can't do anything if
563 only `ip' is provided, so... */
564 _rl_terminal_can_insert = (_rl_term_IC || _rl_term_im || _rl_term_ic);
566 /* Check to see if this terminal has a meta key and clear the capability
567 variables if there is none. */
568 term_has_meta = tgetflag ("km") != 0;
569 if (term_has_meta == 0)
570 _rl_term_mm = _rl_term_mo = (char *)NULL;
571 #endif /* !__MSDOS__ */
573 /* Attempt to find and bind the arrow keys. Do not override already
574 bound keys in an overzealous attempt, however. */
576 bind_termcap_arrow_keys (emacs_standard_keymap);
578 #if defined (VI_MODE)
579 bind_termcap_arrow_keys (vi_movement_keymap);
580 bind_termcap_arrow_keys (vi_insertion_keymap);
586 /* Bind the arrow key sequences from the termcap description in MAP. */
588 bind_termcap_arrow_keys (map)
593 xkeymap = _rl_keymap;
596 rl_bind_keyseq_if_unbound (_rl_term_ku, rl_get_previous_history);
597 rl_bind_keyseq_if_unbound (_rl_term_kd, rl_get_next_history);
598 rl_bind_keyseq_if_unbound (_rl_term_kr, rl_forward_char);
599 rl_bind_keyseq_if_unbound (_rl_term_kl, rl_backward_char);
601 rl_bind_keyseq_if_unbound (_rl_term_kh, rl_beg_of_line); /* Home */
602 rl_bind_keyseq_if_unbound (_rl_term_at7, rl_end_of_line); /* End */
604 rl_bind_keyseq_if_unbound (_rl_term_kD, rl_delete);
606 _rl_keymap = xkeymap;
615 if (tcap_initialized == 0)
616 return ((char *)NULL);
617 for (i = 0; i < NUM_TC_STRINGS; i++)
619 if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
620 return *(tc_strings[i].tc_value);
622 return ((char *)NULL);
625 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
628 rl_reset_terminal (terminal_name)
629 const char *terminal_name;
631 _rl_screenwidth = _rl_screenheight = 0;
632 _rl_init_terminal_io (terminal_name);
636 /* A function for the use of tputs () */
639 _rl_output_character_function (c)
642 putc (c, _rl_out_stream);
646 _rl_output_character_function (c)
649 return putc (c, _rl_out_stream);
653 /* Write COUNT characters from STRING to the output stream. */
655 _rl_output_some_chars (string, count)
659 fwrite (string, 1, count, _rl_out_stream);
662 /* Move the cursor back. */
664 _rl_backspace (count)
670 if (_rl_term_backspace)
671 for (i = 0; i < count; i++)
672 tputs (_rl_term_backspace, 1, _rl_output_character_function);
675 for (i = 0; i < count; i++)
676 putc ('\b', _rl_out_stream);
680 /* Move to the start of the next line. */
684 #if defined (NEW_TTY_DRIVER) || defined (__MINT__)
686 tputs (_rl_term_cr, 1, _rl_output_character_function);
687 #endif /* NEW_TTY_DRIVER || __MINT__ */
688 putc ('\n', _rl_out_stream);
692 /* Ring the terminal bell. */
698 switch (_rl_bell_preference)
704 if (_rl_visible_bell)
709 tputs (_rl_visible_bell, 1, _rl_output_character_function);
715 fprintf (stderr, "\007");
724 /* **************************************************************** */
726 /* Controlling the Meta Key and Keypad */
728 /* **************************************************************** */
730 static int enabled_meta = 0; /* flag indicating we enabled meta mode */
733 _rl_enable_meta_key ()
735 #if !defined (__DJGPP__)
736 if (term_has_meta && _rl_term_mm)
738 tputs (_rl_term_mm, 1, _rl_output_character_function);
745 _rl_disable_meta_key ()
747 #if !defined (__DJGPP__)
748 if (term_has_meta && _rl_term_mo && enabled_meta)
750 tputs (_rl_term_mo, 1, _rl_output_character_function);
757 _rl_control_keypad (on)
760 #if !defined (__DJGPP__)
761 if (on && _rl_term_ks)
762 tputs (_rl_term_ks, 1, _rl_output_character_function);
763 else if (!on && _rl_term_ke)
764 tputs (_rl_term_ke, 1, _rl_output_character_function);
768 /* **************************************************************** */
770 /* Controlling the Cursor */
772 /* **************************************************************** */
774 /* Set the cursor appropriately depending on IM, which is one of the
775 insert modes (insert or overwrite). Insert mode gets the normal
776 cursor. Overwrite mode gets a very visible cursor. Only does
777 anything if we have both capabilities. */
779 _rl_set_cursor (im, force)
783 if (_rl_term_ve && _rl_term_vs)
785 if (force || im != rl_insert_mode)
787 if (im == RL_IM_OVERWRITE)
788 tputs (_rl_term_vs, 1, _rl_output_character_function);
790 tputs (_rl_term_ve, 1, _rl_output_character_function);