blob: 1212ec469a50521ff7905ec8d4ae29f7d6291c4f [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* terminal.c -- controlling the terminal with termcap. */
2
Jari Aalto31859422009-01-12 13:36:28 +00003/* Copyright (C) 1996-2009 Free Software Foundation, Inc.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00004
Jari Aalto31859422009-01-12 13:36:28 +00005 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
Jari Aaltoccc6cda1996-12-23 17:02:34 +00007
Jari Aalto31859422009-01-12 13:36:28 +00008 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
Jari Aaltoccc6cda1996-12-23 17:02:34 +000011 (at your option) any later version.
12
Jari Aalto31859422009-01-12 13:36:28 +000013 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
Jari Aaltoccc6cda1996-12-23 17:02:34 +000016 GNU General Public License for more details.
17
Jari Aalto31859422009-01-12 13:36:28 +000018 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
Jari Aaltoccc6cda1996-12-23 17:02:34 +000022#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
Jari Aaltoccc6cda1996-12-23 17:02:34 +000049#include <stdio.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000050
51/* System-specific feature definitions and include files. */
52#include "rldefs.h"
53
Jari Aaltod166f041997-06-05 14:59:13 +000054#if defined (GWINSZ_IN_SYS_IOCTL) && !defined (TIOCGWINSZ)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000055# include <sys/ioctl.h>
Jari Aaltod166f041997-06-05 14:59:13 +000056#endif /* GWINSZ_IN_SYS_IOCTL && !TIOCGWINSZ */
57
Chet Rameyac50fba2014-02-26 09:36:43 -050058#ifdef __MSDOS__
59# include <pc.h>
60#endif
61
Jari Aaltod166f041997-06-05 14:59:13 +000062#include "rltty.h"
63#include "tcap.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000064
65/* Some standard library routines. */
66#include "readline.h"
67#include "history.h"
68
Jari Aaltobb706242000-03-17 21:46:59 +000069#include "rlprivate.h"
70#include "rlshell.h"
Jari Aaltof73dda02001-11-13 17:56:06 +000071#include "xmalloc.h"
Jari Aaltod166f041997-06-05 14:59:13 +000072
Jari Aalto06285672006-10-10 14:15:34 +000073#if defined (__MINGW32__)
74# include <windows.h>
75# include <wincon.h>
76
77static void _win_get_screensize PARAMS((int *, int *));
78#endif
79
80#if defined (__EMX__)
81static void _emx_get_screensize PARAMS((int *, int *));
82#endif
83
Chet Rameyac50fba2014-02-26 09:36:43 -050084/* If the calling application sets this to a non-zero value, readline will
85 use the $LINES and $COLUMNS environment variables to set its idea of the
86 window size before interrogating the kernel. */
Jari Aalto06285672006-10-10 14:15:34 +000087int rl_prefer_env_winsize = 0;
Jari Aalto95732b42005-12-07 14:08:12 +000088
Chet Rameyac50fba2014-02-26 09:36:43 -050089/* If this is non-zero, readline will set LINES and COLUMNS in the
90 environment when it handles SIGWINCH. */
91int rl_change_environment = 1;
92
Jari Aaltoccc6cda1996-12-23 17:02:34 +000093/* **************************************************************** */
94/* */
95/* Terminal and Termcap */
96/* */
97/* **************************************************************** */
98
Chet Rameyac50fba2014-02-26 09:36:43 -050099#ifndef __MSDOS__
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000100static char *term_buffer = (char *)NULL;
101static char *term_string_buffer = (char *)NULL;
Chet Rameyac50fba2014-02-26 09:36:43 -0500102#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000103
104static int tcap_initialized;
105
Chet Ramey495aee42011-11-22 19:11:26 -0500106#if !defined (__linux__) && !defined (NCURSES_VERSION)
Jari Aaltod166f041997-06-05 14:59:13 +0000107# if defined (__EMX__) || defined (NEED_EXTERN_PC)
108extern
109# endif /* __EMX__ || NEED_EXTERN_PC */
110char PC, *BC, *UP;
Chet Ramey495aee42011-11-22 19:11:26 -0500111#endif /* !__linux__ && !NCURSES_VERSION */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000112
113/* Some strings to control terminal actions. These are output by tputs (). */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000114char *_rl_term_clreol;
115char *_rl_term_clrpag;
116char *_rl_term_cr;
117char *_rl_term_backspace;
118char *_rl_term_goto;
119char *_rl_term_pc;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000120
121/* Non-zero if we determine that the terminal can do character insertion. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000122int _rl_terminal_can_insert = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000123
124/* How to insert characters. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000125char *_rl_term_im;
126char *_rl_term_ei;
127char *_rl_term_ic;
128char *_rl_term_ip;
129char *_rl_term_IC;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000130
131/* How to delete characters. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000132char *_rl_term_dc;
133char *_rl_term_DC;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000134
Jari Aalto28ef6c32001-04-06 19:14:31 +0000135char *_rl_term_forward_char;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000136
137/* How to go up a line. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000138char *_rl_term_up;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000139
Jari Aalto28ef6c32001-04-06 19:14:31 +0000140/* A visible bell; char if the terminal can be made to flash the screen. */
141static char *_rl_visible_bell;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000142
143/* Non-zero means the terminal can auto-wrap lines. */
Jari Aalto06285672006-10-10 14:15:34 +0000144int _rl_term_autowrap = -1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000145
146/* Non-zero means that this terminal has a meta key. */
147static int term_has_meta;
148
149/* The sequences to write to turn on and off the meta key, if this
Jari Aalto28ef6c32001-04-06 19:14:31 +0000150 terminal has one. */
151static char *_rl_term_mm;
152static char *_rl_term_mo;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000153
154/* The key sequences output by the arrow keys, if this terminal has any. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000155static char *_rl_term_ku;
156static char *_rl_term_kd;
157static char *_rl_term_kr;
158static char *_rl_term_kl;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000159
160/* How to initialize and reset the arrow keys, if this terminal has any. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000161static char *_rl_term_ks;
162static char *_rl_term_ke;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000163
164/* The key sequences sent by the Home and End keys, if any. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000165static char *_rl_term_kh;
166static char *_rl_term_kH;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000167static char *_rl_term_at7; /* @7 */
168
Jari Aalto95732b42005-12-07 14:08:12 +0000169/* Delete key */
170static char *_rl_term_kD;
171
Jari Aalto7117c2d2002-07-17 14:10:11 +0000172/* Insert key */
173static char *_rl_term_kI;
174
175/* Cursor control */
176static char *_rl_term_vs; /* very visible */
177static char *_rl_term_ve; /* normal */
178
179static void bind_termcap_arrow_keys PARAMS((Keymap));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000180
181/* Variables that hold the screen dimensions, used by the display code. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000182int _rl_screenwidth, _rl_screenheight, _rl_screenchars;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000183
184/* Non-zero means the user wants to enable the keypad. */
185int _rl_enable_keypad;
186
187/* Non-zero means the user wants to enable a meta key. */
188int _rl_enable_meta = 1;
189
Jari Aaltobb706242000-03-17 21:46:59 +0000190#if defined (__EMX__)
191static void
192_emx_get_screensize (swp, shp)
193 int *swp, *shp;
194{
195 int sz[2];
196
197 _scrsize (sz);
198
199 if (swp)
200 *swp = sz[0];
201 if (shp)
202 *shp = sz[1];
203}
204#endif
205
Jari Aalto06285672006-10-10 14:15:34 +0000206#if defined (__MINGW32__)
207static void
208_win_get_screensize (swp, shp)
209 int *swp, *shp;
210{
211 HANDLE hConOut;
212 CONSOLE_SCREEN_BUFFER_INFO scr;
213
214 hConOut = GetStdHandle (STD_OUTPUT_HANDLE);
215 if (hConOut != INVALID_HANDLE_VALUE)
216 {
217 if (GetConsoleScreenBufferInfo (hConOut, &scr))
218 {
219 *swp = scr.dwSize.X;
220 *shp = scr.srWindow.Bottom - scr.srWindow.Top + 1;
221 }
222 }
223}
224#endif
225
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000226/* Get readline's idea of the screen size. TTY is a file descriptor open
227 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
228 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
229 non-null serve to check whether or not we have initialized termcap. */
230void
231_rl_get_screen_size (tty, ignore_env)
232 int tty, ignore_env;
233{
234 char *ss;
235#if defined (TIOCGWINSZ)
236 struct winsize window_size;
237#endif /* TIOCGWINSZ */
Jari Aalto95732b42005-12-07 14:08:12 +0000238 int wr, wc;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000239
Jari Aalto95732b42005-12-07 14:08:12 +0000240 wr = wc = -1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000241#if defined (TIOCGWINSZ)
242 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
243 {
Jari Aalto95732b42005-12-07 14:08:12 +0000244 wc = (int) window_size.ws_col;
245 wr = (int) window_size.ws_row;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000246 }
247#endif /* TIOCGWINSZ */
248
Jari Aaltod166f041997-06-05 14:59:13 +0000249#if defined (__EMX__)
Jari Aalto06285672006-10-10 14:15:34 +0000250 _emx_get_screensize (&wc, &wr);
251#elif defined (__MINGW32__)
252 _win_get_screensize (&wc, &wr);
Jari Aaltod166f041997-06-05 14:59:13 +0000253#endif
254
Jari Aalto95732b42005-12-07 14:08:12 +0000255 if (ignore_env || rl_prefer_env_winsize == 0)
256 {
257 _rl_screenwidth = wc;
258 _rl_screenheight = wr;
259 }
260 else
261 _rl_screenwidth = _rl_screenheight = -1;
262
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000263 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
Jari Aalto95732b42005-12-07 14:08:12 +0000264 is unset. If we prefer the environment, check it first before
265 assigning the value returned by the kernel. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000266 if (_rl_screenwidth <= 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000267 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000268 if (ignore_env == 0 && (ss = sh_get_env_value ("COLUMNS")))
269 _rl_screenwidth = atoi (ss);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000270
Jari Aalto95732b42005-12-07 14:08:12 +0000271 if (_rl_screenwidth <= 0)
272 _rl_screenwidth = wc;
273
Chet Rameyac50fba2014-02-26 09:36:43 -0500274#if defined (__DJGPP__)
275 if (_rl_screenwidth <= 0)
276 _rl_screenwidth = ScreenCols ();
277#else
Jari Aalto28ef6c32001-04-06 19:14:31 +0000278 if (_rl_screenwidth <= 0 && term_string_buffer)
279 _rl_screenwidth = tgetnum ("co");
Jari Aaltobb706242000-03-17 21:46:59 +0000280#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000281 }
282
283 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
284 is unset. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000285 if (_rl_screenheight <= 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000286 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000287 if (ignore_env == 0 && (ss = sh_get_env_value ("LINES")))
288 _rl_screenheight = atoi (ss);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000289
Jari Aalto95732b42005-12-07 14:08:12 +0000290 if (_rl_screenheight <= 0)
291 _rl_screenheight = wr;
292
Chet Rameyac50fba2014-02-26 09:36:43 -0500293#if defined (__DJGPP__)
294 if (_rl_screenheight <= 0)
295 _rl_screenheight = ScreenRows ();
296#else
Jari Aalto28ef6c32001-04-06 19:14:31 +0000297 if (_rl_screenheight <= 0 && term_string_buffer)
298 _rl_screenheight = tgetnum ("li");
Jari Aaltobb706242000-03-17 21:46:59 +0000299#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000300 }
301
302 /* If all else fails, default to 80x24 terminal. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000303 if (_rl_screenwidth <= 1)
304 _rl_screenwidth = 80;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000305
Jari Aalto28ef6c32001-04-06 19:14:31 +0000306 if (_rl_screenheight <= 0)
307 _rl_screenheight = 24;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000308
309 /* If we're being compiled as part of bash, set the environment
310 variables $LINES and $COLUMNS to new values. Otherwise, just
311 do a pair of putenv () or setenv () calls. */
Chet Rameyac50fba2014-02-26 09:36:43 -0500312 if (rl_change_environment)
313 sh_set_lines_and_columns (_rl_screenheight, _rl_screenwidth);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000314
Jari Aaltobb706242000-03-17 21:46:59 +0000315 if (_rl_term_autowrap == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000316 _rl_screenwidth--;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000317
Jari Aalto28ef6c32001-04-06 19:14:31 +0000318 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000319}
320
321void
322_rl_set_screen_size (rows, cols)
323 int rows, cols;
324{
Jari Aalto06285672006-10-10 14:15:34 +0000325 if (_rl_term_autowrap == -1)
326 _rl_init_terminal_io (rl_terminal_name);
327
Jari Aalto95732b42005-12-07 14:08:12 +0000328 if (rows > 0)
329 _rl_screenheight = rows;
330 if (cols > 0)
331 {
332 _rl_screenwidth = cols;
333 if (_rl_term_autowrap == 0)
334 _rl_screenwidth--;
335 }
Jari Aalto28ef6c32001-04-06 19:14:31 +0000336
Jari Aalto95732b42005-12-07 14:08:12 +0000337 if (rows > 0 || cols > 0)
338 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000339}
340
Jari Aaltob72432f1999-02-19 17:11:39 +0000341void
Jari Aalto28ef6c32001-04-06 19:14:31 +0000342rl_set_screen_size (rows, cols)
343 int rows, cols;
344{
345 _rl_set_screen_size (rows, cols);
346}
347
348void
349rl_get_screen_size (rows, cols)
350 int *rows, *cols;
351{
352 if (rows)
353 *rows = _rl_screenheight;
354 if (cols)
355 *cols = _rl_screenwidth;
356}
Jari Aalto95732b42005-12-07 14:08:12 +0000357
358void
359rl_reset_screen_size ()
360{
361 _rl_get_screen_size (fileno (rl_instream), 0);
362}
Chet Rameyac50fba2014-02-26 09:36:43 -0500363
364void
365_rl_sigwinch_resize_terminal ()
366{
367 _rl_get_screen_size (fileno (rl_instream), 1);
368}
369
Jari Aalto28ef6c32001-04-06 19:14:31 +0000370void
Jari Aaltob72432f1999-02-19 17:11:39 +0000371rl_resize_terminal ()
372{
Chet Ramey495aee42011-11-22 19:11:26 -0500373 _rl_get_screen_size (fileno (rl_instream), 1);
Jari Aalto31859422009-01-12 13:36:28 +0000374 if (_rl_echoing_p)
Jari Aaltob72432f1999-02-19 17:11:39 +0000375 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000376 if (CUSTOM_REDISPLAY_FUNC ())
377 rl_forced_update_display ();
Chet Ramey89a92862011-11-21 20:49:12 -0500378 else if (RL_ISSTATE(RL_STATE_REDISPLAYING) == 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000379 _rl_redisplay_after_sigwinch ();
Jari Aaltob72432f1999-02-19 17:11:39 +0000380 }
381}
382
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000383struct _tc_string {
Jari Aalto31859422009-01-12 13:36:28 +0000384 const char * const tc_var;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000385 char **tc_value;
386};
387
388/* This should be kept sorted, just in case we decide to change the
389 search algorithm to something smarter. */
Jari Aalto31859422009-01-12 13:36:28 +0000390static const struct _tc_string tc_strings[] =
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000391{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000392 { "@7", &_rl_term_at7 },
Jari Aalto28ef6c32001-04-06 19:14:31 +0000393 { "DC", &_rl_term_DC },
394 { "IC", &_rl_term_IC },
395 { "ce", &_rl_term_clreol },
396 { "cl", &_rl_term_clrpag },
397 { "cr", &_rl_term_cr },
398 { "dc", &_rl_term_dc },
399 { "ei", &_rl_term_ei },
400 { "ic", &_rl_term_ic },
401 { "im", &_rl_term_im },
Jari Aalto95732b42005-12-07 14:08:12 +0000402 { "kD", &_rl_term_kD }, /* delete */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000403 { "kH", &_rl_term_kH }, /* home down ?? */
404 { "kI", &_rl_term_kI }, /* insert */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000405 { "kd", &_rl_term_kd },
Jari Aalto7117c2d2002-07-17 14:10:11 +0000406 { "ke", &_rl_term_ke }, /* end keypad mode */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000407 { "kh", &_rl_term_kh }, /* home */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000408 { "kl", &_rl_term_kl },
409 { "kr", &_rl_term_kr },
Jari Aalto7117c2d2002-07-17 14:10:11 +0000410 { "ks", &_rl_term_ks }, /* start keypad mode */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000411 { "ku", &_rl_term_ku },
Jari Aalto28ef6c32001-04-06 19:14:31 +0000412 { "le", &_rl_term_backspace },
413 { "mm", &_rl_term_mm },
414 { "mo", &_rl_term_mo },
Jari Aalto28ef6c32001-04-06 19:14:31 +0000415 { "nd", &_rl_term_forward_char },
Jari Aalto28ef6c32001-04-06 19:14:31 +0000416 { "pc", &_rl_term_pc },
417 { "up", &_rl_term_up },
418 { "vb", &_rl_visible_bell },
Jari Aalto7117c2d2002-07-17 14:10:11 +0000419 { "vs", &_rl_term_vs },
420 { "ve", &_rl_term_ve },
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000421};
422
423#define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
424
425/* Read the desired terminal capability strings into BP. The capabilities
426 are described in the TC_STRINGS table. */
427static void
428get_term_capabilities (bp)
429 char **bp;
430{
Jari Aaltobb706242000-03-17 21:46:59 +0000431#if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000432 register int i;
433
434 for (i = 0; i < NUM_TC_STRINGS; i++)
Jari Aaltof73dda02001-11-13 17:56:06 +0000435 *(tc_strings[i].tc_value) = tgetstr ((char *)tc_strings[i].tc_var, bp);
Jari Aaltobb706242000-03-17 21:46:59 +0000436#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000437 tcap_initialized = 1;
438}
439
440int
441_rl_init_terminal_io (terminal_name)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000442 const char *terminal_name;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000443{
Jari Aalto28ef6c32001-04-06 19:14:31 +0000444 const char *term;
445 char *buffer;
Jari Aaltobb706242000-03-17 21:46:59 +0000446 int tty, tgetent_ret;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000447
Jari Aalto28ef6c32001-04-06 19:14:31 +0000448 term = terminal_name ? terminal_name : sh_get_env_value ("TERM");
449 _rl_term_clrpag = _rl_term_cr = _rl_term_clreol = (char *)NULL;
Jari Aaltobb706242000-03-17 21:46:59 +0000450 tty = rl_instream ? fileno (rl_instream) : 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000451
452 if (term == 0)
453 term = "dumb";
454
Chet Rameyac50fba2014-02-26 09:36:43 -0500455#ifdef __MSDOS__
456 _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
457 _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
458 _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
459 _rl_term_mm = _rl_term_mo = (char *)NULL;
460 _rl_terminal_can_insert = term_has_meta = _rl_term_autowrap = 0;
461 _rl_term_cr = "\r";
462 _rl_term_clreol = _rl_term_clrpag = _rl_term_backspace = (char *)NULL;
463 _rl_term_goto = _rl_term_pc = _rl_term_ip = (char *)NULL;
464 _rl_term_ks = _rl_term_ke =_rl_term_vs = _rl_term_ve = (char *)NULL;
465 _rl_term_kh = _rl_term_kH = _rl_term_at7 = _rl_term_kI = (char *)NULL;
466#if defined(HACK_TERMCAP_MOTION)
467 _rl_term_forward_char = (char *)NULL;
468#endif
469
470 _rl_get_screen_size (tty, 0);
471#else /* !__MSDOS__ */
Jari Aaltobb706242000-03-17 21:46:59 +0000472 /* I've separated this out for later work on not calling tgetent at all
473 if the calling application has supplied a custom redisplay function,
474 (and possibly if the application has supplied a custom input function). */
475 if (CUSTOM_REDISPLAY_FUNC())
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000476 {
Jari Aaltobb706242000-03-17 21:46:59 +0000477 tgetent_ret = -1;
478 }
479 else
480 {
481 if (term_string_buffer == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +0000482 term_string_buffer = (char *)xmalloc(2032);
Jari Aaltobb706242000-03-17 21:46:59 +0000483
484 if (term_buffer == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +0000485 term_buffer = (char *)xmalloc(4080);
Jari Aaltobb706242000-03-17 21:46:59 +0000486
487 buffer = term_string_buffer;
488
489 tgetent_ret = tgetent (term_buffer, term);
490 }
491
492 if (tgetent_ret <= 0)
493 {
494 FREE (term_string_buffer);
495 FREE (term_buffer);
496 buffer = term_buffer = term_string_buffer = (char *)NULL;
497
Jari Aaltobb706242000-03-17 21:46:59 +0000498 _rl_term_autowrap = 0; /* used by _rl_get_screen_size */
499
Jari Aalto95732b42005-12-07 14:08:12 +0000500 /* Allow calling application to set default height and width, using
501 rl_set_screen_size */
502 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
503 {
Jari Aaltobb706242000-03-17 21:46:59 +0000504#if defined (__EMX__)
Jari Aalto95732b42005-12-07 14:08:12 +0000505 _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight);
506 _rl_screenwidth--;
Jari Aaltobb706242000-03-17 21:46:59 +0000507#else /* !__EMX__ */
Jari Aalto95732b42005-12-07 14:08:12 +0000508 _rl_get_screen_size (tty, 0);
Jari Aaltobb706242000-03-17 21:46:59 +0000509#endif /* !__EMX__ */
Jari Aalto95732b42005-12-07 14:08:12 +0000510 }
Jari Aaltobb706242000-03-17 21:46:59 +0000511
512 /* Defaults. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000513 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
Jari Aaltobb706242000-03-17 21:46:59 +0000514 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000515 _rl_screenwidth = 79;
516 _rl_screenheight = 24;
Jari Aaltobb706242000-03-17 21:46:59 +0000517 }
518
519 /* Everything below here is used by the redisplay code (tputs). */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000520 _rl_screenchars = _rl_screenwidth * _rl_screenheight;
521 _rl_term_cr = "\r";
522 _rl_term_im = _rl_term_ei = _rl_term_ic = _rl_term_IC = (char *)NULL;
523 _rl_term_up = _rl_term_dc = _rl_term_DC = _rl_visible_bell = (char *)NULL;
524 _rl_term_ku = _rl_term_kd = _rl_term_kl = _rl_term_kr = (char *)NULL;
Jari Aalto95732b42005-12-07 14:08:12 +0000525 _rl_term_kh = _rl_term_kH = _rl_term_kI = _rl_term_kD = (char *)NULL;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000526 _rl_term_ks = _rl_term_ke = _rl_term_at7 = (char *)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000527 _rl_term_mm = _rl_term_mo = (char *)NULL;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000528 _rl_term_ve = _rl_term_vs = (char *)NULL;
Jari Aalto06285672006-10-10 14:15:34 +0000529 _rl_term_forward_char = (char *)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000530 _rl_terminal_can_insert = term_has_meta = 0;
Jari Aaltobb706242000-03-17 21:46:59 +0000531
532 /* Reasonable defaults for tgoto(). Readline currently only uses
Jari Aalto28ef6c32001-04-06 19:14:31 +0000533 tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
Jari Aaltobb706242000-03-17 21:46:59 +0000534 change that later... */
535 PC = '\0';
Jari Aalto28ef6c32001-04-06 19:14:31 +0000536 BC = _rl_term_backspace = "\b";
537 UP = _rl_term_up;
Jari Aaltobb706242000-03-17 21:46:59 +0000538
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000539 return 0;
540 }
541
542 get_term_capabilities (&buffer);
543
544 /* Set up the variables that the termcap library expects the application
545 to provide. */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000546 PC = _rl_term_pc ? *_rl_term_pc : 0;
547 BC = _rl_term_backspace;
548 UP = _rl_term_up;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000549
Jari Aalto28ef6c32001-04-06 19:14:31 +0000550 if (!_rl_term_cr)
551 _rl_term_cr = "\r";
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000552
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000553 _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn");
554
Jari Aalto95732b42005-12-07 14:08:12 +0000555 /* Allow calling application to set default height and width, using
556 rl_set_screen_size */
557 if (_rl_screenwidth <= 0 || _rl_screenheight <= 0)
558 _rl_get_screen_size (tty, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000559
560 /* "An application program can assume that the terminal can do
561 character insertion if *any one of* the capabilities `IC',
562 `im', `ic' or `ip' is provided." But we can't do anything if
563 only `ip' is provided, so... */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000564 _rl_terminal_can_insert = (_rl_term_IC || _rl_term_im || _rl_term_ic);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000565
566 /* Check to see if this terminal has a meta key and clear the capability
567 variables if there is none. */
Chet Ramey00018032011-11-21 20:51:19 -0500568 term_has_meta = tgetflag ("km") != 0;
569 if (term_has_meta == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000570 _rl_term_mm = _rl_term_mo = (char *)NULL;
Chet Rameyac50fba2014-02-26 09:36:43 -0500571#endif /* !__MSDOS__ */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000572
573 /* Attempt to find and bind the arrow keys. Do not override already
574 bound keys in an overzealous attempt, however. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000575
Jari Aalto7117c2d2002-07-17 14:10:11 +0000576 bind_termcap_arrow_keys (emacs_standard_keymap);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000577
578#if defined (VI_MODE)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000579 bind_termcap_arrow_keys (vi_movement_keymap);
580 bind_termcap_arrow_keys (vi_insertion_keymap);
581#endif /* VI_MODE */
582
583 return 0;
584}
585
586/* Bind the arrow key sequences from the termcap description in MAP. */
587static void
588bind_termcap_arrow_keys (map)
589 Keymap map;
590{
591 Keymap xkeymap;
592
593 xkeymap = _rl_keymap;
594 _rl_keymap = map;
595
Jari Aaltob80f6442004-07-27 13:29:18 +0000596 rl_bind_keyseq_if_unbound (_rl_term_ku, rl_get_previous_history);
597 rl_bind_keyseq_if_unbound (_rl_term_kd, rl_get_next_history);
598 rl_bind_keyseq_if_unbound (_rl_term_kr, rl_forward_char);
599 rl_bind_keyseq_if_unbound (_rl_term_kl, rl_backward_char);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000600
Jari Aaltob80f6442004-07-27 13:29:18 +0000601 rl_bind_keyseq_if_unbound (_rl_term_kh, rl_beg_of_line); /* Home */
602 rl_bind_keyseq_if_unbound (_rl_term_at7, rl_end_of_line); /* End */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000603
Jari Aalto95732b42005-12-07 14:08:12 +0000604 rl_bind_keyseq_if_unbound (_rl_term_kD, rl_delete);
605
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000606 _rl_keymap = xkeymap;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000607}
608
609char *
610rl_get_termcap (cap)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000611 const char *cap;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000612{
613 register int i;
614
615 if (tcap_initialized == 0)
616 return ((char *)NULL);
617 for (i = 0; i < NUM_TC_STRINGS; i++)
618 {
619 if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
620 return *(tc_strings[i].tc_value);
621 }
622 return ((char *)NULL);
623}
624
Jari Aaltod166f041997-06-05 14:59:13 +0000625/* Re-initialize the terminal considering that the TERM/TERMCAP variable
626 has changed. */
627int
628rl_reset_terminal (terminal_name)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000629 const char *terminal_name;
Jari Aaltod166f041997-06-05 14:59:13 +0000630{
Jari Aalto95732b42005-12-07 14:08:12 +0000631 _rl_screenwidth = _rl_screenheight = 0;
Jari Aaltod166f041997-06-05 14:59:13 +0000632 _rl_init_terminal_io (terminal_name);
633 return 0;
634}
635
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000636/* A function for the use of tputs () */
Jari Aaltocce855b1998-04-17 19:52:44 +0000637#ifdef _MINIX
638void
639_rl_output_character_function (c)
640 int c;
641{
642 putc (c, _rl_out_stream);
643}
644#else /* !_MINIX */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000645int
646_rl_output_character_function (c)
647 int c;
648{
649 return putc (c, _rl_out_stream);
650}
Jari Aaltocce855b1998-04-17 19:52:44 +0000651#endif /* !_MINIX */
Jari Aaltobb706242000-03-17 21:46:59 +0000652
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000653/* Write COUNT characters from STRING to the output stream. */
654void
655_rl_output_some_chars (string, count)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000656 const char *string;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000657 int count;
658{
659 fwrite (string, 1, count, _rl_out_stream);
660}
661
662/* Move the cursor back. */
663int
664_rl_backspace (count)
665 int count;
666{
667 register int i;
668
Chet Rameyac50fba2014-02-26 09:36:43 -0500669#ifndef __MSDOS__
Jari Aalto28ef6c32001-04-06 19:14:31 +0000670 if (_rl_term_backspace)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000671 for (i = 0; i < count; i++)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000672 tputs (_rl_term_backspace, 1, _rl_output_character_function);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000673 else
Chet Rameyac50fba2014-02-26 09:36:43 -0500674#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000675 for (i = 0; i < count; i++)
676 putc ('\b', _rl_out_stream);
677 return 0;
678}
679
680/* Move to the start of the next line. */
681int
Jari Aalto28ef6c32001-04-06 19:14:31 +0000682rl_crlf ()
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000683{
Jari Aalto31859422009-01-12 13:36:28 +0000684#if defined (NEW_TTY_DRIVER) || defined (__MINT__)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000685 if (_rl_term_cr)
686 tputs (_rl_term_cr, 1, _rl_output_character_function);
Jari Aalto31859422009-01-12 13:36:28 +0000687#endif /* NEW_TTY_DRIVER || __MINT__ */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000688 putc ('\n', _rl_out_stream);
689 return 0;
690}
691
692/* Ring the terminal bell. */
693int
Jari Aalto28ef6c32001-04-06 19:14:31 +0000694rl_ding ()
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000695{
Jari Aalto31859422009-01-12 13:36:28 +0000696 if (_rl_echoing_p)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000697 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000698 switch (_rl_bell_preference)
699 {
700 case NO_BELL:
701 default:
702 break;
703 case VISIBLE_BELL:
Jari Aalto28ef6c32001-04-06 19:14:31 +0000704 if (_rl_visible_bell)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000705 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500706#ifdef __DJGPP__
707 ScreenVisualBell ();
708#else
Jari Aalto28ef6c32001-04-06 19:14:31 +0000709 tputs (_rl_visible_bell, 1, _rl_output_character_function);
Chet Rameyac50fba2014-02-26 09:36:43 -0500710#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000711 break;
712 }
713 /* FALLTHROUGH */
714 case AUDIBLE_BELL:
715 fprintf (stderr, "\007");
716 fflush (stderr);
717 break;
718 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000719 return (0);
720 }
721 return (-1);
722}
723
724/* **************************************************************** */
725/* */
726/* Controlling the Meta Key and Keypad */
727/* */
728/* **************************************************************** */
729
Chet Rameyac50fba2014-02-26 09:36:43 -0500730static int enabled_meta = 0; /* flag indicating we enabled meta mode */
731
Jari Aaltod166f041997-06-05 14:59:13 +0000732void
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000733_rl_enable_meta_key ()
734{
Jari Aaltobb706242000-03-17 21:46:59 +0000735#if !defined (__DJGPP__)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000736 if (term_has_meta && _rl_term_mm)
Chet Rameyac50fba2014-02-26 09:36:43 -0500737 {
738 tputs (_rl_term_mm, 1, _rl_output_character_function);
739 enabled_meta = 1;
740 }
741#endif
742}
743
744void
745_rl_disable_meta_key ()
746{
747#if !defined (__DJGPP__)
748 if (term_has_meta && _rl_term_mo && enabled_meta)
749 {
750 tputs (_rl_term_mo, 1, _rl_output_character_function);
751 enabled_meta = 0;
752 }
Jari Aaltobb706242000-03-17 21:46:59 +0000753#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000754}
755
756void
757_rl_control_keypad (on)
758 int on;
759{
Jari Aaltobb706242000-03-17 21:46:59 +0000760#if !defined (__DJGPP__)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000761 if (on && _rl_term_ks)
762 tputs (_rl_term_ks, 1, _rl_output_character_function);
763 else if (!on && _rl_term_ke)
764 tputs (_rl_term_ke, 1, _rl_output_character_function);
Jari Aaltobb706242000-03-17 21:46:59 +0000765#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000766}
Jari Aalto7117c2d2002-07-17 14:10:11 +0000767
768/* **************************************************************** */
769/* */
770/* Controlling the Cursor */
771/* */
772/* **************************************************************** */
773
774/* Set the cursor appropriately depending on IM, which is one of the
775 insert modes (insert or overwrite). Insert mode gets the normal
776 cursor. Overwrite mode gets a very visible cursor. Only does
777 anything if we have both capabilities. */
778void
779_rl_set_cursor (im, force)
780 int im, force;
781{
Chet Rameyac50fba2014-02-26 09:36:43 -0500782#ifndef __MSDOS__
Jari Aalto7117c2d2002-07-17 14:10:11 +0000783 if (_rl_term_ve && _rl_term_vs)
784 {
785 if (force || im != rl_insert_mode)
786 {
787 if (im == RL_IM_OVERWRITE)
788 tputs (_rl_term_vs, 1, _rl_output_character_function);
789 else
790 tputs (_rl_term_ve, 1, _rl_output_character_function);
791 }
792 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500793#endif
Jari Aalto7117c2d2002-07-17 14:10:11 +0000794}