Readline 5.1 import for HEAD.
[deliverable/binutils-gdb.git] / readline / rltty.c
index 2ccb9d1a56621800a9f0309c5260509858fcbd2e..9a0326ed2095994a048fa9d6204916b30b98fc90 100644 (file)
@@ -1,7 +1,7 @@
 /* rltty.c -- functions to prepare and restore the terminal for readline's
    use. */
 
-/* Copyright (C) 1992 Free Software Foundation, Inc.
+/* Copyright (C) 1992-2005 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.
@@ -188,6 +188,8 @@ static int set_tty_settings PARAMS((int, TIOTYPE *));
 
 static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));
 
+static void set_special_char PARAMS((Keymap, TIOTYPE *, int, rl_command_func_t));
+
 static void
 save_tty_chars (tiop)
      TIOTYPE *tiop;
@@ -229,12 +231,11 @@ get_tty_settings (tty, tiop)
      int tty;
      TIOTYPE *tiop;
 {
-#if defined (TIOCGWINSZ)
   set_winsize (tty);
-#endif
 
   tiop->flags = tiop->lflag = 0;
 
+  errno = 0;
   if (ioctl (tty, TIOCGETP, &(tiop->sgttyb)) < 0)
     return -1;
   tiop->flags |= SGTTY_SET;
@@ -404,6 +405,9 @@ static int set_tty_settings PARAMS((int, TIOTYPE *));
 
 static void prepare_terminal_settings PARAMS((int, TIOTYPE, TIOTYPE *));
 
+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)
 #  define OUTPUT_BEING_FLUSHED(tp)  (tp->c_lflag & FLUSHO)
 #else
@@ -515,10 +519,9 @@ get_tty_settings (tty, tiop)
      int tty;
      TIOTYPE *tiop;
 {
-#if defined (TIOCGWINSZ)
   set_winsize (tty);
-#endif
 
+  errno = 0;
   if (_get_tty_settings (tty, tiop) < 0)
     return -1;
 
@@ -632,10 +635,9 @@ prepare_terminal_settings (meta_flag, oldtio, tiop)
 
 #endif /* TERMIOS_TTY_DRIVER && _POSIX_VDISABLE */
 }
-#endif  /* NEW_TTY_DRIVER */
+#endif  /* !NEW_TTY_DRIVER */
 
-/* Put the terminal in CBREAK mode so that we can detect key
-   presses. */
+/* Put the terminal in CBREAK mode so that we can detect key presses. */
 #if defined (NO_TTY_DRIVER)
 void
 rl_prep_terminal (meta_flag)
@@ -650,7 +652,6 @@ rl_deprep_terminal ()
 }
 
 #else /* ! NO_TTY_DRIVER */
-/* Put the terminal in CBREAK mode so that we can detect key presses. */
 void
 rl_prep_terminal (meta_flag)
      int meta_flag;
@@ -668,13 +669,43 @@ rl_prep_terminal (meta_flag)
 
   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)
+#else
+      if (errno == ENOTTY)
+#endif
+       readline_echoing_p = 1;         /* XXX */
       release_sigint ();
       return;
     }
 
   otio = tio;
 
+  if (_rl_bind_stty_chars)
+    {
+#if defined (VI_MODE)
+      /* If editing in vi mode, make sure we restore the bindings in the
+        insertion keymap no matter what keymap we ended up in. */
+      if (rl_editing_mode == vi_mode)
+       rl_tty_unset_default_bindings (vi_insertion_keymap);
+      else
+#endif
+       rl_tty_unset_default_bindings (_rl_keymap);
+    }
   save_tty_chars (&otio);
+  RL_SETSTATE(RL_STATE_TTYCSAVED);
+  if (_rl_bind_stty_chars)
+    {
+#if defined (VI_MODE)
+      /* 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);  
+      else
+#endif
+       _rl_bind_tty_special_chars (_rl_keymap, tio);
+    }
 
   prepare_terminal_settings (meta_flag, otio, &tio);
 
@@ -809,71 +840,105 @@ rl_stop_output (count, key)
 /*                                                                 */
 /* **************************************************************** */
 
-/* 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;
-{
 #if !defined (NO_TTY_DRIVER)
-  TIOTYPE ttybuff;
-  int tty = fileno (rl_instream);
+#define SET_SPECIAL(sc, func)  set_special_char(kmap, &ttybuff, sc, func)
+#endif
 
-#if defined (NEW_TTY_DRIVER)
+#if defined (NO_TTY_DRIVER)
 
-#define SET_SPECIAL(sc, func) \
-  do \
-    { \
-      int ic; \
-      ic = sc; \
-      if (ic != -1 && kmap[(unsigned char)ic].type == ISFUNC) \
-       kmap[(unsigned char)ic].function = func; \
-    } \
-  while (0)
+#define SET_SPECIAL(sc, func)
+#define RESET_SPECIAL(c)
 
-  if (get_tty_settings (tty, &ttybuff) == 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;
+{
+  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)
+    kmap[(unsigned char)c].function = rl_insert;
+
+static void
+_rl_bind_tty_special_chars (kmap, ttybuff)
+     Keymap kmap;
+     TIOTYPE ttybuff;
+{
+  if (ttybuff.flags & SGTTY_SET)
     {
-      if (ttybuff.flags & SGTTY_SET)
-       {
-         SET_SPECIAL (ttybuff.sgttyb.sg_erase, rl_rubout);
-         SET_SPECIAL (ttybuff.sgttyb.sg_kill, rl_unix_line_discard);
-       }
+      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 */
+  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 */
+static void
+set_special_char (kmap, tiop, sc, func)
+     Keymap kmap;
+     TIOTYPE *tiop;
+     int sc;
+     rl_command_func_t *func;
+{
+  unsigned char uc;
 
-#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)
+  uc = tiop->c_cc[sc];
+  if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC)
+    kmap[uc].function = func;
+}
 
-  if (get_tty_settings (tty, &ttybuff) == 0)
-    {
-      SET_SPECIAL (VERASE, rl_rubout);
-      SET_SPECIAL (VKILL, rl_unix_line_discard);
+/* used later */
+#define RESET_SPECIAL(uc) \
+  if (uc != (unsigned char)_POSIX_VDISABLE && kmap[uc].type == ISFUNC) \
+    kmap[uc].function = rl_insert;
+
+static void
+_rl_bind_tty_special_chars (kmap, ttybuff)
+     Keymap kmap;
+     TIOTYPE ttybuff;
+{
+  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);
+  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);
+  SET_SPECIAL (VWERASE, rl_unix_word_rubout);
 #  endif /* VWERASE && TERMIOS_TTY_DRIVER */
-    }
+}
+
 #endif /* !NEW_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;
+{
+#if !defined (NO_TTY_DRIVER)
+  TIOTYPE ttybuff;
+  int tty;
+  static int called = 0;
+
+  tty = fileno (rl_instream);
+
+  if (get_tty_settings (tty, &ttybuff) == 0)
+    _rl_bind_tty_special_chars (kmap, ttybuff);
 #endif
 }
 
@@ -886,6 +951,30 @@ rl_tty_set_default_bindings (kmap)
   rltty_set_default_bindings (kmap);
 }
 
+/* Rebind all of the tty special chars that readline worries about back
+   to self-insert.  Call this before saving the current terminal special
+   chars with save_tty_chars().  This only works on POSIX termios or termio
+   systems. */
+void
+rl_tty_unset_default_bindings (kmap)
+     Keymap kmap;
+{
+  /* Don't bother before we've saved the tty special chars at least once. */
+  if (RL_ISSTATE(RL_STATE_TTYCSAVED) == 0)
+    return;
+
+  RESET_SPECIAL (_rl_tty_chars.t_erase);
+  RESET_SPECIAL (_rl_tty_chars.t_kill);
+
+#  if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
+  RESET_SPECIAL (_rl_tty_chars.t_lnext);
+#  endif /* VLNEXT && TERMIOS_TTY_DRIVER */
+
+#  if defined (VWERASE) && defined (TERMIOS_TTY_DRIVER)
+  RESET_SPECIAL (_rl_tty_chars.t_werase);
+#  endif /* VWERASE && TERMIOS_TTY_DRIVER */
+}
+
 #if defined (HANDLE_SIGNALS)
 
 #if defined (NEW_TTY_DRIVER) || defined (NO_TTY_DRIVER)
This page took 0.027755 seconds and 4 git commands to generate.