1 /* $FreeBSD: src/contrib/libreadline/terminal.c,v 1.2.2.2 2000/07/06 23:04:25 ache Exp $ */
2 /* terminal.c -- controlling the terminal with termcap. */
4 /* Copyright (C) 1996 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
30 #include "posixstat.h"
32 #if defined (HAVE_SYS_FILE_H)
33 # include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
36 #if defined (HAVE_UNISTD_H)
38 #endif /* HAVE_UNISTD_H */
40 #if defined (HAVE_STDLIB_H)
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
46 #if defined (HAVE_LOCALE_H)
52 /* System-specific feature definitions and include files. */
55 #if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ)
56 # include <sys/ioctl.h>
57 #endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */
62 /* Some standard library routines. */
66 #include "rlprivate.h"
69 /* **************************************************************** */
71 /* Terminal and Termcap */
73 /* **************************************************************** */
75 static char *term_buffer = (char *)NULL;
76 static char *term_string_buffer = (char *)NULL;
78 static int tcap_initialized;
80 /* Non-zero means this terminal can't really do anything. */
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 *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
94 /* Non-zero if we determine that the terminal can do character insertion. */
95 int terminal_can_insert = 0;
97 /* How to insert characters. */
98 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
100 /* How to delete characters. */
101 char *term_dc, *term_DC;
103 #if defined (HACK_TERMCAP_MOTION)
104 char *term_forward_char;
105 #endif /* HACK_TERMCAP_MOTION */
107 /* How to go up a line. */
110 /* A visible bell, if the terminal can be made to flash the screen. */
111 static char *visible_bell;
113 /* Non-zero means the terminal can auto-wrap lines. */
114 int _rl_term_autowrap;
116 /* Non-zero means that this terminal has a meta key. */
117 static int term_has_meta;
119 /* The sequences to write to turn on and off the meta key, if this
121 static char *term_mm, *term_mo;
123 /* The key sequences output by the arrow keys, if this terminal has any. */
124 static char *term_ku, *term_kd, *term_kr, *term_kl;
126 /* How to initialize and reset the arrow keys, if this terminal has any. */
127 static char *term_ks, *term_ke;
129 /* The key sequences sent by the Home and End keys, if any. */
130 static char *term_kh, *term_kH;
132 /* Variables that hold the screen dimensions, used by the display code. */
133 int screenwidth, screenheight, screenchars;
135 /* Non-zero means the user wants to enable the keypad. */
136 int _rl_enable_keypad;
138 /* Non-zero means the user wants to enable a meta key. */
139 int _rl_enable_meta = 1;
141 #if defined (__EMX__)
143 _emx_get_screensize (swp, shp)
157 /* Get readline's idea of the screen size. TTY is a file descriptor open
158 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
159 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
160 non-null serve to check whether or not we have initialized termcap. */
162 _rl_get_screen_size (tty, ignore_env)
166 #if defined (TIOCGWINSZ)
167 struct winsize window_size;
168 #endif /* TIOCGWINSZ */
170 #if defined (TIOCGWINSZ)
171 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
173 screenwidth = (int) window_size.ws_col;
174 screenheight = (int) window_size.ws_row;
176 #endif /* TIOCGWINSZ */
178 #if defined (__EMX__)
179 _emx_get_screensize (&screenwidth, &screenheight);
182 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
184 if (screenwidth <= 0)
186 if (ignore_env == 0 && (ss = get_env_value ("COLUMNS")))
187 screenwidth = atoi (ss);
189 #if !defined (__DJGPP__)
190 if (screenwidth <= 0 && term_string_buffer)
191 screenwidth = tgetnum ("co");
195 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
197 if (screenheight <= 0)
199 if (ignore_env == 0 && (ss = get_env_value ("LINES")))
200 screenheight = atoi (ss);
202 #if !defined (__DJGPP__)
203 if (screenheight <= 0 && term_string_buffer)
204 screenheight = tgetnum ("li");
208 /* If all else fails, default to 80x24 terminal. */
209 if (screenwidth <= 1)
212 if (screenheight <= 0)
215 /* If we're being compiled as part of bash, set the environment
216 variables $LINES and $COLUMNS to new values. Otherwise, just
217 do a pair of putenv () or setenv () calls. */
218 set_lines_and_columns (screenheight, screenwidth);
220 if (_rl_term_autowrap == 0)
223 screenchars = screenwidth * screenheight;
227 _rl_set_screen_size (rows, cols)
233 if (_rl_term_autowrap == 0)
236 screenchars = screenwidth * screenheight;
240 rl_resize_terminal ()
242 if (readline_echoing_p)
244 _rl_get_screen_size (fileno (rl_instream), 1);
245 _rl_redisplay_after_sigwinch ();
254 /* This should be kept sorted, just in case we decide to change the
255 search algorithm to something smarter. */
256 static struct _tc_string tc_strings[] =
260 { "ce", &term_clreol },
261 { "cl", &term_clrpag },
268 { "kh", &term_kh }, /* home */
269 { "@7", &term_kH }, /* end */
275 { "le", &term_backspace },
278 #if defined (HACK_TERMCAP_MOTION)
279 { "nd", &term_forward_char },
283 { "vb", &visible_bell },
286 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
288 /* Read the desired terminal capability strings into BP. The capabilities
289 are described in the TC_STRINGS table. */
291 get_term_capabilities (bp)
294 #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
297 for (i = 0; i < NUM_TC_STRINGS; i++)
298 *(tc_strings[i].tc_value) = tgetstr (tc_strings[i].tc_var, bp);
300 tcap_initialized = 1;
303 #define CUSTOM_REDISPLAY_FUNC() (rl_redisplay_function != rl_redisplay)
304 #define CUSTOM_INPUT_FUNC() (rl_getc_function != rl_getc)
307 _rl_init_terminal_io (terminal_name)
311 int tty, tgetent_ret;
314 term = terminal_name ? terminal_name : get_env_value ("TERM");
315 term_clrpag = term_cr = term_clreol = (char *)NULL;
316 tty = rl_instream ? fileno (rl_instream) : 0;
317 screenwidth = screenheight = 0;
322 /* I've separated this out for later work on not calling tgetent at all
323 if the calling application has supplied a custom redisplay function,
324 (and possibly if the application has supplied a custom input function). */
325 if (CUSTOM_REDISPLAY_FUNC())
331 if (term_string_buffer == 0)
332 term_string_buffer = xmalloc(2032);
334 if (term_buffer == 0)
335 term_buffer = xmalloc(4080);
337 buffer = term_string_buffer;
339 tgetent_ret = tgetent (term_buffer, term);
342 if (tgetent_ret <= 0)
344 FREE (term_string_buffer);
346 buffer = term_buffer = term_string_buffer = (char *)NULL;
349 _rl_term_autowrap = 0; /* used by _rl_get_screen_size */
351 #if defined (__EMX__)
352 _emx_get_screensize (&screenwidth, &screenheight);
355 _rl_get_screen_size (tty, 0);
356 #endif /* !__EMX__ */
359 if (screenwidth <= 0 || screenheight <= 0)
365 /* Everything below here is used by the redisplay code (tputs). */
366 screenchars = screenwidth * screenheight;
368 term_im = term_ei = term_ic = term_IC = (char *)NULL;
369 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
370 term_ku = term_kd = term_kl = term_kr = (char *)NULL;
371 term_mm = term_mo = (char *)NULL;
372 #if defined (HACK_TERMCAP_MOTION)
373 term_forward_char = (char *)NULL;
375 terminal_can_insert = term_has_meta = 0;
377 /* Reasonable defaults for tgoto(). Readline currently only uses
378 tgoto if term_IC or term_DC is defined, but just in case we
379 change that later... */
381 BC = term_backspace = "\b";
387 get_term_capabilities (&buffer);
389 /* Set up the variables that the termcap library expects the application
391 PC = term_pc ? *term_pc : 0;
398 _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
400 _rl_get_screen_size (tty, 0);
402 /* "An application program can assume that the terminal can do
403 character insertion if *any one of* the capabilities `IC',
404 `im', `ic' or `ip' is provided." But we can't do anything if
405 only `ip' is provided, so... */
406 terminal_can_insert = (term_IC || term_im || term_ic);
408 /* Check to see if this terminal has a meta key and clear the capability
409 variables if there is none. */
410 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
412 term_mm = term_mo = (char *)NULL;
414 /* Attempt to find and bind the arrow keys. Do not override already
415 bound keys in an overzealous attempt, however. */
416 xkeymap = _rl_keymap;
418 _rl_keymap = emacs_standard_keymap;
419 _rl_bind_if_unbound (term_ku, rl_get_previous_history);
420 _rl_bind_if_unbound (term_kd, rl_get_next_history);
421 _rl_bind_if_unbound (term_kr, rl_forward);
422 _rl_bind_if_unbound (term_kl, rl_backward);
424 _rl_bind_if_unbound (term_kh, rl_beg_of_line); /* Home */
425 _rl_bind_if_unbound (term_kH, rl_end_of_line); /* End */
427 #if defined (VI_MODE)
428 _rl_keymap = vi_movement_keymap;
429 _rl_bind_if_unbound (term_ku, rl_get_previous_history);
430 _rl_bind_if_unbound (term_kd, rl_get_next_history);
431 _rl_bind_if_unbound (term_kr, rl_forward);
432 _rl_bind_if_unbound (term_kl, rl_backward);
434 _rl_bind_if_unbound (term_kh, rl_beg_of_line); /* Home */
435 _rl_bind_if_unbound (term_kH, rl_end_of_line); /* End */
438 _rl_keymap = xkeymap;
449 if (tcap_initialized == 0)
450 return ((char *)NULL);
451 for (i = 0; i < NUM_TC_STRINGS; i++)
453 if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
454 return *(tc_strings[i].tc_value);
456 return ((char *)NULL);
459 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
462 rl_reset_terminal (terminal_name)
465 _rl_init_terminal_io (terminal_name);
469 /* A function for the use of tputs () */
472 _rl_output_character_function (c)
475 putc (c, _rl_out_stream);
479 _rl_output_character_function (c)
482 return putc (c, _rl_out_stream);
486 /* Write COUNT characters from STRING to the output stream. */
488 _rl_output_some_chars (string, count)
492 fwrite (string, 1, count, _rl_out_stream);
495 /* Move the cursor back. */
497 _rl_backspace (count)
503 for (i = 0; i < count; i++)
504 tputs (term_backspace, 1, _rl_output_character_function);
506 for (i = 0; i < count; i++)
507 putc ('\b', _rl_out_stream);
511 /* Move to the start of the next line. */
515 #if defined (NEW_TTY_DRIVER)
517 tputs (term_cr, 1, _rl_output_character_function);
518 #endif /* NEW_TTY_DRIVER */
519 putc ('\n', _rl_out_stream);
523 /* Ring the terminal bell. */
527 if (readline_echoing_p)
529 switch (_rl_bell_preference)
537 tputs (visible_bell, 1, _rl_output_character_function);
542 fprintf (stderr, "\007");
551 /* **************************************************************** */
553 /* Controlling the Meta Key and Keypad */
555 /* **************************************************************** */
558 _rl_enable_meta_key ()
560 #if !defined (__DJGPP__)
561 if (term_has_meta && term_mm)
562 tputs (term_mm, 1, _rl_output_character_function);
567 _rl_control_keypad (on)
570 #if !defined (__DJGPP__)
572 tputs (term_ks, 1, _rl_output_character_function);
573 else if (!on && term_ke)
574 tputs (term_ke, 1, _rl_output_character_function);