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-2009 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"
47 #include "execute_cmd.h"
51 #include "mailcheck.h"
55 #include "builtins/getopt.h"
56 #include "builtins/common.h"
58 #include "builtins/builtext.h"
60 #include <tilde/tilde.h>
61 #include <glob/strmatch.h>
67 /* The size that strings change by. */
68 #define DEFAULT_INITIAL_ARRAY_SIZE 112
69 #define DEFAULT_ARRAY_SIZE 128
75 #define VT_ARRAYMEMBER 3
78 #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */
80 /* Flags for quoted_strchr */
81 #define ST_BACKSL 0x01
82 #define ST_CTLESC 0x02
83 #define ST_SQUOTE 0x04 /* unused yet */
84 #define ST_DQUOTE 0x08 /* unused yet */
86 /* Flags for the `pflags' argument to param_expand() */
87 #define PF_NOCOMSUB 0x01 /* Do not perform command substitution */
88 #define PF_IGNUNBOUND 0x02 /* ignore unbound vars even if -u set */
89 #define PF_NOSPLIT2 0x04 /* same as W_NOSPLIT2 */
91 /* These defs make it easier to use the editor. */
97 #if defined (HANDLE_MULTIBYTE)
102 /* Evaluates to 1 if C is one of the shell's special parameters whose length
103 can be taken, but is also one of the special expansion characters. */
104 #define VALID_SPECIAL_LENGTH_PARAM(c) \
105 ((c) == '-' || (c) == '?' || (c) == '#')
107 /* Evaluates to 1 if C is one of the shell's special parameters for which an
108 indirect variable reference may be made. */
109 #define VALID_INDIR_PARAM(c) \
110 ((c) == '#' || (c) == '?' || (c) == '@' || (c) == '*')
112 /* Evaluates to 1 if C is one of the OP characters that follows the parameter
113 in ${parameter[:]OPword}. */
114 #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
116 /* Evaluates to 1 if this is one of the shell's special variables. */
117 #define SPECIAL_VAR(name, wi) \
118 ((DIGIT (*name) && all_digits (name)) || \
119 (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
120 (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))
122 /* An expansion function that takes a string and a quoted flag and returns
123 a WORD_LIST *. Used as the type of the third argument to
124 expand_string_if_necessary(). */
125 typedef WORD_LIST *EXPFUNC __P((char *, int));
127 /* Process ID of the last command executed within command substitution. */
128 pid_t last_command_subst_pid = NO_PID;
129 pid_t current_command_subst_pid = NO_PID;
131 /* Variables used to keep track of the characters in IFS. */
134 unsigned char ifs_cmap[UCHAR_MAX + 1];
136 #if defined (HANDLE_MULTIBYTE)
137 unsigned char ifs_firstc[MB_LEN_MAX];
138 size_t ifs_firstc_len;
140 unsigned char ifs_firstc;
143 /* Sentinel to tell when we are performing variable assignments preceding a
144 command name and putting them into the environment. Used to make sure
145 we use the temporary environment when looking up variable values. */
146 int assigning_in_environment;
148 /* Used to hold a list of variable assignments preceding a command. Global
149 so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
150 SIGCHLD trap and so it can be saved and restored by the trap handlers. */
151 WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
153 /* Extern functions and variables from different files. */
154 extern int last_command_exit_value, last_command_exit_signal;
155 extern int subshell_environment, line_number;
156 extern int subshell_level, parse_and_execute_level, sourcelevel;
157 extern int eof_encountered;
158 extern int return_catch_flag, return_catch_value;
159 extern pid_t dollar_dollar_pid;
160 extern int posixly_correct;
161 extern char *this_command_name;
162 extern struct fd_bitmap *current_fds_to_close;
163 extern int wordexp_only;
164 extern int expanding_redir;
165 extern int tempenv_assign_error;
167 #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
168 extern wchar_t *wcsdup __P((const wchar_t *));
171 /* Non-zero means to allow unmatched globbed filenames to expand to
173 int allow_null_glob_expansion;
175 /* Non-zero means to throw an error when globbing fails to match anything. */
176 int fail_glob_expansion;
179 /* Variables to keep track of which words in an expanded word list (the
180 output of expand_word_list_internal) are the result of globbing
181 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
182 (CURRENTLY UNUSED). */
183 char *glob_argv_flags;
184 static int glob_argv_flags_size;
187 static WORD_LIST expand_word_error, expand_word_fatal;
188 static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
189 static char expand_param_error, expand_param_fatal;
190 static char extract_string_error, extract_string_fatal;
192 /* Tell the expansion functions to not longjmp back to top_level on fatal
193 errors. Enabled when doing completion and prompt string expansion. */
194 static int no_longjmp_on_fatal_error = 0;
196 /* Set by expand_word_unsplit; used to inhibit splitting and re-joining
197 $* on $IFS, primarily when doing assignment statements. */
198 static int expand_no_split_dollar_star = 0;
200 /* A WORD_LIST of words to be expanded by expand_word_list_internal,
201 without any leading variable assignments. */
202 static WORD_LIST *garglist = (WORD_LIST *)NULL;
204 static char *quoted_substring __P((char *, int, int));
205 static int quoted_strlen __P((char *));
206 static char *quoted_strchr __P((char *, int, int));
208 static char *expand_string_if_necessary __P((char *, int, EXPFUNC *));
209 static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *));
210 static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
211 static WORD_LIST *expand_string_internal __P((char *, int));
212 static WORD_LIST *expand_string_leave_quoted __P((char *, int));
213 static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *));
215 static WORD_LIST *list_quote_escapes __P((WORD_LIST *));
216 static char *make_quoted_char __P((int));
217 static WORD_LIST *quote_list __P((WORD_LIST *));
219 static int unquoted_substring __P((char *, char *));
220 static int unquoted_member __P((int, char *));
222 #if defined (ARRAY_VARS)
223 static SHELL_VAR *do_compound_assignment __P((char *, char *, int));
225 static int do_assignment_internal __P((const WORD_DESC *, int));
227 static char *string_extract_verbatim __P((char *, size_t, int *, char *, int));
228 static char *string_extract __P((char *, int *, char *, int));
229 static char *string_extract_double_quoted __P((char *, int *, int));
230 static inline char *string_extract_single_quoted __P((char *, int *));
231 static inline int skip_single_quoted __P((const char *, size_t, int));
232 static int skip_double_quoted __P((char *, size_t, int));
233 static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int));
234 static char *extract_dollar_brace_string __P((char *, int *, int, int));
235 static int skip_matched_pair __P((const char *, int, int, int, int));
237 static char *pos_params __P((char *, int, int, int));
239 static unsigned char *mb_getcharlens __P((char *, int));
241 static char *remove_upattern __P((char *, char *, int));
242 #if defined (HANDLE_MULTIBYTE)
243 static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int));
245 static char *remove_pattern __P((char *, char *, int));
247 static int match_pattern_char __P((char *, char *));
248 static int match_upattern __P((char *, char *, int, char **, char **));
249 #if defined (HANDLE_MULTIBYTE)
250 static int match_pattern_wchar __P((wchar_t *, wchar_t *));
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 *, char *, 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));
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 *, int, SHELL_VAR **, char **));
287 static char *mb_substring __P((char *, int, int));
288 static char *parameter_brace_substring __P((char *, char *, char *, int));
290 static char *pos_params_pat_subst __P((char *, char *, char *, int));
292 static char *parameter_brace_patsub __P((char *, char *, char *, int));
294 static char *pos_params_casemod __P((char *, char *, int, int));
295 static char *parameter_brace_casemod __P((char *, char *, int, char *, int));
297 static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int, int *, int *));
298 static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
300 static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
302 static WORD_LIST *word_list_split __P((WORD_LIST *));
304 static void exp_jump_to_top_level __P((int));
306 static WORD_LIST *separate_out_assignments __P((WORD_LIST *));
307 static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int));
308 #ifdef BRACE_EXPANSION
309 static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int));
311 #if defined (ARRAY_VARS)
312 static int make_internal_declare __P((char *, char *));
314 static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
315 static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
317 /* **************************************************************** */
319 /* Utility Functions */
321 /* **************************************************************** */
325 dump_word_flags (flags)
331 fprintf (stderr, "%d -> ", f);
332 if (f & W_ASSIGNASSOC)
335 fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : "");
340 fprintf (stderr, "W_HASCTLESC%s", f ? "|" : "");
345 fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : "");
350 fprintf (stderr, "W_DQUOTE%s", f ? "|" : "");
352 if (f & W_HASQUOTEDNULL)
354 f &= ~W_HASQUOTEDNULL;
355 fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : "");
360 fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : "");
365 fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : "");
367 if (f & W_COMPASSIGN)
370 fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : "");
375 fprintf (stderr, "W_NOEXPAND%s", f ? "|" : "");
380 fprintf (stderr, "W_ITILDE%s", f ? "|" : "");
385 fprintf (stderr, "W_NOTILDE%s", f ? "|" : "");
390 fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : "");
395 fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : "");
397 if (f & W_DOLLARSTAR)
400 fprintf (stderr, "W_DOLLARSTAR%s", f ? "|" : "");
405 fprintf (stderr, "W_DOLLARAT%s", f ? "|" : "");
410 fprintf (stderr, "W_TILDEEXP%s", f ? "|" : "");
415 fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : "");
420 fprintf (stderr, "W_NOGLOB%s", f ? "|" : "");
425 fprintf (stderr, "W_NOSPLIT%s", f ? "|" : "");
430 fprintf (stderr, "W_GLOBEXP%s", f ? "|" : "");
432 if (f & W_ASSIGNMENT)
435 fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : "");
440 fprintf (stderr, "W_QUOTED%s", f ? "|" : "");
445 fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : "");
447 fprintf (stderr, "\n");
452 #ifdef INCLUDE_UNUSED
454 quoted_substring (string, start, end)
459 register char *result, *s, *r;
463 /* Move to string[start], skipping quoted characters. */
464 for (s = string, l = 0; *s && l < start; )
476 r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */
478 /* Copy LEN characters, including quote characters. */
480 for (l = 0; l < len; s++)
494 #ifdef INCLUDE_UNUSED
495 /* Return the length of S, skipping over quoted characters */
519 /* Find the first occurrence of character C in string S, obeying shell
520 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
521 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
522 escaped with CTLESC are skipped. */
524 quoted_strchr (s, c, flags)
532 if (((flags & ST_BACKSL) && *p == '\\')
533 || ((flags & ST_CTLESC) && *p == CTLESC))
537 return ((char *)NULL);
543 return ((char *)NULL);
546 /* Return 1 if CHARACTER appears in an unquoted portion of
547 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
549 unquoted_member (character, string)
557 slen = strlen (string);
559 while (c = string[sindex])
567 ADVANCE_CHAR (string, slen, sindex);
573 ADVANCE_CHAR (string, slen, sindex);
577 sindex = skip_single_quoted (string, slen, ++sindex);
581 sindex = skip_double_quoted (string, slen, ++sindex);
588 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
590 unquoted_substring (substr, string)
591 char *substr, *string;
594 int sindex, c, sublen;
597 if (substr == 0 || *substr == '\0')
600 slen = strlen (string);
601 sublen = strlen (substr);
602 for (sindex = 0; c = string[sindex]; )
604 if (STREQN (string + sindex, substr, sublen))
613 ADVANCE_CHAR (string, slen, sindex);
617 sindex = skip_single_quoted (string, slen, ++sindex);
621 sindex = skip_double_quoted (string, slen, ++sindex);
625 ADVANCE_CHAR (string, slen, sindex);
632 /* Most of the substitutions must be done in parallel. In order
633 to avoid using tons of unclear goto's, I have some functions
634 for manipulating malloc'ed strings. They all take INDX, a
635 pointer to an integer which is the offset into the string
636 where manipulation is taking place. They also take SIZE, a
637 pointer to an integer which is the current length of the
638 character array for this string. */
640 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
641 of space allocated to TARGET. SOURCE can be NULL, in which
642 case nothing happens. Gets rid of SOURCE by freeing it.
643 Returns TARGET in case the location has changed. */
645 sub_append_string (source, target, indx, size)
646 char *source, *target;
653 srclen = STRLEN (source);
654 if (srclen >= (int)(*size - *indx))
657 n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
658 target = (char *)xrealloc (target, (*size = n));
661 FASTCOPY (source, target + *indx, srclen);
663 target[*indx] = '\0';
672 /* Append the textual representation of NUMBER to TARGET.
673 INDX and SIZE are as in SUB_APPEND_STRING. */
675 sub_append_number (number, target, indx, size)
682 temp = itos (number);
683 return (sub_append_string (temp, target, indx, size));
687 /* Extract a substring from STRING, starting at SINDEX and ending with
688 one of the characters in CHARLIST. Don't make the ending character
689 part of the string. Leave SINDEX pointing at the ending character.
690 Understand about backslashes in the string. If (flags & SX_VARNAME)
691 is non-zero, and array variables have been compiled into the shell,
692 everything between a `[' and a corresponding `]' is skipped over.
693 If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
694 update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
695 contain a closing character from CHARLIST. */
697 string_extract (string, sindex, charlist, flags)
709 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
712 while (c = string[i])
721 #if defined (ARRAY_VARS)
722 else if ((flags & SX_VARNAME) && c == '[')
725 /* If this is an array subscript, skip over it and continue. */
726 ni = skipsubscript (string, i, 0);
727 if (string[ni] == ']')
731 else if (MEMBER (c, charlist))
737 ADVANCE_CHAR (string, slen, i);
740 /* If we had to have a matching delimiter and didn't find one, return an
741 error and let the caller deal with it. */
742 if ((flags & SX_REQMATCH) && found == 0)
745 return (&extract_string_error);
748 temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
754 /* Extract the contents of STRING as if it is enclosed in double quotes.
755 SINDEX, when passed in, is the offset of the character immediately
756 following the opening double quote; on exit, SINDEX is left pointing after
757 the closing double quote. If STRIPDQ is non-zero, unquoted double
758 quotes are stripped and the string is terminated by a null byte.
759 Backslashes between the embedded double quotes are processed. If STRIPDQ
760 is zero, an unquoted `"' terminates the string. */
762 string_extract_double_quoted (string, sindex, stripdq)
764 int *sindex, stripdq;
770 char *temp, *ret; /* The new string we return. */
771 int pass_next, backquote, si; /* State variables for the machine. */
775 slen = strlen (string + *sindex) + *sindex;
776 send = string + slen;
778 pass_next = backquote = dquote = 0;
779 temp = (char *)xmalloc (1 + slen - *sindex);
783 while (c = string[i])
785 /* Process a character that was quoted by a backslash. */
790 ``The backslash shall retain its special meaning as an escape
791 character only when followed by one of the characters:
794 If STRIPDQ is zero, we handle the double quotes here and let
795 expand_word_internal handle the rest. If STRIPDQ is non-zero,
796 we have already been through one round of backslash stripping,
797 and want to strip these backslashes only if DQUOTE is non-zero,
798 indicating that we are inside an embedded double-quoted string. */
800 /* If we are in an embedded quoted string, then don't strip
801 backslashes before characters for which the backslash
802 retains its special meaning, but remove backslashes in
803 front of other characters. If we are not in an
804 embedded quoted string, don't strip backslashes at all.
805 This mess is necessary because the string was already
806 surrounded by double quotes (and sh has some really weird
808 The returned string will be run through expansion as if
809 it were double-quoted. */
810 if ((stripdq == 0 && c != '"') ||
811 (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
816 COPY_CHAR_I (temp, j, string, send, i);
820 /* A backslash protects the next character. The code just above
821 handles preserving the backslash in front of any character but
830 /* Inside backquotes, ``the portion of the quoted string from the
831 initial backquote and the characters up to the next backquote
832 that is not preceded by a backslash, having escape characters
833 removed, defines that command''. */
851 /* Pass everything between `$(' and the matching `)' or a quoted
852 ${ ... } pair through according to the Posix.2 specification. */
853 if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
858 if (string[i + 1] == LPAREN)
859 ret = extract_command_subst (string, &si, 0);
861 ret = extract_dollar_brace_string (string, &si, 1, 0);
864 temp[j++] = string[i + 1];
866 /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
868 if (ret == 0 && no_longjmp_on_fatal_error)
871 ret = string + i + 2;
874 for (t = 0; ret[t]; t++, j++)
876 temp[j] = string[si];
891 /* Add any character but a double quote to the quoted string we're
894 goto add_one_character;
908 /* Point to after the closing quote. */
916 /* This should really be another option to string_extract_double_quoted. */
918 skip_double_quoted (string, slen, sind)
925 int pass_next, backquote, si;
928 pass_next = backquote = 0;
930 while (c = string[i])
935 ADVANCE_CHAR (string, slen, i);
948 ADVANCE_CHAR (string, slen, i);
957 else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
960 if (string[i + 1] == LPAREN)
961 ret = extract_command_subst (string, &si, SX_NOALLOC);
963 ret = extract_dollar_brace_string (string, &si, 1, SX_NOALLOC);
970 ADVANCE_CHAR (string, slen, i);
983 /* Extract the contents of STRING as if it is enclosed in single quotes.
984 SINDEX, when passed in, is the offset of the character immediately
985 following the opening single quote; on exit, SINDEX is left pointing after
986 the closing single quote. */
988 string_extract_single_quoted (string, sindex)
997 /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
998 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
1000 while (string[i] && string[i] != '\'')
1001 ADVANCE_CHAR (string, slen, i);
1003 t = substring (string, *sindex, i);
1013 skip_single_quoted (string, slen, sind)
1022 while (string[c] && string[c] != '\'')
1023 ADVANCE_CHAR (string, slen, c);
1030 /* Just like string_extract, but doesn't hack backslashes or any of
1031 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
1033 string_extract_verbatim (string, slen, sindex, charlist, flags)
1041 #if defined (HANDLE_MULTIBYTE)
1049 if (charlist[0] == '\'' && charlist[1] == '\0')
1051 temp = string_extract_single_quoted (string, sindex);
1052 --*sindex; /* leave *sindex at separator character */
1058 /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need
1059 this only if MB_CUR_MAX > 1. */
1060 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 1;
1062 #if defined (HANDLE_MULTIBYTE)
1063 clen = strlen (charlist);
1066 while (c = string[i])
1068 #if defined (HANDLE_MULTIBYTE)
1071 if ((flags & SX_NOCTLESC) == 0 && c == CTLESC)
1076 /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
1077 through, to protect the CTLNULs from later calls to
1078 remove_quoted_nulls. */
1079 else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC && string[i+1] == CTLNUL)
1085 #if defined (HANDLE_MULTIBYTE)
1086 mblength = MBLEN (string + i, slen - i);
1090 mblength = mbtowc (&wc, string + i, slen - i);
1091 if (MB_INVALIDCH (mblength))
1093 if (MEMBER (c, charlist))
1101 len = mbstowcs (wcharlist, charlist, 0);
1104 wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1));
1105 mbstowcs (wcharlist, charlist, len + 1);
1108 if (wcschr (wcharlist, wc))
1114 if (MEMBER (c, charlist))
1117 ADVANCE_CHAR (string, slen, i);
1120 #if defined (HANDLE_MULTIBYTE)
1124 temp = substring (string, *sindex, i);
1130 /* Extract the $( construct in STRING, and return a new string.
1131 Start extracting at (SINDEX) as if we had just seen "$(".
1132 Make (SINDEX) get the position of the matching ")". )
1133 XFLAGS is additional flags to pass to other extraction functions. */
1135 extract_command_subst (string, sindex, xflags)
1140 if (string[*sindex] == LPAREN)
1141 return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/
1144 xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
1145 return (xparse_dolparen (string, string+*sindex, sindex, xflags));
1149 /* Extract the $[ construct in STRING, and return a new string. (])
1150 Start extracting at (SINDEX) as if we had just seen "$[".
1151 Make (SINDEX) get the position of the matching "]". */
1153 extract_arithmetic_subst (string, sindex)
1157 return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/
1160 #if defined (PROCESS_SUBSTITUTION)
1161 /* Extract the <( or >( construct in STRING, and return a new string.
1162 Start extracting at (SINDEX) as if we had just seen "<(".
1163 Make (SINDEX) get the position of the matching ")". */ /*))*/
1165 extract_process_subst (string, starter, sindex)
1170 return (extract_delimited_string (string, sindex, starter, "(", ")", 0));
1172 #endif /* PROCESS_SUBSTITUTION */
1174 #if defined (ARRAY_VARS)
1175 /* This can be fooled by unquoted right parens in the passed string. If
1176 each caller verifies that the last character in STRING is a right paren,
1177 we don't even need to call extract_delimited_string. */
1179 extract_array_assignment_list (string, sindex)
1186 slen = strlen (string); /* ( */
1187 if (string[slen - 1] == ')')
1189 ret = substring (string, *sindex, slen - 1);
1197 /* Extract and create a new string from the contents of STRING, a
1198 character string delimited with OPENER and CLOSER. SINDEX is
1199 the address of an int describing the current offset in STRING;
1200 it should point to just after the first OPENER found. On exit,
1201 SINDEX gets the position of the last character of the matching CLOSER.
1202 If OPENER is more than a single character, ALT_OPENER, if non-null,
1203 contains a character string that can also match CLOSER and thus
1204 needs to be skipped. */
1206 extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
1209 char *opener, *alt_opener, *closer;
1215 int pass_character, nesting_level, in_comment;
1216 int len_closer, len_opener, len_alt_opener;
1219 slen = strlen (string + *sindex) + *sindex;
1220 len_opener = STRLEN (opener);
1221 len_alt_opener = STRLEN (alt_opener);
1222 len_closer = STRLEN (closer);
1224 pass_character = in_comment = 0;
1229 while (nesting_level)
1240 ADVANCE_CHAR (string, slen, i);
1244 if (pass_character) /* previous char was backslash */
1247 ADVANCE_CHAR (string, slen, i);
1251 /* Not exactly right yet; should handle shell metacharacters and
1252 multibyte characters, too. See COMMENT_BEGIN define in parse.y */
1253 if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1])))
1256 ADVANCE_CHAR (string, slen, i);
1260 if (c == CTLESC || c == '\\')
1268 /* Process a nested command substitution, but only if we're parsing a
1269 command substitution. XXX - for bash-4.2 */
1270 if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN)
1273 t = extract_command_subst (string, &si, flags);
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;
1379 slen = strlen (string + *sindex) + *sindex;
1382 while (c = string[i])
1387 ADVANCE_CHAR (string, slen, i);
1391 /* CTLESCs and backslashes quote the next character. */
1392 if (c == CTLESC || c == '\\')
1399 if (string[i] == '$' && string[i+1] == LBRACE)
1409 if (nesting_level == 0)
1415 /* Pass the contents of old-style command substitutions through
1420 t = string_extract (string, &si, "`", flags|SX_NOALLOC);
1425 /* Pass the contents of new-style command substitutions and
1426 arithmetic substitutions through verbatim. */
1427 if (string[i] == '$' && string[i+1] == LPAREN)
1430 t = extract_command_subst (string, &si, flags|SX_NOALLOC);
1435 /* Pass the contents of single-quoted and double-quoted strings
1436 through verbatim. */
1437 if (c == '\'' || c == '"')
1440 i = (c == '\'') ? skip_single_quoted (string, slen, si)
1441 : skip_double_quoted (string, slen, si);
1442 /* skip_XXX_quoted leaves index one past close quote */
1446 /* move past this character, which was not special. */
1447 ADVANCE_CHAR (string, slen, i);
1450 if (c == 0 && nesting_level)
1452 if (no_longjmp_on_fatal_error == 0)
1454 report_error (_("bad substitution: no closing `%s' in %s"), "}", string);
1455 last_command_exit_value = EXECUTION_FAILURE;
1456 exp_jump_to_top_level (DISCARD);
1461 return ((char *)NULL);
1465 result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
1471 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1472 STRING, and returns a pointer to it. */
1474 de_backslash (string)
1477 register size_t slen;
1478 register int i, j, prev_i;
1481 slen = strlen (string);
1484 /* Loop copying string[i] to string[j], i >= j. */
1487 if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
1488 string[i + 1] == '$'))
1491 ADVANCE_CHAR (string, slen, i);
1493 do string[j++] = string[prev_i++]; while (prev_i < i);
1504 /* Replace instances of \! in a string with !. */
1506 unquote_bang (string)
1510 register char *temp;
1512 temp = (char *)xmalloc (1 + strlen (string));
1514 for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
1516 if (string[i] == '\\' && string[i + 1] == '!')
1522 strcpy (string, temp);
1527 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0)
1529 /* This function assumes s[i] == open; returns with s[ret] == close; used to
1530 parse array subscripts. FLAGS & 1 means to not attempt to skip over
1531 matched pairs of quotes or backquotes, or skip word expansions; it is
1532 intended to be used after expansion has been performed and during final
1533 assignment parsing (see arrayfunc.c:assign_compound_array_list()). */
1535 skip_matched_pair (string, start, open, close, flags)
1537 int start, open, close, flags;
1539 int i, pass_next, backq, si, c, count;
1544 slen = strlen (string + start) + start;
1545 no_longjmp_on_fatal_error = 1;
1547 i = start + 1; /* skip over leading bracket */
1549 pass_next = backq = 0;
1550 ss = (char *)string;
1551 while (c = string[i])
1558 ADVANCE_CHAR (string, slen, i);
1571 ADVANCE_CHAR (string, slen, i);
1574 else if ((flags & 1) == 0 && c == '`')
1580 else if ((flags & 1) == 0 && c == open)
1586 else if (c == close)
1594 else if ((flags & 1) == 0 && (c == '\'' || c == '"'))
1596 i = (c == '\'') ? skip_single_quoted (ss, slen, ++i)
1597 : skip_double_quoted (ss, slen, ++i);
1598 /* no increment, the skip functions increment past the closing quote. */
1600 else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
1603 if (string[si] == '\0')
1606 if (string[i+1] == LPAREN)
1607 temp = extract_delimited_string (ss, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
1609 temp = extract_dollar_brace_string (ss, &si, 0, SX_NOALLOC);
1611 if (string[i] == '\0') /* don't increment i past EOS in loop */
1617 ADVANCE_CHAR (string, slen, i);
1623 #if defined (ARRAY_VARS)
1625 skipsubscript (string, start, flags)
1629 return (skip_matched_pair (string, start, '[', ']', flags));
1633 /* Skip characters in STRING until we find a character in DELIMS, and return
1634 the index of that character. START is the index into string at which we
1635 begin. This is similar in spirit to strpbrk, but it returns an index into
1636 STRING and takes a starting index. This little piece of code knows quite
1637 a lot of shell syntax. It's very similar to skip_double_quoted and other
1638 functions of that ilk. */
1640 skip_to_delim (string, start, delims, flags)
1646 int i, pass_next, backq, si, c, invert, skipquote, skipcmd;
1651 slen = strlen (string + start) + start;
1652 if (flags & SD_NOJMP)
1653 no_longjmp_on_fatal_error = 1;
1654 invert = (flags & SD_INVERT);
1655 skipcmd = (flags & SD_NOSKIPCMD) == 0;
1658 pass_next = backq = 0;
1659 while (c = string[i])
1661 /* If this is non-zero, we should not let quote characters be delimiters
1662 and the current character is a single or double quote. We should not
1663 test whether or not it's a delimiter until after we skip single- or
1664 double-quoted strings. */
1665 skipquote = ((flags & SD_NOQUOTEDELIM) && (c == '\'' || c =='"'));
1671 ADVANCE_CHAR (string, slen, i);
1684 ADVANCE_CHAR (string, slen, i);
1693 else if (skipquote == 0 && invert == 0 && member (c, delims))
1695 else if (c == '\'' || c == '"')
1697 i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
1698 : skip_double_quoted (string, slen, ++i);
1699 /* no increment, the skip functions increment past the closing quote. */
1701 else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE))
1704 if (string[si] == '\0')
1707 if (string[i+1] == LPAREN)
1708 temp = extract_delimited_string (string, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
1710 temp = extract_dollar_brace_string (string, &si, 0, SX_NOALLOC);
1712 if (string[i] == '\0') /* don't increment i past EOS in loop */
1717 #if defined (PROCESS_SUBSTITUTION)
1718 else if (skipcmd && (c == '<' || c == '>') && string[i+1] == LPAREN)
1721 if (string[si] == '\0')
1723 temp = extract_process_subst (string, (c == '<') ? "<(" : ">(", &si);
1725 if (string[i] == '\0')
1730 #endif /* PROCESS_SUBSTITUTION */
1731 else if ((skipquote || invert) && (member (c, delims) == 0))
1734 ADVANCE_CHAR (string, slen, i);
1740 #if defined (READLINE)
1741 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
1742 an unclosed quoted string), or if the character at EINDEX is quoted
1743 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
1744 single and double-quoted string parsing functions should not return an
1745 error if there are unclosed quotes or braces. The characters that this
1746 recognizes need to be the same as the contents of
1747 rl_completer_quote_characters. */
1750 char_is_quoted (string, eindex)
1754 int i, pass_next, c;
1758 slen = strlen (string);
1759 no_longjmp_on_fatal_error = 1;
1768 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
1770 ADVANCE_CHAR (string, slen, i);
1779 else if (c == '\'' || c == '"')
1781 i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
1782 : skip_double_quoted (string, slen, ++i);
1785 /* no increment, the skip_xxx functions go one past end */
1788 ADVANCE_CHAR (string, slen, i);
1795 unclosed_pair (string, eindex, openstr)
1800 int i, pass_next, openc, olen;
1804 slen = strlen (string);
1805 olen = strlen (openstr);
1806 i = pass_next = openc = 0;
1812 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
1814 ADVANCE_CHAR (string, slen, i);
1817 else if (string[i] == '\\')
1823 else if (STREQN (string + i, openstr, olen))
1828 else if (string[i] == '\'' || string[i] == '"')
1830 i = (string[i] == '\'') ? skip_single_quoted (string, slen, i)
1831 : skip_double_quoted (string, slen, i);
1836 ADVANCE_CHAR (string, slen, i);
1841 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
1842 individual words. If DELIMS is NULL, the current value of $IFS is used
1843 to split the string, and the function follows the shell field splitting
1844 rules. SENTINEL is an index to look for. NWP, if non-NULL,
1845 gets the number of words in the returned list. CWP, if non-NULL, gets
1846 the index of the word containing SENTINEL. Non-whitespace chars in
1847 DELIMS delimit separate fields. */
1849 split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp)
1853 int sentinel, flags;
1856 int ts, te, i, nw, cw, ifs_split, dflags;
1857 char *token, *d, *d2;
1858 WORD_LIST *ret, *tl;
1860 if (string == 0 || *string == '\0')
1866 return ((WORD_LIST *)NULL);
1869 d = (delims == 0) ? ifs_value : delims;
1870 ifs_split = delims == 0;
1872 /* Make d2 the non-whitespace characters in delims */
1877 #if defined (HANDLE_MULTIBYTE)
1878 size_t mblength = 1;
1882 slength = strlen (delims);
1883 d2 = (char *)xmalloc (slength + 1);
1887 #if defined (HANDLE_MULTIBYTE)
1888 mbstate_t state_bak;
1890 mblength = MBRLEN (delims + i, slength, &state);
1891 if (MB_INVALIDCH (mblength))
1893 else if (mblength > 1)
1895 memcpy (d2 + ts, delims + i, mblength);
1898 slength -= mblength;
1902 if (whitespace (delims[i]) == 0)
1903 d2[ts++] = delims[i];
1911 ret = (WORD_LIST *)NULL;
1913 /* Remove sequences of whitespace characters at the start of the string, as
1914 long as those characters are delimiters. */
1915 for (i = 0; member (string[i], d) && spctabnl (string[i]); i++)
1917 if (string[i] == '\0')
1923 dflags = flags|SD_NOJMP;
1926 te = skip_to_delim (string, ts, d, dflags);
1928 /* If we have a non-whitespace delimiter character, use it to make a
1929 separate field. This is just about what $IFS splitting does and
1930 is closer to the behavior of the shell parser. */
1931 if (ts == te && d2 && member (string[ts], d2))
1934 /* If we're using IFS splitting, the non-whitespace delimiter char
1935 and any additional IFS whitespace delimits a field. */
1937 while (member (string[te], d) && spctabnl (string[te]))
1940 while (member (string[te], d2))
1944 token = substring (string, ts, te);
1946 ret = add_string_to_list (token, ret);
1950 if (sentinel >= ts && sentinel <= te)
1953 /* If the cursor is at whitespace just before word start, set the
1954 sentinel word to the current word. */
1955 if (cwp && cw == -1 && sentinel == ts-1)
1958 /* If the cursor is at whitespace between two words, make a new, empty
1959 word, add it before (well, after, since the list is in reverse order)
1960 the word we just added, and set the current word to that one. */
1961 if (cwp && cw == -1 && sentinel < ts)
1963 tl = make_word_list (make_word (""), ret->next);
1969 if (string[te] == 0)
1973 while (member (string[i], d) && (ifs_split || spctabnl(string[i])))
1982 /* Special case for SENTINEL at the end of STRING. If we haven't found
1983 the word containing SENTINEL yet, and the index we're looking for is at
1984 the end of STRING (or past the end of the previously-found token,
1985 possible if the end of the line is composed solely of IFS whitespace)
1986 add an additional null argument and set the current word pointer to that. */
1987 if (cwp && cw == -1 && (sentinel >= slen || sentinel >= te))
1989 if (whitespace (string[sentinel - 1]))
1992 ret = add_string_to_list (token, ret);
2003 return (REVERSE_LIST (ret, WORD_LIST *));
2005 #endif /* READLINE */
2009 /* Extract the name of the variable to bind to from the assignment string. */
2011 assignment_name (string)
2017 offset = assignment (string, 0);
2019 return (char *)NULL;
2020 temp = substring (string, 0, offset);
2025 /* **************************************************************** */
2027 /* Functions to convert strings to WORD_LISTs and vice versa */
2029 /* **************************************************************** */
2031 /* Return a single string of all the words in LIST. SEP is the separator
2032 to put between individual elements of LIST in the output string. */
2034 string_list_internal (list, sep)
2038 register WORD_LIST *t;
2040 int word_len, sep_len, result_size;
2043 return ((char *)NULL);
2045 /* Short-circuit quickly if we don't need to separate anything. */
2046 if (list->next == 0)
2047 return (savestring (list->word->word));
2049 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
2050 sep_len = STRLEN (sep);
2053 for (t = list; t; t = t->next)
2056 result_size += sep_len;
2057 result_size += strlen (t->word->word);
2060 r = result = (char *)xmalloc (result_size + 1);
2062 for (t = list; t; t = t->next)
2064 if (t != list && sep_len)
2068 FASTCOPY (sep, r, sep_len);
2075 word_len = strlen (t->word->word);
2076 FASTCOPY (t->word->word, r, word_len);
2084 /* Return a single string of all the words present in LIST, separating
2085 each word with a space. */
2090 return (string_list_internal (list, " "));
2093 /* An external interface that can be used by the rest of the shell to
2094 obtain a string containing the first character in $IFS. Handles all
2095 the multibyte complications. If LENP is non-null, it is set to the
2096 length of the returned string. */
2098 ifs_firstchar (lenp)
2104 ret = xmalloc (MB_LEN_MAX + 1);
2105 #if defined (HANDLE_MULTIBYTE)
2106 if (ifs_firstc_len == 1)
2108 ret[0] = ifs_firstc[0];
2110 len = ret[0] ? 1 : 0;
2114 memcpy (ret, ifs_firstc, ifs_firstc_len);
2115 ret[len = ifs_firstc_len] = '\0';
2118 ret[0] = ifs_firstc;
2120 len = ret[0] ? 0 : 1;
2129 /* Return a single string of all the words present in LIST, obeying the
2130 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
2131 expansion [of $*] appears within a double quoted string, it expands
2132 to a single field with the value of each parameter separated by the
2133 first character of the IFS variable, or by a <space> if IFS is unset." */
2135 string_list_dollar_star (list)
2139 #if defined (HANDLE_MULTIBYTE)
2140 # if defined (__GNUC__)
2141 char sep[MB_CUR_MAX + 1];
2149 #if defined (HANDLE_MULTIBYTE)
2150 # if !defined (__GNUC__)
2151 sep = (char *)xmalloc (MB_CUR_MAX + 1);
2152 # endif /* !__GNUC__ */
2153 if (ifs_firstc_len == 1)
2155 sep[0] = ifs_firstc[0];
2160 memcpy (sep, ifs_firstc, ifs_firstc_len);
2161 sep[ifs_firstc_len] = '\0';
2164 sep[0] = ifs_firstc;
2168 ret = string_list_internal (list, sep);
2169 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2175 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
2176 is non-zero, the $@ appears within double quotes, and we should quote
2177 the list before converting it into a string. If IFS is unset, and the
2178 word is not quoted, we just need to quote CTLESC and CTLNUL characters
2179 in the words in the list, because the default value of $IFS is
2180 <space><tab><newline>, IFS characters in the words in the list should
2181 also be split. If IFS is null, and the word is not quoted, we need
2182 to quote the words in the list to preserve the positional parameters
2185 string_list_dollar_at (list, quoted)
2190 #if defined (HANDLE_MULTIBYTE)
2191 # if defined (__GNUC__)
2192 char sep[MB_CUR_MAX + 1];
2195 # endif /* !__GNUC__ */
2201 /* XXX this could just be ifs = ifs_value; */
2202 ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
2204 #if defined (HANDLE_MULTIBYTE)
2205 # if !defined (__GNUC__)
2206 sep = (char *)xmalloc (MB_CUR_MAX + 1);
2207 # endif /* !__GNUC__ */
2210 if (ifs_firstc_len == 1)
2212 sep[0] = ifs_firstc[0];
2217 memcpy (sep, ifs_firstc, ifs_firstc_len);
2218 sep[ifs_firstc_len] = '\0';
2227 sep[0] = (ifs == 0 || *ifs == 0) ? ' ' : *ifs;
2231 /* XXX -- why call quote_list if ifs == 0? we can get away without doing
2232 it now that quote_escapes quotes spaces */
2234 tlist = ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (ifs && *ifs == 0))
2236 tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
2239 : list_quote_escapes (list);
2241 ret = string_list_internal (tlist, sep);
2242 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2248 /* Turn the positional paramters into a string, understanding quoting and
2249 the various subtleties of using the first character of $IFS as the
2250 separator. Calls string_list_dollar_at, string_list_dollar_star, and
2251 string_list as appropriate. */
2253 string_list_pos_params (pchar, list, quoted)
2261 if (pchar == '*' && (quoted & Q_DOUBLE_QUOTES))
2263 tlist = quote_list (list);
2264 word_list_remove_quoted_nulls (tlist);
2265 ret = string_list_dollar_star (tlist);
2267 else if (pchar == '*' && (quoted & Q_HERE_DOCUMENT))
2269 tlist = quote_list (list);
2270 word_list_remove_quoted_nulls (tlist);
2271 ret = string_list (tlist);
2273 else if (pchar == '*')
2275 /* Even when unquoted, string_list_dollar_star does the right thing
2276 making sure that the first character of $IFS is used as the
2278 ret = string_list_dollar_star (list);
2280 else if (pchar == '@' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
2281 /* We use string_list_dollar_at, but only if the string is quoted, since
2282 that quotes the escapes if it's not, which we don't want. We could
2283 use string_list (the old code did), but that doesn't do the right
2284 thing if the first character of $IFS is not a space. We use
2285 string_list_dollar_star if the string is unquoted so we make sure that
2286 the elements of $@ are separated by the first character of $IFS for
2288 ret = string_list_dollar_at (list, quoted);
2289 else if (pchar == '@')
2290 ret = string_list_dollar_star (list);
2292 ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list);
2297 /* Return the list of words present in STRING. Separate the string into
2298 words at any of the characters found in SEPARATORS. If QUOTED is
2299 non-zero then word in the list will have its quoted flag set, otherwise
2300 the quoted flag is left as make_word () deemed fit.
2302 This obeys the P1003.2 word splitting semantics. If `separators' is
2303 exactly <space><tab><newline>, then the splitting algorithm is that of
2304 the Bourne shell, which treats any sequence of characters from `separators'
2305 as a delimiter. If IFS is unset, which results in `separators' being set
2306 to "", no splitting occurs. If separators has some other value, the
2307 following rules are applied (`IFS white space' means zero or more
2308 occurrences of <space>, <tab>, or <newline>, as long as those characters
2309 are in `separators'):
2311 1) IFS white space is ignored at the start and the end of the
2313 2) Each occurrence of a character in `separators' that is not
2314 IFS white space, along with any adjacent occurrences of
2315 IFS white space delimits a field.
2316 3) Any nonzero-length sequence of IFS white space delimits a field.
2319 /* BEWARE! list_string strips null arguments. Don't call it twice and
2320 expect to have "" preserved! */
2322 /* This performs word splitting and quoted null character removal on
2325 (((separators)[0]) ? ((separators)[1] ? isifs(c) \
2326 : (c) == (separators)[0]) \
2330 list_string (string, separators, quoted)
2331 register char *string, *separators;
2336 char *current_word, *s;
2337 int sindex, sh_style_split, whitesep, xflags;
2340 if (!string || !*string)
2341 return ((WORD_LIST *)NULL);
2343 sh_style_split = separators && separators[0] == ' ' &&
2344 separators[1] == '\t' &&
2345 separators[2] == '\n' &&
2346 separators[3] == '\0';
2347 for (xflags = 0, s = ifs_value; s && *s; s++)
2349 if (*s == CTLESC) xflags |= SX_NOCTLESC;
2350 else if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
2354 /* Remove sequences of whitespace at the beginning of STRING, as
2355 long as those characters appear in IFS. Do not do this if
2356 STRING is quoted or if there are no separator characters. */
2357 if (!quoted || !separators || !*separators)
2359 for (s = string; *s && spctabnl (*s) && issep (*s); s++);
2362 return ((WORD_LIST *)NULL);
2367 /* OK, now STRING points to a word that does not begin with white space.
2368 The splitting algorithm is:
2369 extract a word, stopping at a separator
2370 skip sequences of spc, tab, or nl as long as they are separators
2371 This obeys the field splitting rules in Posix.2. */
2372 slen = (MB_CUR_MAX > 1) ? strlen (string) : 1;
2373 for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
2375 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2376 unless multibyte chars are possible. */
2377 current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags);
2378 if (current_word == 0)
2381 /* If we have a quoted empty string, add a quoted null argument. We
2382 want to preserve the quoted null character iff this is a quoted
2383 empty string; otherwise the quoted null characters are removed
2385 if (QUOTED_NULL (current_word))
2387 t = alloc_word_desc ();
2388 t->word = make_quoted_char ('\0');
2389 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
2390 result = make_word_list (t, result);
2392 else if (current_word[0] != '\0')
2394 /* If we have something, then add it regardless. However,
2395 perform quoted null character removal on the current word. */
2396 remove_quoted_nulls (current_word);
2397 result = add_string_to_list (current_word, result);
2398 result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */
2399 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
2400 result->word->flags |= W_QUOTED;
2403 /* If we're not doing sequences of separators in the traditional
2404 Bourne shell style, then add a quoted null argument. */
2405 else if (!sh_style_split && !spctabnl (string[sindex]))
2407 t = alloc_word_desc ();
2408 t->word = make_quoted_char ('\0');
2409 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
2410 result = make_word_list (t, result);
2413 free (current_word);
2415 /* Note whether or not the separator is IFS whitespace, used later. */
2416 whitesep = string[sindex] && spctabnl (string[sindex]);
2418 /* Move past the current separator character. */
2422 ADVANCE_CHAR (string, slen, sindex);
2425 /* Now skip sequences of space, tab, or newline characters if they are
2426 in the list of separators. */
2427 while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex]))
2430 /* If the first separator was IFS whitespace and the current character
2431 is a non-whitespace IFS character, it should be part of the current
2432 field delimiter, not a separate delimiter that would result in an
2433 empty field. Look at POSIX.2, 3.6.5, (3)(b). */
2434 if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex]))
2437 /* An IFS character that is not IFS white space, along with any
2438 adjacent IFS white space, shall delimit a field. (SUSv3) */
2439 while (string[sindex] && spctabnl (string[sindex]) && isifs (string[sindex]))
2443 return (REVERSE_LIST (result, WORD_LIST *));
2446 /* Parse a single word from STRING, using SEPARATORS to separate fields.
2447 ENDPTR is set to the first character after the word. This is used by
2448 the `read' builtin. This is never called with SEPARATORS != $IFS;
2449 it should be simplified.
2451 XXX - this function is very similar to list_string; they should be
2454 get_word_from_string (stringp, separators, endptr)
2455 char **stringp, *separators, **endptr;
2459 int sindex, sh_style_split, whitesep, xflags;
2462 if (!stringp || !*stringp || !**stringp)
2463 return ((char *)NULL);
2465 sh_style_split = separators && separators[0] == ' ' &&
2466 separators[1] == '\t' &&
2467 separators[2] == '\n' &&
2468 separators[3] == '\0';
2469 for (xflags = 0, s = ifs_value; s && *s; s++)
2471 if (*s == CTLESC) xflags |= SX_NOCTLESC;
2472 if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
2478 /* Remove sequences of whitespace at the beginning of STRING, as
2479 long as those characters appear in IFS. */
2480 if (sh_style_split || !separators || !*separators)
2482 for (; *s && spctabnl (*s) && isifs (*s); s++);
2484 /* If the string is nothing but whitespace, update it and return. */
2490 return ((char *)NULL);
2494 /* OK, S points to a word that does not begin with white space.
2495 Now extract a word, stopping at a separator, save a pointer to
2496 the first character after the word, then skip sequences of spc,
2497 tab, or nl as long as they are separators.
2499 This obeys the field splitting rules in Posix.2. */
2501 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2502 unless multibyte chars are possible. */
2503 slen = (MB_CUR_MAX > 1) ? strlen (s) : 1;
2504 current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags);
2506 /* Set ENDPTR to the first character after the end of the word. */
2508 *endptr = s + sindex;
2510 /* Note whether or not the separator is IFS whitespace, used later. */
2511 whitesep = s[sindex] && spctabnl (s[sindex]);
2513 /* Move past the current separator character. */
2517 ADVANCE_CHAR (s, slen, sindex);
2520 /* Now skip sequences of space, tab, or newline characters if they are
2521 in the list of separators. */
2522 while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
2525 /* If the first separator was IFS whitespace and the current character is
2526 a non-whitespace IFS character, it should be part of the current field
2527 delimiter, not a separate delimiter that would result in an empty field.
2528 Look at POSIX.2, 3.6.5, (3)(b). */
2529 if (s[sindex] && whitesep && isifs (s[sindex]) && !spctabnl (s[sindex]))
2532 /* An IFS character that is not IFS white space, along with any adjacent
2533 IFS white space, shall delimit a field. */
2534 while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
2538 /* Update STRING to point to the next field. */
2539 *stringp = s + sindex;
2540 return (current_word);
2543 /* Remove IFS white space at the end of STRING. Start at the end
2544 of the string and walk backwards until the beginning of the string
2545 or we find a character that's not IFS white space and not CTLESC.
2546 Only let CTLESC escape a white space character if SAW_ESCAPE is
2549 strip_trailing_ifs_whitespace (string, separators, saw_escape)
2550 char *string, *separators;
2555 s = string + STRLEN (string) - 1;
2556 while (s > string && ((spctabnl (*s) && isifs (*s)) ||
2557 (saw_escape && *s == CTLESC && spctabnl (s[1]))))
2565 /* Split STRING into words at whitespace. Obeys shell-style quoting with
2566 backslashes, single and double quotes. */
2568 list_string_with_quotes (string)
2574 int c, i, tokstart, len;
2576 for (s = string; s && *s && spctabnl (*s); s++)
2578 if (s == 0 || *s == 0)
2579 return ((WORD_LIST *)NULL);
2583 list = (WORD_LIST *)NULL;
2594 i = skip_single_quoted (s, s_len, ++i);
2596 i = skip_double_quoted (s, s_len, ++i);
2597 else if (c == 0 || spctabnl (c))
2599 /* We have found the end of a token. Make a word out of it and
2600 add it to the word list. */
2601 token = substring (s, tokstart, i);
2602 list = add_string_to_list (token, list);
2604 while (spctabnl (s[i]))
2612 i++; /* normal character */
2614 return (REVERSE_LIST (list, WORD_LIST *));
2618 /********************************************************/
2620 /* Functions to perform assignment statements */
2622 /********************************************************/
2624 #if defined (ARRAY_VARS)
2626 do_compound_assignment (name, value, flags)
2631 int mklocal, mkassoc;
2634 mklocal = flags & ASS_MKLOCAL;
2635 mkassoc = flags & ASS_MKASSOC;
2637 if (mklocal && variable_context)
2639 v = find_variable (name);
2640 list = expand_compound_array_assignment (v, value, flags);
2642 v = make_local_assoc_variable (name);
2643 else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context)
2644 v = make_local_array_variable (name);
2645 assign_compound_array_list (v, list, flags);
2648 v = assign_array_from_string (name, value, flags);
2654 /* Given STRING, an assignment string, get the value of the right side
2655 of the `=', and bind it to the left side. If EXPAND is true, then
2656 perform parameter expansion, command substitution, and arithmetic
2657 expansion on the right-hand side. Perform tilde expansion in any
2658 case. Do not perform word splitting on the result of expansion. */
2660 do_assignment_internal (word, expand)
2661 const WORD_DESC *word;
2664 int offset, tlen, appendop, assign_list, aflags, retval;
2667 #if defined (ARRAY_VARS)
2673 if (word == 0 || word->word == 0)
2676 appendop = assign_list = aflags = 0;
2677 string = word->word;
2678 offset = assignment (string, 0);
2679 name = savestring (string);
2680 value = (char *)NULL;
2682 if (name[offset] == '=')
2686 if (name[offset - 1] == '+')
2689 name[offset - 1] = '\0';
2692 name[offset] = 0; /* might need this set later */
2693 temp = name + offset + 1;
2694 tlen = STRLEN (temp);
2696 #if defined (ARRAY_VARS)
2697 if (expand && (word->flags & W_COMPASSIGN))
2699 assign_list = ni = 1;
2700 value = extract_array_assignment_list (temp, &ni);
2704 if (expand && temp[0])
2705 value = expand_string_if_necessary (temp, 0, expand_string_assignment);
2707 value = savestring (temp);
2712 value = (char *)xmalloc (1);
2716 if (echo_command_at_execute)
2719 name[offset - 1] = '+';
2720 xtrace_print_assignment (name, value, assign_list, 1);
2722 name[offset - 1] = '\0';
2725 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
2728 aflags |= ASS_APPEND;
2730 #if defined (ARRAY_VARS)
2731 if (t = mbschr (name, '[')) /*]*/
2735 report_error (_("%s: cannot assign list to array member"), name);
2738 entry = assign_array_element (name, value, aflags);
2742 else if (assign_list)
2744 if (word->flags & W_ASSIGNARG)
2745 aflags |= ASS_MKLOCAL;
2746 if (word->flags & W_ASSIGNASSOC)
2747 aflags |= ASS_MKASSOC;
2748 entry = do_compound_assignment (name, value, aflags);
2751 #endif /* ARRAY_VARS */
2752 entry = bind_variable (name, value, aflags);
2754 stupidly_hack_special_variables (name);
2757 /* Return 1 if the assignment seems to have been performed correctly. */
2758 if (entry == 0 || readonly_p (entry))
2759 retval = 0; /* assignment failure */
2760 else if (noassign_p (entry))
2762 last_command_exit_value = EXECUTION_FAILURE;
2763 retval = 1; /* error status, but not assignment failure */
2768 if (entry && retval != 0 && noassign_p (entry) == 0)
2769 VUNSETATTR (entry, att_invisible);
2771 ASSIGN_RETURN (retval);
2774 VUNSETATTR (entry, att_invisible);
2776 ASSIGN_RETURN (entry ? ((readonly_p (entry) == 0) && noassign_p (entry) == 0) : 0);
2780 /* Perform the assignment statement in STRING, and expand the
2781 right side by doing tilde, command and parameter expansion. */
2783 do_assignment (string)
2788 td.flags = W_ASSIGNMENT;
2791 return do_assignment_internal (&td, 1);
2795 do_word_assignment (word)
2798 return do_assignment_internal (word, 1);
2801 /* Given STRING, an assignment string, get the value of the right side
2802 of the `=', and bind it to the left side. Do not perform any word
2803 expansions on the right hand side. */
2805 do_assignment_no_expand (string)
2810 td.flags = W_ASSIGNMENT;
2813 return (do_assignment_internal (&td, 0));
2816 /***************************************************
2818 * Functions to manage the positional parameters *
2820 ***************************************************/
2822 /* Return the word list that corresponds to `$*'. */
2824 list_rest_of_args ()
2826 register WORD_LIST *list, *args;
2829 /* Break out of the loop as soon as one of the dollar variables is null. */
2830 for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++)
2831 list = make_word_list (make_bare_word (dollar_vars[i]), list);
2833 for (args = rest_of_args; args; args = args->next)
2834 list = make_word_list (make_bare_word (args->word->word), list);
2836 return (REVERSE_LIST (list, WORD_LIST *));
2842 register WORD_LIST *list;
2845 for (n = 0; n < 9 && dollar_vars[n+1]; n++)
2847 for (list = rest_of_args; list; list = list->next)
2852 /* Return the value of a positional parameter. This handles values > 10. */
2854 get_dollar_var_value (ind)
2861 temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL;
2862 else /* We want something like ${11} */
2865 for (p = rest_of_args; p && ind--; p = p->next)
2867 temp = p ? savestring (p->word->word) : (char *)NULL;
2872 /* Make a single large string out of the dollar digit variables,
2873 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
2874 case of "$*" with respect to IFS. */
2876 string_rest_of_args (dollar_star)
2879 register WORD_LIST *list;
2882 list = list_rest_of_args ();
2883 string = dollar_star ? string_list_dollar_star (list) : string_list (list);
2884 dispose_words (list);
2888 /* Return a string containing the positional parameters from START to
2889 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
2890 which only makes a difference if QUOTED is non-zero. If QUOTED includes
2891 Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
2892 no quoting chars are added. */
2894 pos_params (string, start, end, quoted)
2896 int start, end, quoted;
2898 WORD_LIST *save, *params, *h, *t;
2902 /* see if we can short-circuit. if start == end, we want 0 parameters. */
2904 return ((char *)NULL);
2906 save = params = list_rest_of_args ();
2908 return ((char *)NULL);
2910 if (start == 0) /* handle ${@:0[:x]} specially */
2912 t = make_word_list (make_word (dollar_vars[0]), params);
2916 for (i = start ? 1 : 0; params && i < start; i++)
2917 params = params->next;
2919 return ((char *)NULL);
2920 for (h = t = params; params && i < end; i++)
2923 params = params->next;
2926 t->next = (WORD_LIST *)NULL;
2928 ret = string_list_pos_params (string[0], h, quoted);
2933 dispose_words (save);
2937 /******************************************************************/
2939 /* Functions to expand strings to strings or WORD_LISTs */
2941 /******************************************************************/
2943 #if defined (PROCESS_SUBSTITUTION)
2944 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
2946 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
2949 /* If there are any characters in STRING that require full expansion,
2950 then call FUNC to expand STRING; otherwise just perform quote
2951 removal if necessary. This returns a new string. */
2953 expand_string_if_necessary (string, quoted, func)
2964 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
2965 slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
2969 if (EXP_CHAR (string[i]))
2971 else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
2973 ADVANCE_CHAR (string, slen, i);
2978 list = (*func) (string, quoted);
2981 ret = string_list (list);
2982 dispose_words (list);
2987 else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
2988 ret = string_quote_removal (string, quoted);
2990 ret = savestring (string);
2995 static inline char *
2996 expand_string_to_string_internal (string, quoted, func)
3004 if (string == 0 || *string == '\0')
3005 return ((char *)NULL);
3007 list = (*func) (string, quoted);
3010 ret = string_list (list);
3011 dispose_words (list);
3020 expand_string_to_string (string, quoted)
3024 return (expand_string_to_string_internal (string, quoted, expand_string));
3028 expand_string_unsplit_to_string (string, quoted)
3032 return (expand_string_to_string_internal (string, quoted, expand_string_unsplit));
3036 expand_assignment_string_to_string (string, quoted)
3040 return (expand_string_to_string_internal (string, quoted, expand_string_assignment));
3044 expand_arith_string (string, quoted)
3048 return (expand_string_if_necessary (string, quoted, expand_string));
3051 #if defined (COND_COMMAND)
3052 /* Just remove backslashes in STRING. Returns a new string. */
3054 remove_backslashes (string)
3059 r = ret = (char *)xmalloc (strlen (string) + 1);
3060 for (s = string; s && *s; )
3072 /* This needs better error handling. */
3073 /* Expand W for use as an argument to a unary or binary operator in a
3074 [[...]] expression. If SPECIAL is 1, this is the rhs argument
3075 to the != or == operator, and should be treated as a pattern. In
3076 this case, we quote the string specially for the globbing code. If
3077 SPECIAL is 2, this is an rhs argument for the =~ operator, and should
3078 be quoted appropriately for regcomp/regexec. The caller is responsible
3079 for removing the backslashes if the unquoted word is needed later. */
3081 cond_expand_word (w, special)
3089 if (w->word == 0 || w->word[0] == '\0')
3090 return ((char *)NULL);
3092 w->flags |= W_NOSPLIT2;
3093 l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
3099 r = string_list (l);
3103 qflags = QGLOB_CVTNULL;
3105 qflags |= QGLOB_REGEXP;
3106 p = string_list (l);
3107 r = quote_string_for_globbing (p, qflags);
3119 /* Call expand_word_internal to expand W and handle error returns.
3120 A convenience function for functions that don't want to handle
3121 any errors or free any memory before aborting. */
3123 call_expand_word_internal (w, q, i, c, e)
3129 result = expand_word_internal (w, q, i, c, e);
3130 if (result == &expand_word_error || result == &expand_word_fatal)
3132 /* By convention, each time this error is returned, w->word has
3133 already been freed (it sometimes may not be in the fatal case,
3134 but that doesn't result in a memory leak because we're going
3135 to exit in most cases). */
3136 w->word = (char *)NULL;
3137 last_command_exit_value = EXECUTION_FAILURE;
3138 exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
3145 /* Perform parameter expansion, command substitution, and arithmetic
3146 expansion on STRING, as if it were a word. Leave the result quoted. */
3148 expand_string_internal (string, quoted)
3155 if (string == 0 || *string == 0)
3156 return ((WORD_LIST *)NULL);
3159 td.word = savestring (string);
3161 tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3167 /* Expand STRING by performing parameter expansion, command substitution,
3168 and arithmetic expansion. Dequote the resulting WORD_LIST before
3169 returning it, but do not perform word splitting. The call to
3170 remove_quoted_nulls () is in here because word splitting normally
3171 takes care of quote removal. */
3173 expand_string_unsplit (string, quoted)
3179 if (string == 0 || *string == '\0')
3180 return ((WORD_LIST *)NULL);
3182 expand_no_split_dollar_star = 1;
3183 value = expand_string_internal (string, quoted);
3184 expand_no_split_dollar_star = 0;
3190 remove_quoted_nulls (value->word->word);
3191 value->word->flags &= ~W_HASQUOTEDNULL;
3193 dequote_list (value);
3198 /* Expand the rhs of an assignment statement */
3200 expand_string_assignment (string, quoted)
3207 if (string == 0 || *string == '\0')
3208 return ((WORD_LIST *)NULL);
3210 expand_no_split_dollar_star = 1;
3212 td.flags = W_ASSIGNRHS;
3213 td.word = savestring (string);
3214 value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3217 expand_no_split_dollar_star = 0;
3223 remove_quoted_nulls (value->word->word);
3224 value->word->flags &= ~W_HASQUOTEDNULL;
3226 dequote_list (value);
3232 /* Expand one of the PS? prompt strings. This is a sort of combination of
3233 expand_string_unsplit and expand_string_internal, but returns the
3234 passed string when an error occurs. Might want to trap other calls
3235 to jump_to_top_level here so we don't endlessly loop. */
3237 expand_prompt_string (string, quoted, wflags)
3245 if (string == 0 || *string == 0)
3246 return ((WORD_LIST *)NULL);
3249 td.word = savestring (string);
3251 no_longjmp_on_fatal_error = 1;
3252 value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3253 no_longjmp_on_fatal_error = 0;
3255 if (value == &expand_word_error || value == &expand_word_fatal)
3257 value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
3265 remove_quoted_nulls (value->word->word);
3266 value->word->flags &= ~W_HASQUOTEDNULL;
3268 dequote_list (value);
3273 /* Expand STRING just as if you were expanding a word, but do not dequote
3274 the resultant WORD_LIST. This is called only from within this file,
3275 and is used to correctly preserve quoted characters when expanding
3276 things like ${1+"$@"}. This does parameter expansion, command
3277 substitution, arithmetic expansion, and word splitting. */
3279 expand_string_leave_quoted (string, quoted)
3286 if (string == 0 || *string == '\0')
3287 return ((WORD_LIST *)NULL);
3289 tlist = expand_string_internal (string, quoted);
3293 tresult = word_list_split (tlist);
3294 dispose_words (tlist);
3297 return ((WORD_LIST *)NULL);
3300 /* This does not perform word splitting or dequote the WORD_LIST
3303 expand_string_for_rhs (string, quoted, dollar_at_p, has_dollar_at)
3305 int quoted, *dollar_at_p, *has_dollar_at;
3310 if (string == 0 || *string == '\0')
3311 return (WORD_LIST *)NULL;
3315 tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, has_dollar_at);
3319 /* Expand STRING just as if you were expanding a word. This also returns
3320 a list of words. Note that filename globbing is *NOT* done for word
3321 or string expansion, just when the shell is expanding a command. This
3322 does parameter expansion, command substitution, arithmetic expansion,
3323 and word splitting. Dequote the resultant WORD_LIST before returning. */
3325 expand_string (string, quoted)
3331 if (string == 0 || *string == '\0')
3332 return ((WORD_LIST *)NULL);
3334 result = expand_string_leave_quoted (string, quoted);
3335 return (result ? dequote_list (result) : result);
3338 /***************************************************
3340 * Functions to handle quoting chars *
3342 ***************************************************/
3346 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
3347 The parser passes CTLNUL as CTLESC CTLNUL. */
3349 /* Quote escape characters in string s, but no other characters. This is
3350 used to protect CTLESC and CTLNUL in variable values from the rest of
3351 the word expansion process after the variable is expanded (word splitting
3352 and filename generation). If IFS is null, we quote spaces as well, just
3353 in case we split on spaces later (in the case of unquoted $@, we will
3354 eventually attempt to split the entire word on spaces). Corresponding
3355 code exists in dequote_escapes. Even if we don't end up splitting on
3356 spaces, quoting spaces is not a problem. This should never be called on
3357 a string that is quoted with single or double quotes or part of a here
3358 document (effectively double-quoted). */
3360 quote_escapes (string)
3363 register char *s, *t;
3365 char *result, *send;
3366 int quote_spaces, skip_ctlesc, skip_ctlnul;
3369 slen = strlen (string);
3370 send = string + slen;
3372 quote_spaces = (ifs_value && *ifs_value == 0);
3374 for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
3375 skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
3377 t = result = (char *)xmalloc ((slen * 2) + 1);
3382 if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' '))
3384 COPY_CHAR_P (t, s, send);
3391 list_quote_escapes (list)
3394 register WORD_LIST *w;
3397 for (w = list; w; w = w->next)
3400 w->word->word = quote_escapes (t);
3406 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
3408 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
3409 This is necessary to make unquoted CTLESC and CTLNUL characters in the
3410 data stream pass through properly.
3412 We need to remove doubled CTLESC characters inside quoted strings before
3413 quoting the entire string, so we do not double the number of CTLESC
3416 Also used by parts of the pattern substitution code. */
3418 dequote_escapes (string)
3421 register char *s, *t, *s1;
3423 char *result, *send;
3430 slen = strlen (string);
3431 send = string + slen;
3433 t = result = (char *)xmalloc (slen + 1);
3435 if (strchr (string, CTLESC) == 0)
3436 return (strcpy (result, string));
3438 quote_spaces = (ifs_value && *ifs_value == 0);
3443 if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' ')))
3449 COPY_CHAR_P (t, s, send);
3455 /* Return a new string with the quoted representation of character C.
3456 This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
3457 set in any resultant WORD_DESC where this value is the word. */
3459 make_quoted_char (c)
3464 temp = (char *)xmalloc (3);
3479 /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
3480 the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
3481 this value is the word. */
3483 quote_string (string)
3488 char *result, *send;
3492 result = (char *)xmalloc (2);
3500 slen = strlen (string);
3501 send = string + slen;
3503 result = (char *)xmalloc ((slen * 2) + 1);
3505 for (t = result; string < send; )
3508 COPY_CHAR_P (t, string, send);
3515 /* De-quote quoted characters in STRING. */
3517 dequote_string (string)
3520 register char *s, *t;
3522 char *result, *send;
3525 slen = strlen (string);
3527 t = result = (char *)xmalloc (slen + 1);
3529 if (QUOTED_NULL (string))
3535 /* If no character in the string can be quoted, don't bother examining
3536 each character. Just return a copy of the string passed to us. */
3537 if (strchr (string, CTLESC) == NULL)
3538 return (strcpy (result, string));
3540 send = string + slen;
3550 COPY_CHAR_P (t, s, send);
3557 /* Quote the entire WORD_LIST list. */
3562 register WORD_LIST *w;
3565 for (w = list; w; w = w->next)
3568 w->word->word = quote_string (t);
3570 w->word->flags |= W_HASQUOTEDNULL; /* XXX - turn on W_HASQUOTEDNULL here? */
3571 w->word->flags |= W_QUOTED;
3577 /* De-quote quoted characters in each word in LIST. */
3583 register WORD_LIST *tlist;
3585 for (tlist = list; tlist; tlist = tlist->next)
3587 s = dequote_string (tlist->word->word);
3588 if (QUOTED_NULL (tlist->word->word))
3589 tlist->word->flags &= ~W_HASQUOTEDNULL;
3590 free (tlist->word->word);
3591 tlist->word->word = s;
3596 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
3599 remove_quoted_escapes (string)
3606 t = dequote_escapes (string);
3614 /* Perform quoted null character removal on STRING. We don't allow any
3615 quoted null characters in the middle or at the ends of strings because
3616 of how expand_word_internal works. remove_quoted_nulls () turns
3617 STRING into an empty string iff it only consists of a quoted null,
3618 and removes all unquoted CTLNUL characters. */
3620 remove_quoted_nulls (string)
3623 register size_t slen;
3624 register int i, j, prev_i;
3627 if (strchr (string, CTLNUL) == 0) /* XXX */
3628 return string; /* XXX */
3630 slen = strlen (string);
3635 if (string[i] == CTLESC)
3637 /* Old code had j++, but we cannot assume that i == j at this
3638 point -- what if a CTLNUL has already been removed from the
3639 string? We don't want to drop the CTLESC or recopy characters
3640 that we've already copied down. */
3641 i++; string[j++] = CTLESC;
3645 else if (string[i] == CTLNUL)
3649 ADVANCE_CHAR (string, slen, i);
3652 do string[j++] = string[prev_i++]; while (prev_i < i);
3662 /* Perform quoted null character removal on each element of LIST.
3663 This modifies LIST. */
3665 word_list_remove_quoted_nulls (list)
3668 register WORD_LIST *t;
3670 for (t = list; t; t = t->next)
3672 remove_quoted_nulls (t->word->word);
3673 t->word->flags &= ~W_HASQUOTEDNULL;
3677 /* **************************************************************** */
3679 /* Functions for Matching and Removing Patterns */
3681 /* **************************************************************** */
3683 #if defined (HANDLE_MULTIBYTE)
3684 #if 0 /* Currently unused */
3685 static unsigned char *
3686 mb_getcharlens (string, len)
3690 int i, offset, last;
3697 ret = (unsigned char *)xmalloc (len);
3698 memset (ret, 0, len);
3699 while (string[last])
3701 ADVANCE_CHAR (string, len, offset);
3702 ret[last] = offset - last;
3710 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
3711 can have one of 4 values:
3712 RP_LONG_LEFT remove longest matching portion at start of PARAM
3713 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
3714 RP_LONG_RIGHT remove longest matching portion at end of PARAM
3715 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
3718 #define RP_LONG_LEFT 1
3719 #define RP_SHORT_LEFT 2
3720 #define RP_LONG_RIGHT 3
3721 #define RP_SHORT_RIGHT 4
3724 remove_upattern (param, pattern, op)
3725 char *param, *pattern;
3730 register char *p, *ret, c;
3732 len = STRLEN (param);
3737 case RP_LONG_LEFT: /* remove longest match at start */
3738 for (p = end; p >= param; p--)
3741 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3744 return (savestring (p));
3751 case RP_SHORT_LEFT: /* remove shortest match at start */
3752 for (p = param; p <= end; p++)
3755 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3758 return (savestring (p));
3764 case RP_LONG_RIGHT: /* remove longest match at end */
3765 for (p = param; p <= end; p++)
3767 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3770 ret = savestring (param);
3777 case RP_SHORT_RIGHT: /* remove shortest match at end */
3778 for (p = end; p >= param; p--)
3780 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3783 ret = savestring (param);
3791 return (savestring (param)); /* no match, return original string */
3794 #if defined (HANDLE_MULTIBYTE)
3796 remove_wpattern (wparam, wstrlen, wpattern, op)
3807 case RP_LONG_LEFT: /* remove longest match at start */
3808 for (n = wstrlen; n >= 0; n--)
3810 wc = wparam[n]; wparam[n] = L'\0';
3811 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3814 return (wcsdup (wparam + n));
3820 case RP_SHORT_LEFT: /* remove shortest match at start */
3821 for (n = 0; n <= wstrlen; n++)
3823 wc = wparam[n]; wparam[n] = L'\0';
3824 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3827 return (wcsdup (wparam + n));
3833 case RP_LONG_RIGHT: /* remove longest match at end */
3834 for (n = 0; n <= wstrlen; n++)
3836 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3838 wc = wparam[n]; wparam[n] = L'\0';
3839 ret = wcsdup (wparam);
3846 case RP_SHORT_RIGHT: /* remove shortest match at end */
3847 for (n = wstrlen; n >= 0; n--)
3849 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3851 wc = wparam[n]; wparam[n] = L'\0';
3852 ret = wcsdup (wparam);
3860 return (wcsdup (wparam)); /* no match, return original string */
3862 #endif /* HANDLE_MULTIBYTE */
3865 remove_pattern (param, pattern, op)
3866 char *param, *pattern;
3871 if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */
3872 return (savestring (param));
3874 #if defined (HANDLE_MULTIBYTE)
3877 wchar_t *ret, *oret;
3879 wchar_t *wparam, *wpattern;
3883 n = xdupmbstowcs (&wpattern, NULL, pattern);
3884 if (n == (size_t)-1)
3885 return (remove_upattern (param, pattern, op));
3886 n = xdupmbstowcs (&wparam, NULL, param);
3887 if (n == (size_t)-1)
3890 return (remove_upattern (param, pattern, op));
3892 oret = ret = remove_wpattern (wparam, n, wpattern, op);
3898 xret = (char *)xmalloc (n + 1);
3899 memset (&ps, '\0', sizeof (mbstate_t));
3900 n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps);
3901 xret[n] = '\0'; /* just to make sure */
3907 return (remove_upattern (param, pattern, op));
3910 /* Return 1 of the first character of STRING could match the first
3911 character of pattern PAT. Used to avoid n2 calls to strmatch(). */
3913 match_pattern_char (pat, string)
3924 return (*string == c);
3926 return (*string == *pat);
3928 return (*pat == LPAREN ? 1 : (*string != '\0'));
3934 return (*pat == LPAREN ? 1 : (*string == c));
3936 return (*string != '\0');
3940 /* Match PAT anywhere in STRING and return the match boundaries.
3941 This returns 1 in case of a successful match, 0 otherwise. SP
3942 and EP are pointers into the string where the match begins and
3943 ends, respectively. MTYPE controls what kind of match is attempted.
3944 MATCH_BEG and MATCH_END anchor the match at the beginning and end
3945 of the string, respectively. The longest match is returned. */
3947 match_upattern (string, pat, mtype, sp, ep)
3953 register char *p, *p1, *npat;
3956 /* If the pattern doesn't match anywhere in the string, go ahead and
3957 short-circuit right away. A minor optimization, saves a bunch of
3958 unnecessary calls to strmatch (up to N calls for a string of N
3959 characters) if the match is unsuccessful. To preserve the semantics
3960 of the substring matches below, we make sure that the pattern has
3961 `*' as first and last character, making a new pattern if necessary. */
3962 /* XXX - check this later if I ever implement `**' with special meaning,
3963 since this will potentially result in `**' at the beginning or end */
3965 if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*')
3967 p = npat = (char *)xmalloc (len + 3);
3969 if (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob))
3973 if (p1[-1] != '*' || p[-2] == '\\')
3979 c = strmatch (npat, string, FNMATCH_EXTFLAG);
3982 if (c == FNM_NOMATCH)
3985 len = STRLEN (string);
3991 for (p = string; p <= end; p++)
3993 if (match_pattern_char (pat, p))
3995 for (p1 = end; p1 >= p; p1--)
3997 c = *p1; *p1 = '\0';
3998 if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
4013 if (match_pattern_char (pat, string) == 0)
4016 for (p = end; p >= string; p--)
4019 if (strmatch (pat, string, FNMATCH_EXTFLAG) == 0)
4032 for (p = string; p <= end; p++)
4034 if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
4049 #if defined (HANDLE_MULTIBYTE)
4050 /* Return 1 of the first character of WSTRING could match the first
4051 character of pattern WPAT. Wide character version. */
4053 match_pattern_wchar (wpat, wstring)
4054 wchar_t *wpat, *wstring;
4061 switch (wc = *wpat++)
4064 return (*wstring == wc);
4066 return (*wstring == *wpat);
4068 return (*wpat == LPAREN ? 1 : (*wstring != L'\0'));
4074 return (*wpat == LPAREN ? 1 : (*wstring == wc));
4076 return (*wstring != L'\0');
4080 /* Match WPAT anywhere in WSTRING and return the match boundaries.
4081 This returns 1 in case of a successful match, 0 otherwise. Wide
4082 character version. */
4084 match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
4092 wchar_t wc, *wp, *nwpat, *wp1;
4095 size_t n, n1; /* Apple's gcc seems to miscompile this badly */
4100 /* If the pattern doesn't match anywhere in the string, go ahead and
4101 short-circuit right away. A minor optimization, saves a bunch of
4102 unnecessary calls to strmatch (up to N calls for a string of N
4103 characters) if the match is unsuccessful. To preserve the semantics
4104 of the substring matches below, we make sure that the pattern has
4105 `*' as first and last character, making a new pattern if necessary. */
4106 /* XXX - check this later if I ever implement `**' with special meaning,
4107 since this will potentially result in `**' at the beginning or end */
4108 len = wcslen (wpat);
4109 if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*')
4111 wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t));
4113 if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob))
4115 while (*wp1 != L'\0')
4117 if (wp1[-1] != L'*' || wp1[-2] == L'\\')
4123 len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG);
4126 if (len == FNM_NOMATCH)
4132 for (n = 0; n <= wstrlen; n++)
4134 if (match_pattern_wchar (wpat, wstring + n))
4136 for (n1 = wstrlen; n1 >= n; n1--)
4138 wc = wstring[n1]; wstring[n1] = L'\0';
4139 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
4154 if (match_pattern_wchar (wpat, wstring) == 0)
4157 for (n = wstrlen; n >= 0; n--)
4159 wc = wstring[n]; wstring[n] = L'\0';
4160 if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG) == 0)
4173 for (n = 0; n <= wstrlen; n++)
4175 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
4178 *ep = indices[wstrlen];
4188 #endif /* HANDLE_MULTIBYTE */
4191 match_pattern (string, pat, mtype, sp, ep)
4196 #if defined (HANDLE_MULTIBYTE)
4199 wchar_t *wstring, *wpat;
4203 if (string == 0 || *string == 0 || pat == 0 || *pat == 0)
4206 #if defined (HANDLE_MULTIBYTE)
4209 n = xdupmbstowcs (&wpat, NULL, pat);
4210 if (n == (size_t)-1)
4211 return (match_upattern (string, pat, mtype, sp, ep));
4212 n = xdupmbstowcs (&wstring, &indices, string);
4213 if (n == (size_t)-1)
4216 return (match_upattern (string, pat, mtype, sp, ep));
4218 ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep);
4228 return (match_upattern (string, pat, mtype, sp, ep));
4232 getpatspec (c, value)
4237 return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
4239 return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
4242 /* Posix.2 says that the WORD should be run through tilde expansion,
4243 parameter expansion, command substitution and arithmetic expansion.
4244 This leaves the result quoted, so quote_string_for_globbing () has
4245 to be called to fix it up for strmatch (). If QUOTED is non-zero,
4246 it means that the entire expression was enclosed in double quotes.
4247 This means that quoting characters in the pattern do not make any
4248 special pattern characters quoted. For example, the `*' in the
4249 following retains its special meaning: "${foo#'*'}". */
4251 getpattern (value, quoted, expandpat)
4253 int quoted, expandpat;
4260 /* There is a problem here: how to handle single or double quotes in the
4261 pattern string when the whole expression is between double quotes?
4262 POSIX.2 says that enclosing double quotes do not cause the pattern to
4263 be quoted, but does that leave us a problem with @ and array[@] and their
4264 expansions inside a pattern? */
4266 if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
4269 pat = string_extract_double_quoted (tword, &i, 1);
4275 /* expand_string_for_rhs () leaves WORD quoted and does not perform
4277 l = *value ? expand_string_for_rhs (value,
4278 (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
4279 (int *)NULL, (int *)NULL)
4281 pat = string_list (l);
4285 tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
4293 /* Handle removing a pattern from a string as a result of ${name%[%]value}
4294 or ${name#[#]value}. */
4296 variable_remove_pattern (value, pattern, patspec, quoted)
4297 char *value, *pattern;
4298 int patspec, quoted;
4302 tword = remove_pattern (value, pattern, patspec);
4309 list_remove_pattern (list, pattern, patspec, itype, quoted)
4312 int patspec, itype, quoted;
4318 for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
4320 tword = remove_pattern (l->word->word, pattern, patspec);
4321 w = alloc_word_desc ();
4322 w->word = tword ? tword : savestring ("");
4323 new = make_word_list (w, new);
4326 l = REVERSE_LIST (new, WORD_LIST *);
4327 tword = string_list_pos_params (itype, l, quoted);
4334 parameter_list_remove_pattern (itype, pattern, patspec, quoted)
4337 int patspec, quoted;
4342 list = list_rest_of_args ();
4344 return ((char *)NULL);
4345 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
4346 dispose_words (list);
4350 #if defined (ARRAY_VARS)
4352 array_remove_pattern (var, pattern, patspec, varname, quoted)
4356 char *varname; /* so we can figure out how it's indexed */
4366 /* compute itype from varname here */
4367 v = array_variable_part (varname, &ret, 0);
4370 a = (v && array_p (v)) ? array_cell (v) : 0;
4371 h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
4373 list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
4375 return ((char *)NULL);
4376 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
4377 dispose_words (list);
4381 #endif /* ARRAY_VARS */
4384 parameter_brace_remove_pattern (varname, value, patstr, rtype, quoted)
4385 char *varname, *value, *patstr;
4388 int vtype, patspec, starsub;
4389 char *temp1, *val, *pattern;
4393 return ((char *)NULL);
4395 this_command_name = varname;
4397 vtype = get_var_and_type (varname, value, quoted, &v, &val);
4399 return ((char *)NULL);
4401 starsub = vtype & VT_STARSUB;
4402 vtype &= ~VT_STARSUB;
4404 patspec = getpatspec (rtype, patstr);
4405 if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
4408 /* Need to pass getpattern newly-allocated memory in case of expansion --
4409 the expansion code will free the passed string on an error. */
4410 temp1 = savestring (patstr);
4411 pattern = getpattern (temp1, quoted, 1);
4414 temp1 = (char *)NULL; /* shut up gcc */
4418 case VT_ARRAYMEMBER:
4419 temp1 = remove_pattern (val, pattern, patspec);
4420 if (vtype == VT_VARIABLE)
4424 val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
4425 ? quote_string (temp1)
4426 : quote_escapes (temp1);
4431 #if defined (ARRAY_VARS)
4433 temp1 = array_remove_pattern (v, pattern, patspec, varname, quoted);
4434 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
4436 val = quote_escapes (temp1);
4443 temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
4444 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
4446 val = quote_escapes (temp1);
4457 /*******************************************
4459 * Functions to expand WORD_DESCs *
4461 *******************************************/
4463 /* Expand WORD, performing word splitting on the result. This does
4464 parameter expansion, command substitution, arithmetic expansion,
4465 word splitting, and quote removal. */
4468 expand_word (word, quoted)
4472 WORD_LIST *result, *tresult;
4474 tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
4475 result = word_list_split (tresult);
4476 dispose_words (tresult);
4477 return (result ? dequote_list (result) : result);
4480 /* Expand WORD, but do not perform word splitting on the result. This
4481 does parameter expansion, command substitution, arithmetic expansion,
4482 and quote removal. */
4484 expand_word_unsplit (word, quoted)
4490 expand_no_split_dollar_star = 1;
4491 #if defined (HANDLE_MULTIBYTE)
4492 if (ifs_firstc[0] == 0)
4494 if (ifs_firstc == 0)
4496 word->flags |= W_NOSPLIT;
4497 result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
4498 expand_no_split_dollar_star = 0;
4500 return (result ? dequote_list (result) : result);
4503 /* Perform shell expansions on WORD, but do not perform word splitting or
4504 quote removal on the result. Virtually identical to expand_word_unsplit;
4505 could be combined if implementations don't diverge. */
4507 expand_word_leave_quoted (word, quoted)
4513 expand_no_split_dollar_star = 1;
4514 #if defined (HANDLE_MULTIBYTE)
4515 if (ifs_firstc[0] == 0)
4517 if (ifs_firstc == 0)
4519 word->flags |= W_NOSPLIT;
4520 word->flags |= W_NOSPLIT2;
4521 result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
4522 expand_no_split_dollar_star = 0;
4527 #if defined (PROCESS_SUBSTITUTION)
4529 /*****************************************************************/
4531 /* Hacking Process Substitution */
4533 /*****************************************************************/
4535 #if !defined (HAVE_DEV_FD)
4536 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
4537 of FIFOs the shell has open. unlink_fifo_list will walk the list and
4538 unlink all of them. add_fifo_list adds the name of an open FIFO to the
4539 list. NFIFO is a count of the number of FIFOs in the list. */
4540 #define FIFO_INCR 20
4547 static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
4549 static int fifo_list_size;
4552 add_fifo_list (pathname)
4555 if (nfifo >= fifo_list_size - 1)
4557 fifo_list_size += FIFO_INCR;
4558 fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
4559 fifo_list_size * sizeof (struct temp_fifo));
4562 fifo_list[nfifo].file = savestring (pathname);
4574 for (i = saved = 0; i < nfifo; i++)
4576 if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
4578 unlink (fifo_list[i].file);
4579 free (fifo_list[i].file);
4580 fifo_list[i].file = (char *)NULL;
4581 fifo_list[i].proc = -1;
4587 /* If we didn't remove some of the FIFOs, compact the list. */
4590 for (i = j = 0; i < nfifo; i++)
4591 if (fifo_list[i].file)
4593 fifo_list[j].file = fifo_list[i].file;
4594 fifo_list[j].proc = fifo_list[i].proc;
4614 tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR);
4615 if (mkfifo (tname, 0600) < 0)
4618 return ((char *)NULL);
4621 add_fifo_list (tname);
4625 #else /* HAVE_DEV_FD */
4627 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
4628 has open to children. NFDS is a count of the number of bits currently
4629 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
4631 static char *dev_fd_list = (char *)NULL;
4633 static int totfds; /* The highest possible number of open files. */
4639 if (!dev_fd_list || fd >= totfds)
4644 totfds = getdtablesize ();
4645 if (totfds < 0 || totfds > 256)
4650 dev_fd_list = (char *)xrealloc (dev_fd_list, totfds);
4651 memset (dev_fd_list + ofds, '\0', totfds - ofds);
4654 dev_fd_list[fd] = 1;
4661 return 0; /* used for cleanup; not needed with /dev/fd */
4672 for (i = 0; nfds && i < totfds; i++)
4683 #if defined (NOTDEF)
4684 print_dev_fd_list ()
4688 fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
4691 for (i = 0; i < totfds; i++)
4694 fprintf (stderr, " %d", i);
4696 fprintf (stderr, "\n");
4701 make_dev_fd_filename (fd)
4704 char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
4706 ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8);
4708 strcpy (ret, DEV_FD_PREFIX);
4709 p = inttostr (fd, intbuf, sizeof (intbuf));
4710 strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
4716 #endif /* HAVE_DEV_FD */
4718 /* Return a filename that will open a connection to the process defined by
4719 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
4720 a filename in /dev/fd corresponding to a descriptor that is one of the
4721 ends of the pipe. If not defined, we use named pipes on systems that have
4722 them. Systems without /dev/fd and named pipes are out of luck.
4724 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
4725 use the read end of the pipe and dup that file descriptor to fd 0 in
4726 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
4727 writing or use the write end of the pipe in the child, and dup that
4728 file descriptor to fd 1 in the child. The parent does the opposite. */
4731 process_substitute (string, open_for_read_in_child)
4733 int open_for_read_in_child;
4738 #if defined (HAVE_DEV_FD)
4739 int parent_pipe_fd, child_pipe_fd;
4741 #endif /* HAVE_DEV_FD */
4742 #if defined (JOB_CONTROL)
4743 pid_t old_pipeline_pgrp;
4746 if (!string || !*string || wordexp_only)
4747 return ((char *)NULL);
4749 #if !defined (HAVE_DEV_FD)
4750 pathname = make_named_pipe ();
4751 #else /* HAVE_DEV_FD */
4752 if (pipe (fildes) < 0)
4754 sys_error (_("cannot make pipe for process substitution"));
4755 return ((char *)NULL);
4757 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
4758 the pipe in the parent, otherwise the read end. */
4759 parent_pipe_fd = fildes[open_for_read_in_child];
4760 child_pipe_fd = fildes[1 - open_for_read_in_child];
4761 /* Move the parent end of the pipe to some high file descriptor, to
4762 avoid clashes with FDs used by the script. */
4763 parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
4765 pathname = make_dev_fd_filename (parent_pipe_fd);
4766 #endif /* HAVE_DEV_FD */
4770 sys_error (_("cannot make pipe for process substitution"));
4771 return ((char *)NULL);
4774 old_pid = last_made_pid;
4776 #if defined (JOB_CONTROL)
4777 old_pipeline_pgrp = pipeline_pgrp;
4778 pipeline_pgrp = shell_pgrp;
4780 #endif /* JOB_CONTROL */
4782 pid = make_child ((char *)NULL, 1);
4785 reset_terminating_signals (); /* XXX */
4786 free_pushed_string_input ();
4787 /* Cancel traps, in trap.c. */
4788 restore_original_signals ();
4789 setup_async_signals ();
4790 subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB;
4793 #if defined (JOB_CONTROL)
4794 set_sigchld_handler ();
4795 stop_making_children ();
4796 /* XXX - should we only do this in the parent? (as in command subst) */
4797 pipeline_pgrp = old_pipeline_pgrp;
4798 #endif /* JOB_CONTROL */
4802 sys_error (_("cannot make child for process substitution"));
4804 #if defined (HAVE_DEV_FD)
4805 close (parent_pipe_fd);
4806 close (child_pipe_fd);
4807 #endif /* HAVE_DEV_FD */
4808 return ((char *)NULL);
4813 #if defined (JOB_CONTROL)
4814 restore_pipeline (1);
4817 #if !defined (HAVE_DEV_FD)
4818 fifo_list[nfifo-1].proc = pid;
4821 last_made_pid = old_pid;
4823 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
4825 #endif /* JOB_CONTROL && PGRP_PIPE */
4827 #if defined (HAVE_DEV_FD)
4828 close (child_pipe_fd);
4829 #endif /* HAVE_DEV_FD */
4834 set_sigint_handler ();
4836 #if defined (JOB_CONTROL)
4837 set_job_control (0);
4838 #endif /* JOB_CONTROL */
4840 #if !defined (HAVE_DEV_FD)
4841 /* Open the named pipe in the child. */
4842 fd = open (pathname, open_for_read_in_child ? O_RDONLY|O_NONBLOCK : O_WRONLY);
4845 /* Two separate strings for ease of translation. */
4846 if (open_for_read_in_child)
4847 sys_error (_("cannot open named pipe %s for reading"), pathname);
4849 sys_error (_("cannot open named pipe %s for writing"), pathname);
4853 if (open_for_read_in_child)
4855 if (sh_unset_nodelay_mode (fd) < 0)
4857 sys_error (_("cannot reset nodelay mode for fd %d"), fd);
4861 #else /* HAVE_DEV_FD */
4863 #endif /* HAVE_DEV_FD */
4865 if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
4867 sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname,
4868 open_for_read_in_child ? 0 : 1);
4872 if (fd != (open_for_read_in_child ? 0 : 1))
4875 /* Need to close any files that this process has open to pipes inherited
4877 if (current_fds_to_close)
4879 close_fd_bitmap (current_fds_to_close);
4880 current_fds_to_close = (struct fd_bitmap *)NULL;
4883 #if defined (HAVE_DEV_FD)
4884 /* Make sure we close the parent's end of the pipe and clear the slot
4885 in the fd list so it is not closed later, if reallocated by, for
4886 instance, pipe(2). */
4887 close (parent_pipe_fd);
4888 dev_fd_list[parent_pipe_fd] = 0;
4889 #endif /* HAVE_DEV_FD */
4891 result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
4893 #if !defined (HAVE_DEV_FD)
4894 /* Make sure we close the named pipe in the child before we exit. */
4895 close (open_for_read_in_child ? 0 : 1);
4896 #endif /* !HAVE_DEV_FD */
4901 #endif /* PROCESS_SUBSTITUTION */
4903 /***********************************/
4905 /* Command Substitution */
4907 /***********************************/
4910 read_comsub (fd, quoted, rflag)
4914 char *istring, buf[128], *bufp, *s;
4915 int istring_index, istring_size, c, tflag, skip_ctlesc, skip_ctlnul;
4918 istring = (char *)NULL;
4919 istring_index = istring_size = bufn = tflag = 0;
4921 for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
4922 skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
4925 setmode (fd, O_TEXT); /* we don't want CR/LF, we want Unix-style */
4928 /* Read the output of the command through the pipe. This may need to be
4929 changed to understand multibyte characters in the future. */
4936 bufn = zread (fd, buf, sizeof (buf));
4946 internal_warning ("read_comsub: ignored null byte in input");
4951 /* Add the character to ISTRING, possibly after resizing it. */
4952 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
4954 /* This is essentially quote_string inline */
4955 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */)
4956 istring[istring_index++] = CTLESC;
4957 /* Escape CTLESC and CTLNUL in the output to protect those characters
4958 from the rest of the word expansions (word splitting and globbing.)
4959 This is essentially quote_escapes inline. */
4960 else if (skip_ctlesc == 0 && c == CTLESC)
4962 tflag |= W_HASCTLESC;
4963 istring[istring_index++] = CTLESC;
4965 else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0)))
4966 istring[istring_index++] = CTLESC;
4968 istring[istring_index++] = c;
4971 #if defined (__CYGWIN__)
4972 if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r')
4975 istring[istring_index - 1] = '\n';
4982 istring[istring_index] = '\0';
4984 /* If we read no output, just return now and save ourselves some
4986 if (istring_index == 0)
4991 return (char *)NULL;
4994 /* Strip trailing newlines from the output of the command. */
4995 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
4997 while (istring_index > 0)
4999 if (istring[istring_index - 1] == '\n')
5003 /* If the newline was quoted, remove the quoting char. */
5004 if (istring[istring_index - 1] == CTLESC)
5010 istring[istring_index] = '\0';
5013 strip_trailing (istring, istring_index - 1, 1);
5020 /* Perform command substitution on STRING. This returns a WORD_DESC * with the
5021 contained string possibly quoted. */
5023 command_substitute (string, quoted)
5027 pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
5029 int result, fildes[2], function_value, pflags, rc, tflag;
5032 istring = (char *)NULL;
5034 /* Don't fork () if there is no need to. In the case of no command to
5035 run, just return NULL. */
5036 if (!string || !*string || (string[0] == '\n' && !string[1]))
5037 return ((WORD_DESC *)NULL);
5039 if (wordexp_only && read_but_dont_execute)
5041 last_command_exit_value = EX_WEXPCOMSUB;
5042 jump_to_top_level (EXITPROG);
5045 /* We're making the assumption here that the command substitution will
5046 eventually run a command from the file system. Since we'll run
5047 maybe_make_export_env in this subshell before executing that command,
5048 the parent shell and any other shells it starts will have to remake
5049 the environment. If we make it before we fork, other shells won't
5050 have to. Don't bother if we have any temporary variable assignments,
5051 though, because the export environment will be remade after this
5052 command completes anyway, but do it if all the words to be expanded
5053 are variable assignments. */
5054 if (subst_assign_varlist == 0 || garglist == 0)
5055 maybe_make_export_env (); /* XXX */
5057 /* Flags to pass to parse_and_execute() */
5058 pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0;
5060 /* Pipe the output of executing STRING into the current shell. */
5061 if (pipe (fildes) < 0)
5063 sys_error (_("cannot make pipe for command substitution"));
5067 old_pid = last_made_pid;
5068 #if defined (JOB_CONTROL)
5069 old_pipeline_pgrp = pipeline_pgrp;
5070 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
5071 if ((subshell_environment & SUBSHELL_PIPE) == 0)
5072 pipeline_pgrp = shell_pgrp;
5073 cleanup_the_pipeline ();
5074 #endif /* JOB_CONTROL */
5076 old_async_pid = last_asynchronous_pid;
5077 pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
5078 last_asynchronous_pid = old_async_pid;
5081 /* Reset the signal handlers in the child, but don't free the
5083 reset_signal_handlers ();
5085 #if defined (JOB_CONTROL)
5086 /* XXX DO THIS ONLY IN PARENT ? XXX */
5087 set_sigchld_handler ();
5088 stop_making_children ();
5090 pipeline_pgrp = old_pipeline_pgrp;
5092 stop_making_children ();
5093 #endif /* JOB_CONTROL */
5097 sys_error (_("cannot make child for command substitution"));
5103 return ((WORD_DESC *)NULL);
5108 set_sigint_handler (); /* XXX */
5110 free_pushed_string_input ();
5112 if (dup2 (fildes[1], 1) < 0)
5114 sys_error (_("command_substitute: cannot duplicate pipe as fd 1"));
5115 exit (EXECUTION_FAILURE);
5118 /* If standard output is closed in the parent shell
5119 (such as after `exec >&-'), file descriptor 1 will be
5120 the lowest available file descriptor, and end up in
5121 fildes[0]. This can happen for stdin and stderr as well,
5122 but stdout is more important -- it will cause no output
5123 to be generated from this command. */
5124 if ((fildes[1] != fileno (stdin)) &&
5125 (fildes[1] != fileno (stdout)) &&
5126 (fildes[1] != fileno (stderr)))
5129 if ((fildes[0] != fileno (stdin)) &&
5130 (fildes[0] != fileno (stdout)) &&
5131 (fildes[0] != fileno (stderr)))
5134 /* The currently executing shell is not interactive. */
5137 /* This is a subshell environment. */
5138 subshell_environment |= SUBSHELL_COMSUB;
5140 /* When not in POSIX mode, command substitution does not inherit
5142 if (posixly_correct == 0)
5143 exit_immediately_on_error = 0;
5145 remove_quoted_escapes (string);
5147 startup_state = 2; /* see if we can avoid a fork */
5148 /* Give command substitution a place to jump back to on failure,
5149 so we don't go back up to main (). */
5150 result = setjmp (top_level);
5152 /* If we're running a command substitution inside a shell function,
5153 trap `return' so we don't return from the function in the subshell
5154 and go off to never-never land. */
5155 if (result == 0 && return_catch_flag)
5156 function_value = setjmp (return_catch);
5160 if (result == ERREXIT)
5161 rc = last_command_exit_value;
5162 else if (result == EXITPROG)
5163 rc = last_command_exit_value;
5165 rc = EXECUTION_FAILURE;
5166 else if (function_value)
5167 rc = return_catch_value;
5171 rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST);
5175 last_command_exit_value = rc;
5176 rc = run_exit_trap ();
5177 #if defined (PROCESS_SUBSTITUTION)
5178 unlink_fifo_list ();
5184 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
5186 #endif /* JOB_CONTROL && PGRP_PIPE */
5191 istring = read_comsub (fildes[0], quoted, &tflag);
5195 current_command_subst_pid = pid;
5196 last_command_exit_value = wait_for (pid);
5197 last_command_subst_pid = pid;
5198 last_made_pid = old_pid;
5200 #if defined (JOB_CONTROL)
5201 /* If last_command_exit_value > 128, then the substituted command
5202 was terminated by a signal. If that signal was SIGINT, then send
5203 SIGINT to ourselves. This will break out of loops, for instance. */
5204 if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT)
5205 kill (getpid (), SIGINT);
5207 /* wait_for gives the terminal back to shell_pgrp. If some other
5208 process group should have it, give it away to that group here.
5209 pipeline_pgrp is non-zero only while we are constructing a
5210 pipline, so what we are concerned about is whether or not that
5211 pipeline was started in the background. A pipeline started in
5212 the background should never get the tty back here. */
5214 if (interactive && pipeline_pgrp != (pid_t)0 && pipeline_pgrp != last_asynchronous_pid)
5216 if (interactive && pipeline_pgrp != (pid_t)0 && (subshell_environment & SUBSHELL_ASYNC) == 0)
5218 give_terminal_to (pipeline_pgrp, 0);
5219 #endif /* JOB_CONTROL */
5221 ret = alloc_word_desc ();
5222 ret->word = istring;
5229 /********************************************************
5231 * Utility functions for parameter expansion *
5233 ********************************************************/
5235 #if defined (ARRAY_VARS)
5238 array_length_reference (s)
5248 var = array_variable_part (s, &t, &len);
5250 /* If unbound variables should generate an error, report one and return
5252 if ((var == 0 || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error)
5256 last_command_exit_value = EXECUTION_FAILURE;
5264 /* We support a couple of expansions for variables that are not arrays.
5265 We'll return the length of the value for v[0], and 1 for v[@] or
5266 v[*]. Return 0 for everything else. */
5268 array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
5270 if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
5273 return (assoc_num_elements (assoc_cell (var)));
5274 else if (array_p (var))
5275 return (array_num_elements (array));
5283 akey = expand_assignment_string_to_string (t, 0); /* [ */
5285 if (akey == 0 || *akey == 0)
5287 err_badarraysub (t);
5290 t = assoc_reference (assoc_cell (var), akey);
5294 ind = array_expand_index (t, len);
5297 err_badarraysub (t);
5301 t = array_reference (array, ind);
5303 t = (ind == 0) ? value_cell (var) : (char *)NULL;
5306 len = MB_STRLEN (t);
5309 #endif /* ARRAY_VARS */
5312 valid_brace_expansion_word (name, var_is_special)
5316 if (DIGIT (*name) && all_digits (name))
5318 else if (var_is_special)
5320 #if defined (ARRAY_VARS)
5321 else if (valid_array_reference (name))
5323 #endif /* ARRAY_VARS */
5324 else if (legal_identifier (name))
5331 chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at)
5334 int *quoted_dollar_atp, *contains_dollar_at;
5340 if (quoted_dollar_atp)
5341 *quoted_dollar_atp = 0;
5342 if (contains_dollar_at)
5343 *contains_dollar_at = 0;
5347 /* check for $@ and $* */
5348 if (name[0] == '@' && name[1] == 0)
5350 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
5351 *quoted_dollar_atp = 1;
5352 if (contains_dollar_at)
5353 *contains_dollar_at = 1;
5356 else if (name[0] == '*' && name[1] == '\0' && quoted == 0)
5358 if (contains_dollar_at)
5359 *contains_dollar_at = 1;
5363 /* Now check for ${array[@]} and ${array[*]} */
5364 #if defined (ARRAY_VARS)
5365 else if (valid_array_reference (name))
5367 temp1 = mbschr (name, '[');
5368 if (temp1 && temp1[1] == '@' && temp1[2] == ']')
5370 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
5371 *quoted_dollar_atp = 1;
5372 if (contains_dollar_at)
5373 *contains_dollar_at = 1;
5376 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
5377 which should result in separate words even when IFS is unset. */
5378 if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0)
5380 if (contains_dollar_at)
5381 *contains_dollar_at = 1;
5389 /* Parameter expand NAME, and return a new string which is the expansion,
5390 or NULL if there was no expansion.
5391 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
5392 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
5393 NAME was found inside of a double-quoted expression. */
5395 parameter_brace_expand_word (name, var_is_special, quoted, pflags)
5397 int var_is_special, quoted, pflags;
5409 /* Handle multiple digit arguments, as in ${11}. */
5410 if (legal_number (name, &arg_index))
5412 tt = get_dollar_var_value (arg_index);
5414 temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
5416 : quote_escapes (tt);
5418 temp = (char *)NULL;
5421 else if (var_is_special) /* ${@} */
5424 tt = (char *)xmalloc (2 + strlen (name));
5425 tt[sindex = 0] = '$';
5426 strcpy (tt + 1, name);
5428 ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
5429 (int *)NULL, (int *)NULL, pflags);
5432 #if defined (ARRAY_VARS)
5433 else if (valid_array_reference (name))
5435 temp = array_value (name, quoted, &atype);
5436 if (atype == 0 && temp)
5437 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
5438 ? quote_string (temp)
5439 : quote_escapes (temp);
5440 else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
5441 rflags |= W_HASQUOTEDNULL;
5444 else if (var = find_variable (name))
5446 if (var_isset (var) && invisible_p (var) == 0)
5448 #if defined (ARRAY_VARS)
5450 temp = assoc_reference (assoc_cell (var), "0");
5451 else if (array_p (var))
5452 temp = array_reference (array_cell (var), 0);
5454 temp = value_cell (var);
5456 temp = value_cell (var);
5460 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
5461 ? quote_string (temp)
5462 : quote_escapes (temp);
5465 temp = (char *)NULL;
5468 temp = (char *)NULL;
5472 ret = alloc_word_desc ();
5474 ret->flags |= rflags;
5479 /* Expand an indirect reference to a variable: ${!NAME} expands to the
5480 value of the variable whose name is the value of NAME. */
5482 parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at)
5484 int var_is_special, quoted;
5485 int *quoted_dollar_atp, *contains_dollar_at;
5490 w = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND);
5492 /* Have to dequote here if necessary */
5495 temp = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
5496 ? dequote_string (t)
5497 : dequote_escapes (t);
5501 dispose_word_desc (w);
5503 chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at);
5505 return (WORD_DESC *)NULL;
5507 w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, 0);
5513 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
5514 depending on the value of C, the separating character. C can be one of
5515 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
5516 between double quotes. */
5518 parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat)
5520 int c, quoted, *qdollaratp, *hasdollarat;
5524 char *t, *t1, *temp;
5527 /* If the entire expression is between double quotes, we want to treat
5528 the value as a double-quoted string, with the exception that we strip
5529 embedded unescaped double quotes (for sh backwards compatibility). */
5530 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
5533 temp = string_extract_double_quoted (value, &hasdol, 1);
5538 w = alloc_word_desc ();
5540 /* XXX was 0 not quoted */
5541 l = *temp ? expand_string_for_rhs (temp, quoted, &hasdol, (int *)NULL)
5544 *hasdollarat = hasdol || (l && l->next);
5549 /* The expansion of TEMP returned something. We need to treat things
5550 slightly differently if HASDOL is non-zero. If we have "$@", the
5551 individual words have already been quoted. We need to turn them
5552 into a string with the words separated by the first character of
5553 $IFS without any additional quoting, so string_list_dollar_at won't
5554 do the right thing. We use string_list_dollar_star instead. */
5555 temp = (hasdol || l->next) ? string_list_dollar_star (l) : string_list (l);
5557 /* If l->next is not null, we know that TEMP contained "$@", since that
5558 is the only expansion that creates more than one word. */
5559 if (qdollaratp && ((hasdol && quoted) || l->next))
5563 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && hasdol)
5565 /* The brace expansion occurred between double quotes and there was
5566 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
5567 it does not expand to anything. In this case, we want to return
5568 a quoted empty string. */
5569 temp = make_quoted_char ('\0');
5570 w->flags |= W_HASQUOTEDNULL;
5573 temp = (char *)NULL;
5575 if (c == '-' || c == '+')
5582 t = temp ? savestring (temp) : savestring ("");
5583 t1 = dequote_string (t);
5585 #if defined (ARRAY_VARS)
5586 if (valid_array_reference (name))
5587 assign_array_element (name, t1, 0);
5589 #endif /* ARRAY_VARS */
5590 bind_variable (name, t1, 0);
5597 /* Deal with the right hand side of a ${name:?value} expansion in the case
5598 that NAME is null or not set. If VALUE is non-null it is expanded and
5599 used as the error message to print, otherwise a standard message is
5602 parameter_brace_expand_error (name, value)
5608 if (value && *value)
5610 l = expand_string (value, 0);
5611 temp = string_list (l);
5612 report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */
5617 report_error (_("%s: parameter null or not set"), name);
5619 /* Free the data we have allocated during this expansion, since we
5620 are about to longjmp out. */
5625 /* Return 1 if NAME is something for which parameter_brace_expand_length is
5628 valid_length_expression (name)
5631 return (name[1] == '\0' || /* ${#} */
5632 ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */
5633 (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */
5634 #if defined (ARRAY_VARS)
5635 valid_array_reference (name + 1) || /* ${#a[7]} */
5637 legal_identifier (name + 1)); /* ${#PS1} */
5640 #if defined (HANDLE_MULTIBYTE)
5646 mbstate_t mbs, mbsbak;
5649 memset (&mbs, 0, sizeof (mbs));
5651 while ((clen = mbrlen(s, MB_CUR_MAX, &mbs)) != 0)
5653 if (MB_INVALIDCH(clen))
5655 clen = 1; /* assume single byte */
5668 /* Handle the parameter brace expansion that requires us to return the
5669 length of a parameter. */
5671 parameter_brace_expand_length (name)
5675 intmax_t number, arg_index;
5677 #if defined (ARRAY_VARS)
5681 if (name[1] == '\0') /* ${#} */
5682 number = number_of_args ();
5683 else if ((name[1] == '@' || name[1] == '*') && name[2] == '\0') /* ${#@}, ${#*} */
5684 number = number_of_args ();
5685 else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
5687 /* Take the lengths of some of the shell's special parameters. */
5691 t = which_set_flags ();
5694 t = itos (last_command_exit_value);
5697 t = itos (dollar_dollar_pid);
5700 if (last_asynchronous_pid == NO_PID)
5703 t = itos (last_asynchronous_pid);
5706 t = itos (number_of_args ());
5709 number = STRLEN (t);
5712 #if defined (ARRAY_VARS)
5713 else if (valid_array_reference (name + 1))
5714 number = array_length_reference (name + 1);
5715 #endif /* ARRAY_VARS */
5720 if (legal_number (name + 1, &arg_index)) /* ${#1} */
5722 t = get_dollar_var_value (arg_index);
5723 number = MB_STRLEN (t);
5726 #if defined (ARRAY_VARS)
5727 else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var)))
5730 t = assoc_reference (assoc_cell (var), "0");
5732 t = array_reference (array_cell (var), 0);
5733 number = MB_STRLEN (t);
5738 newname = savestring (name);
5740 list = expand_string (newname, Q_DOUBLE_QUOTES);
5741 t = list ? string_list (list) : (char *)NULL;
5744 dispose_words (list);
5746 number = MB_STRLEN (t);
5754 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
5755 so we do some ad-hoc parsing of an arithmetic expression to find
5756 the first DELIM, instead of using strchr(3). Two rules:
5757 1. If the substring contains a `(', read until closing `)'.
5758 2. If the substring contains a `?', read past one `:' for each `?'.
5762 skiparith (substr, delim)
5767 int skipcol, pcount, i;
5770 sublen = strlen (substr);
5771 i = skipcol = pcount = 0;
5774 /* Balance parens */
5775 if (substr[i] == LPAREN)
5781 if (substr[i] == RPAREN && pcount)
5789 ADVANCE_CHAR (substr, sublen, i);
5793 /* Skip one `:' for each `?' */
5794 if (substr[i] == ':' && skipcol)
5800 if (substr[i] == delim)
5802 if (substr[i] == '?')
5808 ADVANCE_CHAR (substr, sublen, i);
5811 return (substr + i);
5814 /* Verify and limit the start and end of the desired substring. If
5815 VTYPE == 0, a regular shell variable is being used; if it is 1,
5816 then the positional parameters are being used; if it is 2, then
5817 VALUE is really a pointer to an array variable that should be used.
5818 Return value is 1 if both values were OK, 0 if there was a problem
5819 with an invalid expression, or -1 if the values were out of range. */
5821 verify_substring_values (v, value, substr, vtype, e1p, e2p)
5823 char *value, *substr;
5825 intmax_t *e1p, *e2p;
5827 char *t, *temp1, *temp2;
5830 #if defined (ARRAY_VARS)
5835 /* duplicate behavior of strchr(3) */
5836 t = skiparith (substr, ':');
5837 if (*t && *t == ':')
5842 temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES);
5843 *e1p = evalexp (temp1, &expok);
5848 len = -1; /* paranoia */
5852 case VT_ARRAYMEMBER:
5853 len = MB_STRLEN (value);
5856 len = number_of_args () + 1;
5858 len++; /* add one arg if counting from $0 */
5860 #if defined (ARRAY_VARS)
5862 /* For arrays, the first value deals with array indices. Negative
5863 offsets count from one past the array's maximum index. Associative
5864 arrays treat the number of elements as the maximum index. */
5868 len = assoc_num_elements (h) + (*e1p < 0);
5873 len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */
5879 if (len == -1) /* paranoia */
5882 if (*e1p < 0) /* negative offsets count from end */
5885 if (*e1p > len || *e1p < 0)
5888 #if defined (ARRAY_VARS)
5889 /* For arrays, the second offset deals with the number of elements. */
5890 if (vtype == VT_ARRAYVAR)
5891 len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a);
5897 temp2 = savestring (t);
5898 temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
5901 *e2p = evalexp (temp1, &expok);
5907 internal_error (_("%s: substring expression < 0"), t);
5910 #if defined (ARRAY_VARS)
5911 /* In order to deal with sparse arrays, push the intelligence about how
5912 to deal with the number of elements desired down to the array-
5913 specific functions. */
5914 if (vtype != VT_ARRAYVAR)
5917 *e2p += *e1p; /* want E2 chars starting at E1 */
5928 /* Return the type of variable specified by VARNAME (simple variable,
5929 positional param, or array variable). Also return the value specified
5930 by VARNAME (value of a variable or a reference to an array element).
5931 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
5932 characters in the value are quoted with CTLESC and takes appropriate
5933 steps. For convenience, *VALP is set to the dequoted VALUE. */
5935 get_var_and_type (varname, value, quoted, varp, valp)
5936 char *varname, *value;
5943 #if defined (ARRAY_VARS)
5947 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
5948 vtype = (varname[0] == '@' || varname[0] == '*') && varname[1] == '\0';
5949 if (vtype == VT_POSPARMS && varname[0] == '*')
5950 vtype |= VT_STARSUB;
5951 *varp = (SHELL_VAR *)NULL;
5953 #if defined (ARRAY_VARS)
5954 if (valid_array_reference (varname))
5956 v = array_variable_part (varname, &temp, (int *)0);
5957 if (v && (array_p (v) || assoc_p (v)))
5959 if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')
5961 /* Callers have to differentiate betwen indexed and associative */
5962 vtype = VT_ARRAYVAR;
5964 vtype |= VT_STARSUB;
5965 *valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v);
5969 vtype = VT_ARRAYMEMBER;
5970 *valp = array_value (varname, 1, (int *)NULL);
5974 else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']'))
5976 vtype = VT_VARIABLE;
5978 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
5979 *valp = dequote_string (value);
5981 *valp = dequote_escapes (value);
5985 vtype = VT_ARRAYMEMBER;
5987 *valp = array_value (varname, 1, (int *)NULL);
5990 else if ((v = find_variable (varname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v)))
5992 vtype = VT_ARRAYMEMBER;
5994 *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0);
5999 if (value && vtype == VT_VARIABLE)
6001 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
6002 *valp = dequote_string (value);
6004 *valp = dequote_escapes (value);
6013 /******************************************************/
6015 /* Functions to extract substrings of variable values */
6017 /******************************************************/
6019 #if defined (HANDLE_MULTIBYTE)
6020 /* Character-oriented rather than strictly byte-oriented substrings. S and
6021 E, rather being strict indices into STRING, indicate character (possibly
6022 multibyte character) positions that require calculation.
6023 Used by the ${param:offset[:length]} expansion. */
6025 mb_substring (string, s, e)
6030 int start, stop, i, slen;
6034 /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
6035 slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0;
6038 while (string[start] && i--)
6039 ADVANCE_CHAR (string, slen, start);
6042 while (string[stop] && i--)
6043 ADVANCE_CHAR (string, slen, stop);
6044 tt = substring (string, start, stop);
6049 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
6050 is `@', use the positional parameters; otherwise, use the value of
6051 VARNAME. If VARNAME is an array variable, use the array elements. */
6054 parameter_brace_substring (varname, value, substr, quoted)
6055 char *varname, *value, *substr;
6059 int vtype, r, starsub;
6060 char *temp, *val, *tt, *oname;
6064 return ((char *)NULL);
6066 oname = this_command_name;
6067 this_command_name = varname;
6069 vtype = get_var_and_type (varname, value, quoted, &v, &val);
6072 this_command_name = oname;
6073 return ((char *)NULL);
6076 starsub = vtype & VT_STARSUB;
6077 vtype &= ~VT_STARSUB;
6079 r = verify_substring_values (v, val, substr, vtype, &e1, &e2);
6080 this_command_name = oname;
6082 return ((r == 0) ? &expand_param_error : (char *)NULL);
6087 case VT_ARRAYMEMBER:
6088 #if defined (HANDLE_MULTIBYTE)
6090 tt = mb_substring (val, e1, e2);
6093 tt = substring (val, e1, e2);
6095 if (vtype == VT_VARIABLE)
6097 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
6098 temp = quote_string (tt);
6100 temp = tt ? quote_escapes (tt) : (char *)NULL;
6104 tt = pos_params (varname, e1, e2, quoted);
6105 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
6107 temp = tt ? quote_escapes (tt) : (char *)NULL;
6113 #if defined (ARRAY_VARS)
6116 /* we convert to list and take first e2 elements starting at e1th
6117 element -- officially undefined for now */
6118 temp = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted);
6120 /* We want E2 to be the number of elements desired (arrays can be sparse,
6121 so verify_substring_values just returns the numbers specified and we
6122 rely on array_subrange to understand how to deal with them). */
6123 temp = array_subrange (array_cell (v), e1, e2, starsub, quoted);
6124 /* array_subrange now calls array_quote_escapes as appropriate, so the
6125 caller no longer needs to. */
6129 temp = (char *)NULL;
6135 /****************************************************************/
6137 /* Functions to perform pattern substitution on variable values */
6139 /****************************************************************/
6142 pat_subst (string, pat, rep, mflags)
6143 char *string, *pat, *rep;
6146 char *ret, *s, *e, *str;
6147 int rsize, rptr, l, replen, mtype;
6149 mtype = mflags & MATCH_TYPEMASK;
6152 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
6153 * with REP and return the result.
6154 * 2. A null pattern with mtype == MATCH_END means to append REP to
6155 * STRING and return the result.
6157 if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
6159 replen = STRLEN (rep);
6160 l = strlen (string);
6161 ret = (char *)xmalloc (replen + l + 2);
6163 strcpy (ret, string);
6164 else if (mtype == MATCH_BEG)
6167 strcpy (ret + replen, string);
6171 strcpy (ret, string);
6172 strcpy (ret + l, rep);
6177 ret = (char *)xmalloc (rsize = 64);
6180 for (replen = STRLEN (rep), rptr = 0, str = string;;)
6182 if (match_pattern (str, pat, mtype, &s, &e) == 0)
6185 RESIZE_MALLOCED_BUFFER (ret, rptr, (l + replen), rsize, 64);
6187 /* OK, now copy the leading unmatched portion of the string (from
6188 str to s) to ret starting at rptr (the current offset). Then copy
6189 the replacement string at ret + rptr + (s - str). Increment
6190 rptr (if necessary) and str and go on. */
6193 strncpy (ret + rptr, str, l);
6198 strncpy (ret + rptr, rep, replen);
6201 str = e; /* e == end of match */
6203 if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
6208 /* On a zero-length match, make sure we copy one character, since
6209 we increment one character to avoid infinite recursion. */
6210 RESIZE_MALLOCED_BUFFER (ret, rptr, 1, rsize, 64);
6211 ret[rptr++] = *str++;
6212 e++; /* avoid infinite recursion on zero-length match */
6216 /* Now copy the unmatched portion of the input string */
6219 RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
6220 strcpy (ret + rptr, str);
6228 /* Do pattern match and replacement on the positional parameters. */
6230 pos_params_pat_subst (string, pat, rep, mflags)
6231 char *string, *pat, *rep;
6234 WORD_LIST *save, *params;
6239 save = params = list_rest_of_args ();
6241 return ((char *)NULL);
6243 for ( ; params; params = params->next)
6245 ret = pat_subst (params->word->word, pat, rep, mflags);
6246 w = alloc_word_desc ();
6247 w->word = ret ? ret : savestring ("");
6248 dispose_word (params->word);
6252 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
6253 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
6256 if ((mflags & (MATCH_QUOTED|MATCH_STARSUB)) == (MATCH_QUOTED|MATCH_STARSUB))
6257 ret = string_list_dollar_star (quote_list (save));
6258 else if ((mflags & MATCH_STARSUB) == MATCH_STARSUB)
6259 ret = string_list_dollar_star (save);
6260 else if ((mflags & MATCH_QUOTED) == MATCH_QUOTED)
6261 ret = string_list_dollar_at (save, qflags);
6263 ret = string_list_dollar_star (save);
6265 ret = string_list_pos_params (pchar, save, qflags);
6268 dispose_words (save);
6273 /* Perform pattern substitution on VALUE, which is the expansion of
6274 VARNAME. PATSUB is an expression supplying the pattern to match
6275 and the string to substitute. QUOTED is a flags word containing
6276 the type of quoting currently in effect. */
6278 parameter_brace_patsub (varname, value, patsub, quoted)
6279 char *varname, *value, *patsub;
6282 int vtype, mflags, starsub, delim;
6283 char *val, *temp, *pat, *rep, *p, *lpatsub, *tt;
6287 return ((char *)NULL);
6289 this_command_name = varname;
6291 vtype = get_var_and_type (varname, value, quoted, &v, &val);
6293 return ((char *)NULL);
6295 starsub = vtype & VT_STARSUB;
6296 vtype &= ~VT_STARSUB;
6299 if (patsub && *patsub == '/')
6301 mflags |= MATCH_GLOBREP;
6305 /* Malloc this because expand_string_if_necessary or one of the expansion
6306 functions in its call chain may free it on a substitution error. */
6307 lpatsub = savestring (patsub);
6309 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6310 mflags |= MATCH_QUOTED;
6313 mflags |= MATCH_STARSUB;
6315 /* If the pattern starts with a `/', make sure we skip over it when looking
6316 for the replacement delimiter. */
6318 if (rep = quoted_strchr ((*patsub == '/') ? lpatsub+1 : lpatsub, '/', ST_BACKSL))
6323 delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0);
6324 if (lpatsub[delim] == '/')
6327 rep = lpatsub + delim + 1;
6333 if (rep && *rep == '\0')
6336 /* Perform the same expansions on the pattern as performed by the
6337 pattern removal expansions. */
6338 pat = getpattern (lpatsub, quoted, 1);
6342 if ((mflags & MATCH_QUOTED) == 0)
6343 rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
6345 rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
6348 /* ksh93 doesn't allow the match specifier to be a part of the expanded
6349 pattern. This is an extension. Make sure we don't anchor the pattern
6350 at the beginning or end of the string if we're doing global replacement,
6353 if (mflags & MATCH_GLOBREP)
6354 mflags |= MATCH_ANY;
6355 else if (pat && pat[0] == '#')
6357 mflags |= MATCH_BEG;
6360 else if (pat && pat[0] == '%')
6362 mflags |= MATCH_END;
6366 mflags |= MATCH_ANY;
6368 /* OK, we now want to substitute REP for PAT in VAL. If
6369 flags & MATCH_GLOBREP is non-zero, the substitution is done
6370 everywhere, otherwise only the first occurrence of PAT is
6371 replaced. The pattern matching code doesn't understand
6372 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
6373 values passed in (VT_VARIABLE) so the pattern substitution
6374 code works right. We need to requote special chars after
6375 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
6376 other cases if QUOTED == 0, since the posparams and arrays
6377 indexed by * or @ do special things when QUOTED != 0. */
6382 case VT_ARRAYMEMBER:
6383 temp = pat_subst (val, p, rep, mflags);
6384 if (vtype == VT_VARIABLE)
6388 tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
6394 temp = pos_params_pat_subst (val, p, rep, mflags);
6395 if (temp && (mflags & MATCH_QUOTED) == 0)
6397 tt = quote_escapes (temp);
6402 #if defined (ARRAY_VARS)
6404 temp = assoc_p (v) ? assoc_patsub (assoc_cell (v), p, rep, mflags)
6405 : array_patsub (array_cell (v), p, rep, mflags);
6406 /* Don't call quote_escapes anymore; array_patsub calls
6407 array_quote_escapes as appropriate before adding the
6408 space separators; ditto for assoc_patsub. */
6420 /****************************************************************/
6422 /* Functions to perform case modification on variable values */
6424 /****************************************************************/
6426 /* Do case modification on the positional parameters. */
6429 pos_params_modcase (string, pat, modop, mflags)
6434 WORD_LIST *save, *params;
6439 save = params = list_rest_of_args ();
6441 return ((char *)NULL);
6443 for ( ; params; params = params->next)
6445 ret = sh_modcase (params->word->word, pat, modop);
6446 w = alloc_word_desc ();
6447 w->word = ret ? ret : savestring ("");
6448 dispose_word (params->word);
6452 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
6453 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
6455 ret = string_list_pos_params (pchar, save, qflags);
6456 dispose_words (save);
6461 /* Perform case modification on VALUE, which is the expansion of
6462 VARNAME. MODSPEC is an expression supplying the type of modification
6463 to perform. QUOTED is a flags word containing the type of quoting
6464 currently in effect. */
6466 parameter_brace_casemod (varname, value, modspec, patspec, quoted)
6467 char *varname, *value;
6472 int vtype, starsub, modop, mflags, x;
6473 char *val, *temp, *pat, *p, *lpat, *tt;
6477 return ((char *)NULL);
6479 this_command_name = varname;
6481 vtype = get_var_and_type (varname, value, quoted, &v, &val);
6483 return ((char *)NULL);
6485 starsub = vtype & VT_STARSUB;
6486 vtype &= ~VT_STARSUB;
6490 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6491 mflags |= MATCH_QUOTED;
6493 mflags |= MATCH_STARSUB;
6498 x = p && p[0] == modspec;
6499 modop = x ? CASE_UPPER : CASE_UPFIRST;
6502 else if (modspec == ',')
6504 x = p && p[0] == modspec;
6505 modop = x ? CASE_LOWER : CASE_LOWFIRST;
6508 else if (modspec == '~')
6510 x = p && p[0] == modspec;
6511 modop = x ? CASE_TOGGLEALL : CASE_TOGGLE;
6515 lpat = p ? savestring (p) : 0;
6516 /* Perform the same expansions on the pattern as performed by the
6517 pattern removal expansions. FOR LATER */
6518 pat = lpat ? getpattern (lpat, quoted, 1) : 0;
6520 /* OK, now we do the case modification. */
6524 case VT_ARRAYMEMBER:
6525 temp = sh_modcase (val, pat, modop);
6526 if (vtype == VT_VARIABLE)
6530 tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
6537 temp = pos_params_modcase (val, pat, modop, mflags);
6538 if (temp && (mflags & MATCH_QUOTED) == 0)
6540 tt = quote_escapes (temp);
6546 #if defined (ARRAY_VARS)
6548 temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags)
6549 : array_modcase (array_cell (v), pat, modop, mflags);
6550 /* Don't call quote_escapes; array_modcase calls array_quote_escapes
6551 as appropriate before adding the space separators; ditto for
6563 /* Check for unbalanced parens in S, which is the contents of $(( ... )). If
6564 any occur, this must be a nested command substitution, so return 0.
6565 Otherwise, return 1. A valid arithmetic expression must always have a
6566 ( before a matching ), so any cases where there are more right parens
6567 means that this must not be an arithmetic expression, though the parser
6568 will not accept it without a balanced total number of parens. */
6570 chk_arithsub (s, len)
6582 else if (s[i] == RPAREN)
6592 ADVANCE_CHAR (s, len, i);
6598 ADVANCE_CHAR (s, len, i);
6602 i = skip_single_quoted (s, len, ++i);
6606 i = skip_double_quoted ((char *)s, len, ++i);
6611 return (count == 0);
6614 /****************************************************************/
6616 /* Functions to perform parameter expansion on a string */
6618 /****************************************************************/
6620 /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
6622 parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at)
6624 int *indexp, quoted, *quoted_dollar_atp, *contains_dollar_at, pflags;
6626 int check_nullness, var_is_set, var_is_null, var_is_special;
6627 int want_substring, want_indir, want_patsub, want_casemod;
6628 char *name, *value, *temp, *temp1;
6629 WORD_DESC *tdesc, *ret;
6630 int t_index, sindex, c, tflag, modspec;
6633 temp = temp1 = value = (char *)NULL;
6634 var_is_set = var_is_null = var_is_special = check_nullness = 0;
6635 want_substring = want_indir = want_patsub = want_casemod = 0;
6639 /* ${#var} doesn't have any of the other parameter expansions on it. */
6640 if (string[t_index] == '#' && legal_variable_starter (string[t_index+1])) /* {{ */
6641 name = string_extract (string, &t_index, "}", SX_VARNAME);
6643 #if defined (CASEMOD_EXPANSIONS)
6644 /* To enable case-toggling expansions using the `~' operator character
6645 change the 1 to 0. */
6646 # if defined (CASEMOD_CAPCASE)
6647 name = string_extract (string, &t_index, "#%^,~:-=?+/}", SX_VARNAME);
6649 name = string_extract (string, &t_index, "#%^,:-=?+/}", SX_VARNAME);
6650 # endif /* CASEMOD_CAPCASE */
6652 name = string_extract (string, &t_index, "#%:-=?+/}", SX_VARNAME);
6653 #endif /* CASEMOD_EXPANSIONS */
6658 /* If the name really consists of a special variable, then make sure
6659 that we have the entire name. We don't allow indirect references
6660 to special variables except `#', `?', `@' and `*'. */
6661 if ((sindex == t_index &&
6662 (string[t_index] == '-' ||
6663 string[t_index] == '?' ||
6664 string[t_index] == '#')) ||
6665 (sindex == t_index - 1 && string[sindex] == '!' &&
6666 (string[t_index] == '#' ||
6667 string[t_index] == '?' ||
6668 string[t_index] == '@' ||
6669 string[t_index] == '*')))
6673 temp1 = string_extract (string, &t_index, "#%:-=?+/}", 0);
6674 name = (char *)xmalloc (3 + (strlen (temp1)));
6675 *name = string[sindex];
6676 if (string[sindex] == '!')
6678 /* indirect reference of $#, $?, $@, or $* */
6679 name[1] = string[sindex + 1];
6680 strcpy (name + 2, temp1);
6683 strcpy (name + 1, temp1);
6688 /* Find out what character ended the variable name. Then
6689 do the appropriate thing. */
6690 if (c = string[sindex])
6693 /* If c is followed by one of the valid parameter expansion
6694 characters, move past it as normal. If not, assume that
6695 a substring specification is being given, and do not move
6697 if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
6700 if (c = string[sindex])
6703 else if (c == ':' && string[sindex] != RBRACE)
6705 else if (c == '/' && string[sindex] != RBRACE)
6707 #if defined (CASEMOD_EXPANSIONS)
6708 else if (c == '^' || c == ',' || c == '~')
6715 /* Catch the valid and invalid brace expressions that made it through the
6717 /* ${#-} is a valid expansion and means to take the length of $-.
6718 Similarly for ${#?} and ${##}... */
6719 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
6720 VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
6722 name = (char *)xrealloc (name, 3);
6725 c = string[sindex++];
6728 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
6729 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
6730 member (c, "%:=+/") && string[sindex] == RBRACE)
6732 temp = (char *)NULL;
6733 goto bad_substitution;
6736 /* Indirect expansion begins with a `!'. A valid indirect expansion is
6737 either a variable name, one of the positional parameters or a special
6738 variable that expands to one of the positional parameters. */
6739 want_indir = *name == '!' &&
6740 (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
6741 || VALID_INDIR_PARAM (name[1]));
6743 /* Determine the value of this variable. */
6745 /* Check for special variables, directly referenced. */
6746 if (SPECIAL_VAR (name, want_indir))
6749 /* Check for special expansion things, like the length of a parameter */
6750 if (*name == '#' && name[1])
6752 /* If we are not pointing at the character just after the
6753 closing brace, then we haven't gotten all of the name.
6754 Since it begins with a special character, this is a bad
6755 substitution. Also check NAME for validity before trying
6757 if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
6759 temp = (char *)NULL;
6760 goto bad_substitution;
6763 number = parameter_brace_expand_length (name);
6768 return (&expand_wdesc_error);
6771 ret = alloc_word_desc ();
6772 ret->word = itos (number);
6777 /* ${@} is identical to $@. */
6778 if (name[0] == '@' && name[1] == '\0')
6780 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
6781 *quoted_dollar_atp = 1;
6783 if (contains_dollar_at)
6784 *contains_dollar_at = 1;
6787 /* Process ${!PREFIX*} expansion. */
6788 if (want_indir && string[sindex - 1] == RBRACE &&
6789 (string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
6790 legal_variable_starter ((unsigned char) name[1]))
6795 temp1 = savestring (name + 1);
6796 number = strlen (temp1);
6797 temp1[number - 1] = '\0';
6798 x = all_variables_matching_prefix (temp1);
6799 xlist = strvec_to_word_list (x, 0, 0);
6800 if (string[sindex - 2] == '*')
6801 temp = string_list_dollar_star (xlist);
6804 temp = string_list_dollar_at (xlist, quoted);
6805 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
6806 *quoted_dollar_atp = 1;
6807 if (contains_dollar_at)
6808 *contains_dollar_at = 1;
6811 dispose_words (xlist);
6815 ret = alloc_word_desc ();
6820 #if defined (ARRAY_VARS)
6821 /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */
6822 if (want_indir && string[sindex - 1] == RBRACE &&
6823 string[sindex - 2] == ']' && valid_array_reference (name+1))
6827 temp1 = savestring (name + 1);
6828 x = array_variable_name (temp1, &x1, (int *)0); /* [ */
6830 if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == ']')
6832 temp = array_keys (temp1, quoted); /* handles assoc vars too */
6835 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
6836 *quoted_dollar_atp = 1;
6837 if (contains_dollar_at)
6838 *contains_dollar_at = 1;
6844 ret = alloc_word_desc ();
6851 #endif /* ARRAY_VARS */
6853 /* Make sure that NAME is valid before trying to go on. */
6854 if (valid_brace_expansion_word (want_indir ? name + 1 : name,
6855 var_is_special) == 0)
6857 temp = (char *)NULL;
6858 goto bad_substitution;
6862 tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at);
6864 tdesc = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND|(pflags&PF_NOSPLIT2));
6869 tflag = tdesc->flags;
6870 dispose_word_desc (tdesc);
6875 #if defined (ARRAY_VARS)
6876 if (valid_array_reference (name))
6877 chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at);
6880 var_is_set = temp != (char *)0;
6881 var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
6883 /* Get the rest of the stuff inside the braces. */
6884 if (c && c != RBRACE)
6886 /* Extract the contents of the ${ ... } expansion
6887 according to the Posix.2 rules. */
6888 value = extract_dollar_brace_string (string, &sindex, quoted, 0);
6889 if (string[sindex] == RBRACE)
6892 goto bad_substitution;
6895 value = (char *)NULL;
6899 /* If this is a substring spec, process it and add the result. */
6902 temp1 = parameter_brace_substring (name, temp, value, quoted);
6907 if (temp1 == &expand_param_error)
6908 return (&expand_wdesc_error);
6909 else if (temp1 == &expand_param_fatal)
6910 return (&expand_wdesc_fatal);
6912 ret = alloc_word_desc ();
6914 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
6915 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
6918 else if (want_patsub)
6920 temp1 = parameter_brace_patsub (name, temp, value, quoted);
6925 if (temp1 == &expand_param_error)
6926 return (&expand_wdesc_error);
6927 else if (temp1 == &expand_param_fatal)
6928 return (&expand_wdesc_fatal);
6930 ret = alloc_word_desc ();
6932 ret = alloc_word_desc ();
6934 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
6935 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
6938 #if defined (CASEMOD_EXPANSIONS)
6939 else if (want_casemod)
6941 temp1 = parameter_brace_casemod (name, temp, modspec, value, quoted);
6946 if (temp1 == &expand_param_error)
6947 return (&expand_wdesc_error);
6948 else if (temp1 == &expand_param_fatal)
6949 return (&expand_wdesc_fatal);
6951 ret = alloc_word_desc ();
6953 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
6954 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
6959 /* Do the right thing based on which character ended the variable name. */
6965 report_error (_("%s: bad substitution"), string ? string : "??");
6969 return &expand_wdesc_error;
6972 if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1]))
6974 last_command_exit_value = EXECUTION_FAILURE;
6975 err_unboundvar (name);
6979 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
6983 case '#': /* ${param#[#]pattern} */
6984 case '%': /* ${param%[%]pattern} */
6985 if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
6990 temp1 = parameter_brace_remove_pattern (name, temp, value, c, quoted);
6994 ret = alloc_word_desc ();
6996 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
6997 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
7004 if (var_is_set && var_is_null == 0)
7006 /* If the operator is `+', we don't want the value of the named
7007 variable for anything, just the value of the right hand side. */
7011 /* XXX -- if we're double-quoted and the named variable is "$@",
7012 we want to turn off any special handling of "$@" --
7013 we're not using it, so whatever is on the rhs applies. */
7014 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
7015 *quoted_dollar_atp = 0;
7016 if (contains_dollar_at)
7017 *contains_dollar_at = 0;
7022 ret = parameter_brace_expand_rhs (name, value, c,
7025 contains_dollar_at);
7026 /* XXX - fix up later, esp. noting presence of
7027 W_HASQUOTEDNULL in ret->flags */
7031 temp = (char *)NULL;
7037 /* Otherwise do nothing; just use the value in TEMP. */
7039 else /* VAR not set or VAR is NULL. */
7042 temp = (char *)NULL;
7043 if (c == '=' && var_is_special)
7045 report_error (_("$%s: cannot assign in this way"), name);
7048 return &expand_wdesc_error;
7052 parameter_brace_expand_error (name, value);
7053 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7057 /* XXX -- if we're double-quoted and the named variable is "$@",
7058 we want to turn off any special handling of "$@" --
7059 we're not using it, so whatever is on the rhs applies. */
7060 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
7061 *quoted_dollar_atp = 0;
7062 if (contains_dollar_at)
7063 *contains_dollar_at = 0;
7065 ret = parameter_brace_expand_rhs (name, value, c, quoted,
7067 contains_dollar_at);
7068 /* XXX - fix up later, esp. noting presence of
7069 W_HASQUOTEDNULL in tdesc->flags */
7080 ret = alloc_word_desc ();
7087 /* Expand a single ${xxx} expansion. The braces are optional. When
7088 the braces are used, parameter_brace_expand() does the work,
7089 possibly calling param_expand recursively. */
7091 param_expand (string, sindex, quoted, expanded_something,
7092 contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
7095 int *sindex, quoted, *expanded_something, *contains_dollar_at;
7096 int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
7098 char *temp, *temp1, uerror[3];
7099 int zindex, t_index, expok;
7104 WORD_DESC *tdesc, *ret;
7108 c = string[++zindex];
7110 temp = (char *)NULL;
7111 ret = tdesc = (WORD_DESC *)NULL;
7114 /* Do simple cases first. Switch on what follows '$'. */
7128 temp1 = dollar_vars[TODIGIT (c)];
7129 if (unbound_vars_is_error && temp1 == (char *)NULL)
7134 last_command_exit_value = EXECUTION_FAILURE;
7135 err_unboundvar (uerror);
7136 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7139 temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7140 ? quote_string (temp1)
7141 : quote_escapes (temp1);
7143 temp = (char *)NULL;
7147 /* $$ -- pid of the invoking shell. */
7149 temp = itos (dollar_dollar_pid);
7152 /* $# -- number of positional parameters. */
7154 temp = itos (number_of_args ());
7157 /* $? -- return value of the last synchronous command. */
7159 temp = itos (last_command_exit_value);
7162 /* $- -- flags supplied to the shell on invocation or by `set'. */
7164 temp = which_set_flags ();
7167 /* $! -- Pid of the last asynchronous command. */
7169 /* If no asynchronous pids have been created, expand to nothing.
7170 If `set -u' has been executed, and no async processes have
7171 been created, this is an expansion error. */
7172 if (last_asynchronous_pid == NO_PID)
7174 if (expanded_something)
7175 *expanded_something = 0;
7176 temp = (char *)NULL;
7177 if (unbound_vars_is_error)
7182 last_command_exit_value = EXECUTION_FAILURE;
7183 err_unboundvar (uerror);
7184 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7188 temp = itos (last_asynchronous_pid);
7191 /* The only difference between this and $@ is when the arg is quoted. */
7192 case '*': /* `$*' */
7193 list = list_rest_of_args ();
7196 /* According to austin-group posix proposal by Geoff Clare in
7197 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
7199 "The shell shall write a message to standard error and
7200 immediately exit when it tries to expand an unset parameter
7201 other than the '@' and '*' special parameters."
7204 if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
7209 last_command_exit_value = EXECUTION_FAILURE;
7210 err_unboundvar (uerror);
7211 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7215 /* If there are no command-line arguments, this should just
7216 disappear if there are other characters in the expansion,
7217 even if it's quoted. */
7218 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
7219 temp = (char *)NULL;
7220 else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
7222 /* If we have "$*" we want to make a string of the positional
7223 parameters, separated by the first character of $IFS, and
7224 quote the whole string, including the separators. If IFS
7225 is unset, the parameters are separated by ' '; if $IFS is
7226 null, the parameters are concatenated. */
7227 temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list) : string_list (list);
7228 temp1 = quote_string (temp);
7230 tflag |= W_HASQUOTEDNULL;
7236 /* We check whether or not we're eventually going to split $* here,
7237 for example when IFS is empty and we are processing the rhs of
7238 an assignment statement. In that case, we don't separate the
7239 arguments at all. Otherwise, if the $* is not quoted it is
7242 # if defined (HANDLE_MULTIBYTE)
7243 if (expand_no_split_dollar_star && ifs_firstc[0] == 0)
7245 if (expand_no_split_dollar_star && ifs_firstc == 0)
7247 temp = string_list_dollar_star (list);
7249 temp = string_list_dollar_at (list, quoted);
7251 temp = string_list_dollar_at (list, quoted);
7253 if (expand_no_split_dollar_star == 0 && contains_dollar_at)
7254 *contains_dollar_at = 1;
7257 dispose_words (list);
7260 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
7261 means that we have to turn quoting off after we split into
7262 the individually quoted arguments so that the final split
7263 on the first character of $IFS is still done. */
7264 case '@': /* `$@' */
7265 list = list_rest_of_args ();
7268 /* According to austin-group posix proposal by Geoff Clare in
7269 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
7271 "The shell shall write a message to standard error and
7272 immediately exit when it tries to expand an unset parameter
7273 other than the '@' and '*' special parameters."
7276 if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
7281 last_command_exit_value = EXECUTION_FAILURE;
7282 err_unboundvar (uerror);
7283 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
7287 /* We want to flag the fact that we saw this. We can't turn
7288 off quoting entirely, because other characters in the
7289 string might need it (consider "\"$@\""), but we need some
7290 way to signal that the final split on the first character
7291 of $IFS should be done, even though QUOTED is 1. */
7292 /* XXX - should this test include Q_PATQUOTE? */
7293 if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7294 *quoted_dollar_at_p = 1;
7295 if (contains_dollar_at)
7296 *contains_dollar_at = 1;
7299 if (pflags & PF_NOSPLIT2)
7300 temp = string_list_internal (quoted ? quote_list (list) : list, " ");
7303 /* We want to separate the positional parameters with the first
7304 character of $IFS in case $IFS is something other than a space.
7305 We also want to make sure that splitting is done no matter what --
7306 according to POSIX.2, this expands to a list of the positional
7307 parameters no matter what IFS is set to. */
7308 temp = string_list_dollar_at (list, quoted);
7310 dispose_words (list);
7314 tdesc = parameter_brace_expand (string, &zindex, quoted, pflags,
7316 contains_dollar_at);
7318 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
7320 temp = tdesc ? tdesc->word : (char *)0;
7323 /* Quoted nulls should be removed if there is anything else
7325 /* Note that we saw the quoted null so we can add one back at
7326 the end of this function if there are no other characters
7327 in the string, discard TEMP, and go on. The exception to
7328 this is when we have "${@}" and $1 is '', since $@ needs
7329 special handling. */
7330 if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp))
7332 if (had_quoted_null_p)
7333 *had_quoted_null_p = 1;
7334 if (*quoted_dollar_at_p == 0)
7337 tdesc->word = temp = (char *)NULL;
7345 /* Do command or arithmetic substitution. */
7347 /* We have to extract the contents of this paren substitution. */
7348 t_index = zindex + 1;
7349 temp = extract_command_subst (string, &t_index, 0);
7352 /* For Posix.2-style `$(( ))' arithmetic substitution,
7353 extract the expression and pass it to the evaluator. */
7354 if (temp && *temp == LPAREN)
7358 temp2 = savestring (temp1);
7359 t_index = strlen (temp2) - 1;
7361 if (temp2[t_index] != RPAREN)
7367 /* Cut off ending `)' */
7368 temp2[t_index] = '\0';
7370 if (chk_arithsub (temp2, t_index) == 0)
7374 internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
7379 /* Expand variables found inside the expression. */
7380 temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
7384 /* No error messages. */
7385 this_command_name = (char *)NULL;
7386 number = evalexp (temp1, &expok);
7391 if (interactive_shell == 0 && posixly_correct)
7393 last_command_exit_value = EXECUTION_FAILURE;
7394 return (&expand_wdesc_fatal);
7397 return (&expand_wdesc_error);
7399 temp = itos (number);
7404 if (pflags & PF_NOCOMSUB)
7405 /* we need zindex+1 because string[zindex] == RPAREN */
7406 temp1 = substring (string, *sindex, zindex+1);
7409 tdesc = command_substitute (temp, quoted);
7410 temp1 = tdesc ? tdesc->word : (char *)NULL;
7412 dispose_word_desc (tdesc);
7418 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
7419 away in a future bash release. */
7421 /* Extract the contents of this arithmetic substitution. */
7422 t_index = zindex + 1;
7423 temp = extract_arithmetic_subst (string, &t_index);
7427 temp = savestring (string);
7428 if (expanded_something)
7429 *expanded_something = 0;
7433 /* Do initial variable expansion. */
7434 temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES);
7439 /* Find the variable in VARIABLE_LIST. */
7440 temp = (char *)NULL;
7442 for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
7444 temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
7446 /* If this isn't a variable name, then just output the `$'. */
7447 if (temp1 == 0 || *temp1 == '\0')
7450 temp = (char *)xmalloc (2);
7453 if (expanded_something)
7454 *expanded_something = 0;
7458 /* If the variable exists, return its value cell. */
7459 var = find_variable (temp1);
7461 if (var && invisible_p (var) == 0 && var_isset (var))
7463 #if defined (ARRAY_VARS)
7464 if (assoc_p (var) || array_p (var))
7466 temp = array_p (var) ? array_reference (array_cell (var), 0)
7467 : assoc_reference (assoc_cell (var), "0");
7469 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7470 ? quote_string (temp)
7471 : quote_escapes (temp);
7472 else if (unbound_vars_is_error)
7473 goto unbound_variable;
7478 temp = value_cell (var);
7480 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
7481 ? quote_string (temp)
7482 : quote_escapes (temp);
7490 temp = (char *)NULL;
7493 if (unbound_vars_is_error)
7495 last_command_exit_value = EXECUTION_FAILURE;
7496 err_unboundvar (temp1);
7505 last_command_exit_value = EXECUTION_FAILURE;
7506 return ((unbound_vars_is_error && interactive_shell == 0)
7507 ? &expand_wdesc_fatal
7508 : &expand_wdesc_error);
7519 ret = alloc_word_desc ();
7520 ret->flags = tflag; /* XXX */
7526 /* Make a word list which is the result of parameter and variable
7527 expansion, command substitution, arithmetic substitution, and
7528 quote removal of WORD. Return a pointer to a WORD_LIST which is
7529 the result of the expansion. If WORD contains a null word, the
7530 word list returned is also null.
7532 QUOTED contains flag values defined in shell.h.
7534 ISEXP is used to tell expand_word_internal that the word should be
7535 treated as the result of an expansion. This has implications for
7536 how IFS characters in the word are treated.
7538 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
7539 they point to an integer value which receives information about expansion.
7540 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
7541 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
7544 This only does word splitting in the case of $@ expansion. In that
7545 case, we split on ' '. */
7547 /* Values for the local variable quoted_state. */
7549 #define PARTIALLY_QUOTED 1
7550 #define WHOLLY_QUOTED 2
7553 expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
7556 int *contains_dollar_at;
7557 int *expanded_something;
7562 /* The intermediate string that we build while expanding. */
7565 /* The current size of the above object. */
7568 /* Index into ISTRING. */
7571 /* Temporary string storage. */
7574 /* The text of WORD. */
7575 register char *string;
7577 /* The size of STRING. */
7580 /* The index into STRING. */
7583 /* This gets 1 if we see a $@ while quoted. */
7584 int quoted_dollar_at;
7586 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
7587 whether WORD contains no quoting characters, a partially quoted
7588 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
7592 int had_quoted_null;
7595 int pflags; /* flags passed to param_expand */
7597 int assignoff; /* If assignment, offset of `=' */
7599 register unsigned char c; /* Current character. */
7600 int t_index; /* For calls to string_extract_xxx. */
7606 istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
7607 istring[istring_index = 0] = '\0';
7608 quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
7609 quoted_state = UNQUOTED;
7611 string = word->word;
7613 goto finished_with_string;
7614 /* Don't need the string length for the SADD... and COPY_ macros unless
7615 multibyte characters are possible. */
7616 string_size = (MB_CUR_MAX > 1) ? strlen (string) : 1;
7618 if (contains_dollar_at)
7619 *contains_dollar_at = 0;
7623 /* Begin the expansion. */
7629 /* Case on toplevel character. */
7633 goto finished_with_string;
7637 #if HANDLE_MULTIBYTE
7638 if (MB_CUR_MAX > 1 && string[sindex])
7640 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
7645 temp = (char *)xmalloc (3);
7647 temp[1] = c = string[sindex];
7658 istring = sub_append_string (temp, istring, &istring_index, &istring_size);
7664 #if defined (PROCESS_SUBSTITUTION)
7665 /* Process substitution. */
7669 if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)) || posixly_correct)
7671 sindex--; /* add_character: label increments sindex */
7675 t_index = sindex + 1; /* skip past both '<' and LPAREN */
7677 temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index); /*))*/
7680 /* If the process substitution specification is `<()', we want to
7681 open the pipe for writing in the child and produce output; if
7682 it is `>()', we want to open the pipe for reading in the child
7683 and consume input. */
7684 temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
7688 goto dollar_add_string;
7690 #endif /* PROCESS_SUBSTITUTION */
7693 /* Posix.2 section 3.6.1 says that tildes following `=' in words
7694 which are not assignment statements are not expanded. If the
7695 shell isn't in posix mode, though, we perform tilde expansion
7696 on `likely candidate' unquoted assignment statements (flags
7697 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
7698 contains an unquoted :~ or =~. Something to think about: we
7699 now have a flag that says to perform tilde expansion on arguments
7700 to `assignment builtins' like declare and export that look like
7701 assignment statements. We now do tilde expansion on such words
7702 even in POSIX mode. */
7703 if (word->flags & (W_ASSIGNRHS|W_NOTILDE))
7705 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
7706 goto add_ifs_character;
7710 /* If we're not in posix mode or forcing assignment-statement tilde
7711 expansion, note where the `=' appears in the word and prepare to
7712 do tilde expansion following the first `='. */
7713 if ((word->flags & W_ASSIGNMENT) &&
7714 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
7715 assignoff == -1 && sindex > 0)
7717 if (sindex == assignoff && string[sindex+1] == '~') /* XXX */
7718 word->flags |= W_ITILDE;
7720 else if ((word->flags & W_ASSIGNMENT) &&
7721 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
7722 string[sindex+1] == '~')
7723 word->flags |= W_ITILDE;
7725 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
7726 goto add_ifs_character;
7731 if (word->flags & W_NOTILDE)
7733 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
7734 goto add_ifs_character;
7739 if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) &&
7740 string[sindex+1] == '~')
7741 word->flags |= W_ITILDE;
7743 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
7744 goto add_ifs_character;
7749 /* If the word isn't supposed to be tilde expanded, or we're not
7750 at the start of a word or after an unquoted : or = in an
7751 assignment statement, we don't do tilde expansion. */
7752 if ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
7753 (sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
7754 (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
7756 word->flags &= ~W_ITILDE;
7757 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
7758 goto add_ifs_character;
7763 if (word->flags & W_ASSIGNRHS)
7765 else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
7770 temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
7772 word->flags &= ~W_ITILDE;
7774 if (temp && *temp && t_index > 0)
7776 temp1 = bash_tilde_expand (temp, tflag);
7777 if (temp1 && *temp1 == '~' && STREQ (temp, temp1))
7781 goto add_character; /* tilde expansion failed */
7786 goto add_quoted_string; /* XXX was add_string */
7795 if (expanded_something)
7796 *expanded_something = 1;
7799 pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0;
7800 if (word->flags & W_NOSPLIT2)
7801 pflags |= PF_NOSPLIT2;
7802 tword = param_expand (string, &sindex, quoted, expanded_something,
7803 &has_dollar_at, "ed_dollar_at,
7804 &had_quoted_null, pflags);
7806 if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
7810 return ((tword == &expand_wdesc_error) ? &expand_word_error
7811 : &expand_word_fatal);
7813 if (contains_dollar_at && has_dollar_at)
7814 *contains_dollar_at = 1;
7816 if (tword && (tword->flags & W_HASQUOTEDNULL))
7817 had_quoted_null = 1;
7820 dispose_word_desc (tword);
7825 case '`': /* Backquoted command substitution. */
7829 temp = string_extract (string, &sindex, "`", SX_REQMATCH);
7830 /* The test of sindex against t_index is to allow bare instances of
7831 ` to pass through, for backwards compatibility. */
7832 if (temp == &extract_string_error || temp == &extract_string_fatal)
7834 if (sindex - 1 == t_index)
7839 report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index);
7842 return ((temp == &extract_string_error) ? &expand_word_error
7843 : &expand_word_fatal);
7846 if (expanded_something)
7847 *expanded_something = 1;
7849 if (word->flags & W_NOCOMSUB)
7850 /* sindex + 1 because string[sindex] == '`' */
7851 temp1 = substring (string, t_index, sindex + 1);
7854 de_backslash (temp);
7855 tword = command_substitute (temp, quoted);
7856 temp1 = tword ? tword->word : (char *)NULL;
7858 dispose_word_desc (tword);
7862 goto dollar_add_string;
7866 if (string[sindex + 1] == '\n')
7872 c = string[++sindex];
7874 if (quoted & Q_HERE_DOCUMENT)
7876 else if (quoted & Q_DOUBLE_QUOTES)
7881 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
7883 SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
7888 sindex--; /* add_character: label increments sindex */
7893 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
7898 /* BEFORE jumping here, we need to increment sindex if appropriate */
7899 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
7900 DEFAULT_ARRAY_SIZE);
7901 istring[istring_index++] = twochars[0];
7902 istring[istring_index++] = twochars[1];
7903 istring[istring_index] = '\0';
7909 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
7911 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
7916 temp = string_extract_double_quoted (string, &sindex, 0);
7918 /* If the quotes surrounded the entire string, then the
7919 whole word was quoted. */
7920 quoted_state = (t_index == 1 && string[sindex] == '\0')
7926 tword = alloc_word_desc ();
7929 temp = (char *)NULL;
7932 /* Need to get W_HASQUOTEDNULL flag through this function. */
7933 list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &has_dollar_at, (int *)NULL);
7935 if (list == &expand_word_error || list == &expand_word_fatal)
7939 /* expand_word_internal has already freed temp_word->word
7940 for us because of the way it prints error messages. */
7941 tword->word = (char *)NULL;
7942 dispose_word (tword);
7946 dispose_word (tword);
7948 /* "$@" (a double-quoted dollar-at) expands into nothing,
7949 not even a NULL word, when there are no positional
7951 if (list == 0 && has_dollar_at)
7957 /* If we get "$@", we know we have expanded something, so we
7958 need to remember it for the final split on $IFS. This is
7959 a special case; it's the only case where a quoted string
7960 can expand into more than one word. It's going to come back
7961 from the above call to expand_word_internal as a list with
7962 a single word, in which all characters are quoted and
7963 separated by blanks. What we want to do is to turn it back
7964 into a list for the next piece of code. */
7966 dequote_list (list);
7968 if (list && list->word && (list->word->flags & W_HASQUOTEDNULL))
7969 had_quoted_null = 1;
7974 if (contains_dollar_at)
7975 *contains_dollar_at = 1;
7976 if (expanded_something)
7977 *expanded_something = 1;
7982 /* What we have is "". This is a minor optimization. */
7984 list = (WORD_LIST *)NULL;
7987 /* The code above *might* return a list (consider the case of "$@",
7988 where it returns "$1", "$2", etc.). We can't throw away the
7989 rest of the list, and we have to make sure each word gets added
7990 as quoted. We test on tresult->next: if it is non-NULL, we
7991 quote the whole list, save it to a string with string_list, and
7992 add that string. We don't need to quote the results of this
7993 (and it would be wrong, since that would quote the separators
7994 as well), so we go directly to add_string. */
8000 if (quoted_dollar_at && word->flags & W_NOSPLIT2)
8001 temp = string_list_internal (quote_list (list), " ");
8004 /* Testing quoted_dollar_at makes sure that "$@" is
8005 split correctly when $IFS does not contain a space. */
8006 temp = quoted_dollar_at
8007 ? string_list_dollar_at (list, Q_DOUBLE_QUOTES)
8008 : string_list (quote_list (list));
8009 dispose_words (list);
8014 temp = savestring (list->word->word);
8015 tflag = list->word->flags;
8016 dispose_words (list);
8018 /* If the string is not a quoted null string, we want
8019 to remove any embedded unquoted CTLNUL characters.
8020 We do not want to turn quoted null strings back into
8021 the empty string, though. We do this because we
8022 want to remove any quoted nulls from expansions that
8023 contain other characters. For example, if we have
8024 x"$*"y or "x$*y" and there are no positional parameters,
8025 the $* should expand into nothing. */
8026 /* We use the W_HASQUOTEDNULL flag to differentiate the
8027 cases: a quoted null character as above and when
8028 CTLNUL is contained in the (non-null) expansion
8029 of some variable. We use the had_quoted_null flag to
8030 pass the value through this function to its caller. */
8031 if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0)
8032 remove_quoted_nulls (temp); /* XXX */
8036 temp = (char *)NULL;
8038 /* We do not want to add quoted nulls to strings that are only
8039 partially quoted; we can throw them away. */
8040 if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
8048 temp = quote_string (temp);
8056 sindex--; /* add_character: label increments sindex */
8064 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
8066 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
8071 temp = string_extract_single_quoted (string, &sindex);
8073 /* If the entire STRING was surrounded by single quotes,
8074 then the string is wholly quoted. */
8075 quoted_state = (t_index == 1 && string[sindex] == '\0')
8079 /* If all we had was '', it is a null expansion. */
8083 temp = (char *)NULL;
8086 remove_quoted_escapes (temp); /* ??? */
8088 /* We do not want to add quoted nulls to strings that are only
8089 partially quoted; such nulls are discarded. */
8090 if (temp == 0 && (quoted_state == PARTIALLY_QUOTED))
8093 /* If we have a quoted null expansion, add a quoted NULL to istring. */
8097 sindex--; /* add_character: label increments sindex */
8101 goto add_quoted_string;
8106 /* This is the fix for " $@ " */
8108 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c)))
8110 if (string[sindex]) /* from old goto dollar_add_string */
8119 #if HANDLE_MULTIBYTE
8125 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
8130 twochars[0] = CTLESC;
8137 SADD_MBCHAR (temp, string, sindex, string_size);
8140 RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
8141 DEFAULT_ARRAY_SIZE);
8142 istring[istring_index++] = c;
8143 istring[istring_index] = '\0';
8145 /* Next character. */
8150 finished_with_string:
8151 /* OK, we're ready to return. If we have a quoted string, and
8152 quoted_dollar_at is not set, we do no splitting at all; otherwise
8153 we split on ' '. The routines that call this will handle what to
8154 do if nothing has been expanded. */
8156 /* Partially and wholly quoted strings which expand to the empty
8157 string are retained as an empty arguments. Unquoted strings
8158 which expand to the empty string are discarded. The single
8159 exception is the case of expanding "$@" when there are no
8160 positional parameters. In that case, we discard the expansion. */
8162 /* Because of how the code that handles "" and '' in partially
8163 quoted strings works, we need to make ISTRING into a QUOTED_NULL
8164 if we saw quoting characters, but the expansion was empty.
8165 "" and '' are tossed away before we get to this point when
8166 processing partially quoted strings. This makes "" and $xxx""
8167 equivalent when xxx is unset. We also look to see whether we
8168 saw a quoted null from a ${} expansion and add one back if we
8171 /* If we expand to nothing and there were no single or double quotes
8172 in the word, we throw it away. Otherwise, we return a NULL word.
8173 The single exception is for $@ surrounded by double quotes when
8174 there are no positional parameters. In that case, we also throw
8177 if (*istring == '\0')
8179 if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
8181 istring[0] = CTLNUL;
8183 tword = make_bare_word (istring);
8184 tword->flags |= W_HASQUOTEDNULL; /* XXX */
8185 list = make_word_list (tword, (WORD_LIST *)NULL);
8186 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8187 tword->flags |= W_QUOTED;
8189 /* According to sh, ksh, and Posix.2, if a word expands into nothing
8190 and a double-quoted "$@" appears anywhere in it, then the entire
8192 else if (quoted_state == UNQUOTED || quoted_dollar_at)
8193 list = (WORD_LIST *)NULL;
8197 tword = make_bare_word (istring);
8198 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8199 tword->flags |= W_QUOTED;
8200 list = make_word_list (tword, (WORD_LIST *)NULL);
8204 list = (WORD_LIST *)NULL;
8207 else if (word->flags & W_NOSPLIT)
8209 tword = make_bare_word (istring);
8210 if (word->flags & W_ASSIGNMENT)
8211 tword->flags |= W_ASSIGNMENT; /* XXX */
8212 if (word->flags & W_COMPASSIGN)
8213 tword->flags |= W_COMPASSIGN; /* XXX */
8214 if (word->flags & W_NOGLOB)
8215 tword->flags |= W_NOGLOB; /* XXX */
8216 if (word->flags & W_NOEXPAND)
8217 tword->flags |= W_NOEXPAND; /* XXX */
8218 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8219 tword->flags |= W_QUOTED;
8220 if (had_quoted_null)
8221 tword->flags |= W_HASQUOTEDNULL;
8222 list = make_word_list (tword, (WORD_LIST *)NULL);
8228 ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
8230 /* If we have $@, we need to split the results no matter what. If
8231 IFS is unset or NULL, string_list_dollar_at has separated the
8232 positional parameters with a space, so we split on space (we have
8233 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
8234 string_list_dollar_at has separated the positional parameters
8235 with the first character of $IFS, so we split on $IFS. */
8236 if (has_dollar_at && ifs_chars)
8237 list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
8240 tword = make_bare_word (istring);
8241 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
8242 tword->flags |= W_QUOTED;
8243 if (word->flags & W_ASSIGNMENT)
8244 tword->flags |= W_ASSIGNMENT;
8245 if (word->flags & W_COMPASSIGN)
8246 tword->flags |= W_COMPASSIGN;
8247 if (word->flags & W_NOGLOB)
8248 tword->flags |= W_NOGLOB;
8249 if (word->flags & W_NOEXPAND)
8250 tword->flags |= W_NOEXPAND;
8251 if (had_quoted_null)
8252 tword->flags |= W_HASQUOTEDNULL; /* XXX */
8253 list = make_word_list (tword, (WORD_LIST *)NULL);
8261 /* **************************************************************** */
8263 /* Functions for Quote Removal */
8265 /* **************************************************************** */
8267 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
8268 backslash quoting rules for within double quotes or a here document. */
8270 string_quote_removal (string, quoted)
8275 char *r, *result_string, *temp, *send;
8276 int sindex, tindex, dquote;
8280 /* The result can be no longer than the original string. */
8281 slen = strlen (string);
8282 send = string + slen;
8284 r = result_string = (char *)xmalloc (slen + 1);
8286 for (dquote = sindex = 0; c = string[sindex];)
8291 c = string[++sindex];
8297 if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
8302 SCOPY_CHAR_M (r, string, send, sindex);
8306 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
8312 tindex = sindex + 1;
8313 temp = string_extract_single_quoted (string, &tindex);
8324 dquote = 1 - dquote;
8330 return (result_string);
8335 /* Perform quote removal on word WORD. This allocates and returns a new
8338 word_quote_removal (word, quoted)
8345 t = string_quote_removal (word->word, quoted);
8346 w = alloc_word_desc ();
8347 w->word = t ? t : savestring ("");
8351 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
8352 the members of the list are treated as if they are surrounded by
8353 double quotes. Return a new list, or NULL if LIST is NULL. */
8355 word_list_quote_removal (list, quoted)
8359 WORD_LIST *result, *t, *tresult, *e;
8361 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
8363 tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
8365 result = (WORD_LIST *) list_append (result, tresult);
8368 result = e = tresult;
8381 /*******************************************
8383 * Functions to perform word splitting *
8385 *******************************************/
8395 ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n";
8397 /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
8398 handle multibyte chars in IFS */
8399 memset (ifs_cmap, '\0', sizeof (ifs_cmap));
8400 for (t = ifs_value ; t && *t; t++)
8406 #if defined (HANDLE_MULTIBYTE)
8409 ifs_firstc[0] = '\0';
8415 ifs_len = strnlen (ifs_value, MB_CUR_MAX);
8416 ifs_firstc_len = MBLEN (ifs_value, ifs_len);
8417 if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len))
8419 ifs_firstc[0] = ifs_value[0];
8420 ifs_firstc[1] = '\0';
8424 memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
8427 ifs_firstc = ifs_value ? *ifs_value : 0;
8437 /* This splits a single word into a WORD LIST on $IFS, but only if the word
8438 is not quoted. list_string () performs quote removal for us, even if we
8439 don't do any splitting. */
8441 word_split (w, ifs_chars)
8451 xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
8452 result = list_string (w->word, xifs, w->flags & W_QUOTED);
8455 result = (WORD_LIST *)NULL;
8460 /* Perform word splitting on LIST and return the RESULT. It is possible
8461 to return (WORD_LIST *)NULL. */
8463 word_list_split (list)
8466 WORD_LIST *result, *t, *tresult, *e;
8468 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
8470 tresult = word_split (t->word, ifs_value);
8472 result = e = tresult;
8483 /**************************************************
8485 * Functions to expand an entire WORD_LIST *
8487 **************************************************/
8489 /* Do any word-expansion-specific cleanup and jump to top_level */
8491 exp_jump_to_top_level (v)
8494 set_pipestatus_from_exit (last_command_exit_value);
8496 /* Cleanup code goes here. */
8497 expand_no_split_dollar_star = 0; /* XXX */
8498 expanding_redir = 0;
8499 assigning_in_environment = 0;
8501 if (parse_and_execute_level == 0)
8502 top_level_cleanup (); /* from sig.c */
8504 jump_to_top_level (v);
8507 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
8508 ELIST, and set ELIST to the new list. */
8509 #define PREPEND_LIST(nlist, elist) \
8510 do { nlist->next = elist; elist = nlist; } while (0)
8512 /* Separate out any initial variable assignments from TLIST. If set -k has
8513 been executed, remove all assignment statements from TLIST. Initial
8514 variable assignments and other environment assignments are placed
8515 on SUBST_ASSIGN_VARLIST. */
8517 separate_out_assignments (tlist)
8520 register WORD_LIST *vp, *lp;
8523 return ((WORD_LIST *)NULL);
8525 if (subst_assign_varlist)
8526 dispose_words (subst_assign_varlist); /* Clean up after previous error */
8528 subst_assign_varlist = (WORD_LIST *)NULL;
8531 /* Separate out variable assignments at the start of the command.
8532 Loop invariant: vp->next == lp
8534 lp = list of words left after assignment statements skipped
8535 tlist = original list of words
8537 while (lp && (lp->word->flags & W_ASSIGNMENT))
8543 /* If lp != tlist, we have some initial assignment statements.
8544 We make SUBST_ASSIGN_VARLIST point to the list of assignment
8545 words and TLIST point to the remaining words. */
8548 subst_assign_varlist = tlist;
8549 /* ASSERT(vp->next == lp); */
8550 vp->next = (WORD_LIST *)NULL; /* terminate variable list */
8551 tlist = lp; /* remainder of word list */
8554 /* vp == end of variable list */
8555 /* tlist == remainder of original word list without variable assignments */
8557 /* All the words in tlist were assignment statements */
8558 return ((WORD_LIST *)NULL);
8560 /* ASSERT(tlist != NULL); */
8561 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
8563 /* If the -k option is in effect, we need to go through the remaining
8564 words, separate out the assignment words, and place them on
8565 SUBST_ASSIGN_VARLIST. */
8566 if (place_keywords_in_env)
8568 WORD_LIST *tp; /* tp == running pointer into tlist */
8573 /* Loop Invariant: tp->next == lp */
8574 /* Loop postcondition: tlist == word list without assignment statements */
8577 if (lp->word->flags & W_ASSIGNMENT)
8579 /* Found an assignment statement, add this word to end of
8580 subst_assign_varlist (vp). */
8581 if (!subst_assign_varlist)
8582 subst_assign_varlist = vp = lp;
8589 /* Remove the word pointed to by LP from TLIST. */
8590 tp->next = lp->next;
8591 /* ASSERT(vp == lp); */
8592 lp->next = (WORD_LIST *)NULL;
8605 #define WEXP_VARASSIGN 0x001
8606 #define WEXP_BRACEEXP 0x002
8607 #define WEXP_TILDEEXP 0x004
8608 #define WEXP_PARAMEXP 0x008
8609 #define WEXP_PATHEXP 0x010
8611 /* All of the expansions, including variable assignments at the start of
8613 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
8615 /* All of the expansions except variable assignments at the start of
8617 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
8619 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
8620 expansion, command substitution, arithmetic expansion, word splitting, and
8622 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
8624 /* Take the list of words in LIST and do the various substitutions. Return
8625 a new list of words which is the expanded list, and without things like
8626 variable assignments. */
8632 return (expand_word_list_internal (list, WEXP_ALL));
8635 /* Same as expand_words (), but doesn't hack variable or environment
8638 expand_words_no_vars (list)
8641 return (expand_word_list_internal (list, WEXP_NOVARS));
8645 expand_words_shellexp (list)
8648 return (expand_word_list_internal (list, WEXP_SHELLEXP));
8652 glob_expand_word_list (tlist, eflags)
8656 char **glob_array, *temp_string;
8657 register int glob_index;
8658 WORD_LIST *glob_list, *output_list, *disposables, *next;
8661 output_list = disposables = (WORD_LIST *)NULL;
8662 glob_array = (char **)NULL;
8665 /* For each word, either globbing is attempted or the word is
8666 added to orig_list. If globbing succeeds, the results are
8667 added to orig_list and the word (tlist) is added to the list
8668 of disposable words. If globbing fails and failed glob
8669 expansions are left unchanged (the shell default), the
8670 original word is added to orig_list. If globbing fails and
8671 failed glob expansions are removed, the original word is
8672 added to the list of disposable words. orig_list ends up
8673 in reverse order and requires a call to REVERSE_LIST to
8674 be set right. After all words are examined, the disposable
8678 /* If the word isn't an assignment and contains an unquoted
8679 pattern matching character, then glob it. */
8680 if ((tlist->word->flags & W_NOGLOB) == 0 &&
8681 unquoted_glob_pattern_p (tlist->word->word))
8683 glob_array = shell_glob_filename (tlist->word->word);
8685 /* Handle error cases.
8686 I don't think we should report errors like "No such file
8687 or directory". However, I would like to report errors
8688 like "Read failed". */
8690 if (glob_array == 0 || GLOB_FAILED (glob_array))
8692 glob_array = (char **)xmalloc (sizeof (char *));
8693 glob_array[0] = (char *)NULL;
8696 /* Dequote the current word in case we have to use it. */
8697 if (glob_array[0] == NULL)
8699 temp_string = dequote_string (tlist->word->word);
8700 free (tlist->word->word);
8701 tlist->word->word = temp_string;
8704 /* Make the array into a word list. */
8705 glob_list = (WORD_LIST *)NULL;
8706 for (glob_index = 0; glob_array[glob_index]; glob_index++)
8708 tword = make_bare_word (glob_array[glob_index]);
8709 tword->flags |= W_GLOBEXP; /* XXX */
8710 glob_list = make_word_list (tword, glob_list);
8715 output_list = (WORD_LIST *)list_append (glob_list, output_list);
8716 PREPEND_LIST (tlist, disposables);
8718 else if (fail_glob_expansion != 0)
8720 report_error (_("no match: %s"), tlist->word->word);
8721 exp_jump_to_top_level (DISCARD);
8723 else if (allow_null_glob_expansion == 0)
8725 /* Failed glob expressions are left unchanged. */
8726 PREPEND_LIST (tlist, output_list);
8730 /* Failed glob expressions are removed. */
8731 PREPEND_LIST (tlist, disposables);
8736 /* Dequote the string. */
8737 temp_string = dequote_string (tlist->word->word);
8738 free (tlist->word->word);
8739 tlist->word->word = temp_string;
8740 PREPEND_LIST (tlist, output_list);
8743 strvec_dispose (glob_array);
8744 glob_array = (char **)NULL;
8750 dispose_words (disposables);
8753 output_list = REVERSE_LIST (output_list, WORD_LIST *);
8755 return (output_list);
8758 #if defined (BRACE_EXPANSION)
8760 brace_expand_word_list (tlist, eflags)
8764 register char **expansions;
8766 WORD_LIST *disposables, *output_list, *next;
8770 for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
8774 if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
8776 /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
8777 PREPEND_LIST (tlist, output_list);
8781 /* Only do brace expansion if the word has a brace character. If
8782 not, just add the word list element to BRACES and continue. In
8783 the common case, at least when running shell scripts, this will
8784 degenerate to a bunch of calls to `mbschr', and then what is
8785 basically a reversal of TLIST into BRACES, which is corrected
8786 by a call to REVERSE_LIST () on BRACES when the end of TLIST
8788 if (mbschr (tlist->word->word, LBRACE))
8790 expansions = brace_expand (tlist->word->word);
8792 for (eindex = 0; temp_string = expansions[eindex]; eindex++)
8794 w = make_word (temp_string);
8795 /* If brace expansion didn't change the word, preserve
8796 the flags. We may want to preserve the flags
8797 unconditionally someday -- XXX */
8798 if (STREQ (temp_string, tlist->word->word))
8799 w->flags = tlist->word->flags;
8800 output_list = make_word_list (w, output_list);
8801 free (expansions[eindex]);
8805 /* Add TLIST to the list of words to be freed after brace
8806 expansion has been performed. */
8807 PREPEND_LIST (tlist, disposables);
8810 PREPEND_LIST (tlist, output_list);
8814 dispose_words (disposables);
8817 output_list = REVERSE_LIST (output_list, WORD_LIST *);
8819 return (output_list);
8823 #if defined (ARRAY_VARS)
8824 /* Take WORD, a compound associative array assignment, and internally run
8825 'declare -A w', where W is the variable name portion of WORD. */
8827 make_internal_declare (word, option)
8835 w = make_word (word);
8837 t = assignment (w->word, 0);
8840 wl = make_word_list (w, (WORD_LIST *)NULL);
8841 wl = make_word_list (make_word (option), wl);
8843 return (declare_builtin (wl));
8848 shell_expand_word_list (tlist, eflags)
8852 WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list;
8853 int expanded_something, has_dollar_at;
8856 /* We do tilde expansion all the time. This is what 1003.2 says. */
8857 new_list = (WORD_LIST *)NULL;
8858 for (orig_list = tlist; tlist; tlist = next)
8860 temp_string = tlist->word->word;
8864 #if defined (ARRAY_VARS)
8865 /* If this is a compound array assignment to a builtin that accepts
8866 such assignments (e.g., `declare'), take the assignment and perform
8867 it separately, handling the semantics of declarations inside shell
8868 functions. This avoids the double-evaluation of such arguments,
8869 because `declare' does some evaluation of compound assignments on
8871 if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
8875 if (tlist->word->flags & W_ASSIGNASSOC)
8876 make_internal_declare (tlist->word->word, "-A");
8878 t = do_word_assignment (tlist->word);
8881 last_command_exit_value = EXECUTION_FAILURE;
8882 exp_jump_to_top_level (DISCARD);
8885 /* Now transform the word as ksh93 appears to do and go on */
8886 t = assignment (tlist->word->word, 0);
8887 tlist->word->word[t] = '\0';
8888 tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC);
8892 expanded_something = 0;
8893 expanded = expand_word_internal
8894 (tlist->word, 0, 0, &has_dollar_at, &expanded_something);
8896 if (expanded == &expand_word_error || expanded == &expand_word_fatal)
8898 /* By convention, each time this error is returned,
8899 tlist->word->word has already been freed. */
8900 tlist->word->word = (char *)NULL;
8902 /* Dispose our copy of the original list. */
8903 dispose_words (orig_list);
8904 /* Dispose the new list we're building. */
8905 dispose_words (new_list);
8907 last_command_exit_value = EXECUTION_FAILURE;
8908 if (expanded == &expand_word_error)
8909 exp_jump_to_top_level (DISCARD);
8911 exp_jump_to_top_level (FORCE_EOF);
8914 /* Don't split words marked W_NOSPLIT. */
8915 if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
8917 temp_list = word_list_split (expanded);
8918 dispose_words (expanded);
8922 /* If no parameter expansion, command substitution, process
8923 substitution, or arithmetic substitution took place, then
8924 do not do word splitting. We still have to remove quoted
8925 null characters from the result. */
8926 word_list_remove_quoted_nulls (expanded);
8927 temp_list = expanded;
8930 expanded = REVERSE_LIST (temp_list, WORD_LIST *);
8931 new_list = (WORD_LIST *)list_append (expanded, new_list);
8935 dispose_words (orig_list);
8938 new_list = REVERSE_LIST (new_list, WORD_LIST *);
8943 /* The workhorse for expand_words () and expand_words_no_vars ().
8944 First arg is LIST, a WORD_LIST of words.
8945 Second arg EFLAGS is a flags word controlling which expansions are
8948 This does all of the substitutions: brace expansion, tilde expansion,
8949 parameter expansion, command substitution, arithmetic expansion,
8950 process substitution, word splitting, and pathname expansion, according
8951 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
8952 set, or for which no expansion is done, do not undergo word splitting.
8953 Words with the W_NOGLOB bit set do not undergo pathname expansion. */
8955 expand_word_list_internal (list, eflags)
8959 WORD_LIST *new_list, *temp_list;
8963 return ((WORD_LIST *)NULL);
8965 garglist = new_list = copy_word_list (list);
8966 if (eflags & WEXP_VARASSIGN)
8968 garglist = new_list = separate_out_assignments (new_list);
8971 if (subst_assign_varlist)
8973 /* All the words were variable assignments, so they are placed
8974 into the shell's environment. */
8975 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
8977 this_command_name = (char *)NULL; /* no arithmetic errors */
8978 tint = do_word_assignment (temp_list->word);
8979 /* Variable assignment errors in non-interactive shells
8980 running in Posix.2 mode cause the shell to exit. */
8983 last_command_exit_value = EXECUTION_FAILURE;
8984 if (interactive_shell == 0 && posixly_correct)
8985 exp_jump_to_top_level (FORCE_EOF);
8987 exp_jump_to_top_level (DISCARD);
8990 dispose_words (subst_assign_varlist);
8991 subst_assign_varlist = (WORD_LIST *)NULL;
8993 return ((WORD_LIST *)NULL);
8997 /* Begin expanding the words that remain. The expansions take place on
8998 things that aren't really variable assignments. */
9000 #if defined (BRACE_EXPANSION)
9001 /* Do brace expansion on this word if there are any brace characters
9003 if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
9004 new_list = brace_expand_word_list (new_list, eflags);
9005 #endif /* BRACE_EXPANSION */
9007 /* Perform the `normal' shell expansions: tilde expansion, parameter and
9008 variable substitution, command substitution, arithmetic expansion,
9009 and word splitting. */
9010 new_list = shell_expand_word_list (new_list, eflags);
9012 /* Okay, we're almost done. Now let's just do some filename
9016 if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
9017 /* Glob expand the word list unless globbing has been disabled. */
9018 new_list = glob_expand_word_list (new_list, eflags);
9020 /* Dequote the words, because we're not performing globbing. */
9021 new_list = dequote_list (new_list);
9024 if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
9026 sh_wassign_func_t *assign_func;
9028 /* If the remainder of the words expand to nothing, Posix.2 requires
9029 that the variable and environment assignments affect the shell's
9031 assign_func = new_list ? assign_in_env : do_word_assignment;
9032 tempenv_assign_error = 0;
9034 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
9036 this_command_name = (char *)NULL;
9037 assigning_in_environment = (assign_func == assign_in_env);
9038 tint = (*assign_func) (temp_list->word);
9039 assigning_in_environment = 0;
9040 /* Variable assignment errors in non-interactive shells running
9041 in Posix.2 mode cause the shell to exit. */
9044 if (assign_func == do_word_assignment)
9046 last_command_exit_value = EXECUTION_FAILURE;
9047 if (interactive_shell == 0 && posixly_correct)
9048 exp_jump_to_top_level (FORCE_EOF);
9050 exp_jump_to_top_level (DISCARD);
9053 tempenv_assign_error++;
9057 dispose_words (subst_assign_varlist);
9058 subst_assign_varlist = (WORD_LIST *)NULL;
9062 tint = list_length (new_list) + 1;
9063 RESIZE_MALLOCED_BUFFER (glob_argv_flags, 0, tint, glob_argv_flags_size, 16);
9064 for (tint = 0, temp_list = new_list; temp_list; temp_list = temp_list->next)
9065 glob_argv_flags[tint++] = (temp_list->word->flags & W_GLOBEXP) ? '1' : '0';
9066 glob_argv_flags[tint] = '\0';