OSDN Git Service

readline:
[pf3gnuchains/pf3gnuchains4x.git] / readline / rltty.c
1 /* rltty.c -- functions to prepare and restore the terminal for readline's
2    use. */
3
4 /* Copyright (C) 1992 Free Software Foundation, Inc.
5
6    This file is part of the GNU Readline Library, a library for
7    reading lines of text with interactive input and history editing.
8
9    The GNU Readline Library is free software; you can redistribute it
10    and/or modify it under the terms of the GNU General Public License
11    as published by the Free Software Foundation; either version 2, or
12    (at your option) any later version.
13
14    The GNU Readline Library is distributed in the hope that it will be
15    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    The GNU General Public License is often shipped with GNU software, and
20    is generally kept in a file called COPYING or LICENSE.  If you do not
21    have a copy of the license, write to the Free Software Foundation,
22    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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
40 #if defined (GWINSZ_IN_SYS_IOCTL)
41 #  include <sys/ioctl.h>
42 #endif /* GWINSZ_IN_SYS_IOCTL */
43
44 #include "rltty.h"
45 #include "readline.h"
46 #include "rlprivate.h"
47
48 #if !defined (errno)
49 extern int errno;
50 #endif /* !errno */
51
52 VFunction *rl_prep_term_function = rl_prep_terminal;
53 VFunction *rl_deprep_term_function = rl_deprep_terminal;
54
55 /* **************************************************************** */
56 /*                                                                  */
57 /*                         Signal Management                        */
58 /*                                                                  */
59 /* **************************************************************** */
60
61 #if defined (HAVE_POSIX_SIGNALS)
62 static sigset_t sigint_set, sigint_oset;
63 #else /* !HAVE_POSIX_SIGNALS */
64 #  if defined (HAVE_BSD_SIGNALS)
65 static int sigint_oldmask;
66 #  endif /* HAVE_BSD_SIGNALS */
67 #endif /* !HAVE_POSIX_SIGNALS */
68
69 static int sigint_blocked;
70
71 /* Cause SIGINT to not be delivered until the corresponding call to
72    release_sigint(). */
73 static void
74 block_sigint ()
75 {
76   if (sigint_blocked)
77     return;
78
79 #if defined (HAVE_POSIX_SIGNALS)
80   sigemptyset (&sigint_set);
81   sigemptyset (&sigint_oset);
82   sigaddset (&sigint_set, SIGINT);
83   sigprocmask (SIG_BLOCK, &sigint_set, &sigint_oset);
84 #else /* !HAVE_POSIX_SIGNALS */
85 #  if defined (HAVE_BSD_SIGNALS)
86   sigint_oldmask = sigblock (sigmask (SIGINT));
87 #  else /* !HAVE_BSD_SIGNALS */
88 #    if defined (HAVE_USG_SIGHOLD)
89   sighold (SIGINT);
90 #    endif /* HAVE_USG_SIGHOLD */
91 #  endif /* !HAVE_BSD_SIGNALS */
92 #endif /* !HAVE_POSIX_SIGNALS */
93
94   sigint_blocked = 1;
95 }
96
97 /* Allow SIGINT to be delivered. */
98 static void
99 release_sigint ()
100 {
101   if (sigint_blocked == 0)
102     return;
103
104 #if defined (HAVE_POSIX_SIGNALS)
105   sigprocmask (SIG_SETMASK, &sigint_oset, (sigset_t *)NULL);
106 #else
107 #  if defined (HAVE_BSD_SIGNALS)
108   sigsetmask (sigint_oldmask);
109 #  else /* !HAVE_BSD_SIGNALS */
110 #    if defined (HAVE_USG_SIGHOLD)
111   sigrelse (SIGINT);
112 #    endif /* HAVE_USG_SIGHOLD */
113 #  endif /* !HAVE_BSD_SIGNALS */
114 #endif /* !HAVE_POSIX_SIGNALS */
115
116   sigint_blocked = 0;
117 }
118
119 /* **************************************************************** */
120 /*                                                                  */
121 /*                    Saving and Restoring the TTY                  */
122 /*                                                                  */
123 /* **************************************************************** */
124
125 /* Non-zero means that the terminal is in a prepped state. */
126 static int terminal_prepped;
127
128 static _RL_TTY_CHARS _rl_tty_chars, _rl_last_tty_chars;
129
130 /* If non-zero, means that this process has called tcflow(fd, TCOOFF)
131    and output is suspended. */
132 #if defined (__ksr1__)
133 static int ksrflow;
134 #endif
135
136 /* Dummy call to force a backgrounded readline to stop before it tries
137    to get the tty settings. */
138 static void
139 set_winsize (tty)
140      int tty;
141 {
142 #if defined (TIOCGWINSZ)
143   struct winsize w;
144
145   if (ioctl (tty, TIOCGWINSZ, &w) == 0)
146       (void) ioctl (tty, TIOCSWINSZ, &w);
147 #endif /* TIOCGWINSZ */
148 }
149
150 #if defined (NEW_TTY_DRIVER)
151
152 /* Values for the `flags' field of a struct bsdtty.  This tells which
153    elements of the struct bsdtty have been fetched from the system and
154    are valid. */
155 #define SGTTY_SET       0x01
156 #define LFLAG_SET       0x02
157 #define TCHARS_SET      0x04
158 #define LTCHARS_SET     0x08
159
160 struct bsdtty {
161   struct sgttyb sgttyb; /* Basic BSD tty driver information. */
162   int lflag;            /* Local mode flags, like LPASS8. */
163 #if defined (TIOCGETC)
164   struct tchars tchars; /* Terminal special characters, including ^S and ^Q. */
165 #endif
166 #if defined (TIOCGLTC)
167   struct ltchars ltchars; /* 4.2 BSD editing characters */
168 #endif
169   int flags;            /* Bitmap saying which parts of the struct are valid. */
170 };
171
172 #define TIOTYPE struct bsdtty
173
174 static TIOTYPE otio;
175
176 static void
177 save_tty_chars (tiop)
178      TIOTYPE *tiop;
179 {
180   _rl_last_tty_chars = _rl_tty_chars;
181
182   if (tiop->flags & SGTTY_SET)
183     {
184       _rl_tty_chars.t_erase = tiop->sgttyb.sg_erase;
185       _rl_tty_chars.t_kill = tiop->sgttyb.sg_kill;
186     }
187
188   if (tiop->flags & TCHARS_SET)
189     {
190       _rl_tty_chars.t_intr = tiop->tchars.t_intrc;
191       _rl_tty_chars.t_quit = tiop->tchars.t_quitc;
192       _rl_tty_chars.t_start = tiop->tchars.t_startc;
193       _rl_tty_chars.t_stop = tiop->tchars.t_stopc
194       _rl_tty_chars.t_eof = tiop->tchars.t_eofc;
195       _rl_tty_chars.t_eol = '\n';
196       _rl_tty_chars.t_eol2 = tiop->tchars.t_brkc;
197     }
198
199   if (tiop->flags & LTCHARS_SET)
200     {
201       _rl_tty_chars.t_susp = tiop->ltchars.t_suspc;
202       _rl_tty_chars.t_dsusp = tiop->ltchars.t_dsuspc;
203       _rl_tty_chars.t_reprint = tiop->ltchars.t_rprntc;
204       _rl_tty_chars.t_flush = tiop->ltchars.t_flushc;
205       _rl_tty_chars.t_werase = tiop->ltchars.t_werasc;
206       _rl_tty_chars.t_lnext = tiop->ltchars.t_lnextc;
207     }
208
209   _rl_tty_chars.t_status = -1;
210 }
211
212 static int
213 get_tty_settings (tty, tiop)
214      int tty;
215      TIOTYPE *tiop;
216 {
217 #if defined (TIOCGWINSZ)
218   set_winsize (tty);
219 #endif
220
221   tiop->flags = tiop->lflag = 0;
222
223   ioctl (tty, TIOCGETP, &(tiop->sgttyb));
224   tiop->flags |= SGTTY_SET;
225
226 #if defined (TIOCLGET)
227   ioctl (tty, TIOCLGET, &(tiop->lflag));
228   tiop->flags |= LFLAG_SET;
229 #endif
230
231 #if defined (TIOCGETC)
232   ioctl (tty, TIOCGETC, &(tiop->tchars));
233   tiop->flags |= TCHARS_SET;
234 #endif
235
236 #if defined (TIOCGLTC)
237   ioctl (tty, TIOCGLTC, &(tiop->ltchars));
238   tiop->flags |= LTCHARS_SET;
239 #endif
240
241   return 0;
242 }
243
244 static int
245 set_tty_settings (tty, tiop)
246      int tty;
247      TIOTYPE *tiop;
248 {
249   if (tiop->flags & SGTTY_SET)
250     {
251       ioctl (tty, TIOCSETN, &(tiop->sgttyb));
252       tiop->flags &= ~SGTTY_SET;
253     }
254   readline_echoing_p = 1;
255
256 #if defined (TIOCLSET)
257   if (tiop->flags & LFLAG_SET)
258     {
259       ioctl (tty, TIOCLSET, &(tiop->lflag));
260       tiop->flags &= ~LFLAG_SET;
261     }
262 #endif
263
264 #if defined (TIOCSETC)
265   if (tiop->flags & TCHARS_SET)
266     {
267       ioctl (tty, TIOCSETC, &(tiop->tchars));
268       tiop->flags &= ~TCHARS_SET;
269     }
270 #endif
271
272 #if defined (TIOCSLTC)
273   if (tiop->flags & LTCHARS_SET)
274     {
275       ioctl (tty, TIOCSLTC, &(tiop->ltchars));
276       tiop->flags &= ~LTCHARS_SET;
277     }
278 #endif
279
280   return 0;
281 }
282
283 static void
284 prepare_terminal_settings (meta_flag, otio, tiop)
285      int meta_flag;
286      TIOTYPE otio, *tiop;
287 {
288   readline_echoing_p = (otio.sgttyb.sg_flags & ECHO);
289
290   /* Copy the original settings to the structure we're going to use for
291      our settings. */
292   tiop->sgttyb = otio.sgttyb;
293   tiop->lflag = otio.lflag;
294 #if defined (TIOCGETC)
295   tiop->tchars = otio.tchars;
296 #endif
297 #if defined (TIOCGLTC)
298   tiop->ltchars = otio.ltchars;
299 #endif
300   tiop->flags = otio.flags;
301
302   /* First, the basic settings to put us into character-at-a-time, no-echo
303      input mode. */
304   tiop->sgttyb.sg_flags &= ~(ECHO | CRMOD);
305   tiop->sgttyb.sg_flags |= CBREAK;
306
307   /* If this terminal doesn't care how the 8th bit is used, then we can
308      use it for the meta-key.  If only one of even or odd parity is
309      specified, then the terminal is using parity, and we cannot. */
310 #if !defined (ANYP)
311 #  define ANYP (EVENP | ODDP)
312 #endif
313   if (((otio.sgttyb.sg_flags & ANYP) == ANYP) ||
314       ((otio.sgttyb.sg_flags & ANYP) == 0))
315     {
316       tiop->sgttyb.sg_flags |= ANYP;
317
318       /* Hack on local mode flags if we can. */
319 #if defined (TIOCLGET)
320 #  if defined (LPASS8)
321       tiop->lflag |= LPASS8;
322 #  endif /* LPASS8 */
323 #endif /* TIOCLGET */
324     }
325
326 #if defined (TIOCGETC)
327 #  if defined (USE_XON_XOFF)
328   /* Get rid of terminal output start and stop characters. */
329   tiop->tchars.t_stopc = -1; /* C-s */
330   tiop->tchars.t_startc = -1; /* C-q */
331
332   /* If there is an XON character, bind it to restart the output. */
333   if (otio.tchars.t_startc != -1)
334     rl_bind_key (otio.tchars.t_startc, rl_restart_output);
335 #  endif /* USE_XON_XOFF */
336
337   /* If there is an EOF char, bind _rl_eof_char to it. */
338   if (otio.tchars.t_eofc != -1)
339     _rl_eof_char = otio.tchars.t_eofc;
340
341 #  if defined (NO_KILL_INTR)
342   /* Get rid of terminal-generated SIGQUIT and SIGINT. */
343   tiop->tchars.t_quitc = -1; /* C-\ */
344   tiop->tchars.t_intrc = -1; /* C-c */
345 #  endif /* NO_KILL_INTR */
346 #endif /* TIOCGETC */
347
348 #if defined (TIOCGLTC)
349   /* Make the interrupt keys go away.  Just enough to make people happy. */
350   tiop->ltchars.t_dsuspc = -1;  /* C-y */
351   tiop->ltchars.t_lnextc = -1;  /* C-v */
352 #endif /* TIOCGLTC */
353 }
354
355 #else  /* !defined (NEW_TTY_DRIVER) */
356
357 #if !defined (VMIN)
358 #  define VMIN VEOF
359 #endif
360
361 #if !defined (VTIME)
362 #  define VTIME VEOL
363 #endif
364
365 #if defined (TERMIOS_TTY_DRIVER)
366 #  define TIOTYPE struct termios
367 #  define DRAIN_OUTPUT(fd)      tcdrain (fd)
368 #  define GETATTR(tty, tiop)    (tcgetattr (tty, tiop))
369 #  ifdef M_UNIX
370 #    define SETATTR(tty, tiop)  (tcsetattr (tty, TCSANOW, tiop))
371 #  else
372 #    define SETATTR(tty, tiop)  (tcsetattr (tty, TCSADRAIN, tiop))
373 #  endif /* !M_UNIX */
374 #else
375 #  define TIOTYPE struct termio
376 #  define DRAIN_OUTPUT(fd)
377 #  define GETATTR(tty, tiop)    (ioctl (tty, TCGETA, tiop))
378 #  define SETATTR(tty, tiop)    (ioctl (tty, TCSETA, tiop))
379 #endif /* !TERMIOS_TTY_DRIVER */
380
381 static TIOTYPE otio;
382
383 #if defined (FLUSHO)
384 #  define OUTPUT_BEING_FLUSHED(tp)  (tp->c_lflag & FLUSHO)
385 #else
386 #  define OUTPUT_BEING_FLUSHED(tp)  0
387 #endif
388
389 static void
390 save_tty_chars (tiop)
391      TIOTYPE *tiop;
392 {
393   _rl_last_tty_chars = _rl_tty_chars;
394
395   _rl_tty_chars.t_eof = tiop->c_cc[VEOF];
396   _rl_tty_chars.t_eol = tiop->c_cc[VEOL];
397 #ifdef VEOL2
398   _rl_tty_chars.t_eol2 = tiop->c_cc[VEOL2];
399 #endif
400   _rl_tty_chars.t_erase = tiop->c_cc[VERASE];
401 #ifdef VWERASE
402   _rl_tty_chars.t_werase = tiop->c_cc[VWERASE];
403 #endif
404   _rl_tty_chars.t_kill = tiop->c_cc[VKILL];
405 #ifdef VREPRINT
406   _rl_tty_chars.t_reprint = tiop->c_cc[VREPRINT];
407 #endif
408   _rl_tty_chars.t_intr = tiop->c_cc[VINTR];
409   _rl_tty_chars.t_quit = tiop->c_cc[VQUIT];
410 #ifdef VSUSP
411   _rl_tty_chars.t_susp = tiop->c_cc[VSUSP];
412 #endif
413 #ifdef VDSUSP
414   _rl_tty_chars.t_dsusp = tiop->c_cc[VDSUSP];
415 #endif
416 #ifdef VSTART
417   _rl_tty_chars.t_start = tiop->c_cc[VSTART];
418 #endif
419 #ifdef VSTOP
420   _rl_tty_chars.t_stop = tiop->c_cc[VSTOP];
421 #endif
422 #ifdef VLNEXT
423   _rl_tty_chars.t_lnext = tiop->c_cc[VLNEXT];
424 #endif
425 #ifdef VDISCARD
426   _rl_tty_chars.t_flush = tiop->c_cc[VDISCARD];
427 #endif
428 #ifdef VSTATUS
429   _rl_tty_chars.t_status = tiop->c_cc[VSTATUS];
430 #endif
431 }
432
433 #if defined (_AIX) || defined (_AIX41)
434 /* Currently this is only used on AIX */
435 static void
436 rltty_warning (msg)
437      char *msg;
438 {
439   fprintf (stderr, "readline: warning: %s\n", msg);
440 }
441 #endif
442
443 #if defined (_AIX)
444 void
445 setopost(tp)
446 TIOTYPE *tp;
447 {
448   if ((tp->c_oflag & OPOST) == 0)
449     {
450       rltty_warning ("turning on OPOST for terminal\r");
451       tp->c_oflag |= OPOST|ONLCR;
452     }
453 }
454 #endif
455
456 static int
457 _get_tty_settings (tty, tiop)
458      int tty;
459      TIOTYPE *tiop;
460 {
461   int ioctl_ret;
462
463   while (1)
464     {
465       ioctl_ret = GETATTR (tty, tiop);
466       if (ioctl_ret < 0)
467         {
468           if (errno != EINTR)
469             return -1;
470           else
471             continue;
472         }
473       if (OUTPUT_BEING_FLUSHED (tiop))
474         {
475 #if defined (FLUSHO) && defined (_AIX41)
476           rltty_warning ("turning off output flushing");
477           tiop->c_lflag &= ~FLUSHO;
478           break;
479 #else
480           continue;
481 #endif
482         }
483       break;
484     }
485
486   return 0;
487 }
488
489 static int
490 get_tty_settings (tty, tiop)
491      int tty;
492      TIOTYPE *tiop;
493 {
494 #if defined (TIOCGWINSZ)
495   set_winsize (tty);
496 #endif
497
498   if (_get_tty_settings (tty, tiop) < 0)
499     return -1;
500
501 #if defined (_AIX)
502   setopost(tiop);
503 #endif
504
505   return 0;
506 }
507
508 static int
509 _set_tty_settings (tty, tiop)
510      int tty;
511      TIOTYPE *tiop;
512 {
513   while (SETATTR (tty, tiop) < 0)
514     {
515       if (errno != EINTR)
516         return -1;
517       errno = 0;
518     }
519   return 0;
520 }
521
522 static int
523 set_tty_settings (tty, tiop)
524      int tty;
525      TIOTYPE *tiop;
526 {
527   if (_set_tty_settings (tty, tiop) < 0)
528     return -1;
529     
530 #if 0
531
532 #if defined (TERMIOS_TTY_DRIVER)
533 #  if defined (__ksr1__)
534   if (ksrflow)
535     {
536       ksrflow = 0;
537       tcflow (tty, TCOON);
538     }
539 #  else /* !ksr1 */
540   tcflow (tty, TCOON);          /* Simulate a ^Q. */
541 #  endif /* !ksr1 */
542 #else
543   ioctl (tty, TCXONC, 1);       /* Simulate a ^Q. */
544 #endif /* !TERMIOS_TTY_DRIVER */
545
546 #endif /* 0 */
547
548   return 0;
549 }
550
551 static void
552 prepare_terminal_settings (meta_flag, otio, tiop)
553      int meta_flag;
554      TIOTYPE otio, *tiop;
555 {
556   readline_echoing_p = (otio.c_lflag & ECHO);
557
558   tiop->c_lflag &= ~(ICANON | ECHO);
559
560   if ((unsigned char) otio.c_cc[VEOF] != (unsigned char) _POSIX_VDISABLE)
561     _rl_eof_char = otio.c_cc[VEOF];
562
563 #if defined (USE_XON_XOFF)
564 #if defined (IXANY)
565   tiop->c_iflag &= ~(IXON | IXOFF | IXANY);
566 #else
567   /* `strict' Posix systems do not define IXANY. */
568   tiop->c_iflag &= ~(IXON | IXOFF);
569 #endif /* IXANY */
570 #endif /* USE_XON_XOFF */
571
572   /* Only turn this off if we are using all 8 bits. */
573   if (((tiop->c_cflag & CSIZE) == CS8) || meta_flag)
574     tiop->c_iflag &= ~(ISTRIP | INPCK);
575
576   /* Make sure we differentiate between CR and NL on input. */
577   tiop->c_iflag &= ~(ICRNL | INLCR);
578
579 #if !defined (HANDLE_SIGNALS)
580   tiop->c_lflag &= ~ISIG;
581 #else
582   tiop->c_lflag |= ISIG;
583 #endif
584
585   tiop->c_cc[VMIN] = 1;
586   tiop->c_cc[VTIME] = 0;
587
588 #if defined (FLUSHO)
589   if (OUTPUT_BEING_FLUSHED (tiop))
590     {
591       tiop->c_lflag &= ~FLUSHO;
592       otio.c_lflag &= ~FLUSHO;
593     }
594 #endif
595
596   /* Turn off characters that we need on Posix systems with job control,
597      just to be sure.  This includes ^Y and ^V.  This should not really
598      be necessary.  */
599 #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_VDISABLE)
600
601 #if defined (VLNEXT)
602   tiop->c_cc[VLNEXT] = _POSIX_VDISABLE;
603 #endif
604
605 #if defined (VDSUSP)
606   tiop->c_cc[VDSUSP] = _POSIX_VDISABLE;
607 #endif
608
609 #endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */
610 }
611 #endif  /* NEW_TTY_DRIVER */
612
613 /* Put the terminal in CBREAK mode so that we can detect key presses. */
614 void
615 rl_prep_terminal (meta_flag)
616      int meta_flag;
617 {
618   int tty;
619   TIOTYPE tio;
620
621   if (terminal_prepped)
622     return;
623
624   /* Try to keep this function from being INTerrupted. */
625   block_sigint ();
626
627   tty = fileno (rl_instream);
628
629   if (get_tty_settings (tty, &tio) < 0)
630     {
631       release_sigint ();
632       return;
633     }
634
635   otio = tio;
636
637   save_tty_chars (&otio);
638
639   prepare_terminal_settings (meta_flag, otio, &tio);
640
641   if (set_tty_settings (tty, &tio) < 0)
642     {
643       release_sigint ();
644       return;
645     }
646
647   if (_rl_enable_keypad)
648     _rl_control_keypad (1);
649
650   fflush (rl_outstream);
651   terminal_prepped = 1;
652
653   release_sigint ();
654 }
655
656 /* Restore the terminal's normal settings and modes. */
657 void
658 rl_deprep_terminal ()
659 {
660   int tty;
661
662   if (!terminal_prepped)
663     return;
664
665   /* Try to keep this function from being interrupted. */
666   block_sigint ();
667
668   tty = fileno (rl_instream);
669
670   if (_rl_enable_keypad)
671     _rl_control_keypad (0);
672
673   fflush (rl_outstream);
674
675   if (set_tty_settings (tty, &otio) < 0)
676     {
677       release_sigint ();
678       return;
679     }
680
681   terminal_prepped = 0;
682
683   release_sigint ();
684 }
685 \f
686 /* **************************************************************** */
687 /*                                                                  */
688 /*                      Bogus Flow Control                          */
689 /*                                                                  */
690 /* **************************************************************** */
691
692 int
693 rl_restart_output (count, key)
694      int count, key;
695 {
696   int fildes = fileno (rl_outstream);
697 #if defined (TIOCSTART)
698 #if defined (apollo)
699   ioctl (&fildes, TIOCSTART, 0);
700 #else
701   ioctl (fildes, TIOCSTART, 0);
702 #endif /* apollo */
703
704 #else /* !TIOCSTART */
705 #  if defined (TERMIOS_TTY_DRIVER)
706 #    if defined (__ksr1__)
707   if (ksrflow)
708     {
709       ksrflow = 0;
710       tcflow (fildes, TCOON);
711     }
712 #    else /* !ksr1 */
713   tcflow (fildes, TCOON);               /* Simulate a ^Q. */
714 #    endif /* !ksr1 */
715 #  else /* !TERMIOS_TTY_DRIVER */
716 #    if defined (TCXONC)
717   ioctl (fildes, TCXONC, TCOON);
718 #    endif /* TCXONC */
719 #  endif /* !TERMIOS_TTY_DRIVER */
720 #endif /* !TIOCSTART */
721
722   return 0;
723 }
724
725 int
726 rl_stop_output (count, key)
727      int count, key;
728 {
729   int fildes = fileno (rl_instream);
730
731 #if defined (TIOCSTOP)
732 # if defined (apollo)
733   ioctl (&fildes, TIOCSTOP, 0);
734 # else
735   ioctl (fildes, TIOCSTOP, 0);
736 # endif /* apollo */
737 #else /* !TIOCSTOP */
738 # if defined (TERMIOS_TTY_DRIVER)
739 #  if defined (__ksr1__)
740   ksrflow = 1;
741 #  endif /* ksr1 */
742   tcflow (fildes, TCOOFF);
743 # else
744 #   if defined (TCXONC)
745   ioctl (fildes, TCXONC, TCOON);
746 #   endif /* TCXONC */
747 # endif /* !TERMIOS_TTY_DRIVER */
748 #endif /* !TIOCSTOP */
749
750   return 0;
751 }
752
753 /* **************************************************************** */
754 /*                                                                  */
755 /*                      Default Key Bindings                        */
756 /*                                                                  */
757 /* **************************************************************** */
758 void
759 rltty_set_default_bindings (kmap)
760      Keymap kmap;
761 {
762   TIOTYPE ttybuff;
763   int tty = fileno (rl_instream);
764
765 #if defined (NEW_TTY_DRIVER)
766
767 #define SET_SPECIAL(sc, func) \
768   do \
769     { \
770       int ic; \
771       ic = sc; \
772       if (ic != -1 && kmap[ic].type == ISFUNC) \
773         kmap[ic].function = func; \
774     } \
775   while (0)
776
777   if (get_tty_settings (tty, &ttybuff) == 0)
778     {
779       if (ttybuff.flags & SGTTY_SET)
780         {
781           SET_SPECIAL (ttybuff.sgttyb.sg_erase, rl_rubout);
782           SET_SPECIAL (ttybuff.sgttyb.sg_kill, rl_unix_line_discard);
783         }
784
785 #  if defined (TIOCGLTC)
786       if (ttybuff.flags & LTCHARS_SET)
787         {
788           SET_SPECIAL (ttybuff.ltchars.t_werasc, rl_unix_word_rubout);
789           SET_SPECIAL (ttybuff.ltchars.t_lnextc, rl_quoted_insert);
790         }
791 #  endif /* TIOCGLTC */
792     }
793
794 #else /* !NEW_TTY_DRIVER */
795
796 #define SET_SPECIAL(sc, func) \
797   do \
798     { \
799       unsigned char uc; \
800       uc = ttybuff.c_cc[sc]; \
801       if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) \
802         kmap[uc].function = func; \
803     } \
804   while (0)
805
806   if (get_tty_settings (tty, &ttybuff) == 0)
807     {
808       SET_SPECIAL (VERASE, rl_rubout);
809       SET_SPECIAL (VKILL, rl_unix_line_discard);
810
811 #  if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
812       SET_SPECIAL (VLNEXT, rl_quoted_insert);
813 #  endif /* VLNEXT && TERMIOS_TTY_DRIVER */
814
815 #  if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
816       SET_SPECIAL (VWERASE, rl_unix_word_rubout);
817 #  endif /* VWERASE && TERMIOS_TTY_DRIVER */
818     }
819 #endif /* !NEW_TTY_DRIVER */
820 }
821
822 #if defined (HANDLE_SIGNALS)
823
824 #if defined (NEW_TTY_DRIVER)
825 int
826 _rl_disable_tty_signals ()
827 {
828   return 0;
829 }
830
831 int
832 _rl_restore_tty_signals ()
833 {
834   return 0;
835 }
836 #else
837
838 static TIOTYPE sigstty, nosigstty;
839 static int tty_sigs_disabled = 0;
840
841 int
842 _rl_disable_tty_signals ()
843 {
844   if (tty_sigs_disabled)
845     return 0;
846
847   if (_get_tty_settings (fileno (rl_instream), &sigstty) < 0)
848     return -1;
849
850   nosigstty = sigstty;
851
852   nosigstty.c_lflag &= ~ISIG;
853
854   if (_set_tty_settings (fileno (rl_instream), &nosigstty) < 0)
855     return (_set_tty_settings (fileno (rl_instream), &sigstty));
856
857   tty_sigs_disabled = 1;
858   return 0;
859 }
860
861 int
862 _rl_restore_tty_signals ()
863 {
864   if (tty_sigs_disabled == 0)
865     return 0;
866
867   return (_set_tty_settings (fileno (rl_instream), &sigstty));
868 }
869 #endif /* !NEW_TTY_DRIVER */
870
871 #endif /* HANDLE_SIGNALS */