1 /* subst.c -- The part of the shell that does parameter, command, arithmetic,
2 and 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-2010 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
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 Bash is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with Bash. If not, see <http://www.gnu.org/licenses/>.
27 #include "bashtypes.h"
29 #include "chartypes.h"
30 #if defined (HAVE_PWD_H)
36 #if defined (HAVE_UNISTD_H)
41 #include "posixstat.h"
48 #include "execute_cmd.h"
52 #include "mailcheck.h"
57 #include "builtins/getopt.h"
58 #include "builtins/common.h"
60 #include "builtins/builtext.h"
62 #include <tilde/tilde.h>
63 #include <glob/strmatch.h>
69 /* The size that strings change by. */
70 #define DEFAULT_INITIAL_ARRAY_SIZE 112
71 #define DEFAULT_ARRAY_SIZE 128
77 #define VT_ARRAYMEMBER 3
80 #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */
82 /* Flags for quoted_strchr */
83 #define ST_BACKSL 0x01
84 #define ST_CTLESC 0x02
85 #define ST_SQUOTE 0x04 /* unused yet */
86 #define ST_DQUOTE 0x08 /* unused yet */
88 /* Flags for the `pflags' argument to param_expand() */
89 #define PF_NOCOMSUB 0x01 /* Do not perform command substitution */
90 #define PF_IGNUNBOUND 0x02 /* ignore unbound vars even if -u set */
91 #define PF_NOSPLIT2 0x04 /* same as W_NOSPLIT2 */
93 /* These defs make it easier to use the editor. */
99 #if defined (HANDLE_MULTIBYTE)
104 /* Evaluates to 1 if C is one of the shell's special parameters whose length
105 can be taken, but is also one of the special expansion characters. */
106 #define VALID_SPECIAL_LENGTH_PARAM(c) \
107 ((c) == '-' || (c) == '?' || (c) == '#')
109 /* Evaluates to 1 if C is one of the shell's special parameters for which an
110 indirect variable reference may be made. */
111 #define VALID_INDIR_PARAM(c) \
112 ((posixly_correct == 0 && (c) == '#') || (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*')
114 /* Evaluates to 1 if C is one of the OP characters that follows the parameter
115 in ${parameter[:]OPword}. */
116 #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
118 /* Evaluates to 1 if this is one of the shell's special variables. */
119 #define SPECIAL_VAR(name, wi) \
120 ((DIGIT (*name) && all_digits (name)) || \
121 (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
122 (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))
124 /* An expansion function that takes a string and a quoted flag and returns
125 a WORD_LIST *. Used as the type of the third argument to
126 expand_string_if_necessary(). */
127 typedef WORD_LIST *EXPFUNC __P((char *, int));
129 /* Process ID of the last command executed within command substitution. */
130 pid_t last_command_subst_pid = NO_PID;
131 pid_t current_command_subst_pid = NO_PID;
133 /* Variables used to keep track of the characters in IFS. */
136 unsigned char ifs_cmap[UCHAR_MAX + 1];
138 #if defined (HANDLE_MULTIBYTE)
139 unsigned char ifs_firstc[MB_LEN_MAX];
140 size_t ifs_firstc_len;
142 unsigned char ifs_firstc;
145 /* Sentinel to tell when we are performing variable assignments preceding a
146 command name and putting them into the environment. Used to make sure
147 we use the temporary environment when looking up variable values. */
148 int assigning_in_environment;
150 /* Used to hold a list of variable assignments preceding a command. Global
151 so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
152 SIGCHLD trap and so it can be saved and restored by the trap handlers. */
153 WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
155 /* Extern functions and variables from different files. */
156 extern int last_command_exit_value, last_command_exit_signal;
157 extern int subshell_environment, line_number;
158 extern int subshell_level, parse_and_execute_level, sourcelevel;
159 extern int eof_encountered;
160 extern int return_catch_flag, return_catch_value;
161 extern pid_t dollar_dollar_pid;
162 extern int posixly_correct;
163 extern char *this_command_name;
164 extern struct fd_bitmap *current_fds_to_close;
165 extern int wordexp_only;
166 extern int expanding_redir;
167 extern int tempenv_assign_error;
169 #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
170 extern wchar_t *wcsdup __P((const wchar_t *));
173 /* Non-zero means to allow unmatched globbed filenames to expand to
175 int allow_null_glob_expansion;
177 /* Non-zero means to throw an error when globbing fails to match anything. */
178 int fail_glob_expansion;
181 /* Variables to keep track of which words in an expanded word list (the
182 output of expand_word_list_internal) are the result of globbing
183 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
184 (CURRENTLY UNUSED). */
185 char *glob_argv_flags;
186 static int glob_argv_flags_size;
189 static WORD_LIST expand_word_error, expand_word_fatal;
190 static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
191 static char expand_param_error, expand_param_fatal;
192 static char extract_string_error, extract_string_fatal;
194 /* Tell the expansion functions to not longjmp back to top_level on fatal
195 errors. Enabled when doing completion and prompt string expansion. */
196 static int no_longjmp_on_fatal_error = 0;
198 /* Set by expand_word_unsplit; used to inhibit splitting and re-joining
199 $* on $IFS, primarily when doing assignment statements. */
200 static int expand_no_split_dollar_star = 0;
202 /* A WORD_LIST of words to be expanded by expand_word_list_internal,
203 without any leading variable assignments. */
204 static WORD_LIST *garglist = (WORD_LIST *)NULL;
206 static char *quoted_substring __P((char *, int, int));
207 static int quoted_strlen __P((char *));
208 static char *quoted_strchr __P((char *, int, int));
210 static char *expand_string_if_necessary __P((char *, int, EXPFUNC *));
211 static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *));
212 static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
213 static WORD_LIST *expand_string_internal __P((char *, int));
214 static WORD_LIST *expand_string_leave_quoted __P((char *, int));
215 static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *));
217 static WORD_LIST *list_quote_escapes __P((WORD_LIST *));
218 static char *make_quoted_char __P((int));
219 static WORD_LIST *quote_list __P((WORD_LIST *));
221 static int unquoted_substring __P((char *, char *));
222 static int unquoted_member __P((int, char *));
224 #if defined (ARRAY_VARS)
225 static SHELL_VAR *do_compound_assignment __P((char *, char *, int));
227 static int do_assignment_internal __P((const WORD_DESC *, int));
229 static char *string_extract_verbatim __P((char *, size_t, int *, char *, int));
230 static char *string_extract __P((char *, int *, char *, int));
231 static char *string_extract_double_quoted __P((char *, int *, int));
232 static inline char *string_extract_single_quoted __P((char *, int *));
233 static inline int skip_single_quoted __P((const char *, size_t, int));
234 static int skip_double_quoted __P((char *, size_t, int));
235 static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int));
236 static char *extract_dollar_brace_string __P((char *, int *, int, int));
237 static int skip_matched_pair __P((const char *, int, int, int, int));
239 static char *pos_params __P((char *, int, int, int));
241 static unsigned char *mb_getcharlens __P((char *, int));
243 static char *remove_upattern __P((char *, char *, int));
244 #if defined (HANDLE_MULTIBYTE)
245 static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int));
247 static char *remove_pattern __P((char *, char *, int));
249 static int match_upattern __P((char *, char *, int, char **, char **));
250 #if defined (HANDLE_MULTIBYTE)
251 static int match_wpattern __P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
253 static int match_pattern __P((char *, char *, int, char **, char **));
254 static int getpatspec __P((int, char *));
255 static char *getpattern __P((char *, int, int));
256 static char *variable_remove_pattern __P((char *, char *, int, int));
257 static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int));
258 static char *parameter_list_remove_pattern __P((int, char *, int, int));
260 static char *array_remove_pattern __P((SHELL_VAR *, char *, int, char *, int));
262 static char *parameter_brace_remove_pattern __P((char *, char *, int, char *, int, int, int));
264 static char *process_substitute __P((char *, int));
266 static char *read_comsub __P((int, int, int *));
269 static arrayind_t array_length_reference __P((char *));
272 static int valid_brace_expansion_word __P((char *, int));
273 static int chk_atstar __P((char *, int, int *, int *));
274 static int chk_arithsub __P((const char *, int));
276 static WORD_DESC *parameter_brace_expand_word __P((char *, int, int, int, arrayind_t *));
277 static WORD_DESC *parameter_brace_expand_indir __P((char *, int, int, int *, int *));
278 static WORD_DESC *parameter_brace_expand_rhs __P((char *, char *, int, int, int *, int *));
279 static void parameter_brace_expand_error __P((char *, char *));
281 static int valid_length_expression __P((char *));
282 static intmax_t parameter_brace_expand_length __P((char *));
284 static char *skiparith __P((char *, int));
285 static int verify_substring_values __P((SHELL_VAR *, char *, char *, int, intmax_t *, intmax_t *));
286 static int get_var_and_type __P((char *, char *, arrayind_t, int, int, SHELL_VAR **, char **));
287 static char *mb_substring __P((char *, int, int));
288 static char *parameter_brace_substring __P((char *, char *, int, char *, int, int));
290 static int shouldexp_replacement __P((char *));
292 static char *pos_params_pat_subst __P((char *, char *, char *, int));
294 static char *parameter_brace_patsub __P((char *, char *, int, char *, int, int));
296 static char *pos_params_casemod __P((char *, char *, int, int));
297 static char *parameter_brace_casemod __P((char *, char *, int, int, char *, int, int));
299 static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int, int *, int *));
300 static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
302 static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
304 static WORD_LIST *word_list_split __P((WORD_LIST *));
306 static void exp_jump_to_top_level __P((int));
308 static WORD_LIST *separate_out_assignments __P((WORD_LIST *));
309 static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int));
310 #ifdef BRACE_EXPANSION
311 static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int));
313 #if defined (ARRAY_VARS)
314 static int make_internal_declare __P((char *, char *));
316 static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
317 static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
319 /* **************************************************************** */
321 /* Utility Functions */
323 /* **************************************************************** */
327 dump_word_flags (flags)
333 fprintf (stderr, "%d -> ", f);
334 if (f & W_ASSIGNASSOC)
337 fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : "");
342 fprintf (stderr, "W_HASCTLESC%s", f ? "|" : "");
347 fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : "");
352 fprintf (stderr, "W_DQUOTE%s", f ? "|" : "");
354 if (f & W_HASQUOTEDNULL)
356 f &= ~W_HASQUOTEDNULL;
357 fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : "");
362 fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : "");
367 fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : "");
369 if (f & W_COMPASSIGN)
372 fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : "");
377 fprintf (stderr, "W_NOEXPAND%s", f ? "|" : "");
382 fprintf (stderr, "W_ITILDE%s", f ? "|" : "");
387 fprintf (stderr, "W_NOTILDE%s", f ? "|" : "");
392 fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : "");
397 fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : "");
399 if (f & W_DOLLARSTAR)
402 fprintf (stderr, "W_DOLLARSTAR%s", f ? "|" : "");
407 fprintf (stderr, "W_DOLLARAT%s", f ? "|" : "");
412 fprintf (stderr, "W_TILDEEXP%s", f ? "|" : "");
417 fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : "");
422 fprintf (stderr, "W_NOGLOB%s", f ? "|" : "");
427 fprintf (stderr, "W_NOSPLIT%s", f ? "|" : "");
432 fprintf (stderr, "W_GLOBEXP%s", f ? "|" : "");
434 if (f & W_ASSIGNMENT)
437 fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : "");
442 fprintf (stderr, "W_QUOTED%s", f ? "|" : "");
447 fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : "");
449 fprintf (stderr, "\n");
454 #ifdef INCLUDE_UNUSED
456 quoted_substring (string, start, end)
461 register char *result, *s, *r;
465 /* Move to string[start], skipping quoted characters. */
466 for (s = string, l = 0; *s && l < start; )
478 r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */
480 /* Copy LEN characters, including quote characters. */
482 for (l = 0; l < len; s++)
496 #ifdef INCLUDE_UNUSED
497 /* Return the length of S, skipping over quoted characters */
521 /* Find the first occurrence of character C in string S, obeying shell
522 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
523 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
524 escaped with CTLESC are skipped. */
526 quoted_strchr (s, c, flags)
534 if (((flags & ST_BACKSL) && *p == '\\')
535 || ((flags & ST_CTLESC) && *p == CTLESC))
539 return ((char *)NULL);
545 return ((char *)NULL);
548 /* Return 1 if CHARACTER appears in an unquoted portion of
549 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
551 unquoted_member (character, string)
559 slen = strlen (string);
561 while (c = string[sindex])
569 ADVANCE_CHAR (string, slen, sindex);
575 ADVANCE_CHAR (string, slen, sindex);
579 sindex = skip_single_quoted (string, slen, ++sindex);
583 sindex = skip_double_quoted (string, slen, ++sindex);
590 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
592 unquoted_substring (substr, string)
593 char *substr, *string;
596 int sindex, c, sublen;
599 if (substr == 0 || *substr == '\0')
602 slen = strlen (string);
603 sublen = strlen (substr);
604 for (sindex = 0; c = string[sindex]; )
606 if (STREQN (string + sindex, substr, sublen))
614 ADVANCE_CHAR (string, slen, sindex);
618 sindex = skip_single_quoted (string, slen, ++sindex);
622 sindex = skip_double_quoted (string, slen, ++sindex);
626 ADVANCE_CHAR (string, slen, sindex);
633 /* Most of the substitutions must be done in parallel. In order
634 to avoid using tons of unclear goto's, I have some functions
635 for manipulating malloc'ed strings. They all take INDX, a
636 pointer to an integer which is the offset into the string
637 where manipulation is taking place. They also take SIZE, a
638 pointer to an integer which is the current length of the
639 character array for this string. */
641 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
642 of space allocated to TARGET. SOURCE can be NULL, in which
643 case nothing happens. Gets rid of SOURCE by freeing it.
644 Returns TARGET in case the location has changed. */
646 sub_append_string (source, target, indx, size)
647 char *source, *target;
654 srclen = STRLEN (source);
655 if (srclen >= (int)(*size - *indx))
658 n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
659 target = (char *)xrealloc (target, (*size = n));
662 FASTCOPY (source, target + *indx, srclen);
664 target[*indx] = '\0';
673 /* Append the textual representation of NUMBER to TARGET.
674 INDX and SIZE are as in SUB_APPEND_STRING. */
676 sub_append_number (number, target, indx, size)
683 temp = itos (number);
684 return (sub_append_string (temp, target, indx, size));
688 /* Extract a substring from STRING, starting at SINDEX and ending with
689 one of the characters in CHARLIST. Don't make the ending character
690 part of the string. Leave SINDEX pointing at the ending character.
691 Understand about backslashes in the string. If (flags & SX_VARNAME)
692 is non-zero, and array variables have been compiled into the shell,
693 everything between a `[' and a corresponding `]' is skipped over.
694 If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
695 update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
696 contain a closing character from CHARLIST. */
698 string_extract (string, sindex, charlist, flags)
710 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
713 while (c = string[i])
722 #if defined (ARRAY_VARS)
723 else if ((flags & SX_VARNAME) && c == '[')
726 /* If this is an array subscript, skip over it and continue. */
727 ni = skipsubscript (string, i, 0);
728 if (string[ni] == ']')
732 else if (MEMBER (c, charlist))
738 ADVANCE_CHAR (string, slen, i);
741 /* If we had to have a matching delimiter and didn't find one, return an
742 error and let the caller deal with it. */
743 if ((flags & SX_REQMATCH) && found == 0)
746 return (&extract_string_error);
749 temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
755 /* Extract the contents of STRING as if it is enclosed in double quotes.
756 SINDEX, when passed in, is the offset of the character immediately
757 following the opening double quote; on exit, SINDEX is left pointing after
758 the closing double quote. If STRIPDQ is non-zero, unquoted double
759 quotes are stripped and the string is terminated by a null byte.
760 Backslashes between the embedded double quotes are processed. If STRIPDQ
761 is zero, an unquoted `"' terminates the string. */
763 string_extract_double_quoted (string, sindex, stripdq)
765 int *sindex, stripdq;
771 char *temp, *ret; /* The new string we return. */
772 int pass_next, backquote, si; /* State variables for the machine. */
776 slen = strlen (string + *sindex) + *sindex;
777 send = string + slen;
779 pass_next = backquote = dquote = 0;
780 temp = (char *)xmalloc (1 + slen - *sindex);
784 while (c = string[i])
786 /* Process a character that was quoted by a backslash. */
789 /* XXX - take another look at this in light of Interp 221 */
792 ``The backslash shall retain its special meaning as an escape
793 character only when followed by one of the characters:
796 If STRIPDQ is zero, we handle the double quotes here and let
797 expand_word_internal handle the rest. If STRIPDQ is non-zero,
798 we have already been through one round of backslash stripping,
799 and want to strip these backslashes only if DQUOTE is non-zero,
800 indicating that we are inside an embedded double-quoted string. */
802 /* If we are in an embedded quoted string, then don't strip
803 backslashes before characters for which the backslash
804 retains its special meaning, but remove backslashes in
805 front of other characters. If we are not in an
806 embedded quoted string, don't strip backslashes at all.
807 This mess is necessary because the string was already
808 surrounded by double quotes (and sh has some really weird
810 The returned string will be run through expansion as if
811 it were double-quoted. */
812 if ((stripdq == 0 && c != '"') ||
813 (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
818 COPY_CHAR_I (temp, j, string, send, i);
822 /* A backslash protects the next character. The code just above
823 handles preserving the backslash in front of any character but
832 /* Inside backquotes, ``the portion of the quoted string from the
833 initial backquote and the characters up to the next backquote
834 that is not preceded by a backslash, having escape characters
835 removed, defines that command''. */
853 /* Pass everything between `$(' and the matching `)' or a quoted
854 ${ ... } pair through according to the Posix.2 specification. */
855 if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
860 if (string[i + 1] == LPAREN)
861 ret = extract_command_subst (string, &si, 0);
863 ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, 0);
866 temp[j++] = string[i + 1];
868 /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
870 if (ret == 0 && no_longjmp_on_fatal_error)
873 ret = string + i + 2;
876 for (t = 0; ret[t]; t++, j++)
878 temp[j] = string[si];
893 /* Add any character but a double quote to the quoted string we're
896 goto add_one_character;
910 /* Point to after the closing quote. */
918 /* This should really be another option to string_extract_double_quoted. */
920 skip_double_quoted (string, slen, sind)
927 int pass_next, backquote, si;
930 pass_next = backquote = 0;
932 while (c = string[i])
937 ADVANCE_CHAR (string, slen, i);
950 ADVANCE_CHAR (string, slen, i);
959 else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
962 if (string[i + 1] == LPAREN)
963 ret = extract_command_subst (string, &si, SX_NOALLOC);
965 ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, SX_NOALLOC);
972 ADVANCE_CHAR (string, slen, i);
985 /* Extract the contents of STRING as if it is enclosed in single quotes.
986 SINDEX, when passed in, is the offset of the character immediately
987 following the opening single quote; on exit, SINDEX is left pointing after
988 the closing single quote. */
990 string_extract_single_quoted (string, sindex)
999 /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
1000 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
1002 while (string[i] && string[i] != '\'')
1003 ADVANCE_CHAR (string, slen, i);
1005 t = substring (string, *sindex, i);
1015 skip_single_quoted (string, slen, sind)
1024 while (string[c] && string[c] != '\'')
1025 ADVANCE_CHAR (string, slen, c);
1032 /* Just like string_extract, but doesn't hack backslashes or any of
1033 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
1035 string_extract_verbatim (string, slen, sindex, charlist, flags)
1043 #if defined (HANDLE_MULTIBYTE)
1051 if (charlist[0] == '\'' && charlist[1] == '\0')
1053 temp = string_extract_single_quoted (string, sindex);
1054 --*sindex; /* leave *sindex at separator character */
1060 /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need
1061 this only if MB_CUR_MAX > 1. */
1062 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 1;
1064 #if defined (HANDLE_MULTIBYTE)
1065 clen = strlen (charlist);
1068 while (c = string[i])
1070 #if defined (HANDLE_MULTIBYTE)
1073 if ((flags & SX_NOCTLESC) == 0 && c == CTLESC)
1078 /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
1079 through, to protect the CTLNULs from later calls to
1080 remove_quoted_nulls. */
1081 else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC && string[i+1] == CTLNUL)
1087 #if defined (HANDLE_MULTIBYTE)
1088 mblength = MBLEN (string + i, slen - i);
1092 mblength = mbtowc (&wc, string + i, slen - i);
1093 if (MB_INVALIDCH (mblength))
1095 if (MEMBER (c, charlist))
1103 len = mbstowcs (wcharlist, charlist, 0);
1106 wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1));
1107 mbstowcs (wcharlist, charlist, len + 1);
1110 if (wcschr (wcharlist, wc))
1116 if (MEMBER (c, charlist))
1119 ADVANCE_CHAR (string, slen, i);
1122 #if defined (HANDLE_MULTIBYTE)
1126 temp = substring (string, *sindex, i);
1132 /* Extract the $( construct in STRING, and return a new string.
1133 Start extracting at (SINDEX) as if we had just seen "$(".
1134 Make (SINDEX) get the position of the matching ")". )
1135 XFLAGS is additional flags to pass to other extraction functions. */
1137 extract_command_subst (string, sindex, xflags)
1142 if (string[*sindex] == LPAREN)
1143 return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/
1146 xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
1147 return (xparse_dolparen (string, string+*sindex, sindex, xflags));
1151 /* Extract the $[ construct in STRING, and return a new string. (])
1152 Start extracting at (SINDEX) as if we had just seen "$[".
1153 Make (SINDEX) get the position of the matching "]". */
1155 extract_arithmetic_subst (string, sindex)
1159 return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/
1162 #if defined (PROCESS_SUBSTITUTION)
1163 /* Extract the <( or >( construct in STRING, and return a new string.
1164 Start extracting at (SINDEX) as if we had just seen "<(".
1165 Make (SINDEX) get the position of the matching ")". */ /*))*/
1167 extract_process_subst (string, starter, sindex)
1172 return (extract_delimited_string (string, sindex, starter, "(", ")", 0));
1174 #endif /* PROCESS_SUBSTITUTION */
1176 #if defined (ARRAY_VARS)
1177 /* This can be fooled by unquoted right parens in the passed string. If
1178 each caller verifies that the last character in STRING is a right paren,
1179 we don't even need to call extract_delimited_string. */
1181 extract_array_assignment_list (string, sindex)
1188 slen = strlen (string); /* ( */
1189 if (string[slen - 1] == ')')
1191 ret = substring (string, *sindex, slen - 1);
1199 /* Extract and create a new string from the contents of STRING, a
1200 character string delimited with OPENER and CLOSER. SINDEX is
1201 the address of an int describing the current offset in STRING;
1202 it should point to just after the first OPENER found. On exit,
1203 SINDEX gets the position of the last character of the matching CLOSER.
1204 If OPENER is more than a single character, ALT_OPENER, if non-null,
1205 contains a character string that can also match CLOSER and thus
1206 needs to be skipped. */
1208 extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
1211 char *opener, *alt_opener, *closer;
1217 int pass_character, nesting_level, in_comment;
1218 int len_closer, len_opener, len_alt_opener;
1221 slen = strlen (string + *sindex) + *sindex;
1222 len_opener = STRLEN (opener);
1223 len_alt_opener = STRLEN (alt_opener);
1224 len_closer = STRLEN (closer);
1226 pass_character = in_comment = 0;
1231 while (nesting_level)
1242 ADVANCE_CHAR (string, slen, i);
1246 if (pass_character) /* previous char was backslash */
1249 ADVANCE_CHAR (string, slen, i);
1253 /* Not exactly right yet; should handle shell metacharacters and
1254 multibyte characters, too. See COMMENT_BEGIN define in parse.y */
1255 if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1])))
1258 ADVANCE_CHAR (string, slen, i);
1262 if (c == CTLESC || c == '\\')
1269 /* Process a nested command substitution, but only if we're parsing an
1270 arithmetic substitution. */
1271 if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN)
1274 t = extract_command_subst (string, &si, flags|SX_NOALLOC);
1279 /* Process a nested OPENER. */
1280 if (STREQN (string + i, opener, len_opener))
1282 si = i + len_opener;
1283 t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC);
1288 /* Process a nested ALT_OPENER */
1289 if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
1291 si = i + len_alt_opener;
1292 t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC);
1297 /* If the current substring terminates the delimited string, decrement
1298 the nesting level. */
1299 if (STREQN (string + i, closer, len_closer))
1301 i += len_closer - 1; /* move to last byte of the closer */
1303 if (nesting_level == 0)
1307 /* Pass old-style command substitution through verbatim. */
1311 t = string_extract (string, &si, "`", flags|SX_NOALLOC);
1316 /* Pass single-quoted and double-quoted strings through verbatim. */
1317 if (c == '\'' || c == '"')
1320 i = (c == '\'') ? skip_single_quoted (string, slen, si)
1321 : skip_double_quoted (string, slen, si);
1325 /* move past this character, which was not special. */
1326 ADVANCE_CHAR (string, slen, i);
1329 if (c == 0 && nesting_level)
1331 if (no_longjmp_on_fatal_error == 0)
1333 report_error (_("bad substitution: no closing `%s' in %s"), closer, string);
1334 last_command_exit_value = EXECUTION_FAILURE;
1335 exp_jump_to_top_level (DISCARD);
1340 return (char *)NULL;
1344 si = i - *sindex - len_closer + 1;
1345 if (flags & SX_NOALLOC)
1346 result = (char *)NULL;
1349 result = (char *)xmalloc (1 + si);
1350 strncpy (result, string + *sindex, si);
1358 /* Extract a parameter expansion expression within ${ and } from STRING.
1359 Obey the Posix.2 rules for finding the ending `}': count braces while
1360 skipping over enclosed quoted strings and command substitutions.
1361 SINDEX is the address of an int describing the current offset in STRING;
1362 it should point to just after the first `{' found. On exit, SINDEX
1363 gets the position of the matching `}'. QUOTED is non-zero if this
1364 occurs inside double quotes. */
1365 /* XXX -- this is very similar to extract_delimited_string -- XXX */
1367 extract_dollar_brace_string (string, sindex, quoted, flags)
1369 int *sindex, quoted, flags;
1373 int pass_character, nesting_level, si, dolbrace_state;
1379 slen = strlen (string + *sindex) + *sindex;
1381 /* The handling of dolbrace_state needs to agree with the code in parse.y:
1382 parse_matched_pair(). The different initial value is to handle the
1383 case where this function is called to parse the word in
1384 ${param op word} (SX_WORD). */
1385 dolbrace_state = (flags & SX_WORD) ? DOLBRACE_WORD : DOLBRACE_PARAM;
1386 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && (flags & SX_POSIXEXP))
1387 dolbrace_state = DOLBRACE_QUOTE;
1390 while (c = string[i])
1395 ADVANCE_CHAR (string, slen, i);
1399 /* CTLESCs and backslashes quote the next character. */
1400 if (c == CTLESC || c == '\\')
1407 if (string[i] == '$' && string[i+1] == LBRACE)
1417 if (nesting_level == 0)
1423 /* Pass the contents of old-style command substitutions through
1428 t = string_extract (string, &si, "`", flags|SX_NOALLOC);
1433 /* Pass the contents of new-style command substitutions and
1434 arithmetic substitutions through verbatim. */
1435 if (string[i] == '$' && string[i+1] == LPAREN)
1438 t = extract_command_subst (string, &si, flags|SX_NOALLOC);
1444 /* Pass the contents of single-quoted and double-quoted strings
1445 through verbatim. */
1446 if (c == '\'' || c == '"')
1449 i = (c == '\'') ? skip_single_quoted (string, slen, si)
1450 : skip_double_quoted (string, slen, si);
1451 /* skip_XXX_quoted leaves index one past close quote */
1454 #else /* XXX - bash-4.2 */
1455 /* Pass the contents of double-quoted strings through verbatim. */
1459 i = skip_double_quoted (string, slen, si);
1460 /* skip_XXX_quoted leaves index one past close quote */
1466 /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/
1467 if (posixly_correct && shell_compatibility_level > 41 && dolbrace_state != DOLBRACE_QUOTE && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
1468 ADVANCE_CHAR (string, slen, i);
1472 i = skip_single_quoted (string, slen, si);
1479 /* move past this character, which was not special. */
1480 ADVANCE_CHAR (string, slen, i);
1482 /* This logic must agree with parse.y:parse_matched_pair, since they
1483 share the same defines. */
1484 if (dolbrace_state == DOLBRACE_PARAM && c == '%' && (i - *sindex) > 1)
1485 dolbrace_state = DOLBRACE_QUOTE;
1486 else if (dolbrace_state == DOLBRACE_PARAM && c == '#' && (i - *sindex) > 1)
1487 dolbrace_state = DOLBRACE_QUOTE;
1488 else if (dolbrace_state == DOLBRACE_PARAM && c == '/' && (i - *sindex) > 1)
1489 dolbrace_state = DOLBRACE_QUOTE;
1490 else if (dolbrace_state == DOLBRACE_PARAM && c == '^' && (i - *sindex) > 1)
1491 dolbrace_state = DOLBRACE_QUOTE;
1492 else if (dolbrace_state == DOLBRACE_PARAM && c == ',' && (i - *sindex) > 1)
1493 dolbrace_state = DOLBRACE_QUOTE;
1494 else if (dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", c) != 0)
1495 dolbrace_state = DOLBRACE_OP;
1496 else if (dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", c) == 0)
1497 dolbrace_state = DOLBRACE_WORD;
1500 if (c == 0 && nesting_level)
1502 if (no_longjmp_on_fatal_error == 0)
1504 report_error (_("bad substitution: no closing `%s' in %s"), "}", string);
1505 last_command_exit_value = EXECUTION_FAILURE;
1506 exp_jump_to_top_level (DISCARD);
1511 return ((char *)NULL);
1515 result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
1521 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1522 STRING, and returns a pointer to it. */
1524 de_backslash (string)
1527 register size_t slen;
1528 register int i, j, prev_i;
1531 slen = strlen (string);
1534 /* Loop copying string[i] to string[j], i >= j. */
1537 if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
1538 string[i + 1] == '$'))
1541 ADVANCE_CHAR (string, slen, i);
1543 do string[j++] = string[prev_i++]; while (prev_i < i);
1554 /* Replace instances of \! in a string with !. */
1556 unquote_bang (string)
1560 register char *temp;
1562 temp = (char *)xmalloc (1 + strlen (string));
1564 for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
1566 if (string[i] == '\\' && string[i + 1] == '!')
1572 strcpy (string, temp);
1577 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0)
1579 /* This function assumes s[i] == open; returns with s[ret] == close; used to
1580 parse array subscripts. FLAGS & 1 means to not attempt to skip over
1581 matched pairs of quotes or backquotes, or skip word expansions; it is
1582 intended to be used after expansion has been performed and during final
1583 assignment parsing (see arrayfunc.c:assign_compound_array_list()). */
1585 skip_matched_pair (string, start, open, close, flags)
1587 int start, open, close, flags;
1589 int i, pass_next, backq, si, c, count;
1594 slen = strlen (string + start) + start;
1595 no_longjmp_on_fatal_error = 1;
1597 i = start + 1; /* skip over leading bracket */
1599 pass_next = backq = 0;
1600 ss = (char *)string;
1601 while (c = string[i])
1608 ADVANCE_CHAR (string, slen, i);
1621 ADVANCE_CHAR (string, slen, i);
1624 else if ((flags & 1) == 0 && c == '`')
1630 else if ((flags & 1) == 0 && c == open)
1636 else if (c == close)
1644 else if ((flags & 1) == 0 && (c == '\'' || c == '"'))
1646 i = (c == '\'') ? skip_single_quoted (ss, slen, ++i)
1647 : skip_double_quoted (ss, slen, ++i);
1648 /* no increment, the skip functions increment past the closing quote. */
1650 else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
1653 if (string[si] == '\0')
1656 if (string[i+1] == LPAREN)
1657 temp = extract_delimited_string (ss, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
1659 temp = extract_dollar_brace_string (ss, &si, 0, SX_NOALLOC);
1661 if (string[i] == '\0') /* don't increment i past EOS in loop */
1667 ADVANCE_CHAR (string, slen, i);
1673 #if defined (ARRAY_VARS)
1675 skipsubscript (string, start, flags)
1679 return (skip_matched_pair (string, start, '[', ']', flags));
1683 /* Skip characters in STRING until we find a character in DELIMS, and return
1684 the index of that character. START is the index into string at which we
1685 begin. This is similar in spirit to strpbrk, but it returns an index into
1686 STRING and takes a starting index. This little piece of code knows quite
1687 a lot of shell syntax. It's very similar to skip_double_quoted and other
1688 functions of that ilk. */
1690 skip_to_delim (string, start, delims, flags)
1696 int i, pass_next, backq, si, c, invert, skipquote, skipcmd;
1698 char *temp, open[3];
1701 slen = strlen (string + start) + start;
1702 if (flags & SD_NOJMP)
1703 no_longjmp_on_fatal_error = 1;
1704 invert = (flags & SD_INVERT);
1705 skipcmd = (flags & SD_NOSKIPCMD) == 0;
1708 pass_next = backq = 0;
1709 while (c = string[i])
1711 /* If this is non-zero, we should not let quote characters be delimiters
1712 and the current character is a single or double quote. We should not
1713 test whether or not it's a delimiter until after we skip single- or
1714 double-quoted strings. */
1715 skipquote = ((flags & SD_NOQUOTEDELIM) && (c == '\'' || c =='"'));
1721 ADVANCE_CHAR (string, slen, i);
1734 ADVANCE_CHAR (string, slen, i);
1743 else if (skipquote == 0 && invert == 0 && member (c, delims))
1745 else if (c == '\'' || c == '"')
1747 i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
1748 : skip_double_quoted (string, slen, ++i);
1749 /* no increment, the skip functions increment past the closing quote. */
1751 else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE))
1754 if (string[si] == '\0')
1757 if (string[i+1] == LPAREN)
1758 temp = extract_delimited_string (string, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
1760 temp = extract_dollar_brace_string (string, &si, 0, SX_NOALLOC);
1762 if (string[i] == '\0') /* don't increment i past EOS in loop */
1767 #if defined (PROCESS_SUBSTITUTION)
1768 else if (skipcmd && (c == '<' || c == '>') && string[i+1] == LPAREN)
1771 if (string[si] == '\0')
1773 temp = extract_process_subst (string, (c == '<') ? "<(" : ">(", &si);
1775 if (string[i] == '\0')
1780 #endif /* PROCESS_SUBSTITUTION */
1781 #if defined (EXTENDED_GLOB)
1782 else if ((flags & SD_EXTGLOB) && extended_glob && string[i+1] == LPAREN && member (c, "?*+!@"))
1785 if (string[si] == '\0')
1791 temp = extract_delimited_string (string, &si, open, "(", ")", SX_NOALLOC); /* ) */
1794 if (string[i] == '\0') /* don't increment i past EOS in loop */
1800 else if ((skipquote || invert) && (member (c, delims) == 0))
1803 ADVANCE_CHAR (string, slen, i);
1809 #if defined (READLINE)
1810 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
1811 an unclosed quoted string), or if the character at EINDEX is quoted
1812 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
1813 single and double-quoted string parsing functions should not return an
1814 error if there are unclosed quotes or braces. The characters that this
1815 recognizes need to be the same as the contents of
1816 rl_completer_quote_characters. */
1819 char_is_quoted (string, eindex)
1823 int i, pass_next, c;
1827 slen = strlen (string);
1828 no_longjmp_on_fatal_error = 1;
1837 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
1839 ADVANCE_CHAR (string, slen, i);
1848 else if (c == '\'' || c == '"')
1850 i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
1851 : skip_double_quoted (string, slen, ++i);
1854 /* no increment, the skip_xxx functions go one past end */
1857 ADVANCE_CHAR (string, slen, i);
1864 unclosed_pair (string, eindex, openstr)
1869 int i, pass_next, openc, olen;
1873 slen = strlen (string);
1874 olen = strlen (openstr);
1875 i = pass_next = openc = 0;
1881 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
1883 ADVANCE_CHAR (string, slen, i);
1886 else if (string[i] == '\\')
1892 else if (STREQN (string + i, openstr, olen))
1897 else if (string[i] == '\'' || string[i] == '"')
1899 i = (string[i] == '\'') ? skip_single_quoted (string, slen, i)
1900 : skip_double_quoted (string, slen, i);
1905 ADVANCE_CHAR (string, slen, i);
1910 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
1911 individual words. If DELIMS is NULL, the current value of $IFS is used
1912 to split the string, and the function follows the shell field splitting
1913 rules. SENTINEL is an index to look for. NWP, if non-NULL,
1914 gets the number of words in the returned list. CWP, if non-NULL, gets
1915 the index of the word containing SENTINEL. Non-whitespace chars in
1916 DELIMS delimit separate fields. */
1918 split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp)
1922 int sentinel, flags;
1925 int ts, te, i, nw, cw, ifs_split, dflags;
1926 char *token, *d, *d2;
1927 WORD_LIST *ret, *tl;
1929 if (string == 0 || *string == '\0')
1935 return ((WORD_LIST *)NULL);
1938 d = (delims == 0) ? ifs_value : delims;
1939 ifs_split = delims == 0;
1941 /* Make d2 the non-whitespace characters in delims */
1946 #if defined (HANDLE_MULTIBYTE)
1947 size_t mblength = 1;
1951 slength = strlen (delims);
1952 d2 = (char *)xmalloc (slength + 1);
1956 #if defined (HANDLE_MULTIBYTE)
1957 mbstate_t state_bak;
1959 mblength = MBRLEN (delims + i, slength, &state);
1960 if (MB_INVALIDCH (mblength))
1962 else if (mblength > 1)
1964 memcpy (d2 + ts, delims + i, mblength);
1967 slength -= mblength;
1971 if (whitespace (delims[i]) == 0)
1972 d2[ts++] = delims[i];
1980 ret = (WORD_LIST *)NULL;
1982 /* Remove sequences of whitespace characters at the start of the string, as
1983 long as those characters are delimiters. */
1984 for (i = 0; member (string[i], d) && spctabnl (string[i]); i++)
1986 if (string[i] == '\0')
1992 dflags = flags|SD_NOJMP;
1995 te = skip_to_delim (string, ts, d, dflags);
1997 /* If we have a non-whitespace delimiter character, use it to make a
1998 separate field. This is just about what $IFS splitting does and
1999 is closer to the behavior of the shell parser. */
2000 if (ts == te && d2 && member (string[ts], d2))
2003 /* If we're using IFS splitting, the non-whitespace delimiter char
2004 and any additional IFS whitespace delimits a field. */
2006 while (member (string[te], d) && spctabnl (string[te]))
2009 while (member (string[te], d2))
2013 token = substring (string, ts, te);
2015 ret = add_string_to_list (token, ret);
2019 if (sentinel >= ts && sentinel <= te)
2022 /* If the cursor is at whitespace just before word start, set the
2023 sentinel word to the current word. */
2024 if (cwp && cw == -1 && sentinel == ts-1)
2027 /* If the cursor is at whitespace between two words, make a new, empty
2028 word, add it before (well, after, since the list is in reverse order)
2029 the word we just added, and set the current word to that one. */
2030 if (cwp && cw == -1 && sentinel < ts)
2032 tl = make_word_list (make_word (""), ret->next);
2038 if (string[te] == 0)
2042 while (member (string[i], d) && (ifs_split || spctabnl(string[i])))
2051 /* Special case for SENTINEL at the end of STRING. If we haven't found
2052 the word containing SENTINEL yet, and the index we're looking for is at
2053 the end of STRING (or past the end of the previously-found token,
2054 possible if the end of the line is composed solely of IFS whitespace)
2055 add an additional null argument and set the current word pointer to that. */
2056 if (cwp && cw == -1 && (sentinel >= slen || sentinel >= te))
2058 if (whitespace (string[sentinel - 1]))
2061 ret = add_string_to_list (token, ret);
2072 return (REVERSE_LIST (ret, WORD_LIST *));
2074 #endif /* READLINE */
2078 /* Extract the name of the variable to bind to from the assignment string. */
2080 assignment_name (string)
2086 offset = assignment (string, 0);
2088 return (char *)NULL;
2089 temp = substring (string, 0, offset);
2094 /* **************************************************************** */
2096 /* Functions to convert strings to WORD_LISTs and vice versa */
2098 /* **************************************************************** */
2100 /* Return a single string of all the words in LIST. SEP is the separator
2101 to put between individual elements of LIST in the output string. */
2103 string_list_internal (list, sep)
2107 register WORD_LIST *t;
2109 int word_len, sep_len, result_size;
2112 return ((char *)NULL);
2114 /* Short-circuit quickly if we don't need to separate anything. */
2115 if (list->next == 0)
2116 return (savestring (list->word->word));
2118 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
2119 sep_len = STRLEN (sep);
2122 for (t = list; t; t = t->next)
2125 result_size += sep_len;
2126 result_size += strlen (t->word->word);
2129 r = result = (char *)xmalloc (result_size + 1);
2131 for (t = list; t; t = t->next)
2133 if (t != list && sep_len)
2137 FASTCOPY (sep, r, sep_len);
2144 word_len = strlen (t->word->word);
2145 FASTCOPY (t->word->word, r, word_len);
2153 /* Return a single string of all the words present in LIST, separating
2154 each word with a space. */
2159 return (string_list_internal (list, " "));
2162 /* An external interface that can be used by the rest of the shell to
2163 obtain a string containing the first character in $IFS. Handles all
2164 the multibyte complications. If LENP is non-null, it is set to the
2165 length of the returned string. */
2167 ifs_firstchar (lenp)
2173 ret = xmalloc (MB_LEN_MAX + 1);
2174 #if defined (HANDLE_MULTIBYTE)
2175 if (ifs_firstc_len == 1)
2177 ret[0] = ifs_firstc[0];
2179 len = ret[0] ? 1 : 0;
2183 memcpy (ret, ifs_firstc, ifs_firstc_len);
2184 ret[len = ifs_firstc_len] = '\0';
2187 ret[0] = ifs_firstc;
2189 len = ret[0] ? 0 : 1;
2198 /* Return a single string of all the words present in LIST, obeying the
2199 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
2200 expansion [of $*] appears within a double quoted string, it expands
2201 to a single field with the value of each parameter separated by the
2202 first character of the IFS variable, or by a <space> if IFS is unset." */
2204 string_list_dollar_star (list)
2208 #if defined (HANDLE_MULTIBYTE)
2209 # if defined (__GNUC__)
2210 char sep[MB_CUR_MAX + 1];
2218 #if defined (HANDLE_MULTIBYTE)
2219 # if !defined (__GNUC__)
2220 sep = (char *)xmalloc (MB_CUR_MAX + 1);
2221 # endif /* !__GNUC__ */
2222 if (ifs_firstc_len == 1)
2224 sep[0] = ifs_firstc[0];
2229 memcpy (sep, ifs_firstc, ifs_firstc_len);
2230 sep[ifs_firstc_len] = '\0';
2233 sep[0] = ifs_firstc;
2237 ret = string_list_internal (list, sep);
2238 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2244 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
2245 is non-zero, the $@ appears within double quotes, and we should quote
2246 the list before converting it into a string. If IFS is unset, and the
2247 word is not quoted, we just need to quote CTLESC and CTLNUL characters
2248 in the words in the list, because the default value of $IFS is
2249 <space><tab><newline>, IFS characters in the words in the list should
2250 also be split. If IFS is null, and the word is not quoted, we need
2251 to quote the words in the list to preserve the positional parameters
2254 string_list_dollar_at (list, quoted)
2259 #if defined (HANDLE_MULTIBYTE)
2260 # if defined (__GNUC__)
2261 char sep[MB_CUR_MAX + 1];
2264 # endif /* !__GNUC__ */
2270 /* XXX this could just be ifs = ifs_value; */
2271 ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
2273 #if defined (HANDLE_MULTIBYTE)
2274 # if !defined (__GNUC__)
2275 sep = (char *)xmalloc (MB_CUR_MAX + 1);
2276 # endif /* !__GNUC__ */
2279 if (ifs_firstc_len == 1)
2281 sep[0] = ifs_firstc[0];
2286 memcpy (sep, ifs_firstc, ifs_firstc_len);
2287 sep[ifs_firstc_len] = '\0';
2296 sep[0] = (ifs == 0 || *ifs == 0) ? ' ' : *ifs;
2300 /* XXX -- why call quote_list if ifs == 0? we can get away without doing
2301 it now that quote_escapes quotes spaces */
2302 tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
2304 : list_quote_escapes (list);
2306 ret = string_list_internal (tlist, sep);
2307 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2313 /* Turn the positional paramters into a string, understanding quoting and
2314 the various subtleties of using the first character of $IFS as the
2315 separator. Calls string_list_dollar_at, string_list_dollar_star, and
2316 string_list as appropriate. */
2318 string_list_pos_params (pchar, list, quoted)
2326 if (pchar == '*' && (quoted & Q_DOUBLE_QUOTES))
2328 tlist = quote_list (list);
2329 word_list_remove_quoted_nulls (tlist);
2330 ret = string_list_dollar_star (tlist);
2332 else if (pchar == '*' && (quoted & Q_HERE_DOCUMENT))
2334 tlist = quote_list (list);
2335 word_list_remove_quoted_nulls (tlist);
2336 ret = string_list (tlist);
2338 else if (pchar == '*')
2340 /* Even when unquoted, string_list_dollar_star does the right thing
2341 making sure that the first character of $IFS is used as the
2343 ret = string_list_dollar_star (list);
2345 else if (pchar == '@' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
2346 /* We use string_list_dollar_at, but only if the string is quoted, since
2347 that quotes the escapes if it's not, which we don't want. We could
2348 use string_list (the old code did), but that doesn't do the right
2349 thing if the first character of $IFS is not a space. We use
2350 string_list_dollar_star if the string is unquoted so we make sure that
2351 the elements of $@ are separated by the first character of $IFS for
2353 ret = string_list_dollar_at (list, quoted);
2354 else if (pchar == '@')
2355 ret = string_list_dollar_star (list);
2357 ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list);
2362 /* Return the list of words present in STRING. Separate the string into
2363 words at any of the characters found in SEPARATORS. If QUOTED is
2364 non-zero then word in the list will have its quoted flag set, otherwise
2365 the quoted flag is left as make_word () deemed fit.
2367 This obeys the P1003.2 word splitting semantics. If `separators' is
2368 exactly <space><tab><newline>, then the splitting algorithm is that of
2369 the Bourne shell, which treats any sequence of characters from `separators'
2370 as a delimiter. If IFS is unset, which results in `separators' being set
2371 to "", no splitting occurs. If separators has some other value, the
2372 following rules are applied (`IFS white space' means zero or more
2373 occurrences of <space>, <tab>, or <newline>, as long as those characters
2374 are in `separators'):
2376 1) IFS white space is ignored at the start and the end of the
2378 2) Each occurrence of a character in `separators' that is not
2379 IFS white space, along with any adjacent occurrences of
2380 IFS white space delimits a field.
2381 3) Any nonzero-length sequence of IFS white space delimits a field.
2384 /* BEWARE! list_string strips null arguments. Don't call it twice and
2385 expect to have "" preserved! */
2387 /* This performs word splitting and quoted null character removal on
2390 (((separators)[0]) ? ((separators)[1] ? isifs(c) \
2391 : (c) == (separators)[0]) \
2395 list_string (string, separators, quoted)
2396 register char *string, *separators;
2401 char *current_word, *s;
2402 int sindex, sh_style_split, whitesep, xflags;
2405 if (!string || !*string)
2406 return ((WORD_LIST *)NULL);
2408 sh_style_split = separators && separators[0] == ' ' &&
2409 separators[1] == '\t' &&
2410 separators[2] == '\n' &&
2411 separators[3] == '\0';
2412 for (xflags = 0, s = ifs_value; s && *s; s++)
2414 if (*s == CTLESC) xflags |= SX_NOCTLESC;
2415 else if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
2419 /* Remove sequences of whitespace at the beginning of STRING, as
2420 long as those characters appear in IFS. Do not do this if
2421 STRING is quoted or if there are no separator characters. */
2422 if (!quoted || !separators || !*separators)
2424 for (s = string; *s && spctabnl (*s) && issep (*s); s++);
2427 return ((WORD_LIST *)NULL);
2432 /* OK, now STRING points to a word that does not begin with white space.
2433 The splitting algorithm is:
2434 extract a word, stopping at a separator
2435 skip sequences of spc, tab, or nl as long as they are separators
2436 This obeys the field splitting rules in Posix.2. */
2437 slen = (MB_CUR_MAX > 1) ? strlen (string) : 1;
2438 for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
2440 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2441 unless multibyte chars are possible. */
2442 current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags);
2443 if (current_word == 0)
2446 /* If we have a quoted empty string, add a quoted null argument. We
2447 want to preserve the quoted null character iff this is a quoted
2448 empty string; otherwise the quoted null characters are removed
2450 if (QUOTED_NULL (current_word))
2452 t = alloc_word_desc ();
2453 t->word = make_quoted_char ('\0');
2454 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
2455 result = make_word_list (t, result);
2457 else if (current_word[0] != '\0')
2459 /* If we have something, then add it regardless. However,
2460 perform quoted null character removal on the current word. */
2461 remove_quoted_nulls (current_word);
2462 result = add_string_to_list (current_word, result);
2463 result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */
2464 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
2465 result->word->flags |= W_QUOTED;
2468 /* If we're not doing sequences of separators in the traditional
2469 Bourne shell style, then add a quoted null argument. */
2470 else if (!sh_style_split && !spctabnl (string[sindex]))
2472 t = alloc_word_desc ();
2473 t->word = make_quoted_char ('\0');
2474 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
2475 result = make_word_list (t, result);
2478 free (current_word);
2480 /* Note whether or not the separator is IFS whitespace, used later. */
2481 whitesep = string[sindex] && spctabnl (string[sindex]);
2483 /* Move past the current separator character. */
2487 ADVANCE_CHAR (string, slen, sindex);
2490 /* Now skip sequences of space, tab, or newline characters if they are
2491 in the list of separators. */
2492 while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex]))
2495 /* If the first separator was IFS whitespace and the current character
2496 is a non-whitespace IFS character, it should be part of the current
2497 field delimiter, not a separate delimiter that would result in an
2498 empty field. Look at POSIX.2, 3.6.5, (3)(b). */
2499 if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex]))
2502 /* An IFS character that is not IFS white space, along with any
2503 adjacent IFS white space, shall delimit a field. (SUSv3) */
2504 while (string[sindex] && spctabnl (string[sindex]) && isifs (string[sindex]))
2508 return (REVERSE_LIST (result, WORD_LIST *));
2511 /* Parse a single word from STRING, using SEPARATORS to separate fields.
2512 ENDPTR is set to the first character after the word. This is used by
2513 the `read' builtin. This is never called with SEPARATORS != $IFS;
2514 it should be simplified.
2516 XXX - this function is very similar to list_string; they should be
2519 get_word_from_string (stringp, separators, endptr)
2520 char **stringp, *separators, **endptr;
2524 int sindex, sh_style_split, whitesep, xflags;
2527 if (!stringp || !*stringp || !**stringp)
2528 return ((char *)NULL);
2530 sh_style_split = separators && separators[0] == ' ' &&
2531 separators[1] == '\t' &&
2532 separators[2] == '\n' &&
2533 separators[3] == '\0';
2534 for (xflags = 0, s = ifs_value; s && *s; s++)
2536 if (*s == CTLESC) xflags |= SX_NOCTLESC;
2537 if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
2543 /* Remove sequences of whitespace at the beginning of STRING, as
2544 long as those characters appear in IFS. */
2545 if (sh_style_split || !separators || !*separators)
2547 for (; *s && spctabnl (*s) && isifs (*s); s++);
2549 /* If the string is nothing but whitespace, update it and return. */
2555 return ((char *)NULL);
2559 /* OK, S points to a word that does not begin with white space.
2560 Now extract a word, stopping at a separator, save a pointer to
2561 the first character after the word, then skip sequences of spc,
2562 tab, or nl as long as they are separators.
2564 This obeys the field splitting rules in Posix.2. */
2566 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2567 unless multibyte chars are possible. */
2568 slen = (MB_CUR_MAX > 1) ? strlen (s) : 1;
2569 current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags);
2571 /* Set ENDPTR to the first character after the end of the word. */
2573 *endptr = s + sindex;
2575 /* Note whether or not the separator is IFS whitespace, used later. */
2576 whitesep = s[sindex] && spctabnl (s[sindex]);
2578 /* Move past the current separator character. */
2582 ADVANCE_CHAR (s, slen, sindex);
2585 /* Now skip sequences of space, tab, or newline characters if they are
2586 in the list of separators. */
2587 while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
2590 /* If the first separator was IFS whitespace and the current character is
2591 a non-whitespace IFS character, it should be part of the current field
2592 delimiter, not a separate delimiter that would result in an empty field.
2593 Look at POSIX.2, 3.6.5, (3)(b). */
2594 if (s[sindex] && whitesep && isifs (s[sindex]) && !spctabnl (s[sindex]))
2597 /* An IFS character that is not IFS white space, along with any adjacent
2598 IFS white space, shall delimit a field. */
2599 while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
2603 /* Update STRING to point to the next field. */
2604 *stringp = s + sindex;
2605 return (current_word);
2608 /* Remove IFS white space at the end of STRING. Start at the end
2609 of the string and walk backwards until the beginning of the string
2610 or we find a character that's not IFS white space and not CTLESC.
2611 Only let CTLESC escape a white space character if SAW_ESCAPE is
2614 strip_trailing_ifs_whitespace (string, separators, saw_escape)
2615 char *string, *separators;
2620 s = string + STRLEN (string) - 1;
2621 while (s > string && ((spctabnl (*s) && isifs (*s)) ||
2622 (saw_escape && *s == CTLESC && spctabnl (s[1]))))
2630 /* Split STRING into words at whitespace. Obeys shell-style quoting with
2631 backslashes, single and double quotes. */
2633 list_string_with_quotes (string)
2639 int c, i, tokstart, len;
2641 for (s = string; s && *s && spctabnl (*s); s++)
2643 if (s == 0 || *s == 0)
2644 return ((WORD_LIST *)NULL);
2648 list = (WORD_LIST *)NULL;
2659 i = skip_single_quoted (s, s_len, ++i);
2661 i = skip_double_quoted (s, s_len, ++i);
2662 else if (c == 0 || spctabnl (c))
2664 /* We have found the end of a token. Make a word out of it and
2665 add it to the word list. */
2666 token = substring (s, tokstart, i);
2667 list = add_string_to_list (token, list);
2669 while (spctabnl (s[i]))
2677 i++; /* normal character */
2679 return (REVERSE_LIST (list, WORD_LIST *));
2683 /********************************************************/
2685 /* Functions to perform assignment statements */
2687 /********************************************************/
2689 #if defined (ARRAY_VARS)
2691 do_compound_assignment (name, value, flags)
2696 int mklocal, mkassoc;
2699 mklocal = flags & ASS_MKLOCAL;
2700 mkassoc = flags & ASS_MKASSOC;
2702 if (mklocal && variable_context)
2704 v = find_variable (name);
2705 list = expand_compound_array_assignment (v, value, flags);
2707 v = make_local_assoc_variable (name);
2708 else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context)
2709 v = make_local_array_variable (name);
2710 assign_compound_array_list (v, list, flags);
2713 v = assign_array_from_string (name, value, flags);
2719 /* Given STRING, an assignment string, get the value of the right side
2720 of the `=', and bind it to the left side. If EXPAND is true, then
2721 perform parameter expansion, command substitution, and arithmetic
2722 expansion on the right-hand side. Perform tilde expansion in any
2723 case. Do not perform word splitting on the result of expansion. */
2725 do_assignment_internal (word, expand)
2726 const WORD_DESC *word;
2729 int offset, appendop, assign_list, aflags, retval;
2730 char *name, *value, *temp;
2732 #if defined (ARRAY_VARS)
2738 if (word == 0 || word->word == 0)
2741 appendop = assign_list = aflags = 0;
2742 string = word->word;
2743 offset = assignment (string, 0);
2744 name = savestring (string);
2745 value = (char *)NULL;
2747 if (name[offset] == '=')
2749 if (name[offset - 1] == '+')
2752 name[offset - 1] = '\0';
2755 name[offset] = 0; /* might need this set later */
2756 temp = name + offset + 1;
2758 #if defined (ARRAY_VARS)
2759 if (expand && (word->flags & W_COMPASSIGN))
2761 assign_list = ni = 1;
2762 value = extract_array_assignment_list (temp, &ni);
2766 if (expand && temp[0])
2767 value = expand_string_if_necessary (temp, 0, expand_string_assignment);
2769 value = savestring (temp);
2774 value = (char *)xmalloc (1);
2778 if (echo_command_at_execute)
2781 name[offset - 1] = '+';
2782 xtrace_print_assignment (name, value, assign_list, 1);
2784 name[offset - 1] = '\0';
2787 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
2790 aflags |= ASS_APPEND;
2792 #if defined (ARRAY_VARS)
2793 if (t = mbschr (name, '[')) /*]*/
2797 report_error (_("%s: cannot assign list to array member"), name);
2800 entry = assign_array_element (name, value, aflags);
2804 else if (assign_list)
2806 if (word->flags & W_ASSIGNARG)
2807 aflags |= ASS_MKLOCAL;
2808 if (word->flags & W_ASSIGNASSOC)
2809 aflags |= ASS_MKASSOC;
2810 entry = do_compound_assignment (name, value, aflags);
2813 #endif /* ARRAY_VARS */
2814 entry = bind_variable (name, value, aflags);
2816 stupidly_hack_special_variables (name);
2819 /* Return 1 if the assignment seems to have been performed correctly. */
2820 if (entry == 0 || readonly_p (entry))
2821 retval = 0; /* assignment failure */
2822 else if (noassign_p (entry))
2824 last_command_exit_value = EXECUTION_FAILURE;
2825 retval = 1; /* error status, but not assignment failure */
2830 if (entry && retval != 0 && noassign_p (entry) == 0)
2831 VUNSETATTR (entry, att_invisible);
2833 ASSIGN_RETURN (retval);
2836 VUNSETATTR (entry, att_invisible);
2838 ASSIGN_RETURN (entry ? ((readonly_p (entry) == 0) && noassign_p (entry) == 0) : 0);
2842 /* Perform the assignment statement in STRING, and expand the
2843 right side by doing tilde, command and parameter expansion. */
2845 do_assignment (string)
2850 td.flags = W_ASSIGNMENT;
2853 return do_assignment_internal (&td, 1);
2857 do_word_assignment (word, flags)
2861 return do_assignment_internal (word, 1);
2864 /* Given STRING, an assignment string, get the value of the right side
2865 of the `=', and bind it to the left side. Do not perform any word
2866 expansions on the right hand side. */
2868 do_assignment_no_expand (string)
2873 td.flags = W_ASSIGNMENT;
2876 return (do_assignment_internal (&td, 0));
2879 /***************************************************
2881 * Functions to manage the positional parameters *
2883 ***************************************************/
2885 /* Return the word list that corresponds to `$*'. */
2887 list_rest_of_args ()
2889 register WORD_LIST *list, *args;
2892 /* Break out of the loop as soon as one of the dollar variables is null. */
2893 for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++)
2894 list = make_word_list (make_bare_word (dollar_vars[i]), list);
2896 for (args = rest_of_args; args; args = args->next)
2897 list = make_word_list (make_bare_word (args->word->word), list);
2899 return (REVERSE_LIST (list, WORD_LIST *));
2905 register WORD_LIST *list;
2908 for (n = 0; n < 9 && dollar_vars[n+1]; n++)
2910 for (list = rest_of_args; list; list = list->next)
2915 /* Return the value of a positional parameter. This handles values > 10. */
2917 get_dollar_var_value (ind)
2924 temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL;
2925 else /* We want something like ${11} */
2928 for (p = rest_of_args; p && ind--; p = p->next)
2930 temp = p ? savestring (p->word->word) : (char *)NULL;
2935 /* Make a single large string out of the dollar digit variables,
2936 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
2937 case of "$*" with respect to IFS. */
2939 string_rest_of_args (dollar_star)
2942 register WORD_LIST *list;
2945 list = list_rest_of_args ();
2946 string = dollar_star ? string_list_dollar_star (list) : string_list (list);
2947 dispose_words (list);
2951 /* Return a string containing the positional parameters from START to
2952 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
2953 which only makes a difference if QUOTED is non-zero. If QUOTED includes
2954 Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
2955 no quoting chars are added. */
2957 pos_params (string, start, end, quoted)
2959 int start, end, quoted;
2961 WORD_LIST *save, *params, *h, *t;
2965 /* see if we can short-circuit. if start == end, we want 0 parameters. */
2967 return ((char *)NULL);
2969 save = params = list_rest_of_args ();
2971 return ((char *)NULL);
2973 if (start == 0) /* handle ${@:0[:x]} specially */
2975 t = make_word_list (make_word (dollar_vars[0]), params);
2979 for (i = start ? 1 : 0; params && i < start; i++)
2980 params = params->next;
2982 return ((char *)NULL);
2983 for (h = t = params; params && i < end; i++)
2986 params = params->next;
2989 t->next = (WORD_LIST *)NULL;
2991 ret = string_list_pos_params (string[0], h, quoted);
2996 dispose_words (save);
3000 /******************************************************************/
3002 /* Functions to expand strings to strings or WORD_LISTs */
3004 /******************************************************************/
3006 #if defined (PROCESS_SUBSTITUTION)
3007 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
3009 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
3012 /* If there are any characters in STRING that require full expansion,
3013 then call FUNC to expand STRING; otherwise just perform quote
3014 removal if necessary. This returns a new string. */
3016 expand_string_if_necessary (string, quoted, func)
3027 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3028 slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
3032 if (EXP_CHAR (string[i]))
3034 else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
3036 ADVANCE_CHAR (string, slen, i);
3041 list = (*func) (string, quoted);
3044 ret = string_list (list);
3045 dispose_words (list);
3050 else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
3051 ret = string_quote_removal (string, quoted);
3053 ret = savestring (string);
3058 static inline char *
3059 expand_string_to_string_internal (string, quoted, func)
3067 if (string == 0 || *string == '\0')
3068 return ((char *)NULL);
3070 list = (*func) (string, quoted);
3073 ret = string_list (list);
3074 dispose_words (list);
3083 expand_string_to_string (string, quoted)
3087 return (expand_string_to_string_internal (string, quoted, expand_string));
3091 expand_string_unsplit_to_string (string, quoted)
3095 return (expand_string_to_string_internal (string, quoted, expand_string_unsplit));
3099 expand_assignment_string_to_string (string, quoted)
3103 return (expand_string_to_string_internal (string, quoted, expand_string_assignment));
3107 expand_arith_string (string, quoted)
3111 return (expand_string_if_necessary (string, quoted, expand_string));
3114 #if defined (COND_COMMAND)
3115 /* Just remove backslashes in STRING. Returns a new string. */
3117 remove_backslashes (string)
3122 r = ret = (char *)xmalloc (strlen (string) + 1);
3123 for (s = string; s && *s; )
3135 /* This needs better error handling. */
3136 /* Expand W for use as an argument to a unary or binary operator in a
3137 [[...]] expression. If SPECIAL is 1, this is the rhs argument
3138 to the != or == operator, and should be treated as a pattern. In
3139 this case, we quote the string specially for the globbing code. If
3140 SPECIAL is 2, this is an rhs argument for the =~ operator, and should
3141 be quoted appropriately for regcomp/regexec. The caller is responsible
3142 for removing the backslashes if the unquoted word is needed later. */
3144 cond_expand_word (w, special)
3152 if (w->word == 0 || w->word[0] == '\0')
3153 return ((char *)NULL);
3155 w->flags |= W_NOSPLIT2;
3156 l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
3162 r = string_list (l);
3166 qflags = QGLOB_CVTNULL;
3168 qflags |= QGLOB_REGEXP;
3169 p = string_list (l);
3170 r = quote_string_for_globbing (p, qflags);
3182 /* Call expand_word_internal to expand W and handle error returns.
3183 A convenience function for functions that don't want to handle
3184 any errors or free any memory before aborting. */
3186 call_expand_word_internal (w, q, i, c, e)
3192 result = expand_word_internal (w, q, i, c, e);
3193 if (result == &expand_word_error || result == &expand_word_fatal)
3195 /* By convention, each time this error is returned, w->word has
3196 already been freed (it sometimes may not be in the fatal case,
3197 but that doesn't result in a memory leak because we're going
3198 to exit in most cases). */
3199 w->word = (char *)NULL;
3200 last_command_exit_value = EXECUTION_FAILURE;
3201 exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
3208 /* Perform parameter expansion, command substitution, and arithmetic
3209 expansion on STRING, as if it were a word. Leave the result quoted. */
3211 expand_string_internal (string, quoted)
3218 if (string == 0 || *string == 0)
3219 return ((WORD_LIST *)NULL);
3222 td.word = savestring (string);
3224 tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3230 /* Expand STRING by performing parameter expansion, command substitution,
3231 and arithmetic expansion. Dequote the resulting WORD_LIST before
3232 returning it, but do not perform word splitting. The call to
3233 remove_quoted_nulls () is in here because word splitting normally
3234 takes care of quote removal. */
3236 expand_string_unsplit (string, quoted)
3242 if (string == 0 || *string == '\0')
3243 return ((WORD_LIST *)NULL);
3245 expand_no_split_dollar_star = 1;
3246 value = expand_string_internal (string, quoted);
3247 expand_no_split_dollar_star = 0;
3253 remove_quoted_nulls (value->word->word);
3254 value->word->flags &= ~W_HASQUOTEDNULL;
3256 dequote_list (value);
3261 /* Expand the rhs of an assignment statement */
3263 expand_string_assignment (string, quoted)
3270 if (string == 0 || *string == '\0')
3271 return ((WORD_LIST *)NULL);
3273 expand_no_split_dollar_star = 1;
3275 td.flags = W_ASSIGNRHS;
3276 td.word = savestring (string);
3277 value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3280 expand_no_split_dollar_star = 0;
3286 remove_quoted_nulls (value->word->word);
3287 value->word->flags &= ~W_HASQUOTEDNULL;
3289 dequote_list (value);
3295 /* Expand one of the PS? prompt strings. This is a sort of combination of
3296 expand_string_unsplit and expand_string_internal, but returns the
3297 passed string when an error occurs. Might want to trap other calls
3298 to jump_to_top_level here so we don't endlessly loop. */
3300 expand_prompt_string (string, quoted, wflags)
3308 if (string == 0 || *string == 0)
3309 return ((WORD_LIST *)NULL);
3312 td.word = savestring (string);
3314 no_longjmp_on_fatal_error = 1;
3315 value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3316 no_longjmp_on_fatal_error = 0;
3318 if (value == &expand_word_error || value == &expand_word_fatal)
3320 value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
3328 remove_quoted_nulls (value->word->word);
3329 value->word->flags &= ~W_HASQUOTEDNULL;
3331 dequote_list (value);
3336 /* Expand STRING just as if you were expanding a word, but do not dequote
3337 the resultant WORD_LIST. This is called only from within this file,
3338 and is used to correctly preserve quoted characters when expanding
3339 things like ${1+"$@"}. This does parameter expansion, command
3340 substitution, arithmetic expansion, and word splitting. */
3342 expand_string_leave_quoted (string, quoted)
3349 if (string == 0 || *string == '\0')
3350 return ((WORD_LIST *)NULL);
3352 tlist = expand_string_internal (string, quoted);
3356 tresult = word_list_split (tlist);
3357 dispose_words (tlist);
3360 return ((WORD_LIST *)NULL);
3363 /* This does not perform word splitting or dequote the WORD_LIST
3366 expand_string_for_rhs (string, quoted, dollar_at_p, has_dollar_at)
3368 int quoted, *dollar_at_p, *has_dollar_at;
3373 if (string == 0 || *string == '\0')
3374 return (WORD_LIST *)NULL;
3376 td.flags = W_NOSPLIT2; /* no splitting, remove "" and '' */
3378 tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, has_dollar_at);
3382 /* Expand STRING just as if you were expanding a word. This also returns
3383 a list of words. Note that filename globbing is *NOT* done for word
3384 or string expansion, just when the shell is expanding a command. This
3385 does parameter expansion, command substitution, arithmetic expansion,
3386 and word splitting. Dequote the resultant WORD_LIST before returning. */
3388 expand_string (string, quoted)
3394 if (string == 0 || *string == '\0')
3395 return ((WORD_LIST *)NULL);
3397 result = expand_string_leave_quoted (string, quoted);
3398 return (result ? dequote_list (result) : result);
3401 /***************************************************
3403 * Functions to handle quoting chars *
3405 ***************************************************/
3409 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
3410 The parser passes CTLNUL as CTLESC CTLNUL. */
3412 /* Quote escape characters in string s, but no other characters. This is
3413 used to protect CTLESC and CTLNUL in variable values from the rest of
3414 the word expansion process after the variable is expanded (word splitting
3415 and filename generation). If IFS is null, we quote spaces as well, just
3416 in case we split on spaces later (in the case of unquoted $@, we will
3417 eventually attempt to split the entire word on spaces). Corresponding
3418 code exists in dequote_escapes. Even if we don't end up splitting on
3419 spaces, quoting spaces is not a problem. This should never be called on
3420 a string that is quoted with single or double quotes or part of a here
3421 document (effectively double-quoted). */
3423 quote_escapes (string)
3426 register char *s, *t;
3428 char *result, *send;
3429 int quote_spaces, skip_ctlesc, skip_ctlnul;
3432 slen = strlen (string);
3433 send = string + slen;
3435 quote_spaces = (ifs_value && *ifs_value == 0);
3437 for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
3438 skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
3440 t = result = (char *)xmalloc ((slen * 2) + 1);
3445 if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' '))
3447 COPY_CHAR_P (t, s, send);
3454 list_quote_escapes (list)
3457 register WORD_LIST *w;
3460 for (w = list; w; w = w->next)
3463 w->word->word = quote_escapes (t);
3469 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
3471 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
3472 This is necessary to make unquoted CTLESC and CTLNUL characters in the
3473 data stream pass through properly.
3475 We need to remove doubled CTLESC characters inside quoted strings before
3476 quoting the entire string, so we do not double the number of CTLESC
3479 Also used by parts of the pattern substitution code. */
3481 dequote_escapes (string)
3484 register char *s, *t, *s1;
3486 char *result, *send;
3493 slen = strlen (string);
3494 send = string + slen;
3496 t = result = (char *)xmalloc (slen + 1);
3498 if (strchr (string, CTLESC) == 0)
3499 return (strcpy (result, string));
3501 quote_spaces = (ifs_value && *ifs_value == 0);
3506 if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' ')))
3512 COPY_CHAR_P (t, s, send);
3518 /* Return a new string with the quoted representation of character C.
3519 This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
3520 set in any resultant WORD_DESC where this value is the word. */
3522 make_quoted_char (c)
3527 temp = (char *)xmalloc (3);
3542 /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
3543 the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
3544 this value is the word. */
3546 quote_string (string)
3551 char *result, *send;
3555 result = (char *)xmalloc (2);
3563 slen = strlen (string);
3564 send = string + slen;
3566 result = (char *)xmalloc ((slen * 2) + 1);
3568 for (t = result; string < send; )
3571 COPY_CHAR_P (t, string, send);
3578 /* De-quote quoted characters in STRING. */
3580 dequote_string (string)
3583 register char *s, *t;
3585 char *result, *send;
3588 slen = strlen (string);
3590 t = result = (char *)xmalloc (slen + 1);
3592 if (QUOTED_NULL (string))
3598 /* If no character in the string can be quoted, don't bother examining
3599 each character. Just return a copy of the string passed to us. */
3600 if (strchr (string, CTLESC) == NULL)
3601 return (strcpy (result, string));
3603 send = string + slen;
3613 COPY_CHAR_P (t, s, send);
3620 /* Quote the entire WORD_LIST list. */
3625 register WORD_LIST *w;
3628 for (w = list; w; w = w->next)
3631 w->word->word = quote_string (t);
3633 w->word->flags |= W_HASQUOTEDNULL; /* XXX - turn on W_HASQUOTEDNULL here? */
3634 w->word->flags |= W_QUOTED;
3640 /* De-quote quoted characters in each word in LIST. */
3646 register WORD_LIST *tlist;
3648 for (tlist = list; tlist; tlist = tlist->next)
3650 s = dequote_string (tlist->word->word);
3651 if (QUOTED_NULL (tlist->word->word))
3652 tlist->word->flags &= ~W_HASQUOTEDNULL;
3653 free (tlist->word->word);
3654 tlist->word->word = s;
3659 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
3662 remove_quoted_escapes (string)
3669 t = dequote_escapes (string);
3677 /* Perform quoted null character removal on STRING. We don't allow any
3678 quoted null characters in the middle or at the ends of strings because
3679 of how expand_word_internal works. remove_quoted_nulls () turns
3680 STRING into an empty string iff it only consists of a quoted null,
3681 and removes all unquoted CTLNUL characters. */
3683 remove_quoted_nulls (string)
3686 register size_t slen;
3687 register int i, j, prev_i;
3690 if (strchr (string, CTLNUL) == 0) /* XXX */
3691 return string; /* XXX */
3693 slen = strlen (string);
3698 if (string[i] == CTLESC)
3700 /* Old code had j++, but we cannot assume that i == j at this
3701 point -- what if a CTLNUL has already been removed from the
3702 string? We don't want to drop the CTLESC or recopy characters
3703 that we've already copied down. */
3704 i++; string[j++] = CTLESC;
3708 else if (string[i] == CTLNUL)
3712 ADVANCE_CHAR (string, slen, i);
3715 do string[j++] = string[prev_i++]; while (prev_i < i);
3725 /* Perform quoted null character removal on each element of LIST.
3726 This modifies LIST. */
3728 word_list_remove_quoted_nulls (list)
3731 register WORD_LIST *t;
3733 for (t = list; t; t = t->next)
3735 remove_quoted_nulls (t->word->word);
3736 t->word->flags &= ~W_HASQUOTEDNULL;
3740 /* **************************************************************** */
3742 /* Functions for Matching and Removing Patterns */
3744 /* **************************************************************** */
3746 #if defined (HANDLE_MULTIBYTE)
3747 #if 0 /* Currently unused */
3748 static unsigned char *
3749 mb_getcharlens (string, len)
3753 int i, offset, last;
3760 ret = (unsigned char *)xmalloc (len);
3761 memset (ret, 0, len);
3762 while (string[last])
3764 ADVANCE_CHAR (string, len, offset);
3765 ret[last] = offset - last;
3773 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
3774 can have one of 4 values:
3775 RP_LONG_LEFT remove longest matching portion at start of PARAM
3776 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
3777 RP_LONG_RIGHT remove longest matching portion at end of PARAM
3778 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
3781 #define RP_LONG_LEFT 1
3782 #define RP_SHORT_LEFT 2
3783 #define RP_LONG_RIGHT 3
3784 #define RP_SHORT_RIGHT 4
3786 /* Returns its first argument if nothing matched; new memory otherwise */
3788 remove_upattern (param, pattern, op)
3789 char *param, *pattern;
3794 register char *p, *ret, c;
3796 len = STRLEN (param);
3801 case RP_LONG_LEFT: /* remove longest match at start */
3802 for (p = end; p >= param; p--)
3805 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3808 return (savestring (p));
3815 case RP_SHORT_LEFT: /* remove shortest match at start */
3816 for (p = param; p <= end; p++)
3819 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3822 return (savestring (p));
3828 case RP_LONG_RIGHT: /* remove longest match at end */
3829 for (p = param; p <= end; p++)
3831 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3834 ret = savestring (param);
3841 case RP_SHORT_RIGHT: /* remove shortest match at end */
3842 for (p = end; p >= param; p--)
3844 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3847 ret = savestring (param);
3855 return (param); /* no match, return original string */
3858 #if defined (HANDLE_MULTIBYTE)
3859 /* Returns its first argument if nothing matched; new memory otherwise */
3861 remove_wpattern (wparam, wstrlen, wpattern, op)
3872 case RP_LONG_LEFT: /* remove longest match at start */
3873 for (n = wstrlen; n >= 0; n--)
3875 wc = wparam[n]; wparam[n] = L'\0';
3876 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3879 return (wcsdup (wparam + n));
3885 case RP_SHORT_LEFT: /* remove shortest match at start */
3886 for (n = 0; n <= wstrlen; n++)
3888 wc = wparam[n]; wparam[n] = L'\0';
3889 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3892 return (wcsdup (wparam + n));
3898 case RP_LONG_RIGHT: /* remove longest match at end */
3899 for (n = 0; n <= wstrlen; n++)
3901 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3903 wc = wparam[n]; wparam[n] = L'\0';
3904 ret = wcsdup (wparam);
3911 case RP_SHORT_RIGHT: /* remove shortest match at end */
3912 for (n = wstrlen; n >= 0; n--)
3914 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3916 wc = wparam[n]; wparam[n] = L'\0';
3917 ret = wcsdup (wparam);
3925 return (wparam); /* no match, return original string */
3927 #endif /* HANDLE_MULTIBYTE */
3930 remove_pattern (param, pattern, op)
3931 char *param, *pattern;
3938 if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */
3939 return (savestring (param));
3941 #if defined (HANDLE_MULTIBYTE)
3944 wchar_t *ret, *oret;
3946 wchar_t *wparam, *wpattern;
3949 n = xdupmbstowcs (&wpattern, NULL, pattern);
3950 if (n == (size_t)-1)
3952 xret = remove_upattern (param, pattern, op);
3953 return ((xret == param) ? savestring (param) : xret);
3955 n = xdupmbstowcs (&wparam, NULL, param);
3956 if (n == (size_t)-1)
3959 xret = remove_upattern (param, pattern, op);
3960 return ((xret == param) ? savestring (param) : xret);
3962 oret = ret = remove_wpattern (wparam, n, wpattern, op);
3963 /* Don't bother to convert wparam back to multibyte string if nothing
3964 matched; just return copy of original string */
3969 return (savestring (param));
3976 xret = (char *)xmalloc (n + 1);
3977 memset (&ps, '\0', sizeof (mbstate_t));
3978 n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps);
3979 xret[n] = '\0'; /* just to make sure */
3986 xret = remove_upattern (param, pattern, op);
3987 return ((xret == param) ? savestring (param) : xret);
3991 /* Match PAT anywhere in STRING and return the match boundaries.
3992 This returns 1 in case of a successful match, 0 otherwise. SP
3993 and EP are pointers into the string where the match begins and
3994 ends, respectively. MTYPE controls what kind of match is attempted.
3995 MATCH_BEG and MATCH_END anchor the match at the beginning and end
3996 of the string, respectively. The longest match is returned. */
3998 match_upattern (string, pat, mtype, sp, ep)
4004 register char *p, *p1, *npat;
4008 /* If the pattern doesn't match anywhere in the string, go ahead and
4009 short-circuit right away. A minor optimization, saves a bunch of
4010 unnecessary calls to strmatch (up to N calls for a string of N
4011 characters) if the match is unsuccessful. To preserve the semantics
4012 of the substring matches below, we make sure that the pattern has
4013 `*' as first and last character, making a new pattern if necessary. */
4014 /* XXX - check this later if I ever implement `**' with special meaning,
4015 since this will potentially result in `**' at the beginning or end */
4017 if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*')
4019 p = npat = (char *)xmalloc (len + 3);
4021 if (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob))
4025 if (p1[-1] != '*' || p[-2] == '\\')
4031 c = strmatch (npat, string, FNMATCH_EXTFLAG);
4034 if (c == FNM_NOMATCH)
4037 len = STRLEN (string);
4040 mlen = umatchlen (pat, len);
4045 for (p = string; p <= end; p++)
4047 if (match_pattern_char (pat, p))
4050 for (p1 = end; p1 >= p; p1--)
4052 p1 = (mlen == -1) ? end : p + mlen;
4053 /* p1 - p = length of portion of string to be considered
4054 p = current position in string
4055 mlen = number of characters consumed by match (-1 for entire string)
4057 we want to break immediately if the potential match len
4058 is greater than the number of characters remaining in the
4063 for ( ; p1 >= p; p1--)
4066 c = *p1; *p1 = '\0';
4067 if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
4076 /* If MLEN != -1, we have a fixed length pattern. */
4087 if (match_pattern_char (pat, string) == 0)
4091 for (p = end; p >= string; p--)
4093 for (p = (mlen == -1) ? end : string + mlen; p >= string; p--)
4097 if (strmatch (pat, string, FNMATCH_EXTFLAG) == 0)
4106 /* If MLEN != -1, we have a fixed length pattern. */
4116 for (p = string; p <= end; p++)
4118 for (p = end - ((mlen == -1) ? len : mlen); p <= end; p++)
4121 if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
4128 /* If MLEN != -1, we have a fixed length pattern. */
4140 #if defined (HANDLE_MULTIBYTE)
4141 /* Match WPAT anywhere in WSTRING and return the match boundaries.
4142 This returns 1 in case of a successful match, 0 otherwise. Wide
4143 character version. */
4145 match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
4153 wchar_t wc, *wp, *nwpat, *wp1;
4156 int n, n1, n2, simple;
4158 simple = (wpat[0] != L'\\' && wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'[');
4159 #if defined (EXTENDED_GLOB)
4161 simple |= (wpat[1] != L'(' || (wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'+' && wpat[0] != L'!' && wpat[0] != L'@')); /*)*/
4164 /* If the pattern doesn't match anywhere in the string, go ahead and
4165 short-circuit right away. A minor optimization, saves a bunch of
4166 unnecessary calls to strmatch (up to N calls for a string of N
4167 characters) if the match is unsuccessful. To preserve the semantics
4168 of the substring matches below, we make sure that the pattern has
4169 `*' as first and last character, making a new pattern if necessary. */
4170 len = wcslen (wpat);
4171 if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*')
4173 wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t));
4175 if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob))
4177 while (*wp1 != L'\0')
4179 if (wp1[-1] != L'*' || wp1[-2] == L'\\')
4185 len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG);
4188 if (len == FNM_NOMATCH)
4191 mlen = wmatchlen (wpat, wstrlen);
4193 /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */
4197 for (n = 0; n <= wstrlen; n++)
4200 n2 = simple ? (*wpat == wstring[n]) : match_pattern_wchar (wpat, wstring + n);
4202 n2 = match_pattern_wchar (wpat, wstring + n);
4207 for (n1 = wstrlen; n1 >= n; n1--)
4209 n1 = (mlen == -1) ? wstrlen : n + mlen;
4213 for ( ; n1 >= n; n1--)
4216 wc = wstring[n1]; wstring[n1] = L'\0';
4217 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
4226 /* If MLEN != -1, we have a fixed length pattern. */
4237 if (match_pattern_wchar (wpat, wstring) == 0)
4241 for (n = wstrlen; n >= 0; n--)
4243 for (n = (mlen == -1) ? wstrlen : mlen; n >= 0; n--)
4246 wc = wstring[n]; wstring[n] = L'\0';
4247 if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG) == 0)
4256 /* If MLEN != -1, we have a fixed length pattern. */
4266 for (n = 0; n <= wstrlen; n++)
4268 for (n = wstrlen - ((mlen == -1) ? wstrlen : mlen); n <= wstrlen; n++)
4271 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
4274 *ep = indices[wstrlen];
4278 /* If MLEN != -1, we have a fixed length pattern. */
4289 #endif /* HANDLE_MULTIBYTE */
4292 match_pattern (string, pat, mtype, sp, ep)
4297 #if defined (HANDLE_MULTIBYTE)
4300 wchar_t *wstring, *wpat;
4302 size_t slen, plen, mslen, mplen;
4305 if (string == 0 || *string == 0 || pat == 0 || *pat == 0)
4308 #if defined (HANDLE_MULTIBYTE)
4312 slen = STRLEN (string);
4313 mslen = MBSLEN (string);
4314 plen = STRLEN (pat);
4315 mplen = MBSLEN (pat);
4316 if (slen == mslen && plen == mplen)
4318 if (mbsmbchar (string) == 0 && mbsmbchar (pat) == 0)
4320 return (match_upattern (string, pat, mtype, sp, ep));
4322 n = xdupmbstowcs (&wpat, NULL, pat);
4323 if (n == (size_t)-1)
4324 return (match_upattern (string, pat, mtype, sp, ep));
4325 n = xdupmbstowcs (&wstring, &indices, string);
4326 if (n == (size_t)-1)
4329 return (match_upattern (string, pat, mtype, sp, ep));
4331 ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep);
4341 return (match_upattern (string, pat, mtype, sp, ep));
4345 getpatspec (c, value)
4350 return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
4352 return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
4355 /* Posix.2 says that the WORD should be run through tilde expansion,
4356 parameter expansion, command substitution and arithmetic expansion.
4357 This leaves the result quoted, so quote_string_for_globbing () has
4358 to be called to fix it up for strmatch (). If QUOTED is non-zero,
4359 it means that the entire expression was enclosed in double quotes.
4360 This means that quoting characters in the pattern do not make any
4361 special pattern characters quoted. For example, the `*' in the
4362 following retains its special meaning: "${foo#'*'}". */
4364 getpattern (value, quoted, expandpat)
4366 int quoted, expandpat;
4373 /* There is a problem here: how to handle single or double quotes in the
4374 pattern string when the whole expression is between double quotes?
4375 POSIX.2 says that enclosing double quotes do not cause the pattern to
4376 be quoted, but does that leave us a problem with @ and array[@] and their
4377 expansions inside a pattern? */
4379 if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
4382 pat = string_extract_double_quoted (tword, &i, 1);
4388 /* expand_string_for_rhs () leaves WORD quoted and does not perform
4390 l = *value ? expand_string_for_rhs (value,
4391 (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
4392 (int *)NULL, (int *)NULL)
4394 pat = string_list (l);
4398 tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
4406 /* Handle removing a pattern from a string as a result of ${name%[%]value}
4407 or ${name#[#]value}. */
4409 variable_remove_pattern (value, pattern, patspec, quoted)
4410 char *value, *pattern;
4411 int patspec, quoted;
4415 tword = remove_pattern (value, pattern, patspec);
4422 list_remove_pattern (list, pattern, patspec, itype, quoted)
4425 int patspec, itype, quoted;
4431 for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
4433 tword = remove_pattern (l->word->word, pattern, patspec);
4434 w = alloc_word_desc ();
4435 w->word = tword ? tword : savestring ("");
4436 new = make_word_list (w, new);
4439 l = REVERSE_LIST (new, WORD_LIST *);
4440 tword = string_list_pos_params (itype, l, quoted);
4447 parameter_list_remove_pattern (itype, pattern, patspec, quoted)
4450 int patspec, quoted;
4455 list = list_rest_of_args ();
4457 return ((char *)NULL);
4458 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
4459 dispose_words (list);
4463 #if defined (ARRAY_VARS)
4465 array_remove_pattern (var, pattern, patspec, varname, quoted)
4469 char *varname; /* so we can figure out how it's indexed */
4479 /* compute itype from varname here */
4480 v = array_variable_part (varname, &ret, 0);
4483 a = (v && array_p (v)) ? array_cell (v) : 0;
4484 h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
4486 list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
4488 return ((char *)NULL);
4489 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
4490 dispose_words (list);
4494 #endif /* ARRAY_VARS */
4497 parameter_brace_remove_pattern (varname, value, ind, patstr, rtype, quoted, flags)
4498 char *varname, *value;
4501 int rtype, quoted, flags;
4503 int vtype, patspec, starsub;
4504 char *temp1, *val, *pattern;
4508 return ((char *)NULL);
4510 this_command_name = varname;
4512 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
4514 return ((char *)NULL);
4516 starsub = vtype & VT_STARSUB;
4517 vtype &= ~VT_STARSUB;
4519 patspec = getpatspec (rtype, patstr);
4520 if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
4523 /* Need to pass getpattern newly-allocated memory in case of expansion --
4524 the expansion code will free the passed string on an error. */
4525 temp1 = savestring (patstr);
4526 pattern = getpattern (temp1, quoted, 1);
4529 temp1 = (char *)NULL; /* shut up gcc */
4533 case VT_ARRAYMEMBER:
4534 temp1 = remove_pattern (val, pattern, patspec);
4535 if (vtype == VT_VARIABLE)
4539 val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
4540 ? quote_string (temp1)
4541 : quote_escapes (temp1);
4546 #if defined (ARRAY_VARS)
4548 temp1 = array_remove_pattern (v, pattern, patspec, varname, quoted);
4549 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
4551 val = quote_escapes (temp1);
4558 temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
4559 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
4561 val = quote_escapes (temp1);
4572 /*******************************************
4574 * Functions to expand WORD_DESCs *
4576 *******************************************/
4578 /* Expand WORD, performing word splitting on the result. This does
4579 parameter expansion, command substitution, arithmetic expansion,
4580 word splitting, and quote removal. */
4583 expand_word (word, quoted)
4587 WORD_LIST *result, *tresult;
4589 tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
4590 result = word_list_split (tresult);
4591 dispose_words (tresult);
4592 return (result ? dequote_list (result) : result);
4595 /* Expand WORD, but do not perform word splitting on the result. This
4596 does parameter expansion, command substitution, arithmetic expansion,
4597 and quote removal. */
4599 expand_word_unsplit (word, quoted)
4605 expand_no_split_dollar_star = 1;
4606 #if defined (HANDLE_MULTIBYTE)
4607 if (ifs_firstc[0] == 0)
4609 if (ifs_firstc == 0)
4611 word->flags |= W_NOSPLIT;
4612 word->flags |= W_NOSPLIT2;
4613 result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
4614 expand_no_split_dollar_star = 0;
4616 return (result ? dequote_list (result) : result);
4619 /* Perform shell expansions on WORD, but do not perform word splitting or
4620 quote removal on the result. Virtually identical to expand_word_unsplit;
4621 could be combined if implementations don't diverge. */
4623 expand_word_leave_quoted (word, quoted)
4629 expand_no_split_dollar_star = 1;
4630 #if defined (HANDLE_MULTIBYTE)
4631 if (ifs_firstc[0] == 0)
4633 if (ifs_firstc == 0)
4635 word->flags |= W_NOSPLIT;
4636 word->flags |= W_NOSPLIT2;
4637 result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
4638 expand_no_split_dollar_star = 0;
4643 #if defined (PROCESS_SUBSTITUTION)
4645 /*****************************************************************/
4647 /* Hacking Process Substitution */
4649 /*****************************************************************/
4651 #if !defined (HAVE_DEV_FD)
4652 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
4653 of FIFOs the shell has open. unlink_fifo_list will walk the list and
4654 unlink all of them. add_fifo_list adds the name of an open FIFO to the
4655 list. NFIFO is a count of the number of FIFOs in the list. */
4656 #define FIFO_INCR 20
4663 static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
4665 static int fifo_list_size;
4668 copy_fifo_list (sizep)
4673 return (char *)NULL;
4677 add_fifo_list (pathname)
4680 if (nfifo >= fifo_list_size - 1)
4682 fifo_list_size += FIFO_INCR;
4683 fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
4684 fifo_list_size * sizeof (struct temp_fifo));
4687 fifo_list[nfifo].file = savestring (pathname);
4695 if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
4697 unlink (fifo_list[i].file);
4698 free (fifo_list[i].file);
4699 fifo_list[i].file = (char *)NULL;
4700 fifo_list[i].proc = -1;
4712 for (i = saved = 0; i < nfifo; i++)
4714 if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
4716 unlink (fifo_list[i].file);
4717 free (fifo_list[i].file);
4718 fifo_list[i].file = (char *)NULL;
4719 fifo_list[i].proc = -1;
4725 /* If we didn't remove some of the FIFOs, compact the list. */
4728 for (i = j = 0; i < nfifo; i++)
4729 if (fifo_list[i].file)
4731 fifo_list[j].file = fifo_list[i].file;
4732 fifo_list[j].proc = fifo_list[i].proc;
4741 /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list
4742 from some point in the past, and close all open FIFOs in fifo_list
4743 that are not marked as active in LIST. If LIST is NULL, close
4744 everything in fifo_list. LSIZE is the number of elements in LIST, in
4745 case it's larger than fifo_list_size (size of fifo_list). */
4747 close_new_fifos (list, lsize)
4755 unlink_fifo_list ();
4759 for (i = 0; i < lsize; i++)
4760 if (list[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1)
4763 for (i = lsize; i < fifo_list_size; i++)
4784 tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR);
4785 if (mkfifo (tname, 0600) < 0)
4788 return ((char *)NULL);
4791 add_fifo_list (tname);
4795 #else /* HAVE_DEV_FD */
4797 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
4798 has open to children. NFDS is a count of the number of bits currently
4799 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
4801 static char *dev_fd_list = (char *)NULL;
4803 static int totfds; /* The highest possible number of open files. */
4806 copy_fifo_list (sizep)
4811 if (nfds == 0 || totfds == 0)
4815 return (char *)NULL;
4820 ret = (char *)xmalloc (totfds);
4821 return (memcpy (ret, dev_fd_list, totfds));
4828 if (dev_fd_list == 0 || fd >= totfds)
4833 totfds = getdtablesize ();
4834 if (totfds < 0 || totfds > 256)
4839 dev_fd_list = (char *)xrealloc (dev_fd_list, totfds);
4840 memset (dev_fd_list + ofds, '\0', totfds - ofds);
4843 dev_fd_list[fd] = 1;
4850 return 0; /* used for cleanup; not needed with /dev/fd */
4863 if (dev_fd_list[fd])
4866 dev_fd_list[fd] = 0;
4879 for (i = 0; nfds && i < totfds; i++)
4885 /* Take LIST, which is a snapshot copy of dev_fd_list from some point in
4886 the past, and close all open fds in dev_fd_list that are not marked
4887 as open in LIST. If LIST is NULL, close everything in dev_fd_list.
4888 LSIZE is the number of elements in LIST, in case it's larger than
4889 totfds (size of dev_fd_list). */
4891 close_new_fifos (list, lsize)
4899 unlink_fifo_list ();
4903 for (i = 0; i < lsize; i++)
4904 if (list[i] == 0 && i < totfds && dev_fd_list[i])
4907 for (i = lsize; i < totfds; i++)
4911 #if defined (NOTDEF)
4912 print_dev_fd_list ()
4916 fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
4919 for (i = 0; i < totfds; i++)
4922 fprintf (stderr, " %d", i);
4924 fprintf (stderr, "\n");
4929 make_dev_fd_filename (fd)
4932 char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
4934 ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8);
4936 strcpy (ret, DEV_FD_PREFIX);
4937 p = inttostr (fd, intbuf, sizeof (intbuf));
4938 strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
4944 #endif /* HAVE_DEV_FD */
4946 /* Return a filename that will open a connection to the process defined by
4947 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
4948 a filename in /dev/fd corresponding to a descriptor that is one of the
4949 ends of the pipe. If not defined, we use named pipes on systems that have
4950 them. Systems without /dev/fd and named pipes are out of luck.
4952 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
4953 use the read end of the pipe and dup that file descriptor to fd 0 in
4954 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
4955 writing or use the write end of the pipe in the child, and dup that
4956 file descriptor to fd 1 in the child. The parent does the opposite. */
4959 process_substitute (string, open_for_read_in_child)
4961 int open_for_read_in_child;
4966 #if defined (HAVE_DEV_FD)
4967 int parent_pipe_fd, child_pipe_fd;
4969 #endif /* HAVE_DEV_FD */
4970 #if defined (JOB_CONTROL)
4971 pid_t old_pipeline_pgrp;
4974 if (!string || !*string || wordexp_only)
4975 return ((char *)NULL);
4977 #if !defined (HAVE_DEV_FD)
4978 pathname = make_named_pipe ();
4979 #else /* HAVE_DEV_FD */
4980 if (pipe (fildes) < 0)
4982 sys_error (_("cannot make pipe for process substitution"));
4983 return ((char *)NULL);
4985 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
4986 the pipe in the parent, otherwise the read end. */
4987 parent_pipe_fd = fildes[open_for_read_in_child];
4988 child_pipe_fd = fildes[1 - open_for_read_in_child];
4989 /* Move the parent end of the pipe to some high file descriptor, to
4990 avoid clashes with FDs used by the script. */
4991 parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
4993 pathname = make_dev_fd_filename (parent_pipe_fd);
4994 #endif /* HAVE_DEV_FD */
4998 sys_error (_("cannot make pipe for process substitution"));
4999 return ((char *)NULL);
5002 old_pid = last_made_pid;
5004 #if defined (JOB_CONTROL)
5005 old_pipeline_pgrp = pipeline_pgrp;
5006 pipeline_pgrp = shell_pgrp;
5008 #endif /* JOB_CONTROL */
5010 pid = make_child ((char *)NULL, 1);
5013 reset_terminating_signals (); /* XXX */
5014 free_pushed_string_input ();
5015 /* Cancel traps, in trap.c. */
5016 restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */
5017 setup_async_signals ();
5018 subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB;
5021 #if defined (JOB_CONTROL)
5022 set_sigchld_handler ();
5023 stop_making_children ();
5024 /* XXX - should we only do this in the parent? (as in command subst) */
5025 pipeline_pgrp = old_pipeline_pgrp;
5026 #endif /* JOB_CONTROL */
5030 sys_error (_("cannot make child for process substitution"));
5032 #if defined (HAVE_DEV_FD)
5033 close (parent_pipe_fd);
5034 close (child_pipe_fd);
5035 #endif /* HAVE_DEV_FD */
5036 return ((char *)NULL);
5041 #if defined (JOB_CONTROL)
5042 restore_pipeline (1);
5045 #if !defined (HAVE_DEV_FD)
5046 fifo_list[nfifo-1].proc = pid;
5049 last_made_pid = old_pid;
5051 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
5053 #endif /* JOB_CONTROL && PGRP_PIPE */
5055 #if defined (HAVE_DEV_FD)
5056 close (child_pipe_fd);
5057 #endif /* HAVE_DEV_FD */
5062 set_sigint_handler ();
5064 #if defined (JOB_CONTROL)
5065 set_job_control (0);
5066 #endif /* JOB_CONTROL */
5068 #if !defined (HAVE_DEV_FD)
5069 /* Open the named pipe in the child. */
5070 fd = open (pathname, open_for_read_in_child ? O_RDONLY|O_NONBLOCK : O_WRONLY);
5073 /* Two separate strings for ease of translation. */
5074 if (open_for_read_in_child)
5075 sys_error (_("cannot open named pipe %s for reading"), pathname);
5077 sys_error (_("cannot open named pipe %s for writing"), pathname);
5081 if (open_for_read_in_child)
5083 if (sh_unset_nodelay_mode (fd) < 0)
5085 sys_error (_("cannot reset nodelay mode for fd %d"), fd);
5089 #else /* HAVE_DEV_FD */
5091 #endif /* HAVE_DEV_FD */
5093 if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
5095 sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname,
5096 open_for_read_in_child ? 0 : 1);
5100 if (fd != (open_for_read_in_child ? 0 : 1))
5103 /* Need to close any files that this process has open to pipes inherited
5105 if (current_fds_to_close)
5107 close_fd_bitmap (current_fds_to_close);
5108 current_fds_to_close = (struct fd_bitmap *)NULL;
5111 #if defined (HAVE_DEV_FD)
5112 /* Make sure we close the parent's end of the pipe and clear the slot
5113 in the fd list so it is not closed later, if reallocated by, for
5114 instance, pipe(2). */
5115 close (parent_pipe_fd);
5116 dev_fd_list[parent_pipe_fd] = 0;
5117 #endif /* HAVE_DEV_FD */
5119 result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
5121 #if !defined (HAVE_DEV_FD)
5122 /* Make sure we close the named pipe in the child before we exit. */
5123 close (open_for_read_in_child ? 0 : 1);
5124 #endif /* !HAVE_DEV_FD */
5129 #endif /* PROCESS_SUBSTITUTION */
5131 /***********************************/
5133 /* Command Substitution */
5135 /***********************************/
5138 read_comsub (fd, quoted, rflag)
5142 char *istring, buf[128], *bufp, *s;
5143 int istring_index, istring_size, c, tflag, skip_ctlesc, skip_ctlnul;
5146 istring = (char *)NULL;
5147 istring_index = istring_size = bufn = tflag = 0;
5149 for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
5150 skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
5152 /* Read the output of the command through the pipe. This may need to be
5153 changed to understand multibyte characters in the future. */
5160 bufn = zread (fd, buf, sizeof (buf));
5170 internal_warning ("read_comsub: ignored null byte in input");
5175 /* Add the character to ISTRING, possibly after resizing it. */
5176 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
5178 /* This is essentially quote_string inline */
5179 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */)
5180 istring[istring_index++] = CTLESC;
5181 /* Escape CTLESC and CTLNUL in the output to protect those characters
5182 from the rest of the word expansions (word splitting and globbing.)
5183 This is essentially quote_escapes inline. */
5184 else if (skip_ctlesc == 0 && c == CTLESC)
5186 tflag |= W_HASCTLESC;
5187 istring[istring_index++] = CTLESC;
5189 else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0)))
5190 istring[istring_index++] = CTLESC;
5192 istring[istring_index++] = c;
5195 #if defined (__CYGWIN__)
5196 if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r')
5199 istring[istring_index - 1] = '\n';
5206 istring[istring_index] = '\0';
5208 /* If we read no output, just return now and save ourselves some
5210 if (istring_index == 0)
5215 return (char *)NULL;
5218 /* Strip trailing newlines from the output of the command. */
5219 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
5221 while (istring_index > 0)
5223 if (istring[istring_index - 1] == '\n')
5227 /* If the newline was quoted, remove the quoting char. */
5228 if (istring[istring_index - 1] == CTLESC)
5234 istring[istring_index] = '\0';
5237 strip_trailing (istring, istring_index - 1, 1);
5244 /* Perform command substitution on STRING. This returns a WORD_DESC * with the
5245 contained string possibly quoted. */
5247 command_substitute (string, quoted)
5251 pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
5253 int result, fildes[2], function_value, pflags, rc, tflag;
5256 istring = (char *)NULL;
5258 /* Don't fork () if there is no need to. In the case of no command to
5259 run, just return NULL. */
5260 if (!string || !*string || (string[0] == '\n' && !string[1]))
5261 return ((WORD_DESC *)NULL);
5263 if (wordexp_only && read_but_dont_execute)
5265 last_command_exit_value = EX_WEXPCOMSUB;
5266 jump_to_top_level (EXITPROG);
5269 /* We're making the assumption here that the command substitution will
5270 eventually run a command from the file system. Since we'll run
5271 maybe_make_export_env in this subshell before executing that command,
5272 the parent shell and any other shells it starts will have to remake
5273 the environment. If we make it before we fork, other shells won't
5274 have to. Don't bother if we have any temporary variable assignments,
5275 though, because the export environment will be remade after this
5276 command completes anyway, but do it if all the words to be expanded
5277 are variable assignments. */
5278 if (subst_assign_varlist == 0 || garglist == 0)
5279 maybe_make_export_env (); /* XXX */
5281 /* Flags to pass to parse_and_execute() */
5282 pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0;
5284 /* Pipe the output of executing STRING into the current shell. */
5285 if (pipe (fildes) < 0)
5287 sys_error (_("cannot make pipe for command substitution"));
5291 old_pid = last_made_pid;
5292 #if defined (JOB_CONTROL)
5293 old_pipeline_pgrp = pipeline_pgrp;
5294 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
5295 if ((subshell_environment & SUBSHELL_PIPE) == 0)
5296 pipeline_pgrp = shell_pgrp;
5297 cleanup_the_pipeline ();
5298 #endif /* JOB_CONTROL */
5300 old_async_pid = last_asynchronous_pid;
5301 pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
5302 last_asynchronous_pid = old_async_pid;
5306 /* Reset the signal handlers in the child, but don't free the
5307 trap strings. Set a flag noting that we have to free the
5308 trap strings if we run trap to change a signal disposition. */
5309 reset_signal_handlers ();
5310 subshell_environment |= SUBSHELL_RESETTRAP;
5313 #if defined (JOB_CONTROL)
5314 /* XXX DO THIS ONLY IN PARENT ? XXX */
5315 set_sigchld_handler ();
5316 stop_making_children ();
5318 pipeline_pgrp = old_pipeline_pgrp;
5320 stop_making_children ();
5321 #endif /* JOB_CONTROL */
5325 sys_error (_("cannot make child for command substitution"));
5331 return ((WORD_DESC *)NULL);
5336 set_sigint_handler (); /* XXX */
5338 free_pushed_string_input ();
5340 if (dup2 (fildes[1], 1) < 0)
5342 sys_error (_("command_substitute: cannot duplicate pipe as fd 1"));
5343 exit (EXECUTION_FAILURE);
5346 /* If standard output is closed in the parent shell
5347 (such as after `exec >&-'), file descriptor 1 will be
5348 the lowest available file descriptor, and end up in
5349 fildes[0]. This can happen for stdin and stderr as well,
5350 but stdout is more important -- it will cause no output
5351 to be generated from this command. */
5352 if ((fildes[1] != fileno (stdin)) &&
5353 (fildes[1] != fileno (stdout)) &&
5354 (fildes[1] != fileno (stderr)))
5357 if ((fildes[0] != fileno (stdin)) &&
5358 (fildes[0] != fileno (stdout)) &&
5359 (fildes[0] != fileno (stderr)))
5363 /* Let stdio know the fd may have changed from text to binary mode, and
5364 make sure to preserve stdout line buffering. */
5365 freopen (NULL, "w", stdout);
5366 sh_setlinebuf (stdout);
5367 #endif /* __CYGWIN__ */
5369 /* The currently executing shell is not interactive. */
5372 /* This is a subshell environment. */
5373 subshell_environment |= SUBSHELL_COMSUB;
5375 /* When not in POSIX mode, command substitution does not inherit
5377 if (posixly_correct == 0)
5378 exit_immediately_on_error = 0;
5380 remove_quoted_escapes (string);
5382 startup_state = 2; /* see if we can avoid a fork */
5383 /* Give command substitution a place to jump back to on failure,
5384 so we don't go back up to main (). */
5385 result = setjmp (top_level);
5387 /* If we're running a command substitution inside a shell function,
5388 trap `return' so we don't return from the function in the subshell
5389 and go off to never-never land. */
5390 if (result == 0 && return_catch_flag)
5391 function_value = setjmp (return_catch);
5395 if (result == ERREXIT)
5396 rc = last_command_exit_value;
5397 else if (result == EXITPROG)
5398 rc = last_command_exit_value;
5400 rc = EXECUTION_FAILURE;
5401 else if (function_value)
5402 rc = return_catch_value;
5406 rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST);
5410 last_command_exit_value = rc;
5411 rc = run_exit_trap ();
5412 #if defined (PROCESS_SUBSTITUTION)
5413 unlink_fifo_list ();
5419 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
5421 #endif /* JOB_CONTROL && PGRP_PIPE */
5426 istring = read_comsub (fildes[0], quoted, &tflag);
5430 current_command_subst_pid = pid;
5431 last_command_exit_value = wait_for (pid);
5432 last_command_subst_pid = pid;
5433 last_made_pid = old_pid;
5435 #if defined (JOB_CONTROL)
5436 /* If last_command_exit_value > 128, then the substituted command
5437 was terminated by a signal. If that signal was SIGINT, then send
5438 SIGINT to ourselves. This will break out of loops, for instance. */
5439 if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT)
5440 kill (getpid (), SIGINT);
5442 /* wait_for gives the terminal back to shell_pgrp. If some other
5443 process group should have it, give it away to that group here.
5444 pipeline_pgrp is non-zero only while we are constructing a
5445 pipline, so what we are concerned about is whether or not that
5446 pipeline was started in the background. A pipeline started in
5447 the background should never get the tty back here. */
5448 if (interactive && pipeline_pgrp != (pid_t)0 && (subshell_environment & SUBSHELL_ASYNC) == 0)
5449 give_terminal_to (pipeline_pgrp, 0);
5450 #endif /* JOB_CONTROL */
5452 ret = alloc_word_desc ();
5453 ret->word = istring;
5460 /********************************************************
5462 * Utility functions for parameter expansion *
5464 ********************************************************/
5466 #if defined (ARRAY_VARS)
5469 array_length_reference (s)
5480 var = array_variable_part (s, &t, &len);
5482 /* If unbound variables should generate an error, report one and return
5484 if ((var == 0 || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error)
5488 last_command_exit_value = EXECUTION_FAILURE;
5496 /* We support a couple of expansions for variables that are not arrays.
5497 We'll return the length of the value for v[0], and 1 for v[@] or
5498 v[*]. Return 0 for everything else. */
5500 array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
5501 h = assoc_p (var) ? assoc_cell (var) : (HASH_TABLE *)NULL;
5503 if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
5506 return (h ? assoc_num_elements (h) : 0);
5507 else if (array_p (var))
5508 return (array ? array_num_elements (array) : 0);
5510 return (var_isset (var) ? 1 : 0);
5516 akey = expand_assignment_string_to_string (t, 0); /* [ */
5518 if (akey == 0 || *akey == 0)
5520 err_badarraysub (t);
5523 t = assoc_reference (assoc_cell (var), akey);
5527 ind = array_expand_index (t, len);
5530 err_badarraysub (t);
5534 t = array_reference (array, ind);
5536 t = (ind == 0) ? value_cell (var) : (char *)NULL;
5539 len = MB_STRLEN (t);
5542 #endif /* ARRAY_VARS */
5545 valid_brace_expansion_word (name, var_is_special)
5549 if (DIGIT (*name) && all_digits (name))
5551 else if (var_is_special)
5553 #if defined (ARRAY_VARS)
5554 else if (valid_array_reference (name))
5556 #endif /* ARRAY_VARS */
5557 else if (legal_identifier (name))
5564 chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at)
5567 int *quoted_dollar_atp, *contains_dollar_at;
5573 if (quoted_dollar_atp)
5574 *quoted_dollar_atp = 0;
5575 if (contains_dollar_at)
5576 *contains_dollar_at = 0;
5580 /* check for $@ and $* */
5581 if (name[0] == '@' && name[1] == 0)
5583 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
5584 *quoted_dollar_atp = 1;
5585 if (contains_dollar_at)
5586 *contains_dollar_at = 1;
5589 else if (name[0] == '*' && name[1] == '\0' && quoted == 0)
5591 if (contains_dollar_at)
5592 *contains_dollar_at = 1;
5596 /* Now check for ${array[@]} and ${array[*]} */
5597 #if defined (ARRAY_VARS)
5598 else if (valid_array_reference (name))
5600 temp1 = mbschr (name, '[');
5601 if (temp1 && temp1[1] == '@' && temp1[2] == ']')
5603 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
5604 *quoted_dollar_atp = 1;
5605 if (contains_dollar_at)
5606 *contains_dollar_at = 1;
5609 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
5610 which should result in separate words even when IFS is unset. */
5611 if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0)
5613 if (contains_dollar_at)
5614 *contains_dollar_at = 1;
5622 /* Parameter expand NAME, and return a new string which is the expansion,
5623 or NULL if there was no expansion.
5624 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
5625 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
5626 NAME was found inside of a double-quoted expression. */
5628 parameter_brace_expand_word (name, var_is_special, quoted, pflags, indp)
5630 int var_is_special, quoted, pflags;
5647 /* Handle multiple digit arguments, as in ${11}. */
5648 if (legal_number (name, &arg_index))
5650 tt = get_dollar_var_value (arg_index);
5652 temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
5654 : quote_escapes (tt);
5656 temp = (char *)NULL;
5659 else if (var_is_special) /* ${@} */
5662 tt = (char *)xmalloc (2 + strlen (name));
5663 tt[sindex = 0] = '$';
5664 strcpy (tt + 1, name);
5666 ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
5667 (int *)NULL, (int *)NULL, pflags);
5670 #if defined (ARRAY_VARS)
5671 else if (valid_array_reference (name))
5673 temp = array_value (name, quoted, 0, &atype, &ind);
5674 if (atype == 0 && temp)
5676 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
5677 ? quote_string (temp)
5678 : quote_escapes (temp);
5679 rflags |= W_ARRAYIND;
5683 else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
5684 rflags |= W_HASQUOTEDNULL;
5687 else if (var = find_variable (name))
5689 if (var_isset (var) && invisible_p (var) == 0)
5691 #if defined (ARRAY_VARS)
5693 temp = assoc_reference (assoc_cell (var), "0");
5694 else if (array_p (var))
5695 temp = array_reference (array_cell (var), 0);
5697 temp = value_cell (var);
5699 temp = value_cell (var);
5703 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
5704 ? quote_string (temp)
5705 : quote_escapes (temp);
5708 temp = (char *)NULL;
5711 temp = (char *)NULL;
5715 ret = alloc_word_desc ();
5717 ret->flags |= rflags;
5722 /* Expand an indirect reference to a variable: ${!NAME} expands to the
5723 value of the variable whose name is the value of NAME. */
5725 parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at)
5727 int var_is_special, quoted;
5728 int *quoted_dollar_atp, *contains_dollar_at;
5733 w = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND, 0);
5735 /* Have to dequote here if necessary */
5738 temp = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
5739 ? dequote_string (t)
5740 : dequote_escapes (t);
5744 dispose_word_desc (w);
5746 chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at);
5748 return (WORD_DESC *)NULL;
5750 w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, 0, 0);
5756 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
5757 depending on the value of C, the separating character. C can be one of
5758 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
5759 between double quotes. */
5761 parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat)
5763 int c, quoted, *qdollaratp, *hasdollarat;
5767 char *t, *t1, *temp;
5770 /* If the entire expression is between double quotes, we want to treat
5771 the value as a double-quoted string, with the exception that we strip
5772 embedded unescaped double quotes (for sh backwards compatibility). */
5773 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
5776 temp = string_extract_double_quoted (value, &hasdol, 1);
5781 w = alloc_word_desc ();
5783 /* XXX was 0 not quoted */
5784 l = *temp ? expand_string_for_rhs (temp, quoted, &hasdol, (int *)NULL)
5787 *hasdollarat = hasdol || (l && l->next);
5792 /* The expansion of TEMP returned something. We need to treat things
5793 slightly differently if HASDOL is non-zero. If we have "$@", the
5794 individual words have already been quoted. We need to turn them
5795 into a string with the words separated by the first character of
5796 $IFS without any additional quoting, so string_list_dollar_at won't
5797 do the right thing. We use string_list_dollar_star instead. */
5798 temp = (hasdol || l->next) ? string_list_dollar_star (l) : string_list (l);
5800 /* If l->next is not null, we know that TEMP contained "$@", since that
5801 is the only expansion that creates more than one word. */
5802 if (qdollaratp && ((hasdol && quoted) || l->next))
5806 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && hasdol)
5808 /* The brace expansion occurred between double quotes and there was
5809 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
5810 it does not expand to anything. In this case, we want to return
5811 a quoted empty string. */
5812 temp = make_quoted_char ('\0');
5813 w->flags |= W_HASQUOTEDNULL;
5816 temp = (char *)NULL;
5818 if (c == '-' || c == '+')
5825 t = temp ? savestring (temp) : savestring ("");
5826 t1 = dequote_string (t);
5828 #if defined (ARRAY_VARS)
5829 if (valid_array_reference (name))
5830 assign_array_element (name, t1, 0);
5832 #endif /* ARRAY_VARS */
5833 bind_variable (name, t1, 0);
5835 /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */
5842 /* Deal with the right hand side of a ${name:?value} expansion in the case
5843 that NAME is null or not set. If VALUE is non-null it is expanded and
5844 used as the error message to print, otherwise a standard message is
5847 parameter_brace_expand_error (name, value)
5853 if (value && *value)
5855 l = expand_string (value, 0);
5856 temp = string_list (l);
5857 report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */
5862 report_error (_("%s: parameter null or not set"), name);
5864 /* Free the data we have allocated during this expansion, since we
5865 are about to longjmp out. */
5870 /* Return 1 if NAME is something for which parameter_brace_expand_length is
5873 valid_length_expression (name)
5876 return (name[1] == '\0' || /* ${#} */
5877 ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */
5878 (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */
5879 #if defined (ARRAY_VARS)
5880 valid_array_reference (name + 1) || /* ${#a[7]} */
5882 legal_identifier (name + 1)); /* ${#PS1} */
5885 /* Handle the parameter brace expansion that requires us to return the
5886 length of a parameter. */
5888 parameter_brace_expand_length (name)
5892 intmax_t number, arg_index;
5894 #if defined (ARRAY_VARS)
5898 if (name[1] == '\0') /* ${#} */
5899 number = number_of_args ();
5900 else if ((name[1] == '@' || name[1] == '*') && name[2] == '\0') /* ${#@}, ${#*} */
5901 number = number_of_args ();
5902 else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
5904 /* Take the lengths of some of the shell's special parameters. */
5908 t = which_set_flags ();
5911 t = itos (last_command_exit_value);
5914 t = itos (dollar_dollar_pid);
5917 if (last_asynchronous_pid == NO_PID)
5918 t = (char *)NULL; /* XXX - error if set -u set? */
5920 t = itos (last_asynchronous_pid);
5923 t = itos (number_of_args ());
5926 number = STRLEN (t);
5929 #if defined (ARRAY_VARS)
5930 else if (valid_array_reference (name + 1))
5931 number = array_length_reference (name + 1);
5932 #endif /* ARRAY_VARS */
5937 if (legal_number (name + 1, &arg_index)) /* ${#1} */
5939 t = get_dollar_var_value (arg_index);
5940 if (t == 0 && unbound_vars_is_error)
5942 number = MB_STRLEN (t);
5945 #if defined (ARRAY_VARS)
5946 else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var)))
5949 t = assoc_reference (assoc_cell (var), "0");
5951 t = array_reference (array_cell (var), 0);
5952 if (t == 0 && unbound_vars_is_error)
5954 number = MB_STRLEN (t);
5959 newname = savestring (name);
5961 list = expand_string (newname, Q_DOUBLE_QUOTES);
5962 t = list ? string_list (list) : (char *)NULL;
5965 dispose_words (list);
5967 number = t ? MB_STRLEN (t) : 0;
5975 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
5976 so we do some ad-hoc parsing of an arithmetic expression to find
5977 the first DELIM, instead of using strchr(3). Two rules:
5978 1. If the substring contains a `(', read until closing `)'.
5979 2. If the substring contains a `?', read past one `:' for each `?'.
5983 skiparith (substr, delim)
5988 int skipcol, pcount, i;
5991 sublen = strlen (substr);
5992 i = skipcol = pcount = 0;
5995 /* Balance parens */
5996 if (substr[i] == LPAREN)
6002 if (substr[i] == RPAREN && pcount)
6010 ADVANCE_CHAR (substr, sublen, i);
6014 /* Skip one `:' for each `?' */
6015 if (substr[i] == ':' && skipcol)
6021 if (substr[i] == delim)
6023 if (substr[i] == '?')
6029 ADVANCE_CHAR (substr, sublen, i);
6032 return (substr + i);
6035 /* Verify and limit the start and end of the desired substring. If
6036 VTYPE == 0, a regular shell variable is being used; if it is 1,
6037 then the positional parameters are being used; if it is 2, then
6038 VALUE is really a pointer to an array variable that should be used.
6039 Return value is 1 if both values were OK, 0 if there was a problem
6040 with an invalid expression, or -1 if the values were out of range. */
6042 verify_substring_values (v, value, substr, vtype, e1p, e2p)
6044 char *value, *substr;
6046 intmax_t *e1p, *e2p;
6048 char *t, *temp1, *temp2;
6051 #if defined (ARRAY_VARS)
6056 /* duplicate behavior of strchr(3) */
6057 t = skiparith (substr, ':');
6058 if (*t && *t == ':')
6063 temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES);
6064 *e1p = evalexp (temp1, &expok);
6069 len = -1; /* paranoia */
6073 case VT_ARRAYMEMBER:
6074 len = MB_STRLEN (value);
6077 len = number_of_args () + 1;
6079 len++; /* add one arg if counting from $0 */
6081 #if defined (ARRAY_VARS)
6083 /* For arrays, the first value deals with array indices. Negative
6084 offsets count from one past the array's maximum index. Associative
6085 arrays treat the number of elements as the maximum index. */
6089 len = assoc_num_elements (h) + (*e1p < 0);
6094 len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */
6100 if (len == -1) /* paranoia */
6103 if (*e1p < 0) /* negative offsets count from end */
6106 if (*e1p > len || *e1p < 0)
6109 #if defined (ARRAY_VARS)
6110 /* For arrays, the second offset deals with the number of elements. */
6111 if (vtype == VT_ARRAYVAR)
6112 len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a);
6118 temp2 = savestring (t);
6119 temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
6122 *e2p = evalexp (temp1, &expok);
6126 if ((vtype == VT_ARRAYVAR || vtype == VT_POSPARMS) && *e2p < 0)
6128 internal_error (_("%s: substring expression < 0"), t);
6131 #if defined (ARRAY_VARS)
6132 /* In order to deal with sparse arrays, push the intelligence about how
6133 to deal with the number of elements desired down to the array-
6134 specific functions. */
6135 if (vtype != VT_ARRAYVAR)
6141 if (*e2p < 0 || *e2p < *e1p)
6143 internal_error (_("%s: substring expression < 0"), t);
6148 *e2p += *e1p; /* want E2 chars starting at E1 */
6159 /* Return the type of variable specified by VARNAME (simple variable,
6160 positional param, or array variable). Also return the value specified
6161 by VARNAME (value of a variable or a reference to an array element).
6162 QUOTED is the standard description of quoting state, using Q_* defines.
6163 FLAGS is currently a set of flags to pass to array_value. If IND is
6164 non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is
6165 passed to array_value so the array index is not computed again.
6166 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
6167 characters in the value are quoted with CTLESC and takes appropriate
6168 steps. For convenience, *VALP is set to the dequoted VALUE. */
6170 get_var_and_type (varname, value, ind, quoted, flags, varp, valp)
6171 char *varname, *value;
6179 #if defined (ARRAY_VARS)
6184 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
6185 vtype = (varname[0] == '@' || varname[0] == '*') && varname[1] == '\0';
6186 if (vtype == VT_POSPARMS && varname[0] == '*')
6187 vtype |= VT_STARSUB;
6188 *varp = (SHELL_VAR *)NULL;
6190 #if defined (ARRAY_VARS)
6191 if (valid_array_reference (varname))
6193 v = array_variable_part (varname, &temp, (int *)0);
6194 /* If we want to signal array_value to use an already-computed index,
6195 set LIND to that index */
6196 lind = (ind != INTMAX_MIN && (flags & AV_USEIND)) ? ind : 0;
6197 if (v && (array_p (v) || assoc_p (v)))
6199 if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')
6201 /* Callers have to differentiate betwen indexed and associative */
6202 vtype = VT_ARRAYVAR;
6204 vtype |= VT_STARSUB;
6205 *valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v);
6209 vtype = VT_ARRAYMEMBER;
6210 *valp = array_value (varname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
6214 else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']'))
6216 vtype = VT_VARIABLE;
6218 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
6219 *valp = dequote_string (value);
6221 *valp = dequote_escapes (value);
6225 vtype = VT_ARRAYMEMBER;
6227 *valp = array_value (varname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
6230 else if ((v = find_variable (varname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v)))
6232 vtype = VT_ARRAYMEMBER;
6234 *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0);
6239 if (value && vtype == VT_VARIABLE)
6241 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
6242 *valp = dequote_string (value);
6244 *valp = dequote_escapes (value);
6253 /******************************************************/
6255 /* Functions to extract substrings of variable values */
6257 /******************************************************/
6259 #if defined (HANDLE_MULTIBYTE)
6260 /* Character-oriented rather than strictly byte-oriented substrings. S and
6261 E, rather being strict indices into STRING, indicate character (possibly
6262 multibyte character) positions that require calculation.
6263 Used by the ${param:offset[:length]} expansion. */
6265 mb_substring (string, s, e)
6270 int start, stop, i, slen;
6274 /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
6275 slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0;
6278 while (string[start] && i--)
6279 ADVANCE_CHAR (string, slen, start);
6282 while (string[stop] && i--)
6283 ADVANCE_CHAR (string, slen, stop);
6284 tt = substring (string, start, stop);
6289 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
6290 is `@', use the positional parameters; otherwise, use the value of
6291 VARNAME. If VARNAME is an array variable, use the array elements. */
6294 parameter_brace_substring (varname, value, ind, substr, quoted, flags)
6295 char *varname, *value;
6301 int vtype, r, starsub;
6302 char *temp, *val, *tt, *oname;
6306 return ((char *)NULL);
6308 oname = this_command_name;
6309 this_command_name = varname;
6311 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
6314 this_command_name = oname;
6315 return ((char *)NULL);
6318 starsub = vtype & VT_STARSUB;
6319 vtype &= ~VT_STARSUB;
6321 r = verify_substring_values (v, val, substr, vtype, &e1, &e2);
6322 this_command_name = oname;
6324 return ((r == 0) ? &expand_param_error : (char *)NULL);
6329 case VT_ARRAYMEMBER:
6330 #if defined (HANDLE_MULTIBYTE)
6332 tt = mb_substring (val, e1, e2);
6335 tt = substring (val, e1, e2);
6337 if (vtype == VT_VARIABLE)
6339 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
6340 temp = quote_string (tt);
6342 temp = tt ? quote_escapes (tt) : (char *)NULL;
6346 tt = pos_params (varname, e1, e2, quoted);
6347 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
6349 temp = tt ? quote_escapes (tt) : (char *)NULL;
6355 #if defined (ARRAY_VARS)
6358 /* we convert to list and take first e2 elements starting at e1th
6359 element -- officially undefined for now */
6360 temp = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted);
6362 /* We want E2 to be the number of elements desired (arrays can be sparse,
6363 so verify_substring_values just returns the numbers specified and we
6364 rely on array_subrange to understand how to deal with them). */
6365 temp = array_subrange (array_cell (v), e1, e2, starsub, quoted);
6366 /* array_subrange now calls array_quote_escapes as appropriate, so the
6367 caller no longer needs to. */
6371 temp = (char *)NULL;
6377 /****************************************************************/
6379 /* Functions to perform pattern substitution on variable values */
6381 /****************************************************************/
6384 shouldexp_replacement (s)
6389 for (p = s; p && *p; p++)
6400 pat_subst (string, pat, rep, mflags)
6401 char *string, *pat, *rep;
6404 char *ret, *s, *e, *str, *rstr, *mstr;
6405 int rsize, rptr, l, replen, mtype, rxpand, rslen, mlen;
6408 return (savestring (""));
6410 mtype = mflags & MATCH_TYPEMASK;
6412 #if 0 /* bash-4.2 ? */
6413 rxpand = (rep && *rep) ? shouldexp_replacement (rep) : 0;
6419 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
6420 * with REP and return the result.
6421 * 2. A null pattern with mtype == MATCH_END means to append REP to
6422 * STRING and return the result.
6423 * These don't understand or process `&' in the replacement string.
6425 if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
6427 replen = STRLEN (rep);
6428 l = STRLEN (string);
6429 ret = (char *)xmalloc (replen + l + 2);
6431 strcpy (ret, string);
6432 else if (mtype == MATCH_BEG)
6435 strcpy (ret + replen, string);
6439 strcpy (ret, string);
6440 strcpy (ret + l, rep);
6445 ret = (char *)xmalloc (rsize = 64);
6448 for (replen = STRLEN (rep), rptr = 0, str = string;;)
6450 if (match_pattern (str, pat, mtype, &s, &e) == 0)
6458 mstr = xmalloc (mlen + 1);
6459 for (x = 0; x < mlen; x++)
6462 rstr = strcreplace (rep, '&', mstr, 0);
6463 rslen = strlen (rstr);
6471 RESIZE_MALLOCED_BUFFER (ret, rptr, (l + rslen), rsize, 64);
6473 /* OK, now copy the leading unmatched portion of the string (from
6474 str to s) to ret starting at rptr (the current offset). Then copy
6475 the replacement string at ret + rptr + (s - str). Increment
6476 rptr (if necessary) and str and go on. */
6479 strncpy (ret + rptr, str, l);
6484 strncpy (ret + rptr, rstr, rslen);
6487 str = e; /* e == end of match */
6492 if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
6497 /* On a zero-length match, make sure we copy one character, since
6498 we increment one character to avoid infinite recursion. */
6499 RESIZE_MALLOCED_BUFFER (ret, rptr, 1, rsize, 64);
6500 ret[rptr++] = *str++;
6501 e++; /* avoid infinite recursion on zero-length match */
6505 /* Now copy the unmatched portion of the input string */
6508 RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
6509 strcpy (ret + rptr, str);
6517 /* Do pattern match and replacement on the positional parameters. */
6519 pos_params_pat_subst (string, pat, rep, mflags)
6520 char *string, *pat, *rep;
6523 WORD_LIST *save, *params;
6528 save = params = list_rest_of_args ();
6530 return ((char *)NULL);
6532 for ( ; params; params = params->next)
6534 ret = pat_subst (params->word->word, pat, rep, mflags);
6535 w = alloc_word_desc ();
6536 w->word = ret ? ret : savestring ("");
6537 dispose_word (params->word);
6541 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
6542 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
6545 if ((mflags & (MATCH_QUOTED|MATCH_STARSUB)) == (MATCH_QUOTED|MATCH_STARSUB))
6546 ret = string_list_dollar_star (quote_list (save));
6547 else if ((mflags & MATCH_STARSUB) == MATCH_STARSUB)
6548 ret = string_list_dollar_star (save);
6549 else if ((mflags & MATCH_QUOTED) == MATCH_QUOTED)
6550 ret = string_list_dollar_at (save, qflags);
6552 ret = string_list_dollar_star (save);
6554 ret = string_list_pos_params (pchar, save, qflags);
6557 dispose_words (save);
6562 /* Perform pattern substitution on VALUE, which is the expansion of
6563 VARNAME. PATSUB is an expression supplying the pattern to match
6564 and the string to substitute. QUOTED is a flags word containing
6565 the type of quoting currently in effect. */
6567 parameter_brace_patsub (varname, value, ind, patsub, quoted, flags)
6568 char *varname, *value;
6573 int vtype, mflags, starsub, delim;
6574 char *val, *temp, *pat, *rep, *p, *lpatsub, *tt;
6578 return ((char *)NULL);
6580 this_command_name = varname;
6582 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
6584 return ((char *)NULL);
6586 starsub = vtype & VT_STARSUB;
6587 vtype &= ~VT_STARSUB;
6590 if (patsub && *patsub == '/')
6592 mflags |= MATCH_GLOBREP;
6596 /* Malloc this because expand_string_if_necessary or one of the expansion
6597 functions in its call chain may free it on a substitution error. */
6598 lpatsub = savestring (patsub);
6600 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6601 mflags |= MATCH_QUOTED;
6604 mflags |= MATCH_STARSUB;
6606 /* If the pattern starts with a `/', make sure we skip over it when looking
6607 for the replacement delimiter. */
6609 if (rep = quoted_strchr ((*patsub == '/') ? lpatsub+1 : lpatsub, '/', ST_BACKSL))
6614 delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0);
6615 if (lpatsub[delim] == '/')
6618 rep = lpatsub + delim + 1;
6624 if (rep && *rep == '\0')
6627 /* Perform the same expansions on the pattern as performed by the
6628 pattern removal expansions. */
6629 pat = getpattern (lpatsub, quoted, 1);
6633 if ((mflags & MATCH_QUOTED) == 0)
6634 rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
6636 rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
6639 /* ksh93 doesn't allow the match specifier to be a part of the expanded
6640 pattern. This is an extension. Make sure we don't anchor the pattern
6641 at the beginning or end of the string if we're doing global replacement,
6644 if (mflags & MATCH_GLOBREP)
6645 mflags |= MATCH_ANY;
6646 else if (pat && pat[0] == '#')
6648 mflags |= MATCH_BEG;
6651 else if (pat && pat[0] == '%')
6653 mflags |= MATCH_END;
6657 mflags |= MATCH_ANY;
6659 /* OK, we now want to substitute REP for PAT in VAL. If
6660 flags & MATCH_GLOBREP is non-zero, the substitution is done
6661 everywhere, otherwise only the first occurrence of PAT is
6662 replaced. The pattern matching code doesn't understand
6663 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
6664 values passed in (VT_VARIABLE) so the pattern substitution
6665 code works right. We need to requote special chars after
6666 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
6667 other cases if QUOTED == 0, since the posparams and arrays
6668 indexed by * or @ do special things when QUOTED != 0. */
6673 case VT_ARRAYMEMBER:
6674 temp = pat_subst (val, p, rep, mflags);
6675 if (vtype == VT_VARIABLE)
6679 tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
6685 temp = pos_params_pat_subst (val, p, rep, mflags);
6686 if (temp && (mflags & MATCH_QUOTED) == 0)
6688 tt = quote_escapes (temp);
6693 #if defined (ARRAY_VARS)
6695 temp = assoc_p (v) ? assoc_patsub (assoc_cell (v), p, rep, mflags)
6696 : array_patsub (array_cell (v), p, rep, mflags);
6697 /* Don't call quote_escapes anymore; array_patsub calls
6698 array_quote_escapes as appropriate before adding the
6699 space separators; ditto for assoc_patsub. */
6711 /****************************************************************/
6713 /* Functions to perform case modification on variable values */
6715 /****************************************************************/
6717 /* Do case modification on the positional parameters. */
6720 pos_params_modcase (string, pat, modop, mflags)
6725 WORD_LIST *save, *params;
6730 save = params = list_rest_of_args ();
6732 return ((char *)NULL);
6734 for ( ; params; params = params->next)
6736 ret = sh_modcase (params->word->word, pat, modop);
6737 w = alloc_word_desc ();
6738 w->word = ret ? ret : savestring ("");
6739 dispose_word (params->word);
6743 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
6744 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
6746 ret = string_list_pos_params (pchar, save, qflags);
6747 dispose_words (save);
6752 /* Perform case modification on VALUE, which is the expansion of
6753 VARNAME. MODSPEC is an expression supplying the type of modification
6754 to perform. QUOTED is a flags word containing the type of quoting
6755 currently in effect. */
6757 parameter_brace_casemod (varname, value, ind, modspec, patspec, quoted, flags)
6758 char *varname, *value;
6763 int vtype, starsub, modop, mflags, x;
6764 char *val, *temp, *pat, *p, *lpat, *tt;
6768 return ((char *)NULL);
6770 this_command_name = varname;
6772 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
6774 return ((char *)NULL);
6776 starsub = vtype & VT_STARSUB;
6777 vtype &= ~VT_STARSUB;
6781 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6782 mflags |= MATCH_QUOTED;
6784 mflags |= MATCH_STARSUB;
6789 x = p && p[0] == modspec;
6790 modop = x ? CASE_UPPER : CASE_UPFIRST;
6793 else if (modspec == ',')
6795 x = p && p[0] == modspec;
6796 modop = x ? CASE_LOWER : CASE_LOWFIRST;
6799 else if (modspec == '~')
6801 x = p && p[0] == modspec;
6802 modop = x ? CASE_TOGGLEALL : CASE_TOGGLE;
6806 lpat = p ? savestring (p) : 0;
6807 /* Perform the same expansions on the pattern as performed by the
6808 pattern removal expansions. FOR LATER */
6809 pat = lpat ? getpattern (lpat, quoted, 1) : 0;
6811 /* OK, now we do the case modification. */
6815 case VT_ARRAYMEMBER:
6816 temp = sh_modcase (val, pat, modop);
6817 if (vtype == VT_VARIABLE)
6821 tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
6828 temp = pos_params_modcase (val, pat, modop, mflags);
6829 if (temp && (mflags & MATCH_QUOTED) == 0)
6831 tt = quote_escapes (temp);
6837 #if defined (ARRAY_VARS)
6839 temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags)
6840 : array_modcase (array_cell (v), pat, modop, mflags);
6841 /* Don't call quote_escapes; array_modcase calls array_quote_escapes
6842 as appropriate before adding the space separators; ditto for
6854 /* Check for unbalanced parens in S, which is the contents of $(( ... )). If
6855 any occur, this must be a nested command substitution, so return 0.
6856 Otherwise, return 1. A valid arithmetic expression must always have a
6857 ( before a matching ), so any cases where there are more right parens
6858 means that this must not be an arithmetic expression, though the parser
6859 will not accept it without a balanced total number of parens. */
6861 chk_arithsub (s, len)
6873 else if (s[i] == RPAREN)
6883 ADVANCE_CHAR (s, len, i);
6889 ADVANCE_CHAR (s, len, i);
6893 i = skip_single_quoted (s, len, ++i);
6897 i = skip_double_quoted ((char *)s, len, ++i);
6902 return (count == 0);
6905 /****************************************************************/
6907 /* Functions to perform parameter expansion on a string */
6909 /****************************************************************/
6911 /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
6913 parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at)
6915 int *indexp, quoted, *quoted_dollar_atp, *contains_dollar_at, pflags;
6917 int check_nullness, var_is_set, var_is_null, var_is_special;
6918 int want_substring, want_indir, want_patsub, want_casemod;
6919 char *name, *value, *temp, *temp1;
6920 WORD_DESC *tdesc, *ret;
6921 int t_index, sindex, c, tflag, modspec;
6925 temp = temp1 = value = (char *)NULL;
6926 var_is_set = var_is_null = var_is_special = check_nullness = 0;
6927 want_substring = want_indir = want_patsub = want_casemod = 0;
6931 /* ${#var} doesn't have any of the other parameter expansions on it. */
6932 if (string[t_index] == '#' && legal_variable_starter (string[t_index+1])) /* {{ */
6933 name = string_extract (string, &t_index, "}", SX_VARNAME);
6935 #if defined (CASEMOD_EXPANSIONS)
6936 /* To enable case-toggling expansions using the `~' operator character
6937 change the 1 to 0. */
6938 # if defined (CASEMOD_CAPCASE)
6939 name = string_extract (string, &t_index, "#%^,~:-=?+/}", SX_VARNAME);
6941 name = string_extract (string, &t_index, "#%^,:-=?+/}", SX_VARNAME);
6942 # endif /* CASEMOD_CAPCASE */
6944 name = string_extract (string, &t_index, "#%:-=?+/}", SX_VARNAME);
6945 #endif /* CASEMOD_EXPANSIONS */
6952 /* If the name really consists of a special variable, then make sure
6953 that we have the entire name. We don't allow indirect references
6954 to special variables except `#', `?', `@' and `*'. */
6955 if ((sindex == t_index && VALID_SPECIAL_LENGTH_PARAM (string[t_index])) ||
6956 (sindex == t_index - 1 && string[sindex] == '!' && VALID_INDIR_PARAM (string[t_index])))
6960 temp1 = string_extract (string, &t_index, "#%:-=?+/}", 0);
6961 name = (char *)xmalloc (3 + (strlen (temp1)));
6962 *name = string[sindex];
6963 if (string[sindex] == '!')
6965 /* indirect reference of $#, $?, $@, or $* */
6966 name[1] = string[sindex + 1];
6967 strcpy (name + 2, temp1);
6970 strcpy (name + 1, temp1);
6975 /* Find out what character ended the variable name. Then
6976 do the appropriate thing. */
6977 if (c = string[sindex])
6980 /* If c is followed by one of the valid parameter expansion
6981 characters, move past it as normal. If not, assume that
6982 a substring specification is being given, and do not move
6984 if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
6987 if (c = string[sindex])
6990 else if (c == ':' && string[sindex] != RBRACE)
6992 else if (c == '/' && string[sindex] != RBRACE)
6994 #if defined (CASEMOD_EXPANSIONS)
6995 else if (c == '^' || c == ',' || c == '~')
7002 /* Catch the valid and invalid brace expressions that made it through the
7004 /* ${#-} is a valid expansion and means to take the length of $-.
7005 Similarly for ${#?} and ${##}... */
7006 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
7007 VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
7009 name = (char *)xrealloc (name, 3);
7012 c = string[sindex++];
7015 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
7016 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
7017 member (c, "%:=+/") && string[sindex] == RBRACE)
7019 temp = (char *)NULL;
7020 goto bad_substitution;
7023 /* Indirect expansion begins with a `!'. A valid indirect expansion is
7024 either a variable name, one of the positional parameters or a special
7025 variable that expands to one of the positional parameters. */
7026 want_indir = *name == '!' &&
7027 (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
7028 || VALID_INDIR_PARAM (name[1]));
7030 /* Determine the value of this variable. */
7032 /* Check for special variables, directly referenced. */
7033 if (SPECIAL_VAR (name, want_indir))
7036 /* Check for special expansion things, like the length of a parameter */
7037 if (*name == '#' && name[1])
7039 /* If we are not pointing at the character just after the
7040 closing brace, then we haven't gotten all of the name.
7041 Since it begins with a special character, this is a bad
7042 substitution. Also check NAME for validity before trying
7044 if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
7046 temp = (char *)NULL;
7047 goto bad_substitution;
7050 number = parameter_brace_expand_length (name);
7051 if (number == INTMAX_MIN && unbound_vars_is_error)
7053 last_command_exit_value = EXECUTION_FAILURE;
7054 err_unboundvar (name+1);
7056 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7062 return (&expand_wdesc_error);
7065 ret = alloc_word_desc ();
7066 ret->word = itos (number);
7071 /* ${@} is identical to $@. */
7072 if (name[0] == '@' && name[1] == '\0')
7074 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
7075 *quoted_dollar_atp = 1;
7077 if (contains_dollar_at)
7078 *contains_dollar_at = 1;
7081 /* Process ${!PREFIX*} expansion. */
7082 if (want_indir && string[sindex - 1] == RBRACE &&
7083 (string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
7084 legal_variable_starter ((unsigned char) name[1]))
7089 temp1 = savestring (name + 1);
7090 number = strlen (temp1);
7091 temp1[number - 1] = '\0';
7092 x = all_variables_matching_prefix (temp1);
7093 xlist = strvec_to_word_list (x, 0, 0);
7094 if (string[sindex - 2] == '*')
7095 temp = string_list_dollar_star (xlist);
7098 temp = string_list_dollar_at (xlist, quoted);
7099 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
7100 *quoted_dollar_atp = 1;
7101 if (contains_dollar_at)
7102 *contains_dollar_at = 1;
7105 dispose_words (xlist);
7109 ret = alloc_word_desc ();
7114 #if defined (ARRAY_VARS)
7115 /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */
7116 if (want_indir && string[sindex - 1] == RBRACE &&
7117 string[sindex - 2] == ']' && valid_array_reference (name+1))
7121 temp1 = savestring (name + 1);
7122 x = array_variable_name (temp1, &x1, (int *)0); /* [ */
7124 if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == ']')
7126 temp = array_keys (temp1, quoted); /* handles assoc vars too */
7129 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
7130 *quoted_dollar_atp = 1;
7131 if (contains_dollar_at)
7132 *contains_dollar_at = 1;
7138 ret = alloc_word_desc ();
7145 #endif /* ARRAY_VARS */
7147 /* Make sure that NAME is valid before trying to go on. */
7148 if (valid_brace_expansion_word (want_indir ? name + 1 : name,
7149 var_is_special) == 0)
7151 temp = (char *)NULL;
7152 goto bad_substitution;
7156 tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at);
7158 tdesc = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND|(pflags&PF_NOSPLIT2), &ind);
7163 tflag = tdesc->flags;
7164 dispose_word_desc (tdesc);
7169 #if defined (ARRAY_VARS)
7170 if (valid_array_reference (name))
7171 chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at);
7174 var_is_set = temp != (char *)0;
7175 var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
7177 /* Get the rest of the stuff inside the braces. */
7178 if (c && c != RBRACE)
7180 /* Extract the contents of the ${ ... } expansion
7181 according to the Posix.2 rules. */
7182 value = extract_dollar_brace_string (string, &sindex, quoted, (c == '%' || c == '#' || c =='/' || c == '^' || c == ',' || c ==':') ? SX_POSIXEXP|SX_WORD : SX_WORD);
7183 if (string[sindex] == RBRACE)
7186 goto bad_substitution;
7189 value = (char *)NULL;
7193 /* All the cases where an expansion can possibly generate an unbound
7195 if (want_substring || want_patsub || want_casemod || c == '#' || c == '%' || c == RBRACE)
7197 if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1]))
7199 last_command_exit_value = EXECUTION_FAILURE;
7200 err_unboundvar (name);
7204 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7208 /* If this is a substring spec, process it and add the result. */
7211 temp1 = parameter_brace_substring (name, temp, ind, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
7216 if (temp1 == &expand_param_error)
7217 return (&expand_wdesc_error);
7218 else if (temp1 == &expand_param_fatal)
7219 return (&expand_wdesc_fatal);
7221 ret = alloc_word_desc ();
7223 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7224 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
7227 else if (want_patsub)
7229 temp1 = parameter_brace_patsub (name, temp, ind, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
7234 if (temp1 == &expand_param_error)
7235 return (&expand_wdesc_error);
7236 else if (temp1 == &expand_param_fatal)
7237 return (&expand_wdesc_fatal);
7239 ret = alloc_word_desc ();
7241 ret = alloc_word_desc ();
7243 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7244 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
7247 #if defined (CASEMOD_EXPANSIONS)
7248 else if (want_casemod)
7250 temp1 = parameter_brace_casemod (name, temp, ind, modspec, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
7255 if (temp1 == &expand_param_error)
7256 return (&expand_wdesc_error);
7257 else if (temp1 == &expand_param_fatal)
7258 return (&expand_wdesc_fatal);
7260 ret = alloc_word_desc ();
7262 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7263 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
7268 /* Do the right thing based on which character ended the variable name. */
7274 report_error (_("%s: bad substitution"), string ? string : "??");
7278 return &expand_wdesc_error;
7283 case '#': /* ${param#[#]pattern} */
7284 case '%': /* ${param%[%]pattern} */
7285 if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
7290 temp1 = parameter_brace_remove_pattern (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
7295 ret = alloc_word_desc ();
7297 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7298 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
7305 if (var_is_set && var_is_null == 0)
7307 /* If the operator is `+', we don't want the value of the named
7308 variable for anything, just the value of the right hand side. */
7311 /* XXX -- if we're double-quoted and the named variable is "$@",
7312 we want to turn off any special handling of "$@" --
7313 we're not using it, so whatever is on the rhs applies. */
7314 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
7315 *quoted_dollar_atp = 0;
7316 if (contains_dollar_at)
7317 *contains_dollar_at = 0;
7322 /* From Posix discussion on austin-group list. Issue 221
7323 requires that backslashes escaping `}' inside
7324 double-quoted ${...} be removed. */
7325 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7326 quoted |= Q_DOLBRACE;
7327 ret = parameter_brace_expand_rhs (name, value, c,
7330 contains_dollar_at);
7331 /* XXX - fix up later, esp. noting presence of
7332 W_HASQUOTEDNULL in ret->flags */
7336 temp = (char *)NULL;
7342 /* Otherwise do nothing; just use the value in TEMP. */
7344 else /* VAR not set or VAR is NULL. */
7347 temp = (char *)NULL;
7348 if (c == '=' && var_is_special)
7350 report_error (_("$%s: cannot assign in this way"), name);
7353 return &expand_wdesc_error;
7357 parameter_brace_expand_error (name, value);
7358 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7362 /* XXX -- if we're double-quoted and the named variable is "$@",
7363 we want to turn off any special handling of "$@" --
7364 we're not using it, so whatever is on the rhs applies. */
7365 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
7366 *quoted_dollar_atp = 0;
7367 if (contains_dollar_at)
7368 *contains_dollar_at = 0;
7370 /* From Posix discussion on austin-group list. Issue 221 requires
7371 that backslashes escaping `}' inside double-quoted ${...} be
7373 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7374 quoted |= Q_DOLBRACE;
7375 ret = parameter_brace_expand_rhs (name, value, c, quoted,
7377 contains_dollar_at);
7378 /* XXX - fix up later, esp. noting presence of
7379 W_HASQUOTEDNULL in tdesc->flags */
7390 ret = alloc_word_desc ();
7397 /* Expand a single ${xxx} expansion. The braces are optional. When
7398 the braces are used, parameter_brace_expand() does the work,
7399 possibly calling param_expand recursively. */
7401 param_expand (string, sindex, quoted, expanded_something,
7402 contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
7405 int *sindex, quoted, *expanded_something, *contains_dollar_at;
7406 int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
7408 char *temp, *temp1, uerror[3];
7409 int zindex, t_index, expok;
7414 WORD_DESC *tdesc, *ret;
7418 c = string[++zindex];
7420 temp = (char *)NULL;
7421 ret = tdesc = (WORD_DESC *)NULL;
7424 /* Do simple cases first. Switch on what follows '$'. */
7438 temp1 = dollar_vars[TODIGIT (c)];
7439 if (unbound_vars_is_error && temp1 == (char *)NULL)
7444 last_command_exit_value = EXECUTION_FAILURE;
7445 err_unboundvar (uerror);
7446 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7449 temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7450 ? quote_string (temp1)
7451 : quote_escapes (temp1);
7453 temp = (char *)NULL;
7457 /* $$ -- pid of the invoking shell. */
7459 temp = itos (dollar_dollar_pid);
7462 /* $# -- number of positional parameters. */
7464 temp = itos (number_of_args ());
7467 /* $? -- return value of the last synchronous command. */
7469 temp = itos (last_command_exit_value);
7472 /* $- -- flags supplied to the shell on invocation or by `set'. */
7474 temp = which_set_flags ();
7477 /* $! -- Pid of the last asynchronous command. */
7479 /* If no asynchronous pids have been created, expand to nothing.
7480 If `set -u' has been executed, and no async processes have
7481 been created, this is an expansion error. */
7482 if (last_asynchronous_pid == NO_PID)
7484 if (expanded_something)
7485 *expanded_something = 0;
7486 temp = (char *)NULL;
7487 if (unbound_vars_is_error)
7492 last_command_exit_value = EXECUTION_FAILURE;
7493 err_unboundvar (uerror);
7494 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7498 temp = itos (last_asynchronous_pid);
7501 /* The only difference between this and $@ is when the arg is quoted. */
7502 case '*': /* `$*' */
7503 list = list_rest_of_args ();
7506 /* According to austin-group posix proposal by Geoff Clare in
7507 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
7509 "The shell shall write a message to standard error and
7510 immediately exit when it tries to expand an unset parameter
7511 other than the '@' and '*' special parameters."
7514 if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
7519 last_command_exit_value = EXECUTION_FAILURE;
7520 err_unboundvar (uerror);
7521 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7525 /* If there are no command-line arguments, this should just
7526 disappear if there are other characters in the expansion,
7527 even if it's quoted. */
7528 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
7529 temp = (char *)NULL;
7530 else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
7532 /* If we have "$*" we want to make a string of the positional
7533 parameters, separated by the first character of $IFS, and
7534 quote the whole string, including the separators. If IFS
7535 is unset, the parameters are separated by ' '; if $IFS is
7536 null, the parameters are concatenated. */
7537 temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list) : string_list (list);
7540 temp1 = quote_string (temp);
7542 tflag |= W_HASQUOTEDNULL;
7549 /* We check whether or not we're eventually going to split $* here,
7550 for example when IFS is empty and we are processing the rhs of
7551 an assignment statement. In that case, we don't separate the
7552 arguments at all. Otherwise, if the $* is not quoted it is
7555 # if defined (HANDLE_MULTIBYTE)
7556 if (expand_no_split_dollar_star && ifs_firstc[0] == 0)
7558 if (expand_no_split_dollar_star && ifs_firstc == 0)
7560 temp = string_list_dollar_star (list);
7562 temp = string_list_dollar_at (list, quoted);
7564 temp = string_list_dollar_at (list, quoted);
7566 if (expand_no_split_dollar_star == 0 && contains_dollar_at)
7567 *contains_dollar_at = 1;
7570 dispose_words (list);
7573 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
7574 means that we have to turn quoting off after we split into
7575 the individually quoted arguments so that the final split
7576 on the first character of $IFS is still done. */
7577 case '@': /* `$@' */
7578 list = list_rest_of_args ();
7581 /* According to austin-group posix proposal by Geoff Clare in
7582 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
7584 "The shell shall write a message to standard error and
7585 immediately exit when it tries to expand an unset parameter
7586 other than the '@' and '*' special parameters."
7589 if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
7594 last_command_exit_value = EXECUTION_FAILURE;
7595 err_unboundvar (uerror);
7596 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7600 /* We want to flag the fact that we saw this. We can't turn
7601 off quoting entirely, because other characters in the
7602 string might need it (consider "\"$@\""), but we need some
7603 way to signal that the final split on the first character
7604 of $IFS should be done, even though QUOTED is 1. */
7605 /* XXX - should this test include Q_PATQUOTE? */
7606 if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7607 *quoted_dollar_at_p = 1;
7608 if (contains_dollar_at)
7609 *contains_dollar_at = 1;
7612 if (pflags & PF_NOSPLIT2)
7613 temp = string_list_internal (quoted ? quote_list (list) : list, " ");
7616 /* We want to separate the positional parameters with the first
7617 character of $IFS in case $IFS is something other than a space.
7618 We also want to make sure that splitting is done no matter what --
7619 according to POSIX.2, this expands to a list of the positional
7620 parameters no matter what IFS is set to. */
7621 temp = string_list_dollar_at (list, quoted);
7623 dispose_words (list);
7627 tdesc = parameter_brace_expand (string, &zindex, quoted, pflags,
7629 contains_dollar_at);
7631 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
7633 temp = tdesc ? tdesc->word : (char *)0;
7636 /* Quoted nulls should be removed if there is anything else
7638 /* Note that we saw the quoted null so we can add one back at
7639 the end of this function if there are no other characters
7640 in the string, discard TEMP, and go on. The exception to
7641 this is when we have "${@}" and $1 is '', since $@ needs
7642 special handling. */
7643 if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp))
7645 if (had_quoted_null_p)
7646 *had_quoted_null_p = 1;
7647 if (*quoted_dollar_at_p == 0)
7650 tdesc->word = temp = (char *)NULL;
7658 /* Do command or arithmetic substitution. */
7660 /* We have to extract the contents of this paren substitution. */
7661 t_index = zindex + 1;
7662 temp = extract_command_subst (string, &t_index, 0);
7665 /* For Posix.2-style `$(( ))' arithmetic substitution,
7666 extract the expression and pass it to the evaluator. */
7667 if (temp && *temp == LPAREN)
7671 temp2 = savestring (temp1);
7672 t_index = strlen (temp2) - 1;
7674 if (temp2[t_index] != RPAREN)
7680 /* Cut off ending `)' */
7681 temp2[t_index] = '\0';
7683 if (chk_arithsub (temp2, t_index) == 0)
7687 internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
7692 /* Expand variables found inside the expression. */
7693 temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
7697 /* No error messages. */
7698 this_command_name = (char *)NULL;
7699 number = evalexp (temp1, &expok);
7704 if (interactive_shell == 0 && posixly_correct)
7706 last_command_exit_value = EXECUTION_FAILURE;
7707 return (&expand_wdesc_fatal);
7710 return (&expand_wdesc_error);
7712 temp = itos (number);
7717 if (pflags & PF_NOCOMSUB)
7718 /* we need zindex+1 because string[zindex] == RPAREN */
7719 temp1 = substring (string, *sindex, zindex+1);
7722 tdesc = command_substitute (temp, quoted);
7723 temp1 = tdesc ? tdesc->word : (char *)NULL;
7725 dispose_word_desc (tdesc);
7731 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
7732 away in a future bash release. */
7734 /* Extract the contents of this arithmetic substitution. */
7735 t_index = zindex + 1;
7736 temp = extract_arithmetic_subst (string, &t_index);
7740 temp = savestring (string);
7741 if (expanded_something)
7742 *expanded_something = 0;
7746 /* Do initial variable expansion. */
7747 temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES);
7752 /* Find the variable in VARIABLE_LIST. */
7753 temp = (char *)NULL;
7755 for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
7757 temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
7759 /* If this isn't a variable name, then just output the `$'. */
7760 if (temp1 == 0 || *temp1 == '\0')
7763 temp = (char *)xmalloc (2);
7766 if (expanded_something)
7767 *expanded_something = 0;
7771 /* If the variable exists, return its value cell. */
7772 var = find_variable (temp1);
7774 if (var && invisible_p (var) == 0 && var_isset (var))
7776 #if defined (ARRAY_VARS)
7777 if (assoc_p (var) || array_p (var))
7779 temp = array_p (var) ? array_reference (array_cell (var), 0)
7780 : assoc_reference (assoc_cell (var), "0");
7782 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7783 ? quote_string (temp)
7784 : quote_escapes (temp);
7785 else if (unbound_vars_is_error)
7786 goto unbound_variable;
7791 temp = value_cell (var);
7793 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7794 ? quote_string (temp)
7795 : quote_escapes (temp);
7803 temp = (char *)NULL;
7806 if (unbound_vars_is_error)
7808 last_command_exit_value = EXECUTION_FAILURE;
7809 err_unboundvar (temp1);
7818 last_command_exit_value = EXECUTION_FAILURE;
7819 return ((unbound_vars_is_error && interactive_shell == 0)
7820 ? &expand_wdesc_fatal
7821 : &expand_wdesc_error);
7832 ret = alloc_word_desc ();
7833 ret->flags = tflag; /* XXX */
7839 /* Make a word list which is the result of parameter and variable
7840 expansion, command substitution, arithmetic substitution, and
7841 quote removal of WORD. Return a pointer to a WORD_LIST which is
7842 the result of the expansion. If WORD contains a null word, the
7843 word list returned is also null.
7845 QUOTED contains flag values defined in shell.h.
7847 ISEXP is used to tell expand_word_internal that the word should be
7848 treated as the result of an expansion. This has implications for
7849 how IFS characters in the word are treated.
7851 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
7852 they point to an integer value which receives information about expansion.
7853 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
7854 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
7857 This only does word splitting in the case of $@ expansion. In that
7858 case, we split on ' '. */
7860 /* Values for the local variable quoted_state. */
7862 #define PARTIALLY_QUOTED 1
7863 #define WHOLLY_QUOTED 2
7866 expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
7869 int *contains_dollar_at;
7870 int *expanded_something;
7875 /* The intermediate string that we build while expanding. */
7878 /* The current size of the above object. */
7881 /* Index into ISTRING. */
7884 /* Temporary string storage. */
7887 /* The text of WORD. */
7888 register char *string;
7890 /* The size of STRING. */
7893 /* The index into STRING. */
7896 /* This gets 1 if we see a $@ while quoted. */
7897 int quoted_dollar_at;
7899 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
7900 whether WORD contains no quoting characters, a partially quoted
7901 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
7905 int had_quoted_null;
7908 int pflags; /* flags passed to param_expand */
7910 int assignoff; /* If assignment, offset of `=' */
7912 register unsigned char c; /* Current character. */
7913 int t_index; /* For calls to string_extract_xxx. */
7919 istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
7920 istring[istring_index = 0] = '\0';
7921 quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
7922 quoted_state = UNQUOTED;
7924 string = word->word;
7926 goto finished_with_string;
7927 /* Don't need the string length for the SADD... and COPY_ macros unless
7928 multibyte characters are possible. */
7929 string_size = (MB_CUR_MAX > 1) ? strlen (string) : 1;
7931 if (contains_dollar_at)
7932 *contains_dollar_at = 0;
7936 /* Begin the expansion. */
7942 /* Case on toplevel character. */
7946 goto finished_with_string;
7950 #if HANDLE_MULTIBYTE
7951 if (MB_CUR_MAX > 1 && string[sindex])
7953 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
7958 temp = (char *)xmalloc (3);
7960 temp[1] = c = string[sindex];
7971 istring = sub_append_string (temp, istring, &istring_index, &istring_size);
7977 #if defined (PROCESS_SUBSTITUTION)
7978 /* Process substitution. */
7982 if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)) || posixly_correct)
7984 sindex--; /* add_character: label increments sindex */
7988 t_index = sindex + 1; /* skip past both '<' and LPAREN */
7990 temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index); /*))*/
7993 /* If the process substitution specification is `<()', we want to
7994 open the pipe for writing in the child and produce output; if
7995 it is `>()', we want to open the pipe for reading in the child
7996 and consume input. */
7997 temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
8001 goto dollar_add_string;
8003 #endif /* PROCESS_SUBSTITUTION */
8006 /* Posix.2 section 3.6.1 says that tildes following `=' in words
8007 which are not assignment statements are not expanded. If the
8008 shell isn't in posix mode, though, we perform tilde expansion
8009 on `likely candidate' unquoted assignment statements (flags
8010 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
8011 contains an unquoted :~ or =~. Something to think about: we
8012 now have a flag that says to perform tilde expansion on arguments
8013 to `assignment builtins' like declare and export that look like
8014 assignment statements. We now do tilde expansion on such words
8015 even in POSIX mode. */
8016 if (word->flags & (W_ASSIGNRHS|W_NOTILDE))
8018 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
8019 goto add_ifs_character;
8023 /* If we're not in posix mode or forcing assignment-statement tilde
8024 expansion, note where the `=' appears in the word and prepare to
8025 do tilde expansion following the first `='. */
8026 if ((word->flags & W_ASSIGNMENT) &&
8027 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
8028 assignoff == -1 && sindex > 0)
8030 if (sindex == assignoff && string[sindex+1] == '~') /* XXX */
8031 word->flags |= W_ITILDE;
8033 else if ((word->flags & W_ASSIGNMENT) &&
8034 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
8035 string[sindex+1] == '~')
8036 word->flags |= W_ITILDE;
8038 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
8039 goto add_ifs_character;
8044 if (word->flags & W_NOTILDE)
8046 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
8047 goto add_ifs_character;
8052 if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) &&
8053 string[sindex+1] == '~')
8054 word->flags |= W_ITILDE;
8056 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
8057 goto add_ifs_character;
8062 /* If the word isn't supposed to be tilde expanded, or we're not
8063 at the start of a word or after an unquoted : or = in an
8064 assignment statement, we don't do tilde expansion. */
8065 if ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
8066 (sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
8067 (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
8069 word->flags &= ~W_ITILDE;
8070 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
8071 goto add_ifs_character;
8076 if (word->flags & W_ASSIGNRHS)
8078 else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
8083 temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
8085 word->flags &= ~W_ITILDE;
8087 if (temp && *temp && t_index > 0)
8089 temp1 = bash_tilde_expand (temp, tflag);
8090 if (temp1 && *temp1 == '~' && STREQ (temp, temp1))
8094 goto add_character; /* tilde expansion failed */
8099 goto add_quoted_string; /* XXX was add_string */
8108 if (expanded_something)
8109 *expanded_something = 1;
8112 pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0;
8113 if (word->flags & W_NOSPLIT2)
8114 pflags |= PF_NOSPLIT2;
8115 tword = param_expand (string, &sindex, quoted, expanded_something,
8116 &has_dollar_at, "ed_dollar_at,
8117 &had_quoted_null, pflags);
8119 if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
8123 return ((tword == &expand_wdesc_error) ? &expand_word_error
8124 : &expand_word_fatal);
8126 if (contains_dollar_at && has_dollar_at)
8127 *contains_dollar_at = 1;
8129 if (tword && (tword->flags & W_HASQUOTEDNULL))
8130 had_quoted_null = 1;
8133 dispose_word_desc (tword);
8138 case '`': /* Backquoted command substitution. */
8142 temp = string_extract (string, &sindex, "`", SX_REQMATCH);
8143 /* The test of sindex against t_index is to allow bare instances of
8144 ` to pass through, for backwards compatibility. */
8145 if (temp == &extract_string_error || temp == &extract_string_fatal)
8147 if (sindex - 1 == t_index)
8152 report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index);
8155 return ((temp == &extract_string_error) ? &expand_word_error
8156 : &expand_word_fatal);
8159 if (expanded_something)
8160 *expanded_something = 1;
8162 if (word->flags & W_NOCOMSUB)
8163 /* sindex + 1 because string[sindex] == '`' */
8164 temp1 = substring (string, t_index, sindex + 1);
8167 de_backslash (temp);
8168 tword = command_substitute (temp, quoted);
8169 temp1 = tword ? tword->word : (char *)NULL;
8171 dispose_word_desc (tword);
8175 goto dollar_add_string;
8179 if (string[sindex + 1] == '\n')
8185 c = string[++sindex];
8187 if (quoted & Q_HERE_DOCUMENT)
8189 else if (quoted & Q_DOUBLE_QUOTES)
8194 /* From Posix discussion on austin-group list: Backslash escaping
8195 a } in ${...} is removed. Issue 0000221 */
8196 if ((quoted & Q_DOLBRACE) && c == RBRACE)
8198 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
8200 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
8202 SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
8207 sindex--; /* add_character: label increments sindex */
8212 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
8217 /* BEFORE jumping here, we need to increment sindex if appropriate */
8218 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
8219 DEFAULT_ARRAY_SIZE);
8220 istring[istring_index++] = twochars[0];
8221 istring[istring_index++] = twochars[1];
8222 istring[istring_index] = '\0';
8228 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
8230 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
8235 temp = string_extract_double_quoted (string, &sindex, 0);
8237 /* If the quotes surrounded the entire string, then the
8238 whole word was quoted. */
8239 quoted_state = (t_index == 1 && string[sindex] == '\0')
8245 tword = alloc_word_desc ();
8248 temp = (char *)NULL;
8251 /* Need to get W_HASQUOTEDNULL flag through this function. */
8252 list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &has_dollar_at, (int *)NULL);
8254 if (list == &expand_word_error || list == &expand_word_fatal)
8258 /* expand_word_internal has already freed temp_word->word
8259 for us because of the way it prints error messages. */
8260 tword->word = (char *)NULL;
8261 dispose_word (tword);
8265 dispose_word (tword);
8267 /* "$@" (a double-quoted dollar-at) expands into nothing,
8268 not even a NULL word, when there are no positional
8270 if (list == 0 && has_dollar_at)
8276 /* If we get "$@", we know we have expanded something, so we
8277 need to remember it for the final split on $IFS. This is
8278 a special case; it's the only case where a quoted string
8279 can expand into more than one word. It's going to come back
8280 from the above call to expand_word_internal as a list with
8281 a single word, in which all characters are quoted and
8282 separated by blanks. What we want to do is to turn it back
8283 into a list for the next piece of code. */
8285 dequote_list (list);
8287 if (list && list->word && (list->word->flags & W_HASQUOTEDNULL))
8288 had_quoted_null = 1;
8293 if (contains_dollar_at)
8294 *contains_dollar_at = 1;
8295 if (expanded_something)
8296 *expanded_something = 1;
8301 /* What we have is "". This is a minor optimization. */
8303 list = (WORD_LIST *)NULL;
8306 /* The code above *might* return a list (consider the case of "$@",
8307 where it returns "$1", "$2", etc.). We can't throw away the
8308 rest of the list, and we have to make sure each word gets added
8309 as quoted. We test on tresult->next: if it is non-NULL, we
8310 quote the whole list, save it to a string with string_list, and
8311 add that string. We don't need to quote the results of this
8312 (and it would be wrong, since that would quote the separators
8313 as well), so we go directly to add_string. */
8319 if (quoted_dollar_at && (word->flags & W_NOSPLIT2))
8320 temp = string_list_internal (quote_list (list), " ");
8323 /* Testing quoted_dollar_at makes sure that "$@" is
8324 split correctly when $IFS does not contain a space. */
8325 temp = quoted_dollar_at
8326 ? string_list_dollar_at (list, Q_DOUBLE_QUOTES)
8327 : string_list (quote_list (list));
8328 dispose_words (list);
8333 temp = savestring (list->word->word);
8334 tflag = list->word->flags;
8335 dispose_words (list);
8337 /* If the string is not a quoted null string, we want
8338 to remove any embedded unquoted CTLNUL characters.
8339 We do not want to turn quoted null strings back into
8340 the empty string, though. We do this because we
8341 want to remove any quoted nulls from expansions that
8342 contain other characters. For example, if we have
8343 x"$*"y or "x$*y" and there are no positional parameters,
8344 the $* should expand into nothing. */
8345 /* We use the W_HASQUOTEDNULL flag to differentiate the
8346 cases: a quoted null character as above and when
8347 CTLNUL is contained in the (non-null) expansion
8348 of some variable. We use the had_quoted_null flag to
8349 pass the value through this function to its caller. */
8350 if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0)
8351 remove_quoted_nulls (temp); /* XXX */
8355 temp = (char *)NULL;
8357 /* We do not want to add quoted nulls to strings that are only
8358 partially quoted; we can throw them away. */
8359 if (temp == 0 && quoted_state == PARTIALLY_QUOTED && (word->flags & (W_NOSPLIT|W_NOSPLIT2)))
8367 temp = quote_string (temp);
8375 sindex--; /* add_character: label increments sindex */
8383 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
8385 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
8390 temp = string_extract_single_quoted (string, &sindex);
8392 /* If the entire STRING was surrounded by single quotes,
8393 then the string is wholly quoted. */
8394 quoted_state = (t_index == 1 && string[sindex] == '\0')
8398 /* If all we had was '', it is a null expansion. */
8402 temp = (char *)NULL;
8405 remove_quoted_escapes (temp); /* ??? */
8407 /* We do not want to add quoted nulls to strings that are only
8408 partially quoted; such nulls are discarded. */
8409 if (temp == 0 && (quoted_state == PARTIALLY_QUOTED))
8412 /* If we have a quoted null expansion, add a quoted NULL to istring. */
8416 sindex--; /* add_character: label increments sindex */
8420 goto add_quoted_string;
8425 /* This is the fix for " $@ " */
8427 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c)))
8429 if (string[sindex]) /* from old goto dollar_add_string */
8438 #if HANDLE_MULTIBYTE
8444 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
8449 twochars[0] = CTLESC;
8456 SADD_MBCHAR (temp, string, sindex, string_size);
8459 RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
8460 DEFAULT_ARRAY_SIZE);
8461 istring[istring_index++] = c;
8462 istring[istring_index] = '\0';
8464 /* Next character. */
8469 finished_with_string:
8470 /* OK, we're ready to return. If we have a quoted string, and
8471 quoted_dollar_at is not set, we do no splitting at all; otherwise
8472 we split on ' '. The routines that call this will handle what to
8473 do if nothing has been expanded. */
8475 /* Partially and wholly quoted strings which expand to the empty
8476 string are retained as an empty arguments. Unquoted strings
8477 which expand to the empty string are discarded. The single
8478 exception is the case of expanding "$@" when there are no
8479 positional parameters. In that case, we discard the expansion. */
8481 /* Because of how the code that handles "" and '' in partially
8482 quoted strings works, we need to make ISTRING into a QUOTED_NULL
8483 if we saw quoting characters, but the expansion was empty.
8484 "" and '' are tossed away before we get to this point when
8485 processing partially quoted strings. This makes "" and $xxx""
8486 equivalent when xxx is unset. We also look to see whether we
8487 saw a quoted null from a ${} expansion and add one back if we
8490 /* If we expand to nothing and there were no single or double quotes
8491 in the word, we throw it away. Otherwise, we return a NULL word.
8492 The single exception is for $@ surrounded by double quotes when
8493 there are no positional parameters. In that case, we also throw
8496 if (*istring == '\0')
8498 if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
8500 istring[0] = CTLNUL;
8502 tword = make_bare_word (istring);
8503 tword->flags |= W_HASQUOTEDNULL; /* XXX */
8504 list = make_word_list (tword, (WORD_LIST *)NULL);
8505 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8506 tword->flags |= W_QUOTED;
8508 /* According to sh, ksh, and Posix.2, if a word expands into nothing
8509 and a double-quoted "$@" appears anywhere in it, then the entire
8511 else if (quoted_state == UNQUOTED || quoted_dollar_at)
8512 list = (WORD_LIST *)NULL;
8516 tword = make_bare_word (istring);
8517 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8518 tword->flags |= W_QUOTED;
8519 list = make_word_list (tword, (WORD_LIST *)NULL);
8523 list = (WORD_LIST *)NULL;
8526 else if (word->flags & W_NOSPLIT)
8528 tword = make_bare_word (istring);
8529 if (word->flags & W_ASSIGNMENT)
8530 tword->flags |= W_ASSIGNMENT; /* XXX */
8531 if (word->flags & W_COMPASSIGN)
8532 tword->flags |= W_COMPASSIGN; /* XXX */
8533 if (word->flags & W_NOGLOB)
8534 tword->flags |= W_NOGLOB; /* XXX */
8535 if (word->flags & W_NOEXPAND)
8536 tword->flags |= W_NOEXPAND; /* XXX */
8537 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8538 tword->flags |= W_QUOTED;
8539 if (had_quoted_null)
8540 tword->flags |= W_HASQUOTEDNULL;
8541 list = make_word_list (tword, (WORD_LIST *)NULL);
8547 ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
8549 /* If we have $@, we need to split the results no matter what. If
8550 IFS is unset or NULL, string_list_dollar_at has separated the
8551 positional parameters with a space, so we split on space (we have
8552 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
8553 string_list_dollar_at has separated the positional parameters
8554 with the first character of $IFS, so we split on $IFS. */
8555 if (has_dollar_at && ifs_chars)
8556 list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
8559 tword = make_bare_word (istring);
8560 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
8561 tword->flags |= W_QUOTED;
8562 if (word->flags & W_ASSIGNMENT)
8563 tword->flags |= W_ASSIGNMENT;
8564 if (word->flags & W_COMPASSIGN)
8565 tword->flags |= W_COMPASSIGN;
8566 if (word->flags & W_NOGLOB)
8567 tword->flags |= W_NOGLOB;
8568 if (word->flags & W_NOEXPAND)
8569 tword->flags |= W_NOEXPAND;
8570 if (had_quoted_null)
8571 tword->flags |= W_HASQUOTEDNULL; /* XXX */
8572 list = make_word_list (tword, (WORD_LIST *)NULL);
8580 /* **************************************************************** */
8582 /* Functions for Quote Removal */
8584 /* **************************************************************** */
8586 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
8587 backslash quoting rules for within double quotes or a here document. */
8589 string_quote_removal (string, quoted)
8594 char *r, *result_string, *temp, *send;
8595 int sindex, tindex, dquote;
8599 /* The result can be no longer than the original string. */
8600 slen = strlen (string);
8601 send = string + slen;
8603 r = result_string = (char *)xmalloc (slen + 1);
8605 for (dquote = sindex = 0; c = string[sindex];)
8610 c = string[++sindex];
8616 if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
8621 SCOPY_CHAR_M (r, string, send, sindex);
8625 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
8631 tindex = sindex + 1;
8632 temp = string_extract_single_quoted (string, &tindex);
8643 dquote = 1 - dquote;
8649 return (result_string);
8654 /* Perform quote removal on word WORD. This allocates and returns a new
8657 word_quote_removal (word, quoted)
8664 t = string_quote_removal (word->word, quoted);
8665 w = alloc_word_desc ();
8666 w->word = t ? t : savestring ("");
8670 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
8671 the members of the list are treated as if they are surrounded by
8672 double quotes. Return a new list, or NULL if LIST is NULL. */
8674 word_list_quote_removal (list, quoted)
8678 WORD_LIST *result, *t, *tresult, *e;
8680 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
8682 tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
8684 result = (WORD_LIST *) list_append (result, tresult);
8687 result = e = tresult;
8700 /*******************************************
8702 * Functions to perform word splitting *
8704 *******************************************/
8714 ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n";
8716 /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
8717 handle multibyte chars in IFS */
8718 memset (ifs_cmap, '\0', sizeof (ifs_cmap));
8719 for (t = ifs_value ; t && *t; t++)
8725 #if defined (HANDLE_MULTIBYTE)
8728 ifs_firstc[0] = '\0';
8734 ifs_len = strnlen (ifs_value, MB_CUR_MAX);
8735 ifs_firstc_len = MBLEN (ifs_value, ifs_len);
8736 if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len))
8738 ifs_firstc[0] = ifs_value[0];
8739 ifs_firstc[1] = '\0';
8743 memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
8746 ifs_firstc = ifs_value ? *ifs_value : 0;
8756 /* This splits a single word into a WORD LIST on $IFS, but only if the word
8757 is not quoted. list_string () performs quote removal for us, even if we
8758 don't do any splitting. */
8760 word_split (w, ifs_chars)
8770 xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
8771 result = list_string (w->word, xifs, w->flags & W_QUOTED);
8774 result = (WORD_LIST *)NULL;
8779 /* Perform word splitting on LIST and return the RESULT. It is possible
8780 to return (WORD_LIST *)NULL. */
8782 word_list_split (list)
8785 WORD_LIST *result, *t, *tresult, *e;
8787 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
8789 tresult = word_split (t->word, ifs_value);
8791 result = e = tresult;
8802 /**************************************************
8804 * Functions to expand an entire WORD_LIST *
8806 **************************************************/
8808 /* Do any word-expansion-specific cleanup and jump to top_level */
8810 exp_jump_to_top_level (v)
8813 set_pipestatus_from_exit (last_command_exit_value);
8815 /* Cleanup code goes here. */
8816 expand_no_split_dollar_star = 0; /* XXX */
8817 expanding_redir = 0;
8818 assigning_in_environment = 0;
8820 if (parse_and_execute_level == 0)
8821 top_level_cleanup (); /* from sig.c */
8823 jump_to_top_level (v);
8826 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
8827 ELIST, and set ELIST to the new list. */
8828 #define PREPEND_LIST(nlist, elist) \
8829 do { nlist->next = elist; elist = nlist; } while (0)
8831 /* Separate out any initial variable assignments from TLIST. If set -k has
8832 been executed, remove all assignment statements from TLIST. Initial
8833 variable assignments and other environment assignments are placed
8834 on SUBST_ASSIGN_VARLIST. */
8836 separate_out_assignments (tlist)
8839 register WORD_LIST *vp, *lp;
8842 return ((WORD_LIST *)NULL);
8844 if (subst_assign_varlist)
8845 dispose_words (subst_assign_varlist); /* Clean up after previous error */
8847 subst_assign_varlist = (WORD_LIST *)NULL;
8850 /* Separate out variable assignments at the start of the command.
8851 Loop invariant: vp->next == lp
8853 lp = list of words left after assignment statements skipped
8854 tlist = original list of words
8856 while (lp && (lp->word->flags & W_ASSIGNMENT))
8862 /* If lp != tlist, we have some initial assignment statements.
8863 We make SUBST_ASSIGN_VARLIST point to the list of assignment
8864 words and TLIST point to the remaining words. */
8867 subst_assign_varlist = tlist;
8868 /* ASSERT(vp->next == lp); */
8869 vp->next = (WORD_LIST *)NULL; /* terminate variable list */
8870 tlist = lp; /* remainder of word list */
8873 /* vp == end of variable list */
8874 /* tlist == remainder of original word list without variable assignments */
8876 /* All the words in tlist were assignment statements */
8877 return ((WORD_LIST *)NULL);
8879 /* ASSERT(tlist != NULL); */
8880 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
8882 /* If the -k option is in effect, we need to go through the remaining
8883 words, separate out the assignment words, and place them on
8884 SUBST_ASSIGN_VARLIST. */
8885 if (place_keywords_in_env)
8887 WORD_LIST *tp; /* tp == running pointer into tlist */
8892 /* Loop Invariant: tp->next == lp */
8893 /* Loop postcondition: tlist == word list without assignment statements */
8896 if (lp->word->flags & W_ASSIGNMENT)
8898 /* Found an assignment statement, add this word to end of
8899 subst_assign_varlist (vp). */
8900 if (!subst_assign_varlist)
8901 subst_assign_varlist = vp = lp;
8908 /* Remove the word pointed to by LP from TLIST. */
8909 tp->next = lp->next;
8910 /* ASSERT(vp == lp); */
8911 lp->next = (WORD_LIST *)NULL;
8924 #define WEXP_VARASSIGN 0x001
8925 #define WEXP_BRACEEXP 0x002
8926 #define WEXP_TILDEEXP 0x004
8927 #define WEXP_PARAMEXP 0x008
8928 #define WEXP_PATHEXP 0x010
8930 /* All of the expansions, including variable assignments at the start of
8932 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
8934 /* All of the expansions except variable assignments at the start of
8936 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
8938 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
8939 expansion, command substitution, arithmetic expansion, word splitting, and
8941 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
8943 /* Take the list of words in LIST and do the various substitutions. Return
8944 a new list of words which is the expanded list, and without things like
8945 variable assignments. */
8951 return (expand_word_list_internal (list, WEXP_ALL));
8954 /* Same as expand_words (), but doesn't hack variable or environment
8957 expand_words_no_vars (list)
8960 return (expand_word_list_internal (list, WEXP_NOVARS));
8964 expand_words_shellexp (list)
8967 return (expand_word_list_internal (list, WEXP_SHELLEXP));
8971 glob_expand_word_list (tlist, eflags)
8975 char **glob_array, *temp_string;
8976 register int glob_index;
8977 WORD_LIST *glob_list, *output_list, *disposables, *next;
8980 output_list = disposables = (WORD_LIST *)NULL;
8981 glob_array = (char **)NULL;
8984 /* For each word, either globbing is attempted or the word is
8985 added to orig_list. If globbing succeeds, the results are
8986 added to orig_list and the word (tlist) is added to the list
8987 of disposable words. If globbing fails and failed glob
8988 expansions are left unchanged (the shell default), the
8989 original word is added to orig_list. If globbing fails and
8990 failed glob expansions are removed, the original word is
8991 added to the list of disposable words. orig_list ends up
8992 in reverse order and requires a call to REVERSE_LIST to
8993 be set right. After all words are examined, the disposable
8997 /* If the word isn't an assignment and contains an unquoted
8998 pattern matching character, then glob it. */
8999 if ((tlist->word->flags & W_NOGLOB) == 0 &&
9000 unquoted_glob_pattern_p (tlist->word->word))
9002 glob_array = shell_glob_filename (tlist->word->word);
9004 /* Handle error cases.
9005 I don't think we should report errors like "No such file
9006 or directory". However, I would like to report errors
9007 like "Read failed". */
9009 if (glob_array == 0 || GLOB_FAILED (glob_array))
9011 glob_array = (char **)xmalloc (sizeof (char *));
9012 glob_array[0] = (char *)NULL;
9015 /* Dequote the current word in case we have to use it. */
9016 if (glob_array[0] == NULL)
9018 temp_string = dequote_string (tlist->word->word);
9019 free (tlist->word->word);
9020 tlist->word->word = temp_string;
9023 /* Make the array into a word list. */
9024 glob_list = (WORD_LIST *)NULL;
9025 for (glob_index = 0; glob_array[glob_index]; glob_index++)
9027 tword = make_bare_word (glob_array[glob_index]);
9028 tword->flags |= W_GLOBEXP; /* XXX */
9029 glob_list = make_word_list (tword, glob_list);
9034 output_list = (WORD_LIST *)list_append (glob_list, output_list);
9035 PREPEND_LIST (tlist, disposables);
9037 else if (fail_glob_expansion != 0)
9039 report_error (_("no match: %s"), tlist->word->word);
9040 exp_jump_to_top_level (DISCARD);
9042 else if (allow_null_glob_expansion == 0)
9044 /* Failed glob expressions are left unchanged. */
9045 PREPEND_LIST (tlist, output_list);
9049 /* Failed glob expressions are removed. */
9050 PREPEND_LIST (tlist, disposables);
9055 /* Dequote the string. */
9056 temp_string = dequote_string (tlist->word->word);
9057 free (tlist->word->word);
9058 tlist->word->word = temp_string;
9059 PREPEND_LIST (tlist, output_list);
9062 strvec_dispose (glob_array);
9063 glob_array = (char **)NULL;
9069 dispose_words (disposables);
9072 output_list = REVERSE_LIST (output_list, WORD_LIST *);
9074 return (output_list);
9077 #if defined (BRACE_EXPANSION)
9079 brace_expand_word_list (tlist, eflags)
9083 register char **expansions;
9085 WORD_LIST *disposables, *output_list, *next;
9089 for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
9093 if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
9095 /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
9096 PREPEND_LIST (tlist, output_list);
9100 /* Only do brace expansion if the word has a brace character. If
9101 not, just add the word list element to BRACES and continue. In
9102 the common case, at least when running shell scripts, this will
9103 degenerate to a bunch of calls to `mbschr', and then what is
9104 basically a reversal of TLIST into BRACES, which is corrected
9105 by a call to REVERSE_LIST () on BRACES when the end of TLIST
9107 if (mbschr (tlist->word->word, LBRACE))
9109 expansions = brace_expand (tlist->word->word);
9111 for (eindex = 0; temp_string = expansions[eindex]; eindex++)
9113 w = make_word (temp_string);
9114 /* If brace expansion didn't change the word, preserve
9115 the flags. We may want to preserve the flags
9116 unconditionally someday -- XXX */
9117 if (STREQ (temp_string, tlist->word->word))
9118 w->flags = tlist->word->flags;
9119 output_list = make_word_list (w, output_list);
9120 free (expansions[eindex]);
9124 /* Add TLIST to the list of words to be freed after brace
9125 expansion has been performed. */
9126 PREPEND_LIST (tlist, disposables);
9129 PREPEND_LIST (tlist, output_list);
9133 dispose_words (disposables);
9136 output_list = REVERSE_LIST (output_list, WORD_LIST *);
9138 return (output_list);
9142 #if defined (ARRAY_VARS)
9143 /* Take WORD, a compound associative array assignment, and internally run
9144 'declare -A w', where W is the variable name portion of WORD. */
9146 make_internal_declare (word, option)
9154 w = make_word (word);
9156 t = assignment (w->word, 0);
9159 wl = make_word_list (w, (WORD_LIST *)NULL);
9160 wl = make_word_list (make_word (option), wl);
9162 return (declare_builtin (wl));
9167 shell_expand_word_list (tlist, eflags)
9171 WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list;
9172 int expanded_something, has_dollar_at;
9175 /* We do tilde expansion all the time. This is what 1003.2 says. */
9176 new_list = (WORD_LIST *)NULL;
9177 for (orig_list = tlist; tlist; tlist = next)
9179 temp_string = tlist->word->word;
9183 #if defined (ARRAY_VARS)
9184 /* If this is a compound array assignment to a builtin that accepts
9185 such assignments (e.g., `declare'), take the assignment and perform
9186 it separately, handling the semantics of declarations inside shell
9187 functions. This avoids the double-evaluation of such arguments,
9188 because `declare' does some evaluation of compound assignments on
9190 if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
9194 if (tlist->word->flags & W_ASSIGNASSOC)
9195 make_internal_declare (tlist->word->word, "-A");
9197 t = do_word_assignment (tlist->word, 0);
9200 last_command_exit_value = EXECUTION_FAILURE;
9201 exp_jump_to_top_level (DISCARD);
9204 /* Now transform the word as ksh93 appears to do and go on */
9205 t = assignment (tlist->word->word, 0);
9206 tlist->word->word[t] = '\0';
9207 tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC);
9211 expanded_something = 0;
9212 expanded = expand_word_internal
9213 (tlist->word, 0, 0, &has_dollar_at, &expanded_something);
9215 if (expanded == &expand_word_error || expanded == &expand_word_fatal)
9217 /* By convention, each time this error is returned,
9218 tlist->word->word has already been freed. */
9219 tlist->word->word = (char *)NULL;
9221 /* Dispose our copy of the original list. */
9222 dispose_words (orig_list);
9223 /* Dispose the new list we're building. */
9224 dispose_words (new_list);
9226 last_command_exit_value = EXECUTION_FAILURE;
9227 if (expanded == &expand_word_error)
9228 exp_jump_to_top_level (DISCARD);
9230 exp_jump_to_top_level (FORCE_EOF);
9233 /* Don't split words marked W_NOSPLIT. */
9234 if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
9236 temp_list = word_list_split (expanded);
9237 dispose_words (expanded);
9241 /* If no parameter expansion, command substitution, process
9242 substitution, or arithmetic substitution took place, then
9243 do not do word splitting. We still have to remove quoted
9244 null characters from the result. */
9245 word_list_remove_quoted_nulls (expanded);
9246 temp_list = expanded;
9249 expanded = REVERSE_LIST (temp_list, WORD_LIST *);
9250 new_list = (WORD_LIST *)list_append (expanded, new_list);
9254 dispose_words (orig_list);
9257 new_list = REVERSE_LIST (new_list, WORD_LIST *);
9262 /* The workhorse for expand_words () and expand_words_no_vars ().
9263 First arg is LIST, a WORD_LIST of words.
9264 Second arg EFLAGS is a flags word controlling which expansions are
9267 This does all of the substitutions: brace expansion, tilde expansion,
9268 parameter expansion, command substitution, arithmetic expansion,
9269 process substitution, word splitting, and pathname expansion, according
9270 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
9271 set, or for which no expansion is done, do not undergo word splitting.
9272 Words with the W_NOGLOB bit set do not undergo pathname expansion. */
9274 expand_word_list_internal (list, eflags)
9278 WORD_LIST *new_list, *temp_list;
9282 return ((WORD_LIST *)NULL);
9284 garglist = new_list = copy_word_list (list);
9285 if (eflags & WEXP_VARASSIGN)
9287 garglist = new_list = separate_out_assignments (new_list);
9290 if (subst_assign_varlist)
9292 /* All the words were variable assignments, so they are placed
9293 into the shell's environment. */
9294 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
9296 this_command_name = (char *)NULL; /* no arithmetic errors */
9297 tint = do_word_assignment (temp_list->word, 0);
9298 /* Variable assignment errors in non-interactive shells
9299 running in Posix.2 mode cause the shell to exit. */
9302 last_command_exit_value = EXECUTION_FAILURE;
9303 if (interactive_shell == 0 && posixly_correct)
9304 exp_jump_to_top_level (FORCE_EOF);
9306 exp_jump_to_top_level (DISCARD);
9309 dispose_words (subst_assign_varlist);
9310 subst_assign_varlist = (WORD_LIST *)NULL;
9312 return ((WORD_LIST *)NULL);
9316 /* Begin expanding the words that remain. The expansions take place on
9317 things that aren't really variable assignments. */
9319 #if defined (BRACE_EXPANSION)
9320 /* Do brace expansion on this word if there are any brace characters
9322 if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
9323 new_list = brace_expand_word_list (new_list, eflags);
9324 #endif /* BRACE_EXPANSION */
9326 /* Perform the `normal' shell expansions: tilde expansion, parameter and
9327 variable substitution, command substitution, arithmetic expansion,
9328 and word splitting. */
9329 new_list = shell_expand_word_list (new_list, eflags);
9331 /* Okay, we're almost done. Now let's just do some filename
9335 if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
9336 /* Glob expand the word list unless globbing has been disabled. */
9337 new_list = glob_expand_word_list (new_list, eflags);
9339 /* Dequote the words, because we're not performing globbing. */
9340 new_list = dequote_list (new_list);
9343 if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
9345 sh_wassign_func_t *assign_func;
9346 int is_special_builtin, is_builtin_or_func;
9348 /* If the remainder of the words expand to nothing, Posix.2 requires
9349 that the variable and environment assignments affect the shell's
9351 assign_func = new_list ? assign_in_env : do_word_assignment;
9352 tempenv_assign_error = 0;
9354 is_builtin_or_func = (new_list && new_list->word && (find_shell_builtin (new_list->word->word) || find_function (new_list->word->word)));
9355 /* Posix says that special builtins exit if a variable assignment error
9356 occurs in an assignment preceding it. */
9357 is_special_builtin = (posixly_correct && new_list && new_list->word && find_special_builtin (new_list->word->word));
9359 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
9361 this_command_name = (char *)NULL;
9362 assigning_in_environment = (assign_func == assign_in_env);
9363 tint = (*assign_func) (temp_list->word, is_builtin_or_func);
9364 assigning_in_environment = 0;
9365 /* Variable assignment errors in non-interactive shells running
9366 in Posix.2 mode cause the shell to exit. */
9369 if (assign_func == do_word_assignment)
9371 last_command_exit_value = EXECUTION_FAILURE;
9372 if (interactive_shell == 0 && posixly_correct && is_special_builtin)
9373 exp_jump_to_top_level (FORCE_EOF);
9375 exp_jump_to_top_level (DISCARD);
9378 tempenv_assign_error++;
9382 dispose_words (subst_assign_varlist);
9383 subst_assign_varlist = (WORD_LIST *)NULL;
9387 tint = list_length (new_list) + 1;
9388 RESIZE_MALLOCED_BUFFER (glob_argv_flags, 0, tint, glob_argv_flags_size, 16);
9389 for (tint = 0, temp_list = new_list; temp_list; temp_list = temp_list->next)
9390 glob_argv_flags[tint++] = (temp_list->word->flags & W_GLOBEXP) ? '1' : '0';
9391 glob_argv_flags[tint] = '\0';