Import readline 7.0 (patch 5)
[deliverable/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
775e241e 4/* Copyright (C) 1992-2016 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
EZ
132static void
133save_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
170static int
171get_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
202static int
203set_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
241static void
9255ee31 242prepare_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
340static TIOTYPE otio;
341
9255ee31
EZ
342static void save_tty_chars PARAMS((TIOTYPE *));
343static int _get_tty_settings PARAMS((int, TIOTYPE *));
344static int get_tty_settings PARAMS((int, TIOTYPE *));
345static int _set_tty_settings PARAMS((int, TIOTYPE *));
346static int set_tty_settings PARAMS((int, TIOTYPE *));
347
348static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));
349
775e241e 350static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t *));
5bdf8622
DJ
351static 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
359static void
360save_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
405static void
406rltty_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)
414void
415setopost(tp)
416TIOTYPE *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
426static 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
459static int
460get_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
477static 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
491static int
492set_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
520static void
9255ee31 521prepare_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)
775e241e 537 tiop->c_iflag &= ~(IXON | IXANY);
d60d9f65
SS
538#else
539 /* `strict' Posix systems do not define IXANY. */
775e241e 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)
587void
588rl_prep_terminal (meta_flag)
589 int meta_flag;
590{
cc88a640 591 _rl_echoing_p = 1;
fd8be987
MM
592}
593
594void
595rl_deprep_terminal ()
596{
597}
598
599#else /* ! NO_TTY_DRIVER */
d60d9f65
SS
600void
601rl_prep_terminal (meta_flag)
602 int meta_flag;
603{
775e241e 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)
775e241e 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
775e241e
TT
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);
775e241e 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. */
684void
685rl_deprep_terminal ()
686{
d60d9f65
SS
687 int tty;
688
775e241e 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
775e241e
TT
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 */
775e241e
TT
717
718/* Set readline's idea of whether or not it is echoing output to the terminal,
719 returning the old value. */
720int
721rl_tty_set_echoing (u)
722 int u;
723{
724 int o;
725
726 o = _rl_echoing_p;
727 _rl_echoing_p = u;
728 return o;
729}
d60d9f65
SS
730\f
731/* **************************************************************** */
732/* */
733/* Bogus Flow Control */
734/* */
735/* **************************************************************** */
736
737int
738rl_restart_output (count, key)
739 int count, key;
740{
fd8be987
MM
741#if defined (__MINGW32__)
742 return 0;
743#else /* !__MING32__ */
744
d60d9f65
SS
745 int fildes = fileno (rl_outstream);
746#if defined (TIOCSTART)
747#if defined (apollo)
748 ioctl (&fildes, TIOCSTART, 0);
749#else
750 ioctl (fildes, TIOCSTART, 0);
751#endif /* apollo */
752
753#else /* !TIOCSTART */
754# if defined (TERMIOS_TTY_DRIVER)
755# if defined (__ksr1__)
756 if (ksrflow)
757 {
758 ksrflow = 0;
759 tcflow (fildes, TCOON);
760 }
761# else /* !ksr1 */
762 tcflow (fildes, TCOON); /* Simulate a ^Q. */
763# endif /* !ksr1 */
764# else /* !TERMIOS_TTY_DRIVER */
765# if defined (TCXONC)
766 ioctl (fildes, TCXONC, TCOON);
767# endif /* TCXONC */
768# endif /* !TERMIOS_TTY_DRIVER */
769#endif /* !TIOCSTART */
770
771 return 0;
fd8be987 772#endif /* !__MINGW32__ */
d60d9f65
SS
773}
774
775int
776rl_stop_output (count, key)
777 int count, key;
778{
fd8be987
MM
779#if defined (__MINGW32__)
780 return 0;
781#else
782
d60d9f65
SS
783 int fildes = fileno (rl_instream);
784
785#if defined (TIOCSTOP)
786# if defined (apollo)
787 ioctl (&fildes, TIOCSTOP, 0);
788# else
789 ioctl (fildes, TIOCSTOP, 0);
790# endif /* apollo */
791#else /* !TIOCSTOP */
792# if defined (TERMIOS_TTY_DRIVER)
793# if defined (__ksr1__)
794 ksrflow = 1;
795# endif /* ksr1 */
796 tcflow (fildes, TCOOFF);
797# else
798# if defined (TCXONC)
799 ioctl (fildes, TCXONC, TCOON);
800# endif /* TCXONC */
801# endif /* !TERMIOS_TTY_DRIVER */
802#endif /* !TIOCSTOP */
803
804 return 0;
fd8be987 805#endif /* !__MINGW32__ */
d60d9f65
SS
806}
807
808/* **************************************************************** */
809/* */
810/* Default Key Bindings */
811/* */
812/* **************************************************************** */
9255ee31 813
fd8be987 814#if !defined (NO_TTY_DRIVER)
5bdf8622
DJ
815#define SET_SPECIAL(sc, func) set_special_char(kmap, &ttybuff, sc, func)
816#endif
d60d9f65 817
5bdf8622 818#if defined (NO_TTY_DRIVER)
d60d9f65 819
5bdf8622
DJ
820#define SET_SPECIAL(sc, func)
821#define RESET_SPECIAL(c)
d60d9f65 822
5bdf8622
DJ
823#elif defined (NEW_TTY_DRIVER)
824static void
825set_special_char (kmap, tiop, sc, func)
826 Keymap kmap;
827 TIOTYPE *tiop;
828 int sc;
829 rl_command_func_t *func;
830{
831 if (sc != -1 && kmap[(unsigned char)sc].type == ISFUNC)
832 kmap[(unsigned char)sc].function = func;
833}
834
835#define RESET_SPECIAL(c) \
cc88a640 836 if (c != -1 && kmap[(unsigned char)c].type == ISFUNC) \
5bdf8622
DJ
837 kmap[(unsigned char)c].function = rl_insert;
838
839static void
840_rl_bind_tty_special_chars (kmap, ttybuff)
841 Keymap kmap;
842 TIOTYPE ttybuff;
843{
844 if (ttybuff.flags & SGTTY_SET)
d60d9f65 845 {
5bdf8622
DJ
846 SET_SPECIAL (ttybuff.sgttyb.sg_erase, rl_rubout);
847 SET_SPECIAL (ttybuff.sgttyb.sg_kill, rl_unix_line_discard);
848 }
d60d9f65
SS
849
850# if defined (TIOCGLTC)
5bdf8622
DJ
851 if (ttybuff.flags & LTCHARS_SET)
852 {
853 SET_SPECIAL (ttybuff.ltchars.t_werasc, rl_unix_word_rubout);
854 SET_SPECIAL (ttybuff.ltchars.t_lnextc, rl_quoted_insert);
d60d9f65 855 }
5bdf8622
DJ
856# endif /* TIOCGLTC */
857}
d60d9f65
SS
858
859#else /* !NEW_TTY_DRIVER */
5bdf8622
DJ
860static void
861set_special_char (kmap, tiop, sc, func)
862 Keymap kmap;
863 TIOTYPE *tiop;
864 int sc;
865 rl_command_func_t *func;
866{
867 unsigned char uc;
d60d9f65 868
5bdf8622
DJ
869 uc = tiop->c_cc[sc];
870 if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC)
871 kmap[uc].function = func;
872}
d60d9f65 873
5bdf8622
DJ
874/* used later */
875#define RESET_SPECIAL(uc) \
876 if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) \
877 kmap[uc].function = rl_insert;
878
879static void
880_rl_bind_tty_special_chars (kmap, ttybuff)
881 Keymap kmap;
882 TIOTYPE ttybuff;
883{
884 SET_SPECIAL (VERASE, rl_rubout);
885 SET_SPECIAL (VKILL, rl_unix_line_discard);
d60d9f65
SS
886
887# if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
5bdf8622 888 SET_SPECIAL (VLNEXT, rl_quoted_insert);
d60d9f65
SS
889# endif /* VLNEXT && TERMIOS_TTY_DRIVER */
890
891# if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
775e241e
TT
892# if defined (VI_MODE)
893 if (rl_editing_mode == vi_mode)
894 SET_SPECIAL (VWERASE, rl_vi_unix_word_rubout);
895 else
896# endif
5bdf8622 897 SET_SPECIAL (VWERASE, rl_unix_word_rubout);
d60d9f65 898# endif /* VWERASE && TERMIOS_TTY_DRIVER */
5bdf8622
DJ
899}
900
d60d9f65 901#endif /* !NEW_TTY_DRIVER */
5bdf8622
DJ
902
903/* Set the system's default editing characters to their readline equivalents
904 in KMAP. Should be static, now that we have rl_tty_set_default_bindings. */
905void
906rltty_set_default_bindings (kmap)
907 Keymap kmap;
908{
909#if !defined (NO_TTY_DRIVER)
910 TIOTYPE ttybuff;
911 int tty;
5bdf8622
DJ
912
913 tty = fileno (rl_instream);
914
915 if (get_tty_settings (tty, &ttybuff) == 0)
916 _rl_bind_tty_special_chars (kmap, ttybuff);
fd8be987 917#endif
d60d9f65 918}
1b17e766 919
9255ee31
EZ
920/* New public way to set the system default editing chars to their readline
921 equivalents. */
922void
923rl_tty_set_default_bindings (kmap)
924 Keymap kmap;
925{
926 rltty_set_default_bindings (kmap);
927}
928
5bdf8622
DJ
929/* Rebind all of the tty special chars that readline worries about back
930 to self-insert. Call this before saving the current terminal special
931 chars with save_tty_chars(). This only works on POSIX termios or termio
932 systems. */
933void
934rl_tty_unset_default_bindings (kmap)
935 Keymap kmap;
936{
937 /* Don't bother before we've saved the tty special chars at least once. */
938 if (RL_ISSTATE(RL_STATE_TTYCSAVED) == 0)
939 return;
940
941 RESET_SPECIAL (_rl_tty_chars.t_erase);
942 RESET_SPECIAL (_rl_tty_chars.t_kill);
943
944# if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
945 RESET_SPECIAL (_rl_tty_chars.t_lnext);
946# endif /* VLNEXT && TERMIOS_TTY_DRIVER */
947
948# if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
949 RESET_SPECIAL (_rl_tty_chars.t_werase);
950# endif /* VWERASE && TERMIOS_TTY_DRIVER */
951}
952
1b17e766
EZ
953#if defined (HANDLE_SIGNALS)
954
fd8be987 955#if defined (NEW_TTY_DRIVER) || defined (NO_TTY_DRIVER)
1b17e766
EZ
956int
957_rl_disable_tty_signals ()
958{
959 return 0;
960}
961
962int
963_rl_restore_tty_signals ()
964{
965 return 0;
966}
967#else
968
969static TIOTYPE sigstty, nosigstty;
970static int tty_sigs_disabled = 0;
971
972int
973_rl_disable_tty_signals ()
974{
975 if (tty_sigs_disabled)
976 return 0;
977
978 if (_get_tty_settings (fileno (rl_instream), &sigstty) < 0)
979 return -1;
980
981 nosigstty = sigstty;
982
983 nosigstty.c_lflag &= ~ISIG;
9255ee31 984 nosigstty.c_iflag &= ~IXON;
1b17e766
EZ
985
986 if (_set_tty_settings (fileno (rl_instream), &nosigstty) < 0)
987 return (_set_tty_settings (fileno (rl_instream), &sigstty));
988
989 tty_sigs_disabled = 1;
990 return 0;
991}
992
993int
994_rl_restore_tty_signals ()
995{
9255ee31
EZ
996 int r;
997
1b17e766
EZ
998 if (tty_sigs_disabled == 0)
999 return 0;
1000
9255ee31
EZ
1001 r = _set_tty_settings (fileno (rl_instream), &sigstty);
1002
1003 if (r == 0)
1004 tty_sigs_disabled = 0;
1005
1006 return r;
1b17e766
EZ
1007}
1008#endif /* !NEW_TTY_DRIVER */
1009
1010#endif /* HANDLE_SIGNALS */
This page took 0.879505 seconds and 4 git commands to generate.