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 1, 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 675 Mass Ave, Cambridge, MA 02139, 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)
53 /* System-specific feature definitions and include files. */
58 #if defined (GWINSZ_IN_SYS_IOCTL)
59 # include <sys/ioctl.h>
60 #endif /* GWINSZ_IN_SYS_IOCTL */
62 /* Some standard library routines. */
66 /* Variables and functions imported from readline.c */
67 extern FILE *_rl_in_stream
, *_rl_out_stream
;
68 extern int readline_echoing_p
;
69 extern int _rl_bell_preference
;
70 extern Keymap _rl_keymap
;
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 /* Non-zero means this terminal can't really do anything. */
86 #if !defined (__linux__)
87 /* If this causes problems, add back the `extern'. */
88 /*extern*/ char PC
, *BC
, *UP
;
89 #endif /* __linux__ */
91 /* Some strings to control terminal actions. These are output by tputs (). */
92 char *term_goto
, *term_clreol
, *term_cr
, *term_clrpag
, *term_backspace
;
95 /* Non-zero if we determine that the terminal can do character insertion. */
96 int terminal_can_insert
= 0;
98 /* How to insert characters. */
99 char *term_im
, *term_ei
, *term_ic
, *term_ip
, *term_IC
;
101 /* How to delete characters. */
102 char *term_dc
, *term_DC
;
104 #if defined (HACK_TERMCAP_MOTION)
105 char *term_forward_char
;
106 #endif /* HACK_TERMCAP_MOTION */
108 /* How to go up a line. */
111 /* A visible bell, if the terminal can be made to flash the screen. */
112 static char *visible_bell
;
114 /* Non-zero means the terminal can auto-wrap lines. */
115 int _rl_term_autowrap
;
117 /* Non-zero means that this terminal has a meta key. */
118 static int term_has_meta
;
120 /* The sequences to write to turn on and off the meta key, if this
122 static char *term_mm
, *term_mo
;
124 /* The key sequences output by the arrow keys, if this terminal has any. */
125 static char *term_ku
, *term_kd
, *term_kr
, *term_kl
;
127 /* How to initialize and reset the arrow keys, if this terminal has any. */
128 static char *term_ks
, *term_ke
;
130 /* The key sequences sent by the Home and End keys, if any. */
131 static char *term_kh
, *term_kH
;
133 /* Variables that hold the screen dimensions, used by the display code. */
134 int screenwidth
, screenheight
, screenchars
;
136 /* Non-zero means the user wants to enable the keypad. */
137 int _rl_enable_keypad
;
139 /* Non-zero means the user wants to enable a meta key. */
140 int _rl_enable_meta
= 1;
142 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
145 rl_reset_terminal (terminal_name
)
148 _rl_init_terminal_io (terminal_name
);
154 set_lines_and_columns (lines
, cols
)
159 #if defined (HAVE_PUTENV)
161 sprintf (b
, "LINES=%d", lines
);
164 sprintf (b
, "COLUMNS=%d", cols
);
166 #else /* !HAVE_PUTENV */
167 # if defined (HAVE_SETENV)
169 sprintf (b
, "%d", lines
);
170 setenv ("LINES", b
, 1);
172 sprintf (b
, "%d", cols
);
173 setenv ("COLUMNS", b
, 1);
174 # endif /* HAVE_SETENV */
175 #endif /* !HAVE_PUTENV */
178 extern void set_lines_and_columns ();
181 /* Get readline's idea of the screen size. TTY is a file descriptor open
182 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
183 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
184 non-null serve to check whether or not we have initialized termcap. */
186 _rl_get_screen_size (tty
, ignore_env
)
190 #if defined (TIOCGWINSZ)
191 struct winsize window_size
;
192 #endif /* TIOCGWINSZ */
194 #if defined (TIOCGWINSZ)
195 if (ioctl (tty
, TIOCGWINSZ
, &window_size
) == 0)
197 screenwidth
= (int) window_size
.ws_col
;
198 screenheight
= (int) window_size
.ws_row
;
200 #endif /* TIOCGWINSZ */
202 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
204 if (screenwidth
<= 0)
206 if (ignore_env
== 0 && (ss
= getenv ("COLUMNS")))
207 screenwidth
= atoi (ss
);
209 if (screenwidth
<= 0 && term_string_buffer
)
210 screenwidth
= tgetnum ("co");
213 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
215 if (screenheight
<= 0)
217 if (ignore_env
== 0 && (ss
= getenv ("LINES")))
218 screenheight
= atoi (ss
);
220 if (screenheight
<= 0 && term_string_buffer
)
221 screenheight
= tgetnum ("li");
224 /* If all else fails, default to 80x24 terminal. */
225 if (screenwidth
<= 1)
228 if (screenheight
<= 0)
231 /* If we're being compiled as part of bash, set the environment
232 variables $LINES and $COLUMNS to new values. Otherwise, just
233 do a pair of putenv () or setenv () calls. */
234 set_lines_and_columns (screenheight
, screenwidth
);
236 if (!_rl_term_autowrap
)
239 screenchars
= screenwidth
* screenheight
;
243 _rl_set_screen_size (rows
, cols
)
249 if (_rl_term_autowrap
== 0)
252 screenchars
= screenwidth
* screenheight
;
260 /* This should be kept sorted, just in case we decide to change the
261 search algorithm to something smarter. */
262 static struct _tc_string tc_strings
[] =
274 "kh", &term_kh
, /* home */
275 "kH", &term_kH
, /* end */
281 "le", &term_backspace
,
284 #if defined (HACK_TERMCAP_MOTION)
285 "nd", &term_forward_char
,
292 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
294 /* Read the desired terminal capability strings into BP. The capabilities
295 are described in the TC_STRINGS table. */
297 get_term_capabilities (bp
)
302 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
303 *(tc_strings
[i
].tc_value
) = tgetstr (tc_strings
[i
].tc_var
, bp
);
304 tcap_initialized
= 1;
308 _rl_init_terminal_io (terminal_name
)
311 #if defined (__GO32__)
312 screenwidth
= ScreenCols ();
313 screenheight
= ScreenRows ();
314 screenchars
= screenwidth
* screenheight
;
316 term_im
= term_ei
= term_ic
= term_IC
= (char *)NULL
;
317 term_up
= term_dc
= term_DC
= visible_bell
= (char *)NULL
;
319 /* Does the __GO32__ have a meta key? I don't know. */
321 term_mm
= term_mo
= (char *)NULL
;
323 /* It probably has arrow keys, but I don't know what they are. */
324 term_ku
= term_kd
= term_kr
= term_kl
= (char *)NULL
;
326 #if defined (HACK_TERMCAP_MOTION)
327 term_forward_char
= (char *)NULL
;
328 #endif /* HACK_TERMCAP_MOTION */
329 terminal_can_insert
= _rl_term_autowrap
= 0;
331 #else /* !__GO32__ */
337 term
= terminal_name
? terminal_name
: getenv ("TERM");
339 if (term_string_buffer
== 0)
340 term_string_buffer
= xmalloc (2032);
342 if (term_buffer
== 0)
343 term_buffer
= xmalloc (4080);
345 buffer
= term_string_buffer
;
347 term_clrpag
= term_cr
= term_clreol
= (char *)NULL
;
352 if (tgetent (term_buffer
, term
) <= 0)
357 screenchars
= 79 * 24;
359 term_im
= term_ei
= term_ic
= term_IC
= (char *)NULL
;
360 term_up
= term_dc
= term_DC
= visible_bell
= (char *)NULL
;
361 term_ku
= term_kd
= term_kl
= term_kr
= (char *)NULL
;
362 #if defined (HACK_TERMCAP_MOTION)
363 term_forward_char
= (char *)NULL
;
365 terminal_can_insert
= 0;
369 get_term_capabilities (&buffer
);
371 /* Set up the variables that the termcap library expects the application
373 PC
= term_pc
? *term_pc
: 0;
380 tty
= rl_instream
? fileno (rl_instream
) : 0;
382 screenwidth
= screenheight
= 0;
384 _rl_term_autowrap
= tgetflag ("am") && tgetflag ("xn");
386 _rl_get_screen_size (tty
, 0);
388 /* "An application program can assume that the terminal can do
389 character insertion if *any one of* the capabilities `IC',
390 `im', `ic' or `ip' is provided." But we can't do anything if
391 only `ip' is provided, so... */
392 terminal_can_insert
= (term_IC
|| term_im
|| term_ic
);
394 /* Check to see if this terminal has a meta key and clear the capability
395 variables if there is none. */
396 term_has_meta
= (tgetflag ("km") || tgetflag ("MT"));
398 term_mm
= term_mo
= (char *)NULL
;
400 /* Attempt to find and bind the arrow keys. Do not override already
401 bound keys in an overzealous attempt, however. */
402 xkeymap
= _rl_keymap
;
404 _rl_keymap
= emacs_standard_keymap
;
405 _rl_bind_if_unbound (term_ku
, rl_get_previous_history
);
406 _rl_bind_if_unbound (term_kd
, rl_get_next_history
);
407 _rl_bind_if_unbound (term_kr
, rl_forward
);
408 _rl_bind_if_unbound (term_kl
, rl_backward
);
410 _rl_bind_if_unbound (term_kh
, rl_beg_of_line
); /* Home */
411 _rl_bind_if_unbound (term_kH
, rl_end_of_line
); /* End */
413 #if defined (VI_MODE)
414 _rl_keymap
= vi_movement_keymap
;
415 _rl_bind_if_unbound (term_ku
, rl_get_previous_history
);
416 _rl_bind_if_unbound (term_kd
, rl_get_next_history
);
417 _rl_bind_if_unbound (term_kr
, rl_forward
);
418 _rl_bind_if_unbound (term_kl
, rl_backward
);
420 _rl_bind_if_unbound (term_kh
, rl_beg_of_line
); /* Home */
421 _rl_bind_if_unbound (term_kH
, rl_end_of_line
); /* End */
424 _rl_keymap
= xkeymap
;
426 #endif /* !__GO32__ */
436 if (tcap_initialized
== 0)
437 return ((char *)NULL
);
438 for (i
= 0; i
< NUM_TC_STRINGS
; i
++)
440 if (tc_strings
[i
].tc_var
[0] == cap
[0] && strcmp (tc_strings
[i
].tc_var
, cap
) == 0)
441 return *(tc_strings
[i
].tc_value
);
443 return ((char *)NULL
);
446 /* A function for the use of tputs () */
448 _rl_output_character_function (c
)
451 return putc (c
, _rl_out_stream
);
454 /* Write COUNT characters from STRING to the output stream. */
456 _rl_output_some_chars (string
, count
)
460 fwrite (string
, 1, count
, _rl_out_stream
);
463 /* Move the cursor back. */
465 _rl_backspace (count
)
470 #if !defined (__GO32__)
472 for (i
= 0; i
< count
; i
++)
473 tputs (term_backspace
, 1, _rl_output_character_function
);
475 #endif /* !__GO32__ */
476 for (i
= 0; i
< count
; i
++)
477 putc ('\b', _rl_out_stream
);
481 /* Move to the start of the next line. */
485 #if defined (NEW_TTY_DRIVER)
487 tputs (term_cr
, 1, _rl_output_character_function
);
488 #endif /* NEW_TTY_DRIVER */
489 putc ('\n', _rl_out_stream
);
493 /* Ring the terminal bell. */
497 if (readline_echoing_p
)
499 #if !defined (__GO32__)
500 switch (_rl_bell_preference
)
508 tputs (visible_bell
, 1, _rl_output_character_function
);
513 fprintf (stderr
, "\007");
518 fprintf (stderr
, "\007");
520 #endif /* __GO32__ */
526 /* **************************************************************** */
528 /* Controlling the Meta Key and Keypad */
530 /* **************************************************************** */
536 return putc (c
, rl_outstream
);
540 _rl_enable_meta_key ()
542 if (term_has_meta
&& term_mm
)
543 tputs (term_mm
, 1, outchar
);
547 _rl_control_keypad (on
)
551 tputs (term_ks
, 1, outchar
);
552 else if (!on
&& term_ke
)
553 tputs (term_ke
, 1, outchar
);