Commit | Line | Data |
---|---|---|
d60d9f65 SS |
1 | /* rltty.c -- functions to prepare and restore the terminal for readline's |
2 | use. */ | |
3 | ||
5bdf8622 | 4 | /* Copyright (C) 1992-2005 Free Software Foundation, Inc. |
d60d9f65 | 5 | |
cc88a640 | 6 | This file is part of the GNU Readline Library (Readline), a library |
4a11f206 | 7 | for reading lines of text with interactive input and history editing. |
d60d9f65 | 8 | |
cc88a640 JK |
9 | Readline is free software: you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation, either version 3 of the License, or | |
d60d9f65 SS |
12 | (at your option) any later version. |
13 | ||
cc88a640 JK |
14 | Readline is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
d60d9f65 SS |
17 | GNU General Public License for more details. |
18 | ||
cc88a640 JK |
19 | You should have received a copy of the GNU General Public License |
20 | along with Readline. If not, see <http://www.gnu.org/licenses/>. | |
21 | */ | |
22 | ||
d60d9f65 SS |
23 | #define READLINE_LIBRARY |
24 | ||
25 | #if defined (HAVE_CONFIG_H) | |
26 | # include <config.h> | |
27 | #endif | |
28 | ||
29 | #include <sys/types.h> | |
30 | #include <signal.h> | |
31 | #include <errno.h> | |
32 | #include <stdio.h> | |
33 | ||
34 | #if defined (HAVE_UNISTD_H) | |
35 | # include <unistd.h> | |
36 | #endif /* HAVE_UNISTD_H */ | |
37 | ||
38 | #include "rldefs.h" | |
39 | ||
d60d9f65 | 40 | #include "rltty.h" |
4a11f206 PP |
41 | #if defined (HAVE_SYS_IOCTL_H) |
42 | # include <sys/ioctl.h> /* include for declaration of ioctl */ | |
43 | #endif | |
44 | ||
d60d9f65 | 45 | #include "readline.h" |
1b17e766 | 46 | #include "rlprivate.h" |
d60d9f65 SS |
47 | |
48 | #if !defined (errno) | |
49 | extern int errno; | |
50 | #endif /* !errno */ | |
51 | ||
9255ee31 EZ |
52 | rl_vintfunc_t *rl_prep_term_function = rl_prep_terminal; |
53 | rl_voidfunc_t *rl_deprep_term_function = rl_deprep_terminal; | |
54 | ||
9255ee31 | 55 | static void set_winsize PARAMS((int)); |
d60d9f65 | 56 | |
d60d9f65 SS |
57 | /* **************************************************************** */ |
58 | /* */ | |
59 | /* Saving and Restoring the TTY */ | |
60 | /* */ | |
61 | /* **************************************************************** */ | |
62 | ||
4a11f206 PP |
63 | /* Non-zero means that the terminal is in a prepped state. There are several |
64 | flags that are OR'd in to denote whether or not we have sent various | |
65 | init strings to the terminal. */ | |
66 | #define TPX_PREPPED 0x01 | |
67 | #define TPX_BRACKPASTE 0x02 | |
68 | #define TPX_METAKEY 0x04 | |
69 | ||
d60d9f65 SS |
70 | static int terminal_prepped; |
71 | ||
1b17e766 EZ |
72 | static _RL_TTY_CHARS _rl_tty_chars, _rl_last_tty_chars; |
73 | ||
d60d9f65 SS |
74 | /* If non-zero, means that this process has called tcflow(fd, TCOOFF) |
75 | and output is suspended. */ | |
76 | #if defined (__ksr1__) | |
77 | static int ksrflow; | |
78 | #endif | |
79 | ||
d60d9f65 SS |
80 | /* Dummy call to force a backgrounded readline to stop before it tries |
81 | to get the tty settings. */ | |
82 | static void | |
83 | set_winsize (tty) | |
84 | int tty; | |
85 | { | |
1b17e766 | 86 | #if defined (TIOCGWINSZ) |
d60d9f65 SS |
87 | struct winsize w; |
88 | ||
89 | if (ioctl (tty, TIOCGWINSZ, &w) == 0) | |
90 | (void) ioctl (tty, TIOCSWINSZ, &w); | |
c862e87b | 91 | #endif /* TIOCGWINSZ */ |
1b17e766 | 92 | } |
d60d9f65 | 93 | |
fd8be987 MM |
94 | #if defined (NO_TTY_DRIVER) |
95 | /* Nothing */ | |
96 | #elif defined (NEW_TTY_DRIVER) | |
d60d9f65 SS |
97 | |
98 | /* Values for the `flags' field of a struct bsdtty. This tells which | |
99 | elements of the struct bsdtty have been fetched from the system and | |
100 | are valid. */ | |
101 | #define SGTTY_SET 0x01 | |
102 | #define LFLAG_SET 0x02 | |
103 | #define TCHARS_SET 0x04 | |
104 | #define LTCHARS_SET 0x08 | |
105 | ||
106 | struct bsdtty { | |
107 | struct sgttyb sgttyb; /* Basic BSD tty driver information. */ | |
108 | int lflag; /* Local mode flags, like LPASS8. */ | |
109 | #if defined (TIOCGETC) | |
110 | struct tchars tchars; /* Terminal special characters, including ^S and ^Q. */ | |
111 | #endif | |
112 | #if defined (TIOCGLTC) | |
113 | struct ltchars ltchars; /* 4.2 BSD editing characters */ | |
114 | #endif | |
115 | int flags; /* Bitmap saying which parts of the struct are valid. */ | |
116 | }; | |
117 | ||
118 | #define TIOTYPE struct bsdtty | |
119 | ||
120 | static TIOTYPE otio; | |
121 | ||
9255ee31 EZ |
122 | static void save_tty_chars PARAMS((TIOTYPE *)); |
123 | static int _get_tty_settings PARAMS((int, TIOTYPE *)); | |
124 | static int get_tty_settings PARAMS((int, TIOTYPE *)); | |
125 | static int _set_tty_settings PARAMS((int, TIOTYPE *)); | |
126 | static int set_tty_settings PARAMS((int, TIOTYPE *)); | |
127 | ||
128 | static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *)); | |
129 | ||
4a11f206 | 130 | static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t *)); |
5bdf8622 | 131 | |
1b17e766 EZ |
132 | static void |
133 | save_tty_chars (tiop) | |
134 | TIOTYPE *tiop; | |
135 | { | |
136 | _rl_last_tty_chars = _rl_tty_chars; | |
137 | ||
138 | if (tiop->flags & SGTTY_SET) | |
139 | { | |
140 | _rl_tty_chars.t_erase = tiop->sgttyb.sg_erase; | |
141 | _rl_tty_chars.t_kill = tiop->sgttyb.sg_kill; | |
142 | } | |
143 | ||
144 | if (tiop->flags & TCHARS_SET) | |
145 | { | |
cc88a640 JK |
146 | _rl_intr_char = _rl_tty_chars.t_intr = tiop->tchars.t_intrc; |
147 | _rl_quit_char = _rl_tty_chars.t_quit = tiop->tchars.t_quitc; | |
148 | ||
1b17e766 | 149 | _rl_tty_chars.t_start = tiop->tchars.t_startc; |
5844f845 | 150 | _rl_tty_chars.t_stop = tiop->tchars.t_stopc; |
1b17e766 EZ |
151 | _rl_tty_chars.t_eof = tiop->tchars.t_eofc; |
152 | _rl_tty_chars.t_eol = '\n'; | |
153 | _rl_tty_chars.t_eol2 = tiop->tchars.t_brkc; | |
154 | } | |
155 | ||
156 | if (tiop->flags & LTCHARS_SET) | |
157 | { | |
cc88a640 JK |
158 | _rl_susp_char = _rl_tty_chars.t_susp = tiop->ltchars.t_suspc; |
159 | ||
1b17e766 EZ |
160 | _rl_tty_chars.t_dsusp = tiop->ltchars.t_dsuspc; |
161 | _rl_tty_chars.t_reprint = tiop->ltchars.t_rprntc; | |
162 | _rl_tty_chars.t_flush = tiop->ltchars.t_flushc; | |
163 | _rl_tty_chars.t_werase = tiop->ltchars.t_werasc; | |
164 | _rl_tty_chars.t_lnext = tiop->ltchars.t_lnextc; | |
165 | } | |
166 | ||
167 | _rl_tty_chars.t_status = -1; | |
168 | } | |
169 | ||
d60d9f65 SS |
170 | static int |
171 | get_tty_settings (tty, tiop) | |
172 | int tty; | |
173 | TIOTYPE *tiop; | |
174 | { | |
175 | set_winsize (tty); | |
176 | ||
177 | tiop->flags = tiop->lflag = 0; | |
178 | ||
5bdf8622 | 179 | errno = 0; |
9255ee31 EZ |
180 | if (ioctl (tty, TIOCGETP, &(tiop->sgttyb)) < 0) |
181 | return -1; | |
d60d9f65 SS |
182 | tiop->flags |= SGTTY_SET; |
183 | ||
184 | #if defined (TIOCLGET) | |
9255ee31 EZ |
185 | if (ioctl (tty, TIOCLGET, &(tiop->lflag)) == 0) |
186 | tiop->flags |= LFLAG_SET; | |
d60d9f65 SS |
187 | #endif |
188 | ||
189 | #if defined (TIOCGETC) | |
9255ee31 EZ |
190 | if (ioctl (tty, TIOCGETC, &(tiop->tchars)) == 0) |
191 | tiop->flags |= TCHARS_SET; | |
d60d9f65 SS |
192 | #endif |
193 | ||
194 | #if defined (TIOCGLTC) | |
9255ee31 EZ |
195 | if (ioctl (tty, TIOCGLTC, &(tiop->ltchars)) == 0) |
196 | tiop->flags |= LTCHARS_SET; | |
d60d9f65 SS |
197 | #endif |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
202 | static int | |
203 | set_tty_settings (tty, tiop) | |
204 | int tty; | |
205 | TIOTYPE *tiop; | |
206 | { | |
207 | if (tiop->flags & SGTTY_SET) | |
208 | { | |
209 | ioctl (tty, TIOCSETN, &(tiop->sgttyb)); | |
210 | tiop->flags &= ~SGTTY_SET; | |
211 | } | |
cc88a640 | 212 | _rl_echoing_p = 1; |
d60d9f65 SS |
213 | |
214 | #if defined (TIOCLSET) | |
215 | if (tiop->flags & LFLAG_SET) | |
216 | { | |
217 | ioctl (tty, TIOCLSET, &(tiop->lflag)); | |
218 | tiop->flags &= ~LFLAG_SET; | |
219 | } | |
220 | #endif | |
221 | ||
222 | #if defined (TIOCSETC) | |
223 | if (tiop->flags & TCHARS_SET) | |
224 | { | |
225 | ioctl (tty, TIOCSETC, &(tiop->tchars)); | |
226 | tiop->flags &= ~TCHARS_SET; | |
227 | } | |
228 | #endif | |
229 | ||
230 | #if defined (TIOCSLTC) | |
231 | if (tiop->flags & LTCHARS_SET) | |
232 | { | |
233 | ioctl (tty, TIOCSLTC, &(tiop->ltchars)); | |
234 | tiop->flags &= ~LTCHARS_SET; | |
235 | } | |
236 | #endif | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | static void | |
9255ee31 | 242 | prepare_terminal_settings (meta_flag, oldtio, tiop) |
d60d9f65 | 243 | int meta_flag; |
9255ee31 | 244 | TIOTYPE oldtio, *tiop; |
d60d9f65 | 245 | { |
cc88a640 JK |
246 | _rl_echoing_p = (oldtio.sgttyb.sg_flags & ECHO); |
247 | _rl_echoctl = (oldtio.sgttyb.sg_flags & ECHOCTL); | |
d60d9f65 SS |
248 | |
249 | /* Copy the original settings to the structure we're going to use for | |
250 | our settings. */ | |
9255ee31 EZ |
251 | tiop->sgttyb = oldtio.sgttyb; |
252 | tiop->lflag = oldtio.lflag; | |
d60d9f65 | 253 | #if defined (TIOCGETC) |
9255ee31 | 254 | tiop->tchars = oldtio.tchars; |
d60d9f65 SS |
255 | #endif |
256 | #if defined (TIOCGLTC) | |
9255ee31 | 257 | tiop->ltchars = oldtio.ltchars; |
d60d9f65 | 258 | #endif |
9255ee31 | 259 | tiop->flags = oldtio.flags; |
d60d9f65 SS |
260 | |
261 | /* First, the basic settings to put us into character-at-a-time, no-echo | |
262 | input mode. */ | |
263 | tiop->sgttyb.sg_flags &= ~(ECHO | CRMOD); | |
264 | tiop->sgttyb.sg_flags |= CBREAK; | |
265 | ||
266 | /* If this terminal doesn't care how the 8th bit is used, then we can | |
267 | use it for the meta-key. If only one of even or odd parity is | |
268 | specified, then the terminal is using parity, and we cannot. */ | |
269 | #if !defined (ANYP) | |
270 | # define ANYP (EVENP | ODDP) | |
271 | #endif | |
9255ee31 EZ |
272 | if (((oldtio.sgttyb.sg_flags & ANYP) == ANYP) || |
273 | ((oldtio.sgttyb.sg_flags & ANYP) == 0)) | |
d60d9f65 SS |
274 | { |
275 | tiop->sgttyb.sg_flags |= ANYP; | |
276 | ||
277 | /* Hack on local mode flags if we can. */ | |
278 | #if defined (TIOCLGET) | |
279 | # if defined (LPASS8) | |
280 | tiop->lflag |= LPASS8; | |
281 | # endif /* LPASS8 */ | |
282 | #endif /* TIOCLGET */ | |
283 | } | |
284 | ||
285 | #if defined (TIOCGETC) | |
286 | # if defined (USE_XON_XOFF) | |
287 | /* Get rid of terminal output start and stop characters. */ | |
288 | tiop->tchars.t_stopc = -1; /* C-s */ | |
289 | tiop->tchars.t_startc = -1; /* C-q */ | |
290 | ||
291 | /* If there is an XON character, bind it to restart the output. */ | |
9255ee31 EZ |
292 | if (oldtio.tchars.t_startc != -1) |
293 | rl_bind_key (oldtio.tchars.t_startc, rl_restart_output); | |
d60d9f65 SS |
294 | # endif /* USE_XON_XOFF */ |
295 | ||
296 | /* If there is an EOF char, bind _rl_eof_char to it. */ | |
9255ee31 EZ |
297 | if (oldtio.tchars.t_eofc != -1) |
298 | _rl_eof_char = oldtio.tchars.t_eofc; | |
d60d9f65 SS |
299 | |
300 | # if defined (NO_KILL_INTR) | |
301 | /* Get rid of terminal-generated SIGQUIT and SIGINT. */ | |
302 | tiop->tchars.t_quitc = -1; /* C-\ */ | |
303 | tiop->tchars.t_intrc = -1; /* C-c */ | |
304 | # endif /* NO_KILL_INTR */ | |
305 | #endif /* TIOCGETC */ | |
306 | ||
307 | #if defined (TIOCGLTC) | |
308 | /* Make the interrupt keys go away. Just enough to make people happy. */ | |
309 | tiop->ltchars.t_dsuspc = -1; /* C-y */ | |
310 | tiop->ltchars.t_lnextc = -1; /* C-v */ | |
311 | #endif /* TIOCGLTC */ | |
d60d9f65 SS |
312 | } |
313 | ||
314 | #else /* !defined (NEW_TTY_DRIVER) */ | |
315 | ||
316 | #if !defined (VMIN) | |
317 | # define VMIN VEOF | |
318 | #endif | |
319 | ||
320 | #if !defined (VTIME) | |
321 | # define VTIME VEOL | |
322 | #endif | |
323 | ||
324 | #if defined (TERMIOS_TTY_DRIVER) | |
325 | # define TIOTYPE struct termios | |
326 | # define DRAIN_OUTPUT(fd) tcdrain (fd) | |
327 | # define GETATTR(tty, tiop) (tcgetattr (tty, tiop)) | |
328 | # ifdef M_UNIX | |
329 | # define SETATTR(tty, tiop) (tcsetattr (tty, TCSANOW, tiop)) | |
330 | # else | |
331 | # define SETATTR(tty, tiop) (tcsetattr (tty, TCSADRAIN, tiop)) | |
332 | # endif /* !M_UNIX */ | |
333 | #else | |
334 | # define TIOTYPE struct termio | |
335 | # define DRAIN_OUTPUT(fd) | |
336 | # define GETATTR(tty, tiop) (ioctl (tty, TCGETA, tiop)) | |
9255ee31 | 337 | # define SETATTR(tty, tiop) (ioctl (tty, TCSETAW, tiop)) |
d60d9f65 SS |
338 | #endif /* !TERMIOS_TTY_DRIVER */ |
339 | ||
340 | static TIOTYPE otio; | |
341 | ||
9255ee31 EZ |
342 | static void save_tty_chars PARAMS((TIOTYPE *)); |
343 | static int _get_tty_settings PARAMS((int, TIOTYPE *)); | |
344 | static int get_tty_settings PARAMS((int, TIOTYPE *)); | |
345 | static int _set_tty_settings PARAMS((int, TIOTYPE *)); | |
346 | static int set_tty_settings PARAMS((int, TIOTYPE *)); | |
347 | ||
348 | static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *)); | |
349 | ||
4a11f206 | 350 | static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t *)); |
5bdf8622 DJ |
351 | static void _rl_bind_tty_special_chars PARAMS((Keymap, TIOTYPE)); |
352 | ||
d60d9f65 SS |
353 | #if defined (FLUSHO) |
354 | # define OUTPUT_BEING_FLUSHED(tp) (tp->c_lflag & FLUSHO) | |
355 | #else | |
356 | # define OUTPUT_BEING_FLUSHED(tp) 0 | |
357 | #endif | |
358 | ||
1b17e766 EZ |
359 | static void |
360 | save_tty_chars (tiop) | |
361 | TIOTYPE *tiop; | |
362 | { | |
363 | _rl_last_tty_chars = _rl_tty_chars; | |
364 | ||
365 | _rl_tty_chars.t_eof = tiop->c_cc[VEOF]; | |
366 | _rl_tty_chars.t_eol = tiop->c_cc[VEOL]; | |
367 | #ifdef VEOL2 | |
368 | _rl_tty_chars.t_eol2 = tiop->c_cc[VEOL2]; | |
369 | #endif | |
370 | _rl_tty_chars.t_erase = tiop->c_cc[VERASE]; | |
371 | #ifdef VWERASE | |
372 | _rl_tty_chars.t_werase = tiop->c_cc[VWERASE]; | |
373 | #endif | |
374 | _rl_tty_chars.t_kill = tiop->c_cc[VKILL]; | |
375 | #ifdef VREPRINT | |
376 | _rl_tty_chars.t_reprint = tiop->c_cc[VREPRINT]; | |
377 | #endif | |
cc88a640 JK |
378 | _rl_intr_char = _rl_tty_chars.t_intr = tiop->c_cc[VINTR]; |
379 | _rl_quit_char = _rl_tty_chars.t_quit = tiop->c_cc[VQUIT]; | |
1b17e766 | 380 | #ifdef VSUSP |
cc88a640 | 381 | _rl_susp_char = _rl_tty_chars.t_susp = tiop->c_cc[VSUSP]; |
1b17e766 EZ |
382 | #endif |
383 | #ifdef VDSUSP | |
384 | _rl_tty_chars.t_dsusp = tiop->c_cc[VDSUSP]; | |
385 | #endif | |
386 | #ifdef VSTART | |
387 | _rl_tty_chars.t_start = tiop->c_cc[VSTART]; | |
388 | #endif | |
389 | #ifdef VSTOP | |
390 | _rl_tty_chars.t_stop = tiop->c_cc[VSTOP]; | |
391 | #endif | |
392 | #ifdef VLNEXT | |
393 | _rl_tty_chars.t_lnext = tiop->c_cc[VLNEXT]; | |
394 | #endif | |
395 | #ifdef VDISCARD | |
396 | _rl_tty_chars.t_flush = tiop->c_cc[VDISCARD]; | |
397 | #endif | |
398 | #ifdef VSTATUS | |
399 | _rl_tty_chars.t_status = tiop->c_cc[VSTATUS]; | |
400 | #endif | |
401 | } | |
402 | ||
403 | #if defined (_AIX) || defined (_AIX41) | |
404 | /* Currently this is only used on AIX */ | |
d60d9f65 SS |
405 | static void |
406 | rltty_warning (msg) | |
407 | char *msg; | |
408 | { | |
cc88a640 | 409 | _rl_errmsg ("warning: %s", msg); |
d60d9f65 | 410 | } |
1b17e766 | 411 | #endif |
d60d9f65 SS |
412 | |
413 | #if defined (_AIX) | |
414 | void | |
415 | setopost(tp) | |
416 | TIOTYPE *tp; | |
417 | { | |
418 | if ((tp->c_oflag & OPOST) == 0) | |
419 | { | |
cc88a640 | 420 | _rl_errmsg ("warning: turning on OPOST for terminal\r"); |
d60d9f65 SS |
421 | tp->c_oflag |= OPOST|ONLCR; |
422 | } | |
423 | } | |
424 | #endif | |
425 | ||
426 | static int | |
1b17e766 | 427 | _get_tty_settings (tty, tiop) |
d60d9f65 SS |
428 | int tty; |
429 | TIOTYPE *tiop; | |
430 | { | |
431 | int ioctl_ret; | |
c862e87b | 432 | |
d60d9f65 SS |
433 | while (1) |
434 | { | |
435 | ioctl_ret = GETATTR (tty, tiop); | |
436 | if (ioctl_ret < 0) | |
437 | { | |
438 | if (errno != EINTR) | |
439 | return -1; | |
440 | else | |
441 | continue; | |
442 | } | |
443 | if (OUTPUT_BEING_FLUSHED (tiop)) | |
444 | { | |
cc88a640 JK |
445 | #if defined (FLUSHO) |
446 | _rl_errmsg ("warning: turning off output flushing"); | |
d60d9f65 SS |
447 | tiop->c_lflag &= ~FLUSHO; |
448 | break; | |
449 | #else | |
450 | continue; | |
451 | #endif | |
452 | } | |
453 | break; | |
454 | } | |
455 | ||
1b17e766 EZ |
456 | return 0; |
457 | } | |
458 | ||
459 | static int | |
460 | get_tty_settings (tty, tiop) | |
461 | int tty; | |
462 | TIOTYPE *tiop; | |
463 | { | |
1b17e766 | 464 | set_winsize (tty); |
1b17e766 | 465 | |
5bdf8622 | 466 | errno = 0; |
1b17e766 EZ |
467 | if (_get_tty_settings (tty, tiop) < 0) |
468 | return -1; | |
469 | ||
d60d9f65 SS |
470 | #if defined (_AIX) |
471 | setopost(tiop); | |
472 | #endif | |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
477 | static int | |
1b17e766 | 478 | _set_tty_settings (tty, tiop) |
d60d9f65 SS |
479 | int tty; |
480 | TIOTYPE *tiop; | |
481 | { | |
482 | while (SETATTR (tty, tiop) < 0) | |
483 | { | |
484 | if (errno != EINTR) | |
485 | return -1; | |
486 | errno = 0; | |
487 | } | |
1b17e766 EZ |
488 | return 0; |
489 | } | |
d60d9f65 | 490 | |
1b17e766 EZ |
491 | static int |
492 | set_tty_settings (tty, tiop) | |
493 | int tty; | |
494 | TIOTYPE *tiop; | |
495 | { | |
496 | if (_set_tty_settings (tty, tiop) < 0) | |
497 | return -1; | |
498 | ||
d60d9f65 SS |
499 | #if 0 |
500 | ||
501 | #if defined (TERMIOS_TTY_DRIVER) | |
502 | # if defined (__ksr1__) | |
503 | if (ksrflow) | |
504 | { | |
505 | ksrflow = 0; | |
506 | tcflow (tty, TCOON); | |
507 | } | |
508 | # else /* !ksr1 */ | |
509 | tcflow (tty, TCOON); /* Simulate a ^Q. */ | |
510 | # endif /* !ksr1 */ | |
511 | #else | |
512 | ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */ | |
513 | #endif /* !TERMIOS_TTY_DRIVER */ | |
514 | ||
1b17e766 | 515 | #endif /* 0 */ |
d60d9f65 SS |
516 | |
517 | return 0; | |
518 | } | |
519 | ||
520 | static void | |
9255ee31 | 521 | prepare_terminal_settings (meta_flag, oldtio, tiop) |
d60d9f65 | 522 | int meta_flag; |
9255ee31 | 523 | TIOTYPE oldtio, *tiop; |
d60d9f65 | 524 | { |
cc88a640 JK |
525 | _rl_echoing_p = (oldtio.c_lflag & ECHO); |
526 | #if defined (ECHOCTL) | |
527 | _rl_echoctl = (oldtio.c_lflag & ECHOCTL); | |
528 | #endif | |
d60d9f65 SS |
529 | |
530 | tiop->c_lflag &= ~(ICANON | ECHO); | |
531 | ||
9255ee31 EZ |
532 | if ((unsigned char) oldtio.c_cc[VEOF] != (unsigned char) _POSIX_VDISABLE) |
533 | _rl_eof_char = oldtio.c_cc[VEOF]; | |
d60d9f65 SS |
534 | |
535 | #if defined (USE_XON_XOFF) | |
536 | #if defined (IXANY) | |
4a11f206 | 537 | tiop->c_iflag &= ~(IXON | IXANY); |
d60d9f65 SS |
538 | #else |
539 | /* `strict' Posix systems do not define IXANY. */ | |
4a11f206 | 540 | tiop->c_iflag &= ~IXON; |
d60d9f65 SS |
541 | #endif /* IXANY */ |
542 | #endif /* USE_XON_XOFF */ | |
543 | ||
544 | /* Only turn this off if we are using all 8 bits. */ | |
545 | if (((tiop->c_cflag & CSIZE) == CS8) || meta_flag) | |
546 | tiop->c_iflag &= ~(ISTRIP | INPCK); | |
547 | ||
548 | /* Make sure we differentiate between CR and NL on input. */ | |
549 | tiop->c_iflag &= ~(ICRNL | INLCR); | |
550 | ||
551 | #if !defined (HANDLE_SIGNALS) | |
552 | tiop->c_lflag &= ~ISIG; | |
553 | #else | |
554 | tiop->c_lflag |= ISIG; | |
555 | #endif | |
556 | ||
557 | tiop->c_cc[VMIN] = 1; | |
558 | tiop->c_cc[VTIME] = 0; | |
559 | ||
560 | #if defined (FLUSHO) | |
561 | if (OUTPUT_BEING_FLUSHED (tiop)) | |
562 | { | |
563 | tiop->c_lflag &= ~FLUSHO; | |
9255ee31 | 564 | oldtio.c_lflag &= ~FLUSHO; |
d60d9f65 SS |
565 | } |
566 | #endif | |
567 | ||
568 | /* Turn off characters that we need on Posix systems with job control, | |
569 | just to be sure. This includes ^Y and ^V. This should not really | |
570 | be necessary. */ | |
571 | #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_VDISABLE) | |
572 | ||
573 | #if defined (VLNEXT) | |
574 | tiop->c_cc[VLNEXT] = _POSIX_VDISABLE; | |
575 | #endif | |
576 | ||
577 | #if defined (VDSUSP) | |
578 | tiop->c_cc[VDSUSP] = _POSIX_VDISABLE; | |
579 | #endif | |
580 | ||
581 | #endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */ | |
582 | } | |
5bdf8622 | 583 | #endif /* !NEW_TTY_DRIVER */ |
d60d9f65 | 584 | |
5bdf8622 | 585 | /* Put the terminal in CBREAK mode so that we can detect key presses. */ |
fd8be987 MM |
586 | #if defined (NO_TTY_DRIVER) |
587 | void | |
588 | rl_prep_terminal (meta_flag) | |
589 | int meta_flag; | |
590 | { | |
cc88a640 | 591 | _rl_echoing_p = 1; |
fd8be987 MM |
592 | } |
593 | ||
594 | void | |
595 | rl_deprep_terminal () | |
596 | { | |
597 | } | |
598 | ||
599 | #else /* ! NO_TTY_DRIVER */ | |
d60d9f65 SS |
600 | void |
601 | rl_prep_terminal (meta_flag) | |
602 | int meta_flag; | |
603 | { | |
4a11f206 | 604 | int tty, nprep; |
d60d9f65 SS |
605 | TIOTYPE tio; |
606 | ||
607 | if (terminal_prepped) | |
608 | return; | |
609 | ||
610 | /* Try to keep this function from being INTerrupted. */ | |
87adec2e | 611 | _rl_block_sigint (); |
d60d9f65 | 612 | |
cc88a640 | 613 | tty = rl_instream ? fileno (rl_instream) : fileno (stdin); |
d60d9f65 SS |
614 | |
615 | if (get_tty_settings (tty, &tio) < 0) | |
616 | { | |
5bdf8622 | 617 | #if defined (ENOTSUP) |
cc88a640 JK |
618 | /* MacOS X and Linux, at least, lie about the value of errno if |
619 | tcgetattr fails. */ | |
620 | if (errno == ENOTTY || errno == EINVAL || errno == ENOTSUP) | |
5bdf8622 | 621 | #else |
cc88a640 | 622 | if (errno == ENOTTY || errno == EINVAL) |
5bdf8622 | 623 | #endif |
cc88a640 JK |
624 | _rl_echoing_p = 1; /* XXX */ |
625 | ||
87adec2e | 626 | _rl_release_sigint (); |
d60d9f65 SS |
627 | return; |
628 | } | |
629 | ||
630 | otio = tio; | |
631 | ||
5bdf8622 DJ |
632 | if (_rl_bind_stty_chars) |
633 | { | |
634 | #if defined (VI_MODE) | |
635 | /* If editing in vi mode, make sure we restore the bindings in the | |
636 | insertion keymap no matter what keymap we ended up in. */ | |
637 | if (rl_editing_mode == vi_mode) | |
638 | rl_tty_unset_default_bindings (vi_insertion_keymap); | |
639 | else | |
640 | #endif | |
641 | rl_tty_unset_default_bindings (_rl_keymap); | |
642 | } | |
1b17e766 | 643 | save_tty_chars (&otio); |
5bdf8622 DJ |
644 | RL_SETSTATE(RL_STATE_TTYCSAVED); |
645 | if (_rl_bind_stty_chars) | |
646 | { | |
647 | #if defined (VI_MODE) | |
648 | /* If editing in vi mode, make sure we set the bindings in the | |
649 | insertion keymap no matter what keymap we ended up in. */ | |
650 | if (rl_editing_mode == vi_mode) | |
4a11f206 | 651 | _rl_bind_tty_special_chars (vi_insertion_keymap, tio); |
5bdf8622 DJ |
652 | else |
653 | #endif | |
654 | _rl_bind_tty_special_chars (_rl_keymap, tio); | |
655 | } | |
1b17e766 | 656 | |
d60d9f65 SS |
657 | prepare_terminal_settings (meta_flag, otio, &tio); |
658 | ||
659 | if (set_tty_settings (tty, &tio) < 0) | |
660 | { | |
87adec2e | 661 | _rl_release_sigint (); |
d60d9f65 SS |
662 | return; |
663 | } | |
664 | ||
665 | if (_rl_enable_keypad) | |
666 | _rl_control_keypad (1); | |
667 | ||
4a11f206 PP |
668 | nprep = TPX_PREPPED; |
669 | ||
670 | if (_rl_enable_bracketed_paste) | |
671 | { | |
672 | fprintf (rl_outstream, BRACK_PASTE_INIT); | |
673 | nprep |= TPX_BRACKPASTE; | |
674 | } | |
675 | ||
d60d9f65 | 676 | fflush (rl_outstream); |
4a11f206 | 677 | terminal_prepped = nprep; |
9255ee31 | 678 | RL_SETSTATE(RL_STATE_TERMPREPPED); |
d60d9f65 | 679 | |
87adec2e | 680 | _rl_release_sigint (); |
d60d9f65 SS |
681 | } |
682 | ||
683 | /* Restore the terminal's normal settings and modes. */ | |
684 | void | |
685 | rl_deprep_terminal () | |
686 | { | |
d60d9f65 SS |
687 | int tty; |
688 | ||
4a11f206 | 689 | if (terminal_prepped == 0) |
d60d9f65 SS |
690 | return; |
691 | ||
692 | /* Try to keep this function from being interrupted. */ | |
87adec2e | 693 | _rl_block_sigint (); |
d60d9f65 | 694 | |
4a11f206 PP |
695 | tty = rl_instream ? fileno (rl_instream) : fileno (stdin); |
696 | ||
697 | if (terminal_prepped & TPX_BRACKPASTE) | |
698 | fprintf (rl_outstream, BRACK_PASTE_FINI); | |
d60d9f65 SS |
699 | |
700 | if (_rl_enable_keypad) | |
701 | _rl_control_keypad (0); | |
702 | ||
703 | fflush (rl_outstream); | |
704 | ||
705 | if (set_tty_settings (tty, &otio) < 0) | |
706 | { | |
87adec2e | 707 | _rl_release_sigint (); |
d60d9f65 SS |
708 | return; |
709 | } | |
710 | ||
711 | terminal_prepped = 0; | |
9255ee31 | 712 | RL_UNSETSTATE(RL_STATE_TERMPREPPED); |
d60d9f65 | 713 | |
87adec2e | 714 | _rl_release_sigint (); |
d60d9f65 | 715 | } |
fd8be987 | 716 | #endif /* !NO_TTY_DRIVER */ |
d60d9f65 SS |
717 | \f |
718 | /* **************************************************************** */ | |
719 | /* */ | |
720 | /* Bogus Flow Control */ | |
721 | /* */ | |
722 | /* **************************************************************** */ | |
723 | ||
724 | int | |
725 | rl_restart_output (count, key) | |
726 | int count, key; | |
727 | { | |
fd8be987 MM |
728 | #if defined (__MINGW32__) |
729 | return 0; | |
730 | #else /* !__MING32__ */ | |
731 | ||
d60d9f65 SS |
732 | int fildes = fileno (rl_outstream); |
733 | #if defined (TIOCSTART) | |
734 | #if defined (apollo) | |
735 | ioctl (&fildes, TIOCSTART, 0); | |
736 | #else | |
737 | ioctl (fildes, TIOCSTART, 0); | |
738 | #endif /* apollo */ | |
739 | ||
740 | #else /* !TIOCSTART */ | |
741 | # if defined (TERMIOS_TTY_DRIVER) | |
742 | # if defined (__ksr1__) | |
743 | if (ksrflow) | |
744 | { | |
745 | ksrflow = 0; | |
746 | tcflow (fildes, TCOON); | |
747 | } | |
748 | # else /* !ksr1 */ | |
749 | tcflow (fildes, TCOON); /* Simulate a ^Q. */ | |
750 | # endif /* !ksr1 */ | |
751 | # else /* !TERMIOS_TTY_DRIVER */ | |
752 | # if defined (TCXONC) | |
753 | ioctl (fildes, TCXONC, TCOON); | |
754 | # endif /* TCXONC */ | |
755 | # endif /* !TERMIOS_TTY_DRIVER */ | |
756 | #endif /* !TIOCSTART */ | |
757 | ||
758 | return 0; | |
fd8be987 | 759 | #endif /* !__MINGW32__ */ |
d60d9f65 SS |
760 | } |
761 | ||
762 | int | |
763 | rl_stop_output (count, key) | |
764 | int count, key; | |
765 | { | |
fd8be987 MM |
766 | #if defined (__MINGW32__) |
767 | return 0; | |
768 | #else | |
769 | ||
d60d9f65 SS |
770 | int fildes = fileno (rl_instream); |
771 | ||
772 | #if defined (TIOCSTOP) | |
773 | # if defined (apollo) | |
774 | ioctl (&fildes, TIOCSTOP, 0); | |
775 | # else | |
776 | ioctl (fildes, TIOCSTOP, 0); | |
777 | # endif /* apollo */ | |
778 | #else /* !TIOCSTOP */ | |
779 | # if defined (TERMIOS_TTY_DRIVER) | |
780 | # if defined (__ksr1__) | |
781 | ksrflow = 1; | |
782 | # endif /* ksr1 */ | |
783 | tcflow (fildes, TCOOFF); | |
784 | # else | |
785 | # if defined (TCXONC) | |
786 | ioctl (fildes, TCXONC, TCOON); | |
787 | # endif /* TCXONC */ | |
788 | # endif /* !TERMIOS_TTY_DRIVER */ | |
789 | #endif /* !TIOCSTOP */ | |
790 | ||
791 | return 0; | |
fd8be987 | 792 | #endif /* !__MINGW32__ */ |
d60d9f65 SS |
793 | } |
794 | ||
795 | /* **************************************************************** */ | |
796 | /* */ | |
797 | /* Default Key Bindings */ | |
798 | /* */ | |
799 | /* **************************************************************** */ | |
9255ee31 | 800 | |
fd8be987 | 801 | #if !defined (NO_TTY_DRIVER) |
5bdf8622 DJ |
802 | #define SET_SPECIAL(sc, func) set_special_char(kmap, &ttybuff, sc, func) |
803 | #endif | |
d60d9f65 | 804 | |
5bdf8622 | 805 | #if defined (NO_TTY_DRIVER) |
d60d9f65 | 806 | |
5bdf8622 DJ |
807 | #define SET_SPECIAL(sc, func) |
808 | #define RESET_SPECIAL(c) | |
d60d9f65 | 809 | |
5bdf8622 DJ |
810 | #elif defined (NEW_TTY_DRIVER) |
811 | static void | |
812 | set_special_char (kmap, tiop, sc, func) | |
813 | Keymap kmap; | |
814 | TIOTYPE *tiop; | |
815 | int sc; | |
816 | rl_command_func_t *func; | |
817 | { | |
818 | if (sc != -1 && kmap[(unsigned char)sc].type == ISFUNC) | |
819 | kmap[(unsigned char)sc].function = func; | |
820 | } | |
821 | ||
822 | #define RESET_SPECIAL(c) \ | |
cc88a640 | 823 | if (c != -1 && kmap[(unsigned char)c].type == ISFUNC) \ |
5bdf8622 DJ |
824 | kmap[(unsigned char)c].function = rl_insert; |
825 | ||
826 | static void | |
827 | _rl_bind_tty_special_chars (kmap, ttybuff) | |
828 | Keymap kmap; | |
829 | TIOTYPE ttybuff; | |
830 | { | |
831 | if (ttybuff.flags & SGTTY_SET) | |
d60d9f65 | 832 | { |
5bdf8622 DJ |
833 | SET_SPECIAL (ttybuff.sgttyb.sg_erase, rl_rubout); |
834 | SET_SPECIAL (ttybuff.sgttyb.sg_kill, rl_unix_line_discard); | |
835 | } | |
d60d9f65 SS |
836 | |
837 | # if defined (TIOCGLTC) | |
5bdf8622 DJ |
838 | if (ttybuff.flags & LTCHARS_SET) |
839 | { | |
840 | SET_SPECIAL (ttybuff.ltchars.t_werasc, rl_unix_word_rubout); | |
841 | SET_SPECIAL (ttybuff.ltchars.t_lnextc, rl_quoted_insert); | |
d60d9f65 | 842 | } |
5bdf8622 DJ |
843 | # endif /* TIOCGLTC */ |
844 | } | |
d60d9f65 SS |
845 | |
846 | #else /* !NEW_TTY_DRIVER */ | |
5bdf8622 DJ |
847 | static void |
848 | set_special_char (kmap, tiop, sc, func) | |
849 | Keymap kmap; | |
850 | TIOTYPE *tiop; | |
851 | int sc; | |
852 | rl_command_func_t *func; | |
853 | { | |
854 | unsigned char uc; | |
d60d9f65 | 855 | |
5bdf8622 DJ |
856 | uc = tiop->c_cc[sc]; |
857 | if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) | |
858 | kmap[uc].function = func; | |
859 | } | |
d60d9f65 | 860 | |
5bdf8622 DJ |
861 | /* used later */ |
862 | #define RESET_SPECIAL(uc) \ | |
863 | if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) \ | |
864 | kmap[uc].function = rl_insert; | |
865 | ||
866 | static void | |
867 | _rl_bind_tty_special_chars (kmap, ttybuff) | |
868 | Keymap kmap; | |
869 | TIOTYPE ttybuff; | |
870 | { | |
871 | SET_SPECIAL (VERASE, rl_rubout); | |
872 | SET_SPECIAL (VKILL, rl_unix_line_discard); | |
d60d9f65 SS |
873 | |
874 | # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER) | |
5bdf8622 | 875 | SET_SPECIAL (VLNEXT, rl_quoted_insert); |
d60d9f65 SS |
876 | # endif /* VLNEXT && TERMIOS_TTY_DRIVER */ |
877 | ||
878 | # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER) | |
5bdf8622 | 879 | SET_SPECIAL (VWERASE, rl_unix_word_rubout); |
d60d9f65 | 880 | # endif /* VWERASE && TERMIOS_TTY_DRIVER */ |
5bdf8622 DJ |
881 | } |
882 | ||
d60d9f65 | 883 | #endif /* !NEW_TTY_DRIVER */ |
5bdf8622 DJ |
884 | |
885 | /* Set the system's default editing characters to their readline equivalents | |
886 | in KMAP. Should be static, now that we have rl_tty_set_default_bindings. */ | |
887 | void | |
888 | rltty_set_default_bindings (kmap) | |
889 | Keymap kmap; | |
890 | { | |
891 | #if !defined (NO_TTY_DRIVER) | |
892 | TIOTYPE ttybuff; | |
893 | int tty; | |
5bdf8622 DJ |
894 | |
895 | tty = fileno (rl_instream); | |
896 | ||
897 | if (get_tty_settings (tty, &ttybuff) == 0) | |
898 | _rl_bind_tty_special_chars (kmap, ttybuff); | |
fd8be987 | 899 | #endif |
d60d9f65 | 900 | } |
1b17e766 | 901 | |
9255ee31 EZ |
902 | /* New public way to set the system default editing chars to their readline |
903 | equivalents. */ | |
904 | void | |
905 | rl_tty_set_default_bindings (kmap) | |
906 | Keymap kmap; | |
907 | { | |
908 | rltty_set_default_bindings (kmap); | |
909 | } | |
910 | ||
5bdf8622 DJ |
911 | /* Rebind all of the tty special chars that readline worries about back |
912 | to self-insert. Call this before saving the current terminal special | |
913 | chars with save_tty_chars(). This only works on POSIX termios or termio | |
914 | systems. */ | |
915 | void | |
916 | rl_tty_unset_default_bindings (kmap) | |
917 | Keymap kmap; | |
918 | { | |
919 | /* Don't bother before we've saved the tty special chars at least once. */ | |
920 | if (RL_ISSTATE(RL_STATE_TTYCSAVED) == 0) | |
921 | return; | |
922 | ||
923 | RESET_SPECIAL (_rl_tty_chars.t_erase); | |
924 | RESET_SPECIAL (_rl_tty_chars.t_kill); | |
925 | ||
926 | # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER) | |
927 | RESET_SPECIAL (_rl_tty_chars.t_lnext); | |
928 | # endif /* VLNEXT && TERMIOS_TTY_DRIVER */ | |
929 | ||
930 | # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER) | |
931 | RESET_SPECIAL (_rl_tty_chars.t_werase); | |
932 | # endif /* VWERASE && TERMIOS_TTY_DRIVER */ | |
933 | } | |
934 | ||
1b17e766 EZ |
935 | #if defined (HANDLE_SIGNALS) |
936 | ||
fd8be987 | 937 | #if defined (NEW_TTY_DRIVER) || defined (NO_TTY_DRIVER) |
1b17e766 EZ |
938 | int |
939 | _rl_disable_tty_signals () | |
940 | { | |
941 | return 0; | |
942 | } | |
943 | ||
944 | int | |
945 | _rl_restore_tty_signals () | |
946 | { | |
947 | return 0; | |
948 | } | |
949 | #else | |
950 | ||
951 | static TIOTYPE sigstty, nosigstty; | |
952 | static int tty_sigs_disabled = 0; | |
953 | ||
954 | int | |
955 | _rl_disable_tty_signals () | |
956 | { | |
957 | if (tty_sigs_disabled) | |
958 | return 0; | |
959 | ||
960 | if (_get_tty_settings (fileno (rl_instream), &sigstty) < 0) | |
961 | return -1; | |
962 | ||
963 | nosigstty = sigstty; | |
964 | ||
965 | nosigstty.c_lflag &= ~ISIG; | |
9255ee31 | 966 | nosigstty.c_iflag &= ~IXON; |
1b17e766 EZ |
967 | |
968 | if (_set_tty_settings (fileno (rl_instream), &nosigstty) < 0) | |
969 | return (_set_tty_settings (fileno (rl_instream), &sigstty)); | |
970 | ||
971 | tty_sigs_disabled = 1; | |
972 | return 0; | |
973 | } | |
974 | ||
975 | int | |
976 | _rl_restore_tty_signals () | |
977 | { | |
9255ee31 EZ |
978 | int r; |
979 | ||
1b17e766 EZ |
980 | if (tty_sigs_disabled == 0) |
981 | return 0; | |
982 | ||
9255ee31 EZ |
983 | r = _set_tty_settings (fileno (rl_instream), &sigstty); |
984 | ||
985 | if (r == 0) | |
986 | tty_sigs_disabled = 0; | |
987 | ||
988 | return r; | |
1b17e766 EZ |
989 | } |
990 | #endif /* !NEW_TTY_DRIVER */ | |
991 | ||
992 | #endif /* HANDLE_SIGNALS */ |