Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* rltty.c -- functions to prepare and restore the terminal for readline's |
| 2 | use. */ |
| 3 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 4 | /* Copyright (C) 1992-2005 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 8 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | (at your option) any later version. |
| 13 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | GNU General Public License for more details. |
| 18 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 23 | #define READLINE_LIBRARY |
| 24 | |
| 25 | #if defined (HAVE_CONFIG_H) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 26 | # include <config.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 39 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 40 | #if defined (GWINSZ_IN_SYS_IOCTL) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 41 | # include <sys/ioctl.h> |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 42 | #endif /* GWINSZ_IN_SYS_IOCTL */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 43 | |
| 44 | #include "rltty.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 45 | #include "readline.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 46 | #include "rlprivate.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 47 | |
| 48 | #if !defined (errno) |
| 49 | extern int errno; |
| 50 | #endif /* !errno */ |
| 51 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 52 | rl_vintfunc_t *rl_prep_term_function = rl_prep_terminal; |
| 53 | rl_voidfunc_t *rl_deprep_term_function = rl_deprep_terminal; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 54 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 55 | static void set_winsize PARAMS((int)); |
| 56 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 57 | /* **************************************************************** */ |
| 58 | /* */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 59 | /* Saving and Restoring the TTY */ |
| 60 | /* */ |
| 61 | /* **************************************************************** */ |
| 62 | |
| 63 | /* Non-zero means that the terminal is in a prepped state. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 64 | static int terminal_prepped; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 65 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 66 | static _RL_TTY_CHARS _rl_tty_chars, _rl_last_tty_chars; |
| 67 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 68 | /* If non-zero, means that this process has called tcflow(fd, TCOOFF) |
| 69 | and output is suspended. */ |
| 70 | #if defined (__ksr1__) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 71 | static int ksrflow; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 72 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 73 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 74 | /* Dummy call to force a backgrounded readline to stop before it tries |
| 75 | to get the tty settings. */ |
| 76 | static void |
| 77 | set_winsize (tty) |
| 78 | int tty; |
| 79 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 80 | #if defined (TIOCGWINSZ) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 81 | struct winsize w; |
| 82 | |
| 83 | if (ioctl (tty, TIOCGWINSZ, &w) == 0) |
| 84 | (void) ioctl (tty, TIOCSWINSZ, &w); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 85 | #endif /* TIOCGWINSZ */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 86 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 87 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 88 | #if defined (NO_TTY_DRIVER) |
| 89 | /* Nothing */ |
| 90 | #elif defined (NEW_TTY_DRIVER) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 91 | |
| 92 | /* Values for the `flags' field of a struct bsdtty. This tells which |
| 93 | elements of the struct bsdtty have been fetched from the system and |
| 94 | are valid. */ |
| 95 | #define SGTTY_SET 0x01 |
| 96 | #define LFLAG_SET 0x02 |
| 97 | #define TCHARS_SET 0x04 |
| 98 | #define LTCHARS_SET 0x08 |
| 99 | |
| 100 | struct bsdtty { |
| 101 | struct sgttyb sgttyb; /* Basic BSD tty driver information. */ |
| 102 | int lflag; /* Local mode flags, like LPASS8. */ |
| 103 | #if defined (TIOCGETC) |
| 104 | struct tchars tchars; /* Terminal special characters, including ^S and ^Q. */ |
| 105 | #endif |
| 106 | #if defined (TIOCGLTC) |
| 107 | struct ltchars ltchars; /* 4.2 BSD editing characters */ |
| 108 | #endif |
| 109 | int flags; /* Bitmap saying which parts of the struct are valid. */ |
| 110 | }; |
| 111 | |
| 112 | #define TIOTYPE struct bsdtty |
| 113 | |
| 114 | static TIOTYPE otio; |
| 115 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 116 | static void save_tty_chars PARAMS((TIOTYPE *)); |
| 117 | static int _get_tty_settings PARAMS((int, TIOTYPE *)); |
| 118 | static int get_tty_settings PARAMS((int, TIOTYPE *)); |
| 119 | static int _set_tty_settings PARAMS((int, TIOTYPE *)); |
| 120 | static int set_tty_settings PARAMS((int, TIOTYPE *)); |
| 121 | |
| 122 | static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *)); |
| 123 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 124 | static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t)); |
| 125 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 126 | static void |
| 127 | save_tty_chars (tiop) |
| 128 | TIOTYPE *tiop; |
| 129 | { |
| 130 | _rl_last_tty_chars = _rl_tty_chars; |
| 131 | |
| 132 | if (tiop->flags & SGTTY_SET) |
| 133 | { |
| 134 | _rl_tty_chars.t_erase = tiop->sgttyb.sg_erase; |
| 135 | _rl_tty_chars.t_kill = tiop->sgttyb.sg_kill; |
| 136 | } |
| 137 | |
| 138 | if (tiop->flags & TCHARS_SET) |
| 139 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 140 | _rl_intr_char = _rl_tty_chars.t_intr = tiop->tchars.t_intrc; |
| 141 | _rl_quit_char = _rl_tty_chars.t_quit = tiop->tchars.t_quitc; |
| 142 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 143 | _rl_tty_chars.t_start = tiop->tchars.t_startc; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 144 | _rl_tty_chars.t_stop = tiop->tchars.t_stopc; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 145 | _rl_tty_chars.t_eof = tiop->tchars.t_eofc; |
| 146 | _rl_tty_chars.t_eol = '\n'; |
| 147 | _rl_tty_chars.t_eol2 = tiop->tchars.t_brkc; |
| 148 | } |
| 149 | |
| 150 | if (tiop->flags & LTCHARS_SET) |
| 151 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 152 | _rl_susp_char = _rl_tty_chars.t_susp = tiop->ltchars.t_suspc; |
| 153 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 154 | _rl_tty_chars.t_dsusp = tiop->ltchars.t_dsuspc; |
| 155 | _rl_tty_chars.t_reprint = tiop->ltchars.t_rprntc; |
| 156 | _rl_tty_chars.t_flush = tiop->ltchars.t_flushc; |
| 157 | _rl_tty_chars.t_werase = tiop->ltchars.t_werasc; |
| 158 | _rl_tty_chars.t_lnext = tiop->ltchars.t_lnextc; |
| 159 | } |
| 160 | |
| 161 | _rl_tty_chars.t_status = -1; |
| 162 | } |
| 163 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 164 | static int |
| 165 | get_tty_settings (tty, tiop) |
| 166 | int tty; |
| 167 | TIOTYPE *tiop; |
| 168 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 169 | set_winsize (tty); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 170 | |
| 171 | tiop->flags = tiop->lflag = 0; |
| 172 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 173 | errno = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 174 | if (ioctl (tty, TIOCGETP, &(tiop->sgttyb)) < 0) |
| 175 | return -1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 176 | tiop->flags |= SGTTY_SET; |
| 177 | |
| 178 | #if defined (TIOCLGET) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 179 | if (ioctl (tty, TIOCLGET, &(tiop->lflag)) == 0) |
| 180 | tiop->flags |= LFLAG_SET; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 181 | #endif |
| 182 | |
| 183 | #if defined (TIOCGETC) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 184 | if (ioctl (tty, TIOCGETC, &(tiop->tchars)) == 0) |
| 185 | tiop->flags |= TCHARS_SET; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 186 | #endif |
| 187 | |
| 188 | #if defined (TIOCGLTC) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 189 | if (ioctl (tty, TIOCGLTC, &(tiop->ltchars)) == 0) |
| 190 | tiop->flags |= LTCHARS_SET; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 191 | #endif |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 196 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 197 | set_tty_settings (tty, tiop) |
| 198 | int tty; |
| 199 | TIOTYPE *tiop; |
| 200 | { |
| 201 | if (tiop->flags & SGTTY_SET) |
| 202 | { |
| 203 | ioctl (tty, TIOCSETN, &(tiop->sgttyb)); |
| 204 | tiop->flags &= ~SGTTY_SET; |
| 205 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 206 | _rl_echoing_p = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 207 | |
| 208 | #if defined (TIOCLSET) |
| 209 | if (tiop->flags & LFLAG_SET) |
| 210 | { |
| 211 | ioctl (tty, TIOCLSET, &(tiop->lflag)); |
| 212 | tiop->flags &= ~LFLAG_SET; |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | #if defined (TIOCSETC) |
| 217 | if (tiop->flags & TCHARS_SET) |
| 218 | { |
| 219 | ioctl (tty, TIOCSETC, &(tiop->tchars)); |
| 220 | tiop->flags &= ~TCHARS_SET; |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | #if defined (TIOCSLTC) |
| 225 | if (tiop->flags & LTCHARS_SET) |
| 226 | { |
| 227 | ioctl (tty, TIOCSLTC, &(tiop->ltchars)); |
| 228 | tiop->flags &= ~LTCHARS_SET; |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static void |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 236 | prepare_terminal_settings (meta_flag, oldtio, tiop) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 237 | int meta_flag; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 238 | TIOTYPE oldtio, *tiop; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 239 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 240 | _rl_echoing_p = (oldtio.sgttyb.sg_flags & ECHO); |
| 241 | _rl_echoctl = (oldtio.sgttyb.sg_flags & ECHOCTL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 242 | |
| 243 | /* Copy the original settings to the structure we're going to use for |
| 244 | our settings. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 245 | tiop->sgttyb = oldtio.sgttyb; |
| 246 | tiop->lflag = oldtio.lflag; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 247 | #if defined (TIOCGETC) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 248 | tiop->tchars = oldtio.tchars; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 249 | #endif |
| 250 | #if defined (TIOCGLTC) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 251 | tiop->ltchars = oldtio.ltchars; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 252 | #endif |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 253 | tiop->flags = oldtio.flags; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 254 | |
| 255 | /* First, the basic settings to put us into character-at-a-time, no-echo |
| 256 | input mode. */ |
| 257 | tiop->sgttyb.sg_flags &= ~(ECHO | CRMOD); |
| 258 | tiop->sgttyb.sg_flags |= CBREAK; |
| 259 | |
| 260 | /* If this terminal doesn't care how the 8th bit is used, then we can |
| 261 | use it for the meta-key. If only one of even or odd parity is |
| 262 | specified, then the terminal is using parity, and we cannot. */ |
| 263 | #if !defined (ANYP) |
| 264 | # define ANYP (EVENP | ODDP) |
| 265 | #endif |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 266 | if (((oldtio.sgttyb.sg_flags & ANYP) == ANYP) || |
| 267 | ((oldtio.sgttyb.sg_flags & ANYP) == 0)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 268 | { |
| 269 | tiop->sgttyb.sg_flags |= ANYP; |
| 270 | |
| 271 | /* Hack on local mode flags if we can. */ |
| 272 | #if defined (TIOCLGET) |
| 273 | # if defined (LPASS8) |
| 274 | tiop->lflag |= LPASS8; |
| 275 | # endif /* LPASS8 */ |
| 276 | #endif /* TIOCLGET */ |
| 277 | } |
| 278 | |
| 279 | #if defined (TIOCGETC) |
| 280 | # if defined (USE_XON_XOFF) |
| 281 | /* Get rid of terminal output start and stop characters. */ |
| 282 | tiop->tchars.t_stopc = -1; /* C-s */ |
| 283 | tiop->tchars.t_startc = -1; /* C-q */ |
| 284 | |
| 285 | /* If there is an XON character, bind it to restart the output. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 286 | if (oldtio.tchars.t_startc != -1) |
| 287 | rl_bind_key (oldtio.tchars.t_startc, rl_restart_output); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 288 | # endif /* USE_XON_XOFF */ |
| 289 | |
| 290 | /* If there is an EOF char, bind _rl_eof_char to it. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 291 | if (oldtio.tchars.t_eofc != -1) |
| 292 | _rl_eof_char = oldtio.tchars.t_eofc; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 293 | |
| 294 | # if defined (NO_KILL_INTR) |
| 295 | /* Get rid of terminal-generated SIGQUIT and SIGINT. */ |
| 296 | tiop->tchars.t_quitc = -1; /* C-\ */ |
| 297 | tiop->tchars.t_intrc = -1; /* C-c */ |
| 298 | # endif /* NO_KILL_INTR */ |
| 299 | #endif /* TIOCGETC */ |
| 300 | |
| 301 | #if defined (TIOCGLTC) |
| 302 | /* Make the interrupt keys go away. Just enough to make people happy. */ |
| 303 | tiop->ltchars.t_dsuspc = -1; /* C-y */ |
| 304 | tiop->ltchars.t_lnextc = -1; /* C-v */ |
| 305 | #endif /* TIOCGLTC */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | #else /* !defined (NEW_TTY_DRIVER) */ |
| 309 | |
| 310 | #if !defined (VMIN) |
| 311 | # define VMIN VEOF |
| 312 | #endif |
| 313 | |
| 314 | #if !defined (VTIME) |
| 315 | # define VTIME VEOL |
| 316 | #endif |
| 317 | |
| 318 | #if defined (TERMIOS_TTY_DRIVER) |
| 319 | # define TIOTYPE struct termios |
| 320 | # define DRAIN_OUTPUT(fd) tcdrain (fd) |
| 321 | # define GETATTR(tty, tiop) (tcgetattr (tty, tiop)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 322 | # ifdef M_UNIX |
| 323 | # define SETATTR(tty, tiop) (tcsetattr (tty, TCSANOW, tiop)) |
| 324 | # else |
| 325 | # define SETATTR(tty, tiop) (tcsetattr (tty, TCSADRAIN, tiop)) |
| 326 | # endif /* !M_UNIX */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 327 | #else |
| 328 | # define TIOTYPE struct termio |
| 329 | # define DRAIN_OUTPUT(fd) |
| 330 | # define GETATTR(tty, tiop) (ioctl (tty, TCGETA, tiop)) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 331 | # define SETATTR(tty, tiop) (ioctl (tty, TCSETAW, tiop)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 332 | #endif /* !TERMIOS_TTY_DRIVER */ |
| 333 | |
| 334 | static TIOTYPE otio; |
| 335 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 336 | static void save_tty_chars PARAMS((TIOTYPE *)); |
| 337 | static int _get_tty_settings PARAMS((int, TIOTYPE *)); |
| 338 | static int get_tty_settings PARAMS((int, TIOTYPE *)); |
| 339 | static int _set_tty_settings PARAMS((int, TIOTYPE *)); |
| 340 | static int set_tty_settings PARAMS((int, TIOTYPE *)); |
| 341 | |
| 342 | static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *)); |
| 343 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 344 | static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t)); |
| 345 | static void _rl_bind_tty_special_chars PARAMS((Keymap, TIOTYPE)); |
| 346 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 347 | #if defined (FLUSHO) |
| 348 | # define OUTPUT_BEING_FLUSHED(tp) (tp->c_lflag & FLUSHO) |
| 349 | #else |
| 350 | # define OUTPUT_BEING_FLUSHED(tp) 0 |
| 351 | #endif |
| 352 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 353 | static void |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 354 | save_tty_chars (tiop) |
| 355 | TIOTYPE *tiop; |
| 356 | { |
| 357 | _rl_last_tty_chars = _rl_tty_chars; |
| 358 | |
| 359 | _rl_tty_chars.t_eof = tiop->c_cc[VEOF]; |
| 360 | _rl_tty_chars.t_eol = tiop->c_cc[VEOL]; |
| 361 | #ifdef VEOL2 |
| 362 | _rl_tty_chars.t_eol2 = tiop->c_cc[VEOL2]; |
| 363 | #endif |
| 364 | _rl_tty_chars.t_erase = tiop->c_cc[VERASE]; |
| 365 | #ifdef VWERASE |
| 366 | _rl_tty_chars.t_werase = tiop->c_cc[VWERASE]; |
| 367 | #endif |
| 368 | _rl_tty_chars.t_kill = tiop->c_cc[VKILL]; |
| 369 | #ifdef VREPRINT |
| 370 | _rl_tty_chars.t_reprint = tiop->c_cc[VREPRINT]; |
| 371 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 372 | _rl_intr_char = _rl_tty_chars.t_intr = tiop->c_cc[VINTR]; |
| 373 | _rl_quit_char = _rl_tty_chars.t_quit = tiop->c_cc[VQUIT]; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 374 | #ifdef VSUSP |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 375 | _rl_susp_char = _rl_tty_chars.t_susp = tiop->c_cc[VSUSP]; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 376 | #endif |
| 377 | #ifdef VDSUSP |
| 378 | _rl_tty_chars.t_dsusp = tiop->c_cc[VDSUSP]; |
| 379 | #endif |
| 380 | #ifdef VSTART |
| 381 | _rl_tty_chars.t_start = tiop->c_cc[VSTART]; |
| 382 | #endif |
| 383 | #ifdef VSTOP |
| 384 | _rl_tty_chars.t_stop = tiop->c_cc[VSTOP]; |
| 385 | #endif |
| 386 | #ifdef VLNEXT |
| 387 | _rl_tty_chars.t_lnext = tiop->c_cc[VLNEXT]; |
| 388 | #endif |
| 389 | #ifdef VDISCARD |
| 390 | _rl_tty_chars.t_flush = tiop->c_cc[VDISCARD]; |
| 391 | #endif |
| 392 | #ifdef VSTATUS |
| 393 | _rl_tty_chars.t_status = tiop->c_cc[VSTATUS]; |
| 394 | #endif |
| 395 | } |
| 396 | |
| 397 | #if defined (_AIX) || defined (_AIX41) |
| 398 | /* Currently this is only used on AIX */ |
| 399 | static void |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 400 | rltty_warning (msg) |
| 401 | char *msg; |
| 402 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 403 | _rl_errmsg ("warning: %s", msg); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 404 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 405 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 406 | |
| 407 | #if defined (_AIX) |
| 408 | void |
| 409 | setopost(tp) |
| 410 | TIOTYPE *tp; |
| 411 | { |
| 412 | if ((tp->c_oflag & OPOST) == 0) |
| 413 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 414 | _rl_errmsg ("warning: turning on OPOST for terminal\r"); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 415 | tp->c_oflag |= OPOST|ONLCR; |
| 416 | } |
| 417 | } |
| 418 | #endif |
| 419 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 420 | static int |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 421 | _get_tty_settings (tty, tiop) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 422 | int tty; |
| 423 | TIOTYPE *tiop; |
| 424 | { |
| 425 | int ioctl_ret; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 426 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 427 | while (1) |
| 428 | { |
| 429 | ioctl_ret = GETATTR (tty, tiop); |
| 430 | if (ioctl_ret < 0) |
| 431 | { |
| 432 | if (errno != EINTR) |
| 433 | return -1; |
| 434 | else |
| 435 | continue; |
| 436 | } |
| 437 | if (OUTPUT_BEING_FLUSHED (tiop)) |
| 438 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 439 | #if defined (FLUSHO) |
| 440 | _rl_errmsg ("warning: turning off output flushing"); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 441 | tiop->c_lflag &= ~FLUSHO; |
| 442 | break; |
| 443 | #else |
| 444 | continue; |
| 445 | #endif |
| 446 | } |
| 447 | break; |
| 448 | } |
| 449 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static int |
| 454 | get_tty_settings (tty, tiop) |
| 455 | int tty; |
| 456 | TIOTYPE *tiop; |
| 457 | { |
| 458 | set_winsize (tty); |
| 459 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 460 | errno = 0; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 461 | if (_get_tty_settings (tty, tiop) < 0) |
| 462 | return -1; |
| 463 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 464 | #if defined (_AIX) |
| 465 | setopost(tiop); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 466 | #endif |
| 467 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 472 | _set_tty_settings (tty, tiop) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 473 | int tty; |
| 474 | TIOTYPE *tiop; |
| 475 | { |
| 476 | while (SETATTR (tty, tiop) < 0) |
| 477 | { |
| 478 | if (errno != EINTR) |
| 479 | return -1; |
| 480 | errno = 0; |
| 481 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 482 | return 0; |
| 483 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 484 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 485 | static int |
| 486 | set_tty_settings (tty, tiop) |
| 487 | int tty; |
| 488 | TIOTYPE *tiop; |
| 489 | { |
| 490 | if (_set_tty_settings (tty, tiop) < 0) |
| 491 | return -1; |
| 492 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 493 | #if 0 |
| 494 | |
| 495 | #if defined (TERMIOS_TTY_DRIVER) |
| 496 | # if defined (__ksr1__) |
| 497 | if (ksrflow) |
| 498 | { |
| 499 | ksrflow = 0; |
| 500 | tcflow (tty, TCOON); |
| 501 | } |
| 502 | # else /* !ksr1 */ |
| 503 | tcflow (tty, TCOON); /* Simulate a ^Q. */ |
| 504 | # endif /* !ksr1 */ |
| 505 | #else |
| 506 | ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */ |
| 507 | #endif /* !TERMIOS_TTY_DRIVER */ |
| 508 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 509 | #endif /* 0 */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static void |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 515 | prepare_terminal_settings (meta_flag, oldtio, tiop) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 516 | int meta_flag; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 517 | TIOTYPE oldtio, *tiop; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 518 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 519 | _rl_echoing_p = (oldtio.c_lflag & ECHO); |
| 520 | _rl_echoctl = (oldtio.c_lflag & ECHOCTL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 521 | |
| 522 | tiop->c_lflag &= ~(ICANON | ECHO); |
| 523 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 524 | if ((unsigned char) oldtio.c_cc[VEOF] != (unsigned char) _POSIX_VDISABLE) |
| 525 | _rl_eof_char = oldtio.c_cc[VEOF]; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 526 | |
| 527 | #if defined (USE_XON_XOFF) |
| 528 | #if defined (IXANY) |
| 529 | tiop->c_iflag &= ~(IXON | IXOFF | IXANY); |
| 530 | #else |
| 531 | /* `strict' Posix systems do not define IXANY. */ |
| 532 | tiop->c_iflag &= ~(IXON | IXOFF); |
| 533 | #endif /* IXANY */ |
| 534 | #endif /* USE_XON_XOFF */ |
| 535 | |
| 536 | /* Only turn this off if we are using all 8 bits. */ |
| 537 | if (((tiop->c_cflag & CSIZE) == CS8) || meta_flag) |
| 538 | tiop->c_iflag &= ~(ISTRIP | INPCK); |
| 539 | |
| 540 | /* Make sure we differentiate between CR and NL on input. */ |
| 541 | tiop->c_iflag &= ~(ICRNL | INLCR); |
| 542 | |
| 543 | #if !defined (HANDLE_SIGNALS) |
| 544 | tiop->c_lflag &= ~ISIG; |
| 545 | #else |
| 546 | tiop->c_lflag |= ISIG; |
| 547 | #endif |
| 548 | |
| 549 | tiop->c_cc[VMIN] = 1; |
| 550 | tiop->c_cc[VTIME] = 0; |
| 551 | |
| 552 | #if defined (FLUSHO) |
| 553 | if (OUTPUT_BEING_FLUSHED (tiop)) |
| 554 | { |
| 555 | tiop->c_lflag &= ~FLUSHO; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 556 | oldtio.c_lflag &= ~FLUSHO; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 557 | } |
| 558 | #endif |
| 559 | |
| 560 | /* Turn off characters that we need on Posix systems with job control, |
| 561 | just to be sure. This includes ^Y and ^V. This should not really |
| 562 | be necessary. */ |
| 563 | #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_VDISABLE) |
| 564 | |
| 565 | #if defined (VLNEXT) |
| 566 | tiop->c_cc[VLNEXT] = _POSIX_VDISABLE; |
| 567 | #endif |
| 568 | |
| 569 | #if defined (VDSUSP) |
| 570 | tiop->c_cc[VDSUSP] = _POSIX_VDISABLE; |
| 571 | #endif |
| 572 | |
| 573 | #endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */ |
| 574 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 575 | #endif /* !NEW_TTY_DRIVER */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 576 | |
| 577 | /* Put the terminal in CBREAK mode so that we can detect key presses. */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 578 | #if defined (NO_TTY_DRIVER) |
| 579 | void |
| 580 | rl_prep_terminal (meta_flag) |
| 581 | int meta_flag; |
| 582 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 583 | _rl_echoing_p = 1; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void |
| 587 | rl_deprep_terminal () |
| 588 | { |
| 589 | } |
| 590 | |
| 591 | #else /* ! NO_TTY_DRIVER */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 592 | void |
| 593 | rl_prep_terminal (meta_flag) |
| 594 | int meta_flag; |
| 595 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 596 | int tty; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 597 | TIOTYPE tio; |
| 598 | |
| 599 | if (terminal_prepped) |
| 600 | return; |
| 601 | |
| 602 | /* Try to keep this function from being INTerrupted. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 603 | _rl_block_sigint (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 604 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 605 | tty = fileno (rl_instream); |
| 606 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 607 | if (get_tty_settings (tty, &tio) < 0) |
| 608 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 609 | #if defined (ENOTSUP) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 610 | /* MacOS X and Linux, at least, lie about the value of errno if |
| 611 | tcgetattr fails. */ |
| 612 | if (errno == ENOTTY || errno == EINVAL || errno == ENOTSUP) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 613 | #else |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 614 | if (errno == ENOTTY || errno == EINVAL) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 615 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 616 | _rl_echoing_p = 1; /* XXX */ |
| 617 | |
| 618 | _rl_release_sigint (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 619 | return; |
| 620 | } |
| 621 | |
| 622 | otio = tio; |
| 623 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 624 | if (_rl_bind_stty_chars) |
| 625 | { |
| 626 | #if defined (VI_MODE) |
| 627 | /* If editing in vi mode, make sure we restore the bindings in the |
| 628 | insertion keymap no matter what keymap we ended up in. */ |
| 629 | if (rl_editing_mode == vi_mode) |
| 630 | rl_tty_unset_default_bindings (vi_insertion_keymap); |
| 631 | else |
| 632 | #endif |
| 633 | rl_tty_unset_default_bindings (_rl_keymap); |
| 634 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 635 | save_tty_chars (&otio); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 636 | RL_SETSTATE(RL_STATE_TTYCSAVED); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 637 | if (_rl_bind_stty_chars) |
| 638 | { |
| 639 | #if defined (VI_MODE) |
| 640 | /* If editing in vi mode, make sure we set the bindings in the |
| 641 | insertion keymap no matter what keymap we ended up in. */ |
| 642 | if (rl_editing_mode == vi_mode) |
| 643 | _rl_bind_tty_special_chars (vi_insertion_keymap, tio); |
| 644 | else |
| 645 | #endif |
| 646 | _rl_bind_tty_special_chars (_rl_keymap, tio); |
| 647 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 648 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 649 | prepare_terminal_settings (meta_flag, otio, &tio); |
| 650 | |
| 651 | if (set_tty_settings (tty, &tio) < 0) |
| 652 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 653 | _rl_release_sigint (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 654 | return; |
| 655 | } |
| 656 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 657 | if (_rl_enable_keypad) |
| 658 | _rl_control_keypad (1); |
| 659 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 660 | fflush (rl_outstream); |
| 661 | terminal_prepped = 1; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 662 | RL_SETSTATE(RL_STATE_TERMPREPPED); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 663 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 664 | _rl_release_sigint (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* Restore the terminal's normal settings and modes. */ |
| 668 | void |
| 669 | rl_deprep_terminal () |
| 670 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 671 | int tty; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 672 | |
| 673 | if (!terminal_prepped) |
| 674 | return; |
| 675 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 676 | /* Try to keep this function from being interrupted. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 677 | _rl_block_sigint (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 678 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 679 | tty = fileno (rl_instream); |
| 680 | |
| 681 | if (_rl_enable_keypad) |
| 682 | _rl_control_keypad (0); |
| 683 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 684 | fflush (rl_outstream); |
| 685 | |
| 686 | if (set_tty_settings (tty, &otio) < 0) |
| 687 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 688 | _rl_release_sigint (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 689 | return; |
| 690 | } |
| 691 | |
| 692 | terminal_prepped = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 693 | RL_UNSETSTATE(RL_STATE_TERMPREPPED); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 694 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 695 | _rl_release_sigint (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 696 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 697 | #endif /* !NO_TTY_DRIVER */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 698 | |
| 699 | /* **************************************************************** */ |
| 700 | /* */ |
| 701 | /* Bogus Flow Control */ |
| 702 | /* */ |
| 703 | /* **************************************************************** */ |
| 704 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 705 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 706 | rl_restart_output (count, key) |
| 707 | int count, key; |
| 708 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 709 | #if defined (__MINGW32__) |
| 710 | return 0; |
| 711 | #else /* !__MING32__ */ |
| 712 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 713 | int fildes = fileno (rl_outstream); |
| 714 | #if defined (TIOCSTART) |
| 715 | #if defined (apollo) |
| 716 | ioctl (&fildes, TIOCSTART, 0); |
| 717 | #else |
| 718 | ioctl (fildes, TIOCSTART, 0); |
| 719 | #endif /* apollo */ |
| 720 | |
| 721 | #else /* !TIOCSTART */ |
| 722 | # if defined (TERMIOS_TTY_DRIVER) |
| 723 | # if defined (__ksr1__) |
| 724 | if (ksrflow) |
| 725 | { |
| 726 | ksrflow = 0; |
| 727 | tcflow (fildes, TCOON); |
| 728 | } |
| 729 | # else /* !ksr1 */ |
| 730 | tcflow (fildes, TCOON); /* Simulate a ^Q. */ |
| 731 | # endif /* !ksr1 */ |
| 732 | # else /* !TERMIOS_TTY_DRIVER */ |
| 733 | # if defined (TCXONC) |
| 734 | ioctl (fildes, TCXONC, TCOON); |
| 735 | # endif /* TCXONC */ |
| 736 | # endif /* !TERMIOS_TTY_DRIVER */ |
| 737 | #endif /* !TIOCSTART */ |
| 738 | |
| 739 | return 0; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 740 | #endif /* !__MINGW32__ */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 743 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 744 | rl_stop_output (count, key) |
| 745 | int count, key; |
| 746 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 747 | #if defined (__MINGW32__) |
| 748 | return 0; |
| 749 | #else |
| 750 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 751 | int fildes = fileno (rl_instream); |
| 752 | |
| 753 | #if defined (TIOCSTOP) |
| 754 | # if defined (apollo) |
| 755 | ioctl (&fildes, TIOCSTOP, 0); |
| 756 | # else |
| 757 | ioctl (fildes, TIOCSTOP, 0); |
| 758 | # endif /* apollo */ |
| 759 | #else /* !TIOCSTOP */ |
| 760 | # if defined (TERMIOS_TTY_DRIVER) |
| 761 | # if defined (__ksr1__) |
| 762 | ksrflow = 1; |
| 763 | # endif /* ksr1 */ |
| 764 | tcflow (fildes, TCOOFF); |
| 765 | # else |
| 766 | # if defined (TCXONC) |
| 767 | ioctl (fildes, TCXONC, TCOON); |
| 768 | # endif /* TCXONC */ |
| 769 | # endif /* !TERMIOS_TTY_DRIVER */ |
| 770 | #endif /* !TIOCSTOP */ |
| 771 | |
| 772 | return 0; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 773 | #endif /* !__MINGW32__ */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 774 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 775 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 776 | /* **************************************************************** */ |
| 777 | /* */ |
| 778 | /* Default Key Bindings */ |
| 779 | /* */ |
| 780 | /* **************************************************************** */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 781 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 782 | #if !defined (NO_TTY_DRIVER) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 783 | #define SET_SPECIAL(sc, func) set_special_char(kmap, &ttybuff, sc, func) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 784 | #endif |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 785 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 786 | #if defined (NO_TTY_DRIVER) |
| 787 | |
| 788 | #define SET_SPECIAL(sc, func) |
| 789 | #define RESET_SPECIAL(c) |
| 790 | |
| 791 | #elif defined (NEW_TTY_DRIVER) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 792 | static void |
| 793 | set_special_char (kmap, tiop, sc, func) |
| 794 | Keymap kmap; |
| 795 | TIOTYPE *tiop; |
| 796 | int sc; |
| 797 | rl_command_func_t *func; |
| 798 | { |
| 799 | if (sc != -1 && kmap[(unsigned char)sc].type == ISFUNC) |
| 800 | kmap[(unsigned char)sc].function = func; |
| 801 | } |
| 802 | |
| 803 | #define RESET_SPECIAL(c) \ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 804 | if (c != -1 && kmap[(unsigned char)c].type == ISFUNC) \ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 805 | kmap[(unsigned char)c].function = rl_insert; |
| 806 | |
| 807 | static void |
| 808 | _rl_bind_tty_special_chars (kmap, ttybuff) |
| 809 | Keymap kmap; |
| 810 | TIOTYPE ttybuff; |
| 811 | { |
| 812 | if (ttybuff.flags & SGTTY_SET) |
| 813 | { |
| 814 | SET_SPECIAL (ttybuff.sgttyb.sg_erase, rl_rubout); |
| 815 | SET_SPECIAL (ttybuff.sgttyb.sg_kill, rl_unix_line_discard); |
| 816 | } |
| 817 | |
| 818 | # if defined (TIOCGLTC) |
| 819 | if (ttybuff.flags & LTCHARS_SET) |
| 820 | { |
| 821 | SET_SPECIAL (ttybuff.ltchars.t_werasc, rl_unix_word_rubout); |
| 822 | SET_SPECIAL (ttybuff.ltchars.t_lnextc, rl_quoted_insert); |
| 823 | } |
| 824 | # endif /* TIOCGLTC */ |
| 825 | } |
| 826 | |
| 827 | #else /* !NEW_TTY_DRIVER */ |
| 828 | static void |
| 829 | set_special_char (kmap, tiop, sc, func) |
| 830 | Keymap kmap; |
| 831 | TIOTYPE *tiop; |
| 832 | int sc; |
| 833 | rl_command_func_t *func; |
| 834 | { |
| 835 | unsigned char uc; |
| 836 | |
| 837 | uc = tiop->c_cc[sc]; |
| 838 | if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) |
| 839 | kmap[uc].function = func; |
| 840 | } |
| 841 | |
| 842 | /* used later */ |
| 843 | #define RESET_SPECIAL(uc) \ |
| 844 | if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) \ |
| 845 | kmap[uc].function = rl_insert; |
| 846 | |
| 847 | static void |
| 848 | _rl_bind_tty_special_chars (kmap, ttybuff) |
| 849 | Keymap kmap; |
| 850 | TIOTYPE ttybuff; |
| 851 | { |
| 852 | SET_SPECIAL (VERASE, rl_rubout); |
| 853 | SET_SPECIAL (VKILL, rl_unix_line_discard); |
| 854 | |
| 855 | # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER) |
| 856 | SET_SPECIAL (VLNEXT, rl_quoted_insert); |
| 857 | # endif /* VLNEXT && TERMIOS_TTY_DRIVER */ |
| 858 | |
| 859 | # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER) |
| 860 | SET_SPECIAL (VWERASE, rl_unix_word_rubout); |
| 861 | # endif /* VWERASE && TERMIOS_TTY_DRIVER */ |
| 862 | } |
| 863 | |
| 864 | #endif /* !NEW_TTY_DRIVER */ |
| 865 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 866 | /* Set the system's default editing characters to their readline equivalents |
| 867 | in KMAP. Should be static, now that we have rl_tty_set_default_bindings. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 868 | void |
| 869 | rltty_set_default_bindings (kmap) |
| 870 | Keymap kmap; |
| 871 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 872 | #if !defined (NO_TTY_DRIVER) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 873 | TIOTYPE ttybuff; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 874 | int tty; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 875 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 876 | tty = fileno (rl_instream); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 877 | |
| 878 | if (get_tty_settings (tty, &ttybuff) == 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 879 | _rl_bind_tty_special_chars (kmap, ttybuff); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 880 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 881 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 882 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 883 | /* New public way to set the system default editing chars to their readline |
| 884 | equivalents. */ |
| 885 | void |
| 886 | rl_tty_set_default_bindings (kmap) |
| 887 | Keymap kmap; |
| 888 | { |
| 889 | rltty_set_default_bindings (kmap); |
| 890 | } |
| 891 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 892 | /* Rebind all of the tty special chars that readline worries about back |
| 893 | to self-insert. Call this before saving the current terminal special |
| 894 | chars with save_tty_chars(). This only works on POSIX termios or termio |
| 895 | systems. */ |
| 896 | void |
| 897 | rl_tty_unset_default_bindings (kmap) |
| 898 | Keymap kmap; |
| 899 | { |
| 900 | /* Don't bother before we've saved the tty special chars at least once. */ |
| 901 | if (RL_ISSTATE(RL_STATE_TTYCSAVED) == 0) |
| 902 | return; |
| 903 | |
| 904 | RESET_SPECIAL (_rl_tty_chars.t_erase); |
| 905 | RESET_SPECIAL (_rl_tty_chars.t_kill); |
| 906 | |
| 907 | # if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER) |
| 908 | RESET_SPECIAL (_rl_tty_chars.t_lnext); |
| 909 | # endif /* VLNEXT && TERMIOS_TTY_DRIVER */ |
| 910 | |
| 911 | # if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER) |
| 912 | RESET_SPECIAL (_rl_tty_chars.t_werase); |
| 913 | # endif /* VWERASE && TERMIOS_TTY_DRIVER */ |
| 914 | } |
| 915 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 916 | #if defined (HANDLE_SIGNALS) |
| 917 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 918 | #if defined (NEW_TTY_DRIVER) || defined (NO_TTY_DRIVER) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 919 | int |
| 920 | _rl_disable_tty_signals () |
| 921 | { |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | int |
| 926 | _rl_restore_tty_signals () |
| 927 | { |
| 928 | return 0; |
| 929 | } |
| 930 | #else |
| 931 | |
| 932 | static TIOTYPE sigstty, nosigstty; |
| 933 | static int tty_sigs_disabled = 0; |
| 934 | |
| 935 | int |
| 936 | _rl_disable_tty_signals () |
| 937 | { |
| 938 | if (tty_sigs_disabled) |
| 939 | return 0; |
| 940 | |
| 941 | if (_get_tty_settings (fileno (rl_instream), &sigstty) < 0) |
| 942 | return -1; |
| 943 | |
| 944 | nosigstty = sigstty; |
| 945 | |
| 946 | nosigstty.c_lflag &= ~ISIG; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 947 | nosigstty.c_iflag &= ~IXON; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 948 | |
| 949 | if (_set_tty_settings (fileno (rl_instream), &nosigstty) < 0) |
| 950 | return (_set_tty_settings (fileno (rl_instream), &sigstty)); |
| 951 | |
| 952 | tty_sigs_disabled = 1; |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | int |
| 957 | _rl_restore_tty_signals () |
| 958 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 959 | int r; |
| 960 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 961 | if (tty_sigs_disabled == 0) |
| 962 | return 0; |
| 963 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 964 | r = _set_tty_settings (fileno (rl_instream), &sigstty); |
| 965 | |
| 966 | if (r == 0) |
| 967 | tty_sigs_disabled = 0; |
| 968 | |
| 969 | return r; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 970 | } |
| 971 | #endif /* !NEW_TTY_DRIVER */ |
| 972 | |
| 973 | #endif /* HANDLE_SIGNALS */ |