init version.
[bush.git] / lib / readline / rltty.c
blobd0cd572713a9bea108e0a8bd58fbb9e7582aa3a7
1 /* rltty.c -- functions to prepare and restore the terminal for readline's
2 use. */
4 /* Copyright (C) 1992-2017 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library (Readline), a library
7 for reading lines of text with interactive input and history editing.
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
12 (at your option) any later version.
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
17 GNU General Public License for more details.
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/>.
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
26 # include <config.h>
27 #endif
29 #include <sys/types.h>
30 #include <signal.h>
31 #include <errno.h>
32 #include <stdio.h>
34 #if defined (HAVE_UNISTD_H)
35 # include <unistd.h>
36 #endif /* HAVE_UNISTD_H */
38 #include "rldefs.h"
40 #include "rltty.h"
41 #if defined (HAVE_SYS_IOCTL_H)
42 # include <sys/ioctl.h> /* include for declaration of ioctl */
43 #endif
45 #include "readline.h"
46 #include "rlprivate.h"
48 #if !defined (errno)
49 extern int errno;
50 #endif /* !errno */
52 rl_vintfunc_t *rl_prep_term_function = rl_prep_terminal;
53 rl_voidfunc_t *rl_deprep_term_function = rl_deprep_terminal;
55 static void set_winsize PARAMS((int));
57 /* **************************************************************** */
58 /* */
59 /* Saving and Restoring the TTY */
60 /* */
61 /* **************************************************************** */
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
70 static int terminal_prepped;
72 static _RL_TTY_CHARS _rl_tty_chars, _rl_last_tty_chars;
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
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;
86 #if defined (TIOCGWINSZ)
87 struct winsize w;
89 if (ioctl (tty, TIOCGWINSZ, &w) == 0)
90 (void) ioctl (tty, TIOCSWINSZ, &w);
91 #endif /* TIOCGWINSZ */
94 #if defined (NO_TTY_DRIVER)
95 /* Nothing */
96 #elif defined (NEW_TTY_DRIVER)
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
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. */
118 #define TIOTYPE struct bsdtty
120 static TIOTYPE otio;
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 *));
128 static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));
130 static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t *));
132 static void
133 save_tty_chars (TIOTYPE *tiop)
135 _rl_last_tty_chars = _rl_tty_chars;
137 if (tiop->flags & SGTTY_SET)
139 _rl_tty_chars.t_erase = tiop->sgttyb.sg_erase;
140 _rl_tty_chars.t_kill = tiop->sgttyb.sg_kill;
143 if (tiop->flags & TCHARS_SET)
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;
148 _rl_tty_chars.t_start = tiop->tchars.t_startc;
149 _rl_tty_chars.t_stop = tiop->tchars.t_stopc;
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;
155 if (tiop->flags & LTCHARS_SET)
157 _rl_susp_char = _rl_tty_chars.t_susp = tiop->ltchars.t_suspc;
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;
166 _rl_tty_chars.t_status = -1;
169 static int
170 get_tty_settings (int tty, TIOTYPE *tiop)
172 set_winsize (tty);
174 tiop->flags = tiop->lflag = 0;
176 errno = 0;
177 if (ioctl (tty, TIOCGETP, &(tiop->sgttyb)) < 0)
178 return -1;
179 tiop->flags |= SGTTY_SET;
181 #if defined (TIOCLGET)
182 if (ioctl (tty, TIOCLGET, &(tiop->lflag)) == 0)
183 tiop->flags |= LFLAG_SET;
184 #endif
186 #if defined (TIOCGETC)
187 if (ioctl (tty, TIOCGETC, &(tiop->tchars)) == 0)
188 tiop->flags |= TCHARS_SET;
189 #endif
191 #if defined (TIOCGLTC)
192 if (ioctl (tty, TIOCGLTC, &(tiop->ltchars)) == 0)
193 tiop->flags |= LTCHARS_SET;
194 #endif
196 return 0;
199 static int
200 set_tty_settings (int tty, TIOTYPE *tiop)
202 if (tiop->flags & SGTTY_SET)
204 ioctl (tty, TIOCSETN, &(tiop->sgttyb));
205 tiop->flags &= ~SGTTY_SET;
207 _rl_echoing_p = 1;
209 #if defined (TIOCLSET)
210 if (tiop->flags & LFLAG_SET)
212 ioctl (tty, TIOCLSET, &(tiop->lflag));
213 tiop->flags &= ~LFLAG_SET;
215 #endif
217 #if defined (TIOCSETC)
218 if (tiop->flags & TCHARS_SET)
220 ioctl (tty, TIOCSETC, &(tiop->tchars));
221 tiop->flags &= ~TCHARS_SET;
223 #endif
225 #if defined (TIOCSLTC)
226 if (tiop->flags & LTCHARS_SET)
228 ioctl (tty, TIOCSLTC, &(tiop->ltchars));
229 tiop->flags &= ~LTCHARS_SET;
231 #endif
233 return 0;
236 static void
237 prepare_terminal_settings (int meta_flag, TIOTYPE oldtio, TIOTYPE *tiop)
239 _rl_echoing_p = (oldtio.sgttyb.sg_flags & ECHO);
240 _rl_echoctl = (oldtio.sgttyb.sg_flags & ECHOCTL);
242 /* Copy the original settings to the structure we're going to use for
243 our settings. */
244 tiop->sgttyb = oldtio.sgttyb;
245 tiop->lflag = oldtio.lflag;
246 #if defined (TIOCGETC)
247 tiop->tchars = oldtio.tchars;
248 #endif
249 #if defined (TIOCGLTC)
250 tiop->ltchars = oldtio.ltchars;
251 #endif
252 tiop->flags = oldtio.flags;
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;
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
265 if (((oldtio.sgttyb.sg_flags & ANYP) == ANYP) ||
266 ((oldtio.sgttyb.sg_flags & ANYP) == 0))
268 tiop->sgttyb.sg_flags |= ANYP;
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 */
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 */
284 /* If there is an XON character, bind it to restart the output. */
285 if (oldtio.tchars.t_startc != -1)
286 rl_bind_key (oldtio.tchars.t_startc, rl_restart_output);
287 # endif /* USE_XON_XOFF */
289 /* If there is an EOF char, bind _rl_eof_char to it. */
290 if (oldtio.tchars.t_eofc != -1)
291 _rl_eof_char = oldtio.tchars.t_eofc;
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 */
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 */
307 #else /* !defined (NEW_TTY_DRIVER) */
309 #if !defined (VMIN)
310 # define VMIN VEOF
311 #endif
313 #if !defined (VTIME)
314 # define VTIME VEOL
315 #endif
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))
330 # define SETATTR(tty, tiop) (ioctl (tty, TCSETAW, tiop))
331 #endif /* !TERMIOS_TTY_DRIVER */
333 static TIOTYPE otio;
335 static void save_tty_chars PARAMS((TIOTYPE *));
336 static int _get_tty_settings PARAMS((int, TIOTYPE *));
337 static int get_tty_settings PARAMS((int, TIOTYPE *));
338 static int _set_tty_settings PARAMS((int, TIOTYPE *));
339 static int set_tty_settings PARAMS((int, TIOTYPE *));
341 static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));
343 static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t *));
344 static void _rl_bind_tty_special_chars PARAMS((Keymap, TIOTYPE));
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
352 static void
353 save_tty_chars (TIOTYPE *tiop)
355 _rl_last_tty_chars = _rl_tty_chars;
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
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];
372 #ifdef VSUSP
373 _rl_susp_char = _rl_tty_chars.t_susp = tiop->c_cc[VSUSP];
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
395 #if defined (_AIX) || defined (_AIX41)
396 /* Currently this is only used on AIX */
397 static void
398 rltty_warning (char *msg)
400 _rl_errmsg ("warning: %s", msg);
402 #endif
404 #if defined (_AIX)
405 void
406 setopost (TIOTYPE *tp)
408 if ((tp->c_oflag & OPOST) == 0)
410 _rl_errmsg ("warning: turning on OPOST for terminal\r");
411 tp->c_oflag |= OPOST|ONLCR;
414 #endif
416 static int
417 _get_tty_settings (int tty, TIOTYPE *tiop)
419 int ioctl_ret;
421 while (1)
423 ioctl_ret = GETATTR (tty, tiop);
424 if (ioctl_ret < 0)
426 if (errno != EINTR)
427 return -1;
428 else
429 continue;
431 if (OUTPUT_BEING_FLUSHED (tiop))
433 #if defined (FLUSHO)
434 _rl_errmsg ("warning: turning off output flushing");
435 tiop->c_lflag &= ~FLUSHO;
436 break;
437 #else
438 continue;
439 #endif
441 break;
444 return 0;
447 static int
448 get_tty_settings (int tty, TIOTYPE *tiop)
450 set_winsize (tty);
452 errno = 0;
453 if (_get_tty_settings (tty, tiop) < 0)
454 return -1;
456 #if defined (_AIX)
457 setopost(tiop);
458 #endif
460 return 0;
463 static int
464 _set_tty_settings (int tty, TIOTYPE *tiop)
466 while (SETATTR (tty, tiop) < 0)
468 if (errno != EINTR)
469 return -1;
470 errno = 0;
472 return 0;
475 static int
476 set_tty_settings (int tty, TIOTYPE *tiop)
478 if (_set_tty_settings (tty, tiop) < 0)
479 return -1;
481 #if 0
483 #if defined (TERMIOS_TTY_DRIVER)
484 # if defined (__ksr1__)
485 if (ksrflow)
487 ksrflow = 0;
488 tcflow (tty, TCOON);
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 */
497 #endif /* 0 */
499 return 0;
502 static void
503 prepare_terminal_settings (int meta_flag, TIOTYPE oldtio, TIOTYPE *tiop)
505 int sc;
506 Keymap kmap;
508 _rl_echoing_p = (oldtio.c_lflag & ECHO);
509 #if defined (ECHOCTL)
510 _rl_echoctl = (oldtio.c_lflag & ECHOCTL);
511 #endif
513 tiop->c_lflag &= ~(ICANON | ECHO);
515 if ((unsigned char) oldtio.c_cc[VEOF] != (unsigned char) _POSIX_VDISABLE)
516 _rl_eof_char = oldtio.c_cc[VEOF];
518 #if defined (USE_XON_XOFF)
519 #if defined (IXANY)
520 tiop->c_iflag &= ~(IXON | IXANY);
521 #else
522 /* `strict' Posix systems do not define IXANY. */
523 tiop->c_iflag &= ~IXON;
524 #endif /* IXANY */
525 #endif /* USE_XON_XOFF */
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);
531 /* Make sure we differentiate between CR and NL on input. */
532 tiop->c_iflag &= ~(ICRNL | INLCR);
534 #if !defined (HANDLE_SIGNALS)
535 tiop->c_lflag &= ~ISIG;
536 #else
537 tiop->c_lflag |= ISIG;
538 #endif
540 tiop->c_cc[VMIN] = 1;
541 tiop->c_cc[VTIME] = 0;
543 #if defined (FLUSHO)
544 if (OUTPUT_BEING_FLUSHED (tiop))
546 tiop->c_lflag &= ~FLUSHO;
547 oldtio.c_lflag &= ~FLUSHO;
549 #endif
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)
556 #if defined (VLNEXT)
557 tiop->c_cc[VLNEXT] = _POSIX_VDISABLE;
558 #endif
560 #if defined (VDSUSP)
561 tiop->c_cc[VDSUSP] = _POSIX_VDISABLE;
562 #endif
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 */
578 #endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */
580 #endif /* !NEW_TTY_DRIVER */
582 /* Put the terminal in CBREAK mode so that we can detect key presses. */
583 #if defined (NO_TTY_DRIVER)
584 void
585 rl_prep_terminal (int meta_flag)
587 _rl_echoing_p = 1;
590 void
591 rl_deprep_terminal (void)
595 #else /* ! NO_TTY_DRIVER */
596 void
597 rl_prep_terminal (int meta_flag)
599 int tty, nprep;
600 TIOTYPE tio;
602 if (terminal_prepped)
603 return;
605 /* Try to keep this function from being INTerrupted. */
606 _rl_block_sigint ();
608 tty = rl_instream ? fileno (rl_instream) : fileno (stdin);
610 if (get_tty_settings (tty, &tio) < 0)
612 #if defined (ENOTSUP)
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)
616 #else
617 if (errno == ENOTTY || errno == EINVAL)
618 #endif
619 _rl_echoing_p = 1; /* XXX */
621 _rl_release_sigint ();
622 return;
625 otio = tio;
627 if (_rl_bind_stty_chars)
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);
638 save_tty_chars (&otio);
639 RL_SETSTATE(RL_STATE_TTYCSAVED);
640 if (_rl_bind_stty_chars)
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)
646 _rl_bind_tty_special_chars (vi_insertion_keymap, tio);
647 else
648 #endif
649 _rl_bind_tty_special_chars (_rl_keymap, tio);
652 prepare_terminal_settings (meta_flag, otio, &tio);
654 if (set_tty_settings (tty, &tio) < 0)
656 _rl_release_sigint ();
657 return;
660 if (_rl_enable_keypad)
661 _rl_control_keypad (1);
663 nprep = TPX_PREPPED;
665 if (_rl_enable_bracketed_paste)
667 fprintf (rl_outstream, BRACK_PASTE_INIT);
668 nprep |= TPX_BRACKPASTE;
671 fflush (rl_outstream);
672 terminal_prepped = nprep;
673 RL_SETSTATE(RL_STATE_TERMPREPPED);
675 _rl_release_sigint ();
678 /* Restore the terminal's normal settings and modes. */
679 void
680 rl_deprep_terminal (void)
682 int tty;
684 if (terminal_prepped == 0)
685 return;
687 /* Try to keep this function from being interrupted. */
688 _rl_block_sigint ();
690 tty = rl_instream ? fileno (rl_instream) : fileno (stdin);
692 if (terminal_prepped & TPX_BRACKPASTE)
694 fprintf (rl_outstream, BRACK_PASTE_FINI);
695 if (_rl_eof_found)
696 fprintf (rl_outstream, "\n");
699 if (_rl_enable_keypad)
700 _rl_control_keypad (0);
702 fflush (rl_outstream);
704 if (set_tty_settings (tty, &otio) < 0)
706 _rl_release_sigint ();
707 return;
710 terminal_prepped = 0;
711 RL_UNSETSTATE(RL_STATE_TERMPREPPED);
713 _rl_release_sigint ();
715 #endif /* !NO_TTY_DRIVER */
717 /* Set readline's idea of whether or not it is echoing output to the terminal,
718 returning the old value. */
720 rl_tty_set_echoing (int u)
722 int o;
724 o = _rl_echoing_p;
725 _rl_echoing_p = u;
726 return o;
729 /* **************************************************************** */
730 /* */
731 /* Bogus Flow Control */
732 /* */
733 /* **************************************************************** */
736 rl_restart_output (int count, int key)
738 #if defined (__MINGW32__)
739 return 0;
740 #else /* !__MING32__ */
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 */
750 #else /* !TIOCSTART */
751 # if defined (TERMIOS_TTY_DRIVER)
752 # if defined (__ksr1__)
753 if (ksrflow)
755 ksrflow = 0;
756 tcflow (fildes, TCOON);
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 */
768 return 0;
769 #endif /* !__MINGW32__ */
773 rl_stop_output (int count, int key)
775 #if defined (__MINGW32__)
776 return 0;
777 #else
779 int fildes = fileno (rl_instream);
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 */
800 return 0;
801 #endif /* !__MINGW32__ */
804 /* **************************************************************** */
805 /* */
806 /* Default Key Bindings */
807 /* */
808 /* **************************************************************** */
810 #if !defined (NO_TTY_DRIVER)
811 #define SET_SPECIAL(sc, func) set_special_char(kmap, &ttybuff, sc, func)
812 #endif
814 #if defined (NO_TTY_DRIVER)
816 #define SET_SPECIAL(sc, func)
817 #define RESET_SPECIAL(c)
819 #elif defined (NEW_TTY_DRIVER)
820 static void
821 set_special_char (Keymap kmap, TIOTYPE *tiop, int sc, rl_command_func_t *func)
823 if (sc != -1 && kmap[(unsigned char)sc].type == ISFUNC)
824 kmap[(unsigned char)sc].function = func;
827 #define RESET_SPECIAL(c) \
828 if (c != -1 && kmap[(unsigned char)c].type == ISFUNC) \
829 kmap[(unsigned char)c].function = rl_insert;
831 static void
832 _rl_bind_tty_special_chars (Keymap kmap, TIOTYPE ttybuff)
834 if (ttybuff.flags & SGTTY_SET)
836 SET_SPECIAL (ttybuff.sgttyb.sg_erase, rl_rubout);
837 SET_SPECIAL (ttybuff.sgttyb.sg_kill, rl_unix_line_discard);
840 # if defined (TIOCGLTC)
841 if (ttybuff.flags & LTCHARS_SET)
843 SET_SPECIAL (ttybuff.ltchars.t_werasc, rl_unix_word_rubout);
844 SET_SPECIAL (ttybuff.ltchars.t_lnextc, rl_quoted_insert);
846 # endif /* TIOCGLTC */
849 #else /* !NEW_TTY_DRIVER */
850 static void
851 set_special_char (Keymap kmap, TIOTYPE *tiop, int sc, rl_command_func_t *func)
853 unsigned char uc;
855 uc = tiop->c_cc[sc];
856 if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC)
857 kmap[uc].function = func;
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;
865 static void
866 _rl_bind_tty_special_chars (Keymap kmap, TIOTYPE ttybuff)
868 SET_SPECIAL (VERASE, rl_rubout);
869 SET_SPECIAL (VKILL, rl_unix_line_discard);
871 # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
872 SET_SPECIAL (VLNEXT, rl_quoted_insert);
873 # endif /* VLNEXT && TERMIOS_TTY_DRIVER */
875 # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
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
881 SET_SPECIAL (VWERASE, rl_unix_word_rubout);
882 # endif /* VWERASE && TERMIOS_TTY_DRIVER */
885 #endif /* !NEW_TTY_DRIVER */
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. */
889 void
890 rltty_set_default_bindings (Keymap kmap)
892 #if !defined (NO_TTY_DRIVER)
893 TIOTYPE ttybuff;
894 int tty;
896 tty = fileno (rl_instream);
898 if (get_tty_settings (tty, &ttybuff) == 0)
899 _rl_bind_tty_special_chars (kmap, ttybuff);
900 #endif
903 /* New public way to set the system default editing chars to their readline
904 equivalents. */
905 void
906 rl_tty_set_default_bindings (Keymap kmap)
908 rltty_set_default_bindings (kmap);
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 (Keymap kmap)
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;
922 RESET_SPECIAL (_rl_tty_chars.t_erase);
923 RESET_SPECIAL (_rl_tty_chars.t_kill);
925 # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
926 RESET_SPECIAL (_rl_tty_chars.t_lnext);
927 # endif /* VLNEXT && TERMIOS_TTY_DRIVER */
929 # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
930 RESET_SPECIAL (_rl_tty_chars.t_werase);
931 # endif /* VWERASE && TERMIOS_TTY_DRIVER */
934 #if defined (HANDLE_SIGNALS)
936 #if defined (NEW_TTY_DRIVER) || defined (NO_TTY_DRIVER)
938 _rl_disable_tty_signals (void)
940 return 0;
944 _rl_restore_tty_signals (void)
946 return 0;
948 #else
950 static TIOTYPE sigstty, nosigstty;
951 static int tty_sigs_disabled = 0;
954 _rl_disable_tty_signals (void)
956 if (tty_sigs_disabled)
957 return 0;
959 if (_get_tty_settings (fileno (rl_instream), &sigstty) < 0)
960 return -1;
962 nosigstty = sigstty;
964 nosigstty.c_lflag &= ~ISIG;
965 nosigstty.c_iflag &= ~IXON;
967 if (_set_tty_settings (fileno (rl_instream), &nosigstty) < 0)
968 return (_set_tty_settings (fileno (rl_instream), &sigstty));
970 tty_sigs_disabled = 1;
971 return 0;
975 _rl_restore_tty_signals (void)
977 int r;
979 if (tty_sigs_disabled == 0)
980 return 0;
982 r = _set_tty_settings (fileno (rl_instream), &sigstty);
984 if (r == 0)
985 tty_sigs_disabled = 0;
987 return r;
989 #endif /* !NEW_TTY_DRIVER */
991 #endif /* HANDLE_SIGNALS */