]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - builtins/read.def
Bash-5.2 patch 11: reset readline timeout after read -e -t
[thirdparty/bash.git] / builtins / read.def
index 803bea3532571f7eeb6df0747e48284c728ab837..ddd91d32d5db90d341f3b9173d07719232826704 100644 (file)
@@ -1,7 +1,7 @@
 This file is read.def, from which is created read.c.
 It implements the builtin "read" in Bash.
 
-Copyright (C) 1987-2015 Free Software Foundation, Inc.
+Copyright (C) 1987-2021 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -30,7 +30,8 @@ if the -u option is supplied.  The line is split into fields as with word
 splitting, and the first word is assigned to the first NAME, the second
 word to the second NAME, and so on, with any leftover words assigned to
 the last NAME.  Only the characters found in $IFS are recognized as word
-delimiters.
+delimiters. By default, the backslash character escapes delimiter characters
+and newline.
 
 If no NAMEs are supplied, the line read is stored in the REPLY variable.
 
@@ -39,7 +40,7 @@ Options:
                variable ARRAY, starting at zero
   -d delim     continue until the first character of DELIM is read, rather
                than newline
-  -e   use Readline to obtain the line in an interactive shell
+  -e   use Readline to obtain the line
   -i text      use TEXT as the initial text for Readline
   -n nchars    return after reading NCHARS characters rather than waiting
                for a newline, but honor a delimiter if fewer than
@@ -93,6 +94,7 @@ $END
 #include "../shell.h"
 #include "common.h"
 #include "bashgetopt.h"
+#include "trap.h"
 
 #include <shtty.h>
 
@@ -106,41 +108,37 @@ $END
 #endif
 
 #include "shmbutil.h"
+#include "timer.h"
 
 #if !defined(errno)
 extern int errno;
 #endif
 
-extern void run_pending_traps __P((void));
-
-extern int posixly_correct;
-extern int trapped_signal_received;
-
 struct ttsave
 {
   int fd;
-  TTYSTRUCT *attrs;
+  TTYSTRUCT attrs;
 };
 
 #if defined (READLINE)
-static void reset_attempted_completion_function __P((char *));
-static int set_itext __P((void));
-static char *edit_line __P((char *, char *));
-static void set_eol_delim __P((int));
-static void reset_eol_delim __P((char *));
+static void reset_attempted_completion_function PARAMS((char *));
+static int set_itext PARAMS((void));
+static char *edit_line PARAMS((char *, char *));
+static void set_eol_delim PARAMS((int));
+static void reset_eol_delim PARAMS((char *));
+static void set_readline_timeout PARAMS((sh_timer *t, time_t, long));
 #endif
-static SHELL_VAR *bind_read_variable __P((char *, char *));
+static SHELL_VAR *bind_read_variable PARAMS((char *, char *, int));
 #if defined (HANDLE_MULTIBYTE)
-static int read_mbchar __P((int, char *, int, int, int));
+static int read_mbchar PARAMS((int, char *, int, int, int));
 #endif
-static void ttyrestore __P((struct ttsave *));
+static void ttyrestore PARAMS((struct ttsave *));
 
-static sighandler sigalrm __P((int));
-static void reset_alarm __P((void));
+static sighandler sigalrm PARAMS((int));
+static void reset_timeout PARAMS((void));
 
 /* Try this to see what the rest of the shell can do with the information. */
-procenv_t alrmbuf;
-int sigalrm_seen;
+sh_timer *read_timeout;
 
 static int reading, tty_modified;
 static SigHandler *old_alrm;
@@ -152,22 +150,47 @@ static struct ttsave termsave;
    avoids problems with the semi-tricky stuff we do with the xfree of
    input_string at the top of the unwind-protect list (see below). */
 
-/* Set a flag that CHECK_ALRM can check.  This relies on zread calling
-   trap.c:check_signals_and_traps(), which knows about sigalrm_seen and
-   alrmbuf. */
+/* Set a flag that check_read_timeout can check.  This relies on zread or
+   read_builtin calling trap.c:check_signals() (which calls check_read_timeout()) */
 static sighandler
 sigalrm (s)
      int s;
 {
-  sigalrm_seen = 1;
+  /* Display warning if this is called without read_timeout set? */
+  if (read_timeout)
+    read_timeout->alrmflag = 1;
 }
 
 static void
-reset_alarm ()
+reset_timeout ()
 {
   /* Cancel alarm before restoring signal handler. */
-  falarm (0, 0);
-  set_signal_handler (SIGALRM, old_alrm);
+  if (read_timeout)
+    shtimer_clear (read_timeout);
+#if defined (READLINE)
+  rl_clear_timeout ();
+#endif
+  read_timeout = 0;
+}
+
+void
+check_read_timeout ()
+{
+  if (read_timeout && shtimer_chktimeout (read_timeout))
+    sh_longjmp (read_timeout->jmpenv, 1);
+}
+
+int
+read_builtin_timeout (fd)
+     int fd;
+{
+  if ((read_timeout == 0) ||
+      (read_timeout->fd != fd) ||
+      (read_timeout->tmout.tv_sec == 0 && read_timeout->tmout.tv_usec == 0))
+    return 0;
+
+  return ((read_timeout->flags & SHTIMER_ALARM) ? shtimer_alrm (read_timeout)
+                                               : shtimer_select (read_timeout));
 }
 
 /* Read the value of the shell variables whose names follow.
@@ -181,10 +204,12 @@ read_builtin (list)
      WORD_LIST *list;
 {
   register char *varname;
-  int size, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2;
+  int size, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2, nflag;
   volatile int i;
   int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul;
-  int raw, edit, nchars, silent, have_timeout, ignore_delim, fd, lastsig, t_errno;
+  int raw, edit, nchars, silent, have_timeout, ignore_delim, fd;
+  int lastsig, t_errno;
+  int mb_cur_max;
   unsigned int tmsec, tmusec;
   long ival, uval;
   intmax_t intval;
@@ -194,12 +219,16 @@ read_builtin (list)
   struct stat tsb;
   SHELL_VAR *var;
   TTYSTRUCT ttattrs, ttset;
+  sigset_t chldset, prevset;
 #if defined (ARRAY_VARS)
   WORD_LIST *alist;
+  int vflags;
 #endif
+  int bindflags;
 #if defined (READLINE)
   char *rlbuf, *itext;
   int rlind;
+  FILE *save_instream;
 #endif
 
   USE_VAR(size);
@@ -226,7 +255,8 @@ read_builtin (list)
   USE_VAR(ps2);
   USE_VAR(lastsig);
 
-  sigalrm_seen = reading = tty_modified = 0;
+  reading = tty_modified = 0;
+  read_timeout = 0;
 
   i = 0;               /* Index into the string that we are reading. */
   raw = edit = 0;      /* Not reading raw input by default. */
@@ -239,10 +269,11 @@ read_builtin (list)
   rlind = 0;
 #endif
 
+  mb_cur_max = MB_CUR_MAX;
   tmsec = tmusec = 0;          /* no timeout */
   nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
   delim = '\n';                /* read until newline */
-  ignore_delim = 0;
+  ignore_delim = nflag = 0;
 
   reset_internal_getopt ();
   while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1)
@@ -274,7 +305,7 @@ read_builtin (list)
          break;
 #endif
        case 't':
-         code = uconvert (list_optarg, &ival, &uval);
+         code = uconvert (list_optarg, &ival, &uval, (char **)NULL);
          if (code == 0 || ival < 0 || uval < 0)
            {
              builtin_error (_("%s: invalid timeout specification"), list_optarg);
@@ -291,6 +322,7 @@ read_builtin (list)
          ignore_delim = 1;
          delim = -1;
        case 'n':
+         nflag = 1;
          code = legal_number (list_optarg, &intval);
          if (code == 0 || intval < 0 || intval != (int)intval)
            {
@@ -329,17 +361,16 @@ read_builtin (list)
   /* `read -t 0 var' tests whether input is available with select/FIONREAD,
      and fails if those are unavailable */
   if (have_timeout && tmsec == 0 && tmusec == 0)
-#if 0
-    return (EXECUTION_FAILURE);
-#else
     return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
-#endif
 
   /* Convenience: check early whether or not the first of possibly several
      variable names is a valid identifier, and bail early if so. */
 #if defined (ARRAY_VARS)
-  if (list && legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, 0) == 0)
+  if (list)
+    SET_VFLAGS (list->word->flags, vflags, bindflags);
+  if (list && legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, vflags) == 0)
 #else
+  bindflags = 0;
   if (list && legal_identifier (list->word->word) == 0)
 #endif
     {
@@ -364,10 +395,18 @@ read_builtin (list)
   input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
   input_string[0] = '\0';
 
+  /* More input and options validation */
+  if (nflag == 1 && nchars == 0)
+    {
+      retval = read (fd, &c, 0);
+      retval = (retval >= 0) ? EXECUTION_SUCCESS : EXECUTION_FAILURE;
+      goto assign_vars;                /* bail early if asked to read 0 chars */
+    }
+
   /* $TMOUT, if set, is the default timeout for read. */
   if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
     {
-      code = uconvert (e, &ival, &uval);
+      code = uconvert (e, &ival, &uval, (char **)NULL);
       if (code == 0 || ival < 0 || uval < 0)
        tmsec = tmusec = 0;
       else
@@ -377,6 +416,12 @@ read_builtin (list)
        }
     }
 
+#if defined (SIGCHLD)
+  sigemptyset (&chldset);
+  sigprocmask (SIG_BLOCK, (sigset_t *)0, &chldset);
+  sigaddset (&chldset, SIGCHLD);
+#endif
+
   begin_unwind_frame ("read_builtin");
 
 #if defined (BUFFERED_INPUT)
@@ -384,7 +429,11 @@ read_builtin (list)
     sync_buffered_stream (default_buffered_input);
 #endif
 
+#if 1
   input_is_tty = isatty (fd);
+#else
+  input_is_tty = 1;
+#endif
   if (input_is_tty == 0)
 #ifndef __CYGWIN__
     input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
@@ -421,10 +470,27 @@ read_builtin (list)
 
   if (tmsec > 0 || tmusec > 0)
     {
-      code = setjmp_nosigs (alrmbuf);
+      read_timeout = shtimer_alloc ();
+      read_timeout->flags = SHTIMER_LONGJMP;
+
+#if defined (HAVE_SELECT)
+      read_timeout->flags |= (edit || posixly_correct) ? SHTIMER_ALARM : SHTIMER_SELECT;
+#else
+      read_timeout->flags |= SHTIMER_ALARM;
+#endif
+      read_timeout->fd = fd;
+
+      read_timeout->alrm_handler = sigalrm;
+    }
+
+  if (tmsec > 0 || tmusec > 0)
+    {
+      code = setjmp_nosigs (read_timeout->jmpenv);
       if (code)
        {
-         sigalrm_seen = 0;
+         reset_timeout ();
+         sigprocmask (SIG_SETMASK, &prevset, (sigset_t *)0);
+
          /* Tricky.  The top of the unwind-protect stack is the free of
             input_string.  We want to run all the rest and use input_string,
             so we have to save input_string temporarily, run the unwind-
@@ -446,16 +512,17 @@ read_builtin (list)
        }
       if (interactive_shell == 0)
        initialize_terminating_signals ();
-      old_alrm = set_signal_handler (SIGALRM, sigalrm);
-      add_unwind_protect (reset_alarm, (char *)NULL);
+      add_unwind_protect (reset_timeout, (char *)NULL);
 #if defined (READLINE)
       if (edit)
        {
          add_unwind_protect (reset_attempted_completion_function, (char *)NULL);
          add_unwind_protect (bashline_reset_event_hook, (char *)NULL);
+         set_readline_timeout (read_timeout, tmsec, tmusec);
        }
+      else
 #endif
-      falarm (tmsec, tmusec);
+      shtimer_set (read_timeout, tmsec, tmusec);
     }
 
   /* If we've been asked to read only NCHARS chars, or we're using some
@@ -484,7 +551,7 @@ read_builtin (list)
          /* ttsave() */
          termsave.fd = fd;
          ttgetattr (fd, &ttattrs);
-         termsave.attrs = &ttattrs;
+         termsave.attrs = ttattrs;
 
          ttset = ttattrs;        
          i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset);
@@ -501,7 +568,7 @@ read_builtin (list)
       /* ttsave (); */
       termsave.fd = fd;
       ttgetattr (fd, &ttattrs);
-      termsave.attrs = &ttattrs;
+      termsave.attrs = ttattrs;
 
       ttset = ttattrs;
       i = ttfd_noecho (fd, &ttset);                    /* ttnoecho (); */
@@ -514,16 +581,33 @@ read_builtin (list)
        initialize_terminating_signals ();
     }
 
+#if defined (READLINE)
+  save_instream = 0;
+  if (edit && fd != 0)
+    {
+      if (bash_readline_initialized == 0)
+       initialize_readline ();
+
+      unwind_protect_var (rl_instream);
+      save_instream = rl_instream;
+      rl_instream = fdopen (fd, "r");  
+    }
+#endif
+
   /* This *must* be the top unwind-protect on the stack, so the manipulation
      of the unwind-protect stack after the realloc() works right. */
   add_unwind_protect (xfree, input_string);
 
-  CHECK_ALRM;
+  check_read_timeout ();
+  /* These only matter if edit == 0 */
   if ((nchars > 0) && (input_is_tty == 0) && ignore_delim)     /* read -N */
     unbuffered_read = 2;
+#if 0
   else if ((nchars > 0) || (delim != '\n') || input_is_pipe)
+#else
+  else if (((nchars > 0 || delim != '\n') && input_is_tty) || input_is_pipe)
     unbuffered_read = 1;
-
+#endif
   if (prompt && edit == 0)
     {
       fprintf (stderr, "%s", prompt);
@@ -537,16 +621,21 @@ read_builtin (list)
   ps2 = 0;
   for (print_ps2 = eof = retval = 0;;)
     {
-      CHECK_ALRM;
+      check_read_timeout ();
 
 #if defined (READLINE)
       if (edit)
        {
-         if (rlbuf && rlbuf[rlind] == '\0')
+         /* If we have a null delimiter, don't treat NULL as ending the line */
+         if (rlbuf && rlbuf[rlind] == '\0' && delim != '\0')
            {
-             xfree (rlbuf);
+             free (rlbuf);
              rlbuf = (char *)0;
            }
+#if defined (SIGCHLD)
+         if (tmsec > 0 || tmusec > 0)
+           sigprocmask (SIG_SETMASK, &chldset, &prevset);
+#endif
          if (rlbuf == 0)
            {
              reading = 1;
@@ -554,6 +643,10 @@ read_builtin (list)
              reading = 0;
              rlind = 0;
            }
+#if defined (SIGCHLD)
+         if (tmsec > 0 || tmusec > 0)
+           sigprocmask (SIG_SETMASK, &prevset, (sigset_t *)0);
+#endif
          if (rlbuf == 0)
            {
              eof = 1;
@@ -574,31 +667,41 @@ read_builtin (list)
          print_ps2 = 0;
        }
 
-#if 0
-      if (posixly_correct == 0)
-       interrupt_immediately++;
-#endif
       reading = 1;
+      check_read_timeout ();
+      errno = 0;
+
+#if defined (SIGCHLD)
+      if (tmsec > 0 || tmusec > 0)
+       sigprocmask (SIG_SETMASK, &chldset, &prevset);
+#endif
       if (unbuffered_read == 2)
        retval = posixly_correct ? zreadintr (fd, &c, 1) : zreadn (fd, &c, nchars - nr);
       else if (unbuffered_read)
        retval = posixly_correct ? zreadintr (fd, &c, 1) : zread (fd, &c, 1);
       else
        retval = posixly_correct ? zreadcintr (fd, &c) : zreadc (fd, &c);
-      reading = 0;
-#if 0
-      if (posixly_correct == 0)
-       interrupt_immediately--;
+#if defined (SIGCHLD)
+      if (tmsec > 0 || tmusec > 0)
+       sigprocmask (SIG_SETMASK, &prevset, (sigset_t *)0);
 #endif
 
+      reading = 0;
+
       if (retval <= 0)
        {
+         int t;
+
+         t = errno;
          if (retval < 0 && errno == EINTR)
            {
+             check_signals ();         /* in case we didn't call zread via zreadc */
              lastsig = LASTSIG();
              if (lastsig == 0)
                lastsig = trapped_signal_received;
+#if 0
              run_pending_traps ();     /* because interrupt_immediately is not set */
+#endif
            }
          else
            lastsig = 0;
@@ -606,17 +709,20 @@ read_builtin (list)
            ttyrestore (&termsave);     /* fix terminal before exiting */
          CHECK_TERMSIG;
          eof = 1;
+         errno = t;    /* preserve it for the error message below */
          break;
        }
 
-      CHECK_ALRM;
-
+      QUIT;            /* in case we didn't call check_signals() */
 #if defined (READLINE)
        }
 #endif
 
-      CHECK_ALRM;
-      if (i + 4 >= size)       /* XXX was i + 2; use i + 4 for multibyte/read_mbchar */
+      if (retval <= 0)                 /* XXX shouldn't happen */
+       check_read_timeout ();
+
+      /* XXX -- use i + mb_cur_max (at least 4) for multibyte/read_mbchar */
+      if (i + (mb_cur_max > 4 ? mb_cur_max : 4) >= size)
        {
          char *t;
          t = (char *)xrealloc (input_string, size += 128);
@@ -637,7 +743,8 @@ read_builtin (list)
          pass_next = 0;
          if (c == '\n')
            {
-             i--;              /* back up over the CTLESC */
+             if (skip_ctlesc == 0 && i > 0)
+               i--;            /* back up over the CTLESC */
              if (interactive && input_is_tty && raw == 0)
                print_ps2 = 1;
            }
@@ -672,13 +779,34 @@ read_builtin (list)
 
 add_char:
       input_string[i++] = c;
-      CHECK_ALRM;
+      check_read_timeout ();
 
 #if defined (HANDLE_MULTIBYTE)
-      if (nchars > 0 && MB_CUR_MAX > 1 && is_basic (c) == 0)
+      /* XXX - what if C == 127? Can DEL introduce a multibyte sequence? */
+      if (mb_cur_max > 1 && is_basic (c) == 0)
        {
          input_string[i] = '\0';       /* for simplicity and debugging */
-         i += read_mbchar (fd, input_string, i, c, unbuffered_read);
+         /* If we got input from readline, grab the next multibyte char from
+            rlbuf. */
+#  if defined (READLINE)
+         if (edit)
+           {
+             size_t clen;
+             clen = mbrlen (rlbuf + rlind - 1, mb_cur_max, (mbstate_t *)NULL);
+             /* We only deal with valid multibyte sequences longer than one
+                byte. If we get anything else, we leave the one character
+                copied and move on to the next. */
+             if ((int)clen > 1)
+               {
+                 memcpy (input_string+i, rlbuf+rlind, clen-1);
+                 i += clen - 1;
+                 rlind += clen - 1;
+               }
+           }
+         else
+#  endif
+         if (locale_utf8locale == 0 || ((c & 0x80) != 0))
+           i += read_mbchar (fd, input_string, i, c, unbuffered_read);
        }
 #endif
 
@@ -688,7 +816,7 @@ add_char:
        break;
     }
   input_string[i] = '\0';
-  CHECK_ALRM;
+  check_read_timeout ();
 
 #if defined (READLINE)
   if (edit)
@@ -705,7 +833,7 @@ add_char:
     }
 
   if (tmsec > 0 || tmusec > 0)
-    reset_alarm ();
+    reset_timeout ();
 
   if (nchars > 0 || delim != '\n')
     {
@@ -728,6 +856,11 @@ add_char:
   if (unbuffered_read == 0)
     zsyncfd (fd);
 
+#if defined (READLINE)
+  if (save_instream)
+    rl_instream = save_instream;       /* can't portably free it */
+#endif
+
   discard_unwind_frame ("read_builtin");
 
   retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
@@ -739,28 +872,14 @@ assign_vars:
      an assign them to `arrayname' in turn. */
   if (arrayname)
     {
-      if (legal_identifier (arrayname) == 0)
-       {
-         sh_invalidid (arrayname);
-         xfree (input_string);
-         return (EXECUTION_FAILURE);
-       }
-
-      var = find_or_make_array_variable (arrayname, 1);
+      /* pass 1 for flags arg to clear the existing array + 2 to check for a
+        valid identifier. */
+      var = builtin_find_indexed_array (arrayname, 3);
       if (var == 0)
        {
-         xfree (input_string);
+         free (input_string);
          return EXECUTION_FAILURE;     /* readonly or noassign */
        }
-      if (assoc_p (var))
-       {
-          builtin_error (_("%s: cannot convert associative to indexed array"), arrayname);
-         xfree (input_string);
-         return EXECUTION_FAILURE;     /* existing associative array */
-       }
-      else if (invisible_p (var))
-       VUNSETATTR (var, att_invisible);
-      array_flush (array_cell (var));
 
       alist = list_string (input_string, ifs_chars, 0);
       if (alist)
@@ -772,7 +891,7 @@ assign_vars:
          assign_array_var_from_word_list (var, alist, 0);
          dispose_words (alist);
        }
-      xfree (input_string);
+      free (input_string);
       return (retval);
     }
 #endif /* ARRAY_VARS */ 
@@ -808,7 +927,7 @@ assign_vars:
       else
        VUNSETATTR (var, att_invisible);
 
-      xfree (input_string);
+      free (input_string);
       return (retval);
     }
 
@@ -825,13 +944,14 @@ assign_vars:
     {
       varname = list->word->word;
 #if defined (ARRAY_VARS)
-      if (legal_identifier (varname) == 0 && valid_array_reference (varname, 0) == 0)
+      SET_VFLAGS (list->word->flags, vflags, bindflags);
+      if (legal_identifier (varname) == 0 && valid_array_reference (varname, vflags) == 0)
 #else
       if (legal_identifier (varname) == 0)
 #endif
        {
          sh_invalidid (varname);
-         xfree (orig_input_string);
+         free (orig_input_string);
          return (EXECUTION_FAILURE);
        }
 
@@ -848,22 +968,22 @@ assign_vars:
          if (t && saw_escape)
            {
              t1 = dequote_string (t);
-             var = bind_read_variable (varname, t1);
-             xfree (t1);
+             var = bind_read_variable (varname, t1, bindflags);
+             free (t1);
            }
          else
-           var = bind_read_variable (varname, t ? t : "");
+           var = bind_read_variable (varname, t ? t : "", bindflags);
        }
       else
        {
          t = (char *)0;
-         var = bind_read_variable (varname, "");
+         var = bind_read_variable (varname, "", bindflags);
        }
 
       FREE (t);
       if (var == 0)
        {
-         xfree (orig_input_string);
+         free (orig_input_string);
          return (EXECUTION_FAILURE);
        }
 
@@ -873,13 +993,14 @@ assign_vars:
 
   /* Now assign the rest of the line to the last variable argument. */
 #if defined (ARRAY_VARS)
-  if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, 0) == 0)
+  SET_VFLAGS (list->word->flags, vflags, bindflags);
+  if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, vflags) == 0)
 #else
   if (legal_identifier (list->word->word) == 0)
 #endif
     {
       sh_invalidid (list->word->word);
-      xfree (orig_input_string);
+      free (orig_input_string);
       return (EXECUTION_FAILURE);
     }
 
@@ -909,11 +1030,11 @@ assign_vars:
   if (saw_escape && input_string && *input_string)
     {
       t = dequote_string (input_string);
-      var = bind_read_variable (list->word->word, t);
-      xfree (t);
+      var = bind_read_variable (list->word->word, t, bindflags);
+      free (t);
     }
   else
-    var = bind_read_variable (list->word->word, input_string ? input_string : "");
+    var = bind_read_variable (list->word->word, input_string ? input_string : "", bindflags);
 
   if (var)
     {
@@ -924,25 +1045,19 @@ assign_vars:
     retval = EXECUTION_FAILURE;
 
   FREE (tofree);
-  xfree (orig_input_string);
+  free (orig_input_string);
 
   return (retval);
 }
 
 static SHELL_VAR *
-bind_read_variable (name, value)
+bind_read_variable (name, value, flags)
      char *name, *value;
+     int flags;
 {
   SHELL_VAR *v;
 
-#if defined (ARRAY_VARS)
-  if (valid_array_reference (name, 0) == 0)
-    v = bind_variable (name, value, 0);
-  else
-    v = assign_array_element (name, value, 0);
-#else /* !ARRAY_VARS */
-  v = bind_variable (name, value, 0);
-#endif /* !ARRAY_VARS */
+  v = builtin_bind_variable (name, value, flags);
   return (v == 0 ? v
                 : ((readonly_p (v) || noassign_p (v)) ? (SHELL_VAR *)NULL : v));
 }
@@ -973,12 +1088,15 @@ read_mbchar (fd, string, ind, ch, unbuffered)
       if (ret == (size_t)-2)
        {
          ps = ps_back;
+
          /* We don't want to be interrupted during a multibyte char read */
-         if (unbuffered)
+         if (unbuffered == 2)
+           r = zreadn (fd, &c, 1);
+         else if (unbuffered)
            r = zread (fd, &c, 1);
          else
            r = zreadc (fd, &c);
-         if (r < 0)
+         if (r <= 0)
            goto mbchar_return;
          mbchar[i++] = c;      
          continue;
@@ -1001,7 +1119,7 @@ static void
 ttyrestore (ttp)
      struct ttsave *ttp;
 {
-  ttsetattr (ttp->fd, ttp->attrs);
+  ttsetattr (ttp->fd, &(ttp->attrs));
   tty_modified = 0;
 }
 
@@ -1077,7 +1195,15 @@ edit_line (p, itext)
   bashline_reset_event_hook ();
 
   if (ret == 0)
-    return ret;
+    {
+      if (RL_ISSTATE (RL_STATE_TIMEOUT))
+       {
+         sigalrm (SIGALRM);            /* simulate receiving SIGALRM */
+         check_read_timeout ();
+       }
+      return ret;
+    }
+
   len = strlen (ret);
   ret = (char *)xrealloc (ret, len + 2);
   ret[len++] = delim;
@@ -1085,6 +1211,17 @@ edit_line (p, itext)
   return ret;
 }
 
+static void
+set_readline_timeout (t, sec, usec)
+     sh_timer *t;
+     time_t sec;
+     long usec;
+{
+  t->tmout.tv_sec = sec;
+  t->tmout.tv_usec = usec;
+  rl_set_timeout (sec, usec);
+}
+
 static int old_delim_ctype;
 static rl_command_func_t *old_delim_func;
 static int old_newline_ctype;
@@ -1102,15 +1239,17 @@ set_eol_delim (c)
     initialize_readline ();
   cmap = rl_get_keymap ();
 
-  /* Change newline to self-insert */
+  /* Save the old delimiter char binding */
   old_newline_ctype = cmap[RETURN].type;
   old_newline_func =  cmap[RETURN].function;
+  old_delim_ctype = cmap[c].type;
+  old_delim_func = cmap[c].function;
+
+  /* Change newline to self-insert */
   cmap[RETURN].type = ISFUNC;
   cmap[RETURN].function = rl_insert;
 
   /* Bind the delimiter character to accept-line. */
-  old_delim_ctype = cmap[c].type;
-  old_delim_func = cmap[c].function;
   cmap[c].type = ISFUNC;
   cmap[c].function = rl_newline;