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