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