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