Imported from ../bash-4.0.tar.gz.
[platform/upstream/bash.git] / subst.c
diff --git a/subst.c b/subst.c
index 9dd00a8..62a3d02 100644 (file)
--- a/subst.c
+++ b/subst.c
-/* 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-2009 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.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
-   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. */
+#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>
-/* Not all systems declare ERRNO in errno.h... and some systems #define it! */
-#if !defined (errno)
-extern int errno;
-#endif /* !errno */
+
+#if defined (HAVE_UNISTD_H)
+#  include <unistd.h>
+#endif
 
 #include "bashansi.h"
 #include "posixstat.h"
+#include "bashintl.h"
 
 #include "shell.h"
 #include "flags.h"
 #include "jobs.h"
 #include "execute_cmd.h"
 #include "filecntl.h"
+#include "trap.h"
+#include "pathexp.h"
+#include "mailcheck.h"
 
-#if defined (READLINE)
-#  include <readline/readline.h>
-#else
-#  include <tilde/tilde.h>
-#endif
-
-#if defined (HISTORY)
-#  include "bashhist.h"
-#  include <readline/history.h>
-#endif
+#include "shmbutil.h"
 
-#include <glob/fnmatch.h>
 #include "builtins/getopt.h"
+#include "builtins/common.h"
 
-/* The size that strings change by. */
-#define DEFAULT_ARRAY_SIZE 512
+#include "builtins/builtext.h"
+
+#include <tilde/tilde.h>
+#include <glob/strmatch.h>
 
-/* How to quote and determine the quoted state of the character C. */
-static char *make_quoted_char ();
-#define QUOTED_CHAR(c)  ((c) == CTLESC)
+#if !defined (errno)
+extern int errno;
+#endif /* !errno */
+
+/* The size that strings change by. */
+#define DEFAULT_INITIAL_ARRAY_SIZE 112
+#define DEFAULT_ARRAY_SIZE 128
+
+/* Variable types. */
+#define VT_VARIABLE    0
+#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 */
+
+/* Flags for the `pflags' argument to param_expand() */
+#define PF_NOCOMSUB    0x01    /* Do not perform command substitution */
+
+/* These defs make it easier to use the editor. */
+#define LBRACE         '{'
+#define RBRACE         '}'
+#define LPAREN         '('
+#define RPAREN         ')'
+
+/* 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) == '#')
+
+/* 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) \
+  ((c) == '#' || (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) \
+ ((DIGIT (*name) && all_digits (name)) || \
+      (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
+      (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))
+
+/* 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];
+
+#if defined (HANDLE_MULTIBYTE)
+unsigned char ifs_firstc[MB_LEN_MAX];
+size_t ifs_firstc_len;
+#else
+unsigned char ifs_firstc;
+#endif
+
+int assigning_in_environment;
 
 /* Extern functions and variables from different files. */
-extern int last_command_exit_value, interactive, interactive_shell;
+extern int last_command_exit_value, last_command_exit_signal;
 extern int subshell_environment;
-extern int dollar_dollar_pid, no_brace_expansion;
+extern int subshell_level, parse_and_execute_level;
+extern int eof_encountered;
+extern int return_catch_flag, return_catch_value;
+extern pid_t dollar_dollar_pid;
 extern int posixly_correct;
-extern int eof_encountered, eof_encountered_limit, ignoreeof;
 extern char *this_command_name;
-extern jmp_buf top_level;
-#if defined (READLINE)
-extern int no_line_editing;
-extern int hostname_list_initialized;
+extern struct fd_bitmap *current_fds_to_close;
+extern int wordexp_only;
+extern int expanding_redir;
+extern int tempenv_assign_error;
+
+#if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
+extern wchar_t *wcsdup __P((const wchar_t *));
 #endif
 
-#if !defined (USE_POSIX_GLOB_LIBRARY)
-extern int glob_dot_filenames, noglob_dot_filenames;
-extern char *glob_error_return;
+/* 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.
+   (CURRENTLY UNUSED). */
+char *glob_argv_flags;
+static int glob_argv_flags_size;
 #endif
 
 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;
+
+/* Tell the expansion functions to not longjmp back to top_level on fatal
+   errors.  Enabled when doing completion and prompt string expansion. */
+static int no_longjmp_on_fatal_error = 0;
+
+/* 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;
+
+/* 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. */
+WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
+
+/* 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 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));
+static int skip_double_quoted __P((char *, size_t, 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 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_pattern_char __P((char *, char *));
+static int match_upattern __P((char *, char *, int, char **, char **));
+#if defined (HANDLE_MULTIBYTE)
+static int match_pattern_wchar __P((wchar_t *, wchar_t *));
+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 *, char *, int, int));
+
+static char *process_substitute __P((char *, int));
+
+static char *read_comsub __P((int, int, int *));
+
+#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));
+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 *));
+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 *, int, SHELL_VAR **, char **));
+static char *mb_substring __P((char *, int, int));
+static char *parameter_brace_substring __P((char *, char *, char *, int));
+
+static char *pos_params_pat_subst __P((char *, char *, char *, int));
+
+static char *parameter_brace_patsub __P((char *, char *, char *, int));
+
+static char *pos_params_casemod __P((char *, char *, int, int));
+static char *parameter_brace_casemod __P((char *, char *, int, char *, int));
+
+static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int *, int *));
+static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
 
-static WORD_LIST *expand_string_internal ();
-static WORD_LIST *expand_word_internal (), *expand_words_internal ();
-static WORD_LIST *expand_string_leave_quoted ();
-static WORD_LIST *word_list_split ();
-static char *quote_string ();
-static int unquoted_substring (), unquoted_member ();
-static int unquoted_glob_pattern_p ();
-static void quote_list (), dequote_list ();
-static int do_assignment_internal ();
-static char *string_extract_verbatim (), *string_extract ();
-static char *string_extract_double_quoted (), *string_extract_single_quoted ();
-static char *extract_delimited_string ();
-static char *extract_dollar_brace_string ();
+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 *));
+#endif
+static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
+static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
 
 /* **************************************************************** */
 /*                                                                 */
@@ -103,329 +309,897 @@ 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)
+#ifdef INCLUDE_UNUSED
+static char *
+quoted_substring (string, start, end)
      char *string;
      int start, end;
 {
-  register int len = end - start;
-  register char *result = xmalloc (len + 1);
+  register int len, l;
+  register char *result, *s, *r;
 
-  strncpy (result, string + start, len);
-  result[len] = '\0';
-  return (result);
+  len = end - start;
+
+  /* Move to string[start], skipping quoted characters. */
+  for (s = string, l = 0; *s && l < start; )
+    {
+      if (*s == CTLESC)
+       {
+         s++;
+         continue;
+       }
+      l++;
+      if (*s == 0)
+       break;
+    }
+
+  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;
+      l++;
+      if (*s == 0)
+       break;
+    }
+  *r = '\0';
+  return result;
 }
+#endif
 
-/* Conventions:
+#ifdef INCLUDE_UNUSED
+/* Return the length of S, skipping over quoted characters */
+static int
+quoted_strlen (s)
+     char *s;
+{
+  register char *p;
+  int i;
 
-     A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
-     The parser passes CTLNUL as CTLESC CTLNUL. */
+  i = 0;
+  for (p = s; *p; p++)
+    {
+      if (*p == CTLESC)
+       {
+         p++;
+         if (*p == 0)
+           return (i + 1);
+       }
+      i++;
+    }
 
-/* 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. */
+  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 char *
-remove_quoted_escapes (string)
-     char *string;
+quoted_strchr (s, c, flags)
+     char *s;
+     int c, flags;
 {
-  register char *s;
+  register char *p;
 
-  for (s = string; s && *s; s++)
+  for (p = s; *p; p++)
     {
-      if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL))
-       strcpy (s, s + 1);      /* XXX - should be memmove */
+      if (((flags & ST_BACKSL) && *p == '\\')
+           || ((flags & ST_CTLESC) && *p == CTLESC))
+       {
+         p++;
+         if (*p == '\0')
+           return ((char *)NULL);
+         continue;
+       }
+      else if (*p == c)
+       return p;
     }
-  return (string);
+  return ((char *)NULL);
 }
 
-/* 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 *
-quote_escapes (string)
+/* Return 1 if CHARACTER appears in an unquoted portion of
+   STRING.  Return 0 otherwise.  CHARACTER must be a single-byte character. */
+static int
+unquoted_member (character, string)
+     int character;
      char *string;
 {
-  register char *s, *t;
-  char *result;
+  size_t slen;
+  int sindex, c;
+  DECLARE_MBSTATE;
 
-  result = xmalloc ((strlen (string) * 2) + 1);
-  for (s = string, t = result; s && *s; )
+  slen = strlen (string);
+  sindex = 0;
+  while (c = string[sindex])
     {
-      if (*s == CTLESC || *s == CTLNUL)
-       *t++ = CTLESC;
-      *t++ = *s++;
+      if (c == character)
+       return (1);
+
+      switch (c)
+       {
+       default:
+         ADVANCE_CHAR (string, slen, sindex);
+         break;
+
+       case '\\':
+         sindex++;
+         if (string[sindex])
+           ADVANCE_CHAR (string, slen, sindex);
+         break;
+
+       case '\'':
+         sindex = skip_single_quoted (string, slen, ++sindex);
+         break;
+
+       case '"':
+         sindex = skip_double_quoted (string, slen, ++sindex);
+         break;
+       }
     }
-  *t = '\0';
-  return (result);
-}      
+  return (0);
+}
 
-/* Just like string_extract, but doesn't hack backslashes or any of
-   that other stuff.  Obeys quoting.  Used to do splitting on $IFS. */
-static char *
-string_extract_verbatim (string, sindex, charlist)
-     char *string, *charlist;
-     int *sindex;
+/* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
+static int
+unquoted_substring (substr, string)
+     char *substr, *string;
 {
-  register int i = *sindex;
-  int c;
-  char *temp;
+  size_t slen;
+  int sindex, c, sublen;
+  DECLARE_MBSTATE;
 
-  if (charlist[0] == '\'' && !charlist[1])
+  if (substr == 0 || *substr == '\0')
+    return (0);
+
+  slen = strlen (string);
+  sublen = strlen (substr);
+  for (sindex = 0; c = string[sindex]; )
     {
-      temp = string_extract_single_quoted (string, sindex);
-      i = *sindex - 1;
-      *sindex = i;
-      return (temp);
+      if (STREQN (string + sindex, substr, sublen))
+       return (1);
+
+      switch (c)
+       {
+       case '\\':
+         sindex++;
+
+         if (string[sindex])
+           ADVANCE_CHAR (string, slen, sindex);
+         break;
+
+       case '\'':
+         sindex = skip_single_quoted (string, slen, ++sindex);
+         break;
+
+       case '"':
+         sindex = skip_double_quoted (string, slen, ++sindex);
+         break;
+
+       default:
+         ADVANCE_CHAR (string, slen, sindex);
+         break;
+       }
     }
+  return (0);
+}
+
+/* Most of the substitutions must be done in parallel.  In order
+   to avoid using tons of unclear goto's, I have some functions
+   for manipulating malloc'ed strings.  They all take INDX, a
+   pointer to an integer which is the offset into the string
+   where manipulation is taking place.  They also take SIZE, a
+   pointer to an integer which is the current length of the
+   character array for this string. */
 
-  for (i = *sindex; (c = string[i]); i++)
+/* Append SOURCE to TARGET at INDEX.  SIZE is the current amount
+   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 *
+sub_append_string (source, target, indx, size)
+     char *source, *target;
+     int *indx, *size;
+{
+  if (source)
     {
-      if (c == CTLESC)
+      int srclen, n;
+
+      srclen = STRLEN (source);
+      if (srclen >= (int)(*size - *indx))
        {
-         i++;
-         continue;
+         n = srclen + *indx;
+         n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
+         target = (char *)xrealloc (target, (*size = n));
        }
 
-      if (MEMBER (c, charlist))
-       break;
+      FASTCOPY (source, target + *indx, srclen);
+      *indx += srclen;
+      target[*indx] = '\0';
+
+      free (source);
     }
+  return (target);
+}
 
-  temp = xmalloc (1 + (i - *sindex));
-  strncpy (temp, string + (*sindex), i - (*sindex));
-  temp[i - (*sindex)] = '\0';
-  *sindex = i;
+#if 0
+/* UNUSED */
+/* Append the textual representation of NUMBER to TARGET.
+   INDX and SIZE are as in SUB_APPEND_STRING. */
+char *
+sub_append_number (number, target, indx, size)
+     intmax_t number;
+     int *indx, *size;
+     char *target;
+{
+  char *temp;
 
-  return (temp);
+  temp = itos (number);
+  return (sub_append_string (temp, target, indx, size));
 }
+#endif
 
 /* 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. */
+   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)
-     char *string, *charlist;
+string_extract (string, sindex, charlist, flags)
+     char *string;
      int *sindex;
+     char *charlist;
+     int flags;
 {
-  register int c, i = *sindex;
+  register int c, i;
+  int found;
+  size_t slen;
   char *temp;
+  DECLARE_MBSTATE;
 
+  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;
-      else
-       if (MEMBER (c, charlist))
+       {
+         if (string[i + 1])
+           i++;
+         else
+           break;
+       }
+#if defined (ARRAY_VARS)
+      else if ((flags & SX_VARNAME) && c == '[')
+       {
+         int ni;
+         /* If this is an array subscript, skip over it and continue. */
+         ni = skipsubscript (string, i);
+         if (string[ni] == ']')
+           i = ni;
+       }
+#endif
+      else if (MEMBER (c, charlist))
+       {
+         found = 1;
          break;
-      i++;
+       }
+
+      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);
     }
-  temp = xmalloc (1 + (i - *sindex));
-  strncpy (temp, string + (*sindex), i - (*sindex));
-  temp[i - (*sindex)] = '\0';
+  
+  temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
   *sindex = i;
+  
   return (temp);
 }
 
-/* Remove backslashes which are quoting backquotes from STRING.  Modifies
-   STRING, and returns a pointer to it. */
-char *
-de_backslash (string)
-     char *string;
-{
-  register int i, l = strlen (string);
+/* Extract the contents of STRING as if it is enclosed in double quotes.
+   SINDEX, when passed in, is the offset of the character immediately
+   following the opening double quote; on exit, SINDEX is left pointing after
+   the closing double quote.  If STRIPDQ is non-zero, unquoted double
+   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 char *
+string_extract_double_quoted (string, sindex, stripdq)
+     char *string;
+     int *sindex, stripdq;
+{
+  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;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string + *sindex) + *sindex;
+  send = string + slen;
+
+  pass_next = backquote = dquote = 0;
+  temp = (char *)xmalloc (1 + slen - *sindex);
+
+  j = 0;
+  i = *sindex;
+  while (c = string[i])
+    {
+      /* Process a character that was quoted by a backslash. */
+      if (pass_next)
+       {
+         /* Posix.2 sez:
 
-  for (i = 0; i < l; i++)
-    if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
-                             string[i + 1] == '$'))
-      strcpy (string + i, string + i + 1);     /* XXX - should be memmove */
-  return (string);
+            ``The backslash shall retain its special meaning as an escape
+            character only when followed by one of the characters:
+               $       `       "       \       <newline>''.
+
+            If STRIPDQ is zero, we handle the double quotes here and let
+            expand_word_internal handle the rest.  If STRIPDQ is non-zero,
+            we have already been through one round of backslash stripping,
+            and want to strip these backslashes only if DQUOTE is non-zero,
+            indicating that we are inside an embedded double-quoted string. */
+
+            /* If we are in an embedded quoted string, then don't strip
+               backslashes before characters for which the backslash
+               retains its special meaning, but remove backslashes in
+               front of other characters.  If we are not in an
+               embedded quoted string, don't strip backslashes at all.
+               This mess is necessary because the string was already
+               surrounded by double quotes (and sh has some really weird
+               quoting rules).
+               The returned string will be run through expansion as if
+               it were double-quoted. */
+         if ((stripdq == 0 && c != '"') ||
+             (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
+           temp[j++] = '\\';
+         pass_next = 0;
+
+add_one_character:
+         COPY_CHAR_I (temp, j, string, send, i);
+         continue;
+       }
+
+      /* A backslash protects the next character.  The code just above
+        handles preserving the backslash in front of any character but
+        a double quote. */
+      if (c == '\\')
+       {
+         pass_next++;
+         i++;
+         continue;
+       }
+
+      /* Inside backquotes, ``the portion of the quoted string from the
+        initial backquote and the characters up to the next backquote
+        that is not preceded by a backslash, having escape characters
+        removed, defines that command''. */
+      if (backquote)
+       {
+         if (c == '`')
+           backquote = 0;
+         temp[j++] = c;
+         i++;
+         continue;
+       }
+
+      if (c == '`')
+       {
+         temp[j++] = c;
+         backquote++;
+         i++;
+         continue;
+       }
+
+      /* Pass everything between `$(' and the matching `)' or a quoted
+        ${ ... } 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_command_subst (string, &si, 0);
+         else
+           ret = extract_dollar_brace_string (string, &si, 1, 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];
+
+         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. */
+      if (c != '"')
+       goto add_one_character;
+
+      /* c == '"' */
+      if (stripdq)
+       {
+         dquote ^= 1;
+         i++;
+         continue;
+       }
+
+      break;
+    }
+  temp[j] = '\0';
+
+  /* Point to after the closing quote. */
+  if (c)
+    i++;
+  *sindex = i;
+
+  return (temp);
 }
 
-#if 0
-/* Replace instances of \! in a string with !. */
-void
-unquote_bang (string)
+/* This should really be another option to string_extract_double_quoted. */
+static int
+skip_double_quoted (string, slen, sind)
      char *string;
+     size_t slen;
+     int sind;
 {
-  register int i, j;
-  register char *temp;
-
-  temp = xmalloc (1 + strlen (string));
+  int c, i;
+  char *ret;
+  int pass_next, backquote, si;
+  DECLARE_MBSTATE;
 
-  for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
+  pass_next = backquote = 0;
+  i = sind;
+  while (c = string[i])
     {
-      if (string[i] == '\\' && string[i + 1] == '!')
+      if (pass_next)
        {
-         temp[j] = '!';
+         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_command_subst (string, &si, SX_NOALLOC);
+         else
+           ret = extract_dollar_brace_string (string, &si, 1, SX_NOALLOC);
+
+         i = si + 1;
+         continue;
+       }
+      else if (c != '"')
+       {
+         ADVANCE_CHAR (string, slen, i);
+         continue;
        }
+      else
+       break;
     }
-  strcpy (string, temp);
-  free (temp);
+
+  if (c)
+    i++;
+
+  return (i);
+}
+
+/* Extract the contents of STRING as if it is enclosed in single quotes.
+   SINDEX, when passed in, is the offset of the character immediately
+   following the opening single quote; on exit, SINDEX is left pointing after
+   the closing single quote. */
+static inline char *
+string_extract_single_quoted (string, sindex)
+     char *string;
+     int *sindex;
+{
+  register int i;
+  size_t slen;
+  char *t;
+  DECLARE_MBSTATE;
+
+  /* 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);
+
+  t = substring (string, *sindex, i);
+
+  if (string[i])
+    i++;
+  *sindex = i;
+
+  return (t);
+}
+
+static inline int
+skip_single_quoted (string, slen, sind)
+     const char *string;
+     size_t slen;
+     int sind;
+{
+  register int c;
+  DECLARE_MBSTATE;
+
+  c = sind;
+  while (string[c] && string[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 CTLESC quoting.  Used to do splitting on $IFS. */
+static char *
+string_extract_verbatim (string, slen, sindex, charlist, flags)
+     char *string;
+     size_t slen;
+     int *sindex;
+     char *charlist;
+     int flags;
+{
+  register int i = *sindex;
+#if defined (HANDLE_MULTIBYTE)
+  size_t clen;
+  wchar_t *wcharlist;
+#endif
+  int c;
+  char *temp;
+  DECLARE_MBSTATE;
+
+  if (charlist[0] == '\'' && charlist[1] == '\0')
+    {
+      temp = string_extract_single_quoted (string, sindex);
+      --*sindex;       /* leave *sindex at separator character */
+      return temp;
+    }
+
+  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 defined (HANDLE_MULTIBYTE)
+      size_t mblength;
+#endif
+      if ((flags & SX_NOCTLESC) == 0 && c == CTLESC)
+       {
+         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);
+    }
+
+#if defined (HANDLE_MULTIBYTE)
+  FREE (wcharlist);
 #endif
 
+  temp = substring (string, *sindex, i);
+  *sindex = i;
+
+  return (temp);
+}
+
 /* 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 just after 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, "$(", "(", ")"));
+  if (string[*sindex] == '(')  /*)*/
+    return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/
+  else
+    {
+      xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
+      return (xparse_dolparen (string, string+*sindex, sindex, xflags));
+    }
 }
 
-/* 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 just after the matching "]". */
+   Make (SINDEX) get the position of the matching "]". */
 char *
 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)
 /* Extract the <( or >( construct in STRING, and return a new string.
    Start extracting at (SINDEX) as if we had just seen "<(".
-   Make (SINDEX) get the position just after the matching ")". */
+   Make (SINDEX) get the position of the matching ")". */ /*))*/
 char *
 extract_process_subst (string, starter, sindex)
      char *string;
      char *starter;
      int *sindex;
 {
-  return (extract_delimited_string (string, sindex, starter, "(", ")"));
+  return (extract_delimited_string (string, sindex, starter, "(", ")", 0));
 }
 #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;
+{
+  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
+
 /* Extract and create a new string from the contents of STRING, a
    character string delimited with OPENER and CLOSER.  SINDEX is
    the address of an int describing the current offset in STRING;
    it should point to just after the first OPENER found.  On exit,
-   SINDEX gets the position just after the matching CLOSER.  If
-   OPENER is more than a single character, ALT_OPENER, if non-null,
+   SINDEX gets the position of the last character of the matching CLOSER.
+   If OPENER is more than a single character, ALT_OPENER, if non-null,
    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;
 {
-  register int i, c, l;
-  int pass_character, nesting_level;
-  int delimiter, delimited_nesting_level;
+  int i, c, si;
+  size_t slen;
+  char *t, *result;
+  int pass_character, nesting_level, in_comment;
   int len_closer, len_opener, len_alt_opener;
-  char *result;
+  DECLARE_MBSTATE;
 
+  slen = strlen (string + *sindex) + *sindex;
   len_opener = STRLEN (opener);
   len_alt_opener = STRLEN (alt_opener);
   len_closer = STRLEN (closer);
 
-  pass_character = delimiter = delimited_nesting_level = 0;
+  pass_character = in_comment = 0;
 
   nesting_level = 1;
+  i = *sindex;
 
-  for (i = *sindex; c = string[i]; i++)
+  while (nesting_level)
     {
-      if (pass_character)
+      c = string[i];
+
+      if (c == 0)
+       break;
+
+      if (in_comment)
+       {
+         if (c == '\n')
+           in_comment = 0;
+         ADVANCE_CHAR (string, slen, i);
+         continue;
+       }
+
+      if (pass_character)      /* previous char was backslash */
        {
          pass_character = 0;
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
 
-      if (c == CTLESC)
+      /* Not exactly right yet; should handle shell metacharacters and
+        multibyte characters, too. */
+      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;
        }
 
-      if (c == '\\')
+      /* Process a nested OPENER. */
+      if (STREQN (string + i, opener, len_opener))
        {
-         if ((delimiter == '"') &&
-             (member (string[i + 1], slashify_in_quotes)))
-           {
-             pass_character++;
-             continue;
-           }
+         si = i + len_opener;
+         t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC);
+         i = si + 1;
+         continue;
        }
 
-      if (!delimiter || delimiter == '"')
+      /* Process a nested ALT_OPENER */
+      if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
        {
-         if (STREQN (string + i, opener, len_opener))
-           {
-             if (!delimiter)
-               nesting_level++;
-             else
-               delimited_nesting_level++;
+         si = i + len_alt_opener;
+         t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC);
+         i = si + 1;
+         continue;
+       }
 
-             i += len_opener - 1;
-             continue;
-           }
+      /* If the current substring terminates the delimited string, decrement
+        the nesting level. */
+      if (STREQN (string + i, closer, len_closer))
+       {
+         i += len_closer - 1;  /* move to last byte of the closer */
+         nesting_level--;
+         if (nesting_level == 0)
+           break;
+       }
 
-         if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
-           {
-             if (!delimiter)
-               nesting_level++;
-             else
-               delimited_nesting_level++;
-
-             i += len_alt_opener - 1;
-             continue;
-           }
-
-         if (STREQN (string + i, closer, len_closer))
-           {
-             i += len_closer - 1;
-
-             if (delimiter && delimited_nesting_level)
-               delimited_nesting_level--;
-
-             if (!delimiter)
-               {
-                 nesting_level--;
-                 if (nesting_level == 0)
-                   break;
-               }
-           }
+      /* Pass old-style command substitution through verbatim. */
+      if (c == '`')
+       {
+         si = i + 1;
+         t = string_extract (string, &si, "`", flags|SX_NOALLOC);
+         i = si + 1;
+         continue;
        }
 
-      if (delimiter)
+      /* Pass single-quoted and double-quoted strings through verbatim. */
+      if (c == '\'' || c == '"')
        {
-         if (c == delimiter || delimiter == '\\')
-           delimiter = 0;
+         si = i + 1;
+         i = (c == '\'') ? skip_single_quoted (string, slen, si)
+                         : skip_double_quoted (string, slen, si);
          continue;
        }
+
+      /* move past this character, which was not special. */
+      ADVANCE_CHAR (string, slen, i);
+    }
+
+  if (c == 0 && nesting_level)
+    {
+      if (no_longjmp_on_fatal_error == 0)
+       {
+         report_error (_("bad substitution: no closing `%s' in %s"), closer, string);
+         last_command_exit_value = EXECUTION_FAILURE;
+         exp_jump_to_top_level (DISCARD);
+       }
       else
        {
-         if (c == '"' || c == '\'' || c == '\\')
-           delimiter = c;
+         *sindex = i;
+         return (char *)NULL;
        }
     }
 
-  l = i - *sindex;
-  result = xmalloc (1 + l);
-  strncpy (result, string + *sindex, l);
-  result[l] = '\0';
-  *sindex = i;
-
-  if (!c && (delimiter || nesting_level))
+  si = i - *sindex - len_closer + 1;
+  if (flags & SX_NOALLOC)
+    result = (char *)NULL;
+  else    
     {
-      report_error ("bad substitution: no `%s' in %s", closer, string);
-      free (result);
-      longjmp (top_level, DISCARD);
+      result = (char *)xmalloc (1 + si);
+      strncpy (result, string + *sindex, si);
+      result[si] = '\0';
     }
+  *sindex = i;
+
   return (result);
 }
 
@@ -434,390 +1208,551 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer)
    skipping over enclosed quoted strings and command substitutions.
    SINDEX is the address of an int describing the current offset in STRING;
    it should point to just after the first `{' found.  On exit, SINDEX
-   gets the position just after the matching `}'. */
+   gets the position of the matching `}'.  QUOTED is non-zero if this
+   occurs inside double quotes. */
 /* XXX -- this is very similar to extract_delimited_string -- XXX */
 static char *
-extract_dollar_brace_string (string, sindex)
+extract_dollar_brace_string (string, sindex, quoted, flags)
      char *string;
-     int *sindex;
+     int *sindex, quoted, flags;
 {
-  register int i, c, l;
-  int pass_character, nesting_level;
-  int delimiter, delimited_nesting_level;
-  char *result;
-
-  pass_character = delimiter = delimited_nesting_level = 0;
+  register int i, c;
+  size_t slen;
+  int pass_character, nesting_level, si;
+  char *result, *t;
+  DECLARE_MBSTATE;
 
+  pass_character = 0;
   nesting_level = 1;
+  slen = strlen (string + *sindex) + *sindex;
 
-  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;
        }
 
-      if (c == CTLESC)
+      /* CTLESCs and backslashes quote the next character. */
+      if (c == CTLESC || c == '\\')
        {
          pass_character++;
+         i++;
          continue;
        }
 
-      /* Backslashes quote the next character. */
-      if (c == '\\')
+      if (string[i] == '$' && string[i+1] == LBRACE)
        {
-         if ((delimiter == '"') &&
-             (member (string[i + 1], slashify_in_quotes)))
-           {
-             pass_character++;
-             continue;
-           }
+         nesting_level++;
+         i += 2;
+         continue;
        }
 
-      if (!delimiter || delimiter == '"')
+      if (c == RBRACE)
        {
-         if (string[i] == '$' && string[i+1] == '{')
-           {
-             if (!delimiter)
-               nesting_level++;
-             else
-               delimited_nesting_level++;
-
-             i++;
-             continue;
-           }
-
-         /* Pass the contents of old-style command substitutions through
-            verbatim. */
-         if (string[i] == '`')
-           {
-             int si;
-             char *t;
-
-             si = i + 1;
-             t = string_extract (string, &si, "`");
-             i = si;
-             free (t);
-             continue;
-           }
-
-         /* Pass the contents of new-style command substitutions through
-            verbatim. */
-         if (string[i] == '$' && string[i+1] == '(')
-           {
-             int si;
-             char *t;
-
-             si = i + 2;
-             t = extract_delimited_string (string, &si, "$(", "(", ")");
-             i = si;
-             free (t);
-             continue;
-           }
-
-         if (string[i] == '{')
-           {
-             if (!delimiter)
-               nesting_level++;
-             else
-               delimited_nesting_level++;
-
-             continue;
-           }
+         nesting_level--;
+         if (nesting_level == 0)
+           break;
+         i++;
+         continue;
+       }
 
-         if (string[i] == '}')
-           {
-             if (delimiter && delimited_nesting_level)
-               delimited_nesting_level--;
+      /* Pass the contents of old-style command substitutions through
+        verbatim. */
+      if (c == '`')
+       {
+         si = i + 1;
+         t = string_extract (string, &si, "`", flags|SX_NOALLOC);
+         i = si + 1;
+         continue;
+       }
 
-             if (!delimiter)
-               {
-                 nesting_level--;
-                 if (nesting_level == 0)
-                   break;
-               }
-           }
+      /* Pass the contents of new-style command substitutions and
+        arithmetic substitutions through verbatim. */
+      if (string[i] == '$' && string[i+1] == LPAREN)
+       {
+         si = i + 2;
+         t = extract_command_subst (string, &si, flags|SX_NOALLOC);
+         i = si + 1;
+         continue;
        }
 
-      if (delimiter)
+      /* Pass the contents of single-quoted and double-quoted strings
+        through verbatim. */
+      if (c == '\'' || c == '"')
        {
-         if (c == delimiter || delimiter == '\\')
-           delimiter = 0;
+         si = i + 1;
+         i = (c == '\'') ? skip_single_quoted (string, slen, si)
+                         : skip_double_quoted (string, slen, si);
+         /* skip_XXX_quoted leaves index one past close quote */
          continue;
        }
+
+      /* move past this character, which was not special. */
+      ADVANCE_CHAR (string, slen, i);
+    }
+
+  if (c == 0 && nesting_level)
+    {
+      if (no_longjmp_on_fatal_error == 0)
+       {                       /* { */
+         report_error (_("bad substitution: no closing `%s' in %s"), "}", string);
+         last_command_exit_value = EXECUTION_FAILURE;
+         exp_jump_to_top_level (DISCARD);
+       }
       else
        {
-         if (c == '"' || c == '\'' || c == '\\')
-           delimiter = c;
+         *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;
 
-  if (!c && (delimiter || nesting_level))
+  return (result);
+}
+
+/* Remove backslashes which are quoting backquotes from STRING.  Modifies
+   STRING, and returns a pointer to it. */
+char *
+de_backslash (string)
+     char *string;
+{
+  register size_t slen;
+  register int i, j, prev_i;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string);
+  i = j = 0;
+
+  /* Loop copying string[i] to string[j], i >= j. */
+  while (i < slen)
     {
-      report_error ("bad substitution: no ending `}' in %s", string);
-      free (result);
-      longjmp (top_level, DISCARD);
+      if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
+                             string[i + 1] == '$'))
+       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;
     }
-  return (result);
+  string[j] = '\0';
+
+  return (string);
 }
 
-/* Extract the contents of STRING as if it is enclosed in double quotes.
-   SINDEX, when passed in, is the offset of the character immediately
-   following the opening double quote; on exit, SINDEX is left pointing after
-   the closing double quote. */
-static char *
-string_extract_double_quoted (string, sindex)
+#if 0
+/*UNUSED*/
+/* Replace instances of \! in a string with !. */
+void
+unquote_bang (string)
      char *string;
-     int *sindex;
 {
-  register int c, j, i;
-  char *temp;                  /* The new string we return. */
-  int pass_next, backquote;    /* State variables for the machine. */
+  register int i, j;
+  register char *temp;
 
-  pass_next = backquote = 0;
-  temp = xmalloc (1 + strlen (string) - *sindex);
+  temp = (char *)xmalloc (1 + strlen (string));
 
-  for (j = 0, i = *sindex; c = string[i]; i++)
+  for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
     {
-      /* Process a character that was quoted by a backslash. */
-      if (pass_next)
+      if (string[i] == '\\' && string[i + 1] == '!')
        {
-         /* Posix.2 sez:
+         temp[j] = '!';
+         i++;
+       }
+    }
+  strcpy (string, temp);
+  free (temp);
+}
+#endif
 
-            ``The backslash shall retain its special meaning as an escape
-            character only when followed by one of the characters:
-               $       `       "       \       <newline>''.
+#define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0)
 
-            We handle the double quotes here.  expand_word_internal handles
-            the rest. */
-         if (c != '"')
-           temp[j++] = '\\';
-         temp[j++] = c;
+/* 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, si, c, invert;
+  size_t slen;
+  char *temp;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string + start) + start;
+  if (flags & SD_NOJMP)
+    no_longjmp_on_fatal_error = 1;
+  invert = (flags & SD_INVERT);
+
+  i = start;
+  pass_next = backq = 0;
+  while (c = string[i])
+    {
+      if (pass_next)
+       {
          pass_next = 0;
+         if (c == 0)
+           CQ_RETURN(i);
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
-
-      /* A backslash protects the next character.  The code just above
-        handles preserving the backslash in front of any character but
-        a double quote. */
-      if (c == '\\')
+      else if (c == '\\')
        {
-         pass_next++;
+         pass_next = 1;
+         i++;
          continue;
        }
-
-      /* Inside backquotes, ``the portion of the quoted string from the
-        initial backquote and the characters up to the next backquote
-        that is not preceded by a backslash, having escape characters
-        removed, defines that command''. */
-      if (backquote)
+      else if (backq)
        {
          if (c == '`')
-           backquote = 0;
-         temp[j++] = c;
+           backq = 0;
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
-
-      if (c == '`')
+      else if (c == '`')
        {
-         temp[j++] = c;
-         backquote++;
+         backq = 1;
+         i++;
          continue;
        }
-
-      /* Pass everything between `$(' and the matching `)' or a quoted
-        ${ ... } pair through according to the Posix.2 specification. */
-      if (c == '$' && ((string[i + 1] == '(') || (string[i + 1] == '{')))
+      else if (invert == 0 && member (c, delims))
+       break;
+      else if (c == '\'' || c == '"')
+       {
+         i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
+                         : skip_double_quoted (string, slen, ++i);
+         /* no increment, the skip functions increment past the closing quote. */
+       }
+      else if (c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
        {
-         register int t;
-         int si;
-         char *ret;
-
          si = i + 2;
-         if (string[i + 1] == '(')
-           ret = extract_delimited_string (string, &si, "$(", "(", ")");
-         else
-           ret = extract_dollar_brace_string (string, &si);
-
-         temp[j++] = '$';
-         temp[j++] = string[i + 1];
-
-         for (t = 0; ret[t]; t++)
-           temp[j++] = ret[t];
+         if (string[si] == '\0')
+           CQ_RETURN(si);
 
+         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;
-         temp[j++] = string[i];
-         free (ret);
+         if (string[i] == '\0')        /* don't increment i past EOS in loop */
+           break;
+         i++;
          continue;
        }
-
-      /* An unescaped double quote serves to terminate the string. */
-      if (c == '"')
+      else if (invert && (member (c, delims) == 0))
        break;
-
-      /* Add the character to the quoted string we're accumulating. */
-      temp[j++] = c;
+      else
+       ADVANCE_CHAR (string, slen, i);
     }
-  temp[j] = '\0';
-
-  /* Point to after the closing quote. */
-  if (c)
-    i++;
-  *sindex = i;
-
-  return (temp);
-}
-
-/* Extract the contents of STRING as if it is enclosed in single quotes.
-   SINDEX, when passed in, is the offset of the character immediately
-   following the opening single quote; on exit, SINDEX is left pointing after
-   the closing single quote. */
-static char *
-string_extract_single_quoted (string, sindex)
-     char *string;
-     int *sindex;
-{
-  register int i = *sindex;
-  char *temp;
-
-  while (string[i] && string[i] != '\'')
-    i++;
-
-  temp = xmalloc (1 + i - *sindex);
-  strncpy (temp, string + *sindex, i - *sindex);
-  temp[i - *sindex] = '\0';
-
-  if (string[i])
-    i++;
-  *sindex = i;
 
-  return (temp);
+  CQ_RETURN(i);
 }
 
+#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. */
+   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, quoted;
-  char *temp;
-
-  for (i = pass_next = quoted = 0; i <= eindex; i++)
+  int i, pass_next, c;
+  size_t slen;
+  DECLARE_MBSTATE;
+
+  slen = strlen (string);
+  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) */
-           return 1;
+           CQ_RETURN(1);
+         ADVANCE_CHAR (string, slen, i);
          continue;
        }
-      else if (string[i] == '\'')
-        {
-          i++;
-          temp = string_extract_single_quoted (string, &i);
-          free (temp);
-          if (i > eindex)
-            return 1;
-          i--;
-        }
-      else if (string[i] == '"')
-        {
-          i++;
-          temp = string_extract_double_quoted (string, &i);
-          free (temp);
-          if (i > eindex)
-            return 1;
-          i--;
-        }
-      else if (string[i] == '\\')
-        {
-          pass_next = 1;
-          continue;
-        }
+      else if (c == '\\')
+       {
+         pass_next = 1;
+         i++;
+         continue;
+       }
+      else if (c == '\'' || c == '"')
+       {
+         i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
+                         : skip_double_quoted (string, slen, ++i);
+         if (i > eindex)
+           CQ_RETURN(1);
+         /* no increment, the skip_xxx functions go one past end */
+       }
+      else
+       ADVANCE_CHAR (string, slen, i);
     }
-  return (0);
+
+  CQ_RETURN(0);
 }
 
-#if defined (READLINE)
 int
 unclosed_pair (string, eindex, openstr)
      char *string;
      int eindex;
      char *openstr;
 {
-  int i, pass_next, openc, c, olen;
-  char *temp, *s;
+  int i, pass_next, openc, olen;
+  size_t slen;
+  DECLARE_MBSTATE;
 
+  slen = strlen (string);
   olen = strlen (openstr);
-  for (i = pass_next = openc = 0; i <= eindex; i++)
+  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 (STREQN (string + i, openstr, olen))
+      else if (string[i] == '\\')
        {
-         openc = 1 - openc;
-         i += olen - 1;
+         pass_next = 1;
+         i++;
+         continue;
        }
-      else if (string[i] == '\'')
+      else if (STREQN (string + i, openstr, olen))
        {
-         i++;
-         temp = string_extract_single_quoted (string, &i);
-         free (temp);
-         if (i > eindex)
-           return 0;
+         openc = 1 - openc;
+         i += olen;
        }
-      else if (string[i] == '"')
+      else if (string[i] == '\'' || string[i] == '"')
        {
-         i++;
-         temp = string_extract_double_quoted (string, &i);
-         free (temp);
+         i = (string[i] == '\'') ? skip_single_quoted (string, slen, i)
+                                 : skip_double_quoted (string, slen, i);
          if (i > eindex)
            return 0;
        }
-      else if (string[i] == '\\')
-       {
-         pass_next = 1;
-         continue;
-       }
+      else
+       ADVANCE_CHAR (string, slen, i);
     }
   return (openc);
 }
-#endif /* READLINE */
 
-/* Extract the name of the variable to bind to from the assignment string. */
-char *
-assignment_name (string)
+/* 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, nwp, cwp)
      char *string;
+     int slen;
+     char *delims;
+     int sentinel;
+     int *nwp, *cwp;
 {
-  int offset = assignment (string);
-  char *temp;
+  int ts, te, i, nw, cw, ifs_split;
+  char *token, *d, *d2;
+  WORD_LIST *ret, *tl;
 
-  if (!offset)
-    return (char *)NULL;
-  temp = xmalloc (offset + 1);
-  strncpy (temp, string, offset);
-  temp[offset] = '\0';
-  return (temp);
-}
+  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 whitspace 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')
+    return (ret);
+
+  ts = i;
+  nw = 0;
+  cw = -1;
+  while (1)
+    {
+      te = skip_to_delim (string, ts, d, SD_NOJMP);
+
+      /* 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, add an additional null argument and set the current
+     word pointer to that. */
+  if (cwp && cw == -1 && sentinel >= slen)
+    {
+      if (whitespace (string[sentinel - 1]))
+       {
+         token = "";
+         ret = add_string_to_list (token, ret);
+         nw++;
+       }
+      cw = nw;
+    }
+
+  if (nwp)
+    *nwp = nw;
+  if (cwp)
+    *cwp = cw;
+
+  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. */
-static char *
+char *
 string_list_internal (list, sep)
      WORD_LIST *list;
      char *sep;
@@ -826,9 +1761,13 @@ string_list_internal (list, sep)
   char *result, *r;
   int word_len, sep_len, result_size;
 
-  if (!list)
+  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;
@@ -840,14 +1779,19 @@ 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)
     {
       if (t != list && sep_len)
        {
-         FASTCOPY (sep, r, sep_len);
-         r += sep_len;
+         if (sep_len > 1)
+           {
+             FASTCOPY (sep, r, sep_len);
+             r += sep_len;
+           }
+         else
+           *r++ = sep[0];
        }
 
       word_len = strlen (t->word->word);
@@ -855,7 +1799,7 @@ string_list_internal (list, sep)
       r += word_len;
     }
 
-  *r = '\0';    
+  *r = '\0';
   return (result);
 }
 
@@ -868,6 +1812,42 @@ 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
@@ -877,19 +1857,163 @@ char *
 string_list_dollar_star (list)
      WORD_LIST *list;
 {
-  char *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
 
-  if (!ifs)
-    sep[0] = ' ';
-  else if (!*ifs)
-    sep[0] = '\0';
+#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
-    sep[0] = *ifs;
+    {
+      memcpy (sep, ifs_firstc, ifs_firstc_len);
+      sep[ifs_firstc_len] = '\0';
+    }
+#else
+  sep[0] = ifs_firstc;
+  sep[1] = '\0';
+#endif
 
+  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))
+   is non-zero, the $@ appears within double quotes, and we should quote
+   the list before converting it into a string.  If IFS is unset, and the
+   word is not quoted, we just need to quote CTLESC and CTLNUL characters
+   in the words in the list, because the default value of $IFS is
+   <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. */
+char *
+string_list_dollar_at (list, quoted)
+     WORD_LIST *list;
+     int quoted;
+{
+  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;
+
+  /* XXX this could just be ifs = ifs_value; */
+  ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
+
+#if defined (HANDLE_MULTIBYTE)
+#  if !defined (__GNUC__)
+  sep = (char *)xmalloc (MB_CUR_MAX + 1);
+#  endif /* !__GNUC__ */
+  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
+  sep[0] = (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 */
+#if 0
+  tlist = ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (ifs && *ifs == 0))
+#else
+  tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+#endif
+               ? 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 paramters 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);
+  else if (pchar == '@')
+    ret = string_list_dollar_star (list);
+  else
+    ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list);
 
-  return (string_list_internal (list, sep));
+  return ret;
 }
 
 /* Return the list of words present in STRING.  Separate the string into
@@ -917,82 +2041,38 @@ string_list_dollar_star (list)
 /* BEWARE!  list_string strips null arguments.  Don't call it twice and
    expect to have "" preserved! */
 
-/* Is the first character of STRING a quoted NULL character? */
-#define QUOTED_NULL(string) ((string)[0] == CTLNUL && (string)[1] == '\0')
-
-/* 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 () simply
-   turns STRING into an empty string iff it only consists of a quoted null. */
-/*
-#define remove_quoted_nulls(string) \
-  do { if (QUOTED_NULL (string)) string[0] ='\0'; } while (0)
-*/
-static void
-remove_quoted_nulls (string)
-     char *string;
-{
-  char *nstr, *s, *p;
-
-  nstr = savestring (string);
-  nstr[0] = '\0';
-  for (p = nstr, s = string; *s; s++)
-    {
-      if (*s == CTLESC)
-       {
-         *p++ = *s++;  /* CTLESC */
-         if (*s == 0)
-           break;
-         *p++ = *s;    /* quoted char */
-         continue;
-       }
-      if (*s == CTLNUL)
-       continue;
-      *p++ = *s;
-    }
-  *p = '\0';
-  strcpy (string, nstr);
-  free (nstr);
-}
-
-/* Perform quoted null character removal on each element of LIST.
-   This modifies LIST. */
-void
-word_list_remove_quoted_nulls (list)
-     WORD_LIST *list;
-{
-  register WORD_LIST *t;
-
-  t = list;
-
-  while (t)
-    {
-      remove_quoted_nulls (t->word->word);
-      t = t->next;
-    }
-}
-
 /* 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)
      register char *string, *separators;
      int quoted;
 {
-  WORD_LIST *result = (WORD_LIST *)NULL;
-  char *current_word = (char *)NULL, *s;
-  int sindex = 0;
-  int sh_style_split;
+  WORD_LIST *result;
+  WORD_DESC *t;
+  char *current_word, *s;
+  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. */
@@ -1008,14 +2088,16 @@ 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. */
-
-  while (string[sindex])
+  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);
-      if (!current_word)
+      /* 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;
 
       /* If we have a quoted empty string, add a quoted null argument.  We
@@ -1024,49 +2106,70 @@ list_string (string, separators, quoted)
         below. */
       if (QUOTED_NULL (current_word))
        {
-         WORD_DESC *t = make_word (" ");
-         t->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 (strlen (current_word))
+      else if (current_word[0] != '\0')
        {
          /* If we have something, then add it regardless.  However,
             perform quoted null character removal on the current word. */
          remove_quoted_nulls (current_word);
-         result = make_word_list (make_word (current_word), result);
-         if (quoted)
-           result->word->quoted = 1;
+         result = add_string_to_list (current_word, result);
+         result->word->flags &= ~W_HASQUOTEDNULL;      /* just to be sure */
+         if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
+           result->word->flags |= W_QUOTED;
        }
 
       /* If we're not doing sequences of separators in the traditional
         Bourne shell style, then add a quoted null argument. */
-
       else if (!sh_style_split && !spctabnl (string[sindex]))
        {
-         result = make_word_list (make_word (""), result);
-         result->word->quoted = 1;
+         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 *));
 }
 
 /* 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.
+   the `read' builtin.  This is never called with SEPARATORS != $IFS;
+   it should be simplified.
+
    XXX - this function is very similar to list_string; they should be
         combined - XXX */
 char *
@@ -1075,21 +2178,30 @@ get_word_from_string (stringp, separators, endptr)
 {
   register char *s;
   char *current_word;
-  int sindex, sh_style_split;
+  int sindex, sh_style_split, whitesep, xflags;
+  size_t slen;
 
   if (!stringp || !*stringp || !**stringp)
     return ((char *)NULL);
-    
-  s = *stringp;
 
-  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;
+      if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
+    }
+
+  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)
     {
-      for (; *s && spctabnl (*s) && issep (*s); s++);
+      for (; *s && spctabnl (*s) && isifs (*s); s++);
 
       /* If the string is nothing but whitespace, update it and return. */
       if (!*s)
@@ -1105,24 +2217,46 @@ get_word_from_string (stringp, separators, endptr)
      Now extract a word, stopping at a separator, save a pointer to
      the first character after the word, then skip sequences of spc,
      tab, or nl as long as they are separators.
-     
+
      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 or string_extract_verbatim
+     unless multibyte chars are possible. */
+  slen = (MB_CUR_MAX > 1) ? strlen (s) : 1;
+  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]) && isifs (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 && isifs (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]) && isifs (s[sindex]))
+       sindex++;
+    }
+
   /* Update STRING to point to the next field. */
   *stringp = s + sindex;
   return (current_word);
@@ -1139,202 +2273,323 @@ strip_trailing_ifs_whitespace (string, separators, saw_escape)
      int 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 (PROCESS_SUBSTITUTION)
-#define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC)
-#else
-#define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC)
-#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)
+#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;
-     int quoted;
-     WORD_LIST *(*func)();
 {
   WORD_LIST *list;
-  int i, saw_quote;
-  char *ret;
-     
-  for (i = saw_quote = 0; string[i]; i++)
-    {
-      if (EXP_CHAR (string[i]))
-       break;
-      else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
-       saw_quote = 1;
-    }
+  char *token, *s;
+  size_t s_len;
+  int c, i, tokstart, len;
 
-  if (string[i])
-    {    
-      list = (*func) (string, quoted);
-      if (list)
+  for (s = string; s && *s && spctabnl (*s); s++)
+    ;
+  if (s == 0 || *s == 0)
+    return ((WORD_LIST *)NULL);
+
+  s_len = strlen (s);
+  tokstart = i = 0;
+  list = (WORD_LIST *)NULL;
+  while (1)
+    {
+      c = s[i];
+      if (c == '\\')
        {
-         ret = string_list (list);
-         dispose_words (list);
+         i++;
+         if (s[i])
+           i++;
+       }
+      else if (c == '\'')
+       i = skip_single_quoted (s, s_len, ++i);
+      else if (c == '"')
+       i = skip_double_quoted (s, s_len, ++i);
+      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. */
+         token = substring (s, tokstart, i);
+         list = add_string_to_list (token, list);
+         free (token);
+         while (spctabnl (s[i]))
+           i++;
+         if (s[i])
+           tokstart = i;
+         else
+           break;
        }
       else
-       ret = (char *)NULL;
+       i++;    /* normal character */
     }
-  else if (saw_quote && !quoted)
-    ret = string_quote_removal (string, quoted);
-  else
-    ret = savestring (string);
-  return ret;
+  return (REVERSE_LIST (list, WORD_LIST *));
 }
+#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
-   perform parameter expansion, command substitution, and arithmetic
-   expansion on the right-hand side.  Perform tilde expansion in any
+/********************************************************/
+/*                                                     */
+/*     Functions to perform assignment statements      */
+/*                                                     */
+/********************************************************/
+
+#if defined (ARRAY_VARS)
+static SHELL_VAR *
+do_compound_assignment (name, value, flags)
+     char *name, *value;
+     int flags;
+{
+  SHELL_VAR *v;
+  int mklocal, mkassoc;
+  WORD_LIST *list;
+
+  mklocal = flags & ASS_MKLOCAL;
+  mkassoc = flags & ASS_MKASSOC;
+
+  if (mklocal && variable_context)
+    {
+      v = find_variable (name);
+      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);
+      assign_compound_array_list (v, list, flags);
+    }
+  else
+    v = assign_array_from_string (name, value, flags);
+
+  return (v);
+}
+#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
+   perform parameter expansion, command substitution, and arithmetic
+   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 = assignment (string);
-  char *name = savestring (string);
-  char *value = (char *)NULL;
-  SHELL_VAR *entry = (SHELL_VAR *)NULL;
+  int offset, tlen, appendop, assign_list, aflags, retval;
+  char *name, *value;
+  SHELL_VAR *entry;
+#if defined (ARRAY_VARS)
+  char *t;
+  int ni;
+#endif
+  const char *string;
+
+  if (word == 0 || word->word == 0)
+    return 0;
+
+  appendop = assign_list = aflags = 0;
+  string = word->word;
+  offset = assignment (string, 0);
+  name = savestring (string);
+  value = (char *)NULL;
 
   if (name[offset] == '=')
     {
       char *temp;
 
-      name[offset] = 0;
+      if (name[offset - 1] == '+')
+       {
+         appendop = 1;
+         name[offset - 1] = '\0';
+       }
+
+      name[offset] = 0;                /* might need this set later */
       temp = name + offset + 1;
+      tlen = STRLEN (temp);
 
-      if (expand && temp[0])
+#if defined (ARRAY_VARS)
+      if (expand && (word->flags & W_COMPASSIGN))
        {
-         if (strchr (temp, '~') && unquoted_member ('~', temp))
-           temp = tilde_expand (temp);
-         else
-           temp = savestring (temp);
-
-         value = maybe_expand_string (temp, 0, expand_string_unsplit);
-         free (temp);
+         assign_list = ni = 1;
+         value = extract_array_assignment_list (temp, &ni);
        }
       else
+#endif
+
+      if (expand && temp[0])
+       value = expand_string_if_necessary (temp, 0, expand_string_assignment);
+      else
        value = savestring (temp);
     }
 
   if (value == 0)
-    value = savestring ("");
-
-  entry = bind_variable (name, value);
+    {
+      value = (char *)xmalloc (1);
+      value[0] = '\0';
+    }
 
   if (echo_command_at_execute)
-    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';
+    }
 
-  stupidly_hack_special_variables (name);
+#define ASSIGN_RETURN(r)       do { FREE (value); free (name); return (r); } while (0)
 
-  if (entry)
-    entry->attributes &= ~att_invisible;
+  if (appendop)
+    aflags |= ASS_APPEND;
 
-  FREE (value);
-  free (name);
+#if defined (ARRAY_VARS)
+  if (t = xstrchr (name, '[')) /*]*/
+    {
+      if (assign_list)
+       {
+         report_error (_("%s: cannot assign list to array member"), name);
+         ASSIGN_RETURN (0);
+       }
+      entry = assign_array_element (name, value, aflags);
+      if (entry == 0)
+       ASSIGN_RETURN (0);
+    }
+  else if (assign_list)
+    {
+      if (word->flags & W_ASSIGNARG)
+       aflags |= ASS_MKLOCAL;
+      if (word->flags & W_ASSIGNASSOC)
+       aflags |= ASS_MKASSOC;
+      entry = do_compound_assignment (name, value, aflags);
+    }
+  else
+#endif /* ARRAY_VARS */
+  entry = bind_variable (name, value, aflags);
+
+  stupidly_hack_special_variables (name);
 
+#if 1
   /* Return 1 if the assignment seems to have been performed correctly. */
-  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);
+#else
+  if (entry)
+    VUNSETATTR (entry, att_invisible);
+
+  ASSIGN_RETURN (entry ? ((readonly_p (entry) == 0) && noassign_p (entry) == 0) : 0);
+#endif
 }
 
 /* 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)
+     WORD_DESC *word;
+{
+  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));
 }
 
-/* Most of the substitutions must be done in parallel.  In order
-   to avoid using tons of unclear goto's, I have some functions
-   for manipulating malloc'ed strings.  They all take INDX, a
-   pointer to an integer which is the offset into the string
-   where manipulation is taking place.  They also take SIZE, a
-   pointer to an integer which is the current length of the
-   character array for this string. */
+/***************************************************
+ *                                                *
+ *  Functions to manage the positional parameters  *
+ *                                                *
+ ***************************************************/
 
-/* Append SOURCE to TARGET at INDEX.  SIZE is the current amount
-   of space allocated to TARGET.  SOURCE can be NULL, in which
-   case nothing happens.  Gets rid of SOURCE by free ()ing it.
-   Returns TARGET in case the location has changed. */
-inline char *
-sub_append_string (source, target, indx, size)
-     char *source, *target;
-     int *indx, *size;
+/* Return the word list that corresponds to `$*'. */
+WORD_LIST *
+list_rest_of_args ()
 {
-  if (source)
-    {
-      int srclen, n;
+  register WORD_LIST *list, *args;
+  int i;
 
-      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));
-       }
+  /* Break out of the loop as soon as one of the dollar variables is null. */
+  for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++)
+    list = make_word_list (make_bare_word (dollar_vars[i]), list);
 
-      FASTCOPY (source, target + *indx, srclen);
-      *indx += srclen;
-      target[*indx] = '\0';
+  for (args = rest_of_args; args; args = args->next)
+    list = make_word_list (make_bare_word (args->word->word), list);
 
-      free (source);
-    }
-  return (target);
+  return (REVERSE_LIST (list, WORD_LIST *));
 }
 
-/* Append the textual representation of NUMBER to TARGET.
-   INDX and SIZE are as in SUB_APPEND_STRING. */
-char *
-sub_append_number (number, target, indx, size)
-     int number, *indx, *size;
-     char *target;
+int
+number_of_args ()
 {
-  char *temp;
-
-  temp = itos (number);
-  return (sub_append_string (temp, target, indx, size));
+  register WORD_LIST *list;
+  int n;
+
+  for (n = 0; n < 9 && dollar_vars[n+1]; n++)
+    ;
+  for (list = rest_of_args; list; list = list->next)
+    n++;
+  return n;
 }
 
-/* Return the word list that corresponds to `$*'. */
-WORD_LIST *
-list_rest_of_args ()
+/* Return the value of a positional parameter.  This handles values > 10. */
+char *
+get_dollar_var_value (ind)
+     intmax_t ind;
 {
-  register WORD_LIST *list = (WORD_LIST *)NULL;
-  register WORD_LIST *args = rest_of_args;
-  int i;
-
-  /* Break out of the loop as soon as one of the dollar variables is null. */
-  for (i = 1; i < 10 && dollar_vars[i]; i++)
-    list = make_word_list (make_word (dollar_vars[i]), list);
+  char *temp;
+  WORD_LIST *p;
 
-  while (args)
+  if (ind < 10)
+    temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL;
+  else /* We want something like ${11} */
     {
-      list = make_word_list (make_word (args->word->word), list);
-      args = args->next;
+      ind -= 10;
+      for (p = rest_of_args; p && ind--; p = p->next)
+       ;
+      temp = p ? savestring (p->word->word) : (char *)NULL;
     }
-  return (REVERSE_LIST (list, WORD_LIST *));
+  return (temp);
 }
 
 /* Make a single large string out of the dollar digit variables,
@@ -1344,115 +2599,413 @@ char *
 string_rest_of_args (dollar_star)
      int dollar_star;
 {
-  register WORD_LIST *list = list_rest_of_args ();
+  register WORD_LIST *list;
   char *string;
 
+  list = list_rest_of_args ();
   string = dollar_star ? string_list_dollar_star (list) : string_list (list);
   dispose_words (list);
   return (string);
 }
 
-/***************************************************
- *                                                *
- *        Functions to Expand a String            *
- *                                                *
- ***************************************************/
-/* Call expand_word_internal to expand W and handle error returns.
-   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)
-     WORD_DESC *w;
-     int q, *c, *e;
+/* 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.  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;
+     int start, end, quoted;
 {
-  WORD_LIST *result;
+  WORD_LIST *save, *params, *h, *t;
+  char *ret;
+  int i;
 
-  result = expand_word_internal (w, q, c, e);
-  if (result == &expand_word_error)
-    longjmp (top_level, DISCARD);
-  else if (result == &expand_word_fatal)
-    longjmp (top_level, FORCE_EOF);
-  else
-    return (result);
-}
+  /* see if we can short-circuit.  if start == end, we want 0 parameters. */
+  if (start == end)
+    return ((char *)NULL);
 
-/* Perform parameter expansion, command substitution, and arithmetic
-   expansion on STRING, as if it were a word.  Leave the result quoted. */
-static WORD_LIST *
-expand_string_internal (string, quoted)
-     char *string;
-     int quoted;
-{
-  WORD_DESC td;
-  WORD_LIST *tresult;
+  save = params = list_rest_of_args ();
+  if (save == 0)
+    return ((char *)NULL);
 
-  if (!string || !*string)
-    return ((WORD_LIST *)NULL);
+  if (start == 0)              /* handle ${@:0[:x]} specially */
+    {
+      t = make_word_list (make_word (dollar_vars[0]), params);
+      save = params = t;
+    }
 
-  bzero (&td, sizeof (td));
-  td.word = string;
-  tresult = call_expand_word_internal (&td, quoted, (int *)NULL, (int *)NULL);
-  return (tresult);
+  for (i = 1; params && i < start; i++)
+    params = params->next;
+  if (params == 0)
+    return ((char *)NULL);
+  for (h = t = params; params && i < end; i++)
+    {
+      t = params;
+      params = params->next;
+    }
+
+  t->next = (WORD_LIST *)NULL;
+
+  ret = string_list_pos_params (string[0], h, quoted);
+
+  if (t != params)
+    t->next = params;
+
+  dispose_words (save);
+  return (ret);
 }
 
-/* Expand STRING by performing parameter expansion, command substitution,
-   and arithmetic expansion.  Dequote the resulting WORD_LIST before
-   returning it, but do not perform word splitting.  The call to
-   remove_quoted_nulls () is in here because word splitting normally
-   takes care of quote removal. */
-WORD_LIST *
-expand_string_unsplit (string, quoted)
+/******************************************************************/
+/*                                                               */
+/*     Functions to expand strings to strings or WORD_LISTs      */
+/*                                                               */
+/******************************************************************/
+
+#if defined (PROCESS_SUBSTITUTION)
+#define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
+#else
+#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 *
+expand_string_if_necessary (string, quoted, func)
      char *string;
      int quoted;
+     EXPFUNC *func;
 {
-  WORD_LIST *value;
+  WORD_LIST *list;
+  size_t slen;
+  int i, saw_quote;
+  char *ret;
+  DECLARE_MBSTATE;
 
-  if (!string || !*string)
-    return ((WORD_LIST *)NULL);
+  /* 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);
+    }
 
-  value = expand_string_internal (string, quoted);
-  if (value)
+  if (string[i])
     {
-      if (value->word)
-       remove_quoted_nulls (value->word->word);
-      dequote_list (value);
+      list = (*func) (string, quoted);
+      if (list)
+       {
+         ret = string_list (list);
+         dispose_words (list);
+       }
+      else
+       ret = (char *)NULL;
     }
-  return (value);
+  else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
+    ret = string_quote_removal (string, quoted);
+  else
+    ret = savestring (string);
+
+  return ret;
 }
 
-/* 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)
+static inline char *
+expand_string_to_string_internal (string, quoted, func)
      char *string;
-     int quoted, *dollar_at_p, *has_dollar_at;
+     int quoted;
+     EXPFUNC *func;
 {
-  WORD_DESC td;
-  WORD_LIST *tresult;
+  WORD_LIST *list;
+  char *ret;
 
   if (string == 0 || *string == '\0')
-    return (WORD_LIST *)NULL;
-     
-  bzero (&td, sizeof (td));
-  td.word = string; 
-  tresult = call_expand_word_internal (&td, quoted, dollar_at_p, has_dollar_at);
-  return (tresult);
+    return ((char *)NULL);
+
+  list = (*func) (string, quoted);
+  if (list)
+    {
+      ret = string_list (list);
+      dispose_words (list);
+    }
+  else
+    ret = (char *)NULL;
+
+  return (ret);
 }
 
-/* Expand STRING just as if you were expanding a word, but do not dequote
-   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. */
-static WORD_LIST *
-expand_string_leave_quoted (string, quoted)
+char *
+expand_string_to_string (string, quoted)
      char *string;
      int quoted;
 {
-  WORD_LIST *tlist;
+  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;
+{
+  return (expand_string_if_necessary (string, quoted, expand_string));
+}
+
+#if defined (COND_COMMAND)
+/* Just remove backslashes in STRING.  Returns a new string. */
+char *
+remove_backslashes (string)
+     char *string;
+{
+  char *r, *ret, *s;
+
+  r = ret = (char *)xmalloc (strlen (string) + 1);
+  for (s = string; s && *s; )
+    {
+      if (*s == '\\')
+       s++;
+      if (*s == 0)
+       break;
+      *r++ = *s++;
+    }
+  *r = '\0';
+  return ret;
+}
+
+/* This needs better error handling. */
+/* Expand W for use as an argument to a unary or binary operator in a
+   [[...]] 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.  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;
+     int 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, 0, (int *)0, (int *)0);
+  if (l)
+    {
+      if (special == 0)
+       {
+         dequote_list (l);
+         r = string_list (l);
+       }
+      else
+       {
+         qflags = QGLOB_CVTNULL;
+         if (special == 2)
+           qflags |= QGLOB_REGEXP;
+         p = string_list (l);
+         r = quote_string_for_globbing (p, qflags);
+         free (p);
+       }
+      dispose_words (l);
+    }
+  else
+    r = (char *)NULL;
+
+  return r;
+}
+#endif
+
+/* Call expand_word_internal to expand W and handle error returns.
+   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, i, c, e)
+     WORD_DESC *w;
+     int q, i, *c, *e;
+{
+  WORD_LIST *result;
+
+  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 (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;
+      last_command_exit_value = EXECUTION_FAILURE;
+      exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : 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. */
+static WORD_LIST *
+expand_string_internal (string, quoted)
+     char *string;
+     int quoted;
+{
+  WORD_DESC td;
   WORD_LIST *tresult;
 
-  if (!string || !*string)
+  if (string == 0 || *string == 0)
+    return ((WORD_LIST *)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);
+}
+
+/* Expand STRING by performing parameter expansion, command substitution,
+   and arithmetic expansion.  Dequote the resulting WORD_LIST before
+   returning it, but do not perform word splitting.  The call to
+   remove_quoted_nulls () is in here because word splitting normally
+   takes care of quote removal. */
+WORD_LIST *
+expand_string_unsplit (string, quoted)
+     char *string;
+     int quoted;
+{
+  WORD_LIST *value;
+
+  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);
+         value->word->flags &= ~W_HASQUOTEDNULL;
+       }
+      dequote_list (value);
+    }
+  return (value);
+}
+
+/* Expand STRING just as if you were expanding a word, but do not dequote
+   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
+   substitution, arithmetic expansion, and word splitting. */
+static WORD_LIST *
+expand_string_leave_quoted (string, quoted)
+     char *string;
+     int quoted;
+{
+  WORD_LIST *tlist;
+  WORD_LIST *tresult;
+
+  if (string == 0 || *string == '\0')
     return ((WORD_LIST *)NULL);
 
   tlist = expand_string_internal (string, quoted);
@@ -1466,6 +3019,25 @@ expand_string_leave_quoted (string, quoted)
   return ((WORD_LIST *)NULL);
 }
 
+/* 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)
+     char *string;
+     int quoted, *dollar_at_p, *has_dollar_at;
+{
+  WORD_DESC td;
+  WORD_LIST *tresult;
+
+  if (string == 0 || *string == '\0')
+    return (WORD_LIST *)NULL;
+
+  td.flags = 0;
+  td.word = string;
+  tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, has_dollar_at);
+  return (tresult);
+}
+
 /* Expand STRING just as if you were expanding a word.  This also returns
    a list of words.  Note that filename globbing is *NOT* done for word
    or string expansion, just when the shell is expanding a command.  This
@@ -1478,14 +3050,11 @@ 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);
-
-  if (result)
-    dequote_list (result);
-  return (result);
+  return (result ? dequote_list (result) : result);
 }
 
 /***************************************************
@@ -1494,30 +3063,127 @@ expand_string (string, quoted)
  *                                                *
  ***************************************************/
 
-/* I'm going to have to rewrite expansion because filename globbing is
-   beginning to make the entire arrangement ugly.  I'll do this soon. */
-static void
-dequote_list (list)
-     register WORD_LIST *list;
+/* Conventions:
+
+     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 (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;
+  register char *s, *t;
+  size_t slen;
+  char *result, *send;
+  int quote_spaces, skip_ctlesc, skip_ctlnul;
+  DECLARE_MBSTATE; 
+
+  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 ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' '))
+       *t++ = CTLESC;
+      COPY_CHAR_P (t, s, send);
+    }
+  *t = '\0';
+  return (result);
+}
+
+static WORD_LIST *
+list_quote_escapes (list)
+     WORD_LIST *list;
+{
+  register WORD_LIST *w;
+  char *t;
+
+  for (w = list; w; w = w->next)
+    {
+      t = w->word->word;
+      w->word->word = quote_escapes (t);
+      free (t);
+    }
+  return list;
+}
+
+/* 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, *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));
 
-  while (list)
+  quote_spaces = (ifs_value && *ifs_value == 0);
+
+  s = string;
+  while (*s)
     {
-      s = dequote_string (list->word->word);
-      free (list->word->word);
-      list->word->word = s;
-      list = list->next;
+      if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' ')))
+       {
+         s++;
+         if (*s == '\0')
+           break;
+       }
+      COPY_CHAR_P (t, s, send);
     }
+  *t = '\0';
+  return result;
 }
 
+/* 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;
@@ -1532,44 +3198,55 @@ make_quoted_char (c)
   return (temp);
 }
 
-/* Quote STRING.  Return a new string. */
-static char *
+/* 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;
 {
-  char *result;
+  register char *t;
+  size_t slen;
+  char *result, *send;
 
-  if (!*string)
+  if (*string == 0)
     {
-      result = xmalloc (2);
+      result = (char *)xmalloc (2);
       result[0] = CTLNUL;
       result[1] = '\0';
     }
   else
     {
-      register char *t;
+      DECLARE_MBSTATE;
 
-      result = xmalloc ((strlen (string) * 2) + 1);
+      slen = strlen (string);
+      send = string + slen;
 
-      for (t = result; string && *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;
+
+  slen = strlen (string);
 
-  result = xmalloc (strlen (string) + 1);
+  t = result = (char *)xmalloc (slen + 1);
 
   if (QUOTED_NULL (string))
     {
@@ -1579,23 +3256,20 @@ dequote_string (string)
 
   /* 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; string++)
+  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';
@@ -1603,27 +3277,158 @@ dequote_string (string)
 }
 
 /* Quote the entire WORD_LIST list. */
-static void
+static WORD_LIST *
 quote_list (list)
      WORD_LIST *list;
 {
   register WORD_LIST *w;
+  char *t;
 
   for (w = list; w; w = w->next)
     {
-      char *t = w->word->word;
+      t = w->word->word;
       w->word->word = quote_string (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);
-      w->word->quoted = 1;
+    }
+
+  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. */
+char *
+remove_quoted_nulls (string)
+     char *string;
+{
+  register size_t slen;
+  register int i, j, prev_i;
+  DECLARE_MBSTATE;
+
+  if (strchr (string, CTLNUL) == 0)            /* XXX */
+    return string;                             /* XXX */
+
+  slen = strlen (string);
+  i = j = 0;
+
+  while (i < slen)
+    {
+      if (string[i] == CTLESC)
+       {
+         /* 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;
+       }
+      else if (string[i] == CTLNUL)
+       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);
+}
+
+/* Perform quoted null character removal on each element of LIST.
+   This modifies LIST. */
+void
+word_list_remove_quoted_nulls (list)
+     WORD_LIST *list;
+{
+  register WORD_LIST *t;
+
+  for (t = list; t; t = t->next)
+    {
+      remove_quoted_nulls (t->word->word);
+      t->word->flags &= ~W_HASQUOTEDNULL;
     }
 }
 
 /* **************************************************************** */
 /*                                                                 */
-/*                 Functions for Removing Patterns                 */
+/*        Functions for Matching and Removing Patterns             */
 /*                                                                 */
 /* **************************************************************** */
 
+#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
@@ -1638,19 +3443,16 @@ quote_list (list)
 #define RP_SHORT_RIGHT 4
 
 static char *
-remove_pattern (param, pattern, op)
+remove_upattern (param, pattern, op)
      char *param, *pattern;
      int op;
 {
-  register int len = param ? strlen (param) : 0;
-  register char *end = param + len;
+  register int len;
+  register char *end;
   register char *p, *ret, c;
 
-  if (pattern == NULL || *pattern == '\0')     /* minor optimization */
-    return (savestring (param));
-
-  if (param == NULL || *param == '\0')
-    return (param);
+  len = STRLEN (param);
+  end = param + len;
 
   switch (op)
     {
@@ -1658,12 +3460,13 @@ remove_pattern (param, pattern, op)
        for (p = end; p >= param; p--)
          {
            c = *p; *p = '\0';
-           if (fnmatch (pattern, param, 0) != FNM_NOMATCH)
+           if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
              {
                *p = c;
                return (savestring (p));
              }
            *p = c;
+
          }
        break;
 
@@ -1671,7 +3474,7 @@ remove_pattern (param, pattern, op)
        for (p = param; p <= end; p++)
          {
            c = *p; *p = '\0';
-           if (fnmatch (pattern, param, 0) != FNM_NOMATCH)
+           if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
              {
                *p = c;
                return (savestring (p));
@@ -1683,10 +3486,9 @@ remove_pattern (param, pattern, op)
       case RP_LONG_RIGHT:      /* remove longest match at end */
        for (p = param; p <= end; p++)
          {
-           if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
+           if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
              {
-               c = *p;
-               *p = '\0';
+               c = *p; *p = '\0';
                ret = savestring (param);
                *p = c;
                return (ret);
@@ -1697,10 +3499,9 @@ remove_pattern (param, pattern, op)
       case RP_SHORT_RIGHT:     /* remove shortest match at end */
        for (p = end; p >= param; p--)
          {
-           if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
+           if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
              {
-               c = *p;
-               *p = '\0';
+               c = *p; *p = '\0';
                ret = savestring (param);
                *p = c;
                return (ret);
@@ -1708,182 +3509,830 @@ remove_pattern (param, pattern, op)
          }
        break;
     }
+
   return (savestring (param)); /* no match, return original string */
 }
 
-/*******************************************
- *                                        *
- *     Functions to expand WORD_DESCs     *
- *                                        *
- *******************************************/
+#if defined (HANDLE_MULTIBYTE)
+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;
 
-/* Expand WORD, performing word splitting on the result.  This does
-   parameter expansion, command substitution, arithmetic expansion,
-   word splitting, and quote removal. */
+  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;
 
-WORD_LIST *
-expand_word (word, quoted)
-     WORD_DESC *word;
-     int quoted;
-{
-  WORD_LIST *result, *tresult;
+      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;
 
-  tresult = call_expand_word_internal (word, quoted, (int *)NULL, (int *)NULL);
-  result = word_list_split (tresult);
-  dispose_words (tresult);
-  if (result)
-    dequote_list (result);
-  return (result);
+      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 (wcsdup (wparam));    /* no match, return original string */
 }
+#endif /* HANDLE_MULTIBYTE */
 
-/* Expand WORD, but do not perform word splitting on the result.  This
-   does parameter expansion, command substitution, arithmetic expansion,
-   and quote removal. */
-WORD_LIST *
-expand_word_no_split (word, quoted)
-     WORD_DESC *word;
-     int quoted;
+static char *
+remove_pattern (param, pattern, op)
+     char *param, *pattern;
+     int op;
 {
-  WORD_LIST *result;
+  if (param == NULL)
+    return (param);
+  if (*param == '\0' || pattern == NULL || *pattern == '\0')   /* minor optimization */
+    return (savestring (param));
 
-  result = call_expand_word_internal (word, quoted, (int *)NULL, (int *)NULL);
-  if (result)
-    dequote_list (result);
-  return (result);
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1)
+    {
+      wchar_t *ret, *oret;
+      size_t n;
+      wchar_t *wparam, *wpattern;
+      mbstate_t ps;
+      char *xret;
+
+      n = xdupmbstowcs (&wpattern, NULL, pattern);
+      if (n == (size_t)-1)
+       return (remove_upattern (param, pattern, op));
+      n = xdupmbstowcs (&wparam, NULL, param);
+      if (n == (size_t)-1)
+       {
+         free (wpattern);
+         return (remove_upattern (param, pattern, op));
+       }
+      oret = ret = remove_wpattern (wparam, n, wpattern, op);
+
+      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
+    return (remove_upattern (param, pattern, op));
 }
 
-/* Perform shell expansions on WORD, but do not perform word splitting or
-   quote removal on the result. */
-WORD_LIST *
-expand_word_leave_quoted (word, quoted)
-     WORD_DESC *word;
-     int quoted;
+/* Return 1 of the first character of STRING could match the first
+   character of pattern PAT.  Used to avoid n2 calls to strmatch(). */
+static int
+match_pattern_char (pat, string)
+     char *pat, *string;
 {
-  WORD_LIST *result;
+  char c;
 
-  result = call_expand_word_internal (word, quoted, (int *)NULL, (int *)NULL);
-  return (result);
+  if (*string == 0)
+    return (0);
+
+  switch (c = *pat++)
+    {
+    default:
+      return (*string == c);
+    case '\\':
+      return (*string == *pat);
+    case '?':
+      return (*pat == LPAREN ? 1 : (*string != '\0'));
+    case '*':
+      return (1);
+    case '+':
+    case '!':
+    case '@':
+      return (*pat == LPAREN ? 1 : (*string == c));
+    case '[':
+      return (*string != '\0');
+    }
 }
 
-/* Return the value of a positional parameter.  This handles values > 10. */
-char *
-get_dollar_var_value (ind)
-     int ind;
+/* 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;
 {
-  char *temp;
-
-  if (ind < 10)
+  int c, len;
+  register char *p, *p1, *npat;
+  char *end;
+
+  /* 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] == '(' && extended_glob) || pat[len - 1] != '*')       /*)*/
     {
-      if (dollar_vars[ind])
-       temp = savestring (dollar_vars[ind]);
-      else
-       temp = (char *)NULL;
+      p = npat = (char *)xmalloc (len + 3);
+      p1 = pat;
+      if (*p1 != '*' || (*p1 == '*' && p1[1] == '(' && extended_glob)) /*)*/
+       *p++ = '*';
+      while (*p1)
+       *p++ = *p1++;
+      if (p1[-1] != '*' || p[-2] == '\\')
+       *p++ = '*';
+      *p = '\0';
     }
-  else /* We want something like ${11} */
+  else
+    npat = pat;
+  c = strmatch (npat, string, FNMATCH_EXTFLAG);
+  if (npat != pat)
+    free (npat);
+  if (c == FNM_NOMATCH)
+    return (0);
+
+  len = STRLEN (string);
+  end = string + len;
+
+  switch (mtype)
     {
-      WORD_LIST *p = rest_of_args;
+    case MATCH_ANY:
+      for (p = string; p <= end; p++)
+       {
+         if (match_pattern_char (pat, p))
+           {
+             for (p1 = end; p1 >= p; p1--)
+               {
+                 c = *p1; *p1 = '\0';
+                 if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
+                   {
+                     *p1 = c;
+                     *sp = p;
+                     *ep = p1;
+                     return 1;
+                   }
+                 *p1 = c;
+               }
+           }
+       }
 
-      ind -= 10;
-      while (p && ind--)
-       p = p->next;
-      if (p)
-       temp = savestring (p->word->word);
-      else
-       temp = (char *)NULL;
-    }
-  return (temp);
-}
+      return (0);
 
-#if defined (PROCESS_SUBSTITUTION)
+    case MATCH_BEG:
+      if (match_pattern_char (pat, string) == 0)
+       return (0);
 
-/* **************************************************************** */
-/*                                                               */
-/*                 Hacking Process Substitution                    */
-/*                                                               */
-/* **************************************************************** */
+      for (p = end; p >= string; p--)
+       {
+         c = *p; *p = '\0';
+         if (strmatch (pat, string, FNMATCH_EXTFLAG) == 0)
+           {
+             *p = c;
+             *sp = string;
+             *ep = p;
+             return 1;
+           }
+         *p = c;
+       }
 
-extern struct fd_bitmap *current_fds_to_close;
-extern char *mktemp ();
+      return (0);
 
-#if !defined (HAVE_DEV_FD)
-/* Named pipes must be removed explicitly with `unlink'.  This keeps a list
-   of FIFOs the shell has open.  unlink_fifo_list will walk the list and
-   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
+    case MATCH_END:
+      for (p = string; p <= end; p++)
+       {
+         if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
+           {
+             *sp = p;
+             *ep = end;
+             return 1;
+           }
 
-static char **fifo_list = (char **)NULL;
-static int nfifo = 0;
-static int fifo_list_size = 0;
+       }
 
-static void
-add_fifo_list (pathname)
-     char *pathname;
-{
-  if (nfifo >= fifo_list_size - 1)
-    {
-      fifo_list_size += FIFO_INCR;
-      fifo_list = (char **)xrealloc (fifo_list,
-                                    fifo_list_size * sizeof (char *));
+      return (0);
     }
 
-  fifo_list[nfifo++] = savestring (pathname);
+  return (0);
 }
 
-void
-unlink_fifo_list ()
+#if defined (HANDLE_MULTIBYTE)
+/* Return 1 of the first character of WSTRING could match the first
+   character of pattern WPAT.  Wide character version. */
+static int
+match_pattern_wchar (wpat, wstring)
+     wchar_t *wpat, *wstring;
 {
-  if (!nfifo)
-    return;
+  wchar_t wc;
 
-  while (nfifo--)
+  if (*wstring == 0)
+    return (0);
+
+  switch (wc = *wpat++)
     {
-      unlink (fifo_list[nfifo]);
-      free (fifo_list[nfifo]);
-      fifo_list[nfifo] = (char *)NULL;
+    default:
+      return (*wstring == wc);
+    case L'\\':
+      return (*wstring == *wpat);
+    case L'?':
+      return (*wpat == LPAREN ? 1 : (*wstring != L'\0'));
+    case L'*':
+      return (1);
+    case L'+':
+    case L'!':
+    case L'@':
+      return (*wpat == LPAREN ? 1 : (*wstring == wc));
+    case L'[':
+      return (*wstring != L'\0');
     }
-  nfifo = 0;
 }
 
-static char *
-make_named_pipe ()
+/* 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_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
+     wchar_t *wstring;
+     char **indices;
+     size_t wstrlen;
+     wchar_t *wpat;
+     int mtype;
+     char **sp, **ep;
 {
-  char *tname;
+  wchar_t wc, *wp, *nwpat, *wp1;
+  int len;
+#if 0
+  size_t n, n1;        /* Apple's gcc seems to miscompile this badly */
+#else
+  int n, n1;
+#endif
 
-  tname = mktemp (savestring ("/tmp/sh-np-XXXXXX"));
-  if (mkfifo (tname, 0600) < 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. */
+  /* XXX - check this later if I ever implement `**' with special meaning,
+     since this will potentially result in `**' at the beginning or end */
+  len = wcslen (wpat);
+  if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == L'(' && extended_glob) || wpat[len - 1] != L'*')       /*)*/
     {
-      free (tname);
-      return ((char *)NULL);
+      wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t));
+      wp1 = wpat;
+      if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == '(' && extended_glob))     /*)*/
+       *wp++ = L'*';
+      while (*wp1 != L'\0')
+       *wp++ = *wp1++;
+      if (wp1[-1] != L'*' || wp1[-2] == L'\\')
+        *wp++ = L'*';
+      *wp = '\0';
     }
+  else
+    nwpat = wpat;
+  len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG);
+  if (nwpat != wpat)
+    free (nwpat);
+  if (len == FNM_NOMATCH)
+    return (0);
 
-  add_fifo_list (tname);
-  return (tname);
-}
+  switch (mtype)
+    {
+    case MATCH_ANY:
+      for (n = 0; n <= wstrlen; n++)
+       {
+         if (match_pattern_wchar (wpat, wstring + n))
+           {
+             for (n1 = wstrlen; n1 >= n; n1--)
+               {
+                 wc = wstring[n1]; wstring[n1] = L'\0';
+                 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
+                   {
+                     wstring[n1] = wc;
+                     *sp = indices[n];
+                     *ep = indices[n1];
+                     return 1;
+                   }
+                 wstring[n1] = wc;
+               }
+           }
+       }
 
-#if !defined (_POSIX_VERSION)
-int
-mkfifo (path, mode)
-     char *path;
-     int mode;
-{
-#if defined (S_IFIFO)
-  return (mknod (path, (mode | S_IFIFO), 0));
-#else /* !S_IFIFO */
-  return (-1);
-#endif /* !S_IFIFO */
-}
-#endif /* !_POSIX_VERSION */
+      return (0);
 
-#else /* HAVE_DEV_FD */
+    case MATCH_BEG:
+      if (match_pattern_wchar (wpat, wstring) == 0)
+       return (0);
 
-/* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
-   has open to children.  NFDS is a count of the number of bits currently
-   set in DEV_FD_LIST.  TOTFDS is a count of the highest possible number
-   of open files. */
-static char *dev_fd_list = (char *)NULL;
-static int nfds = 0;
-static int totfds;     /* The highest possible number of open files. */
+      for (n = wstrlen; n >= 0; n--)
+       {
+         wc = wstring[n]; wstring[n] = L'\0';
+         if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG) == 0)
+           {
+             wstring[n] = wc;
+             *sp = indices[0];
+             *ep = indices[n];
+             return 1;
+           }
+         wstring[n] = wc;
+       }
+
+      return (0);
+
+    case MATCH_END:
+      for (n = 0; n <= wstrlen; n++)
+       {
+         if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
+           {
+             *sp = indices[n];
+             *ep = indices[wstrlen];
+             return 1;
+           }
+       }
+
+      return (0);
+    }
+
+  return (0);
+}
+#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;
+#endif
+
+  if (string == 0 || *string == 0 || pat == 0 || *pat == 0)
+    return (0);
+
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1)
+    {
+      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)
+     int c;
+     char *value;
+{
+  if (c == '#')
+    return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
+  else /* c == '%' */
+    return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
+}
+
+/* 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 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
+   following retains its special meaning: "${foo#'*'}". */
+static char *
+getpattern (value, quoted, expandpat)
+     char *value;
+     int quoted, expandpat;
+{
+  char *pat, *tword;
+  WORD_LIST *l;
+#if 0
+  int i;
+#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);
+      free (tword);
+      tword = pat;
+    }
+#endif
+
+  /* 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)
+            : (WORD_LIST *)0;
+  pat = string_list (l);
+  dispose_words (l);
+  if (pat)
+    {
+      tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
+      free (pat);
+      pat = tword;
+    }
+  return (pat);
+}
+
+#if 0
+/* Handle removing a pattern from a string as a result of ${name%[%]value}
+   or ${name#[#]value}. */
+static char *
+variable_remove_pattern (value, pattern, patspec, quoted)
+     char *value, *pattern;
+     int patspec, quoted;
+{
+  char *tword;
+
+  tword = remove_pattern (value, pattern, patspec);
+
+  return (tword);
+}
+#endif
+
+static char *
+list_remove_pattern (list, pattern, patspec, itype, quoted)
+     WORD_LIST *list;
+     char *pattern;
+     int patspec, itype, quoted;
+{
+  WORD_LIST *new, *l;
+  WORD_DESC *w;
+  char *tword;
+
+  for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
+    {
+      tword = remove_pattern (l->word->word, pattern, patspec);
+      w = alloc_word_desc ();
+      w->word = tword ? tword : savestring ("");
+      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 (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);
+  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, patstr, rtype, quoted)
+     char *varname, *value, *patstr;
+     int rtype, quoted;
+{
+  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, quoted, &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;
+}    
+
+/*******************************************
+ *                                        *
+ *     Functions to expand WORD_DESCs     *
+ *                                        *
+ *******************************************/
+
+/* Expand WORD, performing word splitting on the result.  This does
+   parameter expansion, command substitution, arithmetic expansion,
+   word splitting, and quote removal. */
+
+WORD_LIST *
+expand_word (word, quoted)
+     WORD_DESC *word;
+     int quoted;
+{
+  WORD_LIST *result, *tresult;
+
+  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);
+}
+
+/* Expand WORD, but do not perform word splitting on the result.  This
+   does parameter expansion, command substitution, arithmetic expansion,
+   and quote removal. */
+WORD_LIST *
+expand_word_unsplit (word, quoted)
+     WORD_DESC *word;
+     int quoted;
+{
+  WORD_LIST *result;
+
+  expand_no_split_dollar_star = 1;
+  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. */
+WORD_LIST *
+expand_word_leave_quoted (word, quoted)
+     WORD_DESC *word;
+     int quoted;
+{
+  return (call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL));
+}
+
+#if defined (PROCESS_SUBSTITUTION)
+
+/*****************************************************************/
+/*                                                              */
+/*                 Hacking Process Substitution                 */
+/*                                                              */
+/*****************************************************************/
+
+#if !defined (HAVE_DEV_FD)
+/* Named pipes must be removed explicitly with `unlink'.  This keeps a list
+   of FIFOs the shell has open.  unlink_fifo_list will walk the list and
+   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
+
+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;
+
+static void
+add_fifo_list (pathname)
+     char *pathname;
+{
+  if (nfifo >= fifo_list_size - 1)
+    {
+      fifo_list_size += FIFO_INCR;
+      fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
+                               fifo_list_size * sizeof (struct temp_fifo));
+    }
+
+  fifo_list[nfifo].file = savestring (pathname);
+  nfifo++;
+}
+
+void
+unlink_fifo_list ()
+{
+  int saved, i, j;
+
+  if (nfifo == 0)
+    return;
+
+  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;
+}
+
+int
+fifos_pending ()
+{
+  return nfifo;
+}
+
+static char *
+make_named_pipe ()
+{
+  char *tname;
+
+  tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR);
+  if (mkfifo (tname, 0600) < 0)
+    {
+      free (tname);
+      return ((char *)NULL);
+    }
+
+  add_fifo_list (tname);
+  return (tname);
+}
+
+#else /* HAVE_DEV_FD */
+
+/* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
+   has open to children.  NFDS is a count of the number of bits currently
+   set in DEV_FD_LIST.  TOTFDS is a count of the highest possible number
+   of open files. */
+static char *dev_fd_list = (char *)NULL;
+static int nfds;
+static int totfds;     /* The highest possible number of open files. */
 
 static void
 add_fifo_list (fd)
@@ -1897,23 +4346,29 @@ 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 */
+}
+
 void
 unlink_fifo_list ()
 {
   register int i;
 
-  if (!nfds)
+  if (nfds == 0)
     return;
 
   for (i = 0; nfds && i < totfds; i++)
@@ -1932,7 +4387,7 @@ 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++)
@@ -1948,10 +4403,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 (16 * sizeof (char));
-  sprintf (ret, "/dev/fd/%d", fd);
   add_fifo_list (fd);
   return (ret);
 }
@@ -1984,9 +4443,9 @@ process_substitute (string, open_for_read_in_child)
 #endif /* HAVE_DEV_FD */
 #if defined (JOB_CONTROL)
   pid_t old_pipeline_pgrp;
-#endif  
+#endif
 
-  if (!string || !*string)
+  if (!string || !*string || wordexp_only)
     return ((char *)NULL);
 
 #if !defined (HAVE_DEV_FD)
@@ -1994,21 +4453,23 @@ process_substitute (string, open_for_read_in_child)
 #else /* HAVE_DEV_FD */
   if (pipe (fildes) < 0)
     {
-      internal_error ("can't make pipes for process substitution: %s",
-       strerror (errno));
+      sys_error (_("cannot make pipe for process substitution"));
       return ((char *)NULL);
     }
   /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
      the pipe in the parent, otherwise the read end. */
   parent_pipe_fd = fildes[open_for_read_in_child];
   child_pipe_fd = fildes[1 - open_for_read_in_child];
+  /* Move the parent end of the pipe to some high file descriptor, to
+     avoid clashes with FDs used by the script. */
+  parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
+
   pathname = make_dev_fd_filename (parent_pipe_fd);
 #endif /* HAVE_DEV_FD */
 
-  if (!pathname)
+  if (pathname == 0)
     {
-      internal_error ("cannot make pipe for process subsitution: %s",
-       strerror (errno));
+      sys_error (_("cannot make pipe for process substitution"));
       return ((char *)NULL);
     }
 
@@ -2017,33 +4478,30 @@ process_substitute (string, open_for_read_in_child)
 #if defined (JOB_CONTROL)
   old_pipeline_pgrp = pipeline_pgrp;
   pipeline_pgrp = shell_pgrp;
-  cleanup_the_pipeline ();
+  save_pipeline (1);
+#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 ();
       setup_async_signals ();
-      subshell_environment++;
+      subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB;
     }
+
+#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 /* !JOB_CONTROL */
-  pid = make_child ((char *)NULL, 1);
-  if (pid == 0)
-    {
-      /* Cancel traps, in trap.c. */
-      restore_original_signals ();
-      setup_async_signals ();
-      subshell_environment++;
-    }
-#endif /* !JOB_CONTROL */
+#endif /* JOB_CONTROL */
 
   if (pid < 0)
     {
-      internal_error ("cannot make a child for process substitution: %s",
-       strerror (errno));
+      sys_error (_("cannot make child for process substitution"));
       free (pathname);
 #if defined (HAVE_DEV_FD)
       close (parent_pipe_fd);
@@ -2054,6 +4512,14 @@ process_substitute (string, open_for_read_in_child)
 
   if (pid > 0)
     {
+#if defined (JOB_CONTROL)
+      restore_pipeline (1);
+#endif
+
+#if !defined (HAVE_DEV_FD)
+      fifo_list[nfifo-1].proc = pid;
+#endif
+
       last_made_pid = old_pid;
 
 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
@@ -2075,25 +4541,38 @@ 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_WRONLY);
+  fd = open (pathname, open_for_read_in_child ? O_RDONLY|O_NONBLOCK : O_WRONLY);
   if (fd < 0)
     {
-      internal_error ("cannot open named pipe %s for %s: %s", pathname,
-       open_for_read_in_child ? "reading" : "writing", strerror (errno));
+      /* 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 */
 
   if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
     {
-      internal_error ("cannot duplicate named pipe %s as fd %d: %s",
-       pathname, open_for_read_in_child ? 0 : 1, strerror (errno));
+      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. */
@@ -2111,7 +4590,7 @@ process_substitute (string, open_for_read_in_child)
   dev_fd_list[parent_pipe_fd] = 0;
 #endif /* HAVE_DEV_FD */
 
-  result = parse_and_execute (string, "process substitution", 0);
+  result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
 
 #if !defined (HAVE_DEV_FD)
   /* Make sure we close the named pipe in the child before we exit. */
@@ -2123,81 +4602,218 @@ process_substitute (string, open_for_read_in_child)
 }
 #endif /* PROCESS_SUBSTITUTION */
 
-/* Perform command substitution on STRING.  This returns a string,
-   possibly quoted. */
+/***********************************/
+/*                                */
+/*     Command Substitution       */
+/*                                */
+/***********************************/
+
 static char *
+read_comsub (fd, quoted, rflag)
+     int fd, quoted;
+     int *rflag;
+{
+  char *istring, buf[128], *bufp, *s;
+  int istring_index, istring_size, c, tflag, skip_ctlesc, skip_ctlnul;
+  ssize_t bufn;
+
+  istring = (char *)NULL;
+  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;
+
+#ifdef __CYGWIN__
+  setmode (fd, O_TEXT);                /* we don't want CR/LF, we want Unix-style */
+#endif
+
+  /* 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;
+      if (--bufn <= 0)
+       {
+         bufn = zread (fd, buf, sizeof (buf));
+         if (bufn <= 0) 
+           break;
+         bufp = buf;
+       }
+      c = *bufp++;
+
+      if (c == 0)
+       {
+#if 0
+         internal_warning ("read_comsub: ignored null byte in input");
+#endif
+         continue;
+       }
+
+      /* Add the character to ISTRING, possibly after resizing it. */
+      RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
+
+      /* 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)
+    istring[istring_index] = '\0';
+
+  /* If we read no output, just return now and save ourselves some
+     trouble. */
+  if (istring_index == 0)
+    {
+      FREE (istring);
+      if (rflag)
+       *rflag = tflag;
+      return (char *)NULL;
+    }
+
+  /* Strip trailing newlines from the output of the command. */
+  if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+    {
+      while (istring_index > 0)
+       {
+         if (istring[istring_index - 1] == '\n')
+           {
+             --istring_index;
+
+             /* If the newline was quoted, remove the quoting char. */
+             if (istring[istring_index - 1] == CTLESC)
+               --istring_index;
+           }
+         else
+           break;
+       }
+      istring[istring_index] = '\0';
+    }
+  else
+    strip_trailing (istring, istring_index - 1, 1);
+
+  if (rflag)
+    *rflag = tflag;
+  return istring;
+}
+
+/* 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;
-  int fildes[2];
-  char *istring = (char *)NULL;
-  int istring_index, istring_size, c = 1;
-  int result;
+  pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
+  char *istring;
+  int result, fildes[2], function_value, pflags, rc, tflag;
+  WORD_DESC *ret;
 
-  istring_index = istring_size = 0;
+  istring = (char *)NULL;
 
   /* Don't fork () if there is no need to.  In the case of no command to
      run, just return NULL. */
   if (!string || !*string || (string[0] == '\n' && !string[1]))
-    return ((char *)NULL);
+    return ((WORD_DESC *)NULL);
+
+  if (wordexp_only && read_but_dont_execute)
+    {
+      last_command_exit_value = 125;
+      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 ? SEVAL_RESETLINE : 0;
 
   /* Pipe the output of executing STRING into the current shell. */
   if (pipe (fildes) < 0)
     {
-      internal_error ("Can't make pipes for command substitution!");
+      sys_error (_("cannot make pipe for command substitution"));
       goto error_exit;
     }
 
   old_pid = last_made_pid;
 #if defined (JOB_CONTROL)
-  {
-    pid_t old_pipeline_pgrp = pipeline_pgrp;
-
+  old_pipeline_pgrp = pipeline_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 ();
-    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 ();
-    set_sigchld_handler ();
-    stop_making_children ();
-    pipeline_pgrp = old_pipeline_pgrp;
-  }
-#else /* !JOB_CONTROL */
-  pid = make_child ((char *)NULL, 0);
+  cleanup_the_pipeline ();
+#endif /* JOB_CONTROL */
+
+  old_async_pid = last_asynchronous_pid;
+  pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
+  last_asynchronous_pid = old_async_pid;
 
   if (pid == 0)
     /* Reset the signal handlers in the child, but don't free the
        trap strings. */
     reset_signal_handlers ();
-#endif /* !JOB_CONTROL */
+
+#if defined (JOB_CONTROL)
+  /* XXX DO THIS ONLY IN PARENT ? XXX */
+  set_sigchld_handler ();
+  stop_making_children ();
+  if (pid != 0)
+    pipeline_pgrp = old_pipeline_pgrp;
+#else
+  stop_making_children ();
+#endif /* JOB_CONTROL */
 
   if (pid < 0)
     {
-      internal_error ("Can't make a child for command substitution: %s",
-       strerror (errno));
+      sys_error (_("cannot make child for command substitution"));
     error_exit:
 
       FREE (istring);
       close (fildes[0]);
       close (fildes[1]);
-      return ((char *)NULL);
+      return ((WORD_DESC *)NULL);
     }
 
   if (pid == 0)
     {
       set_sigint_handler ();   /* XXX */
-#if defined (JOB_CONTROL)
-      set_job_control (0);
-#endif
+
+      free_pushed_string_input ();
+
       if (dup2 (fildes[1], 1) < 0)
        {
-         internal_error
-           ("command_substitute: cannot duplicate pipe as fd 1: %s",
-            strerror (errno));
+         sys_error (_("command_substitute: cannot duplicate pipe as fd 1"));
          exit (EXECUTION_FAILURE);
        }
 
@@ -2220,71 +4836,65 @@ command_substitute (string, quoted)
       /* The currently executing shell is not interactive. */
       interactive = 0;
 
-      /* Command substitution does not inherit the -e flag. */
-      exit_immediately_on_error = 0;
+      /* This is a subshell environment. */
+      subshell_environment |= SUBSHELL_COMSUB;
+
+      /* When not in POSIX mode, command substitution does not inherit
+        the -e flag. */
+      if (posixly_correct == 0)
+       exit_immediately_on_error = 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);
 
-      if (result == EXITPROG)
-       exit (last_command_exit_value);
+      /* 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 (return_catch);
+      else
+       function_value = 0;
+
+      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", -1));
+       {
+         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
     {
-      FILE *istream;
-
-      istream = fdopen (fildes[0], "r");
-
 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
       close_pgrp_pipe ();
 #endif /* JOB_CONTROL && PGRP_PIPE */
 
       close (fildes[1]);
 
-      if (!istream)
-       {
-         internal_error ("Can't reopen pipe to command substitution (fd %d): %s",
-                       fildes[0], strerror (errno));
-         goto error_exit;
-       }
-
-      /* Read the output of the command through the pipe. */
-      while (1)
-       {
-#if defined (NO_READ_RESTART_ON_SIGNAL)
-         c = getc_with_restart (istream);
-#else
-         c = getc (istream);
-#endif /* !NO_READ_RESTART_ON_SIGNAL */
-
-         if (c == EOF)
-           break;
-
-         /* Add the character to ISTRING. */
-         if (istring_index + 2 >= istring_size)
-           {
-             while (istring_index + 2 >= istring_size)
-               istring_size += DEFAULT_ARRAY_SIZE;
-             istring = xrealloc (istring, istring_size);
-           }
-
-         if (quoted || c == CTLESC || c == CTLNUL)
-           istring[istring_index++] = CTLESC;
-
-         istring[istring_index++] = c;
-         istring[istring_index] = '\0';
-       }
+      tflag = 0;
+      istring = read_comsub (fildes[0], quoted, &tflag);
 
-      fclose (istream);
       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;
@@ -2293,42 +4903,28 @@ 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. */
-      if (interactive && pipeline_pgrp != (pid_t)0)
-       give_terminal_to (pipeline_pgrp);
+        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 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) == 0)
+#endif
+       give_terminal_to (pipeline_pgrp, 0);
 #endif /* JOB_CONTROL */
 
-      /* If we read no output, just return now and save ourselves some
-        trouble. */
-      if (istring_index == 0)
-       goto error_exit;
-
-      /* Strip trailing newlines from the output of the command. */
-      if (quoted)
-       {
-         while (istring_index > 0)
-           {
-             if (istring[istring_index - 1] == '\n')
-               {
-                 --istring_index;
-
-                 /* If the newline was quoted, remove the quoting char. */
-                 if (istring[istring_index - 1] == CTLESC)
-                   --istring_index;
-               }
-             else
-               break;
-           }
-         istring[istring_index] = '\0';
-       }
-      else
-       strip_trailing (istring, 1);
+      ret = alloc_word_desc ();
+      ret->word = istring;
+      ret->flags = tflag;
 
-      return (istring);
+      return ret;
     }
 }
 
@@ -2338,189 +4934,365 @@ command_substitute (string, quoted)
  *                                                     *
  ********************************************************/
 
-/* 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)
-     char *value, *temp;
-     int c;
+#if defined (ARRAY_VARS)
+
+static arrayind_t
+array_length_reference (s)
+     char *s;
 {
-  int pattern_specifier;
-  WORD_LIST *l;
-  char *pattern, *t, *tword;
+  int len;
+  arrayind_t ind;
+  char *akey;
+  char *t, c;
+  ARRAY *array;
+  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 || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error)
+    {
+      c = *--t;
+      *t = '\0';
+      err_unboundvar (s);
+      *t = c;
+      return (-1);
+    }
+  else if (var == 0)
+    return 0;
 
-  if (c == '#')
+  /* 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;
+
+  if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
     {
-      if (*value == '#')
-       {
-         value++;
-         pattern_specifier = RP_LONG_LEFT;
-       }
+      if (assoc_p (var))
+       return (assoc_num_elements (assoc_cell (var)));
+      else if (array_p (var))
+       return (array_num_elements (array));
       else
-       pattern_specifier = RP_SHORT_LEFT;
+       return 1;
     }
-  else /* c == '%' */
+
+  if (assoc_p (var))
     {
-      if (*value == '%')
+      t[len - 1] = '\0';
+      akey = expand_assignment_string_to_string (t, 0);        /* [ */
+      t[len - 1] = ']';
+      if (akey == 0 || *akey == 0)
        {
-         value++;
-         pattern_specifier = RP_LONG_RIGHT;
+         err_badarraysub (t);
+         return (-1);
        }
-      else
-       pattern_specifier = RP_SHORT_RIGHT;
+      t = assoc_reference (assoc_cell (var), akey);
     }
-
-  /* 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 (strchr (value, '~'))
-    tword = tilde_expand (value);
   else
-    tword = savestring (value);
-
-  /* expand_string_internal () leaves WORD quoted and does not perform
-     word splitting. */
-  l = expand_string_internal (tword, 0);
-  free (tword);
-  pattern = string_list (l);
-  dispose_words (l);
-
-  if (pattern)
     {
-      tword = quote_string_for_globbing (pattern, 1);
-      free (pattern);
-      pattern = tword;
+      ind = array_expand_index (t, len);
+      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 = remove_pattern (temp, pattern, pattern_specifier);
-
-  FREE (pattern);
-  return (t);
+  len = MB_STRLEN (t);
+  return (len);
 }
+#endif /* ARRAY_VARS */
 
 static int
 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))
+    return 1;
+#endif /* ARRAY_VARS */
   else if (legal_identifier (name))
     return 1;
   else
     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))
+    {
+      temp1 = xstrchr (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 *
+static WORD_DESC *
 parameter_brace_expand_word (name, var_is_special, quoted)
      char *name;
      int var_is_special, quoted;
 {
-  char *temp = (char *)NULL;
-
-  /* Handle multiple digit arguments, as in ${11}. */
-  if (digit (*name))
+  WORD_DESC *ret;
+  char *temp, *tt;
+  intmax_t arg_index;
+  SHELL_VAR *var;
+  int atype, rflags;
+
+  ret = 0;
+  temp = 0;
+  rflags = 0;
+
+  /* Handle multiple digit arguments, as in ${11}. */  
+  if (legal_number (name, &arg_index))
     {
-      int 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)      /* ${@} */
     {
-      char *tt;
-      WORD_LIST *l;
-
-      tt = xmalloc (2 + strlen (name));
-      tt[0] = '$'; tt[1] = '\0';
+      int sindex;
+      tt = (char *)xmalloc (2 + strlen (name));
+      tt[sindex = 0] = '$';
       strcpy (tt + 1, name);
-      l = expand_string_leave_quoted (tt, quoted);
+
+      ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
+                         (int *)NULL, (int *)NULL, 0);
       free (tt);
-      temp = string_list (l);
-      dispose_words (l);
+    }
+#if defined (ARRAY_VARS)
+  else if (valid_array_reference (name))
+    {
+      temp = array_value (name, quoted, &atype);
+      if (atype == 0 && temp)
+       temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
+                 ? quote_string (temp)
+                 : quote_escapes (temp);
+      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_isset (var) && invisible_p (var) == 0)
+       {
+#if defined (ARRAY_VARS)
+         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 = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
+                     ? quote_string (temp)
+                     : quote_escapes (temp);
+       }
+      else
+       temp = (char *)NULL;
     }
   else
+    temp = (char *)NULL;
+
+  if (ret == 0)
     {
-      SHELL_VAR *var = find_variable (name);
+      ret = alloc_word_desc ();
+      ret->word = temp;
+      ret->flags |= rflags;
+    }
+  return ret;
+}
+
+/* Expand an indirect reference to a variable: ${!NAME} expands to the
+   value of the variable whose name is the value of NAME. */
+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;
 
-      if (var && !invisible_p (var) && (temp = value_cell (var)))
-       temp = quoted && temp && *temp ? quote_string (temp)
-                                      : quote_escapes (temp);
+  w = parameter_brace_expand_word (name, var_is_special, quoted);
+  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;
     }
-  return (temp);
+  dispose_word_desc (w);
+
+  chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at);
+  if (t == 0)
+    return (WORD_DESC *)NULL;
+
+  w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted);
+  free (t);
+
+  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 "=". */
-static char *
-parameter_brace_expand_rhs (name, value, c, quoted)
+   "-", "+", or "=".  QUOTED is true if the entire brace expression occurs
+   between double quotes. */
+static WORD_DESC *
+parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat)
      char *name, *value;
-     int c, quoted;
+     int c, quoted, *qdollaratp, *hasdollarat;
 {
+  WORD_DESC *w;
   WORD_LIST *l;
   char *t, *t1, *temp;
-  int i, lquote, hasdol;
-
-  if (value[0] == '~' ||
-      (strchr (value, '~') && unquoted_substring ("=~", value)))
-    temp = tilde_expand (value);
-  else
-    temp = savestring (value);
+  int hasdol;
 
-  /* This is a hack.  A better fix is coming later. */
-  lquote = 0;
-  if (*temp == '"' && temp[strlen (temp) - 1] == '"')
+  /* 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 (for sh backwards compatibility). */
+  if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
     {
-      i = 1;
-      t = string_extract_double_quoted (temp, &i);     /* XXX */
-      free (temp);
-      temp = t;
-      lquote = 1;      /* XXX */
+      hasdol = 0;
+      temp = string_extract_double_quoted (value, &hasdol, 1);
     }
-  hasdol = 0;
-  /* XXX was quoted not lquote */
-  l = *temp ? expand_string_for_rhs (temp, quoted||lquote, &hasdol, (int *)NULL)
-           : (WORD_LIST *)NULL;
-  free (temp);
-  /* expand_string_for_rhs does not dequote the word list it returns, but
-     there are a few cases in which we need to add quotes. */
-  if (lquote && quoted == 0 && hasdol == 0 && l && l->word->quoted == 0)
-    quote_list (l);
+  else
+    temp = value;
 
+  w = alloc_word_desc ();
+  hasdol = 0;
+  /* XXX was 0 not quoted */
+  l = *temp ? expand_string_for_rhs (temp, quoted, &hasdol, (int *)NULL)
+           : (WORD_LIST *)0;
+  if (hasdollarat)
+    *hasdollarat = hasdol || (l && l->next);
+  if (temp != value)
+    free (temp);
   if (l)
     {
-      temp = string_list (l);
+      /* The expansion of TEMP returned something.  We need to treat things
+         slightly differently if HASDOL 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.  We use string_list_dollar_star instead. */
+      temp = (hasdol || l->next) ? string_list_dollar_star (l) : 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 (qdollaratp && ((hasdol && quoted) || l->next))
+       *qdollaratp = 1;
       dispose_words (l);
     }
-  else if (lquote)
+  else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && hasdol)
     {
-      temp = xmalloc (2);
-      temp[0] = CTLNUL;
-      temp[1] = '\0';
+      /* 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 = make_quoted_char ('\0');
+      w->flags |= W_HASQUOTEDNULL;
     }
   else
     temp = (char *)NULL;
 
   if (c == '-' || c == '+')
-    return (temp);
+    {
+      w->word = temp;
+      return w;
+    }
 
   /* c == '=' */
-  if (temp)
-    t = savestring (temp);
-  else
-    t = savestring ("");
+  t = temp ? savestring (temp) : savestring ("");
   t1 = dequote_string (t);
   free (t);
-  bind_variable (name, t1);
+#if defined (ARRAY_VARS)
+  if (valid_array_reference (name))
+    assign_array_element (name, t1, 0);
+  else
+#endif /* ARRAY_VARS */
+  bind_variable (name, t1, 0);
   free (t1);
-  return (temp);
+
+  w->word = temp;
+  return w;
 }
 
 /* Deal with the right hand side of a ${name:?value} expansion in the case
@@ -2531,16 +5303,19 @@ static void
 parameter_brace_expand_error (name, value)
      char *name, *value;
 {
+  WORD_LIST *l;
+  char *temp;
+
   if (value && *value)
     {
-      WORD_LIST *l = expand_string (value, 0);
-      char *temp1 = string_list (l);
-      report_error ("%s: %s", name, temp1 ? temp1 : value);
-      FREE (temp1);
+      l = expand_string (value, 0);
+      temp =  string_list (l);
+      report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */
+      FREE (temp);
       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. */
@@ -2554,82 +5329,1875 @@ static int
 valid_length_expression (name)
      char *name;
 {
-  return (!name[1] ||                                          /* ${#} */
-         ((name[1] == '@' || name[1] == '*') && !name[2]) ||   /* ${#@}, ${#*} */
-         (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]} */
+#endif
          legal_identifier (name + 1));                         /* ${#PS1} */
 }
 
+#if defined (HANDLE_MULTIBYTE)
+size_t
+mbstrlen (s)
+     const char *s;
+{
+  size_t clen, nc;
+  mbstate_t mbs, mbsbak;
+
+  nc = 0;
+  memset (&mbs, 0, sizeof (mbs));
+  mbsbak = mbs;
+  while ((clen = mbrlen(s, MB_CUR_MAX, &mbs)) != 0)
+    {
+      if (MB_INVALIDCH(clen))
+        {
+         clen = 1;     /* assume single byte */
+         mbs = mbsbak;
+        }
+
+      s += clen;
+      nc++;
+      mbsbak = mbs;
+    }
+  return nc;
+}
+#endif
+      
+
 /* 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;
-  int number = 0;
+  char *t, *newname;
+  intmax_t number, arg_index;
+  WORD_LIST *list;
+#if defined (ARRAY_VARS)
+  SHELL_VAR *var;
+#endif
 
   if (name[1] == '\0')                 /* ${#} */
+    number = number_of_args ();
+  else if ((name[1] == '@' || name[1] == '*') && name[2] == '\0')      /* ${#@}, ${#*} */
+    number = number_of_args ();
+  else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
     {
-      WORD_LIST *l = list_rest_of_args ();
-      number = list_length (l);
-      dispose_words (l);
+      /* Take the lengths of some of the shell's special parameters. */
+      switch (name[1])
+       {
+       case '-':
+         t = which_set_flags ();
+         break;
+       case '?':
+         t = itos (last_command_exit_value);
+         break;
+       case '$':
+         t = itos (dollar_dollar_pid);
+         break;
+       case '!':
+         if (last_asynchronous_pid == NO_PID)
+           t = (char *)NULL;
+         else
+           t = itos (last_asynchronous_pid);
+         break;
+       case '#':
+         t = itos (number_of_args ());
+         break;
+       }
+      number = STRLEN (t);
+      FREE (t);
+    }
+#if defined (ARRAY_VARS)
+  else if (valid_array_reference (name + 1))
+    number = array_length_reference (name + 1);
+#endif /* ARRAY_VARS */
+  else
+    {
+      number = 0;
+
+      if (legal_number (name + 1, &arg_index))         /* ${#1} */
+       {
+         t = get_dollar_var_value (arg_index);
+         number = MB_STRLEN (t);
+         FREE (t);
+       }
+#if defined (ARRAY_VARS)
+      else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var)))
+       {
+         if (assoc_p (var))
+           t = assoc_reference (assoc_cell (var), "0");
+         else
+           t = array_reference (array_cell (var), 0);
+         number = MB_STRLEN (t);
+       }
+#endif
+      else                             /* ${#PS1} */
+       {
+         newname = savestring (name);
+         newname[0] = '$';
+         list = expand_string (newname, Q_DOUBLE_QUOTES);
+         t = list ? string_list (list) : (char *)NULL;
+         free (newname);
+         if (list)
+           dispose_words (list);
+
+         number = MB_STRLEN (t);
+         FREE (t);
+       }
+    }
+
+  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 `?'.
+*/
+
+static char *
+skiparith (substr, delim)
+     char *substr;
+     int delim;
+{
+  size_t sublen;
+  int skipcol, pcount, i;
+  DECLARE_MBSTATE;
+
+  sublen = strlen (substr);
+  i = skipcol = pcount = 0;
+  while (substr[i])
+    {
+      /* Balance parens */
+      if (substr[i] == LPAREN)
+       {
+         pcount++;
+         i++;
+         continue;
+       }
+      if (substr[i] == RPAREN && pcount)
+       {
+         pcount--;
+         i++;
+         continue;
+       }
+      if (pcount)
+       {
+         ADVANCE_CHAR (substr, sublen, i);
+         continue;
+       }
+
+      /* Skip one `:' for each `?' */
+      if (substr[i] == ':' && skipcol)
+       {
+         skipcol--;
+         i++;
+         continue;
+       }
+      if (substr[i] == delim)
+       break;
+      if (substr[i] == '?')
+       {
+         skipcol++;
+         i++;
+         continue;
+       }
+      ADVANCE_CHAR (substr, sublen, i);
+    }
+
+  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
+   VALUE is really a pointer to an array variable that should be used.
+   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 (v, value, substr, vtype, e1p, e2p)
+     SHELL_VAR *v;
+     char *value, *substr;
+     int vtype;
+     intmax_t *e1p, *e2p;
+{
+  char *t, *temp1, *temp2;
+  arrayind_t len;
+  int expok;
+#if defined (ARRAY_VARS)
+ ARRAY *a;
+ HASH_TABLE *h;
+#endif
+
+  /* duplicate behavior of strchr(3) */
+  t = skiparith (substr, ':');
+  if (*t && *t == ':')
+    *t = '\0';
+  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 = 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:
+      /* 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)
+    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++;
+      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 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
+       {
+         *e2p += *e1p;         /* want E2 chars starting at E1 */
+         if (*e2p > len)
+           *e2p = len;
+       }
+    }
+  else
+    *e2p = len;
+
+  return (1);
+}
+
+/* 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).
+   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, quoted, varp, valp)
+     char *varname, *value;
+     int quoted;
+     SHELL_VAR **varp;
+     char **valp;
+{
+  int vtype;
+  char *temp;
+#if defined (ARRAY_VARS)
+  SHELL_VAR *v;
+#endif
+
+  /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
+  vtype = (varname[0] == '@' || varname[0] == '*') && varname[1] == '\0';
+  if (vtype == VT_POSPARMS && varname[0] == '*')
+    vtype |= VT_STARSUB;
+  *varp = (SHELL_VAR *)NULL;
+
+#if defined (ARRAY_VARS)
+  if (valid_array_reference (varname))
+    {
+      v = array_variable_part (varname, &temp, (int *)0);
+      if (v && (array_p (v) || assoc_p (v)))
+       { /* [ */
+         if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')
+           {
+             /* Callers have to differentiate betwen indexed and associative */
+             vtype = VT_ARRAYVAR;
+             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, (int *)NULL);
+           }
+         *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
+       {
+         vtype = VT_ARRAYMEMBER;
+         *varp = v;
+         *valp = array_value (varname, 1, (int *)NULL);
+       }
     }
-  else if (name[1] != '*' && name[1] != '@')
+  else if ((v = find_variable (varname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v)))
+    {
+      vtype = VT_ARRAYMEMBER;
+      *varp = v;
+      *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0);
+    }
+  else
+#endif
+    {
+      if (value && vtype == VT_VARIABLE)
+       {
+         if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
+           *valp = dequote_string (value);
+         else
+           *valp = dequote_escapes (value);
+       }
+      else
+       *valp = value;
+    }
+
+  return vtype;
+}
+
+/******************************************************/
+/*                                                   */
+/* Functions to extract substrings of variable values */
+/*                                                   */
+/******************************************************/
+
+#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, 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;
+{
+  intmax_t e1, e2;
+  int vtype, r, starsub;
+  char *temp, *val, *tt, *oname;
+  SHELL_VAR *v;
+
+  if (value == 0)
+    return ((char *)NULL);
+
+  oname = this_command_name;
+  this_command_name = varname;
+
+  vtype = get_var_and_type (varname, value, quoted, &v, &val);
+  if (vtype == -1)
+    {
+      this_command_name = oname;
+      return ((char *)NULL);
+    }
+
+  starsub = vtype & VT_STARSUB;
+  vtype &= ~VT_STARSUB;
+
+  r = verify_substring_values (v, val, substr, vtype, &e1, &e2);
+  this_command_name = oname;
+  if (r <= 0)
+    return ((r == 0) ? &expand_param_error : (char *)NULL);
+
+  switch (vtype)
+    {
+    case VT_VARIABLE:
+    case VT_ARRAYMEMBER:
+#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:
+      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:
+      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;
+}
+
+/****************************************************************/
+/*                                                             */
+/* Functions to perform pattern substitution on variable values */
+/*                                                             */
+/****************************************************************/
+
+char *
+pat_subst (string, pat, rep, mflags)
+     char *string, *pat, *rep;
+     int mflags;
+{
+  char *ret, *s, *e, *str;
+  int rsize, rptr, l, replen, mtype;
+
+  mtype = mflags & MATCH_TYPEMASK;
+
+  /* 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.
+   */
+  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);
+    }
+
+  ret = (char *)xmalloc (rsize = 64);
+  ret[0] = '\0';
+
+  for (replen = STRLEN (rep), rptr = 0, str = string;;)
+    {
+      if (match_pattern (str, pat, mtype, &s, &e) == 0)
+       break;
+      l = s - str;
+      RESIZE_MALLOCED_BUFFER (ret, rptr, (l + replen), 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. */
+      if (l)
+       {
+         strncpy (ret + rptr, str, l);
+         rptr += l;
+       }
+      if (replen)
+       {
+         strncpy (ret + rptr, rep, replen);
+         rptr += replen;
+       }
+      str = e;         /* e == end of match */
+
+      if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
+       break;
+
+      if (s == e)
+       e++, str++;             /* avoid infinite recursion on zero-length match */
+    }
+
+  /* Now copy the unmatched portion of the input string */
+  if (*str)
+    {
+      RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
+      strcpy (ret + rptr, str);
+    }
+  else
+    ret[rptr] = '\0';
+
+  return ret;
+}
+
+/* Do pattern match and replacement on the positional parameters. */
+static char *
+pos_params_pat_subst (string, pat, rep, mflags)
+     char *string, *pat, *rep;
+     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 = pat_subst (params->word->word, pat, rep, mflags);
+      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;
+
+#if 0
+  if ((mflags & (MATCH_QUOTED|MATCH_STARSUB)) == (MATCH_QUOTED|MATCH_STARSUB))
+    ret = string_list_dollar_star (quote_list (save));
+  else if ((mflags & MATCH_STARSUB) == MATCH_STARSUB)
+    ret = string_list_dollar_star (save);
+  else if ((mflags & MATCH_QUOTED) == MATCH_QUOTED)
+    ret = string_list_dollar_at (save, qflags);
+  else
+    ret = string_list_dollar_star (save);
+#else
+  ret = string_list_pos_params (pchar, save, qflags);
+#endif
+
+  dispose_words (save);
+
+  return (ret);
+}
+
+/* Perform pattern substitution on VALUE, which is the expansion of
+   VARNAME.  PATSUB is an expression supplying the pattern to match
+   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;
+{
+  int vtype, mflags, starsub, delim;
+  char *val, *temp, *pat, *rep, *p, *lpatsub, *tt;
+  SHELL_VAR *v;
+
+  if (value == 0)
+    return ((char *)NULL);
+
+  this_command_name = varname;
+
+  vtype = get_var_and_type (varname, value, quoted, &v, &val);
+  if (vtype == -1)
+    return ((char *)NULL);
+
+  starsub = vtype & VT_STARSUB;
+  vtype &= ~VT_STARSUB;
+
+  mflags = 0;
+  if (patsub && *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 (starsub)
+    mflags |= MATCH_STARSUB;
+
+  /* If the pattern starts with a `/', make sure we skip over it when looking
+     for the replacement delimiter. */
+#if 0
+  if (rep = quoted_strchr ((*patsub == '/') ? lpatsub+1 : lpatsub, '/', ST_BACKSL))
+    *rep++ = '\0';
+  else
+    rep = (char *)NULL;
+#else
+  delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0);
+  if (lpatsub[delim] == '/')
+    {
+      lpatsub[delim] = 0;
+      rep = lpatsub + delim + 1;
+    }
+  else
+    rep = (char *)NULL;
+#endif
+
+  if (rep && *rep == '\0')
+    rep = (char *)NULL;
+
+  /* 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 = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
+      else
+       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 (mflags & MATCH_GLOBREP)
+    mflags |= MATCH_ANY;
+  else if (pat && pat[0] == '#')
+    {
+      mflags |= MATCH_BEG;
+      p++;
+    }
+  else if (pat && pat[0] == '%')
+    {
+      mflags |= MATCH_END;
+      p++;
+    }
+  else
+    mflags |= MATCH_ANY;
+
+  /* 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.  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, modspec, patspec, quoted)
+     char *varname, *value;
+     int modspec;
+     char *patspec;
+     int quoted;
+{
+  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, quoted, &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 = 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
+    }
+
+  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] == '(')
+       count++;
+      else if (s[i] == ')')
+       {
+         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);
+         break;
+
+       case '"':
+         i = skip_double_quoted ((char *)s, len, ++i);
+         break;
+       }
+    }
+
+  return (count == 0);
+}
+
+/****************************************************************/
+/*                                                             */
+/*     Functions to perform parameter expansion on a string    */
+/*                                                             */
+/****************************************************************/
+
+/* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
+static WORD_DESC *
+parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_dollar_at)
+     char *string;
+     int *indexp, quoted, *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, want_casemod;
+  char *name, *value, *temp, *temp1;
+  WORD_DESC *tdesc, *ret;
+  int t_index, sindex, c, tflag, modspec;
+  intmax_t number;
+
+  temp = temp1 = value = (char *)NULL;
+  var_is_set = var_is_null = var_is_special = check_nullness = 0;
+  want_substring = want_indir = want_patsub = want_casemod = 0;
+
+  sindex = *indexp;
+  t_index = ++sindex;
+  /* ${#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 */
+
+  ret = 0;
+  tflag = 0;
+
+  /* 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] == '*')))
+    {
+      t_index++;
+      free (name);
+      temp1 = string_extract (string, &t_index, "#%:-=?+/}", 0);
+      name = (char *)xmalloc (3 + (strlen (temp1)));
+      *name = string[sindex];
+      if (string[sindex] == '!')
+       {
+         /* indirect reference of $#, $?, $@, or $* */
+         name[1] = string[sindex + 1];
+         strcpy (name + 2, temp1);
+       }
+      else     
+       strcpy (name + 1, temp1);
+      free (temp1);
+    }
+  sindex = t_index;
+
+  /* Find out what character ended the variable name.  Then
+     do the appropriate thing. */
+  if (c = string[sindex])
+    sindex++;
+
+  /* If c is followed by one of the valid parameter expansion
+     characters, move past it as normal.  If not, assume that
+     a substring specification is being given, and do not move
+     past it. */
+  if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
+    {
+      check_nullness++;
+      if (c = string[sindex])
+       sindex++;
+    }
+  else if (c == ':' && string[sindex] != RBRACE)
+    want_substring = 1;
+  else if (c == '/' && string[sindex] != RBRACE)
+    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 &&
+       VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
+    {
+      name = (char *)xrealloc (name, 3);
+      name[1] = c;
+      name[2] = '\0';
+      c = string[sindex++];
+    }
+
+  /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
+  if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
+       member (c, "%:=+/") && string[sindex] == RBRACE)
+    {
+      temp = (char *)NULL;
+      goto bad_substitution;
+    }
+
+  /* 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 ((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 (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. */
+      if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
+       {
+         temp = (char *)NULL;
+         goto bad_substitution;
+       }
+
+      number = parameter_brace_expand_length (name);
+      free (name);
+
+      *indexp = sindex;
+      if (number < 0)
+        return (&expand_wdesc_error);
+      else
+       {
+         ret = alloc_word_desc ();
+         ret->word = itos (number);
+         return ret;
+       }
+    }
+
+  /* ${@} is identical to $@. */
+  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;
+    }
+
+  /* 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);
+         if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
+           *quoted_dollar_atp = 1;
+         if (contains_dollar_at)
+           *contains_dollar_at = 1;
+       }
+      free (x);
+      free (xlist);
+      free (temp1);
+      *indexp = sindex;
+
+      ret = alloc_word_desc ();
+      ret->word = temp;
+      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))
+    {
+      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;
+           }       
+
+         free (temp1);
+         *indexp = sindex;
+
+         ret = alloc_word_desc ();
+         ret->word = temp;
+         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;
+    }
+
+  if (want_indir)
+    tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at);
+  else
+    tdesc = parameter_brace_expand_word (name, var_is_special, quoted);
+
+  if (tdesc)
+    {
+      temp = tdesc->word;
+      tflag = tdesc->flags;
+      dispose_word_desc (tdesc);
+    }
+  else
+    temp = (char  *)0;
+
+#if defined (ARRAY_VARS)
+  if (valid_array_reference (name))
+    chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at);
+#endif
+
+  var_is_set = temp != (char *)0;
+  var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
+
+  /* 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, 0);
+      if (string[sindex] == RBRACE)
+       sindex++;
+      else
+       goto bad_substitution;
+    }
+  else
+    value = (char *)NULL;
+
+  *indexp = sindex;
+
+  /* If this is a substring spec, process it and add the result. */
+  if (want_substring)
+    {
+      temp1 = parameter_brace_substring (name, temp, value, quoted);
+      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_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);
+      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;
+      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;
+    }
+#if defined (CASEMOD_EXPANSIONS)
+  else if (want_casemod)
+    {
+      temp1 = parameter_brace_casemod (name, temp, modspec, value, quoted);
+      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_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 : "??");
+      FREE (value);
+      FREE (temp);
+      free (name);
+      return &expand_wdesc_error;
+
+    case RBRACE:
+      if (var_is_set == 0 && unbound_vars_is_error)
+       {
+         err_unboundvar (name);
+         FREE (value);
+         FREE (temp);
+         free (name);
+         last_command_exit_value = EXECUTION_FAILURE;
+         return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
+       }
+      break;
+
+    case '#':  /* ${param#[#]pattern} */
+    case '%':  /* ${param%[%]pattern} */
+      if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
+       {
+         FREE (value);
+         break;
+       }
+      temp1 = parameter_brace_remove_pattern (name, temp, value, c, quoted);
+      free (temp);
+      free (value);
+
+      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)
+       {
+         /* 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)
+               {
+                 ret = parameter_brace_expand_rhs (name, value, c,
+                                                   quoted,
+                                                   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;
+           }
+         else
+           {
+             FREE (value);
+           }
+         /* 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);
+             free (name);
+             free (value);
+             return &expand_wdesc_error;
+           }
+         else if (c == '?')
+           {
+             parameter_brace_expand_error (name, value);
+             return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
+           }
+         else 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;
+
+             ret = parameter_brace_expand_rhs (name, value, c, quoted,
+                                               quoted_dollar_atp,
+                                               contains_dollar_at);
+             /* XXX - fix up later, esp. noting presence of
+                      W_HASQUOTEDNULL in tdesc->flags */
+           }
+         free (value);
+       }
+
+      break;
+    }
+  free (name);
+
+  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 WORD_DESC *
+param_expand (string, sindex, quoted, expanded_something,
+             contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
+             pflags)
+     char *string;
+     int *sindex, quoted, *expanded_something, *contains_dollar_at;
+     int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
+{
+  char *temp, *temp1, uerror[3];
+  int zindex, t_index, expok;
+  unsigned char c;
+  intmax_t number;
+  SHELL_VAR *var;
+  WORD_LIST *list;
+  WORD_DESC *tdesc, *ret;
+  int tflag;
+
+  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)
     {
-      number = 0;
+    /* $0 .. $9? */
+    case '0':
+    case '1':
+    case '2':
+    case '3':
+    case '4':
+    case '5':
+    case '6':
+    case '7':
+    case '8':
+    case '9':
+      temp1 = dollar_vars[TODIGIT (c)];
+      if (unbound_vars_is_error && temp1 == (char *)NULL)
+       {
+         uerror[0] = '$';
+         uerror[1] = c;
+         uerror[2] = '\0';
+         err_unboundvar (uerror);
+         last_command_exit_value = EXECUTION_FAILURE;
+         return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
+       }
+      if (temp1)
+       temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+                 ? quote_string (temp1)
+                 : quote_escapes (temp1);
+      else
+       temp = (char *)NULL;
 
-      if (digit (name[1]))             /* ${#1} */
+      break;
+
+    /* $$ -- pid of the invoking shell. */
+    case '$':
+      temp = itos (dollar_dollar_pid);
+      break;
+
+    /* $# -- number of positional parameters. */
+    case '#':
+      temp = itos (number_of_args ());
+      break;
+
+    /* $? -- return value of the last synchronous command. */
+    case '?':
+      temp = itos (last_command_exit_value);
+      break;
+
+    /* $- -- flags supplied to the shell on invocation or by `set'. */
+    case '-':
+      temp = which_set_flags ();
+      break;
+
+      /* $! -- Pid of the last asynchronous command. */
+    case '!':
+      /* If no asynchronous pids have been created, expand to nothing.
+        If `set -u' has been executed, and no async processes have
+        been created, this is an expansion error. */
+      if (last_asynchronous_pid == NO_PID)
        {
-         if (t = get_dollar_var_value (atoi (name + 1)))
+         if (expanded_something)
+           *expanded_something = 0;
+         temp = (char *)NULL;
+         if (unbound_vars_is_error)
            {
-             number = strlen (t);
-             free (t);
+             uerror[0] = '$';
+             uerror[1] = c;
+             uerror[2] = '\0';
+             err_unboundvar (uerror);
+             last_command_exit_value = EXECUTION_FAILURE;
+             return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
            }
        }
-      else                             /* ${#PS1} */
-       {
-         WORD_LIST *list;
-         char *newname;
+      else
+       temp = itos (last_asynchronous_pid);
+      break;
 
-         newname = savestring (name);
-         newname[0] = '$';
-         list = expand_string (newname, 0);
-         t = string_list (list);
-         free (newname);
-         dispose_words (list);
+    /* The only difference between this and $@ is when the arg is quoted. */
+    case '*':          /* `$*' */
+      list = list_rest_of_args ();
 
-         if (t)
-           number = strlen (t);
+      if (list == 0 && unbound_vars_is_error)
+       {
+         uerror[0] = '$';
+         uerror[1] = '*';
+         uerror[2] = '\0';
+         err_unboundvar (uerror);
+         last_command_exit_value = EXECUTION_FAILURE;
+         return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
+       }
 
-         FREE (t);
+      /* 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))
+       {
+         /* 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 = (quoted & Q_DOUBLE_QUOTES) ? string_list_dollar_star (list) : string_list (list);
+         temp1 = quote_string (temp);
+         if (*temp == 0)
+           tflag |= W_HASQUOTEDNULL;
+         free (temp);
+         temp = temp1;
        }
-    }
-  else                                 /* ${#@} and ${#*} */
-    {
-#if !defined (KSH_INCOMPATIBLE)
-      WORD_LIST *l = list_rest_of_args ();
-      number = l ? list_length (l) : 0;
-      dispose_words (l);
+      else
+       {
+         /* 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 1
+#  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);
 #else
-      if (t = string_rest_of_args (1))
+         temp = string_list_dollar_at (list, quoted);
+#endif
+         if (expand_no_split_dollar_star == 0 && contains_dollar_at)
+           *contains_dollar_at = 1;
+       }
+
+      dispose_words (list);
+      break;
+
+    /* When we have "$@" what we want is "$1" "$2" "$3" ... This
+       means that we have to turn quoting off after we split into
+       the individually quoted arguments so that the final split
+       on the first character of $IFS is still done.  */
+    case '@':          /* `$@' */
+      list = list_rest_of_args ();
+
+      if (list == 0 && unbound_vars_is_error)
+       {
+         uerror[0] = '$';
+         uerror[1] = '@';
+         uerror[2] = '\0';
+         err_unboundvar (uerror);
+         last_command_exit_value = EXECUTION_FAILURE;
+         return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
+       }
+
+      /* 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. */
+      if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+       *quoted_dollar_at_p = 1;
+      if (contains_dollar_at)
+       *contains_dollar_at = 1;
+
+      /* We want to separate the positional parameters with the first
+        character of $IFS in case $IFS is something other than a space.
+        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);
+
+      dispose_words (list);
+      break;
+
+    case LBRACE:
+      tdesc = parameter_brace_expand (string, &zindex, quoted,
+                                     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
+        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.  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;
+         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, 0);
+      zindex = t_index;
+
+      /* For Posix.2-style `$(( ))' arithmetic substitution,
+        extract the expression and pass it to the evaluator. */
+      if (temp && *temp == LPAREN)
+       {
+         char *temp2;
+         temp1 = temp + 1;
+         temp2 = savestring (temp1);
+         t_index = strlen (temp2) - 1;
+
+         if (temp2[t_index] != RPAREN)
+           {
+             free (temp2);
+             goto comsub;
+           }
+
+         /* Cut off ending `)' */
+         temp2[t_index] = '\0';
+
+         if (chk_arithsub (temp2, t_index) == 0)
+           {
+             free (temp2);
+             goto comsub;
+           }
+
+         /* Expand variables found inside the expression. */
+         temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
+         free (temp2);
+
+arithsub:
+         /* No error messages. */
+         this_command_name = (char *)NULL;
+         number = evalexp (temp1, &expok);
+         free (temp);
+         free (temp1);
+         if (expok == 0)
+           {
+             if (interactive_shell == 0 && posixly_correct)
+               {
+                 last_command_exit_value = EXECUTION_FAILURE;
+                 return (&expand_wdesc_fatal);
+               }
+             else
+               return (&expand_wdesc_error);
+           }
+         temp = itos (number);
+         break;
+       }
+
+comsub:
+      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;
+
+    /* Do POSIX.2d9-style arithmetic substitution.  This will probably go
+       away in a future bash release. */
+    case '[':
+      /* 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 = expand_arith_string (temp, Q_DOUBLE_QUOTES);
+
+      goto arithsub;
+
+    default:
+      /* Find the variable in VARIABLE_LIST. */
+      temp = (char *)NULL;
+
+      for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
+       ;
+      temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
+
+      /* If this isn't a variable name, then just output the `$'. */
+      if (temp1 == 0 || *temp1 == '\0')
+       {
+         FREE (temp1);
+         temp = (char *)xmalloc (2);
+         temp[0] = '$';
+         temp[1] = '\0';
+         if (expanded_something)
+           *expanded_something = 0;
+         goto return0;
+       }
+
+      /* If the variable exists, return its value cell. */
+      var = find_variable (temp1);
+
+      if (var && invisible_p (var) == 0 && var_isset (var))
+       {
+#if defined (ARRAY_VARS)
+         if (assoc_p (var) || array_p (var))
+           {
+             temp = array_p (var) ? array_reference (array_cell (var), 0)
+                                  : assoc_reference (assoc_cell (var), "0");
+             if (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 = value_cell (var);
+
+             temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
+                       ? quote_string (temp)
+                       : quote_escapes (temp);
+           }
+
+         free (temp1);
+
+         goto return0;
+       }
+
+      temp = (char *)NULL;
+
+unbound_variable:
+      if (unbound_vars_is_error)
+       err_unboundvar (temp1);
+      else
        {
-         number = strlen (t);
-         free (t);
+         free (temp1);
+         goto return0;
        }
-#endif /* KSH_INCOMPATIBLE */
+
+      free (temp1);
+      last_command_exit_value = EXECUTION_FAILURE;
+      return ((unbound_vars_is_error && interactive_shell == 0)
+               ? &expand_wdesc_fatal
+               : &expand_wdesc_error);
     }
-  return (number);
+
+  if (string[zindex])
+    zindex++;
+
+return0:
+  *sindex = zindex;
+
+  if (ret == 0)
+    {
+      ret = alloc_word_desc ();
+      ret->flags = tflag;      /* XXX */
+      ret->word = temp;
+    }
+  return ret;
 }
 
-/* Make a word list which is the parameter and variable expansion,
-   command substitution, arithmetic substitution, and quote removed
-   expansion of WORD.  Return a pointer to a WORD_LIST which is the
-   result of the expansion.  If WORD contains a null word, the word
-   list returned is also null.
+/* Make a word list which is the result of parameter and variable
+   expansion, command substitution, arithmetic substitution, and
+   quote removal of WORD.  Return a pointer to a WORD_LIST which is
+   the result of the expansion.  If WORD contains a null word, the
+   word list returned is also null.
+
+   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.
 
-   QUOTED, when non-zero specifies that the text of WORD is treated
-   as if it were surrounded by double quotes.
    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.
@@ -2645,56 +7213,78 @@ parameter_brace_expand_length (name)
 #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;
 {
-  /* The thing that we finally output. */
-  WORD_LIST *result = (WORD_LIST *)NULL;
+  WORD_LIST *list;
+  WORD_DESC *tword;
 
   /* The intermediate string that we build while expanding. */
-  char *istring = xmalloc (DEFAULT_ARRAY_SIZE);
+  char *istring;
 
   /* The current size of the above object. */
-  int istring_size = DEFAULT_ARRAY_SIZE;
+  int istring_size;
 
   /* Index into ISTRING. */
-  int istring_index = 0;
+  int istring_index;
 
   /* Temporary string storage. */
-  char *temp = (char *)NULL;
+  char *temp, *temp1;
 
   /* The text of WORD. */
-  register char *string = word->word;
+  register char *string;
+
+  /* The size of STRING. */
+  size_t string_size;
 
   /* The index into STRING. */
-  int sindex = 0;
+  int sindex;
 
   /* This gets 1 if we see a $@ while quoted. */
-  int quoted_dollar_at = 0;
+  int quoted_dollar_at;
 
   /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
      whether WORD contains no quoting characters, a partially quoted
      string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
-  int quoted_state = UNQUOTED;
+  int quoted_state;
+
+  /* State flags */
+  int had_quoted_null;
+  int has_dollar_at;
+  int tflag;
 
-  register int c;              /* Current character. */
-  int number;                  /* Temporary number value. */
+  int assignoff;               /* If assignment, offset of `=' */
+
+  register unsigned char c;    /* Current character. */
   int t_index;                 /* For calls to string_extract_xxx. */
-  char *command_subst_result;  /* For calls to command_substitute (). */
 
-  istring[0] = '\0';
+  char twochars[2];
+
+  DECLARE_MBSTATE;
 
-  if (!string) goto final_exit;
+  istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
+  istring[istring_index = 0] = '\0';
+  quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
+  quoted_state = UNQUOTED;
+
+  string = word->word;
+  if (string == 0)
+    goto finished_with_string;
+  /* 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 (;;)
+  for (sindex = 0; ;)
     {
       c = string[sindex];
 
@@ -2705,41 +7295,55 @@ expand_word_internal (word, quoted, contains_dollar_at, expanded_something)
          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])
            sindex++;
 
-         goto add_string;
+add_string:
+         if (temp)
+           {
+             istring = sub_append_string (temp, istring, &istring_index, &istring_size);
+             temp = (char *)0;
+           }
+
+         break;
 
 #if defined (PROCESS_SUBSTITUTION)
          /* Process substitution. */
        case '<':
        case '>':
          {
-           char *temp1;
-           int old_index;
-
-           if (string[++sindex] != '(' || quoted || posixly_correct)
+           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
-             old_index = ++sindex; /* skip past both '<' and '(' */
+             t_index = sindex + 1; /* skip past both '<' and LPAREN */
 
-           temp1 = extract_process_subst
-             (string, (c == '<') ? "<(" : ">(", &old_index);
-           sindex = old_index;
+           temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index); /*))*/
+           sindex = t_index;
 
            /* If the process substitution specification is `<()', we want to
               open the pipe for writing in the child and produce output; if
               it is `>()', we want to open the pipe for reading in the child
               and consume input. */
-           temp = process_substitute (temp1, (c == '>'));
+           temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
 
            FREE (temp1);
 
@@ -2747,575 +7351,175 @@ expand_word_internal (word, quoted, contains_dollar_at, expanded_something)
          }
 #endif /* PROCESS_SUBSTITUTION */
 
-       /* See about breaking this into a separate function:
-           char *
-           param_expand (string, sindex, quoted, expanded_something,
-                         contains_dollar_at, quoted_dollar_at)
-           char *string;
-           int *sindex, quoted, *expanded_something, *contains_dollar_at;
-           int *quoted_dollar_at;
-       */
-       case '$':
-
-         if (expanded_something)
-           *expanded_something = 1;
-
-         c = string[++sindex];
-
-         /* Do simple cases first. Switch on what follows '$'. */
-         switch (c)
+       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))
            {
-             /* $0 .. $9? */
-           case '0':
-           case '1':
-           case '2':
-           case '3':
-           case '4':
-           case '5':
-           case '6':
-           case '7':
-           case '8':
-           case '9':
-             temp = dollar_vars[digit_value (c)];
-             if (unbound_vars_is_error && temp == (char *)NULL)
-               {
-                 report_error ("$%c: unbound variable", c);
-                 free (string);
-                 free (istring);
-                 last_command_exit_value = 1;
-                 return (&expand_word_error);
-               }
-             if (temp)
-               temp = savestring (temp);
-             goto dollar_add_string;
-
-             /* $$ -- pid of the invoking shell. */
-           case '$':
-             number = dollar_dollar_pid;
-
-           add_number:
-             temp = itos (number);
-           dollar_add_string:
-             if (string[sindex]) sindex++;
-
-             /* Add TEMP to ISTRING. */
-           add_string:
-             istring = sub_append_string
-               (temp, istring, &istring_index, &istring_size);
-             temp = (char *)NULL;
-             break;
-
-             /* $# -- number of positional parameters. */
-           case '#':
-             {
-               WORD_LIST *list = list_rest_of_args ();
-               number = list_length (list);
-               dispose_words (list);
-               goto add_number;
-             }
+             if (isexp == 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
+         if (isexp == 0 && isifs (c))
+           goto add_ifs_character;
+         else
+           goto add_character;
 
-             /* $? -- return value of the last synchronous command. */
-           case '?':
-             number = last_command_exit_value;
-             goto add_number;
+       case ':':
+         if (word->flags & W_NOTILDE)
+           {
+             if (isexp == 0 && isifs (c))
+               goto add_ifs_character;
+             else
+               goto add_character;
+           }
 
-             /* $- -- flags supplied to the shell on invocation or
-                by `set'. */
-           case '-':
-             temp = which_set_flags ();
-             goto dollar_add_string;
+         if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) &&
+             string[sindex+1] == '~')
+           word->flags |= W_ITILDE;
 
-             /* $! -- Pid of the last asynchronous command. */
-           case '!':
-             number = (int)last_asynchronous_pid;
+         if (isexp == 0 && isifs (c))
+           goto add_ifs_character;
+         else
+           goto add_character;
 
-             /* If no asynchronous pids have been created, echo nothing. */
-             if (number == (int)NO_PID)
-               {
-                 if (string[sindex])
-                   sindex++;
-                 if (expanded_something)
-                   *expanded_something = 0;
-                 break;
-               }
-             goto add_number;
+       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 ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
+             (sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
+             (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
+           {
+             word->flags &= ~W_ITILDE;
+             if (isexp == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
+               goto add_ifs_character;
+             else
+               goto add_character;
+           }
 
-             /* The only difference between this and $@ is when the
-                arg is quoted. */
-           case '*':           /* `$*' */
-             temp = string_rest_of_args (quoted);
+         if (word->flags & W_ASSIGNRHS)
+           tflag = 2;
+         else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
+           tflag = 1;
+         else
+           tflag = 0;
 
-             if (quoted && temp && *temp == '\0' /* && istring_index > 0 */)
-               {
-                 free (temp);
-                 temp = (char *)NULL;
-               }
+         temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
+           
+         word->flags &= ~W_ITILDE;
 
-             /* In the case of a quoted string, quote the entire arg-list.
-                "$1 $2 $3". */
-             if (quoted && temp)
+         if (temp && *temp && t_index > 0)
+           {
+             temp1 = bash_tilde_expand (temp, tflag);
+             if  (temp1 && *temp1 == '~' && STREQ (temp, temp1))
                {
-                 char *james_brown = temp;
-                 temp = quote_string (temp);
-                 free (james_brown);
+                 FREE (temp);
+                 FREE (temp1);
+                 goto add_character;           /* tilde expansion failed */
                }
-             goto dollar_add_string;
-
-             /* When we have "$@" what we want is "$1" "$2" "$3" ... This
-                means that we have to turn quoting off after we split into
-                the individually quoted arguments so that the final split
-                on the first character of $IFS is still done.  */
-           case '@':           /* `$@' */
-             {
-               WORD_LIST *tlist = list_rest_of_args ();
-               if (quoted && tlist)
-                 quote_list (tlist);
-
-               /* 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. */
-               if (quoted)
-                 quoted_dollar_at = 1;
-               if (contains_dollar_at)
-                 *contains_dollar_at = 1;
-               temp = string_list (tlist);
-               dispose_words (tlist);          
-               goto dollar_add_string;
-             }
-
-             /* ${[#]name[[:]#[#]%[%]-=?+[word]]} */
-           case '{':
-             {
-               int check_nullness = 0;
-               int var_is_set = 0;
-               int var_is_null = 0;
-               int var_is_special = 0;
-               char *name, *value;
-
-               t_index = ++sindex;
-               name = string_extract (string, &t_index, "#%:-=?+}");
-               value = (char *)NULL;
-
-               /* If the name really consists of a special variable, then
-                  make sure that we have the entire name. */
-               if (sindex == t_index &&
-                   (string[sindex] == '-' ||
-                    string[sindex] == '?' ||
-                    string[sindex] == '#'))
-                 {
-                   char *tt;
-                   t_index++;
-                   free (name);
-                   tt = string_extract (string, &t_index, "#%:-=?+}");
-                   name = xmalloc (2 + (strlen (tt)));
-                   *name = string[sindex];
-                   strcpy (name + 1, tt);
-                   free (tt);
-                 }
-               sindex = t_index;
-
-               /* Find out what character ended the variable name.  Then
-                  do the appropriate thing. */
-               if (c = string[sindex])
-                 sindex++;
-
-               if (c == ':')
-                 {
-                   check_nullness++;
-                   if (c = string[sindex])
-                     sindex++;
-                 }
-
-               /* Determine the value of this variable. */
-               if ((digit (*name) && all_digits (name)) ||
-                   (strlen (name) == 1 && member (*name, "#-?$!@*")))
-                 var_is_special++;
-
-               /* Check for special expansion things. */
-               if (*name == '#')
-                 {
-                   /* Handle ${#-} and ${#?}.  They return the lengths of
-                      $- and $?, respectively. */
-                   if (string[sindex] == '}' &&
-                       !name[1] &&
-                       !check_nullness &&
-                       (c == '-' || c == '?'))
-                     {
-                       char *s;
-
-                       free (name);
-
-                       if (c == '-')
-                         s = which_set_flags ();
-                       else
-                         s = itos (last_command_exit_value);
-
-                       number = STRLEN (s);
-                       FREE (s);
-                       goto add_number;
-                     }
-
-                   /* Don't allow things like ${#:-foo} to go by; they are
-                      errors.  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.  Explicitly check for ${#:},
-                      which the rules do not catch. */
-                   if (string[sindex - 1] != '}' || member (c, "?-=+") ||
-                       (string[sindex - 1] == '}' && !name[1] && c == '}' &&
-                        check_nullness))
-                     {
-                       free (name);
-                       name = string;
-                       goto bad_substitution;
-                     }
-
-                   /* Check NAME for validity before trying to go on. */
-                   if (!valid_length_expression (name))
-                     {
-                       free (name);
-                       name = string;
-                       goto bad_substitution;
-                     }
-
-                   number = parameter_brace_expand_length (name);
-                   free (name);
-                   /* We are pointing one character after the brace which
-                      closes this expression.  Since the code at add_number
-                      increments SINDEX, we back up a single character. */
-                   sindex--;
-                   goto add_number;
-                 }
-
-               /* ${@} is identical to $@. */
-               if (name[0] == '@' && name[1] == '\0')
-                 {
-                   if (quoted)
-                     quoted_dollar_at = 1;
-
-                   if (contains_dollar_at)
-                     *contains_dollar_at = 1;
-                 }
-
-               /* Make sure that NAME is valid before trying to go on. */
-               if (!valid_brace_expansion_word (name, var_is_special))
-                 {
-                   free (name);
-                   name = string;
-                   goto bad_substitution;
-                 }
-
-               temp =
-                 parameter_brace_expand_word (name, var_is_special, quoted);
-
-               if (temp)
-                 var_is_set++;
-
-               if (!var_is_set || !temp || !*temp)
-                 var_is_null++;
-
-               if (!check_nullness)
-                 var_is_null = 0;
-
-               /* Get the rest of the stuff inside the braces. */
-               if (c && c != '}')
-                 {
-                   /* Extract the contents of the ${ ... } expansion
-                      according to the Posix.2 rules.  It's much less of
-                      a hack that the former extract_delimited_string ()
-                      scheme. */
-                   value = extract_dollar_brace_string (string, &sindex);
-
-                   if (string[sindex] == '}')
-                     sindex++;
-                   else
-                     {
-                       free (name);
-                       name = string;
-                       goto bad_substitution;
-                     }
-                 }
-               else
-                 value = (char *)NULL;
-
-               /* Do the right thing based on which character ended the
-                  variable name. */
-               switch (c)
-                 {
-                 default:
-                   free (name);
-                   name = string;
-                   /* FALL THROUGH */
-
-                 case '\0':
-                 bad_substitution:
-                   report_error ("%s: bad substitution", name ? name : "??");
-                   FREE (value);
-                   free (temp);
-                   free (name);
-                   free (istring);
-                   return &expand_word_error;
-
-                 case '}':
-                   if (!var_is_set && unbound_vars_is_error)
-                     {
-                       report_error ("%s: unbound variable", name);
-                       FREE (value);
-                       free (temp);
-                       free (name);
-                       free (string);
-                       last_command_exit_value = 1;
-                       free (istring);
-                       return &expand_word_error;
-                     }
-                   break;
-
-                 case '#':     /* ${param#[#]pattern} */
-                 case '%':     /* ${param%[%]pattern} */
-                   {
-                     char *t;
-                     if (!value || !*value || !temp || !*temp)
-                       break;
-                     if (quoted)
-                       {
-                         t = dequote_string (temp);
-                         free (temp);
-                         temp = t;
-                       }
-                     t = parameter_brace_remove_pattern (value, temp, c);
-                     free (temp);
-                     free (value);
-                     temp = t;
-                   }
-                   break;
-
-                 case '-':
-                 case '=':
-                 case '?':
-                 case '+':
-                   if (var_is_set && !var_is_null)
-                     {
-                       /* We don't want the value of the named variable for
-                          anything, just the value of the right hand side. */
-                       if (c == '+')
-                         {
-                           FREE (temp);
-                           if (value)
-                             {
-                               temp = parameter_brace_expand_rhs
-                                 (name, value, c, quoted);
-                               /* XXX - this is a hack.  A better fix is
-                                        coming later. */
-                               if ((value[0] == '$' && value[1] == '@') ||
-                                   (value[0] == '"' && value[1] == '$' && value[2] == '@'))
-                                 {
-                                   if (quoted)
-                                     quoted_dollar_at++;
-                                   if (contains_dollar_at)
-                                     *contains_dollar_at = 1;
-                                 }
-                               free (value);
-                             }
-                           else
-                             temp = (char *)NULL;
-                         }
-                       else
-                         {
-                           FREE (value);
-                         }
-                       /* 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);
-                           free (name);
-                           free (value);
-                           free (string);
-                           free (istring);
-                           return &expand_word_error;
-                         }
-                       else if (c == '?')
-                         {
-                           free (string);
-                           free (istring);
-                           parameter_brace_expand_error (name, value);
-                           if (!interactive)
-                             return &expand_word_fatal;
-                           else
-                             return &expand_word_error;
-                         }
-                       else if (c != '+')
-                         temp = parameter_brace_expand_rhs
-                           (name, value, c, quoted);
-                       free (value);
-                     }
-                   break;
-                 }             /* end case on closing character. */
-               free (name);
-               goto add_string;
-             }                 /* end case '{' */
-             /* break; */
-
-             /* Do command or arithmetic substitution. */
-           case '(':
-             /* We have to extract the contents of this paren substitution. */
-             {
-               int old_index = ++sindex;
-
-               temp = extract_command_subst (string, &old_index);
-               sindex = old_index;
-
-               /* For Posix.2-style `$(( ))' arithmetic substitution,
-                  extract the expression and pass it to the evaluator. */
-               if (temp && *temp == '(')
-                 {
-                   char *t = temp + 1;
-                   int last = strlen (t) - 1;
-
-                   if (t[last] != ')')
-                     {
-                       report_error ("%s: bad arithmetic substitution", temp);
-                       free (temp);
-                       free (string);
-                       free (istring);
-                       return &expand_word_error;
-                     }
-
-                   /* Cut off ending `)' */
-                   t[last] = '\0';
-
-                   /* Expand variables found inside the expression. */
-                   {
-                     WORD_LIST *l;
-
-                     l = expand_string (t, 1);
-                     t = string_list (l);
-                     dispose_words (l);
-                   }
-
-                   /* No error messages. */
-                   this_command_name = (char *)NULL;
-
-                   number = evalexp (t);
-                   free (temp);
-                   free (t);
-
-                   goto add_number;
-                 }
+             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;
 
-               goto handle_command_substitution;
-             }
+         has_dollar_at = 0;
+         tword = param_expand (string, &sindex, quoted, expanded_something,
+                              &has_dollar_at, &quoted_dollar_at,
+                              &had_quoted_null,
+                              (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0);
 
-             /* Do straight arithmetic substitution. */
-           case '[':
-             /* We have to extract the contents of this
-                arithmetic substitution. */
-             {
-               char *t;
-               int old_index = ++sindex;
-               WORD_LIST *l;
+         if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
+           {
+             free (string);
+             free (istring);
+             return ((tword == &expand_wdesc_error) ? &expand_word_error
+                                                    : &expand_word_fatal);
+           }
+         if (contains_dollar_at && has_dollar_at)
+           *contains_dollar_at = 1;
 
-               temp = extract_arithmetic_subst (string, &old_index);
-               sindex = old_index;
+         if (tword && (tword->flags & W_HASQUOTEDNULL))
+           had_quoted_null = 1;
 
-               /* Do initial variable expansion. */
-               l = expand_string (temp, 1);
-               t = string_list (l);
-               dispose_words (l);
+         temp = tword->word;
+         dispose_word_desc (tword);
 
-               /* No error messages. */
-               this_command_name = (char *)NULL;
-               number = evalexp (t);
-               free (t);
-               free (temp);
+         goto add_string;
+         break;
 
-               goto add_number;
-             }
+       case '`':               /* Backquoted command substitution. */
+         {
+           t_index = sindex++;
 
-           default:
+           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)
              {
-               /* Find the variable in VARIABLE_LIST. */
-               int old_index;
-               char *name;
-               SHELL_VAR *var;
-
-               temp = (char *)NULL;
-
-               for (old_index = sindex;
-                    (c = string[sindex]) &&
-                    (isletter (c) || digit (c) || c == '_');
-                    sindex++);
-               name = substring (string, old_index, sindex);
-
-               /* If this isn't a variable name, then just output the `$'. */
-               if (!name || !*name)
-                 {
-                   FREE (name);
-                   temp = savestring ("$");
-                   if (expanded_something)
-                     *expanded_something = 0;
-                   goto add_string;
-                 }
-
-               /* If the variable exists, return its value cell. */
-               var = find_variable (name);
-
-               if (var && !invisible_p (var) && value_cell (var))
-                 {
-                   temp = value_cell (var);
-                   temp = quoted && temp && *temp ? quote_string (temp)
-                                                  : quote_escapes (temp);
-                   free (name);
-                   goto add_string;
-                 }
-               else
-                 temp = (char *)NULL;
-
-               if (unbound_vars_is_error)
-                 report_error ("%s: unbound variable", name);
-               else
+               if (sindex - 1 == t_index)
                  {
-                   free (name);
-                   goto add_string;
+                   sindex = t_index;
+                   goto add_character;
                  }
-
-               free (name);
+               report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index);
                free (string);
-               last_command_exit_value = 1;
                free (istring);
-               return &expand_word_error;
+               return ((temp == &extract_string_error) ? &expand_word_error
+                                                       : &expand_word_fatal);
              }
-           }
-         break;                /* End case '$': */
-
-       case '`':               /* Backquoted command substitution. */
-         {
-           sindex++;
-
+               
            if (expanded_something)
              *expanded_something = 1;
 
-           temp = string_extract (string, &sindex, "`");
-           de_backslash (temp);
-
-         handle_command_substitution:
-           command_subst_result = command_substitute (temp, quoted);
-
-           FREE (temp);
-
-           temp = command_subst_result;
-
-           if (string[sindex])
-             sindex++;
-
-           goto add_string;
+           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;
          }
 
        case '\\':
@@ -3324,215 +7528,272 @@ expand_word_internal (word, quoted, contains_dollar_at, expanded_something)
              sindex += 2;
              continue;
            }
-         else
-           {
-             char *slashify_chars = "";
 
-             c = string[++sindex];
+         c = string[++sindex];
 
-             if (quoted == Q_HERE_DOCUMENT)
-               slashify_chars = slashify_in_here_document;
-             else if (quoted == Q_DOUBLE_QUOTES)
-               slashify_chars = slashify_in_quotes;
+         if (quoted & Q_HERE_DOCUMENT)
+           tflag = CBSHDOC;
+         else if (quoted & Q_DOUBLE_QUOTES)
+           tflag = CBSDQUOTE;
+         else
+           tflag = 0;
 
-             if (quoted && !member (c, slashify_chars))
-               {
-                 temp = xmalloc (3);
-                 temp[0] = '\\'; temp[1] = c; temp[2] = '\0';
-                 if (c)
-                   sindex++;
-                 goto add_string;
-               }
-             else
-               {
-                 /* This character is quoted, so add it in quoted mode. */
-                 temp = make_quoted_char (c);
-                 if (c)
-                   sindex++;
-                 goto add_string;
-               }
+         if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 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
+           {
+             SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
+           }
+
+         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)
+#if 0
+         if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
+#else
+         if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
+#endif
            goto add_character;
-         sindex++;
-         {
-           WORD_LIST *tresult = (WORD_LIST *)NULL;
 
-           t_index = sindex;
-           temp = string_extract_double_quoted (string, &sindex);
+         t_index = ++sindex;
+         temp = string_extract_double_quoted (string, &sindex, 0);
 
-           /* If the quotes surrounded the entire string, then the
-              whole word was quoted. */
-           if (t_index == 1 && !string[sindex])
-             quoted_state = WHOLLY_QUOTED;
-           else
-             quoted_state = PARTIALLY_QUOTED;
+         /* 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;
 
-           if (temp && *temp)
-             {
-               int dollar_at_flag;
-               int quoting_flags = Q_DOUBLE_QUOTES;
-               WORD_DESC *temp_word = make_word (temp);
+         if (temp && *temp)
+           {
+             tword = alloc_word_desc ();
+             tword->word = temp;
 
-               free (temp);
+             temp = (char *)NULL;
 
-               tresult = expand_word_internal
-                 (temp_word, quoting_flags, &dollar_at_flag, (int *)NULL);
+             has_dollar_at = 0;
+             /* Need to get W_HASQUOTEDNULL flag through this function. */
+             list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &has_dollar_at, (int *)NULL);
 
-               if (tresult == &expand_word_error || tresult == &expand_word_fatal)
-                 {
-                   free (istring);
-                   free (string);
-                   /* expand_word_internal has already freed temp_word->word
-                      for us because of the way it prints error messages. */
-                   temp_word->word = (char *)NULL;
-                   dispose_word (temp_word);
-                   return tresult;
-                 }
+             if (list == &expand_word_error || list == &expand_word_fatal)
+               {
+                 free (istring);
+                 free (string);
+                 /* expand_word_internal has already freed temp_word->word
+                    for us because of the way it prints error messages. */
+                 tword->word = (char *)NULL;
+                 dispose_word (tword);
+                 return list;
+               }
 
-               dispose_word (temp_word);
+             dispose_word (tword);
 
-               /* "$@" (a double-quoted dollar-at) expands into nothing,
-                  not even a NULL word, when there are no positional
-                  parameters. */
-               if (!tresult && dollar_at_flag)
-                 {
-                   quoted_dollar_at++;
-                   break;
-                 }
+             /* "$@" (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)
+               {
+                 quoted_dollar_at++;
+                 break;
+               }
 
-               /* If we get "$@", we know we have expanded something, so we
-                  need to remember it for the final split on $IFS.  This is
-                  a special case; it's the only case where a quoted string
-                  can expand into more than one word.  It's going to come back
-                  from the above call to expand_word_internal as a list with
-                  a single word, in which all characters are quoted and
-                  separated by blanks.  What we want to do is to turn it back
-                  into a list for the next piece of code. */
-               dequote_list (tresult);
-
-               if (dollar_at_flag)
-                 {
-                   quoted_dollar_at++;
-                   if (expanded_something)
-                     *expanded_something = 1;
-                 }
-             }
-           else
-             {
-               /* What we have is "".  This is a minor optimization. */
-               free (temp);
-               tresult = (WORD_LIST *)NULL;
-             }
+             /* If we get "$@", we know we have expanded something, so we
+                need to remember it for the final split on $IFS.  This is
+                a special case; it's the only case where a quoted string
+                can expand into more than one word.  It's going to come back
+                from the above call to expand_word_internal as a list with
+                a single word, in which all characters are quoted and
+                separated by blanks.  What we want to do is to turn it back
+                into a list for the next piece of code. */
+             if (list)
+               dequote_list (list);
+
+             if (list && list->word && (list->word->flags & W_HASQUOTEDNULL))
+               had_quoted_null = 1;
+
+             if (has_dollar_at)
+               {
+                 quoted_dollar_at++;
+                 if (contains_dollar_at)
+                   *contains_dollar_at = 1;
+                 if (expanded_something)
+                   *expanded_something = 1;
+               }
+           }
+         else
+           {
+             /* What we have is "".  This is a minor optimization. */
+             FREE (temp);
+             list = (WORD_LIST *)NULL;
+           }
 
-           /* The code above *might* return a list (consider the case of "$@",
-              where it returns "$1", "$2", etc.).  We can't throw away the
-              rest of the list, and we have to make sure each word gets added
-              as quoted.  We test on tresult->next:  if it is non-NULL, we
-              quote the whole list, save it to a string with string_list, and
-              add that string. We don't need to quote the results of this
-              (and it would be wrong, since that would quote the separators
-              as well), so we go directly to add_string. */
-           if (tresult)
-             {
-               if (tresult->next)
-                 {
-                   quote_list (tresult);
-                   temp = string_list (tresult);
-                   dispose_words (tresult);
-                   goto add_string;
-                 }
-               else
-                 {
-                   temp = savestring (tresult->word->word);
-                   dispose_words (tresult);
-                 }
-             }
-           else
-             temp = (char *)NULL;
+         /* The code above *might* return a list (consider the case of "$@",
+            where it returns "$1", "$2", etc.).  We can't throw away the
+            rest of the list, and we have to make sure each word gets added
+            as quoted.  We test on tresult->next:  if it is non-NULL, we
+            quote the whole list, save it to a string with string_list, and
+            add that string. We don't need to quote the results of this
+            (and it would be wrong, since that would quote the separators
+            as well), so we go directly to add_string. */
+         if (list)
+           {
+             if (list->next)
+               {
+                 /* 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 (quote_list (list));
+                 dispose_words (list);
+                 goto add_string;
+               }
+             else
+               {
+                 temp = savestring (list->word->word);
+                 tflag = list->word->flags;
+                 dispose_words (list);
+
+                 /* 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
+                    the empty string, though.  We do this because we
+                    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. */
+                 /* 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 */
+               }
+           }
+         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 && (quoted_state == PARTIALLY_QUOTED))
-             continue;
+         /* 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)
+           continue;
 
-         add_quoted_string:
+       add_quoted_string:
+
+         if (temp)
+           {
+             temp1 = temp;
+             temp = quote_string (temp);
+             free (temp1);
+             goto add_string;
+           }
+         else
+           {
+             /* Add NULL arg. */
+             c = CTLNUL;
+             sindex--;         /* add_character: label increments sindex */
+             goto add_character;
+           }
 
-           if (temp)
-             {
-               char *t = temp;
-               temp = quote_string (temp);
-               free (t);
-             }
-           else
-             {
-               /* Add NULL arg. */
-               temp = xmalloc (2);
-               temp[0] = CTLNUL;
-               temp[1] = '\0';
-             }
-           goto add_string;
-         }
          /* break; */
 
        case '\'':
-         {
-           if (!quoted)
-             {
-               sindex++;
+#if 0
+         if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
+#else
+         if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
+#endif
+           goto add_character;
 
-               t_index = sindex;
-               temp = string_extract_single_quoted (string, &sindex);
+         t_index = ++sindex;
+         temp = string_extract_single_quoted (string, &sindex);
 
-               /* If the entire STRING was surrounded by single quotes,
-                  then the string is wholly quoted. */
-               if (t_index == 1 && !string[sindex])
-                 quoted_state = WHOLLY_QUOTED;
-               else
-                 quoted_state = PARTIALLY_QUOTED;
+         /* If the entire STRING was surrounded by single quotes,
+            then the string is wholly quoted. */
+         quoted_state = (t_index == 1 && string[sindex] == '\0')
+                           ? WHOLLY_QUOTED
+                           : PARTIALLY_QUOTED;
 
-               /* If all we had was '', it is a null expansion. */
-               if (!*temp)
-                 {
-                   free (temp);
-                   temp = (char *)NULL;
-                 }
-               else
-                 remove_quoted_escapes (temp);
+         /* If all we had was '', it is a null expansion. */
+         if (*temp == '\0')
+           {
+             free (temp);
+             temp = (char *)NULL;
+           }
+         else
+           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 && (quoted_state == PARTIALLY_QUOTED))
-                 continue;
+         /* 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))
+           continue;
 
-               goto add_quoted_string;
-             }
-           else
+         /* 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;
-         }
+         /* break; */
 
        default:
-
          /* This is the fix for " $@ " */
-         if (quoted)
+       add_ifs_character:
+         if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c)))
            {
-             temp = make_quoted_char (c);
-             if (string[sindex])
+             if (string[sindex])       /* from old goto dollar_add_string */
                sindex++;
-             goto add_string;
+             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:
-         if (istring_index + 1 >= istring_size)
-           {
-             while (istring_index + 1 >= istring_size)
-               istring_size += DEFAULT_ARRAY_SIZE;
-             istring = xrealloc (istring, istring_size);
-           }
+         RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
+                                 DEFAULT_ARRAY_SIZE);
          istring[istring_index++] = c;
          istring[istring_index] = '\0';
 
@@ -3542,100 +7803,114 @@ expand_word_internal (word, quoted, contains_dollar_at, expanded_something)
     }
 
 finished_with_string:
-final_exit:
   /* OK, we're ready to return.  If we have a quoted string, and
      quoted_dollar_at is not set, we do no splitting at all; otherwise
      we split on ' '.  The routines that call this will handle what to
      do if nothing has been expanded. */
-  if (istring)
-    {
-      WORD_LIST *temp_list;
 
-      /* Partially and wholly quoted strings which expand to the empty
-        string are retained as an empty arguments.  Unquoted strings
-        which expand to the empty string are discarded.  The single
-        exception is the case of expanding "$@" when there are no
-        positional parameters.  In that case, we discard the expansion. */
-
-      /* Because of how the code that handles "" and '' in partially
-        quoted strings works, we need to make ISTRING into a QUOTED_NULL
-        if we saw quoting characters, but the expansion was empty.
-        "" and '' are tossed away before we get to this point when
-        processing partially quoted strings.  This makes "" and $xxx""
-        equivalent when xxx is unset. */
-      if (!*istring && quoted_state == PARTIALLY_QUOTED)
+  /* Partially and wholly quoted strings which expand to the empty
+     string are retained as an empty arguments.  Unquoted strings
+     which expand to the empty string are discarded.  The single
+     exception is the case of expanding "$@" when there are no
+     positional parameters.  In that case, we discard the expansion. */
+
+  /* Because of how the code that handles "" and '' in partially
+     quoted strings works, we need to make ISTRING into a QUOTED_NULL
+     if we saw quoting characters, but the expansion was empty.
+     "" and '' are tossed away before we get to this point when
+     processing partially quoted strings.  This makes "" and $xxx""
+     equivalent when xxx is unset.  We also look to see whether we
+     saw a quoted null from a ${} expansion and add one back if we
+     need to. */
+
+  /* If we expand to nothing and there were no single or double quotes
+     in the word, we throw it away.  Otherwise, we return a NULL word.
+     The single exception is for $@ surrounded by double quotes when
+     there are no positional parameters.  In that case, we also throw
+     the word away. */
+
+  if (*istring == '\0')
+    {
+      if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
        {
-         if (istring_size < 2)
-           istring = xrealloc (istring, istring_size += 2);
          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;
        }
-
-      /* If we expand to nothing and there were no single or double quotes
-        in the word, we throw it away.  Otherwise, we return a NULL word.
-        The single exception is for $@ surrounded by double quotes when
-        there are no positional parameters.  In that case, we also throw
-        the word away. */
-      if (!*istring)
-       {
-         if (quoted_state == UNQUOTED ||
-             (quoted_dollar_at && quoted_state == WHOLLY_QUOTED))
-           temp_list = (WORD_LIST *)NULL;
-         else
-           {
-             temp_list = make_word_list
-               (make_word (istring), (WORD_LIST *)NULL);
-             temp_list->word->quoted = quoted;
-           }
-       }
-      else if (word->assignment)
+      /* 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. */
+      else  if (quoted_state == UNQUOTED || quoted_dollar_at)
+       list = (WORD_LIST *)NULL;
+#if 0
+      else
        {
-         temp_list = make_word_list (make_word (istring), (WORD_LIST *)NULL);
-         temp_list->word->quoted = quoted;
-         temp_list->word->assignment = assignment (temp_list->word->word);
+         tword = make_bare_word (istring);
+         if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+           tword->flags |= W_QUOTED;
+         list = make_word_list (tword, (WORD_LIST *)NULL);
        }
+#else
       else
-       {
-         char *ifs_chars = (char *)NULL;
+       list = (WORD_LIST *)NULL;
+#endif
+    }
+  else if (word->flags & W_NOSPLIT)
+    {
+      tword = make_bare_word (istring);
+      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_NOEXPAND)
+       tword->flags |= W_NOEXPAND;     /* XXX */
+      if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
+       tword->flags |= W_QUOTED;
+      if (had_quoted_null)
+       tword->flags |= W_HASQUOTEDNULL;
+      list = make_word_list (tword, (WORD_LIST *)NULL);
+    }
+  else
+    {
+      char *ifs_chars;
 
-         if (quoted_dollar_at)
-           {
-             SHELL_VAR *ifs = find_variable ("IFS");     
-             if (ifs)
-               ifs_chars = value_cell (ifs);
-             else
-               ifs_chars = " \t\n";
-           }
+      ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
 
-         /* According to Posix.2, "$@" expands to a single word if
-            IFS="" and the positional parameters are not empty. */
-         if (quoted_dollar_at && ifs_chars && *ifs_chars)
-           {
-             temp_list = list_string (istring, " ", 1);
-#if 0
-             /* This turns quoted null strings back into CTLNULs */
-             dequote_list (temp_list);
-             quote_list (temp_list);
-#endif
-           }
-         else
-           {
-             WORD_DESC *tword;
-             tword = make_word (istring);
-             temp_list = make_word_list (tword, (WORD_LIST *)NULL);
-             tword->quoted = quoted || (quoted_state == WHOLLY_QUOTED);
-             tword->assignment = word->assignment;
-           }
+      /* 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)
+       list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
+      else
+       {
+         tword = make_bare_word (istring);
+         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_NOEXPAND)
+           tword->flags |= W_NOEXPAND;
+         if (had_quoted_null)
+           tword->flags |= W_HASQUOTEDNULL;    /* XXX */
+         list = make_word_list (tword, (WORD_LIST *)NULL);
        }
-
-      free (istring);
-      result = (WORD_LIST *)
-       list_append (REVERSE_LIST (result, WORD_LIST *), temp_list);
     }
-  else
-    result = (WORD_LIST *)NULL;
 
-  return (result);
+  free (istring);
+  return (list);
 }
 
 /* **************************************************************** */
@@ -3645,51 +7920,59 @@ final_exit:
 /* **************************************************************** */
 
 /* 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, *temp1;
-  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 (sindex = dquote = 0; c = string[sindex];)
+  for (dquote = sindex = 0; c = string[sindex];)
     {
       switch (c)
        {
        case '\\':
          c = string[++sindex];
-         if ((quoted || dquote) && !member (c, slashify_in_quotes))
+         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 '\'':
-         if (quoted || dquote)
+         if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
            {
              *r++ = c;
              sindex++;
+             break;
            }
-         else
+         tindex = sindex + 1;
+         temp = string_extract_single_quoted (string, &tindex);
+         if (temp)
            {
-             tindex = ++sindex;
-             temp = string_extract_single_quoted (string, &tindex);
-             sindex = tindex;
-
-             if (temp)
-               {
-                 strcpy (r, temp);
-                 r += strlen (r);
-                 free (temp);
-               }
+             strcpy (r, temp);
+             r += strlen (r);
+             free (temp);
            }
+         sindex = tindex;
          break;
 
        case '"':
@@ -3702,6 +7985,8 @@ string_quote_removal (string, quoted)
     return (result_string);
 }
 
+#if 0
+/* UNUSED */
 /* Perform quote removal on word WORD.  This allocates and returns a new
    WORD_DESC *. */
 WORD_DESC *
@@ -3713,7 +7998,8 @@ word_quote_removal (word, quoted)
   char *t;
 
   t = string_quote_removal (word->word, quoted);
-  w = make_word (t);
+  w = alloc_word_desc ();
+  w->word = t ? t : savestring ("");
   return (w);
 }
 
@@ -3721,120 +8007,31 @@ word_quote_removal (word, quoted)
    the members of the list are treated as if they are surrounded by
    double quotes.  Return a new list, or NULL if LIST is NULL. */
 WORD_LIST *
-word_list_quote_removal (list, quoted)
-     WORD_LIST *list;
-     int quoted;
-{
-  WORD_LIST *result = (WORD_LIST *)NULL, *t, *tresult;
-
-  t = list;
-  while (t)
-    {
-      tresult = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
-      tresult->word = word_quote_removal (t->word, quoted);
-      tresult->next = (WORD_LIST *)NULL;
-      result = (WORD_LIST *) list_append (result, tresult);
-      t = t->next;
-    }
-  return (result);
-}
-
-/* Return 1 if CHARACTER appears in an unquoted portion of
-   STRING.  Return 0 otherwise. */
-static int
-unquoted_member (character, string)
-     int character;
-     char *string;
-{
-  int sindex, tindex, c;
-  char *temp;
-
-  sindex = 0;
-
-  while (c = string[sindex])
-    {
-      if (c == character)
-       return (1);
-
-      switch (c)
-       {
-         case '\\':
-           sindex++;
-           if (string[sindex])
-             sindex++;
-           break;
-
-         case '"':
-         case '\'':
-
-           tindex = ++sindex;
-           if (c == '"')
-             temp = string_extract_double_quoted (string, &tindex);
-           else
-             temp = string_extract_single_quoted (string, &tindex);
-           sindex = tindex;
-
-           FREE (temp);
-           break;
-
-         default:
-           sindex++;
-           break;
-       }
-    }
-  return (0);
-}
-
-/* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
-static int
-unquoted_substring (substr, string)
-     char *substr, *string;
-{
-  int sindex, tindex, c, sublen;
-  char *temp;
-
-  if (!substr || !*substr)
-    return (0);
-
-  sublen = strlen (substr);
-  sindex = 0;
-
-  while (c = string[sindex])
-    {
-      if (STREQN (string + sindex, substr, sublen))
-       return (1);
-
-      switch (c)
-       {
-         case '\\':
-           sindex++;
-
-           if (string[sindex])
-             sindex++;
-           break;
-
-         case '"':
-         case '\'':
-
-           tindex = ++sindex;
-
-           if (c == '"')
-             temp = string_extract_double_quoted (string, &tindex);
-           else
-             temp = string_extract_single_quoted (string, &tindex);
-           sindex = tindex;
-
-           FREE (temp);
-
-           break;
+word_list_quote_removal (list, quoted)
+     WORD_LIST *list;
+     int quoted;
+{
+  WORD_LIST *result, *t, *tresult, *e;
 
-         default:
-           sindex++;
-           break;
+  for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
+    {
+      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 (0);
+  return (result);
 }
+#endif
 
 /*******************************************
  *                                        *
@@ -3842,44 +8039,76 @@ unquoted_substring (substr, string)
  *                                        *
  *******************************************/
 
+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";
+
+  /* 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;
 
   if (w)
     {
-      SHELL_VAR *ifs = find_variable ("IFS");
-      char *ifs_chars;
-
-      /* If IFS is unset, it defaults to " \t\n". */
-      if (ifs)
-       ifs_chars = value_cell (ifs);
-      else
-       ifs_chars = " \t\n";
-
-      if (w->quoted || !ifs_chars)
-       ifs_chars = "";
+      char *xifs;
 
-#ifdef NOT_YET_MAYBE_LATER
-      if (!*ifs)
-       {
-         /* No splitting done if word quoted or ifs set to "". */
-         WORD_DESC *wtemp;
-         wtemp = make_word (w->word);
-         wtemp->quoted = w->quoted;
-         result = make_word_list (wtemp);
-       }
-      else
-#endif
-      result = list_string (w->word, ifs_chars, w->quoted);
+      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;
+
   return (result);
 }
 
@@ -3889,30 +8118,56 @@ static WORD_LIST *
 word_list_split (list)
      WORD_LIST *list;
 {
-  WORD_LIST *result = (WORD_LIST *)NULL, *t, *tresult;
+  WORD_LIST *result, *t, *tresult, *e;
 
-  t = list;
-  while (t)
+  for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
     {
-      tresult = word_split (t->word);
-      result = (WORD_LIST *) list_append (result, tresult);
-      t = t->next;
+      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);
 }
 
 /**************************************************
  *                                               *
- *     Functions to expand an entire WORD_LIST   *
+ *    Functions to expand an entire WORD_LIST    *
  *                                               *
  **************************************************/
 
-static WORD_LIST *varlist = (WORD_LIST *)NULL;
+/* 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)
 
 /* 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;
@@ -3922,27 +8177,30 @@ separate_out_assignments (tlist)
   if (!tlist)
     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->assignment)
+  while (lp && (lp->word->flags & W_ASSIGNMENT))
     {
       vp = lp;
       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 */
@@ -3955,10 +8213,11 @@ separate_out_assignments (tlist)
     return ((WORD_LIST *)NULL);
 
   /* ASSERT(tlist != NULL); */
-  /* ASSERT(tlist->word->assignment == 0); */
+  /* 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 */
@@ -3970,12 +8229,12 @@ separate_out_assignments (tlist)
       /* Loop postcondition: tlist == word list without assignment statements */
       while (lp)
        {
-         if (lp->word->assignment)
+         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;
@@ -3998,6 +8257,25 @@ separate_out_assignments (tlist)
   return (tlist);
 }
 
+#define WEXP_VARASSIGN 0x001
+#define WEXP_BRACEEXP  0x002
+#define WEXP_TILDEEXP  0x004
+#define WEXP_PARAMEXP  0x008
+#define WEXP_PATHEXP   0x010
+
+/* All of the expansions, including variable assignments at the start of
+   the list. */
+#define WEXP_ALL       (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
+
+/* All of the expansions except variable assignments at the start of
+   the list. */
+#define WEXP_NOVARS    (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
+
+/* All of the `shell expansions': brace expansion, tilde expansion, parameter
+   expansion, command substitution, arithmetic expansion, word splitting, and
+   quote removal. */
+#define WEXP_SHELLEXP  (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
+
 /* Take the list of words in LIST and do the various substitutions.  Return
    a new list of words which is the expanded list, and without things like
    variable assignments. */
@@ -4006,862 +8284,435 @@ WORD_LIST *
 expand_words (list)
      WORD_LIST *list;
 {
-  return (expand_words_internal (list, 1));
-}
-
-/* Same as expand_words (), but doesn't hack variable or environment
-   variables. */
-WORD_LIST *
-expand_words_no_vars (list)
-     WORD_LIST *list;
-{
-  return (expand_words_internal (list, 0));
-}
-
-/* Non-zero means to allow unmatched globbed filenames to expand to
-   a null file. */
-static int allow_null_glob_expansion = 0;
-
-/* The workhorse for expand_words () and expand_words_no_var ().
-   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.
-
-   This does all of the substitutions: brace expansion, tilde expansion,
-   parameter expansion, command substitution, arithmetic expansion,
-   process substitution, word splitting, and pathname expansion.
-   Words with the `quoted' or `assignment' bits set, or for which no
-   expansion is done, do not undergo word splitting.  Words with the
-   `assignment' but set do not undergo pathname expansion. */
-static WORD_LIST *
-expand_words_internal (list, do_vars)
-     WORD_LIST *list;
-     int do_vars;
-{
-  register WORD_LIST *tlist, *new_list = (WORD_LIST *)NULL;
-  WORD_LIST *orig_list;
-
-  if (!list)
-    return ((WORD_LIST *)NULL);
-
-  tlist = copy_word_list (list);
-
-  if (do_vars)
-    {
-      tlist = separate_out_assignments (tlist);
-      if (!tlist)
-       {
-         if (varlist)
-           {
-             /* All the words were variable assignments, so they are placed
-                into the shell's environment. */
-             register WORD_LIST *lp;
-             for (lp = varlist; lp; lp = lp->next)
-               do_assignment (lp->word->word);
-             dispose_words (varlist);
-             varlist = (WORD_LIST *)NULL;
-           }
-         return ((WORD_LIST *)NULL);
-       }
-    }
-
-  /* Begin expanding the words that remain.  The expansions take place on
-     things that aren't really variable assignments. */
-
-#if defined (BRACE_EXPANSION)
-  /* Do brace expansion on this word if there are any brace characters
-     in the string. */
-  if (!no_brace_expansion)
-    {
-      register char **expansions;
-      WORD_LIST *braces = (WORD_LIST *)NULL, *disposables = (WORD_LIST *)NULL;
-      int eindex;
-
-      while (tlist)
-       {
-         WORD_LIST *next;
-
-         next = tlist->next;
-
-         /* 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
-            basically a reversal of TLIST into BRACES, which is corrected
-            by a call to reverse_list () on BRACES when the end of TLIST
-            is reached. */
-         if (strchr (tlist->word->word, '{'))
-           {
-             expansions = brace_expand (tlist->word->word);
-
-             for (eindex = 0; expansions[eindex]; eindex++)
-               {
-                 braces = make_word_list (make_word (expansions[eindex]),
-                                          braces);
-                 free (expansions[eindex]);
-               }
-             free (expansions);
-
-             /* Add TLIST to the list of words to be freed after brace
-                expansion has been performed. */
-             tlist->next = disposables;
-             disposables = tlist;
-           }
-         else
-           {
-             tlist->next = braces;
-             braces = tlist;
-           }
-
-         tlist = next;
-       }
-
-      dispose_words (disposables);
-      tlist = REVERSE_LIST (braces, WORD_LIST *);
-    }
-#endif /* BRACE_EXPANSION */
-
-  orig_list = tlist;
-
-  /* We do tilde expansion all the time.  This is what 1003.2 says. */
-  while (tlist)
-    {
-      register char *current_word;
-      WORD_LIST *expanded, *t, *reversed, *next;
-      int expanded_something = 0;
-
-      current_word = 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. */
-      if (current_word[0] == '~' ||
-         (!posixly_correct && strchr (current_word, '~') &&
-          unquoted_substring ("=~", current_word)))
-       {
-         char *tt;
-
-         tt = tlist->word->word;
-         tlist->word->word = tilde_expand (tt);
-         free (tt);
-       }
-
-      expanded = expand_word_internal
-       (tlist->word, 0, (int *)NULL, &expanded_something);
-
-      if (expanded == &expand_word_error || expanded == &expand_word_fatal)
-       {
-         /* By convention, each time this error is returned,
-            tlist->word->word has already been freed. */
-         tlist->word->word = (char *)NULL;
-         
-         /* Dispose our copy of the original list. */
-         dispose_words (orig_list);
-         /* Dispose the  new list we're building. */
-         dispose_words (new_list);
-
-         if (expanded == &expand_word_error)
-           longjmp (top_level, DISCARD);
-         else
-           longjmp (top_level, FORCE_EOF);
-       }
-
-      /* Don't split assignment words, even when they do not precede a
-        command name. */
-      if (expanded_something && tlist->word->assignment == 0)
-       {
-         t = word_list_split (expanded);
-         dispose_words (expanded);
-       }
-      else
-       {
-         /* If no parameter expansion, command substitution, process
-            substitution, or arithmetic substitution took place, then
-            do not do word splitting.  We still have to remove quoted
-            null characters from the result. */
-         word_list_remove_quoted_nulls (expanded);
-         t = expanded;
-       }
-
-      /* In the most common cases, t will be a list containing only one
-        element, so the call to reverse_list would be wasted. */
-      reversed = REVERSE_LIST (t, WORD_LIST *);
-      new_list = (WORD_LIST *)list_append (reversed, new_list);
-
-      tlist = next;
-    }
-
-  new_list = REVERSE_LIST (new_list, WORD_LIST *);
-
-  dispose_words (orig_list);
-
-#if defined (USE_POSIX_GLOB_LIBRARY)
-#  define GLOB_FAILED(glist)   !(glist)
-#else /* !USE_POSIX_GLOB_LIBRARY */
-#  define GLOB_FAILED(glist)   (glist) == (char **)&glob_error_return
-#endif /* !USE_POSIX_GLOB_LIBRARY */
-
-  /* Okay, we're almost done.  Now let's just do some filename
-     globbing. */
-  if (new_list)
-    {
-      char **temp_list = (char **)NULL;
-      register int list_index;
-      WORD_LIST *glob_list, *disposables;
-
-      orig_list = disposables = (WORD_LIST *)NULL;
-      tlist = new_list;
-
-      /* orig_list == output list, despite the name. */
-      if (!disallow_filename_globbing)
-       {
-         while (tlist)
-           {
-             /* For each word, either globbing is attempted or the word is
-                added to orig_list.  If globbing succeeds, the results are
-                added to orig_list and the word (tlist) is added to the list
-                of disposable words.  If globbing fails and failed glob
-                expansions are left unchanged (the shell default), the
-                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
-                be set right.  After all words are examined, the disposable
-                words are freed. */
-             WORD_LIST *next;
-
-             next = tlist->next;
-
-             /* If the word isn't quoted and there is an unquoted pattern
-                matching character in the word, then glob it. */
-             if (!tlist->word->quoted && !tlist->word->assignment &&
-                 unquoted_glob_pattern_p (tlist->word->word))
-               {
-                 temp_list = shell_glob_filename (tlist->word->word);
-
-                 /* Handle error cases.
-                    I don't think we should report errors like "No such file
-                    or directory".  However, I would like to report errors
-                    like "Read failed". */
-
-                 if (GLOB_FAILED (temp_list))
-                   {
-                     temp_list = (char **) xmalloc (sizeof (char *));
-                     temp_list[0] = (char *)NULL;
-                   }
-
-                 /* Dequote the current word in case we have to use it. */
-                 if (!temp_list[0])
-                   {
-                     register char *t = dequote_string (tlist->word->word);
-                     free (tlist->word->word);
-                     tlist->word->word = t;
-                   }
-
-                 /* Make the array into a word list. */
-                 glob_list = (WORD_LIST *)NULL;
-                 for (list_index = 0; temp_list[list_index]; list_index++)
-                   glob_list = make_word_list
-                     (make_word (temp_list[list_index]), glob_list);
-
-                 if (glob_list)
-                   {
-                     orig_list = (WORD_LIST *)list_append
-                       (glob_list, orig_list);
-                     tlist->next = disposables;
-                     disposables = tlist;
-                   }
-                 else
-                   if (!allow_null_glob_expansion)
-                     {
-                       /* Failed glob expressions are left unchanged. */
-                       tlist->next = orig_list;
-                       orig_list = tlist;
-                     }
-                   else
-                     {
-                       /* Failed glob expressions are removed. */
-                       tlist->next = disposables;
-                       disposables = tlist;
-                     }
-               }
-             else
-               {
-                 /* Dequote the string. */
-                 register char *t = dequote_string (tlist->word->word);
-                 free (tlist->word->word);
-                 tlist->word->word = t;
-                 tlist->next = orig_list;
-                 orig_list = tlist;
-               }
-
-             free_array (temp_list);
-             temp_list = (char **)NULL;
-
-             tlist = next;
-           }
-
-         if (disposables)
-           dispose_words (disposables);
-
-         new_list = REVERSE_LIST (orig_list, WORD_LIST *);
-       }
-      else
-       {
-         /* Dequote the words, because we're not performing globbing. */
-         register WORD_LIST *wl = new_list;
-         register char *wp;
-         while (wl)
-           {
-             wp = dequote_string (wl->word->word);
-             free (wl->word->word);
-             wl->word->word = wp;
-             wl = wl->next;
-           }
-       }
-    }
-
-  if (do_vars)
-    {
-      register WORD_LIST *lp;
-      Function *assign_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 (lp = varlist; lp; lp = lp->next)
-       (*assign_func) (lp->word->word);
-
-      dispose_words (varlist);
-      varlist = (WORD_LIST *)NULL;
-    }
-
-  return (new_list);
-}
-
-/* Return nonzero if S has any unquoted special globbing chars in it.  */
-static int
-unquoted_glob_pattern_p (string)
-     register char *string;
-{
-  register int c;
-  int open = 0;
-
-  while (c = *string++)
-    {
-      switch (c)
-       {
-       case '?':
-       case '*':
-         return (1);
-
-       case '[':
-         open++;
-         continue;
-
-       case ']':
-         if (open)
-           return (1);
-         continue;
-
-       case CTLESC:
-       case '\\':
-         if (*string++ == '\0')
-           return (0);
-       }
-    }
-  return (0);
-}
-
-/* PATHNAME can contain characters prefixed by CTLESC; this indicates
-   that the character is to be quoted.  We quote it here in the style
-   that the glob library recognizes.  If CONVERT_QUOTED_NULLS is non-zero,
-   we change quoted null strings (pathname[0] == CTLNUL) into empty
-   strings (pathname[0] == 0).  If this is called after quote removal
-   is performed, CONVERT_QUOTED_NULLS should be 0; if called when quote
-   removal has not been done (for example, before attempting to match a
-   pattern while executing a case statement), CONVERT_QUOTED_NULLS should
-   be 1. */
-char *
-quote_string_for_globbing (pathname, convert_quoted_nulls)
-     char *pathname;
-     int convert_quoted_nulls;
-{
-  char *temp;
-  register int i;
-
-  temp = savestring (pathname);
-
-  if (convert_quoted_nulls && QUOTED_NULL (pathname))
-    {
-      temp[0] = '\0';
-      return temp;
-    }
-
-  for (i = 0; temp[i]; i++)
-    {
-      if (temp[i] == CTLESC)
-       temp[i++] = '\\';
-    }
-
-  return (temp);
+  return (expand_word_list_internal (list, WEXP_ALL));
 }
 
-/* Call the glob library to do globbing on PATHNAME. */
-char **
-shell_glob_filename (pathname)
-     char *pathname;
+/* Same as expand_words (), but doesn't hack variable or environment
+   variables. */
+WORD_LIST *
+expand_words_no_vars (list)
+     WORD_LIST *list;
 {
-#if defined (USE_POSIX_GLOB_LIBRARY)
-  extern int glob_dot_filenames;
-  register int i;
-  char *temp, **return_value;
-  glob_t filenames;
-  int glob_flags;
-
-  temp = quote_string_for_globbing (pathname, 0);
+  return (expand_word_list_internal (list, WEXP_NOVARS));
+}
 
-  filenames.gl_offs = 0;
+WORD_LIST *
+expand_words_shellexp (list)
+     WORD_LIST *list;
+{
+  return (expand_word_list_internal (list, WEXP_SHELLEXP));
+}
 
-  glob_flags = glob_dot_filenames ? GLOB_PERIOD : 0;
-  glob_flags |= (GLOB_ERR | GLOB_DOOFFS);
+static WORD_LIST *
+glob_expand_word_list (tlist, eflags)
+     WORD_LIST *tlist;
+     int eflags;
+{
+  char **glob_array, *temp_string;
+  register int glob_index;
+  WORD_LIST *glob_list, *output_list, *disposables, *next;
+  WORD_DESC *tword;
 
-  i = glob (temp, glob_flags, (Function *)NULL, &filenames);
+  output_list = disposables = (WORD_LIST *)NULL;
+  glob_array = (char **)NULL;
+  while (tlist)
+    {
+      /* For each word, either globbing is attempted or the word is
+        added to orig_list.  If globbing succeeds, the results are
+        added to orig_list and the word (tlist) is added to the list
+        of disposable words.  If globbing fails and failed glob
+        expansions are left unchanged (the shell default), the
+        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
+        be set right.  After all words are examined, the disposable
+        words are freed. */
+      next = tlist->next;
 
-  free (temp);
+      /* If the word isn't an assignment and contains an unquoted
+        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);
 
-  if (i == GLOB_NOSPACE || i == GLOB_ABEND)
-    return ((char **)NULL);
+         /* Handle error cases.
+            I don't think we should report errors like "No such file
+            or directory".  However, I would like to report errors
+            like "Read failed". */
 
-  if (i == GLOB_NOMATCH)
-    filenames.gl_pathv[0] = (char *)NULL;
+         if (glob_array == 0 || GLOB_FAILED (glob_array))
+           {
+             glob_array = (char **)xmalloc (sizeof (char *));
+             glob_array[0] = (char *)NULL;
+           }
 
-  return (filenames.gl_pathv);
+         /* Dequote the current word in case we have to use it. */
+         if (glob_array[0] == NULL)
+           {
+             temp_string = dequote_string (tlist->word->word);
+             free (tlist->word->word);
+             tlist->word->word = temp_string;
+           }
 
-#else /* !USE_POSIX_GLOB_LIBRARY */
+         /* Make the array into a word list. */
+         glob_list = (WORD_LIST *)NULL;
+         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);
+           }
 
-  char *temp, **results;
+         if (glob_list)
+           {
+             output_list = (WORD_LIST *)list_append (glob_list, output_list);
+             PREPEND_LIST (tlist, disposables);
+           }
+         else if (fail_glob_expansion != 0)
+           {
+             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. */
+             PREPEND_LIST (tlist, output_list);
+           }
+         else
+           {
+             /* Failed glob expressions are removed. */
+             PREPEND_LIST (tlist, disposables);
+           }
+       }
+      else
+       {
+         /* Dequote the string. */
+         temp_string = dequote_string (tlist->word->word);
+         free (tlist->word->word);
+         tlist->word->word = temp_string;
+         PREPEND_LIST (tlist, output_list);
+       }
 
-  noglob_dot_filenames = !glob_dot_filenames;
+      strvec_dispose (glob_array);
+      glob_array = (char **)NULL;
 
-  temp = quote_string_for_globbing (pathname, 0);
+      tlist = next;
+    }
 
-  results = glob_filename (temp);
-  free (temp);
+  if (disposables)
+    dispose_words (disposables);
 
-  if (results && !(GLOB_FAILED(results)))
-    sort_char_array (results);
+  if (output_list)
+    output_list = REVERSE_LIST (output_list, WORD_LIST *);
 
-  return (results);
-#endif /* !USE_POSIX_GLOB_LIBRARY */
+  return (output_list);
 }
 
-/*************************************************
- *                                              *
- *     Functions to manage special variables    *
- *                                              *
- *************************************************/
-
-/* An alist of name.function for each special variable.  Most of the
-   functions don't do much, and in fact, this would be faster with a
-   switch statement, but by the end of this file, I am sick of switch
-   statements. */
+#if defined (BRACE_EXPANSION)
+static WORD_LIST *
+brace_expand_word_list (tlist, eflags)
+     WORD_LIST *tlist;
+     int eflags;
+{
+  register char **expansions;
+  char *temp_string;
+  WORD_LIST *disposables, *output_list, *next;
+  WORD_DESC *w;
+  int eindex;
 
-/* The functions that get called. */
-void
-  sv_path (), sv_mail (), sv_uids (), sv_ignoreeof (),
-  sv_glob_dot_filenames (), sv_nolinks (),
-  sv_noclobber (), sv_allow_null_glob_expansion (), sv_strict_posix ();
+  for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
+    {
+      next = tlist->next;
 
-#if defined (READLINE)
-void sv_terminal (), sv_hostname_completion_file ();
-#endif
+      /* 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 `xstrchr', 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
+        is reached. */
+      if (xstrchr (tlist->word->word, LBRACE))
+       {
+         expansions = brace_expand (tlist->word->word);
 
-#if defined (HISTORY)
-void sv_histsize (), sv_histfilesize (),
-     sv_history_control (), sv_command_oriented_history ();
-#  if defined (BANG_HISTORY)
-void sv_histchars ();
-#  endif
-#endif /* HISTORY */
+         for (eindex = 0; temp_string = expansions[eindex]; eindex++)
+           {
+             w = make_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;
+             output_list = make_word_list (w, output_list);
+             free (expansions[eindex]);
+           }
+         free (expansions);
 
-#if defined (GETOPTS_BUILTIN)
-void sv_optind (), sv_opterr ();
-#endif /* GETOPTS_BUILTIN */
+         /* Add TLIST to the list of words to be freed after brace
+            expansion has been performed. */
+         PREPEND_LIST (tlist, disposables);
+       }
+      else
+       PREPEND_LIST (tlist, output_list);
+    }
 
-#if defined (JOB_CONTROL)
-void sv_notify ();
-#endif
+  if (disposables)
+    dispose_words (disposables);
 
-#define SET_INT_VAR(name, intvar)  intvar = find_variable (name) != 0
+  if (output_list)
+    output_list = REVERSE_LIST (output_list, WORD_LIST *);
 
-struct name_and_function {
-  char *name;
-  VFunction *function;
-} special_vars[] = {
-  { "PATH", sv_path },
-  { "MAIL", sv_mail },
-  { "MAILPATH", sv_mail },
-  { "MAILCHECK", sv_mail },
+  return (output_list);
+}
+#endif
 
-  { "POSIXLY_CORRECT", sv_strict_posix },
-  { "POSIX_PEDANTIC", sv_strict_posix },
-  /* Variables which only do something special when READLINE is defined. */
-#if defined (READLINE)
-  { "TERM", sv_terminal },
-  { "TERMCAP", sv_terminal },
-  { "TERMINFO", sv_terminal },
-  { "hostname_completion_file", sv_hostname_completion_file },
-  { "HOSTFILE", sv_hostname_completion_file },
-#endif /* READLINE */
+#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)
+     char *word;
+     char *option;
+{
+  int t;
+  WORD_LIST *wl;
+  WORD_DESC *w;
 
-  /* Variables which only do something special when HISTORY is defined. */
-#if defined (HISTORY)
-  { "HISTSIZE", sv_histsize },
-  { "HISTFILESIZE", sv_histfilesize },
-  { "command_oriented_history", sv_command_oriented_history },
-#  if defined (BANG_HISTORY)
-  { "histchars", sv_histchars },
-#  endif
-  { "history_control", sv_history_control },
-  { "HISTCONTROL", sv_history_control },
-#endif /* HISTORY */
+  w = make_word (word);
 
-  { "EUID", sv_uids},
-  { "UID", sv_uids},
-  { "IGNOREEOF", sv_ignoreeof },
-  { "ignoreeof", sv_ignoreeof },
+  t = assignment (w->word, 0);
+  w->word[t] = '\0';
 
-#if defined (GETOPTS_BUILTIN)
-  { "OPTIND", sv_optind },
-  { "OPTERR", sv_opterr },
-#endif /* GETOPTS_BUILTIN */
+  wl = make_word_list (w, (WORD_LIST *)NULL);
+  wl = make_word_list (make_word (option), wl);
 
-#if defined (JOB_CONTROL)
-  { "notify", sv_notify },
-#endif  /* JOB_CONTROL */
-
-  { "glob_dot_filenames", sv_glob_dot_filenames },
-  { "allow_null_glob_expansion", sv_allow_null_glob_expansion },
-  { "noclobber", sv_noclobber },
-  { "nolinks", sv_nolinks },
-  { (char *)0x00, (VFunction *)0x00 }
-};
+  return (declare_builtin (wl));  
+}  
+#endif
 
-/* The variable in NAME has just had its state changed.  Check to see if it
-   is one of the special ones where something special happens. */
-void
-stupidly_hack_special_variables (name)
-     char *name;
+static WORD_LIST *
+shell_expand_word_list (tlist, eflags)
+     WORD_LIST *tlist;
+     int eflags;
 {
-  int i = 0;
+  WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list;
+  int expanded_something, has_dollar_at;
+  char *temp_string;
 
-  while (special_vars[i].name)
+  /* We do tilde expansion all the time.  This is what 1003.2 says. */
+  new_list = (WORD_LIST *)NULL;
+  for (orig_list = tlist; tlist; tlist = next)
     {
-      if (STREQ (special_vars[i].name, name))
+      temp_string = tlist->word->word;
+
+      next = tlist->next;
+
+#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))
        {
-         (*(special_vars[i].function)) (name);
-         return;
-       }
-      i++;
-    }
-}
+         int t;
 
-/* Set/unset noclobber. */
-void
-sv_noclobber (name)
-     char *name;
-{
-  SET_INT_VAR (name, noclobber);
-}
+         if (tlist->word->flags & W_ASSIGNASSOC)
+           make_internal_declare (tlist->word->word, "-A");
 
-/* What to do just after the PATH variable has changed. */
-void
-sv_path (name)
-     char *name;
-{
-  /* hash -r */
-  WORD_LIST *args;
+         t = do_word_assignment (tlist->word);
+         if (t == 0)
+           {
+             last_command_exit_value = EXECUTION_FAILURE;
+             exp_jump_to_top_level (DISCARD);
+           }
 
-  args = make_word_list (make_word ("-r"), NULL);
-  hash_builtin (args);
-  dispose_words (args);
-}
+         /* Now transform the word as ksh93 appears to do and go on */
+         t = assignment (tlist->word->word, 0);
+         tlist->word->word[t] = '\0';
+         tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC);
+       }
+#endif
 
-/* What to do just after one of the MAILxxxx variables has changed.  NAME
-   is the name of the variable.  This is called with NAME set to one of
-   MAIL, MAILCHECK, or MAILPATH.  */
-void
-sv_mail (name)
-     char *name;
-{
-  /* If the time interval for checking the files has changed, then
-     reset the mail timer.  Otherwise, one of the pathname vars
-     to the users mailbox has changed, so rebuild the array of
-     filenames. */
-  if (name[4] == 'C')  /* if (strcmp (name, "MAILCHECK") == 0) */
-    reset_mail_timer ();
-  else
-    {
-      free_mail_files ();
-      remember_mail_dates ();
-    }
-}
+      expanded_something = 0;
+      expanded = expand_word_internal
+       (tlist->word, 0, 0, &has_dollar_at, &expanded_something);
 
-#if defined (READLINE)
-/* What to do just after one of the TERMxxx variables has changed.
-   If we are an interactive shell, then try to reset the terminal
-   information in readline. */
-void
-sv_terminal (name)
-     char *name;
-{
-  if (interactive_shell && !no_line_editing)
-    rl_reset_terminal (get_string_value ("TERM"));
-}
+      if (expanded == &expand_word_error || expanded == &expand_word_fatal)
+       {
+         /* By convention, each time this error is returned,
+            tlist->word->word has already been freed. */
+         tlist->word->word = (char *)NULL;
 
-void
-sv_hostname_completion_file (name)
-     char *name;
-{
-  hostname_list_initialized = 0;
-}
-#endif /* READLINE */
+         /* Dispose our copy of the original list. */
+         dispose_words (orig_list);
+         /* Dispose the new list we're building. */
+         dispose_words (new_list);
 
-#if defined (HISTORY)
-/* What to do after the HISTSIZE variable changes.
-   If there is a value for this variable (and it is numeric), then stifle
-   the history.  Otherwise, if there is NO value for this variable,
-   unstifle the history. */
-void
-sv_histsize (name)
-     char *name;
-{
-  char *temp = get_string_value (name);
+         last_command_exit_value = EXECUTION_FAILURE;
+         if (expanded == &expand_word_error)
+           exp_jump_to_top_level (DISCARD);
+         else
+           exp_jump_to_top_level (FORCE_EOF);
+       }
 
-  if (temp && *temp)
-    {
-      int num;
-      if (sscanf (temp, "%d", &num) == 1)
+      /* Don't split words marked W_NOSPLIT. */
+      if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
        {
-         stifle_history (num);
-         if (history_lines_this_session > where_history ())
-           history_lines_this_session = where_history ();
+         temp_list = word_list_split (expanded);
+         dispose_words (expanded);
        }
-    }
-  else
-    unstifle_history ();
-}
-
-/* What to do if the HISTFILESIZE variable changes. */
-void
-sv_histfilesize (name)
-     char *name;
-{
-  char *temp = get_string_value (name);
-
-  if (temp && *temp)
-    {
-      int num;
-      if (sscanf (temp, "%d", &num) == 1)
+      else
        {
-         history_truncate_file (get_string_value ("HISTFILE"), num);
-         if (num <= history_lines_in_file)
-           history_lines_in_file = num;
+         /* If no parameter expansion, command substitution, process
+            substitution, or arithmetic substitution took place, then
+            do not do word splitting.  We still have to remove quoted
+            null characters from the result. */
+         word_list_remove_quoted_nulls (expanded);
+         temp_list = expanded;
        }
-    }
-}
 
-/* What to do after the HISTORY_CONTROL variable changes. */
-void
-sv_history_control (name)
-     char *name;
-{
-  char *temp = get_string_value (name);
+      expanded = REVERSE_LIST (temp_list, WORD_LIST *);
+      new_list = (WORD_LIST *)list_append (expanded, new_list);
+    }
 
-  history_control = 0;
+  if (orig_list)  
+    dispose_words (orig_list);
 
-  if (temp && *temp)
-    {
-      if (strcmp (temp, "ignorespace") == 0)
-       history_control = 1;
-      else if (strcmp (temp, "ignoredups") == 0)
-       history_control = 2;
-      else if (strcmp (temp, "ignoreboth") == 0)
-       history_control = 3;
-    }
-}
+  if (new_list)
+    new_list = REVERSE_LIST (new_list, WORD_LIST *);
 
-/* What to do after the COMMAND_ORIENTED_HISTORY variable changes. */
-void
-sv_command_oriented_history (name)
-     char *name;
-{
-  SET_INT_VAR (name, command_oriented_history);
+  return (new_list);
 }
 
-#  if defined (BANG_HISTORY)
-/* Setting/unsetting of the history expansion character. */
+/* The workhorse for expand_words () and expand_words_no_vars ().
+   First arg is LIST, a WORD_LIST of words.
+   Second arg EFLAGS is a flags word controlling which expansions are
+   performed.
 
-void
-sv_histchars (name)
-     char *name;
+   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_NOGLOB bit set do not undergo pathname expansion. */
+static WORD_LIST *
+expand_word_list_internal (list, eflags)
+     WORD_LIST *list;
+     int eflags;
 {
-  char *temp = get_string_value (name);
+  WORD_LIST *new_list, *temp_list;
+  int tint;
 
-  if (temp)
+  if (list == 0)
+    return ((WORD_LIST *)NULL);
+
+  garglist = new_list = copy_word_list (list);
+  if (eflags & WEXP_VARASSIGN)
     {
-      history_expansion_char = *temp;
-      if (temp[0] && temp[1])
+      garglist = new_list = separate_out_assignments (new_list);
+      if (new_list == 0)
        {
-         history_subst_char = temp[1];
-         if (temp[2])
-             history_comment_char = temp[2];
+         if (subst_assign_varlist)
+           {
+             /* All the words were variable assignments, so they are placed
+                into the shell's environment. */
+             for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
+               {
+                 this_command_name = (char *)NULL;     /* no arithmetic errors */
+                 tint = do_word_assignment (temp_list->word);
+                 /* Variable assignment errors in non-interactive shells
+                    running in Posix.2 mode cause the shell to exit. */
+                 if (tint == 0)
+                   {
+                     last_command_exit_value = EXECUTION_FAILURE;
+                     if (interactive_shell == 0 && posixly_correct)
+                       exp_jump_to_top_level (FORCE_EOF);
+                     else
+                       exp_jump_to_top_level (DISCARD);
+                   }
+               }
+             dispose_words (subst_assign_varlist);
+             subst_assign_varlist = (WORD_LIST *)NULL;
+           }
+         return ((WORD_LIST *)NULL);
        }
     }
-  else
-    {
-      history_expansion_char = '!';
-      history_subst_char = '^';
-      history_comment_char = '#';
-    }
-}
-#  endif /* BANG_HISTORY */
-#endif /* HISTORY */
 
-void
-sv_allow_null_glob_expansion (name)
-     char *name;
-{
-  SET_INT_VAR (name, allow_null_glob_expansion);
-}
+  /* Begin expanding the words that remain.  The expansions take place on
+     things that aren't really variable assignments. */
 
-/* If the variable exists, then the value of it can be the number
-   of times we actually ignore the EOF.  The default is small,
-   (smaller than csh, anyway). */
-void
-sv_ignoreeof (name)
-     char *name;
-{
-  SHELL_VAR *tmp_var;
-  char *temp;
-  int new_limit;
+#if defined (BRACE_EXPANSION)
+  /* Do brace expansion on this word if there are any brace characters
+     in the string. */
+  if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
+    new_list = brace_expand_word_list (new_list, eflags);
+#endif /* BRACE_EXPANSION */
 
-  eof_encountered = 0;
+  /* Perform the `normal' shell expansions: tilde expansion, parameter and
+     variable substitution, command substitution, arithmetic expansion,
+     and word splitting. */
+  new_list = shell_expand_word_list (new_list, eflags);
 
-  tmp_var = find_variable (name);
-  ignoreeof = tmp_var != 0;
-  temp = tmp_var ? value_cell (tmp_var) : (char *)NULL;
-  if (temp)
+  /* Okay, we're almost done.  Now let's just do some filename
+     globbing. */
+  if (new_list)
     {
-      if (sscanf (temp, "%d", &new_limit) == 1)
-       eof_encountered_limit = new_limit;
+      if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
+       /* Glob expand the word list unless globbing has been disabled. */
+       new_list = glob_expand_word_list (new_list, eflags);
       else
-       eof_encountered_limit = 10; /* csh uses 26. */
+       /* Dequote the words, because we're not performing globbing. */
+       new_list = dequote_list (new_list);
     }
-}
-
-/* Control whether * matches .files in globbing.  Yechh. */
-int glob_dot_filenames = 0;
-
-void
-sv_glob_dot_filenames (name)
-     char *name;
-{
-  SET_INT_VAR (name, glob_dot_filenames);
-}
-
-#if defined (JOB_CONTROL)
-/* Job notification feature desired? */
-void
-sv_notify (name)
-     char *name;
-{
-  SET_INT_VAR (name, asynchronous_notification);
-}
-#endif  /* JOB_CONTROL */
-
-/* If the variable `nolinks' exists, it specifies that symbolic links are
-   not to be followed in `cd' commands. */
-void
-sv_nolinks (name)
-     char *name;
-{
-  SET_INT_VAR (name, no_symbolic_links);
-}
-
-/* Don't let users hack the user id variables. */
-void
-sv_uids (name)
-     char *name;
-{
-  char *buff;
-  register SHELL_VAR *v;
-
-  buff = itos (current_user.uid);
-  v = find_variable ("UID");
-  if (v)
-    v->attributes &= ~att_readonly;
-
-  v = bind_variable ("UID", buff);
-  v->attributes |= (att_readonly | att_integer);
-  free (buff);
 
-  buff = itos (current_user.euid);
-  v = find_variable ("EUID");
-  if (v)
-    v->attributes &= ~att_readonly;
-
-  v = bind_variable ("EUID", buff);
-  v->attributes |= (att_readonly | att_integer);
-  free (buff);
-}
+  if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
+    {
+      sh_wassign_func_t *assign_func;
 
-#if defined (GETOPTS_BUILTIN)
-void
-sv_optind (name)
-     char *name;
-{
-  char *tt = get_string_value ("OPTIND");
-  int s = 0;
+      /* 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_word_assignment;
+      tempenv_assign_error = 0;
 
-  if (tt && *tt)
-    {
-      s = atoi (tt);
+      for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
+       {
+         this_command_name = (char *)NULL;
+         assigning_in_environment = (assign_func == assign_in_env);
+         tint = (*assign_func) (temp_list->word);
+         assigning_in_environment = 0;
+         /* Variable assignment errors in non-interactive shells running
+            in Posix.2 mode cause the shell to exit. */
+         if (tint == 0)
+           {
+             if (assign_func == do_word_assignment)
+               {
+                 last_command_exit_value = EXECUTION_FAILURE;
+                 if (interactive_shell == 0 && posixly_correct)
+                   exp_jump_to_top_level (FORCE_EOF);
+                 else
+                   exp_jump_to_top_level (DISCARD);
+               }
+             else
+               tempenv_assign_error++;
+           }
+       }
 
-      /* According to POSIX, setting OPTIND=1 resets the internal state
-        of getopt (). */
-      if (s < 0 || s == 1)
-       s = 0;
+      dispose_words (subst_assign_varlist);
+      subst_assign_varlist = (WORD_LIST *)NULL;
     }
-  getopts_reset (s);
-}
-
-void
-sv_opterr (name)
-     char *name;
-{
-  char *tt = get_string_value ("OPTERR");
-  int s = 1;
 
-  if (tt && *tt)
-    s = atoi (tt);
-  sh_opterr = s;
-}
-#endif /* GETOPTS_BUILTIN */
+#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
 
-void
-sv_strict_posix (name)
-     char *name;
-{
-  SET_INT_VAR (name, posixly_correct);
-  if (posixly_correct)
-    interactive_comments = 1;
-#if defined (READLINE)
-  posix_readline_initialize (posixly_correct);
-#endif /* READLINE */
+  return (new_list);
 }