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-2005 Free Software Foundation, Inc.
9 This file is part of GNU Bash, the Bourne Again SHell.
11 Bash is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
16 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License along
22 with Bash; see the file COPYING. If not, write to the Free Software
23 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
27 #include "bashtypes.h"
29 #include "chartypes.h"
34 #if defined (HAVE_UNISTD_H)
39 #include "posixstat.h"
45 #include "execute_cmd.h"
49 #include "mailcheck.h"
53 #include "builtins/getopt.h"
54 #include "builtins/common.h"
56 #include <tilde/tilde.h>
57 #include <glob/strmatch.h>
63 /* The size that strings change by. */
64 #define DEFAULT_INITIAL_ARRAY_SIZE 112
65 #define DEFAULT_ARRAY_SIZE 128
71 #define VT_ARRAYMEMBER 3
73 #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */
75 /* Flags for quoted_strchr */
76 #define ST_BACKSL 0x01
77 #define ST_CTLESC 0x02
78 #define ST_SQUOTE 0x04 /* unused yet */
79 #define ST_DQUOTE 0x08 /* unused yet */
81 /* Flags for the string extraction functions. */
82 #define EX_NOALLOC 0x01 /* just skip; don't return substring */
83 #define EX_VARNAME 0x02 /* variable name; for string_extract () */
84 #define EX_REQMATCH 0x04 /* closing/matching delimiter required */
86 /* Flags for the `pflags' argument to param_expand() */
87 #define PF_NOCOMSUB 0x01 /* Do not perform command substitution */
89 /* These defs make it easier to use the editor. */
95 /* Evaluates to 1 if C is one of the shell's special parameters whose length
96 can be taken, but is also one of the special expansion characters. */
97 #define VALID_SPECIAL_LENGTH_PARAM(c) \
98 ((c) == '-' || (c) == '?' || (c) == '#')
100 /* Evaluates to 1 if C is one of the shell's special parameters for which an
101 indirect variable reference may be made. */
102 #define VALID_INDIR_PARAM(c) \
103 ((c) == '#' || (c) == '?' || (c) == '@' || (c) == '*')
105 /* Evaluates to 1 if C is one of the OP characters that follows the parameter
106 in ${parameter[:]OPword}. */
107 #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
109 /* Evaluates to 1 if this is one of the shell's special variables. */
110 #define SPECIAL_VAR(name, wi) \
111 ((DIGIT (*name) && all_digits (name)) || \
112 (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
113 (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))
115 /* An expansion function that takes a string and a quoted flag and returns
116 a WORD_LIST *. Used as the type of the third argument to
117 expand_string_if_necessary(). */
118 typedef WORD_LIST *EXPFUNC __P((char *, int));
120 /* Process ID of the last command executed within command substitution. */
121 pid_t last_command_subst_pid = NO_PID;
122 pid_t current_command_subst_pid = NO_PID;
124 /* Variables used to keep track of the characters in IFS. */
127 unsigned char ifs_cmap[UCHAR_MAX + 1];
129 #if defined (HANDLE_MULTIBYTE)
130 unsigned char ifs_firstc[MB_LEN_MAX];
131 size_t ifs_firstc_len;
133 unsigned char ifs_firstc;
136 /* Extern functions and variables from different files. */
137 extern int last_command_exit_value, last_command_exit_signal;
138 extern int subshell_environment;
139 extern int subshell_level;
140 extern int eof_encountered;
141 extern int return_catch_flag, return_catch_value;
142 extern pid_t dollar_dollar_pid;
143 extern int posixly_correct;
144 extern char *this_command_name;
145 extern struct fd_bitmap *current_fds_to_close;
146 extern int wordexp_only;
147 extern int expanding_redir;
148 extern int tempenv_assign_error;
150 /* Non-zero means to allow unmatched globbed filenames to expand to
152 int allow_null_glob_expansion;
154 /* Non-zero means to throw an error when globbing fails to match anything. */
155 int fail_glob_expansion;
158 /* Variables to keep track of which words in an expanded word list (the
159 output of expand_word_list_internal) are the result of globbing
160 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
161 (CURRENTLY UNUSED). */
162 char *glob_argv_flags;
163 static int glob_argv_flags_size;
166 static WORD_LIST expand_word_error, expand_word_fatal;
167 static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
168 static char expand_param_error, expand_param_fatal;
169 static char extract_string_error, extract_string_fatal;
171 /* Tell the expansion functions to not longjmp back to top_level on fatal
172 errors. Enabled when doing completion and prompt string expansion. */
173 static int no_longjmp_on_fatal_error = 0;
175 /* Set by expand_word_unsplit; used to inhibit splitting and re-joining
176 $* on $IFS, primarily when doing assignment statements. */
177 static int expand_no_split_dollar_star = 0;
179 /* Used to hold a list of variable assignments preceding a command. Global
180 so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
182 WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
184 /* A WORD_LIST of words to be expanded by expand_word_list_internal,
185 without any leading variable assignments. */
186 static WORD_LIST *garglist = (WORD_LIST *)NULL;
188 static char *quoted_substring __P((char *, int, int));
189 static int quoted_strlen __P((char *));
190 static char *quoted_strchr __P((char *, int, int));
192 static char *expand_string_if_necessary __P((char *, int, EXPFUNC *));
193 static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *));
194 static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
195 static WORD_LIST *expand_string_internal __P((char *, int));
196 static WORD_LIST *expand_string_leave_quoted __P((char *, int));
197 static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *));
199 static WORD_LIST *list_quote_escapes __P((WORD_LIST *));
200 static char *dequote_escapes __P((char *));
201 static char *make_quoted_char __P((int));
202 static WORD_LIST *quote_list __P((WORD_LIST *));
203 static WORD_LIST *dequote_list __P((WORD_LIST *));
204 static char *remove_quoted_escapes __P((char *));
205 static char *remove_quoted_nulls __P((char *));
207 static int unquoted_substring __P((char *, char *));
208 static int unquoted_member __P((int, char *));
210 #if defined (ARRAY_VARS)
211 static SHELL_VAR *do_compound_assignment __P((char *, char *, int));
213 static int do_assignment_internal __P((const WORD_DESC *, int));
215 static char *string_extract_verbatim __P((char *, size_t, int *, char *));
216 static char *string_extract __P((char *, int *, char *, int));
217 static char *string_extract_double_quoted __P((char *, int *, int));
218 static inline char *string_extract_single_quoted __P((char *, int *));
219 static inline int skip_single_quoted __P((char *, size_t, int));
220 static int skip_double_quoted __P((char *, size_t, int));
221 static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int));
222 static char *extract_dollar_brace_string __P((char *, int *, int, int));
224 static char *pos_params __P((char *, int, int, int));
226 static unsigned char *mb_getcharlens __P((char *, int));
228 static char *remove_upattern __P((char *, char *, int));
229 #if defined (HANDLE_MULTIBYTE)
230 # if !defined (HAVE_WCSDUP)
231 static wchar_t *wcsdup __P((wchar_t *));
233 static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int));
235 static char *remove_pattern __P((char *, char *, int));
237 static int match_pattern_char __P((char *, char *));
238 static int match_upattern __P((char *, char *, int, char **, char **));
239 #if defined (HANDLE_MULTIBYTE)
240 static int match_pattern_wchar __P((wchar_t *, wchar_t *));
241 static int match_wpattern __P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
243 static int match_pattern __P((char *, char *, int, char **, char **));
244 static int getpatspec __P((int, char *));
245 static char *getpattern __P((char *, int, int));
246 static char *variable_remove_pattern __P((char *, char *, int, int));
247 static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int));
248 static char *parameter_list_remove_pattern __P((int, char *, int, int));
250 static char *array_remove_pattern __P((ARRAY *, char *, int, char *, int));
252 static char *parameter_brace_remove_pattern __P((char *, char *, char *, int, int));
254 static char *process_substitute __P((char *, int));
256 static char *read_comsub __P((int, int));
259 static arrayind_t array_length_reference __P((char *));
262 static int valid_brace_expansion_word __P((char *, int));
263 static int chk_atstar __P((char *, int, int *, int *));
265 static WORD_DESC *parameter_brace_expand_word __P((char *, int, int));
266 static WORD_DESC *parameter_brace_expand_indir __P((char *, int, int, int *, int *));
267 static WORD_DESC *parameter_brace_expand_rhs __P((char *, char *, int, int, int *, int *));
268 static void parameter_brace_expand_error __P((char *, char *));
270 static int valid_length_expression __P((char *));
271 static intmax_t parameter_brace_expand_length __P((char *));
273 static char *skiparith __P((char *, int));
274 static int verify_substring_values __P((char *, char *, int, intmax_t *, intmax_t *));
275 static int get_var_and_type __P((char *, char *, int, SHELL_VAR **, char **));
276 static char *mb_substring __P((char *, int, int));
277 static char *parameter_brace_substring __P((char *, char *, char *, int));
279 static char *pos_params_pat_subst __P((char *, char *, char *, int));
281 static char *parameter_brace_patsub __P((char *, char *, char *, int));
283 static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int *, int *));
284 static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
286 static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
288 static WORD_LIST *word_list_split __P((WORD_LIST *));
290 static void exp_jump_to_top_level __P((int));
292 static WORD_LIST *separate_out_assignments __P((WORD_LIST *));
293 static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int));
294 #ifdef BRACE_EXPANSION
295 static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int));
297 static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
298 static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
300 /* **************************************************************** */
302 /* Utility Functions */
304 /* **************************************************************** */
306 #ifdef INCLUDE_UNUSED
308 quoted_substring (string, start, end)
313 register char *result, *s, *r;
317 /* Move to string[start], skipping quoted characters. */
318 for (s = string, l = 0; *s && l < start; )
330 r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */
332 /* Copy LEN characters, including quote characters. */
334 for (l = 0; l < len; s++)
348 #ifdef INCLUDE_UNUSED
349 /* Return the length of S, skipping over quoted characters */
373 /* Find the first occurrence of character C in string S, obeying shell
374 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
375 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
376 escaped with CTLESC are skipped. */
378 quoted_strchr (s, c, flags)
386 if (((flags & ST_BACKSL) && *p == '\\')
387 || ((flags & ST_CTLESC) && *p == CTLESC))
391 return ((char *)NULL);
397 return ((char *)NULL);
400 /* Return 1 if CHARACTER appears in an unquoted portion of
401 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
403 unquoted_member (character, string)
411 slen = strlen (string);
413 while (c = string[sindex])
421 ADVANCE_CHAR (string, slen, sindex);
427 ADVANCE_CHAR (string, slen, sindex);
431 sindex = skip_single_quoted (string, slen, ++sindex);
435 sindex = skip_double_quoted (string, slen, ++sindex);
442 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
444 unquoted_substring (substr, string)
445 char *substr, *string;
448 int sindex, c, sublen;
451 if (substr == 0 || *substr == '\0')
454 slen = strlen (string);
455 sublen = strlen (substr);
456 for (sindex = 0; c = string[sindex]; )
458 if (STREQN (string + sindex, substr, sublen))
467 ADVANCE_CHAR (string, slen, sindex);
471 sindex = skip_single_quoted (string, slen, ++sindex);
475 sindex = skip_double_quoted (string, slen, ++sindex);
479 ADVANCE_CHAR (string, slen, sindex);
486 /* Most of the substitutions must be done in parallel. In order
487 to avoid using tons of unclear goto's, I have some functions
488 for manipulating malloc'ed strings. They all take INDX, a
489 pointer to an integer which is the offset into the string
490 where manipulation is taking place. They also take SIZE, a
491 pointer to an integer which is the current length of the
492 character array for this string. */
494 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
495 of space allocated to TARGET. SOURCE can be NULL, in which
496 case nothing happens. Gets rid of SOURCE by freeing it.
497 Returns TARGET in case the location has changed. */
499 sub_append_string (source, target, indx, size)
500 char *source, *target;
507 srclen = STRLEN (source);
508 if (srclen >= (int)(*size - *indx))
511 n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
512 target = (char *)xrealloc (target, (*size = n));
515 FASTCOPY (source, target + *indx, srclen);
517 target[*indx] = '\0';
526 /* Append the textual representation of NUMBER to TARGET.
527 INDX and SIZE are as in SUB_APPEND_STRING. */
529 sub_append_number (number, target, indx, size)
536 temp = itos (number);
537 return (sub_append_string (temp, target, indx, size));
541 /* Extract a substring from STRING, starting at SINDEX and ending with
542 one of the characters in CHARLIST. Don't make the ending character
543 part of the string. Leave SINDEX pointing at the ending character.
544 Understand about backslashes in the string. If (flags & EX_VARNAME)
545 is non-zero, and array variables have been compiled into the shell,
546 everything between a `[' and a corresponding `]' is skipped over.
547 If (flags & EX_NOALLOC) is non-zero, don't return the substring, just
548 update SINDEX. If (flags & EX_REQMATCH) is non-zero, the string must
549 contain a closing character from CHARLIST. */
551 string_extract (string, sindex, charlist, flags)
563 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
566 while (c = string[i])
575 #if defined (ARRAY_VARS)
576 else if ((flags & EX_VARNAME) && c == '[')
579 /* If this is an array subscript, skip over it and continue. */
580 ni = skipsubscript (string, i);
581 if (string[ni] == ']')
585 else if (MEMBER (c, charlist))
591 ADVANCE_CHAR (string, slen, i);
594 /* If we had to have a matching delimiter and didn't find one, return an
595 error and let the caller deal with it. */
596 if ((flags & EX_REQMATCH) && found == 0)
599 return (&extract_string_error);
602 temp = (flags & EX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
608 /* Extract the contents of STRING as if it is enclosed in double quotes.
609 SINDEX, when passed in, is the offset of the character immediately
610 following the opening double quote; on exit, SINDEX is left pointing after
611 the closing double quote. If STRIPDQ is non-zero, unquoted double
612 quotes are stripped and the string is terminated by a null byte.
613 Backslashes between the embedded double quotes are processed. If STRIPDQ
614 is zero, an unquoted `"' terminates the string. */
616 string_extract_double_quoted (string, sindex, stripdq)
618 int *sindex, stripdq;
624 char *temp, *ret; /* The new string we return. */
625 int pass_next, backquote, si; /* State variables for the machine. */
629 slen = strlen (string + *sindex) + *sindex;
630 send = string + slen;
632 pass_next = backquote = dquote = 0;
633 temp = (char *)xmalloc (1 + slen - *sindex);
637 while (c = string[i])
639 /* Process a character that was quoted by a backslash. */
644 ``The backslash shall retain its special meaning as an escape
645 character only when followed by one of the characters:
648 If STRIPDQ is zero, we handle the double quotes here and let
649 expand_word_internal handle the rest. If STRIPDQ is non-zero,
650 we have already been through one round of backslash stripping,
651 and want to strip these backslashes only if DQUOTE is non-zero,
652 indicating that we are inside an embedded double-quoted string. */
654 /* If we are in an embedded quoted string, then don't strip
655 backslashes before characters for which the backslash
656 retains its special meaning, but remove backslashes in
657 front of other characters. If we are not in an
658 embedded quoted string, don't strip backslashes at all.
659 This mess is necessary because the string was already
660 surrounded by double quotes (and sh has some really weird
662 The returned string will be run through expansion as if
663 it were double-quoted. */
664 if ((stripdq == 0 && c != '"') ||
665 (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
670 COPY_CHAR_I (temp, j, string, send, i);
674 /* A backslash protects the next character. The code just above
675 handles preserving the backslash in front of any character but
684 /* Inside backquotes, ``the portion of the quoted string from the
685 initial backquote and the characters up to the next backquote
686 that is not preceded by a backslash, having escape characters
687 removed, defines that command''. */
705 /* Pass everything between `$(' and the matching `)' or a quoted
706 ${ ... } pair through according to the Posix.2 specification. */
707 if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
712 if (string[i + 1] == LPAREN)
713 ret = extract_delimited_string (string, &si, "$(", "(", ")", 0); /*)*/
715 ret = extract_dollar_brace_string (string, &si, 1, 0);
718 temp[j++] = string[i + 1];
720 /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
722 if (ret == 0 && no_longjmp_on_fatal_error)
725 ret = string + i + 2;
728 for (t = 0; ret[t]; t++, j++)
730 temp[j] = string[si];
745 /* Add any character but a double quote to the quoted string we're
748 goto add_one_character;
762 /* Point to after the closing quote. */
770 /* This should really be another option to string_extract_double_quoted. */
772 skip_double_quoted (string, slen, sind)
779 int pass_next, backquote, si;
782 pass_next = backquote = 0;
784 while (c = string[i])
789 ADVANCE_CHAR (string, slen, i);
802 ADVANCE_CHAR (string, slen, i);
811 else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
814 if (string[i + 1] == LPAREN)
815 ret = extract_delimited_string (string, &si, "$(", "(", ")", EX_NOALLOC); /* ) */
817 ret = extract_dollar_brace_string (string, &si, 0, EX_NOALLOC);
824 ADVANCE_CHAR (string, slen, i);
837 /* Extract the contents of STRING as if it is enclosed in single quotes.
838 SINDEX, when passed in, is the offset of the character immediately
839 following the opening single quote; on exit, SINDEX is left pointing after
840 the closing single quote. */
842 string_extract_single_quoted (string, sindex)
851 /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
852 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
854 while (string[i] && string[i] != '\'')
855 ADVANCE_CHAR (string, slen, i);
857 t = substring (string, *sindex, i);
867 skip_single_quoted (string, slen, sind)
876 while (string[c] && string[c] != '\'')
877 ADVANCE_CHAR (string, slen, c);
884 /* Just like string_extract, but doesn't hack backslashes or any of
885 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
887 string_extract_verbatim (string, slen, sindex, charlist)
893 register int i = *sindex;
894 #if defined (HANDLE_MULTIBYTE)
902 if (charlist[0] == '\'' && charlist[1] == '\0')
904 temp = string_extract_single_quoted (string, sindex);
905 --*sindex; /* leave *sindex at separator character */
911 /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need
912 this only if MB_CUR_MAX > 1. */
913 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 1;
915 #if defined (HANDLE_MULTIBYTE)
916 clen = strlen (charlist);
919 while (c = string[i])
921 #if defined (HANDLE_MULTIBYTE)
930 #if defined (HANDLE_MULTIBYTE)
931 mblength = MBLEN (string + i, slen - i);
935 mblength = mbtowc (&wc, string + i, slen - i);
936 if (MB_INVALIDCH (mblength))
938 if (MEMBER (c, charlist))
946 len = mbstowcs (wcharlist, charlist, 0);
949 wcharlist = xmalloc ((sizeof (wchar_t) * len) + 1);
950 mbstowcs (wcharlist, charlist, len);
953 if (wcschr (wcharlist, wc))
959 if (MEMBER (c, charlist))
962 ADVANCE_CHAR (string, slen, i);
965 #if defined (HANDLE_MULTIBYTE)
969 temp = substring (string, *sindex, i);
975 /* Extract the $( construct in STRING, and return a new string.
976 Start extracting at (SINDEX) as if we had just seen "$(".
977 Make (SINDEX) get the position of the matching ")". ) */
979 extract_command_subst (string, sindex)
983 return (extract_delimited_string (string, sindex, "$(", "(", ")", 0)); /*)*/
986 /* Extract the $[ construct in STRING, and return a new string. (])
987 Start extracting at (SINDEX) as if we had just seen "$[".
988 Make (SINDEX) get the position of the matching "]". */
990 extract_arithmetic_subst (string, sindex)
994 return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/
997 #if defined (PROCESS_SUBSTITUTION)
998 /* Extract the <( or >( construct in STRING, and return a new string.
999 Start extracting at (SINDEX) as if we had just seen "<(".
1000 Make (SINDEX) get the position of the matching ")". */ /*))*/
1002 extract_process_subst (string, starter, sindex)
1007 return (extract_delimited_string (string, sindex, starter, "(", ")", 0));
1009 #endif /* PROCESS_SUBSTITUTION */
1011 #if defined (ARRAY_VARS)
1012 /* This can be fooled by unquoted right parens in the passed string. If
1013 each caller verifies that the last character in STRING is a right paren,
1014 we don't even need to call extract_delimited_string. */
1016 extract_array_assignment_list (string, sindex)
1023 slen = strlen (string); /* ( */
1024 if (string[slen - 1] == ')')
1026 ret = substring (string, *sindex, slen - 1);
1034 /* Extract and create a new string from the contents of STRING, a
1035 character string delimited with OPENER and CLOSER. SINDEX is
1036 the address of an int describing the current offset in STRING;
1037 it should point to just after the first OPENER found. On exit,
1038 SINDEX gets the position of the last character of the matching CLOSER.
1039 If OPENER is more than a single character, ALT_OPENER, if non-null,
1040 contains a character string that can also match CLOSER and thus
1041 needs to be skipped. */
1043 extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
1046 char *opener, *alt_opener, *closer;
1052 int pass_character, nesting_level;
1053 int len_closer, len_opener, len_alt_opener;
1056 slen = strlen (string + *sindex) + *sindex;
1057 len_opener = STRLEN (opener);
1058 len_alt_opener = STRLEN (alt_opener);
1059 len_closer = STRLEN (closer);
1066 while (nesting_level)
1073 if (pass_character) /* previous char was backslash */
1076 ADVANCE_CHAR (string, slen, i);
1080 if (c == CTLESC || c == '\\')
1087 /* Process a nested OPENER. */
1088 if (STREQN (string + i, opener, len_opener))
1090 si = i + len_opener;
1091 t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|EX_NOALLOC);
1096 /* Process a nested ALT_OPENER */
1097 if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
1099 si = i + len_alt_opener;
1100 t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|EX_NOALLOC);
1105 /* If the current substring terminates the delimited string, decrement
1106 the nesting level. */
1107 if (STREQN (string + i, closer, len_closer))
1109 i += len_closer - 1; /* move to last byte of the closer */
1111 if (nesting_level == 0)
1115 /* Pass old-style command substitution through verbatim. */
1119 t = string_extract (string, &si, "`", flags|EX_NOALLOC);
1124 /* Pass single-quoted and double-quoted strings through verbatim. */
1125 if (c == '\'' || c == '"')
1128 i = (c == '\'') ? skip_single_quoted (string, slen, si)
1129 : skip_double_quoted (string, slen, si);
1133 /* move past this character, which was not special. */
1134 ADVANCE_CHAR (string, slen, i);
1137 if (c == 0 && nesting_level)
1139 if (no_longjmp_on_fatal_error == 0)
1141 report_error (_("bad substitution: no closing `%s' in %s"), closer, string);
1142 last_command_exit_value = EXECUTION_FAILURE;
1143 exp_jump_to_top_level (DISCARD);
1148 return (char *)NULL;
1152 si = i - *sindex - len_closer + 1;
1153 if (flags & EX_NOALLOC)
1154 result = (char *)NULL;
1157 result = (char *)xmalloc (1 + si);
1158 strncpy (result, string + *sindex, si);
1166 /* Extract a parameter expansion expression within ${ and } from STRING.
1167 Obey the Posix.2 rules for finding the ending `}': count braces while
1168 skipping over enclosed quoted strings and command substitutions.
1169 SINDEX is the address of an int describing the current offset in STRING;
1170 it should point to just after the first `{' found. On exit, SINDEX
1171 gets the position of the matching `}'. QUOTED is non-zero if this
1172 occurs inside double quotes. */
1173 /* XXX -- this is very similar to extract_delimited_string -- XXX */
1175 extract_dollar_brace_string (string, sindex, quoted, flags)
1177 int *sindex, quoted, flags;
1181 int pass_character, nesting_level, si;
1187 slen = strlen (string + *sindex) + *sindex;
1190 while (c = string[i])
1195 ADVANCE_CHAR (string, slen, i);
1199 /* CTLESCs and backslashes quote the next character. */
1200 if (c == CTLESC || c == '\\')
1207 if (string[i] == '$' && string[i+1] == LBRACE)
1217 if (nesting_level == 0)
1223 /* Pass the contents of old-style command substitutions through
1228 t = string_extract (string, &si, "`", flags|EX_NOALLOC);
1233 /* Pass the contents of new-style command substitutions and
1234 arithmetic substitutions through verbatim. */
1235 if (string[i] == '$' && string[i+1] == LPAREN)
1238 t = extract_delimited_string (string, &si, "$(", "(", ")", flags|EX_NOALLOC); /*)*/
1243 /* Pass the contents of single-quoted and double-quoted strings
1244 through verbatim. */
1245 if (c == '\'' || c == '"')
1248 i = (c == '\'') ? skip_single_quoted (string, slen, si)
1249 : skip_double_quoted (string, slen, si);
1250 /* skip_XXX_quoted leaves index one past close quote */
1254 /* move past this character, which was not special. */
1255 ADVANCE_CHAR (string, slen, i);
1258 if (c == 0 && nesting_level)
1260 if (no_longjmp_on_fatal_error == 0)
1262 report_error ("bad substitution: no closing `%s' in %s", "}", string);
1263 last_command_exit_value = EXECUTION_FAILURE;
1264 exp_jump_to_top_level (DISCARD);
1269 return ((char *)NULL);
1273 result = (flags & EX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
1279 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1280 STRING, and returns a pointer to it. */
1282 de_backslash (string)
1285 register size_t slen;
1286 register int i, j, prev_i;
1289 slen = strlen (string);
1292 /* Loop copying string[i] to string[j], i >= j. */
1295 if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
1296 string[i + 1] == '$'))
1299 ADVANCE_CHAR (string, slen, i);
1301 do string[j++] = string[prev_i++]; while (prev_i < i);
1312 /* Replace instances of \! in a string with !. */
1314 unquote_bang (string)
1318 register char *temp;
1320 temp = (char *)xmalloc (1 + strlen (string));
1322 for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
1324 if (string[i] == '\\' && string[i + 1] == '!')
1330 strcpy (string, temp);
1335 #if defined (READLINE)
1336 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
1337 an unclosed quoted string), or if the character at EINDEX is quoted
1338 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
1339 single and double-quoted string parsing functions should not return an
1340 error if there are unclosed quotes or braces. The characters that this
1341 recognizes need to be the same as the contents of
1342 rl_completer_quote_characters. */
1344 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0)
1347 char_is_quoted (string, eindex)
1351 int i, pass_next, c;
1355 slen = strlen (string);
1356 no_longjmp_on_fatal_error = 1;
1365 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
1367 ADVANCE_CHAR (string, slen, i);
1376 else if (c == '\'' || c == '"')
1378 i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
1379 : skip_double_quoted (string, slen, ++i);
1382 /* no increment, the skip_xxx functions go one past end */
1385 ADVANCE_CHAR (string, slen, i);
1392 unclosed_pair (string, eindex, openstr)
1397 int i, pass_next, openc, olen;
1401 slen = strlen (string);
1402 olen = strlen (openstr);
1403 i = pass_next = openc = 0;
1409 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
1411 ADVANCE_CHAR (string, slen, i);
1414 else if (string[i] == '\\')
1420 else if (STREQN (string + i, openstr, olen))
1425 else if (string[i] == '\'' || string[i] == '"')
1427 i = (string[i] == '\'') ? skip_single_quoted (string, slen, i)
1428 : skip_double_quoted (string, slen, i);
1433 ADVANCE_CHAR (string, slen, i);
1438 /* Skip characters in STRING until we find a character in DELIMS, and return
1439 the index of that character. START is the index into string at which we
1440 begin. This is similar in spirit to strpbrk, but it returns an index into
1441 STRING and takes a starting index. This little piece of code knows quite
1442 a lot of shell syntax. It's very similar to skip_double_quoted and other
1443 functions of that ilk. */
1445 skip_to_delim (string, start, delims)
1450 int i, pass_next, backq, si, c;
1455 slen = strlen (string + start) + start;
1456 no_longjmp_on_fatal_error = 1;
1458 pass_next = backq = 0;
1459 while (c = string[i])
1466 ADVANCE_CHAR (string, slen, i);
1479 ADVANCE_CHAR (string, slen, i);
1488 else if (c == '\'' || c == '"')
1490 i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
1491 : skip_double_quoted (string, slen, ++i);
1492 /* no increment, the skip functions increment past the closing quote. */
1494 else if (c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
1497 if (string[si] == '\0')
1500 if (string[i+1] == LPAREN)
1501 temp = extract_delimited_string (string, &si, "$(", "(", ")", EX_NOALLOC); /* ) */
1503 temp = extract_dollar_brace_string (string, &si, 0, EX_NOALLOC);
1505 if (string[i] == '\0') /* don't increment i past EOS in loop */
1510 else if (member (c, delims))
1513 ADVANCE_CHAR (string, slen, i);
1519 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
1520 individual words. If DELIMS is NULL, the current value of $IFS is used
1521 to split the string, and the function follows the shell field splitting
1522 rules. SENTINEL is an index to look for. NWP, if non-NULL,
1523 gets the number of words in the returned list. CWP, if non-NULL, gets
1524 the index of the word containing SENTINEL. Non-whitespace chars in
1525 DELIMS delimit separate fields. */
1527 split_at_delims (string, slen, delims, sentinel, nwp, cwp)
1534 int ts, te, i, nw, cw, ifs_split;
1535 char *token, *d, *d2;
1536 WORD_LIST *ret, *tl;
1538 if (string == 0 || *string == '\0')
1544 return ((WORD_LIST *)NULL);
1547 d = (delims == 0) ? ifs_value : delims;
1548 ifs_split = delims == 0;
1550 /* Make d2 the non-whitespace characters in delims */
1555 #if defined (HANDLE_MULTIBYTE)
1556 size_t mblength = 1;
1560 slength = strlen (delims);
1561 d2 = (char *)xmalloc (slength + 1);
1565 #if defined (HANDLE_MULTIBYTE)
1566 mbstate_t state_bak = state;
1567 mblength = MBRLEN (delims + i, slength, &state);
1568 if (MB_INVALIDCH (mblength))
1570 else if (mblength > 1)
1572 memcpy (d2 + ts, delims + i, mblength);
1575 slength -= mblength;
1579 if (whitespace (delims[i]) == 0)
1580 d2[ts++] = delims[i];
1588 ret = (WORD_LIST *)NULL;
1590 /* Remove sequences of whitspace characters at the start of the string, as
1591 long as those characters are delimiters. */
1592 for (i = 0; member (string[i], d) && spctabnl (string[i]); i++)
1594 if (string[i] == '\0')
1602 te = skip_to_delim (string, ts, d);
1604 /* If we have a non-whitespace delimiter character, use it to make a
1605 separate field. This is just about what $IFS splitting does and
1606 is closer to the behavior of the shell parser. */
1607 if (ts == te && d2 && member (string[ts], d2))
1610 /* If we're using IFS splitting, the non-whitespace delimiter char
1611 and any additional IFS whitespace delimits a field. */
1613 while (member (string[te], d) && spctabnl (string[te]))
1616 while (member (string[te], d2))
1620 token = substring (string, ts, te);
1622 ret = add_string_to_list (token, ret);
1626 if (sentinel >= ts && sentinel <= te)
1629 /* If the cursor is at whitespace just before word start, set the
1630 sentinel word to the current word. */
1631 if (cwp && cw == -1 && sentinel == ts-1)
1634 /* If the cursor is at whitespace between two words, make a new, empty
1635 word, add it before (well, after, since the list is in reverse order)
1636 the word we just added, and set the current word to that one. */
1637 if (cwp && cw == -1 && sentinel < ts)
1639 tl = make_word_list (make_word (""), ret->next);
1645 if (string[te] == 0)
1649 while (member (string[i], d) && (ifs_split || spctabnl(string[i])))
1658 /* Special case for SENTINEL at the end of STRING. If we haven't found
1659 the word containing SENTINEL yet, and the index we're looking for is at
1660 the end of STRING, add an additional null argument and set the current
1661 word pointer to that. */
1662 if (cwp && cw == -1 && sentinel >= slen)
1664 if (whitespace (string[sentinel - 1]))
1667 ret = add_string_to_list (token, ret);
1678 return (REVERSE_LIST (ret, WORD_LIST *));
1680 #endif /* READLINE */
1684 /* Extract the name of the variable to bind to from the assignment string. */
1686 assignment_name (string)
1692 offset = assignment (string, 0);
1694 return (char *)NULL;
1695 temp = substring (string, 0, offset);
1700 /* **************************************************************** */
1702 /* Functions to convert strings to WORD_LISTs and vice versa */
1704 /* **************************************************************** */
1706 /* Return a single string of all the words in LIST. SEP is the separator
1707 to put between individual elements of LIST in the output string. */
1709 string_list_internal (list, sep)
1713 register WORD_LIST *t;
1715 int word_len, sep_len, result_size;
1718 return ((char *)NULL);
1720 /* Short-circuit quickly if we don't need to separate anything. */
1721 if (list->next == 0)
1722 return (savestring (list->word->word));
1724 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
1725 sep_len = STRLEN (sep);
1728 for (t = list; t; t = t->next)
1731 result_size += sep_len;
1732 result_size += strlen (t->word->word);
1735 r = result = (char *)xmalloc (result_size + 1);
1737 for (t = list; t; t = t->next)
1739 if (t != list && sep_len)
1743 FASTCOPY (sep, r, sep_len);
1750 word_len = strlen (t->word->word);
1751 FASTCOPY (t->word->word, r, word_len);
1759 /* Return a single string of all the words present in LIST, separating
1760 each word with a space. */
1765 return (string_list_internal (list, " "));
1768 /* Return a single string of all the words present in LIST, obeying the
1769 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
1770 expansion [of $*] appears within a double quoted string, it expands
1771 to a single field with the value of each parameter separated by the
1772 first character of the IFS variable, or by a <space> if IFS is unset." */
1774 string_list_dollar_star (list)
1777 #if defined (HANDLE_MULTIBYTE)
1778 char sep[MB_CUR_MAX + 1];
1784 #if defined (HANDLE_MULTIBYTE)
1785 if (ifs_firstc_len == 1)
1787 sep[0] = ifs_firstc[0];
1792 memcpy (sep, ifs_firstc, ifs_firstc_len);
1793 sep[ifs_firstc_len] = '\0';
1796 sep[0] = ifs_firstc;
1800 return (string_list_internal (list, sep));
1803 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
1804 is non-zero, the $@ appears within double quotes, and we should quote
1805 the list before converting it into a string. If IFS is unset, and the
1806 word is not quoted, we just need to quote CTLESC and CTLNUL characters
1807 in the words in the list, because the default value of $IFS is
1808 <space><tab><newline>, IFS characters in the words in the list should
1809 also be split. If IFS is null, and the word is not quoted, we need
1810 to quote the words in the list to preserve the positional parameters
1813 string_list_dollar_at (list, quoted)
1818 #if defined (HANDLE_MULTIBYTE)
1819 char sep[MB_CUR_MAX + 1];
1825 /* XXX this could just be ifs = ifs_value; */
1826 ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
1828 #if defined (HANDLE_MULTIBYTE)
1831 if (ifs_firstc_len == 1)
1833 sep[0] = ifs_firstc[0];
1838 memcpy (sep, ifs_firstc, ifs_firstc_len);
1839 sep[ifs_firstc_len] = '\0';
1848 sep[0] = (ifs == 0 || *ifs == 0) ? ' ' : *ifs;
1852 tlist = ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (ifs && *ifs == 0))
1854 : list_quote_escapes (list);
1855 return (string_list_internal (tlist, sep));
1858 /* Return the list of words present in STRING. Separate the string into
1859 words at any of the characters found in SEPARATORS. If QUOTED is
1860 non-zero then word in the list will have its quoted flag set, otherwise
1861 the quoted flag is left as make_word () deemed fit.
1863 This obeys the P1003.2 word splitting semantics. If `separators' is
1864 exactly <space><tab><newline>, then the splitting algorithm is that of
1865 the Bourne shell, which treats any sequence of characters from `separators'
1866 as a delimiter. If IFS is unset, which results in `separators' being set
1867 to "", no splitting occurs. If separators has some other value, the
1868 following rules are applied (`IFS white space' means zero or more
1869 occurrences of <space>, <tab>, or <newline>, as long as those characters
1870 are in `separators'):
1872 1) IFS white space is ignored at the start and the end of the
1874 2) Each occurrence of a character in `separators' that is not
1875 IFS white space, along with any adjacent occurrences of
1876 IFS white space delimits a field.
1877 3) Any nonzero-length sequence of IFS white space delimits a field.
1880 /* BEWARE! list_string strips null arguments. Don't call it twice and
1881 expect to have "" preserved! */
1883 /* This performs word splitting and quoted null character removal on
1886 (((separators)[0]) ? ((separators)[1] ? isifs(c) \
1887 : (c) == (separators)[0]) \
1891 list_string (string, separators, quoted)
1892 register char *string, *separators;
1897 char *current_word, *s;
1898 int sindex, sh_style_split, whitesep;
1901 if (!string || !*string)
1902 return ((WORD_LIST *)NULL);
1904 sh_style_split = separators && separators[0] == ' ' &&
1905 separators[1] == '\t' &&
1906 separators[2] == '\n' &&
1907 separators[3] == '\0';
1910 /* Remove sequences of whitespace at the beginning of STRING, as
1911 long as those characters appear in IFS. Do not do this if
1912 STRING is quoted or if there are no separator characters. */
1913 if (!quoted || !separators || !*separators)
1915 for (s = string; *s && spctabnl (*s) && issep (*s); s++);
1918 return ((WORD_LIST *)NULL);
1923 /* OK, now STRING points to a word that does not begin with white space.
1924 The splitting algorithm is:
1925 extract a word, stopping at a separator
1926 skip sequences of spc, tab, or nl as long as they are separators
1927 This obeys the field splitting rules in Posix.2. */
1928 slen = (MB_CUR_MAX > 1) ? strlen (string) : 1;
1929 for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
1931 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
1932 unless multibyte chars are possible. */
1933 current_word = string_extract_verbatim (string, slen, &sindex, separators);
1934 if (current_word == 0)
1937 /* If we have a quoted empty string, add a quoted null argument. We
1938 want to preserve the quoted null character iff this is a quoted
1939 empty string; otherwise the quoted null characters are removed
1941 if (QUOTED_NULL (current_word))
1943 t = alloc_word_desc ();
1944 t->word = make_quoted_char ('\0');
1945 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
1946 result = make_word_list (t, result);
1948 else if (current_word[0] != '\0')
1950 /* If we have something, then add it regardless. However,
1951 perform quoted null character removal on the current word. */
1952 remove_quoted_nulls (current_word);
1953 result = add_string_to_list (current_word, result);
1954 result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */
1955 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
1956 result->word->flags |= W_QUOTED;
1959 /* If we're not doing sequences of separators in the traditional
1960 Bourne shell style, then add a quoted null argument. */
1961 else if (!sh_style_split && !spctabnl (string[sindex]))
1963 t = alloc_word_desc ();
1964 t->word = make_quoted_char ('\0');
1965 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
1966 result = make_word_list (t, result);
1969 free (current_word);
1971 /* Note whether or not the separator is IFS whitespace, used later. */
1972 whitesep = string[sindex] && spctabnl (string[sindex]);
1974 /* Move past the current separator character. */
1978 ADVANCE_CHAR (string, slen, sindex);
1981 /* Now skip sequences of space, tab, or newline characters if they are
1982 in the list of separators. */
1983 while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex]))
1986 /* If the first separator was IFS whitespace and the current character
1987 is a non-whitespace IFS character, it should be part of the current
1988 field delimiter, not a separate delimiter that would result in an
1989 empty field. Look at POSIX.2, 3.6.5, (3)(b). */
1990 if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex]))
1993 /* An IFS character that is not IFS white space, along with any
1994 adjacent IFS white space, shall delimit a field. (SUSv3) */
1995 while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
1999 return (REVERSE_LIST (result, WORD_LIST *));
2002 /* Parse a single word from STRING, using SEPARATORS to separate fields.
2003 ENDPTR is set to the first character after the word. This is used by
2004 the `read' builtin. This is never called with SEPARATORS != $IFS;
2005 it should be simplified.
2007 XXX - this function is very similar to list_string; they should be
2010 get_word_from_string (stringp, separators, endptr)
2011 char **stringp, *separators, **endptr;
2015 int sindex, sh_style_split, whitesep;
2018 if (!stringp || !*stringp || !**stringp)
2019 return ((char *)NULL);
2023 sh_style_split = separators && separators[0] == ' ' &&
2024 separators[1] == '\t' &&
2025 separators[2] == '\n' &&
2026 separators[3] == '\0';
2030 /* Remove sequences of whitespace at the beginning of STRING, as
2031 long as those characters appear in IFS. */
2032 if (sh_style_split || !separators || !*separators)
2034 for (; *s && spctabnl (*s) && isifs (*s); s++);
2036 /* If the string is nothing but whitespace, update it and return. */
2042 return ((char *)NULL);
2046 /* OK, S points to a word that does not begin with white space.
2047 Now extract a word, stopping at a separator, save a pointer to
2048 the first character after the word, then skip sequences of spc,
2049 tab, or nl as long as they are separators.
2051 This obeys the field splitting rules in Posix.2. */
2053 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2054 unless multibyte chars are possible. */
2055 slen = (MB_CUR_MAX > 1) ? strlen (s) : 1;
2056 current_word = string_extract_verbatim (s, slen, &sindex, separators);
2058 /* Set ENDPTR to the first character after the end of the word. */
2060 *endptr = s + sindex;
2062 /* Note whether or not the separator is IFS whitespace, used later. */
2063 whitesep = s[sindex] && spctabnl (s[sindex]);
2065 /* Move past the current separator character. */
2069 ADVANCE_CHAR (s, slen, sindex);
2072 /* Now skip sequences of space, tab, or newline characters if they are
2073 in the list of separators. */
2074 while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
2077 /* If the first separator was IFS whitespace and the current character is
2078 a non-whitespace IFS character, it should be part of the current field
2079 delimiter, not a separate delimiter that would result in an empty field.
2080 Look at POSIX.2, 3.6.5, (3)(b). */
2081 if (s[sindex] && whitesep && isifs (s[sindex]) && !spctabnl (s[sindex]))
2084 /* An IFS character that is not IFS white space, along with any adjacent
2085 IFS white space, shall delimit a field. */
2086 while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
2090 /* Update STRING to point to the next field. */
2091 *stringp = s + sindex;
2092 return (current_word);
2095 /* Remove IFS white space at the end of STRING. Start at the end
2096 of the string and walk backwards until the beginning of the string
2097 or we find a character that's not IFS white space and not CTLESC.
2098 Only let CTLESC escape a white space character if SAW_ESCAPE is
2101 strip_trailing_ifs_whitespace (string, separators, saw_escape)
2102 char *string, *separators;
2107 s = string + STRLEN (string) - 1;
2108 while (s > string && ((spctabnl (*s) && isifs (*s)) ||
2109 (saw_escape && *s == CTLESC && spctabnl (s[1]))))
2117 /* Split STRING into words at whitespace. Obeys shell-style quoting with
2118 backslashes, single and double quotes. */
2120 list_string_with_quotes (string)
2126 int c, i, tokstart, len;
2128 for (s = string; s && *s && spctabnl (*s); s++)
2130 if (s == 0 || *s == 0)
2131 return ((WORD_LIST *)NULL);
2135 list = (WORD_LIST *)NULL;
2146 i = skip_single_quoted (s, s_len, ++i);
2148 i = skip_double_quoted (s, s_len, ++i);
2149 else if (c == 0 || spctabnl (c))
2151 /* We have found the end of a token. Make a word out of it and
2152 add it to the word list. */
2153 token = substring (s, tokstart, i);
2154 list = add_string_to_list (token, list);
2156 while (spctabnl (s[i]))
2164 i++; /* normal character */
2166 return (REVERSE_LIST (list, WORD_LIST *));
2170 /********************************************************/
2172 /* Functions to perform assignment statements */
2174 /********************************************************/
2176 #if defined (ARRAY_VARS)
2178 do_compound_assignment (name, value, flags)
2185 mklocal = flags & ASS_MKLOCAL;
2187 if (mklocal && variable_context)
2189 v = find_variable (name);
2190 if (v == 0 || array_p (v) == 0)
2191 v = make_local_array_variable (name);
2192 v = assign_array_var_from_string (v, value, flags);
2195 v = assign_array_from_string (name, value, flags);
2201 /* Given STRING, an assignment string, get the value of the right side
2202 of the `=', and bind it to the left side. If EXPAND is true, then
2203 perform parameter expansion, command substitution, and arithmetic
2204 expansion on the right-hand side. Perform tilde expansion in any
2205 case. Do not perform word splitting on the result of expansion. */
2207 do_assignment_internal (word, expand)
2208 const WORD_DESC *word;
2211 int offset, tlen, appendop, assign_list, aflags;
2212 char *name, *value, *ovalue, *nvalue;
2214 #if defined (ARRAY_VARS)
2220 if (word == 0 || word->word == 0)
2223 appendop = assign_list = aflags = 0;
2224 string = word->word;
2225 offset = assignment (string, 0);
2226 name = savestring (string);
2227 value = (char *)NULL;
2229 if (name[offset] == '=')
2233 if (name[offset - 1] == '+')
2236 name[offset - 1] = '\0';
2239 name[offset] = 0; /* might need this set later */
2240 temp = name + offset + 1;
2241 tlen = STRLEN (temp);
2243 #if defined (ARRAY_VARS)
2245 if (expand && temp[0] == LPAREN && temp[tlen-1] == RPAREN)
2247 if (expand && (word->flags & W_COMPASSIGN))
2250 assign_list = ni = 1;
2251 value = extract_array_assignment_list (temp, &ni);
2256 if (expand && temp[0])
2257 value = expand_string_if_necessary (temp, 0, expand_string_assignment);
2259 value = savestring (temp);
2264 value = (char *)xmalloc (1);
2268 if (echo_command_at_execute)
2271 name[offset - 1] = '+';
2272 xtrace_print_assignment (name, value, assign_list, 1);
2274 name[offset - 1] = '\0';
2277 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
2280 aflags |= ASS_APPEND;
2282 #if defined (ARRAY_VARS)
2283 if (t = xstrchr (name, '[')) /*]*/
2287 report_error (_("%s: cannot assign list to array member"), name);
2290 entry = assign_array_element (name, value, aflags);
2294 else if (assign_list)
2296 if (word->flags & W_ASSIGNARG)
2297 aflags |= ASS_MKLOCAL;
2298 entry = do_compound_assignment (name, value, aflags);
2301 #endif /* ARRAY_VARS */
2302 entry = bind_variable (name, value, aflags);
2304 stupidly_hack_special_variables (name);
2307 VUNSETATTR (entry, att_invisible);
2309 /* Return 1 if the assignment seems to have been performed correctly. */
2310 ASSIGN_RETURN (entry ? ((readonly_p (entry) == 0) && noassign_p (entry) == 0) : 0);
2313 /* Perform the assignment statement in STRING, and expand the
2314 right side by doing tilde, command and parameter expansion. */
2316 do_assignment (string)
2321 td.flags = W_ASSIGNMENT;
2324 return do_assignment_internal (&td, 1);
2328 do_word_assignment (word)
2331 return do_assignment_internal (word, 1);
2334 /* Given STRING, an assignment string, get the value of the right side
2335 of the `=', and bind it to the left side. Do not perform any word
2336 expansions on the right hand side. */
2338 do_assignment_no_expand (string)
2343 td.flags = W_ASSIGNMENT;
2346 return (do_assignment_internal (&td, 0));
2349 /***************************************************
2351 * Functions to manage the positional parameters *
2353 ***************************************************/
2355 /* Return the word list that corresponds to `$*'. */
2357 list_rest_of_args ()
2359 register WORD_LIST *list, *args;
2362 /* Break out of the loop as soon as one of the dollar variables is null. */
2363 for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++)
2364 list = make_word_list (make_bare_word (dollar_vars[i]), list);
2366 for (args = rest_of_args; args; args = args->next)
2367 list = make_word_list (make_bare_word (args->word->word), list);
2369 return (REVERSE_LIST (list, WORD_LIST *));
2375 register WORD_LIST *list;
2378 for (n = 0; n < 9 && dollar_vars[n+1]; n++)
2380 for (list = rest_of_args; list; list = list->next)
2385 /* Return the value of a positional parameter. This handles values > 10. */
2387 get_dollar_var_value (ind)
2394 temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL;
2395 else /* We want something like ${11} */
2398 for (p = rest_of_args; p && ind--; p = p->next)
2400 temp = p ? savestring (p->word->word) : (char *)NULL;
2405 /* Make a single large string out of the dollar digit variables,
2406 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
2407 case of "$*" with respect to IFS. */
2409 string_rest_of_args (dollar_star)
2412 register WORD_LIST *list;
2415 list = list_rest_of_args ();
2416 string = dollar_star ? string_list_dollar_star (list) : string_list (list);
2417 dispose_words (list);
2421 /* Return a string containing the positional parameters from START to
2422 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
2423 which only makes a difference if QUOTED is non-zero. If QUOTED includes
2424 Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
2425 no quoting chars are added. */
2427 pos_params (string, start, end, quoted)
2429 int start, end, quoted;
2431 WORD_LIST *save, *params, *h, *t;
2435 /* see if we can short-circuit. if start == end, we want 0 parameters. */
2437 return ((char *)NULL);
2439 save = params = list_rest_of_args ();
2441 return ((char *)NULL);
2443 for (i = 1; params && i < start; i++)
2444 params = params->next;
2446 return ((char *)NULL);
2447 for (h = t = params; params && i < end; i++)
2450 params = params->next;
2453 t->next = (WORD_LIST *)NULL;
2454 if (string[0] == '*')
2456 if (quoted & Q_DOUBLE_QUOTES)
2457 ret = string_list_dollar_star (quote_list (h));
2458 else if (quoted & Q_HERE_DOCUMENT)
2459 ret = string_list (quote_list (h));
2461 ret = string_list (h);
2464 ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (h) : h);
2468 dispose_words (save);
2472 /******************************************************************/
2474 /* Functions to expand strings to strings or WORD_LISTs */
2476 /******************************************************************/
2478 #if defined (PROCESS_SUBSTITUTION)
2479 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
2481 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
2484 /* If there are any characters in STRING that require full expansion,
2485 then call FUNC to expand STRING; otherwise just perform quote
2486 removal if necessary. This returns a new string. */
2488 expand_string_if_necessary (string, quoted, func)
2499 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
2500 slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
2504 if (EXP_CHAR (string[i]))
2506 else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
2508 ADVANCE_CHAR (string, slen, i);
2513 list = (*func) (string, quoted);
2516 ret = string_list (list);
2517 dispose_words (list);
2522 else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
2523 ret = string_quote_removal (string, quoted);
2525 ret = savestring (string);
2530 static inline char *
2531 expand_string_to_string_internal (string, quoted, func)
2539 if (string == 0 || *string == '\0')
2540 return ((char *)NULL);
2542 list = (*func) (string, quoted);
2545 ret = string_list (list);
2546 dispose_words (list);
2555 expand_string_to_string (string, quoted)
2559 return (expand_string_to_string_internal (string, quoted, expand_string));
2563 expand_string_unsplit_to_string (string, quoted)
2567 return (expand_string_to_string_internal (string, quoted, expand_string_unsplit));
2571 expand_assignment_string_to_string (string, quoted)
2575 return (expand_string_to_string_internal (string, quoted, expand_string_assignment));
2578 #if defined (COND_COMMAND)
2579 /* Just remove backslashes in STRING. Returns a new string. */
2581 remove_backslashes (string)
2586 r = ret = (char *)xmalloc (strlen (string) + 1);
2587 for (s = string; s && *s; )
2599 /* This needs better error handling. */
2600 /* Expand W for use as an argument to a unary or binary operator in a
2601 [[...]] expression. If SPECIAL is nonzero, this is the rhs argument
2602 to the != or == operator, and should be treated as a pattern. In
2603 this case, we quote the string specially for the globbing code. The
2604 caller is responsible for removing the backslashes if the unquoted
2605 words is needed later. */
2607 cond_expand_word (w, special)
2614 if (w->word == 0 || w->word[0] == '\0')
2615 return ((char *)NULL);
2617 l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
2623 r = string_list (l);
2627 p = string_list (l);
2628 r = quote_string_for_globbing (p, QGLOB_CVTNULL);
2640 /* Call expand_word_internal to expand W and handle error returns.
2641 A convenience function for functions that don't want to handle
2642 any errors or free any memory before aborting. */
2644 call_expand_word_internal (w, q, i, c, e)
2650 result = expand_word_internal (w, q, i, c, e);
2651 if (result == &expand_word_error || result == &expand_word_fatal)
2653 /* By convention, each time this error is returned, w->word has
2654 already been freed (it sometimes may not be in the fatal case,
2655 but that doesn't result in a memory leak because we're going
2656 to exit in most cases). */
2657 w->word = (char *)NULL;
2658 last_command_exit_value = EXECUTION_FAILURE;
2659 exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
2666 /* Perform parameter expansion, command substitution, and arithmetic
2667 expansion on STRING, as if it were a word. Leave the result quoted. */
2669 expand_string_internal (string, quoted)
2676 if (string == 0 || *string == 0)
2677 return ((WORD_LIST *)NULL);
2680 td.word = savestring (string);
2682 tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
2688 /* Expand STRING by performing parameter expansion, command substitution,
2689 and arithmetic expansion. Dequote the resulting WORD_LIST before
2690 returning it, but do not perform word splitting. The call to
2691 remove_quoted_nulls () is in here because word splitting normally
2692 takes care of quote removal. */
2694 expand_string_unsplit (string, quoted)
2700 if (string == 0 || *string == '\0')
2701 return ((WORD_LIST *)NULL);
2703 expand_no_split_dollar_star = 1;
2704 value = expand_string_internal (string, quoted);
2705 expand_no_split_dollar_star = 0;
2711 remove_quoted_nulls (value->word->word);
2712 value->word->flags &= ~W_HASQUOTEDNULL;
2714 dequote_list (value);
2719 /* Expand the rhs of an assignment statement */
2721 expand_string_assignment (string, quoted)
2728 if (string == 0 || *string == '\0')
2729 return ((WORD_LIST *)NULL);
2731 expand_no_split_dollar_star = 1;
2733 td.flags = W_ASSIGNRHS;
2734 td.word = savestring (string);
2735 value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
2738 expand_no_split_dollar_star = 0;
2744 remove_quoted_nulls (value->word->word);
2745 value->word->flags &= ~W_HASQUOTEDNULL;
2747 dequote_list (value);
2753 /* Expand one of the PS? prompt strings. This is a sort of combination of
2754 expand_string_unsplit and expand_string_internal, but returns the
2755 passed string when an error occurs. Might want to trap other calls
2756 to jump_to_top_level here so we don't endlessly loop. */
2758 expand_prompt_string (string, quoted)
2765 if (string == 0 || *string == 0)
2766 return ((WORD_LIST *)NULL);
2769 td.word = savestring (string);
2771 no_longjmp_on_fatal_error = 1;
2772 value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
2773 no_longjmp_on_fatal_error = 0;
2775 if (value == &expand_word_error || value == &expand_word_fatal)
2777 value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
2785 remove_quoted_nulls (value->word->word);
2786 value->word->flags &= ~W_HASQUOTEDNULL;
2788 dequote_list (value);
2793 /* Expand STRING just as if you were expanding a word, but do not dequote
2794 the resultant WORD_LIST. This is called only from within this file,
2795 and is used to correctly preserve quoted characters when expanding
2796 things like ${1+"$@"}. This does parameter expansion, command
2797 substitution, arithmetic expansion, and word splitting. */
2799 expand_string_leave_quoted (string, quoted)
2806 if (string == 0 || *string == '\0')
2807 return ((WORD_LIST *)NULL);
2809 tlist = expand_string_internal (string, quoted);
2813 tresult = word_list_split (tlist);
2814 dispose_words (tlist);
2817 return ((WORD_LIST *)NULL);
2820 /* This does not perform word splitting or dequote the WORD_LIST
2823 expand_string_for_rhs (string, quoted, dollar_at_p, has_dollar_at)
2825 int quoted, *dollar_at_p, *has_dollar_at;
2830 if (string == 0 || *string == '\0')
2831 return (WORD_LIST *)NULL;
2835 tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, has_dollar_at);
2839 /* Expand STRING just as if you were expanding a word. This also returns
2840 a list of words. Note that filename globbing is *NOT* done for word
2841 or string expansion, just when the shell is expanding a command. This
2842 does parameter expansion, command substitution, arithmetic expansion,
2843 and word splitting. Dequote the resultant WORD_LIST before returning. */
2845 expand_string (string, quoted)
2851 if (string == 0 || *string == '\0')
2852 return ((WORD_LIST *)NULL);
2854 result = expand_string_leave_quoted (string, quoted);
2855 return (result ? dequote_list (result) : result);
2858 /***************************************************
2860 * Functions to handle quoting chars *
2862 ***************************************************/
2866 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
2867 The parser passes CTLNUL as CTLESC CTLNUL. */
2869 /* Quote escape characters in string s, but no other characters. This is
2870 used to protect CTLESC and CTLNUL in variable values from the rest of
2871 the word expansion process after the variable is expanded. */
2873 quote_escapes (string)
2876 register char *s, *t;
2878 char *result, *send;
2881 slen = strlen (string);
2882 send = string + slen;
2884 t = result = (char *)xmalloc ((slen * 2) + 1);
2889 if (*s == CTLESC || *s == CTLNUL)
2891 COPY_CHAR_P (t, s, send);
2898 list_quote_escapes (list)
2901 register WORD_LIST *w;
2904 for (w = list; w; w = w->next)
2907 w->word->word = quote_escapes (t);
2913 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
2915 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
2916 This is necessary to make unquoted CTLESC and CTLNUL characters in the
2917 data stream pass through properly.
2919 We need to remove doubled CTLESC characters inside quoted strings before
2920 quoting the entire string, so we do not double the number of CTLESC
2923 Also used by parts of the pattern substitution code. */
2925 dequote_escapes (string)
2928 register char *s, *t;
2930 char *result, *send;
2936 slen = strlen (string);
2937 send = string + slen;
2939 t = result = (char *)xmalloc (slen + 1);
2942 if (strchr (string, CTLESC) == 0)
2943 return (strcpy (result, s));
2947 if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL))
2953 COPY_CHAR_P (t, s, send);
2959 /* Return a new string with the quoted representation of character C. */
2961 make_quoted_char (c)
2966 temp = (char *)xmalloc (3);
2981 /* Quote STRING. Return a new string. */
2983 quote_string (string)
2988 char *result, *send;
2992 result = (char *)xmalloc (2);
3000 slen = strlen (string);
3001 send = string + slen;
3003 result = (char *)xmalloc ((slen * 2) + 1);
3005 for (t = result; string < send; )
3008 COPY_CHAR_P (t, string, send);
3015 /* De-quoted quoted characters in STRING. */
3017 dequote_string (string)
3020 register char *s, *t;
3022 char *result, *send;
3025 slen = strlen (string);
3027 t = result = (char *)xmalloc (slen + 1);
3029 if (QUOTED_NULL (string))
3035 /* If no character in the string can be quoted, don't bother examining
3036 each character. Just return a copy of the string passed to us. */
3037 if (strchr (string, CTLESC) == NULL)
3038 return (strcpy (result, string));
3040 send = string + slen;
3050 COPY_CHAR_P (t, s, send);
3057 /* Quote the entire WORD_LIST list. */
3062 register WORD_LIST *w;
3065 for (w = list; w; w = w->next)
3068 w->word->word = quote_string (t);
3070 w->word->flags |= W_QUOTED;
3080 register WORD_LIST *tlist;
3082 for (tlist = list; tlist; tlist = tlist->next)
3084 s = dequote_string (tlist->word->word);
3085 free (tlist->word->word);
3086 tlist->word->word = s;
3091 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
3094 remove_quoted_escapes (string)
3101 t = dequote_escapes (string);
3109 /* Perform quoted null character removal on STRING. We don't allow any
3110 quoted null characters in the middle or at the ends of strings because
3111 of how expand_word_internal works. remove_quoted_nulls () turns
3112 STRING into an empty string iff it only consists of a quoted null,
3113 and removes all unquoted CTLNUL characters. */
3115 remove_quoted_nulls (string)
3118 register size_t slen;
3119 register int i, j, prev_i;
3122 if (strchr (string, CTLNUL) == 0) /* XXX */
3123 return string; /* XXX */
3125 slen = strlen (string);
3130 if (string[i] == CTLESC)
3132 /* Old code had j++, but we cannot assume that i == j at this
3133 point -- what if a CTLNUL has already been removed from the
3134 string? We don't want to drop the CTLESC or recopy characters
3135 that we've already copied down. */
3136 i++; string[j++] = CTLESC;
3140 else if (string[i] == CTLNUL)
3144 ADVANCE_CHAR (string, slen, i);
3147 do string[j++] = string[prev_i++]; while (prev_i < i);
3157 /* Perform quoted null character removal on each element of LIST.
3158 This modifies LIST. */
3160 word_list_remove_quoted_nulls (list)
3163 register WORD_LIST *t;
3165 for (t = list; t; t = t->next)
3167 remove_quoted_nulls (t->word->word);
3168 t->word->flags &= ~W_HASQUOTEDNULL;
3172 /* **************************************************************** */
3174 /* Functions for Matching and Removing Patterns */
3176 /* **************************************************************** */
3178 #if defined (HANDLE_MULTIBYTE)
3179 #if 0 /* Currently unused */
3180 static unsigned char *
3181 mb_getcharlens (string, len)
3185 int i, offset, last;
3192 ret = (unsigned char *)xmalloc (len);
3193 memset (ret, 0, len);
3194 while (string[last])
3196 ADVANCE_CHAR (string, len, offset);
3197 ret[last] = offset - last;
3205 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
3206 can have one of 4 values:
3207 RP_LONG_LEFT remove longest matching portion at start of PARAM
3208 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
3209 RP_LONG_RIGHT remove longest matching portion at end of PARAM
3210 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
3213 #define RP_LONG_LEFT 1
3214 #define RP_SHORT_LEFT 2
3215 #define RP_LONG_RIGHT 3
3216 #define RP_SHORT_RIGHT 4
3219 remove_upattern (param, pattern, op)
3220 char *param, *pattern;
3225 register char *p, *ret, c;
3227 len = STRLEN (param);
3232 case RP_LONG_LEFT: /* remove longest match at start */
3233 for (p = end; p >= param; p--)
3236 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3239 return (savestring (p));
3246 case RP_SHORT_LEFT: /* remove shortest match at start */
3247 for (p = param; p <= end; p++)
3250 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3253 return (savestring (p));
3259 case RP_LONG_RIGHT: /* remove longest match at end */
3260 for (p = param; p <= end; p++)
3262 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3265 ret = savestring (param);
3272 case RP_SHORT_RIGHT: /* remove shortest match at end */
3273 for (p = end; p >= param; p--)
3275 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3278 ret = savestring (param);
3286 return (savestring (param)); /* no match, return original string */
3289 #if defined (HANDLE_MULTIBYTE)
3291 #if !defined (HAVE_WCSDUP)
3300 ret = xmalloc ((len + 1) * sizeof (wchar_t));
3303 return (wcscpy (ret, ws));
3305 #endif /* !HAVE_WCSDUP */
3308 remove_wpattern (wparam, wstrlen, wpattern, op)
3320 case RP_LONG_LEFT: /* remove longest match at start */
3321 for (n = wstrlen; n >= 0; n--)
3323 wc = wparam[n]; wparam[n] = L'\0';
3324 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3327 return (wcsdup (wparam + n));
3333 case RP_SHORT_LEFT: /* remove shortest match at start */
3334 for (n = 0; n <= wstrlen; n++)
3336 wc = wparam[n]; wparam[n] = L'\0';
3337 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3340 return (wcsdup (wparam + n));
3346 case RP_LONG_RIGHT: /* remove longest match at end */
3347 for (n = 0; n <= wstrlen; n++)
3349 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3351 wc = wparam[n]; wparam[n] = L'\0';
3352 ret = wcsdup (wparam);
3359 case RP_SHORT_RIGHT: /* remove shortest match at end */
3360 for (n = wstrlen; n >= 0; n--)
3362 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
3364 wc = wparam[n]; wparam[n] = L'\0';
3365 ret = wcsdup (wparam);
3373 return (wcsdup (wparam)); /* no match, return original string */
3375 #endif /* HANDLE_MULTIBYTE */
3378 remove_pattern (param, pattern, op)
3379 char *param, *pattern;
3384 if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */
3385 return (savestring (param));
3387 #if defined (HANDLE_MULTIBYTE)
3390 wchar_t *ret, *oret;
3392 wchar_t *wparam, *wpattern;
3396 n = xdupmbstowcs (&wpattern, NULL, pattern);
3397 if (n == (size_t)-1)
3398 return (remove_upattern (param, pattern, op));
3399 n = xdupmbstowcs (&wparam, NULL, param);
3400 if (n == (size_t)-1)
3403 return (remove_upattern (param, pattern, op));
3405 oret = ret = remove_wpattern (wparam, n, wpattern, op);
3411 xret = xmalloc (n + 1);
3412 memset (&ps, '\0', sizeof (mbstate_t));
3413 n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps);
3414 xret[n] = '\0'; /* just to make sure */
3420 return (remove_upattern (param, pattern, op));
3423 /* Return 1 of the first character of STRING could match the first
3424 character of pattern PAT. Used to avoid n2 calls to strmatch(). */
3426 match_pattern_char (pat, string)
3437 return (*string == c);
3439 return (*string == *pat);
3441 return (*pat == LPAREN ? 1 : (*string != '\0'));
3447 return (*pat == LPAREN ? 1 : (*string == c));
3449 return (*string != '\0');
3453 /* Match PAT anywhere in STRING and return the match boundaries.
3454 This returns 1 in case of a successful match, 0 otherwise. SP
3455 and EP are pointers into the string where the match begins and
3456 ends, respectively. MTYPE controls what kind of match is attempted.
3457 MATCH_BEG and MATCH_END anchor the match at the beginning and end
3458 of the string, respectively. The longest match is returned. */
3460 match_upattern (string, pat, mtype, sp, ep)
3466 register char *p, *p1, *npat;
3469 /* If the pattern doesn't match anywhere in the string, go ahead and
3470 short-circuit right away. A minor optimization, saves a bunch of
3471 unnecessary calls to strmatch (up to N calls for a string of N
3472 characters) if the match is unsuccessful. To preserve the semantics
3473 of the substring matches below, we make sure that the pattern has
3474 `*' as first and last character, making a new pattern if necessary. */
3475 /* XXX - check this later if I ever implement `**' with special meaning,
3476 since this will potentially result in `**' at the beginning or end */
3478 if (pat[0] != '*' || pat[len - 1] != '*')
3480 p = npat = xmalloc (len + 3);
3486 if (p1[-1] != '*' || p[-2] == '\\')
3492 c = strmatch (npat, string, FNMATCH_EXTFLAG);
3495 if (c == FNM_NOMATCH)
3498 len = STRLEN (string);
3504 for (p = string; p <= end; p++)
3506 if (match_pattern_char (pat, p))
3508 for (p1 = end; p1 >= p; p1--)
3510 c = *p1; *p1 = '\0';
3511 if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
3526 if (match_pattern_char (pat, string) == 0)
3529 for (p = end; p >= string; p--)
3532 if (strmatch (pat, string, FNMATCH_EXTFLAG) == 0)
3545 for (p = string; p <= end; p++)
3547 if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
3562 #if defined (HANDLE_MULTIBYTE)
3563 /* Return 1 of the first character of WSTRING could match the first
3564 character of pattern WPAT. Wide character version. */
3566 match_pattern_wchar (wpat, wstring)
3567 wchar_t *wpat, *wstring;
3574 switch (wc = *wpat++)
3577 return (*wstring == wc);
3579 return (*wstring == *wpat);
3581 return (*wpat == LPAREN ? 1 : (*wstring != L'\0'));
3587 return (*wpat == LPAREN ? 1 : (*wstring == wc));
3589 return (*wstring != L'\0');
3593 /* Match WPAT anywhere in WSTRING and return the match boundaries.
3594 This returns 1 in case of a successful match, 0 otherwise. Wide
3595 character version. */
3597 match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
3605 wchar_t wc, *wp, *nwpat, *wp1;
3608 size_t n, n1; /* Apple's gcc seems to miscompile this badly */
3613 /* If the pattern doesn't match anywhere in the string, go ahead and
3614 short-circuit right away. A minor optimization, saves a bunch of
3615 unnecessary calls to strmatch (up to N calls for a string of N
3616 characters) if the match is unsuccessful. To preserve the semantics
3617 of the substring matches below, we make sure that the pattern has
3618 `*' as first and last character, making a new pattern if necessary. */
3619 /* XXX - check this later if I ever implement `**' with special meaning,
3620 since this will potentially result in `**' at the beginning or end */
3621 len = wcslen (wpat);
3622 if (wpat[0] != L'*' || wpat[len - 1] != L'*')
3624 wp = nwpat = xmalloc ((len + 3) * sizeof (wchar_t));
3628 while (*wp1 != L'\0')
3630 if (wp1[-1] != L'*' || wp1[-2] == L'\\')
3636 len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG);
3639 if (len == FNM_NOMATCH)
3645 for (n = 0; n <= wstrlen; n++)
3647 if (match_pattern_wchar (wpat, wstring + n))
3649 for (n1 = wstrlen; n1 >= n; n1--)
3651 wc = wstring[n1]; wstring[n1] = L'\0';
3652 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
3667 if (match_pattern_wchar (wpat, wstring) == 0)
3670 for (n = wstrlen; n >= 0; n--)
3672 wc = wstring[n]; wstring[n] = L'\0';
3673 if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG) == 0)
3686 for (n = 0; n <= wstrlen; n++)
3688 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
3691 *ep = indices[wstrlen];
3701 #endif /* HANDLE_MULTIBYTE */
3704 match_pattern (string, pat, mtype, sp, ep)
3709 #if defined (HANDLE_MULTIBYTE)
3712 wchar_t *wstring, *wpat;
3716 if (string == 0 || *string == 0 || pat == 0 || *pat == 0)
3719 #if defined (HANDLE_MULTIBYTE)
3722 n = xdupmbstowcs (&wpat, NULL, pat);
3723 if (n == (size_t)-1)
3724 return (match_upattern (string, pat, mtype, sp, ep));
3725 n = xdupmbstowcs (&wstring, &indices, string);
3726 if (n == (size_t)-1)
3729 return (match_upattern (string, pat, mtype, sp, ep));
3731 ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep);
3741 return (match_upattern (string, pat, mtype, sp, ep));
3745 getpatspec (c, value)
3750 return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
3752 return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
3755 /* Posix.2 says that the WORD should be run through tilde expansion,
3756 parameter expansion, command substitution and arithmetic expansion.
3757 This leaves the result quoted, so quote_string_for_globbing () has
3758 to be called to fix it up for strmatch (). If QUOTED is non-zero,
3759 it means that the entire expression was enclosed in double quotes.
3760 This means that quoting characters in the pattern do not make any
3761 special pattern characters quoted. For example, the `*' in the
3762 following retains its special meaning: "${foo#'*'}". */
3764 getpattern (value, quoted, expandpat)
3766 int quoted, expandpat;
3772 /* There is a problem here: how to handle single or double quotes in the
3773 pattern string when the whole expression is between double quotes?
3774 POSIX.2 says that enclosing double quotes do not cause the pattern to
3775 be quoted, but does that leave us a problem with @ and array[@] and their
3776 expansions inside a pattern? */
3778 if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
3781 pat = string_extract_double_quoted (tword, &i, 1);
3787 /* expand_string_for_rhs () leaves WORD quoted and does not perform
3789 l = *value ? expand_string_for_rhs (value,
3790 (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
3791 (int *)NULL, (int *)NULL)
3793 pat = string_list (l);
3797 tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
3805 /* Handle removing a pattern from a string as a result of ${name%[%]value}
3806 or ${name#[#]value}. */
3808 variable_remove_pattern (value, pattern, patspec, quoted)
3809 char *value, *pattern;
3810 int patspec, quoted;
3814 tword = remove_pattern (value, pattern, patspec);
3821 list_remove_pattern (list, pattern, patspec, itype, quoted)
3824 int patspec, itype, quoted;
3830 for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
3832 tword = remove_pattern (l->word->word, pattern, patspec);
3833 w = alloc_word_desc ();
3834 w->word = tword ? tword : savestring ("");
3835 new = make_word_list (w, new);
3838 l = REVERSE_LIST (new, WORD_LIST *);
3840 tword = (quoted & Q_DOUBLE_QUOTES) ? string_list_dollar_star (l) : string_list (l);
3842 tword = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (l) : l);
3849 parameter_list_remove_pattern (itype, pattern, patspec, quoted)
3852 int patspec, quoted;
3857 list = list_rest_of_args ();
3859 return ((char *)NULL);
3860 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
3861 dispose_words (list);
3865 #if defined (ARRAY_VARS)
3867 array_remove_pattern (a, pattern, patspec, varname, quoted)
3871 char *varname; /* so we can figure out how it's indexed */
3879 /* compute itype from varname here */
3880 v = array_variable_part (varname, &ret, 0);
3883 list = array_to_word_list (a);
3885 return ((char *)NULL);
3886 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
3887 dispose_words (list);
3891 #endif /* ARRAY_VARS */
3894 parameter_brace_remove_pattern (varname, value, patstr, rtype, quoted)
3895 char *varname, *value, *patstr;
3898 int vtype, patspec, starsub;
3899 char *temp1, *val, *pattern;
3903 return ((char *)NULL);
3905 this_command_name = varname;
3907 vtype = get_var_and_type (varname, value, quoted, &v, &val);
3909 return ((char *)NULL);
3911 starsub = vtype & VT_STARSUB;
3912 vtype &= ~VT_STARSUB;
3914 patspec = getpatspec (rtype, patstr);
3915 if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
3918 pattern = getpattern (patstr, quoted, 1);
3920 temp1 = (char *)NULL; /* shut up gcc */
3924 case VT_ARRAYMEMBER:
3925 temp1 = remove_pattern (val, pattern, patspec);
3926 if (vtype == VT_VARIABLE)
3930 val = quote_escapes (temp1);
3935 #if defined (ARRAY_VARS)
3937 temp1 = array_remove_pattern (array_cell (v), pattern, patspec, varname, quoted);
3938 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
3940 val = quote_escapes (temp1);
3947 temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
3948 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
3950 val = quote_escapes (temp1);
3961 /*******************************************
3963 * Functions to expand WORD_DESCs *
3965 *******************************************/
3967 /* Expand WORD, performing word splitting on the result. This does
3968 parameter expansion, command substitution, arithmetic expansion,
3969 word splitting, and quote removal. */
3972 expand_word (word, quoted)
3976 WORD_LIST *result, *tresult;
3978 tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
3979 result = word_list_split (tresult);
3980 dispose_words (tresult);
3981 return (result ? dequote_list (result) : result);
3984 /* Expand WORD, but do not perform word splitting on the result. This
3985 does parameter expansion, command substitution, arithmetic expansion,
3986 and quote removal. */
3988 expand_word_unsplit (word, quoted)
3994 expand_no_split_dollar_star = 1;
3995 result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
3996 expand_no_split_dollar_star = 0;
3998 return (result ? dequote_list (result) : result);
4001 /* Perform shell expansions on WORD, but do not perform word splitting or
4002 quote removal on the result. */
4004 expand_word_leave_quoted (word, quoted)
4008 return (call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL));
4011 #if defined (PROCESS_SUBSTITUTION)
4013 /*****************************************************************/
4015 /* Hacking Process Substitution */
4017 /*****************************************************************/
4019 #if !defined (HAVE_DEV_FD)
4020 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
4021 of FIFOs the shell has open. unlink_fifo_list will walk the list and
4022 unlink all of them. add_fifo_list adds the name of an open FIFO to the
4023 list. NFIFO is a count of the number of FIFOs in the list. */
4024 #define FIFO_INCR 20
4031 static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
4033 static int fifo_list_size;
4036 add_fifo_list (pathname)
4039 if (nfifo >= fifo_list_size - 1)
4041 fifo_list_size += FIFO_INCR;
4042 fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
4043 fifo_list_size * sizeof (struct temp_fifo));
4046 fifo_list[nfifo].file = savestring (pathname);
4058 for (i = saved = 0; i < nfifo; i++)
4060 if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
4062 unlink (fifo_list[i].file);
4063 free (fifo_list[i].file);
4064 fifo_list[i].file = (char *)NULL;
4065 fifo_list[i].proc = -1;
4071 /* If we didn't remove some of the FIFOs, compact the list. */
4074 for (i = j = 0; i < nfifo; i++)
4075 if (fifo_list[i].file)
4077 fifo_list[j].file = fifo_list[i].file;
4078 fifo_list[j].proc = fifo_list[i].proc;
4092 tname = sh_mktmpname ("sh-np", MT_USERANDOM);
4093 if (mkfifo (tname, 0600) < 0)
4096 return ((char *)NULL);
4099 add_fifo_list (tname);
4103 #else /* HAVE_DEV_FD */
4105 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
4106 has open to children. NFDS is a count of the number of bits currently
4107 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
4109 static char *dev_fd_list = (char *)NULL;
4111 static int totfds; /* The highest possible number of open files. */
4117 if (!dev_fd_list || fd >= totfds)
4122 totfds = getdtablesize ();
4123 if (totfds < 0 || totfds > 256)
4128 dev_fd_list = (char *)xrealloc (dev_fd_list, totfds);
4129 memset (dev_fd_list + ofds, '\0', totfds - ofds);
4132 dev_fd_list[fd] = 1;
4144 for (i = 0; nfds && i < totfds; i++)
4155 #if defined (NOTDEF)
4156 print_dev_fd_list ()
4160 fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
4163 for (i = 0; i < totfds; i++)
4166 fprintf (stderr, " %d", i);
4168 fprintf (stderr, "\n");
4173 make_dev_fd_filename (fd)
4176 char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
4178 ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 4);
4180 strcpy (ret, DEV_FD_PREFIX);
4181 p = inttostr (fd, intbuf, sizeof (intbuf));
4182 strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
4188 #endif /* HAVE_DEV_FD */
4190 /* Return a filename that will open a connection to the process defined by
4191 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
4192 a filename in /dev/fd corresponding to a descriptor that is one of the
4193 ends of the pipe. If not defined, we use named pipes on systems that have
4194 them. Systems without /dev/fd and named pipes are out of luck.
4196 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
4197 use the read end of the pipe and dup that file descriptor to fd 0 in
4198 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
4199 writing or use the write end of the pipe in the child, and dup that
4200 file descriptor to fd 1 in the child. The parent does the opposite. */
4203 process_substitute (string, open_for_read_in_child)
4205 int open_for_read_in_child;
4210 #if defined (HAVE_DEV_FD)
4211 int parent_pipe_fd, child_pipe_fd;
4213 #endif /* HAVE_DEV_FD */
4214 #if defined (JOB_CONTROL)
4215 pid_t old_pipeline_pgrp;
4218 if (!string || !*string || wordexp_only)
4219 return ((char *)NULL);
4221 #if !defined (HAVE_DEV_FD)
4222 pathname = make_named_pipe ();
4223 #else /* HAVE_DEV_FD */
4224 if (pipe (fildes) < 0)
4226 sys_error (_("cannot make pipe for process substitution"));
4227 return ((char *)NULL);
4229 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
4230 the pipe in the parent, otherwise the read end. */
4231 parent_pipe_fd = fildes[open_for_read_in_child];
4232 child_pipe_fd = fildes[1 - open_for_read_in_child];
4233 /* Move the parent end of the pipe to some high file descriptor, to
4234 avoid clashes with FDs used by the script. */
4235 parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
4237 pathname = make_dev_fd_filename (parent_pipe_fd);
4238 #endif /* HAVE_DEV_FD */
4242 sys_error (_("cannot make pipe for process substitution"));
4243 return ((char *)NULL);
4246 old_pid = last_made_pid;
4248 #if defined (JOB_CONTROL)
4249 old_pipeline_pgrp = pipeline_pgrp;
4250 pipeline_pgrp = shell_pgrp;
4252 #endif /* JOB_CONTROL */
4254 pid = make_child ((char *)NULL, 1);
4257 reset_terminating_signals (); /* XXX */
4258 free_pushed_string_input ();
4259 /* Cancel traps, in trap.c. */
4260 restore_original_signals ();
4261 setup_async_signals ();
4262 subshell_environment |= SUBSHELL_COMSUB;
4265 #if defined (JOB_CONTROL)
4266 set_sigchld_handler ();
4267 stop_making_children ();
4268 pipeline_pgrp = old_pipeline_pgrp;
4269 #endif /* JOB_CONTROL */
4273 sys_error (_("cannot make child for process substitution"));
4275 #if defined (HAVE_DEV_FD)
4276 close (parent_pipe_fd);
4277 close (child_pipe_fd);
4278 #endif /* HAVE_DEV_FD */
4279 return ((char *)NULL);
4284 #if defined (JOB_CONTROL)
4285 restore_pipeline (1);
4288 #if !defined (HAVE_DEV_FD)
4289 fifo_list[nfifo-1].proc = pid;
4292 last_made_pid = old_pid;
4294 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
4296 #endif /* JOB_CONTROL && PGRP_PIPE */
4298 #if defined (HAVE_DEV_FD)
4299 close (child_pipe_fd);
4300 #endif /* HAVE_DEV_FD */
4305 set_sigint_handler ();
4307 #if defined (JOB_CONTROL)
4308 set_job_control (0);
4309 #endif /* JOB_CONTROL */
4311 #if !defined (HAVE_DEV_FD)
4312 /* Open the named pipe in the child. */
4313 fd = open (pathname, open_for_read_in_child ? O_RDONLY|O_NONBLOCK : O_WRONLY);
4316 /* Two separate strings for ease of translation. */
4317 if (open_for_read_in_child)
4318 sys_error (_("cannot open named pipe %s for reading"), pathname);
4320 sys_error (_("cannot open named pipe %s for writing"), pathname);
4324 if (open_for_read_in_child)
4326 if (sh_unset_nodelay_mode (fd) < 0)
4328 sys_error (_("cannout reset nodelay mode for fd %d"), fd);
4332 #else /* HAVE_DEV_FD */
4334 #endif /* HAVE_DEV_FD */
4336 if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
4338 sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname,
4339 open_for_read_in_child ? 0 : 1);
4343 if (fd != (open_for_read_in_child ? 0 : 1))
4346 /* Need to close any files that this process has open to pipes inherited
4348 if (current_fds_to_close)
4350 close_fd_bitmap (current_fds_to_close);
4351 current_fds_to_close = (struct fd_bitmap *)NULL;
4354 #if defined (HAVE_DEV_FD)
4355 /* Make sure we close the parent's end of the pipe and clear the slot
4356 in the fd list so it is not closed later, if reallocated by, for
4357 instance, pipe(2). */
4358 close (parent_pipe_fd);
4359 dev_fd_list[parent_pipe_fd] = 0;
4360 #endif /* HAVE_DEV_FD */
4362 result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
4364 #if !defined (HAVE_DEV_FD)
4365 /* Make sure we close the named pipe in the child before we exit. */
4366 close (open_for_read_in_child ? 0 : 1);
4367 #endif /* !HAVE_DEV_FD */
4372 #endif /* PROCESS_SUBSTITUTION */
4374 /***********************************/
4376 /* Command Substitution */
4378 /***********************************/
4381 read_comsub (fd, quoted)
4384 char *istring, buf[128], *bufp;
4385 int istring_index, istring_size, c;
4388 istring = (char *)NULL;
4389 istring_index = istring_size = bufn = 0;
4392 setmode (fd, O_TEXT); /* we don't want CR/LF, we want Unix-style */
4395 /* Read the output of the command through the pipe. */
4402 bufn = zread (fd, buf, sizeof (buf));
4412 internal_warning ("read_comsub: ignored null byte in input");
4417 /* Add the character to ISTRING, possibly after resizing it. */
4418 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
4420 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || c == CTLESC || c == CTLNUL)
4421 istring[istring_index++] = CTLESC;
4423 istring[istring_index++] = c;
4426 #if defined (__CYGWIN__)
4427 if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r')
4430 istring[istring_index - 1] = '\n';
4437 istring[istring_index] = '\0';
4439 /* If we read no output, just return now and save ourselves some
4441 if (istring_index == 0)
4444 return (char *)NULL;
4447 /* Strip trailing newlines from the output of the command. */
4448 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
4450 while (istring_index > 0)
4452 if (istring[istring_index - 1] == '\n')
4456 /* If the newline was quoted, remove the quoting char. */
4457 if (istring[istring_index - 1] == CTLESC)
4463 istring[istring_index] = '\0';
4466 strip_trailing (istring, istring_index - 1, 1);
4471 /* Perform command substitution on STRING. This returns a string,
4474 command_substitute (string, quoted)
4478 pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
4480 int result, fildes[2], function_value, pflags, rc;
4482 istring = (char *)NULL;
4484 /* Don't fork () if there is no need to. In the case of no command to
4485 run, just return NULL. */
4486 if (!string || !*string || (string[0] == '\n' && !string[1]))
4487 return ((char *)NULL);
4489 if (wordexp_only && read_but_dont_execute)
4491 last_command_exit_value = 125;
4492 jump_to_top_level (EXITPROG);
4495 /* We're making the assumption here that the command substitution will
4496 eventually run a command from the file system. Since we'll run
4497 maybe_make_export_env in this subshell before executing that command,
4498 the parent shell and any other shells it starts will have to remake
4499 the environment. If we make it before we fork, other shells won't
4500 have to. Don't bother if we have any temporary variable assignments,
4501 though, because the export environment will be remade after this
4502 command completes anyway, but do it if all the words to be expanded
4503 are variable assignments. */
4504 if (subst_assign_varlist == 0 || garglist == 0)
4505 maybe_make_export_env (); /* XXX */
4507 /* Flags to pass to parse_and_execute() */
4508 pflags = interactive ? SEVAL_RESETLINE : 0;
4510 /* Pipe the output of executing STRING into the current shell. */
4511 if (pipe (fildes) < 0)
4513 sys_error (_("cannot make pipe for command substitution"));
4517 old_pid = last_made_pid;
4518 #if defined (JOB_CONTROL)
4519 old_pipeline_pgrp = pipeline_pgrp;
4520 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
4521 if ((subshell_environment & SUBSHELL_PIPE) == 0)
4522 pipeline_pgrp = shell_pgrp;
4523 cleanup_the_pipeline ();
4524 #endif /* JOB_CONTROL */
4526 old_async_pid = last_asynchronous_pid;
4528 pid = make_child ((char *)NULL, 0);
4530 pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
4532 last_asynchronous_pid = old_async_pid;
4535 /* Reset the signal handlers in the child, but don't free the
4537 reset_signal_handlers ();
4539 #if defined (JOB_CONTROL)
4540 set_sigchld_handler ();
4541 stop_making_children ();
4542 pipeline_pgrp = old_pipeline_pgrp;
4544 stop_making_children ();
4545 #endif /* JOB_CONTROL */
4549 sys_error (_("cannot make child for command substitution"));
4555 return ((char *)NULL);
4560 set_sigint_handler (); /* XXX */
4562 free_pushed_string_input ();
4564 if (dup2 (fildes[1], 1) < 0)
4566 sys_error (_("command_substitute: cannot duplicate pipe as fd 1"));
4567 exit (EXECUTION_FAILURE);
4570 /* If standard output is closed in the parent shell
4571 (such as after `exec >&-'), file descriptor 1 will be
4572 the lowest available file descriptor, and end up in
4573 fildes[0]. This can happen for stdin and stderr as well,
4574 but stdout is more important -- it will cause no output
4575 to be generated from this command. */
4576 if ((fildes[1] != fileno (stdin)) &&
4577 (fildes[1] != fileno (stdout)) &&
4578 (fildes[1] != fileno (stderr)))
4581 if ((fildes[0] != fileno (stdin)) &&
4582 (fildes[0] != fileno (stdout)) &&
4583 (fildes[0] != fileno (stderr)))
4586 /* The currently executing shell is not interactive. */
4589 /* This is a subshell environment. */
4590 subshell_environment |= SUBSHELL_COMSUB;
4592 /* When not in POSIX mode, command substitution does not inherit
4594 if (posixly_correct == 0)
4595 exit_immediately_on_error = 0;
4597 remove_quoted_escapes (string);
4599 startup_state = 2; /* see if we can avoid a fork */
4600 /* Give command substitution a place to jump back to on failure,
4601 so we don't go back up to main (). */
4602 result = setjmp (top_level);
4604 /* If we're running a command substitution inside a shell function,
4605 trap `return' so we don't return from the function in the subshell
4606 and go off to never-never land. */
4607 if (result == 0 && return_catch_flag)
4608 function_value = setjmp (return_catch);
4612 if (result == ERREXIT)
4613 rc = last_command_exit_value;
4614 else if (result == EXITPROG)
4615 rc = last_command_exit_value;
4617 rc = EXECUTION_FAILURE;
4618 else if (function_value)
4619 rc = return_catch_value;
4623 rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST);
4627 last_command_exit_value = rc;
4628 rc = run_exit_trap ();
4633 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
4635 #endif /* JOB_CONTROL && PGRP_PIPE */
4639 istring = read_comsub (fildes[0], quoted);
4643 current_command_subst_pid = pid;
4644 last_command_exit_value = wait_for (pid);
4645 last_command_subst_pid = pid;
4646 last_made_pid = old_pid;
4648 #if defined (JOB_CONTROL)
4649 /* If last_command_exit_value > 128, then the substituted command
4650 was terminated by a signal. If that signal was SIGINT, then send
4651 SIGINT to ourselves. This will break out of loops, for instance. */
4652 if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT)
4653 kill (getpid (), SIGINT);
4655 /* wait_for gives the terminal back to shell_pgrp. If some other
4656 process group should have it, give it away to that group here.
4657 pipeline_pgrp is non-zero only while we are constructing a
4658 pipline, so what we are concerned about is whether or not that
4659 pipeline was started in the background. A pipeline started in
4660 the background should never get the tty back here. */
4662 if (interactive && pipeline_pgrp != (pid_t)0 && pipeline_pgrp != last_asynchronous_pid)
4664 if (interactive && pipeline_pgrp != (pid_t)0 && (subshell_environment & SUBSHELL_ASYNC) == 0)
4666 give_terminal_to (pipeline_pgrp, 0);
4667 #endif /* JOB_CONTROL */
4673 /********************************************************
4675 * Utility functions for parameter expansion *
4677 ********************************************************/
4679 #if defined (ARRAY_VARS)
4682 array_length_reference (s)
4691 var = array_variable_part (s, &t, &len);
4693 /* If unbound variables should generate an error, report one and return
4695 if ((var == 0 || array_p (var) == 0) && unbound_vars_is_error)
4706 /* We support a couple of expansions for variables that are not arrays.
4707 We'll return the length of the value for v[0], and 1 for v[@] or
4708 v[*]. Return 0 for everything else. */
4710 array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
4712 if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
4713 return (array_p (var) ? array_num_elements (array) : 1);
4715 ind = array_expand_index (t, len);
4718 err_badarraysub (t);
4723 t = array_reference (array, ind);
4725 t = (ind == 0) ? value_cell (var) : (char *)NULL;
4730 #endif /* ARRAY_VARS */
4733 valid_brace_expansion_word (name, var_is_special)
4737 if (DIGIT (*name) && all_digits (name))
4739 else if (var_is_special)
4741 #if defined (ARRAY_VARS)
4742 else if (valid_array_reference (name))
4744 #endif /* ARRAY_VARS */
4745 else if (legal_identifier (name))
4752 chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at)
4755 int *quoted_dollar_atp, *contains_dollar_at;
4761 if (quoted_dollar_atp)
4762 *quoted_dollar_atp = 0;
4763 if (contains_dollar_at)
4764 *contains_dollar_at = 0;
4768 /* check for $@ and $* */
4769 if (name[0] == '@' && name[1] == 0)
4771 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
4772 *quoted_dollar_atp = 1;
4773 if (contains_dollar_at)
4774 *contains_dollar_at = 1;
4777 else if (name[0] == '*' && name[1] == '\0' && quoted == 0)
4779 if (contains_dollar_at)
4780 *contains_dollar_at = 1;
4784 /* Now check for ${array[@]} and ${array[*]} */
4785 #if defined (ARRAY_VARS)
4786 else if (valid_array_reference (name))
4788 temp1 = xstrchr (name, '[');
4789 if (temp1 && temp1[1] == '@' && temp1[2] == ']')
4791 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
4792 *quoted_dollar_atp = 1;
4793 if (contains_dollar_at)
4794 *contains_dollar_at = 1;
4797 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
4798 which should result in separate words even when IFS is unset. */
4799 if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0)
4801 if (contains_dollar_at)
4802 *contains_dollar_at = 1;
4810 /* Parameter expand NAME, and return a new string which is the expansion,
4811 or NULL if there was no expansion.
4812 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
4813 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
4814 NAME was found inside of a double-quoted expression. */
4816 parameter_brace_expand_word (name, var_is_special, quoted)
4818 int var_is_special, quoted;
4829 /* Handle multiple digit arguments, as in ${11}. */
4830 if (legal_number (name, &arg_index))
4832 tt = get_dollar_var_value (arg_index);
4834 temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
4836 : quote_escapes (tt);
4838 temp = (char *)NULL;
4841 else if (var_is_special) /* ${@} */
4844 tt = (char *)xmalloc (2 + strlen (name));
4845 tt[sindex = 0] = '$';
4846 strcpy (tt + 1, name);
4848 ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
4849 (int *)NULL, (int *)NULL, 0);
4852 #if defined (ARRAY_VARS)
4853 else if (valid_array_reference (name))
4855 temp = array_value (name, quoted, &atype);
4856 if (atype == 0 && temp)
4857 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
4858 ? quote_string (temp)
4859 : quote_escapes (temp);
4862 else if (var = find_variable (name))
4864 if (var_isset (var) && invisible_p (var) == 0)
4866 #if defined (ARRAY_VARS)
4867 temp = array_p (var) ? array_reference (array_cell (var), 0) : value_cell (var);
4869 temp = value_cell (var);
4873 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
4874 ? quote_string (temp)
4875 : quote_escapes (temp);
4878 temp = (char *)NULL;
4881 temp = (char *)NULL;
4885 ret = alloc_word_desc ();
4891 /* Expand an indirect reference to a variable: ${!NAME} expands to the
4892 value of the variable whose name is the value of NAME. */
4894 parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at)
4896 int var_is_special, quoted;
4897 int *quoted_dollar_atp, *contains_dollar_at;
4902 w = parameter_brace_expand_word (name, var_is_special, quoted);
4904 /* Have to dequote here if necessary */
4907 temp = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
4908 ? dequote_string (t)
4909 : dequote_escapes (t);
4913 dispose_word_desc (w);
4915 chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at);
4917 return (WORD_DESC *)NULL;
4919 w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted);
4925 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
4926 depending on the value of C, the separating character. C can be one of
4927 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
4928 between double quotes. */
4930 parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat)
4932 int c, quoted, *qdollaratp, *hasdollarat;
4936 char *t, *t1, *temp;
4939 /* If the entire expression is between double quotes, we want to treat
4940 the value as a double-quoted string, with the exception that we strip
4941 embedded unescaped double quotes. */
4942 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
4945 temp = string_extract_double_quoted (value, &hasdol, 1);
4950 w = alloc_word_desc ();
4952 /* XXX was 0 not quoted */
4953 l = *temp ? expand_string_for_rhs (temp, quoted, &hasdol, (int *)NULL)
4956 *hasdollarat = hasdol || (l && l->next);
4961 /* The expansion of TEMP returned something. We need to treat things
4962 slightly differently if HASDOL is non-zero. If we have "$@", the
4963 individual words have already been quoted. We need to turn them
4964 into a string with the words separated by the first character of
4965 $IFS without any additional quoting, so string_list_dollar_at won't
4966 do the right thing. We use string_list_dollar_star instead. */
4967 temp = (hasdol || l->next) ? string_list_dollar_star (l) : string_list (l);
4969 /* If l->next is not null, we know that TEMP contained "$@", since that
4970 is the only expansion that creates more than one word. */
4971 if (qdollaratp && ((hasdol && quoted) || l->next))
4975 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && hasdol)
4977 /* The brace expansion occurred between double quotes and there was
4978 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
4979 it does not expand to anything. In this case, we want to return
4980 a quoted empty string. */
4981 temp = (char *)xmalloc (2);
4984 w->flags |= W_HASQUOTEDNULL;
4987 temp = (char *)NULL;
4989 if (c == '-' || c == '+')
4996 t = temp ? savestring (temp) : savestring ("");
4997 t1 = dequote_string (t);
4999 #if defined (ARRAY_VARS)
5000 if (valid_array_reference (name))
5001 assign_array_element (name, t1, 0);
5003 #endif /* ARRAY_VARS */
5004 bind_variable (name, t1, 0);
5011 /* Deal with the right hand side of a ${name:?value} expansion in the case
5012 that NAME is null or not set. If VALUE is non-null it is expanded and
5013 used as the error message to print, otherwise a standard message is
5016 parameter_brace_expand_error (name, value)
5022 if (value && *value)
5024 l = expand_string (value, 0);
5025 temp = string_list (l);
5026 report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */
5031 report_error (_("%s: parameter null or not set"), name);
5033 /* Free the data we have allocated during this expansion, since we
5034 are about to longjmp out. */
5039 /* Return 1 if NAME is something for which parameter_brace_expand_length is
5042 valid_length_expression (name)
5045 return (name[1] == '\0' || /* ${#} */
5046 ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */
5047 (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */
5048 #if defined (ARRAY_VARS)
5049 valid_array_reference (name + 1) || /* ${#a[7]} */
5051 legal_identifier (name + 1)); /* ${#PS1} */
5054 #if defined (HANDLE_MULTIBYTE)
5060 mbstate_t mbs, mbsbak;
5063 memset (&mbs, 0, sizeof (mbs));
5065 while ((clen = mbrlen(s, MB_CUR_MAX, &mbs)) != 0)
5067 if (MB_INVALIDCH(clen))
5069 clen = 1; /* assume single byte */
5082 /* Handle the parameter brace expansion that requires us to return the
5083 length of a parameter. */
5085 parameter_brace_expand_length (name)
5089 intmax_t number, arg_index;
5091 #if defined (ARRAY_VARS)
5095 if (name[1] == '\0') /* ${#} */
5096 number = number_of_args ();
5097 else if ((name[1] == '@' || name[1] == '*') && name[2] == '\0') /* ${#@}, ${#*} */
5098 number = number_of_args ();
5099 else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
5101 /* Take the lengths of some of the shell's special parameters. */
5105 t = which_set_flags ();
5108 t = itos (last_command_exit_value);
5111 t = itos (dollar_dollar_pid);
5114 if (last_asynchronous_pid == NO_PID)
5117 t = itos (last_asynchronous_pid);
5120 t = itos (number_of_args ());
5123 number = STRLEN (t);
5126 #if defined (ARRAY_VARS)
5127 else if (valid_array_reference (name + 1))
5128 number = array_length_reference (name + 1);
5129 #endif /* ARRAY_VARS */
5134 if (legal_number (name + 1, &arg_index)) /* ${#1} */
5136 t = get_dollar_var_value (arg_index);
5137 number = MB_STRLEN (t);
5140 #if defined (ARRAY_VARS)
5141 else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && array_p (var))
5143 t = array_reference (array_cell (var), 0);
5144 number = MB_STRLEN (t);
5149 newname = savestring (name);
5151 list = expand_string (newname, Q_DOUBLE_QUOTES);
5152 t = list ? string_list (list) : (char *)NULL;
5155 dispose_words (list);
5157 number = MB_STRLEN (t);
5165 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
5166 so we do some ad-hoc parsing of an arithmetic expression to find
5167 the first DELIM, instead of using strchr(3). Two rules:
5168 1. If the substring contains a `(', read until closing `)'.
5169 2. If the substring contains a `?', read past one `:' for each `?'.
5173 skiparith (substr, delim)
5178 int skipcol, pcount, i;
5181 sublen = strlen (substr);
5182 i = skipcol = pcount = 0;
5185 /* Balance parens */
5186 if (substr[i] == LPAREN)
5192 if (substr[i] == RPAREN && pcount)
5200 ADVANCE_CHAR (substr, sublen, i);
5204 /* Skip one `:' for each `?' */
5205 if (substr[i] == ':' && skipcol)
5211 if (substr[i] == delim)
5213 if (substr[i] == '?')
5219 ADVANCE_CHAR (substr, sublen, i);
5222 return (substr + i);
5225 /* Verify and limit the start and end of the desired substring. If
5226 VTYPE == 0, a regular shell variable is being used; if it is 1,
5227 then the positional parameters are being used; if it is 2, then
5228 VALUE is really a pointer to an array variable that should be used.
5229 Return value is 1 if both values were OK, 0 if there was a problem
5230 with an invalid expression, or -1 if the values were out of range. */
5232 verify_substring_values (value, substr, vtype, e1p, e2p)
5233 char *value, *substr;
5235 intmax_t *e1p, *e2p;
5237 char *t, *temp1, *temp2;
5240 #if defined (ARRAY_VARS)
5244 /* duplicate behavior of strchr(3) */
5245 t = skiparith (substr, ':');
5246 if (*t && *t == ':')
5251 temp1 = expand_string_if_necessary (substr, Q_DOUBLE_QUOTES, expand_string);
5252 *e1p = evalexp (temp1, &expok);
5257 len = -1; /* paranoia */
5261 case VT_ARRAYMEMBER:
5262 len = MB_STRLEN (value);
5265 len = number_of_args () + 1;
5267 #if defined (ARRAY_VARS)
5270 /* For arrays, the first value deals with array indices. Negative
5271 offsets count from one past the array's maximum index. */
5272 len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */
5277 if (len == -1) /* paranoia */
5280 if (*e1p < 0) /* negative offsets count from end */
5283 if (*e1p > len || *e1p < 0)
5286 #if defined (ARRAY_VARS)
5287 /* For arrays, the second offset deals with the number of elements. */
5288 if (vtype == VT_ARRAYVAR)
5289 len = array_num_elements (a);
5295 temp2 = savestring (t);
5296 temp1 = expand_string_if_necessary (temp2, Q_DOUBLE_QUOTES, expand_string);
5299 *e2p = evalexp (temp1, &expok);
5305 internal_error (_("%s: substring expression < 0"), t);
5308 #if defined (ARRAY_VARS)
5309 /* In order to deal with sparse arrays, push the intelligence about how
5310 to deal with the number of elements desired down to the array-
5311 specific functions. */
5312 if (vtype != VT_ARRAYVAR)
5315 *e2p += *e1p; /* want E2 chars starting at E1 */
5326 /* Return the type of variable specified by VARNAME (simple variable,
5327 positional param, or array variable). Also return the value specified
5328 by VARNAME (value of a variable or a reference to an array element).
5329 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
5330 characters in the value are quoted with CTLESC and takes appropriate
5331 steps. For convenience, *VALP is set to the dequoted VALUE. */
5333 get_var_and_type (varname, value, quoted, varp, valp)
5334 char *varname, *value;
5341 #if defined (ARRAY_VARS)
5345 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
5346 vtype = (varname[0] == '@' || varname[0] == '*') && varname[1] == '\0';
5347 if (vtype == VT_POSPARMS && varname[0] == '*')
5348 vtype |= VT_STARSUB;
5349 *varp = (SHELL_VAR *)NULL;
5351 #if defined (ARRAY_VARS)
5352 if (valid_array_reference (varname))
5354 v = array_variable_part (varname, &temp, (int *)0);
5355 if (v && array_p (v))
5357 if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')
5359 vtype = VT_ARRAYVAR;
5361 vtype |= VT_STARSUB;
5362 *valp = (char *)array_cell (v);
5366 vtype = VT_ARRAYMEMBER;
5367 *valp = array_value (varname, 1, (int *)NULL);
5371 else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']'))
5373 vtype = VT_VARIABLE;
5375 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
5376 *valp = dequote_string (value);
5378 *valp = dequote_escapes (value);
5383 else if ((v = find_variable (varname)) && (invisible_p (v) == 0) && array_p (v))
5385 vtype = VT_ARRAYMEMBER;
5387 *valp = array_reference (array_cell (v), 0);
5392 if (value && vtype == VT_VARIABLE)
5394 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
5395 *valp = dequote_string (value);
5397 *valp = dequote_escapes (value);
5406 /******************************************************/
5408 /* Functions to extract substrings of variable values */
5410 /******************************************************/
5412 #if defined (HANDLE_MULTIBYTE)
5413 /* Character-oriented rather than strictly byte-oriented substrings. S and
5414 E, rather being strict indices into STRING, indicate character (possibly
5415 multibyte character) positions that require calculation.
5416 Used by the ${param:offset[:length]} expansion. */
5418 mb_substring (string, s, e)
5423 int start, stop, i, slen;
5427 /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
5428 slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0;
5431 while (string[start] && i--)
5432 ADVANCE_CHAR (string, slen, start);
5435 while (string[stop] && i--)
5436 ADVANCE_CHAR (string, slen, stop);
5437 tt = substring (string, start, stop);
5442 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
5443 is `@', use the positional parameters; otherwise, use the value of
5444 VARNAME. If VARNAME is an array variable, use the array elements. */
5447 parameter_brace_substring (varname, value, substr, quoted)
5448 char *varname, *value, *substr;
5452 int vtype, r, starsub;
5453 char *temp, *val, *tt;
5457 return ((char *)NULL);
5459 this_command_name = varname;
5461 vtype = get_var_and_type (varname, value, quoted, &v, &val);
5463 return ((char *)NULL);
5465 starsub = vtype & VT_STARSUB;
5466 vtype &= ~VT_STARSUB;
5468 r = verify_substring_values (val, substr, vtype, &e1, &e2);
5470 return ((r == 0) ? &expand_param_error : (char *)NULL);
5475 case VT_ARRAYMEMBER:
5476 #if defined (HANDLE_MULTIBYTE)
5478 tt = mb_substring (val, e1, e2);
5481 tt = substring (val, e1, e2);
5483 if (vtype == VT_VARIABLE)
5485 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
5486 temp = quote_string (tt);
5488 temp = tt ? quote_escapes (tt) : (char *)NULL;
5492 tt = pos_params (varname, e1, e2, quoted);
5493 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
5495 temp = tt ? quote_escapes (tt) : (char *)NULL;
5501 #if defined (ARRAY_VARS)
5503 /* We want E2 to be the number of elements desired (arrays can be sparse,
5504 so verify_substring_values just returns the numbers specified and we
5505 rely on array_subrange to understand how to deal with them). */
5506 tt = array_subrange (array_cell (v), e1, e2, starsub, quoted);
5507 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
5509 temp = tt ? quote_escapes (tt) : (char *)NULL;
5517 temp = (char *)NULL;
5523 /****************************************************************/
5525 /* Functions to perform pattern substitution on variable values */
5527 /****************************************************************/
5530 pat_subst (string, pat, rep, mflags)
5531 char *string, *pat, *rep;
5534 char *ret, *s, *e, *str;
5535 int rsize, rptr, l, replen, mtype;
5537 mtype = mflags & MATCH_TYPEMASK;
5540 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
5541 * with REP and return the result.
5542 * 2. A null pattern with mtype == MATCH_END means to append REP to
5543 * STRING and return the result.
5545 if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
5547 replen = STRLEN (rep);
5548 l = strlen (string);
5549 ret = (char *)xmalloc (replen + l + 2);
5551 strcpy (ret, string);
5552 else if (mtype == MATCH_BEG)
5555 strcpy (ret + replen, string);
5559 strcpy (ret, string);
5560 strcpy (ret + l, rep);
5565 ret = (char *)xmalloc (rsize = 64);
5568 for (replen = STRLEN (rep), rptr = 0, str = string;;)
5570 if (match_pattern (str, pat, mtype, &s, &e) == 0)
5573 RESIZE_MALLOCED_BUFFER (ret, rptr, (l + replen), rsize, 64);
5575 /* OK, now copy the leading unmatched portion of the string (from
5576 str to s) to ret starting at rptr (the current offset). Then copy
5577 the replacement string at ret + rptr + (s - str). Increment
5578 rptr (if necessary) and str and go on. */
5581 strncpy (ret + rptr, str, l);
5586 strncpy (ret + rptr, rep, replen);
5589 str = e; /* e == end of match */
5591 if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
5595 e++, str++; /* avoid infinite recursion on zero-length match */
5598 /* Now copy the unmatched portion of the input string */
5601 RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
5602 strcpy (ret + rptr, str);
5610 /* Do pattern match and replacement on the positional parameters. */
5612 pos_params_pat_subst (string, pat, rep, mflags)
5613 char *string, *pat, *rep;
5616 WORD_LIST *save, *params;
5620 save = params = list_rest_of_args ();
5622 return ((char *)NULL);
5624 for ( ; params; params = params->next)
5626 ret = pat_subst (params->word->word, pat, rep, mflags);
5627 w = alloc_word_desc ();
5628 w->word = ret ? ret : savestring ("");
5629 dispose_word (params->word);
5633 if ((mflags & (MATCH_QUOTED|MATCH_STARSUB)) == (MATCH_QUOTED|MATCH_STARSUB))
5634 ret = string_list_dollar_star (quote_list (save));
5636 ret = string_list ((mflags & MATCH_QUOTED) ? quote_list (save) : save);
5637 dispose_words (save);
5642 /* Perform pattern substitution on VALUE, which is the expansion of
5643 VARNAME. PATSUB is an expression supplying the pattern to match
5644 and the string to substitute. QUOTED is a flags word containing
5645 the type of quoting currently in effect. */
5647 parameter_brace_patsub (varname, value, patsub, quoted)
5648 char *varname, *value, *patsub;
5651 int vtype, mflags, starsub;
5652 char *val, *temp, *pat, *rep, *p, *lpatsub, *tt;
5656 return ((char *)NULL);
5658 this_command_name = varname;
5660 vtype = get_var_and_type (varname, value, quoted, &v, &val);
5662 return ((char *)NULL);
5664 starsub = vtype & VT_STARSUB;
5665 vtype &= ~VT_STARSUB;
5670 mflags |= MATCH_GLOBREP;
5674 /* Malloc this because expand_string_if_necessary or one of the expansion
5675 functions in its call chain may free it on a substitution error. */
5676 lpatsub = savestring (patsub);
5678 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
5679 mflags |= MATCH_QUOTED;
5682 mflags |= MATCH_STARSUB;
5684 if (rep = quoted_strchr (lpatsub, '/', ST_BACKSL))
5689 if (rep && *rep == '\0')
5692 /* Perform the same expansions on the pattern as performed by the
5693 pattern removal expansions. */
5694 pat = getpattern (lpatsub, quoted, 1);
5698 if ((mflags & MATCH_QUOTED) == 0)
5699 rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
5701 rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
5705 if (pat && pat[0] == '#')
5707 mflags |= MATCH_BEG;
5710 else if (pat && pat[0] == '%')
5712 mflags |= MATCH_END;
5716 mflags |= MATCH_ANY;
5718 /* OK, we now want to substitute REP for PAT in VAL. If
5719 flags & MATCH_GLOBREP is non-zero, the substitution is done
5720 everywhere, otherwise only the first occurrence of PAT is
5721 replaced. The pattern matching code doesn't understand
5722 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
5723 values passed in (VT_VARIABLE) so the pattern substitution
5724 code works right. We need to requote special chars after
5725 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
5726 other cases if QUOTED == 0, since the posparams and arrays
5727 indexed by * or @ do special things when QUOTED != 0. */
5732 case VT_ARRAYMEMBER:
5733 temp = pat_subst (val, p, rep, mflags);
5734 if (vtype == VT_VARIABLE)
5738 tt = quote_escapes (temp);
5744 temp = pos_params_pat_subst (val, p, rep, mflags);
5745 if (temp && (mflags & MATCH_QUOTED) == 0)
5747 tt = quote_escapes (temp);
5752 #if defined (ARRAY_VARS)
5754 temp = array_patsub (array_cell (v), p, rep, mflags);
5755 if (temp && (mflags & MATCH_QUOTED) == 0)
5757 tt = quote_escapes (temp);
5772 /****************************************************************/
5774 /* Functions to perform parameter expansion on a string */
5776 /****************************************************************/
5778 /* ${[#][!]name[[:]#[#]%[%]-=?+[word][:e1[:e2]]]} */
5780 parameter_brace_expand (string, indexp, quoted, quoted_dollar_atp, contains_dollar_at)
5782 int *indexp, quoted, *quoted_dollar_atp, *contains_dollar_at;
5784 int check_nullness, var_is_set, var_is_null, var_is_special;
5785 int want_substring, want_indir, want_patsub;
5786 char *name, *value, *temp, *temp1;
5787 WORD_DESC *tdesc, *ret;
5788 int t_index, sindex, c, tflag;
5791 value = (char *)NULL;
5792 var_is_set = var_is_null = var_is_special = check_nullness = 0;
5793 want_substring = want_indir = want_patsub = 0;
5797 name = string_extract (string, &t_index, "#%:-=?+/}", EX_VARNAME);
5802 /* If the name really consists of a special variable, then make sure
5803 that we have the entire name. We don't allow indirect references
5804 to special variables except `#', `?', `@' and `*'. */
5805 if ((sindex == t_index &&
5806 (string[t_index] == '-' ||
5807 string[t_index] == '?' ||
5808 string[t_index] == '#')) ||
5809 (sindex == t_index - 1 && string[sindex] == '!' &&
5810 (string[t_index] == '#' ||
5811 string[t_index] == '?' ||
5812 string[t_index] == '@' ||
5813 string[t_index] == '*')))
5817 temp1 = string_extract (string, &t_index, "#%:-=?+/}", 0);
5818 name = (char *)xmalloc (3 + (strlen (temp1)));
5819 *name = string[sindex];
5820 if (string[sindex] == '!')
5822 /* indirect reference of $#, $?, $@, or $* */
5823 name[1] = string[sindex + 1];
5824 strcpy (name + 2, temp1);
5827 strcpy (name + 1, temp1);
5832 /* Find out what character ended the variable name. Then
5833 do the appropriate thing. */
5834 if (c = string[sindex])
5837 /* If c is followed by one of the valid parameter expansion
5838 characters, move past it as normal. If not, assume that
5839 a substring specification is being given, and do not move
5841 if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
5844 if (c = string[sindex])
5847 else if (c == ':' && string[sindex] != RBRACE)
5849 else if (c == '/' && string[sindex] != RBRACE)
5852 /* Catch the valid and invalid brace expressions that made it through the
5854 /* ${#-} is a valid expansion and means to take the length of $-.
5855 Similarly for ${#?} and ${##}... */
5856 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
5857 VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
5859 name = (char *)xrealloc (name, 3);
5862 c = string[sindex++];
5865 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
5866 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
5867 member (c, "%:=+/") && string[sindex] == RBRACE)
5869 temp = (char *)NULL;
5870 goto bad_substitution;
5873 /* Indirect expansion begins with a `!'. A valid indirect expansion is
5874 either a variable name, one of the positional parameters or a special
5875 variable that expands to one of the positional parameters. */
5876 want_indir = *name == '!' &&
5877 (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
5878 || VALID_INDIR_PARAM (name[1]));
5880 /* Determine the value of this variable. */
5882 /* Check for special variables, directly referenced. */
5883 if (SPECIAL_VAR (name, want_indir))
5886 /* Check for special expansion things, like the length of a parameter */
5887 if (*name == '#' && name[1])
5889 /* If we are not pointing at the character just after the
5890 closing brace, then we haven't gotten all of the name.
5891 Since it begins with a special character, this is a bad
5892 substitution. Also check NAME for validity before trying
5894 if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
5896 temp = (char *)NULL;
5897 goto bad_substitution;
5900 number = parameter_brace_expand_length (name);
5905 return (&expand_wdesc_error);
5908 ret = alloc_word_desc ();
5909 ret->word = itos (number);
5914 /* ${@} is identical to $@. */
5915 if (name[0] == '@' && name[1] == '\0')
5917 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
5918 *quoted_dollar_atp = 1;
5920 if (contains_dollar_at)
5921 *contains_dollar_at = 1;
5924 /* Process ${!PREFIX*} expansion. */
5925 if (want_indir && string[sindex - 1] == RBRACE &&
5926 (string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
5927 legal_variable_starter ((unsigned char) name[1]))
5932 temp1 = savestring (name + 1);
5933 number = strlen (temp1);
5934 temp1[number - 1] = '\0';
5935 x = all_variables_matching_prefix (temp1);
5936 xlist = strvec_to_word_list (x, 0, 0);
5937 if (string[sindex - 2] == '*')
5938 temp = string_list_dollar_star (xlist);
5941 temp = string_list_dollar_at (xlist, quoted);
5942 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
5943 *quoted_dollar_atp = 1;
5944 if (contains_dollar_at)
5945 *contains_dollar_at = 1;
5952 ret = alloc_word_desc ();
5957 #if defined (ARRAY_VARS)
5958 /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */
5959 if (want_indir && string[sindex - 1] == RBRACE &&
5960 string[sindex - 2] == ']' && valid_array_reference (name+1))
5964 temp1 = savestring (name + 1);
5965 x = array_variable_name (temp1, &x1, (int *)0); /* [ */
5967 if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == ']')
5969 temp = array_keys (temp1, quoted);
5972 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
5973 *quoted_dollar_atp = 1;
5974 if (contains_dollar_at)
5975 *contains_dollar_at = 1;
5981 ret = alloc_word_desc ();
5988 #endif /* ARRAY_VARS */
5990 /* Make sure that NAME is valid before trying to go on. */
5991 if (valid_brace_expansion_word (want_indir ? name + 1 : name,
5992 var_is_special) == 0)
5994 temp = (char *)NULL;
5995 goto bad_substitution;
5999 tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at);
6001 tdesc = parameter_brace_expand_word (name, var_is_special, quoted);
6006 tflag = tdesc->flags;
6007 dispose_word_desc (tdesc);
6012 #if defined (ARRAY_VARS)
6013 if (valid_array_reference (name))
6014 chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at);
6017 var_is_set = temp != (char *)0;
6018 var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
6020 /* Get the rest of the stuff inside the braces. */
6021 if (c && c != RBRACE)
6023 /* Extract the contents of the ${ ... } expansion
6024 according to the Posix.2 rules. */
6025 value = extract_dollar_brace_string (string, &sindex, quoted, 0);
6026 if (string[sindex] == RBRACE)
6029 goto bad_substitution;
6032 value = (char *)NULL;
6036 /* If this is a substring spec, process it and add the result. */
6039 temp1 = parameter_brace_substring (name, temp, value, quoted);
6044 if (temp1 == &expand_param_error)
6045 return (&expand_wdesc_error);
6046 else if (temp1 == &expand_param_fatal)
6047 return (&expand_wdesc_fatal);
6049 ret = alloc_word_desc ();
6053 else if (want_patsub)
6055 temp1 = parameter_brace_patsub (name, temp, value, quoted);
6060 if (temp1 == &expand_param_error)
6061 return (&expand_wdesc_error);
6062 else if (temp1 == &expand_param_fatal)
6063 return (&expand_wdesc_fatal);
6065 ret = alloc_word_desc ();
6070 /* Do the right thing based on which character ended the variable name. */
6076 report_error (_("%s: bad substitution"), string ? string : "??");
6080 return &expand_wdesc_error;
6083 if (var_is_set == 0 && unbound_vars_is_error)
6085 err_unboundvar (name);
6089 last_command_exit_value = EXECUTION_FAILURE;
6090 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
6094 case '#': /* ${param#[#]pattern} */
6095 case '%': /* ${param%[%]pattern} */
6096 if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
6101 temp1 = parameter_brace_remove_pattern (name, temp, value, c, quoted);
6111 if (var_is_set && var_is_null == 0)
6113 /* If the operator is `+', we don't want the value of the named
6114 variable for anything, just the value of the right hand side. */
6118 /* XXX -- if we're double-quoted and the named variable is "$@",
6119 we want to turn off any special handling of "$@" --
6120 we're not using it, so whatever is on the rhs applies. */
6121 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
6122 *quoted_dollar_atp = 0;
6123 if (contains_dollar_at)
6124 *contains_dollar_at = 0;
6129 ret = parameter_brace_expand_rhs (name, value, c,
6132 contains_dollar_at);
6133 /* XXX - fix up later, esp. noting presence of
6134 W_HASQUOTEDNULL in ret->flags */
6138 temp = (char *)NULL;
6144 /* Otherwise do nothing; just use the value in TEMP. */
6146 else /* VAR not set or VAR is NULL. */
6149 temp = (char *)NULL;
6150 if (c == '=' && var_is_special)
6152 report_error (_("$%s: cannot assign in this way"), name);
6155 return &expand_wdesc_error;
6159 parameter_brace_expand_error (name, value);
6160 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
6164 /* XXX -- if we're double-quoted and the named variable is "$@",
6165 we want to turn off any special handling of "$@" --
6166 we're not using it, so whatever is on the rhs applies. */
6167 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
6168 *quoted_dollar_atp = 0;
6169 if (contains_dollar_at)
6170 *contains_dollar_at = 0;
6172 ret = parameter_brace_expand_rhs (name, value, c, quoted,
6174 contains_dollar_at);
6175 /* XXX - fix up later, esp. noting presence of
6176 W_HASQUOTEDNULL in tdesc->flags */
6187 ret = alloc_word_desc ();
6194 /* Expand a single ${xxx} expansion. The braces are optional. When
6195 the braces are used, parameter_brace_expand() does the work,
6196 possibly calling param_expand recursively. */
6198 param_expand (string, sindex, quoted, expanded_something,
6199 contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
6202 int *sindex, quoted, *expanded_something, *contains_dollar_at;
6203 int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
6205 char *temp, *temp1, uerror[3];
6206 int zindex, t_index, expok;
6211 WORD_DESC *tdesc, *ret;
6215 c = string[++zindex];
6217 temp = (char *)NULL;
6218 ret = tdesc = (WORD_DESC *)NULL;
6221 /* Do simple cases first. Switch on what follows '$'. */
6235 temp1 = dollar_vars[TODIGIT (c)];
6236 if (unbound_vars_is_error && temp1 == (char *)NULL)
6241 err_unboundvar (uerror);
6242 last_command_exit_value = EXECUTION_FAILURE;
6243 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
6246 temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
6247 ? quote_string (temp1)
6248 : quote_escapes (temp1);
6250 temp = (char *)NULL;
6254 /* $$ -- pid of the invoking shell. */
6256 temp = itos (dollar_dollar_pid);
6259 /* $# -- number of positional parameters. */
6261 temp = itos (number_of_args ());
6264 /* $? -- return value of the last synchronous command. */
6266 temp = itos (last_command_exit_value);
6269 /* $- -- flags supplied to the shell on invocation or by `set'. */
6271 temp = which_set_flags ();
6274 /* $! -- Pid of the last asynchronous command. */
6276 /* If no asynchronous pids have been created, expand to nothing.
6277 If `set -u' has been executed, and no async processes have
6278 been created, this is an expansion error. */
6279 if (last_asynchronous_pid == NO_PID)
6281 if (expanded_something)
6282 *expanded_something = 0;
6283 temp = (char *)NULL;
6284 if (unbound_vars_is_error)
6289 err_unboundvar (uerror);
6290 last_command_exit_value = EXECUTION_FAILURE;
6291 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
6295 temp = itos (last_asynchronous_pid);
6298 /* The only difference between this and $@ is when the arg is quoted. */
6299 case '*': /* `$*' */
6300 list = list_rest_of_args ();
6302 /* If there are no command-line arguments, this should just
6303 disappear if there are other characters in the expansion,
6304 even if it's quoted. */
6305 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
6306 temp = (char *)NULL;
6307 else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6309 /* If we have "$*" we want to make a string of the positional
6310 parameters, separated by the first character of $IFS, and
6311 quote the whole string, including the separators. If IFS
6312 is unset, the parameters are separated by ' '; if $IFS is
6313 null, the parameters are concatenated. */
6314 temp = (quoted & Q_DOUBLE_QUOTES) ? string_list_dollar_star (list) : string_list (list);
6315 temp1 = quote_string (temp);
6317 tflag |= W_HASQUOTEDNULL;
6323 /* We check whether or not we're eventually going to split $* here,
6324 for example when IFS is empty and we are processing the rhs of
6325 an assignment statement. In that case, we don't separate the
6326 arguments at all. Otherwise, if the $* is not quoted it is
6329 # if defined (HANDLE_MULTIBYTE)
6330 if (expand_no_split_dollar_star && ifs_firstc[0] == 0)
6332 if (expand_no_split_dollar_star && ifs_firstc == 0)
6334 temp = string_list_dollar_star (list);
6336 temp = string_list_dollar_at (list, quoted);
6338 temp = string_list_dollar_at (list, quoted);
6340 if (expand_no_split_dollar_star == 0 && contains_dollar_at)
6341 *contains_dollar_at = 1;
6344 dispose_words (list);
6347 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
6348 means that we have to turn quoting off after we split into
6349 the individually quoted arguments so that the final split
6350 on the first character of $IFS is still done. */
6351 case '@': /* `$@' */
6352 list = list_rest_of_args ();
6354 /* We want to flag the fact that we saw this. We can't turn
6355 off quoting entirely, because other characters in the
6356 string might need it (consider "\"$@\""), but we need some
6357 way to signal that the final split on the first character
6358 of $IFS should be done, even though QUOTED is 1. */
6359 if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
6360 *quoted_dollar_at_p = 1;
6361 if (contains_dollar_at)
6362 *contains_dollar_at = 1;
6364 /* We want to separate the positional parameters with the first
6365 character of $IFS in case $IFS is something other than a space.
6366 We also want to make sure that splitting is done no matter what --
6367 according to POSIX.2, this expands to a list of the positional
6368 parameters no matter what IFS is set to. */
6369 temp = string_list_dollar_at (list, quoted);
6371 dispose_words (list);
6375 tdesc = parameter_brace_expand (string, &zindex, quoted,
6377 contains_dollar_at);
6379 /* Fix this later when parameter_brace_expand returns a WORD_DESC * */
6380 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
6382 temp = tdesc ? tdesc->word : (char *)0;
6385 /* Quoted nulls should be removed if there is anything else
6387 /* Note that we saw the quoted null so we can add one back at
6388 the end of this function if there are no other characters
6389 in the string, discard TEMP, and go on. The exception to
6390 this is when we have "${@}" and $1 is '', since $@ needs
6391 special handling. */
6392 /* XXX - fix this once parameter_brace_expand returns a WORD_DESC * */
6394 if (temp && QUOTED_NULL (temp))
6396 if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp))
6399 if (had_quoted_null_p)
6400 *had_quoted_null_p = 1;
6401 if (*quoted_dollar_at_p == 0)
6404 tdesc->word = temp = (char *)NULL;
6412 /* Do command or arithmetic substitution. */
6414 /* We have to extract the contents of this paren substitution. */
6415 t_index = zindex + 1;
6416 temp = extract_command_subst (string, &t_index);
6419 /* For Posix.2-style `$(( ))' arithmetic substitution,
6420 extract the expression and pass it to the evaluator. */
6421 if (temp && *temp == LPAREN)
6425 temp2 = savestring (temp1);
6426 t_index = strlen (temp2) - 1;
6428 if (temp2[t_index] != RPAREN)
6434 /* Cut off ending `)' */
6435 temp2[t_index] = '\0';
6437 /* Expand variables found inside the expression. */
6438 temp1 = expand_string_if_necessary (temp2, Q_DOUBLE_QUOTES, expand_string);
6442 /* No error messages. */
6443 this_command_name = (char *)NULL;
6444 number = evalexp (temp1, &expok);
6449 if (interactive_shell == 0 && posixly_correct)
6451 last_command_exit_value = EXECUTION_FAILURE;
6452 return (&expand_wdesc_fatal);
6455 return (&expand_wdesc_error);
6457 temp = itos (number);
6462 if (pflags & PF_NOCOMSUB)
6463 /* we need zindex+1 because string[zindex] == RPAREN */
6464 temp1 = substring (string, *sindex, zindex+1);
6466 temp1 = command_substitute (temp, quoted);
6471 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
6472 away in a future bash release. */
6474 /* Extract the contents of this arithmetic substitution. */
6475 t_index = zindex + 1;
6476 temp = extract_arithmetic_subst (string, &t_index);
6479 /* Do initial variable expansion. */
6480 temp1 = expand_string_if_necessary (temp, Q_DOUBLE_QUOTES, expand_string);
6485 /* Find the variable in VARIABLE_LIST. */
6486 temp = (char *)NULL;
6488 for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
6490 temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
6492 /* If this isn't a variable name, then just output the `$'. */
6493 if (temp1 == 0 || *temp1 == '\0')
6496 temp = (char *)xmalloc (2);
6499 if (expanded_something)
6500 *expanded_something = 0;
6504 /* If the variable exists, return its value cell. */
6505 var = find_variable (temp1);
6507 if (var && invisible_p (var) == 0 && var_isset (var))
6509 #if defined (ARRAY_VARS)
6512 temp = array_reference (array_cell (var), 0);
6514 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
6515 ? quote_string (temp)
6516 : quote_escapes (temp);
6517 else if (unbound_vars_is_error)
6518 goto unbound_variable;
6523 temp = value_cell (var);
6525 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
6526 ? quote_string (temp)
6527 : quote_escapes (temp);
6535 temp = (char *)NULL;
6538 if (unbound_vars_is_error)
6539 err_unboundvar (temp1);
6547 last_command_exit_value = EXECUTION_FAILURE;
6548 return ((unbound_vars_is_error && interactive_shell == 0)
6549 ? &expand_wdesc_fatal
6550 : &expand_wdesc_error);
6561 ret = alloc_word_desc ();
6562 ret->flags = tflag; /* XXX */
6568 /* Make a word list which is the result of parameter and variable
6569 expansion, command substitution, arithmetic substitution, and
6570 quote removal of WORD. Return a pointer to a WORD_LIST which is
6571 the result of the expansion. If WORD contains a null word, the
6572 word list returned is also null.
6574 QUOTED contains flag values defined in shell.h.
6576 ISEXP is used to tell expand_word_internal that the word should be
6577 treated as the result of an expansion. This has implications for
6578 how IFS characters in the word are treated.
6580 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
6581 they point to an integer value which receives information about expansion.
6582 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
6583 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
6586 This only does word splitting in the case of $@ expansion. In that
6587 case, we split on ' '. */
6589 /* Values for the local variable quoted_state. */
6591 #define PARTIALLY_QUOTED 1
6592 #define WHOLLY_QUOTED 2
6595 expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
6598 int *contains_dollar_at;
6599 int *expanded_something;
6604 /* The intermediate string that we build while expanding. */
6607 /* The current size of the above object. */
6610 /* Index into ISTRING. */
6613 /* Temporary string storage. */
6616 /* The text of WORD. */
6617 register char *string;
6619 /* The size of STRING. */
6622 /* The index into STRING. */
6625 /* This gets 1 if we see a $@ while quoted. */
6626 int quoted_dollar_at;
6628 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
6629 whether WORD contains no quoting characters, a partially quoted
6630 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
6634 int had_quoted_null;
6638 int assignoff; /* If assignment, offset of `=' */
6640 register unsigned char c; /* Current character. */
6641 int t_index; /* For calls to string_extract_xxx. */
6647 istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
6648 istring[istring_index = 0] = '\0';
6649 quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
6650 quoted_state = UNQUOTED;
6652 string = word->word;
6654 goto finished_with_string;
6655 /* Don't need the string length for the SADD... and COPY_ macros unless
6656 multibyte characters are possible. */
6657 string_size = (MB_CUR_MAX > 1) ? strlen (string) : 1;
6659 if (contains_dollar_at)
6660 *contains_dollar_at = 0;
6664 /* Begin the expansion. */
6670 /* Case on toplevel character. */
6674 goto finished_with_string;
6678 #if HANDLE_MULTIBYTE
6679 if (MB_CUR_MAX > 1 && string[sindex])
6681 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
6686 temp = (char *)xmalloc (3);
6688 temp[1] = c = string[sindex];
6699 istring = sub_append_string (temp, istring, &istring_index, &istring_size);
6705 #if defined (PROCESS_SUBSTITUTION)
6706 /* Process substitution. */
6710 if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & W_DQUOTE) || posixly_correct)
6712 sindex--; /* add_character: label increments sindex */
6716 t_index = sindex + 1; /* skip past both '<' and LPAREN */
6718 temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index); /*))*/
6721 /* If the process substitution specification is `<()', we want to
6722 open the pipe for writing in the child and produce output; if
6723 it is `>()', we want to open the pipe for reading in the child
6724 and consume input. */
6725 temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
6729 goto dollar_add_string;
6731 #endif /* PROCESS_SUBSTITUTION */
6734 /* Posix.2 section 3.6.1 says that tildes following `=' in words
6735 which are not assignment statements are not expanded. If the
6736 shell isn't in posix mode, though, we perform tilde expansion
6737 on `likely candidate' unquoted assignment statements (flags
6738 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
6739 contains an unquoted :~ or =~. Something to think about: we
6740 now have a flag that says to perform tilde expansion on arguments
6741 to `assignment builtins' like declare and export that look like
6742 assignment statements. We now do tilde expansion on such words
6743 even in POSIX mode. */
6744 if (word->flags & (W_ASSIGNRHS|W_NOTILDE))
6746 /* If we're not in posix mode or forcing assignment-statement tilde
6747 expansion, note where the `=' appears in the word and prepare to
6748 do tilde expansion following the first `='. */
6749 if ((word->flags & W_ASSIGNMENT) &&
6750 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
6751 assignoff == -1 && sindex > 0)
6753 if (sindex == assignoff && string[sindex+1] == '~') /* XXX */
6754 word->flags |= W_ITILDE;
6756 else if ((word->flags & W_ASSIGNMENT) &&
6757 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
6758 string[sindex+1] == '~')
6759 word->flags |= W_ITILDE;
6764 if (word->flags & W_NOTILDE)
6767 if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) &&
6768 string[sindex+1] == '~')
6769 word->flags |= W_ITILDE;
6773 /* If the word isn't supposed to be tilde expanded, or we're not
6774 at the start of a word or after an unquoted : or = in an
6775 assignment statement, we don't do tilde expansion. */
6776 if ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
6777 (sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
6778 (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6780 word->flags &= ~W_ITILDE;
6784 if (word->flags & W_ASSIGNRHS)
6786 else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
6791 temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
6793 word->flags &= ~W_ITILDE;
6795 if (temp && *temp && t_index > 0)
6797 temp1 = bash_tilde_expand (temp, tflag);
6810 if (expanded_something)
6811 *expanded_something = 1;
6814 tword = param_expand (string, &sindex, quoted, expanded_something,
6815 &has_dollar_at, "ed_dollar_at,
6817 (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0);
6819 if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
6823 return ((tword == &expand_wdesc_error) ? &expand_word_error
6824 : &expand_word_fatal);
6826 if (contains_dollar_at && has_dollar_at)
6827 *contains_dollar_at = 1;
6829 if (tword && (tword->flags & W_HASQUOTEDNULL))
6830 had_quoted_null = 1;
6833 dispose_word_desc (tword);
6838 case '`': /* Backquoted command substitution. */
6842 temp = string_extract (string, &sindex, "`", EX_REQMATCH);
6843 /* The test of sindex against t_index is to allow bare instances of
6844 ` to pass through, for backwards compatibility. */
6845 if (temp == &extract_string_error || temp == &extract_string_fatal)
6847 if (sindex - 1 == t_index)
6852 report_error ("bad substitution: no closing \"`\" in %s", string+t_index);
6855 return ((temp == &extract_string_error) ? &expand_word_error
6856 : &expand_word_fatal);
6859 if (expanded_something)
6860 *expanded_something = 1;
6862 if (word->flags & W_NOCOMSUB)
6863 /* sindex + 1 because string[sindex] == '`' */
6864 temp1 = substring (string, t_index, sindex + 1);
6867 de_backslash (temp);
6868 temp1 = command_substitute (temp, quoted);
6872 goto dollar_add_string;
6876 if (string[sindex + 1] == '\n')
6882 c = string[++sindex];
6884 if (quoted & Q_HERE_DOCUMENT)
6886 else if (quoted & Q_DOUBLE_QUOTES)
6891 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
6893 SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
6898 sindex--; /* add_character: label increments sindex */
6903 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
6908 /* BEFORE jumping here, we need to increment sindex if appropriate */
6909 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
6910 DEFAULT_ARRAY_SIZE);
6911 istring[istring_index++] = twochars[0];
6912 istring[istring_index++] = twochars[1];
6913 istring[istring_index] = '\0';
6919 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
6921 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6926 temp = string_extract_double_quoted (string, &sindex, 0);
6928 /* If the quotes surrounded the entire string, then the
6929 whole word was quoted. */
6930 quoted_state = (t_index == 1 && string[sindex] == '\0')
6936 tword = alloc_word_desc ();
6939 temp = (char *)NULL;
6942 /* Need to get W_HASQUOTEDNULL flag through this function. */
6943 list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &has_dollar_at, (int *)NULL);
6945 if (list == &expand_word_error || list == &expand_word_fatal)
6949 /* expand_word_internal has already freed temp_word->word
6950 for us because of the way it prints error messages. */
6951 tword->word = (char *)NULL;
6952 dispose_word (tword);
6956 dispose_word (tword);
6958 /* "$@" (a double-quoted dollar-at) expands into nothing,
6959 not even a NULL word, when there are no positional
6961 if (list == 0 && has_dollar_at)
6967 /* If we get "$@", we know we have expanded something, so we
6968 need to remember it for the final split on $IFS. This is
6969 a special case; it's the only case where a quoted string
6970 can expand into more than one word. It's going to come back
6971 from the above call to expand_word_internal as a list with
6972 a single word, in which all characters are quoted and
6973 separated by blanks. What we want to do is to turn it back
6974 into a list for the next piece of code. */
6976 dequote_list (list);
6978 if (list && list->word && (list->word->flags & W_HASQUOTEDNULL))
6979 had_quoted_null = 1;
6984 if (contains_dollar_at)
6985 *contains_dollar_at = 1;
6986 if (expanded_something)
6987 *expanded_something = 1;
6992 /* What we have is "". This is a minor optimization. */
6994 list = (WORD_LIST *)NULL;
6997 /* The code above *might* return a list (consider the case of "$@",
6998 where it returns "$1", "$2", etc.). We can't throw away the
6999 rest of the list, and we have to make sure each word gets added
7000 as quoted. We test on tresult->next: if it is non-NULL, we
7001 quote the whole list, save it to a string with string_list, and
7002 add that string. We don't need to quote the results of this
7003 (and it would be wrong, since that would quote the separators
7004 as well), so we go directly to add_string. */
7009 /* Testing quoted_dollar_at makes sure that "$@" is
7010 split correctly when $IFS does not contain a space. */
7011 temp = quoted_dollar_at
7012 ? string_list_dollar_at (list, Q_DOUBLE_QUOTES)
7013 : string_list (quote_list (list));
7014 dispose_words (list);
7019 temp = savestring (list->word->word);
7020 tflag = list->word->flags;
7021 dispose_words (list);
7023 /* If the string is not a quoted null string, we want
7024 to remove any embedded unquoted CTLNUL characters.
7025 We do not want to turn quoted null strings back into
7026 the empty string, though. We do this because we
7027 want to remove any quoted nulls from expansions that
7028 contain other characters. For example, if we have
7029 x"$*"y or "x$*y" and there are no positional parameters,
7030 the $* should expand into nothing. */
7031 /* We use the W_HASQUOTEDNULL flag to differentiate the
7032 cases: a quoted null character as above and when
7033 CTLNUL is contained in the (non-null) expansion
7034 of some variable. We use the had_quoted_null flag to
7035 pass the value through this function to its caller. */
7036 if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0)
7037 remove_quoted_nulls (temp); /* XXX */
7041 temp = (char *)NULL;
7043 /* We do not want to add quoted nulls to strings that are only
7044 partially quoted; we can throw them away. */
7045 if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
7053 temp = quote_string (temp);
7061 sindex--; /* add_character: label increments sindex */
7069 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
7071 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
7076 temp = string_extract_single_quoted (string, &sindex);
7078 /* If the entire STRING was surrounded by single quotes,
7079 then the string is wholly quoted. */
7080 quoted_state = (t_index == 1 && string[sindex] == '\0')
7084 /* If all we had was '', it is a null expansion. */
7088 temp = (char *)NULL;
7091 remove_quoted_escapes (temp); /* ??? */
7093 /* We do not want to add quoted nulls to strings that are only
7094 partially quoted; such nulls are discarded. */
7095 if (temp == 0 && (quoted_state == PARTIALLY_QUOTED))
7098 /* If we have a quoted null expansion, add a quoted NULL to istring. */
7102 sindex--; /* add_character: label increments sindex */
7106 goto add_quoted_string;
7111 /* This is the fix for " $@ " */
7112 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c)))
7114 if (string[sindex]) /* from old goto dollar_add_string */
7123 #if HANDLE_MULTIBYTE
7129 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
7134 twochars[0] = CTLESC;
7141 SADD_MBCHAR (temp, string, sindex, string_size);
7144 RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
7145 DEFAULT_ARRAY_SIZE);
7146 istring[istring_index++] = c;
7147 istring[istring_index] = '\0';
7149 /* Next character. */
7154 finished_with_string:
7155 /* OK, we're ready to return. If we have a quoted string, and
7156 quoted_dollar_at is not set, we do no splitting at all; otherwise
7157 we split on ' '. The routines that call this will handle what to
7158 do if nothing has been expanded. */
7160 /* Partially and wholly quoted strings which expand to the empty
7161 string are retained as an empty arguments. Unquoted strings
7162 which expand to the empty string are discarded. The single
7163 exception is the case of expanding "$@" when there are no
7164 positional parameters. In that case, we discard the expansion. */
7166 /* Because of how the code that handles "" and '' in partially
7167 quoted strings works, we need to make ISTRING into a QUOTED_NULL
7168 if we saw quoting characters, but the expansion was empty.
7169 "" and '' are tossed away before we get to this point when
7170 processing partially quoted strings. This makes "" and $xxx""
7171 equivalent when xxx is unset. We also look to see whether we
7172 saw a quoted null from a ${} expansion and add one back if we
7175 /* If we expand to nothing and there were no single or double quotes
7176 in the word, we throw it away. Otherwise, we return a NULL word.
7177 The single exception is for $@ surrounded by double quotes when
7178 there are no positional parameters. In that case, we also throw
7181 if (*istring == '\0')
7183 if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
7185 istring[0] = CTLNUL;
7187 tword = make_bare_word (istring);
7188 tword->flags |= W_HASQUOTEDNULL; /* XXX */
7189 list = make_word_list (tword, (WORD_LIST *)NULL);
7190 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7191 tword->flags |= W_QUOTED;
7193 /* According to sh, ksh, and Posix.2, if a word expands into nothing
7194 and a double-quoted "$@" appears anywhere in it, then the entire
7196 else if (quoted_state == UNQUOTED || quoted_dollar_at)
7197 list = (WORD_LIST *)NULL;
7201 tword = make_bare_word (istring);
7202 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7203 tword->flags |= W_QUOTED;
7204 list = make_word_list (tword, (WORD_LIST *)NULL);
7208 list = (WORD_LIST *)NULL;
7211 else if (word->flags & W_NOSPLIT)
7213 tword = make_bare_word (istring);
7214 if (word->flags & W_ASSIGNMENT)
7215 tword->flags |= W_ASSIGNMENT; /* XXX */
7216 if (word->flags & W_COMPASSIGN)
7217 tword->flags |= W_COMPASSIGN; /* XXX */
7218 if (word->flags & W_NOGLOB)
7219 tword->flags |= W_NOGLOB; /* XXX */
7220 if (word->flags & W_NOEXPAND)
7221 tword->flags |= W_NOEXPAND; /* XXX */
7222 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7223 tword->flags |= W_QUOTED;
7224 if (had_quoted_null)
7225 tword->flags |= W_HASQUOTEDNULL;
7226 list = make_word_list (tword, (WORD_LIST *)NULL);
7232 ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
7234 /* If we have $@, we need to split the results no matter what. If
7235 IFS is unset or NULL, string_list_dollar_at has separated the
7236 positional parameters with a space, so we split on space (we have
7237 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
7238 string_list_dollar_at has separated the positional parameters
7239 with the first character of $IFS, so we split on $IFS. */
7240 if (has_dollar_at && ifs_chars)
7241 list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
7244 tword = make_bare_word (istring);
7245 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
7246 tword->flags |= W_QUOTED;
7247 if (word->flags & W_ASSIGNMENT)
7248 tword->flags |= W_ASSIGNMENT;
7249 if (word->flags & W_COMPASSIGN)
7250 tword->flags |= W_COMPASSIGN;
7251 if (word->flags & W_NOGLOB)
7252 tword->flags |= W_NOGLOB;
7253 if (word->flags & W_NOEXPAND)
7254 tword->flags |= W_NOEXPAND;
7255 if (had_quoted_null)
7256 tword->flags |= W_HASQUOTEDNULL; /* XXX */
7257 list = make_word_list (tword, (WORD_LIST *)NULL);
7265 /* **************************************************************** */
7267 /* Functions for Quote Removal */
7269 /* **************************************************************** */
7271 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
7272 backslash quoting rules for within double quotes or a here document. */
7274 string_quote_removal (string, quoted)
7279 char *r, *result_string, *temp, *send;
7280 int sindex, tindex, dquote;
7284 /* The result can be no longer than the original string. */
7285 slen = strlen (string);
7286 send = string + slen;
7288 r = result_string = (char *)xmalloc (slen + 1);
7290 for (dquote = sindex = 0; c = string[sindex];)
7295 c = string[++sindex];
7296 if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
7301 SCOPY_CHAR_M (r, string, send, sindex);
7305 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
7311 tindex = sindex + 1;
7312 temp = string_extract_single_quoted (string, &tindex);
7323 dquote = 1 - dquote;
7329 return (result_string);
7334 /* Perform quote removal on word WORD. This allocates and returns a new
7337 word_quote_removal (word, quoted)
7344 t = string_quote_removal (word->word, quoted);
7345 w = alloc_word_desc ();
7346 w->word = t ? t : savestring ("");
7350 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
7351 the members of the list are treated as if they are surrounded by
7352 double quotes. Return a new list, or NULL if LIST is NULL. */
7354 word_list_quote_removal (list, quoted)
7358 WORD_LIST *result, *t, *tresult, *e;
7360 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
7362 tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
7364 result = (WORD_LIST *) list_append (result, tresult);
7367 result = e = tresult;
7380 /*******************************************
7382 * Functions to perform word splitting *
7384 *******************************************/
7395 ifs_value = v ? value_cell (v) : " \t\n";
7397 ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n";
7400 /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
7401 handle multibyte chars in IFS */
7402 memset (ifs_cmap, '\0', sizeof (ifs_cmap));
7403 for (t = ifs_value ; t && *t; t++)
7409 #if defined (HANDLE_MULTIBYTE)
7412 ifs_firstc[0] = '\0';
7418 ifs_len = strnlen (ifs_value, MB_CUR_MAX);
7419 ifs_firstc_len = MBLEN (ifs_value, ifs_len);
7420 if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len))
7422 ifs_firstc[0] = ifs_value[0];
7423 ifs_firstc[1] = '\0';
7427 memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
7430 ifs_firstc = ifs_value ? *ifs_value : 0;
7440 /* This splits a single word into a WORD LIST on $IFS, but only if the word
7441 is not quoted. list_string () performs quote removal for us, even if we
7442 don't do any splitting. */
7444 word_split (w, ifs_chars)
7454 xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
7455 result = list_string (w->word, xifs, w->flags & W_QUOTED);
7458 result = (WORD_LIST *)NULL;
7463 /* Perform word splitting on LIST and return the RESULT. It is possible
7464 to return (WORD_LIST *)NULL. */
7466 word_list_split (list)
7469 WORD_LIST *result, *t, *tresult, *e;
7471 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
7473 tresult = word_split (t->word, ifs_value);
7475 result = (WORD_LIST *) list_append (result, tresult);
7478 result = e = tresult;
7490 /**************************************************
7492 * Functions to expand an entire WORD_LIST *
7494 **************************************************/
7496 /* Do any word-expansion-specific cleanup and jump to top_level */
7498 exp_jump_to_top_level (v)
7501 /* Cleanup code goes here. */
7502 expand_no_split_dollar_star = 0; /* XXX */
7503 expanding_redir = 0;
7505 jump_to_top_level (v);
7508 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
7509 ELIST, and set ELIST to the new list. */
7510 #define PREPEND_LIST(nlist, elist) \
7511 do { nlist->next = elist; elist = nlist; } while (0)
7513 /* Separate out any initial variable assignments from TLIST. If set -k has
7514 been executed, remove all assignment statements from TLIST. Initial
7515 variable assignments and other environment assignments are placed
7516 on SUBST_ASSIGN_VARLIST. */
7518 separate_out_assignments (tlist)
7521 register WORD_LIST *vp, *lp;
7524 return ((WORD_LIST *)NULL);
7526 if (subst_assign_varlist)
7527 dispose_words (subst_assign_varlist); /* Clean up after previous error */
7529 subst_assign_varlist = (WORD_LIST *)NULL;
7532 /* Separate out variable assignments at the start of the command.
7533 Loop invariant: vp->next == lp
7535 lp = list of words left after assignment statements skipped
7536 tlist = original list of words
7538 while (lp && (lp->word->flags & W_ASSIGNMENT))
7544 /* If lp != tlist, we have some initial assignment statements.
7545 We make SUBST_ASSIGN_VARLIST point to the list of assignment
7546 words and TLIST point to the remaining words. */
7549 subst_assign_varlist = tlist;
7550 /* ASSERT(vp->next == lp); */
7551 vp->next = (WORD_LIST *)NULL; /* terminate variable list */
7552 tlist = lp; /* remainder of word list */
7555 /* vp == end of variable list */
7556 /* tlist == remainder of original word list without variable assignments */
7558 /* All the words in tlist were assignment statements */
7559 return ((WORD_LIST *)NULL);
7561 /* ASSERT(tlist != NULL); */
7562 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
7564 /* If the -k option is in effect, we need to go through the remaining
7565 words, separate out the assignment words, and place them on
7566 SUBST_ASSIGN_VARLIST. */
7567 if (place_keywords_in_env)
7569 WORD_LIST *tp; /* tp == running pointer into tlist */
7574 /* Loop Invariant: tp->next == lp */
7575 /* Loop postcondition: tlist == word list without assignment statements */
7578 if (lp->word->flags & W_ASSIGNMENT)
7580 /* Found an assignment statement, add this word to end of
7581 subst_assign_varlist (vp). */
7582 if (!subst_assign_varlist)
7583 subst_assign_varlist = vp = lp;
7590 /* Remove the word pointed to by LP from TLIST. */
7591 tp->next = lp->next;
7592 /* ASSERT(vp == lp); */
7593 lp->next = (WORD_LIST *)NULL;
7606 #define WEXP_VARASSIGN 0x001
7607 #define WEXP_BRACEEXP 0x002
7608 #define WEXP_TILDEEXP 0x004
7609 #define WEXP_PARAMEXP 0x008
7610 #define WEXP_PATHEXP 0x010
7612 /* All of the expansions, including variable assignments at the start of
7614 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
7616 /* All of the expansions except variable assignments at the start of
7618 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
7620 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
7621 expansion, command substitution, arithmetic expansion, word splitting, and
7623 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
7625 /* Take the list of words in LIST and do the various substitutions. Return
7626 a new list of words which is the expanded list, and without things like
7627 variable assignments. */
7633 return (expand_word_list_internal (list, WEXP_ALL));
7636 /* Same as expand_words (), but doesn't hack variable or environment
7639 expand_words_no_vars (list)
7642 return (expand_word_list_internal (list, WEXP_NOVARS));
7646 expand_words_shellexp (list)
7649 return (expand_word_list_internal (list, WEXP_SHELLEXP));
7653 glob_expand_word_list (tlist, eflags)
7657 char **glob_array, *temp_string;
7658 register int glob_index;
7659 WORD_LIST *glob_list, *output_list, *disposables, *next;
7662 output_list = disposables = (WORD_LIST *)NULL;
7663 glob_array = (char **)NULL;
7666 /* For each word, either globbing is attempted or the word is
7667 added to orig_list. If globbing succeeds, the results are
7668 added to orig_list and the word (tlist) is added to the list
7669 of disposable words. If globbing fails and failed glob
7670 expansions are left unchanged (the shell default), the
7671 original word is added to orig_list. If globbing fails and
7672 failed glob expansions are removed, the original word is
7673 added to the list of disposable words. orig_list ends up
7674 in reverse order and requires a call to REVERSE_LIST to
7675 be set right. After all words are examined, the disposable
7679 /* If the word isn't an assignment and contains an unquoted
7680 pattern matching character, then glob it. */
7681 if ((tlist->word->flags & W_NOGLOB) == 0 &&
7682 unquoted_glob_pattern_p (tlist->word->word))
7684 glob_array = shell_glob_filename (tlist->word->word);
7686 /* Handle error cases.
7687 I don't think we should report errors like "No such file
7688 or directory". However, I would like to report errors
7689 like "Read failed". */
7691 if (glob_array == 0 || GLOB_FAILED (glob_array))
7693 glob_array = (char **)xmalloc (sizeof (char *));
7694 glob_array[0] = (char *)NULL;
7697 /* Dequote the current word in case we have to use it. */
7698 if (glob_array[0] == NULL)
7700 temp_string = dequote_string (tlist->word->word);
7701 free (tlist->word->word);
7702 tlist->word->word = temp_string;
7705 /* Make the array into a word list. */
7706 glob_list = (WORD_LIST *)NULL;
7707 for (glob_index = 0; glob_array[glob_index]; glob_index++)
7709 tword = make_bare_word (glob_array[glob_index]);
7710 tword->flags |= W_GLOBEXP; /* XXX */
7711 glob_list = make_word_list (tword, glob_list);
7716 output_list = (WORD_LIST *)list_append (glob_list, output_list);
7717 PREPEND_LIST (tlist, disposables);
7719 else if (fail_glob_expansion != 0)
7721 report_error (_("no match: %s"), tlist->word->word);
7722 jump_to_top_level (DISCARD);
7724 else if (allow_null_glob_expansion == 0)
7726 /* Failed glob expressions are left unchanged. */
7727 PREPEND_LIST (tlist, output_list);
7731 /* Failed glob expressions are removed. */
7732 PREPEND_LIST (tlist, disposables);
7737 /* Dequote the string. */
7738 temp_string = dequote_string (tlist->word->word);
7739 free (tlist->word->word);
7740 tlist->word->word = temp_string;
7741 PREPEND_LIST (tlist, output_list);
7744 strvec_dispose (glob_array);
7745 glob_array = (char **)NULL;
7751 dispose_words (disposables);
7754 output_list = REVERSE_LIST (output_list, WORD_LIST *);
7756 return (output_list);
7759 #if defined (BRACE_EXPANSION)
7761 brace_expand_word_list (tlist, eflags)
7765 register char **expansions;
7767 WORD_LIST *disposables, *output_list, *next;
7771 for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
7775 /* Only do brace expansion if the word has a brace character. If
7776 not, just add the word list element to BRACES and continue. In
7777 the common case, at least when running shell scripts, this will
7778 degenerate to a bunch of calls to `xstrchr', and then what is
7779 basically a reversal of TLIST into BRACES, which is corrected
7780 by a call to REVERSE_LIST () on BRACES when the end of TLIST
7782 if (xstrchr (tlist->word->word, LBRACE))
7784 expansions = brace_expand (tlist->word->word);
7786 for (eindex = 0; temp_string = expansions[eindex]; eindex++)
7788 w = make_word (temp_string);
7789 /* If brace expansion didn't change the word, preserve
7790 the flags. We may want to preserve the flags
7791 unconditionally someday -- XXX */
7792 if (STREQ (temp_string, tlist->word->word))
7793 w->flags = tlist->word->flags;
7794 output_list = make_word_list (w, output_list);
7795 free (expansions[eindex]);
7799 /* Add TLIST to the list of words to be freed after brace
7800 expansion has been performed. */
7801 PREPEND_LIST (tlist, disposables);
7804 PREPEND_LIST (tlist, output_list);
7808 dispose_words (disposables);
7811 output_list = REVERSE_LIST (output_list, WORD_LIST *);
7813 return (output_list);
7818 shell_expand_word_list (tlist, eflags)
7822 WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list;
7823 int expanded_something, has_dollar_at;
7826 /* We do tilde expansion all the time. This is what 1003.2 says. */
7827 new_list = (WORD_LIST *)NULL;
7828 for (orig_list = tlist; tlist; tlist = next)
7830 temp_string = tlist->word->word;
7834 #if defined (ARRAY_VARS)
7835 /* If this is a compound array assignment to a builtin that accepts
7836 such assignments (e.g., `declare'), take the assignment and perform
7837 it separately, handling the semantics of declarations inside shell
7838 functions. This avoids the double-evaluation of such arguments,
7839 because `declare' does some evaluation of compound assignments on
7841 if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
7845 t = do_word_assignment (tlist->word);
7848 last_command_exit_value = EXECUTION_FAILURE;
7849 exp_jump_to_top_level (DISCARD);
7852 /* Now transform the word as ksh93 appears to do and go on */
7853 t = assignment (tlist->word->word, 0);
7854 tlist->word->word[t] = '\0';
7855 tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG);
7859 expanded_something = 0;
7860 expanded = expand_word_internal
7861 (tlist->word, 0, 0, &has_dollar_at, &expanded_something);
7863 if (expanded == &expand_word_error || expanded == &expand_word_fatal)
7865 /* By convention, each time this error is returned,
7866 tlist->word->word has already been freed. */
7867 tlist->word->word = (char *)NULL;
7869 /* Dispose our copy of the original list. */
7870 dispose_words (orig_list);
7871 /* Dispose the new list we're building. */
7872 dispose_words (new_list);
7874 last_command_exit_value = EXECUTION_FAILURE;
7875 if (expanded == &expand_word_error)
7876 exp_jump_to_top_level (DISCARD);
7878 exp_jump_to_top_level (FORCE_EOF);
7881 /* Don't split words marked W_NOSPLIT. */
7882 if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
7884 temp_list = word_list_split (expanded);
7885 dispose_words (expanded);
7889 /* If no parameter expansion, command substitution, process
7890 substitution, or arithmetic substitution took place, then
7891 do not do word splitting. We still have to remove quoted
7892 null characters from the result. */
7893 word_list_remove_quoted_nulls (expanded);
7894 temp_list = expanded;
7897 expanded = REVERSE_LIST (temp_list, WORD_LIST *);
7898 new_list = (WORD_LIST *)list_append (expanded, new_list);
7902 dispose_words (orig_list);
7905 new_list = REVERSE_LIST (new_list, WORD_LIST *);
7910 /* The workhorse for expand_words () and expand_words_no_vars ().
7911 First arg is LIST, a WORD_LIST of words.
7912 Second arg EFLAGS is a flags word controlling which expansions are
7915 This does all of the substitutions: brace expansion, tilde expansion,
7916 parameter expansion, command substitution, arithmetic expansion,
7917 process substitution, word splitting, and pathname expansion, according
7918 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
7919 set, or for which no expansion is done, do not undergo word splitting.
7920 Words with the W_NOGLOB bit set do not undergo pathname expansion. */
7922 expand_word_list_internal (list, eflags)
7926 WORD_LIST *new_list, *temp_list;
7930 return ((WORD_LIST *)NULL);
7932 garglist = new_list = copy_word_list (list);
7933 if (eflags & WEXP_VARASSIGN)
7935 garglist = new_list = separate_out_assignments (new_list);
7938 if (subst_assign_varlist)
7940 /* All the words were variable assignments, so they are placed
7941 into the shell's environment. */
7942 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
7944 this_command_name = (char *)NULL; /* no arithmetic errors */
7945 tint = do_word_assignment (temp_list->word);
7946 /* Variable assignment errors in non-interactive shells
7947 running in Posix.2 mode cause the shell to exit. */
7950 last_command_exit_value = EXECUTION_FAILURE;
7951 if (interactive_shell == 0 && posixly_correct)
7952 exp_jump_to_top_level (FORCE_EOF);
7954 exp_jump_to_top_level (DISCARD);
7957 dispose_words (subst_assign_varlist);
7958 subst_assign_varlist = (WORD_LIST *)NULL;
7960 return ((WORD_LIST *)NULL);
7964 /* Begin expanding the words that remain. The expansions take place on
7965 things that aren't really variable assignments. */
7967 #if defined (BRACE_EXPANSION)
7968 /* Do brace expansion on this word if there are any brace characters
7970 if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
7971 new_list = brace_expand_word_list (new_list, eflags);
7972 #endif /* BRACE_EXPANSION */
7974 /* Perform the `normal' shell expansions: tilde expansion, parameter and
7975 variable substitution, command substitution, arithmetic expansion,
7976 and word splitting. */
7977 new_list = shell_expand_word_list (new_list, eflags);
7979 /* Okay, we're almost done. Now let's just do some filename
7983 if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
7984 /* Glob expand the word list unless globbing has been disabled. */
7985 new_list = glob_expand_word_list (new_list, eflags);
7987 /* Dequote the words, because we're not performing globbing. */
7988 new_list = dequote_list (new_list);
7991 if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
7993 sh_wassign_func_t *assign_func;
7995 /* If the remainder of the words expand to nothing, Posix.2 requires
7996 that the variable and environment assignments affect the shell's
7998 assign_func = new_list ? assign_in_env : do_word_assignment;
7999 tempenv_assign_error = 0;
8001 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
8003 this_command_name = (char *)NULL;
8004 tint = (*assign_func) (temp_list->word);
8005 /* Variable assignment errors in non-interactive shells running
8006 in Posix.2 mode cause the shell to exit. */
8009 if (assign_func == do_word_assignment)
8011 last_command_exit_value = EXECUTION_FAILURE;
8012 if (interactive_shell == 0 && posixly_correct)
8013 exp_jump_to_top_level (FORCE_EOF);
8015 exp_jump_to_top_level (DISCARD);
8018 tempenv_assign_error++;
8022 dispose_words (subst_assign_varlist);
8023 subst_assign_varlist = (WORD_LIST *)NULL;
8027 tint = list_length (new_list) + 1;
8028 RESIZE_MALLOCED_BUFFER (glob_argv_flags, 0, tint, glob_argv_flags_size, 16);
8029 for (tint = 0, temp_list = new_list; temp_list; temp_list = temp_list->next)
8030 glob_argv_flags[tint++] = (temp_list->word->flags & W_GLOBEXP) ? '1' : '0';
8031 glob_argv_flags[tint] = '\0';