1 /* rltty.c -- functions to prepare and restore the terminal for readline's
4 /* Copyright (C) 1992 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
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 1, or
12 (at your option) any later version.
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.
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 675 Mass Ave, Cambridge, MA 02139, USA. */
31 #if defined (HAVE_UNISTD_H)
33 #endif /* HAVE_UNISTD_H */
42 extern int readline_echoing_p
;
43 extern int _rl_eof_char
;
45 #if defined (__GO32__)
47 # undef HANDLE_SIGNALS
50 /* **************************************************************** */
52 /* Signal Management */
54 /* **************************************************************** */
56 #if defined (HAVE_POSIX_SIGNALS)
57 static sigset_t sigint_set
, sigint_oset
;
58 #else /* !HAVE_POSIX_SIGNALS */
59 # if defined (HAVE_BSD_SIGNALS)
60 static int sigint_oldmask
;
61 # endif /* HAVE_BSD_SIGNALS */
62 #endif /* !HAVE_POSIX_SIGNALS */
64 static int sigint_blocked
= 0;
66 /* Cause SIGINT to not be delivered until the corresponding call to
74 #if defined (HAVE_POSIX_SIGNALS)
75 sigemptyset (&sigint_set
);
76 sigemptyset (&sigint_oset
);
77 sigaddset (&sigint_set
, SIGINT
);
78 sigprocmask (SIG_BLOCK
, &sigint_set
, &sigint_oset
);
79 #else /* !HAVE_POSIX_SIGNALS */
80 # if defined (HAVE_BSD_SIGNALS)
81 sigint_oldmask
= sigblock (sigmask (SIGINT
));
82 # else /* !HAVE_BSD_SIGNALS */
83 # if defined (HAVE_USG_SIGHOLD)
85 # endif /* HAVE_USG_SIGHOLD */
86 # endif /* !HAVE_BSD_SIGNALS */
87 #endif /* !HAVE_POSIX_SIGNALS */
91 /* Allow SIGINT to be delivered. */
98 #if defined (HAVE_POSIX_SIGNALS)
99 sigprocmask (SIG_SETMASK
, &sigint_oset
, (sigset_t
*)NULL
);
101 # if defined (HAVE_BSD_SIGNALS)
102 sigsetmask (sigint_oldmask
);
103 # else /* !HAVE_BSD_SIGNALS */
104 # if defined (HAVE_USG_SIGHOLD)
106 # endif /* HAVE_USG_SIGHOLD */
107 # endif /* !HAVE_BSD_SIGNALS */
108 #endif /* !HAVE_POSIX_SIGNALS */
113 /* **************************************************************** */
115 /* Controlling the Meta Key */
117 /* **************************************************************** */
119 extern int term_has_meta
;
120 extern char *term_mm
;
121 extern char *term_mo
;
127 putc (c
, rl_outstream
);
130 /* Turn on/off the meta key depending on ON. */
132 control_meta_key (on
)
138 tputs (term_mm
, 1, outchar
);
139 else if (!on
&& term_mo
)
140 tputs (term_mo
, 1, outchar
);
144 /* **************************************************************** */
146 /* Saving and Restoring the TTY */
148 /* **************************************************************** */
150 /* Non-zero means that the terminal is in a prepped state. */
151 static int terminal_prepped
= 0;
153 /* If non-zero, means that this process has called tcflow(fd, TCOOFF)
154 and output is suspended. */
155 #if defined (__ksr1__)
156 static int ksrflow
= 0;
158 #if defined (NEW_TTY_DRIVER)
160 /* Values for the `flags' field of a struct bsdtty. This tells which
161 elements of the struct bsdtty have been fetched from the system and
163 #define SGTTY_SET 0x01
164 #define LFLAG_SET 0x02
165 #define TCHARS_SET 0x04
166 #define LTCHARS_SET 0x08
169 struct sgttyb sgttyb
; /* Basic BSD tty driver information. */
170 int lflag
; /* Local mode flags, like LPASS8. */
171 #if defined (TIOCGETC)
172 struct tchars tchars
; /* Terminal special characters, including ^S and ^Q. */
174 #if defined (TIOCGLTC)
175 struct ltchars ltchars
; /* 4.2 BSD editing characters */
177 int flags
; /* Bitmap saying which parts of the struct are valid. */
180 #define TIOTYPE struct bsdtty
185 get_tty_settings (tty
, tiop
)
189 tiop
->flags
= tiop
->lflag
= 0;
191 ioctl (tty
, TIOCGETP
, &(tiop
->sgttyb
));
192 tiop
->flags
|= SGTTY_SET
;
194 #if defined (TIOCLGET)
195 ioctl (tty
, TIOCLGET
, &(tiop
->lflag
));
196 tiop
->flags
|= LFLAG_SET
;
199 #if defined (TIOCGETC)
200 ioctl (tty
, TIOCGETC
, &(tiop
->tchars
));
201 tiop
->flags
|= TCHARS_SET
;
204 #if defined (TIOCGLTC)
205 ioctl (tty
, TIOCGLTC
, &(tiop
->ltchars
));
206 tiop
->flags
|= LTCHARS_SET
;
212 set_tty_settings (tty
, tiop
)
216 if (tiop
->flags
& SGTTY_SET
)
218 ioctl (tty
, TIOCSETN
, &(tiop
->sgttyb
));
219 tiop
->flags
&= ~SGTTY_SET
;
222 #if defined (TIOCLSET)
223 if (tiop
->flags
& LFLAG_SET
)
225 ioctl (tty
, TIOCLSET
, &(tiop
->lflag
));
226 tiop
->flags
&= ~LFLAG_SET
;
230 #if defined (TIOCSETC)
231 if (tiop
->flags
& TCHARS_SET
)
233 ioctl (tty
, TIOCSETC
, &(tiop
->tchars
));
234 tiop
->flags
&= ~TCHARS_SET
;
238 #if defined (TIOCSLTC)
239 if (tiop
->flags
& LTCHARS_SET
)
241 ioctl (tty
, TIOCSLTC
, &(tiop
->ltchars
));
242 tiop
->flags
&= ~LTCHARS_SET
;
250 prepare_terminal_settings (meta_flag
, otio
, tiop
)
254 #if !defined (__GO32__)
255 readline_echoing_p
= (otio
.sgttyb
.sg_flags
& ECHO
);
257 /* Copy the original settings to the structure we're going to use for
259 tiop
->sgttyb
= otio
.sgttyb
;
260 tiop
->lflag
= otio
.lflag
;
261 #if defined (TIOCGETC)
262 tiop
->tchars
= otio
.tchars
;
264 #if defined (TIOCGLTC)
265 tiop
->ltchars
= otio
.ltchars
;
267 tiop
->flags
= otio
.flags
;
269 /* First, the basic settings to put us into character-at-a-time, no-echo
271 tiop
->sgttyb
.sg_flags
&= ~(ECHO
| CRMOD
);
272 tiop
->sgttyb
.sg_flags
|= CBREAK
;
274 /* If this terminal doesn't care how the 8th bit is used, then we can
275 use it for the meta-key. If only one of even or odd parity is
276 specified, then the terminal is using parity, and we cannot. */
278 # define ANYP (EVENP | ODDP)
280 if (((otio
.sgttyb
.sg_flags
& ANYP
) == ANYP
) ||
281 ((otio
.sgttyb
.sg_flags
& ANYP
) == 0))
283 tiop
->sgttyb
.sg_flags
|= ANYP
;
285 /* Hack on local mode flags if we can. */
286 #if defined (TIOCLGET)
287 # if defined (LPASS8)
288 tiop
->lflag
|= LPASS8
;
290 #endif /* TIOCLGET */
293 #if defined (TIOCGETC)
294 # if defined (USE_XON_XOFF)
295 /* Get rid of terminal output start and stop characters. */
296 tiop
->tchars
.t_stopc
= -1; /* C-s */
297 tiop
->tchars
.t_startc
= -1; /* C-q */
299 /* If there is an XON character, bind it to restart the output. */
300 if (otio
.tchars
.t_startc
!= -1)
301 rl_bind_key (otio
.tchars
.t_startc
, rl_restart_output
);
302 # endif /* USE_XON_XOFF */
304 /* If there is an EOF char, bind _rl_eof_char to it. */
305 if (otio
.tchars
.t_eofc
!= -1)
306 _rl_eof_char
= otio
.tchars
.t_eofc
;
308 # if defined (NO_KILL_INTR)
309 /* Get rid of terminal-generated SIGQUIT and SIGINT. */
310 tiop
->tchars
.t_quitc
= -1; /* C-\ */
311 tiop
->tchars
.t_intrc
= -1; /* C-c */
312 # endif /* NO_KILL_INTR */
313 #endif /* TIOCGETC */
315 #if defined (TIOCGLTC)
316 /* Make the interrupt keys go away. Just enough to make people happy. */
317 tiop
->ltchars
.t_dsuspc
= -1; /* C-y */
318 tiop
->ltchars
.t_lnextc
= -1; /* C-v */
319 #endif /* TIOCGLTC */
320 #endif /* !__GO32__ */
322 #endif /* defined (NEW_TTY_DRIVER) */
324 #if !defined (NEW_TTY_DRIVER) && !defined(__GO32__)
334 #if defined (TERMIOS_TTY_DRIVER)
335 # define TIOTYPE struct termios
336 # define DRAIN_OUTPUT(fd) tcdrain (fd)
337 # define GETATTR(tty, tiop) (tcgetattr (tty, tiop))
338 # define SETATTR(tty, tiop) (tcsetattr (tty, TCSANOW, tiop))
340 # define TIOTYPE struct termio
341 # define DRAIN_OUTPUT(fd)
342 # define GETATTR(tty, tiop) (ioctl (tty, TCGETA, tiop))
343 # define SETATTR(tty, tiop) (ioctl (tty, TCSETA, tiop))
344 #endif /* !TERMIOS_TTY_DRIVER */
349 get_tty_settings (tty
, tiop
)
353 while (GETATTR (tty
, tiop
) < 0)
363 set_tty_settings (tty
, tiop
)
367 while (SETATTR (tty
, tiop
) < 0)
376 #if defined (TERMIOS_TTY_DRIVER)
377 # if defined (__ksr1__)
384 tcflow (tty
, TCOON
); /* Simulate a ^Q. */
387 ioctl (tty
, TCXONC
, 1); /* Simulate a ^Q. */
388 #endif /* !TERMIOS_TTY_DRIVER */
396 prepare_terminal_settings (meta_flag
, otio
, tiop
)
400 readline_echoing_p
= (otio
.c_lflag
& ECHO
);
402 tiop
->c_lflag
&= ~(ICANON
| ECHO
);
404 if ((unsigned char) otio
.c_cc
[VEOF
] != (unsigned char) _POSIX_VDISABLE
)
405 _rl_eof_char
= otio
.c_cc
[VEOF
];
407 #if defined (USE_XON_XOFF)
409 tiop
->c_iflag
&= ~(IXON
| IXOFF
| IXANY
);
411 /* `strict' Posix systems do not define IXANY. */
412 tiop
->c_iflag
&= ~(IXON
| IXOFF
);
414 #endif /* USE_XON_XOFF */
416 /* Only turn this off if we are using all 8 bits. */
417 if (((tiop
->c_cflag
& CSIZE
) == CS8
) || meta_flag
)
418 tiop
->c_iflag
&= ~(ISTRIP
| INPCK
);
420 /* Make sure we differentiate between CR and NL on input. */
421 tiop
->c_iflag
&= ~(ICRNL
| INLCR
);
423 #if !defined (HANDLE_SIGNALS)
424 tiop
->c_lflag
&= ~ISIG
;
426 tiop
->c_lflag
|= ISIG
;
429 tiop
->c_cc
[VMIN
] = 1;
430 tiop
->c_cc
[VTIME
] = 0;
432 /* Turn off characters that we need on Posix systems with job control,
433 just to be sure. This includes ^Y and ^V. This should not really
435 #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_VDISABLE)
438 tiop
->c_cc
[VLNEXT
] = _POSIX_VDISABLE
;
442 tiop
->c_cc
[VDSUSP
] = _POSIX_VDISABLE
;
445 #endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */
447 #endif /* !defined (NEW_TTY_DRIVER) && !defined(__GO32__) */
449 /* Put the terminal in CBREAK mode so that we can detect key presses. */
451 rl_prep_terminal (meta_flag
)
454 #if !defined (__GO32__)
455 int tty
= fileno (rl_instream
);
458 if (terminal_prepped
)
461 /* Try to keep this function from being INTerrupted. */
464 if (get_tty_settings (tty
, &tio
) < 0)
472 prepare_terminal_settings (meta_flag
, otio
, &tio
);
474 if (set_tty_settings (tty
, &tio
) < 0)
480 control_meta_key (1);
481 terminal_prepped
= 1;
484 #endif /* !__GO32__ */
487 /* Restore the terminal's normal settings and modes. */
489 rl_deprep_terminal ()
491 #if !defined (__GO32__)
492 int tty
= fileno (rl_instream
);
494 if (!terminal_prepped
)
497 /* Try to keep this function from being INTerrupted. */
500 if (set_tty_settings (tty
, &otio
) < 0)
505 #ifdef NEW_TTY_DRIVER
506 readline_echoing_p
= 1;
509 control_meta_key (0);
510 terminal_prepped
= 0;
513 #endif /* !__GO32__ */
516 /* **************************************************************** */
518 /* Bogus Flow Control */
520 /* **************************************************************** */
522 rl_restart_output (count
, key
)
525 int fildes
= fileno (rl_outstream
);
526 #if defined (TIOCSTART)
528 ioctl (&fildes
, TIOCSTART
, 0);
530 ioctl (fildes
, TIOCSTART
, 0);
533 #else /* !TIOCSTART */
534 # if defined (TERMIOS_TTY_DRIVER)
535 # if defined (__ksr1__)
539 tcflow (fildes
, TCOON
);
542 tcflow (fildes
, TCOON
); /* Simulate a ^Q. */
544 # else /* !TERMIOS_TTY_DRIVER */
545 # if defined (TCXONC)
546 ioctl (fildes
, TCXONC
, TCOON
);
548 # endif /* !TERMIOS_TTY_DRIVER */
549 #endif /* !TIOCSTART */
552 rl_stop_output (count
, key
)
555 int fildes
= fileno (rl_instream
);
557 #if defined (TIOCSTOP)
558 # if defined (apollo)
559 ioctl (&fildes
, TIOCSTOP
, 0);
561 ioctl (fildes
, TIOCSTOP
, 0);
563 #else /* !TIOCSTOP */
564 # if defined (TERMIOS_TTY_DRIVER)
565 # if defined (__ksr1__)
568 tcflow (fildes
, TCOOFF
);
570 # if defined (TCXONC)
571 ioctl (fildes
, TCXONC
, TCOON
);
573 # endif /* !TERMIOS_TTY_DRIVER */
574 #endif /* !TIOCSTOP */
577 /* **************************************************************** */
579 /* Default Key Bindings */
581 /* **************************************************************** */
582 #if !defined (__GO32__)
584 rltty_set_default_bindings (kmap
)
588 int tty
= fileno (rl_instream
);
590 #if defined (NEW_TTY_DRIVER)
592 if (get_tty_settings (tty
, &ttybuff
) == 0)
594 if (ttybuff
.flags
& SGTTY_SET
)
598 erase
= ttybuff
.sgttyb
.sg_erase
;
599 kill
= ttybuff
.sgttyb
.sg_kill
;
601 if (erase
!= -1 && kmap
[erase
].type
== ISFUNC
)
602 kmap
[erase
].function
= rl_rubout
;
604 if (kill
!= -1 && kmap
[kill
].type
== ISFUNC
)
605 kmap
[kill
].function
= rl_unix_line_discard
;
608 # if defined (TIOCGLTC)
610 if (ttybuff
.flags
& LTCHARS_SET
)
614 werase
= ttybuff
.ltchars
.t_werasc
;
615 nextc
= ttybuff
.ltchars
.t_lnextc
;
617 if (werase
!= -1 && kmap
[werase
].type
== ISFUNC
)
618 kmap
[werase
].function
= rl_unix_word_rubout
;
620 if (nextc
!= -1 && kmap
[nextc
].type
== ISFUNC
)
621 kmap
[nextc
].function
= rl_quoted_insert
;
624 # endif /* TIOCGLTC */
626 #else /* !NEW_TTY_DRIVER */
628 if (get_tty_settings (tty
, &ttybuff
) == 0)
630 unsigned char erase
, kill
;
632 erase
= ttybuff
.c_cc
[VERASE
];
633 kill
= ttybuff
.c_cc
[VKILL
];
635 if (erase
!= (unsigned char)_POSIX_VDISABLE
&&
636 kmap
[erase
].type
== ISFUNC
)
637 kmap
[erase
].function
= rl_rubout
;
639 if (kill
!= (unsigned char)_POSIX_VDISABLE
&&
640 kmap
[kill
].type
== ISFUNC
)
641 kmap
[kill
].function
= rl_unix_line_discard
;
643 # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
647 nextc
= ttybuff
.c_cc
[VLNEXT
];
649 if (nextc
!= (unsigned char)_POSIX_VDISABLE
&&
650 kmap
[nextc
].type
== ISFUNC
)
651 kmap
[nextc
].function
= rl_quoted_insert
;
653 # endif /* VLNEXT && TERMIOS_TTY_DRIVER */
655 # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
657 unsigned char werase
;
659 werase
= ttybuff
.c_cc
[VWERASE
];
661 if (werase
!= (unsigned char)_POSIX_VDISABLE
&&
662 kmap
[werase
].type
== ISFUNC
)
663 kmap
[werase
].function
= rl_unix_word_rubout
;
665 # endif /* VWERASE && TERMIOS_TTY_DRIVER */
667 #endif /* !NEW_TTY_DRIVER */
669 #endif /* !__GO32__ */
This page took 0.045443 seconds and 4 git commands to generate.