]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - subst.c
saved background process status hash table loop fixes
[thirdparty/bash.git] / subst.c
diff --git a/subst.c b/subst.c
index 94c230f79cb11c80f098ac4d6a2f7f5cec8fb8ba..fc00cab08fbb8c4ea2d6014c9de112627d1103c1 100644 (file)
--- a/subst.c
+++ b/subst.c
@@ -1,29 +1,35 @@
-/* subst.c -- The part of the shell that does parameter, command, and
-   globbing substitutions. */
+/* subst.c -- The part of the shell that does parameter, command, arithmetic,
+   and globbing substitutions. */
 
-/* Copyright (C) 1987,1989 Free Software Foundation, Inc.
+/* ``Have a little faith, there's magic in the night.  You ain't a
+     beauty, but, hey, you're alright.'' */
+
+/* Copyright (C) 1987-2016 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
-   Bash 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 (at your option) any later
-   version.
+   Bash 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.
 
-   Bash 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.
+   Bash 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.
 
-   You should have received a copy of the GNU General Public License along
-   with Bash; see the file COPYING.  If not, write to the Free Software
-   Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include "config.h"
 
 #include "bashtypes.h"
 #include <stdio.h>
-#include <pwd.h>
+#include "chartypes.h"
+#if defined (HAVE_PWD_H)
+#  include <pwd.h>
+#endif
 #include <signal.h>
 #include <errno.h>
 
 #  include <unistd.h>
 #endif
 
+#define NEED_FPURGE_DECL
+
 #include "bashansi.h"
 #include "posixstat.h"
+#include "bashintl.h"
 
 #include "shell.h"
+#include "parser.h"
 #include "flags.h"
 #include "jobs.h"
 #include "execute_cmd.h"
 #include "pathexp.h"
 #include "mailcheck.h"
 
-#if !defined (HAVE_RESTARTABLE_SYSCALLS)       /* for getc_with_restart */
-#include "input.h"
+#include "shmbutil.h"
+#if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
+#  include <mbstr.h>           /* mbschr */
 #endif
+#include "typemax.h"
 
 #include "builtins/getopt.h"
 #include "builtins/common.h"
 
+#include "builtins/builtext.h"
+
 #include <tilde/tilde.h>
-#include <glob/fnmatch.h>
+#include <glob/strmatch.h>
 
 #if !defined (errno)
 extern int errno;
@@ -66,65 +80,288 @@ extern int errno;
 #define VT_POSPARMS    1
 #define VT_ARRAYVAR    2
 #define VT_ARRAYMEMBER 3
+#define VT_ASSOCVAR    4
+
+#define VT_STARSUB     128     /* $* or ${array[*]} -- used to split */
 
 /* Flags for quoted_strchr */
 #define ST_BACKSL      0x01
 #define ST_CTLESC      0x02
+#define ST_SQUOTE      0x04    /* unused yet */
+#define ST_DQUOTE      0x08    /* unused yet */
 
 /* These defs make it easier to use the editor. */
 #define LBRACE         '{'
 #define RBRACE         '}'
 #define LPAREN         '('
 #define RPAREN         ')'
+#define LBRACK         '['
+#define RBRACK         ']'
+
+#if defined (HANDLE_MULTIBYTE)
+#define WLPAREN                L'('
+#define WRPAREN                L')'
+#endif
+
+#define DOLLAR_AT_STAR(c)      ((c) == '@' || (c) == '*')
+#define STR_DOLLAR_AT_STAR(s)  (DOLLAR_AT_STAR ((s)[0]) && (s)[1] == '\0')
+
+/* Evaluates to 1 if C is one of the shell's special parameters whose length
+   can be taken, but is also one of the special expansion characters. */
+#define VALID_SPECIAL_LENGTH_PARAM(c) \
+  ((c) == '-' || (c) == '?' || (c) == '#' || (c) == '@')
+
+/* Evaluates to 1 if C is one of the shell's special parameters for which an
+   indirect variable reference may be made. */
+#define VALID_INDIR_PARAM(c) \
+  ((posixly_correct == 0 && (c) == '#') || (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*')
+
+/* Evaluates to 1 if C is one of the OP characters that follows the parameter
+   in ${parameter[:]OPword}. */
+#define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
+
+/* Evaluates to 1 if this is one of the shell's special variables. */
+#define SPECIAL_VAR(name, wi) \
+ (*name && ((DIGIT (*name) && all_digits (name)) || \
+      (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
+      (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1]))))
+
+/* This can be used by all of the *_extract_* functions that have a similar
+   structure.  It can't just be wrapped in a do...while(0) loop because of
+   the embedded `break'. The dangling else accommodates a trailing semicolon;
+   we could also put in a do ; while (0) */
+
+   
+#define CHECK_STRING_OVERRUN(oind, ind, len, ch) \
+  if (ind >= len) \
+    { \
+      oind = len; \
+      ch = 0; \
+      break; \
+    } \
+  else \
+
+/* An expansion function that takes a string and a quoted flag and returns
+   a WORD_LIST *.  Used as the type of the third argument to
+   expand_string_if_necessary(). */
+typedef WORD_LIST *EXPFUNC __P((char *, int));
 
 /* Process ID of the last command executed within command substitution. */
 pid_t last_command_subst_pid = NO_PID;
+pid_t current_command_subst_pid = NO_PID;
+
+/* Variables used to keep track of the characters in IFS. */
+SHELL_VAR *ifs_var;
+char *ifs_value;
+unsigned char ifs_cmap[UCHAR_MAX + 1];
+int ifs_is_set, ifs_is_null;
+
+#if defined (HANDLE_MULTIBYTE)
+unsigned char ifs_firstc[MB_LEN_MAX];
+size_t ifs_firstc_len;
+#else
+unsigned char ifs_firstc;
+#endif
+
+/* If non-zero, command substitution inherits the value of errexit option */
+int inherit_errexit = 0;
+
+/* Sentinel to tell when we are performing variable assignments preceding a
+   command name and putting them into the environment.  Used to make sure
+   we use the temporary environment when looking up variable values. */
+int assigning_in_environment;
+
+/* Used to hold a list of variable assignments preceding a command.  Global
+   so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
+   SIGCHLD trap and so it can be saved and restored by the trap handlers. */
+WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
+
+/* Tell the expansion functions to not longjmp back to top_level on fatal
+   errors.  Enabled when doing completion and prompt string expansion. */
+int no_longjmp_on_fatal_error = 0;
 
 /* Extern functions and variables from different files. */
-extern int last_command_exit_value, interactive, interactive_shell;
-extern int subshell_environment, startup_state;
-extern int dollar_dollar_pid;
+extern int last_command_exit_value, last_command_exit_signal;
+extern int subshell_environment, running_in_background;
+extern int subshell_level, parse_and_execute_level, sourcelevel;
+extern int eof_encountered, line_number;
+extern int return_catch_flag, return_catch_value;
+extern pid_t dollar_dollar_pid;
 extern int posixly_correct;
 extern char *this_command_name;
 extern struct fd_bitmap *current_fds_to_close;
 extern int wordexp_only;
+extern int expanding_redir;
+extern int tempenv_assign_error;
+extern int builtin_ignoring_errexit;
+
+#if defined (JOB_CONTROL) && defined (PROCESS_SUBSTITUTION)
+extern PROCESS *last_procsub_child;
+#endif
 
-extern void getopts_reset ();
+#if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
+extern wchar_t *wcsdup __P((const wchar_t *));
+#endif
 
 /* Non-zero means to allow unmatched globbed filenames to expand to
    a null file. */
 int allow_null_glob_expansion;
 
+/* Non-zero means to throw an error when globbing fails to match anything. */
+int fail_glob_expansion;
+
+#if 0
 /* Variables to keep track of which words in an expanded word list (the
    output of expand_word_list_internal) are the result of globbing
-   expansions.  GLOB_ARGV_FLAGS is used by execute_cmd.c. */
+   expansions.  GLOB_ARGV_FLAGS is used by execute_cmd.c.
+   (CURRENTLY UNUSED). */
 char *glob_argv_flags;
 static int glob_argv_flags_size;
+#endif
+
+static WORD_LIST *cached_quoted_dollar_at = 0;
 
 static WORD_LIST expand_word_error, expand_word_fatal;
+static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
 static char expand_param_error, expand_param_fatal;
+static char extract_string_error, extract_string_fatal;
+
+/* Set by expand_word_unsplit; used to inhibit splitting and re-joining
+   $* on $IFS, primarily when doing assignment statements. */
+static int expand_no_split_dollar_star = 0;
+
+/* A WORD_LIST of words to be expanded by expand_word_list_internal,
+   without any leading variable assignments. */
+static WORD_LIST *garglist = (WORD_LIST *)NULL;
+
+static char *quoted_substring __P((char *, int, int));
+static int quoted_strlen __P((char *));
+static char *quoted_strchr __P((char *, int, int));
+
+static char *expand_string_if_necessary __P((char *, int, EXPFUNC *));
+static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *));
+static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
+static WORD_LIST *expand_string_internal __P((char *, int));
+static WORD_LIST *expand_string_leave_quoted __P((char *, int));
+static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *));
+
+static WORD_LIST *list_quote_escapes __P((WORD_LIST *));
+static WORD_LIST *list_dequote_escapes __P((WORD_LIST *));
+
+static char *make_quoted_char __P((int));
+static WORD_LIST *quote_list __P((WORD_LIST *));
+
+static int unquoted_substring __P((char *, char *));
+static int unquoted_member __P((int, char *));
+
+#if defined (ARRAY_VARS)
+static SHELL_VAR *do_compound_assignment __P((char *, char *, int));
+#endif
+static int do_assignment_internal __P((const WORD_DESC *, int));
+
+static char *string_extract_verbatim __P((char *, size_t, int *, char *, int));
+static char *string_extract __P((char *, int *, char *, int));
+static char *string_extract_double_quoted __P((char *, int *, int));
+static inline char *string_extract_single_quoted __P((char *, int *));
+static inline int skip_single_quoted __P((const char *, size_t, int, int));
+static int skip_double_quoted __P((char *, size_t, int, int));
+static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int));
+static char *extract_dollar_brace_string __P((char *, int *, int, int));
+static int skip_matched_pair __P((const char *, int, int, int, int));
+
+static char *pos_params __P((char *, int, int, int));
+
+static unsigned char *mb_getcharlens __P((char *, int));
+
+static char *remove_upattern __P((char *, char *, int));
+#if defined (HANDLE_MULTIBYTE) 
+static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int));
+#endif
+static char *remove_pattern __P((char *, char *, int));
+
+static int match_upattern __P((char *, char *, int, char **, char **));
+#if defined (HANDLE_MULTIBYTE)
+static int match_wpattern __P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
+#endif
+static int match_pattern __P((char *, char *, int, char **, char **));
+static int getpatspec __P((int, char *));
+static char *getpattern __P((char *, int, int));
+static char *variable_remove_pattern __P((char *, char *, int, int));
+static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int));
+static char *parameter_list_remove_pattern __P((int, char *, int, int));
+#ifdef ARRAY_VARS
+static char *array_remove_pattern __P((SHELL_VAR *, char *, int, char *, int));
+#endif
+static char *parameter_brace_remove_pattern __P((char *, char *, int, char *, int, int, int));
+
+static char *string_var_assignment __P((SHELL_VAR *, char *));
+#if defined (ARRAY_VARS)
+static char *array_var_assignment __P((SHELL_VAR *, int, int));
+#endif
+static char *pos_params_assignment __P((WORD_LIST *, int, int));
+static char *string_transform __P((int, SHELL_VAR *, char *));
+static char *list_transform __P((int, SHELL_VAR *, WORD_LIST *, int, int));
+static char *parameter_list_transform __P((int, int, int));
+#if defined ARRAY_VARS
+static char *array_transform __P((int, SHELL_VAR *, char *, int));
+#endif
+static char *parameter_brace_transform __P((char *, char *, int, char *, int, int, int));
+
+static char *process_substitute __P((char *, int));
+
+static char *read_comsub __P((int, int, int *));
 
-static char *make_quoted_char ();
-static void remove_quoted_nulls ();
-static char *param_expand ();
-static char *maybe_expand_string ();
-static WORD_LIST *call_expand_word_internal ();
-static WORD_LIST *expand_string_internal ();
-static WORD_LIST *expand_word_internal (), *expand_word_list_internal ();
-static WORD_LIST *expand_string_leave_quoted ();
-static WORD_LIST *expand_string_for_rhs ();
-static WORD_LIST *word_list_split ();
-static WORD_LIST *quote_list (), *dequote_list ();
-static char *quote_escapes ();
-static WORD_LIST *list_quote_escapes ();
-static int unquoted_substring (), unquoted_member ();
-static int do_assignment_internal ();
-static char *string_extract_verbatim (), *string_extract ();
-static char *string_extract_double_quoted (), *string_extract_single_quoted ();
-static char *string_list_dollar_at (), *string_list_dollar_star ();
-static inline int skip_single_quoted (), skip_double_quoted ();
-static char *extract_delimited_string ();
-static char *extract_dollar_brace_string ();
+#ifdef ARRAY_VARS
+static arrayind_t array_length_reference __P((char *));
+#endif
+
+static int valid_brace_expansion_word __P((char *, int));
+static int chk_atstar __P((char *, int, int *, int *));
+static int chk_arithsub __P((const char *, int));
+
+static WORD_DESC *parameter_brace_expand_word __P((char *, int, int, int, arrayind_t *));
+static char *parameter_brace_find_indir __P((char *, int, int, int));
+static WORD_DESC *parameter_brace_expand_indir __P((char *, int, int, int *, int *));
+static WORD_DESC *parameter_brace_expand_rhs __P((char *, char *, int, int, int, int *, int *));
+static void parameter_brace_expand_error __P((char *, char *));
+
+static int valid_length_expression __P((char *));
+static intmax_t parameter_brace_expand_length __P((char *));
+
+static char *skiparith __P((char *, int));
+static int verify_substring_values __P((SHELL_VAR *, char *, char *, int, intmax_t *, intmax_t *));
+static int get_var_and_type __P((char *, char *, arrayind_t, int, int, SHELL_VAR **, char **));
+static char *mb_substring __P((char *, int, int));
+static char *parameter_brace_substring __P((char *, char *, int, char *, int, int));
+
+static int shouldexp_replacement __P((char *));
+
+static char *pos_params_pat_subst __P((char *, char *, char *, int));
+
+static char *parameter_brace_patsub __P((char *, char *, int, char *, int, int, int));
+
+static char *pos_params_casemod __P((char *, char *, int, int));
+static char *parameter_brace_casemod __P((char *, char *, int, int, char *, int, int));
+
+static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int, int *, int *));
+static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
+
+static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
+
+static WORD_LIST *word_list_split __P((WORD_LIST *));
+
+static void exp_jump_to_top_level __P((int));
+
+static WORD_LIST *separate_out_assignments __P((WORD_LIST *));
+static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int));
+#ifdef BRACE_EXPANSION
+static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int));
+#endif
+#if defined (ARRAY_VARS)
+static int make_internal_declare __P((char *, char *, char *));
+#endif
+static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
+static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
 
 /* **************************************************************** */
 /*                                                                 */
@@ -132,23 +369,162 @@ static char *extract_dollar_brace_string ();
 /*                                                                 */
 /* **************************************************************** */
 
-/* Cons a new string from STRING starting at START and ending at END,
-   not including END. */
-char *
-substring (string, start, end)
-     char *string;
-     int start, end;
+#if defined (DEBUG)
+void
+dump_word_flags (flags)
+     int flags;
 {
-  register int len;
-  register char *result;
+  int f;
 
-  len = end - start;
-  result = xmalloc (len + 1);
-  strncpy (result, string + start, len);
-  result[len] = '\0';
-  return (result);
+  f = flags;
+  fprintf (stderr, "%d -> ", f);
+  if (f & W_ARRAYIND)
+    {
+      f &= ~W_ARRAYIND;
+      fprintf (stderr, "W_ARRAYIND%s", f ? "|" : "");
+    }
+  if (f & W_ASSIGNASSOC)
+    {
+      f &= ~W_ASSIGNASSOC;
+      fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : "");
+    }
+  if (f & W_ASSIGNARRAY)
+    {
+      f &= ~W_ASSIGNARRAY;
+      fprintf (stderr, "W_ASSIGNARRAY%s", f ? "|" : "");
+    }
+  if (f & W_HASCTLESC)
+    {
+      f &= ~W_HASCTLESC;
+      fprintf (stderr, "W_HASCTLESC%s", f ? "|" : "");
+    }
+  if (f & W_NOPROCSUB)
+    {
+      f &= ~W_NOPROCSUB;
+      fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : "");
+    }
+  if (f & W_DQUOTE)
+    {
+      f &= ~W_DQUOTE;
+      fprintf (stderr, "W_DQUOTE%s", f ? "|" : "");
+    }
+  if (f & W_HASQUOTEDNULL)
+    {
+      f &= ~W_HASQUOTEDNULL;
+      fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : "");
+    }
+  if (f & W_ASSIGNARG)
+    {
+      f &= ~W_ASSIGNARG;
+      fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : "");
+    }
+  if (f & W_ASSNBLTIN)
+    {
+      f &= ~W_ASSNBLTIN;
+      fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : "");
+    }
+  if (f & W_ASSNGLOBAL)
+    {
+      f &= ~W_ASSNGLOBAL;
+      fprintf (stderr, "W_ASSNGLOBAL%s", f ? "|" : "");
+    }
+  if (f & W_COMPASSIGN)
+    {
+      f &= ~W_COMPASSIGN;
+      fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : "");
+    }
+  if (f & W_NOEXPAND)
+    {
+      f &= ~W_NOEXPAND;
+      fprintf (stderr, "W_NOEXPAND%s", f ? "|" : "");
+    }
+  if (f & W_ITILDE)
+    {
+      f &= ~W_ITILDE;
+      fprintf (stderr, "W_ITILDE%s", f ? "|" : "");
+    }
+  if (f & W_NOTILDE)
+    {
+      f &= ~W_NOTILDE;
+      fprintf (stderr, "W_NOTILDE%s", f ? "|" : "");
+    }
+  if (f & W_ASSIGNRHS)
+    {
+      f &= ~W_ASSIGNRHS;
+      fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : "");
+    }
+  if (f & W_NOCOMSUB)
+    {
+      f &= ~W_NOCOMSUB;
+      fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : "");
+    }
+  if (f & W_DOLLARSTAR)
+    {
+      f &= ~W_DOLLARSTAR;
+      fprintf (stderr, "W_DOLLARSTAR%s", f ? "|" : "");
+    }
+  if (f & W_DOLLARAT)
+    {
+      f &= ~W_DOLLARAT;
+      fprintf (stderr, "W_DOLLARAT%s", f ? "|" : "");
+    }
+  if (f & W_TILDEEXP)
+    {
+      f &= ~W_TILDEEXP;
+      fprintf (stderr, "W_TILDEEXP%s", f ? "|" : "");
+    }
+  if (f & W_NOSPLIT2)
+    {
+      f &= ~W_NOSPLIT2;
+      fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : "");
+    }
+  if (f & W_NOSPLIT)
+    {
+      f &= ~W_NOSPLIT;
+      fprintf (stderr, "W_NOSPLIT%s", f ? "|" : "");
+    }
+  if (f & W_NOBRACE)
+    {
+      f &= ~W_NOBRACE;
+      fprintf (stderr, "W_NOBRACE%s", f ? "|" : "");
+    }
+  if (f & W_NOGLOB)
+    {
+      f &= ~W_NOGLOB;
+      fprintf (stderr, "W_NOGLOB%s", f ? "|" : "");
+    }
+  if (f & W_SPLITSPACE)
+    {
+      f &= ~W_SPLITSPACE;
+      fprintf (stderr, "W_SPLITSPACE%s", f ? "|" : "");
+    }
+  if (f & W_ASSIGNMENT)
+    {
+      f &= ~W_ASSIGNMENT;
+      fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : "");
+    }
+  if (f & W_QUOTED)
+    {
+      f &= ~W_QUOTED;
+      fprintf (stderr, "W_QUOTED%s", f ? "|" : "");
+    }
+  if (f & W_HASDOLLAR)
+    {
+      f &= ~W_HASDOLLAR;
+      fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : "");
+    }
+  if (f & W_COMPLETE)
+    {
+      f &= ~W_COMPLETE;
+      fprintf (stderr, "W_COMPLETE%s", f ? "|" : "");
+    }
+  
+  fprintf (stderr, "\n");
+  fflush (stderr);
 }
+#endif
 
+#ifdef INCLUDE_UNUSED
 static char *
 quoted_substring (string, start, end)
      char *string;
@@ -164,36 +540,62 @@ quoted_substring (string, start, end)
     {
       if (*s == CTLESC)
        {
-          s++;
-          continue;
+         s++;
+         continue;
        }
       l++;
       if (*s == 0)
-        break;
+       break;
     }
 
-  r = result = xmalloc (2*len + 1);      /* save room for quotes */
+  r = result = (char *)xmalloc (2*len + 1);      /* save room for quotes */
 
   /* Copy LEN characters, including quote characters. */
   s = string + l;
   for (l = 0; l < len; s++)
     {
       if (*s == CTLESC)
-        *r++ = *s++;
+       *r++ = *s++;
       *r++ = *s;
       l++;
       if (*s == 0)
-        break;
+       break;
     }
   *r = '\0';
   return result;
 }
+#endif
+
+#ifdef INCLUDE_UNUSED
+/* Return the length of S, skipping over quoted characters */
+static int
+quoted_strlen (s)
+     char *s;
+{
+  register char *p;
+  int i;
+
+  i = 0;
+  for (p = s; *p; p++)
+    {
+      if (*p == CTLESC)
+       {
+         p++;
+         if (*p == 0)
+           return (i + 1);
+       }
+      i++;
+    }
+
+  return i;
+}
+#endif
 
 /* Find the first occurrence of character C in string S, obeying shell
    quoting rules.  If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
    characters are skipped.  If (FLAGS & ST_CTLESC) is non-zero, characters
    escaped with CTLESC are skipped. */
-static inline char *
+static char *
 quoted_strchr (s, c, flags)
      char *s;
      int c, flags;
@@ -216,16 +618,21 @@ quoted_strchr (s, c, flags)
   return ((char *)NULL);
 }
 
+#if defined (INCLUDE_UNUSED)
 /* Return 1 if CHARACTER appears in an unquoted portion of
-   STRING.  Return 0 otherwise. */
+   STRING.  Return 0 otherwise.  CHARACTER must be a single-byte character. */
 static int
 unquoted_member (character, string)
      int character;
      char *string;
 {
+  size_t slen;
   int sindex, c;
+  DECLARE_MBSTATE;
 
-  for (sindex = 0; c = string[sindex]; )
+  slen = strlen (string);
+  sindex = 0;
+  while (c = string[sindex])
     {
       if (c == character)
        return (1);
@@ -233,21 +640,21 @@ unquoted_member (character, string)
       switch (c)
        {
        default:
-         sindex++;
+         ADVANCE_CHAR (string, slen, sindex);
          break;
 
        case '\\':
          sindex++;
          if (string[sindex])
-           sindex++;
+           ADVANCE_CHAR (string, slen, sindex);
          break;
 
        case '\'':
-         sindex = skip_single_quoted (string, ++sindex);
+         sindex = skip_single_quoted (string, slen, ++sindex, 0);
          break;
 
        case '"':
-         sindex = skip_double_quoted (string, ++sindex);
+         sindex = skip_double_quoted (string, slen, ++sindex, 0);
          break;
        }
     }
@@ -259,11 +666,14 @@ static int
 unquoted_substring (substr, string)
      char *substr, *string;
 {
+  size_t slen;
   int sindex, c, sublen;
+  DECLARE_MBSTATE;
 
   if (substr == 0 || *substr == '\0')
     return (0);
 
+  slen = strlen (string);
   sublen = strlen (substr);
   for (sindex = 0; c = string[sindex]; )
     {
@@ -274,26 +684,26 @@ unquoted_substring (substr, string)
        {
        case '\\':
          sindex++;
-
          if (string[sindex])
-           sindex++;
+           ADVANCE_CHAR (string, slen, sindex);
          break;
 
        case '\'':
-         sindex = skip_single_quoted (string, ++sindex);
+         sindex = skip_single_quoted (string, slen, ++sindex, 0);
          break;
 
        case '"':
-         sindex = skip_double_quoted (string, ++sindex);
+         sindex = skip_double_quoted (string, slen, ++sindex, 0);
          break;
 
        default:
-         sindex++;
+         ADVANCE_CHAR (string, slen, sindex);
          break;
        }
     }
   return (0);
 }
+#endif
 
 /* Most of the substitutions must be done in parallel.  In order
    to avoid using tons of unclear goto's, I have some functions
@@ -307,21 +717,23 @@ unquoted_substring (substr, string)
    of space allocated to TARGET.  SOURCE can be NULL, in which
    case nothing happens.  Gets rid of SOURCE by freeing it.
    Returns TARGET in case the location has changed. */
-inline char *
+INLINE char *
 sub_append_string (source, target, indx, size)
      char *source, *target;
-     int *indx, *size;
+     int *indx;
+     size_t *size;
 {
   if (source)
     {
-      int srclen, n;
+      int n;
+      size_t srclen;
 
       srclen = STRLEN (source);
       if (srclen >= (int)(*size - *indx))
        {
          n = srclen + *indx;
          n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
-         target = xrealloc (target, (*size = n));
+         target = (char *)xrealloc (target, (*size = n));
        }
 
       FASTCOPY (source, target + *indx, srclen);
@@ -339,8 +751,10 @@ sub_append_string (source, target, indx, size)
    INDX and SIZE are as in SUB_APPEND_STRING. */
 char *
 sub_append_number (number, target, indx, size)
-     int number, *indx, *size;
+     intmax_t number;
      char *target;
+     int *indx;
+     size_t *size;
 {
   char *temp;
 
@@ -352,42 +766,67 @@ sub_append_number (number, target, indx, size)
 /* Extract a substring from STRING, starting at SINDEX and ending with
    one of the characters in CHARLIST.  Don't make the ending character
    part of the string.  Leave SINDEX pointing at the ending character.
-   Understand about backslashes in the string.  If VARNAME is non-zero,
-   and array variables have been compiled into the shell, everything
-   between a `[' and a corresponding `]' is skipped over. */
+   Understand about backslashes in the string.  If (flags & SX_VARNAME)
+   is non-zero, and array variables have been compiled into the shell,
+   everything between a `[' and a corresponding `]' is skipped over.
+   If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
+   update SINDEX.  If (flags & SX_REQMATCH) is non-zero, the string must
+   contain a closing character from CHARLIST. */
 static char *
-string_extract (string, sindex, charlist, varname)
-     char *string, *charlist;
-     int *sindex, varname;
+string_extract (string, sindex, charlist, flags)
+     char *string;
+     int *sindex;
+     char *charlist;
+     int flags;
 {
   register int c, i;
+  int found;
+  size_t slen;
   char *temp;
+  DECLARE_MBSTATE;
 
-  for (i = *sindex; c = string[i]; i++)
+  slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
+  i = *sindex;
+  found = 0;
+  while (c = string[i])
     {
       if (c == '\\')
-       if (string[i + 1])
-         i++;
-       else
-         break;
+       {
+         if (string[i + 1])
+           i++;
+         else
+           break;
+       }
 #if defined (ARRAY_VARS)
-      else if (varname && c == '[')
+      else if ((flags & SX_VARNAME) && c == '[')
        {
          int ni;
          /* If this is an array subscript, skip over it and continue. */
-         ni = skipsubscript (string, i);
+         ni = skipsubscript (string, i, 0);
          if (string[ni] == ']')
            i = ni;
        }
 #endif
       else if (MEMBER (c, charlist))
+       {
+         found = 1;
          break;
+       }
+
+      ADVANCE_CHAR (string, slen, i);
+    }
+
+  /* If we had to have a matching delimiter and didn't find one, return an
+     error and let the caller deal with it. */
+  if ((flags & SX_REQMATCH) && found == 0)
+    {
+      *sindex = i;
+      return (&extract_string_error);
     }
-  c = i - *sindex;
-  temp = xmalloc (1 + c);
-  strncpy (temp, string + *sindex, c);
-  temp[c] = '\0';
+  
+  temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
   *sindex = i;
+  
   return (temp);
 }
 
@@ -398,29 +837,42 @@ string_extract (string, sindex, charlist, varname)
    quotes are stripped and the string is terminated by a null byte.
    Backslashes between the embedded double quotes are processed.  If STRIPDQ
    is zero, an unquoted `"' terminates the string. */
-static inline char *
-string_extract_double_quoted (string, sindex, stripdq)
+static char *
+string_extract_double_quoted (string, sindex, flags)
      char *string;
-     int *sindex, stripdq;
+     int *sindex, flags;
 {
-  int c, j, i, t;
+  size_t slen;
+  char *send;
+  int j, i, t;
+  unsigned char c;
   char *temp, *ret;            /* The new string we return. */
   int pass_next, backquote, si;        /* State variables for the machine. */
   int dquote;
+  int stripdq;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string + *sindex) + *sindex;
+  send = string + slen;
+
+  stripdq = (flags & SX_STRIPDQ);
 
   pass_next = backquote = dquote = 0;
-  temp = xmalloc (1 + strlen (string) - *sindex);
+  temp = (char *)xmalloc (1 + slen - *sindex);
 
-  for (j = 0, i = *sindex; c = string[i]; i++)
+  j = 0;
+  i = *sindex;
+  while (c = string[i])
     {
       /* Process a character that was quoted by a backslash. */
       if (pass_next)
        {
+         /* XXX - take another look at this in light of Interp 221 */
          /* Posix.2 sez:
 
             ``The backslash shall retain its special meaning as an escape
             character only when followed by one of the characters:
-               $       `       "       \       <newline>''.
+               $       `       "       \       <newline>''.
 
             If STRIPDQ is zero, we handle the double quotes here and let
             expand_word_internal handle the rest.  If STRIPDQ is non-zero,
@@ -439,10 +891,12 @@ string_extract_double_quoted (string, sindex, stripdq)
                The returned string will be run through expansion as if
                it were double-quoted. */
          if ((stripdq == 0 && c != '"') ||
-             (stripdq && ((dquote && strchr (slashify_in_quotes, c)) || dquote == 0)))
+             (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
            temp[j++] = '\\';
-         temp[j++] = c;
          pass_next = 0;
+
+add_one_character:
+         COPY_CHAR_I (temp, j, string, send, i);
          continue;
        }
 
@@ -452,6 +906,7 @@ string_extract_double_quoted (string, sindex, stripdq)
       if (c == '\\')
        {
          pass_next++;
+         i++;
          continue;
        }
 
@@ -464,6 +919,7 @@ string_extract_double_quoted (string, sindex, stripdq)
          if (c == '`')
            backquote = 0;
          temp[j++] = c;
+         i++;
          continue;
        }
 
@@ -471,6 +927,7 @@ string_extract_double_quoted (string, sindex, stripdq)
        {
          temp[j++] = c;
          backquote++;
+         i++;
          continue;
        }
 
@@ -478,36 +935,52 @@ string_extract_double_quoted (string, sindex, stripdq)
         ${ ... } pair through according to the Posix.2 specification. */
       if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
        {
+         int free_ret = 1;
+
          si = i + 2;
          if (string[i + 1] == LPAREN)
-           ret = extract_delimited_string (string, &si, "$(", "(", ")"); /*)*/
+           ret = extract_command_subst (string, &si, (flags & SX_COMPLETE));
          else
-           ret = extract_dollar_brace_string (string, &si, 1);
+           ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, 0);
 
          temp[j++] = '$';
          temp[j++] = string[i + 1];
 
+         /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
+            is set. */
+         if (ret == 0 && no_longjmp_on_fatal_error)
+           {
+             free_ret = 0;
+             ret = string + i + 2;
+           }
+
          for (t = 0; ret[t]; t++, j++)
            temp[j] = ret[t];
-         temp[j++] = string[si];
+         temp[j] = string[si];
 
-         i = si;
-         free (ret);
+         if (string[si])
+           {
+             j++;
+             i = si + 1;
+           }
+         else
+           i = si;
+
+         if (free_ret)
+           free (ret);
          continue;
        }
 
       /* Add any character but a double quote to the quoted string we're
-         accumulating. */
+        accumulating. */
       if (c != '"')
-       {
-         temp[j++] = c;
-         continue;
-       }
+       goto add_one_character;
 
       /* c == '"' */
       if (stripdq)
        {
          dquote ^= 1;
+         i++;
          continue;
        }
 
@@ -524,54 +997,66 @@ string_extract_double_quoted (string, sindex, stripdq)
 }
 
 /* This should really be another option to string_extract_double_quoted. */
-static inline int
-skip_double_quoted (string, sind)
+static int
+skip_double_quoted (string, slen, sind, flags)
      char *string;
+     size_t slen;
      int sind;
+     int flags;
 {
-  int c, j, i;
+  int c, i;
   char *ret;
   int pass_next, backquote, si;
+  DECLARE_MBSTATE;
 
   pass_next = backquote = 0;
-
-  for (j = 0, i = sind; c = string[i]; i++)
+  i = sind;
+  while (c = string[i])
     {
       if (pass_next)
        {
          pass_next = 0;
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
       else if (c == '\\')
        {
          pass_next++;
+         i++;
          continue;
        }
       else if (backquote)
        {
          if (c == '`')
            backquote = 0;
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
       else if (c == '`')
        {
          backquote++;
+         i++;
          continue;
        }
       else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
        {
          si = i + 2;
          if (string[i + 1] == LPAREN)
-           ret = extract_delimited_string (string, &si, "$(", "(", ")");
+           ret = extract_command_subst (string, &si, SX_NOALLOC|(flags&SX_COMPLETE));
          else
-           ret = extract_dollar_brace_string (string, &si, 0);
+           ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, SX_NOALLOC);
 
-         i = si;
-         free (ret);
+         /* These can consume the entire string if they are unterminated */
+         CHECK_STRING_OVERRUN (i, si, slen, c);
+
+         i = si + 1;
          continue;
        }
       else if (c != '"')
-       continue;
+       {
+         ADVANCE_CHAR (string, slen, i);
+         continue;
+       }
       else
        break;
     }
@@ -591,16 +1076,18 @@ string_extract_single_quoted (string, sindex)
      char *string;
      int *sindex;
 {
-  register int i, j;
+  register int i;
+  size_t slen;
   char *t;
+  DECLARE_MBSTATE;
 
-  for (i = *sindex; string[i] && string[i] != '\''; i++)
-    ;
+  /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
+  slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
+  i = *sindex;
+  while (string[i] && string[i] != '\'')
+    ADVANCE_CHAR (string, slen, i);
 
-  j = i - *sindex;
-  t = xmalloc (1 + j);
-  strncpy (t, string + *sindex, j);
-  t[j] = '\0';
+  t = substring (string, *sindex, i);
 
   if (string[i])
     i++;
@@ -609,54 +1096,127 @@ string_extract_single_quoted (string, sindex)
   return (t);
 }
 
+/* Skip over a single-quoted string.  We overload the SX_COMPLETE flag to mean
+   that we are splitting out words for completion and have encountered a $'...'
+   string, which allows backslash-escaped single quotes. */
 static inline int
-skip_single_quoted (string, sind)
-     char *string;
+skip_single_quoted (string, slen, sind, flags)
+     const char *string;
+     size_t slen;
      int sind;
+     int flags;
 {
-  register int i;
+  register int c;
+  DECLARE_MBSTATE;
 
-  for (i = sind; string[i] && string[i] != '\''; i++)
-    ;
-  if (string[i])
-    i++;
-  return i;
+  c = sind;
+  while (string[c] && string[c] != '\'')
+    {
+      if ((flags & SX_COMPLETE) && string[c] == '\\' && string[c+1] == '\'' && string[c+2])
+       ADVANCE_CHAR (string, slen, c);
+      ADVANCE_CHAR (string, slen, c);
+    }
+
+  if (string[c])
+    c++;
+  return c;
 }
 
 /* Just like string_extract, but doesn't hack backslashes or any of
-   that other stuff.  Obeys quoting.  Used to do splitting on $IFS. */
+   that other stuff.  Obeys CTLESC quoting.  Used to do splitting on $IFS. */
 static char *
-string_extract_verbatim (string, sindex, charlist)
-     char *string, *charlist;
+string_extract_verbatim (string, slen, sindex, charlist, flags)
+     char *string;
+     size_t slen;
      int *sindex;
+     char *charlist;
+     int flags;
 {
-  register int i = *sindex;
+  register int i;
+#if defined (HANDLE_MULTIBYTE)
+  size_t clen;
+  wchar_t *wcharlist;
+#endif
   int c;
   char *temp;
+  DECLARE_MBSTATE;
 
-  if (charlist[0] == '\'' && charlist[1] == '\0')
+  if ((flags & SX_NOCTLESC) && charlist[0] == '\'' && charlist[1] == '\0')
     {
       temp = string_extract_single_quoted (string, sindex);
       --*sindex;       /* leave *sindex at separator character */
       return temp;
     }
 
-  for (i = *sindex; c = string[i]; i++)
+  i = *sindex;
+#if 0
+  /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need
+     this only if MB_CUR_MAX > 1. */
+  slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 1;
+#endif
+#if defined (HANDLE_MULTIBYTE)
+  clen = strlen (charlist);
+  wcharlist = 0;
+#endif
+  while (c = string[i])
     {
-      if (c == CTLESC)
+#if defined (HANDLE_MULTIBYTE)
+      size_t mblength;
+#endif
+      if ((flags & SX_NOCTLESC) == 0 && c == CTLESC)
        {
-         i++;
+         i += 2;
+         continue;
+       }
+      /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
+        through, to protect the CTLNULs from later calls to
+        remove_quoted_nulls. */
+      else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC && string[i+1] == CTLNUL)
+       {
+         i += 2;
          continue;
        }
 
+#if defined (HANDLE_MULTIBYTE)
+      mblength = MBLEN (string + i, slen - i);
+      if (mblength > 1)
+       {
+         wchar_t wc;
+         mblength = mbtowc (&wc, string + i, slen - i);
+         if (MB_INVALIDCH (mblength))
+           {
+             if (MEMBER (c, charlist))
+               break;
+           }
+         else
+           {
+             if (wcharlist == 0)
+               {
+                 size_t len;
+                 len = mbstowcs (wcharlist, charlist, 0);
+                 if (len == -1)
+                   len = 0;
+                 wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1));
+                 mbstowcs (wcharlist, charlist, len + 1);
+               }
+
+             if (wcschr (wcharlist, wc))
+               break;
+           }
+       }
+      else             
+#endif
       if (MEMBER (c, charlist))
        break;
+
+      ADVANCE_CHAR (string, slen, i);
     }
 
-  c = i - *sindex;
-  temp = xmalloc (1 + c);
-  strncpy (temp, string + *sindex, c);
-  temp[c] = '\0';
+#if defined (HANDLE_MULTIBYTE)
+  FREE (wcharlist);
+#endif
+
+  temp = substring (string, *sindex, i);
   *sindex = i;
 
   return (temp);
@@ -664,16 +1224,27 @@ string_extract_verbatim (string, sindex, charlist)
 
 /* Extract the $( construct in STRING, and return a new string.
    Start extracting at (SINDEX) as if we had just seen "$(".
-   Make (SINDEX) get the position of the matching ")". */
+   Make (SINDEX) get the position of the matching ")". )
+   XFLAGS is additional flags to pass to other extraction functions. */
 char *
-extract_command_subst (string, sindex)
+extract_command_subst (string, sindex, xflags)
      char *string;
      int *sindex;
+     int xflags;
 {
-  return (extract_delimited_string (string, sindex, "$(", "(", ")"));
-}
+  char *ret;
+
+  if (string[*sindex] == LPAREN || (xflags & SX_COMPLETE))
+    return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/
+  else
+    {
+      xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
+      ret = xparse_dolparen (string, string+*sindex, sindex, xflags);
+      return ret;
+    }
+}
 
-/* Extract the $[ construct in STRING, and return a new string.
+/* Extract the $[ construct in STRING, and return a new string. (])
    Start extracting at (SINDEX) as if we had just seen "$[".
    Make (SINDEX) get the position of the matching "]". */
 char *
@@ -681,7 +1252,7 @@ extract_arithmetic_subst (string, sindex)
      char *string;
      int *sindex;
 {
-  return (extract_delimited_string (string, sindex, "$[", "[", "]"));
+  return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/
 }
 
 #if defined (PROCESS_SUBSTITUTION)
@@ -689,22 +1260,41 @@ extract_arithmetic_subst (string, sindex)
    Start extracting at (SINDEX) as if we had just seen "<(".
    Make (SINDEX) get the position of the matching ")". */ /*))*/
 char *
-extract_process_subst (string, starter, sindex)
+extract_process_subst (string, starter, sindex, xflags)
      char *string;
      char *starter;
      int *sindex;
+     int xflags;
 {
-  return (extract_delimited_string (string, sindex, starter, "(", ")"));
+#if 0
+  return (extract_delimited_string (string, sindex, starter, "(", ")", SX_COMMAND));
+#else
+  xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
+  return (xparse_dolparen (string, string+*sindex, sindex, xflags));
+#endif
 }
 #endif /* PROCESS_SUBSTITUTION */
 
 #if defined (ARRAY_VARS)
+/* This can be fooled by unquoted right parens in the passed string. If
+   each caller verifies that the last character in STRING is a right paren,
+   we don't even need to call extract_delimited_string. */
 char *
 extract_array_assignment_list (string, sindex)
      char *string;
      int *sindex;
 {
-  return (extract_delimited_string (string, sindex, "(", (char *)NULL, ")"));
+  int slen;
+  char *ret;
+
+  slen = strlen (string);      /* ( */
+  if (string[slen - 1] == ')')
+   {
+      ret = substring (string, *sindex, slen - 1);
+      *sindex = slen - 1;
+      return ret;
+    }
+  return 0;  
 }
 #endif
 
@@ -717,21 +1307,25 @@ extract_array_assignment_list (string, sindex)
    contains a character string that can also match CLOSER and thus
    needs to be skipped. */
 static char *
-extract_delimited_string (string, sindex, opener, alt_opener, closer)
+extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
      char *string;
      int *sindex;
      char *opener, *alt_opener, *closer;
+     int flags;
 {
   int i, c, si;
+  size_t slen;
   char *t, *result;
-  int pass_character, nesting_level;
+  int pass_character, nesting_level, in_comment;
   int len_closer, len_opener, len_alt_opener;
+  DECLARE_MBSTATE;
 
+  slen = strlen (string + *sindex) + *sindex;
   len_opener = STRLEN (opener);
   len_alt_opener = STRLEN (alt_opener);
   len_closer = STRLEN (closer);
 
-  pass_character = 0;
+  pass_character = in_comment = 0;
 
   nesting_level = 1;
   i = *sindex;
@@ -740,42 +1334,65 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer)
     {
       c = string[i];
 
+      /* If a recursive call or a call to ADVANCE_CHAR leaves the index beyond
+        the end of the string, catch it and cut the loop. */
+      if (i > slen)
+       {
+         i = slen;
+         c = string[i = slen];
+         break;
+       }
+
       if (c == 0)
-        break;
+       break;
 
-      if (pass_character)      /* previous char was backslash */
+      if (in_comment)
        {
-         pass_character = 0;
-         i++;
+         if (c == '\n')
+           in_comment = 0;
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
 
-      if (c == CTLESC)
+      if (pass_character)      /* previous char was backslash */
        {
-         pass_character++;
-         i++;
+         pass_character = 0;
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
 
-#if 0
-      if (c == '\\' && delimiter == '"' &&
-             (member (string[i], slashify_in_quotes)))
-#else
-      if (c == '\\')
-#endif
+      /* Not exactly right yet; should handle shell metacharacters and
+        multibyte characters, too.  See COMMENT_BEGIN define in parse.y */
+      if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1])))
+       {
+          in_comment = 1;
+          ADVANCE_CHAR (string, slen, i);
+          continue;
+       }
+        
+      if (c == CTLESC || c == '\\')
        {
          pass_character++;
          i++;
          continue;
        }
 
+      /* Process a nested command substitution, but only if we're parsing an
+        arithmetic substitution. */
+      if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN)
+        {
+          si = i + 2;
+          t = extract_command_subst (string, &si, flags|SX_NOALLOC);
+          i = si + 1;
+          continue;
+        }
+
       /* Process a nested OPENER. */
       if (STREQN (string + i, opener, len_opener))
        {
          si = i + len_opener;
-         t = extract_delimited_string (string, &si, opener, alt_opener, closer);
+         t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC);
          i = si + 1;
-         FREE (t);
          continue;
        }
 
@@ -783,9 +1400,8 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer)
       if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
        {
          si = i + len_alt_opener;
-         t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer);
+         t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC);
          i = si + 1;
-         FREE (t);
          continue;
        }
 
@@ -793,7 +1409,7 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer)
         the nesting level. */
       if (STREQN (string + i, closer, len_closer))
        {
-         i += len_closer - 1;  /* move to last char of the closer */
+         i += len_closer - 1;  /* move to last byte of the closer */
          nesting_level--;
          if (nesting_level == 0)
            break;
@@ -801,43 +1417,50 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer)
 
       /* Pass old-style command substitution through verbatim. */
       if (c == '`')
-        {
-          si = i + 1;
-          t = string_extract (string, &si, "`", 0);
-          i = si + 1;
-          FREE (t);
-          continue;
-        }
-
-      /* Pass single-quoted strings through verbatim. */
-      if (c == '\'')
-        {
-          si = i + 1;
-          i = skip_single_quoted (string, si);
-          continue;
-        }
+       {
+         si = i + 1;
+         t = string_extract (string, &si, "`", flags|SX_NOALLOC);
+         i = si + 1;
+         continue;
+       }
 
-      /* Pass embedded double-quoted strings through verbatim as well. */
-      if (c == '"')
-        {
-          si = i + 1;
-          i = skip_double_quoted (string, si);
-          continue;
-        }
+      /* Pass single-quoted and double-quoted strings through verbatim. */
+      if (c == '\'' || c == '"')
+       {
+         si = i + 1;
+         i = (c == '\'') ? skip_single_quoted (string, slen, si, 0)
+                         : skip_double_quoted (string, slen, si, 0);
+         continue;
+       }
 
-      i++;     /* move past this character, which was not special. */
+      /* move past this character, which was not special. */
+      ADVANCE_CHAR (string, slen, i);
     }
 
   if (c == 0 && nesting_level)
     {
-      report_error ("bad substitution: no `%s' in %s", closer, string);
-      jump_to_top_level (DISCARD);
+      if (no_longjmp_on_fatal_error == 0)
+       {
+         last_command_exit_value = EXECUTION_FAILURE;
+         report_error (_("bad substitution: no closing `%s' in %s"), closer, string);
+         exp_jump_to_top_level (DISCARD);
+       }
+      else
+       {
+         *sindex = i;
+         return (char *)NULL;
+       }
     }
 
   si = i - *sindex - len_closer + 1;
-  result = xmalloc (1 + si);
-  strncpy (result, string + *sindex, si);
-  result[si] = '\0';
+  if (flags & SX_NOALLOC)
+    result = (char *)NULL;
+  else    
+    {
+      result = (char *)xmalloc (1 + si);
+      strncpy (result, string + *sindex, si);
+      result[si] = '\0';
+    }
   *sindex = i;
 
   return (result);
@@ -852,23 +1475,35 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer)
    occurs inside double quotes. */
 /* XXX -- this is very similar to extract_delimited_string -- XXX */
 static char *
-extract_dollar_brace_string (string, sindex, quoted)
+extract_dollar_brace_string (string, sindex, quoted, flags)
      char *string;
-     int *sindex, quoted;
+     int *sindex, quoted, flags;
 {
-  register int i, c, l;
-  int pass_character, nesting_level, si;
+  register int i, c;
+  size_t slen;
+  int pass_character, nesting_level, si, dolbrace_state;
   char *result, *t;
+  DECLARE_MBSTATE;
 
   pass_character = 0;
-
   nesting_level = 1;
+  slen = strlen (string + *sindex) + *sindex;
+
+  /* The handling of dolbrace_state needs to agree with the code in parse.y:
+     parse_matched_pair().  The different initial value is to handle the
+     case where this function is called to parse the word in
+     ${param op word} (SX_WORD). */
+  dolbrace_state = (flags & SX_WORD) ? DOLBRACE_WORD : DOLBRACE_PARAM;
+  if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && (flags & SX_POSIXEXP))
+    dolbrace_state = DOLBRACE_QUOTE;
 
-  for (i = *sindex; (c = string[i]); i++)
+  i = *sindex;
+  while (c = string[i])
     {
       if (pass_character)
        {
          pass_character = 0;
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
 
@@ -876,13 +1511,14 @@ extract_dollar_brace_string (string, sindex, quoted)
       if (c == CTLESC || c == '\\')
        {
          pass_character++;
+         i++;
          continue;
        }
 
       if (string[i] == '$' && string[i+1] == LBRACE)
        {
          nesting_level++;
-         i++;
+         i += 2;
          continue;
        }
 
@@ -891,6 +1527,7 @@ extract_dollar_brace_string (string, sindex, quoted)
          nesting_level--;
          if (nesting_level == 0)
            break;
+         i++;
          continue;
        }
 
@@ -899,9 +1536,11 @@ extract_dollar_brace_string (string, sindex, quoted)
       if (c == '`')
        {
          si = i + 1;
-         t = string_extract (string, &si, "`", 0);
-         i = si;
-         free (t);
+         t = string_extract (string, &si, "`", flags|SX_NOALLOC);
+
+         CHECK_STRING_OVERRUN (i, si, slen, c);
+    
+         i = si + 1;
          continue;
        }
 
@@ -910,35 +1549,73 @@ extract_dollar_brace_string (string, sindex, quoted)
       if (string[i] == '$' && string[i+1] == LPAREN)
        {
          si = i + 2;
-         t = extract_delimited_string (string, &si, "$(", "(", ")"); /*)*/
-         i = si;
-         free (t);
+         t = extract_command_subst (string, &si, flags|SX_NOALLOC);
+         i = si + 1;
          continue;
        }
 
-      /* Pass the contents of single-quoted and double-quoted strings
-        through verbatim. */
-      if (c == '\'' || c == '"')
+      /* Pass the contents of double-quoted strings through verbatim. */
+      if (c == '"')
        {
          si = i + 1;
-         i = (c == '\'') ? skip_single_quoted (string, si)
-                         : skip_double_quoted (string, si);
+         i = skip_double_quoted (string, slen, si, 0);
          /* skip_XXX_quoted leaves index one past close quote */
-         i--;
          continue;
        }
+
+      if (c == '\'')
+       {
+/*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/
+         if (posixly_correct && shell_compatibility_level > 42 && dolbrace_state != DOLBRACE_QUOTE && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+           ADVANCE_CHAR (string, slen, i);
+         else
+           {
+             si = i + 1;
+             i = skip_single_quoted (string, slen, si, 0);
+           }
+
+          continue;
+       }
+
+      /* move past this character, which was not special. */
+      ADVANCE_CHAR (string, slen, i);
+
+      /* This logic must agree with parse.y:parse_matched_pair, since they
+        share the same defines. */
+      if (dolbrace_state == DOLBRACE_PARAM && c == '%' && (i - *sindex) > 1)
+       dolbrace_state = DOLBRACE_QUOTE;
+      else if (dolbrace_state == DOLBRACE_PARAM && c == '#' && (i - *sindex) > 1)
+        dolbrace_state = DOLBRACE_QUOTE;
+      else if (dolbrace_state == DOLBRACE_PARAM && c == '/' && (i - *sindex) > 1)
+        dolbrace_state = DOLBRACE_QUOTE2;      /* XXX */
+      else if (dolbrace_state == DOLBRACE_PARAM && c == '^' && (i - *sindex) > 1)
+        dolbrace_state = DOLBRACE_QUOTE;
+      else if (dolbrace_state == DOLBRACE_PARAM && c == ',' && (i - *sindex) > 1)
+        dolbrace_state = DOLBRACE_QUOTE;
+      /* This is intended to handle all of the [:]op expansions and the substring/
+        length/pattern removal/pattern substitution expansions. */
+      else if (dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", c) != 0)
+       dolbrace_state = DOLBRACE_OP;
+      else if (dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", c) == 0)
+       dolbrace_state = DOLBRACE_WORD;
     }
 
   if (c == 0 && nesting_level)
     {
-      report_error ("bad substitution: no ending `}' in %s", string);
-      jump_to_top_level (DISCARD);
+      if (no_longjmp_on_fatal_error == 0)
+       {                       /* { */
+         last_command_exit_value = EXECUTION_FAILURE;
+         report_error (_("bad substitution: no closing `%s' in %s"), "}", string);
+         exp_jump_to_top_level (DISCARD);
+       }
+      else
+       {
+         *sindex = i;
+         return ((char *)NULL);
+       }
     }
 
-  l = i - *sindex;
-  result = xmalloc (1 + l);
-  strncpy (result, string + *sindex, l);
-  result[l] = '\0';
+  result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
   *sindex = i;
 
   return (result);
@@ -950,12 +1627,28 @@ char *
 de_backslash (string)
      char *string;
 {
-  register int i, l;
+  register size_t slen;
+  register int i, j, prev_i;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string);
+  i = j = 0;
 
-  for (i = 0, l = strlen (string); i < l; i++)
-    if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
+  /* Loop copying string[i] to string[j], i >= j. */
+  while (i < slen)
+    {
+      if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
                              string[i + 1] == '$'))
-      strcpy (string + i, string + i + 1);     /* XXX - should be memmove */
+       i++;
+      prev_i = i;
+      ADVANCE_CHAR (string, slen, i);
+      if (j < prev_i)
+       do string[j++] = string[prev_i++]; while (prev_i < i);
+      else
+       j = i;
+    }
+  string[j] = '\0';
+
   return (string);
 }
 
@@ -969,7 +1662,7 @@ unquote_bang (string)
   register int i, j;
   register char *temp;
 
-  temp = xmalloc (1 + strlen (string));
+  temp = (char *)xmalloc (1 + strlen (string));
 
   for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
     {
@@ -984,123 +1677,745 @@ unquote_bang (string)
 }
 #endif
 
-#if defined (READLINE)
-/* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
-   an unclosed quoted string), or if the character at EINDEX is quoted
-   by a backslash. */
-int
-char_is_quoted (string, eindex)
-     char *string;
-     int eindex;
+#define CQ_RETURN(x) do { no_longjmp_on_fatal_error = oldjmp; return (x); } while (0)
+
+/* This function assumes s[i] == open; returns with s[ret] == close; used to
+   parse array subscripts.  FLAGS & 1 means to not attempt to skip over
+   matched pairs of quotes or backquotes, or skip word expansions; it is
+   intended to be used after expansion has been performed and during final
+   assignment parsing (see arrayfunc.c:assign_compound_array_list()). */
+static int
+skip_matched_pair (string, start, open, close, flags)
+     const char *string;
+     int start, open, close, flags;
 {
-  int i, pass_next, quoted;
+  int i, pass_next, backq, si, c, count, oldjmp;
+  size_t slen;
+  char *temp, *ss;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string + start) + start;
+  oldjmp = no_longjmp_on_fatal_error;
+  no_longjmp_on_fatal_error = 1;
 
-  for (i = pass_next = quoted = 0; i <= eindex; i++)
+  i = start + 1;               /* skip over leading bracket */
+  count = 1;
+  pass_next = backq = 0;
+  ss = (char *)string;
+  while (c = string[i])
     {
       if (pass_next)
        {
          pass_next = 0;
-         if (i >= eindex)      /* XXX was if (i >= eindex - 1) */
-           return 1;
+         if (c == 0)
+           CQ_RETURN(i);
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
-      else if (string[i] == '\'' || string[i] == '"')
+      else if (c == '\\')
        {
-         i = (string[i] == '\'') ? skip_single_quoted (string, ++i)
-                                 : skip_double_quoted (string, ++i);
-         if (i > eindex)
-           return 1;
-         i--;  /* the skip functions increment past the closing quote. */
+         pass_next = 1;
+         i++;
+         continue;
        }
-      else if (string[i] == '\\')
+      else if (backq)
        {
-         pass_next = 1;
+         if (c == '`')
+           backq = 0;
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
+      else if ((flags & 1) == 0 && c == '`')
+       {
+         backq = 1;
+         i++;
+         continue;
+       }
+      else if ((flags & 1) == 0 && c == open)
+       {
+         count++;
+         i++;
+         continue;
+       }
+      else if (c == close)
+       {
+         count--;
+         if (count == 0)
+           break;
+         i++;
+         continue;
+       }
+      else if ((flags & 1) == 0 && (c == '\'' || c == '"'))
+       {
+         i = (c == '\'') ? skip_single_quoted (ss, slen, ++i, 0)
+                         : skip_double_quoted (ss, slen, ++i, 0);
+         /* no increment, the skip functions increment past the closing quote. */
+       }
+      else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
+       {
+         si = i + 2;
+         if (string[si] == '\0')
+           CQ_RETURN(si);
+
+         if (string[i+1] == LPAREN)
+           temp = extract_delimited_string (ss, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
+         else
+           temp = extract_dollar_brace_string (ss, &si, 0, SX_NOALLOC);
+
+         CHECK_STRING_OVERRUN (i, si, slen, c);
+
+         i = si;
+         if (string[i] == '\0')        /* don't increment i past EOS in loop */
+           break;
+         i++;
+         continue;
+       }
+      else
+       ADVANCE_CHAR (string, slen, i);
     }
-  return (0);
+
+  CQ_RETURN(i);
 }
 
+#if defined (ARRAY_VARS)
 int
-unclosed_pair (string, eindex, openstr)
-     char *string;
-     int eindex;
-     char *openstr;
+skipsubscript (string, start, flags)
+     const char *string;
+     int start, flags;
 {
-  int i, pass_next, openc, olen;
+  return (skip_matched_pair (string, start, '[', ']', flags));
+}
+#endif
 
-  olen = strlen (openstr);
-  for (i = pass_next = openc = 0; i <= eindex; i++)
-    {
+/* Skip characters in STRING until we find a character in DELIMS, and return
+   the index of that character.  START is the index into string at which we
+   begin.  This is similar in spirit to strpbrk, but it returns an index into
+   STRING and takes a starting index.  This little piece of code knows quite
+   a lot of shell syntax.  It's very similar to skip_double_quoted and other
+   functions of that ilk. */
+int
+skip_to_delim (string, start, delims, flags)
+     char *string;
+     int start;
+     char *delims;
+     int flags;
+{
+  int i, pass_next, backq, dquote, si, c, oldjmp;
+  int invert, skipquote, skipcmd, noprocsub, completeflag;
+  int arithexp, skipcol;
+  size_t slen;
+  char *temp, open[3];
+  DECLARE_MBSTATE;
+
+  slen = strlen (string + start) + start;
+  oldjmp = no_longjmp_on_fatal_error;
+  if (flags & SD_NOJMP)
+    no_longjmp_on_fatal_error = 1;
+  invert = (flags & SD_INVERT);
+  skipcmd = (flags & SD_NOSKIPCMD) == 0;
+  noprocsub = (flags & SD_NOPROCSUB);
+  completeflag = (flags & SD_COMPLETE) ? SX_COMPLETE : 0;
+
+  arithexp = (flags & SD_ARITHEXP);
+  skipcol = 0;
+
+  i = start;
+  pass_next = backq = dquote = 0;
+  while (c = string[i])
+    {
+      /* If this is non-zero, we should not let quote characters be delimiters
+        and the current character is a single or double quote.  We should not
+        test whether or not it's a delimiter until after we skip single- or
+        double-quoted strings. */
+      skipquote = ((flags & SD_NOQUOTEDELIM) && (c == '\'' || c =='"'));
       if (pass_next)
        {
          pass_next = 0;
-         if (i >= eindex)      /* XXX was if (i >= eindex - 1) */
-           return 0;
+         if (c == 0)
+           CQ_RETURN(i);
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
-      else if (STREQN (string + i, openstr, olen))
+      else if (c == '\\')
        {
-         openc = 1 - openc;
-         i += olen - 1;
+         pass_next = 1;
+         i++;
+         continue;
        }
-      else if (string[i] == '\'' || string[i] == '"')
+      else if (backq)
        {
-         i = (string[i] == '\'') ? skip_single_quoted (string, i)
-                                 : skip_double_quoted (string, i);
-         if (i > eindex)
-           return 0;
+         if (c == '`')
+           backq = 0;
+         ADVANCE_CHAR (string, slen, i);
+         continue;
        }
-      else if (string[i] == '\\')
+      else if (c == '`')
        {
-         pass_next = 1;
+         backq = 1;
+         i++;
          continue;
        }
-    }
-  return (openc);
-}
-#endif /* READLINE */
+      else if (arithexp && skipcol && c == ':')
+       {
+         skipcol--;
+         i++;
+         continue;
+       }
+      else if (arithexp && c == '?')
+       {
+         skipcol++;
+         i++;
+         continue;
+       }
+      else if (skipquote == 0 && invert == 0 && member (c, delims))
+       break;
+      /* the usual case is to use skip_xxx_quoted, but we don't skip over double
+        quoted strings when looking for the history expansion character as a
+        delimiter. */
+      /* special case for programmable completion which takes place before
+         parser converts backslash-escaped single quotes between $'...' to
+         `regular' single-quoted strings. */
+      else if (completeflag && i > 0 && string[i-1] == '$' && c == '\'')
+       i = skip_single_quoted (string, slen, ++i, SX_COMPLETE);
+      else if (c == '\'')
+       i = skip_single_quoted (string, slen, ++i, 0);
+      else if (c == '"')
+       i = skip_double_quoted (string, slen, ++i, completeflag);
+      else if (c == LPAREN && arithexp)
+        {
+          si = i + 1;
+          if (string[si] == '\0')
+           CQ_RETURN(si);
 
-#if 0
-/* UNUSED */
-/* Extract the name of the variable to bind to from the assignment string. */
-char *
-assignment_name (string)
-     char *string;
-{
-  int offset;
-  char *temp;
+         temp = extract_delimited_string (string, &si, "(", "(", ")", SX_NOALLOC); /* ) */
+         i = si;
+         if (string[i] == '\0')        /* don't increment i past EOS in loop */
+           break;
+         i++;
+         continue;         
+        }
+      else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE))
+       {
+         si = i + 2;
+         if (string[si] == '\0')
+           CQ_RETURN(si);
 
-  offset = assignment (string);
-  if (offset == 0)
-    return (char *)NULL;
-  temp = xmalloc (offset + 1);
-  strncpy (temp, string, offset);
-  temp[offset] = '\0';
-  return (temp);
-}
+         if (string[i+1] == LPAREN)
+           temp = extract_delimited_string (string, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
+         else
+           temp = extract_dollar_brace_string (string, &si, 0, SX_NOALLOC);
+         i = si;
+         if (string[i] == '\0')        /* don't increment i past EOS in loop */
+           break;
+         i++;
+         continue;
+       }
+#if defined (PROCESS_SUBSTITUTION)
+      else if (skipcmd && noprocsub == 0 && (c == '<' || c == '>') && string[i+1] == LPAREN)
+       {
+         si = i + 2;
+         if (string[si] == '\0')
+           CQ_RETURN(si);
+#if 1
+         temp = extract_delimited_string (string, &si, (c == '<') ? "<(" : ">(", "(", ")", SX_COMMAND|SX_NOALLOC); /* )) */
+#else
+         temp = extract_process_subst (string, (c == '<') ? "<(" : ">(", &si, 0);
+         free (temp);          /* XXX - not using SX_NOALLOC here yet */
 #endif
+         i = si;
+         if (string[i] == '\0')
+           break;
+         i++;
+         continue;
+       }
+#endif /* PROCESS_SUBSTITUTION */
+#if defined (EXTENDED_GLOB)
+      else if ((flags & SD_EXTGLOB) && extended_glob && string[i+1] == LPAREN && member (c, "?*+!@"))
+       {
+         si = i + 2;
+         if (string[si] == '\0')
+           CQ_RETURN(si);
 
-/* **************************************************************** */
-/*                                                                 */
-/*     Functions to convert strings to WORD_LISTs and vice versa    */
-/*                                                                 */
-/* **************************************************************** */
+         open[0] = c;
+         open[1] = LPAREN;
+         open[2] = '\0';
+         temp = extract_delimited_string (string, &si, open, "(", ")", SX_NOALLOC); /* ) */
 
-/* Return a single string of all the words in LIST.  SEP is the separator
-   to put between individual elements of LIST in the output string. */
-static char *
-string_list_internal (list, sep)
-     WORD_LIST *list;
-     char *sep;
-{
-  register WORD_LIST *t;
-  char *result, *r;
-  int word_len, sep_len, result_size;
+         i = si;
+         if (string[i] == '\0')        /* don't increment i past EOS in loop */
+           break;
+         i++;
+         continue;
+       }
+#endif
+      else if ((flags & SD_GLOB) && c == LBRACK)
+       {
+         si = i + 1;
+         if (string[si] == '\0')
+           CQ_RETURN(si);
+
+         temp = extract_delimited_string (string, &si, "[", "[", "]", SX_NOALLOC); /* ] */
+
+         i = si;
+         if (string[i] == '\0')        /* don't increment i past EOS in loop */
+           break;
+         i++;
+         continue;
+       }
+      else if ((skipquote || invert) && (member (c, delims) == 0))
+       break;
+      else
+       ADVANCE_CHAR (string, slen, i);
+    }
+
+  CQ_RETURN(i);
+}
+
+#if defined (BANG_HISTORY)
+/* Skip to the history expansion character (delims[0]), paying attention to
+   quoted strings and command and process substitution.  This is a stripped-
+   down version of skip_to_delims.  The essential difference is that this
+   resets the quoting state when starting a command substitution */
+int
+skip_to_histexp (string, start, delims, flags)
+     char *string;
+     int start;
+     char *delims;
+     int flags;
+{
+  int i, pass_next, backq, dquote, si, c, oldjmp;
+  int histexp_comsub, histexp_backq, old_dquote;
+  size_t slen;
+  char *temp, open[3];
+  DECLARE_MBSTATE;
+
+  slen = strlen (string + start) + start;
+  oldjmp = no_longjmp_on_fatal_error;
+  if (flags & SD_NOJMP)
+    no_longjmp_on_fatal_error = 1;
+
+  histexp_comsub = histexp_backq = old_dquote = 0;
+
+  i = start;
+  pass_next = backq = dquote = 0;
+  while (c = string[i])
+    {
+      if (pass_next)
+       {
+         pass_next = 0;
+         if (c == 0)
+           CQ_RETURN(i);
+         ADVANCE_CHAR (string, slen, i);
+         continue;
+       }
+      else if (c == '\\')
+       {
+         pass_next = 1;
+         i++;
+         continue;
+       }
+      else if (backq && c == '`')
+       {
+         backq = 0;
+         histexp_backq--;
+         dquote = old_dquote;
+         i++;
+         continue;
+       }
+      else if (c == '`')
+       {
+         backq = 1;
+         histexp_backq++;
+         old_dquote = dquote;          /* simple - one level for now */
+         dquote = 0;
+         i++;
+         continue;
+       }
+      /* When in double quotes, act as if the double quote is a member of
+        history_no_expand_chars, like the history library does */
+      else if (dquote && c == delims[0] && string[i+1] == '"')
+       {
+         i++;
+         continue;
+       }
+      else if (c == delims[0])
+       break;
+      /* the usual case is to use skip_xxx_quoted, but we don't skip over double
+        quoted strings when looking for the history expansion character as a
+        delimiter. */
+      else if (dquote && c == '\'')
+        {
+          i++;
+          continue;
+        }
+      else if (c == '\'')
+       i = skip_single_quoted (string, slen, ++i, 0);
+      /* The posixly_correct test makes posix-mode shells allow double quotes
+        to quote the history expansion character */
+      else if (posixly_correct == 0 && c == '"')
+       {
+         dquote = 1 - dquote;
+         i++;
+         continue;
+       }     
+      else if (c == '"')
+       i = skip_double_quoted (string, slen, ++i, 0);
+#if defined (PROCESS_SUBSTITUTION)
+      else if ((c == '$' || c == '<' || c == '>') && string[i+1] == LPAREN && string[i+2] != LPAREN)
+#else
+      else if (c == '$' && string[i+1] == LPAREN && string[i+2] != LPAREN)
+#endif
+        {
+         if (string[i+2] == '\0')
+           CQ_RETURN(i+2);
+         i += 2;
+         histexp_comsub++;
+         old_dquote = dquote;
+         dquote = 0;
+        }
+      else if (histexp_comsub && c == RPAREN)
+       {
+         histexp_comsub--;
+         dquote = old_dquote;
+         i++;
+         continue;
+       }
+      else if (backq)          /* placeholder */
+       {
+         ADVANCE_CHAR (string, slen, i);
+         continue;
+       }
+      else
+       ADVANCE_CHAR (string, slen, i);
+    }
+
+  CQ_RETURN(i);
+}
+#endif /* BANG_HISTORY */
+
+#if defined (READLINE)
+/* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
+   an unclosed quoted string), or if the character at EINDEX is quoted
+   by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
+   single and double-quoted string parsing functions should not return an
+   error if there are unclosed quotes or braces.  The characters that this
+   recognizes need to be the same as the contents of
+   rl_completer_quote_characters. */
+
+int
+char_is_quoted (string, eindex)
+     char *string;
+     int eindex;
+{
+  int i, pass_next, c, oldjmp;
+  size_t slen;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string);
+  oldjmp = no_longjmp_on_fatal_error;
+  no_longjmp_on_fatal_error = 1;
+  i = pass_next = 0;
+  while (i <= eindex)
+    {
+      c = string[i];
+
+      if (pass_next)
+       {
+         pass_next = 0;
+         if (i >= eindex)      /* XXX was if (i >= eindex - 1) */
+           CQ_RETURN(1);
+         ADVANCE_CHAR (string, slen, i);
+         continue;
+       }
+      else if (c == '\\')
+       {
+         pass_next = 1;
+         i++;
+         continue;
+       }
+      else if (c == '$' && string[i+1] == '\'' && string[i+2])
+       {
+         i += 2;
+         i = skip_single_quoted (string, slen, i, SX_COMPLETE);
+         if (i > eindex)
+           CQ_RETURN (i);
+       }
+      else if (c == '\'' || c == '"')
+       {
+         i = (c == '\'') ? skip_single_quoted (string, slen, ++i, 0)
+                         : skip_double_quoted (string, slen, ++i, SX_COMPLETE);
+         if (i > eindex)
+           CQ_RETURN(1);
+         /* no increment, the skip_xxx functions go one past end */
+       }
+      else
+       ADVANCE_CHAR (string, slen, i);
+    }
+
+  CQ_RETURN(0);
+}
+
+int
+unclosed_pair (string, eindex, openstr)
+     char *string;
+     int eindex;
+     char *openstr;
+{
+  int i, pass_next, openc, olen;
+  size_t slen;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string);
+  olen = strlen (openstr);
+  i = pass_next = openc = 0;
+  while (i <= eindex)
+    {
+      if (pass_next)
+       {
+         pass_next = 0;
+         if (i >= eindex)      /* XXX was if (i >= eindex - 1) */
+           return 0;
+         ADVANCE_CHAR (string, slen, i);
+         continue;
+       }
+      else if (string[i] == '\\')
+       {
+         pass_next = 1;
+         i++;
+         continue;
+       }
+      else if (STREQN (string + i, openstr, olen))
+       {
+         openc = 1 - openc;
+         i += olen;
+       }
+      /* XXX - may want to handle $'...' specially here */
+      else if (string[i] == '\'' || string[i] == '"')
+       {
+         i = (string[i] == '\'') ? skip_single_quoted (string, slen, i, 0)
+                                 : skip_double_quoted (string, slen, i, SX_COMPLETE);
+         if (i > eindex)
+           return 0;
+       }
+      else
+       ADVANCE_CHAR (string, slen, i);
+    }
+  return (openc);
+}
+
+/* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
+   individual words.  If DELIMS is NULL, the current value of $IFS is used
+   to split the string, and the function follows the shell field splitting
+   rules.  SENTINEL is an index to look for.  NWP, if non-NULL,
+   gets the number of words in the returned list.  CWP, if non-NULL, gets
+   the index of the word containing SENTINEL.  Non-whitespace chars in
+   DELIMS delimit separate fields. */
+WORD_LIST *
+split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp)
+     char *string;
+     int slen;
+     char *delims;
+     int sentinel, flags;
+     int *nwp, *cwp;
+{
+  int ts, te, i, nw, cw, ifs_split, dflags;
+  char *token, *d, *d2;
+  WORD_LIST *ret, *tl;
+
+  if (string == 0 || *string == '\0')
+    {
+      if (nwp)
+       *nwp = 0;
+      if (cwp)
+       *cwp = 0;       
+      return ((WORD_LIST *)NULL);
+    }
+
+  d = (delims == 0) ? ifs_value : delims;
+  ifs_split = delims == 0;
+
+  /* Make d2 the non-whitespace characters in delims */
+  d2 = 0;
+  if (delims)
+    {
+      size_t slength;
+#if defined (HANDLE_MULTIBYTE)
+      size_t mblength = 1;
+#endif
+      DECLARE_MBSTATE;
+
+      slength = strlen (delims);
+      d2 = (char *)xmalloc (slength + 1);
+      i = ts = 0;
+      while (delims[i])
+       {
+#if defined (HANDLE_MULTIBYTE)
+         mbstate_t state_bak;
+         state_bak = state;
+         mblength = MBRLEN (delims + i, slength, &state);
+         if (MB_INVALIDCH (mblength))
+           state = state_bak;
+         else if (mblength > 1)
+           {
+             memcpy (d2 + ts, delims + i, mblength);
+             ts += mblength;
+             i += mblength;
+             slength -= mblength;
+             continue;
+           }
+#endif
+         if (whitespace (delims[i]) == 0)
+           d2[ts++] = delims[i];
+
+         i++;
+         slength--;
+       }
+      d2[ts] = '\0';
+    }
+
+  ret = (WORD_LIST *)NULL;
+
+  /* Remove sequences of whitespace characters at the start of the string, as
+     long as those characters are delimiters. */
+  for (i = 0; member (string[i], d) && spctabnl (string[i]); i++)
+    ;
+  if (string[i] == '\0')
+    {
+      FREE (d2);
+      return (ret);
+    }
+
+  ts = i;
+  nw = 0;
+  cw = -1;
+  dflags = flags|SD_NOJMP;
+  while (1)
+    {
+      te = skip_to_delim (string, ts, d, dflags);
+
+      /* If we have a non-whitespace delimiter character, use it to make a
+        separate field.  This is just about what $IFS splitting does and
+        is closer to the behavior of the shell parser. */
+      if (ts == te && d2 && member (string[ts], d2))
+       {
+         te = ts + 1;
+         /* If we're using IFS splitting, the non-whitespace delimiter char
+            and any additional IFS whitespace delimits a field. */
+         if (ifs_split)
+           while (member (string[te], d) && spctabnl (string[te]))
+             te++;
+         else
+           while (member (string[te], d2))
+             te++;
+       }
+
+      token = substring (string, ts, te);
+
+      ret = add_string_to_list (token, ret);
+      free (token);
+      nw++;
+
+      if (sentinel >= ts && sentinel <= te)
+       cw = nw;
+
+      /* If the cursor is at whitespace just before word start, set the
+        sentinel word to the current word. */
+      if (cwp && cw == -1 && sentinel == ts-1)
+       cw = nw;
+
+      /* If the cursor is at whitespace between two words, make a new, empty
+        word, add it before (well, after, since the list is in reverse order)
+        the word we just added, and set the current word to that one. */
+      if (cwp && cw == -1 && sentinel < ts)
+       {
+         tl = make_word_list (make_word (""), ret->next);
+         ret->next = tl;
+         cw = nw;
+         nw++;
+       }
+
+      if (string[te] == 0)
+       break;
+
+      i = te;
+      while (member (string[i], d) && (ifs_split || spctabnl(string[i])))
+       i++;
+
+      if (string[i])
+       ts = i;
+      else
+       break;
+    }
+
+  /* Special case for SENTINEL at the end of STRING.  If we haven't found
+     the word containing SENTINEL yet, and the index we're looking for is at
+     the end of STRING (or past the end of the previously-found token,
+     possible if the end of the line is composed solely of IFS whitespace)
+     add an additional null argument and set the current word pointer to that. */
+  if (cwp && cw == -1 && (sentinel >= slen || sentinel >= te))
+    {
+      if (whitespace (string[sentinel - 1]))
+       {
+         token = "";
+         ret = add_string_to_list (token, ret);
+         nw++;
+       }
+      cw = nw;
+    }
+
+  if (nwp)
+    *nwp = nw;
+  if (cwp)
+    *cwp = cw;
+
+  FREE (d2);
+
+  return (REVERSE_LIST (ret, WORD_LIST *));
+}
+#endif /* READLINE */
+
+#if 0
+/* UNUSED */
+/* Extract the name of the variable to bind to from the assignment string. */
+char *
+assignment_name (string)
+     char *string;
+{
+  int offset;
+  char *temp;
+
+  offset = assignment (string, 0);
+  if (offset == 0)
+    return (char *)NULL;
+  temp = substring (string, 0, offset);
+  return (temp);
+}
+#endif
+
+/* **************************************************************** */
+/*                                                                 */
+/*     Functions to convert strings to WORD_LISTs and vice versa    */
+/*                                                                 */
+/* **************************************************************** */
+
+/* Return a single string of all the words in LIST.  SEP is the separator
+   to put between individual elements of LIST in the output string. */
+char *
+string_list_internal (list, sep)
+     WORD_LIST *list;
+     char *sep;
+{
+  register WORD_LIST *t;
+  char *result, *r;
+  size_t word_len, sep_len, result_size;
 
   if (list == 0)
     return ((char *)NULL);
 
+  /* Short-circuit quickly if we don't need to separate anything. */
+  if (list->next == 0)
+    return (savestring (list->word->word));
+
   /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
   sep_len = STRLEN (sep);
   result_size = 0;
@@ -1112,7 +2427,7 @@ string_list_internal (list, sep)
       result_size += strlen (t->word->word);
     }
 
-  r = result = xmalloc (result_size + 1);
+  r = result = (char *)xmalloc (result_size + 1);
 
   for (t = list; t; t = t->next)
     {
@@ -1145,23 +2460,86 @@ string_list (list)
   return (string_list_internal (list, " "));
 }
 
+/* An external interface that can be used by the rest of the shell to
+   obtain a string containing the first character in $IFS.  Handles all
+   the multibyte complications.  If LENP is non-null, it is set to the
+   length of the returned string. */
+char *
+ifs_firstchar (lenp)
+     int *lenp;
+{
+  char *ret;
+  int len;
+
+  ret = xmalloc (MB_LEN_MAX + 1);
+#if defined (HANDLE_MULTIBYTE)
+  if (ifs_firstc_len == 1)
+    {
+      ret[0] = ifs_firstc[0];
+      ret[1] = '\0';
+      len = ret[0] ? 1 : 0;
+    }
+  else
+    {
+      memcpy (ret, ifs_firstc, ifs_firstc_len);
+      ret[len = ifs_firstc_len] = '\0';
+    }
+#else
+  ret[0] = ifs_firstc;
+  ret[1] = '\0';
+  len = ret[0] ? 0 : 1;
+#endif
+
+  if (lenp)
+    *lenp = len;
+
+  return ret;
+}
+
 /* Return a single string of all the words present in LIST, obeying the
    quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
    expansion [of $*] appears within a double quoted string, it expands
    to a single field with the value of each parameter separated by the
    first character of the IFS variable, or by a <space> if IFS is unset." */
-static char *
+char *
 string_list_dollar_star (list)
      WORD_LIST *list;
 {
-  char *ifs, sep[2];
-
-  ifs = get_string_value ("IFS");
+  char *ret;
+#if defined (HANDLE_MULTIBYTE)
+#  if defined (__GNUC__)
+  char sep[MB_CUR_MAX + 1];
+#  else
+  char *sep = 0;
+#  endif
+#else
+  char sep[2];
+#endif
 
-  sep[0] = (ifs == 0) ? ' ' : *ifs;
+#if defined (HANDLE_MULTIBYTE)
+#  if !defined (__GNUC__)
+  sep = (char *)xmalloc (MB_CUR_MAX + 1);
+#  endif /* !__GNUC__ */
+  if (ifs_firstc_len == 1)
+    {
+      sep[0] = ifs_firstc[0];
+      sep[1] = '\0';
+    }
+  else
+    {
+      memcpy (sep, ifs_firstc, ifs_firstc_len);
+      sep[ifs_firstc_len] = '\0';
+    }
+#else
+  sep[0] = ifs_firstc;
   sep[1] = '\0';
+#endif
 
-  return (string_list_internal (list, sep));
+  ret = string_list_internal (list, sep);
+#if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
+  free (sep);
+#endif
+  return ret;
 }
 
 /* Turn $@ into a string.  If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
@@ -1172,24 +2550,128 @@ string_list_dollar_star (list)
    <space><tab><newline>, IFS characters in the words in the list should
    also be split.  If IFS is null, and the word is not quoted, we need
    to quote the words in the list to preserve the positional parameters
-   exactly. */
-static char *
-string_list_dollar_at (list, quoted)
+   exactly.
+   Valid values for the FLAGS argument are the PF_ flags in command.h,
+   the only one we care about is PF_ASSIGNRHS.  $@ is supposed to expand
+   to the positional parameters separated by spaces no matter what IFS is
+   set to if in a context where word splitting is not performed.  The only
+   one that we didn't handle before is assignment statement arguments to
+   declaration builtins like `declare'. */
+char *
+string_list_dollar_at (list, quoted, flags)
      WORD_LIST *list;
      int quoted;
+     int flags;
 {
-  char *ifs, sep[2];
+  char *ifs, *ret;
+#if defined (HANDLE_MULTIBYTE)
+#  if defined (__GNUC__)
+  char sep[MB_CUR_MAX + 1];
+#  else
+  char *sep = 0;
+#  endif /* !__GNUC__ */
+#else
+  char sep[2];
+#endif
   WORD_LIST *tlist;
 
-  ifs = get_string_value ("IFS");
+  /* XXX this could just be ifs = ifs_value; */
+  ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
 
-  sep[0] = (ifs == 0 || *ifs == 0) ? ' ' : *ifs;
+#if defined (HANDLE_MULTIBYTE)
+#  if !defined (__GNUC__)
+  sep = (char *)xmalloc (MB_CUR_MAX + 1);
+#  endif /* !__GNUC__ */
+  /* XXX - bash-4.4/bash-5.0 testing PF_ASSIGNRHS */
+  if (flags & PF_ASSIGNRHS)
+    {
+      sep[0] = ' ';
+      sep[1] = '\0';
+    }
+  else if (ifs && *ifs)
+    {
+      if (ifs_firstc_len == 1)
+       {
+         sep[0] = ifs_firstc[0];
+         sep[1] = '\0';
+       }
+      else
+       {
+         memcpy (sep, ifs_firstc, ifs_firstc_len);
+         sep[ifs_firstc_len] = '\0';
+       }
+    }
+  else
+    {
+      sep[0] = ' ';
+      sep[1] = '\0';
+    }
+#else
+  /* XXX - bash-4.4/bash-5.0 test PF_ASSIGNRHS */
+  sep[0] = ((flags & PF_ASSIGNRHS) || ifs == 0 || *ifs == 0) ? ' ' : *ifs;
   sep[1] = '\0';
+#endif
+
+  /* XXX -- why call quote_list if ifs == 0?  we can get away without doing
+     it now that quote_escapes quotes spaces */
+  tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
+               ? quote_list (list)
+               : list_quote_escapes (list);
+
+  ret = string_list_internal (tlist, sep);
+#if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
+  free (sep);
+#endif
+  return ret;
+}
+
+/* Turn the positional parameters into a string, understanding quoting and
+   the various subtleties of using the first character of $IFS as the
+   separator.  Calls string_list_dollar_at, string_list_dollar_star, and
+   string_list as appropriate. */
+char *
+string_list_pos_params (pchar, list, quoted)
+     int pchar;
+     WORD_LIST *list;
+     int quoted;
+{
+  char *ret;
+  WORD_LIST *tlist;
+
+  if (pchar == '*' && (quoted & Q_DOUBLE_QUOTES))
+    {
+      tlist = quote_list (list);
+      word_list_remove_quoted_nulls (tlist);
+      ret = string_list_dollar_star (tlist);
+    }
+  else if (pchar == '*' && (quoted & Q_HERE_DOCUMENT))
+    {
+      tlist = quote_list (list);
+      word_list_remove_quoted_nulls (tlist);
+      ret = string_list (tlist);
+    }
+  else if (pchar == '*')
+    {
+      /* Even when unquoted, string_list_dollar_star does the right thing
+        making sure that the first character of $IFS is used as the
+        separator. */
+      ret = string_list_dollar_star (list);
+    }
+  else if (pchar == '@' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+    /* We use string_list_dollar_at, but only if the string is quoted, since
+       that quotes the escapes if it's not, which we don't want.  We could
+       use string_list (the old code did), but that doesn't do the right
+       thing if the first character of $IFS is not a space.  We use
+       string_list_dollar_star if the string is unquoted so we make sure that
+       the elements of $@ are separated by the first character of $IFS for
+       later splitting. */
+    ret = string_list_dollar_at (list, quoted, 0);
+  else if (pchar == '@')
+    ret = string_list_dollar_star (list);
+  else
+    ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list);
 
-  tlist = ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (ifs && *ifs == 0))
-               ? quote_list (list)
-               : list_quote_escapes (list);
-  return (string_list_internal (tlist, sep));
+  return ret;
 }
 
 /* Return the list of words present in STRING.  Separate the string into
@@ -1219,7 +2701,10 @@ string_list_dollar_at (list, quoted)
 
 /* This performs word splitting and quoted null character removal on
    STRING. */
-#define issep(c)       (member ((c), separators))
+#define issep(c) \
+       (((separators)[0]) ? ((separators)[1] ? isifs(c) \
+                                             : (c) == (separators)[0]) \
+                          : 0)
 
 WORD_LIST *
 list_string (string, separators, quoted)
@@ -1229,14 +2714,23 @@ list_string (string, separators, quoted)
   WORD_LIST *result;
   WORD_DESC *t;
   char *current_word, *s;
-  int sindex, sh_style_split;
+  int sindex, sh_style_split, whitesep, xflags;
+  size_t slen;
 
   if (!string || !*string)
     return ((WORD_LIST *)NULL);
 
-  sh_style_split =
-    separators && *separators && (STREQ (separators, " \t\n"));
+  sh_style_split = separators && separators[0] == ' ' &&
+                                separators[1] == '\t' &&
+                                separators[2] == '\n' &&
+                                separators[3] == '\0';
+  for (xflags = 0, s = ifs_value; s && *s; s++)
+    {
+      if (*s == CTLESC) xflags |= SX_NOCTLESC;
+      else if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
+    }
 
+  slen = 0;
   /* Remove sequences of whitespace at the beginning of STRING, as
      long as those characters appear in IFS.  Do not do this if
      STRING is quoted or if there are no separator characters. */
@@ -1252,12 +2746,15 @@ list_string (string, separators, quoted)
 
   /* OK, now STRING points to a word that does not begin with white space.
      The splitting algorithm is:
-       extract a word, stopping at a separator
-       skip sequences of spc, tab, or nl as long as they are separators
+       extract a word, stopping at a separator
+       skip sequences of spc, tab, or nl as long as they are separators
      This obeys the field splitting rules in Posix.2. */
+  slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 1;
   for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
     {
-      current_word = string_extract_verbatim (string, &sindex, separators);
+      /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
+        unless multibyte chars are possible. */
+      current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags);
       if (current_word == 0)
        break;
 
@@ -1267,10 +2764,9 @@ list_string (string, separators, quoted)
         below. */
       if (QUOTED_NULL (current_word))
        {
-         t = make_bare_word ("");
-         t->flags |= W_QUOTED;
-         free (t->word);
+         t = alloc_word_desc ();
          t->word = make_quoted_char ('\0');
+         t->flags |= W_QUOTED|W_HASQUOTEDNULL;
          result = make_word_list (t, result);
        }
       else if (current_word[0] != '\0')
@@ -1278,11 +2774,8 @@ list_string (string, separators, quoted)
          /* If we have something, then add it regardless.  However,
             perform quoted null character removal on the current word. */
          remove_quoted_nulls (current_word);
-#if 0
-         result = make_word_list (make_word (current_word), result);
-#else
          result = add_string_to_list (current_word, result);
-#endif   
+         result->word->flags &= ~W_HASQUOTEDNULL;      /* just to be sure */
          if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
            result->word->flags |= W_QUOTED;
        }
@@ -1291,23 +2784,41 @@ list_string (string, separators, quoted)
         Bourne shell style, then add a quoted null argument. */
       else if (!sh_style_split && !spctabnl (string[sindex]))
        {
-         t = make_bare_word ("");
-         t->flags |= W_QUOTED;
-         free (t->word);
+         t = alloc_word_desc ();
          t->word = make_quoted_char ('\0');
+         t->flags |= W_QUOTED|W_HASQUOTEDNULL;
          result = make_word_list (t, result);
        }
 
       free (current_word);
 
+      /* Note whether or not the separator is IFS whitespace, used later. */
+      whitesep = string[sindex] && spctabnl (string[sindex]);
+
       /* Move past the current separator character. */
       if (string[sindex])
-       sindex++;
+       {
+         DECLARE_MBSTATE;
+         ADVANCE_CHAR (string, slen, sindex);
+       }
 
       /* Now skip sequences of space, tab, or newline characters if they are
         in the list of separators. */
       while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex]))
        sindex++;
+
+      /* If the first separator was IFS whitespace and the current character
+        is a non-whitespace IFS character, it should be part of the current
+        field delimiter, not a separate delimiter that would result in an
+        empty field.  Look at POSIX.2, 3.6.5, (3)(b). */
+      if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex]))
+       {
+         sindex++;
+         /* An IFS character that is not IFS white space, along with any
+            adjacent IFS white space, shall delimit a field. (SUSv3) */
+         while (string[sindex] && spctabnl (string[sindex]) && isifs (string[sindex]))
+           sindex++;
+       }
     }
   return (REVERSE_LIST (result, WORD_LIST *));
 }
@@ -1315,29 +2826,48 @@ list_string (string, separators, quoted)
 /* Parse a single word from STRING, using SEPARATORS to separate fields.
    ENDPTR is set to the first character after the word.  This is used by
    the `read' builtin.
+   
+   This is never called with SEPARATORS != $IFS, and takes advantage of that.
+
    XXX - this function is very similar to list_string; they should be
         combined - XXX */
+
+#define islocalsep(c)  (local_cmap[(unsigned char)(c)] != 0)
+
 char *
 get_word_from_string (stringp, separators, endptr)
      char **stringp, *separators, **endptr;
 {
   register char *s;
   char *current_word;
-  int sindex, sh_style_split;
+  int sindex, sh_style_split, whitesep, xflags;
+  unsigned char local_cmap[UCHAR_MAX+1];       /* really only need single-byte chars here */
+  size_t slen;
 
   if (!stringp || !*stringp || !**stringp)
     return ((char *)NULL);
 
-  s = *stringp;
+  sh_style_split = separators && separators[0] == ' ' &&
+                                separators[1] == '\t' &&
+                                separators[2] == '\n' &&
+                                separators[3] == '\0';
+  memset (local_cmap, '\0', sizeof (local_cmap));
+  for (xflags = 0, s = separators; s && *s; s++)
+    {
+      if (*s == CTLESC) xflags |= SX_NOCTLESC;
+      if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
+      local_cmap[(unsigned char)*s] = 1;       /* local charmap of separators */
+    }
 
-  sh_style_split =
-    separators && *separators && (STREQ (separators, " \t\n"));
+  s = *stringp;
+  slen = 0;
 
   /* Remove sequences of whitespace at the beginning of STRING, as
-     long as those characters appear in IFS. */
-  if (sh_style_split || !separators || !*separators)
+     long as those characters appear in SEPARATORS.  This happens if
+     SEPARATORS == $' \t\n' or if IFS is unset. */
+  if (sh_style_split || separators == 0)
     {
-      for (; *s && spctabnl (*s) && issep (*s); s++);
+      for (; *s && spctabnl (*s) && islocalsep (*s); s++);
 
       /* If the string is nothing but whitespace, update it and return. */
       if (!*s)
@@ -1356,21 +2886,43 @@ get_word_from_string (stringp, separators, endptr)
 
      This obeys the field splitting rules in Posix.2. */
   sindex = 0;
-  current_word = string_extract_verbatim (s, &sindex, separators);
+  /* Don't need string length in ADVANCE_CHAR unless multibyte chars are
+     possible, but need it in string_extract_verbatim for bounds checking */
+  slen = STRLEN (s);
+  current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags);
 
   /* Set ENDPTR to the first character after the end of the word. */
   if (endptr)
     *endptr = s + sindex;
 
+  /* Note whether or not the separator is IFS whitespace, used later. */
+  whitesep = s[sindex] && spctabnl (s[sindex]);
+
   /* Move past the current separator character. */
   if (s[sindex])
-    sindex++;
+    {
+      DECLARE_MBSTATE;
+      ADVANCE_CHAR (s, slen, sindex);
+    }
 
   /* Now skip sequences of space, tab, or newline characters if they are
      in the list of separators. */
-  while (s[sindex] && spctabnl (s[sindex]) && issep (s[sindex]))
+  while (s[sindex] && spctabnl (s[sindex]) && islocalsep (s[sindex]))
     sindex++;
 
+  /* If the first separator was IFS whitespace and the current character is
+     a non-whitespace IFS character, it should be part of the current field
+     delimiter, not a separate delimiter that would result in an empty field.
+     Look at POSIX.2, 3.6.5, (3)(b). */
+  if (s[sindex] && whitesep && islocalsep (s[sindex]) && !spctabnl (s[sindex]))
+    {
+      sindex++;
+      /* An IFS character that is not IFS white space, along with any adjacent
+        IFS white space, shall delimit a field. */
+      while (s[sindex] && spctabnl (s[sindex]) && islocalsep(s[sindex]))
+       sindex++;
+    }
+
   /* Update STRING to point to the next field. */
   *stringp = s + sindex;
   return (current_word);
@@ -1389,20 +2941,24 @@ strip_trailing_ifs_whitespace (string, separators, saw_escape)
   char *s;
 
   s = string + STRLEN (string) - 1;
-  while (s > string && ((spctabnl (*s) && issep (*s)) ||
+  while (s > string && ((spctabnl (*s) && isifs (*s)) ||
                        (saw_escape && *s == CTLESC && spctabnl (s[1]))))
     s--;
   *++s = '\0';
   return string;
 }
 
-#if defined (ARRAY_VARS)
+#if 0
+/* UNUSED */
+/* Split STRING into words at whitespace.  Obeys shell-style quoting with
+   backslashes, single and double quotes. */
 WORD_LIST *
 list_string_with_quotes (string)
      char *string;
 {
   WORD_LIST *list;
   char *token, *s;
+  size_t s_len;
   int c, i, tokstart, len;
 
   for (s = string; s && *s && spctabnl (*s); s++)
@@ -1410,6 +2966,7 @@ list_string_with_quotes (string)
   if (s == 0 || *s == 0)
     return ((WORD_LIST *)NULL);
 
+  s_len = strlen (s);
   tokstart = i = 0;
   list = (WORD_LIST *)NULL;
   while (1)
@@ -1422,22 +2979,15 @@ list_string_with_quotes (string)
            i++;
        }
       else if (c == '\'')
-        i = skip_single_quoted (s, ++i);
+       i = skip_single_quoted (s, s_len, ++i, 0);
       else if (c == '"')
-       i = skip_double_quoted (s, ++i);
+       i = skip_double_quoted (s, s_len, ++i, 0);
       else if (c == 0 || spctabnl (c))
        {
          /* We have found the end of a token.  Make a word out of it and
             add it to the word list. */
-         len = i - tokstart;
-         token = xmalloc (len + 1);
-         strncpy (token, s + tokstart, len);
-         token[len] = '\0';
-#if 0
-         list = make_word_list (make_word (token), list);
-#else
+         token = substring (s, tokstart, i);
          list = add_string_to_list (token, list);
-#endif
          free (token);
          while (spctabnl (s[i]))
            i++;
@@ -1451,7 +3001,7 @@ list_string_with_quotes (string)
     }
   return (REVERSE_LIST (list, WORD_LIST *));
 }
-#endif /* ARRAY_VARS */
+#endif
 
 /********************************************************/
 /*                                                     */
@@ -1460,37 +3010,76 @@ list_string_with_quotes (string)
 /********************************************************/
 
 #if defined (ARRAY_VARS)
-SHELL_VAR *
-do_array_element_assignment (name, value)
+static SHELL_VAR *
+do_compound_assignment (name, value, flags)
      char *name, *value;
+     int flags;
 {
-  char *t;
-  int ind, ni;
-  SHELL_VAR *entry;
+  SHELL_VAR *v;
+  int mklocal, mkassoc, mkglobal;
+  WORD_LIST *list;
 
-  t = strchr (name, '[');
-  if (t == 0)
-    return ((SHELL_VAR *)NULL);
-  ind = t - name;
-  ni = skipsubscript (name, ind);
-  if ((ALL_ELEMENT_SUB (t[1]) && t[2] == ']') || (ni <= ind + 1))
+  mklocal = flags & ASS_MKLOCAL;
+  mkassoc = flags & ASS_MKASSOC;
+  mkglobal = flags & ASS_MKGLOBAL;
+
+  if (mklocal && variable_context)
+    {
+      v = find_variable (name);
+      if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
+       {
+         if (v && readonly_p (v))
+           err_readonly (name);
+         return (v);   /* XXX */
+       }
+      list = expand_compound_array_assignment (v, value, flags);
+      if (mkassoc)
+       v = make_local_assoc_variable (name);
+      else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context)
+        v = make_local_array_variable (name, 0);
+      if (v)
+       assign_compound_array_list (v, list, flags);
+      if (list)
+       dispose_words (list);
+    }
+  /* In a function but forcing assignment in global context */
+  else if (mkglobal && variable_context)
     {
-      report_error ("%s: bad array subscript", name);
-      return ((SHELL_VAR *)NULL);
+      v = find_global_variable (name);
+      if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
+       {
+         if (v && readonly_p (v))
+           err_readonly (name);
+         return (v);   /* XXX */
+       }
+      list = expand_compound_array_assignment (v, value, flags);
+      if (v == 0 && mkassoc)
+       v = make_new_assoc_variable (name);
+      else if (v && mkassoc && assoc_p (v) == 0)
+       v = convert_var_to_assoc (v);
+      else if (v == 0)
+       v = make_new_array_variable (name);
+      else if (v && mkassoc == 0 && array_p (v) == 0)
+       v = convert_var_to_array (v);
+      if (v)
+       assign_compound_array_list (v, list, flags);
+      if (list)
+       dispose_words (list);
     }
-  *t++ = '\0';
-  ind = array_expand_index (t, ni - ind);
-  if (ind < 0)
+  else
     {
-      t[-1] = '[';             /* restore original name */
-      report_error ("%s: bad array subscript", name);
-      return ((SHELL_VAR *)NULL);
+      v = assign_array_from_string (name, value, flags);
+      if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
+       {
+         if (v && readonly_p (v))
+           err_readonly (name);
+         return (v);   /* XXX */
+       }
     }
-  entry = bind_array_variable (name, ind, value);
-  t[-1] = '[';         /* restore original name */
-  return (entry);
+
+  return (v);
 }
-#endif /* ARRAY_VARS */
+#endif
 
 /* Given STRING, an assignment string, get the value of the right side
    of the `=', and bind it to the left side.  If EXPAND is true, then
@@ -1498,112 +3087,153 @@ do_array_element_assignment (name, value)
    expansion on the right-hand side.  Perform tilde expansion in any
    case.  Do not perform word splitting on the result of expansion. */
 static int
-do_assignment_internal (string, expand)
-     char *string;
+do_assignment_internal (word, expand)
+     const WORD_DESC *word;
      int expand;
 {
-  int offset;
-  char *name, *value;
+  int offset, appendop, assign_list, aflags, retval;
+  char *name, *value, *temp;
   SHELL_VAR *entry;
 #if defined (ARRAY_VARS)
   char *t;
-  int ni, assign_list = 0;
+  int ni;
 #endif
+  const char *string;
+
+  if (word == 0 || word->word == 0)
+    return 0;
 
-  offset = assignment (string);
+  appendop = assign_list = aflags = 0;
+  string = word->word;
+  offset = assignment (string, 0);
   name = savestring (string);
   value = (char *)NULL;
 
   if (name[offset] == '=')
     {
-      char *temp;
+      if (name[offset - 1] == '+')
+       {
+         appendop = 1;
+         name[offset - 1] = '\0';
+       }
 
-      name[offset] = 0;
+      name[offset] = 0;                /* might need this set later */
       temp = name + offset + 1;
 
 #if defined (ARRAY_VARS)
-      if (expand && temp[0] == LPAREN && strchr (temp, RPAREN))
+      if (expand && (word->flags & W_COMPASSIGN))
        {
          assign_list = ni = 1;
-         value = extract_delimited_string (temp, &ni, "(", (char *)NULL, ")");
+         value = extract_array_assignment_list (temp, &ni);
        }
       else
 #endif
-
-      /* Perform tilde expansion. */
       if (expand && temp[0])
-        {
-         temp = (strchr (temp, '~') && unquoted_member ('~', temp))
-                       ? bash_tilde_expand (temp)
-                       : savestring (temp);
-
-         value = maybe_expand_string (temp, 0, expand_string_unsplit);
-         free (temp);
-       }
+       value = expand_string_if_necessary (temp, 0, expand_string_assignment);
       else
        value = savestring (temp);
     }
 
   if (value == 0)
     {
-      value = xmalloc (1);
+      value = (char *)xmalloc (1);
       value[0] = '\0';
     }
 
   if (echo_command_at_execute)
-#if defined (ARRAY_VARS)
-    if (assign_list)
-      fprintf (stderr, "%s%s=(%s)\n", indirection_level_string (), name, value);
-    else
-#endif
-    fprintf (stderr, "%s%s=%s\n", indirection_level_string (), name, value);
+    {
+      if (appendop)
+       name[offset - 1] = '+';
+      xtrace_print_assignment (name, value, assign_list, 1);
+      if (appendop)
+       name[offset - 1] = '\0';
+    }
 
 #define ASSIGN_RETURN(r)       do { FREE (value); free (name); return (r); } while (0)
 
+  if (appendop)
+    aflags |= ASS_APPEND;
+
 #if defined (ARRAY_VARS)
-  if (t = strchr (name, '['))
+  if (t = mbschr (name, '['))  /*]*/
     {
       if (assign_list)
        {
-         report_error ("%s: cannot assign list to array member", name);
+         report_error (_("%s: cannot assign list to array member"), name);
          ASSIGN_RETURN (0);
        }
-      entry = do_array_element_assignment (name, value);
+      entry = assign_array_element (name, value, aflags);
       if (entry == 0)
-        ASSIGN_RETURN (0);
+       ASSIGN_RETURN (0);
     }
   else if (assign_list)
-    entry = assign_array_from_string (name, value);
+    {
+      if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL) == 0)
+       aflags |= ASS_MKLOCAL;
+      if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL))
+       aflags |= ASS_MKGLOBAL;
+      if (word->flags & W_ASSIGNASSOC)
+       aflags |= ASS_MKASSOC;
+      entry = do_compound_assignment (name, value, aflags);
+    }
   else
 #endif /* ARRAY_VARS */
-  entry = bind_variable (name, value);
+  entry = bind_variable (name, value, aflags);
 
   stupidly_hack_special_variables (name);
 
-  if (entry)
-    entry->attributes &= ~att_invisible;
-
   /* Return 1 if the assignment seems to have been performed correctly. */
-  ASSIGN_RETURN (entry ? ((entry->attributes & att_readonly) == 0) : 0);
+  if (entry == 0 || readonly_p (entry))
+    retval = 0;                /* assignment failure */
+  else if (noassign_p (entry))
+    {
+      last_command_exit_value = EXECUTION_FAILURE;
+      retval = 1;      /* error status, but not assignment failure */
+    }
+  else
+    retval = 1;
+
+  if (entry && retval != 0 && noassign_p (entry) == 0)
+    VUNSETATTR (entry, att_invisible);
+
+  ASSIGN_RETURN (retval);
 }
 
 /* Perform the assignment statement in STRING, and expand the
-   right side by doing command and parameter expansion. */
+   right side by doing tilde, command and parameter expansion. */
 int
 do_assignment (string)
      char *string;
 {
-  return do_assignment_internal (string, 1);
+  WORD_DESC td;
+
+  td.flags = W_ASSIGNMENT;
+  td.word = string;
+
+  return do_assignment_internal (&td, 1);
+}
+
+int
+do_word_assignment (word, flags)
+     WORD_DESC *word;
+     int flags;
+{
+  return do_assignment_internal (word, 1);
 }
 
 /* Given STRING, an assignment string, get the value of the right side
-   of the `=', and bind it to the left side.  Do not do command and
-   parameter substitution on the right hand side. */
+   of the `=', and bind it to the left side.  Do not perform any word
+   expansions on the right hand side. */
 int
 do_assignment_no_expand (string)
      char *string;
 {
-  return do_assignment_internal (string, 0);
+  WORD_DESC td;
+
+  td.flags = W_ASSIGNMENT;
+  td.word = string;
+
+  return (do_assignment_internal (&td, 0));
 }
 
 /***************************************************
@@ -1645,7 +3275,7 @@ number_of_args ()
 /* Return the value of a positional parameter.  This handles values > 10. */
 char *
 get_dollar_var_value (ind)
-     int ind;
+     intmax_t ind;
 {
   char *temp;
   WORD_LIST *p;
@@ -1656,7 +3286,7 @@ get_dollar_var_value (ind)
     {
       ind -= 10;
       for (p = rest_of_args; p && ind--; p = p->next)
-        ;
+       ;
       temp = p ? savestring (p->word->word) : (char *)NULL;
     }
   return (temp);
@@ -1680,7 +3310,9 @@ string_rest_of_args (dollar_star)
 
 /* Return a string containing the positional parameters from START to
    END, inclusive.  If STRING[0] == '*', we obey the rules for $*,
-   which only makes a difference if QUOTED is non-zero. */
+   which only makes a difference if QUOTED is non-zero.  If QUOTED includes
+   Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
+   no quoting chars are added. */
 static char *
 pos_params (string, start, end, quoted)
      char *string;
@@ -1690,14 +3322,27 @@ pos_params (string, start, end, quoted)
   char *ret;
   int i;
 
+  /* see if we can short-circuit.  if start == end, we want 0 parameters. */
+  if (start == end)
+    return ((char *)NULL);
+
   save = params = list_rest_of_args ();
-  if (save == 0)
+  if (save == 0 && start > 0)
     return ((char *)NULL);
 
-  for (i = 1; params && i < start; i++)
+  if (start == 0)              /* handle ${@:0[:x]} specially */
+    {
+      t = make_word_list (make_word (dollar_vars[0]), params);
+      save = params = t;
+    }
+
+  for (i = start ? 1 : 0; params && i < start; i++)
     params = params->next;
   if (params == 0)
-    return ((char *)NULL);
+    {
+      dispose_words (save);
+      return ((char *)NULL);
+    }
   for (h = t = params; params && i < end; i++)
     {
       t = params;
@@ -1705,11 +3350,11 @@ pos_params (string, start, end, quoted)
     }
 
   t->next = (WORD_LIST *)NULL;
-  if (string[0] == '*')
-    ret = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? string_list_dollar_star (h) : string_list (h);
-  else
-    ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (h) : h);
-  t->next = params;
+
+  ret = string_list_pos_params (string[0], h, quoted);
+
+  if (t != params)
+    t->next = params;
 
   dispose_words (save);
   return (ret);
@@ -1722,30 +3367,36 @@ pos_params (string, start, end, quoted)
 /******************************************************************/
 
 #if defined (PROCESS_SUBSTITUTION)
-#define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC)
+#define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
 #else
-#define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC)
+#define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
 #endif
 
 /* If there are any characters in STRING that require full expansion,
    then call FUNC to expand STRING; otherwise just perform quote
    removal if necessary.  This returns a new string. */
 static char *
-maybe_expand_string (string, quoted, func)
+expand_string_if_necessary (string, quoted, func)
      char *string;
      int quoted;
-     WORD_LIST *(*func)();
+     EXPFUNC *func;
 {
   WORD_LIST *list;
+  size_t slen;
   int i, saw_quote;
   char *ret;
+  DECLARE_MBSTATE;
 
-  for (i = saw_quote = 0; string[i]; i++)
+  /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
+  slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
+  i = saw_quote = 0;
+  while (string[i])
     {
       if (EXP_CHAR (string[i]))
        break;
       else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
        saw_quote = 1;
+      ADVANCE_CHAR (string, slen, i);
     }
 
   if (string[i])
@@ -1763,14 +3414,15 @@ maybe_expand_string (string, quoted, func)
     ret = string_quote_removal (string, quoted);
   else
     ret = savestring (string);
+
   return ret;
 }
 
 static inline char *
-expand_string_to_string (string, quoted, func)
+expand_string_to_string_internal (string, quoted, func)
      char *string;
      int quoted;
-     WORD_LIST *(*func)();
+     EXPFUNC *func;
 {
   WORD_LIST *list;
   char *ret;
@@ -1790,6 +3442,91 @@ expand_string_to_string (string, quoted, func)
   return (ret);
 }
 
+char *
+expand_string_to_string (string, quoted)
+     char *string;
+     int quoted;
+{
+  return (expand_string_to_string_internal (string, quoted, expand_string));
+}
+
+char *
+expand_string_unsplit_to_string (string, quoted)
+     char *string;
+     int quoted;
+{
+  return (expand_string_to_string_internal (string, quoted, expand_string_unsplit));
+}
+
+char *
+expand_assignment_string_to_string (string, quoted)
+     char *string;
+     int quoted;
+{
+  return (expand_string_to_string_internal (string, quoted, expand_string_assignment));
+}
+
+char *
+expand_arith_string (string, quoted)
+     char *string;
+     int quoted;
+{
+  WORD_DESC td;
+  WORD_LIST *list, *tlist;
+  size_t slen;
+  int i, saw_quote;
+  char *ret;
+  DECLARE_MBSTATE;
+
+  /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
+  slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
+  i = saw_quote = 0;
+  while (string[i])
+    {
+      if (EXP_CHAR (string[i]))
+       break;
+      else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
+       saw_quote = 1;
+      ADVANCE_CHAR (string, slen, i);
+    }
+
+  if (string[i])
+    {
+      /* This is expanded version of expand_string_internal as it's called by
+        expand_string_leave_quoted  */
+      td.flags = W_NOPROCSUB;  /* don't want process substitution */
+      td.word = savestring (string);
+      list = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
+      /* This takes care of the calls from expand_string_leave_quoted and
+        expand_string */
+      if (list)
+       {
+         tlist = word_list_split (list);
+         dispose_words (list);
+         list = tlist;
+         if (list)
+           dequote_list (list);
+       }
+      /* This comes from expand_string_if_necessary */
+      if (list)
+       {
+         ret = string_list (list);
+         dispose_words (list);
+       }
+      else
+       ret = (char *)NULL;
+      FREE (td.word);
+    }
+  else if (saw_quote && (quoted & Q_ARITH))
+    ret = string_quote_removal (string, quoted);
+  else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
+    ret = string_quote_removal (string, quoted);
+  else
+    ret = savestring (string);
+
+  return ret;
+}
+
 #if defined (COND_COMMAND)
 /* Just remove backslashes in STRING.  Returns a new string. */
 char *
@@ -1798,13 +3535,13 @@ remove_backslashes (string)
 {
   char *r, *ret, *s;
 
-  r = ret = xmalloc (strlen (string) + 1);
+  r = ret = (char *)xmalloc (strlen (string) + 1);
   for (s = string; s && *s; )
     {
       if (*s == '\\')
-        s++;
+       s++;
       if (*s == 0)
-        break;
+       break;
       *r++ = *s++;
     }
   *r = '\0';
@@ -1813,11 +3550,12 @@ remove_backslashes (string)
 
 /* This needs better error handling. */
 /* Expand W for use as an argument to a unary or binary operator in a
-   [[...]] expression.  If SPECIAL is nonzero, this is the rhs argument
+   [[...]] expression.  If SPECIAL is 1, this is the rhs argument
    to the != or == operator, and should be treated as a pattern.  In
-   this case, we quote the string specially for the globbing code.  The
-   caller is responsible for removing the backslashes if the unquoted
-   words is needed later. */   
+   this case, we quote the string specially for the globbing code.  If
+   SPECIAL is 2, this is an rhs argument for the =~ operator, and should
+   be quoted appropriately for regcomp/regexec.  The caller is responsible
+   for removing the backslashes if the unquoted word is needed later. */   
 char *
 cond_expand_word (w, special)
      WORD_DESC *w;
@@ -1825,24 +3563,35 @@ cond_expand_word (w, special)
 {
   char *r, *p;
   WORD_LIST *l;
+  int qflags;
 
   if (w->word == 0 || w->word[0] == '\0')
     return ((char *)NULL);
 
-  l = call_expand_word_internal (w, 0, (int *)0, (int *)0);
+  expand_no_split_dollar_star = 1;
+  w->flags |= W_NOSPLIT2;
+  l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
+  expand_no_split_dollar_star = 0;
   if (l)
     {
-      if (special == 0)
+      if (special == 0)                        /* LHS */
        {
          dequote_list (l);
          r = string_list (l);
        }
       else
-        {
-          p = string_list (l);
-          r = quote_string_for_globbing (p, QGLOB_CVTNULL);
-          free (p);
-        }
+       {
+         /* Need to figure out whether or not we should call dequote_escapes
+            or a new dequote_ctlnul function here, and under what
+            circumstances. */
+         qflags = QGLOB_CVTNULL;
+         if (special == 2)
+           qflags |= QGLOB_REGEXP;
+         word_list_remove_quoted_nulls (l);
+         p = string_list (l);
+         r = quote_string_for_globbing (p, qflags);
+         free (p);
+       }
       dispose_words (l);
     }
   else
@@ -1856,30 +3605,33 @@ cond_expand_word (w, special)
    A convenience function for functions that don't want to handle
    any errors or free any memory before aborting. */
 static WORD_LIST *
-call_expand_word_internal (w, q, c, e)
+call_expand_word_internal (w, q, i, c, e)
      WORD_DESC *w;
-     int q, *c, *e;
+     int q, i, *c, *e;
 {
   WORD_LIST *result;
 
-  result = expand_word_internal (w, q, c, e);
-  if (result == &expand_word_error)
+  result = expand_word_internal (w, q, i, c, e);
+  if (result == &expand_word_error || result == &expand_word_fatal)
     {
       /* By convention, each time this error is returned, w->word has
-        already been freed. */
+        already been freed (it sometimes may not be in the fatal case,
+        but that doesn't result in a memory leak because we're going
+        to exit in most cases). */
       w->word = (char *)NULL;
-      jump_to_top_level (DISCARD);
+      last_command_exit_value = EXECUTION_FAILURE;
+      exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
       /* NOTREACHED */
+      return (NULL);
     }
-  else if (result == &expand_word_fatal)
-    jump_to_top_level (FORCE_EOF);
-    /* NOTREACHED */
   else
     return (result);
 }
 
 /* Perform parameter expansion, command substitution, and arithmetic
-   expansion on STRING, as if it were a word.  Leave the result quoted. */
+   expansion on STRING, as if it were a word.  Leave the result quoted.
+   Since this does not perform word splitting, it leaves quoted nulls
+   in the result.  */
 static WORD_LIST *
 expand_string_internal (string, quoted)
      char *string;
@@ -1891,9 +3643,12 @@ expand_string_internal (string, quoted)
   if (string == 0 || *string == 0)
     return ((WORD_LIST *)NULL);
 
-  bzero ((char *)&td, sizeof (td));
-  td.word = string;
-  tresult = call_expand_word_internal (&td, quoted, (int *)NULL, (int *)NULL);
+  td.flags = 0;
+  td.word = savestring (string);
+
+  tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
+
+  FREE (td.word);
   return (tresult);
 }
 
@@ -1909,14 +3664,95 @@ expand_string_unsplit (string, quoted)
 {
   WORD_LIST *value;
 
-  if (!string || !*string)
+  if (string == 0 || *string == '\0')
     return ((WORD_LIST *)NULL);
 
+  expand_no_split_dollar_star = 1;
   value = expand_string_internal (string, quoted);
+  expand_no_split_dollar_star = 0;
+
+  if (value)
+    {
+      if (value->word)
+       {
+         remove_quoted_nulls (value->word->word);
+         value->word->flags &= ~W_HASQUOTEDNULL;
+       }
+      dequote_list (value);
+    }
+  return (value);
+}
+
+/* Expand the rhs of an assignment statement */
+WORD_LIST *
+expand_string_assignment (string, quoted)
+     char *string;
+     int quoted;
+{
+  WORD_DESC td;
+  WORD_LIST *value;
+
+  if (string == 0 || *string == '\0')
+    return ((WORD_LIST *)NULL);
+
+  expand_no_split_dollar_star = 1;
+
+  td.flags = W_ASSIGNRHS;
+  td.word = savestring (string);
+  value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
+  FREE (td.word);
+
+  expand_no_split_dollar_star = 0;
+
+  if (value)
+    {
+      if (value->word)
+       {
+         remove_quoted_nulls (value->word->word);
+         value->word->flags &= ~W_HASQUOTEDNULL;
+       }
+      dequote_list (value);
+    }
+  return (value);
+}
+
+
+/* Expand one of the PS? prompt strings. This is a sort of combination of
+   expand_string_unsplit and expand_string_internal, but returns the
+   passed string when an error occurs.  Might want to trap other calls
+   to jump_to_top_level here so we don't endlessly loop. */
+WORD_LIST *
+expand_prompt_string (string, quoted, wflags)
+     char *string;
+     int quoted;
+     int wflags;
+{
+  WORD_LIST *value;
+  WORD_DESC td;
+
+  if (string == 0 || *string == 0)
+    return ((WORD_LIST *)NULL);
+
+  td.flags = wflags;
+  td.word = savestring (string);
+
+  no_longjmp_on_fatal_error = 1;
+  value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
+  no_longjmp_on_fatal_error = 0;
+
+  if (value == &expand_word_error || value == &expand_word_fatal)
+    {
+      value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
+      return value;
+    }
+  FREE (td.word);
   if (value)
     {
       if (value->word)
-       remove_quoted_nulls (value->word->word);
+       {
+         remove_quoted_nulls (value->word->word);
+         value->word->flags &= ~W_HASQUOTEDNULL;
+       }
       dequote_list (value);
     }
   return (value);
@@ -1926,7 +3762,7 @@ expand_string_unsplit (string, quoted)
    the resultant WORD_LIST.  This is called only from within this file,
    and is used to correctly preserve quoted characters when expanding
    things like ${1+"$@"}.  This does parameter expansion, command
-   subsitution, arithmetic expansion, and word splitting. */
+   substitution, arithmetic expansion, and word splitting. */
 static WORD_LIST *
 expand_string_leave_quoted (string, quoted)
      char *string;
@@ -1952,9 +3788,9 @@ expand_string_leave_quoted (string, quoted)
 /* This does not perform word splitting or dequote the WORD_LIST
    it returns. */
 static WORD_LIST *
-expand_string_for_rhs (string, quoted, dollar_at_p, has_dollar_at)
+expand_string_for_rhs (string, quoted, dollar_at_p, expanded_p)
      char *string;
-     int quoted, *dollar_at_p, *has_dollar_at;
+     int quoted, *dollar_at_p, *expanded_p;
 {
   WORD_DESC td;
   WORD_LIST *tresult;
@@ -1962,9 +3798,12 @@ expand_string_for_rhs (string, quoted, dollar_at_p, has_dollar_at)
   if (string == 0 || *string == '\0')
     return (WORD_LIST *)NULL;
 
-  bzero ((char *)&td, sizeof (td));
+  expand_no_split_dollar_star = 1;
+  td.flags = W_NOSPLIT2;               /* no splitting, remove "" and '' */
   td.word = string;
-  tresult = call_expand_word_internal (&td, quoted, dollar_at_p, has_dollar_at);
+  tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, expanded_p);
+  expand_no_split_dollar_star = 0;
+
   return (tresult);
 }
 
@@ -1980,7 +3819,7 @@ expand_string (string, quoted)
 {
   WORD_LIST *result;
 
-  if (!string || !*string)
+  if (string == 0 || *string == '\0')
     return ((WORD_LIST *)NULL);
 
   result = expand_string_leave_quoted (string, quoted);
@@ -1989,67 +3828,54 @@ expand_string (string, quoted)
 
 /***************************************************
  *                                                *
- *     Functions to handle quoting chars          *
- *                                                *
- ***************************************************/
-
-/* Conventions:
-
-     A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
-     The parser passes CTLNUL as CTLESC CTLNUL. */
-
-/* The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
-   This is necessary to make unquoted CTLESC and CTLNUL characters in the
-   data stream pass through properly.
-   Here we remove doubled CTLESC characters inside quoted strings before
-   quoting the entire string, so we do not double the number of CTLESC
-   characters. */
-static char *
-remove_quoted_escapes (string)
-     char *string;
-{
-  register char *s;
-  int docopy;
-  char *t, *t1;
+ *     Functions to handle quoting chars          *
+ *                                                *
+ ***************************************************/
 
-  if (string == NULL)
-    return (string);
+/* Conventions:
 
-  t1 = t = xmalloc (strlen (string) + 1);
-  for (docopy = 0, s = string; *s; s++, t1++)
-    {
-      if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL))
-       {
-         s++;
-         docopy = 1;
-       }
-      *t1 = *s;
-    }
-  *t1 = '\0';
-  if (docopy)
-    strcpy (string, t);
-  free (t);
-  return (string);
-}
+     A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
+     The parser passes CTLNUL as CTLESC CTLNUL. */
 
 /* Quote escape characters in string s, but no other characters.  This is
    used to protect CTLESC and CTLNUL in variable values from the rest of
-   the word expansion process after the variable is expanded. */
-static char *
+   the word expansion process after the variable is expanded (word splitting
+   and filename generation).  If IFS is null, we quote spaces as well, just
+   in case we split on spaces later (in the case of unquoted $@, we will
+   eventually attempt to split the entire word on spaces).  Corresponding
+   code exists in dequote_escapes.  Even if we don't end up splitting on
+   spaces, quoting spaces is not a problem.  This should never be called on
+   a string that is quoted with single or double quotes or part of a here
+   document (effectively double-quoted). */
+char *
 quote_escapes (string)
      char *string;
 {
   register char *s, *t;
-  char *result;
+  size_t slen;
+  char *result, *send;
+  int quote_spaces, skip_ctlesc, skip_ctlnul;
+  DECLARE_MBSTATE; 
 
-  result = xmalloc ((strlen (string) * 2) + 1);
-  for (s = string, t = result; *s; )
+  slen = strlen (string);
+  send = string + slen;
+
+  quote_spaces = (ifs_value && *ifs_value == 0);
+
+  for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
+    skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
+
+  t = result = (char *)xmalloc ((slen * 2) + 1);
+  s = string;
+
+  while (*s)
     {
-      if (*s == CTLESC || *s == CTLNUL)
+      if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' '))
        *t++ = CTLESC;
-      *t++ = *s++;
+      COPY_CHAR_P (t, s, send);
     }
   *t = '\0';
+
   return (result);
 }
 
@@ -2069,53 +3895,82 @@ list_quote_escapes (list)
   return list;
 }
 
-#ifdef INCLUDE_UNUSED
-static char *
+/* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
+
+   The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
+   This is necessary to make unquoted CTLESC and CTLNUL characters in the
+   data stream pass through properly.
+
+   We need to remove doubled CTLESC characters inside quoted strings before
+   quoting the entire string, so we do not double the number of CTLESC
+   characters.
+
+   Also used by parts of the pattern substitution code. */
+char *
 dequote_escapes (string)
      char *string;
 {
-  register char *s, *t;
-  char *result;
+  register char *s, *t, *s1;
+  size_t slen;
+  char *result, *send;
+  int quote_spaces;
+  DECLARE_MBSTATE;
+
+  if (string == 0)
+    return string;
+
+  slen = strlen (string);
+  send = string + slen;
+
+  t = result = (char *)xmalloc (slen + 1);
+
+  if (strchr (string, CTLESC) == 0)
+    return (strcpy (result, string));
 
-  result = xmalloc (strlen (string) + 1);
-  for (s = string, t = result; *s; )
+  quote_spaces = (ifs_value && *ifs_value == 0);
+
+  s = string;
+  while (*s)
     {
-      if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL))
+      if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' ')))
        {
          s++;
          if (*s == '\0')
            break;
        }
-      *t++ = *s++;
+      COPY_CHAR_P (t, s, send);
     }
   *t = '\0';
+
   return result;
 }
-#endif
 
 static WORD_LIST *
-dequote_list (list)
+list_dequote_escapes (list)
      WORD_LIST *list;
 {
-  register char *s;
-  register WORD_LIST *tlist;
+  register WORD_LIST *w;
+  char *t;
 
-  for (tlist = list; tlist; tlist = tlist->next)
+  for (w = list; w; w = w->next)
     {
-      s = dequote_string (tlist->word->word);
-      free (tlist->word->word);
-      tlist->word->word = s;
+      t = w->word->word;
+      w->word->word = dequote_escapes (t);
+      free (t);
     }
   return list;
 }
 
+/* Return a new string with the quoted representation of character C.
+   This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
+   set in any resultant WORD_DESC where this value is the word. */
 static char *
 make_quoted_char (c)
      int c;
 {
   char *temp;
 
-  temp = xmalloc (3);
+  temp = (char *)xmalloc (3);
   if (c == 0)
     {
       temp[0] = CTLNUL;
@@ -2130,43 +3985,60 @@ make_quoted_char (c)
   return (temp);
 }
 
-/* Quote STRING.  Return a new string. */
+/* Quote STRING, returning a new string.  This turns "" into QUOTED_NULL, so
+   the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
+   this value is the word. */
 char *
 quote_string (string)
      char *string;
 {
   register char *t;
-  char *result;
+  size_t slen;
+  char *result, *send;
 
   if (*string == 0)
     {
-      result = xmalloc (2);
+      result = (char *)xmalloc (2);
       result[0] = CTLNUL;
       result[1] = '\0';
     }
   else
     {
-      result = xmalloc ((strlen (string) * 2) + 1);
+      DECLARE_MBSTATE;
+
+      slen = strlen (string);
+      send = string + slen;
 
-      for (t = result; *string; )
+      result = (char *)xmalloc ((slen * 2) + 1);
+
+      for (t = result; string < send; )
        {
          *t++ = CTLESC;
-         *t++ = *string++;
+         COPY_CHAR_P (t, string, send);
        }
       *t = '\0';
     }
   return (result);
 }
 
-/* De-quoted quoted characters in STRING. */
+/* De-quote quoted characters in STRING. */
 char *
 dequote_string (string)
      char *string;
 {
-  register char *t;
-  char *result;
+  register char *s, *t;
+  size_t slen;
+  char *result, *send;
+  DECLARE_MBSTATE;
+
+#if defined (DEBUG)
+  if (string[0] == CTLESC && string[1] == 0)
+    internal_inform ("dequote_string: string with bare CTLESC");
+#endif
+
+  slen = strlen (string);
 
-  result = xmalloc (strlen (string) + 1);
+  t = result = (char *)xmalloc (slen + 1);
 
   if (QUOTED_NULL (string))
     {
@@ -2174,25 +4046,30 @@ dequote_string (string)
       return (result);
     }
 
+  /* A string consisting of only a single CTLESC should pass through unchanged */
+  if (string[0] == CTLESC && string[1] == 0)
+    {
+      result[0] = CTLESC;
+      result[1] = '\0';
+      return (result);
+    }
+
   /* If no character in the string can be quoted, don't bother examining
      each character.  Just return a copy of the string passed to us. */
-  if (strchr (string, CTLESC) == NULL)         /* XXX */
-    {                                          /* XXX */
-      strcpy (result, string);                 /* XXX */
-      return (result);                         /* XXX */
-    }
+  if (strchr (string, CTLESC) == NULL)
+    return (strcpy (result, string));
 
-  for (t = result; *string; string++, t++)
+  send = string + slen;
+  s = string;
+  while (*s)
     {
-      if (*string == CTLESC)
+      if (*s == CTLESC)
        {
-         string++;
-
-         if (!*string)
+         s++;
+         if (*s == '\0')
            break;
        }
-
-      *t = *string;
+      COPY_CHAR_P (t, s, send);
     }
 
   *t = '\0';
@@ -2211,46 +4088,100 @@ quote_list (list)
     {
       t = w->word->word;
       w->word->word = quote_string (t);
-      free (t);
+      if (*t == 0)
+       w->word->flags |= W_HASQUOTEDNULL;      /* XXX - turn on W_HASQUOTEDNULL here? */
       w->word->flags |= W_QUOTED;
+      free (t);
+    }
+  return list;
+}
+
+/* De-quote quoted characters in each word in LIST. */
+WORD_LIST *
+dequote_list (list)
+     WORD_LIST *list;
+{
+  register char *s;
+  register WORD_LIST *tlist;
+
+  for (tlist = list; tlist; tlist = tlist->next)
+    {
+      s = dequote_string (tlist->word->word);
+      if (QUOTED_NULL (tlist->word->word))
+       tlist->word->flags &= ~W_HASQUOTEDNULL;
+      free (tlist->word->word);
+      tlist->word->word = s;
     }
   return list;
 }
 
+/* Remove CTLESC protecting a CTLESC or CTLNUL in place.  Return the passed
+   string. */
+char *
+remove_quoted_escapes (string)
+     char *string;
+{
+  char *t;
+
+  if (string)
+    {
+      t = dequote_escapes (string);
+      strcpy (string, t);
+      free (t);
+    }
+
+  return (string);
+}
+
 /* Perform quoted null character removal on STRING.  We don't allow any
    quoted null characters in the middle or at the ends of strings because
    of how expand_word_internal works.  remove_quoted_nulls () turns
    STRING into an empty string iff it only consists of a quoted null,
    and removes all unquoted CTLNUL characters. */
-/*
-#define remove_quoted_nulls(string) \
-  do { if (QUOTED_NULL (string)) string[0] ='\0'; } while (0)
-*/
-static void
+char *
 remove_quoted_nulls (string)
      char *string;
 {
-  char *nstr, *s, *p;
+  register size_t slen;
+  register int i, j, prev_i;
+  DECLARE_MBSTATE;
+
+  if (strchr (string, CTLNUL) == 0)            /* XXX */
+    return string;                             /* XXX */
 
-  nstr = savestring (string);
-  nstr[0] = '\0';
-  for (p = nstr, s = string; *s; s++)
+  slen = strlen (string);
+  i = j = 0;
+
+  while (i < slen)
     {
-      if (*s == CTLESC)
+      if (string[i] == CTLESC)
        {
-         *p++ = *s++;  /* CTLESC */
-         if (*s == 0)
+         /* Old code had j++, but we cannot assume that i == j at this
+            point -- what if a CTLNUL has already been removed from the
+            string?  We don't want to drop the CTLESC or recopy characters
+            that we've already copied down. */
+         i++; string[j++] = CTLESC;
+         if (i == slen)
            break;
-         *p++ = *s;    /* quoted char */
+       }
+      else if (string[i] == CTLNUL)
+       {
+         i++;
          continue;
        }
-      if (*s == CTLNUL)
-        continue;
-      *p++ = *s;
+
+      prev_i = i;
+      ADVANCE_CHAR (string, slen, i);
+      if (j < prev_i)
+       {
+         do string[j++] = string[prev_i++]; while (prev_i < i);
+       }
+      else
+       j = i;
     }
-  *p = '\0';
-  strcpy (string, nstr);
-  free (nstr);
+  string[j] = '\0';
+
+  return (string);
 }
 
 /* Perform quoted null character removal on each element of LIST.
@@ -2262,7 +4193,10 @@ word_list_remove_quoted_nulls (list)
   register WORD_LIST *t;
 
   for (t = list; t; t = t->next)
-    remove_quoted_nulls (t->word->word);
+    {
+      remove_quoted_nulls (t->word->word);
+      t->word->flags &= ~W_HASQUOTEDNULL;
+    }
 }
 
 /* **************************************************************** */
@@ -2271,6 +4205,33 @@ word_list_remove_quoted_nulls (list)
 /*                                                                 */
 /* **************************************************************** */
 
+#if defined (HANDLE_MULTIBYTE)
+#if 0 /* Currently unused */
+static unsigned char *
+mb_getcharlens (string, len)
+     char *string;
+     int len;
+{
+  int i, offset, last;
+  unsigned char *ret;
+  char *p;
+  DECLARE_MBSTATE;
+
+  i = offset = 0;
+  last = 0;
+  ret = (unsigned char *)xmalloc (len);
+  memset (ret, 0, len);
+  while (string[last])
+    {
+      ADVANCE_CHAR (string, len, offset);
+      ret[last] = offset - last;
+      last = offset;
+    }
+  return ret;
+}
+#endif
+#endif
+
 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
    can have one of 4 values:
        RP_LONG_LEFT    remove longest matching portion at start of PARAM
@@ -2284,20 +4245,16 @@ word_list_remove_quoted_nulls (list)
 #define RP_LONG_RIGHT  3
 #define RP_SHORT_RIGHT 4
 
+/* Returns its first argument if nothing matched; new memory otherwise */
 static char *
-remove_pattern (param, pattern, op)
+remove_upattern (param, pattern, op)
      char *param, *pattern;
      int op;
 {
-  register int len;
+  register size_t len;
   register char *end;
   register char *p, *ret, c;
 
-  if (param == NULL || *param == '\0')
-    return (param);
-  if (pattern == NULL || *pattern == '\0')     /* minor optimization */
-    return (savestring (param));
-
   len = STRLEN (param);
   end = param + len;
 
@@ -2307,12 +4264,13 @@ remove_pattern (param, pattern, op)
        for (p = end; p >= param; p--)
          {
            c = *p; *p = '\0';
-           if (fnmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
+           if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
              {
                *p = c;
                return (savestring (p));
              }
            *p = c;
+
          }
        break;
 
@@ -2320,7 +4278,7 @@ remove_pattern (param, pattern, op)
        for (p = param; p <= end; p++)
          {
            c = *p; *p = '\0';
-           if (fnmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
+           if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
              {
                *p = c;
                return (savestring (p));
@@ -2332,7 +4290,7 @@ remove_pattern (param, pattern, op)
       case RP_LONG_RIGHT:      /* remove longest match at end */
        for (p = param; p <= end; p++)
          {
-           if (fnmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
+           if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
              {
                c = *p; *p = '\0';
                ret = savestring (param);
@@ -2345,7 +4303,7 @@ remove_pattern (param, pattern, op)
       case RP_SHORT_RIGHT:     /* remove shortest match at end */
        for (p = end; p >= param; p--)
          {
-           if (fnmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
+           if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
              {
                c = *p; *p = '\0';
                ret = savestring (param);
@@ -2355,113 +4313,494 @@ remove_pattern (param, pattern, op)
          }
        break;
     }
-  return (savestring (param)); /* no match, return original string */
+
+  return (param);      /* no match, return original string */
+}
+
+#if defined (HANDLE_MULTIBYTE)
+/* Returns its first argument if nothing matched; new memory otherwise */
+static wchar_t *
+remove_wpattern (wparam, wstrlen, wpattern, op)
+     wchar_t *wparam;
+     size_t wstrlen;
+     wchar_t *wpattern;
+     int op;
+{
+  wchar_t wc, *ret;
+  int n;
+
+  switch (op)
+    {
+      case RP_LONG_LEFT:       /* remove longest match at start */
+        for (n = wstrlen; n >= 0; n--)
+         {
+           wc = wparam[n]; wparam[n] = L'\0';
+           if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
+             {
+               wparam[n] = wc;
+               return (wcsdup (wparam + n));
+             }
+           wparam[n] = wc;
+         }
+       break;
+
+      case RP_SHORT_LEFT:      /* remove shortest match at start */
+       for (n = 0; n <= wstrlen; n++)
+         {
+           wc = wparam[n]; wparam[n] = L'\0';
+           if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
+             {
+               wparam[n] = wc;
+               return (wcsdup (wparam + n));
+             }
+           wparam[n] = wc;
+         }
+       break;
+
+      case RP_LONG_RIGHT:      /* remove longest match at end */
+        for (n = 0; n <= wstrlen; n++)
+         {
+           if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
+             {
+               wc = wparam[n]; wparam[n] = L'\0';
+               ret = wcsdup (wparam);
+               wparam[n] = wc;
+               return (ret);
+             }
+         }
+       break;
+
+      case RP_SHORT_RIGHT:     /* remove shortest match at end */
+       for (n = wstrlen; n >= 0; n--)
+         {
+           if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
+             {
+               wc = wparam[n]; wparam[n] = L'\0';
+               ret = wcsdup (wparam);
+               wparam[n] = wc;
+               return (ret);
+             }
+         }
+       break;
+    }
+
+  return (wparam);     /* no match, return original string */
+}
+#endif /* HANDLE_MULTIBYTE */
+
+static char *
+remove_pattern (param, pattern, op)
+     char *param, *pattern;
+     int op;
+{
+  char *xret;
+
+  if (param == NULL)
+    return (param);
+  if (*param == '\0' || pattern == NULL || *pattern == '\0')   /* minor optimization */
+    return (savestring (param));
+
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1)
+    {
+      wchar_t *ret, *oret;
+      size_t n;
+      wchar_t *wparam, *wpattern;
+      mbstate_t ps;
+
+      n = xdupmbstowcs (&wpattern, NULL, pattern);
+      if (n == (size_t)-1)
+       {
+         xret = remove_upattern (param, pattern, op);
+         return ((xret == param) ? savestring (param) : xret);
+       }
+      n = xdupmbstowcs (&wparam, NULL, param);
+
+      if (n == (size_t)-1)
+       {
+         free (wpattern);
+         xret = remove_upattern (param, pattern, op);
+         return ((xret == param) ? savestring (param) : xret);
+       }
+      oret = ret = remove_wpattern (wparam, n, wpattern, op);
+      /* Don't bother to convert wparam back to multibyte string if nothing
+        matched; just return copy of original string */
+      if (ret == wparam)
+        {
+          free (wparam);
+          free (wpattern);
+          return (savestring (param));
+        }
+
+      free (wparam);
+      free (wpattern);
+
+      n = strlen (param);
+      xret = (char *)xmalloc (n + 1);
+      memset (&ps, '\0', sizeof (mbstate_t));
+      n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps);
+      xret[n] = '\0';          /* just to make sure */
+      free (oret);
+      return xret;      
+    }
+  else
+#endif
+    {
+      xret = remove_upattern (param, pattern, op);
+      return ((xret == param) ? savestring (param) : xret);
+    }
 }
 
-/* Return 1 of the first character of STRING could match the first
-   character of pattern PAT.  Used to avoid n2 calls to fnmatch(). */
-static int
-match_pattern_char (pat, string)
-     char *pat, *string;
-{
-  char c;
+/* Match PAT anywhere in STRING and return the match boundaries.
+   This returns 1 in case of a successful match, 0 otherwise.  SP
+   and EP are pointers into the string where the match begins and
+   ends, respectively.  MTYPE controls what kind of match is attempted.
+   MATCH_BEG and MATCH_END anchor the match at the beginning and end
+   of the string, respectively.  The longest match is returned. */
+static int
+match_upattern (string, pat, mtype, sp, ep)
+     char *string, *pat;
+     int mtype;
+     char **sp, **ep;
+{
+  int c, mlen;
+  size_t len;
+  register char *p, *p1, *npat;
+  char *end;
+  int n1;
+
+  /* If the pattern doesn't match anywhere in the string, go ahead and
+     short-circuit right away.  A minor optimization, saves a bunch of
+     unnecessary calls to strmatch (up to N calls for a string of N
+     characters) if the match is unsuccessful.  To preserve the semantics
+     of the substring matches below, we make sure that the pattern has
+     `*' as first and last character, making a new pattern if necessary. */
+  /* XXX - check this later if I ever implement `**' with special meaning,
+     since this will potentially result in `**' at the beginning or end */
+  len = STRLEN (pat);
+  if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*')
+    {
+      int unescaped_backslash;
+      char *pp;
+
+      p = npat = (char *)xmalloc (len + 3);
+      p1 = pat;
+      if (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob))
+       *p++ = '*';
+      while (*p1)
+       *p++ = *p1++;
+#if 1
+      /* Need to also handle a pattern that ends with an unescaped backslash.
+        For right now, we ignore it because the pattern matching code will
+        fail the match anyway */
+      /* If the pattern ends with a `*' we leave it alone if it's preceded by
+        an even number of backslashes, but if it's escaped by a backslash
+        we need to add another `*'. */
+      if (p1[-1] == '*' && (unescaped_backslash = p1[-2] == '\\'))
+       {
+         pp = p1 - 3;
+         while (pp >= pat && *pp-- == '\\')
+           unescaped_backslash = 1 - unescaped_backslash;
+         if (unescaped_backslash)
+           *p++ = '*';
+       }
+      else if (p1[-1] != '*')
+       *p++ = '*';
+#else 
+      if (p1[-1] != '*' || p1[-2] == '\\')
+       *p++ = '*';
+#endif
+      *p = '\0';
+    }
+  else
+    npat = pat;
+  c = strmatch (npat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE);
+  if (npat != pat)
+    free (npat);
+  if (c == FNM_NOMATCH)
+    return (0);
+
+  len = STRLEN (string);
+  end = string + len;
+
+  mlen = umatchlen (pat, len);
+
+  switch (mtype)
+    {
+    case MATCH_ANY:
+      for (p = string; p <= end; p++)
+       {
+         if (match_pattern_char (pat, p, FNMATCH_IGNCASE))
+           {
+             p1 = (mlen == -1) ? end : p + mlen;
+             /* p1 - p = length of portion of string to be considered
+                p = current position in string
+                mlen = number of characters consumed by match (-1 for entire string)
+                end = end of string
+                we want to break immediately if the potential match len
+                is greater than the number of characters remaining in the
+                string
+             */
+             if (p1 > end)
+               break;
+             for ( ; p1 >= p; p1--)
+               {
+                 c = *p1; *p1 = '\0';
+                 if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
+                   {
+                     *p1 = c;
+                     *sp = p;
+                     *ep = p1;
+                     return 1;
+                   }
+                 *p1 = c;
+#if 1
+                 /* If MLEN != -1, we have a fixed length pattern. */
+                 if (mlen != -1)
+                   break;
+#endif
+               }
+           }
+       }
+
+      return (0);
+
+    case MATCH_BEG:
+      if (match_pattern_char (pat, string, FNMATCH_IGNCASE) == 0)
+       return (0);
+
+      for (p = (mlen == -1) ? end : string + mlen; p >= string; p--)
+       {
+         c = *p; *p = '\0';
+         if (strmatch (pat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
+           {
+             *p = c;
+             *sp = string;
+             *ep = p;
+             return 1;
+           }
+         *p = c;
+         /* If MLEN != -1, we have a fixed length pattern. */
+         if (mlen != -1)
+           break;
+       }
+
+      return (0);
 
-  if (*string == 0)
-    return (0);
+    case MATCH_END:
+      for (p = end - ((mlen == -1) ? len : mlen); p <= end; p++)
+       {
+         if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
+           {
+             *sp = p;
+             *ep = end;
+             return 1;
+           }
+         /* If MLEN != -1, we have a fixed length pattern. */
+         if (mlen != -1)
+           break;
+       }
 
-  switch (c = *pat++)
-    {
-    default:
-      return (*string == c);
-    case '\\':
-      return (*string == *pat);
-    case '?':
-      return (*string == LPAREN ? 1 : (*string != '\0'));
-    case '*':
-      return (1);
-    case '+':
-    case '!':
-    case '@':
-      return (*string == LPAREN ? 1 : (*string == c));
-    case '[':
-      return (*string != '\0');
+      return (0);
     }
+
+  return (0);
 }
 
-/* Match PAT anywhere in STRING and return the match boundaries.
-   This returns 1 in case of a successful match, 0 otherwise.  SP
-   and EP are pointers into the string where the match begins and
-   ends, respectively.  MTYPE controls what kind of match is attempted.
-   MATCH_BEG and MATCH_END anchor the match at the beginning and end
-   of the string, respectively.  The longest match is returned. */
+#if defined (HANDLE_MULTIBYTE)
+
+#define WFOLD(c) (match_ignore_case && iswupper (c) ? towlower (c) : (c))
+
+/* Match WPAT anywhere in WSTRING and return the match boundaries.
+   This returns 1 in case of a successful match, 0 otherwise.  Wide
+   character version. */
 static int
-match_pattern (string, pat, mtype, sp, ep)
-     char *string, *pat;
+match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
+     wchar_t *wstring;
+     char **indices;
+     size_t wstrlen;
+     wchar_t *wpat;
      int mtype;
      char **sp, **ep;
 {
-  int c;
-  register char *p, *p1;
-  char *end;
+  wchar_t wc, *wp, *nwpat, *wp1;
+  size_t len;
+  int mlen;
+  int n, n1, n2, simple;
+
+  simple = (wpat[0] != L'\\' && wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'[');
+#if defined (EXTENDED_GLOB)
+  if (extended_glob)
+    simple &= (wpat[1] != L'(' || (wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'+' && wpat[0] != L'!' && wpat[0] != L'@')); /*)*/
+#endif
 
-  if (string == 0 || *string == 0 || pat == 0 || *pat == 0)
+  /* If the pattern doesn't match anywhere in the string, go ahead and
+     short-circuit right away.  A minor optimization, saves a bunch of
+     unnecessary calls to strmatch (up to N calls for a string of N
+     characters) if the match is unsuccessful.  To preserve the semantics
+     of the substring matches below, we make sure that the pattern has
+     `*' as first and last character, making a new pattern if necessary. */
+  len = wcslen (wpat);
+  if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*')
+    {
+      int unescaped_backslash;
+      wchar_t *wpp;
+
+      wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t));
+      wp1 = wpat;
+      if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob))
+       *wp++ = L'*';
+      while (*wp1 != L'\0')
+       *wp++ = *wp1++;
+#if 1
+      /* See comments above in match_upattern. */
+      if (wp1[-1] == L'*' && (unescaped_backslash = wp1[-2] == L'\\'))
+        {
+          wpp = wp1 - 3;
+          while (wpp >= wpat && *wpp-- == L'\\')
+            unescaped_backslash = 1 - unescaped_backslash;
+          if (unescaped_backslash)
+            *wp++ = L'*';
+        }
+      else if (wp1[-1] != L'*')
+        *wp++ = L'*';
+#else      
+      if (wp1[-1] != L'*' || wp1[-2] == L'\\')
+        *wp++ = L'*';
+#endif
+      *wp = '\0';
+    }
+  else
+    nwpat = wpat;
+  len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE);
+  if (nwpat != wpat)
+    free (nwpat);
+  if (len == FNM_NOMATCH)
     return (0);
 
-  end = string + STRLEN (string);
+  mlen = wmatchlen (wpat, wstrlen);
 
+/* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */
   switch (mtype)
     {
     case MATCH_ANY:
-      for (p = string; p <= end; p++)
+      for (n = 0; n <= wstrlen; n++)
        {
-         if (match_pattern_char (pat, p))
+         n2 = simple ? (WFOLD(*wpat) == WFOLD(wstring[n])) : match_pattern_wchar (wpat, wstring + n, FNMATCH_IGNCASE);
+         if (n2)
            {
-             for (p1 = end; p1 >= p; p1--)
+             n1 = (mlen == -1) ? wstrlen : n + mlen;
+             if (n1 > wstrlen)
+               break;
+
+             for ( ; n1 >= n; n1--)
                {
-                 c = *p1; *p1 = '\0';
-                 if (fnmatch (pat, p, FNMATCH_EXTFLAG) == 0)
+                 wc = wstring[n1]; wstring[n1] = L'\0';
+                 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
                    {
-                     *p1 = c;
-                     *sp = p;
-                     *ep = p1;
+                     wstring[n1] = wc;
+                     *sp = indices[n];
+                     *ep = indices[n1];
                      return 1;
                    }
-                 *p1 = c;
+                 wstring[n1] = wc;
+                 /* If MLEN != -1, we have a fixed length pattern. */
+                 if (mlen != -1)
+                   break;
                }
            }
        }
+
       return (0);
 
     case MATCH_BEG:
-      if (match_pattern_char (pat, string) == 0)
-        return (0);
-      for (p = end; p >= string; p--)
+      if (match_pattern_wchar (wpat, wstring, FNMATCH_IGNCASE) == 0)
+       return (0);
+
+      for (n = (mlen == -1) ? wstrlen : mlen; n >= 0; n--)
        {
-         c = *p; *p = '\0';
-         if (fnmatch (pat, string, FNMATCH_EXTFLAG) == 0)
+         wc = wstring[n]; wstring[n] = L'\0';
+         if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
            {
-             *p = c;
-             *sp = string;
-             *ep = p;
+             wstring[n] = wc;
+             *sp = indices[0];
+             *ep = indices[n];
              return 1;
            }
-         *p = c;
+         wstring[n] = wc;
+         /* If MLEN != -1, we have a fixed length pattern. */
+         if (mlen != -1)
+           break;
        }
+
       return (0);
 
     case MATCH_END:
-      for (p = string; p <= end; p++)
-       if (fnmatch (pat, p, FNMATCH_EXTFLAG) == 0)
-         {
-           *sp = p;
-           *ep = end;
-           return 1;
-         }
+      for (n = wstrlen - ((mlen == -1) ? wstrlen : mlen); n <= wstrlen; n++)
+       {
+         if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
+           {
+             *sp = indices[n];
+             *ep = indices[wstrlen];
+             return 1;
+           }
+         /* If MLEN != -1, we have a fixed length pattern. */
+         if (mlen != -1)
+           break;
+       }
+
       return (0);
     }
 
   return (0);
 }
+#undef WFOLD
+#endif /* HANDLE_MULTIBYTE */
+
+static int
+match_pattern (string, pat, mtype, sp, ep)
+     char *string, *pat;
+     int mtype;
+     char **sp, **ep;
+{
+#if defined (HANDLE_MULTIBYTE)
+  int ret;
+  size_t n;
+  wchar_t *wstring, *wpat;
+  char **indices;
+  size_t slen, plen, mslen, mplen;
+#endif
+
+  if (string == 0 || pat == 0 || *pat == 0)
+    return (0);
+
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1)
+    {
+      if (mbsmbchar (string) == 0 && mbsmbchar (pat) == 0)
+        return (match_upattern (string, pat, mtype, sp, ep));
+
+      n = xdupmbstowcs (&wpat, NULL, pat);
+      if (n == (size_t)-1)
+       return (match_upattern (string, pat, mtype, sp, ep));
+      n = xdupmbstowcs (&wstring, &indices, string);
+      if (n == (size_t)-1)
+       {
+         free (wpat);
+         return (match_upattern (string, pat, mtype, sp, ep));
+       }
+      ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep);
+
+      free (wpat);
+      free (wstring);
+      free (indices);
+
+      return (ret);
+    }
+  else
+#endif
+    return (match_upattern (string, pat, mtype, sp, ep));
+}
 
 static int
 getpatspec (c, value)
@@ -2477,7 +4816,7 @@ getpatspec (c, value)
 /* Posix.2 says that the WORD should be run through tilde expansion,
    parameter expansion, command substitution and arithmetic expansion.
    This leaves the result quoted, so quote_string_for_globbing () has
-   to be called to fix it up for fnmatch ().  If QUOTED is non-zero,
+   to be called to fix it up for strmatch ().  If QUOTED is non-zero,
    it means that the entire expression was enclosed in double quotes.
    This means that quoting characters in the pattern do not make any
    special pattern characters quoted.  For example, the `*' in the
@@ -2489,31 +4828,30 @@ getpattern (value, quoted, expandpat)
 {
   char *pat, *tword;
   WORD_LIST *l;
+#if 0
   int i;
-
-  tword = strchr (value, '~') ? bash_tilde_expand (value) : savestring (value);
-
-  /* expand_string_internal () leaves WORD quoted and does not perform
-     word splitting. */
+#endif
+  /* There is a problem here:  how to handle single or double quotes in the
+     pattern string when the whole expression is between double quotes?
+     POSIX.2 says that enclosing double quotes do not cause the pattern to
+     be quoted, but does that leave us a problem with @ and array[@] and their
+     expansions inside a pattern? */
+#if 0
   if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
     {
       i = 0;
-      pat = string_extract_double_quoted (tword, &i, 1);
+      pat = string_extract_double_quoted (tword, &i, SX_STRIPDQ);
       free (tword);
       tword = pat;
     }
+#endif
 
-  /* There is a problem here:  how to handle single or double quotes in the
-     pattern string when the whole expression is between double quotes? */
-#if 0
-  l = *tword ? expand_string_for_rhs (tword, quoted, (int *)NULL, (int *)NULL)
-#else
-  l = *tword ? expand_string_for_rhs (tword,
-                                     (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_NOQUOTE : quoted,
+  /* expand_string_for_rhs () leaves WORD quoted and does not perform
+     word splitting. */
+  l = *value ? expand_string_for_rhs (value,
+                                     (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
                                      (int *)NULL, (int *)NULL)
-#endif
             : (WORD_LIST *)0;
-  free (tword);
   pat = string_list (l);
   dispose_words (l);
   if (pat)
@@ -2525,33 +4863,27 @@ getpattern (value, quoted, expandpat)
   return (pat);
 }
 
+#if 0
 /* Handle removing a pattern from a string as a result of ${name%[%]value}
    or ${name#[#]value}. */
 static char *
-parameter_brace_remove_pattern (value, temp, c, quoted)
-     char *value, *temp;
-     int c, quoted;
+variable_remove_pattern (value, pattern, patspec, quoted)
+     char *value, *pattern;
+     int patspec, quoted;
 {
-  int patspec;
-  char *pattern, *tword;
-
-  patspec = getpatspec (c, value);
-  if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
-    value++;
-
-  pattern = getpattern (value, quoted, 1);
+  char *tword;
 
-  tword = remove_pattern (temp, pattern, patspec);
+  tword = remove_pattern (value, pattern, patspec);
 
-  FREE (pattern);
   return (tword);
 }
+#endif
 
 static char *
-list_remove_pattern (list, pattern, patspec, type, quoted)
+list_remove_pattern (list, pattern, patspec, itype, quoted)
      WORD_LIST *list;
      char *pattern;
-     int patspec, type, quoted;
+     int patspec, itype, quoted;
 {
   WORD_LIST *new, *l;
   WORD_DESC *w;
@@ -2560,93 +4892,418 @@ list_remove_pattern (list, pattern, patspec, type, quoted)
   for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
     {
       tword = remove_pattern (l->word->word, pattern, patspec);
-      w = make_bare_word (tword);
-      free (tword);
+      w = alloc_word_desc ();
+      w->word = tword ? tword : savestring ("");
       new = make_word_list (w, new);
     }
 
   l = REVERSE_LIST (new, WORD_LIST *);
-  if (type == '*')
-    tword = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? string_list_dollar_star (l) : string_list (l);
+  tword = string_list_pos_params (itype, l, quoted);
+  dispose_words (l);
+
+  return (tword);
+}
+
+static char *
+parameter_list_remove_pattern (itype, pattern, patspec, quoted)
+     int itype;
+     char *pattern;
+     int patspec, quoted;
+{
+  char *ret;
+  WORD_LIST *list;
+
+  list = list_rest_of_args ();
+  if (list == 0)
+    return ((char *)NULL);
+  ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
+  dispose_words (list);
+  return (ret);
+}
+
+#if defined (ARRAY_VARS)
+static char *
+array_remove_pattern (var, pattern, patspec, varname, quoted)
+     SHELL_VAR *var;
+     char *pattern;
+     int patspec;
+     char *varname;    /* so we can figure out how it's indexed */
+     int quoted;
+{
+  ARRAY *a;
+  HASH_TABLE *h;
+  int itype;
+  char *ret;
+  WORD_LIST *list;
+  SHELL_VAR *v;
+
+  /* compute itype from varname here */
+  v = array_variable_part (varname, &ret, 0);
+
+  /* XXX */
+  if (v && invisible_p (v))
+    return ((char *)NULL);
+
+  itype = ret[0];
+
+  a = (v && array_p (v)) ? array_cell (v) : 0;
+  h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
+  
+  list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
+  if (list == 0)
+   return ((char *)NULL);
+  ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
+  dispose_words (list);
+
+  return ret;
+}
+#endif /* ARRAY_VARS */
+
+static char *
+parameter_brace_remove_pattern (varname, value, ind, patstr, rtype, quoted, flags)
+     char *varname, *value;
+     int ind;
+     char *patstr;
+     int rtype, quoted, flags;
+{
+  int vtype, patspec, starsub;
+  char *temp1, *val, *pattern;
+  SHELL_VAR *v;
+
+  if (value == 0)
+    return ((char *)NULL);
+
+  this_command_name = varname;
+
+  vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
+  if (vtype == -1)
+    return ((char *)NULL);
+
+  starsub = vtype & VT_STARSUB;
+  vtype &= ~VT_STARSUB;
+
+  patspec = getpatspec (rtype, patstr);
+  if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
+    patstr++;
+
+  /* Need to pass getpattern newly-allocated memory in case of expansion --
+     the expansion code will free the passed string on an error. */
+  temp1 = savestring (patstr);
+  pattern = getpattern (temp1, quoted, 1);
+  free (temp1);
+
+  temp1 = (char *)NULL;                /* shut up gcc */
+  switch (vtype)
+    {
+    case VT_VARIABLE:
+    case VT_ARRAYMEMBER:
+      temp1 = remove_pattern (val, pattern, patspec);
+      if (vtype == VT_VARIABLE)
+       FREE (val);
+      if (temp1)
+       {
+         val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+                       ? quote_string (temp1)
+                       : quote_escapes (temp1);
+         free (temp1);
+         temp1 = val;
+       }
+      break;
+#if defined (ARRAY_VARS)
+    case VT_ARRAYVAR:
+      temp1 = array_remove_pattern (v, pattern, patspec, varname, quoted);
+      if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
+       {
+         val = quote_escapes (temp1);
+         free (temp1);
+         temp1 = val;
+       }
+      break;
+#endif
+    case VT_POSPARMS:
+      temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
+      if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
+       {
+         val = quote_escapes (temp1);
+         free (temp1);
+         temp1 = val;
+       }
+      break;
+    }
+
+  FREE (pattern);
+  return temp1;
+}    
+
+static char *
+string_var_assignment (v, s)
+     SHELL_VAR *v;
+     char *s;
+{
+  char flags[MAX_ATTRIBUTES], *ret, *val;
+  int i;
+
+  val = sh_quote_reusable (s, 0);
+  i = var_attribute_string (v, 0, flags);
+  ret = (char *)xmalloc (i + strlen (val) + strlen (v->name) + 16 + MAX_ATTRIBUTES);
+  if (i > 0)
+    sprintf (ret, "declare -%s %s=%s", flags, v->name, val);
+  else
+    sprintf (ret, "%s=%s", v->name, val);
+  free (val);
+  return ret;
+}
+
+#if defined (ARRAY_VARS)
+static char *
+array_var_assignment (v, itype, quoted)
+     SHELL_VAR *v;
+     int itype, quoted;
+{
+  char *ret, *val, flags[MAX_ATTRIBUTES];
+  int i;
+
+  if (v == 0)
+    return (char *)NULL;
+  val = array_p (v) ? array_to_assign (array_cell (v), 0)
+                   : assoc_to_assign (assoc_cell (v), 0);
+  if (val == 0)
+    {
+      val = (char *)xmalloc (3);
+      val[0] = '(';
+      val[1] = ')';
+      val[2] = 0;
+    }
   else
-    tword = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (l) : l);
+    {
+      ret = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) ? quote_string (val) : quote_escapes (val);
+      free (val);
+      val = ret;
+    }
+  i = var_attribute_string (v, 0, flags);
+  ret = (char *)xmalloc (i + strlen (val) + strlen (v->name) + 16);
+  sprintf (ret, "declare -%s %s=%s", flags, v->name, val);
+  free (val);
+  return ret;
+}
+#endif
+
+static char *
+pos_params_assignment (list, itype, quoted)
+     WORD_LIST *list;
+     int itype;
+     int quoted;
+{
+  char *temp, *ret;
+
+  /* first, we transform the list to quote each word. */
+  temp = list_transform ('Q', (SHELL_VAR *)0, list, itype, quoted);
+  ret = (char *)xmalloc (strlen (temp) + 8);
+  strcpy (ret, "set -- ");
+  strcpy (ret + 7, temp);
+  free (temp);
+  return ret;
+}
 
+static char *
+string_transform (xc, v, s)
+     int xc;
+     SHELL_VAR *v;
+     char *s;
+{
+  char *ret, flags[MAX_ATTRIBUTES];
+  int i;
+
+  if (((xc == 'A' || xc == 'a') && v == 0) || (xc != 'a' && s == 0))
+    return (char *)NULL;
+
+  switch (xc)
+    {
+      /* Transformations that interrogate the variable */
+      case 'a':
+       i = var_attribute_string (v, 0, flags);
+       ret = (i > 0) ? savestring (flags) : (char *)NULL;
+       break;
+      case 'A':
+       ret = string_var_assignment (v, s);
+       break;
+      /* Transformations that modify the variable's value */
+      case 'E':
+       ret = ansiexpand (s, 0, strlen (s), (int *)0);
+       break;
+      case 'P':
+       ret = decode_prompt_string (s);
+       break;
+      case 'Q':
+       ret = sh_quote_reusable (s, 0);
+       break;
+      default:
+       ret = (char *)NULL;
+       break;
+    }
+  return ret;
+}
+
+static char *
+list_transform (xc, v, list, itype, quoted)
+     int xc;
+     SHELL_VAR *v;
+     WORD_LIST *list;
+     int itype, quoted;
+{
+  WORD_LIST *new, *l;
+  WORD_DESC *w;
+  char *tword;
+
+  for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
+    {
+      tword = string_transform (xc, v, l->word->word);
+      w = alloc_word_desc ();
+      w->word = tword ? tword : savestring ("");       /* XXX */
+      new = make_word_list (w, new);
+    }
+
+  l = REVERSE_LIST (new, WORD_LIST *);
+  tword = string_list_pos_params (itype, l, quoted);
   dispose_words (l);
+
   return (tword);
 }
 
 static char *
-parameter_list_remove_pattern (value, type, c, quoted)
-     char *value;
-     int type, c, quoted;
+parameter_list_transform (xc, itype, quoted)
+     int xc;
+     int itype;
+     int quoted;
 {
-  int patspec;
-  char *pattern, *ret;
+  char *ret;
   WORD_LIST *list;
 
-  patspec = getpatspec (c, value);
-  if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
-    value++;
-
-  pattern = getpattern (value, quoted, 1);
-
   list = list_rest_of_args ();
-  ret = list_remove_pattern (list, pattern, patspec, type, quoted);
+  if (list == 0)
+    return ((char *)NULL);
+  if (xc == 'A')
+    return (pos_params_assignment (list, itype, quoted));
+  ret = list_transform (xc, (SHELL_VAR *)0, list, itype, quoted);
   dispose_words (list);
-  FREE (pattern);
   return (ret);
 }
 
 #if defined (ARRAY_VARS)
 static char *
-array_remove_pattern (value, aspec, aval, c, quoted)
-     char *value, *aspec, *aval;       /* AVAL == evaluated ASPEC */
-     int c, quoted;
+array_transform (xc, var, varname, quoted)
+     int xc;
+     SHELL_VAR *var;
+     char *varname;    /* so we can figure out how it's indexed */
+     int quoted;
 {
-  SHELL_VAR *var;
-  int len, patspec;
-  char *ret, *t, *pattern;
-  WORD_LIST *l;
+  ARRAY *a;
+  HASH_TABLE *h;
+  int itype;
+  char *ret;
+  WORD_LIST *list;
+  SHELL_VAR *v;
 
-  var = array_variable_part (aspec, &t, &len);
-  if (var == 0)
+  /* compute itype from varname here */
+  v = array_variable_part (varname, &ret, 0);
+
+  /* XXX */
+  if (v && invisible_p (v))
     return ((char *)NULL);
 
-  patspec = getpatspec (c, value);
-  if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
-    value++;
+  itype = ret[0];
 
-  pattern = getpattern (value, quoted, 1);
+  if (xc == 'A')
+    return (array_var_assignment (v, itype, quoted));
 
-  if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
+  a = (v && array_p (v)) ? array_cell (v) : 0;
+  h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
+  
+  list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
+  if (list == 0)
+   return ((char *)NULL);
+  ret = list_transform (xc, v, list, itype, quoted);
+  dispose_words (list);
+
+  return ret;
+}
+#endif /* ARRAY_VARS */
+
+static char *
+parameter_brace_transform (varname, value, ind, xform, rtype, quoted, flags)
+     char *varname, *value;
+     int ind;
+     char *xform;
+     int rtype, quoted, flags;
+{
+  int vtype, xc;
+  char *temp1, *val;
+  SHELL_VAR *v;
+
+  xc = xform[0];
+  if (value == 0 && xc != 'A' && xc != 'a')
+    return ((char *)NULL);
+
+  this_command_name = varname;
+
+  vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
+  if (vtype == -1)
+    return ((char *)NULL);
+
+  /* check for valid values of xc */
+  switch (xc)
     {
-      if (array_p (var) == 0)
-        {
-          report_error ("%s: bad array subscript", aspec);
-          FREE (pattern);
-          return ((char *)NULL);
-        }
-      l = array_to_word_list (array_cell (var));
-      if (l == 0)
-        return ((char *)NULL);
-      ret = list_remove_pattern (l, pattern, patspec, t[0], quoted);
-      dispose_words (l);
+    case 'a':          /* expand to a string with just attributes */
+    case 'A':          /* expand as an assignment statement with attributes */
+    case 'E':          /* expand like $'...' */
+    case 'P':          /* expand like prompt string */
+    case 'Q':          /* quote reusably */
+      break;
+    default:
+      return &expand_param_error;
     }
-  else
+
+  temp1 = (char *)NULL;                /* shut up gcc */
+  switch (vtype & ~VT_STARSUB)
     {
-      ret = remove_pattern (aval, pattern, patspec);
-      if (ret)
+    case VT_VARIABLE:
+    case VT_ARRAYMEMBER:
+      temp1 = string_transform (xc, v, val);
+      if (vtype == VT_VARIABLE)
+       FREE (val);
+      if (temp1)
+       {
+         val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+                       ? quote_string (temp1)
+                       : quote_escapes (temp1);
+         free (temp1);
+         temp1 = val;
+       }
+      break;
+#if defined (ARRAY_VARS)
+    case VT_ARRAYVAR:
+      temp1 = array_transform (xc, v, varname, quoted);
+      if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
+       {
+         val = quote_escapes (temp1);
+         free (temp1);
+         temp1 = val;
+       }
+      break;
+#endif
+    case VT_POSPARMS:
+      temp1 = parameter_list_transform (xc, varname[0], quoted);
+      if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
        {
-         t = quote_escapes (ret);
-         free (ret);
-         ret = t;
+         val = quote_escapes (temp1);
+         free (temp1);
+         temp1 = val;
        }
+      break;
     }
 
-  FREE (pattern);
-  return ret;
+  return temp1;
 }
-#endif /* ARRAY_VARS */
 
 /*******************************************
  *                                        *
@@ -2665,7 +5322,7 @@ expand_word (word, quoted)
 {
   WORD_LIST *result, *tresult;
 
-  tresult = call_expand_word_internal (word, quoted, (int *)NULL, (int *)NULL);
+  tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
   result = word_list_split (tresult);
   dispose_words (tresult);
   return (result ? dequote_list (result) : result);
@@ -2675,24 +5332,48 @@ expand_word (word, quoted)
    does parameter expansion, command substitution, arithmetic expansion,
    and quote removal. */
 WORD_LIST *
-expand_word_no_split (word, quoted)
+expand_word_unsplit (word, quoted)
      WORD_DESC *word;
      int quoted;
 {
   WORD_LIST *result;
 
-  result = call_expand_word_internal (word, quoted, (int *)NULL, (int *)NULL);
+  expand_no_split_dollar_star = 1;
+#if defined (HANDLE_MULTIBYTE)
+  if (ifs_firstc[0] == 0)
+#else
+  if (ifs_firstc == 0)
+#endif
+    word->flags |= W_NOSPLIT;
+  word->flags |= W_NOSPLIT2;
+  result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
+  expand_no_split_dollar_star = 0;
+
   return (result ? dequote_list (result) : result);
 }
 
 /* Perform shell expansions on WORD, but do not perform word splitting or
-   quote removal on the result. */
+   quote removal on the result.  Virtually identical to expand_word_unsplit;
+   could be combined if implementations don't diverge. */
 WORD_LIST *
 expand_word_leave_quoted (word, quoted)
      WORD_DESC *word;
      int quoted;
 {
-  return (call_expand_word_internal (word, quoted, (int *)NULL, (int *)NULL));
+  WORD_LIST *result;
+
+  expand_no_split_dollar_star = 1;
+#if defined (HANDLE_MULTIBYTE)
+  if (ifs_firstc[0] == 0)
+#else
+  if (ifs_firstc == 0)
+#endif
+    word->flags |= W_NOSPLIT;
+  word->flags |= W_NOSPLIT2;
+  result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
+  expand_no_split_dollar_star = 0;
+
+  return result;
 }
 
 #if defined (PROCESS_SUBSTITUTION)
@@ -2709,12 +5390,30 @@ expand_word_leave_quoted (word, quoted)
    unlink all of them. add_fifo_list adds the name of an open FIFO to the
    list.  NFIFO is a count of the number of FIFOs in the list. */
 #define FIFO_INCR 20
-extern char *mktemp ();
 
-static char **fifo_list = (char **)NULL;
+struct temp_fifo {
+  char *file;
+  pid_t proc;
+};
+
+static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
 static int nfifo;
 static int fifo_list_size;
 
+void
+clear_fifo_list ()
+{
+}
+
+char *
+copy_fifo_list (sizep)
+     int *sizep;
+{
+  if (sizep)
+    *sizep = 0;
+  return (char *)NULL;
+}
+
 static void
 add_fifo_list (pathname)
      char *pathname;
@@ -2722,26 +5421,100 @@ add_fifo_list (pathname)
   if (nfifo >= fifo_list_size - 1)
     {
       fifo_list_size += FIFO_INCR;
-      fifo_list = (char **)xrealloc (fifo_list,
-                                    fifo_list_size * sizeof (char *));
+      fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
+                               fifo_list_size * sizeof (struct temp_fifo));
     }
 
-  fifo_list[nfifo++] = savestring (pathname);
+  fifo_list[nfifo].file = savestring (pathname);
+  nfifo++;
+}
+
+void
+unlink_fifo (i)
+     int i;
+{
+  if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
+    {
+      unlink (fifo_list[i].file);
+      free (fifo_list[i].file);
+      fifo_list[i].file = (char *)NULL;
+      fifo_list[i].proc = -1;
+    }
 }
 
 void
 unlink_fifo_list ()
 {
+  int saved, i, j;
+
   if (nfifo == 0)
     return;
 
-  while (nfifo--)
+  for (i = saved = 0; i < nfifo; i++)
+    {
+      if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
+       {
+         unlink (fifo_list[i].file);
+         free (fifo_list[i].file);
+         fifo_list[i].file = (char *)NULL;
+         fifo_list[i].proc = -1;
+       }
+      else
+       saved++;
+    }
+
+  /* If we didn't remove some of the FIFOs, compact the list. */
+  if (saved)
+    {
+      for (i = j = 0; i < nfifo; i++)
+       if (fifo_list[i].file)
+         {
+           fifo_list[j].file = fifo_list[i].file;
+           fifo_list[j].proc = fifo_list[i].proc;
+           j++;
+         }
+      nfifo = j;
+    }
+  else
+    nfifo = 0;
+}
+
+/* Take LIST, which is a bitmap denoting active FIFOs in fifo_list
+   from some point in the past, and close all open FIFOs in fifo_list
+   that are not marked as active in LIST.  If LIST is NULL, close
+   everything in fifo_list. LSIZE is the number of elements in LIST, in
+   case it's larger than fifo_list_size (size of fifo_list). */
+void
+close_new_fifos (list, lsize)
+     char *list;
+     int lsize;
+{
+  int i;
+
+  if (list == 0)
     {
-      unlink (fifo_list[nfifo]);
-      free (fifo_list[nfifo]);
-      fifo_list[nfifo] = (char *)NULL;
+      unlink_fifo_list ();
+      return;
     }
-  nfifo = 0;
+
+  for (i = 0; i < lsize; i++)
+    if (list[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1)
+      unlink_fifo (i);
+
+  for (i = lsize; i < fifo_list_size; i++)
+    unlink_fifo (i);  
+}
+
+int
+fifos_pending ()
+{
+  return nfifo;
+}
+
+int
+num_fifos ()
+{
+  return nfifo;
 }
 
 static char *
@@ -2749,7 +5522,7 @@ make_named_pipe ()
 {
   char *tname;
 
-  tname = mktemp (savestring ("/tmp/sh-np-XXXXXX"));
+  tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR);
   if (mkfifo (tname, 0600) < 0)
     {
       free (tname);
@@ -2770,11 +5543,55 @@ static char *dev_fd_list = (char *)NULL;
 static int nfds;
 static int totfds;     /* The highest possible number of open files. */
 
+void
+clear_fifo (i)
+     int i;
+{
+  if (dev_fd_list[i])
+    {
+      dev_fd_list[i] = 0;
+      nfds--;
+    }
+}
+
+void
+clear_fifo_list ()
+{
+  register int i;
+
+  if (nfds == 0)
+    return;
+
+  for (i = 0; nfds && i < totfds; i++)
+    clear_fifo (i);
+
+  nfds = 0;
+}
+
+char *
+copy_fifo_list (sizep)
+     int *sizep;
+{
+  char *ret;
+
+  if (nfds == 0 || totfds == 0)
+    {
+      if (sizep)
+       *sizep = 0;
+      return (char *)NULL;
+    }
+
+  if (sizep)
+    *sizep = totfds;
+  ret = (char *)xmalloc (totfds);
+  return (memcpy (ret, dev_fd_list, totfds));
+}
+
 static void
 add_fifo_list (fd)
      int fd;
 {
-  if (!dev_fd_list || fd >= totfds)
+  if (dev_fd_list == 0 || fd >= totfds)
     {
       int ofds;
 
@@ -2782,17 +5599,41 @@ add_fifo_list (fd)
       totfds = getdtablesize ();
       if (totfds < 0 || totfds > 256)
        totfds = 256;
-      if (fd > totfds)
+      if (fd >= totfds)
        totfds = fd + 2;
 
-      dev_fd_list = xrealloc (dev_fd_list, totfds);
-      bzero (dev_fd_list + ofds, totfds - ofds);
+      dev_fd_list = (char *)xrealloc (dev_fd_list, totfds);
+      memset (dev_fd_list + ofds, '\0', totfds - ofds);
     }
 
   dev_fd_list[fd] = 1;
   nfds++;
 }
 
+int
+fifos_pending ()
+{
+  return 0;    /* used for cleanup; not needed with /dev/fd */
+}
+
+int
+num_fifos ()
+{
+  return nfds;
+}
+
+void
+unlink_fifo (fd)
+     int fd;
+{
+  if (dev_fd_list[fd])
+    {
+      close (fd);
+      dev_fd_list[fd] = 0;
+      nfds--;
+    }
+}
+
 void
 unlink_fifo_list ()
 {
@@ -2802,22 +5643,43 @@ unlink_fifo_list ()
     return;
 
   for (i = 0; nfds && i < totfds; i++)
-    if (dev_fd_list[i])
-      {
-       close (i);
-       dev_fd_list[i] = 0;
-       nfds--;
-      }
+    unlink_fifo (i);
 
   nfds = 0;
 }
 
+/* Take LIST, which is a snapshot copy of dev_fd_list from some point in
+   the past, and close all open fds in dev_fd_list that are not marked
+   as open in LIST.  If LIST is NULL, close everything in dev_fd_list.
+   LSIZE is the number of elements in LIST, in case it's larger than
+   totfds (size of dev_fd_list). */
+void
+close_new_fifos (list, lsize)
+     char *list;
+     int lsize;
+{
+  int i;
+
+  if (list == 0)
+    {
+      unlink_fifo_list ();
+      return;
+    }
+
+  for (i = 0; i < lsize; i++)
+    if (list[i] == 0 && i < totfds && dev_fd_list[i])
+      unlink_fifo (i);
+
+  for (i = lsize; i < totfds; i++)
+    unlink_fifo (i);  
+}
+
 #if defined (NOTDEF)
 print_dev_fd_list ()
 {
   register int i;
 
-  fprintf (stderr, "pid %d: dev_fd_list:", getpid ());
+  fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
   fflush (stderr);
 
   for (i = 0; i < totfds; i++)
@@ -2833,10 +5695,14 @@ static char *
 make_dev_fd_filename (fd)
      int fd;
 {
-  char *ret;
+  char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
+
+  ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8);
+
+  strcpy (ret, DEV_FD_PREFIX);
+  p = inttostr (fd, intbuf, sizeof (intbuf));
+  strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
 
-  ret = xmalloc (sizeof (DEV_FD_PREFIX) + 4);
-  sprintf (ret, "%s%d", DEV_FD_PREFIX, fd);
   add_fifo_list (fd);
   return (ret);
 }
@@ -2879,7 +5745,7 @@ process_substitute (string, open_for_read_in_child)
 #else /* HAVE_DEV_FD */
   if (pipe (fildes) < 0)
     {
-      sys_error ("cannot make pipe for process substitution");
+      sys_error ("%s", _("cannot make pipe for process substitution"));
       return ((char *)NULL);
     }
   /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
@@ -2893,9 +5759,9 @@ process_substitute (string, open_for_read_in_child)
   pathname = make_dev_fd_filename (parent_pipe_fd);
 #endif /* HAVE_DEV_FD */
 
-  if (!pathname)
+  if (pathname == 0)
     {
-      sys_error ("cannot make pipe for process substitution");
+      sys_error ("%s", _("cannot make pipe for process substitution"));
       return ((char *)NULL);
     }
 
@@ -2903,33 +5769,41 @@ process_substitute (string, open_for_read_in_child)
 
 #if defined (JOB_CONTROL)
   old_pipeline_pgrp = pipeline_pgrp;
-  pipeline_pgrp = shell_pgrp;
-#if 0
-  cleanup_the_pipeline ();
-#else
+  if (pipeline_pgrp == 0 || (subshell_environment & (SUBSHELL_PIPE|SUBSHELL_FORK|SUBSHELL_ASYNC)) == 0)
+    pipeline_pgrp = shell_pgrp;
   save_pipeline (1);
-#endif
 #endif /* JOB_CONTROL */
 
   pid = make_child ((char *)NULL, 1);
   if (pid == 0)
     {
       reset_terminating_signals ();    /* XXX */
+      free_pushed_string_input ();
       /* Cancel traps, in trap.c. */
-      restore_original_signals ();
+      restore_original_signals ();     /* XXX - what about special builtins? bash-4.2 */
+      QUIT;    /* catch any interrupts we got post-fork */
       setup_async_signals ();
-      subshell_environment = SUBSHELL_COMSUB;
+      subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB;
+
+      /* if we're expanding a redirection, we shouldn't have access to the
+        temporary environment, but commands in the subshell should have
+        access to their own temporary environment. */
+      if (expanding_redir)
+        flush_temporary_env ();
     }
 
 #if defined (JOB_CONTROL)
   set_sigchld_handler ();
   stop_making_children ();
+  /* XXX - should we only do this in the parent? (as in command subst) */
   pipeline_pgrp = old_pipeline_pgrp;
+#else
+  stop_making_children ();
 #endif /* JOB_CONTROL */
 
   if (pid < 0)
     {
-      sys_error ("cannot make child for process substitution");
+      sys_error ("%s", _("cannot make child for process substitution"));
       free (pathname);
 #if defined (HAVE_DEV_FD)
       close (parent_pipe_fd);
@@ -2941,7 +5815,13 @@ process_substitute (string, open_for_read_in_child)
   if (pid > 0)
     {
 #if defined (JOB_CONTROL)
-      restore_pipeline (1);
+      if (last_procsub_child)
+       discard_last_procsub_child ();
+      last_procsub_child = restore_pipeline (0);
+#endif
+
+#if !defined (HAVE_DEV_FD)
+      fifo_list[nfifo-1].proc = pid;
 #endif
 
       last_made_pid = old_pid;
@@ -2965,25 +5845,43 @@ process_substitute (string, open_for_read_in_child)
 
 #if !defined (HAVE_DEV_FD)
   /* Open the named pipe in the child. */
-  fd = open (pathname, open_for_read_in_child ? O_RDONLY|O_NONBLOCK : O_WRONLY);
+  fd = open (pathname, open_for_read_in_child ? O_RDONLY : O_WRONLY);
   if (fd < 0)
     {
-      sys_error ("cannot open named pipe %s for %s", pathname,
-       open_for_read_in_child ? "reading" : "writing");
+      /* Two separate strings for ease of translation. */
+      if (open_for_read_in_child)
+       sys_error (_("cannot open named pipe %s for reading"), pathname);
+      else
+       sys_error (_("cannot open named pipe %s for writing"), pathname);
+
       exit (127);
     }
+  if (open_for_read_in_child)
+    {
+      if (sh_unset_nodelay_mode (fd) < 0)
+       {
+         sys_error (_("cannot reset nodelay mode for fd %d"), fd);
+         exit (127);
+       }
+    }
 #else /* HAVE_DEV_FD */
   fd = child_pipe_fd;
 #endif /* HAVE_DEV_FD */
 
+  /* Discard  buffered stdio output before replacing the underlying file
+     descriptor. */
+  if (open_for_read_in_child == 0)
+    fpurge (stdout);
+
   if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
     {
-      sys_error ("cannot duplicate named pipe %s as fd %d", pathname,
+      sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname,
        open_for_read_in_child ? 0 : 1);
       exit (127);
     }
 
-  close (fd);
+  if (fd != (open_for_read_in_child ? 0 : 1))
+    close (fd);
 
   /* Need to close any files that this process has open to pipes inherited
      from its parent. */
@@ -3001,13 +5899,26 @@ process_substitute (string, open_for_read_in_child)
   dev_fd_list[parent_pipe_fd] = 0;
 #endif /* HAVE_DEV_FD */
 
+  /* subshells shouldn't have this flag, which controls using the temporary
+     environment for variable lookups.  We have already flushed the temporary
+     environment above in the case we're expanding a redirection, so processes
+     executed by this command need to be able to set it independently of their
+     parent. */
+  expanding_redir = 0;
+
+  remove_quoted_escapes (string);
+
+  subshell_level++;
   result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
+  subshell_level--;
 
 #if !defined (HAVE_DEV_FD)
   /* Make sure we close the named pipe in the child before we exit. */
   close (open_for_read_in_child ? 0 : 1);
 #endif /* !HAVE_DEV_FD */
 
+  last_command_exit_value = result;
+  result = run_exit_trap ();
   exit (result);
   /*NOTREACHED*/
 }
@@ -3020,37 +5931,78 @@ process_substitute (string, open_for_read_in_child)
 /***********************************/
 
 static char *
-read_comsub (fd, quoted)
+read_comsub (fd, quoted, rflag)
      int fd, quoted;
+     int *rflag;
 {
-  char *istring, buf[128], *bufp;
-  int bufn, istring_index, istring_size, c;
+  char *istring, buf[128], *bufp, *s;
+  int istring_index, istring_size, c, tflag, skip_ctlesc, skip_ctlnul;
+  ssize_t bufn;
+  int nullbyte;
 
   istring = (char *)NULL;
-  istring_index = istring_size = bufn = 0;
+  istring_index = istring_size = bufn = tflag = 0;
+
+  for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
+    skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
 
-  /* Read the output of the command through the pipe. */
+  nullbyte = 0;
+
+  /* Read the output of the command through the pipe.  This may need to be
+     changed to understand multibyte characters in the future. */
   while (1)
     {
       if (fd < 0)
-        break;
+       break;
       if (--bufn <= 0)
        {
-         while ((bufn = read (fd, buf, sizeof(buf))) < 0 && errno == EINTR)
-           ;
+         bufn = zread (fd, buf, sizeof (buf));
          if (bufn <= 0) 
            break;
          bufp = buf;
        }
       c = *bufp++;
 
+      if (c == 0)
+       {
+#if 1
+         if (nullbyte == 0)
+           {
+             internal_warning ("%s", _("command substitution: ignored null byte in input"));
+             nullbyte = 1;
+           }
+#endif
+         continue;
+       }
+
       /* Add the character to ISTRING, possibly after resizing it. */
       RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
 
-      if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || c == CTLESC || c == CTLNUL)
+      /* This is essentially quote_string inline */
+      if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */)
+       istring[istring_index++] = CTLESC;
+      /* Escape CTLESC and CTLNUL in the output to protect those characters
+        from the rest of the word expansions (word splitting and globbing.)
+        This is essentially quote_escapes inline. */
+      else if (skip_ctlesc == 0 && c == CTLESC)
+       {
+         tflag |= W_HASCTLESC;
+         istring[istring_index++] = CTLESC;
+       }
+      else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0)))
        istring[istring_index++] = CTLESC;
 
       istring[istring_index++] = c;
+
+#if 0
+#if defined (__CYGWIN__)
+      if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r')
+       {
+         istring_index--;
+         istring[istring_index - 1] = '\n';
+       }
+#endif
+#endif
     }
 
   if (istring)
@@ -3061,6 +6013,8 @@ read_comsub (fd, quoted)
   if (istring_index == 0)
     {
       FREE (istring);
+      if (rflag)
+       *rflag = tflag;
       return (char *)NULL;
     }
 
@@ -3085,79 +6039,132 @@ read_comsub (fd, quoted)
   else
     strip_trailing (istring, istring_index - 1, 1);
 
+  if (rflag)
+    *rflag = tflag;
   return istring;
 }
 
-/* Perform command substitution on STRING.  This returns a string,
-   possibly quoted. */
-static char *
+/* Perform command substitution on STRING.  This returns a WORD_DESC * with the
+   contained string possibly quoted. */
+WORD_DESC *
 command_substitute (string, quoted)
      char *string;
      int quoted;
 {
-  pid_t pid, old_pid, old_pipeline_pgrp;
-  char *istring;
-  int result, fildes[2];
+  pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
+  char *istring, *s;
+  int result, fildes[2], function_value, pflags, rc, tflag;
+  WORD_DESC *ret;
 
   istring = (char *)NULL;
 
   /* Don't fork () if there is no need to.  In the case of no command to
      run, just return NULL. */
+#if 1
+  for (s = string; s && *s && (shellblank (*s) || *s == '\n'); s++)
+    ;
+  if (s == 0 || *s == 0)
+    return ((WORD_DESC *)NULL);
+#else
   if (!string || !*string || (string[0] == '\n' && !string[1]))
-    return ((char *)NULL);
+    return ((WORD_DESC *)NULL);
+#endif
 
   if (wordexp_only && read_but_dont_execute)
     {
-      last_command_exit_value = 125;
+      last_command_exit_value = EX_WEXPCOMSUB;
       jump_to_top_level (EXITPROG);
     }
 
+  /* We're making the assumption here that the command substitution will
+     eventually run a command from the file system.  Since we'll run
+     maybe_make_export_env in this subshell before executing that command,
+     the parent shell and any other shells it starts will have to remake
+     the environment.  If we make it before we fork, other shells won't
+     have to.  Don't bother if we have any temporary variable assignments,
+     though, because the export environment will be remade after this
+     command completes anyway, but do it if all the words to be expanded
+     are variable assignments. */
+  if (subst_assign_varlist == 0 || garglist == 0)
+    maybe_make_export_env ();  /* XXX */
+
+  /* Flags to pass to parse_and_execute() */
+  pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0;
+
   /* Pipe the output of executing STRING into the current shell. */
   if (pipe (fildes) < 0)
     {
-      sys_error ("cannot make pipes for command substitution");
+      sys_error ("%s", _("cannot make pipe for command substitution"));
       goto error_exit;
     }
 
   old_pid = last_made_pid;
 #if defined (JOB_CONTROL)
   old_pipeline_pgrp = pipeline_pgrp;
-  pipeline_pgrp = shell_pgrp;
+  /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
+  if ((subshell_environment & SUBSHELL_PIPE) == 0)
+    pipeline_pgrp = shell_pgrp;
   cleanup_the_pipeline ();
-#endif
+#endif /* JOB_CONTROL */
+
+  old_async_pid = last_asynchronous_pid;
+  pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
+  last_asynchronous_pid = old_async_pid;
 
-  pid = make_child ((char *)NULL, 0);
   if (pid == 0)
-    /* Reset the signal handlers in the child, but don't free the
-       trap strings. */
-    reset_signal_handlers ();
+    {
+      /* Reset the signal handlers in the child, but don't free the
+        trap strings.  Set a flag noting that we have to free the
+        trap strings if we run trap to change a signal disposition. */
+      reset_signal_handlers ();
+      if (ISINTERRUPT)
+       {
+         kill (getpid (), SIGINT);
+         CLRINTERRUPT;         /* if we're ignoring SIGINT somehow */
+       }       
+      QUIT;    /* catch any interrupts we got post-fork */
+      subshell_environment |= SUBSHELL_RESETTRAP;
+    }
 
 #if defined (JOB_CONTROL)
+  /* XXX DO THIS ONLY IN PARENT ? XXX */
   set_sigchld_handler ();
   stop_making_children ();
-  pipeline_pgrp = old_pipeline_pgrp;
+  if (pid != 0)
+    pipeline_pgrp = old_pipeline_pgrp;
+#else
+  stop_making_children ();
 #endif /* JOB_CONTROL */
 
   if (pid < 0)
     {
-      sys_error ("cannot make child for command substitution");
+      sys_error (_("cannot make child for command substitution"));
     error_exit:
 
+      last_made_pid = old_pid;
+
       FREE (istring);
       close (fildes[0]);
       close (fildes[1]);
-      return ((char *)NULL);
+      return ((WORD_DESC *)NULL);
     }
 
   if (pid == 0)
     {
+      /* The currently executing shell is not interactive. */
+      interactive = 0;
+
       set_sigint_handler ();   /* XXX */
-#if defined (JOB_CONTROL)
-      set_job_control (0);
-#endif
+
+      free_pushed_string_input ();
+
+      /* Discard  buffered stdio output before replacing the underlying file
+        descriptor. */
+      fpurge (stdout);
+
       if (dup2 (fildes[1], 1) < 0)
        {
-         sys_error ("command_substitute: cannot duplicate pipe as fd 1");
+         sys_error ("%s", _("command_substitute: cannot duplicate pipe as fd 1"));
          exit (EXECUTION_FAILURE);
        }
 
@@ -3177,28 +6184,76 @@ command_substitute (string, quoted)
          (fildes[0] != fileno (stderr)))
        close (fildes[0]);
 
-      /* The currently executing shell is not interactive. */
-      interactive = 0;
+#ifdef __CYGWIN__
+      /* Let stdio know the fd may have changed from text to binary mode, and
+        make sure to preserve stdout line buffering. */
+      freopen (NULL, "w", stdout);
+      sh_setlinebuf (stdout);
+#endif /* __CYGWIN__ */
 
       /* This is a subshell environment. */
-      subshell_environment = SUBSHELL_COMSUB;
+      subshell_environment |= SUBSHELL_COMSUB;
+
+      /* Many shells do not appear to inherit the -v option for command
+        substitutions. */
+      change_flag ('v', FLAG_OFF);
 
-      /* Command substitution does not inherit the -e flag. */
-      exit_immediately_on_error = 0;
+      /* When inherit_errexit option is not enabled, command substitution does
+        not inherit the -e flag.  It is enabled when Posix mode is enabled */
+      if (inherit_errexit == 0)
+        {
+          builtin_ignoring_errexit = 0;
+         change_flag ('e', FLAG_OFF);
+        }
+      set_shellopts ();
+
+      /* If we are expanding a redirection, we can dispose of any temporary
+        environment we received, since redirections are not supposed to have
+        access to the temporary environment.  We will have to see whether this
+        affects temporary environments supplied to `eval', but the temporary
+        environment gets copied to builtin_env at some point. */
+      if (expanding_redir)
+       {
+         flush_temporary_env ();
+         expanding_redir = 0;
+       }
 
       remove_quoted_escapes (string);
 
       startup_state = 2;       /* see if we can avoid a fork */
       /* Give command substitution a place to jump back to on failure,
         so we don't go back up to main (). */
-      result = setjmp (top_level);
+      result = setjmp_nosigs (top_level);
+
+      /* If we're running a command substitution inside a shell function,
+        trap `return' so we don't return from the function in the subshell
+        and go off to never-never land. */
+      if (result == 0 && return_catch_flag)
+       function_value = setjmp_nosigs (return_catch);
+      else
+       function_value = 0;
 
-      if (result == EXITPROG)
-       exit (last_command_exit_value);
+      if (result == ERREXIT)
+       rc = last_command_exit_value;
+      else if (result == EXITPROG)
+       rc = last_command_exit_value;
       else if (result)
-       exit (EXECUTION_FAILURE);
+       rc = EXECUTION_FAILURE;
+      else if (function_value)
+       rc = return_catch_value;
       else
-       exit (parse_and_execute (string, "command substitution", SEVAL_NOHIST));
+       {
+         subshell_level++;
+         rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST);
+         subshell_level--;
+       }
+
+      last_command_exit_value = rc;
+      rc = run_exit_trap ();
+#if defined (PROCESS_SUBSTITUTION)
+      unlink_fifo_list ();
+#endif
+      exit (rc);
     }
   else
     {
@@ -3208,10 +6263,12 @@ command_substitute (string, quoted)
 
       close (fildes[1]);
 
-      istring = read_comsub (fildes[0], quoted);
+      tflag = 0;
+      istring = read_comsub (fildes[0], quoted, &tflag);
 
       close (fildes[0]);
 
+      current_command_subst_pid = pid;
       last_command_exit_value = wait_for (pid);
       last_command_subst_pid = pid;
       last_made_pid = old_pid;
@@ -3220,24 +6277,29 @@ command_substitute (string, quoted)
       /* If last_command_exit_value > 128, then the substituted command
         was terminated by a signal.  If that signal was SIGINT, then send
         SIGINT to ourselves.  This will break out of loops, for instance. */
-      if (last_command_exit_value == (128 + SIGINT))
+      if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT)
        kill (getpid (), SIGINT);
 
       /* wait_for gives the terminal back to shell_pgrp.  If some other
         process group should have it, give it away to that group here.
         pipeline_pgrp is non-zero only while we are constructing a
-        pipline, so what we are concerned about is whether or not that
+        pipeline, so what we are concerned about is whether or not that
         pipeline was started in the background.  A pipeline started in
-        the background should never get the tty back here. */
-#if 0
-      if (interactive && pipeline_pgrp != (pid_t)0 && pipeline_pgrp != last_asynchronous_pid)
-#else
-      if (interactive && pipeline_pgrp != (pid_t)0 && subshell_environment != SUBSHELL_ASYNC)
-#endif
-       give_terminal_to (pipeline_pgrp);
+        the background should never get the tty back here.  We duplicate
+        the conditions that wait_for tests to make sure we only give
+        the terminal back to pipeline_pgrp under the conditions that wait_for
+        gave it to shell_pgrp.  If wait_for doesn't mess with the terminal
+        pgrp, we should not either. */
+      if (interactive && pipeline_pgrp != (pid_t)0 && running_in_background == 0 &&
+          (subshell_environment & (SUBSHELL_ASYNC|SUBSHELL_PIPE)) == 0)
+       give_terminal_to (pipeline_pgrp, 0);
 #endif /* JOB_CONTROL */
 
-      return (istring);
+      ret = alloc_word_desc ();
+      ret->word = istring;
+      ret->flags = tflag;
+
+      return ret;
     }
 }
 
@@ -3247,212 +6309,85 @@ command_substitute (string, quoted)
  *                                                     *
  ********************************************************/
 
-/* Utility functions to manage arrays and their contents for expansion */
-
 #if defined (ARRAY_VARS)
-int
-valid_array_reference (name)
-     char *name;
-{
-  char *t;
-  int r, len;
-
-  t = strchr (name, '[');
-  if (t)
-    {
-      *t = '\0';
-      r = legal_identifier (name);
-      *t = '[';
-      if (r == 0)
-       return 0;
-      /* Check for a properly-terminated non-blank subscript. */
-      len = skipsubscript (t, 0);
-      if (t[len] != ']' || len == 1)
-       return 0;
-      for (r = 1; r < len; r++)
-       if (whitespace (t[r]) == 0)
-         return 1;
-      return 0;
-    }
-  return 0;
-}
-
-/* Expand the array index beginning at S and extending LEN characters. */
-int
-array_expand_index (s, len)
-     char *s;
-     int len;
-{
-  char *exp, *t;
-  int val, expok;
-
-  exp = xmalloc (len);
-  strncpy (exp, s, len - 1);
-  exp[len - 1] = '\0';
-  t = maybe_expand_string (exp, 0, expand_string);
-  this_command_name = (char *)NULL;
-  val = evalexp (t, &expok);
-  free (t);
-  free (exp);
-  if (expok == 0)
-    jump_to_top_level (DISCARD);
-  return val;
-}
-
-/* Return the variable specified by S without any subscript.  If non-null,
-   return the index of the start of the subscript in *SUBP.  If non-null,
-   the length of the subscript is returned in *LENP. */
-SHELL_VAR *
-array_variable_part (s, subp, lenp)
-     char *s, **subp;
-     int *lenp;
-{
-  char *t;
-  int ind, ni;
-  SHELL_VAR *var;
-
-  t = strchr (s, '[');
-  ind = t - s;
-  ni = skipsubscript (s, ind);
-  if (ni <= ind + 1 || s[ni] != ']')
-    {
-      report_error ("%s: bad array subscript", s);
-      return ((SHELL_VAR *)NULL);
-    }
-
-  *t = '\0';
-  var = find_variable (s);
-  *t++ = '[';
-
-  if (subp)
-    *subp = t;
-  if (lenp)
-    *lenp = ni - ind;
-  return var;
-}
-
-static char *
-array_value_internal (s, quoted, allow_all)
-     char *s;
-     int quoted, allow_all;
-{
-  int len, ind;
-  char *retval, *t, *temp;
-  WORD_LIST *l, *list;
-  SHELL_VAR *var;
-
-  var = array_variable_part (s, &t, &len);
-
-  if (var == 0)
-    return (char *)NULL;
-
-  if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
-    {
-      if (array_p (var) == 0 || allow_all == 0)
-        {
-          report_error ("%s: bad array subscript", s);
-          return ((char *)NULL);
-        }
-      l = array_to_word_list (array_cell (var));
-      if (l == (WORD_LIST *)NULL)
-       return ((char *) NULL);
-
-#if 0
-      if (t[0] == '*')         /* ${name[*]} */
-       retval = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? string_list_dollar_star (l) : string_list (l);
-      else                     /* ${name[@]} */
-       retval = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (l) : l);
-#else
-      if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
-       {
-         temp = string_list_dollar_star (l);
-         retval = quote_string (temp);
-         free (temp);
-       }
-      else     /* ${name[@]} or unquoted ${name[*]} */
-       retval = string_list_dollar_at (l, quoted);
-#endif
-
-      dispose_words (l);
-    }
-  else
-    {
-      ind = array_expand_index (t, len);
-      if (ind < 0)
-       {
-         report_error ("%s: bad array subscript", var->name);
-         return ((char *)NULL);
-       }
-      if (array_p (var) == 0)
-        return (ind == 0 ? savestring (value_cell (var)) : (char *)NULL);
-      retval = array_reference (array_cell (var), ind);
-      if (retval)
-       retval = quote_escapes (retval);
-    }
-
-  return retval;
-}
-
-static char *
-array_value (s, quoted)
-     char *s;
-     int quoted;
-{
-  return (array_value_internal (s, quoted, 1));
-}
-
-/* Return the value of the array indexing expression S as a single string.
-   If ALLOW_ALL is 0, do not allow `@' and `*' subscripts.  This is used
-   by other parts of the shell such as the arithmetic expression evaluator
-   in expr.c. */
-char *
-get_array_value (s, allow_all)
-     char *s;
-     int allow_all;
-{
-  return (array_value_internal (s, 0, allow_all));
-}
 
-static int
+static arrayind_t
 array_length_reference (s)
      char *s;
 {
-  int ind, len;
-  char *t;
+  int len;
+  arrayind_t ind;
+  char *akey;
+  char *t, c;
   ARRAY *array;
+  HASH_TABLE *h;
   SHELL_VAR *var;
 
   var = array_variable_part (s, &t, &len);
 
   /* If unbound variables should generate an error, report one and return
      failure. */
-  if ((var == 0 || array_p (var) == 0) && unbound_vars_is_error)
+  if ((var == 0 || invisible_p (var) || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error)
     {
-      ind = *--t;
+      c = *--t;
       *t = '\0';
-      report_error ("%s: unbound variable", s);
-      *t++ = (char)ind;
+      last_command_exit_value = EXECUTION_FAILURE;
+      err_unboundvar (s);
+      *t = c;
       return (-1);
     }
-  else if (var == 0)
+  else if (var == 0 || invisible_p (var))
     return 0;
-  else if (array_p (var) == 0)
-    return 1;
 
-  array = array_cell (var);
+  /* We support a couple of expansions for variables that are not arrays.
+     We'll return the length of the value for v[0], and 1 for v[@] or
+     v[*].  Return 0 for everything else. */
+
+  array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
+  h = assoc_p (var) ? assoc_cell (var) : (HASH_TABLE *)NULL;
 
   if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
-    return (array_num_elements (array));
+    {
+      if (assoc_p (var))
+       return (h ? assoc_num_elements (h) : 0);
+      else if (array_p (var))
+       return (array ? array_num_elements (array) : 0);
+      else
+       return (var_isset (var) ? 1 : 0);
+    }
 
-  ind = array_expand_index (t, len);
-  if (ind < 0)
+  if (assoc_p (var))
     {
-      report_error ("%s: bad array subscript", t);
-      return (-1);
+      t[len - 1] = '\0';
+      akey = expand_assignment_string_to_string (t, 0);        /* [ */
+      t[len - 1] = ']';
+      if (akey == 0 || *akey == 0)
+       {
+         err_badarraysub (t);
+         FREE (akey);
+         return (-1);
+       }
+      t = assoc_reference (assoc_cell (var), akey);
+      free (akey);
+    }
+  else
+    {
+      ind = array_expand_index (var, t, len);
+      /* negative subscripts to indexed arrays count back from end */
+      if (var && array_p (var) && ind < 0)
+       ind = array_max_index (array_cell (var)) + 1 + ind;
+      if (ind < 0)
+       {
+         err_badarraysub (t);
+         return (-1);
+       }
+      if (array_p (var))
+       t = array_reference (array, ind);
+      else
+       t = (ind == 0) ? value_cell (var) : (char *)NULL;
     }
-  t = array_reference (array, ind);
-  len = STRLEN (t);
 
+  len = MB_STRLEN (t);
   return (len);
 }
 #endif /* ARRAY_VARS */
@@ -3462,12 +6397,12 @@ valid_brace_expansion_word (name, var_is_special)
      char *name;
      int var_is_special;
 {
-  if (digit (*name) && all_digits (name))
+  if (DIGIT (*name) && all_digits (name))
     return 1;
   else if (var_is_special)
     return 1;
 #if defined (ARRAY_VARS)
-  else if (valid_array_reference (name))
+  else if (valid_array_reference (name, 0))
     return 1;
 #endif /* ARRAY_VARS */
   else if (legal_identifier (name))
@@ -3476,65 +6411,188 @@ valid_brace_expansion_word (name, var_is_special)
     return 0;
 }
 
+static int
+chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at)
+     char *name;
+     int quoted;
+     int *quoted_dollar_atp, *contains_dollar_at;
+{
+  char *temp1;
+
+  if (name == 0)
+    {
+      if (quoted_dollar_atp)
+       *quoted_dollar_atp = 0;
+      if (contains_dollar_at)
+       *contains_dollar_at = 0;
+      return 0;
+    }
+
+  /* check for $@ and $* */
+  if (name[0] == '@' && name[1] == 0)
+    {
+      if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
+       *quoted_dollar_atp = 1;
+      if (contains_dollar_at)
+       *contains_dollar_at = 1;
+      return 1;
+    }
+  else if (name[0] == '*' && name[1] == '\0' && quoted == 0)
+    {
+      if (contains_dollar_at)
+       *contains_dollar_at = 1;
+      return 1;
+    }
+
+  /* Now check for ${array[@]} and ${array[*]} */
+#if defined (ARRAY_VARS)
+  else if (valid_array_reference (name, 0))
+    {
+      temp1 = mbschr (name, '[');
+      if (temp1 && temp1[1] == '@' && temp1[2] == ']')
+       {
+         if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
+           *quoted_dollar_atp = 1;
+         if (contains_dollar_at)
+           *contains_dollar_at = 1;
+         return 1;
+       }       /* [ */
+      /* ${array[*]}, when unquoted, should be treated like ${array[@]},
+        which should result in separate words even when IFS is unset. */
+      if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0)
+       {
+         if (contains_dollar_at)
+           *contains_dollar_at = 1;
+         return 1;
+       }
+    }
+#endif
+  return 0;
+}
+
 /* Parameter expand NAME, and return a new string which is the expansion,
    or NULL if there was no expansion.
    VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
    the shell, e.g., "@", "$", "*", etc.  QUOTED, if non-zero, means that
    NAME was found inside of a double-quoted expression. */
-static char *
-parameter_brace_expand_word (name, var_is_special, quoted)
+static WORD_DESC *
+parameter_brace_expand_word (name, var_is_special, quoted, pflags, indp)
      char *name;
-     int var_is_special, quoted;
+     int var_is_special, quoted, pflags;
+     arrayind_t *indp;
 {
+  WORD_DESC *ret;
   char *temp, *tt;
-  int arg_index;
+  intmax_t arg_index;
   SHELL_VAR *var;
-  WORD_LIST *l;
+  int atype, rflags;
+  arrayind_t ind;
+
+  ret = 0;
+  temp = 0;
+  rflags = 0;
 
-  /* Handle multiple digit arguments, as in ${11}. */
-  if (digit (*name))
+  if (indp)
+    *indp = INTMAX_MIN;
+
+  /* Handle multiple digit arguments, as in ${11}. */  
+  if (legal_number (name, &arg_index))
     {
-      arg_index = atoi (name);
-      temp = get_dollar_var_value (arg_index);
+      tt = get_dollar_var_value (arg_index);
+      if (tt)
+       temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
+                 ? quote_string (tt)
+                 : quote_escapes (tt);
+      else
+        temp = (char *)NULL;
+      FREE (tt);
     }
   else if (var_is_special)      /* ${@} */
     {
       int sindex;
-      tt = xmalloc (2 + strlen (name));
+      tt = (char *)xmalloc (2 + strlen (name));
       tt[sindex = 0] = '$';
       strcpy (tt + 1, name);
-#if 0
-      l = expand_string_leave_quoted (tt, quoted);
-      free (tt);
-      temp = string_list (l);
-      dispose_words (l);
-#else
-      temp = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
-                          (int *)NULL, (int *)NULL, 0);
+
+      ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
+                         (int *)NULL, (int *)NULL, pflags);
       free (tt);
-#endif
     }
 #if defined (ARRAY_VARS)
-  else if (valid_array_reference (name))
+  else if (valid_array_reference (name, 0))
     {
-      temp = array_value (name, quoted);
+expand_arrayref:
+      if (pflags & PF_ASSIGNRHS)
+       {
+         var = array_variable_part (name, &tt, (int *)0);
+         if (ALL_ELEMENT_SUB (tt[0]) && tt[1] == ']')
+           {
+             /* Only treat as double quoted if array variable */
+             if (var && (array_p (var) || assoc_p (var)))
+               /* XXX - bash-4.4/bash-5.0 pass AV_ASSIGNRHS */
+               temp = array_value (name, quoted|Q_DOUBLE_QUOTES, AV_ASSIGNRHS, &atype, &ind);
+             else              
+               temp = array_value (name, quoted, 0, &atype, &ind);
+           }
+         else
+           temp = array_value (name, quoted, 0, &atype, &ind);
+       }
+      else
+       temp = array_value (name, quoted, 0, &atype, &ind);
+      if (atype == 0 && temp)
+       {
+         temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
+                   ? quote_string (temp)
+                   : quote_escapes (temp);
+         rflags |= W_ARRAYIND;
+         if (indp)
+           *indp = ind;
+       }                 
+      else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
+       rflags |= W_HASQUOTEDNULL;
     }
 #endif
   else if (var = find_variable (name))
     {
-      if (var && invisible_p (var) == 0)
-        {
+      if (var_isset (var) && invisible_p (var) == 0)
+       {
 #if defined (ARRAY_VARS)
-         temp = array_p (var) ? array_reference (array_cell (var), 0) : value_cell (var);
+         if (assoc_p (var))
+           temp = assoc_reference (assoc_cell (var), "0");
+         else if (array_p (var))
+           temp = array_reference (array_cell (var), 0);
+         else
+           temp = value_cell (var);
 #else
          temp = value_cell (var);
 #endif
 
          if (temp)
-           temp = quote_escapes (temp);
-
-         if (tempvar_p (var))
-           dispose_variable (var);
+           temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
+                     ? quote_string (temp)
+                     : quote_escapes (temp);
+       }
+      else
+       temp = (char *)NULL;
+    }
+  else if (var = find_variable_last_nameref (name, 0))
+    {
+      temp = nameref_cell (var);
+#if defined (ARRAY_VARS)
+      /* Handle expanding nameref whose value is x[n] */
+      if (temp && *temp && valid_array_reference (temp, 0))
+       {
+         name = temp;
+         goto expand_arrayref;
+       }
+      else
+#endif
+      /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */
+      if (temp && *temp && legal_identifier (temp) == 0)
+        {
+         last_command_exit_value = EXECUTION_FAILURE;
+         report_error (_("%s: invalid variable name for name reference"), temp);
+         temp = &expand_param_error;
         }
       else
        temp = (char *)NULL;
@@ -3542,95 +6600,248 @@ parameter_brace_expand_word (name, var_is_special, quoted)
   else
     temp = (char *)NULL;
 
-  return (temp);
+  if (ret == 0)
+    {
+      ret = alloc_word_desc ();
+      ret->word = temp;
+      ret->flags |= rflags;
+    }
+  return ret;
 }
 
+static char *
+parameter_brace_find_indir (name, var_is_special, quoted, find_nameref)
+     char *name;
+     int var_is_special, quoted, find_nameref;
+{
+  char *temp, *t;
+  WORD_DESC *w;
+  SHELL_VAR *v;
+
+  if (find_nameref && var_is_special == 0 && (v = find_variable_last_nameref (name, 0)) &&
+      nameref_p (v) && (t = nameref_cell (v)) && *t)
+    return (savestring (t));
+
+  /* If var_is_special == 0, and name is not an array reference, this does
+     more expansion than necessary.  It should really look up the variable's
+     value and not try to expand it. */
+  w = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND, 0);
+  t = w->word;
+  /* Have to dequote here if necessary */
+  if (t)
+    {
+      temp = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
+               ? dequote_string (t)
+               : dequote_escapes (t);
+      free (t);
+      t = temp;
+    }
+  dispose_word_desc (w);
+
+  return t;
+}
+  
 /* Expand an indirect reference to a variable: ${!NAME} expands to the
    value of the variable whose name is the value of NAME. */
-static char *
-parameter_brace_expand_indir (name, var_is_special, quoted)
+static WORD_DESC *
+parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at)
      char *name;
      int var_is_special, quoted;
+     int *quoted_dollar_atp, *contains_dollar_at;
 {
   char *temp, *t;
+  WORD_DESC *w;
+  SHELL_VAR *v;
+
+  /* See if it's a nameref first, behave in ksh93-compatible fashion.
+     There is at least one incompatibility: given ${!foo[0]} where foo=bar,
+     bash performs an indirect lookup on foo[0] and expands the result;
+     ksh93 expands bar[0].  We could do that here -- there are enough usable
+     primitives to do that -- but do not at this point. */
+  if (var_is_special == 0 && (v = find_variable_last_nameref (name, 0)))
+    {
+      if (nameref_p (v) && (t = nameref_cell (v)) && *t)
+       {
+         w = alloc_word_desc ();
+         w->word = savestring (t);
+         w->flags = 0;
+         return w;
+       }
+    }
+
+  t = parameter_brace_find_indir (name, var_is_special, quoted, 0);
 
-  t = parameter_brace_expand_word (name, var_is_special, quoted);
+  chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at);
   if (t == 0)
-    return (t);
-  temp = parameter_brace_expand_word (t, t[0] == '@' && t[1] == '\0', quoted);
+    return (WORD_DESC *)NULL;
+
+  if (valid_brace_expansion_word (t, SPECIAL_VAR (t, 0)) == 0)
+    {
+      report_error (_("%s: bad substitution"), t);
+      free (t);
+      w = alloc_word_desc ();
+      w->word = &expand_param_error;
+      w->flags = 0;
+      return (w);
+    }
+       
+  w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, 0, 0);
   free (t);
-  return temp;
+
+  return w;
 }
 
 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
    depending on the value of C, the separating character.  C can be one of
    "-", "+", or "=".  QUOTED is true if the entire brace expression occurs
    between double quotes. */
-static char *
-parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat)
+static WORD_DESC *
+parameter_brace_expand_rhs (name, value, c, quoted, pflags, qdollaratp, hasdollarat)
      char *name, *value;
-     int c, quoted, *qdollaratp, *hasdollarat;
+     int c, quoted, pflags, *qdollaratp, *hasdollarat;
 {
+  WORD_DESC *w;
   WORD_LIST *l;
-  char *t, *t1, *temp;
-  int hasdol;
-
-  temp = (*value == '~' || (strchr (value, '~') && unquoted_substring ("=~", value)))
-        ? bash_tilde_expand (value)
-       : savestring (value);
+  char *t, *t1, *temp, *vname;
+  int l_hasdollat, sindex;
 
+/*itrace("parameter_brace_expand_rhs: %s:%s pflags = %d", name, value, pflags);*/
   /* If the entire expression is between double quotes, we want to treat
      the value as a double-quoted string, with the exception that we strip
-     embedded unescaped double quotes. */
-  if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *temp)
+     embedded unescaped double quotes (for sh backwards compatibility). */
+  if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
     {
-      hasdol = 0;
-      t = string_extract_double_quoted (temp, &hasdol, 1);
-      free (temp);
-      temp = t;
+      sindex = 0;
+      temp = string_extract_double_quoted (value, &sindex, SX_STRIPDQ);
     }
+  else
+    temp = value;
 
-  hasdol = 0;
+  w = alloc_word_desc ();
+  l_hasdollat = 0;
   /* XXX was 0 not quoted */
-  l = *temp ? expand_string_for_rhs (temp, quoted, &hasdol, (int *)NULL)
+  l = *temp ? expand_string_for_rhs (temp, quoted, &l_hasdollat, (int *)NULL)
            : (WORD_LIST *)0;
   if (hasdollarat)
-    *hasdollarat = hasdol || (l && l->next);
-  free (temp);
+    *hasdollarat = l_hasdollat || (l && l->next);
+  if (temp != value)
+    free (temp);
   if (l)
     {
-      /* The expansion of TEMP returned something.  We need to treat things
-         slightly differently if HASDOL is non-zero. */
-      temp = string_list (l);
       /* If l->next is not null, we know that TEMP contained "$@", since that
         is the only expansion that creates more than one word. */
-      if ((hasdol && quoted) || l->next)
-       *qdollaratp = 1;
+      if (qdollaratp && ((l_hasdollat && quoted) || l->next))
+       {
+/*itrace("parameter_brace_expand_rhs: %s:%s: l != NULL, set *qdollaratp", name, value);*/
+         *qdollaratp = 1;
+       }
+
+      /* The expansion of TEMP returned something.  We need to treat things
+         slightly differently if L_HASDOLLAT is non-zero.  If we have "$@",
+         the individual words have already been quoted.  We need to turn them
+         into a string with the words separated by the first character of
+         $IFS without any additional quoting, so string_list_dollar_at won't
+         do the right thing.  If IFS is null, we want "$@" to split into
+         separate arguments, not be concatenated, so we use string_list_internal
+         and mark the word to be split on spaces later.  We use
+         string_list_dollar_star for "$@" otherwise. */
+      if (l->next && ifs_is_null)
+       {
+         temp = string_list_internal (l, " ");
+         w->flags |= W_SPLITSPACE;
+       }
+      else
+       temp = (l_hasdollat || l->next) ? string_list_dollar_star (l) : string_list (l);
+
+      /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is
+        a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the
+        flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the
+        expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+        (which is more paranoia than anything else), we need to return the
+        quoted null string and set the flags to indicate it. */
+      if (l->next == 0 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp) && QUOTED_NULL (l->word->word) && (l->word->flags & W_HASQUOTEDNULL))
+       {
+         w->flags |= W_HASQUOTEDNULL;
+/*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null, turning off qdollaratp", name, value);*/
+         /* If we return a quoted null with L_HASDOLLARAT, we either have a
+            construct like "${@-$@}" or "${@-${@-$@}}" with no positional
+            parameters or a quoted expansion of "$@" with $1 == ''.  In either
+            case, we don't want to enable special handling of $@. */
+         if (qdollaratp && l_hasdollat)
+           *qdollaratp = 0;
+       }
       dispose_words (l);
     }
-  else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && hasdol)
+  else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && l_hasdollat)
     {
+      /* Posix interp 221 changed the rules on this.  The idea is that
+        something like "$xxx$@" should expand the same as "${foo-$xxx$@}"
+        when foo and xxx are unset.  The problem is that it's not in any
+        way backwards compatible and few other shells do it.  We're eventually
+        going to try and split the difference (heh) a little bit here. */
+      /* l_hasdollat == 1 means we saw a quoted dollar at.  */
+
       /* The brace expansion occurred between double quotes and there was
         a $@ in TEMP.  It does not matter if the $@ is quoted, as long as
-        it does not expand to anything.  In this case, we want to return
-        a quoted empty string. */
-      temp = xmalloc (2);
-      temp[0] = CTLNUL;
-      temp[1] = '\0';
+        it does not expand to anything.  In this case, we want to return
+        a quoted empty string.  Posix interp 888 */
+      temp = make_quoted_char ('\0');
+      w->flags |= W_HASQUOTEDNULL;
+/*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null", name, value);*/
     }
   else
     temp = (char *)NULL;
 
   if (c == '-' || c == '+')
-    return (temp);
+    {
+      w->word = temp;
+      return w;
+    }
 
   /* c == '=' */
   t = temp ? savestring (temp) : savestring ("");
   t1 = dequote_string (t);
   free (t);
-  bind_variable (name, t1);
-  free (t1);
-  return (temp);
+
+  /* bash-4.4/5.0 */
+  vname = name;
+  if (*name == '!' &&
+      (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) || VALID_INDIR_PARAM (name[1])))
+    {
+      vname = parameter_brace_find_indir (name + 1, SPECIAL_VAR (name, 1), quoted, 1);
+      if (vname == 0 || *vname == 0)
+       {
+         report_error (_("%s: invalid indirect expansion"), name);
+         free (vname);
+         dispose_word (w);
+         return &expand_wdesc_error;
+       }
+      if (legal_identifier (vname) == 0)
+       {
+         report_error (_("%s: invalid variable name"), vname);
+         free (vname);
+         dispose_word (w);
+         return &expand_wdesc_error;
+       }
+    }
+    
+#if defined (ARRAY_VARS)
+  if (valid_array_reference (vname, 0))
+    assign_array_element (vname, t1, 0);
+  else
+#endif /* ARRAY_VARS */
+  bind_variable (vname, t1, 0);
+
+  stupidly_hack_special_variables (vname);
+
+  if (vname != name)
+    free (vname);
+
+  /* From Posix group discussion Feb-March 2010.  Issue 7 0000221 */
+  free (temp);
+
+  w->word = t1;
+  return w;
 }
 
 /* Deal with the right hand side of a ${name:?value} expansion in the case
@@ -3644,6 +6855,7 @@ parameter_brace_expand_error (name, value)
   WORD_LIST *l;
   char *temp;
 
+  last_command_exit_value = EXECUTION_FAILURE; /* ensure it's non-zero */
   if (value && *value)
     {
       l = expand_string (value, 0);
@@ -3653,7 +6865,7 @@ parameter_brace_expand_error (name, value)
       dispose_words (l);
     }
   else
-    report_error ("%s: parameter null or not set", name);
+    report_error (_("%s: parameter null or not set"), name);
 
   /* Free the data we have allocated during this expansion, since we
      are about to longjmp out. */
@@ -3667,24 +6879,23 @@ static int
 valid_length_expression (name)
      char *name;
 {
-  return (!name[1] ||                                          /* ${#} */
-         ((name[1] == '@' || name[1] == '*') && !name[2]) ||   /* ${#@}, ${#*} */
-         (member (name[1], "-?$!#") && !name[2]) ||            /* ${#-}, etc. */
-         (digit (name[1]) && all_digits (name + 1)) ||         /* ${#11} */
+  return (name[1] == '\0' ||                                   /* ${#} */
+         ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') ||  /* special param */
+         (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */
 #if defined (ARRAY_VARS)
-         valid_array_reference (name + 1) ||                   /* ${#a[7]} */
+         valid_array_reference (name + 1, 0) ||                /* ${#a[7]} */
 #endif
          legal_identifier (name + 1));                         /* ${#PS1} */
 }
 
 /* Handle the parameter brace expansion that requires us to return the
    length of a parameter. */
-static int
+static intmax_t
 parameter_brace_expand_length (name)
      char *name;
 {
   char *t, *newname;
-  int number;
+  intmax_t number, arg_index;
   WORD_LIST *list;
 #if defined (ARRAY_VARS)
   SHELL_VAR *var;
@@ -3692,9 +6903,9 @@ parameter_brace_expand_length (name)
 
   if (name[1] == '\0')                 /* ${#} */
     number = number_of_args ();
-  else if ((name[1] == '@' || name[1] == '*') && name[2] == '\0')      /* ${#@}, ${#*} */
+  else if (DOLLAR_AT_STAR (name[1]) && name[2] == '\0')        /* ${#@}, ${#*} */
     number = number_of_args ();
-  else if (member (name[1], "-?$!#") && name[2] == '\0')
+  else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
     {
       /* Take the lengths of some of the shell's special parameters. */
       switch (name[1])
@@ -3710,9 +6921,9 @@ parameter_brace_expand_length (name)
          break;
        case '!':
          if (last_asynchronous_pid == NO_PID)
-           t = (char *)NULL;
+           t = (char *)NULL;   /* XXX - error if set -u set? */
          else
-           t = itos ((int)last_asynchronous_pid);
+           t = itos (last_asynchronous_pid);
          break;
        case '#':
          t = itos (number_of_args ());
@@ -3722,24 +6933,31 @@ parameter_brace_expand_length (name)
       FREE (t);
     }
 #if defined (ARRAY_VARS)
-  else if (valid_array_reference (name + 1))
+  else if (valid_array_reference (name + 1, 0))
     number = array_length_reference (name + 1);
 #endif /* ARRAY_VARS */
   else
     {
       number = 0;
 
-      if (digit (name[1]))             /* ${#1} */
+      if (legal_number (name + 1, &arg_index))         /* ${#1} */
        {
-         t = get_dollar_var_value (atoi (name + 1));
-         number = STRLEN (t);
+         t = get_dollar_var_value (arg_index);
+         if (t == 0 && unbound_vars_is_error)
+           return INTMAX_MIN;
+         number = MB_STRLEN (t);
          FREE (t);
        }
 #if defined (ARRAY_VARS)
-      else if ((var = find_variable (name + 1)) && array_p (var))
+      else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var)))
        {
-         t = array_reference (array_cell (var), 0);
-         number = STRLEN (t);
+         if (assoc_p (var))
+           t = assoc_reference (assoc_cell (var), "0");
+         else
+           t = array_reference (array_cell (var), 0);
+         if (t == 0 && unbound_vars_is_error)
+           return INTMAX_MIN;
+         number = MB_STRLEN (t);
        }
 #endif
       else                             /* ${#PS1} */
@@ -3752,7 +6970,7 @@ parameter_brace_expand_length (name)
          if (list)
            dispose_words (list);
 
-         number = STRLEN (t);
+         number = t ? MB_STRLEN (t) : 0;
          FREE (t);
        }
     }
@@ -3760,6 +6978,29 @@ parameter_brace_expand_length (name)
   return (number);
 }
 
+/* Skip characters in SUBSTR until DELIM.  SUBSTR is an arithmetic expression,
+   so we do some ad-hoc parsing of an arithmetic expression to find
+   the first DELIM, instead of using strchr(3).  Two rules:
+       1.  If the substring contains a `(', read until closing `)'.
+       2.  If the substring contains a `?', read past one `:' for each `?'.
+   The SD_ARITHEXP flag to skip_to_delim takes care of doing this.
+*/
+
+static char *
+skiparith (substr, delim)
+     char *substr;
+     int delim;
+{
+  int i;
+  char delims[2];
+
+  delims[0] = delim;
+  delims[1] = '\0';
+
+  i = skip_to_delim (substr, 0, delims, SD_ARITHEXP);
+  return (substr + i);
+}
+
 /* Verify and limit the start and end of the desired substring.  If
    VTYPE == 0, a regular shell variable is being used; if it is 1,
    then the positional parameters are being used; if it is 2, then
@@ -3767,65 +7008,122 @@ parameter_brace_expand_length (name)
    Return value is 1 if both values were OK, 0 if there was a problem
    with an invalid expression, or -1 if the values were out of range. */
 static int
-verify_substring_values (value, substr, vtype, e1p, e2p)
+verify_substring_values (v, value, substr, vtype, e1p, e2p)
+     SHELL_VAR *v;
      char *value, *substr;
-     int vtype, *e1p, *e2p;
+     int vtype;
+     intmax_t *e1p, *e2p;
 {
-  char *t, *temp1;
-  int len, expok;
+  char *t, *temp1, *temp2;
+  arrayind_t len;
+  int expok;
 #if defined (ARRAY_VARS)
  ARRAY *a;
+ HASH_TABLE *h;
 #endif
 
-  t = strchr (substr, ':');
-  if (t)
+  /* duplicate behavior of strchr(3) */
+  t = skiparith (substr, ':');
+  if (*t && *t == ':')
     *t = '\0';
-  temp1 = maybe_expand_string (substr, Q_DOUBLE_QUOTES, expand_string);
+  else
+    t = (char *)0;
+
+  temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES);
   *e1p = evalexp (temp1, &expok);
   free (temp1);
   if (expok == 0)
     return (0);
 
+  len = -1;    /* paranoia */
   switch (vtype)
     {
     case VT_VARIABLE:
     case VT_ARRAYMEMBER:
-      len = strlen (value);
+      len = MB_STRLEN (value);
       break;
     case VT_POSPARMS:
       len = number_of_args () + 1;
+      if (*e1p == 0)
+       len++;          /* add one arg if counting from $0 */
       break;
 #if defined (ARRAY_VARS)
     case VT_ARRAYVAR:
-      a = (ARRAY *)value;
-      len = array_num_elements (a) + 1;
+      /* For arrays, the first value deals with array indices.  Negative
+        offsets count from one past the array's maximum index.  Associative
+        arrays treat the number of elements as the maximum index. */
+      if (assoc_p (v))
+       {
+         h = assoc_cell (v);
+         len = assoc_num_elements (h) + (*e1p < 0);
+       }
+      else
+       {
+         a = (ARRAY *)value;
+         len = array_max_index (a) + (*e1p < 0);       /* arrays index from 0 to n - 1 */
+       }
       break;
 #endif
     }
 
+  if (len == -1)       /* paranoia */
+    return -1;
+
   if (*e1p < 0)                /* negative offsets count from end */
     *e1p += len;
 
-  if (*e1p >= len || *e1p < 0)
+  if (*e1p > len || *e1p < 0)
     return (-1);
 
+#if defined (ARRAY_VARS)
+  /* For arrays, the second offset deals with the number of elements. */
+  if (vtype == VT_ARRAYVAR)
+    len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a);
+#endif
+
   if (t)
     {
       t++;
-      temp1 = maybe_expand_string (t, Q_DOUBLE_QUOTES, expand_string);
+      temp2 = savestring (t);
+      temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
+      free (temp2);
       t[-1] = ':';
       *e2p = evalexp (temp1, &expok);
       free (temp1);
       if (expok == 0)
-        return (0);
-      if (*e2p < 0)
-        {
-          internal_error ("%s: substring expression < 0", t);
+       return (0);
+#if 1
+      if ((vtype == VT_ARRAYVAR || vtype == VT_POSPARMS) && *e2p < 0)
+#else
+      /* bash-4.3: allow positional parameter length < 0 to count backwards
+        from end of positional parameters */
+      if (vtype == VT_ARRAYVAR && *e2p < 0)
+#endif
+       {
+         internal_error (_("%s: substring expression < 0"), t);
          return (0);
-        }
-      *e2p += *e1p;            /* want E2 chars starting at E1 */
-      if (*e2p > len)
-        *e2p = len;
+       }
+#if defined (ARRAY_VARS)
+      /* In order to deal with sparse arrays, push the intelligence about how
+        to deal with the number of elements desired down to the array-
+        specific functions.  */
+      if (vtype != VT_ARRAYVAR)
+#endif
+       {
+         if (*e2p < 0)
+           {
+             *e2p += len;
+             if (*e2p < 0 || *e2p < *e1p)
+               {
+                 internal_error (_("%s: substring expression < 0"), t);
+                 return (0);
+               }
+           }
+         else
+           *e2p += *e1p;               /* want E2 chars starting at E1 */
+         if (*e2p > len)
+           *e2p = len;
+       }
     }
   else
     *e2p = len;
@@ -3835,52 +7133,120 @@ verify_substring_values (value, substr, vtype, e1p, e2p)
 
 /* Return the type of variable specified by VARNAME (simple variable,
    positional param, or array variable).  Also return the value specified
-   by VARNAME (value of a variable or a reference to an array element). */
+   by VARNAME (value of a variable or a reference to an array element).
+   QUOTED is the standard description of quoting state, using Q_* defines.
+   FLAGS is currently a set of flags to pass to array_value.  If IND is
+   non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is
+   passed to array_value so the array index is not computed again.
+   If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
+   characters in the value are quoted with CTLESC and takes appropriate
+   steps.  For convenience, *VALP is set to the dequoted VALUE. */
 static int
-get_var_and_type (varname, value, varp, valp)
+get_var_and_type (varname, value, ind, quoted, flags, varp, valp)
      char *varname, *value;
+     arrayind_t ind;
+     int quoted, flags;
      SHELL_VAR **varp;
      char **valp;
 {
-  int vtype;
-  char *temp;
-#if defined (ARRAY_VARS)
+  int vtype, want_indir;
+  char *temp, *vname;
+  WORD_DESC *wd;
   SHELL_VAR *v;
-#endif
+  arrayind_t lind;
+
+  want_indir = *varname == '!' &&
+    (legal_variable_starter ((unsigned char)varname[1]) || DIGIT (varname[1])
+                                       || VALID_INDIR_PARAM (varname[1]));
+  if (want_indir)
+    vname = parameter_brace_find_indir (varname+1, SPECIAL_VAR (varname, 1), quoted, 1);
+    /* XXX - what if vname == 0 || *vname == 0 ? */
+  else
+    vname = varname;
+
+  if (vname == 0)
+    {
+      vtype = VT_VARIABLE;
+      *varp = (SHELL_VAR *)NULL;
+      *valp = (char *)NULL;
+      return (vtype);
+    }
 
-  vtype = (varname[0] == '@' || varname[0] == '*') && varname[1] == '\0';      /* VT_POSPARMS */
+  /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
+  vtype = STR_DOLLAR_AT_STAR (vname);
+  if (vtype == VT_POSPARMS && vname[0] == '*')
+    vtype |= VT_STARSUB;
   *varp = (SHELL_VAR *)NULL;
 
 #if defined (ARRAY_VARS)
-  if (valid_array_reference (varname))
+  if (valid_array_reference (vname, 0))
     {
-      v = array_variable_part (varname, &temp, (int *)0);
-      if (v && array_p (v))
+      v = array_variable_part (vname, &temp, (int *)0);
+      /* If we want to signal array_value to use an already-computed index,
+        set LIND to that index */
+      lind = (ind != INTMAX_MIN && (flags & AV_USEIND)) ? ind : 0;
+      if (v && invisible_p (v))
        {
-         if ((temp[0] == '@' || temp[0] == '*') && temp[1] == ']')
+         vtype = VT_ARRAYMEMBER;
+         *varp = (SHELL_VAR *)NULL;
+         *valp = (char *)NULL;
+       }
+      if (v && (array_p (v) || assoc_p (v)))
+       { /* [ */
+         if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')
            {
+             /* Callers have to differentiate between indexed and associative */
              vtype = VT_ARRAYVAR;
-             *valp = (char *)array_cell (v);
+             if (temp[0] == '*')
+               vtype |= VT_STARSUB;
+             *valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v);
            }
          else
            {
              vtype = VT_ARRAYMEMBER;
-             *valp = array_value (varname, 1);
+             *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
            }
          *varp = v;
        }
+      else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']'))
+       {
+         vtype = VT_VARIABLE;
+         *varp = v;
+         if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
+           *valp = dequote_string (value);
+         else
+           *valp = dequote_escapes (value);
+       }
       else
-       return -1;
+       {
+         vtype = VT_ARRAYMEMBER;
+         *varp = v;
+         *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
+       }
     }
-  else if ((v = find_variable (varname)) && array_p (v))
+  else if ((v = find_variable (vname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v)))
     {
-      vtype = VT_VARIABLE;
+      vtype = VT_ARRAYMEMBER;
       *varp = v;
-      *valp = array_reference (array_cell (v), 0);
+      *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0);
     }
   else
 #endif
-  *valp = value;
+    {
+      if (value && vtype == VT_VARIABLE)
+       {
+         *varp = find_variable (vname);
+         if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
+           *valp = dequote_string (value);
+         else
+           *valp = dequote_escapes (value);
+       }
+      else
+       *valp = value;
+    }
+
+  if (want_indir)
+    free (vname);
 
   return vtype;
 }
@@ -3891,33 +7257,75 @@ get_var_and_type (varname, value, varp, valp)
 /*                                                   */
 /******************************************************/
 
+#if defined (HANDLE_MULTIBYTE)
+/* Character-oriented rather than strictly byte-oriented substrings.  S and
+   E, rather being strict indices into STRING, indicate character (possibly
+   multibyte character) positions that require calculation.
+   Used by the ${param:offset[:length]} expansion. */
+static char *
+mb_substring (string, s, e)
+     char *string;
+     int s, e;
+{
+  char *tt;
+  int start, stop, i;
+  size_t slen;
+  DECLARE_MBSTATE;
+
+  start = 0;
+  /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
+  slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0;
+
+  i = s;
+  while (string[start] && i--)
+    ADVANCE_CHAR (string, slen, start);
+  stop = start;
+  i = e - s;
+  while (string[stop] && i--)
+    ADVANCE_CHAR (string, slen, stop);
+  tt = substring (string, start, stop);
+  return tt;
+}
+#endif
+  
 /* Process a variable substring expansion: ${name:e1[:e2]}.  If VARNAME
    is `@', use the positional parameters; otherwise, use the value of
    VARNAME.  If VARNAME is an array variable, use the array elements. */
 
 static char *
-parameter_brace_substring (varname, value, substr, quoted)
-     char *varname, *value, *substr;
-     int quoted;
+parameter_brace_substring (varname, value, ind, substr, quoted, flags)
+     char *varname, *value;
+     int ind;
+     char *substr;
+     int quoted, flags;
 {
-  int e1, e2, vtype, r;
-  char *temp, *val;
+  intmax_t e1, e2;
+  int vtype, r, starsub;
+  char *temp, *val, *tt, *oname;
   SHELL_VAR *v;
 
-  if (value == 0)
+  if (value == 0 && ((varname[0] != '@' && varname[0] != '*') || varname[1]))
     return ((char *)NULL);
 
+  oname = this_command_name;
   this_command_name = varname;
 
-  vtype = get_var_and_type (varname, value, &v, &val);
+  vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
   if (vtype == -1)
-    return ((char *)NULL);
+    {
+      this_command_name = oname;
+      return ((char *)NULL);
+    }
+
+  starsub = vtype & VT_STARSUB;
+  vtype &= ~VT_STARSUB;
 
-  r = verify_substring_values (val, substr, vtype, &e1, &e2);
+  r = verify_substring_values (v, val, substr, vtype, &e1, &e2);
+  this_command_name = oname;
   if (r <= 0)
     {
-      if (val && vtype == VT_ARRAYMEMBER)
-       free (val);
+      if (vtype == VT_VARIABLE)
+       FREE (val);
       return ((r == 0) ? &expand_param_error : (char *)NULL);
     }
 
@@ -3925,16 +7333,48 @@ parameter_brace_substring (varname, value, substr, quoted)
     {
     case VT_VARIABLE:
     case VT_ARRAYMEMBER:
-      temp = quoted ? quoted_substring (value, e1, e2) : substring (value, e1, e2);
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1)
+       tt = mb_substring (val, e1, e2);
+      else
+#endif
+      tt = substring (val, e1, e2);
+
+      if (vtype == VT_VARIABLE)
+       FREE (val);
+      if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
+       temp = quote_string (tt);
+      else
+       temp = tt ? quote_escapes (tt) : (char *)NULL;
+      FREE (tt);
       break;
     case VT_POSPARMS:
-      temp = pos_params (varname, e1, e2, quoted);
+      tt = pos_params (varname, e1, e2, quoted);
+      if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
+       {
+         temp = tt ? quote_escapes (tt) : (char *)NULL;
+         FREE (tt);
+       }
+      else
+       temp = tt;
       break;
 #if defined (ARRAY_VARS)
     case VT_ARRAYVAR:
-      temp = array_subrange (array_cell (v), e1, e2, quoted);
+      if (assoc_p (v))
+       /* we convert to list and take first e2 elements starting at e1th
+          element -- officially undefined for now */   
+       temp = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted);
+      else
+      /* We want E2 to be the number of elements desired (arrays can be sparse,
+        so verify_substring_values just returns the numbers specified and we
+        rely on array_subrange to understand how to deal with them). */
+       temp = array_subrange (array_cell (v), e1, e2, starsub, quoted);
+      /* array_subrange now calls array_quote_escapes as appropriate, so the
+        caller no longer needs to. */
       break;
 #endif
+    default:
+      temp = (char *)NULL;
     }
 
   return temp;
@@ -3946,30 +7386,116 @@ parameter_brace_substring (varname, value, substr, quoted)
 /*                                                             */
 /****************************************************************/
 
+#if 0  /* Unused */
+static int
+shouldexp_replacement (s)
+     char *s;
+{
+  register char *p;
+
+  for (p = s; p && *p; p++)
+    {
+      if (*p == '\\')
+       p++;
+      else if (*p == '&')
+       return 1;
+    }
+  return 0;
+}
+#endif
+
 char *
 pat_subst (string, pat, rep, mflags)
      char *string, *pat, *rep;
      int mflags;
 {
-  char *ret, *s, *e, *str;
-  int rsize, rptr, l, replen, mtype;
+  char *ret, *s, *e, *str, *rstr, *mstr;
+  int rptr, mtype, rxpand, mlen;
+  size_t rsize, l, replen, rslen;
 
-  ret = xmalloc (rsize = 64);
-  ret[0] = '\0';
+  if (string  == 0)
+    return (savestring (""));
 
   mtype = mflags & MATCH_TYPEMASK;
 
-  for (replen = STRLEN (rep), rptr = 0, str = string;;)
+#if 0  /* bash-4.2 ? */
+  rxpand = (rep && *rep) ? shouldexp_replacement (rep) : 0;
+#else
+  rxpand = 0;
+#endif
+
+  /* Special cases:
+   *   1.  A null pattern with mtype == MATCH_BEG means to prefix STRING
+   *       with REP and return the result.
+   *   2.  A null pattern with mtype == MATCH_END means to append REP to
+   *       STRING and return the result.
+   *   3.  A null STRING with a matching pattern means to append REP to
+   *       STRING and return the result.
+   * These don't understand or process `&' in the replacement string.
+   */
+  if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
+    {
+      replen = STRLEN (rep);
+      l = STRLEN (string);
+      ret = (char *)xmalloc (replen + l + 2);
+      if (replen == 0)
+       strcpy (ret, string);
+      else if (mtype == MATCH_BEG)
+       {
+         strcpy (ret, rep);
+         strcpy (ret + replen, string);
+       }
+      else
+       {
+         strcpy (ret, string);
+         strcpy (ret + l, rep);
+       }
+      return (ret);
+    }
+  else if (*string == 0 && (match_pattern (string, pat, mtype, &s, &e) != 0))
+    {
+      replen = STRLEN (rep);
+      ret = (char *)xmalloc (replen + 1);
+      if (replen == 0)
+       ret[0] = '\0';
+      else
+       strcpy (ret, rep);
+      return (ret);
+    }
+
+  ret = (char *)xmalloc (rsize = 64);
+  ret[0] = '\0';
+
+  for (replen = STRLEN (rep), rptr = 0, str = string; *str;)
     {
       if (match_pattern (str, pat, mtype, &s, &e) == 0)
        break;
       l = s - str;
-      RESIZE_MALLOCED_BUFFER (ret, rptr, (l + replen), rsize, 64);
+
+      if (rep && rxpand)
+        {
+         int x;
+         mlen = e - s;
+         mstr = xmalloc (mlen + 1);
+         for (x = 0; x < mlen; x++)
+           mstr[x] = s[x];
+         mstr[mlen] = '\0';
+         rstr = strcreplace (rep, '&', mstr, 0);
+         free (mstr);
+         rslen = strlen (rstr);
+        }
+      else
+       {
+         rstr = rep;
+         rslen = replen;
+       }
+        
+      RESIZE_MALLOCED_BUFFER (ret, rptr, (l + rslen), rsize, 64);
 
       /* OK, now copy the leading unmatched portion of the string (from
         str to s) to ret starting at rptr (the current offset).  Then copy
-         the replacement string at ret + rptr + (s - str).  Increment
-         rptr (if necessary) and str and go on. */
+        the replacement string at ret + rptr + (s - str).  Increment
+        rptr (if necessary) and str and go on. */
       if (l)
        {
          strncpy (ret + rptr, str, l);
@@ -3977,16 +7503,29 @@ pat_subst (string, pat, rep, mflags)
        }
       if (replen)
        {
-         strncpy (ret + rptr, rep, replen);
-         rptr += replen;
+         strncpy (ret + rptr, rstr, rslen);
+         rptr += rslen;
        }
       str = e;         /* e == end of match */
+
+      if (rstr != rep)
+       free (rstr);
+
       if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
-        break;
+       break;
+
+      if (s == e)
+       {
+         /* On a zero-length match, make sure we copy one character, since
+            we increment one character to avoid infinite recursion. */
+         RESIZE_MALLOCED_BUFFER (ret, rptr, 1, rsize, 64);
+         ret[rptr++] = *str++;
+         e++;          /* avoid infinite recursion on zero-length match */
+       }
     }
 
   /* Now copy the unmatched portion of the input string */
-  if (*str)
+  if (str && *str)
     {
       RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
       strcpy (ret + rptr, str);
@@ -4006,6 +7545,7 @@ pos_params_pat_subst (string, pat, rep, mflags)
   WORD_LIST *save, *params;
   WORD_DESC *w;
   char *ret;
+  int pchar, qflags;
 
   save = params = list_rest_of_args ();
   if (save == 0)
@@ -4014,13 +7554,17 @@ pos_params_pat_subst (string, pat, rep, mflags)
   for ( ; params; params = params->next)
     {
       ret = pat_subst (params->word->word, pat, rep, mflags);
-      w = make_bare_word (ret);
+      w = alloc_word_desc ();
+      w->word = ret ? ret : savestring ("");
       dispose_word (params->word);
       params->word = w;
-      FREE (ret);
     }
 
-  ret = string_list ((mflags & MATCH_QUOTED) ? quote_list (save) : save);
+  pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
+  qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
+
+  ret = string_list_pos_params (pchar, save, qflags);
+
   dispose_words (save);
 
   return (ret);
@@ -4031,12 +7575,14 @@ pos_params_pat_subst (string, pat, rep, mflags)
    and the string to substitute.  QUOTED is a flags word containing
    the type of quoting currently in effect. */
 static char *
-parameter_brace_patsub (varname, value, patsub, quoted)
-     char *varname, *value, *patsub;
-     int quoted;
+parameter_brace_patsub (varname, value, ind, patsub, quoted, pflags, flags)
+     char *varname, *value;
+     int ind;
+     char *patsub;
+     int quoted, pflags, flags;
 {
-  int vtype, mflags;
-  char *val, *temp, *pat, *rep, *p;
+  int vtype, mflags, starsub, delim;
+  char *val, *temp, *pat, *rep, *p, *lpatsub, *tt;
   SHELL_VAR *v;
 
   if (value == 0)
@@ -4044,46 +7590,75 @@ parameter_brace_patsub (varname, value, patsub, quoted)
 
   this_command_name = varname;
 
-  vtype = get_var_and_type (varname, value, &v, &val);
+  vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
   if (vtype == -1)
     return ((char *)NULL);
 
+  starsub = vtype & VT_STARSUB;
+  vtype &= ~VT_STARSUB;
+
   mflags = 0;
+  /* PATSUB is never NULL when this is called. */
   if (*patsub == '/')
     {
       mflags |= MATCH_GLOBREP;
       patsub++;
     }
 
+  /* Malloc this because expand_string_if_necessary or one of the expansion
+     functions in its call chain may free it on a substitution error. */
+  lpatsub = savestring (patsub);
+
   if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
     mflags |= MATCH_QUOTED;
 
-  if (rep = quoted_strchr (patsub, '/', ST_BACKSL))
-    *rep++ = '\0';
+  if (starsub)
+    mflags |= MATCH_STARSUB;
+
+  if (pflags & PF_ASSIGNRHS)
+    mflags |= MATCH_ASSIGNRHS;
+
+  /* If the pattern starts with a `/', make sure we skip over it when looking
+     for the replacement delimiter. */
+  delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0);
+  if (lpatsub[delim] == '/')
+    {
+      lpatsub[delim] = 0;
+      rep = lpatsub + delim + 1;
+    }
   else
     rep = (char *)NULL;
 
   if (rep && *rep == '\0')
     rep = (char *)NULL;
 
-  /* Expand PAT and REP for command, variable and parameter, arithmetic,
-     and process substitution.  Also perform quote removal.  Do not
-     perform word splitting or filename generation. */
-#if 0
-  pat = maybe_expand_string (patsub, quoted, expand_string_unsplit);
-#else
-  pat = maybe_expand_string (patsub, (quoted & ~Q_DOUBLE_QUOTES), expand_string_unsplit);
-#endif
+  /* Perform the same expansions on the pattern as performed by the
+     pattern removal expansions. */
+  pat = getpattern (lpatsub, quoted, 1);
+
   if (rep)
     {
-      if ((mflags & MATCH_QUOTED) == 0)
-       rep = maybe_expand_string (rep, quoted, expand_string_unsplit);
+      /* We want to perform quote removal on the expanded replacement even if
+        the entire expansion is double-quoted because the parser and string
+        extraction functions treated quotes in the replacement string as
+        special.  THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */
+      if (shell_compatibility_level > 42)
+       rep = expand_string_if_necessary (rep, quoted & ~(Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT), expand_string_unsplit);
+      /* This is the bash-4.2 code. */      
+      else if ((mflags & MATCH_QUOTED) == 0)
+       rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
       else
-        rep = expand_string_to_string (rep, quoted, expand_string_unsplit);
+       rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
     }
 
+  /* ksh93 doesn't allow the match specifier to be a part of the expanded
+     pattern.  This is an extension.  Make sure we don't anchor the pattern
+     at the beginning or end of the string if we're doing global replacement,
+     though. */
   p = pat;
-  if (pat && pat[0] == '#')
+  if (mflags & MATCH_GLOBREP)
+    mflags |= MATCH_ANY;
+  else if (pat && pat[0] == '#')
     {
       mflags |= MATCH_BEG;
       p++;
@@ -4099,30 +7674,247 @@ parameter_brace_patsub (varname, value, patsub, quoted)
   /* OK, we now want to substitute REP for PAT in VAL.  If
      flags & MATCH_GLOBREP is non-zero, the substitution is done
      everywhere, otherwise only the first occurrence of PAT is
-     replaced. */
+     replaced.  The pattern matching code doesn't understand
+     CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
+     values passed in (VT_VARIABLE) so the pattern substitution
+     code works right.  We need to requote special chars after
+     we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
+     other cases if QUOTED == 0, since the posparams and arrays
+     indexed by * or @ do special things when QUOTED != 0. */
+
   switch (vtype)
     {
     case VT_VARIABLE:
     case VT_ARRAYMEMBER:
       temp = pat_subst (val, p, rep, mflags);
+      if (vtype == VT_VARIABLE)
+       FREE (val);
+      if (temp)
+       {
+         tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
+         free (temp);
+         temp = tt;
+       }
       break;
     case VT_POSPARMS:
       temp = pos_params_pat_subst (val, p, rep, mflags);
+      if (temp && (mflags & MATCH_QUOTED) == 0)
+       {
+         tt = quote_escapes (temp);
+         free (temp);
+         temp = tt;
+       }
+      break;
+#if defined (ARRAY_VARS)
+    case VT_ARRAYVAR:
+      temp = assoc_p (v) ? assoc_patsub (assoc_cell (v), p, rep, mflags)
+                        : array_patsub (array_cell (v), p, rep, mflags);
+      /* Don't call quote_escapes anymore; array_patsub calls
+        array_quote_escapes as appropriate before adding the
+        space separators; ditto for assoc_patsub. */
+      break;
+#endif
+    }
+
+  FREE (pat);
+  FREE (rep);
+  free (lpatsub);
+
+  return temp;
+}
+
+/****************************************************************/
+/*                                                             */
+/*   Functions to perform case modification on variable values  */
+/*                                                             */
+/****************************************************************/
+
+/* Do case modification on the positional parameters. */
+
+static char *
+pos_params_modcase (string, pat, modop, mflags)
+     char *string, *pat;
+     int modop;
+     int mflags;
+{
+  WORD_LIST *save, *params;
+  WORD_DESC *w;
+  char *ret;
+  int pchar, qflags;
+
+  save = params = list_rest_of_args ();
+  if (save == 0)
+    return ((char *)NULL);
+
+  for ( ; params; params = params->next)
+    {
+      ret = sh_modcase (params->word->word, pat, modop);
+      w = alloc_word_desc ();
+      w->word = ret ? ret : savestring ("");
+      dispose_word (params->word);
+      params->word = w;
+    }
+
+  pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
+  qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
+
+  ret = string_list_pos_params (pchar, save, qflags);
+  dispose_words (save);
+
+  return (ret);
+}
+
+/* Perform case modification on VALUE, which is the expansion of
+   VARNAME.  MODSPEC is an expression supplying the type of modification
+   to perform.  QUOTED is a flags word containing the type of quoting
+   currently in effect. */
+static char *
+parameter_brace_casemod (varname, value, ind, modspec, patspec, quoted, flags)
+     char *varname, *value;
+     int ind, modspec;
+     char *patspec;
+     int quoted, flags;
+{
+  int vtype, starsub, modop, mflags, x;
+  char *val, *temp, *pat, *p, *lpat, *tt;
+  SHELL_VAR *v;
+
+  if (value == 0)
+    return ((char *)NULL);
+
+  this_command_name = varname;
+
+  vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
+  if (vtype == -1)
+    return ((char *)NULL);
+
+  starsub = vtype & VT_STARSUB;
+  vtype &= ~VT_STARSUB;
+
+  modop = 0;
+  mflags = 0;
+  if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+    mflags |= MATCH_QUOTED;
+  if (starsub)
+    mflags |= MATCH_STARSUB;
+  
+  p = patspec;
+  if (modspec == '^')
+    {
+      x = p && p[0] == modspec;
+      modop = x ? CASE_UPPER : CASE_UPFIRST;
+      p += x;
+    }
+  else if (modspec == ',')
+    {
+      x = p && p[0] == modspec;
+      modop = x ? CASE_LOWER : CASE_LOWFIRST;
+      p += x;
+    }
+  else if (modspec == '~')
+    {
+      x = p && p[0] == modspec;
+      modop = x ? CASE_TOGGLEALL : CASE_TOGGLE;
+      p += x;
+    }
+    
+  lpat = p ? savestring (p) : 0;
+  /* Perform the same expansions on the pattern as performed by the
+     pattern removal expansions.  FOR LATER */
+  pat = lpat ? getpattern (lpat, quoted, 1) : 0;
+
+  /* OK, now we do the case modification. */
+  switch (vtype)
+    {
+    case VT_VARIABLE:
+    case VT_ARRAYMEMBER:
+      temp = sh_modcase (val, pat, modop);
+      if (vtype == VT_VARIABLE)
+       FREE (val);
+      if (temp)
+       {
+         tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
+         free (temp);
+         temp = tt;
+       }
+      break;
+
+    case VT_POSPARMS:
+      temp = pos_params_modcase (val, pat, modop, mflags);
+      if (temp && (mflags & MATCH_QUOTED)  == 0)
+       {
+         tt = quote_escapes (temp);
+         free (temp);
+         temp = tt;
+       }
       break;
+
 #if defined (ARRAY_VARS)
     case VT_ARRAYVAR:
-      temp = array_pat_subst (array_cell (v), p, rep, mflags);
+      temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags)
+                        : array_modcase (array_cell (v), pat, modop, mflags);
+      /* Don't call quote_escapes; array_modcase calls array_quote_escapes
+        as appropriate before adding the space separators; ditto for
+        assoc_modcase. */
       break;
 #endif
     }
 
-  if (val && v && array_p (v) && vtype == VT_ARRAYMEMBER)
-    free (val);
+  FREE (pat);
+  free (lpat);
+
+  return temp;
+}
+
+/* Check for unbalanced parens in S, which is the contents of $(( ... )).  If
+   any occur, this must be a nested command substitution, so return 0.
+   Otherwise, return 1.  A valid arithmetic expression must always have a
+   ( before a matching ), so any cases where there are more right parens
+   means that this must not be an arithmetic expression, though the parser
+   will not accept it without a balanced total number of parens. */
+static int
+chk_arithsub (s, len)
+     const char *s;
+     int len;
+{
+  int i, count;
+  DECLARE_MBSTATE;
+
+  i = count = 0;
+  while (i < len)
+    {
+      if (s[i] == LPAREN)
+       count++;
+      else if (s[i] == RPAREN)
+       {
+         count--;
+         if (count < 0)
+           return 0;
+       }
+
+      switch (s[i])
+       {
+       default:
+         ADVANCE_CHAR (s, len, i);
+         break;
+
+       case '\\':
+         i++;
+         if (s[i])
+           ADVANCE_CHAR (s, len, i);
+         break;
+
+       case '\'':
+         i = skip_single_quoted (s, len, ++i, 0);
+         break;
 
-  FREE (pat);
-  FREE (rep);
+       case '"':
+         i = skip_double_quoted ((char *)s, len, ++i, 0);
+         break;
+       }
+    }
 
-  return temp;
+  return (count == 0);
 }
 
 /****************************************************************/
@@ -4131,48 +7923,84 @@ parameter_brace_patsub (varname, value, patsub, quoted)
 /*                                                             */
 /****************************************************************/
 
-/* ${[#][!]name[[:]#[#]%[%]-=?+[word][:e1[:e2]]]} */
-static char *
-parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_dollar_at)
+/* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
+static WORD_DESC *
+parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at)
      char *string;
-     int *indexp, quoted, *quoted_dollar_atp, *contains_dollar_at;
+     int *indexp, quoted, pflags, *quoted_dollar_atp, *contains_dollar_at;
 {
   int check_nullness, var_is_set, var_is_null, var_is_special;
-  int want_substring, want_indir, want_patsub;
+  int want_substring, want_indir, want_patsub, want_casemod;
   char *name, *value, *temp, *temp1;
-  int t_index, sindex, c, number;
+  WORD_DESC *tdesc, *ret;
+  int t_index, sindex, c, tflag, modspec, all_element_arrayref;
+  intmax_t number;
+  arrayind_t ind;
 
-  value = (char *)NULL;
+  temp = temp1 = value = (char *)NULL;
   var_is_set = var_is_null = var_is_special = check_nullness = 0;
-  want_substring = want_indir = want_patsub = 0;
+  want_substring = want_indir = want_patsub = want_casemod = 0;
+
+  all_element_arrayref = 0;
 
   sindex = *indexp;
   t_index = ++sindex;
-  name = string_extract (string, &t_index, "#%:-=?+/}", 1);
+  /* ${#var} doesn't have any of the other parameter expansions on it. */
+  if (string[t_index] == '#' && legal_variable_starter (string[t_index+1]))            /* {{ */
+    name = string_extract (string, &t_index, "}", SX_VARNAME);
+  else
+#if defined (CASEMOD_EXPANSIONS)
+    /* To enable case-toggling expansions using the `~' operator character
+       change the 1 to 0. */
+#  if defined (CASEMOD_CAPCASE)
+    name = string_extract (string, &t_index, "#%^,~:-=?+/@}", SX_VARNAME);
+#  else
+    name = string_extract (string, &t_index, "#%^,:-=?+/@}", SX_VARNAME);
+#  endif /* CASEMOD_CAPCASE */
+#else
+    name = string_extract (string, &t_index, "#%:-=?+/@}", SX_VARNAME);
+#endif /* CASEMOD_EXPANSIONS */
+
+  /* Handle ${@[stuff]} now that @ is a word expansion operator.  Not exactly
+     the cleanest code ever. */
+  if (*name == 0 && sindex == t_index && string[sindex] == '@')
+    {
+      name = (char *)xrealloc (name, 2);
+      name[0] = '@';
+      name[1] = '\0';
+      t_index++;
+    }
+  else if (*name == '!' && t_index > sindex && string[t_index] == '@' && string[t_index+1] == '}')
+    {
+      name = (char *)xrealloc (name, t_index - sindex + 2);
+      name[t_index - sindex] = '@';
+      name[t_index - sindex + 1] = '\0';
+      t_index++;
+    }
+
+  ret = 0;
+  tflag = 0;
+
+  ind = INTMAX_MIN;
 
   /* If the name really consists of a special variable, then make sure
      that we have the entire name.  We don't allow indirect references
-     to special variables except `#', `?', `@' and `*'. */
-  if ((sindex == t_index &&
-       (string[t_index] == '-' ||
-        string[t_index] == '?' ||
-        string[t_index] == '#')) ||
-      (sindex == t_index - 1 && string[sindex] == '!' &&
-        (string[t_index] == '#' ||
-         string[t_index] == '?' ||
-         string[t_index] == '@' ||
-         string[t_index] == '*')))
+     to special variables except `#', `?', `@' and `*'.  This clause is
+     designed to handle ${#SPECIAL} and ${!SPECIAL}, not anything more
+     general. */
+  if ((sindex == t_index && VALID_SPECIAL_LENGTH_PARAM (string[t_index])) ||
+      (sindex == t_index && string[sindex] == '#' && VALID_SPECIAL_LENGTH_PARAM (string[sindex + 1])) ||
+      (sindex == t_index - 1 && string[sindex] == '!' && VALID_INDIR_PARAM (string[t_index])))
     {
       t_index++;
-      free (name);
-      temp1 = string_extract (string, &t_index, "#%:-=?+/}", 0);
-      name = xmalloc (3 + (strlen (temp1)));
+      temp1 = string_extract (string, &t_index, "#%:-=?+/@}", 0);
+      name = (char *)xrealloc (name, 3 + (strlen (temp1)));
       *name = string[sindex];
       if (string[sindex] == '!')
        {
-          /* indirect reference of $#, $?, $@, or $* */
-          name[1] = string[sindex + 1];
-          strcpy (name + 2, temp1);
+         /* indirect reference of $#, $?, $@, or $* */
+         name[1] = string[sindex + 1];
+         strcpy (name + 2, temp1);
        }
       else     
        strcpy (name + 1, temp1);
@@ -4189,7 +8017,7 @@ parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_doll
      characters, move past it as normal.  If not, assume that
      a substring specification is being given, and do not move
      past it. */
-  if (c == ':' && member (string[sindex], "-=?+"))
+  if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
     {
       check_nullness++;
       if (c = string[sindex])
@@ -4197,17 +8025,24 @@ parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_doll
     }
   else if (c == ':' && string[sindex] != RBRACE)
     want_substring = 1;
-  else if (c == '/' && string[sindex] != RBRACE)
+  else if (c == '/' /* && string[sindex] != RBRACE */) /* XXX */
     want_patsub = 1;
+#if defined (CASEMOD_EXPANSIONS)
+  else if (c == '^' || c == ',' || c == '~')
+    {
+      modspec = c;
+      want_casemod = 1;
+    }
+#endif
 
   /* Catch the valid and invalid brace expressions that made it through the
      tests above. */
   /* ${#-} is a valid expansion and means to take the length of $-.
      Similarly for ${#?} and ${##}... */
   if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
-       member (c, "-?#") && string[sindex] == RBRACE)
+       VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
     {
-      name = xrealloc (name, 3);
+      name = (char *)xrealloc (name, 3);
       name[1] = c;
       name[2] = '\0';
       c = string[sindex++];
@@ -4218,42 +8053,55 @@ parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_doll
        member (c, "%:=+/") && string[sindex] == RBRACE)
     {
       temp = (char *)NULL;
-      goto bad_substitution;
+      goto bad_substitution;   /* XXX - substitution error */
     }
 
   /* Indirect expansion begins with a `!'.  A valid indirect expansion is
      either a variable name, one of the positional parameters or a special
      variable that expands to one of the positional parameters. */
   want_indir = *name == '!' &&
-    (legal_variable_starter (name[1]) || digit (name[1]) || member (name[1], "#?@*"));
+    (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
+                                       || VALID_INDIR_PARAM (name[1]));
 
   /* Determine the value of this variable. */
 
   /* Check for special variables, directly referenced. */
-  if ((digit (*name) && all_digits (name)) ||
-      (name[1] == '\0' && member (*name, "#-?$!@*")) ||
-      (want_indir && name[2] == '\0' && member (name[1], "#?@*")))
+  if (SPECIAL_VAR (name, want_indir))
     var_is_special++;
 
   /* Check for special expansion things, like the length of a parameter */
   if (*name == '#' && name[1])
     {
       /* If we are not pointing at the character just after the
-         closing brace, then we haven't gotten all of the name.
-         Since it begins with a special character, this is a bad
-         substitution.  Also check NAME for validity before trying
-         to go on. */
+        closing brace, then we haven't gotten all of the name.
+        Since it begins with a special character, this is a bad
+        substitution.  Also check NAME for validity before trying
+        to go on. */
       if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
        {
          temp = (char *)NULL;
-         goto bad_substitution;
+         goto bad_substitution;        /* substitution error */
        }
 
       number = parameter_brace_expand_length (name);
+      if (number == INTMAX_MIN && unbound_vars_is_error)
+       {
+         last_command_exit_value = EXECUTION_FAILURE;
+         err_unboundvar (name+1);
+         free (name);
+         return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
+       }
       free (name);
 
       *indexp = sindex;
-      return ((number < 0) ? &expand_param_error : itos (number));
+      if (number < 0)
+        return (&expand_wdesc_error);
+      else
+       {
+         ret = alloc_word_desc ();
+         ret->word = itos (number);
+         return ret;
+       }
     }
 
   /* ${@} is identical to $@. */
@@ -4264,148 +8112,341 @@ parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_doll
 
       if (contains_dollar_at)
        *contains_dollar_at = 1;
+
+      tflag |= W_DOLLARAT;
+    }
+
+  /* Process ${!PREFIX*} expansion. */
+  if (want_indir && string[sindex - 1] == RBRACE &&
+      (string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
+      legal_variable_starter ((unsigned char) name[1]))
+    {
+      char **x;
+      WORD_LIST *xlist;
+
+      temp1 = savestring (name + 1);
+      number = strlen (temp1);
+      temp1[number - 1] = '\0';
+      x = all_variables_matching_prefix (temp1);
+      xlist = strvec_to_word_list (x, 0, 0);
+      if (string[sindex - 2] == '*')
+       temp = string_list_dollar_star (xlist);
+      else
+       {
+         temp = string_list_dollar_at (xlist, quoted, 0);
+         if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
+           *quoted_dollar_atp = 1;
+         if (contains_dollar_at)
+           *contains_dollar_at = 1;
+
+         tflag |= W_DOLLARAT;
+       }
+      free (x);
+      dispose_words (xlist);
+      free (temp1);
+      *indexp = sindex;
+
+      free (name);
+
+      ret = alloc_word_desc ();
+      ret->word = temp;
+      ret->flags = tflag;      /* XXX */
+      return ret;
     }
 
+#if defined (ARRAY_VARS)      
+  /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */
+  if (want_indir && string[sindex - 1] == RBRACE &&
+      string[sindex - 2] == ']' && valid_array_reference (name+1, 0))
+    {
+      char *x, *x1;
+
+      temp1 = savestring (name + 1);
+      x = array_variable_name (temp1, &x1, (int *)0);  /* [ */
+      FREE (x);
+      if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == ']')
+       {
+         temp = array_keys (temp1, quoted);    /* handles assoc vars too */
+         if (x1[0] == '@')
+           {
+             if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
+               *quoted_dollar_atp = 1;
+             if (contains_dollar_at)
+               *contains_dollar_at = 1;
+
+             tflag |= W_DOLLARAT;
+           }       
+
+         free (name);
+         free (temp1);
+         *indexp = sindex;
+
+         ret = alloc_word_desc ();
+         ret->word = temp;
+         ret->flags = tflag;   /* XXX */
+         return ret;
+       }
+
+      free (temp1);
+    }
+#endif /* ARRAY_VARS */
+      
   /* Make sure that NAME is valid before trying to go on. */
   if (valid_brace_expansion_word (want_indir ? name + 1 : name,
                                        var_is_special) == 0)
     {
       temp = (char *)NULL;
-      goto bad_substitution;
+      goto bad_substitution;           /* substitution error */
     }
 
   if (want_indir)
-    temp = parameter_brace_expand_indir (name + 1, var_is_special, quoted);
-  else
-    temp = parameter_brace_expand_word (name, var_is_special, quoted);
-
-#if defined (ARRAY_VARS)
-#if 0
-  if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && valid_array_reference (name))
-#else
-  if (valid_array_reference (name))
-#endif
     {
-      temp1 = strchr (name, '[');
-      if (temp1 && temp1[1] == '@' && temp1[2] == ']')
-        {
-         if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
-           *quoted_dollar_atp = 1;
-         if (contains_dollar_at)
-           *contains_dollar_at = 1;
-       }       /* [ */
-      /* ${array[*]}, when unquoted, should be treated like ${array[@]},
-        which should result in separate words even when IFS is unset. */
-      if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0)
+      tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at);
+      if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
        {
-         if (contains_dollar_at)
-           *contains_dollar_at = 1;
+         temp = (char *)NULL;
+         goto bad_substitution;
        }
+      /* Turn off the W_ARRAYIND flag because there is no way for this function
+        to return the index we're supposed to be using. */
+      if (tdesc && tdesc->flags)
+       tdesc->flags &= ~W_ARRAYIND;
+    }
+  else
+    tdesc = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND|(pflags&(PF_NOSPLIT2|PF_ASSIGNRHS)), &ind);
+
+  if (tdesc)
+    {
+      temp = tdesc->word;
+      tflag = tdesc->flags;
+      dispose_word_desc (tdesc);
+    }
+  else
+    temp = (char  *)0;
+
+  if (temp == &expand_param_error || temp == &expand_param_fatal)
+    {
+      FREE (name);
+      FREE (value);
+      return (temp == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
+    }
+
+#if defined (ARRAY_VARS)
+  if (valid_array_reference (name, 0))
+    {
+      int qflags;
+      char *t;
+
+      qflags = quoted;
+      /* If in a context where word splitting will not take place, treat as
+        if double-quoted.  Has effects with $* and ${array[*]} */
+      if (pflags & PF_ASSIGNRHS)
+       qflags |= Q_DOUBLE_QUOTES;
+      chk_atstar (name, qflags, quoted_dollar_atp, contains_dollar_at);
+      /* We duplicate a little code here */
+      t = mbschr (name, '[');
+      if (t && ALL_ELEMENT_SUB (t[1]) && t[2] == ']')
+        all_element_arrayref = 1;
     }
 #endif
 
   var_is_set = temp != (char *)0;
   var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
+  /* XXX - this may not need to be restricted to special variables */
+  if (check_nullness)
+    var_is_null |= var_is_set && var_is_special && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp);
 
   /* Get the rest of the stuff inside the braces. */
   if (c && c != RBRACE)
     {
       /* Extract the contents of the ${ ... } expansion
-         according to the Posix.2 rules. */
-      value = extract_dollar_brace_string (string, &sindex, quoted);
+        according to the Posix.2 rules. */
+      value = extract_dollar_brace_string (string, &sindex, quoted, (c == '%' || c == '#' || c =='/' || c == '^' || c == ',' || c ==':') ? SX_POSIXEXP|SX_WORD : SX_WORD);
       if (string[sindex] == RBRACE)
-        sindex++;
+       sindex++;
       else
-       goto bad_substitution;
+       goto bad_substitution;          /* substitution error */
     }
   else
     value = (char *)NULL;
 
   *indexp = sindex;
 
+  /* All the cases where an expansion can possibly generate an unbound
+     variable error. */
+  if (want_substring || want_patsub || want_casemod || c == '#' || c == '%' || c == RBRACE)
+    {
+      if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1]) && all_element_arrayref == 0)
+       {
+         last_command_exit_value = EXECUTION_FAILURE;
+         err_unboundvar (name);
+         FREE (value);
+         FREE (temp);
+         free (name);
+         return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
+       }
+    }
+    
   /* If this is a substring spec, process it and add the result. */
   if (want_substring)
     {
-      temp1 = parameter_brace_substring (name, temp, value, quoted);
+      temp1 = parameter_brace_substring (name, temp, ind, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
       FREE (name);
       FREE (value);
       FREE (temp);
-      return (temp1);
+
+      if (temp1 == &expand_param_error)
+       return (&expand_wdesc_error);
+      else if (temp1 == &expand_param_fatal)
+       return (&expand_wdesc_fatal);
+
+      ret = alloc_word_desc ();
+      ret->word = temp1;
+      /* We test quoted_dollar_atp because we want variants with double-quoted
+        "$@" to take a different code path. In fact, we make sure at the end
+        of expand_word_internal that we're only looking at these flags if
+        quoted_dollar_at == 0. */
+      if (temp1 && 
+          (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
+         QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+       ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
+      return ret;
     }
   else if (want_patsub)
     {
-      temp1 = parameter_brace_patsub (name, temp, value, quoted);
+      temp1 = parameter_brace_patsub (name, temp, ind, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
+      FREE (name);
+      FREE (value);
+      FREE (temp);
+
+      if (temp1 == &expand_param_error)
+       return (&expand_wdesc_error);
+      else if (temp1 == &expand_param_fatal)
+       return (&expand_wdesc_fatal);
+
+      ret = alloc_word_desc ();
+      ret->word = temp1;
+      if (temp1 && 
+          (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
+         QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+       ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
+      return ret;
+    }
+#if defined (CASEMOD_EXPANSIONS)
+  else if (want_casemod)
+    {
+      temp1 = parameter_brace_casemod (name, temp, ind, modspec, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
       FREE (name);
       FREE (value);
       FREE (temp);
-      return (temp1);
+
+      if (temp1 == &expand_param_error)
+       return (&expand_wdesc_error);
+      else if (temp1 == &expand_param_fatal)
+       return (&expand_wdesc_fatal);
+
+      ret = alloc_word_desc ();
+      ret->word = temp1;
+      if (temp1 &&
+          (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
+         QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+       ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
+      return ret;
     }
+#endif
 
   /* Do the right thing based on which character ended the variable name. */
   switch (c)
     {
     default:
     case '\0':
-    bad_substitution:
-      report_error ("%s: bad substitution", string ? string : "??");
+bad_substitution:
+      last_command_exit_value = EXECUTION_FAILURE;
+      report_error (_("%s: bad substitution"), string ? string : "??");
       FREE (value);
       FREE (temp);
       free (name);
-      return &expand_param_error;
+      if (shell_compatibility_level <= 43)
+       return &expand_wdesc_error;
+      else
+       return ((posixly_correct && interactive_shell == 0) ? &expand_wdesc_fatal : &expand_wdesc_error);
 
     case RBRACE:
-      if (var_is_set == 0 && unbound_vars_is_error)
-        {
-         report_error ("%s: unbound variable", name);
-         FREE (value);
-         FREE (temp);
-         free (name);
+      break;
+
+    case '@':
+      temp1 = parameter_brace_transform (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
+      free (temp);
+      free (value);
+      free (name);
+      if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
+       {
          last_command_exit_value = EXECUTION_FAILURE;
-         return (interactive_shell ? &expand_param_error : &expand_param_fatal);
+         report_error (_("%s: bad substitution"), string ? string : "??");
+         return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
        }
-      break;
+
+      ret = alloc_word_desc ();
+      ret->word = temp1;
+      if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+       ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
+      return ret;
 
     case '#':  /* ${param#[#]pattern} */
     case '%':  /* ${param%[%]pattern} */
       if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
-        {
-          FREE (value);
+       {
+         FREE (value);
          break;
-        }
-      if ((name[0] == '@' || name[0] == '*') && name[1] == '\0')
-       temp1 = parameter_list_remove_pattern (value, name[0], c, quoted);
-#if defined (ARRAY_VARS)
-      else if (valid_array_reference (name))
-       temp1 = array_remove_pattern (value, name, temp, c, quoted);
-#endif
-      else
-       temp1 = parameter_brace_remove_pattern (value, temp, c, quoted);
+       }
+      temp1 = parameter_brace_remove_pattern (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
       free (temp);
       free (value);
-      temp = temp1;
-      break;
+      free (name);
+
+      ret = alloc_word_desc ();
+      ret->word = temp1;
+      if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+       ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
+      return ret;
 
     case '-':
     case '=':
     case '?':
     case '+':
       if (var_is_set && var_is_null == 0)
-        {
-         /* We don't want the value of the named variable for
-            anything, just the value of the right hand side. */
+       {
+         /* If the operator is `+', we don't want the value of the named
+            variable for anything, just the value of the right hand side. */
          if (c == '+')
            {
+             /* XXX -- if we're double-quoted and the named variable is "$@",
+                       we want to turn off any special handling of "$@" --
+                       we're not using it, so whatever is on the rhs applies. */
+             if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
+               *quoted_dollar_atp = 0;
+             if (contains_dollar_at)
+               *contains_dollar_at = 0;
+
              FREE (temp);
              if (value)
-               {
-                 temp = parameter_brace_expand_rhs (name, value, c,
-                                                    quoted,
-                                                    quoted_dollar_atp,
-                                                    contains_dollar_at);
+               {
+                 /* From Posix discussion on austin-group list.  Issue 221
+                    requires that backslashes escaping `}' inside
+                    double-quoted ${...} be removed. */
+                 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+                   quoted |= Q_DOLBRACE;
+                 ret = parameter_brace_expand_rhs (name, value, c,
+                                                   quoted,
+                                                   pflags,
+                                                   quoted_dollar_atp,
+                                                   contains_dollar_at);
+                 /* XXX - fix up later, esp. noting presence of
+                          W_HASQUOTEDNULL in ret->flags */
                  free (value);
                }
              else
-               temp = (char *)NULL;
+               temp = (char *)NULL;
            }
          else
            {
@@ -4414,37 +8455,63 @@ parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_doll
          /* Otherwise do nothing; just use the value in TEMP. */
        }
       else     /* VAR not set or VAR is NULL. */
-        {
+       {
          FREE (temp);
          temp = (char *)NULL;
          if (c == '=' && var_is_special)
            {
-             report_error ("$%s: cannot assign in this way", name);
+             last_command_exit_value = EXECUTION_FAILURE;
+             report_error (_("$%s: cannot assign in this way"), name);
              free (name);
              free (value);
-             return &expand_param_error;
+             return &expand_wdesc_error;
            }
          else if (c == '?')
            {
              parameter_brace_expand_error (name, value);
-             return (interactive ? &expand_param_error : &expand_param_fatal);
+             return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
            }
          else if (c != '+')
-           temp = parameter_brace_expand_rhs (name, value, c, quoted,
-                                              quoted_dollar_atp,
-                                              contains_dollar_at);
+           {
+             /* XXX -- if we're double-quoted and the named variable is "$@",
+                       we want to turn off any special handling of "$@" --
+                       we're not using it, so whatever is on the rhs applies. */
+             if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
+               *quoted_dollar_atp = 0;
+             if (contains_dollar_at)
+               *contains_dollar_at = 0;
+
+             /* From Posix discussion on austin-group list.  Issue 221 requires
+                that backslashes escaping `}' inside double-quoted ${...} be
+                removed. */
+             if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+               quoted |= Q_DOLBRACE;
+             ret = parameter_brace_expand_rhs (name, value, c, quoted, pflags,
+                                               quoted_dollar_atp,
+                                               contains_dollar_at);
+             /* XXX - fix up later, esp. noting presence of
+                      W_HASQUOTEDNULL in tdesc->flags */
+           }
          free (value);
        }
+
       break;
     }
   free (name);
-  return (temp);
+
+  if (ret == 0)
+    {
+      ret = alloc_word_desc ();
+      ret->flags = tflag;
+      ret->word = temp;
+    }
+  return (ret);
 }
 
 /* Expand a single ${xxx} expansion.  The braces are optional.  When
    the braces are used, parameter_brace_expand() does the work,
    possibly calling param_expand recursively. */
-static char *
+static WORD_DESC *
 param_expand (string, sindex, quoted, expanded_something,
              contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
              pflags)
@@ -4452,15 +8519,22 @@ param_expand (string, sindex, quoted, expanded_something,
      int *sindex, quoted, *expanded_something, *contains_dollar_at;
      int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
 {
-  char *temp, *temp1;
-  int zindex, number, c, t_index, expok;
+  char *temp, *temp1, uerror[3], *savecmd;
+  int zindex, t_index, expok;
+  unsigned char c;
+  intmax_t number;
   SHELL_VAR *var;
-  WORD_LIST *list, *tlist;
+  WORD_LIST *list;
+  WORD_DESC *tdesc, *ret;
+  int tflag;
 
+/*itrace("param_expand: `%s' pflags = %d", string+*sindex, pflags);*/
   zindex = *sindex;
   c = string[++zindex];
 
   temp = (char *)NULL;
+  ret = tdesc = (WORD_DESC *)NULL;
+  tflag = 0;
 
   /* Do simple cases first. Switch on what follows '$'. */
   switch (c)
@@ -4476,14 +8550,23 @@ param_expand (string, sindex, quoted, expanded_something,
     case '7':
     case '8':
     case '9':
-      temp1 = dollar_vars[digit_value (c)];
+      temp1 = dollar_vars[TODIGIT (c)];
       if (unbound_vars_is_error && temp1 == (char *)NULL)
        {
-         report_error ("$%c: unbound variable", c);
+         uerror[0] = '$';
+         uerror[1] = c;
+         uerror[2] = '\0';
          last_command_exit_value = EXECUTION_FAILURE;
-         return (interactive_shell ? &expand_param_error : &expand_param_fatal);
+         err_unboundvar (uerror);
+         return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
        }
-      temp = temp1 ? savestring (temp1) : (char *)NULL;
+      if (temp1)
+       temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+                 ? quote_string (temp1)
+                 : quote_escapes (temp1);
+      else
+       temp = (char *)NULL;
+
       break;
 
     /* $$ -- pid of the invoking shell. */
@@ -4518,43 +8601,97 @@ param_expand (string, sindex, quoted, expanded_something,
          temp = (char *)NULL;
          if (unbound_vars_is_error)
            {
-             report_error ("$%c: unbound variable", c);
+             uerror[0] = '$';
+             uerror[1] = c;
+             uerror[2] = '\0';
              last_command_exit_value = EXECUTION_FAILURE;
-             return (interactive_shell ? &expand_param_error : &expand_param_fatal);
+             err_unboundvar (uerror);
+             return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
            }
        }
       else
-       temp = itos ((int)last_asynchronous_pid);
+       temp = itos (last_asynchronous_pid);
       break;
 
     /* The only difference between this and $@ is when the arg is quoted. */
     case '*':          /* `$*' */
       list = list_rest_of_args ();
 
+#if 0
+      /* According to austin-group posix proposal by Geoff Clare in
+        <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
+
+       "The shell shall write a message to standard error and
+        immediately exit when it tries to expand an unset parameter
+        other than the '@' and '*' special parameters."
+      */
+
+      if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
+       {
+         uerror[0] = '$';
+         uerror[1] = '*';
+         uerror[2] = '\0';
+         last_command_exit_value = EXECUTION_FAILURE;
+         err_unboundvar (uerror);
+         return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
+       }
+#endif
+
       /* If there are no command-line arguments, this should just
         disappear if there are other characters in the expansion,
         even if it's quoted. */
       if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
        temp = (char *)NULL;
-      else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+      else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
        {
          /* If we have "$*" we want to make a string of the positional
             parameters, separated by the first character of $IFS, and
             quote the whole string, including the separators.  If IFS
             is unset, the parameters are separated by ' '; if $IFS is
             null, the parameters are concatenated. */
-         temp = string_list_dollar_star (list);
-         temp1 = quote_string (temp);
-         free (temp);
-         temp = temp1;
+         temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list) : string_list (list);
+         if (temp)
+           {
+             temp1 = (quoted & Q_DOUBLE_QUOTES) ? quote_string (temp) : temp;
+             if (*temp == 0)
+               tflag |= W_HASQUOTEDNULL;
+             if (temp != temp1)
+               free (temp);
+             temp = temp1;
+           }
        }
       else
-        {
-          /* If the $* is not quoted it is identical to $@ */
-          temp = string_list_dollar_at (list, quoted);
-          if (contains_dollar_at)
-            *contains_dollar_at = 1;
-        }
+       {
+         /* We check whether or not we're eventually going to split $* here,
+            for example when IFS is empty and we are processing the rhs of
+            an assignment statement.  In that case, we don't separate the
+            arguments at all.  Otherwise, if the $* is not quoted it is
+            identical to $@ */
+#  if defined (HANDLE_MULTIBYTE)
+         if (expand_no_split_dollar_star && ifs_firstc[0] == 0)
+#  else
+         if (expand_no_split_dollar_star && ifs_firstc == 0)
+#  endif
+           temp = string_list_dollar_star (list);
+         else
+           {
+             temp = string_list_dollar_at (list, quoted, 0);
+             if (quoted == 0 && (ifs_is_set == 0 || ifs_is_null))
+               tflag |= W_SPLITSPACE;
+             /* If we're not quoted but we still don't want word splitting, make
+                we quote the IFS characters to protect them from splitting (e.g.,
+                when $@ is in the string as well). */
+             else if (temp && quoted == 0 && ifs_is_set && (pflags & PF_ASSIGNRHS))
+               {
+                 temp1 = quote_string (temp);
+                 free (temp);
+                 temp = temp1;
+               }
+           }
+
+         if (expand_no_split_dollar_star == 0 && contains_dollar_at)
+           *contains_dollar_at = 1;
+       }
 
       dispose_words (list);
       break;
@@ -4566,11 +8703,32 @@ param_expand (string, sindex, quoted, expanded_something,
     case '@':          /* `$@' */
       list = list_rest_of_args ();
 
+#if 0
+      /* According to austin-group posix proposal by Geoff Clare in
+        <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
+
+       "The shell shall write a message to standard error and
+        immediately exit when it tries to expand an unset parameter
+        other than the '@' and '*' special parameters."
+      */
+
+      if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
+       {
+         uerror[0] = '$';
+         uerror[1] = '@';
+         uerror[2] = '\0';
+         last_command_exit_value = EXECUTION_FAILURE;
+         err_unboundvar (uerror);
+         return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
+       }
+#endif
+
       /* We want to flag the fact that we saw this.  We can't turn
         off quoting entirely, because other characters in the
         string might need it (consider "\"$@\""), but we need some
         way to signal that the final split on the first character
         of $IFS should be done, even though QUOTED is 1. */
+      /* XXX - should this test include Q_PATQUOTE? */
       if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
        *quoted_dollar_at_p = 1;
       if (contains_dollar_at)
@@ -4581,43 +8739,60 @@ param_expand (string, sindex, quoted, expanded_something,
         We also want to make sure that splitting is done no matter what --
         according to POSIX.2, this expands to a list of the positional
         parameters no matter what IFS is set to. */
-      temp = string_list_dollar_at (list, quoted);
-
+      /* XXX - what to do when in a context where word splitting is not
+        performed? Even when IFS is not the default, posix seems to imply
+        that we behave like unquoted $* ?  Maybe we should use PF_NOSPLIT2
+        here. */
+      /* XXX - bash-4.4/bash-5.0 passing PFLAGS */
+      temp = string_list_dollar_at (list, (pflags & PF_ASSIGNRHS) ? (quoted|Q_DOUBLE_QUOTES) : quoted, pflags);
+
+      tflag |= W_DOLLARAT;
       dispose_words (list);
       break;
 
     case LBRACE:
-      temp = parameter_brace_expand (string, &zindex, quoted,
-                                    quoted_dollar_at_p,
-                                    contains_dollar_at);
-      if (temp == &expand_param_error || temp == &expand_param_fatal)
-       return (temp);
+      tdesc = parameter_brace_expand (string, &zindex, quoted, pflags,
+                                     quoted_dollar_at_p,
+                                     contains_dollar_at);
+
+      if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
+       return (tdesc);
+      temp = tdesc ? tdesc->word : (char *)0;
 
       /* XXX */
-      /* quoted nulls should be removed if there is anything else
+      /* Quoted nulls should be removed if there is anything else
         in the string. */
       /* Note that we saw the quoted null so we can add one back at
         the end of this function if there are no other characters
-        in the string, discard TEMP, and go on. */
-      if (temp && QUOTED_NULL (temp))
+        in the string, discard TEMP, and go on.  The exception to
+        this is when we have "${@}" and $1 is '', since $@ needs
+        special handling. */
+      if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp))
        {
          if (had_quoted_null_p)
            *had_quoted_null_p = 1;
-         free (temp);
-         temp = (char *)NULL;
+         if (*quoted_dollar_at_p == 0)
+           {
+             free (temp);
+             tdesc->word = temp = (char *)NULL;
+           }
+           
        }
 
+      ret = tdesc;
       goto return0;
 
     /* Do command or arithmetic substitution. */
     case LPAREN:
       /* We have to extract the contents of this paren substitution. */
       t_index = zindex + 1;
-      temp = extract_command_subst (string, &t_index);
+      /* XXX - might want to check for string[t_index+2] == LPAREN and parse
+        as arithmetic substitution immediately. */
+      temp = extract_command_subst (string, &t_index, (pflags&PF_COMPLETE) ? SX_COMPLETE : 0);
       zindex = t_index;
 
       /* For Posix.2-style `$(( ))' arithmetic substitution,
-         extract the expression and pass it to the evaluator. */
+        extract the expression and pass it to the evaluator. */
       if (temp && *temp == LPAREN)
        {
          char *temp2;
@@ -4634,14 +8809,25 @@ param_expand (string, sindex, quoted, expanded_something,
          /* Cut off ending `)' */
          temp2[t_index] = '\0';
 
+         if (chk_arithsub (temp2, t_index) == 0)
+           {
+             free (temp2);
+#if 0
+             internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
+#endif
+             goto comsub;
+           }
+
          /* Expand variables found inside the expression. */
-         temp1 = maybe_expand_string (temp2, Q_DOUBLE_QUOTES, expand_string);
+         temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES|Q_ARITH);
          free (temp2);
 
 arithsub:
          /* No error messages. */
+         savecmd = this_command_name;
          this_command_name = (char *)NULL;
          number = evalexp (temp1, &expok);
+         this_command_name = savecmd;
          free (temp);
          free (temp1);
          if (expok == 0)
@@ -4649,17 +8835,26 @@ arithsub:
              if (interactive_shell == 0 && posixly_correct)
                {
                  last_command_exit_value = EXECUTION_FAILURE;
-                 return (&expand_param_fatal);
+                 return (&expand_wdesc_fatal);
                }
              else
-               return (&expand_param_error);
+               return (&expand_wdesc_error);
            }
          temp = itos (number);
          break;
        }
 
 comsub:
-      temp1 = command_substitute (temp, quoted);
+      if (pflags & PF_NOCOMSUB)
+       /* we need zindex+1 because string[zindex] == RPAREN */
+       temp1 = substring (string, *sindex, zindex+1);
+      else
+       {
+         tdesc = command_substitute (temp, quoted);
+         temp1 = tdesc ? tdesc->word : (char *)NULL;
+         if (tdesc)
+           dispose_word_desc (tdesc);
+       }
       FREE (temp);
       temp = temp1;
       break;
@@ -4667,13 +8862,20 @@ comsub:
     /* Do POSIX.2d9-style arithmetic substitution.  This will probably go
        away in a future bash release. */
     case '[':
-      /* We have to extract the contents of this arithmetic substitution. */
+      /* Extract the contents of this arithmetic substitution. */
       t_index = zindex + 1;
       temp = extract_arithmetic_subst (string, &t_index);
       zindex = t_index;
+      if (temp == 0)
+       {
+         temp = savestring (string);
+         if (expanded_something)
+           *expanded_something = 0;
+         goto return0;
+       }         
 
        /* Do initial variable expansion. */
-      temp1 = maybe_expand_string (temp, Q_DOUBLE_QUOTES, expand_string);
+      temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES|Q_ARITH);
 
       goto arithsub;
 
@@ -4689,7 +8891,7 @@ comsub:
       if (temp1 == 0 || *temp1 == '\0')
        {
          FREE (temp1);
-         temp = xmalloc (2);
+         temp = (char *)xmalloc (2);
          temp[0] = '$';
          temp[1] = '\0';
          if (expanded_something)
@@ -4700,31 +8902,69 @@ comsub:
       /* If the variable exists, return its value cell. */
       var = find_variable (temp1);
 
-      if (var && invisible_p (var) == 0 && value_cell (var))
+      if (var && invisible_p (var) == 0 && var_isset (var))
        {
 #if defined (ARRAY_VARS)
-         if (array_p (var))
+         if (assoc_p (var) || array_p (var))
            {
-             temp = array_reference (array_cell (var), 0);
+             temp = array_p (var) ? array_reference (array_cell (var), 0)
+                                  : assoc_reference (assoc_cell (var), "0");
              if (temp)
-               temp = quote_escapes (temp);
+               temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+                         ? quote_string (temp)
+                         : quote_escapes (temp);
+             else if (unbound_vars_is_error)
+               goto unbound_variable;
            }
          else
 #endif
-         temp = quote_escapes (value_cell (var));
-         free (temp1);
-         if (tempvar_p (var))          /* XXX */
            {
-             dispose_variable (var);   /* XXX */
-             var = (SHELL_VAR *)NULL;
+             temp = value_cell (var);
+
+             temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+                       ? quote_string (temp)
+                       : quote_escapes (temp);
            }
+
+         free (temp1);
+
          goto return0;
        }
+      else if (var && (invisible_p (var) || var_isset (var) == 0))
+       temp = (char *)NULL;
+      else if ((var = find_variable_last_nameref (temp1, 0)) && var_isset (var) && invisible_p (var) == 0)
+       {
+         temp = nameref_cell (var);
+#if defined (ARRAY_VARS)
+         if (temp && *temp && valid_array_reference (temp, 0))
+           {
+             tdesc = parameter_brace_expand_word (temp, SPECIAL_VAR (temp, 0), quoted, pflags, (arrayind_t *)NULL);
+             if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
+               return (tdesc);
+             ret = tdesc;
+             goto return0;
+           }
+         else
+#endif
+         /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */
+         if (temp && *temp && legal_identifier (temp) == 0)
+           {
+             last_command_exit_value = EXECUTION_FAILURE;
+             report_error (_("%s: invalid variable name for name reference"), temp);
+             return (&expand_wdesc_error);     /* XXX */
+           }
+         else
+           temp = (char *)NULL;
+       }
 
       temp = (char *)NULL;
 
+unbound_variable:
       if (unbound_vars_is_error)
-       report_error ("%s: unbound variable", temp1);
+       {
+         last_command_exit_value = EXECUTION_FAILURE;
+         err_unboundvar (temp1);
+       }
       else
        {
          free (temp1);
@@ -4734,8 +8974,8 @@ comsub:
       free (temp1);
       last_command_exit_value = EXECUTION_FAILURE;
       return ((unbound_vars_is_error && interactive_shell == 0)
-               ? &expand_param_fatal
-               : &expand_param_error);
+               ? &expand_wdesc_fatal
+               : &expand_wdesc_error);
     }
 
   if (string[zindex])
@@ -4743,7 +8983,21 @@ comsub:
 
 return0:
   *sindex = zindex;
-  return (temp);
+
+  if (ret == 0)
+    {
+      ret = alloc_word_desc ();
+      ret->flags = tflag;      /* XXX */
+      ret->word = temp;
+    }
+  return ret;
+}
+
+void
+invalidate_cached_quoted_dollar_at ()
+{
+  dispose_words (cached_quoted_dollar_at);
+  cached_quoted_dollar_at = 0;
 }
 
 /* Make a word list which is the result of parameter and variable
@@ -4754,6 +9008,10 @@ return0:
 
    QUOTED contains flag values defined in shell.h.
 
+   ISEXP is used to tell expand_word_internal that the word should be
+   treated as the result of an expansion.  This has implications for
+   how IFS characters in the word are treated.
+
    CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
    they point to an integer value which receives information about expansion.
    CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
@@ -4769,21 +9027,20 @@ return0:
 #define WHOLLY_QUOTED    2
 
 static WORD_LIST *
-expand_word_internal (word, quoted, contains_dollar_at, expanded_something)
+expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
      WORD_DESC *word;
-     int quoted;
+     int quoted, isexp;
      int *contains_dollar_at;
      int *expanded_something;
 {
   WORD_LIST *list;
   WORD_DESC *tword;
-  SHELL_VAR *var;
 
   /* The intermediate string that we build while expanding. */
   char *istring;
 
   /* The current size of the above object. */
-  int istring_size;
+  size_t istring_size;
 
   /* Index into ISTRING. */
   int istring_index;
@@ -4794,6 +9051,9 @@ expand_word_internal (word, quoted, contains_dollar_at, expanded_something)
   /* The text of WORD. */
   register char *string;
 
+  /* The size of STRING. */
+  size_t string_size;
+
   /* The index into STRING. */
   int sindex;
 
@@ -4805,44 +9065,87 @@ expand_word_internal (word, quoted, contains_dollar_at, expanded_something)
      string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
   int quoted_state;
 
+  /* State flags */
   int had_quoted_null;
-  int has_dollar_at;
+  int has_dollar_at, temp_has_dollar_at;
+  int split_on_spaces;
+  int tflag;
+  int pflags;                  /* flags passed to param_expand */
+  int mb_cur_max;
 
-  int expok;
+  int assignoff;               /* If assignment, offset of `=' */
 
-  register int c;              /* Current character. */
-  int number;                  /* Temporary number value. */
+  register unsigned char c;    /* Current character. */
   int t_index;                 /* For calls to string_extract_xxx. */
 
-  istring = xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
+  char twochars[2];
+
+  DECLARE_MBSTATE;
+
+  /* OK, let's see if we can optimize a common idiom: "$@" */
+  if (STREQ (word->word, "\"$@\"") &&
+      (word->flags == (W_HASDOLLAR|W_QUOTED)) &&
+      dollar_vars[1])          /* XXX - check IFS here as well? */
+    {
+      if (contains_dollar_at)
+       *contains_dollar_at = 1;
+      if (expanded_something)
+       *expanded_something = 1;
+      if (cached_quoted_dollar_at)
+       return (copy_word_list (cached_quoted_dollar_at));
+      list = list_rest_of_args ();
+      list = quote_list (list);
+      cached_quoted_dollar_at = copy_word_list (list);
+      return (list);
+    }
+
+  istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
   istring[istring_index = 0] = '\0';
   quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
+  split_on_spaces = 0;
   quoted_state = UNQUOTED;
 
   string = word->word;
   if (string == 0)
     goto finished_with_string;
+  mb_cur_max = MB_CUR_MAX;
+
+  /* Don't need the string length for the SADD... and COPY_ macros unless
+     multibyte characters are possible. */
+  string_size = (mb_cur_max > 1) ? strlen (string) : 1;
 
   if (contains_dollar_at)
     *contains_dollar_at = 0;
 
+  assignoff = -1;
+
   /* Begin the expansion. */
 
   for (sindex = 0; ;)
     {
       c = string[sindex];
 
-      /* Case on toplevel character. */
+      /* Case on top-level character. */
       switch (c)
        {
        case '\0':
          goto finished_with_string;
 
        case CTLESC:
-         temp = xmalloc (3);
-         temp[0] = CTLESC;
-         temp[1] = c = string[++sindex];
-         temp[2] = '\0';
+         sindex++;
+#if HANDLE_MULTIBYTE
+         if (mb_cur_max > 1 && string[sindex])
+           {
+             SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
+           }
+         else
+#endif
+           {
+             temp = (char *)xmalloc (3);
+             temp[0] = CTLESC;
+             temp[1] = c = string[sindex];
+             temp[2] = '\0';
+           }
 
 dollar_add_string:
          if (string[sindex])
@@ -4862,15 +9165,18 @@ add_string:
        case '<':
        case '>':
          {
-           if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || posixly_correct)
+           /* bash-4.4/bash-5.0
+              XXX - technically this should only be expanded at the start
+              of a word */
+           if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)) || posixly_correct)
              {
-               sindex--;
+               sindex--;       /* add_character: label increments sindex */
                goto add_character;
              }
            else
              t_index = sindex + 1; /* skip past both '<' and LPAREN */
 
-           temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index); /*))*/
+           temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index, 0); /*))*/
            sindex = t_index;
 
            /* If the process substitution specification is `<()', we want to
@@ -4885,37 +9191,196 @@ add_string:
          }
 #endif /* PROCESS_SUBSTITUTION */
 
+       case '=':
+         /* Posix.2 section 3.6.1 says that tildes following `=' in words
+            which are not assignment statements are not expanded.  If the
+            shell isn't in posix mode, though, we perform tilde expansion
+            on `likely candidate' unquoted assignment statements (flags
+            include W_ASSIGNMENT but not W_QUOTED).  A likely candidate
+            contains an unquoted :~ or =~.  Something to think about: we
+            now have a flag that says  to perform tilde expansion on arguments
+            to `assignment builtins' like declare and export that look like
+            assignment statements.  We now do tilde expansion on such words
+            even in POSIX mode. */     
+         if (word->flags & (W_ASSIGNRHS|W_NOTILDE))
+           {
+             if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
+               goto add_ifs_character;
+             else
+               goto add_character;
+           }
+         /* If we're not in posix mode or forcing assignment-statement tilde
+            expansion, note where the `=' appears in the word and prepare to
+            do tilde expansion following the first `='. */
+         if ((word->flags & W_ASSIGNMENT) &&
+             (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
+             assignoff == -1 && sindex > 0)
+           assignoff = sindex;
+         if (sindex == assignoff && string[sindex+1] == '~')   /* XXX */
+           word->flags |= W_ITILDE;
+#if 0
+         else if ((word->flags & W_ASSIGNMENT) &&
+                  (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
+                  string[sindex+1] == '~')
+           word->flags |= W_ITILDE;
+#endif
+
+         /* XXX - bash-4.4/bash-5.0 */
+         if (word->flags & W_ASSIGNARG)
+           word->flags |= W_ASSIGNRHS;         /* affects $@ */
+
+         if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
+           goto add_ifs_character;
+         else
+           goto add_character;
+
+       case ':':
+         if (word->flags & W_NOTILDE)
+           {
+             if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
+               goto add_ifs_character;
+             else
+               goto add_character;
+           }
+
+         if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) &&
+             string[sindex+1] == '~')
+           word->flags |= W_ITILDE;
+
+         if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
+           goto add_ifs_character;
+         else
+           goto add_character;
+
+       case '~':
+         /* If the word isn't supposed to be tilde expanded, or we're not
+            at the start of a word or after an unquoted : or = in an
+            assignment statement, we don't do tilde expansion.  If we don't want
+            tilde expansion when expanding words to be passed to the arithmetic
+            evaluator, remove the check for Q_ARITH. */
+         if ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
+             (sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
+             ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && ((quoted & Q_ARITH) == 0)))
+           {
+             word->flags &= ~W_ITILDE;
+             if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
+               goto add_ifs_character;
+             else
+               goto add_character;
+           }
+
+         if (word->flags & W_ASSIGNRHS)
+           tflag = 2;
+         else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
+           tflag = 1;
+         else
+           tflag = 0;
+
+         temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
+           
+         word->flags &= ~W_ITILDE;
+
+         if (temp && *temp && t_index > 0)
+           {
+             temp1 = bash_tilde_expand (temp, tflag);
+             if  (temp1 && *temp1 == '~' && STREQ (temp, temp1))
+               {
+                 FREE (temp);
+                 FREE (temp1);
+                 goto add_character;           /* tilde expansion failed */
+               }
+             free (temp);
+             temp = temp1;
+             sindex += t_index;
+             goto add_quoted_string;           /* XXX was add_string */
+           }
+         else
+           {
+             FREE (temp);
+             goto add_character;
+           }
+       
        case '$':
          if (expanded_something)
            *expanded_something = 1;
 
-         has_dollar_at = 0;
-         temp = param_expand (string, &sindex, quoted, expanded_something,
-                              &has_dollar_at, &quoted_dollar_at,
-                              &had_quoted_null, 0);
-
-         if (temp == &expand_param_error || temp == &expand_param_fatal)
+         temp_has_dollar_at = 0;
+         pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0;
+         if (word->flags & W_NOSPLIT2)
+           pflags |= PF_NOSPLIT2;
+         if (word->flags & W_ASSIGNRHS)
+           pflags |= PF_ASSIGNRHS;
+         if (word->flags & W_COMPLETE)
+           pflags |= PF_COMPLETE;
+         tword = param_expand (string, &sindex, quoted, expanded_something,
+                              &temp_has_dollar_at, &quoted_dollar_at,
+                              &had_quoted_null, pflags);
+         has_dollar_at += temp_has_dollar_at;
+         split_on_spaces += (tword->flags & W_SPLITSPACE);
+
+         if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
            {
              free (string);
              free (istring);
-             return ((temp == &expand_param_error) ? &expand_word_error
-                                                   : &expand_word_fatal);
+             return ((tword == &expand_wdesc_error) ? &expand_word_error
+                                                    : &expand_word_fatal);
            }
          if (contains_dollar_at && has_dollar_at)
            *contains_dollar_at = 1;
+
+         if (tword && (tword->flags & W_HASQUOTEDNULL))
+           had_quoted_null = 1;
+
+         temp = tword ? tword->word : (char *)NULL;
+         dispose_word_desc (tword);
+
+         /* Kill quoted nulls; we will add them back at the end of
+            expand_word_internal if nothing else in the string */
+         if (had_quoted_null && temp && QUOTED_NULL (temp))
+           {
+             FREE (temp);
+             temp = (char *)NULL;
+           }
+
          goto add_string;
          break;
 
        case '`':               /* Backquoted command substitution. */
          {
-           sindex++;
+           t_index = sindex++;
 
+           temp = string_extract (string, &sindex, "`", SX_REQMATCH);
+           /* The test of sindex against t_index is to allow bare instances of
+              ` to pass through, for backwards compatibility. */
+           if (temp == &extract_string_error || temp == &extract_string_fatal)
+             {
+               if (sindex - 1 == t_index)
+                 {
+                   sindex = t_index;
+                   goto add_character;
+                 }
+               last_command_exit_value = EXECUTION_FAILURE;
+               report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index);
+               free (string);
+               free (istring);
+               return ((temp == &extract_string_error) ? &expand_word_error
+                                                       : &expand_word_fatal);
+             }
+               
            if (expanded_something)
              *expanded_something = 1;
 
-           temp = string_extract (string, &sindex, "`", 0);
-           de_backslash (temp);
-           temp1 = command_substitute (temp, quoted);
+           if (word->flags & W_NOCOMSUB)
+             /* sindex + 1 because string[sindex] == '`' */
+             temp1 = substring (string, t_index, sindex + 1);
+           else
+             {
+               de_backslash (temp);
+               tword = command_substitute (temp, quoted);
+               temp1 = tword ? tword->word : (char *)NULL;
+               if (tword)
+                 dispose_word_desc (tword);
+             }
            FREE (temp);
            temp = temp1;
            goto dollar_add_string;
@@ -4931,46 +9396,89 @@ add_string:
          c = string[++sindex];
 
          if (quoted & Q_HERE_DOCUMENT)
-           temp1 = slashify_in_here_document;
+           tflag = CBSHDOC;
          else if (quoted & Q_DOUBLE_QUOTES)
-           temp1 = slashify_in_quotes;
+           tflag = CBSDQUOTE;
          else
-           temp1 = "";
+           tflag = 0;
+
+         /* From Posix discussion on austin-group list:  Backslash escaping
+            a } in ${...} is removed.  Issue 0000221 */
+         if ((quoted & Q_DOLBRACE) && c == RBRACE)
+           {
+             SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
+           }
+         /* This is the fix for " $@\ " */
+         else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0) && isexp == 0 && isifs (c))
+           {
+             RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
+                                     DEFAULT_ARRAY_SIZE);
+             istring[istring_index++] = CTLESC;
+             istring[istring_index++] = '\\';
+             istring[istring_index] = '\0';
 
-         if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && member (c, temp1) == 0)
+             SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
+           }
+         else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
            {
-             temp = xmalloc (3);
-             temp[0] = '\\'; temp[1] = c; temp[2] = '\0';
+             SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
+           }
+         else if (c == 0)
+           {
+             c = CTLNUL;
+             sindex--;         /* add_character: label increments sindex */
+             goto add_character;
            }
          else
-           /* This character is quoted, so add it in quoted mode. */
-           temp = make_quoted_char (c);
+           {
+             SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
+           }
 
-         if (c)
-           sindex++;
-         goto add_string;
+         sindex++;
+add_twochars:
+         /* BEFORE jumping here, we need to increment sindex if appropriate */
+         RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
+                                 DEFAULT_ARRAY_SIZE);
+         istring[istring_index++] = twochars[0];
+         istring[istring_index++] = twochars[1];
+         istring[istring_index] = '\0';
+
+         break;
 
        case '"':
-         if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT|Q_NOQUOTE))
+         if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && ((quoted & Q_ARITH) == 0))
            goto add_character;
 
          t_index = ++sindex;
-         temp = string_extract_double_quoted (string, &sindex, 0);
+         temp = string_extract_double_quoted (string, &sindex, (word->flags & W_COMPLETE) ? SX_COMPLETE : 0);
 
          /* If the quotes surrounded the entire string, then the
             whole word was quoted. */
          quoted_state = (t_index == 1 && string[sindex] == '\0')
                            ? WHOLLY_QUOTED
-                           : PARTIALLY_QUOTED;
+                           : PARTIALLY_QUOTED;
 
          if (temp && *temp)
            {
-             tword = make_word (temp);         /* XXX */
-             free (temp);
+             tword = alloc_word_desc ();
+             tword->word = temp;
+
+             /* XXX - bash-4.4/bash-5.0 */
+             if (word->flags & W_ASSIGNARG)
+               tword->flags |= word->flags & (W_ASSIGNARG|W_ASSIGNRHS);        /* affects $@ */
+             if (word->flags & W_COMPLETE)
+               tword->flags |= W_COMPLETE;     /* for command substitutions */
+             if (word->flags & W_NOCOMSUB)
+               tword->flags |= W_NOCOMSUB;
+             if (word->flags & W_NOPROCSUB)
+               tword->flags |= W_NOPROCSUB;
+
              temp = (char *)NULL;
 
-             has_dollar_at = 0;
-             list = expand_word_internal (tword, Q_DOUBLE_QUOTES, &has_dollar_at, (int *)NULL);
+             temp_has_dollar_at = 0;   /* XXX */
+             /* Need to get W_HASQUOTEDNULL flag through this function. */
+             list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &temp_has_dollar_at, (int *)NULL);
+             has_dollar_at += temp_has_dollar_at;
 
              if (list == &expand_word_error || list == &expand_word_fatal)
                {
@@ -4988,7 +9496,7 @@ add_string:
              /* "$@" (a double-quoted dollar-at) expands into nothing,
                 not even a NULL word, when there are no positional
                 parameters. */
-             if (list == 0 && has_dollar_at)
+             if (list == 0 && temp_has_dollar_at)      /* XXX - was has_dollar_at */
                {
                  quoted_dollar_at++;
                  break;
@@ -5005,7 +9513,10 @@ add_string:
              if (list)
                dequote_list (list);
 
-             if (has_dollar_at)
+             if (list && list->word && (list->word->flags & W_HASQUOTEDNULL))
+               had_quoted_null = 1;            /* XXX */
+
+             if (temp_has_dollar_at)           /* XXX - was has_dollar_at */
                {
                  quoted_dollar_at++;
                  if (contains_dollar_at)
@@ -5017,7 +9528,7 @@ add_string:
          else
            {
              /* What we have is "".  This is a minor optimization. */
-             free (temp);
+             FREE (temp);
              list = (WORD_LIST *)NULL;
            }
 
@@ -5036,7 +9547,7 @@ add_string:
                  /* Testing quoted_dollar_at makes sure that "$@" is
                     split correctly when $IFS does not contain a space. */
                  temp = quoted_dollar_at
-                               ? string_list_dollar_at (list, Q_DOUBLE_QUOTES)
+                               ? string_list_dollar_at (list, Q_DOUBLE_QUOTES, 0)
                                : string_list (quote_list (list));
                  dispose_words (list);
                  goto add_string;
@@ -5044,8 +9555,9 @@ add_string:
              else
                {
                  temp = savestring (list->word->word);
+                 tflag = list->word->flags;
                  dispose_words (list);
-#if 1
+
                  /* If the string is not a quoted null string, we want
                     to remove any embedded unquoted CTLNUL characters.
                     We do not want to turn quoted null strings back into
@@ -5053,18 +9565,25 @@ add_string:
                     want to remove any quoted nulls from expansions that
                     contain other characters.  For example, if we have
                     x"$*"y or "x$*y" and there are no positional parameters,
-                    the $* should expand into nothing. */                   
-                 if (QUOTED_NULL (temp) == 0)
+                    the $* should expand into nothing. */
+                 /* We use the W_HASQUOTEDNULL flag to differentiate the
+                    cases:  a quoted null character as above and when
+                    CTLNUL is contained in the (non-null) expansion
+                    of some variable.  We use the had_quoted_null flag to
+                    pass the value through this function to its caller. */
+                 if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0)
                    remove_quoted_nulls (temp); /* XXX */
-#endif
                }
            }
          else
            temp = (char *)NULL;
 
          /* We do not want to add quoted nulls to strings that are only
-            partially quoted; we can throw them away. */
-         if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
+            partially quoted; we can throw them away.  The exception to
+            this is when we are going to be performing word splitting,
+            since we have to preserve a null argument if the next character
+            will cause word splitting. */
+         if (temp == 0 && quoted_state == PARTIALLY_QUOTED && (word->flags & (W_NOSPLIT|W_NOSPLIT2)))
            continue;
 
        add_quoted_string:
@@ -5074,19 +9593,20 @@ add_string:
              temp1 = temp;
              temp = quote_string (temp);
              free (temp1);
+             goto add_string;
            }
          else
            {
              /* Add NULL arg. */
-             temp = xmalloc (2);
-             temp[0] = CTLNUL;
-             temp[1] = '\0';
+             c = CTLNUL;
+             sindex--;         /* add_character: label increments sindex */
+             goto add_character;
            }
-         goto add_string;
+
          /* break; */
 
        case '\'':
-         if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT|Q_NOQUOTE))
+         if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
            goto add_character;
 
          t_index = ++sindex;
@@ -5096,7 +9616,7 @@ add_string:
             then the string is wholly quoted. */
          quoted_state = (t_index == 1 && string[sindex] == '\0')
                            ? WHOLLY_QUOTED
-                           : PARTIALLY_QUOTED;
+                           : PARTIALLY_QUOTED;
 
          /* If all we had was '', it is a null expansion. */
          if (*temp == '\0')
@@ -5105,24 +9625,60 @@ add_string:
              temp = (char *)NULL;
            }
          else
-           remove_quoted_escapes (temp);
+           remove_quoted_escapes (temp);       /* ??? */
 
          /* We do not want to add quoted nulls to strings that are only
-            partially quoted; such nulls are discarded. */
-         if (temp == 0 && (quoted_state == PARTIALLY_QUOTED))
+            partially quoted; such nulls are discarded.  See above for the
+            exception, which is when the string is going to be split. */
+         if (temp == 0 && (quoted_state == PARTIALLY_QUOTED) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)))
            continue;
 
-         goto add_quoted_string;
+         /* If we have a quoted null expansion, add a quoted NULL to istring. */
+         if (temp == 0)
+           {
+             c = CTLNUL;
+             sindex--;         /* add_character: label increments sindex */
+             goto add_character;
+           }
+         else
+           goto add_quoted_string;
+
          /* break; */
 
        default:
          /* This is the fix for " $@ " */
-         if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+       add_ifs_character:
+         if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0))
            {
-             temp = make_quoted_char (c);
-             goto dollar_add_string;
+             if (string[sindex])       /* from old goto dollar_add_string */
+               sindex++;
+             if (c == 0)
+               {
+                 c = CTLNUL;
+                 goto add_character;
+               }
+             else
+               {
+#if HANDLE_MULTIBYTE
+                 if (mb_cur_max > 1)
+                   sindex--;
+
+                 if (mb_cur_max > 1)
+                   {
+                     SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
+                   }
+                 else
+#endif
+                   {
+                     twochars[0] = CTLESC;
+                     twochars[1] = c;
+                     goto add_twochars;
+                   }
+               }
            }
 
+         SADD_MBCHAR (temp, string, sindex, string_size);
+
        add_character:
          RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
                                  DEFAULT_ARRAY_SIZE);
@@ -5168,6 +9724,7 @@ finished_with_string:
          istring[0] = CTLNUL;
          istring[1] = '\0';
          tword = make_bare_word (istring);
+         tword->flags |= W_HASQUOTEDNULL;              /* XXX */
          list = make_word_list (tword, (WORD_LIST *)NULL);
          if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
            tword->flags |= W_QUOTED;
@@ -5175,55 +9732,118 @@ finished_with_string:
       /* According to sh, ksh, and Posix.2, if a word expands into nothing
         and a double-quoted "$@" appears anywhere in it, then the entire
         word is removed. */
+      /* XXX - exception appears to be that quoted null strings result in
+        null arguments */
       else  if (quoted_state == UNQUOTED || quoted_dollar_at)
        list = (WORD_LIST *)NULL;
 #if 0
       else
        {
          tword = make_bare_word (istring);
-         list = make_word_list (tword, (WORD_LIST *)NULL);
          if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
            tword->flags |= W_QUOTED;
+         list = make_word_list (tword, (WORD_LIST *)NULL);
        }
+#else
+      else
+       list = (WORD_LIST *)NULL;
 #endif
     }
   else if (word->flags & W_NOSPLIT)
     {
       tword = make_bare_word (istring);
-      list = make_word_list (tword, (WORD_LIST *)NULL);
       if (word->flags & W_ASSIGNMENT)
        tword->flags |= W_ASSIGNMENT;   /* XXX */
+      if (word->flags & W_COMPASSIGN)
+       tword->flags |= W_COMPASSIGN;   /* XXX */
+      if (word->flags & W_NOGLOB)
+       tword->flags |= W_NOGLOB;       /* XXX */
+      if (word->flags & W_NOBRACE)
+       tword->flags |= W_NOBRACE;      /* XXX */
+      if (word->flags & W_NOEXPAND)
+       tword->flags |= W_NOEXPAND;     /* XXX */
       if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
-        tword->flags |= W_QUOTED;
+       tword->flags |= W_QUOTED;
+      if (had_quoted_null && QUOTED_NULL (istring))
+       tword->flags |= W_HASQUOTEDNULL;
+      list = make_word_list (tword, (WORD_LIST *)NULL);
     }
   else
     {
       char *ifs_chars;
+      char *tstring;
 
-      if (quoted_dollar_at || has_dollar_at)
-       {
-         var = find_variable ("IFS");
-         ifs_chars = var ? value_cell (var) : " \t\n";
-       }
-      else
-       ifs_chars = (char *)NULL;
+      ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
 
       /* If we have $@, we need to split the results no matter what.  If
         IFS is unset or NULL, string_list_dollar_at has separated the
         positional parameters with a space, so we split on space (we have
         set ifs_chars to " \t\n" above if ifs is unset).  If IFS is set,
         string_list_dollar_at has separated the positional parameters
-        with the first character of $IFS, so we split on $IFS. */
-      if (has_dollar_at && ifs_chars)
+        with the first character of $IFS, so we split on $IFS.  If
+        SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either
+        unset or null, and we want to make sure that we split on spaces
+        regardless of what else has happened to IFS since the expansion,
+        or we expanded "$@" with IFS null and we need to split the positional
+        parameters into separate words. */
+      if (split_on_spaces)
+       list = list_string (istring, " ", 1);   /* XXX quoted == 1? */
+      /* If we have $@ (has_dollar_at != 0) and we are in a context where we
+        don't want to split the result (W_NOSPLIT2), and we are not quoted,
+        we have already separated the arguments with the first character of
+        $IFS.  In this case, we want to return a list with a single word
+        with the separator possibly replaced with a space (it's what other
+        shells seem to do).
+        quoted_dollar_at is internal to this function and is set if we are
+        passed an argument that is unquoted (quoted == 0) but we encounter a
+        double-quoted $@ while expanding it. */
+      else if (has_dollar_at && quoted_dollar_at == 0 && ifs_chars && quoted == 0 && (word->flags & W_NOSPLIT2))
+       {
+         /* Only split and rejoin if we have to */
+         if (*ifs_chars && *ifs_chars != ' ')
+           {
+             list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
+             tstring = string_list (list);
+           }
+         else
+           tstring = istring;
+         tword = make_bare_word (tstring);
+         if (tstring != istring)
+           free (tstring);
+         goto set_word_flags;
+       }
+      /* This is the attempt to make $* in an assignment context (a=$*) and
+        array variables subscripted with * in an assignment context (a=${foo[*]})
+        behave similarly.  It has side effects that, though they increase
+        compatibility with other shells, are not backwards compatible. */
+#if 0
+      else if (has_dollar_at && quoted == 0 && ifs_chars && (word->flags & W_ASSIGNRHS))
+       {
+         tword = make_bare_word (istring);
+         goto set_word_flags;
+       }
+#endif
+      else if (has_dollar_at && ifs_chars)
        list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
       else
        {
          tword = make_bare_word (istring);
-         list = make_word_list (tword, (WORD_LIST *)NULL);
+set_word_flags:
          if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
            tword->flags |= W_QUOTED;
          if (word->flags & W_ASSIGNMENT)
            tword->flags |= W_ASSIGNMENT;
+         if (word->flags & W_COMPASSIGN)
+           tword->flags |= W_COMPASSIGN;
+         if (word->flags & W_NOGLOB)
+           tword->flags |= W_NOGLOB;
+         if (word->flags & W_NOBRACE)
+           tword->flags |= W_NOBRACE;
+         if (word->flags & W_NOEXPAND)
+           tword->flags |= W_NOEXPAND;
+         if (had_quoted_null && QUOTED_NULL (istring))
+           tword->flags |= W_HASQUOTEDNULL;    /* XXX */
+         list = make_word_list (tword, (WORD_LIST *)NULL);
        }
     }
 
@@ -5238,17 +9858,23 @@ finished_with_string:
 /* **************************************************************** */
 
 /* Perform quote removal on STRING.  If QUOTED > 0, assume we are obeying the
-   backslash quoting rules for within double quotes. */
+   backslash quoting rules for within double quotes or a here document. */
 char *
 string_quote_removal (string, quoted)
      char *string;
      int quoted;
 {
-  char *r, *result_string, *temp;
-  int sindex, tindex, c, dquote;
+  size_t slen;
+  char *r, *result_string, *temp, *send;
+  int sindex, tindex, dquote;
+  unsigned char c;
+  DECLARE_MBSTATE;
 
   /* The result can be no longer than the original string. */
-  r = result_string = xmalloc (strlen (string) + 1);
+  slen = strlen (string);
+  send = string + slen;
+
+  r = result_string = (char *)xmalloc (slen + 1);
 
   for (dquote = sindex = 0; c = string[sindex];)
     {
@@ -5256,13 +9882,17 @@ string_quote_removal (string, quoted)
        {
        case '\\':
          c = string[++sindex];
-         if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && member (c, slashify_in_quotes) == 0)
+         if (c == 0)
+           {
+             *r++ = '\\';
+             break;
+           }
+         if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
            *r++ = '\\';
          /* FALLTHROUGH */
 
        default:
-         *r++ = c;
-         sindex++;
+         SCOPY_CHAR_M (r, string, send, sindex);
          break;
 
        case '\'':
@@ -5306,8 +9936,8 @@ word_quote_removal (word, quoted)
   char *t;
 
   t = string_quote_removal (word->word, quoted);
-  w = make_bare_word (t);
-  free (t);
+  w = alloc_word_desc ();
+  w->word = t ? t : savestring ("");
   return (w);
 }
 
@@ -5319,14 +9949,23 @@ word_list_quote_removal (list, quoted)
      WORD_LIST *list;
      int quoted;
 {
-  WORD_LIST *result, *t, *tresult;
+  WORD_LIST *result, *t, *tresult, *e;
 
   for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
     {
-      tresult = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
-      tresult->word = word_quote_removal (t->word, quoted);
-      tresult->next = (WORD_LIST *)NULL;
+      tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
+#if 0
       result = (WORD_LIST *) list_append (result, tresult);
+#else
+      if (result == 0)
+       result = e = tresult;
+      else
+       {
+         e->next = tresult;
+         while (e->next)
+           e = e->next;
+       }
+#endif
     }
   return (result);
 }
@@ -5338,30 +9977,75 @@ word_list_quote_removal (list, quoted)
  *                                        *
  *******************************************/
 
+void
+setifs (v)
+     SHELL_VAR *v;
+{
+  char *t;
+  unsigned char uc;
+
+  ifs_var = v;
+  ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n";
+
+  ifs_is_set = ifs_var != 0;
+  ifs_is_null = ifs_is_set && (*ifs_value == 0);
+
+  /* Should really merge ifs_cmap with sh_syntaxtab.  XXX - doesn't yet
+     handle multibyte chars in IFS */
+  memset (ifs_cmap, '\0', sizeof (ifs_cmap));
+  for (t = ifs_value ; t && *t; t++)
+    {
+      uc = *t;
+      ifs_cmap[uc] = 1;
+    }
+
+#if defined (HANDLE_MULTIBYTE)
+  if (ifs_value == 0)
+    {
+      ifs_firstc[0] = '\0';
+      ifs_firstc_len = 1;
+    }
+  else
+    {
+      size_t ifs_len;
+      ifs_len = strnlen (ifs_value, MB_CUR_MAX);
+      ifs_firstc_len = MBLEN (ifs_value, ifs_len);
+      if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len))
+       {
+         ifs_firstc[0] = ifs_value[0];
+         ifs_firstc[1] = '\0';
+         ifs_firstc_len = 1;
+       }
+      else
+       memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
+    }
+#else
+  ifs_firstc = ifs_value ? *ifs_value : 0;
+#endif
+}
+
+char *
+getifs ()
+{
+  return ifs_value;
+}
+
 /* This splits a single word into a WORD LIST on $IFS, but only if the word
    is not quoted.  list_string () performs quote removal for us, even if we
    don't do any splitting. */
 WORD_LIST *
-word_split (w)
+word_split (w, ifs_chars)
      WORD_DESC *w;
+     char *ifs_chars;
 {
   WORD_LIST *result;
-  SHELL_VAR *ifs;
-  char *ifs_chars;
 
   if (w)
     {
-      ifs = find_variable ("IFS");
-      /* If IFS is unset, it defaults to " \t\n". */
-      ifs_chars = ifs ? value_cell (ifs) : " \t\n";
-
-      if ((w->flags & W_QUOTED) || !ifs_chars)
-       ifs_chars = "";
+      char *xifs;
 
-      result = list_string (w->word, ifs_chars, w->flags & W_QUOTED);
-
-      if (ifs && tempvar_p (ifs))      /* XXX */
-       dispose_variable (ifs);         /* XXX */
+      xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
+      result = list_string (w->word, xifs, w->flags & W_QUOTED);
     }
   else
     result = (WORD_LIST *)NULL;
@@ -5375,12 +10059,19 @@ static WORD_LIST *
 word_list_split (list)
      WORD_LIST *list;
 {
-  WORD_LIST *result, *t, *tresult;
+  WORD_LIST *result, *t, *tresult, *e;
 
   for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
     {
-      tresult = word_split (t->word);
-      result = (WORD_LIST *) list_append (result, tresult);
+      tresult = word_split (t->word, ifs_value);
+      if (result == 0)
+        result = e = tresult;
+      else
+       {
+         e->next = tresult;
+         while (e->next)
+           e = e->next;
+       }
     }
   return (result);
 }
@@ -5391,34 +10082,53 @@ word_list_split (list)
  *                                               *
  **************************************************/
 
+/* Do any word-expansion-specific cleanup and jump to top_level */
+static void
+exp_jump_to_top_level (v)
+     int v;
+{
+  set_pipestatus_from_exit (last_command_exit_value);
+
+  /* Cleanup code goes here. */
+  expand_no_split_dollar_star = 0;     /* XXX */
+  expanding_redir = 0;
+  assigning_in_environment = 0;
+
+  if (parse_and_execute_level == 0)
+    top_level_cleanup ();                      /* from sig.c */
+
+  jump_to_top_level (v);
+}
+
 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
    ELIST, and set ELIST to the new list. */
 #define PREPEND_LIST(nlist, elist) \
        do { nlist->next = elist; elist = nlist; } while (0)
 
-static WORD_LIST *varlist = (WORD_LIST *)NULL;
-
 /* Separate out any initial variable assignments from TLIST.  If set -k has
    been executed, remove all assignment statements from TLIST.  Initial
    variable assignments and other environment assignments are placed
-   on VARLIST. */
+   on SUBST_ASSIGN_VARLIST. */
 static WORD_LIST *
 separate_out_assignments (tlist)
      WORD_LIST *tlist;
 {
   register WORD_LIST *vp, *lp;
 
-  if (!tlist)
+  if (tlist == 0)
     return ((WORD_LIST *)NULL);
 
-  varlist = (WORD_LIST *)NULL;
+  if (subst_assign_varlist)
+    dispose_words (subst_assign_varlist);      /* Clean up after previous error */
+
+  subst_assign_varlist = (WORD_LIST *)NULL;
   vp = lp = tlist;
 
   /* Separate out variable assignments at the start of the command.
      Loop invariant: vp->next == lp
      Loop postcondition:
-       lp = list of words left after assignment statements skipped
-       tlist = original list of words
+       lp = list of words left after assignment statements skipped
+       tlist = original list of words
   */
   while (lp && (lp->word->flags & W_ASSIGNMENT))
     {
@@ -5426,12 +10136,12 @@ separate_out_assignments (tlist)
       lp = lp->next;
     }
 
-  /* If lp != tlist, we have some initial assignment statements. */
-  /* We make VARLIST point to the list of assignment words and
-     TLIST point to the remaining words.  */
+  /* If lp != tlist, we have some initial assignment statements.
+     We make SUBST_ASSIGN_VARLIST point to the list of assignment
+     words and TLIST point to the remaining words.  */
   if (lp != tlist)
     {
-      varlist = tlist;
+      subst_assign_varlist = tlist;
       /* ASSERT(vp->next == lp); */
       vp->next = (WORD_LIST *)NULL;    /* terminate variable list */
       tlist = lp;                      /* remainder of word list */
@@ -5447,7 +10157,8 @@ separate_out_assignments (tlist)
   /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
 
   /* If the -k option is in effect, we need to go through the remaining
-     words, separate out the assignment words, and place them on VARLIST. */
+     words, separate out the assignment words, and place them on
+     SUBST_ASSIGN_VARLIST. */
   if (place_keywords_in_env)
     {
       WORD_LIST *tp;   /* tp == running pointer into tlist */
@@ -5462,9 +10173,9 @@ separate_out_assignments (tlist)
          if (lp->word->flags & W_ASSIGNMENT)
            {
              /* Found an assignment statement, add this word to end of
-                varlist (vp). */
-             if (!varlist)
-               varlist = vp = lp;
+                subst_assign_varlist (vp). */
+             if (!subst_assign_varlist)
+               subst_assign_varlist = vp = lp;
              else
                {
                  vp->next = lp;
@@ -5555,14 +10266,14 @@ glob_expand_word_list (tlist, eflags)
         original word is added to orig_list.  If globbing fails and
         failed glob expansions are removed, the original word is
         added to the list of disposable words.  orig_list ends up
-        in reverse order and requires a call to reverse_list to
+        in reverse order and requires a call to REVERSE_LIST to
         be set right.  After all words are examined, the disposable
         words are freed. */
       next = tlist->next;
 
       /* If the word isn't an assignment and contains an unquoted
-         pattern matching character, then glob it. */
-      if ((tlist->word->flags & W_ASSIGNMENT) == 0 &&
+        pattern matching character, then glob it. */
+      if ((tlist->word->flags & W_NOGLOB) == 0 &&
          unquoted_glob_pattern_p (tlist->word->word))
        {
          glob_array = shell_glob_filename (tlist->word->word);
@@ -5572,9 +10283,9 @@ glob_expand_word_list (tlist, eflags)
             or directory".  However, I would like to report errors
             like "Read failed". */
 
-         if (GLOB_FAILED (glob_array))
+         if (glob_array == 0 || GLOB_FAILED (glob_array))
            {
-             glob_array = (char **) xmalloc (sizeof (char *));
+             glob_array = (char **)xmalloc (sizeof (char *));
              glob_array[0] = (char *)NULL;
            }
 
@@ -5591,7 +10302,6 @@ glob_expand_word_list (tlist, eflags)
          for (glob_index = 0; glob_array[glob_index]; glob_index++)
            {
              tword = make_bare_word (glob_array[glob_index]);
-             tword->flags |= W_GLOBEXP;        /* XXX */
              glob_list = make_word_list (tword, glob_list);
            }
 
@@ -5600,6 +10310,12 @@ glob_expand_word_list (tlist, eflags)
              output_list = (WORD_LIST *)list_append (glob_list, output_list);
              PREPEND_LIST (tlist, disposables);
            }
+         else if (fail_glob_expansion != 0)
+           {
+             last_command_exit_value = EXECUTION_FAILURE;
+             report_error (_("no match: %s"), tlist->word->word);
+             exp_jump_to_top_level (DISCARD);
+           }
          else if (allow_null_glob_expansion == 0)
            {
              /* Failed glob expressions are left unchanged. */
@@ -5620,7 +10336,7 @@ glob_expand_word_list (tlist, eflags)
          PREPEND_LIST (tlist, output_list);
        }
 
-      free_array (glob_array);
+      strvec_dispose (glob_array);
       glob_array = (char **)NULL;
 
       tlist = next;
@@ -5651,27 +10367,45 @@ brace_expand_word_list (tlist, eflags)
     {
       next = tlist->next;
 
+      if (tlist->word->flags & W_NOBRACE)
+        {
+/*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/
+         PREPEND_LIST (tlist, output_list);
+         continue;
+        }
+
+      if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
+        {
+/*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
+         PREPEND_LIST (tlist, output_list);
+         continue;
+        }
+
       /* Only do brace expansion if the word has a brace character.  If
         not, just add the word list element to BRACES and continue.  In
         the common case, at least when running shell scripts, this will
-        degenerate to a bunch of calls to `strchr', and then what is
+        degenerate to a bunch of calls to `mbschr', and then what is
         basically a reversal of TLIST into BRACES, which is corrected
-        by a call to reverse_list () on BRACES when the end of TLIST
+        by a call to REVERSE_LIST () on BRACES when the end of TLIST
         is reached. */
-      if (strchr (tlist->word->word, LBRACE))
+      if (mbschr (tlist->word->word, LBRACE))
        {
          expansions = brace_expand (tlist->word->word);
 
          for (eindex = 0; temp_string = expansions[eindex]; eindex++)
            {
-             w = make_word (temp_string);
+             w = alloc_word_desc ();
+             w->word = temp_string;
+
              /* If brace expansion didn't change the word, preserve
                 the flags.  We may want to preserve the flags
                 unconditionally someday -- XXX */
              if (STREQ (temp_string, tlist->word->word))
                w->flags = tlist->word->flags;
+             else
+               w = make_word_flags (w, temp_string);
+
              output_list = make_word_list (w, output_list);
-             free (expansions[eindex]);
            }
          free (expansions);
 
@@ -5693,41 +10427,161 @@ brace_expand_word_list (tlist, eflags)
 }
 #endif
 
+#if defined (ARRAY_VARS)
+/* Take WORD, a compound associative array assignment, and internally run
+   'declare -A w', where W is the variable name portion of WORD. */
+static int
+make_internal_declare (word, option, cmd)
+     char *word;
+     char *option;
+     char *cmd;
+{
+  int t, r;
+  WORD_LIST *wl;
+  WORD_DESC *w;
+
+  w = make_word (word);
+
+  t = assignment (w->word, 0);
+  if (w->word[t] == '=')
+    {
+      w->word[t] = '\0';
+      if (w->word[t - 1] == '+')       /* cut off any append op */
+       w->word[t - 1] = '\0';
+    }
+
+  wl = make_word_list (w, (WORD_LIST *)NULL);
+  wl = make_word_list (make_word (option), wl);
+
+  r = declare_builtin (wl);
+
+  dispose_words (wl);
+  return r;
+}  
+#endif
+
 static WORD_LIST *
 shell_expand_word_list (tlist, eflags)
      WORD_LIST *tlist;
      int eflags;
 {
-  WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list;
+  WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list, *wcmd;
   int expanded_something, has_dollar_at;
   char *temp_string;
 
   /* We do tilde expansion all the time.  This is what 1003.2 says. */
   new_list = (WORD_LIST *)NULL;
+  for (wcmd = tlist; wcmd; wcmd = wcmd->next)
+    if (wcmd->word->flags & W_ASSNBLTIN)
+      break;
+
   for (orig_list = tlist; tlist; tlist = next)
     {
       temp_string = tlist->word->word;
 
       next = tlist->next;
 
-      /* Posix.2 section 3.6.1 says that tildes following `=' in words
-        which are not assignment statements are not expanded.  We do
-        this only if POSIXLY_CORRECT is enabled.  Essentially, we do
-        tilde expansion on unquoted assignment statements (flags include
-        W_ASSIGNMENT but not W_QUOTED). */
-      if (temp_string[0] == '~' ||
-           (((tlist->word->flags & (W_ASSIGNMENT|W_QUOTED)) == W_ASSIGNMENT) &&
-            posixly_correct == 0 &&
-            strchr (temp_string, '~') &&
-            (unquoted_substring ("=~", temp_string) || unquoted_substring (":~", temp_string))))
+#if defined (ARRAY_VARS)
+      /* If this is a compound array assignment to a builtin that accepts
+         such assignments (e.g., `declare'), take the assignment and perform
+         it separately, handling the semantics of declarations inside shell
+         functions.  This avoids the double-evaluation of such arguments,
+         because `declare' does some evaluation of compound assignments on
+         its own. */
+      if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
        {
-         tlist->word->word = bash_tilde_expand (temp_string);
-         free (temp_string);
+         int t;
+         char opts[16], opti;
+
+         opti = 0;
+         if (tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL|W_ASSIGNARRAY))
+           opts[opti++] = '-';
+
+         if ((tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL)) == (W_ASSIGNASSOC|W_ASSNGLOBAL))
+           {
+             opts[opti++] = 'g';
+             opts[opti++] = 'A';
+           }
+         else if (tlist->word->flags & W_ASSIGNASSOC)
+           opts[opti++] = 'A';
+         else if ((tlist->word->flags & (W_ASSIGNARRAY|W_ASSNGLOBAL)) == (W_ASSIGNARRAY|W_ASSNGLOBAL))
+           {
+             opts[opti++] = 'g';
+             opts[opti++] = 'a';
+           }
+         else if (tlist->word->flags & W_ASSIGNARRAY)
+           opts[opti++] = 'a';
+         else if (tlist->word->flags & W_ASSNGLOBAL)
+           opts[opti++] = 'g';
+
+         /* If we have special handling note the integer attribute and others
+            that transform the value upon assignment.  What we do is take all
+            of the option arguments and scan through them looking for options
+            that cause such transformations, and add them to the `opts' array. */
+/*       if (opti > 0) */
+           {
+             char omap[128];
+             int oind;
+             WORD_LIST *l;
+
+             memset (omap, '\0', sizeof (omap));
+             for (l = orig_list->next; l != tlist; l = l->next)
+               {
+                 if (l->word->word[0] != '-')
+                   break;      /* non-option argument */
+                 if (l->word->word[0] == '-' && l->word->word[1] == '-' && l->word->word[2] == 0)
+                   break;      /* -- signals end of options */
+                 for (oind = 1; l->word->word[oind]; oind++)
+                   switch (l->word->word[oind])
+                     {
+                       case 'i':
+                       case 'l':
+                       case 'u':
+                       case 'c':
+                         omap[l->word->word[oind]] = 1;
+                         if (opti == 0)
+                           opts[opti++] = '-';
+                         break;
+                       default:
+                         break;
+                     }
+               }
+
+             for (oind = 0; oind < sizeof (omap); oind++)
+               if (omap[oind])
+                 opts[opti++] = oind;
+           }
+
+         opts[opti] = '\0';
+         if (opti > 0)
+           {
+             t = make_internal_declare (tlist->word->word, opts, wcmd ? wcmd->word->word : (char *)0);
+             if (t != EXECUTION_SUCCESS)
+               {
+                 last_command_exit_value = t;
+                 exp_jump_to_top_level (DISCARD);
+               }
+           }
+
+         t = do_word_assignment (tlist->word, 0);
+         if (t == 0)
+           {
+             last_command_exit_value = EXECUTION_FAILURE;
+             exp_jump_to_top_level (DISCARD);
+           }
+
+         /* Now transform the word as ksh93 appears to do and go on */
+         t = assignment (tlist->word->word, 0);
+         tlist->word->word[t] = '\0';
+         if (tlist->word->word[t - 1] == '+')
+           tlist->word->word[t - 1] = '\0';    /* cut off append op */
+         tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC|W_ASSIGNARRAY);
        }
+#endif
 
       expanded_something = 0;
       expanded = expand_word_internal
-       (tlist->word, 0, &has_dollar_at, &expanded_something);
+       (tlist->word, 0, 0, &has_dollar_at, &expanded_something);
 
       if (expanded == &expand_word_error || expanded == &expand_word_fatal)
        {
@@ -5740,10 +10594,11 @@ shell_expand_word_list (tlist, eflags)
          /* Dispose the new list we're building. */
          dispose_words (new_list);
 
+         last_command_exit_value = EXECUTION_FAILURE;
          if (expanded == &expand_word_error)
-           jump_to_top_level (DISCARD);
+           exp_jump_to_top_level (DISCARD);
          else
-           jump_to_top_level (FORCE_EOF);
+           exp_jump_to_top_level (FORCE_EOF);
        }
 
       /* Don't split words marked W_NOSPLIT. */
@@ -5777,15 +10632,17 @@ shell_expand_word_list (tlist, eflags)
 
 /* The workhorse for expand_words () and expand_words_no_vars ().
    First arg is LIST, a WORD_LIST of words.
-   Second arg DO_VARS is non-zero if you want to do environment and
-   variable assignments, else zero.
+   Second arg EFLAGS is a flags word controlling which expansions are
+   performed.
 
    This does all of the substitutions: brace expansion, tilde expansion,
    parameter expansion, command substitution, arithmetic expansion,
    process substitution, word splitting, and pathname expansion, according
    to the bits set in EFLAGS.  Words with the W_QUOTED or W_NOSPLIT bits
    set, or for which no expansion is done, do not undergo word splitting.
-   Words with the W_ASSIGNMENT bit set do not undergo pathname expansion. */
+   Words with the W_NOGLOB bit set do not undergo pathname expansion; words
+   with W_NOBRACE set do not undergo brace expansion (see
+   brace_expand_word_list above). */
 static WORD_LIST *
 expand_word_list_internal (list, eflags)
      WORD_LIST *list;
@@ -5793,35 +10650,41 @@ expand_word_list_internal (list, eflags)
 {
   WORD_LIST *new_list, *temp_list;
   int tint;
+  char *savecmd;
 
+  tempenv_assign_error = 0;
   if (list == 0)
     return ((WORD_LIST *)NULL);
 
-  new_list = copy_word_list (list);
-
+  garglist = new_list = copy_word_list (list);
   if (eflags & WEXP_VARASSIGN)
     {
-      new_list = separate_out_assignments (new_list);
+      garglist = new_list = separate_out_assignments (new_list);
       if (new_list == 0)
        {
-         if (varlist)
+         if (subst_assign_varlist)
            {
              /* All the words were variable assignments, so they are placed
                 into the shell's environment. */
-             for (temp_list = varlist; temp_list; temp_list = temp_list->next)
+             for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
                {
+                 savecmd = this_command_name;
                  this_command_name = (char *)NULL;     /* no arithmetic errors */
-                 tint = do_assignment (temp_list->word->word);
+                 tint = do_word_assignment (temp_list->word, 0);
+                 this_command_name = savecmd;
                  /* Variable assignment errors in non-interactive shells
                     running in Posix.2 mode cause the shell to exit. */
-                 if (tint == 0 && interactive_shell == 0 && posixly_correct)
+                 if (tint == 0)
                    {
                      last_command_exit_value = EXECUTION_FAILURE;
-                     jump_to_top_level (FORCE_EOF);
+                     if (interactive_shell == 0 && posixly_correct)
+                       exp_jump_to_top_level (FORCE_EOF);
+                     else
+                       exp_jump_to_top_level (DISCARD);
                    }
                }
-             dispose_words (varlist);
-             varlist = (WORD_LIST *)NULL;
+             dispose_words (subst_assign_varlist);
+             subst_assign_varlist = (WORD_LIST *)NULL;
            }
          return ((WORD_LIST *)NULL);
        }
@@ -5854,40 +10717,50 @@ expand_word_list_internal (list, eflags)
        new_list = dequote_list (new_list);
     }
 
-  if ((eflags & WEXP_VARASSIGN) && varlist)
+  if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
     {
-      Function *assign_func;
+      sh_wassign_func_t *assign_func;
+      int is_special_builtin, is_builtin_or_func;
 
       /* If the remainder of the words expand to nothing, Posix.2 requires
         that the variable and environment assignments affect the shell's
         environment. */
-      assign_func = new_list ? assign_in_env : do_assignment;
-
-      for (temp_list = varlist; temp_list; temp_list = temp_list->next)
+      assign_func = new_list ? assign_in_env : do_word_assignment;
+      tempenv_assign_error = 0;
+
+      is_builtin_or_func = (new_list && new_list->word && (find_shell_builtin (new_list->word->word) || find_function (new_list->word->word)));
+      /* Posix says that special builtins exit if a variable assignment error
+        occurs in an assignment preceding it. */
+      is_special_builtin = (posixly_correct && new_list && new_list->word && find_special_builtin (new_list->word->word));
+      
+      for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
        {
+         savecmd = this_command_name;
          this_command_name = (char *)NULL;
-         tint = (*assign_func) (temp_list->word->word);
+         assigning_in_environment = (assign_func == assign_in_env);
+         tint = (*assign_func) (temp_list->word, is_builtin_or_func);
+         assigning_in_environment = 0;
+         this_command_name = savecmd;
          /* Variable assignment errors in non-interactive shells running
             in Posix.2 mode cause the shell to exit. */
-         if (tint == 0 && assign_func == do_assignment &&
-               interactive_shell == 0 && posixly_correct)
+         if (tint == 0)
            {
-             last_command_exit_value = EXECUTION_FAILURE;
-             jump_to_top_level (FORCE_EOF);
+             if (assign_func == do_word_assignment)
+               {
+                 last_command_exit_value = EXECUTION_FAILURE;
+                 if (interactive_shell == 0 && posixly_correct && is_special_builtin)
+                   exp_jump_to_top_level (FORCE_EOF);
+                 else
+                   exp_jump_to_top_level (DISCARD);
+               }
+             else
+               tempenv_assign_error++;
            }
        }
 
-      dispose_words (varlist);
-      varlist = (WORD_LIST *)NULL;
+      dispose_words (subst_assign_varlist);
+      subst_assign_varlist = (WORD_LIST *)NULL;
     }
 
-#if 0
-  tint = list_length (new_list) + 1;
-  RESIZE_MALLOCED_BUFFER (glob_argv_flags, 0, tint, glob_argv_flags_size, 16);
-  for (tint = 0, temp_list = new_list; temp_list; temp_list = temp_list->next)
-    glob_argv_flags[tint++] = (temp_list->word->flags & W_GLOBEXP) ? '1' : '0';
-  glob_argv_flags[tint] = '\0';
-#endif
-
   return (new_list);
 }