]>
Commit | Line | Data |
---|---|---|
d60d9f65 SS |
1 | /* terminal.c -- controlling the terminal with termcap. */ |
2 | ||
775e241e | 3 | /* Copyright (C) 1996-2015 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; | |
115 | char *_rl_term_cr; | |
116 | char *_rl_term_backspace; | |
117 | char *_rl_term_goto; | |
118 | char *_rl_term_pc; | |
d60d9f65 SS |
119 | |
120 | /* Non-zero if we determine that the terminal can do character insertion. */ | |
9255ee31 | 121 | int _rl_terminal_can_insert = 0; |
d60d9f65 SS |
122 | |
123 | /* How to insert characters. */ | |
9255ee31 EZ |
124 | char *_rl_term_im; |
125 | char *_rl_term_ei; | |
126 | char *_rl_term_ic; | |
127 | char *_rl_term_ip; | |
128 | char *_rl_term_IC; | |
d60d9f65 SS |
129 | |
130 | /* How to delete characters. */ | |
9255ee31 EZ |
131 | char *_rl_term_dc; |
132 | char *_rl_term_DC; | |
d60d9f65 | 133 | |
9255ee31 | 134 | char *_rl_term_forward_char; |
d60d9f65 SS |
135 | |
136 | /* How to go up a line. */ | |
9255ee31 | 137 | char *_rl_term_up; |
d60d9f65 | 138 | |
9255ee31 EZ |
139 | /* A visible bell; char if the terminal can be made to flash the screen. */ |
140 | static char *_rl_visible_bell; | |
d60d9f65 SS |
141 | |
142 | /* Non-zero means the terminal can auto-wrap lines. */ | |
5bdf8622 | 143 | int _rl_term_autowrap = -1; |
d60d9f65 SS |
144 | |
145 | /* Non-zero means that this terminal has a meta key. */ | |
146 | static int term_has_meta; | |
147 | ||
148 | /* The sequences to write to turn on and off the meta key, if this | |
9255ee31 EZ |
149 | terminal has one. */ |
150 | static char *_rl_term_mm; | |
151 | static char *_rl_term_mo; | |
d60d9f65 SS |
152 | |
153 | /* The key sequences output by the arrow keys, if this terminal has any. */ | |
9255ee31 EZ |
154 | static char *_rl_term_ku; |
155 | static char *_rl_term_kd; | |
156 | static char *_rl_term_kr; | |
157 | static char *_rl_term_kl; | |
d60d9f65 SS |
158 | |
159 | /* How to initialize and reset the arrow keys, if this terminal has any. */ | |
9255ee31 EZ |
160 | static char *_rl_term_ks; |
161 | static char *_rl_term_ke; | |
d60d9f65 SS |
162 | |
163 | /* The key sequences sent by the Home and End keys, if any. */ | |
9255ee31 EZ |
164 | static char *_rl_term_kh; |
165 | static char *_rl_term_kH; | |
166 | static char *_rl_term_at7; /* @7 */ | |
167 | ||
5bdf8622 DJ |
168 | /* Delete key */ |
169 | static char *_rl_term_kD; | |
170 | ||
9255ee31 EZ |
171 | /* Insert key */ |
172 | static char *_rl_term_kI; | |
173 | ||
174 | /* Cursor control */ | |
175 | static char *_rl_term_vs; /* very visible */ | |
176 | static char *_rl_term_ve; /* normal */ | |
177 | ||
178 | static void bind_termcap_arrow_keys PARAMS((Keymap)); | |
d60d9f65 SS |
179 | |
180 | /* Variables that hold the screen dimensions, used by the display code. */ | |
9255ee31 | 181 | int _rl_screenwidth, _rl_screenheight, _rl_screenchars; |
d60d9f65 SS |
182 | |
183 | /* Non-zero means the user wants to enable the keypad. */ | |
184 | int _rl_enable_keypad; | |
185 | ||
186 | /* Non-zero means the user wants to enable a meta key. */ | |
187 | int _rl_enable_meta = 1; | |
188 | ||
1b17e766 EZ |
189 | #if defined (__EMX__) |
190 | static 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 | ||
cc88a640 JK |
205 | #if defined (__MINGW32__) |
206 | static 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 | ||
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 | |
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 */ | |
5bdf8622 | 237 | int wr, wc; |
d60d9f65 | 238 | |
5bdf8622 | 239 | wr = wc = -1; |
d60d9f65 SS |
240 | #if defined (TIOCGWINSZ) |
241 | if (ioctl (tty, TIOCGWINSZ, &window_size) == 0) | |
242 | { | |
5bdf8622 DJ |
243 | wc = (int) window_size.ws_col; |
244 | wr = (int) window_size.ws_row; | |
d60d9f65 SS |
245 | } |
246 | #endif /* TIOCGWINSZ */ | |
247 | ||
248 | #if defined (__EMX__) | |
cc88a640 JK |
249 | _emx_get_screensize (&wc, &wr); |
250 | #elif defined (__MINGW32__) | |
251 | _win_get_screensize (&wc, &wr); | |
d60d9f65 SS |
252 | #endif |
253 | ||
5bdf8622 DJ |
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 | ||
d60d9f65 | 262 | /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV |
5bdf8622 DJ |
263 | is unset. If we prefer the environment, check it first before |
264 | assigning the value returned by the kernel. */ | |
9255ee31 | 265 | if (_rl_screenwidth <= 0) |
d60d9f65 | 266 | { |
9255ee31 EZ |
267 | if (ignore_env == 0 && (ss = sh_get_env_value ("COLUMNS"))) |
268 | _rl_screenwidth = atoi (ss); | |
d60d9f65 | 269 | |
5bdf8622 DJ |
270 | if (_rl_screenwidth <= 0) |
271 | _rl_screenwidth = wc; | |
272 | ||
b0f0a30e EZ |
273 | #if defined (__DJGPP__) |
274 | if (_rl_screenwidth <= 0) | |
275 | _rl_screenwidth = ScreenCols (); | |
276 | #else | |
9255ee31 EZ |
277 | if (_rl_screenwidth <= 0 && term_string_buffer) |
278 | _rl_screenwidth = tgetnum ("co"); | |
771578d1 | 279 | #endif |
d60d9f65 SS |
280 | } |
281 | ||
282 | /* Environment variable LINES overrides setting of "li" if IGNORE_ENV | |
283 | is unset. */ | |
9255ee31 | 284 | if (_rl_screenheight <= 0) |
d60d9f65 | 285 | { |
9255ee31 EZ |
286 | if (ignore_env == 0 && (ss = sh_get_env_value ("LINES"))) |
287 | _rl_screenheight = atoi (ss); | |
d60d9f65 | 288 | |
5bdf8622 DJ |
289 | if (_rl_screenheight <= 0) |
290 | _rl_screenheight = wr; | |
291 | ||
b0f0a30e EZ |
292 | #if defined (__DJGPP__) |
293 | if (_rl_screenheight <= 0) | |
294 | _rl_screenheight = ScreenRows (); | |
295 | #else | |
9255ee31 EZ |
296 | if (_rl_screenheight <= 0 && term_string_buffer) |
297 | _rl_screenheight = tgetnum ("li"); | |
771578d1 | 298 | #endif |
d60d9f65 SS |
299 | } |
300 | ||
301 | /* If all else fails, default to 80x24 terminal. */ | |
9255ee31 EZ |
302 | if (_rl_screenwidth <= 1) |
303 | _rl_screenwidth = 80; | |
d60d9f65 | 304 | |
9255ee31 EZ |
305 | if (_rl_screenheight <= 0) |
306 | _rl_screenheight = 24; | |
d60d9f65 SS |
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. */ | |
775e241e TT |
311 | if (rl_change_environment) |
312 | sh_set_lines_and_columns (_rl_screenheight, _rl_screenwidth); | |
d60d9f65 | 313 | |
1b17e766 | 314 | if (_rl_term_autowrap == 0) |
9255ee31 | 315 | _rl_screenwidth--; |
d60d9f65 | 316 | |
9255ee31 | 317 | _rl_screenchars = _rl_screenwidth * _rl_screenheight; |
d60d9f65 SS |
318 | } |
319 | ||
320 | void | |
321 | _rl_set_screen_size (rows, cols) | |
322 | int rows, cols; | |
323 | { | |
5bdf8622 DJ |
324 | if (_rl_term_autowrap == -1) |
325 | _rl_init_terminal_io (rl_terminal_name); | |
9255ee31 | 326 | |
5bdf8622 DJ |
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 | } | |
d60d9f65 | 335 | |
5bdf8622 DJ |
336 | if (rows > 0 || cols > 0) |
337 | _rl_screenchars = _rl_screenwidth * _rl_screenheight; | |
d60d9f65 SS |
338 | } |
339 | ||
9255ee31 EZ |
340 | void |
341 | rl_set_screen_size (rows, cols) | |
342 | int rows, cols; | |
343 | { | |
344 | _rl_set_screen_size (rows, cols); | |
345 | } | |
346 | ||
347 | void | |
348 | rl_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 | } | |
5bdf8622 DJ |
356 | |
357 | void | |
358 | rl_reset_screen_size () | |
359 | { | |
360 | _rl_get_screen_size (fileno (rl_instream), 0); | |
361 | } | |
775e241e TT |
362 | |
363 | void | |
364 | _rl_sigwinch_resize_terminal () | |
365 | { | |
366 | _rl_get_screen_size (fileno (rl_instream), 1); | |
367 | } | |
368 | ||
c862e87b JM |
369 | void |
370 | rl_resize_terminal () | |
371 | { | |
cc88a640 JK |
372 | _rl_get_screen_size (fileno (rl_instream), 1); |
373 | if (_rl_echoing_p) | |
c862e87b | 374 | { |
9255ee31 EZ |
375 | if (CUSTOM_REDISPLAY_FUNC ()) |
376 | rl_forced_update_display (); | |
cc88a640 | 377 | else if (RL_ISSTATE(RL_STATE_REDISPLAYING) == 0) |
9255ee31 | 378 | _rl_redisplay_after_sigwinch (); |
c862e87b JM |
379 | } |
380 | } | |
381 | ||
d60d9f65 | 382 | struct _tc_string { |
cc88a640 | 383 | const char * const tc_var; |
d60d9f65 SS |
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. */ | |
cc88a640 | 389 | static const struct _tc_string tc_strings[] = |
d60d9f65 | 390 | { |
9255ee31 EZ |
391 | { "@7", &_rl_term_at7 }, |
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 }, | |
5bdf8622 | 401 | { "kD", &_rl_term_kD }, /* delete */ |
9255ee31 EZ |
402 | { "kH", &_rl_term_kH }, /* home down ?? */ |
403 | { "kI", &_rl_term_kI }, /* insert */ | |
404 | { "kd", &_rl_term_kd }, | |
405 | { "ke", &_rl_term_ke }, /* end keypad mode */ | |
406 | { "kh", &_rl_term_kh }, /* home */ | |
407 | { "kl", &_rl_term_kl }, | |
408 | { "kr", &_rl_term_kr }, | |
409 | { "ks", &_rl_term_ks }, /* start keypad mode */ | |
410 | { "ku", &_rl_term_ku }, | |
411 | { "le", &_rl_term_backspace }, | |
412 | { "mm", &_rl_term_mm }, | |
413 | { "mo", &_rl_term_mo }, | |
9255ee31 | 414 | { "nd", &_rl_term_forward_char }, |
9255ee31 EZ |
415 | { "pc", &_rl_term_pc }, |
416 | { "up", &_rl_term_up }, | |
417 | { "vb", &_rl_visible_bell }, | |
418 | { "vs", &_rl_term_vs }, | |
419 | { "ve", &_rl_term_ve }, | |
d60d9f65 SS |
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. */ | |
426 | static void | |
427 | get_term_capabilities (bp) | |
428 | char **bp; | |
429 | { | |
1b17e766 | 430 | #if !defined (__DJGPP__) /* XXX - doesn't DJGPP have a termcap library? */ |
d60d9f65 SS |
431 | register int i; |
432 | ||
433 | for (i = 0; i < NUM_TC_STRINGS; i++) | |
9255ee31 | 434 | *(tc_strings[i].tc_value) = tgetstr ((char *)tc_strings[i].tc_var, bp); |
771578d1 | 435 | #endif |
d60d9f65 SS |
436 | tcap_initialized = 1; |
437 | } | |
438 | ||
439 | int | |
440 | _rl_init_terminal_io (terminal_name) | |
9255ee31 | 441 | const char *terminal_name; |
d60d9f65 | 442 | { |
9255ee31 EZ |
443 | const char *term; |
444 | char *buffer; | |
1b17e766 | 445 | int tty, tgetent_ret; |
d60d9f65 | 446 | |
9255ee31 EZ |
447 | term = terminal_name ? terminal_name : sh_get_env_value ("TERM"); |
448 | _rl_term_clrpag = _rl_term_cr = _rl_term_clreol = (char *)NULL; | |
1b17e766 | 449 | tty = rl_instream ? fileno (rl_instream) : 0; |
d60d9f65 | 450 | |
1b17e766 EZ |
451 | if (term == 0) |
452 | term = "dumb"; | |
d60d9f65 | 453 | |
30083a32 | 454 | #ifdef __MSDOS__ |
b0f0a30e EZ |
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 | |
30083a32 EZ |
468 | |
469 | _rl_get_screen_size (tty, 0); | |
470 | #else /* !__MSDOS__ */ | |
1b17e766 EZ |
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) | |
9255ee31 | 481 | term_string_buffer = (char *)xmalloc(2032); |
d60d9f65 | 482 | |
1b17e766 | 483 | if (term_buffer == 0) |
9255ee31 | 484 | term_buffer = (char *)xmalloc(4080); |
d60d9f65 | 485 | |
1b17e766 | 486 | buffer = term_string_buffer; |
d60d9f65 | 487 | |
1b17e766 EZ |
488 | tgetent_ret = tgetent (term_buffer, term); |
489 | } | |
d60d9f65 | 490 | |
1b17e766 | 491 | if (tgetent_ret <= 0) |
d60d9f65 | 492 | { |
1b17e766 EZ |
493 | FREE (term_string_buffer); |
494 | FREE (term_buffer); | |
495 | buffer = term_buffer = term_string_buffer = (char *)NULL; | |
496 | ||
1b17e766 EZ |
497 | _rl_term_autowrap = 0; /* used by _rl_get_screen_size */ |
498 | ||
5bdf8622 DJ |
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 | { | |
1b17e766 | 503 | #if defined (__EMX__) |
5bdf8622 DJ |
504 | _emx_get_screensize (&_rl_screenwidth, &_rl_screenheight); |
505 | _rl_screenwidth--; | |
1b17e766 | 506 | #else /* !__EMX__ */ |
5bdf8622 | 507 | _rl_get_screen_size (tty, 0); |
1b17e766 | 508 | #endif /* !__EMX__ */ |
5bdf8622 | 509 | } |
1b17e766 EZ |
510 | |
511 | /* Defaults. */ | |
9255ee31 | 512 | if (_rl_screenwidth <= 0 || _rl_screenheight <= 0) |
1b17e766 | 513 | { |
9255ee31 EZ |
514 | _rl_screenwidth = 79; |
515 | _rl_screenheight = 24; | |
1b17e766 EZ |
516 | } |
517 | ||
518 | /* Everything below here is used by the redisplay code (tputs). */ | |
9255ee31 EZ |
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; | |
5bdf8622 | 524 | _rl_term_kh = _rl_term_kH = _rl_term_kI = _rl_term_kD = (char *)NULL; |
9255ee31 EZ |
525 | _rl_term_ks = _rl_term_ke = _rl_term_at7 = (char *)NULL; |
526 | _rl_term_mm = _rl_term_mo = (char *)NULL; | |
527 | _rl_term_ve = _rl_term_vs = (char *)NULL; | |
cc88a640 | 528 | _rl_term_forward_char = (char *)NULL; |
9255ee31 | 529 | _rl_terminal_can_insert = term_has_meta = 0; |
1b17e766 EZ |
530 | |
531 | /* Reasonable defaults for tgoto(). Readline currently only uses | |
9255ee31 | 532 | tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we |
1b17e766 EZ |
533 | change that later... */ |
534 | PC = '\0'; | |
9255ee31 EZ |
535 | BC = _rl_term_backspace = "\b"; |
536 | UP = _rl_term_up; | |
1b17e766 | 537 | |
d60d9f65 SS |
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. */ | |
9255ee31 EZ |
545 | PC = _rl_term_pc ? *_rl_term_pc : 0; |
546 | BC = _rl_term_backspace; | |
547 | UP = _rl_term_up; | |
d60d9f65 | 548 | |
9255ee31 EZ |
549 | if (!_rl_term_cr) |
550 | _rl_term_cr = "\r"; | |
d60d9f65 | 551 | |
d60d9f65 SS |
552 | _rl_term_autowrap = tgetflag ("am") && tgetflag ("xn"); |
553 | ||
5bdf8622 DJ |
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); | |
d60d9f65 SS |
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... */ | |
9255ee31 | 563 | _rl_terminal_can_insert = (_rl_term_IC || _rl_term_im || _rl_term_ic); |
d60d9f65 SS |
564 | |
565 | /* Check to see if this terminal has a meta key and clear the capability | |
566 | variables if there is none. */ | |
cc88a640 JK |
567 | term_has_meta = tgetflag ("km") != 0; |
568 | if (term_has_meta == 0) | |
9255ee31 | 569 | _rl_term_mm = _rl_term_mo = (char *)NULL; |
30083a32 EZ |
570 | #endif /* !__MSDOS__ */ |
571 | ||
d60d9f65 SS |
572 | /* Attempt to find and bind the arrow keys. Do not override already |
573 | bound keys in an overzealous attempt, however. */ | |
d60d9f65 | 574 | |
9255ee31 | 575 | bind_termcap_arrow_keys (emacs_standard_keymap); |
d60d9f65 SS |
576 | |
577 | #if defined (VI_MODE) | |
9255ee31 EZ |
578 | bind_termcap_arrow_keys (vi_movement_keymap); |
579 | bind_termcap_arrow_keys (vi_insertion_keymap); | |
d60d9f65 SS |
580 | #endif /* VI_MODE */ |
581 | ||
d60d9f65 SS |
582 | return 0; |
583 | } | |
584 | ||
9255ee31 EZ |
585 | /* Bind the arrow key sequences from the termcap description in MAP. */ |
586 | static void | |
587 | bind_termcap_arrow_keys (map) | |
588 | Keymap map; | |
589 | { | |
590 | Keymap xkeymap; | |
591 | ||
592 | xkeymap = _rl_keymap; | |
593 | _rl_keymap = map; | |
594 | ||
5bdf8622 DJ |
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); | |
599 | ||
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 */ | |
9255ee31 | 602 | |
5bdf8622 | 603 | rl_bind_keyseq_if_unbound (_rl_term_kD, rl_delete); |
9255ee31 EZ |
604 | |
605 | _rl_keymap = xkeymap; | |
606 | } | |
607 | ||
d60d9f65 SS |
608 | char * |
609 | rl_get_termcap (cap) | |
9255ee31 | 610 | const char *cap; |
d60d9f65 SS |
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 | ||
624 | /* Re-initialize the terminal considering that the TERM/TERMCAP variable | |
625 | has changed. */ | |
626 | int | |
627 | rl_reset_terminal (terminal_name) | |
9255ee31 | 628 | const char *terminal_name; |
d60d9f65 | 629 | { |
5bdf8622 | 630 | _rl_screenwidth = _rl_screenheight = 0; |
d60d9f65 SS |
631 | _rl_init_terminal_io (terminal_name); |
632 | return 0; | |
633 | } | |
634 | ||
635 | /* A function for the use of tputs () */ | |
636 | #ifdef _MINIX | |
637 | void | |
638 | _rl_output_character_function (c) | |
639 | int c; | |
640 | { | |
641 | putc (c, _rl_out_stream); | |
642 | } | |
643 | #else /* !_MINIX */ | |
644 | int | |
645 | _rl_output_character_function (c) | |
646 | int c; | |
647 | { | |
648 | return putc (c, _rl_out_stream); | |
649 | } | |
650 | #endif /* !_MINIX */ | |
1b17e766 | 651 | |
d60d9f65 SS |
652 | /* Write COUNT characters from STRING to the output stream. */ |
653 | void | |
654 | _rl_output_some_chars (string, count) | |
9255ee31 | 655 | const char *string; |
d60d9f65 SS |
656 | int count; |
657 | { | |
658 | fwrite (string, 1, count, _rl_out_stream); | |
659 | } | |
660 | ||
661 | /* Move the cursor back. */ | |
662 | int | |
663 | _rl_backspace (count) | |
664 | int count; | |
665 | { | |
666 | register int i; | |
667 | ||
30083a32 | 668 | #ifndef __MSDOS__ |
9255ee31 | 669 | if (_rl_term_backspace) |
d60d9f65 | 670 | for (i = 0; i < count; i++) |
9255ee31 | 671 | tputs (_rl_term_backspace, 1, _rl_output_character_function); |
d60d9f65 | 672 | else |
30083a32 | 673 | #endif |
d60d9f65 SS |
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. */ | |
680 | int | |
9255ee31 | 681 | rl_crlf () |
d60d9f65 | 682 | { |
cc88a640 | 683 | #if defined (NEW_TTY_DRIVER) || defined (__MINT__) |
9255ee31 EZ |
684 | if (_rl_term_cr) |
685 | tputs (_rl_term_cr, 1, _rl_output_character_function); | |
cc88a640 | 686 | #endif /* NEW_TTY_DRIVER || __MINT__ */ |
d60d9f65 SS |
687 | putc ('\n', _rl_out_stream); |
688 | return 0; | |
689 | } | |
690 | ||
691 | /* Ring the terminal bell. */ | |
692 | int | |
9255ee31 | 693 | rl_ding () |
d60d9f65 | 694 | { |
cc88a640 | 695 | if (_rl_echoing_p) |
d60d9f65 | 696 | { |
d60d9f65 SS |
697 | switch (_rl_bell_preference) |
698 | { | |
699 | case NO_BELL: | |
700 | default: | |
701 | break; | |
702 | case VISIBLE_BELL: | |
9255ee31 | 703 | if (_rl_visible_bell) |
d60d9f65 | 704 | { |
775e241e TT |
705 | #ifdef __DJGPP__ |
706 | ScreenVisualBell (); | |
707 | #else | |
9255ee31 | 708 | tputs (_rl_visible_bell, 1, _rl_output_character_function); |
775e241e | 709 | #endif |
d60d9f65 SS |
710 | break; |
711 | } | |
712 | /* FALLTHROUGH */ | |
713 | case AUDIBLE_BELL: | |
714 | fprintf (stderr, "\007"); | |
715 | fflush (stderr); | |
716 | break; | |
717 | } | |
d60d9f65 SS |
718 | return (0); |
719 | } | |
720 | return (-1); | |
721 | } | |
722 | ||
723 | /* **************************************************************** */ | |
724 | /* */ | |
725 | /* Controlling the Meta Key and Keypad */ | |
726 | /* */ | |
727 | /* **************************************************************** */ | |
728 | ||
775e241e TT |
729 | static int enabled_meta = 0; /* flag indicating we enabled meta mode */ |
730 | ||
d60d9f65 SS |
731 | void |
732 | _rl_enable_meta_key () | |
733 | { | |
1b17e766 | 734 | #if !defined (__DJGPP__) |
9255ee31 | 735 | if (term_has_meta && _rl_term_mm) |
775e241e TT |
736 | { |
737 | tputs (_rl_term_mm, 1, _rl_output_character_function); | |
738 | enabled_meta = 1; | |
739 | } | |
740 | #endif | |
741 | } | |
742 | ||
743 | void | |
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 | } | |
771578d1 | 752 | #endif |
d60d9f65 SS |
753 | } |
754 | ||
755 | void | |
756 | _rl_control_keypad (on) | |
757 | int on; | |
758 | { | |
1b17e766 | 759 | #if !defined (__DJGPP__) |
9255ee31 EZ |
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); | |
771578d1 | 764 | #endif |
d60d9f65 | 765 | } |
9255ee31 EZ |
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. */ | |
777 | void | |
778 | _rl_set_cursor (im, force) | |
779 | int im, force; | |
780 | { | |
b0f0a30e | 781 | #ifndef __MSDOS__ |
9255ee31 EZ |
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 | } | |
b0f0a30e | 792 | #endif |
9255ee31 | 793 | } |