1 /* terminal.c -- controlling the terminal with termcap. */
3 /* Copyright (C) 1996 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 #define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
74 #define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
76 /* **************************************************************** */
78 /* Terminal and Termcap */
80 /* **************************************************************** */
83 static char *term_buffer
= (char *)NULL
;
84 static char *term_string_buffer
= (char *)NULL
;
86 /* Non-zero means this terminal can't really do anything. */
88 #endif /* !__MSDOS__ */
90 static int tcap_initialized
;
92 #if !defined (__linux__)
93 # if defined (__EMX__) || defined (NEED_EXTERN_PC)
95 # endif /* __EMX__ || NEED_EXTERN_PC */
97 #endif /* __linux__ */
99 /* Some strings to control terminal actions. These are output by tputs (). */
100 char *_rl_term_clreol
;
101 char *_rl_term_clrpag
;
103 char *_rl_term_backspace
;
107 /* Non-zero if we determine that the terminal can do character insertion. */
108 int _rl_terminal_can_insert
= 0;
110 /* How to insert characters. */
117 /* How to delete characters. */
121 #if defined (HACK_TERMCAP_MOTION)
122 char *_rl_term_forward_char
;
123 #endif /* HACK_TERMCAP_MOTION */
125 /* How to go up a line. */
128 /* A visible bell; char if the terminal can be made to flash the screen. */
129 static char *_rl_visible_bell
;
131 /* Non-zero means the terminal can auto-wrap lines. */
132 int _rl_term_autowrap
;
134 /* Non-zero means that this terminal has a meta key. */
135 static int term_has_meta
;
137 /* The sequences to write to turn on and off the meta key, if this
139 static char *_rl_term_mm
;
140 static char *_rl_term_mo
;
142 /* The key sequences output by the arrow keys, if this terminal has any. */
143 static char *_rl_term_ku
;
144 static char *_rl_term_kd
;
145 static char *_rl_term_kr
;
146 static char *_rl_term_kl
;
148 /* How to initialize and reset the arrow keys, if this terminal has any. */
149 static char *_rl_term_ks
;
150 static char *_rl_term_ke
;
152 /* The key sequences sent by the Home and End keys, if any. */
153 static char *_rl_term_kh
;
154 static char *_rl_term_kH
;
155 static char *_rl_term_at7
; /* @7 */
158 static char *_rl_term_kI
;
161 static char *_rl_term_vs
; /* very visible */
162 static char *_rl_term_ve
; /* normal */
164 static void bind_termcap_arrow_keys
PARAMS((Keymap
));
166 /* Variables that hold the screen dimensions, used by the display code. */
167 int _rl_screenwidth
, _rl_screenheight
, _rl_screenchars
;
169 /* Non-zero means the user wants to enable the keypad. */
170 int _rl_enable_keypad
;
172 /* Non-zero means the user wants to enable a meta key. */
173 int _rl_enable_meta
= 1;
175 #if defined (__EMX__)
177 _emx_get_screensize (swp
, shp
)
191 /* Get readline's idea of the screen size. TTY is a file descriptor open
192 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
193 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
194 non-null serve to check whether or not we have initialized termcap. */
196 _rl_get_screen_size (tty
, ignore_env
)
200 #if defined (TIOCGWINSZ)
201 struct winsize window_size
;
202 #endif /* TIOCGWINSZ */
204 #if defined (TIOCGWINSZ)
205 if (ioctl (tty
, TIOCGWINSZ
, &window_size
) == 0)
207 _rl_screenwidth
= (int) window_size
.ws_col
;
208 _rl_screenheight
= (int) window_size
.ws_row
;
210 #endif /* TIOCGWINSZ */
212 #if defined (__EMX__)
213 _emx_get_screensize (&_rl_screenwidth
, &_rl_screenheight
);
216 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
218 if (_rl_screenwidth
<= 0)
220 if (ignore_env
== 0 && (ss
= sh_get_env_value ("COLUMNS")))
221 _rl_screenwidth
= atoi (ss
);
223 #if !defined (__DJGPP__)
224 if (_rl_screenwidth
<= 0 && term_string_buffer
)
225 _rl_screenwidth
= tgetnum ("co");
229 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
231 if (_rl_screenheight
<= 0)
233 if (ignore_env
== 0 && (ss
= sh_get_env_value ("LINES")))
234 _rl_screenheight
= atoi (ss
);
236 #if !defined (__DJGPP__)
237 if (_rl_screenheight
<= 0 && term_string_buffer
)
238 _rl_screenheight
= tgetnum ("li");
242 /* If all else fails, default to 80x24 terminal. */
243 if (_rl_screenwidth
<= 1)
244 _rl_screenwidth
= 80;
246 if (_rl_screenheight
<= 0)
247 _rl_screenheight
= 24;
249 /* If we're being compiled as part of bash, set the environment
250 variables $LINES and $COLUMNS to new values. Otherwise, just
251 do a pair of putenv () or setenv () calls. */
252 sh_set_lines_and_columns (_rl_screenheight
, _rl_screenwidth
);
254 if (_rl_term_autowrap
== 0)
257 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
261 _rl_set_screen_size (rows
, cols
)
264 if (rows
== 0 || cols
== 0)
267 _rl_screenheight
= rows
;
268 _rl_screenwidth
= cols
;
270 if (_rl_term_autowrap
== 0)
273 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
277 rl_set_screen_size (rows
, cols
)
280 _rl_set_screen_size (rows
, cols
);
284 rl_get_screen_size (rows
, cols
)
288 *rows
= _rl_screenheight
;
290 *cols
= _rl_screenwidth
;
294 rl_resize_terminal ()
296 if (readline_echoing_p
)
298 _rl_get_screen_size (fileno (rl_instream
), 1);
299 if (CUSTOM_REDISPLAY_FUNC ())
300 rl_forced_update_display ();
302 _rl_redisplay_after_sigwinch ();
311 /* This should be kept sorted, just in case we decide to change the
312 search algorithm to something smarter. */
313 static struct _tc_string tc_strings
[] =
315 { "@7", &_rl_term_at7
},
316 { "DC", &_rl_term_DC
},
317 { "IC", &_rl_term_IC
},
318 { "ce", &_rl_term_clreol
},
319 { "cl", &_rl_term_clrpag
},
320 { "cr", &_rl_term_cr
},
321 { "dc", &_rl_term_dc
},
322 { "ei", &_rl_term_ei
},
323 { "ic", &_rl_term_ic
},
324 { "im", &_rl_term_im
},
325 { "kH", &_rl_term_kH
}, /* home down ?? */
326 { "kI", &_rl_term_kI
}, /* insert */
327 { "kd", &_rl_term_kd
},
328 { "ke", &_rl_term_ke
}, /* end keypad mode */
329 { "kh", &_rl_term_kh
}, /* home */
330 { "kl", &_rl_term_kl
},
331 { "kr", &_rl_term_kr
},
332 { "ks", &_rl_term_ks
}, /* start keypad mode */
333 { "ku", &_rl_term_ku
},
334 { "le", &_rl_term_backspace
},
335 { "mm", &_rl_term_mm
},
336 { "mo", &_rl_term_mo
},
337 #if defined (HACK_TERMCAP_MOTION)
338 { "nd", &_rl_term_forward_char
},
340 { "pc", &_rl_term_pc
},
341 { "up", &_rl_term_up
},
342 { "vb", &_rl_visible_bell
},
343 { "vs", &_rl_term_vs
},
344 { "ve", &_rl_term_ve
},
347 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
349 /* Read the desired terminal capability strings into BP. The capabilities
350 are described in the TC_STRINGS table. */
352 get_term_capabilities (bp
)
355 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
358 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
360 *(tc_strings
[i
].tc_value
) = tgetstr ((char *)tc_strings
[i
].tc_var
, bp
);
362 *(tc_strings
[i
].tc_value
) = tgetstr (tc_strings
[i
].tc_var
, bp
);
365 tcap_initialized
= 1;
369 _rl_init_terminal_io (terminal_name
)
370 const char *terminal_name
;
374 int tty
, tgetent_ret
;
376 term
= terminal_name
? terminal_name
: sh_get_env_value ("TERM");
377 _rl_term_clrpag
= _rl_term_cr
= _rl_term_clreol
= (char *)NULL
;
378 tty
= rl_instream
? fileno (rl_instream
) : 0;
379 _rl_screenwidth
= _rl_screenheight
= 0;
385 term_im
= term_ei
= term_ic
= term_IC
= (char *)NULL
;
386 term_up
= term_dc
= term_DC
= visible_bell
= (char *)NULL
;
387 term_ku
= term_kd
= term_kl
= term_kr
= (char *)NULL
;
388 term_mm
= term_mo
= (char *)NULL
;
389 terminal_can_insert
= term_has_meta
= _rl_term_autowrap
= 0;
392 _rl_get_screen_size (tty
, 0);
393 #else /* !__MSDOS__ */
394 /* I've separated this out for later work on not calling tgetent at all
395 if the calling application has supplied a custom redisplay function,
396 (and possibly if the application has supplied a custom input function). */
397 if (CUSTOM_REDISPLAY_FUNC())
403 if (term_string_buffer
== 0)
404 term_string_buffer
= (char *)xmalloc(2032);
406 if (term_buffer
== 0)
407 term_buffer
= (char *)xmalloc(4080);
409 buffer
= term_string_buffer
;
411 tgetent_ret
= tgetent (term_buffer
, term
);
414 if (tgetent_ret
<= 0)
416 FREE (term_string_buffer
);
418 buffer
= term_buffer
= term_string_buffer
= (char *)NULL
;
420 _rl_term_autowrap
= 0; /* used by _rl_get_screen_size */
422 #if defined (__EMX__)
423 _emx_get_screensize (&_rl_screenwidth
, &_rl_screenheight
);
426 _rl_get_screen_size (tty
, 0);
427 #endif /* !__EMX__ */
430 if (_rl_screenwidth
<= 0 || _rl_screenheight
<= 0)
432 _rl_screenwidth
= 79;
433 _rl_screenheight
= 24;
436 /* Everything below here is used by the redisplay code (tputs). */
437 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
439 _rl_term_im
= _rl_term_ei
= _rl_term_ic
= _rl_term_IC
= (char *)NULL
;
440 _rl_term_up
= _rl_term_dc
= _rl_term_DC
= _rl_visible_bell
= (char *)NULL
;
441 _rl_term_ku
= _rl_term_kd
= _rl_term_kl
= _rl_term_kr
= (char *)NULL
;
442 _rl_term_kh
= _rl_term_kH
= _rl_term_kI
= (char *)NULL
;
443 _rl_term_ks
= _rl_term_ke
= _rl_term_at7
= (char *)NULL
;
444 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
445 _rl_term_ve
= _rl_term_vs
= (char *)NULL
;
446 #if defined (HACK_TERMCAP_MOTION)
447 term_forward_char
= (char *)NULL
;
449 _rl_terminal_can_insert
= term_has_meta
= 0;
451 /* Reasonable defaults for tgoto(). Readline currently only uses
452 tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
453 change that later... */
455 BC
= _rl_term_backspace
= "\b";
461 get_term_capabilities (&buffer
);
463 /* Set up the variables that the termcap library expects the application
465 PC
= _rl_term_pc
? *_rl_term_pc
: 0;
466 BC
= _rl_term_backspace
;
472 _rl_term_autowrap
= tgetflag ("am") && tgetflag ("xn");
474 _rl_get_screen_size (tty
, 0);
476 /* "An application program can assume that the terminal can do
477 character insertion if *any one of* the capabilities `IC',
478 `im', `ic' or `ip' is provided." But we can't do anything if
479 only `ip' is provided, so... */
480 _rl_terminal_can_insert
= (_rl_term_IC
|| _rl_term_im
|| _rl_term_ic
);
482 /* Check to see if this terminal has a meta key and clear the capability
483 variables if there is none. */
484 term_has_meta
= (tgetflag ("km") || tgetflag ("MT"));
486 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
488 #endif /* !__MSDOS__ */
490 /* Attempt to find and bind the arrow keys. Do not override already
491 bound keys in an overzealous attempt, however. */
493 bind_termcap_arrow_keys (emacs_standard_keymap
);
495 #if defined (VI_MODE)
496 bind_termcap_arrow_keys (vi_movement_keymap
);
497 bind_termcap_arrow_keys (vi_insertion_keymap
);
503 /* Bind the arrow key sequences from the termcap description in MAP. */
505 bind_termcap_arrow_keys (map
)
510 xkeymap
= _rl_keymap
;
513 _rl_bind_if_unbound (_rl_term_ku
, rl_get_previous_history
);
514 _rl_bind_if_unbound (_rl_term_kd
, rl_get_next_history
);
515 _rl_bind_if_unbound (_rl_term_kr
, rl_forward
);
516 _rl_bind_if_unbound (_rl_term_kl
, rl_backward
);
518 _rl_bind_if_unbound (_rl_term_kh
, rl_beg_of_line
); /* Home */
519 _rl_bind_if_unbound (_rl_term_at7
, rl_end_of_line
); /* End */
521 _rl_keymap
= xkeymap
;
530 if (tcap_initialized
== 0)
531 return ((char *)NULL
);
532 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
534 if (tc_strings
[i
].tc_var
[0] == cap
[0] && strcmp (tc_strings
[i
].tc_var
, cap
) == 0)
535 return *(tc_strings
[i
].tc_value
);
537 return ((char *)NULL
);
540 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
543 rl_reset_terminal (terminal_name
)
544 const char *terminal_name
;
546 _rl_init_terminal_io (terminal_name
);
550 /* A function for the use of tputs () */
553 _rl_output_character_function (c
)
556 putc (c
, _rl_out_stream
);
560 _rl_output_character_function (c
)
563 return putc (c
, _rl_out_stream
);
567 /* Write COUNT characters from STRING to the output stream. */
569 _rl_output_some_chars (string
, count
)
573 fwrite (string
, 1, count
, _rl_out_stream
);
576 /* Move the cursor back. */
578 _rl_backspace (count
)
584 if (_rl_term_backspace
)
585 for (i
= 0; i
< count
; i
++)
586 tputs (_rl_term_backspace
, 1, _rl_output_character_function
);
589 for (i
= 0; i
< count
; i
++)
590 putc ('\b', _rl_out_stream
);
594 /* Move to the start of the next line. */
598 #if defined (NEW_TTY_DRIVER)
600 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
601 #endif /* NEW_TTY_DRIVER */
602 putc ('\n', _rl_out_stream
);
606 /* Ring the terminal bell. */
610 if (readline_echoing_p
)
612 switch (_rl_bell_preference
)
622 if (_rl_visible_bell
)
624 tputs (_rl_visible_bell
, 1, _rl_output_character_function
);
630 fprintf (stderr
, "\007");
639 /* **************************************************************** */
641 /* Controlling the Meta Key and Keypad */
643 /* **************************************************************** */
646 _rl_enable_meta_key ()
648 #if !defined (__DJGPP__)
649 if (term_has_meta
&& _rl_term_mm
)
650 tputs (_rl_term_mm
, 1, _rl_output_character_function
);
655 _rl_control_keypad (on
)
658 #if !defined (__DJGPP__)
659 if (on
&& _rl_term_ks
)
660 tputs (_rl_term_ks
, 1, _rl_output_character_function
);
661 else if (!on
&& _rl_term_ke
)
662 tputs (_rl_term_ke
, 1, _rl_output_character_function
);
666 /* **************************************************************** */
668 /* Controlling the Cursor */
670 /* **************************************************************** */
672 /* Set the cursor appropriately depending on IM, which is one of the
673 insert modes (insert or overwrite). Insert mode gets the normal
674 cursor. Overwrite mode gets a very visible cursor. Only does
675 anything if we have both capabilities. */
677 _rl_set_cursor (im
, force
)
680 if (_rl_term_ve
&& _rl_term_vs
)
682 if (force
|| im
!= rl_insert_mode
)
684 if (im
== RL_IM_OVERWRITE
)
685 tputs (_rl_term_vs
, 1, _rl_output_character_function
);
687 tputs (_rl_term_ve
, 1, _rl_output_character_function
);