/* rltty.c -- functions to prepare and restore the terminal for readline's
use. */
-/* Copyright (C) 1992-2005 Free Software Foundation, Inc.
+/* Copyright (C) 1992-2017 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.
+ This file is part of the GNU Readline Library (Readline), 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
- as published by the Free Software Foundation; either version 2, or
+ Readline is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
(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
+ Readline 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,
- 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+ You should have received a copy of the GNU General Public License
+ along with Readline. If not, see <http://www.gnu.org/licenses/>.
+*/
+
#define READLINE_LIBRARY
#if defined (HAVE_CONFIG_H)
#include "rldefs.h"
-#if defined (GWINSZ_IN_SYS_IOCTL)
-# include <sys/ioctl.h>
-#endif /* GWINSZ_IN_SYS_IOCTL */
-
#include "rltty.h"
+#if defined (HAVE_SYS_IOCTL_H)
+# include <sys/ioctl.h> /* include for declaration of ioctl */
+#endif
+
#include "readline.h"
#include "rlprivate.h"
rl_vintfunc_t *rl_prep_term_function = rl_prep_terminal;
rl_voidfunc_t *rl_deprep_term_function = rl_deprep_terminal;
-void _rl_block_sigint PARAMS((void));
-void _rl_release_sigint PARAMS((void));
-
static void set_winsize PARAMS((int));
-/* **************************************************************** */
-/* */
-/* 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
- _rl_release_sigint(). */
-void
-_rl_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 */
-
- sigint_blocked = 1;
-}
-
-/* Allow SIGINT to be delivered. */
-void
-_rl_release_sigint ()
-{
- if (sigint_blocked == 0)
- 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. */
+/* Non-zero means that the terminal is in a prepped state. There are several
+ flags that are OR'd in to denote whether or not we have sent various
+ init strings to the terminal. */
+#define TPX_PREPPED 0x01
+#define TPX_BRACKPASTE 0x02
+#define TPX_METAKEY 0x04
+
static int terminal_prepped;
static _RL_TTY_CHARS _rl_tty_chars, _rl_last_tty_chars;
static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));
-static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t));
+static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t *));
static void
-save_tty_chars (tiop)
- TIOTYPE *tiop;
+save_tty_chars (TIOTYPE *tiop)
{
_rl_last_tty_chars = _rl_tty_chars;
if (tiop->flags & TCHARS_SET)
{
- _rl_tty_chars.t_intr = tiop->tchars.t_intrc;
- _rl_tty_chars.t_quit = tiop->tchars.t_quitc;
+ _rl_intr_char = _rl_tty_chars.t_intr = tiop->tchars.t_intrc;
+ _rl_quit_char = _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;
if (tiop->flags & LTCHARS_SET)
{
- _rl_tty_chars.t_susp = tiop->ltchars.t_suspc;
+ _rl_susp_char = _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;
}
static int
-get_tty_settings (tty, tiop)
- int tty;
- TIOTYPE *tiop;
+get_tty_settings (int tty, TIOTYPE *tiop)
{
set_winsize (tty);
}
static int
-set_tty_settings (tty, tiop)
- int tty;
- TIOTYPE *tiop;
+set_tty_settings (int tty, TIOTYPE *tiop)
{
if (tiop->flags & SGTTY_SET)
{
ioctl (tty, TIOCSETN, &(tiop->sgttyb));
tiop->flags &= ~SGTTY_SET;
}
- readline_echoing_p = 1;
+ _rl_echoing_p = 1;
#if defined (TIOCLSET)
if (tiop->flags & LFLAG_SET)
}
static void
-prepare_terminal_settings (meta_flag, oldtio, tiop)
- int meta_flag;
- TIOTYPE oldtio, *tiop;
+prepare_terminal_settings (int meta_flag, TIOTYPE oldtio, TIOTYPE *tiop)
{
- readline_echoing_p = (oldtio.sgttyb.sg_flags & ECHO);
+ _rl_echoing_p = (oldtio.sgttyb.sg_flags & ECHO);
+ _rl_echoctl = (oldtio.sgttyb.sg_flags & ECHOCTL);
/* Copy the original settings to the structure we're going to use for
our settings. */
static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));
-static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t));
+static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t *));
static void _rl_bind_tty_special_chars PARAMS((Keymap, TIOTYPE));
#if defined (FLUSHO)
#endif
static void
-save_tty_chars (tiop)
- TIOTYPE *tiop;
+save_tty_chars (TIOTYPE *tiop)
{
_rl_last_tty_chars = _rl_tty_chars;
#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];
+ _rl_intr_char = _rl_tty_chars.t_intr = tiop->c_cc[VINTR];
+ _rl_quit_char = _rl_tty_chars.t_quit = tiop->c_cc[VQUIT];
#ifdef VSUSP
- _rl_tty_chars.t_susp = tiop->c_cc[VSUSP];
+ _rl_susp_char = _rl_tty_chars.t_susp = tiop->c_cc[VSUSP];
#endif
#ifdef VDSUSP
_rl_tty_chars.t_dsusp = tiop->c_cc[VDSUSP];
#if defined (_AIX) || defined (_AIX41)
/* Currently this is only used on AIX */
static void
-rltty_warning (msg)
- char *msg;
+rltty_warning (char *msg)
{
- fprintf (stderr, "readline: warning: %s\n", msg);
+ _rl_errmsg ("warning: %s", msg);
}
#endif
#if defined (_AIX)
void
-setopost(tp)
-TIOTYPE *tp;
+setopost (TIOTYPE *tp)
{
if ((tp->c_oflag & OPOST) == 0)
{
- rltty_warning ("turning on OPOST for terminal\r");
+ _rl_errmsg ("warning: turning on OPOST for terminal\r");
tp->c_oflag |= OPOST|ONLCR;
}
}
#endif
static int
-_get_tty_settings (tty, tiop)
- int tty;
- TIOTYPE *tiop;
+_get_tty_settings (int tty, TIOTYPE *tiop)
{
int ioctl_ret;
}
if (OUTPUT_BEING_FLUSHED (tiop))
{
-#if defined (FLUSHO) && defined (_AIX41)
- rltty_warning ("turning off output flushing");
+#if defined (FLUSHO)
+ _rl_errmsg ("warning: turning off output flushing");
tiop->c_lflag &= ~FLUSHO;
break;
#else
}
static int
-get_tty_settings (tty, tiop)
- int tty;
- TIOTYPE *tiop;
+get_tty_settings (int tty, TIOTYPE *tiop)
{
set_winsize (tty);
}
static int
-_set_tty_settings (tty, tiop)
- int tty;
- TIOTYPE *tiop;
+_set_tty_settings (int tty, TIOTYPE *tiop)
{
while (SETATTR (tty, tiop) < 0)
{
}
static int
-set_tty_settings (tty, tiop)
- int tty;
- TIOTYPE *tiop;
+set_tty_settings (int tty, TIOTYPE *tiop)
{
if (_set_tty_settings (tty, tiop) < 0)
return -1;
}
static void
-prepare_terminal_settings (meta_flag, oldtio, tiop)
- int meta_flag;
- TIOTYPE oldtio, *tiop;
+prepare_terminal_settings (int meta_flag, TIOTYPE oldtio, TIOTYPE *tiop)
{
- readline_echoing_p = (oldtio.c_lflag & ECHO);
+ int sc;
+ Keymap kmap;
+
+ _rl_echoing_p = (oldtio.c_lflag & ECHO);
+#if defined (ECHOCTL)
+ _rl_echoctl = (oldtio.c_lflag & ECHOCTL);
+#endif
tiop->c_lflag &= ~(ICANON | ECHO);
#if defined (USE_XON_XOFF)
#if defined (IXANY)
- tiop->c_iflag &= ~(IXON | IXOFF | IXANY);
+ tiop->c_iflag &= ~(IXON | IXANY);
#else
/* `strict' Posix systems do not define IXANY. */
- tiop->c_iflag &= ~(IXON | IXOFF);
+ tiop->c_iflag &= ~IXON;
#endif /* IXANY */
#endif /* USE_XON_XOFF */
tiop->c_cc[VDSUSP] = _POSIX_VDISABLE;
#endif
+ /* Conditionally disable some other tty special characters if there is a
+ key binding for them in the current keymap. Readline ordinarily doesn't
+ bind these characters, but an application or user might. */
+#if defined (VI_MODE)
+ kmap = (rl_editing_mode == vi_mode) ? vi_insertion_keymap : _rl_keymap;
+#else
+ kmap = _rl_keymap;
+#endif
+#if defined (VDISCARD)
+ sc = tiop->c_cc[VDISCARD];
+ if (sc != _POSIX_VDISABLE && kmap[(unsigned char)sc].type == ISFUNC)
+ tiop->c_cc[VDISCARD] = _POSIX_VDISABLE;
+#endif /* VDISCARD */
+
#endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */
}
#endif /* !NEW_TTY_DRIVER */
/* Put the terminal in CBREAK mode so that we can detect key presses. */
#if defined (NO_TTY_DRIVER)
void
-rl_prep_terminal (meta_flag)
- int meta_flag;
+rl_prep_terminal (int meta_flag)
{
- readline_echoing_p = 1;
+ _rl_echoing_p = 1;
}
void
-rl_deprep_terminal ()
+rl_deprep_terminal (void)
{
}
#else /* ! NO_TTY_DRIVER */
void
-rl_prep_terminal (meta_flag)
- int meta_flag;
+rl_prep_terminal (int meta_flag)
{
- int tty;
+ int tty, nprep;
TIOTYPE tio;
if (terminal_prepped)
/* Try to keep this function from being INTerrupted. */
_rl_block_sigint ();
- tty = fileno (rl_instream);
+ tty = rl_instream ? fileno (rl_instream) : fileno (stdin);
if (get_tty_settings (tty, &tio) < 0)
{
#if defined (ENOTSUP)
- /* MacOS X, at least, lies about the value of errno if tcgetattr fails. */
- if (errno == ENOTTY || errno == ENOTSUP)
+ /* MacOS X and Linux, at least, lie about the value of errno if
+ tcgetattr fails. */
+ if (errno == ENOTTY || errno == EINVAL || errno == ENOTSUP)
#else
- if (errno == ENOTTY)
+ if (errno == ENOTTY || errno == EINVAL)
#endif
- readline_echoing_p = 1; /* XXX */
+ _rl_echoing_p = 1; /* XXX */
+
_rl_release_sigint ();
return;
}
/* If editing in vi mode, make sure we set the bindings in the
insertion keymap no matter what keymap we ended up in. */
if (rl_editing_mode == vi_mode)
- _rl_bind_tty_special_chars (vi_insertion_keymap, tio);
+ _rl_bind_tty_special_chars (vi_insertion_keymap, tio);
else
#endif
_rl_bind_tty_special_chars (_rl_keymap, tio);
if (_rl_enable_keypad)
_rl_control_keypad (1);
+ nprep = TPX_PREPPED;
+
+ if (_rl_enable_bracketed_paste)
+ {
+ fprintf (rl_outstream, BRACK_PASTE_INIT);
+ nprep |= TPX_BRACKPASTE;
+ }
+
fflush (rl_outstream);
- terminal_prepped = 1;
+ terminal_prepped = nprep;
RL_SETSTATE(RL_STATE_TERMPREPPED);
_rl_release_sigint ();
/* Restore the terminal's normal settings and modes. */
void
-rl_deprep_terminal ()
+rl_deprep_terminal (void)
{
int tty;
- if (!terminal_prepped)
+ if (terminal_prepped == 0)
return;
/* Try to keep this function from being interrupted. */
_rl_block_sigint ();
- tty = fileno (rl_instream);
+ tty = rl_instream ? fileno (rl_instream) : fileno (stdin);
+
+ if (terminal_prepped & TPX_BRACKPASTE)
+ {
+ fprintf (rl_outstream, BRACK_PASTE_FINI);
+ if (_rl_eof_found)
+ fprintf (rl_outstream, "\n");
+ }
if (_rl_enable_keypad)
_rl_control_keypad (0);
_rl_release_sigint ();
}
#endif /* !NO_TTY_DRIVER */
+
+/* Set readline's idea of whether or not it is echoing output to the terminal,
+ returning the old value. */
+int
+rl_tty_set_echoing (int u)
+{
+ int o;
+
+ o = _rl_echoing_p;
+ _rl_echoing_p = u;
+ return o;
+}
\f
/* **************************************************************** */
/* */
/* **************************************************************** */
int
-rl_restart_output (count, key)
- int count, key;
+rl_restart_output (int count, int key)
{
#if defined (__MINGW32__)
return 0;
}
int
-rl_stop_output (count, key)
- int count, key;
+rl_stop_output (int count, int key)
{
#if defined (__MINGW32__)
return 0;
#elif defined (NEW_TTY_DRIVER)
static void
-set_special_char (kmap, tiop, sc, func)
- Keymap kmap;
- TIOTYPE *tiop;
- int sc;
- rl_command_func_t *func;
+set_special_char (Keymap kmap, TIOTYPE *tiop, int sc, rl_command_func_t *func)
{
if (sc != -1 && kmap[(unsigned char)sc].type == ISFUNC)
kmap[(unsigned char)sc].function = func;
}
#define RESET_SPECIAL(c) \
- if (c != -1 && kmap[(unsigned char)c].type == ISFUNC)
+ if (c != -1 && kmap[(unsigned char)c].type == ISFUNC) \
kmap[(unsigned char)c].function = rl_insert;
static void
-_rl_bind_tty_special_chars (kmap, ttybuff)
- Keymap kmap;
- TIOTYPE ttybuff;
+_rl_bind_tty_special_chars (Keymap kmap, TIOTYPE ttybuff)
{
if (ttybuff.flags & SGTTY_SET)
{
#else /* !NEW_TTY_DRIVER */
static void
-set_special_char (kmap, tiop, sc, func)
- Keymap kmap;
- TIOTYPE *tiop;
- int sc;
- rl_command_func_t *func;
+set_special_char (Keymap kmap, TIOTYPE *tiop, int sc, rl_command_func_t *func)
{
unsigned char uc;
kmap[uc].function = rl_insert;
static void
-_rl_bind_tty_special_chars (kmap, ttybuff)
- Keymap kmap;
- TIOTYPE ttybuff;
+_rl_bind_tty_special_chars (Keymap kmap, TIOTYPE ttybuff)
{
SET_SPECIAL (VERASE, rl_rubout);
SET_SPECIAL (VKILL, rl_unix_line_discard);
# endif /* VLNEXT && TERMIOS_TTY_DRIVER */
# if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
+# if defined (VI_MODE)
+ if (rl_editing_mode == vi_mode)
+ SET_SPECIAL (VWERASE, rl_vi_unix_word_rubout);
+ else
+# endif
SET_SPECIAL (VWERASE, rl_unix_word_rubout);
# endif /* VWERASE && TERMIOS_TTY_DRIVER */
}
/* 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. */
void
-rltty_set_default_bindings (kmap)
- Keymap kmap;
+rltty_set_default_bindings (Keymap kmap)
{
#if !defined (NO_TTY_DRIVER)
TIOTYPE ttybuff;
int tty;
- static int called = 0;
tty = fileno (rl_instream);
/* New public way to set the system default editing chars to their readline
equivalents. */
void
-rl_tty_set_default_bindings (kmap)
- Keymap kmap;
+rl_tty_set_default_bindings (Keymap kmap)
{
rltty_set_default_bindings (kmap);
}
chars with save_tty_chars(). This only works on POSIX termios or termio
systems. */
void
-rl_tty_unset_default_bindings (kmap)
- Keymap kmap;
+rl_tty_unset_default_bindings (Keymap kmap)
{
/* Don't bother before we've saved the tty special chars at least once. */
if (RL_ISSTATE(RL_STATE_TTYCSAVED) == 0)
#if defined (NEW_TTY_DRIVER) || defined (NO_TTY_DRIVER)
int
-_rl_disable_tty_signals ()
+_rl_disable_tty_signals (void)
{
return 0;
}
int
-_rl_restore_tty_signals ()
+_rl_restore_tty_signals (void)
{
return 0;
}
static int tty_sigs_disabled = 0;
int
-_rl_disable_tty_signals ()
+_rl_disable_tty_signals (void)
{
if (tty_sigs_disabled)
return 0;
}
int
-_rl_restore_tty_signals ()
+_rl_restore_tty_signals (void)
{
int r;