2 /* terminal.c -- controlling the terminal with termcap. */
4 /* Copyright (C) 1996-2006 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
30 #include "posixstat.h"
32 #if defined (HAVE_SYS_FILE_H)
33 # include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
36 #if defined (HAVE_UNISTD_H)
38 #endif /* HAVE_UNISTD_H */
40 #if defined (HAVE_STDLIB_H)
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
46 #if defined (HAVE_LOCALE_H)
52 /* System-specific feature definitions and include files. */
55 #if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ)
56 # include <sys/ioctl.h>
57 #endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */
62 /* Some standard library routines. */
66 #include "rlprivate.h"
70 #if defined (__MINGW32__)
74 static void _win_get_screensize PARAMS((int *, int *));
78 static void _emx_get_screensize PARAMS((int *, int *));
81 #define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
82 #define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
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 /* **************************************************************** */
91 /* Terminal and Termcap */
93 /* **************************************************************** */
95 static char *term_buffer = (char *)NULL;
96 static char *term_string_buffer = (char *)NULL;
98 static int tcap_initialized;
100 #if !defined (__linux__)
101 # if defined (__EMX__) || defined (NEED_EXTERN_PC)
103 # endif /* __EMX__ || NEED_EXTERN_PC */
105 #endif /* __linux__ */
107 /* Some strings to control terminal actions. These are output by tputs (). */
108 char *_rl_term_clreol;
109 char *_rl_term_clrpag;
111 char *_rl_term_backspace;
115 /* Non-zero if we determine that the terminal can do character insertion. */
116 int _rl_terminal_can_insert = 0;
118 /* How to insert characters. */
125 /* How to delete characters. */
129 char *_rl_term_forward_char;
131 /* How to go up a line. */
134 /* A visible bell; char if the terminal can be made to flash the screen. */
135 static char *_rl_visible_bell;
137 /* Non-zero means the terminal can auto-wrap lines. */
138 int _rl_term_autowrap = -1;
140 /* Non-zero means that this terminal has a meta key. */
141 static int term_has_meta;
143 /* The sequences to write to turn on and off the meta key, if this
145 static char *_rl_term_mm;
146 static char *_rl_term_mo;
148 /* The key sequences output by the arrow keys, if this terminal has any. */
149 static char *_rl_term_ku;
150 static char *_rl_term_kd;
151 static char *_rl_term_kr;
152 static char *_rl_term_kl;
154 /* How to initialize and reset the arrow keys, if this terminal has any. */
155 static char *_rl_term_ks;
156 static char *_rl_term_ke;
158 /* The key sequences sent by the Home and End keys, if any. */
159 static char *_rl_term_kh;
160 static char *_rl_term_kH;
161 static char *_rl_term_at7; /* @7 */
164 static char *_rl_term_kD;
167 static char *_rl_term_kI;
170 static char *_rl_term_vs; /* very visible */
171 static char *_rl_term_ve; /* normal */
173 static void bind_termcap_arrow_keys PARAMS((Keymap));
175 /* Variables that hold the screen dimensions, used by the display code. */
176 int _rl_screenwidth, _rl_screenheight, _rl_screenchars;
178 /* Non-zero means the user wants to enable the keypad. */
179 int _rl_enable_keypad;
181 /* Non-zero means the user wants to enable a meta key. */
182 int _rl_enable_meta = 1;
184 #if defined (__EMX__)
186 _emx_get_screensize (swp, shp)
200 #if defined (__MINGW32__)
202 _win_get_screensize (swp, shp)
206 CONSOLE_SCREEN_BUFFER_INFO scr;
208 hConOut = GetStdHandle (STD_OUTPUT_HANDLE);
209 if (hConOut != INVALID_HANDLE_VALUE)
211 if (GetConsoleScreenBufferInfo (hConOut, &scr))
214 *shp = scr.srWindow.Bottom - scr.srWindow.Top + 1;
220 /* Get readline's idea of the screen size. TTY is a file descriptor open
221 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
222 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
223 non-null serve to check whether or not we have initialized termcap. */
225 _rl_get_screen_size (tty, ignore_env)
229 #if defined (TIOCGWINSZ)
230 struct winsize window_size;
231 #endif /* TIOCGWINSZ */
235 #if defined (TIOCGWINSZ)
236 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
238 wc = (int) window_size.ws_col;
239 wr = (int) window_size.ws_row;
241 #endif /* TIOCGWINSZ */
243 #if defined (__EMX__)
244 _emx_get_screensize (&wc, &wr);
245 #elif defined (__MINGW32__)
246 _win_get_screensize (&wc, &wr);
249 if (ignore_env || rl_prefer_env_winsize == 0)
251 _rl_screenwidth = wc;
252 _rl_screenheight = wr;
255 _rl_screenwidth = _rl_screenheight = -1;
257 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
258 is unset. If we prefer the environment, check it first before
259 assigning the value returned by the kernel. */
260 if (_rl_screenwidth <= 0)
262 if (ignore_env == 0 && (ss = sh_get_env_value ("COLUMNS")))
263 _rl_screenwidth = atoi (ss);
265 if (_rl_screenwidth <= 0)
266 _rl_screenwidth = wc;
268 #if !defined (__DJGPP__)
269 if (_rl_screenwidth <= 0 && term_string_buffer)
270 _rl_screenwidth = tgetnum ("co");
274 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
276 if (_rl_screenheight <= 0)
278 if (ignore_env == 0 && (ss = sh_get_env_value ("LINES")))
279 _rl_screenheight = atoi (ss);
281 if (_rl_screenheight <= 0)
282 _rl_screenheight = wr;
284 #if !defined (__DJGPP__)
285 if (_rl_screenheight <= 0 && term_string_buffer)
286 _rl_screenheight = tgetnum ("li");
290 /* If all else fails, default to 80x24 terminal. */
291 if (_rl_screenwidth <= 1)
292 _rl_screenwidth = 80;
294 if (_rl_screenheight <= 0)
295 _rl_screenheight = 24;
297 /* If we're being compiled as part of bash, set the environment
298 variables $LINES and $COLUMNS to new values. Otherwise, just
299 do a pair of putenv () or setenv () calls. */
300 sh_set_lines_and_columns (_rl_screenheight, _rl_screenwidth);
302 if (_rl_term_autowrap == 0)
305 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
309 _rl_set_screen_size (rows, cols)
312 if (_rl_term_autowrap == -1)
313 _rl_init_terminal_io (rl_terminal_name);
316 _rl_screenheight = rows;
319 _rl_screenwidth = cols;
320 if (_rl_term_autowrap == 0)
324 if (rows > 0 || cols > 0)
325 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
329 rl_set_screen_size (rows, cols)
332 _rl_set_screen_size (rows, cols);
336 rl_get_screen_size (rows, cols)
340 *rows = _rl_screenheight;
342 *cols = _rl_screenwidth;
346 rl_reset_screen_size ()
348 _rl_get_screen_size (fileno (rl_instream), 0);
352 rl_resize_terminal ()
354 if (readline_echoing_p)
356 _rl_get_screen_size (fileno (rl_instream), 1);
357 if (CUSTOM_REDISPLAY_FUNC ())
358 rl_forced_update_display ();
360 _rl_redisplay_after_sigwinch ();
369 /* This should be kept sorted, just in case we decide to change the
370 search algorithm to something smarter. */
371 static struct _tc_string tc_strings[] =
373 { "@7", &_rl_term_at7 },
374 { "DC", &_rl_term_DC },
375 { "IC", &_rl_term_IC },
376 { "ce", &_rl_term_clreol },
377 { "cl", &_rl_term_clrpag },
378 { "cr", &_rl_term_cr },
379 { "dc", &_rl_term_dc },
380 { "ei", &_rl_term_ei },
381 { "ic", &_rl_term_ic },
382 { "im", &_rl_term_im },
383 { "kD", &_rl_term_kD }, /* delete */
384 { "kH", &_rl_term_kH }, /* home down ?? */
385 { "kI", &_rl_term_kI }, /* insert */
386 { "kd", &_rl_term_kd },
387 { "ke", &_rl_term_ke }, /* end keypad mode */
388 { "kh", &_rl_term_kh }, /* home */
389 { "kl", &_rl_term_kl },
390 { "kr", &_rl_term_kr },
391 { "ks", &_rl_term_ks }, /* start keypad mode */
392 { "ku", &_rl_term_ku },
393 { "le", &_rl_term_backspace },
394 { "mm", &_rl_term_mm },
395 { "mo", &_rl_term_mo },
396 { "nd", &_rl_term_forward_char },
397 { "pc", &_rl_term_pc },
398 { "up", &_rl_term_up },
399 { "vb", &_rl_visible_bell },
400 { "vs", &_rl_term_vs },
401 { "ve", &_rl_term_ve },
404 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
406 /* Read the desired terminal capability strings into BP. The capabilities
407 are described in the TC_STRINGS table. */
409 get_term_capabilities (bp)
412 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
415 for (i = 0; i < NUM_TC_STRINGS; i++)
416 *(tc_strings[i].tc_value) = tgetstr ((char *)tc_strings[i].tc_var, bp);
418 tcap_initialized = 1;
422 _rl_init_terminal_io (terminal_name)
423 const char *terminal_name;
427 int tty, tgetent_ret;
429 term = terminal_name ? terminal_name : sh_get_env_value ("TERM");
430 _rl_term_clrpag = _rl_term_cr = _rl_term_clreol = (char *)NULL;
431 tty = rl_instream ? fileno (rl_instream) : 0;
436 /* I've separated this out for later work on not calling tgetent at all
437 if the calling application has supplied a custom redisplay function,
438 (and possibly if the application has supplied a custom input function). */
439 if (CUSTOM_REDISPLAY_FUNC())
445 if (term_string_buffer == 0)
446 term_string_buffer = (char *)xmalloc(2032);
448 if (term_buffer == 0)
449 term_buffer = (char *)xmalloc(4080);
451 buffer = term_string_buffer;
453 tgetent_ret = tgetent (term_buffer, term);
456 if (tgetent_ret <= 0)
458 FREE (term_string_buffer);
460 buffer = term_buffer = term_string_buffer = (char *)NULL;
462 _rl_term_autowrap = 0; /* used by _rl_get_screen_size */
464 /* Allow calling application to set default height and width, using
465 rl_set_screen_size */
466 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
468 #if defined (__EMX__)
469 _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
472 _rl_get_screen_size (tty, 0);
473 #endif /* !__EMX__ */
477 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
479 _rl_screenwidth = 79;
480 _rl_screenheight = 24;
483 /* Everything below here is used by the redisplay code (tputs). */
484 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
486 _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
487 _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
488 _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
489 _rl_term_kh = _rl_term_kH = _rl_term_kI = _rl_term_kD = (char *)NULL;
490 _rl_term_ks = _rl_term_ke = _rl_term_at7 = (char *)NULL;
491 _rl_term_mm = _rl_term_mo = (char *)NULL;
492 _rl_term_ve = _rl_term_vs = (char *)NULL;
493 _rl_term_forward_char = (char *)NULL;
494 _rl_terminal_can_insert = term_has_meta = 0;
496 /* Reasonable defaults for tgoto(). Readline currently only uses
497 tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
498 change that later... */
500 BC = _rl_term_backspace = "\b";
506 get_term_capabilities (&buffer);
508 /* Set up the variables that the termcap library expects the application
510 PC = _rl_term_pc ? *_rl_term_pc : 0;
511 BC = _rl_term_backspace;
517 _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
519 /* Allow calling application to set default height and width, using
520 rl_set_screen_size */
521 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
522 _rl_get_screen_size (tty, 0);
524 /* "An application program can assume that the terminal can do
525 character insertion if *any one of* the capabilities `IC',
526 `im', `ic' or `ip' is provided." But we can't do anything if
527 only `ip' is provided, so... */
528 _rl_terminal_can_insert = (_rl_term_IC || _rl_term_im || _rl_term_ic);
530 /* Check to see if this terminal has a meta key and clear the capability
531 variables if there is none. */
532 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
534 _rl_term_mm = _rl_term_mo = (char *)NULL;
536 /* Attempt to find and bind the arrow keys. Do not override already
537 bound keys in an overzealous attempt, however. */
539 bind_termcap_arrow_keys (emacs_standard_keymap);
541 #if defined (VI_MODE)
542 bind_termcap_arrow_keys (vi_movement_keymap);
543 bind_termcap_arrow_keys (vi_insertion_keymap);
549 /* Bind the arrow key sequences from the termcap description in MAP. */
551 bind_termcap_arrow_keys (map)
556 xkeymap = _rl_keymap;
559 rl_bind_keyseq_if_unbound (_rl_term_ku, rl_get_previous_history);
560 rl_bind_keyseq_if_unbound (_rl_term_kd, rl_get_next_history);
561 rl_bind_keyseq_if_unbound (_rl_term_kr, rl_forward_char);
562 rl_bind_keyseq_if_unbound (_rl_term_kl, rl_backward_char);
564 rl_bind_keyseq_if_unbound (_rl_term_kh, rl_beg_of_line); /* Home */
565 rl_bind_keyseq_if_unbound (_rl_term_at7, rl_end_of_line); /* End */
567 rl_bind_keyseq_if_unbound (_rl_term_kD, rl_delete);
569 _rl_keymap = xkeymap;
578 if (tcap_initialized == 0)
579 return ((char *)NULL);
580 for (i = 0; i < NUM_TC_STRINGS; i++)
582 if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
583 return *(tc_strings[i].tc_value);
585 return ((char *)NULL);
588 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
591 rl_reset_terminal (terminal_name)
592 const char *terminal_name;
594 _rl_screenwidth = _rl_screenheight = 0;
595 _rl_init_terminal_io (terminal_name);
599 /* A function for the use of tputs () */
602 _rl_output_character_function (c)
605 putc (c, _rl_out_stream);
609 _rl_output_character_function (c)
612 return putc (c, _rl_out_stream);
616 /* Write COUNT characters from STRING to the output stream. */
618 _rl_output_some_chars (string, count)
622 fwrite (string, 1, count, _rl_out_stream);
625 /* Move the cursor back. */
627 _rl_backspace (count)
632 if (_rl_term_backspace)
633 for (i = 0; i < count; i++)
634 tputs (_rl_term_backspace, 1, _rl_output_character_function);
636 for (i = 0; i < count; i++)
637 putc ('\b', _rl_out_stream);
641 /* Move to the start of the next line. */
645 #if defined (NEW_TTY_DRIVER)
647 tputs (_rl_term_cr, 1, _rl_output_character_function);
648 #endif /* NEW_TTY_DRIVER */
649 putc ('\n', _rl_out_stream);
653 /* Ring the terminal bell. */
657 if (readline_echoing_p)
659 switch (_rl_bell_preference)
665 if (_rl_visible_bell)
667 tputs (_rl_visible_bell, 1, _rl_output_character_function);
672 fprintf (stderr, "\007");
681 /* **************************************************************** */
683 /* Controlling the Meta Key and Keypad */
685 /* **************************************************************** */
688 _rl_enable_meta_key ()
690 #if !defined (__DJGPP__)
691 if (term_has_meta && _rl_term_mm)
692 tputs (_rl_term_mm, 1, _rl_output_character_function);
697 _rl_control_keypad (on)
700 #if !defined (__DJGPP__)
701 if (on && _rl_term_ks)
702 tputs (_rl_term_ks, 1, _rl_output_character_function);
703 else if (!on && _rl_term_ke)
704 tputs (_rl_term_ke, 1, _rl_output_character_function);
708 /* **************************************************************** */
710 /* Controlling the Cursor */
712 /* **************************************************************** */
714 /* Set the cursor appropriately depending on IM, which is one of the
715 insert modes (insert or overwrite). Insert mode gets the normal
716 cursor. Overwrite mode gets a very visible cursor. Only does
717 anything if we have both capabilities. */
719 _rl_set_cursor (im, force)
722 if (_rl_term_ve && _rl_term_vs)
724 if (force || im != rl_insert_mode)
726 if (im == RL_IM_OVERWRITE)
727 tputs (_rl_term_vs, 1, _rl_output_character_function);
729 tputs (_rl_term_ve, 1, _rl_output_character_function);