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"
68 /* **************************************************************** */
70 /* Terminal and Termcap */
72 /* **************************************************************** */
74 static char *term_buffer
= (char *)NULL
;
75 static char *term_string_buffer
= (char *)NULL
;
77 static int tcap_initialized
;
79 /* Non-zero means this terminal can't really do anything. */
82 #if !defined (__linux__)
83 # if defined (__EMX__) || defined (NEED_EXTERN_PC)
85 # endif /* __EMX__ || NEED_EXTERN_PC */
87 #endif /* __linux__ */
89 /* Some strings to control terminal actions. These are output by tputs (). */
90 char *term_goto
, *term_clreol
, *term_cr
, *term_clrpag
, *term_backspace
;
93 /* Non-zero if we determine that the terminal can do character insertion. */
94 int terminal_can_insert
= 0;
96 /* How to insert characters. */
97 char *term_im
, *term_ei
, *term_ic
, *term_ip
, *term_IC
;
99 /* How to delete characters. */
100 char *term_dc
, *term_DC
;
102 #if defined (HACK_TERMCAP_MOTION)
103 char *term_forward_char
;
104 #endif /* HACK_TERMCAP_MOTION */
106 /* How to go up a line. */
109 /* A visible bell, if the terminal can be made to flash the screen. */
110 static char *visible_bell
;
112 /* Non-zero means the terminal can auto-wrap lines. */
113 int _rl_term_autowrap
;
115 /* Non-zero means that this terminal has a meta key. */
116 static int term_has_meta
;
118 /* The sequences to write to turn on and off the meta key, if this
120 static char *term_mm
, *term_mo
;
122 /* The key sequences output by the arrow keys, if this terminal has any. */
123 static char *term_ku
, *term_kd
, *term_kr
, *term_kl
;
125 /* How to initialize and reset the arrow keys, if this terminal has any. */
126 static char *term_ks
, *term_ke
;
128 /* The key sequences sent by the Home and End keys, if any. */
129 static char *term_kh
, *term_kH
;
131 /* Variables that hold the screen dimensions, used by the display code. */
132 int screenwidth
, screenheight
, screenchars
;
134 /* Non-zero means the user wants to enable the keypad. */
135 int _rl_enable_keypad
;
137 /* Non-zero means the user wants to enable a meta key. */
138 int _rl_enable_meta
= 1;
140 #if defined (__EMX__)
142 _emx_get_screensize (swp
, shp
)
156 /* Get readline's idea of the screen size. TTY is a file descriptor open
157 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
158 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
159 non-null serve to check whether or not we have initialized termcap. */
161 _rl_get_screen_size (tty
, ignore_env
)
165 #if defined (TIOCGWINSZ)
166 struct winsize window_size
;
167 #endif /* TIOCGWINSZ */
169 #if defined (TIOCGWINSZ)
170 if (ioctl (tty
, TIOCGWINSZ
, &window_size
) == 0)
172 screenwidth
= (int) window_size
.ws_col
;
173 screenheight
= (int) window_size
.ws_row
;
175 #endif /* TIOCGWINSZ */
177 #if defined (__EMX__)
178 _emx_get_screensize (&screenwidth
, &screenheight
);
181 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
183 if (screenwidth
<= 0)
185 if (ignore_env
== 0 && (ss
= get_env_value ("COLUMNS")))
186 screenwidth
= atoi (ss
);
188 #if !defined (__DJGPP__)
189 if (screenwidth
<= 0 && term_string_buffer
)
190 screenwidth
= tgetnum ("co");
194 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
196 if (screenheight
<= 0)
198 if (ignore_env
== 0 && (ss
= get_env_value ("LINES")))
199 screenheight
= atoi (ss
);
201 #if !defined (__DJGPP__)
202 if (screenheight
<= 0 && term_string_buffer
)
203 screenheight
= tgetnum ("li");
207 /* If all else fails, default to 80x24 terminal. */
208 if (screenwidth
<= 1)
211 if (screenheight
<= 0)
214 /* If we're being compiled as part of bash, set the environment
215 variables $LINES and $COLUMNS to new values. Otherwise, just
216 do a pair of putenv () or setenv () calls. */
217 set_lines_and_columns (screenheight
, screenwidth
);
219 if (_rl_term_autowrap
== 0)
222 screenchars
= screenwidth
* screenheight
;
226 _rl_set_screen_size (rows
, cols
)
232 if (_rl_term_autowrap
== 0)
235 screenchars
= screenwidth
* screenheight
;
239 rl_resize_terminal ()
241 if (readline_echoing_p
)
243 _rl_get_screen_size (fileno (rl_instream
), 1);
244 _rl_redisplay_after_sigwinch ();
253 /* This should be kept sorted, just in case we decide to change the
254 search algorithm to something smarter. */
255 static struct _tc_string tc_strings
[] =
259 { "ce", &term_clreol
},
260 { "cl", &term_clrpag
},
267 { "kh", &term_kh
}, /* home */
268 { "kH", &term_kH
}, /* end */
274 { "le", &term_backspace
},
277 #if defined (HACK_TERMCAP_MOTION)
278 { "nd", &term_forward_char
},
282 { "vb", &visible_bell
},
285 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
287 /* Read the desired terminal capability strings into BP. The capabilities
288 are described in the TC_STRINGS table. */
290 get_term_capabilities (bp
)
293 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
296 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
297 *(tc_strings
[i
].tc_value
) = tgetstr (tc_strings
[i
].tc_var
, bp
);
299 tcap_initialized
= 1;
302 #define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
303 #define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
306 _rl_init_terminal_io (terminal_name
)
310 int tty
, tgetent_ret
;
313 term
= terminal_name
? terminal_name
: get_env_value ("TERM");
314 term_clrpag
= term_cr
= term_clreol
= (char *)NULL
;
315 tty
= rl_instream
? fileno (rl_instream
) : 0;
316 screenwidth
= screenheight
= 0;
321 /* I've separated this out for later work on not calling tgetent at all
322 if the calling application has supplied a custom redisplay function,
323 (and possibly if the application has supplied a custom input function). */
324 if (CUSTOM_REDISPLAY_FUNC())
330 if (term_string_buffer
== 0)
331 term_string_buffer
= xmalloc(2032);
333 if (term_buffer
== 0)
334 term_buffer
= xmalloc(4080);
336 buffer
= term_string_buffer
;
338 tgetent_ret
= tgetent (term_buffer
, term
);
341 if (tgetent_ret
<= 0)
343 FREE (term_string_buffer
);
345 buffer
= term_buffer
= term_string_buffer
= (char *)NULL
;
348 _rl_term_autowrap
= 0; /* used by _rl_get_screen_size */
350 #if defined (__EMX__)
351 _emx_get_screensize (&screenwidth
, &screenheight
);
354 _rl_get_screen_size (tty
, 0);
355 #endif /* !__EMX__ */
358 if (screenwidth
<= 0 || screenheight
<= 0)
364 /* Everything below here is used by the redisplay code (tputs). */
365 screenchars
= screenwidth
* screenheight
;
367 term_im
= term_ei
= term_ic
= term_IC
= (char *)NULL
;
368 term_up
= term_dc
= term_DC
= visible_bell
= (char *)NULL
;
369 term_ku
= term_kd
= term_kl
= term_kr
= (char *)NULL
;
370 term_mm
= term_mo
= (char *)NULL
;
371 #if defined (HACK_TERMCAP_MOTION)
372 term_forward_char
= (char *)NULL
;
374 terminal_can_insert
= term_has_meta
= 0;
376 /* Reasonable defaults for tgoto(). Readline currently only uses
377 tgoto if term_IC or term_DC is defined, but just in case we
378 change that later... */
380 BC
= term_backspace
= "\b";
386 get_term_capabilities (&buffer
);
388 /* Set up the variables that the termcap library expects the application
390 PC
= term_pc
? *term_pc
: 0;
397 _rl_term_autowrap
= tgetflag ("am") && tgetflag ("xn");
399 _rl_get_screen_size (tty
, 0);
401 /* "An application program can assume that the terminal can do
402 character insertion if *any one of* the capabilities `IC',
403 `im', `ic' or `ip' is provided." But we can't do anything if
404 only `ip' is provided, so... */
405 terminal_can_insert
= (term_IC
|| term_im
|| term_ic
);
407 /* Check to see if this terminal has a meta key and clear the capability
408 variables if there is none. */
409 term_has_meta
= (tgetflag ("km") || tgetflag ("MT"));
411 term_mm
= term_mo
= (char *)NULL
;
413 /* Attempt to find and bind the arrow keys. Do not override already
414 bound keys in an overzealous attempt, however. */
415 xkeymap
= _rl_keymap
;
417 _rl_keymap
= emacs_standard_keymap
;
418 _rl_bind_if_unbound (term_ku
, rl_get_previous_history
);
419 _rl_bind_if_unbound (term_kd
, rl_get_next_history
);
420 _rl_bind_if_unbound (term_kr
, rl_forward
);
421 _rl_bind_if_unbound (term_kl
, rl_backward
);
423 _rl_bind_if_unbound (term_kh
, rl_beg_of_line
); /* Home */
424 _rl_bind_if_unbound (term_kH
, rl_end_of_line
); /* End */
426 #if defined (VI_MODE)
427 _rl_keymap
= vi_movement_keymap
;
428 _rl_bind_if_unbound (term_ku
, rl_get_previous_history
);
429 _rl_bind_if_unbound (term_kd
, rl_get_next_history
);
430 _rl_bind_if_unbound (term_kr
, rl_forward
);
431 _rl_bind_if_unbound (term_kl
, rl_backward
);
433 _rl_bind_if_unbound (term_kh
, rl_beg_of_line
); /* Home */
434 _rl_bind_if_unbound (term_kH
, rl_end_of_line
); /* End */
437 _rl_keymap
= xkeymap
;
448 if (tcap_initialized
== 0)
449 return ((char *)NULL
);
450 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
452 if (tc_strings
[i
].tc_var
[0] == cap
[0] && strcmp (tc_strings
[i
].tc_var
, cap
) == 0)
453 return *(tc_strings
[i
].tc_value
);
455 return ((char *)NULL
);
458 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
461 rl_reset_terminal (terminal_name
)
464 _rl_init_terminal_io (terminal_name
);
468 /* A function for the use of tputs () */
471 _rl_output_character_function (c
)
474 putc (c
, _rl_out_stream
);
478 _rl_output_character_function (c
)
481 return putc (c
, _rl_out_stream
);
485 /* Write COUNT characters from STRING to the output stream. */
487 _rl_output_some_chars (string
, count
)
491 fwrite (string
, 1, count
, _rl_out_stream
);
494 /* Move the cursor back. */
496 _rl_backspace (count
)
502 for (i
= 0; i
< count
; i
++)
503 tputs (term_backspace
, 1, _rl_output_character_function
);
505 for (i
= 0; i
< count
; i
++)
506 putc ('\b', _rl_out_stream
);
510 /* Move to the start of the next line. */
514 #if defined (NEW_TTY_DRIVER)
516 tputs (term_cr
, 1, _rl_output_character_function
);
517 #endif /* NEW_TTY_DRIVER */
518 putc ('\n', _rl_out_stream
);
522 /* Ring the terminal bell. */
526 if (readline_echoing_p
)
528 switch (_rl_bell_preference
)
536 tputs (visible_bell
, 1, _rl_output_character_function
);
541 fprintf (stderr
, "\007");
550 /* **************************************************************** */
552 /* Controlling the Meta Key and Keypad */
554 /* **************************************************************** */
557 _rl_enable_meta_key ()
559 #if !defined (__DJGPP__)
560 if (term_has_meta
&& term_mm
)
561 tputs (term_mm
, 1, _rl_output_character_function
);
566 _rl_control_keypad (on
)
569 #if !defined (__DJGPP__)
571 tputs (term_ks
, 1, _rl_output_character_function
);
572 else if (!on
&& term_ke
)
573 tputs (term_ke
, 1, _rl_output_character_function
);