1 /* terminal.c -- controlling the terminal with termcap. */
3 /* Copyright (C) 1996-2005 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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__)
78 #define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
79 #define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
81 int rl_prefer_env_winsize
;
83 /* **************************************************************** */
85 /* Terminal and Termcap */
87 /* **************************************************************** */
90 static char *term_buffer
= (char *)NULL
;
91 static char *term_string_buffer
= (char *)NULL
;
92 #endif /* !__MSDOS__ */
94 static int tcap_initialized
;
96 #if !defined (__linux__)
97 # if defined (__EMX__) || defined (NEED_EXTERN_PC)
99 # endif /* __EMX__ || NEED_EXTERN_PC */
101 #endif /* __linux__ */
103 /* Some strings to control terminal actions. These are output by tputs (). */
104 char *_rl_term_clreol
;
105 char *_rl_term_clrpag
;
107 char *_rl_term_backspace
;
111 /* Non-zero if we determine that the terminal can do character insertion. */
112 int _rl_terminal_can_insert
= 0;
114 /* How to insert characters. */
121 /* How to delete characters. */
125 #if defined (HACK_TERMCAP_MOTION)
126 char *_rl_term_forward_char
;
127 #endif /* HACK_TERMCAP_MOTION */
129 /* How to go up a line. */
132 /* A visible bell; char if the terminal can be made to flash the screen. */
133 static char *_rl_visible_bell
;
135 /* Non-zero means the terminal can auto-wrap lines. */
136 int _rl_term_autowrap
= -1;
138 /* Non-zero means that this terminal has a meta key. */
139 static int term_has_meta
;
141 /* The sequences to write to turn on and off the meta key, if this
143 static char *_rl_term_mm
;
144 static char *_rl_term_mo
;
146 /* The key sequences output by the arrow keys, if this terminal has any. */
147 static char *_rl_term_ku
;
148 static char *_rl_term_kd
;
149 static char *_rl_term_kr
;
150 static char *_rl_term_kl
;
152 /* How to initialize and reset the arrow keys, if this terminal has any. */
153 static char *_rl_term_ks
;
154 static char *_rl_term_ke
;
156 /* The key sequences sent by the Home and End keys, if any. */
157 static char *_rl_term_kh
;
158 static char *_rl_term_kH
;
159 static char *_rl_term_at7
; /* @7 */
162 static char *_rl_term_kD
;
165 static char *_rl_term_kI
;
168 static char *_rl_term_vs
; /* very visible */
169 static char *_rl_term_ve
; /* normal */
171 static void bind_termcap_arrow_keys
PARAMS((Keymap
));
173 /* Variables that hold the screen dimensions, used by the display code. */
174 int _rl_screenwidth
, _rl_screenheight
, _rl_screenchars
;
176 /* Non-zero means the user wants to enable the keypad. */
177 int _rl_enable_keypad
;
179 /* Non-zero means the user wants to enable a meta key. */
180 int _rl_enable_meta
= 1;
182 #if defined (__EMX__)
184 _emx_get_screensize (swp
, shp
)
198 /* Get readline's idea of the screen size. TTY is a file descriptor open
199 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
200 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
201 non-null serve to check whether or not we have initialized termcap. */
203 _rl_get_screen_size (tty
, ignore_env
)
207 #if defined (TIOCGWINSZ)
208 struct winsize window_size
;
209 #endif /* TIOCGWINSZ */
213 #if defined (TIOCGWINSZ)
214 if (ioctl (tty
, TIOCGWINSZ
, &window_size
) == 0)
216 wc
= (int) window_size
.ws_col
;
217 wr
= (int) window_size
.ws_row
;
219 #endif /* TIOCGWINSZ */
221 /* For MinGW, we get the console size from the Windows API. */
222 #if defined (__MINGW32__)
223 HANDLE hConOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
224 if (hConOut
!= INVALID_HANDLE_VALUE
)
226 CONSOLE_SCREEN_BUFFER_INFO scr
;
227 if (GetConsoleScreenBufferInfo (hConOut
, &scr
))
230 wr
= scr
.srWindow
.Bottom
- scr
.srWindow
.Top
+ 1;
235 #if defined (__EMX__)
236 _emx_get_screensize (&_rl_screenwidth
, &_rl_screenheight
);
239 if (ignore_env
|| rl_prefer_env_winsize
== 0)
241 _rl_screenwidth
= wc
;
242 _rl_screenheight
= wr
;
245 _rl_screenwidth
= _rl_screenheight
= -1;
247 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
248 is unset. If we prefer the environment, check it first before
249 assigning the value returned by the kernel. */
250 if (_rl_screenwidth
<= 0)
252 if (ignore_env
== 0 && (ss
= sh_get_env_value ("COLUMNS")))
253 _rl_screenwidth
= atoi (ss
);
255 if (_rl_screenwidth
<= 0)
256 _rl_screenwidth
= wc
;
258 #if defined (__DJGPP__)
259 if (_rl_screenwidth
<= 0)
260 _rl_screenwidth
= ScreenCols ();
262 if (_rl_screenwidth
<= 0 && term_string_buffer
)
263 _rl_screenwidth
= tgetnum ("co");
267 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
269 if (_rl_screenheight
<= 0)
271 if (ignore_env
== 0 && (ss
= sh_get_env_value ("LINES")))
272 _rl_screenheight
= atoi (ss
);
274 if (_rl_screenheight
<= 0)
275 _rl_screenheight
= wr
;
277 #if defined (__DJGPP__)
278 if (_rl_screenheight
<= 0)
279 _rl_screenheight
= ScreenRows ();
281 if (_rl_screenheight
<= 0 && term_string_buffer
)
282 _rl_screenheight
= tgetnum ("li");
286 /* If all else fails, default to 80x24 terminal. */
287 if (_rl_screenwidth
<= 1)
288 _rl_screenwidth
= 80;
290 if (_rl_screenheight
<= 0)
291 _rl_screenheight
= 24;
293 /* If we're being compiled as part of bash, set the environment
294 variables $LINES and $COLUMNS to new values. Otherwise, just
295 do a pair of putenv () or setenv () calls. */
296 sh_set_lines_and_columns (_rl_screenheight
, _rl_screenwidth
);
298 if (_rl_term_autowrap
== 0)
301 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
305 _rl_set_screen_size (rows
, cols
)
308 if (_rl_term_autowrap
== -1)
309 _rl_init_terminal_io (rl_terminal_name
);
312 _rl_screenheight
= rows
;
315 _rl_screenwidth
= cols
;
316 if (_rl_term_autowrap
== 0)
320 if (rows
> 0 || cols
> 0)
321 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
325 rl_set_screen_size (rows
, cols
)
328 _rl_set_screen_size (rows
, cols
);
332 rl_get_screen_size (rows
, cols
)
336 *rows
= _rl_screenheight
;
338 *cols
= _rl_screenwidth
;
342 rl_reset_screen_size ()
344 _rl_get_screen_size (fileno (rl_instream
), 0);
348 rl_resize_terminal ()
350 if (readline_echoing_p
)
352 _rl_get_screen_size (fileno (rl_instream
), 1);
353 if (CUSTOM_REDISPLAY_FUNC ())
354 rl_forced_update_display ();
356 _rl_redisplay_after_sigwinch ();
365 /* This should be kept sorted, just in case we decide to change the
366 search algorithm to something smarter. */
367 static struct _tc_string tc_strings
[] =
369 { "@7", &_rl_term_at7
},
370 { "DC", &_rl_term_DC
},
371 { "IC", &_rl_term_IC
},
372 { "ce", &_rl_term_clreol
},
373 { "cl", &_rl_term_clrpag
},
374 { "cr", &_rl_term_cr
},
375 { "dc", &_rl_term_dc
},
376 { "ei", &_rl_term_ei
},
377 { "ic", &_rl_term_ic
},
378 { "im", &_rl_term_im
},
379 { "kD", &_rl_term_kD
}, /* delete */
380 { "kH", &_rl_term_kH
}, /* home down ?? */
381 { "kI", &_rl_term_kI
}, /* insert */
382 { "kd", &_rl_term_kd
},
383 { "ke", &_rl_term_ke
}, /* end keypad mode */
384 { "kh", &_rl_term_kh
}, /* home */
385 { "kl", &_rl_term_kl
},
386 { "kr", &_rl_term_kr
},
387 { "ks", &_rl_term_ks
}, /* start keypad mode */
388 { "ku", &_rl_term_ku
},
389 { "le", &_rl_term_backspace
},
390 { "mm", &_rl_term_mm
},
391 { "mo", &_rl_term_mo
},
392 #if defined (HACK_TERMCAP_MOTION)
393 { "nd", &_rl_term_forward_char
},
395 { "pc", &_rl_term_pc
},
396 { "up", &_rl_term_up
},
397 { "vb", &_rl_visible_bell
},
398 { "vs", &_rl_term_vs
},
399 { "ve", &_rl_term_ve
},
402 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
404 /* Read the desired terminal capability strings into BP. The capabilities
405 are described in the TC_STRINGS table. */
407 get_term_capabilities (bp
)
410 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
413 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
414 *(tc_strings
[i
].tc_value
) = tgetstr ((char *)tc_strings
[i
].tc_var
, bp
);
416 tcap_initialized
= 1;
420 _rl_init_terminal_io (terminal_name
)
421 const char *terminal_name
;
425 int tty
, tgetent_ret
;
427 term
= terminal_name
? terminal_name
: sh_get_env_value ("TERM");
428 _rl_term_clrpag
= _rl_term_cr
= _rl_term_clreol
= (char *)NULL
;
429 tty
= rl_instream
? fileno (rl_instream
) : 0;
435 _rl_term_im
= _rl_term_ei
= _rl_term_ic
= _rl_term_IC
= (char *)NULL
;
436 _rl_term_up
= _rl_term_dc
= _rl_term_DC
= _rl_visible_bell
= (char *)NULL
;
437 _rl_term_ku
= _rl_term_kd
= _rl_term_kl
= _rl_term_kr
= (char *)NULL
;
438 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
439 _rl_terminal_can_insert
= term_has_meta
= _rl_term_autowrap
= 0;
441 _rl_term_clreol
= _rl_term_clrpag
= _rl_term_backspace
= (char *)NULL
;
442 _rl_term_goto
= _rl_term_pc
= _rl_term_ip
= (char *)NULL
;
443 _rl_term_ks
= _rl_term_ke
=_rl_term_vs
= _rl_term_ve
= (char *)NULL
;
444 _rl_term_kh
= _rl_term_kH
= _rl_term_at7
= _rl_term_kI
= (char *)NULL
;
445 #if defined(HACK_TERMCAP_MOTION)
446 _rl_term_forward_char
= (char *)NULL
;
449 _rl_get_screen_size (tty
, 0);
450 #else /* !__MSDOS__ */
451 /* I've separated this out for later work on not calling tgetent at all
452 if the calling application has supplied a custom redisplay function,
453 (and possibly if the application has supplied a custom input function). */
454 if (CUSTOM_REDISPLAY_FUNC())
460 if (term_string_buffer
== 0)
461 term_string_buffer
= (char *)xmalloc(2032);
463 if (term_buffer
== 0)
464 term_buffer
= (char *)xmalloc(4080);
466 buffer
= term_string_buffer
;
468 tgetent_ret
= tgetent (term_buffer
, term
);
471 if (tgetent_ret
<= 0)
473 FREE (term_string_buffer
);
475 buffer
= term_buffer
= term_string_buffer
= (char *)NULL
;
477 _rl_term_autowrap
= 0; /* used by _rl_get_screen_size */
479 /* Allow calling application to set default height and width, using
480 rl_set_screen_size */
481 if (_rl_screenwidth
<= 0 || _rl_screenheight
<= 0)
483 #if defined (__EMX__)
484 _emx_get_screensize (&_rl_screenwidth
, &_rl_screenheight
);
487 _rl_get_screen_size (tty
, 0);
488 #endif /* !__EMX__ */
492 if (_rl_screenwidth
<= 0 || _rl_screenheight
<= 0)
494 _rl_screenwidth
= 79;
495 _rl_screenheight
= 24;
498 /* Everything below here is used by the redisplay code (tputs). */
499 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
501 _rl_term_im
= _rl_term_ei
= _rl_term_ic
= _rl_term_IC
= (char *)NULL
;
502 _rl_term_up
= _rl_term_dc
= _rl_term_DC
= _rl_visible_bell
= (char *)NULL
;
503 _rl_term_ku
= _rl_term_kd
= _rl_term_kl
= _rl_term_kr
= (char *)NULL
;
504 _rl_term_kh
= _rl_term_kH
= _rl_term_kI
= _rl_term_kD
= (char *)NULL
;
505 _rl_term_ks
= _rl_term_ke
= _rl_term_at7
= (char *)NULL
;
506 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
507 _rl_term_ve
= _rl_term_vs
= (char *)NULL
;
508 #if defined (HACK_TERMCAP_MOTION)
509 term_forward_char
= (char *)NULL
;
511 _rl_terminal_can_insert
= term_has_meta
= 0;
513 /* Reasonable defaults for tgoto(). Readline currently only uses
514 tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
515 change that later... */
517 BC
= _rl_term_backspace
= "\b";
523 get_term_capabilities (&buffer
);
525 /* Set up the variables that the termcap library expects the application
527 PC
= _rl_term_pc
? *_rl_term_pc
: 0;
528 BC
= _rl_term_backspace
;
534 _rl_term_autowrap
= tgetflag ("am") && tgetflag ("xn");
536 /* Allow calling application to set default height and width, using
537 rl_set_screen_size */
538 if (_rl_screenwidth
<= 0 || _rl_screenheight
<= 0)
539 _rl_get_screen_size (tty
, 0);
541 /* "An application program can assume that the terminal can do
542 character insertion if *any one of* the capabilities `IC',
543 `im', `ic' or `ip' is provided." But we can't do anything if
544 only `ip' is provided, so... */
545 _rl_terminal_can_insert
= (_rl_term_IC
|| _rl_term_im
|| _rl_term_ic
);
547 /* Check to see if this terminal has a meta key and clear the capability
548 variables if there is none. */
549 term_has_meta
= (tgetflag ("km") || tgetflag ("MT"));
551 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
553 #endif /* !__MSDOS__ */
555 /* Attempt to find and bind the arrow keys. Do not override already
556 bound keys in an overzealous attempt, however. */
558 bind_termcap_arrow_keys (emacs_standard_keymap
);
560 #if defined (VI_MODE)
561 bind_termcap_arrow_keys (vi_movement_keymap
);
562 bind_termcap_arrow_keys (vi_insertion_keymap
);
568 /* Bind the arrow key sequences from the termcap description in MAP. */
570 bind_termcap_arrow_keys (map
)
575 xkeymap
= _rl_keymap
;
578 rl_bind_keyseq_if_unbound (_rl_term_ku
, rl_get_previous_history
);
579 rl_bind_keyseq_if_unbound (_rl_term_kd
, rl_get_next_history
);
580 rl_bind_keyseq_if_unbound (_rl_term_kr
, rl_forward_char
);
581 rl_bind_keyseq_if_unbound (_rl_term_kl
, rl_backward_char
);
583 rl_bind_keyseq_if_unbound (_rl_term_kh
, rl_beg_of_line
); /* Home */
584 rl_bind_keyseq_if_unbound (_rl_term_at7
, rl_end_of_line
); /* End */
586 rl_bind_keyseq_if_unbound (_rl_term_kD
, rl_delete
);
588 _rl_keymap
= xkeymap
;
597 if (tcap_initialized
== 0)
598 return ((char *)NULL
);
599 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
601 if (tc_strings
[i
].tc_var
[0] == cap
[0] && strcmp (tc_strings
[i
].tc_var
, cap
) == 0)
602 return *(tc_strings
[i
].tc_value
);
604 return ((char *)NULL
);
607 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
610 rl_reset_terminal (terminal_name
)
611 const char *terminal_name
;
613 _rl_screenwidth
= _rl_screenheight
= 0;
614 _rl_init_terminal_io (terminal_name
);
618 /* A function for the use of tputs () */
621 _rl_output_character_function (c
)
624 putc (c
, _rl_out_stream
);
628 _rl_output_character_function (c
)
631 return putc (c
, _rl_out_stream
);
635 /* Write COUNT characters from STRING to the output stream. */
637 _rl_output_some_chars (string
, count
)
641 fwrite (string
, 1, count
, _rl_out_stream
);
644 /* Move the cursor back. */
646 _rl_backspace (count
)
652 if (_rl_term_backspace
)
653 for (i
= 0; i
< count
; i
++)
654 tputs (_rl_term_backspace
, 1, _rl_output_character_function
);
657 for (i
= 0; i
< count
; i
++)
658 putc ('\b', _rl_out_stream
);
662 /* Move to the start of the next line. */
666 #if defined (NEW_TTY_DRIVER)
668 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
669 #endif /* NEW_TTY_DRIVER */
670 putc ('\n', _rl_out_stream
);
674 /* Ring the terminal bell. */
678 if (readline_echoing_p
)
680 switch (_rl_bell_preference
)
690 if (_rl_visible_bell
)
692 tputs (_rl_visible_bell
, 1, _rl_output_character_function
);
698 fprintf (stderr
, "\007");
707 /* **************************************************************** */
709 /* Controlling the Meta Key and Keypad */
711 /* **************************************************************** */
714 _rl_enable_meta_key ()
716 #if !defined (__DJGPP__)
717 if (term_has_meta
&& _rl_term_mm
)
718 tputs (_rl_term_mm
, 1, _rl_output_character_function
);
723 _rl_control_keypad (on
)
726 #if !defined (__DJGPP__)
727 if (on
&& _rl_term_ks
)
728 tputs (_rl_term_ks
, 1, _rl_output_character_function
);
729 else if (!on
&& _rl_term_ke
)
730 tputs (_rl_term_ke
, 1, _rl_output_character_function
);
734 /* **************************************************************** */
736 /* Controlling the Cursor */
738 /* **************************************************************** */
740 /* Set the cursor appropriately depending on IM, which is one of the
741 insert modes (insert or overwrite). Insert mode gets the normal
742 cursor. Overwrite mode gets a very visible cursor. Only does
743 anything if we have both capabilities. */
745 _rl_set_cursor (im
, force
)
749 if (_rl_term_ve
&& _rl_term_vs
)
751 if (force
|| im
!= rl_insert_mode
)
753 if (im
== RL_IM_OVERWRITE
)
754 tputs (_rl_term_vs
, 1, _rl_output_character_function
);
756 tputs (_rl_term_ve
, 1, _rl_output_character_function
);