1 /* subst.c -- The part of the shell that does parameter, command, and
2 globbing substitutions. */
4 /* ``Have a little faith, there's magic in the night. You ain't a
5 beauty, but, hey, you're alright.'' */
7 /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
9 This file is part of GNU Bash, the Bourne Again SHell.
11 Bash is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
16 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License along
22 with Bash; see the file COPYING. If not, write to the Free Software
23 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
27 #include "bashtypes.h"
29 #include "chartypes.h"
34 #if defined (HAVE_UNISTD_H)
39 #include "posixstat.h"
44 #include "execute_cmd.h"
48 #include "mailcheck.h"
52 #include "builtins/getopt.h"
53 #include "builtins/common.h"
55 #include <tilde/tilde.h>
56 #include <glob/strmatch.h>
62 /* The size that strings change by. */
63 #define DEFAULT_INITIAL_ARRAY_SIZE 112
64 #define DEFAULT_ARRAY_SIZE 128
70 #define VT_ARRAYMEMBER 3
72 /* Flags for quoted_strchr */
73 #define ST_BACKSL 0x01
74 #define ST_CTLESC 0x02
75 #define ST_SQUOTE 0x04 /* unused yet */
76 #define ST_DQUOTE 0x08 /* unused yet */
78 /* Flags for the string extraction functions. */
79 #define EX_NOALLOC 0x01 /* just skip; don't return substring */
80 #define EX_VARNAME 0x02 /* variable name; for string_extract () */
82 /* These defs make it easier to use the editor. */
88 /* Evaluates to 1 if C is one of the shell's special parameters whose length
89 can be taken, but is also one of the special expansion characters. */
90 #define VALID_SPECIAL_LENGTH_PARAM(c) \
91 ((c) == '-' || (c) == '?' || (c) == '#')
93 /* Evaluates to 1 if C is one of the shell's special parameters for which an
94 indirect variable reference may be made. */
95 #define VALID_INDIR_PARAM(c) \
96 ((c) == '#' || (c) == '?' || (c) == '@' || (c) == '*')
98 /* Evaluates to 1 if C is one of the OP characters that follows the parameter
99 in ${parameter[:]OPword}. */
100 #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
102 /* Evaluates to 1 if this is one of the shell's special variables. */
103 #define SPECIAL_VAR(name, wi) \
104 ((DIGIT (*name) && all_digits (name)) || \
105 (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
106 (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))
108 /* An expansion function that takes a string and a quoted flag and returns
109 a WORD_LIST *. Used as the type of the third argument to
110 expand_string_if_necessary(). */
111 typedef WORD_LIST *EXPFUNC __P((char *, int));
113 /* Process ID of the last command executed within command substitution. */
114 pid_t last_command_subst_pid = NO_PID;
115 pid_t current_command_subst_pid = NO_PID;
117 /* Variables used to keep track of the characters in IFS. */
120 unsigned char ifs_cmap[UCHAR_MAX + 1];
121 unsigned char ifs_firstc;
123 /* Extern functions and variables from different files. */
124 extern int last_command_exit_value;
125 extern int subshell_environment;
126 extern int eof_encountered;
127 extern int return_catch_flag, return_catch_value;
128 extern pid_t dollar_dollar_pid;
129 extern int posixly_correct;
130 extern char *this_command_name;
131 extern struct fd_bitmap *current_fds_to_close;
132 extern int wordexp_only;
134 /* Non-zero means to allow unmatched globbed filenames to expand to
136 int allow_null_glob_expansion;
139 /* Variables to keep track of which words in an expanded word list (the
140 output of expand_word_list_internal) are the result of globbing
141 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
142 (CURRENTLY UNUSED). */
143 char *glob_argv_flags;
144 static int glob_argv_flags_size;
147 static WORD_LIST expand_word_error, expand_word_fatal;
148 static char expand_param_error, expand_param_fatal;
150 /* Tell the expansion functions to not longjmp back to top_level on fatal
151 errors. Enabled when doing completion and prompt string expansion. */
152 static int no_longjmp_on_fatal_error = 0;
154 /* Set by expand_word_unsplit; used to inhibit splitting and re-joining
155 $* on $IFS, primarily when doing assignment statements. */
156 static int expand_no_split_dollar_star = 0;
158 /* Used to hold a list of variable assignments preceding a command. Global
159 so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
161 WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
163 /* A WORD_LIST of words to be expanded by expand_word_list_internal,
164 without any leading variable assignments. */
165 static WORD_LIST *garglist = (WORD_LIST *)NULL;
167 static char *quoted_substring __P((char *, int, int));
168 static int quoted_strlen __P((char *));
169 static char *quoted_strchr __P((char *, int, int));
171 static char *expand_string_if_necessary __P((char *, int, EXPFUNC *));
172 static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *));
173 static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
174 static WORD_LIST *expand_string_internal __P((char *, int));
175 static WORD_LIST *expand_string_leave_quoted __P((char *, int));
176 static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *));
178 static WORD_LIST *list_quote_escapes __P((WORD_LIST *));
179 static char *dequote_escapes __P((char *));
180 static char *make_quoted_char __P((int));
181 static WORD_LIST *quote_list __P((WORD_LIST *));
182 static WORD_LIST *dequote_list __P((WORD_LIST *));
183 static char *remove_quoted_escapes __P((char *));
184 static char *remove_quoted_nulls __P((char *));
186 static int unquoted_substring __P((char *, char *));
187 static int unquoted_member __P((int, char *));
189 static int do_assignment_internal __P((const char *, int));
191 static char *string_extract_verbatim __P((char *, int *, char *));
192 static char *string_extract __P((char *, int *, char *, int));
193 static char *string_extract_double_quoted __P((char *, int *, int));
194 static inline char *string_extract_single_quoted __P((char *, int *));
195 static inline int skip_single_quoted __P((char *, size_t, int));
196 static int skip_double_quoted __P((char *, size_t, int));
197 static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int));
198 static char *extract_dollar_brace_string __P((char *, int *, int, int));
200 static char *pos_params __P((char *, int, int, int));
202 static char *remove_pattern __P((char *, char *, int));
203 static int match_pattern_char __P((char *, char *));
204 static int match_pattern __P((char *, char *, int, char **, char **));
205 static int getpatspec __P((int, char *));
206 static char *getpattern __P((char *, int, int));
207 static char *variable_remove_pattern __P((char *, char *, int, int));
208 static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int));
209 static char *parameter_list_remove_pattern __P((int, char *, int, int));
211 static char *array_remove_pattern __P((ARRAY *, char *, int, char *, int));
213 static char *parameter_brace_remove_pattern __P((char *, char *, char *, int, int));
215 static char *process_substitute __P((char *, int));
217 static char *read_comsub __P((int, int));
220 static arrayind_t array_length_reference __P((char *));
223 static int valid_brace_expansion_word __P((char *, int));
224 static char *parameter_brace_expand_word __P((char *, int, int));
225 static char *parameter_brace_expand_indir __P((char *, int, int));
226 static char *parameter_brace_expand_rhs __P((char *, char *, int, int, int *, int *));
227 static void parameter_brace_expand_error __P((char *, char *));
229 static int valid_length_expression __P((char *));
230 static intmax_t parameter_brace_expand_length __P((char *));
232 static char *skiparith __P((char *, int));
233 static int verify_substring_values __P((char *, char *, int, intmax_t *, intmax_t *));
234 static int get_var_and_type __P((char *, char *, SHELL_VAR **, char **));
235 static char *parameter_brace_substring __P((char *, char *, char *, int));
237 static char *pos_params_pat_subst __P((char *, char *, char *, int));
239 static char *parameter_brace_patsub __P((char *, char *, char *, int));
241 static char *parameter_brace_expand __P((char *, int *, int, int *, int *));
242 static char *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
244 static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
246 static WORD_LIST *word_list_split __P((WORD_LIST *));
248 static WORD_LIST *separate_out_assignments __P((WORD_LIST *));
249 static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int));
250 #ifdef BRACE_EXPANSION
251 static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int));
253 static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
254 static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
256 /* **************************************************************** */
258 /* Utility Functions */
260 /* **************************************************************** */
262 #ifdef INCLUDE_UNUSED
264 quoted_substring (string, start, end)
269 register char *result, *s, *r;
273 /* Move to string[start], skipping quoted characters. */
274 for (s = string, l = 0; *s && l < start; )
286 r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */
288 /* Copy LEN characters, including quote characters. */
290 for (l = 0; l < len; s++)
304 #ifdef INCLUDE_UNUSED
305 /* Return the length of S, skipping over quoted characters */
329 /* Find the first occurrence of character C in string S, obeying shell
330 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
331 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
332 escaped with CTLESC are skipped. */
334 quoted_strchr (s, c, flags)
342 if (((flags & ST_BACKSL) && *p == '\\')
343 || ((flags & ST_CTLESC) && *p == CTLESC))
347 return ((char *)NULL);
353 return ((char *)NULL);
356 /* Return 1 if CHARACTER appears in an unquoted portion of
357 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
359 unquoted_member (character, string)
367 slen = strlen (string);
369 while (c = string[sindex])
377 ADVANCE_CHAR (string, slen, sindex);
383 ADVANCE_CHAR (string, slen, sindex);
387 sindex = skip_single_quoted (string, slen, ++sindex);
391 sindex = skip_double_quoted (string, slen, ++sindex);
398 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
400 unquoted_substring (substr, string)
401 char *substr, *string;
404 int sindex, c, sublen;
407 if (substr == 0 || *substr == '\0')
410 slen = strlen (string);
411 sublen = strlen (substr);
412 for (sindex = 0; c = string[sindex]; )
414 if (STREQN (string + sindex, substr, sublen))
423 ADVANCE_CHAR (string, slen, sindex);
427 sindex = skip_single_quoted (string, slen, ++sindex);
431 sindex = skip_double_quoted (string, slen, ++sindex);
435 ADVANCE_CHAR (string, slen, sindex);
442 /* Most of the substitutions must be done in parallel. In order
443 to avoid using tons of unclear goto's, I have some functions
444 for manipulating malloc'ed strings. They all take INDX, a
445 pointer to an integer which is the offset into the string
446 where manipulation is taking place. They also take SIZE, a
447 pointer to an integer which is the current length of the
448 character array for this string. */
450 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
451 of space allocated to TARGET. SOURCE can be NULL, in which
452 case nothing happens. Gets rid of SOURCE by freeing it.
453 Returns TARGET in case the location has changed. */
455 sub_append_string (source, target, indx, size)
456 char *source, *target;
463 srclen = STRLEN (source);
464 if (srclen >= (int)(*size - *indx))
467 n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
468 target = (char *)xrealloc (target, (*size = n));
471 FASTCOPY (source, target + *indx, srclen);
473 target[*indx] = '\0';
482 /* Append the textual representation of NUMBER to TARGET.
483 INDX and SIZE are as in SUB_APPEND_STRING. */
485 sub_append_number (number, target, indx, size)
492 temp = itos (number);
493 return (sub_append_string (temp, target, indx, size));
497 /* Extract a substring from STRING, starting at SINDEX and ending with
498 one of the characters in CHARLIST. Don't make the ending character
499 part of the string. Leave SINDEX pointing at the ending character.
500 Understand about backslashes in the string. If (flags & EX_VARNAME)
501 is non-zero, and array variables have been compiled into the shell,
502 everything between a `[' and a corresponding `]' is skipped over.
503 If (flags & EX_NOALLOC) is non-zero, don't return the substring, just
506 string_extract (string, sindex, charlist, flags)
517 slen = strlen (string + *sindex) + *sindex;
519 while (c = string[i])
528 #if defined (ARRAY_VARS)
529 else if ((flags & EX_VARNAME) && c == '[')
532 /* If this is an array subscript, skip over it and continue. */
533 ni = skipsubscript (string, i);
534 if (string[ni] == ']')
538 else if (MEMBER (c, charlist))
541 ADVANCE_CHAR (string, slen, i);
544 temp = (flags & EX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
549 /* Extract the contents of STRING as if it is enclosed in double quotes.
550 SINDEX, when passed in, is the offset of the character immediately
551 following the opening double quote; on exit, SINDEX is left pointing after
552 the closing double quote. If STRIPDQ is non-zero, unquoted double
553 quotes are stripped and the string is terminated by a null byte.
554 Backslashes between the embedded double quotes are processed. If STRIPDQ
555 is zero, an unquoted `"' terminates the string. */
557 string_extract_double_quoted (string, sindex, stripdq)
559 int *sindex, stripdq;
565 char *temp, *ret; /* The new string we return. */
566 int pass_next, backquote, si; /* State variables for the machine. */
570 slen = strlen (string + *sindex) + *sindex;
571 send = string + slen;
573 pass_next = backquote = dquote = 0;
574 temp = (char *)xmalloc (1 + slen - *sindex);
578 while (c = string[i])
580 /* Process a character that was quoted by a backslash. */
585 ``The backslash shall retain its special meaning as an escape
586 character only when followed by one of the characters:
589 If STRIPDQ is zero, we handle the double quotes here and let
590 expand_word_internal handle the rest. If STRIPDQ is non-zero,
591 we have already been through one round of backslash stripping,
592 and want to strip these backslashes only if DQUOTE is non-zero,
593 indicating that we are inside an embedded double-quoted string. */
595 /* If we are in an embedded quoted string, then don't strip
596 backslashes before characters for which the backslash
597 retains its special meaning, but remove backslashes in
598 front of other characters. If we are not in an
599 embedded quoted string, don't strip backslashes at all.
600 This mess is necessary because the string was already
601 surrounded by double quotes (and sh has some really weird
603 The returned string will be run through expansion as if
604 it were double-quoted. */
605 if ((stripdq == 0 && c != '"') ||
606 (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
611 COPY_CHAR_I (temp, j, string, send, i);
615 /* A backslash protects the next character. The code just above
616 handles preserving the backslash in front of any character but
625 /* Inside backquotes, ``the portion of the quoted string from the
626 initial backquote and the characters up to the next backquote
627 that is not preceded by a backslash, having escape characters
628 removed, defines that command''. */
646 /* Pass everything between `$(' and the matching `)' or a quoted
647 ${ ... } pair through according to the Posix.2 specification. */
648 if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
651 if (string[i + 1] == LPAREN)
652 ret = extract_delimited_string (string, &si, "$(", "(", ")", 0); /*)*/
654 ret = extract_dollar_brace_string (string, &si, 1, 0);
657 temp[j++] = string[i + 1];
659 for (t = 0; ret[t]; t++, j++)
661 temp[j++] = string[si];
668 /* Add any character but a double quote to the quoted string we're
671 goto add_one_character;
685 /* Point to after the closing quote. */
693 /* This should really be another option to string_extract_double_quoted. */
695 skip_double_quoted (string, slen, sind)
702 int pass_next, backquote, si;
705 pass_next = backquote = 0;
707 while (c = string[i])
712 ADVANCE_CHAR (string, slen, i);
725 ADVANCE_CHAR (string, slen, i);
734 else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
737 if (string[i + 1] == LPAREN)
738 ret = extract_delimited_string (string, &si, "$(", "(", ")", EX_NOALLOC);
740 ret = extract_dollar_brace_string (string, &si, 0, EX_NOALLOC);
747 ADVANCE_CHAR (string, slen, i);
760 /* Extract the contents of STRING as if it is enclosed in single quotes.
761 SINDEX, when passed in, is the offset of the character immediately
762 following the opening single quote; on exit, SINDEX is left pointing after
763 the closing single quote. */
765 string_extract_single_quoted (string, sindex)
774 slen = strlen (string + *sindex) + *sindex;
776 while (string[i] && string[i] != '\'')
777 ADVANCE_CHAR (string, slen, i);
779 t = substring (string, *sindex, i);
789 skip_single_quoted (string, slen, sind)
798 while (string[c] && string[c] != '\'')
799 ADVANCE_CHAR (string, slen, c);
806 /* Just like string_extract, but doesn't hack backslashes or any of
807 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
809 string_extract_verbatim (string, sindex, charlist)
814 register int i = *sindex;
818 if (charlist[0] == '\'' && charlist[1] == '\0')
820 temp = string_extract_single_quoted (string, sindex);
821 --*sindex; /* leave *sindex at separator character */
825 for (i = *sindex; c = string[i]; i++)
833 if (MEMBER (c, charlist))
837 temp = substring (string, *sindex, i);
843 /* Extract the $( construct in STRING, and return a new string.
844 Start extracting at (SINDEX) as if we had just seen "$(".
845 Make (SINDEX) get the position of the matching ")". */
847 extract_command_subst (string, sindex)
851 return (extract_delimited_string (string, sindex, "$(", "(", ")", 0));
854 /* Extract the $[ construct in STRING, and return a new string. (])
855 Start extracting at (SINDEX) as if we had just seen "$[".
856 Make (SINDEX) get the position of the matching "]". */
858 extract_arithmetic_subst (string, sindex)
862 return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/
865 #if defined (PROCESS_SUBSTITUTION)
866 /* Extract the <( or >( construct in STRING, and return a new string.
867 Start extracting at (SINDEX) as if we had just seen "<(".
868 Make (SINDEX) get the position of the matching ")". */ /*))*/
870 extract_process_subst (string, starter, sindex)
875 return (extract_delimited_string (string, sindex, starter, "(", ")", 0));
877 #endif /* PROCESS_SUBSTITUTION */
879 #if defined (ARRAY_VARS)
881 extract_array_assignment_list (string, sindex)
885 return (extract_delimited_string (string, sindex, "(", (char *)NULL, ")", 0));
889 /* Extract and create a new string from the contents of STRING, a
890 character string delimited with OPENER and CLOSER. SINDEX is
891 the address of an int describing the current offset in STRING;
892 it should point to just after the first OPENER found. On exit,
893 SINDEX gets the position of the last character of the matching CLOSER.
894 If OPENER is more than a single character, ALT_OPENER, if non-null,
895 contains a character string that can also match CLOSER and thus
896 needs to be skipped. */
898 extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
901 char *opener, *alt_opener, *closer;
907 int pass_character, nesting_level;
908 int len_closer, len_opener, len_alt_opener;
911 slen = strlen (string + *sindex) + *sindex;
912 len_opener = STRLEN (opener);
913 len_alt_opener = STRLEN (alt_opener);
914 len_closer = STRLEN (closer);
921 while (nesting_level)
928 if (pass_character) /* previous char was backslash */
931 ADVANCE_CHAR (string, slen, i);
935 if (c == CTLESC || c == '\\')
942 /* Process a nested OPENER. */
943 if (STREQN (string + i, opener, len_opener))
946 t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|EX_NOALLOC);
951 /* Process a nested ALT_OPENER */
952 if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
954 si = i + len_alt_opener;
955 t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|EX_NOALLOC);
960 /* If the current substring terminates the delimited string, decrement
961 the nesting level. */
962 if (STREQN (string + i, closer, len_closer))
964 i += len_closer - 1; /* move to last byte of the closer */
966 if (nesting_level == 0)
970 /* Pass old-style command substitution through verbatim. */
974 t = string_extract (string, &si, "`", flags|EX_NOALLOC);
979 /* Pass single-quoted and double-quoted strings through verbatim. */
980 if (c == '\'' || c == '"')
983 i = (c == '\'') ? skip_single_quoted (string, slen, si)
984 : skip_double_quoted (string, slen, si);
988 /* move past this character, which was not special. */
989 ADVANCE_CHAR (string, slen, i);
992 if (c == 0 && nesting_level && no_longjmp_on_fatal_error == 0)
994 report_error ("bad substitution: no `%s' in %s", closer, string);
995 last_command_exit_value = EXECUTION_FAILURE;
996 jump_to_top_level (DISCARD);
999 si = i - *sindex - len_closer + 1;
1000 if (flags & EX_NOALLOC)
1001 result = (char *)NULL;
1004 result = (char *)xmalloc (1 + si);
1005 strncpy (result, string + *sindex, si);
1013 /* Extract a parameter expansion expression within ${ and } from STRING.
1014 Obey the Posix.2 rules for finding the ending `}': count braces while
1015 skipping over enclosed quoted strings and command substitutions.
1016 SINDEX is the address of an int describing the current offset in STRING;
1017 it should point to just after the first `{' found. On exit, SINDEX
1018 gets the position of the matching `}'. QUOTED is non-zero if this
1019 occurs inside double quotes. */
1020 /* XXX -- this is very similar to extract_delimited_string -- XXX */
1022 extract_dollar_brace_string (string, sindex, quoted, flags)
1024 int *sindex, quoted, flags;
1028 int pass_character, nesting_level, si;
1034 slen = strlen (string + *sindex) + *sindex;
1037 while (c = string[i])
1042 ADVANCE_CHAR (string, slen, i);
1046 /* CTLESCs and backslashes quote the next character. */
1047 if (c == CTLESC || c == '\\')
1054 if (string[i] == '$' && string[i+1] == LBRACE)
1064 if (nesting_level == 0)
1070 /* Pass the contents of old-style command substitutions through
1075 t = string_extract (string, &si, "`", flags|EX_NOALLOC);
1080 /* Pass the contents of new-style command substitutions and
1081 arithmetic substitutions through verbatim. */
1082 if (string[i] == '$' && string[i+1] == LPAREN)
1085 t = extract_delimited_string (string, &si, "$(", "(", ")", flags|EX_NOALLOC); /*)*/
1090 /* Pass the contents of single-quoted and double-quoted strings
1091 through verbatim. */
1092 if (c == '\'' || c == '"')
1095 i = (c == '\'') ? skip_single_quoted (string, slen, si)
1096 : skip_double_quoted (string, slen, si);
1097 /* skip_XXX_quoted leaves index one past close quote */
1101 /* move past this character, which was not special. */
1102 ADVANCE_CHAR (string, slen, i);
1105 if (c == 0 && nesting_level && no_longjmp_on_fatal_error == 0)
1107 report_error ("bad substitution: no ending `}' in %s", string);
1108 last_command_exit_value = EXECUTION_FAILURE;
1109 jump_to_top_level (DISCARD);
1112 result = (flags & EX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
1118 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1119 STRING, and returns a pointer to it. */
1121 de_backslash (string)
1124 register size_t slen;
1125 register int i, j, prev_i;
1128 slen = strlen (string);
1131 /* Loop copying string[i] to string[j], i >= j. */
1134 if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
1135 string[i + 1] == '$'))
1138 ADVANCE_CHAR (string, slen, i);
1140 do string[j++] = string[prev_i++]; while (prev_i < i);
1151 /* Replace instances of \! in a string with !. */
1153 unquote_bang (string)
1157 register char *temp;
1159 temp = (char *)xmalloc (1 + strlen (string));
1161 for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
1163 if (string[i] == '\\' && string[i + 1] == '!')
1169 strcpy (string, temp);
1174 #if defined (READLINE)
1175 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
1176 an unclosed quoted string), or if the character at EINDEX is quoted
1177 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
1178 single and double-quoted string parsing functions should not return an
1179 error if there are unclosed quotes or braces. The characters that this
1180 recognizes need to be the same as the contents of
1181 rl_completer_quote_characters. */
1183 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0)
1186 char_is_quoted (string, eindex)
1190 int i, pass_next, c;
1194 slen = strlen (string);
1195 no_longjmp_on_fatal_error = 1;
1204 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
1206 ADVANCE_CHAR (string, slen, i);
1215 else if (c == '\'' || c == '"')
1217 i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
1218 : skip_double_quoted (string, slen, ++i);
1221 /* no increment, the skip_xxx functions go one past end */
1224 ADVANCE_CHAR (string, slen, i);
1231 unclosed_pair (string, eindex, openstr)
1236 int i, pass_next, openc, olen;
1240 slen = strlen (string);
1241 olen = strlen (openstr);
1242 i = pass_next = openc = 0;
1248 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
1250 ADVANCE_CHAR (string, slen, i);
1253 else if (string[i] == '\\')
1259 else if (STREQN (string + i, openstr, olen))
1264 else if (string[i] == '\'' || string[i] == '"')
1266 i = (string[i] == '\'') ? skip_single_quoted (string, slen, i)
1267 : skip_double_quoted (string, slen, i);
1272 ADVANCE_CHAR (string, slen, i);
1277 /* Skip characters in STRING until we find a character in DELIMS, and return
1278 the index of that character. START is the index into string at which we
1279 begin. This is similar in spirit to strpbrk, but it returns an index into
1280 STRING and takes a starting index. This little piece of code knows quite
1281 a lot of shell syntax. It's very similar to skip_double_quoted and other
1282 functions of that ilk. */
1284 skip_to_delim (string, start, delims)
1289 int i, pass_next, backq, si, c;
1294 slen = strlen (string + start) + start;
1295 no_longjmp_on_fatal_error = 1;
1297 pass_next = backq = 0;
1298 while (c = string[i])
1305 ADVANCE_CHAR (string, slen, i);
1318 ADVANCE_CHAR (string, slen, i);
1327 else if (c == '\'' || c == '"')
1329 i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
1330 : skip_double_quoted (string, slen, ++i);
1331 /* no increment, the skip functions increment past the closing quote. */
1333 else if (c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
1336 if (string[si] == '\0')
1339 if (string[i+1] == LPAREN)
1340 temp = extract_delimited_string (string, &si, "$(", "(", ")", EX_NOALLOC); /* ) */
1342 temp = extract_dollar_brace_string (string, &si, 0, EX_NOALLOC);
1344 if (string[i] == '\0') /* don't increment i past EOS in loop */
1349 else if (member (c, delims))
1352 ADVANCE_CHAR (string, slen, i);
1358 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
1359 individual words. If DELIMS is NULL, the current value of $IFS is used
1360 to split the string. SENTINEL is an index to look for. NWP, if non-NULL
1361 gets the number of words in the returned list. CWP, if non-NULL, gets
1362 the index of the word containing SENTINEL. Non-whitespace chars in
1363 DELIMS delimit separate fields. */
1365 split_at_delims (string, slen, delims, sentinel, nwp, cwp)
1372 int ts, te, i, nw, cw;
1373 char *token, *d, *d2;
1374 WORD_LIST *ret, *tl;
1376 if (string == 0 || *string == '\0')
1382 return ((WORD_LIST *)NULL);
1385 d = (delims == 0) ? ifs_value : delims;
1387 /* Make d2 the non-whitespace characters in delims */
1391 d2 = (char *)xmalloc (strlen (delims) + 1);
1392 for (i = ts = 0; delims[i]; i++)
1394 if (whitespace(delims[i]) == 0)
1395 d2[ts++] = delims[i];
1400 ret = (WORD_LIST *)NULL;
1402 for (i = 0; member (string[i], d) && spctabnl(string[i]); i++)
1404 if (string[i] == '\0')
1412 te = skip_to_delim (string, ts, d);
1414 /* If we have a non-whitespace delimiter character, use it to make a
1415 separate field. This is just about what $IFS splitting does and
1416 is closer to the behavior of the shell parser. */
1417 if (ts == te && d2 && member (string[ts], d2))
1420 while (member (string[te], d2))
1424 token = substring (string, ts, te);
1426 ret = add_string_to_list (token, ret);
1430 if (sentinel >= ts && sentinel <= te)
1433 /* If the cursor is at whitespace just before word start, set the
1434 sentinel word to the current word. */
1435 if (cwp && cw == -1 && sentinel == ts-1)
1438 /* If the cursor is at whitespace between two words, make a new, empty
1439 word, add it before (well, after, since the list is in reverse order)
1440 the word we just added, and set the current word to that one. */
1441 if (cwp && cw == -1 && sentinel < ts)
1443 tl = make_word_list (make_word (""), ret->next);
1449 if (string[te] == 0)
1452 i = te /* + member (string[te], d) */;
1453 while (member (string[i], d) && spctabnl(string[i]))
1462 /* Special case for SENTINEL at the end of STRING. If we haven't found
1463 the word containing SENTINEL yet, and the index we're looking for is at
1464 the end of STRING, add an additional null argument and set the current
1465 word pointer to that. */
1466 if (cwp && cw == -1 && sentinel >= slen)
1468 if (whitespace (string[sentinel - 1]))
1471 ret = add_string_to_list (token, ret);
1482 return (REVERSE_LIST (ret, WORD_LIST *));
1484 #endif /* READLINE */
1488 /* Extract the name of the variable to bind to from the assignment string. */
1490 assignment_name (string)
1496 offset = assignment (string);
1498 return (char *)NULL;
1499 temp = substring (string, 0, offset);
1504 /* **************************************************************** */
1506 /* Functions to convert strings to WORD_LISTs and vice versa */
1508 /* **************************************************************** */
1510 /* Return a single string of all the words in LIST. SEP is the separator
1511 to put between individual elements of LIST in the output string. */
1513 string_list_internal (list, sep)
1517 register WORD_LIST *t;
1519 int word_len, sep_len, result_size;
1522 return ((char *)NULL);
1524 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
1525 sep_len = STRLEN (sep);
1528 for (t = list; t; t = t->next)
1531 result_size += sep_len;
1532 result_size += strlen (t->word->word);
1535 r = result = (char *)xmalloc (result_size + 1);
1537 for (t = list; t; t = t->next)
1539 if (t != list && sep_len)
1543 FASTCOPY (sep, r, sep_len);
1550 word_len = strlen (t->word->word);
1551 FASTCOPY (t->word->word, r, word_len);
1559 /* Return a single string of all the words present in LIST, separating
1560 each word with a space. */
1565 return (string_list_internal (list, " "));
1568 /* Return a single string of all the words present in LIST, obeying the
1569 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
1570 expansion [of $*] appears within a double quoted string, it expands
1571 to a single field with the value of each parameter separated by the
1572 first character of the IFS variable, or by a <space> if IFS is unset." */
1574 string_list_dollar_star (list)
1579 sep[0] = ifs_firstc;
1582 return (string_list_internal (list, sep));
1585 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
1586 is non-zero, the $@ appears within double quotes, and we should quote
1587 the list before converting it into a string. If IFS is unset, and the
1588 word is not quoted, we just need to quote CTLESC and CTLNUL characters
1589 in the words in the list, because the default value of $IFS is
1590 <space><tab><newline>, IFS characters in the words in the list should
1591 also be split. If IFS is null, and the word is not quoted, we need
1592 to quote the words in the list to preserve the positional parameters
1595 string_list_dollar_at (list, quoted)
1602 /* XXX this could just be ifs = ifs_value; */
1603 ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
1605 sep[0] = (ifs == 0 || *ifs == 0) ? ' ' : *ifs;
1608 tlist = ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (ifs && *ifs == 0))
1610 : list_quote_escapes (list);
1611 return (string_list_internal (tlist, sep));
1614 /* Return the list of words present in STRING. Separate the string into
1615 words at any of the characters found in SEPARATORS. If QUOTED is
1616 non-zero then word in the list will have its quoted flag set, otherwise
1617 the quoted flag is left as make_word () deemed fit.
1619 This obeys the P1003.2 word splitting semantics. If `separators' is
1620 exactly <space><tab><newline>, then the splitting algorithm is that of
1621 the Bourne shell, which treats any sequence of characters from `separators'
1622 as a delimiter. If IFS is unset, which results in `separators' being set
1623 to "", no splitting occurs. If separators has some other value, the
1624 following rules are applied (`IFS white space' means zero or more
1625 occurrences of <space>, <tab>, or <newline>, as long as those characters
1626 are in `separators'):
1628 1) IFS white space is ignored at the start and the end of the
1630 2) Each occurrence of a character in `separators' that is not
1631 IFS white space, along with any adjacent occurrences of
1632 IFS white space delimits a field.
1633 3) Any nonzero-length sequence of IFS white space delimits a field.
1636 /* BEWARE! list_string strips null arguments. Don't call it twice and
1637 expect to have "" preserved! */
1639 /* This performs word splitting and quoted null character removal on
1642 #define issep(c) ((separators)[1] ? (member ((c), separators)) : (c) == (separators)[0])
1644 #define issep(c) ((separators)[1] ? isifs(c) : (c) == (separators)[0])
1648 list_string (string, separators, quoted)
1649 register char *string, *separators;
1654 char *current_word, *s;
1655 int sindex, sh_style_split, whitesep;
1657 if (!string || !*string)
1658 return ((WORD_LIST *)NULL);
1660 sh_style_split = separators && separators[0] == ' ' &&
1661 separators[1] == '\t' &&
1662 separators[2] == '\n' &&
1663 separators[3] == '\0';
1665 /* Remove sequences of whitespace at the beginning of STRING, as
1666 long as those characters appear in IFS. Do not do this if
1667 STRING is quoted or if there are no separator characters. */
1668 if (!quoted || !separators || !*separators)
1670 for (s = string; *s && spctabnl (*s) && issep (*s); s++);
1673 return ((WORD_LIST *)NULL);
1678 /* OK, now STRING points to a word that does not begin with white space.
1679 The splitting algorithm is:
1680 extract a word, stopping at a separator
1681 skip sequences of spc, tab, or nl as long as they are separators
1682 This obeys the field splitting rules in Posix.2. */
1683 for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
1685 current_word = string_extract_verbatim (string, &sindex, separators);
1686 if (current_word == 0)
1689 /* If we have a quoted empty string, add a quoted null argument. We
1690 want to preserve the quoted null character iff this is a quoted
1691 empty string; otherwise the quoted null characters are removed
1693 if (QUOTED_NULL (current_word))
1695 t = make_bare_word ("");
1696 t->flags |= W_QUOTED;
1698 t->word = make_quoted_char ('\0');
1699 result = make_word_list (t, result);
1701 else if (current_word[0] != '\0')
1703 /* If we have something, then add it regardless. However,
1704 perform quoted null character removal on the current word. */
1705 remove_quoted_nulls (current_word);
1706 result = add_string_to_list (current_word, result);
1707 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
1708 result->word->flags |= W_QUOTED;
1711 /* If we're not doing sequences of separators in the traditional
1712 Bourne shell style, then add a quoted null argument. */
1713 else if (!sh_style_split && !spctabnl (string[sindex]))
1715 t = make_bare_word ("");
1716 t->flags |= W_QUOTED;
1718 t->word = make_quoted_char ('\0');
1719 result = make_word_list (t, result);
1722 free (current_word);
1724 /* Note whether or not the separator is IFS whitespace, used later. */
1725 whitesep = string[sindex] && spctabnl (string[sindex]);
1727 /* Move past the current separator character. */
1731 /* Now skip sequences of space, tab, or newline characters if they are
1732 in the list of separators. */
1733 while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex]))
1736 /* If the first separator was IFS whitespace and the current character
1737 is a non-whitespace IFS character, it should be part of the current
1738 field delimiter, not a separate delimiter that would result in an
1739 empty field. Look at POSIX.2, 3.6.5, (3)(b). */
1740 if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex]))
1743 return (REVERSE_LIST (result, WORD_LIST *));
1746 /* Parse a single word from STRING, using SEPARATORS to separate fields.
1747 ENDPTR is set to the first character after the word. This is used by
1748 the `read' builtin. This is never called with SEPARATORS != $IFS;
1749 it should be simplified.
1751 XXX - this function is very similar to list_string; they should be
1754 get_word_from_string (stringp, separators, endptr)
1755 char **stringp, *separators, **endptr;
1759 int sindex, sh_style_split, whitesep;
1761 if (!stringp || !*stringp || !**stringp)
1762 return ((char *)NULL);
1766 sh_style_split = separators && separators[0] == ' ' &&
1767 separators[1] == '\t' &&
1768 separators[2] == '\n' &&
1769 separators[3] == '\0';
1771 /* Remove sequences of whitespace at the beginning of STRING, as
1772 long as those characters appear in IFS. */
1773 if (sh_style_split || !separators || !*separators)
1775 for (; *s && spctabnl (*s) && isifs (*s); s++);
1777 /* If the string is nothing but whitespace, update it and return. */
1783 return ((char *)NULL);
1787 /* OK, S points to a word that does not begin with white space.
1788 Now extract a word, stopping at a separator, save a pointer to
1789 the first character after the word, then skip sequences of spc,
1790 tab, or nl as long as they are separators.
1792 This obeys the field splitting rules in Posix.2. */
1794 current_word = string_extract_verbatim (s, &sindex, separators);
1796 /* Set ENDPTR to the first character after the end of the word. */
1798 *endptr = s + sindex;
1800 /* Note whether or not the separator is IFS whitespace, used later. */
1801 whitesep = s[sindex] && spctabnl (s[sindex]);
1803 /* Move past the current separator character. */
1807 /* Now skip sequences of space, tab, or newline characters if they are
1808 in the list of separators. */
1809 while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
1812 /* If the first separator was IFS whitespace and the current character is
1813 a non-whitespace IFS character, it should be part of the current field
1814 delimiter, not a separate delimiter that would result in an empty field.
1815 Look at POSIX.2, 3.6.5, (3)(b). */
1816 if (s[sindex] && whitesep && isifs (s[sindex]) && !spctabnl (s[sindex]))
1819 /* Update STRING to point to the next field. */
1820 *stringp = s + sindex;
1821 return (current_word);
1824 /* Remove IFS white space at the end of STRING. Start at the end
1825 of the string and walk backwards until the beginning of the string
1826 or we find a character that's not IFS white space and not CTLESC.
1827 Only let CTLESC escape a white space character if SAW_ESCAPE is
1830 strip_trailing_ifs_whitespace (string, separators, saw_escape)
1831 char *string, *separators;
1836 s = string + STRLEN (string) - 1;
1837 while (s > string && ((spctabnl (*s) && isifs (*s)) ||
1838 (saw_escape && *s == CTLESC && spctabnl (s[1]))))
1846 /* Split STRING into words at whitespace. Obeys shell-style quoting with
1847 backslashes, single and double quotes. */
1849 list_string_with_quotes (string)
1855 int c, i, tokstart, len;
1857 for (s = string; s && *s && spctabnl (*s); s++)
1859 if (s == 0 || *s == 0)
1860 return ((WORD_LIST *)NULL);
1864 list = (WORD_LIST *)NULL;
1875 i = skip_single_quoted (s, s_len, ++i);
1877 i = skip_double_quoted (s, s_len, ++i);
1878 else if (c == 0 || spctabnl (c))
1880 /* We have found the end of a token. Make a word out of it and
1881 add it to the word list. */
1882 token = substring (s, tokstart, i);
1883 list = add_string_to_list (token, list);
1885 while (spctabnl (s[i]))
1893 i++; /* normal character */
1895 return (REVERSE_LIST (list, WORD_LIST *));
1899 /********************************************************/
1901 /* Functions to perform assignment statements */
1903 /********************************************************/
1905 /* Given STRING, an assignment string, get the value of the right side
1906 of the `=', and bind it to the left side. If EXPAND is true, then
1907 perform parameter expansion, command substitution, and arithmetic
1908 expansion on the right-hand side. Perform tilde expansion in any
1909 case. Do not perform word splitting on the result of expansion. */
1911 do_assignment_internal (string, expand)
1918 #if defined (ARRAY_VARS)
1920 int ni, assign_list = 0;
1923 offset = assignment (string);
1924 name = savestring (string);
1925 value = (char *)NULL;
1927 if (name[offset] == '=')
1932 temp = name + offset + 1;
1934 #if defined (ARRAY_VARS)
1935 if (expand && temp[0] == LPAREN && xstrchr (temp, RPAREN))
1937 assign_list = ni = 1;
1938 value = extract_delimited_string (temp, &ni, "(", (char *)NULL, ")", 0);
1943 /* Perform tilde expansion. */
1944 if (expand && temp[0])
1946 temp = (xstrchr (temp, '~') && unquoted_member ('~', temp))
1947 ? bash_tilde_expand (temp, 1)
1948 : savestring (temp);
1950 value = expand_string_if_necessary (temp, 0, expand_string_unsplit);
1954 value = savestring (temp);
1959 value = (char *)xmalloc (1);
1963 if (echo_command_at_execute)
1965 #if defined (ARRAY_VARS)
1967 fprintf (stderr, "%s%s=(%s)\n", indirection_level_string (), name, value);
1970 fprintf (stderr, "%s%s=%s\n", indirection_level_string (), name, value);
1973 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
1975 #if defined (ARRAY_VARS)
1976 if (t = xstrchr (name, '[')) /*]*/
1980 report_error ("%s: cannot assign list to array member", name);
1983 entry = assign_array_element (name, value);
1987 else if (assign_list)
1988 entry = assign_array_from_string (name, value);
1990 #endif /* ARRAY_VARS */
1991 entry = bind_variable (name, value);
1993 stupidly_hack_special_variables (name);
1996 VUNSETATTR (entry, att_invisible);
1998 /* Return 1 if the assignment seems to have been performed correctly. */
1999 ASSIGN_RETURN (entry ? ((readonly_p (entry) == 0) && noassign_p (entry) == 0) : 0);
2002 /* Perform the assignment statement in STRING, and expand the
2003 right side by doing command and parameter expansion. */
2005 do_assignment (string)
2008 return do_assignment_internal (string, 1);
2011 /* Given STRING, an assignment string, get the value of the right side
2012 of the `=', and bind it to the left side. Do not do command and
2013 parameter substitution on the right hand side. */
2015 do_assignment_no_expand (string)
2018 return do_assignment_internal (string, 0);
2021 /***************************************************
2023 * Functions to manage the positional parameters *
2025 ***************************************************/
2027 /* Return the word list that corresponds to `$*'. */
2029 list_rest_of_args ()
2031 register WORD_LIST *list, *args;
2034 /* Break out of the loop as soon as one of the dollar variables is null. */
2035 for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++)
2036 list = make_word_list (make_bare_word (dollar_vars[i]), list);
2038 for (args = rest_of_args; args; args = args->next)
2039 list = make_word_list (make_bare_word (args->word->word), list);
2041 return (REVERSE_LIST (list, WORD_LIST *));
2047 register WORD_LIST *list;
2050 for (n = 0; n < 9 && dollar_vars[n+1]; n++)
2052 for (list = rest_of_args; list; list = list->next)
2057 /* Return the value of a positional parameter. This handles values > 10. */
2059 get_dollar_var_value (ind)
2066 temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL;
2067 else /* We want something like ${11} */
2070 for (p = rest_of_args; p && ind--; p = p->next)
2072 temp = p ? savestring (p->word->word) : (char *)NULL;
2077 /* Make a single large string out of the dollar digit variables,
2078 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
2079 case of "$*" with respect to IFS. */
2081 string_rest_of_args (dollar_star)
2084 register WORD_LIST *list;
2087 list = list_rest_of_args ();
2088 string = dollar_star ? string_list_dollar_star (list) : string_list (list);
2089 dispose_words (list);
2093 /* Return a string containing the positional parameters from START to
2094 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
2095 which only makes a difference if QUOTED is non-zero. If QUOTED includes
2096 Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
2097 no quoting chars are added. */
2099 pos_params (string, start, end, quoted)
2101 int start, end, quoted;
2103 WORD_LIST *save, *params, *h, *t;
2107 /* see if we can short-circuit. if start == end, we want 0 parameters. */
2109 return ((char *)NULL);
2111 save = params = list_rest_of_args ();
2113 return ((char *)NULL);
2115 for (i = 1; params && i < start; i++)
2116 params = params->next;
2118 return ((char *)NULL);
2119 for (h = t = params; params && i < end; i++)
2122 params = params->next;
2125 t->next = (WORD_LIST *)NULL;
2126 if (string[0] == '*')
2127 ret = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? string_list_dollar_star (quote_list (h)) : string_list (h);
2129 ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (h) : h);
2133 dispose_words (save);
2137 /******************************************************************/
2139 /* Functions to expand strings to strings or WORD_LISTs */
2141 /******************************************************************/
2143 #if defined (PROCESS_SUBSTITUTION)
2144 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC)
2146 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC)
2149 /* If there are any characters in STRING that require full expansion,
2150 then call FUNC to expand STRING; otherwise just perform quote
2151 removal if necessary. This returns a new string. */
2153 expand_string_if_necessary (string, quoted, func)
2164 slen = strlen (string);
2168 if (EXP_CHAR (string[i]))
2170 else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
2172 ADVANCE_CHAR (string, slen, i);
2177 list = (*func) (string, quoted);
2180 ret = string_list (list);
2181 dispose_words (list);
2186 else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
2187 ret = string_quote_removal (string, quoted);
2189 ret = savestring (string);
2194 static inline char *
2195 expand_string_to_string_internal (string, quoted, func)
2203 if (string == 0 || *string == '\0')
2204 return ((char *)NULL);
2206 list = (*func) (string, quoted);
2209 ret = string_list (list);
2210 dispose_words (list);
2219 expand_string_to_string (string, quoted)
2223 return (expand_string_to_string_internal (string, quoted, expand_string));
2227 expand_string_unsplit_to_string (string, quoted)
2231 return (expand_string_to_string_internal (string, quoted, expand_string_unsplit));
2234 #if defined (COND_COMMAND)
2235 /* Just remove backslashes in STRING. Returns a new string. */
2237 remove_backslashes (string)
2242 r = ret = (char *)xmalloc (strlen (string) + 1);
2243 for (s = string; s && *s; )
2255 /* This needs better error handling. */
2256 /* Expand W for use as an argument to a unary or binary operator in a
2257 [[...]] expression. If SPECIAL is nonzero, this is the rhs argument
2258 to the != or == operator, and should be treated as a pattern. In
2259 this case, we quote the string specially for the globbing code. The
2260 caller is responsible for removing the backslashes if the unquoted
2261 words is needed later. */
2263 cond_expand_word (w, special)
2270 if (w->word == 0 || w->word[0] == '\0')
2271 return ((char *)NULL);
2273 if (xstrchr (w->word, '~') && unquoted_member ('~', w->word))
2275 p = bash_tilde_expand (w->word, 0);
2280 l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
2286 r = string_list (l);
2290 p = string_list (l);
2291 r = quote_string_for_globbing (p, QGLOB_CVTNULL);
2303 /* Call expand_word_internal to expand W and handle error returns.
2304 A convenience function for functions that don't want to handle
2305 any errors or free any memory before aborting. */
2307 call_expand_word_internal (w, q, i, c, e)
2313 result = expand_word_internal (w, q, i, c, e);
2314 if (result == &expand_word_error || result == &expand_word_fatal)
2316 expand_no_split_dollar_star = 0; /* XXX */
2317 /* By convention, each time this error is returned, w->word has
2318 already been freed (it sometimes may not be in the fatal case,
2319 but that doesn't result in a memory leak because we're going
2320 to exit in most cases). */
2321 w->word = (char *)NULL;
2322 last_command_exit_value = EXECUTION_FAILURE;
2323 jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
2330 /* Perform parameter expansion, command substitution, and arithmetic
2331 expansion on STRING, as if it were a word. Leave the result quoted. */
2333 expand_string_internal (string, quoted)
2340 if (string == 0 || *string == 0)
2341 return ((WORD_LIST *)NULL);
2344 td.word = savestring (string);
2346 tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
2352 /* Expand STRING by performing parameter expansion, command substitution,
2353 and arithmetic expansion. Dequote the resulting WORD_LIST before
2354 returning it, but do not perform word splitting. The call to
2355 remove_quoted_nulls () is in here because word splitting normally
2356 takes care of quote removal. */
2358 expand_string_unsplit (string, quoted)
2364 if (string == 0 || *string == '\0')
2365 return ((WORD_LIST *)NULL);
2367 expand_no_split_dollar_star = 1;
2368 value = expand_string_internal (string, quoted);
2369 expand_no_split_dollar_star = 0;
2374 remove_quoted_nulls (value->word->word);
2375 dequote_list (value);
2381 /* Expand one of the PS? prompt strings. This is a sort of combination of
2382 expand_string_unsplit and expand_string_internal, but returns the
2383 passed string when an error occurs. Might want to trap other calls
2384 to jump_to_top_level here so we don't endlessly loop. */
2386 expand_prompt_string (string, quoted)
2393 if (string == 0 || *string == 0)
2394 return ((WORD_LIST *)NULL);
2397 td.word = savestring (string);
2399 no_longjmp_on_fatal_error = 1;
2400 value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
2401 no_longjmp_on_fatal_error = 0;
2403 if (value == &expand_word_error || value == &expand_word_fatal)
2405 value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
2412 remove_quoted_nulls (value->word->word);
2413 dequote_list (value);
2418 /* Expand STRING just as if you were expanding a word, but do not dequote
2419 the resultant WORD_LIST. This is called only from within this file,
2420 and is used to correctly preserve quoted characters when expanding
2421 things like ${1+"$@"}. This does parameter expansion, command
2422 substitution, arithmetic expansion, and word splitting. */
2424 expand_string_leave_quoted (string, quoted)
2431 if (string == 0 || *string == '\0')
2432 return ((WORD_LIST *)NULL);
2434 tlist = expand_string_internal (string, quoted);
2438 tresult = word_list_split (tlist);
2439 dispose_words (tlist);
2442 return ((WORD_LIST *)NULL);
2445 /* This does not perform word splitting or dequote the WORD_LIST
2448 expand_string_for_rhs (string, quoted, dollar_at_p, has_dollar_at)
2450 int quoted, *dollar_at_p, *has_dollar_at;
2455 if (string == 0 || *string == '\0')
2456 return (WORD_LIST *)NULL;
2460 tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, has_dollar_at);
2464 /* Expand STRING just as if you were expanding a word. This also returns
2465 a list of words. Note that filename globbing is *NOT* done for word
2466 or string expansion, just when the shell is expanding a command. This
2467 does parameter expansion, command substitution, arithmetic expansion,
2468 and word splitting. Dequote the resultant WORD_LIST before returning. */
2470 expand_string (string, quoted)
2476 if (string == 0 || *string == '\0')
2477 return ((WORD_LIST *)NULL);
2479 result = expand_string_leave_quoted (string, quoted);
2480 return (result ? dequote_list (result) : result);
2483 /***************************************************
2485 * Functions to handle quoting chars *
2487 ***************************************************/
2491 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
2492 The parser passes CTLNUL as CTLESC CTLNUL. */
2494 /* Quote escape characters in string s, but no other characters. This is
2495 used to protect CTLESC and CTLNUL in variable values from the rest of
2496 the word expansion process after the variable is expanded. */
2498 quote_escapes (string)
2501 register char *s, *t;
2503 char *result, *send;
2506 slen = strlen (string);
2507 send = string + slen;
2509 t = result = (char *)xmalloc ((slen * 2) + 1);
2514 if (*s == CTLESC || *s == CTLNUL)
2516 COPY_CHAR_P (t, s, send);
2523 list_quote_escapes (list)
2526 register WORD_LIST *w;
2529 for (w = list; w; w = w->next)
2532 w->word->word = quote_escapes (t);
2538 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
2540 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
2541 This is necessary to make unquoted CTLESC and CTLNUL characters in the
2542 data stream pass through properly.
2544 We need to remove doubled CTLESC characters inside quoted strings before
2545 quoting the entire string, so we do not double the number of CTLESC
2548 Also used by parts of the pattern substitution code. */
2550 dequote_escapes (string)
2553 register char *s, *t;
2555 char *result, *send;
2561 slen = strlen (string);
2562 send = string + slen;
2564 t = result = (char *)xmalloc (slen + 1);
2567 if (strchr (string, CTLESC) == 0)
2568 return (strcpy (result, s));
2572 if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL))
2578 COPY_CHAR_P (t, s, send);
2584 /* Return a new string with the quoted representation of character C. */
2586 make_quoted_char (c)
2591 temp = (char *)xmalloc (3);
2606 /* Quote STRING. Return a new string. */
2608 quote_string (string)
2613 char *result, *send;
2617 result = (char *)xmalloc (2);
2625 slen = strlen (string);
2626 send = string + slen;
2628 result = (char *)xmalloc ((slen * 2) + 1);
2630 for (t = result; string < send; )
2633 COPY_CHAR_P (t, string, send);
2640 /* De-quoted quoted characters in STRING. */
2642 dequote_string (string)
2645 register char *s, *t;
2647 char *result, *send;
2650 slen = strlen (string);
2652 t = result = (char *)xmalloc (slen + 1);
2654 if (QUOTED_NULL (string))
2660 /* If no character in the string can be quoted, don't bother examining
2661 each character. Just return a copy of the string passed to us. */
2662 if (strchr (string, CTLESC) == NULL)
2663 return (strcpy (result, string));
2665 send = string + slen;
2675 COPY_CHAR_P (t, s, send);
2682 /* Quote the entire WORD_LIST list. */
2687 register WORD_LIST *w;
2690 for (w = list; w; w = w->next)
2693 w->word->word = quote_string (t);
2695 w->word->flags |= W_QUOTED;
2705 register WORD_LIST *tlist;
2707 for (tlist = list; tlist; tlist = tlist->next)
2709 s = dequote_string (tlist->word->word);
2710 free (tlist->word->word);
2711 tlist->word->word = s;
2716 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
2719 remove_quoted_escapes (string)
2726 t = dequote_escapes (string);
2734 /* Perform quoted null character removal on STRING. We don't allow any
2735 quoted null characters in the middle or at the ends of strings because
2736 of how expand_word_internal works. remove_quoted_nulls () turns
2737 STRING into an empty string iff it only consists of a quoted null,
2738 and removes all unquoted CTLNUL characters. */
2740 remove_quoted_nulls (string)
2743 register size_t slen;
2744 register int i, j, prev_i;
2747 if (strchr (string, CTLNUL) == 0) /* XXX */
2748 return string; /* XXX */
2750 slen = strlen (string);
2755 if (string[i] == CTLESC)
2761 else if (string[i] == CTLNUL)
2765 ADVANCE_CHAR (string, slen, i);
2768 do string[j++] = string[prev_i++]; while (prev_i < i);
2778 /* Perform quoted null character removal on each element of LIST.
2779 This modifies LIST. */
2781 word_list_remove_quoted_nulls (list)
2784 register WORD_LIST *t;
2786 for (t = list; t; t = t->next)
2787 remove_quoted_nulls (t->word->word);
2790 /* **************************************************************** */
2792 /* Functions for Matching and Removing Patterns */
2794 /* **************************************************************** */
2796 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
2797 can have one of 4 values:
2798 RP_LONG_LEFT remove longest matching portion at start of PARAM
2799 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
2800 RP_LONG_RIGHT remove longest matching portion at end of PARAM
2801 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
2804 #define RP_LONG_LEFT 1
2805 #define RP_SHORT_LEFT 2
2806 #define RP_LONG_RIGHT 3
2807 #define RP_SHORT_RIGHT 4
2810 remove_pattern (param, pattern, op)
2811 char *param, *pattern;
2816 register char *p, *ret, c;
2818 if (param == NULL || *param == '\0')
2820 if (pattern == NULL || *pattern == '\0') /* minor optimization */
2821 return (savestring (param));
2823 len = STRLEN (param);
2828 case RP_LONG_LEFT: /* remove longest match at start */
2829 for (p = end; p >= param; p--)
2832 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
2835 return (savestring (p));
2841 case RP_SHORT_LEFT: /* remove shortest match at start */
2842 for (p = param; p <= end; p++)
2845 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
2848 return (savestring (p));
2854 case RP_LONG_RIGHT: /* remove longest match at end */
2855 for (p = param; p <= end; p++)
2857 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
2860 ret = savestring (param);
2867 case RP_SHORT_RIGHT: /* remove shortest match at end */
2868 for (p = end; p >= param; p--)
2870 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
2873 ret = savestring (param);
2880 return (savestring (param)); /* no match, return original string */
2883 /* Return 1 of the first character of STRING could match the first
2884 character of pattern PAT. Used to avoid n2 calls to strmatch(). */
2886 match_pattern_char (pat, string)
2897 return (*string == c);
2899 return (*string == *pat);
2901 return (*pat == LPAREN ? 1 : (*string != '\0'));
2907 return (*pat == LPAREN ? 1 : (*string == c));
2909 return (*string != '\0');
2913 /* Match PAT anywhere in STRING and return the match boundaries.
2914 This returns 1 in case of a successful match, 0 otherwise. SP
2915 and EP are pointers into the string where the match begins and
2916 ends, respectively. MTYPE controls what kind of match is attempted.
2917 MATCH_BEG and MATCH_END anchor the match at the beginning and end
2918 of the string, respectively. The longest match is returned. */
2920 match_pattern (string, pat, mtype, sp, ep)
2926 register char *p, *p1;
2929 if (string == 0 || *string == 0 || pat == 0 || *pat == 0)
2932 end = string + STRLEN (string);
2937 for (p = string; p <= end; p++)
2939 if (match_pattern_char (pat, p))
2941 for (p1 = end; p1 >= p; p1--)
2943 c = *p1; *p1 = '\0';
2944 if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
2958 if (match_pattern_char (pat, string) == 0)
2960 for (p = end; p >= string; p--)
2963 if (strmatch (pat, string, FNMATCH_EXTFLAG) == 0)
2975 for (p = string; p <= end; p++)
2976 if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
2989 getpatspec (c, value)
2994 return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
2996 return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
2999 /* Posix.2 says that the WORD should be run through tilde expansion,
3000 parameter expansion, command substitution and arithmetic expansion.
3001 This leaves the result quoted, so quote_string_for_globbing () has
3002 to be called to fix it up for strmatch (). If QUOTED is non-zero,
3003 it means that the entire expression was enclosed in double quotes.
3004 This means that quoting characters in the pattern do not make any
3005 special pattern characters quoted. For example, the `*' in the
3006 following retains its special meaning: "${foo#'*'}". */
3008 getpattern (value, quoted, expandpat)
3010 int quoted, expandpat;
3016 tword = xstrchr (value, '~') ? bash_tilde_expand (value, 0) : savestring (value);
3018 /* There is a problem here: how to handle single or double quotes in the
3019 pattern string when the whole expression is between double quotes?
3020 POSIX.2 says that enclosing double quotes do not cause the pattern to
3021 be quoted, but does that leave us a problem with @ and array[@] and their
3022 expansions inside a pattern? */
3024 if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
3027 pat = string_extract_double_quoted (tword, &i, 1);
3033 /* expand_string_for_rhs () leaves WORD quoted and does not perform
3035 l = *tword ? expand_string_for_rhs (tword,
3036 (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
3037 (int *)NULL, (int *)NULL)
3040 pat = string_list (l);
3044 tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
3052 /* Handle removing a pattern from a string as a result of ${name%[%]value}
3053 or ${name#[#]value}. */
3055 variable_remove_pattern (value, pattern, patspec, quoted)
3056 char *value, *pattern;
3057 int patspec, quoted;
3061 tword = remove_pattern (value, pattern, patspec);
3068 list_remove_pattern (list, pattern, patspec, itype, quoted)
3071 int patspec, itype, quoted;
3077 for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
3079 tword = remove_pattern (l->word->word, pattern, patspec);
3080 w = make_bare_word (tword);
3082 new = make_word_list (w, new);
3085 l = REVERSE_LIST (new, WORD_LIST *);
3087 tword = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? string_list_dollar_star (l) : string_list (l);
3089 tword = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (l) : l);
3096 parameter_list_remove_pattern (itype, pattern, patspec, quoted)
3099 int patspec, quoted;
3104 list = list_rest_of_args ();
3106 return ((char *)NULL);
3107 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
3108 dispose_words (list);
3112 #if defined (ARRAY_VARS)
3114 array_remove_pattern (a, pattern, patspec, varname, quoted)
3118 char *varname; /* so we can figure out how it's indexed */
3126 /* compute itype from varname here */
3127 v = array_variable_part (varname, &ret, 0);
3130 list = array_to_word_list (a);
3132 return ((char *)NULL);
3133 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
3134 dispose_words (list);
3138 #endif /* ARRAY_VARS */
3141 parameter_brace_remove_pattern (varname, value, patstr, rtype, quoted)
3142 char *varname, *value, *patstr;
3146 char *temp1, *val, *pattern;
3150 return ((char *)NULL);
3152 this_command_name = varname;
3154 vtype = get_var_and_type (varname, value, &v, &val);
3156 return ((char *)NULL);
3158 patspec = getpatspec (rtype, patstr);
3159 if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
3162 pattern = getpattern (patstr, quoted, 1);
3164 temp1 = (char *)NULL; /* shut up gcc */
3168 case VT_ARRAYMEMBER:
3169 temp1 = remove_pattern (val, pattern, patspec);
3170 if (vtype == VT_VARIABLE)
3174 val = quote_escapes (temp1);
3179 #if defined (ARRAY_VARS)
3181 temp1 = array_remove_pattern (array_cell (v), pattern, patspec, varname, quoted);
3182 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
3184 val = quote_escapes (temp1);
3191 temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
3192 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
3194 val = quote_escapes (temp1);
3205 /*******************************************
3207 * Functions to expand WORD_DESCs *
3209 *******************************************/
3211 /* Expand WORD, performing word splitting on the result. This does
3212 parameter expansion, command substitution, arithmetic expansion,
3213 word splitting, and quote removal. */
3216 expand_word (word, quoted)
3220 WORD_LIST *result, *tresult;
3222 tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
3223 result = word_list_split (tresult);
3224 dispose_words (tresult);
3225 return (result ? dequote_list (result) : result);
3228 /* Expand WORD, but do not perform word splitting on the result. This
3229 does parameter expansion, command substitution, arithmetic expansion,
3230 and quote removal. */
3232 expand_word_unsplit (word, quoted)
3238 expand_no_split_dollar_star = 1;
3239 result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
3240 expand_no_split_dollar_star = 0;
3242 return (result ? dequote_list (result) : result);
3245 /* Perform shell expansions on WORD, but do not perform word splitting or
3246 quote removal on the result. */
3248 expand_word_leave_quoted (word, quoted)
3252 return (call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL));
3255 #if defined (PROCESS_SUBSTITUTION)
3257 /*****************************************************************/
3259 /* Hacking Process Substitution */
3261 /*****************************************************************/
3263 #if !defined (HAVE_DEV_FD)
3264 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
3265 of FIFOs the shell has open. unlink_fifo_list will walk the list and
3266 unlink all of them. add_fifo_list adds the name of an open FIFO to the
3267 list. NFIFO is a count of the number of FIFOs in the list. */
3268 #define FIFO_INCR 20
3275 static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
3277 static int fifo_list_size;
3280 add_fifo_list (pathname)
3283 if (nfifo >= fifo_list_size - 1)
3285 fifo_list_size += FIFO_INCR;
3286 fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
3287 fifo_list_size * sizeof (struct temp_fifo));
3290 fifo_list[nfifo].file = savestring (pathname);
3302 for (i = saved = 0; i < nfifo; i++)
3304 if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
3306 unlink (fifo_list[i].file);
3307 free (fifo_list[i].file);
3308 fifo_list[i].file = (char *)NULL;
3309 fifo_list[i].proc = -1;
3315 /* If we didn't remove some of the FIFOs, compact the list. */
3318 for (i = j = 0; i < nfifo; i++)
3319 if (fifo_list[i].file)
3321 fifo_list[j].file = fifo_list[i].file;
3322 fifo_list[j].proc = fifo_list[i].proc;
3336 tname = sh_mktmpname ("sh-np", MT_USERANDOM);
3337 if (mkfifo (tname, 0600) < 0)
3340 return ((char *)NULL);
3343 add_fifo_list (tname);
3347 #else /* HAVE_DEV_FD */
3349 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
3350 has open to children. NFDS is a count of the number of bits currently
3351 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
3353 static char *dev_fd_list = (char *)NULL;
3355 static int totfds; /* The highest possible number of open files. */
3361 if (!dev_fd_list || fd >= totfds)
3366 totfds = getdtablesize ();
3367 if (totfds < 0 || totfds > 256)
3372 dev_fd_list = (char *)xrealloc (dev_fd_list, totfds);
3373 memset (dev_fd_list + ofds, '\0', totfds - ofds);
3376 dev_fd_list[fd] = 1;
3388 for (i = 0; nfds && i < totfds; i++)
3399 #if defined (NOTDEF)
3400 print_dev_fd_list ()
3404 fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
3407 for (i = 0; i < totfds; i++)
3410 fprintf (stderr, " %d", i);
3412 fprintf (stderr, "\n");
3417 make_dev_fd_filename (fd)
3420 char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
3422 ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 4);
3424 strcpy (ret, DEV_FD_PREFIX);
3425 p = inttostr (fd, intbuf, sizeof (intbuf));
3426 strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
3432 #endif /* HAVE_DEV_FD */
3434 /* Return a filename that will open a connection to the process defined by
3435 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
3436 a filename in /dev/fd corresponding to a descriptor that is one of the
3437 ends of the pipe. If not defined, we use named pipes on systems that have
3438 them. Systems without /dev/fd and named pipes are out of luck.
3440 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
3441 use the read end of the pipe and dup that file descriptor to fd 0 in
3442 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
3443 writing or use the write end of the pipe in the child, and dup that
3444 file descriptor to fd 1 in the child. The parent does the opposite. */
3447 process_substitute (string, open_for_read_in_child)
3449 int open_for_read_in_child;
3454 #if defined (HAVE_DEV_FD)
3455 int parent_pipe_fd, child_pipe_fd;
3457 #endif /* HAVE_DEV_FD */
3458 #if defined (JOB_CONTROL)
3459 pid_t old_pipeline_pgrp;
3462 if (!string || !*string || wordexp_only)
3463 return ((char *)NULL);
3465 #if !defined (HAVE_DEV_FD)
3466 pathname = make_named_pipe ();
3467 #else /* HAVE_DEV_FD */
3468 if (pipe (fildes) < 0)
3470 sys_error ("cannot make pipe for process substitution");
3471 return ((char *)NULL);
3473 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
3474 the pipe in the parent, otherwise the read end. */
3475 parent_pipe_fd = fildes[open_for_read_in_child];
3476 child_pipe_fd = fildes[1 - open_for_read_in_child];
3477 /* Move the parent end of the pipe to some high file descriptor, to
3478 avoid clashes with FDs used by the script. */
3479 parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
3481 pathname = make_dev_fd_filename (parent_pipe_fd);
3482 #endif /* HAVE_DEV_FD */
3486 sys_error ("cannot make pipe for process substitution");
3487 return ((char *)NULL);
3490 old_pid = last_made_pid;
3492 #if defined (JOB_CONTROL)
3493 old_pipeline_pgrp = pipeline_pgrp;
3494 pipeline_pgrp = shell_pgrp;
3496 #endif /* JOB_CONTROL */
3498 pid = make_child ((char *)NULL, 1);
3501 reset_terminating_signals (); /* XXX */
3502 /* Cancel traps, in trap.c. */
3503 restore_original_signals ();
3504 setup_async_signals ();
3505 subshell_environment |= SUBSHELL_COMSUB;
3508 #if defined (JOB_CONTROL)
3509 set_sigchld_handler ();
3510 stop_making_children ();
3511 pipeline_pgrp = old_pipeline_pgrp;
3512 #endif /* JOB_CONTROL */
3516 sys_error ("cannot make child for process substitution");
3518 #if defined (HAVE_DEV_FD)
3519 close (parent_pipe_fd);
3520 close (child_pipe_fd);
3521 #endif /* HAVE_DEV_FD */
3522 return ((char *)NULL);
3527 #if defined (JOB_CONTROL)
3528 restore_pipeline (1);
3531 #if !defined (HAVE_DEV_FD)
3532 fifo_list[nfifo-1].proc = pid;
3535 last_made_pid = old_pid;
3537 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
3539 #endif /* JOB_CONTROL && PGRP_PIPE */
3541 #if defined (HAVE_DEV_FD)
3542 close (child_pipe_fd);
3543 #endif /* HAVE_DEV_FD */
3548 set_sigint_handler ();
3550 #if defined (JOB_CONTROL)
3551 set_job_control (0);
3552 #endif /* JOB_CONTROL */
3554 #if !defined (HAVE_DEV_FD)
3555 /* Open the named pipe in the child. */
3556 fd = open (pathname, open_for_read_in_child ? O_RDONLY|O_NONBLOCK : O_WRONLY);
3559 sys_error ("cannot open named pipe %s for %s", pathname,
3560 open_for_read_in_child ? "reading" : "writing");
3563 if (open_for_read_in_child)
3565 if (sh_unset_nodelay_mode (fd) < 0)
3567 sys_error ("cannout reset nodelay mode for fd %d", fd);
3571 #else /* HAVE_DEV_FD */
3573 #endif /* HAVE_DEV_FD */
3575 if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
3577 sys_error ("cannot duplicate named pipe %s as fd %d", pathname,
3578 open_for_read_in_child ? 0 : 1);
3582 if (fd != (open_for_read_in_child ? 0 : 1))
3585 /* Need to close any files that this process has open to pipes inherited
3587 if (current_fds_to_close)
3589 close_fd_bitmap (current_fds_to_close);
3590 current_fds_to_close = (struct fd_bitmap *)NULL;
3593 #if defined (HAVE_DEV_FD)
3594 /* Make sure we close the parent's end of the pipe and clear the slot
3595 in the fd list so it is not closed later, if reallocated by, for
3596 instance, pipe(2). */
3597 close (parent_pipe_fd);
3598 dev_fd_list[parent_pipe_fd] = 0;
3599 #endif /* HAVE_DEV_FD */
3601 result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
3603 #if !defined (HAVE_DEV_FD)
3604 /* Make sure we close the named pipe in the child before we exit. */
3605 close (open_for_read_in_child ? 0 : 1);
3606 #endif /* !HAVE_DEV_FD */
3611 #endif /* PROCESS_SUBSTITUTION */
3613 /***********************************/
3615 /* Command Substitution */
3617 /***********************************/
3620 read_comsub (fd, quoted)
3623 char *istring, buf[128], *bufp;
3624 int istring_index, istring_size, c;
3627 istring = (char *)NULL;
3628 istring_index = istring_size = bufn = 0;
3631 setmode (fd, O_TEXT); /* we don't want CR/LF, we want Unix-style */
3634 /* Read the output of the command through the pipe. */
3641 bufn = zread (fd, buf, sizeof (buf));
3651 internal_warning ("read_comsub: ignored null byte in input");
3656 /* Add the character to ISTRING, possibly after resizing it. */
3657 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
3659 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || c == CTLESC || c == CTLNUL)
3660 istring[istring_index++] = CTLESC;
3662 istring[istring_index++] = c;
3665 #if defined (__CYGWIN__)
3666 if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r')
3669 istring[istring_index - 1] = '\n';
3676 istring[istring_index] = '\0';
3678 /* If we read no output, just return now and save ourselves some
3680 if (istring_index == 0)
3683 return (char *)NULL;
3686 /* Strip trailing newlines from the output of the command. */
3687 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
3689 while (istring_index > 0)
3691 if (istring[istring_index - 1] == '\n')
3695 /* If the newline was quoted, remove the quoting char. */
3696 if (istring[istring_index - 1] == CTLESC)
3702 istring[istring_index] = '\0';
3705 strip_trailing (istring, istring_index - 1, 1);
3710 /* Perform command substitution on STRING. This returns a string,
3713 command_substitute (string, quoted)
3717 pid_t pid, old_pid, old_pipeline_pgrp;
3719 int result, fildes[2], function_value;
3721 istring = (char *)NULL;
3723 /* Don't fork () if there is no need to. In the case of no command to
3724 run, just return NULL. */
3725 if (!string || !*string || (string[0] == '\n' && !string[1]))
3726 return ((char *)NULL);
3728 if (wordexp_only && read_but_dont_execute)
3730 last_command_exit_value = 125;
3731 jump_to_top_level (EXITPROG);
3734 /* We're making the assumption here that the command substitution will
3735 eventually run a command from the file system. Since we'll run
3736 maybe_make_export_env in this subshell before executing that command,
3737 the parent shell and any other shells it starts will have to remake
3738 the environment. If we make it before we fork, other shells won't
3739 have to. Don't bother if we have any temporary variable assignments,
3740 though, because the export environment will be remade after this
3741 command completes anyway, but do it if all the words to be expanded
3742 are variable assignments. */
3743 if (subst_assign_varlist == 0 || garglist == 0)
3744 maybe_make_export_env (); /* XXX */
3746 /* Pipe the output of executing STRING into the current shell. */
3747 if (pipe (fildes) < 0)
3749 sys_error ("cannot make pipe for command substitution");
3753 old_pid = last_made_pid;
3754 #if defined (JOB_CONTROL)
3755 old_pipeline_pgrp = pipeline_pgrp;
3756 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
3757 if ((subshell_environment & SUBSHELL_PIPE) == 0)
3758 pipeline_pgrp = shell_pgrp;
3759 cleanup_the_pipeline ();
3762 pid = make_child ((char *)NULL, 0);
3764 /* Reset the signal handlers in the child, but don't free the
3766 reset_signal_handlers ();
3768 #if defined (JOB_CONTROL)
3769 set_sigchld_handler ();
3770 stop_making_children ();
3771 pipeline_pgrp = old_pipeline_pgrp;
3773 stop_making_children ();
3774 #endif /* JOB_CONTROL */
3778 sys_error ("cannot make child for command substitution");
3784 return ((char *)NULL);
3789 set_sigint_handler (); /* XXX */
3791 if (dup2 (fildes[1], 1) < 0)
3793 sys_error ("command_substitute: cannot duplicate pipe as fd 1");
3794 exit (EXECUTION_FAILURE);
3797 /* If standard output is closed in the parent shell
3798 (such as after `exec >&-'), file descriptor 1 will be
3799 the lowest available file descriptor, and end up in
3800 fildes[0]. This can happen for stdin and stderr as well,
3801 but stdout is more important -- it will cause no output
3802 to be generated from this command. */
3803 if ((fildes[1] != fileno (stdin)) &&
3804 (fildes[1] != fileno (stdout)) &&
3805 (fildes[1] != fileno (stderr)))
3808 if ((fildes[0] != fileno (stdin)) &&
3809 (fildes[0] != fileno (stdout)) &&
3810 (fildes[0] != fileno (stderr)))
3813 /* The currently executing shell is not interactive. */
3816 /* This is a subshell environment. */
3817 subshell_environment |= SUBSHELL_COMSUB;
3819 /* When not in POSIX mode, command substitution does not inherit
3821 if (posixly_correct == 0)
3822 exit_immediately_on_error = 0;
3824 remove_quoted_escapes (string);
3826 startup_state = 2; /* see if we can avoid a fork */
3827 /* Give command substitution a place to jump back to on failure,
3828 so we don't go back up to main (). */
3829 result = setjmp (top_level);
3831 /* If we're running a command substitution inside a shell function,
3832 trap `return' so we don't return from the function in the subshell
3833 and go off to never-never land. */
3834 if (result == 0 && return_catch_flag)
3835 function_value = setjmp (return_catch);
3839 if (result == EXITPROG)
3840 exit (last_command_exit_value);
3842 exit (EXECUTION_FAILURE);
3843 else if (function_value)
3844 exit (return_catch_value);
3846 exit (parse_and_execute (string, "command substitution", SEVAL_NOHIST));
3850 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
3852 #endif /* JOB_CONTROL && PGRP_PIPE */
3856 istring = read_comsub (fildes[0], quoted);
3860 current_command_subst_pid = pid;
3861 last_command_exit_value = wait_for (pid);
3862 last_command_subst_pid = pid;
3863 last_made_pid = old_pid;
3865 #if defined (JOB_CONTROL)
3866 /* If last_command_exit_value > 128, then the substituted command
3867 was terminated by a signal. If that signal was SIGINT, then send
3868 SIGINT to ourselves. This will break out of loops, for instance. */
3869 if (last_command_exit_value == (128 + SIGINT))
3870 kill (getpid (), SIGINT);
3872 /* wait_for gives the terminal back to shell_pgrp. If some other
3873 process group should have it, give it away to that group here.
3874 pipeline_pgrp is non-zero only while we are constructing a
3875 pipline, so what we are concerned about is whether or not that
3876 pipeline was started in the background. A pipeline started in
3877 the background should never get the tty back here. */
3879 if (interactive && pipeline_pgrp != (pid_t)0 && pipeline_pgrp != last_asynchronous_pid)
3881 if (interactive && pipeline_pgrp != (pid_t)0 && (subshell_environment & SUBSHELL_ASYNC) == 0)
3883 give_terminal_to (pipeline_pgrp, 0);
3884 #endif /* JOB_CONTROL */
3890 /********************************************************
3892 * Utility functions for parameter expansion *
3894 ********************************************************/
3896 #if defined (ARRAY_VARS)
3899 array_length_reference (s)
3908 var = array_variable_part (s, &t, &len);
3910 /* If unbound variables should generate an error, report one and return
3912 if ((var == 0 || array_p (var) == 0) && unbound_vars_is_error)
3923 /* We support a couple of expansions for variables that are not arrays.
3924 We'll return the length of the value for v[0], and 1 for v[@] or
3925 v[*]. Return 0 for everything else. */
3927 array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
3929 if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
3930 return (array_p (var) ? array_num_elements (array) : 1);
3932 ind = array_expand_index (t, len);
3935 err_badarraysub (t);
3940 t = array_reference (array, ind);
3942 t = (ind == 0) ? value_cell (var) : (char *)NULL;
3947 #endif /* ARRAY_VARS */
3950 valid_brace_expansion_word (name, var_is_special)
3954 if (DIGIT (*name) && all_digits (name))
3956 else if (var_is_special)
3958 #if defined (ARRAY_VARS)
3959 else if (valid_array_reference (name))
3961 #endif /* ARRAY_VARS */
3962 else if (legal_identifier (name))
3968 /* Parameter expand NAME, and return a new string which is the expansion,
3969 or NULL if there was no expansion.
3970 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
3971 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
3972 NAME was found inside of a double-quoted expression. */
3974 parameter_brace_expand_word (name, var_is_special, quoted)
3976 int var_is_special, quoted;
3983 /* Handle multiple digit arguments, as in ${11}. */
3985 if (legal_number (name, &arg_index))
3987 tt = get_dollar_var_value (arg_index);
3988 temp = tt ? quote_escapes (tt) : (char *)NULL;
3991 else if (var_is_special) /* ${@} */
3994 tt = (char *)xmalloc (2 + strlen (name));
3995 tt[sindex = 0] = '$';
3996 strcpy (tt + 1, name);
3998 temp = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
3999 (int *)NULL, (int *)NULL, 0);
4002 #if defined (ARRAY_VARS)
4003 else if (valid_array_reference (name))
4005 temp = array_value (name, quoted, &atype);
4006 if (atype == 0 && temp)
4007 temp = quote_escapes (temp);
4010 else if (var = find_variable (name))
4012 if (var_isset (var) && invisible_p (var) == 0)
4014 #if defined (ARRAY_VARS)
4015 temp = array_p (var) ? array_reference (array_cell (var), 0) : value_cell (var);
4017 temp = value_cell (var);
4021 temp = quote_escapes (temp);
4024 temp = (char *)NULL;
4027 temp = (char *)NULL;
4032 /* Expand an indirect reference to a variable: ${!NAME} expands to the
4033 value of the variable whose name is the value of NAME. */
4035 parameter_brace_expand_indir (name, var_is_special, quoted)
4037 int var_is_special, quoted;
4041 t = parameter_brace_expand_word (name, var_is_special, quoted);
4044 temp = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted);
4049 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
4050 depending on the value of C, the separating character. C can be one of
4051 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
4052 between double quotes. */
4054 parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat)
4056 int c, quoted, *qdollaratp, *hasdollarat;
4059 char *t, *t1, *temp;
4062 /* XXX - Should we tilde expand in an assignment context if C is `='? */
4064 temp = bash_tilde_expand (value, 0);
4065 else if (xstrchr (value, '~') && unquoted_substring ("=~", value))
4066 temp = bash_tilde_expand (value, 1);
4068 temp = savestring (value);
4070 /* If the entire expression is between double quotes, we want to treat
4071 the value as a double-quoted string, with the exception that we strip
4072 embedded unescaped double quotes. */
4073 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *temp)
4076 t = string_extract_double_quoted (temp, &hasdol, 1);
4082 /* XXX was 0 not quoted */
4083 l = *temp ? expand_string_for_rhs (temp, quoted, &hasdol, (int *)NULL)
4086 *hasdollarat = hasdol || (l && l->next);
4090 /* The expansion of TEMP returned something. We need to treat things
4091 slightly differently if HASDOL is non-zero. */
4092 temp = string_list (l);
4093 /* If l->next is not null, we know that TEMP contained "$@", since that
4094 is the only expansion that creates more than one word. */
4095 if ((hasdol && quoted) || l->next)
4099 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && hasdol)
4101 /* The brace expansion occurred between double quotes and there was
4102 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
4103 it does not expand to anything. In this case, we want to return
4104 a quoted empty string. */
4105 temp = (char *)xmalloc (2);
4110 temp = (char *)NULL;
4112 if (c == '-' || c == '+')
4116 t = temp ? savestring (temp) : savestring ("");
4117 t1 = dequote_string (t);
4119 bind_variable (name, t1);
4124 /* Deal with the right hand side of a ${name:?value} expansion in the case
4125 that NAME is null or not set. If VALUE is non-null it is expanded and
4126 used as the error message to print, otherwise a standard message is
4129 parameter_brace_expand_error (name, value)
4135 if (value && *value)
4138 temp = bash_tilde_expand (value, 0);
4139 else if (xstrchr (value, '~') && unquoted_substring ("=~", value))
4140 temp = bash_tilde_expand (value, 1);
4142 temp = savestring (value);
4144 l = expand_string (temp, 0);
4146 temp = string_list (l);
4147 report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */
4152 report_error ("%s: parameter null or not set", name);
4154 /* Free the data we have allocated during this expansion, since we
4155 are about to longjmp out. */
4160 /* Return 1 if NAME is something for which parameter_brace_expand_length is
4163 valid_length_expression (name)
4166 return (name[1] == '\0' || /* ${#} */
4167 ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */
4168 (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */
4169 #if defined (ARRAY_VARS)
4170 valid_array_reference (name + 1) || /* ${#a[7]} */
4172 legal_identifier (name + 1)); /* ${#PS1} */
4175 /* Handle the parameter brace expansion that requires us to return the
4176 length of a parameter. */
4178 parameter_brace_expand_length (name)
4182 intmax_t number, arg_index;
4184 #if defined (ARRAY_VARS)
4188 if (name[1] == '\0') /* ${#} */
4189 number = number_of_args ();
4190 else if ((name[1] == '@' || name[1] == '*') && name[2] == '\0') /* ${#@}, ${#*} */
4191 number = number_of_args ();
4192 else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
4194 /* Take the lengths of some of the shell's special parameters. */
4198 t = which_set_flags ();
4201 t = itos (last_command_exit_value);
4204 t = itos (dollar_dollar_pid);
4207 if (last_asynchronous_pid == NO_PID)
4210 t = itos (last_asynchronous_pid);
4213 t = itos (number_of_args ());
4216 number = STRLEN (t);
4219 #if defined (ARRAY_VARS)
4220 else if (valid_array_reference (name + 1))
4221 number = array_length_reference (name + 1);
4222 #endif /* ARRAY_VARS */
4227 if (legal_number (name + 1, &arg_index)) /* ${#1} */
4229 t = get_dollar_var_value (arg_index);
4230 number = STRLEN (t);
4233 #if defined (ARRAY_VARS)
4234 else if ((var = find_variable (name + 1)) && array_p (var))
4236 t = array_reference (array_cell (var), 0);
4237 number = STRLEN (t);
4242 newname = savestring (name);
4244 list = expand_string (newname, Q_DOUBLE_QUOTES);
4245 t = list ? string_list (list) : (char *)NULL;
4248 dispose_words (list);
4250 number = STRLEN (t);
4258 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
4259 so we do some ad-hoc parsing of an arithmetic expression to find
4260 the first DELIM, instead of using strchr(3). Two rules:
4261 1. If the substring contains a `(', read until closing `)'.
4262 2. If the substring contains a `?', read past one `:' for each `?'.
4266 skiparith (substr, delim)
4271 int skipcol, pcount, i;
4274 sublen = strlen (substr);
4275 i = skipcol = pcount = 0;
4278 /* Balance parens */
4279 if (substr[i] == LPAREN)
4285 if (substr[i] == RPAREN && pcount)
4293 ADVANCE_CHAR (substr, sublen, i);
4297 /* Skip one `:' for each `?' */
4298 if (substr[i] == ':' && skipcol)
4304 if (substr[i] == delim)
4306 if (substr[i] == '?')
4312 ADVANCE_CHAR (substr, sublen, i);
4315 return (substr + i);
4318 /* Verify and limit the start and end of the desired substring. If
4319 VTYPE == 0, a regular shell variable is being used; if it is 1,
4320 then the positional parameters are being used; if it is 2, then
4321 VALUE is really a pointer to an array variable that should be used.
4322 Return value is 1 if both values were OK, 0 if there was a problem
4323 with an invalid expression, or -1 if the values were out of range. */
4325 verify_substring_values (value, substr, vtype, e1p, e2p)
4326 char *value, *substr;
4328 intmax_t *e1p, *e2p;
4330 char *t, *temp1, *temp2;
4333 #if defined (ARRAY_VARS)
4337 /* duplicate behavior of strchr(3) */
4338 t = skiparith (substr, ':');
4339 if (*t && *t == ':')
4344 temp1 = expand_string_if_necessary (substr, Q_DOUBLE_QUOTES, expand_string);
4345 *e1p = evalexp (temp1, &expok);
4350 len = -1; /* paranoia */
4354 case VT_ARRAYMEMBER:
4355 len = strlen (value);
4358 len = number_of_args () + 1;
4360 #if defined (ARRAY_VARS)
4363 len = array_num_elements (a) + 1;
4368 if (len == -1) /* paranoia */
4371 if (*e1p < 0) /* negative offsets count from end */
4374 if (*e1p >= len || *e1p < 0)
4380 temp2 = savestring (t);
4381 temp1 = expand_string_if_necessary (temp2, Q_DOUBLE_QUOTES, expand_string);
4384 *e2p = evalexp (temp1, &expok);
4390 internal_error ("%s: substring expression < 0", t);
4393 *e2p += *e1p; /* want E2 chars starting at E1 */
4403 /* Return the type of variable specified by VARNAME (simple variable,
4404 positional param, or array variable). Also return the value specified
4405 by VARNAME (value of a variable or a reference to an array element).
4406 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
4407 characters in the value are quoted with CTLESC and takes appropriate
4408 steps. For convenience, *VALP is set to the dequoted VALUE. */
4410 get_var_and_type (varname, value, varp, valp)
4411 char *varname, *value;
4417 #if defined (ARRAY_VARS)
4421 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
4422 vtype = (varname[0] == '@' || varname[0] == '*') && varname[1] == '\0';
4423 *varp = (SHELL_VAR *)NULL;
4425 #if defined (ARRAY_VARS)
4426 if (valid_array_reference (varname))
4428 v = array_variable_part (varname, &temp, (int *)0);
4429 if (v && array_p (v))
4431 if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')
4433 vtype = VT_ARRAYVAR;
4434 *valp = (char *)array_cell (v);
4438 vtype = VT_ARRAYMEMBER;
4439 *valp = array_value (varname, 1, (int *)NULL);
4446 else if ((v = find_variable (varname)) && array_p (v))
4448 vtype = VT_ARRAYMEMBER;
4450 *valp = array_reference (array_cell (v), 0);
4454 *valp = (value && vtype == VT_VARIABLE) ? dequote_escapes (value) : value;
4459 /******************************************************/
4461 /* Functions to extract substrings of variable values */
4463 /******************************************************/
4465 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
4466 is `@', use the positional parameters; otherwise, use the value of
4467 VARNAME. If VARNAME is an array variable, use the array elements. */
4470 parameter_brace_substring (varname, value, substr, quoted)
4471 char *varname, *value, *substr;
4476 char *temp, *val, *tt;
4480 return ((char *)NULL);
4482 this_command_name = varname;
4484 vtype = get_var_and_type (varname, value, &v, &val);
4486 return ((char *)NULL);
4488 r = verify_substring_values (val, substr, vtype, &e1, &e2);
4490 return ((r == 0) ? &expand_param_error : (char *)NULL);
4495 case VT_ARRAYMEMBER:
4496 tt = substring (val, e1, e2);
4497 if (vtype == VT_VARIABLE)
4499 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
4500 temp = quote_string (tt);
4502 temp = tt ? quote_escapes (tt) : (char *)NULL;
4506 tt = pos_params (varname, e1, e2, quoted);
4507 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
4509 temp = tt ? quote_escapes (tt) : (char *)NULL;
4515 #if defined (ARRAY_VARS)
4517 tt = array_subrange (array_cell (v), e1, e2, quoted);
4518 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
4520 temp = tt ? quote_escapes (tt) : (char *)NULL;
4528 temp = (char *)NULL;
4534 /****************************************************************/
4536 /* Functions to perform pattern substitution on variable values */
4538 /****************************************************************/
4541 pat_subst (string, pat, rep, mflags)
4542 char *string, *pat, *rep;
4545 char *ret, *s, *e, *str;
4546 int rsize, rptr, l, replen, mtype;
4548 mtype = mflags & MATCH_TYPEMASK;
4551 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
4552 * with REP and return the result.
4553 * 2. A null pattern with mtype == MATCH_END means to append REP to
4554 * STRING and return the result.
4556 if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
4558 replen = STRLEN (rep);
4559 l = strlen (string);
4560 ret = (char *)xmalloc (replen + l + 2);
4562 strcpy (ret, string);
4563 else if (mtype == MATCH_BEG)
4566 strcpy (ret + replen, string);
4570 strcpy (ret, string);
4571 strcpy (ret + l, rep);
4576 ret = (char *)xmalloc (rsize = 64);
4579 for (replen = STRLEN (rep), rptr = 0, str = string;;)
4581 if (match_pattern (str, pat, mtype, &s, &e) == 0)
4584 RESIZE_MALLOCED_BUFFER (ret, rptr, (l + replen), rsize, 64);
4586 /* OK, now copy the leading unmatched portion of the string (from
4587 str to s) to ret starting at rptr (the current offset). Then copy
4588 the replacement string at ret + rptr + (s - str). Increment
4589 rptr (if necessary) and str and go on. */
4592 strncpy (ret + rptr, str, l);
4597 strncpy (ret + rptr, rep, replen);
4601 e++; /* avoid infinite recursion on zero-length match */
4602 str = e; /* e == end of match */
4603 if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
4607 /* Now copy the unmatched portion of the input string */
4610 RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
4611 strcpy (ret + rptr, str);
4619 /* Do pattern match and replacement on the positional parameters. */
4621 pos_params_pat_subst (string, pat, rep, mflags)
4622 char *string, *pat, *rep;
4625 WORD_LIST *save, *params;
4629 save = params = list_rest_of_args ();
4631 return ((char *)NULL);
4633 for ( ; params; params = params->next)
4635 ret = pat_subst (params->word->word, pat, rep, mflags);
4636 w = make_bare_word (ret);
4637 dispose_word (params->word);
4642 ret = string_list ((mflags & MATCH_QUOTED) ? quote_list (save) : save);
4643 dispose_words (save);
4648 /* Perform pattern substitution on VALUE, which is the expansion of
4649 VARNAME. PATSUB is an expression supplying the pattern to match
4650 and the string to substitute. QUOTED is a flags word containing
4651 the type of quoting currently in effect. */
4653 parameter_brace_patsub (varname, value, patsub, quoted)
4654 char *varname, *value, *patsub;
4658 char *val, *temp, *pat, *rep, *p, *lpatsub, *tt;
4662 return ((char *)NULL);
4664 this_command_name = varname;
4666 vtype = get_var_and_type (varname, value, &v, &val);
4668 return ((char *)NULL);
4673 mflags |= MATCH_GLOBREP;
4677 /* Malloc this because expand_string_if_necessary or one of the expansion
4678 functions in its call chain may free it on a substitution error. */
4679 lpatsub = savestring (patsub);
4681 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
4682 mflags |= MATCH_QUOTED;
4684 if (rep = quoted_strchr (lpatsub, '/', ST_BACKSL))
4689 if (rep && *rep == '\0')
4692 /* Expand PAT and REP for command, variable and parameter, arithmetic,
4693 and process substitution. Also perform quote removal. Do not
4694 perform word splitting or filename generation. */
4695 pat = expand_string_if_necessary (lpatsub, (quoted & ~Q_DOUBLE_QUOTES), expand_string_unsplit);
4699 if ((mflags & MATCH_QUOTED) == 0)
4700 rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
4702 rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
4706 if (pat && pat[0] == '#')
4708 mflags |= MATCH_BEG;
4711 else if (pat && pat[0] == '%')
4713 mflags |= MATCH_END;
4717 mflags |= MATCH_ANY;
4719 /* OK, we now want to substitute REP for PAT in VAL. If
4720 flags & MATCH_GLOBREP is non-zero, the substitution is done
4721 everywhere, otherwise only the first occurrence of PAT is
4722 replaced. The pattern matching code doesn't understand
4723 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
4724 values passed in (VT_VARIABLE) so the pattern substitution
4725 code works right. We need to requote special chars after
4726 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
4727 other cases if QUOTED == 0, since the posparams and arrays
4728 indexed by * or @ do special things when QUOTED != 0. */
4733 case VT_ARRAYMEMBER:
4734 temp = pat_subst (val, p, rep, mflags);
4735 if (vtype == VT_VARIABLE)
4739 tt = quote_escapes (temp);
4745 temp = pos_params_pat_subst (val, p, rep, mflags);
4746 if (temp && (mflags & MATCH_QUOTED) == 0)
4748 tt = quote_escapes (temp);
4753 #if defined (ARRAY_VARS)
4755 temp = array_patsub (array_cell (v), p, rep, mflags);
4756 if (temp && (mflags & MATCH_QUOTED) == 0)
4758 tt = quote_escapes (temp);
4773 /****************************************************************/
4775 /* Functions to perform parameter expansion on a string */
4777 /****************************************************************/
4779 /* ${[#][!]name[[:]#[#]%[%]-=?+[word][:e1[:e2]]]} */
4781 parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_dollar_at)
4783 int *indexp, quoted, *quoted_dollar_atp, *contains_dollar_at;
4785 int check_nullness, var_is_set, var_is_null, var_is_special;
4786 int want_substring, want_indir, want_patsub;
4787 char *name, *value, *temp, *temp1;
4788 int t_index, sindex, c;
4791 value = (char *)NULL;
4792 var_is_set = var_is_null = var_is_special = check_nullness = 0;
4793 want_substring = want_indir = want_patsub = 0;
4797 name = string_extract (string, &t_index, "#%:-=?+/}", EX_VARNAME);
4799 /* If the name really consists of a special variable, then make sure
4800 that we have the entire name. We don't allow indirect references
4801 to special variables except `#', `?', `@' and `*'. */
4802 if ((sindex == t_index &&
4803 (string[t_index] == '-' ||
4804 string[t_index] == '?' ||
4805 string[t_index] == '#')) ||
4806 (sindex == t_index - 1 && string[sindex] == '!' &&
4807 (string[t_index] == '#' ||
4808 string[t_index] == '?' ||
4809 string[t_index] == '@' ||
4810 string[t_index] == '*')))
4814 temp1 = string_extract (string, &t_index, "#%:-=?+/}", 0);
4815 name = (char *)xmalloc (3 + (strlen (temp1)));
4816 *name = string[sindex];
4817 if (string[sindex] == '!')
4819 /* indirect reference of $#, $?, $@, or $* */
4820 name[1] = string[sindex + 1];
4821 strcpy (name + 2, temp1);
4824 strcpy (name + 1, temp1);
4829 /* Find out what character ended the variable name. Then
4830 do the appropriate thing. */
4831 if (c = string[sindex])
4834 /* If c is followed by one of the valid parameter expansion
4835 characters, move past it as normal. If not, assume that
4836 a substring specification is being given, and do not move
4838 if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
4841 if (c = string[sindex])
4844 else if (c == ':' && string[sindex] != RBRACE)
4846 else if (c == '/' && string[sindex] != RBRACE)
4849 /* Catch the valid and invalid brace expressions that made it through the
4851 /* ${#-} is a valid expansion and means to take the length of $-.
4852 Similarly for ${#?} and ${##}... */
4853 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
4854 VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
4856 name = (char *)xrealloc (name, 3);
4859 c = string[sindex++];
4862 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
4863 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
4864 member (c, "%:=+/") && string[sindex] == RBRACE)
4866 temp = (char *)NULL;
4867 goto bad_substitution;
4870 /* Indirect expansion begins with a `!'. A valid indirect expansion is
4871 either a variable name, one of the positional parameters or a special
4872 variable that expands to one of the positional parameters. */
4873 want_indir = *name == '!' &&
4874 (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
4875 || VALID_INDIR_PARAM (name[1]));
4877 /* Determine the value of this variable. */
4879 /* Check for special variables, directly referenced. */
4880 if (SPECIAL_VAR (name, want_indir))
4883 /* Check for special expansion things, like the length of a parameter */
4884 if (*name == '#' && name[1])
4886 /* If we are not pointing at the character just after the
4887 closing brace, then we haven't gotten all of the name.
4888 Since it begins with a special character, this is a bad
4889 substitution. Also check NAME for validity before trying
4891 if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
4893 temp = (char *)NULL;
4894 goto bad_substitution;
4897 number = parameter_brace_expand_length (name);
4901 return ((number < 0) ? &expand_param_error : itos (number));
4904 /* ${@} is identical to $@. */
4905 if (name[0] == '@' && name[1] == '\0')
4907 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
4908 *quoted_dollar_atp = 1;
4910 if (contains_dollar_at)
4911 *contains_dollar_at = 1;
4914 /* Process ${PREFIX*} expansion. */
4915 if (want_indir && string[sindex - 1] == RBRACE &&
4916 (string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
4917 legal_variable_starter ((unsigned char) name[1]))
4922 temp1 = savestring (name + 1);
4923 number = strlen (temp1);
4924 temp1[number - 1] = '\0';
4925 x = all_variables_matching_prefix (temp1);
4926 xlist = strvec_to_word_list (x, 0, 0);
4927 if (string[sindex - 2] == '*')
4928 temp = string_list_dollar_star (xlist);
4931 temp = string_list_dollar_at (xlist, quoted);
4932 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
4933 *quoted_dollar_atp = 1;
4934 if (contains_dollar_at)
4935 *contains_dollar_at = 1;
4944 /* Make sure that NAME is valid before trying to go on. */
4945 if (valid_brace_expansion_word (want_indir ? name + 1 : name,
4946 var_is_special) == 0)
4948 temp = (char *)NULL;
4949 goto bad_substitution;
4953 temp = parameter_brace_expand_indir (name + 1, var_is_special, quoted);
4955 temp = parameter_brace_expand_word (name, var_is_special, quoted);
4957 #if defined (ARRAY_VARS)
4958 if (valid_array_reference (name))
4960 temp1 = xstrchr (name, '[');
4961 if (temp1 && temp1[1] == '@' && temp1[2] == ']')
4963 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
4964 *quoted_dollar_atp = 1;
4965 if (contains_dollar_at)
4966 *contains_dollar_at = 1;
4968 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
4969 which should result in separate words even when IFS is unset. */
4970 if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0)
4972 if (contains_dollar_at)
4973 *contains_dollar_at = 1;
4978 var_is_set = temp != (char *)0;
4979 var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
4981 /* Get the rest of the stuff inside the braces. */
4982 if (c && c != RBRACE)
4984 /* Extract the contents of the ${ ... } expansion
4985 according to the Posix.2 rules. */
4986 value = extract_dollar_brace_string (string, &sindex, quoted, 0);
4987 if (string[sindex] == RBRACE)
4990 goto bad_substitution;
4993 value = (char *)NULL;
4997 /* If this is a substring spec, process it and add the result. */
5000 temp1 = parameter_brace_substring (name, temp, value, quoted);
5006 else if (want_patsub)
5008 temp1 = parameter_brace_patsub (name, temp, value, quoted);
5015 /* Do the right thing based on which character ended the variable name. */
5021 report_error ("%s: bad substitution", string ? string : "??");
5025 return &expand_param_error;
5028 if (var_is_set == 0 && unbound_vars_is_error)
5030 err_unboundvar (name);
5034 last_command_exit_value = EXECUTION_FAILURE;
5035 return (interactive_shell ? &expand_param_error : &expand_param_fatal);
5039 case '#': /* ${param#[#]pattern} */
5040 case '%': /* ${param%[%]pattern} */
5041 if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
5046 temp1 = parameter_brace_remove_pattern (name, temp, value, c, quoted);
5056 if (var_is_set && var_is_null == 0)
5058 /* If the operator is `+', we don't want the value of the named
5059 variable for anything, just the value of the right hand side. */
5063 /* XXX -- if we're double-quoted and the named variable is "$@",
5064 we want to turn off any special handling of "$@" --
5065 we're not using it, so whatever is on the rhs applies. */
5066 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
5067 *quoted_dollar_atp = 0;
5068 if (contains_dollar_at)
5069 *contains_dollar_at = 0;
5074 temp = parameter_brace_expand_rhs (name, value, c,
5077 contains_dollar_at);
5081 temp = (char *)NULL;
5087 /* Otherwise do nothing; just use the value in TEMP. */
5089 else /* VAR not set or VAR is NULL. */
5092 temp = (char *)NULL;
5093 if (c == '=' && var_is_special)
5095 report_error ("$%s: cannot assign in this way", name);
5098 return &expand_param_error;
5102 parameter_brace_expand_error (name, value);
5103 return (interactive_shell ? &expand_param_error : &expand_param_fatal);
5107 /* XXX -- if we're double-quoted and the named variable is "$@",
5108 we want to turn off any special handling of "$@" --
5109 we're not using it, so whatever is on the rhs applies. */
5110 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
5111 *quoted_dollar_atp = 0;
5112 if (contains_dollar_at)
5113 *contains_dollar_at = 0;
5115 temp = parameter_brace_expand_rhs (name, value, c, quoted,
5117 contains_dollar_at);
5128 /* Expand a single ${xxx} expansion. The braces are optional. When
5129 the braces are used, parameter_brace_expand() does the work,
5130 possibly calling param_expand recursively. */
5132 param_expand (string, sindex, quoted, expanded_something,
5133 contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
5136 int *sindex, quoted, *expanded_something, *contains_dollar_at;
5137 int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
5139 char *temp, *temp1, uerror[3];
5140 int zindex, t_index, expok;
5147 c = string[++zindex];
5149 temp = (char *)NULL;
5151 /* Do simple cases first. Switch on what follows '$'. */
5165 temp1 = dollar_vars[TODIGIT (c)];
5166 if (unbound_vars_is_error && temp1 == (char *)NULL)
5171 err_unboundvar (uerror);
5172 last_command_exit_value = EXECUTION_FAILURE;
5173 return (interactive_shell ? &expand_param_error : &expand_param_fatal);
5175 temp = temp1 ? quote_escapes (temp1) : (char *)NULL;
5178 /* $$ -- pid of the invoking shell. */
5180 temp = itos (dollar_dollar_pid);
5183 /* $# -- number of positional parameters. */
5185 temp = itos (number_of_args ());
5188 /* $? -- return value of the last synchronous command. */
5190 temp = itos (last_command_exit_value);
5193 /* $- -- flags supplied to the shell on invocation or by `set'. */
5195 temp = which_set_flags ();
5198 /* $! -- Pid of the last asynchronous command. */
5200 /* If no asynchronous pids have been created, expand to nothing.
5201 If `set -u' has been executed, and no async processes have
5202 been created, this is an expansion error. */
5203 if (last_asynchronous_pid == NO_PID)
5205 if (expanded_something)
5206 *expanded_something = 0;
5207 temp = (char *)NULL;
5208 if (unbound_vars_is_error)
5213 err_unboundvar (uerror);
5214 last_command_exit_value = EXECUTION_FAILURE;
5215 return (interactive_shell ? &expand_param_error : &expand_param_fatal);
5219 temp = itos (last_asynchronous_pid);
5222 /* The only difference between this and $@ is when the arg is quoted. */
5223 case '*': /* `$*' */
5224 list = list_rest_of_args ();
5226 /* If there are no command-line arguments, this should just
5227 disappear if there are other characters in the expansion,
5228 even if it's quoted. */
5229 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
5230 temp = (char *)NULL;
5231 else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
5233 /* If we have "$*" we want to make a string of the positional
5234 parameters, separated by the first character of $IFS, and
5235 quote the whole string, including the separators. If IFS
5236 is unset, the parameters are separated by ' '; if $IFS is
5237 null, the parameters are concatenated. */
5238 temp = string_list_dollar_star (list);
5239 temp1 = quote_string (temp);
5245 /* If the $* is not quoted it is identical to $@ */
5246 temp = string_list_dollar_at (list, quoted);
5247 if (expand_no_split_dollar_star == 0 && contains_dollar_at)
5248 *contains_dollar_at = 1;
5251 dispose_words (list);
5254 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
5255 means that we have to turn quoting off after we split into
5256 the individually quoted arguments so that the final split
5257 on the first character of $IFS is still done. */
5258 case '@': /* `$@' */
5259 list = list_rest_of_args ();
5261 /* We want to flag the fact that we saw this. We can't turn
5262 off quoting entirely, because other characters in the
5263 string might need it (consider "\"$@\""), but we need some
5264 way to signal that the final split on the first character
5265 of $IFS should be done, even though QUOTED is 1. */
5266 if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
5267 *quoted_dollar_at_p = 1;
5268 if (contains_dollar_at)
5269 *contains_dollar_at = 1;
5271 /* We want to separate the positional parameters with the first
5272 character of $IFS in case $IFS is something other than a space.
5273 We also want to make sure that splitting is done no matter what --
5274 according to POSIX.2, this expands to a list of the positional
5275 parameters no matter what IFS is set to. */
5276 temp = string_list_dollar_at (list, quoted);
5278 dispose_words (list);
5282 temp = parameter_brace_expand (string, &zindex, quoted,
5284 contains_dollar_at);
5285 if (temp == &expand_param_error || temp == &expand_param_fatal)
5289 /* Quoted nulls should be removed if there is anything else
5291 /* Note that we saw the quoted null so we can add one back at
5292 the end of this function if there are no other characters
5293 in the string, discard TEMP, and go on. The exception to
5294 this is when we have "${@}" and $1 is '', since $@ needs
5295 special handling. */
5296 if (temp && QUOTED_NULL (temp))
5298 if (had_quoted_null_p)
5299 *had_quoted_null_p = 1;
5300 if (*quoted_dollar_at_p == 0)
5303 temp = (char *)NULL;
5310 /* Do command or arithmetic substitution. */
5312 /* We have to extract the contents of this paren substitution. */
5313 t_index = zindex + 1;
5314 temp = extract_command_subst (string, &t_index);
5317 /* For Posix.2-style `$(( ))' arithmetic substitution,
5318 extract the expression and pass it to the evaluator. */
5319 if (temp && *temp == LPAREN)
5323 temp2 = savestring (temp1);
5324 t_index = strlen (temp2) - 1;
5326 if (temp2[t_index] != RPAREN)
5332 /* Cut off ending `)' */
5333 temp2[t_index] = '\0';
5335 /* Expand variables found inside the expression. */
5336 temp1 = expand_string_if_necessary (temp2, Q_DOUBLE_QUOTES, expand_string);
5340 /* No error messages. */
5341 this_command_name = (char *)NULL;
5342 number = evalexp (temp1, &expok);
5347 if (interactive_shell == 0 && posixly_correct)
5349 last_command_exit_value = EXECUTION_FAILURE;
5350 return (&expand_param_fatal);
5353 return (&expand_param_error);
5355 temp = itos (number);
5360 temp1 = command_substitute (temp, quoted);
5365 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
5366 away in a future bash release. */
5368 /* Extract the contents of this arithmetic substitution. */
5369 t_index = zindex + 1;
5370 temp = extract_arithmetic_subst (string, &t_index);
5373 /* Do initial variable expansion. */
5374 temp1 = expand_string_if_necessary (temp, Q_DOUBLE_QUOTES, expand_string);
5379 /* Find the variable in VARIABLE_LIST. */
5380 temp = (char *)NULL;
5382 for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
5384 temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
5386 /* If this isn't a variable name, then just output the `$'. */
5387 if (temp1 == 0 || *temp1 == '\0')
5390 temp = (char *)xmalloc (2);
5393 if (expanded_something)
5394 *expanded_something = 0;
5398 /* If the variable exists, return its value cell. */
5399 var = find_variable (temp1);
5401 if (var && invisible_p (var) == 0 && var_isset (var))
5403 #if defined (ARRAY_VARS)
5406 temp = array_reference (array_cell (var), 0);
5408 temp = quote_escapes (temp);
5412 temp = quote_escapes (value_cell (var));
5418 temp = (char *)NULL;
5420 if (unbound_vars_is_error)
5421 err_unboundvar (temp1);
5429 last_command_exit_value = EXECUTION_FAILURE;
5430 return ((unbound_vars_is_error && interactive_shell == 0)
5431 ? &expand_param_fatal
5432 : &expand_param_error);
5443 /* Make a word list which is the result of parameter and variable
5444 expansion, command substitution, arithmetic substitution, and
5445 quote removal of WORD. Return a pointer to a WORD_LIST which is
5446 the result of the expansion. If WORD contains a null word, the
5447 word list returned is also null.
5449 QUOTED contains flag values defined in shell.h.
5451 ISEXP is used to tell expand_word_internal that the word should be
5452 treated as the result of an expansion. This has implications for
5453 how IFS characters in the word are treated.
5455 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
5456 they point to an integer value which receives information about expansion.
5457 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
5458 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
5461 This only does word splitting in the case of $@ expansion. In that
5462 case, we split on ' '. */
5464 /* Values for the local variable quoted_state. */
5466 #define PARTIALLY_QUOTED 1
5467 #define WHOLLY_QUOTED 2
5470 expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
5473 int *contains_dollar_at;
5474 int *expanded_something;
5479 /* The intermediate string that we build while expanding. */
5482 /* The current size of the above object. */
5485 /* Index into ISTRING. */
5488 /* Temporary string storage. */
5491 /* The text of WORD. */
5492 register char *string;
5494 /* The size of STRING. */
5497 /* The index into STRING. */
5500 /* This gets 1 if we see a $@ while quoted. */
5501 int quoted_dollar_at;
5503 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
5504 whether WORD contains no quoting characters, a partially quoted
5505 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
5508 int had_quoted_null;
5512 register unsigned char c; /* Current character. */
5513 int t_index; /* For calls to string_extract_xxx. */
5519 istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
5520 istring[istring_index = 0] = '\0';
5521 quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
5522 quoted_state = UNQUOTED;
5524 string = word->word;
5526 goto finished_with_string;
5527 string_size = strlen (string);
5529 if (contains_dollar_at)
5530 *contains_dollar_at = 0;
5532 /* Begin the expansion. */
5538 /* Case on toplevel character. */
5542 goto finished_with_string;
5546 #if HANDLE_MULTIBYTE
5547 if (MB_CUR_MAX > 1 && string[sindex])
5550 mbstate_t state_bak;
5554 mblength = mbrlen (string + sindex, string_size - sindex, &state);
5555 if (mblength == (size_t)-1 || mblength == (size_t)-2)
5562 temp = (char *)xmalloc (mblength + 2);
5564 for (i = 0; i < mblength; i++)
5565 temp[i+1] = string[sindex++];
5566 temp[mblength + 1] = '\0';
5573 temp = (char *)xmalloc (3);
5575 temp[1] = c = string[sindex];
5586 istring = sub_append_string (temp, istring, &istring_index, &istring_size);
5592 #if defined (PROCESS_SUBSTITUTION)
5593 /* Process substitution. */
5597 if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || posixly_correct)
5599 sindex--; /* add_character: label increments sindex */
5603 t_index = sindex + 1; /* skip past both '<' and LPAREN */
5605 temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index); /*))*/
5608 /* If the process substitution specification is `<()', we want to
5609 open the pipe for writing in the child and produce output; if
5610 it is `>()', we want to open the pipe for reading in the child
5611 and consume input. */
5612 temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
5616 goto dollar_add_string;
5618 #endif /* PROCESS_SUBSTITUTION */
5621 if (expanded_something)
5622 *expanded_something = 1;
5625 temp = param_expand (string, &sindex, quoted, expanded_something,
5626 &has_dollar_at, "ed_dollar_at,
5627 &had_quoted_null, 0);
5629 if (temp == &expand_param_error || temp == &expand_param_fatal)
5633 return ((temp == &expand_param_error) ? &expand_word_error
5634 : &expand_word_fatal);
5636 if (contains_dollar_at && has_dollar_at)
5637 *contains_dollar_at = 1;
5641 case '`': /* Backquoted command substitution. */
5645 if (expanded_something)
5646 *expanded_something = 1;
5648 temp = string_extract (string, &sindex, "`", 0);
5649 de_backslash (temp);
5650 temp1 = command_substitute (temp, quoted);
5653 goto dollar_add_string;
5657 if (string[sindex + 1] == '\n')
5663 c = string[++sindex];
5665 if (quoted & Q_HERE_DOCUMENT)
5667 else if (quoted & Q_DOUBLE_QUOTES)
5672 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
5674 SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
5679 sindex--; /* add_character: label increments sindex */
5684 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
5689 /* BEFORE jumping here, we need to increment sindex if appropriate */
5690 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
5691 DEFAULT_ARRAY_SIZE);
5692 istring[istring_index++] = twochars[0];
5693 istring[istring_index++] = twochars[1];
5694 istring[istring_index] = '\0';
5700 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT|Q_PATQUOTE))
5702 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
5707 temp = string_extract_double_quoted (string, &sindex, 0);
5709 /* If the quotes surrounded the entire string, then the
5710 whole word was quoted. */
5711 quoted_state = (t_index == 1 && string[sindex] == '\0')
5717 tword = make_word (temp); /* XXX */
5719 temp = (char *)NULL;
5722 list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &has_dollar_at, (int *)NULL);
5724 if (list == &expand_word_error || list == &expand_word_fatal)
5728 /* expand_word_internal has already freed temp_word->word
5729 for us because of the way it prints error messages. */
5730 tword->word = (char *)NULL;
5731 dispose_word (tword);
5735 dispose_word (tword);
5737 /* "$@" (a double-quoted dollar-at) expands into nothing,
5738 not even a NULL word, when there are no positional
5740 if (list == 0 && has_dollar_at)
5746 /* If we get "$@", we know we have expanded something, so we
5747 need to remember it for the final split on $IFS. This is
5748 a special case; it's the only case where a quoted string
5749 can expand into more than one word. It's going to come back
5750 from the above call to expand_word_internal as a list with
5751 a single word, in which all characters are quoted and
5752 separated by blanks. What we want to do is to turn it back
5753 into a list for the next piece of code. */
5755 dequote_list (list);
5760 if (contains_dollar_at)
5761 *contains_dollar_at = 1;
5762 if (expanded_something)
5763 *expanded_something = 1;
5768 /* What we have is "". This is a minor optimization. */
5770 list = (WORD_LIST *)NULL;
5773 /* The code above *might* return a list (consider the case of "$@",
5774 where it returns "$1", "$2", etc.). We can't throw away the
5775 rest of the list, and we have to make sure each word gets added
5776 as quoted. We test on tresult->next: if it is non-NULL, we
5777 quote the whole list, save it to a string with string_list, and
5778 add that string. We don't need to quote the results of this
5779 (and it would be wrong, since that would quote the separators
5780 as well), so we go directly to add_string. */
5785 /* Testing quoted_dollar_at makes sure that "$@" is
5786 split correctly when $IFS does not contain a space. */
5787 temp = quoted_dollar_at
5788 ? string_list_dollar_at (list, Q_DOUBLE_QUOTES)
5789 : string_list (quote_list (list));
5790 dispose_words (list);
5795 temp = savestring (list->word->word);
5796 dispose_words (list);
5798 /* If the string is not a quoted null string, we want
5799 to remove any embedded unquoted CTLNUL characters.
5800 We do not want to turn quoted null strings back into
5801 the empty string, though. We do this because we
5802 want to remove any quoted nulls from expansions that
5803 contain other characters. For example, if we have
5804 x"$*"y or "x$*y" and there are no positional parameters,
5805 the $* should expand into nothing. */
5806 /* HOWEVER, this fails if the string contains a literal
5807 CTLNUL or CTLNUL is contained in the (non-null) expansion
5808 of some variable. I'm not sure what to do about this
5810 if (QUOTED_NULL (temp) == 0)
5811 remove_quoted_nulls (temp); /* XXX */
5816 temp = (char *)NULL;
5818 /* We do not want to add quoted nulls to strings that are only
5819 partially quoted; we can throw them away. */
5820 if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
5828 temp = quote_string (temp);
5836 sindex--; /* add_character: label increments sindex */
5844 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT|Q_PATQUOTE))
5846 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
5851 temp = string_extract_single_quoted (string, &sindex);
5853 /* If the entire STRING was surrounded by single quotes,
5854 then the string is wholly quoted. */
5855 quoted_state = (t_index == 1 && string[sindex] == '\0')
5859 /* If all we had was '', it is a null expansion. */
5863 temp = (char *)NULL;
5866 remove_quoted_escapes (temp); /* ??? */
5868 /* We do not want to add quoted nulls to strings that are only
5869 partially quoted; such nulls are discarded. */
5870 if (temp == 0 && (quoted_state == PARTIALLY_QUOTED))
5873 /* If we have a quoted null expansion, add a quoted NULL to istring. */
5877 sindex--; /* add_character: label increments sindex */
5881 goto add_quoted_string;
5886 /* This is the fix for " $@ " */
5887 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c)))
5889 if (string[sindex]) /* from old goto dollar_add_string */
5898 #if HANDLE_MULTIBYTE
5899 /* XXX - I'd like to use SCOPY_CHAR_I here. */
5903 mbstate_t state_bak;
5908 mblength = mbrlen (string + sindex, string_size - sindex, &state);
5909 if (mblength == (size_t)-1 || mblength == (size_t)-2)
5917 temp = (char *)xmalloc (mblength + 2);
5919 for (i = 0; i < mblength; i++)
5920 temp[i + 1] = string[sindex++];
5921 temp[mblength + 1] = '\0';
5928 twochars[0] = CTLESC;
5935 SADD_MBCHAR (temp, string, sindex, string_size);
5938 RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
5939 DEFAULT_ARRAY_SIZE);
5940 istring[istring_index++] = c;
5941 istring[istring_index] = '\0';
5943 /* Next character. */
5948 finished_with_string:
5949 /* OK, we're ready to return. If we have a quoted string, and
5950 quoted_dollar_at is not set, we do no splitting at all; otherwise
5951 we split on ' '. The routines that call this will handle what to
5952 do if nothing has been expanded. */
5954 /* Partially and wholly quoted strings which expand to the empty
5955 string are retained as an empty arguments. Unquoted strings
5956 which expand to the empty string are discarded. The single
5957 exception is the case of expanding "$@" when there are no
5958 positional parameters. In that case, we discard the expansion. */
5960 /* Because of how the code that handles "" and '' in partially
5961 quoted strings works, we need to make ISTRING into a QUOTED_NULL
5962 if we saw quoting characters, but the expansion was empty.
5963 "" and '' are tossed away before we get to this point when
5964 processing partially quoted strings. This makes "" and $xxx""
5965 equivalent when xxx is unset. We also look to see whether we
5966 saw a quoted null from a ${} expansion and add one back if we
5969 /* If we expand to nothing and there were no single or double quotes
5970 in the word, we throw it away. Otherwise, we return a NULL word.
5971 The single exception is for $@ surrounded by double quotes when
5972 there are no positional parameters. In that case, we also throw
5975 if (*istring == '\0')
5977 if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
5979 istring[0] = CTLNUL;
5981 tword = make_bare_word (istring);
5982 list = make_word_list (tword, (WORD_LIST *)NULL);
5983 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
5984 tword->flags |= W_QUOTED;
5986 /* According to sh, ksh, and Posix.2, if a word expands into nothing
5987 and a double-quoted "$@" appears anywhere in it, then the entire
5989 else if (quoted_state == UNQUOTED || quoted_dollar_at)
5990 list = (WORD_LIST *)NULL;
5994 tword = make_bare_word (istring);
5995 list = make_word_list (tword, (WORD_LIST *)NULL);
5996 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
5997 tword->flags |= W_QUOTED;
6001 list = (WORD_LIST *)NULL;
6004 else if (word->flags & W_NOSPLIT)
6006 tword = make_bare_word (istring);
6007 list = make_word_list (tword, (WORD_LIST *)NULL);
6008 if (word->flags & W_ASSIGNMENT)
6009 tword->flags |= W_ASSIGNMENT; /* XXX */
6010 if (word->flags & W_NOGLOB)
6011 tword->flags |= W_NOGLOB; /* XXX */
6012 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6013 tword->flags |= W_QUOTED;
6019 ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
6021 /* If we have $@, we need to split the results no matter what. If
6022 IFS is unset or NULL, string_list_dollar_at has separated the
6023 positional parameters with a space, so we split on space (we have
6024 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
6025 string_list_dollar_at has separated the positional parameters
6026 with the first character of $IFS, so we split on $IFS. */
6027 if (has_dollar_at && ifs_chars)
6028 list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
6031 tword = make_bare_word (istring);
6032 list = make_word_list (tword, (WORD_LIST *)NULL);
6033 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
6034 tword->flags |= W_QUOTED;
6035 if (word->flags & W_ASSIGNMENT)
6036 tword->flags |= W_ASSIGNMENT;
6037 if (word->flags & W_NOGLOB)
6038 tword->flags |= W_NOGLOB;
6046 /* **************************************************************** */
6048 /* Functions for Quote Removal */
6050 /* **************************************************************** */
6052 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
6053 backslash quoting rules for within double quotes or a here document. */
6055 string_quote_removal (string, quoted)
6060 char *r, *result_string, *temp, *send;
6061 int sindex, tindex, dquote;
6065 /* The result can be no longer than the original string. */
6066 slen = strlen (string);
6067 send = string + slen;
6069 r = result_string = (char *)xmalloc (slen + 1);
6071 for (dquote = sindex = 0; c = string[sindex];)
6076 c = string[++sindex];
6077 if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
6082 SCOPY_CHAR_M (r, string, send, sindex);
6086 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
6092 tindex = sindex + 1;
6093 temp = string_extract_single_quoted (string, &tindex);
6104 dquote = 1 - dquote;
6110 return (result_string);
6115 /* Perform quote removal on word WORD. This allocates and returns a new
6118 word_quote_removal (word, quoted)
6125 t = string_quote_removal (word->word, quoted);
6126 w = make_bare_word (t);
6131 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
6132 the members of the list are treated as if they are surrounded by
6133 double quotes. Return a new list, or NULL if LIST is NULL. */
6135 word_list_quote_removal (list, quoted)
6139 WORD_LIST *result, *t, *tresult;
6141 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
6143 tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
6144 result = (WORD_LIST *) list_append (result, tresult);
6150 /*******************************************
6152 * Functions to perform word splitting *
6154 *******************************************/
6164 ifs_value = v ? value_cell (v) : " \t\n";
6166 /* Should really merge ifs_cmap with sh_syntaxtab. */
6167 memset (ifs_cmap, '\0', sizeof (ifs_cmap));
6168 for (t = ifs_value ; t && *t; t++)
6174 ifs_firstc = ifs_value ? *ifs_value : 0;
6183 /* This splits a single word into a WORD LIST on $IFS, but only if the word
6184 is not quoted. list_string () performs quote removal for us, even if we
6185 don't do any splitting. */
6187 word_split (w, ifs_chars)
6197 xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
6198 result = list_string (w->word, xifs, w->flags & W_QUOTED);
6201 result = (WORD_LIST *)NULL;
6206 /* Perform word splitting on LIST and return the RESULT. It is possible
6207 to return (WORD_LIST *)NULL. */
6209 word_list_split (list)
6212 WORD_LIST *result, *t, *tresult;
6214 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
6216 tresult = word_split (t->word, ifs_value);
6217 result = (WORD_LIST *) list_append (result, tresult);
6222 /**************************************************
6224 * Functions to expand an entire WORD_LIST *
6226 **************************************************/
6228 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
6229 ELIST, and set ELIST to the new list. */
6230 #define PREPEND_LIST(nlist, elist) \
6231 do { nlist->next = elist; elist = nlist; } while (0)
6233 /* Separate out any initial variable assignments from TLIST. If set -k has
6234 been executed, remove all assignment statements from TLIST. Initial
6235 variable assignments and other environment assignments are placed
6236 on SUBST_ASSIGN_VARLIST. */
6238 separate_out_assignments (tlist)
6241 register WORD_LIST *vp, *lp;
6244 return ((WORD_LIST *)NULL);
6246 if (subst_assign_varlist)
6247 dispose_words (subst_assign_varlist); /* Clean up after previous error */
6249 subst_assign_varlist = (WORD_LIST *)NULL;
6252 /* Separate out variable assignments at the start of the command.
6253 Loop invariant: vp->next == lp
6255 lp = list of words left after assignment statements skipped
6256 tlist = original list of words
6258 while (lp && (lp->word->flags & W_ASSIGNMENT))
6264 /* If lp != tlist, we have some initial assignment statements.
6265 We make SUBST_ASSIGN_VARLIST point to the list of assignment
6266 words and TLIST point to the remaining words. */
6269 subst_assign_varlist = tlist;
6270 /* ASSERT(vp->next == lp); */
6271 vp->next = (WORD_LIST *)NULL; /* terminate variable list */
6272 tlist = lp; /* remainder of word list */
6275 /* vp == end of variable list */
6276 /* tlist == remainder of original word list without variable assignments */
6278 /* All the words in tlist were assignment statements */
6279 return ((WORD_LIST *)NULL);
6281 /* ASSERT(tlist != NULL); */
6282 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
6284 /* If the -k option is in effect, we need to go through the remaining
6285 words, separate out the assignment words, and place them on
6286 SUBST_ASSIGN_VARLIST. */
6287 if (place_keywords_in_env)
6289 WORD_LIST *tp; /* tp == running pointer into tlist */
6294 /* Loop Invariant: tp->next == lp */
6295 /* Loop postcondition: tlist == word list without assignment statements */
6298 if (lp->word->flags & W_ASSIGNMENT)
6300 /* Found an assignment statement, add this word to end of
6301 subst_assign_varlist (vp). */
6302 if (!subst_assign_varlist)
6303 subst_assign_varlist = vp = lp;
6310 /* Remove the word pointed to by LP from TLIST. */
6311 tp->next = lp->next;
6312 /* ASSERT(vp == lp); */
6313 lp->next = (WORD_LIST *)NULL;
6326 #define WEXP_VARASSIGN 0x001
6327 #define WEXP_BRACEEXP 0x002
6328 #define WEXP_TILDEEXP 0x004
6329 #define WEXP_PARAMEXP 0x008
6330 #define WEXP_PATHEXP 0x010
6332 /* All of the expansions, including variable assignments at the start of
6334 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
6336 /* All of the expansions except variable assignments at the start of
6338 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
6340 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
6341 expansion, command substitution, arithmetic expansion, word splitting, and
6343 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
6345 /* Take the list of words in LIST and do the various substitutions. Return
6346 a new list of words which is the expanded list, and without things like
6347 variable assignments. */
6353 return (expand_word_list_internal (list, WEXP_ALL));
6356 /* Same as expand_words (), but doesn't hack variable or environment
6359 expand_words_no_vars (list)
6362 return (expand_word_list_internal (list, WEXP_NOVARS));
6366 expand_words_shellexp (list)
6369 return (expand_word_list_internal (list, WEXP_SHELLEXP));
6373 glob_expand_word_list (tlist, eflags)
6377 char **glob_array, *temp_string;
6378 register int glob_index;
6379 WORD_LIST *glob_list, *output_list, *disposables, *next;
6382 output_list = disposables = (WORD_LIST *)NULL;
6383 glob_array = (char **)NULL;
6386 /* For each word, either globbing is attempted or the word is
6387 added to orig_list. If globbing succeeds, the results are
6388 added to orig_list and the word (tlist) is added to the list
6389 of disposable words. If globbing fails and failed glob
6390 expansions are left unchanged (the shell default), the
6391 original word is added to orig_list. If globbing fails and
6392 failed glob expansions are removed, the original word is
6393 added to the list of disposable words. orig_list ends up
6394 in reverse order and requires a call to REVERSE_LIST to
6395 be set right. After all words are examined, the disposable
6399 /* If the word isn't an assignment and contains an unquoted
6400 pattern matching character, then glob it. */
6401 if ((tlist->word->flags & W_NOGLOB) == 0 &&
6402 unquoted_glob_pattern_p (tlist->word->word))
6404 glob_array = shell_glob_filename (tlist->word->word);
6406 /* Handle error cases.
6407 I don't think we should report errors like "No such file
6408 or directory". However, I would like to report errors
6409 like "Read failed". */
6411 if (GLOB_FAILED (glob_array))
6413 glob_array = (char **)xmalloc (sizeof (char *));
6414 glob_array[0] = (char *)NULL;
6417 /* Dequote the current word in case we have to use it. */
6418 if (glob_array[0] == NULL)
6420 temp_string = dequote_string (tlist->word->word);
6421 free (tlist->word->word);
6422 tlist->word->word = temp_string;
6425 /* Make the array into a word list. */
6426 glob_list = (WORD_LIST *)NULL;
6427 for (glob_index = 0; glob_array[glob_index]; glob_index++)
6429 tword = make_bare_word (glob_array[glob_index]);
6430 tword->flags |= W_GLOBEXP; /* XXX */
6431 glob_list = make_word_list (tword, glob_list);
6436 output_list = (WORD_LIST *)list_append (glob_list, output_list);
6437 PREPEND_LIST (tlist, disposables);
6439 else if (allow_null_glob_expansion == 0)
6441 /* Failed glob expressions are left unchanged. */
6442 PREPEND_LIST (tlist, output_list);
6446 /* Failed glob expressions are removed. */
6447 PREPEND_LIST (tlist, disposables);
6452 /* Dequote the string. */
6453 temp_string = dequote_string (tlist->word->word);
6454 free (tlist->word->word);
6455 tlist->word->word = temp_string;
6456 PREPEND_LIST (tlist, output_list);
6459 strvec_dispose (glob_array);
6460 glob_array = (char **)NULL;
6466 dispose_words (disposables);
6469 output_list = REVERSE_LIST (output_list, WORD_LIST *);
6471 return (output_list);
6474 #if defined (BRACE_EXPANSION)
6476 brace_expand_word_list (tlist, eflags)
6480 register char **expansions;
6482 WORD_LIST *disposables, *output_list, *next;
6486 for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
6490 /* Only do brace expansion if the word has a brace character. If
6491 not, just add the word list element to BRACES and continue. In
6492 the common case, at least when running shell scripts, this will
6493 degenerate to a bunch of calls to `xstrchr', and then what is
6494 basically a reversal of TLIST into BRACES, which is corrected
6495 by a call to REVERSE_LIST () on BRACES when the end of TLIST
6497 if (xstrchr (tlist->word->word, LBRACE))
6499 expansions = brace_expand (tlist->word->word);
6501 for (eindex = 0; temp_string = expansions[eindex]; eindex++)
6503 w = make_word (temp_string);
6504 /* If brace expansion didn't change the word, preserve
6505 the flags. We may want to preserve the flags
6506 unconditionally someday -- XXX */
6507 if (STREQ (temp_string, tlist->word->word))
6508 w->flags = tlist->word->flags;
6509 output_list = make_word_list (w, output_list);
6510 free (expansions[eindex]);
6514 /* Add TLIST to the list of words to be freed after brace
6515 expansion has been performed. */
6516 PREPEND_LIST (tlist, disposables);
6519 PREPEND_LIST (tlist, output_list);
6523 dispose_words (disposables);
6526 output_list = REVERSE_LIST (output_list, WORD_LIST *);
6528 return (output_list);
6533 shell_expand_word_list (tlist, eflags)
6537 WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list;
6538 int expanded_something, has_dollar_at;
6541 /* We do tilde expansion all the time. This is what 1003.2 says. */
6542 new_list = (WORD_LIST *)NULL;
6543 for (orig_list = tlist; tlist; tlist = next)
6545 temp_string = tlist->word->word;
6549 /* Posix.2 section 3.6.1 says that tildes following `=' in words
6550 which are not assignment statements are not expanded. If the
6551 shell isn't in posix mode, though, we perform tilde expansion
6552 on `likely candidate' unquoted assignment statements (flags
6553 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
6554 contains an unquoted :~ or =~. Something to think about: we
6555 now have a flag that says to perform tilde expansion on arguments
6556 to `assignment builtins' like declare and export that look like
6557 assignment statements. We now do tilde expansion on such words
6558 even in POSIX mode. */
6559 if (((tlist->word->flags & (W_ASSIGNMENT|W_QUOTED)) == W_ASSIGNMENT) &&
6560 (posixly_correct == 0 || (tlist->word->flags & W_TILDEEXP)) &&
6561 (unquoted_substring ("=~", temp_string) || unquoted_substring (":~", temp_string)))
6563 tlist->word->word = bash_tilde_expand (temp_string, 1);
6566 else if (temp_string[0] == '~')
6568 tlist->word->word = bash_tilde_expand (temp_string, 0);
6572 expanded_something = 0;
6573 expanded = expand_word_internal
6574 (tlist->word, 0, 0, &has_dollar_at, &expanded_something);
6576 if (expanded == &expand_word_error || expanded == &expand_word_fatal)
6578 /* By convention, each time this error is returned,
6579 tlist->word->word has already been freed. */
6580 tlist->word->word = (char *)NULL;
6582 /* Dispose our copy of the original list. */
6583 dispose_words (orig_list);
6584 /* Dispose the new list we're building. */
6585 dispose_words (new_list);
6587 last_command_exit_value = EXECUTION_FAILURE;
6588 if (expanded == &expand_word_error)
6589 jump_to_top_level (DISCARD);
6591 jump_to_top_level (FORCE_EOF);
6594 /* Don't split words marked W_NOSPLIT. */
6595 if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
6597 temp_list = word_list_split (expanded);
6598 dispose_words (expanded);
6602 /* If no parameter expansion, command substitution, process
6603 substitution, or arithmetic substitution took place, then
6604 do not do word splitting. We still have to remove quoted
6605 null characters from the result. */
6606 word_list_remove_quoted_nulls (expanded);
6607 temp_list = expanded;
6610 expanded = REVERSE_LIST (temp_list, WORD_LIST *);
6611 new_list = (WORD_LIST *)list_append (expanded, new_list);
6615 dispose_words (orig_list);
6618 new_list = REVERSE_LIST (new_list, WORD_LIST *);
6623 /* The workhorse for expand_words () and expand_words_no_vars ().
6624 First arg is LIST, a WORD_LIST of words.
6625 Second arg EFLAGS is a flags word controlling which expansions are
6628 This does all of the substitutions: brace expansion, tilde expansion,
6629 parameter expansion, command substitution, arithmetic expansion,
6630 process substitution, word splitting, and pathname expansion, according
6631 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
6632 set, or for which no expansion is done, do not undergo word splitting.
6633 Words with the W_NOGLOB bit set do not undergo pathname expansion. */
6635 expand_word_list_internal (list, eflags)
6639 WORD_LIST *new_list, *temp_list;
6643 return ((WORD_LIST *)NULL);
6645 garglist = new_list = copy_word_list (list);
6646 if (eflags & WEXP_VARASSIGN)
6648 garglist = new_list = separate_out_assignments (new_list);
6651 if (subst_assign_varlist)
6653 /* All the words were variable assignments, so they are placed
6654 into the shell's environment. */
6655 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
6657 this_command_name = (char *)NULL; /* no arithmetic errors */
6658 tint = do_assignment (temp_list->word->word);
6659 /* Variable assignment errors in non-interactive shells
6660 running in Posix.2 mode cause the shell to exit. */
6663 last_command_exit_value = EXECUTION_FAILURE;
6664 if (interactive_shell == 0 && posixly_correct)
6665 jump_to_top_level (FORCE_EOF);
6667 jump_to_top_level (DISCARD);
6670 dispose_words (subst_assign_varlist);
6671 subst_assign_varlist = (WORD_LIST *)NULL;
6673 return ((WORD_LIST *)NULL);
6677 /* Begin expanding the words that remain. The expansions take place on
6678 things that aren't really variable assignments. */
6680 #if defined (BRACE_EXPANSION)
6681 /* Do brace expansion on this word if there are any brace characters
6683 if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
6684 new_list = brace_expand_word_list (new_list, eflags);
6685 #endif /* BRACE_EXPANSION */
6687 /* Perform the `normal' shell expansions: tilde expansion, parameter and
6688 variable substitution, command substitution, arithmetic expansion,
6689 and word splitting. */
6690 new_list = shell_expand_word_list (new_list, eflags);
6692 /* Okay, we're almost done. Now let's just do some filename
6696 if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
6697 /* Glob expand the word list unless globbing has been disabled. */
6698 new_list = glob_expand_word_list (new_list, eflags);
6700 /* Dequote the words, because we're not performing globbing. */
6701 new_list = dequote_list (new_list);
6704 if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
6706 sh_assign_func_t *assign_func;
6708 /* If the remainder of the words expand to nothing, Posix.2 requires
6709 that the variable and environment assignments affect the shell's
6711 assign_func = new_list ? assign_in_env : do_assignment;
6713 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
6715 this_command_name = (char *)NULL;
6716 tint = (*assign_func) (temp_list->word->word);
6717 /* Variable assignment errors in non-interactive shells running
6718 in Posix.2 mode cause the shell to exit. */
6719 if (tint == 0 && assign_func == do_assignment)
6721 last_command_exit_value = EXECUTION_FAILURE;
6722 if (interactive_shell == 0 && posixly_correct)
6723 jump_to_top_level (FORCE_EOF);
6725 jump_to_top_level (DISCARD);
6729 dispose_words (subst_assign_varlist);
6730 subst_assign_varlist = (WORD_LIST *)NULL;
6734 tint = list_length (new_list) + 1;
6735 RESIZE_MALLOCED_BUFFER (glob_argv_flags, 0, tint, glob_argv_flags_size, 16);
6736 for (tint = 0, temp_list = new_list; temp_list; temp_list = temp_list->next)
6737 glob_argv_flags[tint++] = (temp_list->word->flags & W_GLOBEXP) ? '1' : '0';
6738 glob_argv_flags[tint] = '\0';