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 */
61 /* Some standard library routines. */
65 #include "rlprivate.h"
69 #define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
70 #define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
72 /* **************************************************************** */
74 /* Terminal and Termcap */
76 /* **************************************************************** */
78 static char *term_buffer
= (char *)NULL
;
79 static char *term_string_buffer
= (char *)NULL
;
81 static int tcap_initialized
;
83 #if !defined (__linux__)
84 # if defined (__EMX__) || defined (NEED_EXTERN_PC)
86 # endif /* __EMX__ || NEED_EXTERN_PC */
88 #endif /* __linux__ */
90 /* Some strings to control terminal actions. These are output by tputs (). */
91 char *_rl_term_clreol
;
92 char *_rl_term_clrpag
;
94 char *_rl_term_backspace
;
98 /* Non-zero if we determine that the terminal can do character insertion. */
99 int _rl_terminal_can_insert
= 0;
101 /* How to insert characters. */
108 /* How to delete characters. */
112 #if defined (HACK_TERMCAP_MOTION)
113 char *_rl_term_forward_char
;
114 #endif /* HACK_TERMCAP_MOTION */
116 /* How to go up a line. */
119 /* A visible bell; char if the terminal can be made to flash the screen. */
120 static char *_rl_visible_bell
;
122 /* Non-zero means the terminal can auto-wrap lines. */
123 int _rl_term_autowrap
;
125 /* Non-zero means that this terminal has a meta key. */
126 static int term_has_meta
;
128 /* The sequences to write to turn on and off the meta key, if this
130 static char *_rl_term_mm
;
131 static char *_rl_term_mo
;
133 /* The key sequences output by the arrow keys, if this terminal has any. */
134 static char *_rl_term_ku
;
135 static char *_rl_term_kd
;
136 static char *_rl_term_kr
;
137 static char *_rl_term_kl
;
139 /* How to initialize and reset the arrow keys, if this terminal has any. */
140 static char *_rl_term_ks
;
141 static char *_rl_term_ke
;
143 /* The key sequences sent by the Home and End keys, if any. */
144 static char *_rl_term_kh
;
145 static char *_rl_term_kH
;
146 static char *_rl_term_at7
; /* @7 */
149 static char *_rl_term_kI
;
152 static char *_rl_term_vs
; /* very visible */
153 static char *_rl_term_ve
; /* normal */
155 static void bind_termcap_arrow_keys
PARAMS((Keymap
));
157 /* Variables that hold the screen dimensions, used by the display code. */
158 int _rl_screenwidth
, _rl_screenheight
, _rl_screenchars
;
160 /* Non-zero means the user wants to enable the keypad. */
161 int _rl_enable_keypad
;
163 /* Non-zero means the user wants to enable a meta key. */
164 int _rl_enable_meta
= 1;
166 #if defined (__EMX__)
168 _emx_get_screensize (swp
, shp
)
182 /* Get readline's idea of the screen size. TTY is a file descriptor open
183 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
184 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
185 non-null serve to check whether or not we have initialized termcap. */
187 _rl_get_screen_size (tty
, ignore_env
)
191 #if defined (TIOCGWINSZ)
192 struct winsize window_size
;
193 #endif /* TIOCGWINSZ */
195 #if defined (TIOCGWINSZ)
196 if (ioctl (tty
, TIOCGWINSZ
, &window_size
) == 0)
198 _rl_screenwidth
= (int) window_size
.ws_col
;
199 _rl_screenheight
= (int) window_size
.ws_row
;
201 #endif /* TIOCGWINSZ */
203 #if defined (__EMX__)
204 _emx_get_screensize (&_rl_screenwidth
, &_rl_screenheight
);
207 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
209 if (_rl_screenwidth
<= 0)
211 if (ignore_env
== 0 && (ss
= sh_get_env_value ("COLUMNS")))
212 _rl_screenwidth
= atoi (ss
);
214 #if !defined (__DJGPP__)
215 if (_rl_screenwidth
<= 0 && term_string_buffer
)
216 _rl_screenwidth
= tgetnum ("co");
220 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
222 if (_rl_screenheight
<= 0)
224 if (ignore_env
== 0 && (ss
= sh_get_env_value ("LINES")))
225 _rl_screenheight
= atoi (ss
);
227 #if !defined (__DJGPP__)
228 if (_rl_screenheight
<= 0 && term_string_buffer
)
229 _rl_screenheight
= tgetnum ("li");
233 /* If all else fails, default to 80x24 terminal. */
234 if (_rl_screenwidth
<= 1)
235 _rl_screenwidth
= 80;
237 if (_rl_screenheight
<= 0)
238 _rl_screenheight
= 24;
240 /* If we're being compiled as part of bash, set the environment
241 variables $LINES and $COLUMNS to new values. Otherwise, just
242 do a pair of putenv () or setenv () calls. */
243 sh_set_lines_and_columns (_rl_screenheight
, _rl_screenwidth
);
245 if (_rl_term_autowrap
== 0)
248 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
252 _rl_set_screen_size (rows
, cols
)
255 if (rows
== 0 || cols
== 0)
258 _rl_screenheight
= rows
;
259 _rl_screenwidth
= cols
;
261 if (_rl_term_autowrap
== 0)
264 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
268 rl_set_screen_size (rows
, cols
)
271 _rl_set_screen_size (rows
, cols
);
275 rl_get_screen_size (rows
, cols
)
279 *rows
= _rl_screenheight
;
281 *cols
= _rl_screenwidth
;
285 rl_resize_terminal ()
287 if (readline_echoing_p
)
289 _rl_get_screen_size (fileno (rl_instream
), 1);
290 if (CUSTOM_REDISPLAY_FUNC ())
291 rl_forced_update_display ();
293 _rl_redisplay_after_sigwinch ();
302 /* This should be kept sorted, just in case we decide to change the
303 search algorithm to something smarter. */
304 static struct _tc_string tc_strings
[] =
306 { "@7", &_rl_term_at7
},
307 { "DC", &_rl_term_DC
},
308 { "IC", &_rl_term_IC
},
309 { "ce", &_rl_term_clreol
},
310 { "cl", &_rl_term_clrpag
},
311 { "cr", &_rl_term_cr
},
312 { "dc", &_rl_term_dc
},
313 { "ei", &_rl_term_ei
},
314 { "ic", &_rl_term_ic
},
315 { "im", &_rl_term_im
},
316 { "kH", &_rl_term_kH
}, /* home down ?? */
317 { "kI", &_rl_term_kI
}, /* insert */
318 { "kd", &_rl_term_kd
},
319 { "ke", &_rl_term_ke
}, /* end keypad mode */
320 { "kh", &_rl_term_kh
}, /* home */
321 { "kl", &_rl_term_kl
},
322 { "kr", &_rl_term_kr
},
323 { "ks", &_rl_term_ks
}, /* start keypad mode */
324 { "ku", &_rl_term_ku
},
325 { "le", &_rl_term_backspace
},
326 { "mm", &_rl_term_mm
},
327 { "mo", &_rl_term_mo
},
328 #if defined (HACK_TERMCAP_MOTION)
329 { "nd", &_rl_term_forward_char
},
331 { "pc", &_rl_term_pc
},
332 { "up", &_rl_term_up
},
333 { "vb", &_rl_visible_bell
},
334 { "vs", &_rl_term_vs
},
335 { "ve", &_rl_term_ve
},
338 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
340 /* Read the desired terminal capability strings into BP. The capabilities
341 are described in the TC_STRINGS table. */
343 get_term_capabilities (bp
)
346 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
349 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
350 *(tc_strings
[i
].tc_value
) = tgetstr ((char *)tc_strings
[i
].tc_var
, bp
);
352 tcap_initialized
= 1;
356 _rl_init_terminal_io (terminal_name
)
357 const char *terminal_name
;
361 int tty
, tgetent_ret
;
363 term
= terminal_name
? terminal_name
: sh_get_env_value ("TERM");
364 _rl_term_clrpag
= _rl_term_cr
= _rl_term_clreol
= (char *)NULL
;
365 tty
= rl_instream
? fileno (rl_instream
) : 0;
366 _rl_screenwidth
= _rl_screenheight
= 0;
371 /* I've separated this out for later work on not calling tgetent at all
372 if the calling application has supplied a custom redisplay function,
373 (and possibly if the application has supplied a custom input function). */
374 if (CUSTOM_REDISPLAY_FUNC())
380 if (term_string_buffer
== 0)
381 term_string_buffer
= (char *)xmalloc(2032);
383 if (term_buffer
== 0)
384 term_buffer
= (char *)xmalloc(4080);
386 buffer
= term_string_buffer
;
388 tgetent_ret
= tgetent (term_buffer
, term
);
391 if (tgetent_ret
<= 0)
393 FREE (term_string_buffer
);
395 buffer
= term_buffer
= term_string_buffer
= (char *)NULL
;
397 _rl_term_autowrap
= 0; /* used by _rl_get_screen_size */
399 #if defined (__EMX__)
400 _emx_get_screensize (&_rl_screenwidth
, &_rl_screenheight
);
403 _rl_get_screen_size (tty
, 0);
404 #endif /* !__EMX__ */
407 if (_rl_screenwidth
<= 0 || _rl_screenheight
<= 0)
409 _rl_screenwidth
= 79;
410 _rl_screenheight
= 24;
413 /* Everything below here is used by the redisplay code (tputs). */
414 _rl_screenchars
= _rl_screenwidth
* _rl_screenheight
;
416 _rl_term_im
= _rl_term_ei
= _rl_term_ic
= _rl_term_IC
= (char *)NULL
;
417 _rl_term_up
= _rl_term_dc
= _rl_term_DC
= _rl_visible_bell
= (char *)NULL
;
418 _rl_term_ku
= _rl_term_kd
= _rl_term_kl
= _rl_term_kr
= (char *)NULL
;
419 _rl_term_kh
= _rl_term_kH
= _rl_term_kI
= (char *)NULL
;
420 _rl_term_ks
= _rl_term_ke
= _rl_term_at7
= (char *)NULL
;
421 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
422 _rl_term_ve
= _rl_term_vs
= (char *)NULL
;
423 #if defined (HACK_TERMCAP_MOTION)
424 term_forward_char
= (char *)NULL
;
426 _rl_terminal_can_insert
= term_has_meta
= 0;
428 /* Reasonable defaults for tgoto(). Readline currently only uses
429 tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
430 change that later... */
432 BC
= _rl_term_backspace
= "\b";
438 get_term_capabilities (&buffer
);
440 /* Set up the variables that the termcap library expects the application
442 PC
= _rl_term_pc
? *_rl_term_pc
: 0;
443 BC
= _rl_term_backspace
;
449 _rl_term_autowrap
= tgetflag ("am") && tgetflag ("xn");
451 _rl_get_screen_size (tty
, 0);
453 /* "An application program can assume that the terminal can do
454 character insertion if *any one of* the capabilities `IC',
455 `im', `ic' or `ip' is provided." But we can't do anything if
456 only `ip' is provided, so... */
457 _rl_terminal_can_insert
= (_rl_term_IC
|| _rl_term_im
|| _rl_term_ic
);
459 /* Check to see if this terminal has a meta key and clear the capability
460 variables if there is none. */
461 term_has_meta
= (tgetflag ("km") || tgetflag ("MT"));
463 _rl_term_mm
= _rl_term_mo
= (char *)NULL
;
465 /* Attempt to find and bind the arrow keys. Do not override already
466 bound keys in an overzealous attempt, however. */
468 bind_termcap_arrow_keys (emacs_standard_keymap
);
470 #if defined (VI_MODE)
471 bind_termcap_arrow_keys (vi_movement_keymap
);
472 bind_termcap_arrow_keys (vi_insertion_keymap
);
478 /* Bind the arrow key sequences from the termcap description in MAP. */
480 bind_termcap_arrow_keys (map
)
485 xkeymap
= _rl_keymap
;
488 rl_bind_keyseq_if_unbound (_rl_term_ku
, rl_get_previous_history
);
489 rl_bind_keyseq_if_unbound (_rl_term_kd
, rl_get_next_history
);
490 rl_bind_keyseq_if_unbound (_rl_term_kr
, rl_forward_char
);
491 rl_bind_keyseq_if_unbound (_rl_term_kl
, rl_backward_char
);
493 rl_bind_keyseq_if_unbound (_rl_term_kh
, rl_beg_of_line
); /* Home */
494 rl_bind_keyseq_if_unbound (_rl_term_at7
, rl_end_of_line
); /* End */
496 _rl_keymap
= xkeymap
;
505 if (tcap_initialized
== 0)
506 return ((char *)NULL
);
507 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
509 if (tc_strings
[i
].tc_var
[0] == cap
[0] && strcmp (tc_strings
[i
].tc_var
, cap
) == 0)
510 return *(tc_strings
[i
].tc_value
);
512 return ((char *)NULL
);
515 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
518 rl_reset_terminal (terminal_name
)
519 const char *terminal_name
;
521 _rl_init_terminal_io (terminal_name
);
525 /* A function for the use of tputs () */
528 _rl_output_character_function (c
)
531 putc (c
, _rl_out_stream
);
535 _rl_output_character_function (c
)
538 return putc (c
, _rl_out_stream
);
542 /* Write COUNT characters from STRING to the output stream. */
544 _rl_output_some_chars (string
, count
)
548 fwrite (string
, 1, count
, _rl_out_stream
);
551 /* Move the cursor back. */
553 _rl_backspace (count
)
558 if (_rl_term_backspace
)
559 for (i
= 0; i
< count
; i
++)
560 tputs (_rl_term_backspace
, 1, _rl_output_character_function
);
562 for (i
= 0; i
< count
; i
++)
563 putc ('\b', _rl_out_stream
);
567 /* Move to the start of the next line. */
571 #if defined (NEW_TTY_DRIVER)
573 tputs (_rl_term_cr
, 1, _rl_output_character_function
);
574 #endif /* NEW_TTY_DRIVER */
575 putc ('\n', _rl_out_stream
);
579 /* Ring the terminal bell. */
583 if (readline_echoing_p
)
585 switch (_rl_bell_preference
)
591 if (_rl_visible_bell
)
593 tputs (_rl_visible_bell
, 1, _rl_output_character_function
);
598 fprintf (stderr
, "\007");
607 /* **************************************************************** */
609 /* Controlling the Meta Key and Keypad */
611 /* **************************************************************** */
614 _rl_enable_meta_key ()
616 #if !defined (__DJGPP__)
617 if (term_has_meta
&& _rl_term_mm
)
618 tputs (_rl_term_mm
, 1, _rl_output_character_function
);
623 _rl_control_keypad (on
)
626 #if !defined (__DJGPP__)
627 if (on
&& _rl_term_ks
)
628 tputs (_rl_term_ks
, 1, _rl_output_character_function
);
629 else if (!on
&& _rl_term_ke
)
630 tputs (_rl_term_ke
, 1, _rl_output_character_function
);
634 /* **************************************************************** */
636 /* Controlling the Cursor */
638 /* **************************************************************** */
640 /* Set the cursor appropriately depending on IM, which is one of the
641 insert modes (insert or overwrite). Insert mode gets the normal
642 cursor. Overwrite mode gets a very visible cursor. Only does
643 anything if we have both capabilities. */
645 _rl_set_cursor (im
, force
)
648 if (_rl_term_ve
&& _rl_term_vs
)
650 if (force
|| im
!= rl_insert_mode
)
652 if (im
== RL_IM_OVERWRITE
)
653 tputs (_rl_term_vs
, 1, _rl_output_character_function
);
655 tputs (_rl_term_ve
, 1, _rl_output_character_function
);