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