rltty.c 20.6 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10
/* rltty.c -- functions to prepare and restore the terminal for readline's
   use. */

/* Copyright (C) 1992 Free Software Foundation, Inc.

   This file is part of the GNU Readline Library, a library for
   reading lines of text with interactive input and history editing.

   The GNU Readline Library is free software; you can redistribute it
   and/or modify it under the terms of the GNU General Public License
unknown's avatar
unknown committed
11
   as published by the Free Software Foundation; either version 2, or
unknown's avatar
unknown committed
12 13 14 15 16 17 18 19 20 21
   (at your option) any later version.

   The GNU Readline Library is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   The GNU General Public License is often shipped with GNU software, and
   is generally kept in a file called COPYING or LICENSE.  If you do not
   have a copy of the license, write to the Free Software Foundation,
unknown's avatar
unknown committed
22
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
unknown's avatar
unknown committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#define READLINE_LIBRARY

#if defined (HAVE_CONFIG_H)
#  include <config.h>
#endif

#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include "rldefs.h"

#if defined (GWINSZ_IN_SYS_IOCTL)
#  include <sys/ioctl.h>
#endif /* GWINSZ_IN_SYS_IOCTL */

#include "rltty.h"
#include "readline.h"
unknown's avatar
unknown committed
46
#include "rlprivate.h"
unknown's avatar
unknown committed
47 48 49 50 51

#if !defined (errno)
extern int errno;
#endif /* !errno */

unknown's avatar
unknown committed
52 53
rl_vintfunc_t *rl_prep_term_function = rl_prep_terminal;
rl_voidfunc_t *rl_deprep_term_function = rl_deprep_terminal;
unknown's avatar
unknown committed
54

unknown's avatar
unknown committed
55 56
static void block_sigint PARAMS((void));
static void release_sigint PARAMS((void));
unknown's avatar
unknown committed
57

unknown's avatar
unknown committed
58
static void set_winsize PARAMS((int));
unknown's avatar
unknown committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

/* **************************************************************** */
/*								    */
/*			   Signal Management			    */
/*								    */
/* **************************************************************** */

#if defined (HAVE_POSIX_SIGNALS)
static sigset_t sigint_set, sigint_oset;
#else /* !HAVE_POSIX_SIGNALS */
#  if defined (HAVE_BSD_SIGNALS)
static int sigint_oldmask;
#  endif /* HAVE_BSD_SIGNALS */
#endif /* !HAVE_POSIX_SIGNALS */

static int sigint_blocked;

/* Cause SIGINT to not be delivered until the corresponding call to
   release_sigint(). */
static void
block_sigint ()
{
  if (sigint_blocked)
    return;

#if defined (HAVE_POSIX_SIGNALS)
  sigemptyset (&sigint_set);
  sigemptyset (&sigint_oset);
  sigaddset (&sigint_set, SIGINT);
  sigprocmask (SIG_BLOCK, &sigint_set, &sigint_oset);
#else /* !HAVE_POSIX_SIGNALS */
#  if defined (HAVE_BSD_SIGNALS)
  sigint_oldmask = sigblock (sigmask (SIGINT));
#  else /* !HAVE_BSD_SIGNALS */
#    if defined (HAVE_USG_SIGHOLD)
  sighold (SIGINT);
#    endif /* HAVE_USG_SIGHOLD */
#  endif /* !HAVE_BSD_SIGNALS */
#endif /* !HAVE_POSIX_SIGNALS */
unknown's avatar
unknown committed
98

unknown's avatar
unknown committed
99 100 101 102 103 104 105
  sigint_blocked = 1;
}

/* Allow SIGINT to be delivered. */
static void
release_sigint ()
{
unknown's avatar
unknown committed
106
  if (sigint_blocked == 0)
unknown's avatar
unknown committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    return;

#if defined (HAVE_POSIX_SIGNALS)
  sigprocmask (SIG_SETMASK, &sigint_oset, (sigset_t *)NULL);
#else
#  if defined (HAVE_BSD_SIGNALS)
  sigsetmask (sigint_oldmask);
#  else /* !HAVE_BSD_SIGNALS */
#    if defined (HAVE_USG_SIGHOLD)
  sigrelse (SIGINT);
#    endif /* HAVE_USG_SIGHOLD */
#  endif /* !HAVE_BSD_SIGNALS */
#endif /* !HAVE_POSIX_SIGNALS */

  sigint_blocked = 0;
}

/* **************************************************************** */
/*								    */
/*		      Saving and Restoring the TTY	    	    */
/*								    */
/* **************************************************************** */

/* Non-zero means that the terminal is in a prepped state. */
static int terminal_prepped;

unknown's avatar
unknown committed
133 134
static _RL_TTY_CHARS _rl_tty_chars, _rl_last_tty_chars;

unknown's avatar
unknown committed
135 136 137 138 139 140 141 142 143 144 145 146
/* If non-zero, means that this process has called tcflow(fd, TCOOFF)
   and output is suspended. */
#if defined (__ksr1__)
static int ksrflow;
#endif

/* Dummy call to force a backgrounded readline to stop before it tries
   to get the tty settings. */
static void
set_winsize (tty)
     int tty;
{
unknown's avatar
unknown committed
147
#if defined (TIOCGWINSZ)
unknown's avatar
unknown committed
148 149 150 151 152
  struct winsize w;

  if (ioctl (tty, TIOCGWINSZ, &w) == 0)
      (void) ioctl (tty, TIOCSWINSZ, &w);
#endif /* TIOCGWINSZ */
unknown's avatar
unknown committed
153
}
unknown's avatar
unknown committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

#if defined (NEW_TTY_DRIVER)

/* Values for the `flags' field of a struct bsdtty.  This tells which
   elements of the struct bsdtty have been fetched from the system and
   are valid. */
#define SGTTY_SET	0x01
#define LFLAG_SET	0x02
#define TCHARS_SET	0x04
#define LTCHARS_SET	0x08

struct bsdtty {
  struct sgttyb sgttyb;	/* Basic BSD tty driver information. */
  int lflag;		/* Local mode flags, like LPASS8. */
#if defined (TIOCGETC)
  struct tchars tchars;	/* Terminal special characters, including ^S and ^Q. */
#endif
#if defined (TIOCGLTC)
  struct ltchars ltchars; /* 4.2 BSD editing characters */
#endif
  int flags;		/* Bitmap saying which parts of the struct are valid. */
};

#define TIOTYPE struct bsdtty

static TIOTYPE otio;

unknown's avatar
unknown committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static void save_tty_chars PARAMS((TIOTYPE *));
static int _get_tty_settings PARAMS((int, TIOTYPE *));
static int get_tty_settings PARAMS((int, TIOTYPE *));
static int _set_tty_settings PARAMS((int, TIOTYPE *));
static int set_tty_settings PARAMS((int, TIOTYPE *));

static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));

static void
save_tty_chars (tiop)
     TIOTYPE *tiop;
{
  _rl_last_tty_chars = _rl_tty_chars;

  if (tiop->flags & SGTTY_SET)
    {
      _rl_tty_chars.t_erase = tiop->sgttyb.sg_erase;
      _rl_tty_chars.t_kill = tiop->sgttyb.sg_kill;
    }

  if (tiop->flags & TCHARS_SET)
    {
      _rl_tty_chars.t_intr = tiop->tchars.t_intrc;
      _rl_tty_chars.t_quit = tiop->tchars.t_quitc;
      _rl_tty_chars.t_start = tiop->tchars.t_startc;
      _rl_tty_chars.t_stop = tiop->tchars.t_stopc;
      _rl_tty_chars.t_eof = tiop->tchars.t_eofc;
      _rl_tty_chars.t_eol = '\n';
      _rl_tty_chars.t_eol2 = tiop->tchars.t_brkc;
    }

  if (tiop->flags & LTCHARS_SET)
    {
      _rl_tty_chars.t_susp = tiop->ltchars.t_suspc;
      _rl_tty_chars.t_dsusp = tiop->ltchars.t_dsuspc;
      _rl_tty_chars.t_reprint = tiop->ltchars.t_rprntc;
      _rl_tty_chars.t_flush = tiop->ltchars.t_flushc;
      _rl_tty_chars.t_werase = tiop->ltchars.t_werasc;
      _rl_tty_chars.t_lnext = tiop->ltchars.t_lnextc;
    }

  _rl_tty_chars.t_status = -1;
}

unknown's avatar
unknown committed
225 226 227 228 229 230 231 232 233
static int
get_tty_settings (tty, tiop)
     int tty;
     TIOTYPE *tiop;
{
  set_winsize (tty);

  tiop->flags = tiop->lflag = 0;

unknown's avatar
unknown committed
234 235
  if (ioctl (tty, TIOCGETP, &(tiop->sgttyb)) < 0)
    return -1;
unknown's avatar
unknown committed
236 237 238
  tiop->flags |= SGTTY_SET;

#if defined (TIOCLGET)
unknown's avatar
unknown committed
239 240
  if (ioctl (tty, TIOCLGET, &(tiop->lflag)) == 0)
    tiop->flags |= LFLAG_SET;
unknown's avatar
unknown committed
241 242 243
#endif

#if defined (TIOCGETC)
unknown's avatar
unknown committed
244 245
  if (ioctl (tty, TIOCGETC, &(tiop->tchars)) == 0)
    tiop->flags |= TCHARS_SET;
unknown's avatar
unknown committed
246 247 248
#endif

#if defined (TIOCGLTC)
unknown's avatar
unknown committed
249 250
  if (ioctl (tty, TIOCGLTC, &(tiop->ltchars)) == 0)
    tiop->flags |= LTCHARS_SET;
unknown's avatar
unknown committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
#endif

  return 0;
}

static int
set_tty_settings (tty, tiop)
     int tty;
     TIOTYPE *tiop;
{
  if (tiop->flags & SGTTY_SET)
    {
      ioctl (tty, TIOCSETN, &(tiop->sgttyb));
      tiop->flags &= ~SGTTY_SET;
    }
  readline_echoing_p = 1;

#if defined (TIOCLSET)
  if (tiop->flags & LFLAG_SET)
    {
      ioctl (tty, TIOCLSET, &(tiop->lflag));
      tiop->flags &= ~LFLAG_SET;
    }
#endif

#if defined (TIOCSETC)
  if (tiop->flags & TCHARS_SET)
    {
      ioctl (tty, TIOCSETC, &(tiop->tchars));
      tiop->flags &= ~TCHARS_SET;
    }
#endif

#if defined (TIOCSLTC)
  if (tiop->flags & LTCHARS_SET)
    {
      ioctl (tty, TIOCSLTC, &(tiop->ltchars));
      tiop->flags &= ~LTCHARS_SET;
    }
#endif

  return 0;
}

static void
unknown's avatar
unknown committed
296
prepare_terminal_settings (meta_flag, oldtio, tiop)
unknown's avatar
unknown committed
297
     int meta_flag;
unknown's avatar
unknown committed
298
     TIOTYPE oldtio, *tiop;
unknown's avatar
unknown committed
299
{
unknown's avatar
unknown committed
300
  readline_echoing_p = (oldtio.sgttyb.sg_flags & ECHO);
unknown's avatar
unknown committed
301 302 303

  /* Copy the original settings to the structure we're going to use for
     our settings. */
unknown's avatar
unknown committed
304 305
  tiop->sgttyb = oldtio.sgttyb;
  tiop->lflag = oldtio.lflag;
unknown's avatar
unknown committed
306
#if defined (TIOCGETC)
unknown's avatar
unknown committed
307
  tiop->tchars = oldtio.tchars;
unknown's avatar
unknown committed
308 309
#endif
#if defined (TIOCGLTC)
unknown's avatar
unknown committed
310
  tiop->ltchars = oldtio.ltchars;
unknown's avatar
unknown committed
311
#endif
unknown's avatar
unknown committed
312
  tiop->flags = oldtio.flags;
unknown's avatar
unknown committed
313 314 315 316 317 318 319 320 321 322 323 324

  /* First, the basic settings to put us into character-at-a-time, no-echo
     input mode. */
  tiop->sgttyb.sg_flags &= ~(ECHO | CRMOD);
  tiop->sgttyb.sg_flags |= CBREAK;

  /* If this terminal doesn't care how the 8th bit is used, then we can
     use it for the meta-key.  If only one of even or odd parity is
     specified, then the terminal is using parity, and we cannot. */
#if !defined (ANYP)
#  define ANYP (EVENP | ODDP)
#endif
unknown's avatar
unknown committed
325 326
  if (((oldtio.sgttyb.sg_flags & ANYP) == ANYP) ||
      ((oldtio.sgttyb.sg_flags & ANYP) == 0))
unknown's avatar
unknown committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    {
      tiop->sgttyb.sg_flags |= ANYP;

      /* Hack on local mode flags if we can. */
#if defined (TIOCLGET)
#  if defined (LPASS8)
      tiop->lflag |= LPASS8;
#  endif /* LPASS8 */
#endif /* TIOCLGET */
    }

#if defined (TIOCGETC)
#  if defined (USE_XON_XOFF)
  /* Get rid of terminal output start and stop characters. */
  tiop->tchars.t_stopc = -1; /* C-s */
  tiop->tchars.t_startc = -1; /* C-q */

  /* If there is an XON character, bind it to restart the output. */
unknown's avatar
unknown committed
345 346
  if (oldtio.tchars.t_startc != -1)
    rl_bind_key (oldtio.tchars.t_startc, rl_restart_output);
unknown's avatar
unknown committed
347 348 349
#  endif /* USE_XON_XOFF */

  /* If there is an EOF char, bind _rl_eof_char to it. */
unknown's avatar
unknown committed
350 351
  if (oldtio.tchars.t_eofc != -1)
    _rl_eof_char = oldtio.tchars.t_eofc;
unknown's avatar
unknown committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

#  if defined (NO_KILL_INTR)
  /* Get rid of terminal-generated SIGQUIT and SIGINT. */
  tiop->tchars.t_quitc = -1; /* C-\ */
  tiop->tchars.t_intrc = -1; /* C-c */
#  endif /* NO_KILL_INTR */
#endif /* TIOCGETC */

#if defined (TIOCGLTC)
  /* Make the interrupt keys go away.  Just enough to make people happy. */
  tiop->ltchars.t_dsuspc = -1;	/* C-y */
  tiop->ltchars.t_lnextc = -1;	/* C-v */
#endif /* TIOCGLTC */
}

#else  /* !defined (NEW_TTY_DRIVER) */

#if !defined (VMIN)
#  define VMIN VEOF
#endif

#if !defined (VTIME)
#  define VTIME VEOL
#endif

#if defined (TERMIOS_TTY_DRIVER)
#  define TIOTYPE struct termios
#  define DRAIN_OUTPUT(fd)	tcdrain (fd)
#  define GETATTR(tty, tiop)	(tcgetattr (tty, tiop))
#  ifdef M_UNIX
#    define SETATTR(tty, tiop)	(tcsetattr (tty, TCSANOW, tiop))
#  else
#    define SETATTR(tty, tiop)	(tcsetattr (tty, TCSADRAIN, tiop))
#  endif /* !M_UNIX */
#else
#  define TIOTYPE struct termio
#  define DRAIN_OUTPUT(fd)
#  define GETATTR(tty, tiop)	(ioctl (tty, TCGETA, tiop))
unknown's avatar
unknown committed
390
#  define SETATTR(tty, tiop)	(ioctl (tty, TCSETAW, tiop))
unknown's avatar
unknown committed
391 392 393 394
#endif /* !TERMIOS_TTY_DRIVER */

static TIOTYPE otio;

unknown's avatar
unknown committed
395 396 397 398 399 400 401 402
static void save_tty_chars PARAMS((TIOTYPE *));
static int _get_tty_settings PARAMS((int, TIOTYPE *));
static int get_tty_settings PARAMS((int, TIOTYPE *));
static int _set_tty_settings PARAMS((int, TIOTYPE *));
static int set_tty_settings PARAMS((int, TIOTYPE *));

static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));

unknown's avatar
unknown committed
403 404 405 406 407 408
#if defined (FLUSHO)
#  define OUTPUT_BEING_FLUSHED(tp)  (tp->c_lflag & FLUSHO)
#else
#  define OUTPUT_BEING_FLUSHED(tp)  0
#endif

unknown's avatar
unknown committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
static void
save_tty_chars (tiop)
     TIOTYPE *tiop;
{
  _rl_last_tty_chars = _rl_tty_chars;

  _rl_tty_chars.t_eof = tiop->c_cc[VEOF];
  _rl_tty_chars.t_eol = tiop->c_cc[VEOL];
#ifdef VEOL2
  _rl_tty_chars.t_eol2 = tiop->c_cc[VEOL2];
#endif
  _rl_tty_chars.t_erase = tiop->c_cc[VERASE];
#ifdef VWERASE
  _rl_tty_chars.t_werase = tiop->c_cc[VWERASE];
#endif
  _rl_tty_chars.t_kill = tiop->c_cc[VKILL];
#ifdef VREPRINT
  _rl_tty_chars.t_reprint = tiop->c_cc[VREPRINT];
#endif
  _rl_tty_chars.t_intr = tiop->c_cc[VINTR];
  _rl_tty_chars.t_quit = tiop->c_cc[VQUIT];
#ifdef VSUSP
  _rl_tty_chars.t_susp = tiop->c_cc[VSUSP];
#endif
#ifdef VDSUSP
  _rl_tty_chars.t_dsusp = tiop->c_cc[VDSUSP];
#endif
#ifdef VSTART
  _rl_tty_chars.t_start = tiop->c_cc[VSTART];
#endif
#ifdef VSTOP
  _rl_tty_chars.t_stop = tiop->c_cc[VSTOP];
#endif
#ifdef VLNEXT
  _rl_tty_chars.t_lnext = tiop->c_cc[VLNEXT];
#endif
#ifdef VDISCARD
  _rl_tty_chars.t_flush = tiop->c_cc[VDISCARD];
#endif
#ifdef VSTATUS
  _rl_tty_chars.t_status = tiop->c_cc[VSTATUS];
#endif
}

#if defined (_AIX) || defined (_AIX41)
/* Currently this is only used on AIX */
455 456 457 458 459 460 461 462
static void
rltty_warning (msg)
     char *msg;
{
  fprintf (stderr, "readline: warning: %s\n", msg);
}
#endif

unknown's avatar
unknown committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476
#if defined (_AIX)
void
setopost(tp)
TIOTYPE *tp;
{
  if ((tp->c_oflag & OPOST) == 0)
    {
      rltty_warning ("turning on OPOST for terminal\r");
      tp->c_oflag |= OPOST|ONLCR;
    }
}
#endif

static int
unknown's avatar
unknown committed
477
_get_tty_settings (tty, tiop)
unknown's avatar
unknown committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
     int tty;
     TIOTYPE *tiop;
{
  int ioctl_ret;

  while (1)
    {
      ioctl_ret = GETATTR (tty, tiop);
      if (ioctl_ret < 0)
	{
	  if (errno != EINTR)
	    return -1;
	  else
	    continue;
	}
      if (OUTPUT_BEING_FLUSHED (tiop))
	{
#if defined (FLUSHO) && defined (_AIX41)
	  rltty_warning ("turning off output flushing");
	  tiop->c_lflag &= ~FLUSHO;
	  break;
#else
	  continue;
#endif
	}
      break;
    }

unknown's avatar
unknown committed
506 507 508 509 510 511 512 513 514 515 516 517 518
  return 0;
}

static int
get_tty_settings (tty, tiop)
     int tty;
     TIOTYPE *tiop;
{
  set_winsize (tty);

  if (_get_tty_settings (tty, tiop) < 0)
    return -1;

unknown's avatar
unknown committed
519 520 521 522 523 524 525 526
#if defined (_AIX)
  setopost(tiop);
#endif

  return 0;
}

static int
unknown's avatar
unknown committed
527
_set_tty_settings (tty, tiop)
unknown's avatar
unknown committed
528 529 530 531 532 533 534 535 536
     int tty;
     TIOTYPE *tiop;
{
  while (SETATTR (tty, tiop) < 0)
    {
      if (errno != EINTR)
	return -1;
      errno = 0;
    }
unknown's avatar
unknown committed
537 538
  return 0;
}
unknown's avatar
unknown committed
539

unknown's avatar
unknown committed
540 541 542 543 544 545 546 547
static int
set_tty_settings (tty, tiop)
     int tty;
     TIOTYPE *tiop;
{
  if (_set_tty_settings (tty, tiop) < 0)
    return -1;
    
unknown's avatar
unknown committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
#if 0

#if defined (TERMIOS_TTY_DRIVER)
#  if defined (__ksr1__)
  if (ksrflow)
    {
      ksrflow = 0;
      tcflow (tty, TCOON);
    }
#  else /* !ksr1 */
  tcflow (tty, TCOON);		/* Simulate a ^Q. */
#  endif /* !ksr1 */
#else
  ioctl (tty, TCXONC, 1);	/* Simulate a ^Q. */
#endif /* !TERMIOS_TTY_DRIVER */

unknown's avatar
unknown committed
564
#endif /* 0 */
unknown's avatar
unknown committed
565 566 567 568 569

  return 0;
}

static void
unknown's avatar
unknown committed
570
prepare_terminal_settings (meta_flag, oldtio, tiop)
unknown's avatar
unknown committed
571
     int meta_flag;
unknown's avatar
unknown committed
572
     TIOTYPE oldtio, *tiop;
unknown's avatar
unknown committed
573
{
unknown's avatar
unknown committed
574
  readline_echoing_p = (oldtio.c_lflag & ECHO);
unknown's avatar
unknown committed
575 576 577

  tiop->c_lflag &= ~(ICANON | ECHO);

unknown's avatar
unknown committed
578 579
  if ((unsigned char) oldtio.c_cc[VEOF] != (unsigned char) _POSIX_VDISABLE)
    _rl_eof_char = oldtio.c_cc[VEOF];
unknown's avatar
unknown committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

#if defined (USE_XON_XOFF)
#if defined (IXANY)
  tiop->c_iflag &= ~(IXON | IXOFF | IXANY);
#else
  /* `strict' Posix systems do not define IXANY. */
  tiop->c_iflag &= ~(IXON | IXOFF);
#endif /* IXANY */
#endif /* USE_XON_XOFF */

  /* Only turn this off if we are using all 8 bits. */
  if (((tiop->c_cflag & CSIZE) == CS8) || meta_flag)
    tiop->c_iflag &= ~(ISTRIP | INPCK);

  /* Make sure we differentiate between CR and NL on input. */
  tiop->c_iflag &= ~(ICRNL | INLCR);

#if !defined (HANDLE_SIGNALS)
  tiop->c_lflag &= ~ISIG;
#else
  tiop->c_lflag |= ISIG;
#endif

  tiop->c_cc[VMIN] = 1;
  tiop->c_cc[VTIME] = 0;

#if defined (FLUSHO)
  if (OUTPUT_BEING_FLUSHED (tiop))
    {
      tiop->c_lflag &= ~FLUSHO;
unknown's avatar
unknown committed
610
      oldtio.c_lflag &= ~FLUSHO;
unknown's avatar
unknown committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    }
#endif

  /* Turn off characters that we need on Posix systems with job control,
     just to be sure.  This includes ^Y and ^V.  This should not really
     be necessary.  */
#if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_VDISABLE)

#if defined (VLNEXT)
  tiop->c_cc[VLNEXT] = _POSIX_VDISABLE;
#endif

#if defined (VDSUSP)
  tiop->c_cc[VDSUSP] = _POSIX_VDISABLE;
#endif

#endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */
}
#endif  /* NEW_TTY_DRIVER */

/* Put the terminal in CBREAK mode so that we can detect key presses. */
void
rl_prep_terminal (meta_flag)
     int meta_flag;
{
  int tty;
  TIOTYPE tio;

  if (terminal_prepped)
    return;

  /* Try to keep this function from being INTerrupted. */
  block_sigint ();

  tty = fileno (rl_instream);

  if (get_tty_settings (tty, &tio) < 0)
    {
      release_sigint ();
      return;
    }

  otio = tio;

unknown's avatar
unknown committed
655 656
  save_tty_chars (&otio);

unknown's avatar
unknown committed
657 658 659 660 661 662 663 664 665 666 667 668 669
  prepare_terminal_settings (meta_flag, otio, &tio);

  if (set_tty_settings (tty, &tio) < 0)
    {
      release_sigint ();
      return;
    }

  if (_rl_enable_keypad)
    _rl_control_keypad (1);

  fflush (rl_outstream);
  terminal_prepped = 1;
unknown's avatar
unknown committed
670
  RL_SETSTATE(RL_STATE_TERMPREPPED);
unknown's avatar
unknown committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

  release_sigint ();
}

/* Restore the terminal's normal settings and modes. */
void
rl_deprep_terminal ()
{
  int tty;

  if (!terminal_prepped)
    return;

  /* Try to keep this function from being interrupted. */
  block_sigint ();

  tty = fileno (rl_instream);

  if (_rl_enable_keypad)
    _rl_control_keypad (0);

  fflush (rl_outstream);

  if (set_tty_settings (tty, &otio) < 0)
    {
      release_sigint ();
      return;
    }

  terminal_prepped = 0;
unknown's avatar
unknown committed
701
  RL_UNSETSTATE(RL_STATE_TERMPREPPED);
unknown's avatar
unknown committed
702 703 704 705 706 707 708 709 710 711 712

  release_sigint ();
}

/* **************************************************************** */
/*								    */
/*			Bogus Flow Control      		    */
/*								    */
/* **************************************************************** */

int
unknown's avatar
unknown committed
713 714
rl_restart_output (count, key)
     int count __attribute__((unused)), key __attribute__((unused));
unknown's avatar
unknown committed
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
{
  int fildes = fileno (rl_outstream);
#if defined (TIOCSTART)
#if defined (apollo)
  ioctl (&fildes, TIOCSTART, 0);
#else
  ioctl (fildes, TIOCSTART, 0);
#endif /* apollo */

#else /* !TIOCSTART */
#  if defined (TERMIOS_TTY_DRIVER)
#    if defined (__ksr1__)
  if (ksrflow)
    {
      ksrflow = 0;
      tcflow (fildes, TCOON);
    }
#    else /* !ksr1 */
  tcflow (fildes, TCOON);		/* Simulate a ^Q. */
#    endif /* !ksr1 */
#  else /* !TERMIOS_TTY_DRIVER */
#    if defined (TCXONC)
  ioctl (fildes, TCXONC, TCOON);
#    endif /* TCXONC */
#  endif /* !TERMIOS_TTY_DRIVER */
#endif /* !TIOCSTART */

  return 0;
}

int
unknown's avatar
unknown committed
746 747
rl_stop_output (count, key)
     int count __attribute__((unused)), key __attribute__((unused));
unknown's avatar
unknown committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
{
  int fildes = fileno (rl_instream);

#if defined (TIOCSTOP)
# if defined (apollo)
  ioctl (&fildes, TIOCSTOP, 0);
# else
  ioctl (fildes, TIOCSTOP, 0);
# endif /* apollo */
#else /* !TIOCSTOP */
# if defined (TERMIOS_TTY_DRIVER)
#  if defined (__ksr1__)
  ksrflow = 1;
#  endif /* ksr1 */
  tcflow (fildes, TCOOFF);
# else
#   if defined (TCXONC)
  ioctl (fildes, TCXONC, TCOON);
#   endif /* TCXONC */
# endif /* !TERMIOS_TTY_DRIVER */
#endif /* !TIOCSTOP */

  return 0;
}

/* **************************************************************** */
/*								    */
/*			Default Key Bindings			    */
/*								    */
/* **************************************************************** */
unknown's avatar
unknown committed
778 779 780

/* Set the system's default editing characters to their readline equivalents
   in KMAP.  Should be static, now that we have rl_tty_set_default_bindings. */
unknown's avatar
unknown committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794
void
rltty_set_default_bindings (kmap)
     Keymap kmap;
{
  TIOTYPE ttybuff;
  int tty = fileno (rl_instream);

#if defined (NEW_TTY_DRIVER)

#define SET_SPECIAL(sc, func) \
  do \
    { \
      int ic; \
      ic = sc; \
unknown's avatar
unknown committed
795 796
      if (ic != -1 && kmap[(unsigned char)ic].type == ISFUNC) \
	kmap[(unsigned char)ic].function = func; \
unknown's avatar
unknown committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
    } \
  while (0)

  if (get_tty_settings (tty, &ttybuff) == 0)
    {
      if (ttybuff.flags & SGTTY_SET)
	{
	  SET_SPECIAL (ttybuff.sgttyb.sg_erase, rl_rubout);
	  SET_SPECIAL (ttybuff.sgttyb.sg_kill, rl_unix_line_discard);
	}

#  if defined (TIOCGLTC)
      if (ttybuff.flags & LTCHARS_SET)
	{
	  SET_SPECIAL (ttybuff.ltchars.t_werasc, rl_unix_word_rubout);
	  SET_SPECIAL (ttybuff.ltchars.t_lnextc, rl_quoted_insert);
	}
#  endif /* TIOCGLTC */
    }

#else /* !NEW_TTY_DRIVER */

#define SET_SPECIAL(sc, func) \
  do \
    { \
      unsigned char uc; \
      uc = ttybuff.c_cc[sc]; \
      if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) \
	kmap[uc].function = func; \
    } \
  while (0)

  if (get_tty_settings (tty, &ttybuff) == 0)
    {
      SET_SPECIAL (VERASE, rl_rubout);
      SET_SPECIAL (VKILL, rl_unix_line_discard);

#  if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
      SET_SPECIAL (VLNEXT, rl_quoted_insert);
#  endif /* VLNEXT && TERMIOS_TTY_DRIVER */

#  if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
      SET_SPECIAL (VWERASE, rl_unix_word_rubout);
#  endif /* VWERASE && TERMIOS_TTY_DRIVER */
    }
#endif /* !NEW_TTY_DRIVER */
}
unknown's avatar
unknown committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911

/* New public way to set the system default editing chars to their readline
   equivalents. */
void
rl_tty_set_default_bindings (kmap)
     Keymap kmap;
{
  rltty_set_default_bindings (kmap);
}

#if defined (HANDLE_SIGNALS)

#if defined (NEW_TTY_DRIVER)
int
_rl_disable_tty_signals ()
{
  return 0;
}

int
_rl_restore_tty_signals ()
{
  return 0;
}
#else

static TIOTYPE sigstty, nosigstty;
static int tty_sigs_disabled = 0;

int
_rl_disable_tty_signals ()
{
  if (tty_sigs_disabled)
    return 0;

  if (_get_tty_settings (fileno (rl_instream), &sigstty) < 0)
    return -1;

  nosigstty = sigstty;

  nosigstty.c_lflag &= ~ISIG;
  nosigstty.c_iflag &= ~IXON;

  if (_set_tty_settings (fileno (rl_instream), &nosigstty) < 0)
    return (_set_tty_settings (fileno (rl_instream), &sigstty));

  tty_sigs_disabled = 1;
  return 0;
}

int
_rl_restore_tty_signals ()
{
  int r;

  if (tty_sigs_disabled == 0)
    return 0;

  r = _set_tty_settings (fileno (rl_instream), &sigstty);

  if (r == 0)
    tty_sigs_disabled = 0;

  return r;
}
#endif /* !NEW_TTY_DRIVER */

#endif /* HANDLE_SIGNALS */