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