]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/terminal.c
Bash-4.4 distribution sources and documentation
[thirdparty/bash.git] / lib / readline / terminal.c
CommitLineData
ccc6cda3
JA
1/* terminal.c -- controlling the terminal with termcap. */
2
a0c0a00f 3/* Copyright (C) 1996-2015 Free Software Foundation, Inc.
ccc6cda3 4
3185942a
JA
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
ccc6cda3 7
3185942a
JA
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
ccc6cda3
JA
11 (at your option) any later version.
12
3185942a
JA
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ccc6cda3
JA
16 GNU General Public License for more details.
17
3185942a
JA
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*/
21
ccc6cda3
JA
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
25# include <config.h>
26#endif
27
28#include <sys/types.h>
29#include "posixstat.h"
30#include <fcntl.h>
31#if defined (HAVE_SYS_FILE_H)
32# include <sys/file.h>
33#endif /* HAVE_SYS_FILE_H */
34
35#if defined (HAVE_UNISTD_H)
36# include <unistd.h>
37#endif /* HAVE_UNISTD_H */
38
39#if defined (HAVE_STDLIB_H)
40# include <stdlib.h>
41#else
42# include "ansi_stdlib.h"
43#endif /* HAVE_STDLIB_H */
44
45#if defined (HAVE_LOCALE_H)
46# include <locale.h>
47#endif
48
ccc6cda3 49#include <stdio.h>
ccc6cda3
JA
50
51/* System-specific feature definitions and include files. */
52#include "rldefs.h"
53
ac50fbac
CR
54#ifdef __MSDOS__
55# include <pc.h>
56#endif
57
d166f048 58#include "rltty.h"
a0c0a00f
CR
59#if defined (HAVE_SYS_IOCTL_H)
60# include <sys/ioctl.h> /* include for declaration of ioctl */
61#endif
d166f048 62#include "tcap.h"
ccc6cda3
JA
63
64/* Some standard library routines. */
65#include "readline.h"
66#include "history.h"
67
bb70624e
JA
68#include "rlprivate.h"
69#include "rlshell.h"
f73dda09 70#include "xmalloc.h"
d166f048 71
0628567a
JA
72#if defined (__MINGW32__)
73# include <windows.h>
74# include <wincon.h>
75
76static void _win_get_screensize PARAMS((int *, int *));
77#endif
78
79#if defined (__EMX__)
80static void _emx_get_screensize PARAMS((int *, int *));
81#endif
82
ac50fbac
CR
83/* If the calling application sets this to a non-zero value, readline will
84 use the $LINES and $COLUMNS environment variables to set its idea of the
85 window size before interrogating the kernel. */
0628567a 86int rl_prefer_env_winsize = 0;
95732b49 87
ac50fbac
CR
88/* If this is non-zero, readline will set LINES and COLUMNS in the
89 environment when it handles SIGWINCH. */
90int rl_change_environment = 1;
91
ccc6cda3
JA
92/* **************************************************************** */
93/* */
94/* Terminal and Termcap */
95/* */
96/* **************************************************************** */
97
ac50fbac 98#ifndef __MSDOS__
ccc6cda3
JA
99static char *term_buffer = (char *)NULL;
100static char *term_string_buffer = (char *)NULL;
ac50fbac 101#endif
ccc6cda3
JA
102
103static int tcap_initialized;
104
495aee44 105#if !defined (__linux__) && !defined (NCURSES_VERSION)
d166f048
JA
106# if defined (__EMX__) || defined (NEED_EXTERN_PC)
107extern
108# endif /* __EMX__ || NEED_EXTERN_PC */
109char PC, *BC, *UP;
495aee44 110#endif /* !__linux__ && !NCURSES_VERSION */
ccc6cda3
JA
111
112/* Some strings to control terminal actions. These are output by tputs (). */
28ef6c31
JA
113char *_rl_term_clreol;
114char *_rl_term_clrpag;
115char *_rl_term_cr;
116char *_rl_term_backspace;
117char *_rl_term_goto;
118char *_rl_term_pc;
ccc6cda3
JA
119
120/* Non-zero if we determine that the terminal can do character insertion. */
28ef6c31 121int _rl_terminal_can_insert = 0;
ccc6cda3
JA
122
123/* How to insert characters. */
28ef6c31
JA
124char *_rl_term_im;
125char *_rl_term_ei;
126char *_rl_term_ic;
127char *_rl_term_ip;
128char *_rl_term_IC;
ccc6cda3
JA
129
130/* How to delete characters. */
28ef6c31
JA
131char *_rl_term_dc;
132char *_rl_term_DC;
ccc6cda3 133
28ef6c31 134char *_rl_term_forward_char;
ccc6cda3
JA
135
136/* How to go up a line. */
28ef6c31 137char *_rl_term_up;
ccc6cda3 138
28ef6c31
JA
139/* A visible bell; char if the terminal can be made to flash the screen. */
140static char *_rl_visible_bell;
ccc6cda3
JA
141
142/* Non-zero means the terminal can auto-wrap lines. */
0628567a 143int _rl_term_autowrap = -1;
ccc6cda3
JA
144
145/* Non-zero means that this terminal has a meta key. */
146static int term_has_meta;
147
148/* The sequences to write to turn on and off the meta key, if this
28ef6c31
JA
149 terminal has one. */
150static char *_rl_term_mm;
151static char *_rl_term_mo;
ccc6cda3
JA
152
153/* The key sequences output by the arrow keys, if this terminal has any. */
28ef6c31
JA
154static char *_rl_term_ku;
155static char *_rl_term_kd;
156static char *_rl_term_kr;
157static char *_rl_term_kl;
ccc6cda3
JA
158
159/* How to initialize and reset the arrow keys, if this terminal has any. */
28ef6c31
JA
160static char *_rl_term_ks;
161static char *_rl_term_ke;
ccc6cda3
JA
162
163/* The key sequences sent by the Home and End keys, if any. */
28ef6c31
JA
164static char *_rl_term_kh;
165static char *_rl_term_kH;
7117c2d2
JA
166static char *_rl_term_at7; /* @7 */
167
95732b49
JA
168/* Delete key */
169static char *_rl_term_kD;
170
7117c2d2
JA
171/* Insert key */
172static char *_rl_term_kI;
173
174/* Cursor control */
175static char *_rl_term_vs; /* very visible */
176static char *_rl_term_ve; /* normal */
177
178static void bind_termcap_arrow_keys PARAMS((Keymap));
ccc6cda3
JA
179
180/* Variables that hold the screen dimensions, used by the display code. */
28ef6c31 181int _rl_screenwidth, _rl_screenheight, _rl_screenchars;
ccc6cda3
JA
182
183/* Non-zero means the user wants to enable the keypad. */
184int _rl_enable_keypad;
185
186/* Non-zero means the user wants to enable a meta key. */
187int _rl_enable_meta = 1;
188
bb70624e
JA
189#if defined (__EMX__)
190static void
191_emx_get_screensize (swp, shp)
192 int *swp, *shp;
193{
194 int sz[2];
195
196 _scrsize (sz);
197
198 if (swp)
199 *swp = sz[0];
200 if (shp)
201 *shp = sz[1];
202}
203#endif
204
0628567a
JA
205#if defined (__MINGW32__)
206static void
207_win_get_screensize (swp, shp)
208 int *swp, *shp;
209{
210 HANDLE hConOut;
211 CONSOLE_SCREEN_BUFFER_INFO scr;
212
213 hConOut = GetStdHandle (STD_OUTPUT_HANDLE);
214 if (hConOut != INVALID_HANDLE_VALUE)
215 {
216 if (GetConsoleScreenBufferInfo (hConOut, &scr))
217 {
218 *swp = scr.dwSize.X;
219 *shp = scr.srWindow.Bottom - scr.srWindow.Top + 1;
220 }
221 }
222}
223#endif
224
ccc6cda3
JA
225/* Get readline's idea of the screen size. TTY is a file descriptor open
226 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
227 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
228 non-null serve to check whether or not we have initialized termcap. */
229void
230_rl_get_screen_size (tty, ignore_env)
231 int tty, ignore_env;
232{
233 char *ss;
234#if defined (TIOCGWINSZ)
235 struct winsize window_size;
236#endif /* TIOCGWINSZ */
95732b49 237 int wr, wc;
ccc6cda3 238
95732b49 239 wr = wc = -1;
ccc6cda3
JA
240#if defined (TIOCGWINSZ)
241 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
242 {
95732b49
JA
243 wc = (int) window_size.ws_col;
244 wr = (int) window_size.ws_row;
ccc6cda3
JA
245 }
246#endif /* TIOCGWINSZ */
247
d166f048 248#if defined (__EMX__)
0628567a
JA
249 _emx_get_screensize (&wc, &wr);
250#elif defined (__MINGW32__)
251 _win_get_screensize (&wc, &wr);
d166f048
JA
252#endif
253
95732b49
JA
254 if (ignore_env || rl_prefer_env_winsize == 0)
255 {
256 _rl_screenwidth = wc;
257 _rl_screenheight = wr;
258 }
259 else
260 _rl_screenwidth = _rl_screenheight = -1;
261
ccc6cda3 262 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
95732b49
JA
263 is unset. If we prefer the environment, check it first before
264 assigning the value returned by the kernel. */
28ef6c31 265 if (_rl_screenwidth <= 0)
ccc6cda3 266 {
28ef6c31
JA
267 if (ignore_env == 0 && (ss = sh_get_env_value ("COLUMNS")))
268 _rl_screenwidth = atoi (ss);
ccc6cda3 269
95732b49
JA
270 if (_rl_screenwidth <= 0)
271 _rl_screenwidth = wc;
272
ac50fbac
CR
273#if defined (__DJGPP__)
274 if (_rl_screenwidth <= 0)
275 _rl_screenwidth = ScreenCols ();
276#else
28ef6c31
JA
277 if (_rl_screenwidth <= 0 && term_string_buffer)
278 _rl_screenwidth = tgetnum ("co");
bb70624e 279#endif
ccc6cda3
JA
280 }
281
282 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
283 is unset. */
28ef6c31 284 if (_rl_screenheight <= 0)
ccc6cda3 285 {
28ef6c31
JA
286 if (ignore_env == 0 && (ss = sh_get_env_value ("LINES")))
287 _rl_screenheight = atoi (ss);
ccc6cda3 288
95732b49
JA
289 if (_rl_screenheight <= 0)
290 _rl_screenheight = wr;
291
ac50fbac
CR
292#if defined (__DJGPP__)
293 if (_rl_screenheight <= 0)
294 _rl_screenheight = ScreenRows ();
295#else
28ef6c31
JA
296 if (_rl_screenheight <= 0 && term_string_buffer)
297 _rl_screenheight = tgetnum ("li");
bb70624e 298#endif
ccc6cda3
JA
299 }
300
301 /* If all else fails, default to 80x24 terminal. */
28ef6c31
JA
302 if (_rl_screenwidth <= 1)
303 _rl_screenwidth = 80;
ccc6cda3 304
28ef6c31
JA
305 if (_rl_screenheight <= 0)
306 _rl_screenheight = 24;
ccc6cda3
JA
307
308 /* If we're being compiled as part of bash, set the environment
309 variables $LINES and $COLUMNS to new values. Otherwise, just
310 do a pair of putenv () or setenv () calls. */
ac50fbac
CR
311 if (rl_change_environment)
312 sh_set_lines_and_columns (_rl_screenheight, _rl_screenwidth);
ccc6cda3 313
bb70624e 314 if (_rl_term_autowrap == 0)
28ef6c31 315 _rl_screenwidth--;
ccc6cda3 316
28ef6c31 317 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
ccc6cda3
JA
318}
319
320void
321_rl_set_screen_size (rows, cols)
322 int rows, cols;
323{
0628567a
JA
324 if (_rl_term_autowrap == -1)
325 _rl_init_terminal_io (rl_terminal_name);
326
95732b49
JA
327 if (rows > 0)
328 _rl_screenheight = rows;
329 if (cols > 0)
330 {
331 _rl_screenwidth = cols;
332 if (_rl_term_autowrap == 0)
333 _rl_screenwidth--;
334 }
ccc6cda3 335
95732b49
JA
336 if (rows > 0 || cols > 0)
337 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
28ef6c31
JA
338}
339
340void
341rl_set_screen_size (rows, cols)
342 int rows, cols;
343{
344 _rl_set_screen_size (rows, cols);
ccc6cda3
JA
345}
346
28ef6c31
JA
347void
348rl_get_screen_size (rows, cols)
349 int *rows, *cols;
350{
351 if (rows)
352 *rows = _rl_screenheight;
353 if (cols)
354 *cols = _rl_screenwidth;
355}
95732b49
JA
356
357void
358rl_reset_screen_size ()
359{
360 _rl_get_screen_size (fileno (rl_instream), 0);
361}
ac50fbac
CR
362
363void
364_rl_sigwinch_resize_terminal ()
365{
366 _rl_get_screen_size (fileno (rl_instream), 1);
367}
368
b72432fd
JA
369void
370rl_resize_terminal ()
371{
495aee44 372 _rl_get_screen_size (fileno (rl_instream), 1);
3185942a 373 if (_rl_echoing_p)
b72432fd 374 {
7117c2d2
JA
375 if (CUSTOM_REDISPLAY_FUNC ())
376 rl_forced_update_display ();
89a92869 377 else if (RL_ISSTATE(RL_STATE_REDISPLAYING) == 0)
7117c2d2 378 _rl_redisplay_after_sigwinch ();
b72432fd
JA
379 }
380}
381
ccc6cda3 382struct _tc_string {
3185942a 383 const char * const tc_var;
ccc6cda3
JA
384 char **tc_value;
385};
386
387/* This should be kept sorted, just in case we decide to change the
388 search algorithm to something smarter. */
3185942a 389static const struct _tc_string tc_strings[] =
ccc6cda3 390{
7117c2d2 391 { "@7", &_rl_term_at7 },
28ef6c31
JA
392 { "DC", &_rl_term_DC },
393 { "IC", &_rl_term_IC },
394 { "ce", &_rl_term_clreol },
395 { "cl", &_rl_term_clrpag },
396 { "cr", &_rl_term_cr },
397 { "dc", &_rl_term_dc },
398 { "ei", &_rl_term_ei },
399 { "ic", &_rl_term_ic },
400 { "im", &_rl_term_im },
95732b49 401 { "kD", &_rl_term_kD }, /* delete */
7117c2d2
JA
402 { "kH", &_rl_term_kH }, /* home down ?? */
403 { "kI", &_rl_term_kI }, /* insert */
28ef6c31 404 { "kd", &_rl_term_kd },
7117c2d2 405 { "ke", &_rl_term_ke }, /* end keypad mode */
28ef6c31 406 { "kh", &_rl_term_kh }, /* home */
28ef6c31
JA
407 { "kl", &_rl_term_kl },
408 { "kr", &_rl_term_kr },
7117c2d2 409 { "ks", &_rl_term_ks }, /* start keypad mode */
28ef6c31 410 { "ku", &_rl_term_ku },
28ef6c31
JA
411 { "le", &_rl_term_backspace },
412 { "mm", &_rl_term_mm },
413 { "mo", &_rl_term_mo },
28ef6c31 414 { "nd", &_rl_term_forward_char },
28ef6c31
JA
415 { "pc", &_rl_term_pc },
416 { "up", &_rl_term_up },
417 { "vb", &_rl_visible_bell },
7117c2d2
JA
418 { "vs", &_rl_term_vs },
419 { "ve", &_rl_term_ve },
ccc6cda3
JA
420};
421
422#define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
423
424/* Read the desired terminal capability strings into BP. The capabilities
425 are described in the TC_STRINGS table. */
426static void
427get_term_capabilities (bp)
428 char **bp;
429{
bb70624e 430#if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
ccc6cda3
JA
431 register int i;
432
433 for (i = 0; i < NUM_TC_STRINGS; i++)
f73dda09 434 *(tc_strings[i].tc_value) = tgetstr ((char *)tc_strings[i].tc_var, bp);
bb70624e 435#endif
ccc6cda3
JA
436 tcap_initialized = 1;
437}
438
439int
440_rl_init_terminal_io (terminal_name)
28ef6c31 441 const char *terminal_name;
ccc6cda3 442{
28ef6c31
JA
443 const char *term;
444 char *buffer;
bb70624e 445 int tty, tgetent_ret;
ccc6cda3 446
28ef6c31
JA
447 term = terminal_name ? terminal_name : sh_get_env_value ("TERM");
448 _rl_term_clrpag = _rl_term_cr = _rl_term_clreol = (char *)NULL;
bb70624e 449 tty = rl_instream ? fileno (rl_instream) : 0;
ccc6cda3 450
bb70624e
JA
451 if (term == 0)
452 term = "dumb";
ccc6cda3 453
ac50fbac
CR
454#ifdef __MSDOS__
455 _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
456 _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
457 _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
458 _rl_term_mm = _rl_term_mo = (char *)NULL;
459 _rl_terminal_can_insert = term_has_meta = _rl_term_autowrap = 0;
460 _rl_term_cr = "\r";
461 _rl_term_clreol = _rl_term_clrpag = _rl_term_backspace = (char *)NULL;
462 _rl_term_goto = _rl_term_pc = _rl_term_ip = (char *)NULL;
463 _rl_term_ks = _rl_term_ke =_rl_term_vs = _rl_term_ve = (char *)NULL;
464 _rl_term_kh = _rl_term_kH = _rl_term_at7 = _rl_term_kI = (char *)NULL;
465#if defined(HACK_TERMCAP_MOTION)
466 _rl_term_forward_char = (char *)NULL;
467#endif
468
469 _rl_get_screen_size (tty, 0);
470#else /* !__MSDOS__ */
bb70624e
JA
471 /* I've separated this out for later work on not calling tgetent at all
472 if the calling application has supplied a custom redisplay function,
473 (and possibly if the application has supplied a custom input function). */
474 if (CUSTOM_REDISPLAY_FUNC())
475 {
476 tgetent_ret = -1;
477 }
478 else
479 {
480 if (term_string_buffer == 0)
f73dda09 481 term_string_buffer = (char *)xmalloc(2032);
ccc6cda3 482
bb70624e 483 if (term_buffer == 0)
f73dda09 484 term_buffer = (char *)xmalloc(4080);
ccc6cda3 485
bb70624e 486 buffer = term_string_buffer;
ccc6cda3 487
bb70624e
JA
488 tgetent_ret = tgetent (term_buffer, term);
489 }
ccc6cda3 490
bb70624e 491 if (tgetent_ret <= 0)
ccc6cda3 492 {
bb70624e
JA
493 FREE (term_string_buffer);
494 FREE (term_buffer);
495 buffer = term_buffer = term_string_buffer = (char *)NULL;
496
bb70624e
JA
497 _rl_term_autowrap = 0; /* used by _rl_get_screen_size */
498
95732b49
JA
499 /* Allow calling application to set default height and width, using
500 rl_set_screen_size */
501 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
502 {
bb70624e 503#if defined (__EMX__)
95732b49
JA
504 _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
505 _rl_screenwidth--;
bb70624e 506#else /* !__EMX__ */
95732b49 507 _rl_get_screen_size (tty, 0);
bb70624e 508#endif /* !__EMX__ */
95732b49 509 }
bb70624e
JA
510
511 /* Defaults. */
28ef6c31 512 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
bb70624e 513 {
28ef6c31
JA
514 _rl_screenwidth = 79;
515 _rl_screenheight = 24;
bb70624e
JA
516 }
517
518 /* Everything below here is used by the redisplay code (tputs). */
28ef6c31
JA
519 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
520 _rl_term_cr = "\r";
521 _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
522 _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
523 _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
95732b49 524 _rl_term_kh = _rl_term_kH = _rl_term_kI = _rl_term_kD = (char *)NULL;
7117c2d2 525 _rl_term_ks = _rl_term_ke = _rl_term_at7 = (char *)NULL;
28ef6c31 526 _rl_term_mm = _rl_term_mo = (char *)NULL;
7117c2d2 527 _rl_term_ve = _rl_term_vs = (char *)NULL;
0628567a 528 _rl_term_forward_char = (char *)NULL;
28ef6c31 529 _rl_terminal_can_insert = term_has_meta = 0;
bb70624e
JA
530
531 /* Reasonable defaults for tgoto(). Readline currently only uses
28ef6c31 532 tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
bb70624e
JA
533 change that later... */
534 PC = '\0';
28ef6c31
JA
535 BC = _rl_term_backspace = "\b";
536 UP = _rl_term_up;
bb70624e 537
ccc6cda3
JA
538 return 0;
539 }
540
541 get_term_capabilities (&buffer);
542
543 /* Set up the variables that the termcap library expects the application
544 to provide. */
28ef6c31
JA
545 PC = _rl_term_pc ? *_rl_term_pc : 0;
546 BC = _rl_term_backspace;
547 UP = _rl_term_up;
ccc6cda3 548
28ef6c31
JA
549 if (!_rl_term_cr)
550 _rl_term_cr = "\r";
ccc6cda3 551
ccc6cda3
JA
552 _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
553
95732b49
JA
554 /* Allow calling application to set default height and width, using
555 rl_set_screen_size */
556 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
557 _rl_get_screen_size (tty, 0);
ccc6cda3
JA
558
559 /* "An application program can assume that the terminal can do
560 character insertion if *any one of* the capabilities `IC',
561 `im', `ic' or `ip' is provided." But we can't do anything if
562 only `ip' is provided, so... */
28ef6c31 563 _rl_terminal_can_insert = (_rl_term_IC || _rl_term_im || _rl_term_ic);
ccc6cda3
JA
564
565 /* Check to see if this terminal has a meta key and clear the capability
566 variables if there is none. */
0001803f
CR
567 term_has_meta = tgetflag ("km") != 0;
568 if (term_has_meta == 0)
28ef6c31 569 _rl_term_mm = _rl_term_mo = (char *)NULL;
ac50fbac 570#endif /* !__MSDOS__ */
ccc6cda3
JA
571
572 /* Attempt to find and bind the arrow keys. Do not override already
573 bound keys in an overzealous attempt, however. */
ccc6cda3 574
7117c2d2 575 bind_termcap_arrow_keys (emacs_standard_keymap);
ccc6cda3
JA
576
577#if defined (VI_MODE)
7117c2d2
JA
578 bind_termcap_arrow_keys (vi_movement_keymap);
579 bind_termcap_arrow_keys (vi_insertion_keymap);
580#endif /* VI_MODE */
581
582 return 0;
583}
584
585/* Bind the arrow key sequences from the termcap description in MAP. */
586static void
587bind_termcap_arrow_keys (map)
588 Keymap map;
589{
590 Keymap xkeymap;
591
592 xkeymap = _rl_keymap;
593 _rl_keymap = map;
594
b80f6443
JA
595 rl_bind_keyseq_if_unbound (_rl_term_ku, rl_get_previous_history);
596 rl_bind_keyseq_if_unbound (_rl_term_kd, rl_get_next_history);
597 rl_bind_keyseq_if_unbound (_rl_term_kr, rl_forward_char);
598 rl_bind_keyseq_if_unbound (_rl_term_kl, rl_backward_char);
ccc6cda3 599
b80f6443
JA
600 rl_bind_keyseq_if_unbound (_rl_term_kh, rl_beg_of_line); /* Home */
601 rl_bind_keyseq_if_unbound (_rl_term_at7, rl_end_of_line); /* End */
ccc6cda3 602
95732b49
JA
603 rl_bind_keyseq_if_unbound (_rl_term_kD, rl_delete);
604
ccc6cda3 605 _rl_keymap = xkeymap;
ccc6cda3
JA
606}
607
608char *
609rl_get_termcap (cap)
28ef6c31 610 const char *cap;
ccc6cda3
JA
611{
612 register int i;
613
614 if (tcap_initialized == 0)
615 return ((char *)NULL);
616 for (i = 0; i < NUM_TC_STRINGS; i++)
617 {
618 if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
619 return *(tc_strings[i].tc_value);
620 }
621 return ((char *)NULL);
622}
623
d166f048
JA
624/* Re-initialize the terminal considering that the TERM/TERMCAP variable
625 has changed. */
626int
627rl_reset_terminal (terminal_name)
28ef6c31 628 const char *terminal_name;
d166f048 629{
95732b49 630 _rl_screenwidth = _rl_screenheight = 0;
d166f048
JA
631 _rl_init_terminal_io (terminal_name);
632 return 0;
633}
634
ccc6cda3 635/* A function for the use of tputs () */
cce855bc
JA
636#ifdef _MINIX
637void
638_rl_output_character_function (c)
639 int c;
640{
641 putc (c, _rl_out_stream);
642}
643#else /* !_MINIX */
ccc6cda3
JA
644int
645_rl_output_character_function (c)
646 int c;
647{
648 return putc (c, _rl_out_stream);
649}
cce855bc 650#endif /* !_MINIX */
bb70624e 651
ccc6cda3
JA
652/* Write COUNT characters from STRING to the output stream. */
653void
654_rl_output_some_chars (string, count)
28ef6c31 655 const char *string;
ccc6cda3
JA
656 int count;
657{
658 fwrite (string, 1, count, _rl_out_stream);
659}
660
661/* Move the cursor back. */
662int
663_rl_backspace (count)
664 int count;
665{
666 register int i;
667
ac50fbac 668#ifndef __MSDOS__
28ef6c31 669 if (_rl_term_backspace)
ccc6cda3 670 for (i = 0; i < count; i++)
28ef6c31 671 tputs (_rl_term_backspace, 1, _rl_output_character_function);
ccc6cda3 672 else
ac50fbac 673#endif
ccc6cda3
JA
674 for (i = 0; i < count; i++)
675 putc ('\b', _rl_out_stream);
676 return 0;
677}
678
679/* Move to the start of the next line. */
680int
28ef6c31 681rl_crlf ()
ccc6cda3 682{
3185942a 683#if defined (NEW_TTY_DRIVER) || defined (__MINT__)
28ef6c31
JA
684 if (_rl_term_cr)
685 tputs (_rl_term_cr, 1, _rl_output_character_function);
3185942a 686#endif /* NEW_TTY_DRIVER || __MINT__ */
ccc6cda3
JA
687 putc ('\n', _rl_out_stream);
688 return 0;
689}
690
691/* Ring the terminal bell. */
692int
28ef6c31 693rl_ding ()
ccc6cda3 694{
3185942a 695 if (_rl_echoing_p)
ccc6cda3 696 {
ccc6cda3
JA
697 switch (_rl_bell_preference)
698 {
699 case NO_BELL:
700 default:
701 break;
702 case VISIBLE_BELL:
28ef6c31 703 if (_rl_visible_bell)
ccc6cda3 704 {
ac50fbac
CR
705#ifdef __DJGPP__
706 ScreenVisualBell ();
707#else
28ef6c31 708 tputs (_rl_visible_bell, 1, _rl_output_character_function);
ac50fbac 709#endif
ccc6cda3
JA
710 break;
711 }
712 /* FALLTHROUGH */
713 case AUDIBLE_BELL:
714 fprintf (stderr, "\007");
715 fflush (stderr);
716 break;
717 }
ccc6cda3
JA
718 return (0);
719 }
720 return (-1);
721}
722
723/* **************************************************************** */
724/* */
725/* Controlling the Meta Key and Keypad */
726/* */
727/* **************************************************************** */
728
ac50fbac
CR
729static int enabled_meta = 0; /* flag indicating we enabled meta mode */
730
d166f048 731void
ccc6cda3
JA
732_rl_enable_meta_key ()
733{
bb70624e 734#if !defined (__DJGPP__)
28ef6c31 735 if (term_has_meta && _rl_term_mm)
ac50fbac
CR
736 {
737 tputs (_rl_term_mm, 1, _rl_output_character_function);
738 enabled_meta = 1;
739 }
740#endif
741}
742
743void
744_rl_disable_meta_key ()
745{
746#if !defined (__DJGPP__)
747 if (term_has_meta && _rl_term_mo && enabled_meta)
748 {
749 tputs (_rl_term_mo, 1, _rl_output_character_function);
750 enabled_meta = 0;
751 }
bb70624e 752#endif
ccc6cda3
JA
753}
754
755void
756_rl_control_keypad (on)
757 int on;
758{
bb70624e 759#if !defined (__DJGPP__)
28ef6c31
JA
760 if (on && _rl_term_ks)
761 tputs (_rl_term_ks, 1, _rl_output_character_function);
762 else if (!on && _rl_term_ke)
763 tputs (_rl_term_ke, 1, _rl_output_character_function);
bb70624e 764#endif
ccc6cda3 765}
7117c2d2
JA
766
767/* **************************************************************** */
768/* */
769/* Controlling the Cursor */
770/* */
771/* **************************************************************** */
772
773/* Set the cursor appropriately depending on IM, which is one of the
774 insert modes (insert or overwrite). Insert mode gets the normal
775 cursor. Overwrite mode gets a very visible cursor. Only does
776 anything if we have both capabilities. */
777void
778_rl_set_cursor (im, force)
779 int im, force;
780{
ac50fbac 781#ifndef __MSDOS__
7117c2d2
JA
782 if (_rl_term_ve && _rl_term_vs)
783 {
784 if (force || im != rl_insert_mode)
785 {
786 if (im == RL_IM_OVERWRITE)
787 tputs (_rl_term_vs, 1, _rl_output_character_function);
788 else
789 tputs (_rl_term_ve, 1, _rl_output_character_function);
790 }
791 }
ac50fbac 792#endif
7117c2d2 793}