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-2020 Free Software Foundation, Inc.
9 This file is part of GNU Bash, the Bourne Again SHell.
11 Bash is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 Bash is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with Bash. If not, see <http://www.gnu.org/licenses/>.
27 #include "bashtypes.h"
29 #include "chartypes.h"
30 #if defined (HAVE_PWD_H)
36 #if defined (HAVE_UNISTD_H)
40 #define NEED_FPURGE_DECL
43 #include "posixstat.h"
50 #include "execute_cmd.h"
54 #include "mailcheck.h"
57 #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
58 # include <mbstr.h> /* mbschr */
62 #include "builtins/getopt.h"
63 #include "builtins/common.h"
65 #include "builtins/builtext.h"
67 #include <tilde/tilde.h>
68 #include <glob/strmatch.h>
74 /* The size that strings change by. */
75 #define DEFAULT_INITIAL_ARRAY_SIZE 112
76 #define DEFAULT_ARRAY_SIZE 128
82 #define VT_ARRAYMEMBER 3
85 #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */
87 /* Flags for quoted_strchr */
88 #define ST_BACKSL 0x01
89 #define ST_CTLESC 0x02
90 #define ST_SQUOTE 0x04 /* unused yet */
91 #define ST_DQUOTE 0x08 /* unused yet */
93 /* These defs make it easier to use the editor. */
101 #if defined (HANDLE_MULTIBYTE)
106 #define DOLLAR_AT_STAR(c) ((c) == '@' || (c) == '*')
107 #define STR_DOLLAR_AT_STAR(s) (DOLLAR_AT_STAR ((s)[0]) && (s)[1] == '\0')
109 /* Evaluates to 1 if C is one of the shell's special parameters whose length
110 can be taken, but is also one of the special expansion characters. */
111 #define VALID_SPECIAL_LENGTH_PARAM(c) \
112 ((c) == '-' || (c) == '?' || (c) == '#' || (c) == '@')
114 /* Evaluates to 1 if C is one of the shell's special parameters for which an
115 indirect variable reference may be made. */
116 #define VALID_INDIR_PARAM(c) \
117 ((posixly_correct == 0 && (c) == '#') || (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*')
119 /* Evaluates to 1 if C is one of the OP characters that follows the parameter
120 in ${parameter[:]OPword}. */
121 #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
123 /* Evaluates to 1 if this is one of the shell's special variables. */
124 #define SPECIAL_VAR(name, wi) \
125 (*name && ((DIGIT (*name) && all_digits (name)) || \
126 (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
127 (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1]))))
129 /* This can be used by all of the *_extract_* functions that have a similar
130 structure. It can't just be wrapped in a do...while(0) loop because of
131 the embedded `break'. The dangling else accommodates a trailing semicolon;
132 we could also put in a do ; while (0) */
134 #define CHECK_STRING_OVERRUN(oind, ind, len, ch) \
143 /* An expansion function that takes a string and a quoted flag and returns
144 a WORD_LIST *. Used as the type of the third argument to
145 expand_string_if_necessary(). */
146 typedef WORD_LIST
*EXPFUNC
PARAMS((char *, int));
148 /* Process ID of the last command executed within command substitution. */
149 pid_t last_command_subst_pid
= NO_PID
;
150 pid_t current_command_subst_pid
= NO_PID
;
152 /* Variables used to keep track of the characters in IFS. */
155 unsigned char ifs_cmap
[UCHAR_MAX
+ 1];
156 int ifs_is_set
, ifs_is_null
;
158 #if defined (HANDLE_MULTIBYTE)
159 unsigned char ifs_firstc
[MB_LEN_MAX
];
160 size_t ifs_firstc_len
;
162 unsigned char ifs_firstc
;
165 /* If non-zero, command substitution inherits the value of errexit option */
166 int inherit_errexit
= 0;
168 /* Sentinel to tell when we are performing variable assignments preceding a
169 command name and putting them into the environment. Used to make sure
170 we use the temporary environment when looking up variable values. */
171 int assigning_in_environment
;
173 /* Used to hold a list of variable assignments preceding a command. Global
174 so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
175 SIGCHLD trap and so it can be saved and restored by the trap handlers. */
176 WORD_LIST
*subst_assign_varlist
= (WORD_LIST
*)NULL
;
178 /* Tell the expansion functions to not longjmp back to top_level on fatal
179 errors. Enabled when doing completion and prompt string expansion. */
180 int no_longjmp_on_fatal_error
= 0;
182 /* Non-zero means to allow unmatched globbed filenames to expand to
184 int allow_null_glob_expansion
;
186 /* Non-zero means to throw an error when globbing fails to match anything. */
187 int fail_glob_expansion
;
189 /* Extern functions and variables from different files. */
190 extern struct fd_bitmap
*current_fds_to_close
;
191 extern int wordexp_only
;
193 #if defined (JOB_CONTROL) && defined (PROCESS_SUBSTITUTION)
194 extern PROCESS
*last_procsub_child
;
197 #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
198 extern wchar_t *wcsdup
PARAMS((const wchar_t *));
202 /* Variables to keep track of which words in an expanded word list (the
203 output of expand_word_list_internal) are the result of globbing
204 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
205 (CURRENTLY UNUSED). */
206 char *glob_argv_flags
;
207 static int glob_argv_flags_size
;
210 static WORD_LIST
*cached_quoted_dollar_at
= 0;
212 /* Distinguished error values to return from expansion functions */
213 static WORD_LIST expand_word_error
, expand_word_fatal
;
214 static WORD_DESC expand_wdesc_error
, expand_wdesc_fatal
;
215 static char expand_param_error
, expand_param_fatal
, expand_param_unset
;
216 static char extract_string_error
, extract_string_fatal
;
218 /* Set by expand_word_unsplit and several of the expand_string_XXX functions;
219 used to inhibit splitting and re-joining $* on $IFS, primarily when doing
220 assignment statements. The idea is that if we're in a context where this
221 is set, we're not going to be performing word splitting, so we use the same
222 rules to expand $* as we would if it appeared within double quotes. */
223 static int expand_no_split_dollar_star
= 0;
225 /* A WORD_LIST of words to be expanded by expand_word_list_internal,
226 without any leading variable assignments. */
227 static WORD_LIST
*garglist
= (WORD_LIST
*)NULL
;
229 static char *quoted_substring
PARAMS((char *, int, int));
230 static int quoted_strlen
PARAMS((char *));
231 static char *quoted_strchr
PARAMS((char *, int, int));
233 static char *expand_string_if_necessary
PARAMS((char *, int, EXPFUNC
*));
234 static inline char *expand_string_to_string_internal
PARAMS((char *, int, EXPFUNC
*));
235 static WORD_LIST
*call_expand_word_internal
PARAMS((WORD_DESC
*, int, int, int *, int *));
236 static WORD_LIST
*expand_string_internal
PARAMS((char *, int));
237 static WORD_LIST
*expand_string_leave_quoted
PARAMS((char *, int));
238 static WORD_LIST
*expand_string_for_rhs
PARAMS((char *, int, int, int, int *, int *));
239 static WORD_LIST
*expand_string_for_pat
PARAMS((char *, int, int *, int *));
241 static char *quote_escapes_internal
PARAMS((const char *, int));
243 static WORD_LIST
*list_quote_escapes
PARAMS((WORD_LIST
*));
244 static WORD_LIST
*list_dequote_escapes
PARAMS((WORD_LIST
*));
246 static char *make_quoted_char
PARAMS((int));
247 static WORD_LIST
*quote_list
PARAMS((WORD_LIST
*));
249 static int unquoted_substring
PARAMS((char *, char *));
250 static int unquoted_member
PARAMS((int, char *));
252 #if defined (ARRAY_VARS)
253 static SHELL_VAR
*do_compound_assignment
PARAMS((char *, char *, int));
255 static int do_assignment_internal
PARAMS((const WORD_DESC
*, int));
257 static char *string_extract_verbatim
PARAMS((char *, size_t, int *, char *, int));
258 static char *string_extract
PARAMS((char *, int *, char *, int));
259 static char *string_extract_double_quoted
PARAMS((char *, int *, int));
260 static inline char *string_extract_single_quoted
PARAMS((char *, int *));
261 static inline int skip_single_quoted
PARAMS((const char *, size_t, int, int));
262 static int skip_double_quoted
PARAMS((char *, size_t, int, int));
263 static char *extract_delimited_string
PARAMS((char *, int *, char *, char *, char *, int));
264 static char *extract_dollar_brace_string
PARAMS((char *, int *, int, int));
265 static int skip_matched_pair
PARAMS((const char *, int, int, int, int));
267 static char *pos_params
PARAMS((char *, int, int, int, int));
269 static unsigned char *mb_getcharlens
PARAMS((char *, int));
271 static char *remove_upattern
PARAMS((char *, char *, int));
272 #if defined (HANDLE_MULTIBYTE)
273 static wchar_t *remove_wpattern
PARAMS((wchar_t *, size_t, wchar_t *, int));
275 static char *remove_pattern
PARAMS((char *, char *, int));
277 static int match_upattern
PARAMS((char *, char *, int, char **, char **));
278 #if defined (HANDLE_MULTIBYTE)
279 static int match_wpattern
PARAMS((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
281 static int match_pattern
PARAMS((char *, char *, int, char **, char **));
282 static int getpatspec
PARAMS((int, char *));
283 static char *getpattern
PARAMS((char *, int, int));
284 static char *variable_remove_pattern
PARAMS((char *, char *, int, int));
285 static char *list_remove_pattern
PARAMS((WORD_LIST
*, char *, int, int, int));
286 static char *parameter_list_remove_pattern
PARAMS((int, char *, int, int));
288 static char *array_remove_pattern
PARAMS((SHELL_VAR
*, char *, int, int, int));
290 static char *parameter_brace_remove_pattern
PARAMS((char *, char *, int, char *, int, int, int));
292 static char *string_var_assignment
PARAMS((SHELL_VAR
*, char *));
293 #if defined (ARRAY_VARS)
294 static char *array_var_assignment
PARAMS((SHELL_VAR
*, int, int, int));
296 static char *pos_params_assignment
PARAMS((WORD_LIST
*, int, int));
297 static char *string_transform
PARAMS((int, SHELL_VAR
*, char *));
298 static char *list_transform
PARAMS((int, SHELL_VAR
*, WORD_LIST
*, int, int));
299 static char *parameter_list_transform
PARAMS((int, int, int));
300 #if defined ARRAY_VARS
301 static char *array_transform
PARAMS((int, SHELL_VAR
*, int, int));
303 static char *parameter_brace_transform
PARAMS((char *, char *, int, char *, int, int, int, int));
304 static int valid_parameter_transform
PARAMS((char *));
306 static char *process_substitute
PARAMS((char *, int));
308 static char *read_comsub
PARAMS((int, int, int, int *));
311 static arrayind_t array_length_reference
PARAMS((char *));
314 static int valid_brace_expansion_word
PARAMS((char *, int));
315 static int chk_atstar
PARAMS((char *, int, int, int *, int *));
316 static int chk_arithsub
PARAMS((const char *, int));
318 static WORD_DESC
*parameter_brace_expand_word
PARAMS((char *, int, int, int, arrayind_t
*));
319 static char *parameter_brace_find_indir
PARAMS((char *, int, int, int));
320 static WORD_DESC
*parameter_brace_expand_indir
PARAMS((char *, int, int, int, int *, int *));
321 static WORD_DESC
*parameter_brace_expand_rhs
PARAMS((char *, char *, int, int, int, int *, int *));
322 static void parameter_brace_expand_error
PARAMS((char *, char *, int));
324 static int valid_length_expression
PARAMS((char *));
325 static intmax_t parameter_brace_expand_length
PARAMS((char *));
327 static char *skiparith
PARAMS((char *, int));
328 static int verify_substring_values
PARAMS((SHELL_VAR
*, char *, char *, int, intmax_t *, intmax_t *));
329 static int get_var_and_type
PARAMS((char *, char *, arrayind_t
, int, int, SHELL_VAR
**, char **));
330 static char *mb_substring
PARAMS((char *, int, int));
331 static char *parameter_brace_substring
PARAMS((char *, char *, int, char *, int, int, int));
333 static int shouldexp_replacement
PARAMS((char *));
335 static char *pos_params_pat_subst
PARAMS((char *, char *, char *, int));
337 static char *parameter_brace_patsub
PARAMS((char *, char *, int, char *, int, int, int));
339 static char *pos_params_casemod
PARAMS((char *, char *, int, int));
340 static char *parameter_brace_casemod
PARAMS((char *, char *, int, int, char *, int, int, int));
342 static WORD_DESC
*parameter_brace_expand
PARAMS((char *, int *, int, int, int *, int *));
343 static WORD_DESC
*param_expand
PARAMS((char *, int *, int, int *, int *, int *, int *, int));
345 static WORD_LIST
*expand_word_internal
PARAMS((WORD_DESC
*, int, int, int *, int *));
347 static WORD_LIST
*word_list_split
PARAMS((WORD_LIST
*));
349 static void exp_jump_to_top_level
PARAMS((int));
351 static WORD_LIST
*separate_out_assignments
PARAMS((WORD_LIST
*));
352 static WORD_LIST
*glob_expand_word_list
PARAMS((WORD_LIST
*, int));
353 #ifdef BRACE_EXPANSION
354 static WORD_LIST
*brace_expand_word_list
PARAMS((WORD_LIST
*, int));
356 #if defined (ARRAY_VARS)
357 static int make_internal_declare
PARAMS((char *, char *, char *));
358 static void expand_compound_assignment_word
PARAMS((WORD_LIST
*, int));
359 static WORD_LIST
*expand_declaration_argument
PARAMS((WORD_LIST
*, WORD_LIST
*));
361 static WORD_LIST
*shell_expand_word_list
PARAMS((WORD_LIST
*, int));
362 static WORD_LIST
*expand_word_list_internal
PARAMS((WORD_LIST
*, int));
364 /* **************************************************************** */
366 /* Utility Functions */
368 /* **************************************************************** */
372 dump_word_flags (flags
)
378 fprintf (stderr
, "%d -> ", f
);
382 fprintf (stderr
, "W_ARRAYIND%s", f
? "|" : "");
384 if (f
& W_ASSIGNASSOC
)
387 fprintf (stderr
, "W_ASSIGNASSOC%s", f
? "|" : "");
389 if (f
& W_ASSIGNARRAY
)
392 fprintf (stderr
, "W_ASSIGNARRAY%s", f
? "|" : "");
394 if (f
& W_SAWQUOTEDNULL
)
396 f
&= ~W_SAWQUOTEDNULL
;
397 fprintf (stderr
, "W_SAWQUOTEDNULL%s", f
? "|" : "");
402 fprintf (stderr
, "W_NOPROCSUB%s", f
? "|" : "");
407 fprintf (stderr
, "W_DQUOTE%s", f
? "|" : "");
409 if (f
& W_HASQUOTEDNULL
)
411 f
&= ~W_HASQUOTEDNULL
;
412 fprintf (stderr
, "W_HASQUOTEDNULL%s", f
? "|" : "");
417 fprintf (stderr
, "W_ASSIGNARG%s", f
? "|" : "");
422 fprintf (stderr
, "W_ASSNBLTIN%s", f
? "|" : "");
424 if (f
& W_ASSNGLOBAL
)
427 fprintf (stderr
, "W_ASSNGLOBAL%s", f
? "|" : "");
429 if (f
& W_COMPASSIGN
)
432 fprintf (stderr
, "W_COMPASSIGN%s", f
? "|" : "");
437 fprintf (stderr
, "W_EXPANDRHS%s", f
? "|" : "");
442 fprintf (stderr
, "W_ITILDE%s", f
? "|" : "");
447 fprintf (stderr
, "W_NOTILDE%s", f
? "|" : "");
452 fprintf (stderr
, "W_ASSIGNRHS%s", f
? "|" : "");
454 if (f
& W_NOASSNTILDE
)
457 fprintf (stderr
, "W_NOASSNTILDE%s", f
? "|" : "");
462 fprintf (stderr
, "W_NOCOMSUB%s", f
? "|" : "");
464 if (f
& W_DOLLARSTAR
)
467 fprintf (stderr
, "W_DOLLARSTAR%s", f
? "|" : "");
472 fprintf (stderr
, "W_DOLLARAT%s", f
? "|" : "");
477 fprintf (stderr
, "W_TILDEEXP%s", f
? "|" : "");
482 fprintf (stderr
, "W_NOSPLIT2%s", f
? "|" : "");
487 fprintf (stderr
, "W_NOSPLIT%s", f
? "|" : "");
492 fprintf (stderr
, "W_NOBRACE%s", f
? "|" : "");
497 fprintf (stderr
, "W_NOGLOB%s", f
? "|" : "");
499 if (f
& W_SPLITSPACE
)
502 fprintf (stderr
, "W_SPLITSPACE%s", f
? "|" : "");
504 if (f
& W_ASSIGNMENT
)
507 fprintf (stderr
, "W_ASSIGNMENT%s", f
? "|" : "");
512 fprintf (stderr
, "W_QUOTED%s", f
? "|" : "");
517 fprintf (stderr
, "W_HASDOLLAR%s", f
? "|" : "");
522 fprintf (stderr
, "W_COMPLETE%s", f
? "|" : "");
527 fprintf (stderr
, "W_CHKLOCAL%s", f
? "|" : "");
529 if (f
& W_FORCELOCAL
)
532 fprintf (stderr
, "W_FORCELOCAL%s", f
? "|" : "");
535 fprintf (stderr
, "\n");
540 #ifdef INCLUDE_UNUSED
542 quoted_substring (string
, start
, end
)
547 register char *result
, *s
, *r
;
551 /* Move to string[start], skipping quoted characters. */
552 for (s
= string
, l
= 0; *s
&& l
< start
; )
564 r
= result
= (char *)xmalloc (2*len
+ 1); /* save room for quotes */
566 /* Copy LEN characters, including quote characters. */
568 for (l
= 0; l
< len
; s
++)
582 #ifdef INCLUDE_UNUSED
583 /* Return the length of S, skipping over quoted characters */
607 #ifdef INCLUDE_UNUSED
608 /* Find the first occurrence of character C in string S, obeying shell
609 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
610 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
611 escaped with CTLESC are skipped. */
613 quoted_strchr (s
, c
, flags
)
621 if (((flags
& ST_BACKSL
) && *p
== '\\')
622 || ((flags
& ST_CTLESC
) && *p
== CTLESC
))
626 return ((char *)NULL
);
632 return ((char *)NULL
);
635 /* Return 1 if CHARACTER appears in an unquoted portion of
636 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
638 unquoted_member (character
, string
)
646 slen
= strlen (string
);
648 while (c
= string
[sindex
])
656 ADVANCE_CHAR (string
, slen
, sindex
);
662 ADVANCE_CHAR (string
, slen
, sindex
);
666 sindex
= skip_single_quoted (string
, slen
, ++sindex
, 0);
670 sindex
= skip_double_quoted (string
, slen
, ++sindex
, 0);
677 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
679 unquoted_substring (substr
, string
)
680 char *substr
, *string
;
683 int sindex
, c
, sublen
;
686 if (substr
== 0 || *substr
== '\0')
689 slen
= strlen (string
);
690 sublen
= strlen (substr
);
691 for (sindex
= 0; c
= string
[sindex
]; )
693 if (STREQN (string
+ sindex
, substr
, sublen
))
701 ADVANCE_CHAR (string
, slen
, sindex
);
705 sindex
= skip_single_quoted (string
, slen
, ++sindex
, 0);
709 sindex
= skip_double_quoted (string
, slen
, ++sindex
, 0);
713 ADVANCE_CHAR (string
, slen
, sindex
);
721 /* Most of the substitutions must be done in parallel. In order
722 to avoid using tons of unclear goto's, I have some functions
723 for manipulating malloc'ed strings. They all take INDX, a
724 pointer to an integer which is the offset into the string
725 where manipulation is taking place. They also take SIZE, a
726 pointer to an integer which is the current length of the
727 character array for this string. */
729 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
730 of space allocated to TARGET. SOURCE can be NULL, in which
731 case nothing happens. Gets rid of SOURCE by freeing it.
732 Returns TARGET in case the location has changed. */
734 sub_append_string (source
, target
, indx
, size
)
735 char *source
, *target
;
744 srclen
= STRLEN (source
);
745 if (srclen
>= (int)(*size
- *indx
))
748 n
= (n
+ DEFAULT_ARRAY_SIZE
) - (n
% DEFAULT_ARRAY_SIZE
);
749 target
= (char *)xrealloc (target
, (*size
= n
));
752 FASTCOPY (source
, target
+ *indx
, srclen
);
754 target
[*indx
] = '\0';
763 /* Append the textual representation of NUMBER to TARGET.
764 INDX and SIZE are as in SUB_APPEND_STRING. */
766 sub_append_number (number
, target
, indx
, size
)
774 temp
= itos (number
);
775 return (sub_append_string (temp
, target
, indx
, size
));
779 /* Extract a substring from STRING, starting at SINDEX and ending with
780 one of the characters in CHARLIST. Don't make the ending character
781 part of the string. Leave SINDEX pointing at the ending character.
782 Understand about backslashes in the string. If (flags & SX_VARNAME)
783 is non-zero, and array variables have been compiled into the shell,
784 everything between a `[' and a corresponding `]' is skipped over.
785 If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
786 update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
787 contain a closing character from CHARLIST. */
789 string_extract (string
, sindex
, charlist
, flags
)
801 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 0;
804 while (c
= string
[i
])
813 #if defined (ARRAY_VARS)
814 else if ((flags
& SX_VARNAME
) && c
== LBRACK
)
817 /* If this is an array subscript, skip over it and continue. */
818 ni
= skipsubscript (string
, i
, 0);
819 if (string
[ni
] == RBRACK
)
823 else if (MEMBER (c
, charlist
))
829 ADVANCE_CHAR (string
, slen
, i
);
832 /* If we had to have a matching delimiter and didn't find one, return an
833 error and let the caller deal with it. */
834 if ((flags
& SX_REQMATCH
) && found
== 0)
837 return (&extract_string_error
);
840 temp
= (flags
& SX_NOALLOC
) ? (char *)NULL
: substring (string
, *sindex
, i
);
846 /* Extract the contents of STRING as if it is enclosed in double quotes.
847 SINDEX, when passed in, is the offset of the character immediately
848 following the opening double quote; on exit, SINDEX is left pointing after
849 the closing double quote. If STRIPDQ is non-zero, unquoted double
850 quotes are stripped and the string is terminated by a null byte.
851 Backslashes between the embedded double quotes are processed. If STRIPDQ
852 is zero, an unquoted `"' terminates the string. */
854 string_extract_double_quoted (string
, sindex
, flags
)
862 char *temp
, *ret
; /* The new string we return. */
863 int pass_next
, backquote
, si
; /* State variables for the machine. */
868 slen
= strlen (string
+ *sindex
) + *sindex
;
869 send
= string
+ slen
;
871 stripdq
= (flags
& SX_STRIPDQ
);
873 pass_next
= backquote
= dquote
= 0;
874 temp
= (char *)xmalloc (1 + slen
- *sindex
);
878 while (c
= string
[i
])
880 /* Process a character that was quoted by a backslash. */
883 /* XXX - take another look at this in light of Interp 221 */
886 ``The backslash shall retain its special meaning as an escape
887 character only when followed by one of the characters:
890 If STRIPDQ is zero, we handle the double quotes here and let
891 expand_word_internal handle the rest. If STRIPDQ is non-zero,
892 we have already been through one round of backslash stripping,
893 and want to strip these backslashes only if DQUOTE is non-zero,
894 indicating that we are inside an embedded double-quoted string. */
896 /* If we are in an embedded quoted string, then don't strip
897 backslashes before characters for which the backslash
898 retains its special meaning, but remove backslashes in
899 front of other characters. If we are not in an
900 embedded quoted string, don't strip backslashes at all.
901 This mess is necessary because the string was already
902 surrounded by double quotes (and sh has some really weird
904 The returned string will be run through expansion as if
905 it were double-quoted. */
906 if ((stripdq
== 0 && c
!= '"') ||
907 (stripdq
&& ((dquote
&& (sh_syntaxtab
[c
] & CBSDQUOTE
)) || dquote
== 0)))
912 COPY_CHAR_I (temp
, j
, string
, send
, i
);
916 /* A backslash protects the next character. The code just above
917 handles preserving the backslash in front of any character but
926 /* Inside backquotes, ``the portion of the quoted string from the
927 initial backquote and the characters up to the next backquote
928 that is not preceded by a backslash, having escape characters
929 removed, defines that command''. */
934 temp
[j
++] = c
; /* COPY_CHAR_I? */
947 /* Pass everything between `$(' and the matching `)' or a quoted
948 ${ ... } pair through according to the Posix.2 specification. */
949 if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
954 if (string
[i
+ 1] == LPAREN
)
955 ret
= extract_command_subst (string
, &si
, (flags
& SX_COMPLETE
));
957 ret
= extract_dollar_brace_string (string
, &si
, Q_DOUBLE_QUOTES
, 0);
960 temp
[j
++] = string
[i
+ 1];
962 /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
964 if (ret
== 0 && no_longjmp_on_fatal_error
)
967 ret
= string
+ i
+ 2;
970 /* XXX - CHECK_STRING_OVERRUN here? */
971 for (t
= 0; ret
[t
]; t
++, j
++)
973 temp
[j
] = string
[si
];
975 if (si
< i
+ 2) /* we went back? */
990 /* Add any character but a double quote to the quoted string we're
993 goto add_one_character
;
1007 /* Point to after the closing quote. */
1015 /* This should really be another option to string_extract_double_quoted. */
1017 skip_double_quoted (string
, slen
, sind
, flags
)
1025 int pass_next
, backquote
, si
;
1028 pass_next
= backquote
= 0;
1030 while (c
= string
[i
])
1035 ADVANCE_CHAR (string
, slen
, i
);
1048 ADVANCE_CHAR (string
, slen
, i
);
1057 else if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
1060 if (string
[i
+ 1] == LPAREN
)
1061 ret
= extract_command_subst (string
, &si
, SX_NOALLOC
|(flags
&SX_COMPLETE
));
1063 ret
= extract_dollar_brace_string (string
, &si
, Q_DOUBLE_QUOTES
, SX_NOALLOC
);
1065 /* These can consume the entire string if they are unterminated */
1066 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1073 ADVANCE_CHAR (string
, slen
, i
);
1086 /* Extract the contents of STRING as if it is enclosed in single quotes.
1087 SINDEX, when passed in, is the offset of the character immediately
1088 following the opening single quote; on exit, SINDEX is left pointing after
1089 the closing single quote. */
1090 static inline char *
1091 string_extract_single_quoted (string
, sindex
)
1100 /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
1101 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 0;
1103 while (string
[i
] && string
[i
] != '\'')
1104 ADVANCE_CHAR (string
, slen
, i
);
1106 t
= substring (string
, *sindex
, i
);
1115 /* Skip over a single-quoted string. We overload the SX_COMPLETE flag to mean
1116 that we are splitting out words for completion and have encountered a $'...'
1117 string, which allows backslash-escaped single quotes. */
1119 skip_single_quoted (string
, slen
, sind
, flags
)
1129 while (string
[c
] && string
[c
] != '\'')
1131 if ((flags
& SX_COMPLETE
) && string
[c
] == '\\' && string
[c
+1] == '\'' && string
[c
+2])
1132 ADVANCE_CHAR (string
, slen
, c
);
1133 ADVANCE_CHAR (string
, slen
, c
);
1141 /* Just like string_extract, but doesn't hack backslashes or any of
1142 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
1144 string_extract_verbatim (string
, slen
, sindex
, charlist
, flags
)
1152 #if defined (HANDLE_MULTIBYTE)
1159 if ((flags
& SX_NOCTLESC
) && charlist
[0] == '\'' && charlist
[1] == '\0')
1161 temp
= string_extract_single_quoted (string
, sindex
);
1162 --*sindex
; /* leave *sindex at separator character */
1166 /* This can never be called with charlist == NULL. If *charlist == NULL,
1167 we can skip the loop and just return a copy of the string, updating
1171 temp
= string
+ *sindex
;
1172 c
= (*sindex
== 0) ? slen
: STRLEN (temp
);
1173 temp
= savestring (temp
);
1179 #if defined (HANDLE_MULTIBYTE)
1182 while (c
= string
[i
])
1184 #if defined (HANDLE_MULTIBYTE)
1187 if ((flags
& SX_NOCTLESC
) == 0 && c
== CTLESC
)
1190 CHECK_STRING_OVERRUN (i
, i
, slen
, c
);
1193 /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
1194 through, to protect the CTLNULs from later calls to
1195 remove_quoted_nulls. */
1196 else if ((flags
& SX_NOESCCTLNUL
) == 0 && c
== CTLESC
&& string
[i
+1] == CTLNUL
)
1199 CHECK_STRING_OVERRUN (i
, i
, slen
, c
);
1203 #if defined (HANDLE_MULTIBYTE)
1204 if (locale_utf8locale
&& slen
> i
&& UTF8_SINGLEBYTE (string
[i
]))
1205 mblength
= (string
[i
] != 0) ? 1 : 0;
1207 mblength
= MBLEN (string
+ i
, slen
- i
);
1211 mblength
= mbtowc (&wc
, string
+ i
, slen
- i
);
1212 if (MB_INVALIDCH (mblength
))
1214 if (MEMBER (c
, charlist
))
1222 len
= mbstowcs (wcharlist
, charlist
, 0);
1225 wcharlist
= (wchar_t *)xmalloc (sizeof (wchar_t) * (len
+ 1));
1226 mbstowcs (wcharlist
, charlist
, len
+ 1);
1229 if (wcschr (wcharlist
, wc
))
1235 if (MEMBER (c
, charlist
))
1238 ADVANCE_CHAR (string
, slen
, i
);
1241 #if defined (HANDLE_MULTIBYTE)
1245 temp
= substring (string
, *sindex
, i
);
1251 /* Extract the $( construct in STRING, and return a new string.
1252 Start extracting at (SINDEX) as if we had just seen "$(".
1253 Make (SINDEX) get the position of the matching ")". )
1254 XFLAGS is additional flags to pass to other extraction functions. */
1256 extract_command_subst (string
, sindex
, xflags
)
1263 if (string
[*sindex
] == LPAREN
|| (xflags
& SX_COMPLETE
))
1264 return (extract_delimited_string (string
, sindex
, "$(", "(", ")", xflags
|SX_COMMAND
)); /*)*/
1267 xflags
|= (no_longjmp_on_fatal_error
? SX_NOLONGJMP
: 0);
1268 ret
= xparse_dolparen (string
, string
+*sindex
, sindex
, xflags
);
1273 /* Extract the $[ construct in STRING, and return a new string. (])
1274 Start extracting at (SINDEX) as if we had just seen "$[".
1275 Make (SINDEX) get the position of the matching "]". */
1277 extract_arithmetic_subst (string
, sindex
)
1281 return (extract_delimited_string (string
, sindex
, "$[", "[", "]", 0)); /*]*/
1284 #if defined (PROCESS_SUBSTITUTION)
1285 /* Extract the <( or >( construct in STRING, and return a new string.
1286 Start extracting at (SINDEX) as if we had just seen "<(".
1287 Make (SINDEX) get the position of the matching ")". */ /*))*/
1289 extract_process_subst (string
, starter
, sindex
, xflags
)
1296 /* XXX - check xflags&SX_COMPLETE here? */
1297 return (extract_delimited_string (string
, sindex
, starter
, "(", ")", SX_COMMAND
));
1299 xflags
|= (no_longjmp_on_fatal_error
? SX_NOLONGJMP
: 0);
1300 return (xparse_dolparen (string
, string
+*sindex
, sindex
, xflags
));
1303 #endif /* PROCESS_SUBSTITUTION */
1305 #if defined (ARRAY_VARS)
1306 /* This can be fooled by unquoted right parens in the passed string. If
1307 each caller verifies that the last character in STRING is a right paren,
1308 we don't even need to call extract_delimited_string. */
1310 extract_array_assignment_list (string
, sindex
)
1317 slen
= strlen (string
);
1318 if (string
[slen
- 1] == RPAREN
)
1320 ret
= substring (string
, *sindex
, slen
- 1);
1328 /* Extract and create a new string from the contents of STRING, a
1329 character string delimited with OPENER and CLOSER. SINDEX is
1330 the address of an int describing the current offset in STRING;
1331 it should point to just after the first OPENER found. On exit,
1332 SINDEX gets the position of the last character of the matching CLOSER.
1333 If OPENER is more than a single character, ALT_OPENER, if non-null,
1334 contains a character string that can also match CLOSER and thus
1335 needs to be skipped. */
1337 extract_delimited_string (string
, sindex
, opener
, alt_opener
, closer
, flags
)
1340 char *opener
, *alt_opener
, *closer
;
1346 int pass_character
, nesting_level
, in_comment
;
1347 int len_closer
, len_opener
, len_alt_opener
;
1350 slen
= strlen (string
+ *sindex
) + *sindex
;
1351 len_opener
= STRLEN (opener
);
1352 len_alt_opener
= STRLEN (alt_opener
);
1353 len_closer
= STRLEN (closer
);
1355 pass_character
= in_comment
= 0;
1360 while (nesting_level
)
1364 /* If a recursive call or a call to ADVANCE_CHAR leaves the index beyond
1365 the end of the string, catch it and cut the loop. */
1369 c
= string
[i
= slen
];
1380 ADVANCE_CHAR (string
, slen
, i
);
1384 if (pass_character
) /* previous char was backslash */
1387 ADVANCE_CHAR (string
, slen
, i
);
1391 /* Not exactly right yet; should handle shell metacharacters and
1392 multibyte characters, too. See COMMENT_BEGIN define in parse.y */
1393 if ((flags
& SX_COMMAND
) && c
== '#' && (i
== 0 || string
[i
- 1] == '\n' || shellblank (string
[i
- 1])))
1396 ADVANCE_CHAR (string
, slen
, i
);
1400 if (c
== CTLESC
|| c
== '\\')
1407 /* Process a nested command substitution, but only if we're parsing an
1408 arithmetic substitution. */
1409 if ((flags
& SX_COMMAND
) && string
[i
] == '$' && string
[i
+1] == LPAREN
)
1412 t
= extract_command_subst (string
, &si
, flags
|SX_NOALLOC
);
1413 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1418 /* Process a nested OPENER. */
1419 if (STREQN (string
+ i
, opener
, len_opener
))
1421 si
= i
+ len_opener
;
1422 t
= extract_delimited_string (string
, &si
, opener
, alt_opener
, closer
, flags
|SX_NOALLOC
);
1423 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1428 /* Process a nested ALT_OPENER */
1429 if (len_alt_opener
&& STREQN (string
+ i
, alt_opener
, len_alt_opener
))
1431 si
= i
+ len_alt_opener
;
1432 t
= extract_delimited_string (string
, &si
, alt_opener
, alt_opener
, closer
, flags
|SX_NOALLOC
);
1433 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1438 /* If the current substring terminates the delimited string, decrement
1439 the nesting level. */
1440 if (STREQN (string
+ i
, closer
, len_closer
))
1442 i
+= len_closer
- 1; /* move to last byte of the closer */
1444 if (nesting_level
== 0)
1448 /* Pass old-style command substitution through verbatim. */
1452 t
= string_extract (string
, &si
, "`", flags
|SX_NOALLOC
);
1453 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1458 /* Pass single-quoted and double-quoted strings through verbatim. */
1459 if (c
== '\'' || c
== '"')
1462 i
= (c
== '\'') ? skip_single_quoted (string
, slen
, si
, 0)
1463 : skip_double_quoted (string
, slen
, si
, 0);
1467 /* move past this character, which was not special. */
1468 ADVANCE_CHAR (string
, slen
, i
);
1471 if (c
== 0 && nesting_level
)
1473 if (no_longjmp_on_fatal_error
== 0)
1475 last_command_exit_value
= EXECUTION_FAILURE
;
1476 report_error (_("bad substitution: no closing `%s' in %s"), closer
, string
);
1477 exp_jump_to_top_level (DISCARD
);
1482 return (char *)NULL
;
1486 si
= i
- *sindex
- len_closer
+ 1;
1487 if (flags
& SX_NOALLOC
)
1488 result
= (char *)NULL
;
1491 result
= (char *)xmalloc (1 + si
);
1492 strncpy (result
, string
+ *sindex
, si
);
1500 /* Extract a parameter expansion expression within ${ and } from STRING.
1501 Obey the Posix.2 rules for finding the ending `}': count braces while
1502 skipping over enclosed quoted strings and command substitutions.
1503 SINDEX is the address of an int describing the current offset in STRING;
1504 it should point to just after the first `{' found. On exit, SINDEX
1505 gets the position of the matching `}'. QUOTED is non-zero if this
1506 occurs inside double quotes. */
1507 /* XXX -- this is very similar to extract_delimited_string -- XXX */
1509 extract_dollar_brace_string (string
, sindex
, quoted
, flags
)
1511 int *sindex
, quoted
, flags
;
1515 int pass_character
, nesting_level
, si
, dolbrace_state
;
1521 slen
= strlen (string
+ *sindex
) + *sindex
;
1523 /* The handling of dolbrace_state needs to agree with the code in parse.y:
1524 parse_matched_pair(). The different initial value is to handle the
1525 case where this function is called to parse the word in
1526 ${param op word} (SX_WORD). */
1527 dolbrace_state
= (flags
& SX_WORD
) ? DOLBRACE_WORD
: DOLBRACE_PARAM
;
1528 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && (flags
& SX_POSIXEXP
))
1529 dolbrace_state
= DOLBRACE_QUOTE
;
1532 while (c
= string
[i
])
1537 ADVANCE_CHAR (string
, slen
, i
);
1541 /* CTLESCs and backslashes quote the next character. */
1542 if (c
== CTLESC
|| c
== '\\')
1549 if (string
[i
] == '$' && string
[i
+1] == LBRACE
)
1559 if (nesting_level
== 0)
1565 /* Pass the contents of old-style command substitutions through
1570 t
= string_extract (string
, &si
, "`", flags
|SX_NOALLOC
);
1572 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1578 /* Pass the contents of new-style command substitutions and
1579 arithmetic substitutions through verbatim. */
1580 if (string
[i
] == '$' && string
[i
+1] == LPAREN
)
1583 t
= extract_command_subst (string
, &si
, flags
|SX_NOALLOC
);
1585 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1591 #if defined (PROCESS_SUBSTITUTION)
1592 /* Technically this should only work at the start of a word */
1593 if ((string
[i
] == '<' || string
[i
] == '>') && string
[i
+1] == LPAREN
)
1596 t
= extract_process_subst (string
, (string
[i
] == '<' ? "<(" : ">)"), &si
, flags
|SX_NOALLOC
);
1598 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1605 /* Pass the contents of double-quoted strings through verbatim. */
1609 i
= skip_double_quoted (string
, slen
, si
, 0);
1610 /* skip_XXX_quoted leaves index one past close quote */
1616 /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/
1617 if (posixly_correct
&& shell_compatibility_level
> 42 && dolbrace_state
!= DOLBRACE_QUOTE
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
1618 ADVANCE_CHAR (string
, slen
, i
);
1622 i
= skip_single_quoted (string
, slen
, si
, 0);
1628 #if defined (ARRAY_VARS)
1629 if (c
== LBRACK
&& dolbrace_state
== DOLBRACE_PARAM
)
1631 si
= skipsubscript (string
, i
, 0);
1632 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1633 if (string
[si
] == RBRACK
)
1638 /* move past this character, which was not special. */
1639 ADVANCE_CHAR (string
, slen
, i
);
1641 /* This logic must agree with parse.y:parse_matched_pair, since they
1642 share the same defines. */
1643 if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '%' && (i
- *sindex
) > 1)
1644 dolbrace_state
= DOLBRACE_QUOTE
;
1645 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '#' && (i
- *sindex
) > 1)
1646 dolbrace_state
= DOLBRACE_QUOTE
;
1647 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '/' && (i
- *sindex
) > 1)
1648 dolbrace_state
= DOLBRACE_QUOTE2
; /* XXX */
1649 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '^' && (i
- *sindex
) > 1)
1650 dolbrace_state
= DOLBRACE_QUOTE
;
1651 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== ',' && (i
- *sindex
) > 1)
1652 dolbrace_state
= DOLBRACE_QUOTE
;
1653 /* This is intended to handle all of the [:]op expansions and the substring/
1654 length/pattern removal/pattern substitution expansions. */
1655 else if (dolbrace_state
== DOLBRACE_PARAM
&& strchr ("#%^,~:-=?+/", c
) != 0)
1656 dolbrace_state
= DOLBRACE_OP
;
1657 else if (dolbrace_state
== DOLBRACE_OP
&& strchr ("#%^,~:-=?+/", c
) == 0)
1658 dolbrace_state
= DOLBRACE_WORD
;
1661 if (c
== 0 && nesting_level
)
1663 if (no_longjmp_on_fatal_error
== 0)
1665 last_command_exit_value
= EXECUTION_FAILURE
;
1666 report_error (_("bad substitution: no closing `%s' in %s"), "}", string
);
1667 exp_jump_to_top_level (DISCARD
);
1672 return ((char *)NULL
);
1676 result
= (flags
& SX_NOALLOC
) ? (char *)NULL
: substring (string
, *sindex
, i
);
1682 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1683 STRING, and returns a pointer to it. */
1685 de_backslash (string
)
1688 register size_t slen
;
1689 register int i
, j
, prev_i
;
1692 slen
= strlen (string
);
1695 /* Loop copying string[i] to string[j], i >= j. */
1698 if (string
[i
] == '\\' && (string
[i
+ 1] == '`' || string
[i
+ 1] == '\\' ||
1699 string
[i
+ 1] == '$'))
1702 ADVANCE_CHAR (string
, slen
, i
);
1704 do string
[j
++] = string
[prev_i
++]; while (prev_i
< i
);
1715 /* Replace instances of \! in a string with !. */
1717 unquote_bang (string
)
1721 register char *temp
;
1723 temp
= (char *)xmalloc (1 + strlen (string
));
1725 for (i
= 0, j
= 0; (temp
[j
] = string
[i
]); i
++, j
++)
1727 if (string
[i
] == '\\' && string
[i
+ 1] == '!')
1733 strcpy (string
, temp
);
1738 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = oldjmp; return (x); } while (0)
1740 /* This function assumes s[i] == open; returns with s[ret] == close; used to
1741 parse array subscripts. FLAGS & 1 means to not attempt to skip over
1742 matched pairs of quotes or backquotes, or skip word expansions; it is
1743 intended to be used after expansion has been performed and during final
1744 assignment parsing (see arrayfunc.c:assign_compound_array_list()) or
1745 during execution by a builtin which has already undergone word expansion. */
1747 skip_matched_pair (string
, start
, open
, close
, flags
)
1749 int start
, open
, close
, flags
;
1751 int i
, pass_next
, backq
, si
, c
, count
, oldjmp
;
1756 slen
= strlen (string
+ start
) + start
;
1757 oldjmp
= no_longjmp_on_fatal_error
;
1758 no_longjmp_on_fatal_error
= 1;
1760 i
= start
+ 1; /* skip over leading bracket */
1762 pass_next
= backq
= 0;
1763 ss
= (char *)string
;
1764 while (c
= string
[i
])
1771 ADVANCE_CHAR (string
, slen
, i
);
1774 else if ((flags
& 1) == 0 && c
== '\\')
1784 ADVANCE_CHAR (string
, slen
, i
);
1787 else if ((flags
& 1) == 0 && c
== '`')
1793 else if ((flags
& 1) == 0 && c
== open
)
1799 else if (c
== close
)
1807 else if ((flags
& 1) == 0 && (c
== '\'' || c
== '"'))
1809 i
= (c
== '\'') ? skip_single_quoted (ss
, slen
, ++i
, 0)
1810 : skip_double_quoted (ss
, slen
, ++i
, 0);
1811 /* no increment, the skip functions increment past the closing quote. */
1813 else if ((flags
&1) == 0 && c
== '$' && (string
[i
+1] == LPAREN
|| string
[i
+1] == LBRACE
))
1816 if (string
[si
] == '\0')
1819 /* XXX - extract_command_subst here? */
1820 if (string
[i
+1] == LPAREN
)
1821 temp
= extract_delimited_string (ss
, &si
, "$(", "(", ")", SX_NOALLOC
|SX_COMMAND
); /* ) */
1823 temp
= extract_dollar_brace_string (ss
, &si
, 0, SX_NOALLOC
);
1825 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1828 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1834 ADVANCE_CHAR (string
, slen
, i
);
1840 #if defined (ARRAY_VARS)
1841 /* Flags has 1 as a reserved value, since skip_matched_pair uses it for
1842 skipping over quoted strings and taking the first instance of the
1843 closing character. */
1845 skipsubscript (string
, start
, flags
)
1849 return (skip_matched_pair (string
, start
, '[', ']', flags
));
1853 /* Skip characters in STRING until we find a character in DELIMS, and return
1854 the index of that character. START is the index into string at which we
1855 begin. This is similar in spirit to strpbrk, but it returns an index into
1856 STRING and takes a starting index. This little piece of code knows quite
1857 a lot of shell syntax. It's very similar to skip_double_quoted and other
1858 functions of that ilk. */
1860 skip_to_delim (string
, start
, delims
, flags
)
1866 int i
, pass_next
, backq
, dquote
, si
, c
, oldjmp
;
1867 int invert
, skipquote
, skipcmd
, noprocsub
, completeflag
;
1868 int arithexp
, skipcol
;
1870 char *temp
, open
[3];
1873 slen
= strlen (string
+ start
) + start
;
1874 oldjmp
= no_longjmp_on_fatal_error
;
1875 if (flags
& SD_NOJMP
)
1876 no_longjmp_on_fatal_error
= 1;
1877 invert
= (flags
& SD_INVERT
);
1878 skipcmd
= (flags
& SD_NOSKIPCMD
) == 0;
1879 noprocsub
= (flags
& SD_NOPROCSUB
);
1880 completeflag
= (flags
& SD_COMPLETE
) ? SX_COMPLETE
: 0;
1882 arithexp
= (flags
& SD_ARITHEXP
);
1886 pass_next
= backq
= dquote
= 0;
1887 while (c
= string
[i
])
1889 /* If this is non-zero, we should not let quote characters be delimiters
1890 and the current character is a single or double quote. We should not
1891 test whether or not it's a delimiter until after we skip single- or
1892 double-quoted strings. */
1893 skipquote
= ((flags
& SD_NOQUOTEDELIM
) && (c
== '\'' || c
=='"'));
1899 ADVANCE_CHAR (string
, slen
, i
);
1912 ADVANCE_CHAR (string
, slen
, i
);
1921 else if (arithexp
&& skipcol
&& c
== ':')
1927 else if (arithexp
&& c
== '?')
1933 else if (skipquote
== 0 && invert
== 0 && member (c
, delims
))
1935 /* the usual case is to use skip_xxx_quoted, but we don't skip over double
1936 quoted strings when looking for the history expansion character as a
1938 /* special case for programmable completion which takes place before
1939 parser converts backslash-escaped single quotes between $'...' to
1940 `regular' single-quoted strings. */
1941 else if (completeflag
&& i
> 0 && string
[i
-1] == '$' && c
== '\'')
1942 i
= skip_single_quoted (string
, slen
, ++i
, SX_COMPLETE
);
1944 i
= skip_single_quoted (string
, slen
, ++i
, 0);
1946 i
= skip_double_quoted (string
, slen
, ++i
, completeflag
);
1947 else if (c
== LPAREN
&& arithexp
)
1950 if (string
[si
] == '\0')
1953 temp
= extract_delimited_string (string
, &si
, "(", "(", ")", SX_NOALLOC
); /* ) */
1955 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1960 else if (c
== '$' && ((skipcmd
&& string
[i
+1] == LPAREN
) || string
[i
+1] == LBRACE
))
1963 if (string
[si
] == '\0')
1966 if (string
[i
+1] == LPAREN
)
1967 temp
= extract_delimited_string (string
, &si
, "$(", "(", ")", SX_NOALLOC
|SX_COMMAND
); /* ) */
1969 temp
= extract_dollar_brace_string (string
, &si
, 0, SX_NOALLOC
);
1970 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1972 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1977 #if defined (PROCESS_SUBSTITUTION)
1978 else if (skipcmd
&& noprocsub
== 0 && (c
== '<' || c
== '>') && string
[i
+1] == LPAREN
)
1981 if (string
[si
] == '\0')
1984 temp
= extract_delimited_string (string
, &si
, (c
== '<') ? "<(" : ">(", "(", ")", SX_COMMAND
|SX_NOALLOC
); /* )) */
1985 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1987 if (string
[i
] == '\0')
1992 #endif /* PROCESS_SUBSTITUTION */
1993 #if defined (EXTENDED_GLOB)
1994 else if ((flags
& SD_EXTGLOB
) && extended_glob
&& string
[i
+1] == LPAREN
&& member (c
, "?*+!@"))
1997 if (string
[si
] == '\0')
2003 temp
= extract_delimited_string (string
, &si
, open
, "(", ")", SX_NOALLOC
); /* ) */
2005 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
2007 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
2013 else if ((flags
& SD_GLOB
) && c
== LBRACK
)
2016 if (string
[si
] == '\0')
2019 temp
= extract_delimited_string (string
, &si
, "[", "[", "]", SX_NOALLOC
); /* ] */
2022 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
2027 else if ((skipquote
|| invert
) && (member (c
, delims
) == 0))
2030 ADVANCE_CHAR (string
, slen
, i
);
2036 #if defined (BANG_HISTORY)
2037 /* Skip to the history expansion character (delims[0]), paying attention to
2038 quoted strings and command and process substitution. This is a stripped-
2039 down version of skip_to_delims. The essential difference is that this
2040 resets the quoting state when starting a command substitution */
2042 skip_to_histexp (string
, start
, delims
, flags
)
2048 int i
, pass_next
, backq
, dquote
, c
, oldjmp
;
2049 int histexp_comsub
, histexp_backq
, old_dquote
;
2053 slen
= strlen (string
+ start
) + start
;
2054 oldjmp
= no_longjmp_on_fatal_error
;
2055 if (flags
& SD_NOJMP
)
2056 no_longjmp_on_fatal_error
= 1;
2058 histexp_comsub
= histexp_backq
= old_dquote
= 0;
2061 pass_next
= backq
= dquote
= 0;
2062 while (c
= string
[i
])
2069 ADVANCE_CHAR (string
, slen
, i
);
2078 else if (backq
&& c
== '`')
2082 dquote
= old_dquote
;
2090 old_dquote
= dquote
; /* simple - one level for now */
2095 /* When in double quotes, act as if the double quote is a member of
2096 history_no_expand_chars, like the history library does */
2097 else if (dquote
&& c
== delims
[0] && string
[i
+1] == '"')
2102 else if (c
== delims
[0])
2104 /* the usual case is to use skip_xxx_quoted, but we don't skip over double
2105 quoted strings when looking for the history expansion character as a
2107 else if (dquote
&& c
== '\'')
2113 i
= skip_single_quoted (string
, slen
, ++i
, 0);
2114 /* The posixly_correct test makes posix-mode shells allow double quotes
2115 to quote the history expansion character */
2116 else if (posixly_correct
== 0 && c
== '"')
2118 dquote
= 1 - dquote
;
2123 i
= skip_double_quoted (string
, slen
, ++i
, 0);
2124 #if defined (PROCESS_SUBSTITUTION)
2125 else if ((c
== '$' || c
== '<' || c
== '>') && string
[i
+1] == LPAREN
&& string
[i
+2] != LPAREN
)
2127 else if (c
== '$' && string
[i
+1] == LPAREN
&& string
[i
+2] != LPAREN
)
2130 if (string
[i
+2] == '\0')
2134 old_dquote
= dquote
;
2137 else if (histexp_comsub
&& c
== RPAREN
)
2140 dquote
= old_dquote
;
2144 else if (backq
) /* placeholder */
2146 ADVANCE_CHAR (string
, slen
, i
);
2150 ADVANCE_CHAR (string
, slen
, i
);
2155 #endif /* BANG_HISTORY */
2157 #if defined (READLINE)
2158 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
2159 an unclosed quoted string), or if the character at EINDEX is quoted
2160 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
2161 single and double-quoted string parsing functions should not return an
2162 error if there are unclosed quotes or braces. The characters that this
2163 recognizes need to be the same as the contents of
2164 rl_completer_quote_characters. */
2167 char_is_quoted (string
, eindex
)
2171 int i
, pass_next
, c
, oldjmp
;
2175 slen
= strlen (string
);
2176 oldjmp
= no_longjmp_on_fatal_error
;
2177 no_longjmp_on_fatal_error
= 1;
2186 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
2188 ADVANCE_CHAR (string
, slen
, i
);
2197 else if (c
== '$' && string
[i
+1] == '\'' && string
[i
+2])
2200 i
= skip_single_quoted (string
, slen
, i
, SX_COMPLETE
);
2204 else if (c
== '\'' || c
== '"')
2206 i
= (c
== '\'') ? skip_single_quoted (string
, slen
, ++i
, 0)
2207 : skip_double_quoted (string
, slen
, ++i
, SX_COMPLETE
);
2210 /* no increment, the skip_xxx functions go one past end */
2213 ADVANCE_CHAR (string
, slen
, i
);
2220 unclosed_pair (string
, eindex
, openstr
)
2225 int i
, pass_next
, openc
, olen
;
2229 slen
= strlen (string
);
2230 olen
= strlen (openstr
);
2231 i
= pass_next
= openc
= 0;
2237 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
2239 ADVANCE_CHAR (string
, slen
, i
);
2242 else if (string
[i
] == '\\')
2248 else if (STREQN (string
+ i
, openstr
, olen
))
2253 /* XXX - may want to handle $'...' specially here */
2254 else if (string
[i
] == '\'' || string
[i
] == '"')
2256 i
= (string
[i
] == '\'') ? skip_single_quoted (string
, slen
, i
, 0)
2257 : skip_double_quoted (string
, slen
, i
, SX_COMPLETE
);
2262 ADVANCE_CHAR (string
, slen
, i
);
2267 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
2268 individual words. If DELIMS is NULL, the current value of $IFS is used
2269 to split the string, and the function follows the shell field splitting
2270 rules. SENTINEL is an index to look for. NWP, if non-NULL,
2271 gets the number of words in the returned list. CWP, if non-NULL, gets
2272 the index of the word containing SENTINEL. Non-whitespace chars in
2273 DELIMS delimit separate fields. This is used by programmable completion. */
2275 split_at_delims (string
, slen
, delims
, sentinel
, flags
, nwp
, cwp
)
2279 int sentinel
, flags
;
2282 int ts
, te
, i
, nw
, cw
, ifs_split
, dflags
;
2283 char *token
, *d
, *d2
;
2284 WORD_LIST
*ret
, *tl
;
2286 if (string
== 0 || *string
== '\0')
2292 return ((WORD_LIST
*)NULL
);
2295 d
= (delims
== 0) ? ifs_value
: delims
;
2296 ifs_split
= delims
== 0;
2298 /* Make d2 the non-whitespace characters in delims */
2303 #if defined (HANDLE_MULTIBYTE)
2304 size_t mblength
= 1;
2308 slength
= strlen (delims
);
2309 d2
= (char *)xmalloc (slength
+ 1);
2313 #if defined (HANDLE_MULTIBYTE)
2314 mbstate_t state_bak
;
2316 mblength
= MBRLEN (delims
+ i
, slength
, &state
);
2317 if (MB_INVALIDCH (mblength
))
2319 else if (mblength
> 1)
2321 memcpy (d2
+ ts
, delims
+ i
, mblength
);
2324 slength
-= mblength
;
2328 if (whitespace (delims
[i
]) == 0)
2329 d2
[ts
++] = delims
[i
];
2337 ret
= (WORD_LIST
*)NULL
;
2339 /* Remove sequences of whitespace characters at the start of the string, as
2340 long as those characters are delimiters. */
2341 for (i
= 0; member (string
[i
], d
) && spctabnl (string
[i
]); i
++)
2343 if (string
[i
] == '\0')
2352 dflags
= flags
|SD_NOJMP
;
2355 te
= skip_to_delim (string
, ts
, d
, dflags
);
2357 /* If we have a non-whitespace delimiter character, use it to make a
2358 separate field. This is just about what $IFS splitting does and
2359 is closer to the behavior of the shell parser. */
2360 if (ts
== te
&& d2
&& member (string
[ts
], d2
))
2363 /* If we're using IFS splitting, the non-whitespace delimiter char
2364 and any additional IFS whitespace delimits a field. */
2366 while (member (string
[te
], d
) && spctabnl (string
[te
]) && ((flags
&SD_NOQUOTEDELIM
) == 0 || (string
[te
] != '\'' && string
[te
] != '"')))
2369 while (member (string
[te
], d2
) && ((flags
&SD_NOQUOTEDELIM
) == 0 || (string
[te
] != '\'' && string
[te
] != '"')))
2373 token
= substring (string
, ts
, te
);
2375 ret
= add_string_to_list (token
, ret
); /* XXX */
2379 if (sentinel
>= ts
&& sentinel
<= te
)
2382 /* If the cursor is at whitespace just before word start, set the
2383 sentinel word to the current word. */
2384 if (cwp
&& cw
== -1 && sentinel
== ts
-1)
2387 /* If the cursor is at whitespace between two words, make a new, empty
2388 word, add it before (well, after, since the list is in reverse order)
2389 the word we just added, and set the current word to that one. */
2390 if (cwp
&& cw
== -1 && sentinel
< ts
)
2392 tl
= make_word_list (make_word (""), ret
->next
);
2398 if (string
[te
] == 0)
2402 /* XXX - honor SD_NOQUOTEDELIM here */
2403 while (member (string
[i
], d
) && (ifs_split
|| spctabnl(string
[i
])) && ((flags
&SD_NOQUOTEDELIM
) == 0 || (string
[te
] != '\'' && string
[te
] != '"')))
2412 /* Special case for SENTINEL at the end of STRING. If we haven't found
2413 the word containing SENTINEL yet, and the index we're looking for is at
2414 the end of STRING (or past the end of the previously-found token,
2415 possible if the end of the line is composed solely of IFS whitespace)
2416 add an additional null argument and set the current word pointer to that. */
2417 if (cwp
&& cw
== -1 && (sentinel
>= slen
|| sentinel
>= te
))
2419 if (whitespace (string
[sentinel
- 1]))
2422 ret
= add_string_to_list (token
, ret
);
2435 return (REVERSE_LIST (ret
, WORD_LIST
*));
2437 #endif /* READLINE */
2441 /* Extract the name of the variable to bind to from the assignment string. */
2443 assignment_name (string
)
2449 offset
= assignment (string
, 0);
2451 return (char *)NULL
;
2452 temp
= substring (string
, 0, offset
);
2457 /* **************************************************************** */
2459 /* Functions to convert strings to WORD_LISTs and vice versa */
2461 /* **************************************************************** */
2463 /* Return a single string of all the words in LIST. SEP is the separator
2464 to put between individual elements of LIST in the output string. */
2466 string_list_internal (list
, sep
)
2470 register WORD_LIST
*t
;
2472 size_t word_len
, sep_len
, result_size
;
2475 return ((char *)NULL
);
2477 /* Short-circuit quickly if we don't need to separate anything. */
2478 if (list
->next
== 0)
2479 return (savestring (list
->word
->word
));
2481 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
2482 sep_len
= STRLEN (sep
);
2485 for (t
= list
; t
; t
= t
->next
)
2488 result_size
+= sep_len
;
2489 result_size
+= strlen (t
->word
->word
);
2492 r
= result
= (char *)xmalloc (result_size
+ 1);
2494 for (t
= list
; t
; t
= t
->next
)
2496 if (t
!= list
&& sep_len
)
2500 FASTCOPY (sep
, r
, sep_len
);
2507 word_len
= strlen (t
->word
->word
);
2508 FASTCOPY (t
->word
->word
, r
, word_len
);
2516 /* Return a single string of all the words present in LIST, separating
2517 each word with a space. */
2522 return (string_list_internal (list
, " "));
2525 /* An external interface that can be used by the rest of the shell to
2526 obtain a string containing the first character in $IFS. Handles all
2527 the multibyte complications. If LENP is non-null, it is set to the
2528 length of the returned string. */
2530 ifs_firstchar (lenp
)
2536 ret
= xmalloc (MB_LEN_MAX
+ 1);
2537 #if defined (HANDLE_MULTIBYTE)
2538 if (ifs_firstc_len
== 1)
2540 ret
[0] = ifs_firstc
[0];
2542 len
= ret
[0] ? 1 : 0;
2546 memcpy (ret
, ifs_firstc
, ifs_firstc_len
);
2547 ret
[len
= ifs_firstc_len
] = '\0';
2550 ret
[0] = ifs_firstc
;
2552 len
= ret
[0] ? 0 : 1;
2561 /* Return a single string of all the words present in LIST, obeying the
2562 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
2563 expansion [of $*] appears within a double quoted string, it expands
2564 to a single field with the value of each parameter separated by the
2565 first character of the IFS variable, or by a <space> if IFS is unset." */
2566 /* Posix interpretation 888 changes this when IFS is null by specifying
2567 that when unquoted, this expands to separate arguments */
2569 string_list_dollar_star (list
, quoted
, flags
)
2574 #if defined (HANDLE_MULTIBYTE)
2575 # if defined (__GNUC__)
2576 char sep
[MB_CUR_MAX
+ 1];
2584 #if defined (HANDLE_MULTIBYTE)
2585 # if !defined (__GNUC__)
2586 sep
= (char *)xmalloc (MB_CUR_MAX
+ 1);
2587 # endif /* !__GNUC__ */
2588 if (ifs_firstc_len
== 1)
2590 sep
[0] = ifs_firstc
[0];
2595 memcpy (sep
, ifs_firstc
, ifs_firstc_len
);
2596 sep
[ifs_firstc_len
] = '\0';
2599 sep
[0] = ifs_firstc
;
2603 ret
= string_list_internal (list
, sep
);
2604 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2610 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
2611 is non-zero, the $@ appears within double quotes, and we should quote
2612 the list before converting it into a string. If IFS is unset, and the
2613 word is not quoted, we just need to quote CTLESC and CTLNUL characters
2614 in the words in the list, because the default value of $IFS is
2615 <space><tab><newline>, IFS characters in the words in the list should
2616 also be split. If IFS is null, and the word is not quoted, we need
2617 to quote the words in the list to preserve the positional parameters
2619 Valid values for the FLAGS argument are the PF_ flags in command.h,
2620 the only one we care about is PF_ASSIGNRHS. $@ is supposed to expand
2621 to the positional parameters separated by spaces no matter what IFS is
2622 set to if in a context where word splitting is not performed. The only
2623 one that we didn't handle before is assignment statement arguments to
2624 declaration builtins like `declare'. */
2626 string_list_dollar_at (list
, quoted
, flags
)
2632 #if defined (HANDLE_MULTIBYTE)
2633 # if defined (__GNUC__)
2634 char sep
[MB_CUR_MAX
+ 1];
2637 # endif /* !__GNUC__ */
2643 /* XXX this could just be ifs = ifs_value; */
2644 ifs
= ifs_var
? value_cell (ifs_var
) : (char *)0;
2646 #if defined (HANDLE_MULTIBYTE)
2647 # if !defined (__GNUC__)
2648 sep
= (char *)xmalloc (MB_CUR_MAX
+ 1);
2649 # endif /* !__GNUC__ */
2650 /* XXX - testing PF_ASSIGNRHS to make sure positional parameters are
2651 separated with a space even when word splitting will not occur. */
2652 if (flags
& PF_ASSIGNRHS
)
2657 else if (ifs
&& *ifs
)
2659 if (ifs_firstc_len
== 1)
2661 sep
[0] = ifs_firstc
[0];
2666 memcpy (sep
, ifs_firstc
, ifs_firstc_len
);
2667 sep
[ifs_firstc_len
] = '\0';
2675 #else /* !HANDLE_MULTIBYTE */
2676 /* XXX - PF_ASSIGNRHS means no word splitting, so we want positional
2677 parameters separated by a space. */
2678 sep
[0] = ((flags
& PF_ASSIGNRHS
) || ifs
== 0 || *ifs
== 0) ? ' ' : *ifs
;
2680 #endif /* !HANDLE_MULTIBYTE */
2682 /* XXX -- why call quote_list if ifs == 0? we can get away without doing
2683 it now that quote_escapes quotes spaces */
2684 tlist
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
|Q_PATQUOTE
))
2686 : list_quote_escapes (list
);
2688 ret
= string_list_internal (tlist
, sep
);
2689 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2695 /* Turn the positional parameters into a string, understanding quoting and
2696 the various subtleties of using the first character of $IFS as the
2697 separator. Calls string_list_dollar_at, string_list_dollar_star, and
2698 string_list as appropriate. */
2699 /* This needs to fully understand the additional contexts where word
2700 splitting does not occur (W_ASSIGNRHS, etc.) */
2702 string_list_pos_params (pchar
, list
, quoted
, pflags
)
2710 if (pchar
== '*' && (quoted
& Q_DOUBLE_QUOTES
))
2712 tlist
= quote_list (list
);
2713 word_list_remove_quoted_nulls (tlist
);
2714 ret
= string_list_dollar_star (tlist
, 0, 0);
2716 else if (pchar
== '*' && (quoted
& Q_HERE_DOCUMENT
))
2718 tlist
= quote_list (list
);
2719 word_list_remove_quoted_nulls (tlist
);
2720 ret
= string_list (tlist
);
2722 else if (pchar
== '*' && quoted
== 0 && ifs_is_null
) /* XXX */
2723 ret
= expand_no_split_dollar_star
? string_list_dollar_star (list
, quoted
, 0) : string_list_dollar_at (list
, quoted
, 0); /* Posix interp 888 */
2724 else if (pchar
== '*' && quoted
== 0 && (pflags
& PF_ASSIGNRHS
)) /* XXX */
2725 ret
= expand_no_split_dollar_star
? string_list_dollar_star (list
, quoted
, 0) : string_list_dollar_at (list
, quoted
, 0); /* Posix interp 888 */
2726 else if (pchar
== '*')
2728 /* Even when unquoted, string_list_dollar_star does the right thing
2729 making sure that the first character of $IFS is used as the
2731 ret
= string_list_dollar_star (list
, quoted
, 0);
2733 else if (pchar
== '@' && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
2734 /* We use string_list_dollar_at, but only if the string is quoted, since
2735 that quotes the escapes if it's not, which we don't want. We could
2736 use string_list (the old code did), but that doesn't do the right
2737 thing if the first character of $IFS is not a space. We use
2738 string_list_dollar_star if the string is unquoted so we make sure that
2739 the elements of $@ are separated by the first character of $IFS for
2741 ret
= string_list_dollar_at (list
, quoted
, 0);
2742 else if (pchar
== '@' && quoted
== 0 && ifs_is_null
) /* XXX */
2743 ret
= string_list_dollar_at (list
, quoted
, 0); /* Posix interp 888 */
2744 else if (pchar
== '@' && quoted
== 0 && (pflags
& PF_ASSIGNRHS
))
2745 ret
= string_list_dollar_at (list
, quoted
, pflags
); /* Posix interp 888 */
2746 else if (pchar
== '@')
2747 ret
= string_list_dollar_star (list
, quoted
, 0);
2749 ret
= string_list ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? quote_list (list
) : list
);
2754 /* Return the list of words present in STRING. Separate the string into
2755 words at any of the characters found in SEPARATORS. If QUOTED is
2756 non-zero then word in the list will have its quoted flag set, otherwise
2757 the quoted flag is left as make_word () deemed fit.
2759 This obeys the P1003.2 word splitting semantics. If `separators' is
2760 exactly <space><tab><newline>, then the splitting algorithm is that of
2761 the Bourne shell, which treats any sequence of characters from `separators'
2762 as a delimiter. If IFS is unset, which results in `separators' being set
2763 to "", no splitting occurs. If separators has some other value, the
2764 following rules are applied (`IFS white space' means zero or more
2765 occurrences of <space>, <tab>, or <newline>, as long as those characters
2766 are in `separators'):
2768 1) IFS white space is ignored at the start and the end of the
2770 2) Each occurrence of a character in `separators' that is not
2771 IFS white space, along with any adjacent occurrences of
2772 IFS white space delimits a field.
2773 3) Any nonzero-length sequence of IFS white space delimits a field.
2776 /* BEWARE! list_string strips null arguments. Don't call it twice and
2777 expect to have "" preserved! */
2779 /* This performs word splitting and quoted null character removal on
2782 (((separators)[0]) ? ((separators)[1] ? isifs(c) \
2783 : (c) == (separators)[0]) \
2786 /* member of the space character class in the current locale */
2787 #define ifs_whitespace(c) ISSPACE(c)
2789 /* "adjacent IFS white space" */
2790 #define ifs_whitesep(c) ((sh_style_split || separators == 0) ? spctabnl (c) \
2791 : ifs_whitespace (c))
2794 list_string (string
, separators
, quoted
)
2795 register char *string
, *separators
;
2800 char *current_word
, *s
;
2801 int sindex
, sh_style_split
, whitesep
, xflags
, free_word
;
2804 if (!string
|| !*string
)
2805 return ((WORD_LIST
*)NULL
);
2807 sh_style_split
= separators
&& separators
[0] == ' ' &&
2808 separators
[1] == '\t' &&
2809 separators
[2] == '\n' &&
2810 separators
[3] == '\0';
2811 for (xflags
= 0, s
= ifs_value
; s
&& *s
; s
++)
2813 if (*s
== CTLESC
) xflags
|= SX_NOCTLESC
;
2814 else if (*s
== CTLNUL
) xflags
|= SX_NOESCCTLNUL
;
2818 /* Remove sequences of whitespace at the beginning of STRING, as
2819 long as those characters appear in IFS. Do not do this if
2820 STRING is quoted or if there are no separator characters. We use the
2821 Posix definition of whitespace as a member of the space character
2822 class in the current locale. */
2824 if (!quoted
|| !separators
|| !*separators
)
2826 /* issep() requires that separators be non-null, and always returns 0 if
2827 separator is the empty string, so don't bother if we get an empty string
2828 for separators. We already returned NULL above if STRING is empty. */
2829 if (!quoted
&& separators
&& *separators
)
2832 for (s
= string
; *s
&& issep (*s
) && ifs_whitespace (*s
); s
++);
2835 return ((WORD_LIST
*)NULL
);
2840 /* OK, now STRING points to a word that does not begin with white space.
2841 The splitting algorithm is:
2842 extract a word, stopping at a separator
2843 skip sequences of whitespace characters as long as they are separators
2844 This obeys the field splitting rules in Posix.2. */
2845 slen
= STRLEN (string
);
2846 for (result
= (WORD_LIST
*)NULL
, sindex
= 0; string
[sindex
]; )
2848 /* Don't need string length in ADVANCE_CHAR unless multibyte chars are
2849 possible, but need it in string_extract_verbatim for bounds checking */
2850 current_word
= string_extract_verbatim (string
, slen
, &sindex
, separators
, xflags
);
2851 if (current_word
== 0)
2854 free_word
= 1; /* If non-zero, we free current_word */
2856 /* If we have a quoted empty string, add a quoted null argument. We
2857 want to preserve the quoted null character iff this is a quoted
2858 empty string; otherwise the quoted null characters are removed
2860 if (QUOTED_NULL (current_word
))
2862 t
= alloc_word_desc ();
2863 t
->word
= make_quoted_char ('\0');
2864 t
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
2865 result
= make_word_list (t
, result
);
2867 else if (current_word
[0] != '\0')
2869 /* If we have something, then add it regardless. However,
2870 perform quoted null character removal on the current word. */
2871 remove_quoted_nulls (current_word
);
2873 /* We don't want to set the word flags based on the string contents
2874 here -- that's mostly for the parser -- so we just allocate a
2875 WORD_DESC *, assign current_word (noting that we don't want to
2876 free it), and skip all of make_word. */
2877 t
= alloc_word_desc ();
2878 t
->word
= current_word
;
2879 result
= make_word_list (t
, result
);
2881 result
->word
->flags
&= ~W_HASQUOTEDNULL
; /* just to be sure */
2882 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
2883 result
->word
->flags
|= W_QUOTED
;
2884 /* If removing quoted null characters leaves an empty word, note
2885 that we saw this for the caller to act on. */
2886 if (current_word
== 0 || current_word
[0] == '\0')
2887 result
->word
->flags
|= W_SAWQUOTEDNULL
;
2890 /* If we're not doing sequences of separators in the traditional
2891 Bourne shell style, then add a quoted null argument. */
2892 else if (!sh_style_split
&& !ifs_whitespace (string
[sindex
]))
2894 t
= alloc_word_desc ();
2895 t
->word
= make_quoted_char ('\0');
2896 t
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
2897 result
= make_word_list (t
, result
);
2901 free (current_word
);
2903 /* Note whether or not the separator is IFS whitespace, used later. */
2904 whitesep
= string
[sindex
] && ifs_whitesep (string
[sindex
]);
2906 /* Move past the current separator character. */
2910 ADVANCE_CHAR (string
, slen
, sindex
);
2913 /* Now skip sequences of whitespace characters if they are
2914 in the list of separators. */
2915 while (string
[sindex
] && ifs_whitesep (string
[sindex
]) && issep (string
[sindex
]))
2918 /* If the first separator was IFS whitespace and the current character
2919 is a non-whitespace IFS character, it should be part of the current
2920 field delimiter, not a separate delimiter that would result in an
2921 empty field. Look at POSIX.2, 3.6.5, (3)(b). */
2922 if (string
[sindex
] && whitesep
&& issep (string
[sindex
]) && !ifs_whitesep (string
[sindex
]))
2925 /* An IFS character that is not IFS white space, along with any
2926 adjacent IFS white space, shall delimit a field. (SUSv3) */
2927 while (string
[sindex
] && ifs_whitesep (string
[sindex
]) && isifs (string
[sindex
]))
2931 return (REVERSE_LIST (result
, WORD_LIST
*));
2934 /* Parse a single word from STRING, using SEPARATORS to separate fields.
2935 ENDPTR is set to the first character after the word. This is used by
2938 This is never called with SEPARATORS != $IFS, and takes advantage of that.
2940 XXX - this function is very similar to list_string; they should be
2943 /* character is in $IFS */
2944 #define islocalsep(c) (local_cmap[(unsigned char)(c)] != 0)
2947 get_word_from_string (stringp
, separators
, endptr
)
2948 char **stringp
, *separators
, **endptr
;
2952 int sindex
, sh_style_split
, whitesep
, xflags
;
2953 unsigned char local_cmap
[UCHAR_MAX
+1]; /* really only need single-byte chars here */
2956 if (!stringp
|| !*stringp
|| !**stringp
)
2957 return ((char *)NULL
);
2959 sh_style_split
= separators
&& separators
[0] == ' ' &&
2960 separators
[1] == '\t' &&
2961 separators
[2] == '\n' &&
2962 separators
[3] == '\0';
2963 memset (local_cmap
, '\0', sizeof (local_cmap
));
2964 for (xflags
= 0, s
= separators
; s
&& *s
; s
++)
2966 if (*s
== CTLESC
) xflags
|= SX_NOCTLESC
;
2967 if (*s
== CTLNUL
) xflags
|= SX_NOESCCTLNUL
;
2968 local_cmap
[(unsigned char)*s
] = 1; /* local charmap of separators */
2974 /* Remove sequences of whitespace at the beginning of STRING, as
2975 long as those characters appear in SEPARATORS. This happens if
2976 SEPARATORS == $' \t\n' or if IFS is unset. */
2977 if (sh_style_split
|| separators
== 0)
2978 for (; *s
&& spctabnl (*s
) && islocalsep (*s
); s
++);
2980 for (; *s
&& ifs_whitespace (*s
) && islocalsep (*s
); s
++);
2982 /* If the string is nothing but whitespace, update it and return. */
2988 return ((char *)NULL
);
2991 /* OK, S points to a word that does not begin with white space.
2992 Now extract a word, stopping at a separator, save a pointer to
2993 the first character after the word, then skip sequences of spc,
2994 tab, or nl as long as they are separators.
2996 This obeys the field splitting rules in Posix.2. */
2998 /* Don't need string length in ADVANCE_CHAR unless multibyte chars are
2999 possible, but need it in string_extract_verbatim for bounds checking */
3001 current_word
= string_extract_verbatim (s
, slen
, &sindex
, separators
, xflags
);
3003 /* Set ENDPTR to the first character after the end of the word. */
3005 *endptr
= s
+ sindex
;
3007 /* Note whether or not the separator is IFS whitespace, used later. */
3008 whitesep
= s
[sindex
] && ifs_whitesep (s
[sindex
]);
3010 /* Move past the current separator character. */
3014 ADVANCE_CHAR (s
, slen
, sindex
);
3017 /* Now skip sequences of space, tab, or newline characters if they are
3018 in the list of separators. */
3019 while (s
[sindex
] && spctabnl (s
[sindex
]) && islocalsep (s
[sindex
]))
3022 /* If the first separator was IFS whitespace and the current character is
3023 a non-whitespace IFS character, it should be part of the current field
3024 delimiter, not a separate delimiter that would result in an empty field.
3025 Look at POSIX.2, 3.6.5, (3)(b). */
3026 if (s
[sindex
] && whitesep
&& islocalsep (s
[sindex
]) && !ifs_whitesep (s
[sindex
]))
3029 /* An IFS character that is not IFS white space, along with any adjacent
3030 IFS white space, shall delimit a field. */
3031 while (s
[sindex
] && ifs_whitesep (s
[sindex
]) && islocalsep(s
[sindex
]))
3035 /* Update STRING to point to the next field. */
3036 *stringp
= s
+ sindex
;
3037 return (current_word
);
3040 /* Remove IFS white space at the end of STRING. Start at the end
3041 of the string and walk backwards until the beginning of the string
3042 or we find a character that's not IFS white space and not CTLESC.
3043 Only let CTLESC escape a white space character if SAW_ESCAPE is
3046 strip_trailing_ifs_whitespace (string
, separators
, saw_escape
)
3047 char *string
, *separators
;
3052 s
= string
+ STRLEN (string
) - 1;
3053 while (s
> string
&& ((spctabnl (*s
) && isifs (*s
)) ||
3054 (saw_escape
&& *s
== CTLESC
&& spctabnl (s
[1]))))
3062 /* Split STRING into words at whitespace. Obeys shell-style quoting with
3063 backslashes, single and double quotes. */
3065 list_string_with_quotes (string
)
3071 int c
, i
, tokstart
, len
;
3073 for (s
= string
; s
&& *s
&& spctabnl (*s
); s
++)
3075 if (s
== 0 || *s
== 0)
3076 return ((WORD_LIST
*)NULL
);
3080 list
= (WORD_LIST
*)NULL
;
3091 i
= skip_single_quoted (s
, s_len
, ++i
, 0);
3093 i
= skip_double_quoted (s
, s_len
, ++i
, 0);
3094 else if (c
== 0 || spctabnl (c
))
3096 /* We have found the end of a token. Make a word out of it and
3097 add it to the word list. */
3098 token
= substring (s
, tokstart
, i
);
3099 list
= add_string_to_list (token
, list
);
3101 while (spctabnl (s
[i
]))
3109 i
++; /* normal character */
3111 return (REVERSE_LIST (list
, WORD_LIST
*));
3115 /********************************************************/
3117 /* Functions to perform assignment statements */
3119 /********************************************************/
3121 #if defined (ARRAY_VARS)
3123 do_compound_assignment (name
, value
, flags
)
3128 int mklocal
, mkassoc
, mkglobal
, chklocal
;
3130 char *newname
; /* used for local nameref references */
3132 mklocal
= flags
& ASS_MKLOCAL
;
3133 mkassoc
= flags
& ASS_MKASSOC
;
3134 mkglobal
= flags
& ASS_MKGLOBAL
;
3135 chklocal
= flags
& ASS_CHKLOCAL
;
3137 if (mklocal
&& variable_context
)
3139 v
= find_variable (name
); /* follows namerefs */
3140 newname
= (v
== 0) ? nameref_transform_name (name
, flags
) : v
->name
;
3141 if (v
&& ((readonly_p (v
) && (flags
& ASS_FORCE
) == 0) || noassign_p (v
)))
3144 err_readonly (name
);
3145 return (v
); /* XXX */
3147 list
= expand_compound_array_assignment (v
, value
, flags
);
3149 v
= make_local_assoc_variable (newname
, 0);
3150 else if (v
== 0 || (array_p (v
) == 0 && assoc_p (v
) == 0) || v
->context
!= variable_context
)
3151 v
= make_local_array_variable (newname
, 0);
3153 assign_compound_array_list (v
, list
, flags
);
3155 dispose_words (list
);
3157 /* In a function but forcing assignment in global context. CHKLOCAL means to
3158 check for an existing local variable first. */
3159 else if (mkglobal
&& variable_context
)
3161 v
= chklocal
? find_variable (name
) : 0;
3162 if (v
&& (local_p (v
) == 0 || v
->context
!= variable_context
))
3165 v
= find_global_variable (name
);
3166 if (v
&& ((readonly_p (v
) && (flags
& ASS_FORCE
) == 0) || noassign_p (v
)))
3169 err_readonly (name
);
3170 return (v
); /* XXX */
3173 newname
= (v
== 0) ? nameref_transform_name (name
, flags
) : name
;
3174 list
= expand_compound_array_assignment (v
, value
, flags
);
3175 if (v
== 0 && mkassoc
)
3176 v
= make_new_assoc_variable (newname
);
3177 else if (v
&& mkassoc
&& assoc_p (v
) == 0)
3178 v
= convert_var_to_assoc (v
);
3180 v
= make_new_array_variable (newname
);
3181 else if (v
&& mkassoc
== 0 && array_p (v
) == 0)
3182 v
= convert_var_to_array (v
);
3184 assign_compound_array_list (v
, list
, flags
);
3186 dispose_words (list
);
3190 v
= assign_array_from_string (name
, value
, flags
);
3191 if (v
&& ((readonly_p (v
) && (flags
& ASS_FORCE
) == 0) || noassign_p (v
)))
3194 err_readonly (name
);
3195 return (v
); /* XXX */
3203 /* Given STRING, an assignment string, get the value of the right side
3204 of the `=', and bind it to the left side. If EXPAND is true, then
3205 perform parameter expansion, command substitution, and arithmetic
3206 expansion on the right-hand side. Perform tilde expansion in any
3207 case. Do not perform word splitting on the result of expansion. */
3209 do_assignment_internal (word
, expand
)
3210 const WORD_DESC
*word
;
3213 int offset
, appendop
, assign_list
, aflags
, retval
;
3214 char *name
, *value
, *temp
;
3216 #if defined (ARRAY_VARS)
3222 if (word
== 0 || word
->word
== 0)
3225 appendop
= assign_list
= aflags
= 0;
3226 string
= word
->word
;
3227 offset
= assignment (string
, 0);
3228 name
= savestring (string
);
3229 value
= (char *)NULL
;
3231 if (name
[offset
] == '=')
3233 if (name
[offset
- 1] == '+')
3236 name
[offset
- 1] = '\0';
3239 name
[offset
] = 0; /* might need this set later */
3240 temp
= name
+ offset
+ 1;
3242 #if defined (ARRAY_VARS)
3243 if (expand
&& (word
->flags
& W_COMPASSIGN
))
3245 assign_list
= ni
= 1;
3246 value
= extract_array_assignment_list (temp
, &ni
);
3250 if (expand
&& temp
[0])
3251 value
= expand_string_if_necessary (temp
, 0, expand_string_assignment
);
3253 value
= savestring (temp
);
3258 value
= (char *)xmalloc (1);
3262 if (echo_command_at_execute
)
3265 name
[offset
- 1] = '+';
3266 xtrace_print_assignment (name
, value
, assign_list
, 1);
3268 name
[offset
- 1] = '\0';
3271 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
3274 aflags
|= ASS_APPEND
;
3276 #if defined (ARRAY_VARS)
3277 if (t
= mbschr (name
, LBRACK
))
3281 report_error (_("%s: cannot assign list to array member"), name
);
3284 entry
= assign_array_element (name
, value
, aflags
);
3288 else if (assign_list
)
3290 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_CHKLOCAL
))
3291 aflags
|= ASS_CHKLOCAL
;
3292 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_ASSNGLOBAL
) == 0)
3293 aflags
|= ASS_MKLOCAL
;
3294 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_ASSNGLOBAL
))
3295 aflags
|= ASS_MKGLOBAL
;
3296 if (word
->flags
& W_ASSIGNASSOC
)
3297 aflags
|= ASS_MKASSOC
;
3298 entry
= do_compound_assignment (name
, value
, aflags
);
3301 #endif /* ARRAY_VARS */
3302 entry
= bind_variable (name
, value
, aflags
);
3305 stupidly_hack_special_variables (entry
->name
); /* might be a nameref */
3307 stupidly_hack_special_variables (name
);
3309 /* Return 1 if the assignment seems to have been performed correctly. */
3310 if (entry
== 0 || readonly_p (entry
))
3311 retval
= 0; /* assignment failure */
3312 else if (noassign_p (entry
))
3314 set_exit_status (EXECUTION_FAILURE
);
3315 retval
= 1; /* error status, but not assignment failure */
3320 if (entry
&& retval
!= 0 && noassign_p (entry
) == 0)
3321 VUNSETATTR (entry
, att_invisible
);
3323 ASSIGN_RETURN (retval
);
3326 /* Perform the assignment statement in STRING, and expand the
3327 right side by doing tilde, command and parameter expansion. */
3329 do_assignment (string
)
3334 td
.flags
= W_ASSIGNMENT
;
3337 return do_assignment_internal (&td
, 1);
3341 do_word_assignment (word
, flags
)
3345 return do_assignment_internal (word
, 1);
3348 /* Given STRING, an assignment string, get the value of the right side
3349 of the `=', and bind it to the left side. Do not perform any word
3350 expansions on the right hand side. */
3352 do_assignment_no_expand (string
)
3357 td
.flags
= W_ASSIGNMENT
;
3360 return (do_assignment_internal (&td
, 0));
3363 /***************************************************
3365 * Functions to manage the positional parameters *
3367 ***************************************************/
3369 /* Return the word list that corresponds to `$*'. */
3371 list_rest_of_args ()
3373 register WORD_LIST
*list
, *args
;
3376 /* Break out of the loop as soon as one of the dollar variables is null. */
3377 for (i
= 1, list
= (WORD_LIST
*)NULL
; i
< 10 && dollar_vars
[i
]; i
++)
3378 list
= make_word_list (make_bare_word (dollar_vars
[i
]), list
);
3380 for (args
= rest_of_args
; args
; args
= args
->next
)
3381 list
= make_word_list (make_bare_word (args
->word
->word
), list
);
3383 return (REVERSE_LIST (list
, WORD_LIST
*));
3386 /* Return the value of a positional parameter. This handles values > 10. */
3388 get_dollar_var_value (ind
)
3395 temp
= dollar_vars
[ind
] ? savestring (dollar_vars
[ind
]) : (char *)NULL
;
3396 else /* We want something like ${11} */
3399 for (p
= rest_of_args
; p
&& ind
--; p
= p
->next
)
3401 temp
= p
? savestring (p
->word
->word
) : (char *)NULL
;
3406 /* Make a single large string out of the dollar digit variables,
3407 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
3408 case of "$*" with respect to IFS. */
3410 string_rest_of_args (dollar_star
)
3413 register WORD_LIST
*list
;
3416 list
= list_rest_of_args ();
3417 string
= dollar_star
? string_list_dollar_star (list
, 0, 0) : string_list (list
);
3418 dispose_words (list
);
3422 /* Return a string containing the positional parameters from START to
3423 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
3424 which only makes a difference if QUOTED is non-zero. If QUOTED includes
3425 Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
3426 no quoting chars are added. */
3428 pos_params (string
, start
, end
, quoted
, pflags
)
3430 int start
, end
, quoted
, pflags
;
3432 WORD_LIST
*save
, *params
, *h
, *t
;
3436 /* see if we can short-circuit. if start == end, we want 0 parameters. */
3438 return ((char *)NULL
);
3440 save
= params
= list_rest_of_args ();
3441 if (save
== 0 && start
> 0)
3442 return ((char *)NULL
);
3444 if (start
== 0) /* handle ${@:0[:x]} specially */
3446 t
= make_word_list (make_word (dollar_vars
[0]), params
);
3450 for (i
= start
? 1 : 0; params
&& i
< start
; i
++)
3451 params
= params
->next
;
3454 dispose_words (save
);
3455 return ((char *)NULL
);
3457 for (h
= t
= params
; params
&& i
< end
; i
++)
3460 params
= params
->next
;
3462 t
->next
= (WORD_LIST
*)NULL
;
3464 ret
= string_list_pos_params (string
[0], h
, quoted
, pflags
);
3469 dispose_words (save
);
3473 /******************************************************************/
3475 /* Functions to expand strings to strings or WORD_LISTs */
3477 /******************************************************************/
3479 #if defined (PROCESS_SUBSTITUTION)
3480 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
3482 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
3485 /* If there are any characters in STRING that require full expansion,
3486 then call FUNC to expand STRING; otherwise just perform quote
3487 removal if necessary. This returns a new string. */
3489 expand_string_if_necessary (string
, quoted
, func
)
3500 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3501 slen
= (MB_CUR_MAX
> 1) ? strlen (string
) : 0;
3505 if (EXP_CHAR (string
[i
]))
3507 else if (string
[i
] == '\'' || string
[i
] == '\\' || string
[i
] == '"')
3509 ADVANCE_CHAR (string
, slen
, i
);
3514 list
= (*func
) (string
, quoted
);
3517 ret
= string_list (list
);
3518 dispose_words (list
);
3523 else if (saw_quote
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
3524 ret
= string_quote_removal (string
, quoted
);
3526 ret
= savestring (string
);
3531 static inline char *
3532 expand_string_to_string_internal (string
, quoted
, func
)
3540 if (string
== 0 || *string
== '\0')
3541 return ((char *)NULL
);
3543 list
= (*func
) (string
, quoted
);
3546 ret
= string_list (list
);
3547 dispose_words (list
);
3556 expand_string_to_string (string
, quoted
)
3560 return (expand_string_to_string_internal (string
, quoted
, expand_string
));
3564 expand_string_unsplit_to_string (string
, quoted
)
3568 return (expand_string_to_string_internal (string
, quoted
, expand_string_unsplit
));
3572 expand_assignment_string_to_string (string
, quoted
)
3576 return (expand_string_to_string_internal (string
, quoted
, expand_string_assignment
));
3580 expand_arith_string (string
, quoted
)
3585 WORD_LIST
*list
, *tlist
;
3591 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3592 slen
= (MB_CUR_MAX
> 1) ? strlen (string
) : 0;
3596 if (EXP_CHAR (string
[i
]))
3598 else if (string
[i
] == '\'' || string
[i
] == '\\' || string
[i
] == '"')
3600 ADVANCE_CHAR (string
, slen
, i
);
3605 /* This is expanded version of expand_string_internal as it's called by
3606 expand_string_leave_quoted */
3607 td
.flags
= W_NOPROCSUB
|W_NOTILDE
; /* don't want process substitution or tilde expansion */
3608 #if 0 /* TAG: bash-5.2 */
3609 if (quoted
& Q_ARRAYSUB
)
3610 td
.flags
|= W_NOCOMSUB
;
3612 td
.word
= savestring (string
);
3613 list
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3614 /* This takes care of the calls from expand_string_leave_quoted and
3618 tlist
= word_list_split (list
);
3619 dispose_words (list
);
3622 dequote_list (list
);
3624 /* This comes from expand_string_if_necessary */
3627 ret
= string_list (list
);
3628 dispose_words (list
);
3634 else if (saw_quote
&& (quoted
& Q_ARITH
))
3635 ret
= string_quote_removal (string
, quoted
);
3636 else if (saw_quote
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
3637 ret
= string_quote_removal (string
, quoted
);
3639 ret
= savestring (string
);
3644 #if defined (COND_COMMAND)
3645 /* Just remove backslashes in STRING. Returns a new string. */
3647 remove_backslashes (string
)
3652 r
= ret
= (char *)xmalloc (strlen (string
) + 1);
3653 for (s
= string
; s
&& *s
; )
3665 /* This needs better error handling. */
3666 /* Expand W for use as an argument to a unary or binary operator in a
3667 [[...]] expression. If SPECIAL is 1, this is the rhs argument
3668 to the != or == operator, and should be treated as a pattern. In
3669 this case, we quote the string specially for the globbing code. If
3670 SPECIAL is 2, this is an rhs argument for the =~ operator, and should
3671 be quoted appropriately for regcomp/regexec. The caller is responsible
3672 for removing the backslashes if the unquoted word is needed later. In
3673 any case, since we don't perform word splitting, we need to do quoted
3674 null character removal. */
3676 cond_expand_word (w
, special
)
3684 if (w
->word
== 0 || w
->word
[0] == '\0')
3685 return ((char *)NULL
);
3687 expand_no_split_dollar_star
= 1;
3688 w
->flags
|= W_NOSPLIT2
;
3689 l
= call_expand_word_internal (w
, 0, 0, (int *)0, (int *)0);
3690 expand_no_split_dollar_star
= 0;
3693 if (special
== 0) /* LHS */
3696 word_list_remove_quoted_nulls (l
);
3698 r
= string_list (l
);
3702 /* Need to figure out whether or not we should call dequote_escapes
3703 or a new dequote_ctlnul function here, and under what
3705 qflags
= QGLOB_CVTNULL
|QGLOB_CTLESC
;
3707 qflags
|= QGLOB_REGEXP
;
3708 word_list_remove_quoted_nulls (l
);
3709 p
= string_list (l
);
3710 r
= quote_string_for_globbing (p
, qflags
);
3722 /* Call expand_word_internal to expand W and handle error returns.
3723 A convenience function for functions that don't want to handle
3724 any errors or free any memory before aborting. */
3726 call_expand_word_internal (w
, q
, i
, c
, e
)
3732 result
= expand_word_internal (w
, q
, i
, c
, e
);
3733 if (result
== &expand_word_error
|| result
== &expand_word_fatal
)
3735 /* By convention, each time this error is returned, w->word has
3736 already been freed (it sometimes may not be in the fatal case,
3737 but that doesn't result in a memory leak because we're going
3738 to exit in most cases). */
3739 w
->word
= (char *)NULL
;
3740 last_command_exit_value
= EXECUTION_FAILURE
;
3741 exp_jump_to_top_level ((result
== &expand_word_error
) ? DISCARD
: FORCE_EOF
);
3749 /* Perform parameter expansion, command substitution, and arithmetic
3750 expansion on STRING, as if it were a word. Leave the result quoted.
3751 Since this does not perform word splitting, it leaves quoted nulls
3754 expand_string_internal (string
, quoted
)
3761 if (string
== 0 || *string
== 0)
3762 return ((WORD_LIST
*)NULL
);
3765 td
.word
= savestring (string
);
3767 tresult
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3773 /* Expand STRING by performing parameter expansion, command substitution,
3774 and arithmetic expansion. Dequote the resulting WORD_LIST before
3775 returning it, but do not perform word splitting. The call to
3776 remove_quoted_nulls () is in here because word splitting normally
3777 takes care of quote removal. */
3779 expand_string_unsplit (string
, quoted
)
3785 if (string
== 0 || *string
== '\0')
3786 return ((WORD_LIST
*)NULL
);
3788 expand_no_split_dollar_star
= 1;
3789 value
= expand_string_internal (string
, quoted
);
3790 expand_no_split_dollar_star
= 0;
3796 remove_quoted_nulls (value
->word
->word
); /* XXX */
3797 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3799 dequote_list (value
);
3804 /* Expand the rhs of an assignment statement */
3806 expand_string_assignment (string
, quoted
)
3813 if (string
== 0 || *string
== '\0')
3814 return ((WORD_LIST
*)NULL
);
3816 expand_no_split_dollar_star
= 1;
3819 /* Other shells (ksh93) do it this way, which affects how $@ is expanded
3820 in constructs like bar=${@#0} (preserves the spaces resulting from the
3821 expansion of $@ in a context where you don't do word splitting); Posix
3822 interp 888 makes the expansion of $@ in contexts where word splitting
3823 is not performed unspecified. */
3824 td
.flags
= W_ASSIGNRHS
|W_NOSPLIT2
; /* Posix interp 888 */
3826 td
.flags
= W_ASSIGNRHS
;
3828 td
.word
= savestring (string
);
3829 value
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3832 expand_no_split_dollar_star
= 0;
3838 remove_quoted_nulls (value
->word
->word
); /* XXX */
3839 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3841 dequote_list (value
);
3847 /* Expand one of the PS? prompt strings. This is a sort of combination of
3848 expand_string_unsplit and expand_string_internal, but returns the
3849 passed string when an error occurs. Might want to trap other calls
3850 to jump_to_top_level here so we don't endlessly loop. */
3852 expand_prompt_string (string
, quoted
, wflags
)
3860 if (string
== 0 || *string
== 0)
3861 return ((WORD_LIST
*)NULL
);
3864 td
.word
= savestring (string
);
3866 no_longjmp_on_fatal_error
= 1;
3867 value
= expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3868 no_longjmp_on_fatal_error
= 0;
3870 if (value
== &expand_word_error
|| value
== &expand_word_fatal
)
3872 value
= make_word_list (make_bare_word (string
), (WORD_LIST
*)NULL
);
3880 remove_quoted_nulls (value
->word
->word
); /* XXX */
3881 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3883 dequote_list (value
);
3888 /* Expand STRING just as if you were expanding a word, but do not dequote
3889 the resultant WORD_LIST. This is called only from within this file,
3890 and is used to correctly preserve quoted characters when expanding
3891 things like ${1+"$@"}. This does parameter expansion, command
3892 substitution, arithmetic expansion, and word splitting. */
3894 expand_string_leave_quoted (string
, quoted
)
3901 if (string
== 0 || *string
== '\0')
3902 return ((WORD_LIST
*)NULL
);
3904 tlist
= expand_string_internal (string
, quoted
);
3908 tresult
= word_list_split (tlist
);
3909 dispose_words (tlist
);
3912 return ((WORD_LIST
*)NULL
);
3915 /* This does not perform word splitting or dequote the WORD_LIST
3918 expand_string_for_rhs (string
, quoted
, op
, pflags
, dollar_at_p
, expanded_p
)
3920 int quoted
, op
, pflags
;
3921 int *dollar_at_p
, *expanded_p
;
3927 if (string
== 0 || *string
== '\0')
3928 return (WORD_LIST
*)NULL
;
3930 /* We want field splitting to be determined by what is going to be done with
3931 the entire ${parameterOPword} expansion, so we don't want to split the RHS
3932 we expand here. However, the expansion of $* is determined by whether we
3933 are going to eventually perform word splitting, so we want to set this
3934 depending on whether or not are are going to be splitting: if the expansion
3935 is quoted, if the OP is `=', or if IFS is set to the empty string, we
3936 are not going to be splitting, so we set expand_no_split_dollar_star to
3937 note this to callees.
3938 We pass through PF_ASSIGNRHS as W_ASSIGNRHS if this is on the RHS of an
3939 assignment statement. */
3940 /* The updated treatment of $* is the result of Posix interp 888 */
3941 /* This was further clarified on the austin-group list in March, 2017 and
3942 in Posix bug 1129 */
3943 old_nosplit
= expand_no_split_dollar_star
;
3944 expand_no_split_dollar_star
= (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) || op
== '=' || ifs_is_null
== 0; /* XXX - was 1 */
3945 td
.flags
= W_EXPANDRHS
; /* expanding RHS of ${paramOPword} */
3946 td
.flags
|= W_NOSPLIT2
; /* no splitting, remove "" and '' */
3947 if (pflags
& PF_ASSIGNRHS
) /* pass through */
3948 td
.flags
|= W_ASSIGNRHS
;
3951 td
.flags
|= W_ASSIGNRHS
; /* expand b in ${a=b} like assignment */
3953 td
.flags
|= W_ASSIGNRHS
|W_NOASSNTILDE
; /* expand b in ${a=b} like assignment */
3956 tresult
= call_expand_word_internal (&td
, quoted
, 1, dollar_at_p
, expanded_p
);
3957 expand_no_split_dollar_star
= old_nosplit
;
3962 /* This does not perform word splitting or dequote the WORD_LIST
3963 it returns and it treats $* as if it were quoted. */
3965 expand_string_for_pat (string
, quoted
, dollar_at_p
, expanded_p
)
3967 int quoted
, *dollar_at_p
, *expanded_p
;
3973 if (string
== 0 || *string
== '\0')
3974 return (WORD_LIST
*)NULL
;
3976 oexp
= expand_no_split_dollar_star
;
3977 expand_no_split_dollar_star
= 1;
3978 td
.flags
= W_NOSPLIT2
; /* no splitting, remove "" and '' */
3980 tresult
= call_expand_word_internal (&td
, quoted
, 1, dollar_at_p
, expanded_p
);
3981 expand_no_split_dollar_star
= oexp
;
3986 /* Expand STRING just as if you were expanding a word. This also returns
3987 a list of words. Note that filename globbing is *NOT* done for word
3988 or string expansion, just when the shell is expanding a command. This
3989 does parameter expansion, command substitution, arithmetic expansion,
3990 and word splitting. Dequote the resultant WORD_LIST before returning. */
3992 expand_string (string
, quoted
)
3998 if (string
== 0 || *string
== '\0')
3999 return ((WORD_LIST
*)NULL
);
4001 result
= expand_string_leave_quoted (string
, quoted
);
4002 return (result
? dequote_list (result
) : result
);
4005 /*******************************************
4007 * Functions to expand WORD_DESCs *
4009 *******************************************/
4011 /* Expand WORD, performing word splitting on the result. This does
4012 parameter expansion, command substitution, arithmetic expansion,
4013 word splitting, and quote removal. */
4016 expand_word (word
, quoted
)
4020 WORD_LIST
*result
, *tresult
;
4022 tresult
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
4023 result
= word_list_split (tresult
);
4024 dispose_words (tresult
);
4025 return (result
? dequote_list (result
) : result
);
4028 /* Expand WORD, but do not perform word splitting on the result. This
4029 does parameter expansion, command substitution, arithmetic expansion,
4030 and quote removal. */
4032 expand_word_unsplit (word
, quoted
)
4038 result
= expand_word_leave_quoted (word
, quoted
);
4039 return (result
? dequote_list (result
) : result
);
4042 /* Perform shell expansions on WORD, but do not perform word splitting or
4043 quote removal on the result. Virtually identical to expand_word_unsplit;
4044 could be combined if implementations don't diverge. */
4046 expand_word_leave_quoted (word
, quoted
)
4052 expand_no_split_dollar_star
= 1;
4054 word
->flags
|= W_NOSPLIT
;
4055 word
->flags
|= W_NOSPLIT2
;
4056 result
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
4057 expand_no_split_dollar_star
= 0;
4062 /***************************************************
4064 * Functions to handle quoting chars *
4066 ***************************************************/
4070 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
4071 The parser passes CTLNUL as CTLESC CTLNUL. */
4073 /* Quote escape characters in string s, but no other characters. This is
4074 used to protect CTLESC and CTLNUL in variable values from the rest of
4075 the word expansion process after the variable is expanded (word splitting
4076 and filename generation). If IFS is null, we quote spaces as well, just
4077 in case we split on spaces later (in the case of unquoted $@, we will
4078 eventually attempt to split the entire word on spaces). Corresponding
4079 code exists in dequote_escapes. Even if we don't end up splitting on
4080 spaces, quoting spaces is not a problem. This should never be called on
4081 a string that is quoted with single or double quotes or part of a here
4082 document (effectively double-quoted).
4083 FLAGS says whether or not we are going to split the result. If we are not,
4084 and there is a CTLESC or CTLNUL in IFS, we need to quote CTLESC and CTLNUL,
4085 respectively, to prevent them from being removed as part of dequoting. */
4087 quote_escapes_internal (string
, flags
)
4091 const char *s
, *send
;
4094 int quote_spaces
, skip_ctlesc
, skip_ctlnul
, nosplit
;
4097 slen
= strlen (string
);
4098 send
= string
+ slen
;
4100 quote_spaces
= (ifs_value
&& *ifs_value
== 0);
4101 nosplit
= (flags
& PF_NOSPLIT2
);
4103 for (skip_ctlesc
= skip_ctlnul
= 0, s
= ifs_value
; s
&& *s
; s
++)
4105 skip_ctlesc
|= (nosplit
== 0 && *s
== CTLESC
);
4106 skip_ctlnul
|= (nosplit
== 0 && *s
== CTLNUL
);
4109 t
= result
= (char *)xmalloc ((slen
* 2) + 1);
4114 if ((skip_ctlesc
== 0 && *s
== CTLESC
) || (skip_ctlnul
== 0 && *s
== CTLNUL
) || (quote_spaces
&& *s
== ' '))
4116 COPY_CHAR_P (t
, s
, send
);
4124 quote_escapes (string
)
4127 return (quote_escapes_internal (string
, 0));
4134 return (quote_escapes_internal (string
, PF_NOSPLIT2
));
4138 list_quote_escapes (list
)
4141 register WORD_LIST
*w
;
4144 for (w
= list
; w
; w
= w
->next
)
4147 w
->word
->word
= quote_escapes (t
);
4153 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
4155 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
4156 This is necessary to make unquoted CTLESC and CTLNUL characters in the
4157 data stream pass through properly.
4159 We need to remove doubled CTLESC characters inside quoted strings before
4160 quoting the entire string, so we do not double the number of CTLESC
4163 Also used by parts of the pattern substitution code. */
4165 dequote_escapes (string
)
4168 const char *s
, *send
;
4177 slen
= strlen (string
);
4178 send
= string
+ slen
;
4180 t
= result
= (char *)xmalloc (slen
+ 1);
4182 if (strchr (string
, CTLESC
) == 0)
4183 return (strcpy (result
, string
));
4185 quote_spaces
= (ifs_value
&& *ifs_value
== 0);
4190 if (*s
== CTLESC
&& (s
[1] == CTLESC
|| s
[1] == CTLNUL
|| (quote_spaces
&& s
[1] == ' ')))
4196 COPY_CHAR_P (t
, s
, send
);
4203 #if defined (INCLUDE_UNUSED)
4205 list_dequote_escapes (list
)
4208 register WORD_LIST
*w
;
4211 for (w
= list
; w
; w
= w
->next
)
4214 w
->word
->word
= dequote_escapes (t
);
4221 /* Return a new string with the quoted representation of character C.
4222 This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
4223 set in any resultant WORD_DESC where this value is the word. */
4225 make_quoted_char (c
)
4230 temp
= (char *)xmalloc (3);
4245 /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
4246 the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
4247 this value is the word. */
4249 quote_string (string
)
4254 char *result
, *send
;
4258 result
= (char *)xmalloc (2);
4266 slen
= strlen (string
);
4267 send
= string
+ slen
;
4269 result
= (char *)xmalloc ((slen
* 2) + 1);
4271 for (t
= result
; string
< send
; )
4274 COPY_CHAR_P (t
, string
, send
);
4281 /* De-quote quoted characters in STRING. */
4283 dequote_string (string
)
4286 register char *s
, *t
;
4288 char *result
, *send
;
4292 if (string
[0] == CTLESC
&& string
[1] == 0)
4293 internal_inform ("dequote_string: string with bare CTLESC");
4296 slen
= STRLEN (string
);
4298 t
= result
= (char *)xmalloc (slen
+ 1);
4300 if (QUOTED_NULL (string
))
4306 /* A string consisting of only a single CTLESC should pass through unchanged */
4307 if (string
[0] == CTLESC
&& string
[1] == 0)
4314 /* If no character in the string can be quoted, don't bother examining
4315 each character. Just return a copy of the string passed to us. */
4316 if (strchr (string
, CTLESC
) == NULL
)
4317 return (strcpy (result
, string
));
4319 send
= string
+ slen
;
4329 COPY_CHAR_P (t
, s
, send
);
4336 /* Quote the entire WORD_LIST list. */
4341 register WORD_LIST
*w
;
4344 for (w
= list
; w
; w
= w
->next
)
4347 w
->word
->word
= quote_string (t
);
4349 w
->word
->flags
|= W_HASQUOTEDNULL
; /* XXX - turn on W_HASQUOTEDNULL here? */
4350 w
->word
->flags
|= W_QUOTED
;
4362 s
= dequote_string (word
->word
);
4363 if (QUOTED_NULL (word
->word
))
4364 word
->flags
&= ~W_HASQUOTEDNULL
;
4371 /* De-quote quoted characters in each word in LIST. */
4377 register WORD_LIST
*tlist
;
4379 for (tlist
= list
; tlist
; tlist
= tlist
->next
)
4381 s
= dequote_string (tlist
->word
->word
);
4382 if (QUOTED_NULL (tlist
->word
->word
))
4383 tlist
->word
->flags
&= ~W_HASQUOTEDNULL
;
4384 free (tlist
->word
->word
);
4385 tlist
->word
->word
= s
;
4390 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
4393 remove_quoted_escapes (string
)
4400 t
= dequote_escapes (string
);
4408 /* Remove quoted $IFS characters from STRING. Quoted IFS characters are
4409 added to protect them from word splitting, but we need to remove them
4410 if no word splitting takes place. This returns newly-allocated memory,
4411 so callers can use it to replace savestring(). */
4413 remove_quoted_ifs (string
)
4416 register size_t slen
;
4421 slen
= strlen (string
);
4422 send
= string
+ slen
;
4425 ret
= (char *)xmalloc (slen
+ 1);
4429 if (string
[i
] == CTLESC
)
4432 if (string
[i
] == 0 || isifs (string
[i
]) == 0)
4438 COPY_CHAR_I (ret
, j
, string
, send
, i
);
4446 remove_quoted_nulls (string
)
4449 register size_t slen
;
4450 register int i
, j
, prev_i
;
4453 if (strchr (string
, CTLNUL
) == 0) /* XXX */
4454 return string
; /* XXX */
4456 slen
= strlen (string
);
4461 if (string
[i
] == CTLESC
)
4463 /* Old code had j++, but we cannot assume that i == j at this
4464 point -- what if a CTLNUL has already been removed from the
4465 string? We don't want to drop the CTLESC or recopy characters
4466 that we've already copied down. */
4468 string
[j
++] = CTLESC
;
4472 else if (string
[i
] == CTLNUL
)
4479 ADVANCE_CHAR (string
, slen
, i
); /* COPY_CHAR_I? */
4482 do string
[j
++] = string
[prev_i
++]; while (prev_i
< i
);
4492 /* Perform quoted null character removal on each element of LIST.
4493 This modifies LIST. */
4495 word_list_remove_quoted_nulls (list
)
4498 register WORD_LIST
*t
;
4500 for (t
= list
; t
; t
= t
->next
)
4502 remove_quoted_nulls (t
->word
->word
);
4503 t
->word
->flags
&= ~W_HASQUOTEDNULL
;
4507 /* **************************************************************** */
4509 /* Functions for Matching and Removing Patterns */
4511 /* **************************************************************** */
4513 #if defined (HANDLE_MULTIBYTE)
4514 # ifdef INCLUDE_UNUSED
4515 static unsigned char *
4516 mb_getcharlens (string
, len
)
4520 int i
, offset
, last
;
4527 ret
= (unsigned char *)xmalloc (len
);
4528 memset (ret
, 0, len
);
4529 while (string
[last
])
4531 ADVANCE_CHAR (string
, len
, offset
);
4532 ret
[last
] = offset
- last
;
4540 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
4541 can have one of 4 values:
4542 RP_LONG_LEFT remove longest matching portion at start of PARAM
4543 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
4544 RP_LONG_RIGHT remove longest matching portion at end of PARAM
4545 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
4548 #define RP_LONG_LEFT 1
4549 #define RP_SHORT_LEFT 2
4550 #define RP_LONG_RIGHT 3
4551 #define RP_SHORT_RIGHT 4
4553 /* Returns its first argument if nothing matched; new memory otherwise */
4555 remove_upattern (param
, pattern
, op
)
4556 char *param
, *pattern
;
4559 register size_t len
;
4561 register char *p
, *ret
, c
;
4563 len
= STRLEN (param
);
4568 case RP_LONG_LEFT
: /* remove longest match at start */
4569 for (p
= end
; p
>= param
; p
--)
4572 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4575 return (savestring (p
));
4582 case RP_SHORT_LEFT
: /* remove shortest match at start */
4583 for (p
= param
; p
<= end
; p
++)
4586 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4589 return (savestring (p
));
4595 case RP_LONG_RIGHT
: /* remove longest match at end */
4596 for (p
= param
; p
<= end
; p
++)
4598 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4601 ret
= savestring (param
);
4608 case RP_SHORT_RIGHT
: /* remove shortest match at end */
4609 for (p
= end
; p
>= param
; p
--)
4611 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4614 ret
= savestring (param
);
4622 return (param
); /* no match, return original string */
4625 #if defined (HANDLE_MULTIBYTE)
4626 /* Returns its first argument if nothing matched; new memory otherwise */
4628 remove_wpattern (wparam
, wstrlen
, wpattern
, op
)
4639 case RP_LONG_LEFT
: /* remove longest match at start */
4640 for (n
= wstrlen
; n
>= 0; n
--)
4642 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4643 if (wcsmatch (wpattern
, wparam
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4646 return (wcsdup (wparam
+ n
));
4652 case RP_SHORT_LEFT
: /* remove shortest match at start */
4653 for (n
= 0; n
<= wstrlen
; n
++)
4655 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4656 if (wcsmatch (wpattern
, wparam
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4659 return (wcsdup (wparam
+ n
));
4665 case RP_LONG_RIGHT
: /* remove longest match at end */
4666 for (n
= 0; n
<= wstrlen
; n
++)
4668 if (wcsmatch (wpattern
, wparam
+ n
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4670 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4671 ret
= wcsdup (wparam
);
4678 case RP_SHORT_RIGHT
: /* remove shortest match at end */
4679 for (n
= wstrlen
; n
>= 0; n
--)
4681 if (wcsmatch (wpattern
, wparam
+ n
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4683 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4684 ret
= wcsdup (wparam
);
4692 return (wparam
); /* no match, return original string */
4694 #endif /* HANDLE_MULTIBYTE */
4697 remove_pattern (param
, pattern
, op
)
4698 char *param
, *pattern
;
4705 if (*param
== '\0' || pattern
== NULL
|| *pattern
== '\0') /* minor optimization */
4706 return (savestring (param
));
4708 #if defined (HANDLE_MULTIBYTE)
4711 wchar_t *ret
, *oret
;
4713 wchar_t *wparam
, *wpattern
;
4716 /* XXX - could optimize here by checking param and pattern for multibyte
4717 chars with mbsmbchar and calling remove_upattern. */
4719 n
= xdupmbstowcs (&wpattern
, NULL
, pattern
);
4720 if (n
== (size_t)-1)
4722 xret
= remove_upattern (param
, pattern
, op
);
4723 return ((xret
== param
) ? savestring (param
) : xret
);
4725 n
= xdupmbstowcs (&wparam
, NULL
, param
);
4727 if (n
== (size_t)-1)
4730 xret
= remove_upattern (param
, pattern
, op
);
4731 return ((xret
== param
) ? savestring (param
) : xret
);
4733 oret
= ret
= remove_wpattern (wparam
, n
, wpattern
, op
);
4734 /* Don't bother to convert wparam back to multibyte string if nothing
4735 matched; just return copy of original string */
4740 return (savestring (param
));
4747 xret
= (char *)xmalloc (n
+ 1);
4748 memset (&ps
, '\0', sizeof (mbstate_t));
4749 n
= wcsrtombs (xret
, (const wchar_t **)&ret
, n
, &ps
);
4750 xret
[n
] = '\0'; /* just to make sure */
4757 xret
= remove_upattern (param
, pattern
, op
);
4758 return ((xret
== param
) ? savestring (param
) : xret
);
4762 /* Match PAT anywhere in STRING and return the match boundaries.
4763 This returns 1 in case of a successful match, 0 otherwise. SP
4764 and EP are pointers into the string where the match begins and
4765 ends, respectively. MTYPE controls what kind of match is attempted.
4766 MATCH_BEG and MATCH_END anchor the match at the beginning and end
4767 of the string, respectively. The longest match is returned. */
4769 match_upattern (string
, pat
, mtype
, sp
, ep
)
4776 register char *p
, *p1
, *npat
;
4779 /* If the pattern doesn't match anywhere in the string, go ahead and
4780 short-circuit right away. A minor optimization, saves a bunch of
4781 unnecessary calls to strmatch (up to N calls for a string of N
4782 characters) if the match is unsuccessful. To preserve the semantics
4783 of the substring matches below, we make sure that the pattern has
4784 `*' as first and last character, making a new pattern if necessary. */
4785 /* XXX - check this later if I ever implement `**' with special meaning,
4786 since this will potentially result in `**' at the beginning or end */
4788 if (pat
[0] != '*' || (pat
[0] == '*' && pat
[1] == LPAREN
&& extended_glob
) || pat
[len
- 1] != '*')
4790 int unescaped_backslash
;
4793 p
= npat
= (char *)xmalloc (len
+ 3);
4795 if ((mtype
!= MATCH_BEG
) && (*p1
!= '*' || (*p1
== '*' && p1
[1] == LPAREN
&& extended_glob
)))
4800 /* Need to also handle a pattern that ends with an unescaped backslash.
4801 For right now, we ignore it because the pattern matching code will
4802 fail the match anyway */
4803 /* If the pattern ends with a `*' we leave it alone if it's preceded by
4804 an even number of backslashes, but if it's escaped by a backslash
4805 we need to add another `*'. */
4806 if ((mtype
!= MATCH_END
) && (p1
[-1] == '*' && (unescaped_backslash
= p1
[-2] == '\\')))
4809 while (pp
>= pat
&& *pp
-- == '\\')
4810 unescaped_backslash
= 1 - unescaped_backslash
;
4811 if (unescaped_backslash
)
4814 else if (mtype
!= MATCH_END
&& p1
[-1] != '*')
4817 if (p1
[-1] != '*' || p1
[-2] == '\\')
4824 c
= strmatch (npat
, string
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
);
4827 if (c
== FNM_NOMATCH
)
4830 len
= STRLEN (string
);
4833 mlen
= umatchlen (pat
, len
);
4834 if (mlen
> (int)len
)
4840 for (p
= string
; p
<= end
; p
++)
4842 if (match_pattern_char (pat
, p
, FNMATCH_IGNCASE
))
4844 p1
= (mlen
== -1) ? end
: p
+ mlen
;
4845 /* p1 - p = length of portion of string to be considered
4846 p = current position in string
4847 mlen = number of characters consumed by match (-1 for entire string)
4849 we want to break immediately if the potential match len
4850 is greater than the number of characters remaining in the
4855 for ( ; p1
>= p
; p1
--)
4857 c
= *p1
; *p1
= '\0';
4858 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4867 /* If MLEN != -1, we have a fixed length pattern. */
4878 if (match_pattern_char (pat
, string
, FNMATCH_IGNCASE
) == 0)
4881 for (p
= (mlen
== -1) ? end
: string
+ mlen
; p
>= string
; p
--)
4884 if (strmatch (pat
, string
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4892 /* If MLEN != -1, we have a fixed length pattern. */
4900 for (p
= end
- ((mlen
== -1) ? len
: mlen
); p
<= end
; p
++)
4902 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4908 /* If MLEN != -1, we have a fixed length pattern. */
4919 #if defined (HANDLE_MULTIBYTE)
4921 #define WFOLD(c) (match_ignore_case && iswupper (c) ? towlower (c) : (c))
4923 /* Match WPAT anywhere in WSTRING and return the match boundaries.
4924 This returns 1 in case of a successful match, 0 otherwise. Wide
4925 character version. */
4927 match_wpattern (wstring
, indices
, wstrlen
, wpat
, mtype
, sp
, ep
)
4935 wchar_t wc
, *wp
, *nwpat
, *wp1
;
4938 int n
, n1
, n2
, simple
;
4940 simple
= (wpat
[0] != L
'\\' && wpat
[0] != L
'*' && wpat
[0] != L
'?' && wpat
[0] != L
'[');
4941 #if defined (EXTENDED_GLOB)
4943 simple
&= (wpat
[1] != L
'(' || (wpat
[0] != L
'*' && wpat
[0] != L
'?' && wpat
[0] != L
'+' && wpat
[0] != L
'!' && wpat
[0] != L
'@')); /*)*/
4946 /* If the pattern doesn't match anywhere in the string, go ahead and
4947 short-circuit right away. A minor optimization, saves a bunch of
4948 unnecessary calls to strmatch (up to N calls for a string of N
4949 characters) if the match is unsuccessful. To preserve the semantics
4950 of the substring matches below, we make sure that the pattern has
4951 `*' as first and last character, making a new pattern if necessary. */
4952 len
= wcslen (wpat
);
4953 if (wpat
[0] != L
'*' || (wpat
[0] == L
'*' && wpat
[1] == WLPAREN
&& extended_glob
) || wpat
[len
- 1] != L
'*')
4955 int unescaped_backslash
;
4958 wp
= nwpat
= (wchar_t *)xmalloc ((len
+ 3) * sizeof (wchar_t));
4960 if (*wp1
!= L
'*' || (*wp1
== '*' && wp1
[1] == WLPAREN
&& extended_glob
))
4962 while (*wp1
!= L
'\0')
4965 /* See comments above in match_upattern. */
4966 if (wp1
[-1] == L
'*' && (unescaped_backslash
= wp1
[-2] == L
'\\'))
4969 while (wpp
>= wpat
&& *wpp
-- == L
'\\')
4970 unescaped_backslash
= 1 - unescaped_backslash
;
4971 if (unescaped_backslash
)
4974 else if (wp1
[-1] != L
'*')
4977 if (wp1
[-1] != L
'*' || wp1
[-2] == L
'\\')
4984 len
= wcsmatch (nwpat
, wstring
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
);
4987 if (len
== FNM_NOMATCH
)
4990 mlen
= wmatchlen (wpat
, wstrlen
);
4991 if (mlen
> (int)wstrlen
)
4994 /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */
4998 for (n
= 0; n
<= wstrlen
; n
++)
5000 n2
= simple
? (WFOLD(*wpat
) == WFOLD(wstring
[n
])) : match_pattern_wchar (wpat
, wstring
+ n
, FNMATCH_IGNCASE
);
5003 n1
= (mlen
== -1) ? wstrlen
: n
+ mlen
;
5007 for ( ; n1
>= n
; n1
--)
5009 wc
= wstring
[n1
]; wstring
[n1
] = L
'\0';
5010 if (wcsmatch (wpat
, wstring
+ n
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
5018 /* If MLEN != -1, we have a fixed length pattern. */
5028 if (match_pattern_wchar (wpat
, wstring
, FNMATCH_IGNCASE
) == 0)
5031 for (n
= (mlen
== -1) ? wstrlen
: mlen
; n
>= 0; n
--)
5033 wc
= wstring
[n
]; wstring
[n
] = L
'\0';
5034 if (wcsmatch (wpat
, wstring
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
5042 /* If MLEN != -1, we have a fixed length pattern. */
5050 for (n
= wstrlen
- ((mlen
== -1) ? wstrlen
: mlen
); n
<= wstrlen
; n
++)
5052 if (wcsmatch (wpat
, wstring
+ n
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
5055 *ep
= indices
[wstrlen
];
5058 /* If MLEN != -1, we have a fixed length pattern. */
5069 #endif /* HANDLE_MULTIBYTE */
5072 match_pattern (string
, pat
, mtype
, sp
, ep
)
5077 #if defined (HANDLE_MULTIBYTE)
5080 wchar_t *wstring
, *wpat
;
5084 if (string
== 0 || pat
== 0 || *pat
== 0)
5087 #if defined (HANDLE_MULTIBYTE)
5090 if (mbsmbchar (string
) == 0 && mbsmbchar (pat
) == 0)
5091 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
5093 n
= xdupmbstowcs (&wpat
, NULL
, pat
);
5094 if (n
== (size_t)-1)
5095 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
5096 n
= xdupmbstowcs (&wstring
, &indices
, string
);
5097 if (n
== (size_t)-1)
5100 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
5102 ret
= match_wpattern (wstring
, indices
, n
, wpat
, mtype
, sp
, ep
);
5112 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
5116 getpatspec (c
, value
)
5121 return ((*value
== '#') ? RP_LONG_LEFT
: RP_SHORT_LEFT
);
5123 return ((*value
== '%') ? RP_LONG_RIGHT
: RP_SHORT_RIGHT
);
5126 /* Posix.2 says that the WORD should be run through tilde expansion,
5127 parameter expansion, command substitution and arithmetic expansion.
5128 This leaves the result quoted, so quote_string_for_globbing () has
5129 to be called to fix it up for strmatch (). If QUOTED is non-zero,
5130 it means that the entire expression was enclosed in double quotes.
5131 This means that quoting characters in the pattern do not make any
5132 special pattern characters quoted. For example, the `*' in the
5133 following retains its special meaning: "${foo#'*'}". */
5135 getpattern (value
, quoted
, expandpat
)
5137 int quoted
, expandpat
;
5144 /* There is a problem here: how to handle single or double quotes in the
5145 pattern string when the whole expression is between double quotes?
5146 POSIX.2 says that enclosing double quotes do not cause the pattern to
5147 be quoted, but does that leave us a problem with @ and array[@] and their
5148 expansions inside a pattern? */
5150 if (expandpat
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *tword
)
5153 pat
= string_extract_double_quoted (tword
, &i
, SX_STRIPDQ
);
5159 /* expand_string_for_pat () leaves WORD quoted and does not perform
5161 l
= *value
? expand_string_for_pat (value
,
5162 (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? Q_PATQUOTE
: quoted
,
5163 (int *)NULL
, (int *)NULL
)
5166 word_list_remove_quoted_nulls (l
);
5167 pat
= string_list (l
);
5171 tword
= quote_string_for_globbing (pat
, QGLOB_CVTNULL
);
5179 /* Handle removing a pattern from a string as a result of ${name%[%]value}
5180 or ${name#[#]value}. */
5182 variable_remove_pattern (value
, pattern
, patspec
, quoted
)
5183 char *value
, *pattern
;
5184 int patspec
, quoted
;
5188 tword
= remove_pattern (value
, pattern
, patspec
);
5195 list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
)
5198 int patspec
, itype
, quoted
;
5204 for (new = (WORD_LIST
*)NULL
, l
= list
; l
; l
= l
->next
)
5206 tword
= remove_pattern (l
->word
->word
, pattern
, patspec
);
5207 w
= alloc_word_desc ();
5208 w
->word
= tword
? tword
: savestring ("");
5209 new = make_word_list (w
, new);
5212 l
= REVERSE_LIST (new, WORD_LIST
*);
5213 tword
= string_list_pos_params (itype
, l
, quoted
, 0);
5220 parameter_list_remove_pattern (itype
, pattern
, patspec
, quoted
)
5223 int patspec
, quoted
;
5228 list
= list_rest_of_args ();
5230 return ((char *)NULL
);
5231 ret
= list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
);
5232 dispose_words (list
);
5236 #if defined (ARRAY_VARS)
5238 array_remove_pattern (var
, pattern
, patspec
, starsub
, quoted
)
5242 int starsub
; /* so we can figure out how it's indexed */
5252 v
= var
; /* XXX - for now */
5254 itype
= starsub
? '*' : '@';
5256 a
= (v
&& array_p (v
)) ? array_cell (v
) : 0;
5257 h
= (v
&& assoc_p (v
)) ? assoc_cell (v
) : 0;
5259 list
= a
? array_to_word_list (a
) : (h
? assoc_to_word_list (h
) : 0);
5261 return ((char *)NULL
);
5262 ret
= list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
);
5263 dispose_words (list
);
5267 #endif /* ARRAY_VARS */
5270 parameter_brace_remove_pattern (varname
, value
, ind
, patstr
, rtype
, quoted
, flags
)
5271 char *varname
, *value
;
5274 int rtype
, quoted
, flags
;
5276 int vtype
, patspec
, starsub
;
5277 char *temp1
, *val
, *pattern
, *oname
;
5281 return ((char *)NULL
);
5283 oname
= this_command_name
;
5284 this_command_name
= varname
;
5286 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
5289 this_command_name
= oname
;
5290 return ((char *)NULL
);
5293 starsub
= vtype
& VT_STARSUB
;
5294 vtype
&= ~VT_STARSUB
;
5296 patspec
= getpatspec (rtype
, patstr
);
5297 if (patspec
== RP_LONG_LEFT
|| patspec
== RP_LONG_RIGHT
)
5300 /* Need to pass getpattern newly-allocated memory in case of expansion --
5301 the expansion code will free the passed string on an error. */
5302 temp1
= savestring (patstr
);
5303 pattern
= getpattern (temp1
, quoted
, 1);
5306 temp1
= (char *)NULL
; /* shut up gcc */
5310 case VT_ARRAYMEMBER
:
5311 temp1
= remove_pattern (val
, pattern
, patspec
);
5312 if (vtype
== VT_VARIABLE
)
5316 val
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
5317 ? quote_string (temp1
)
5318 : quote_escapes (temp1
);
5323 #if defined (ARRAY_VARS)
5325 temp1
= array_remove_pattern (v
, pattern
, patspec
, starsub
, quoted
);
5326 if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
5328 val
= quote_escapes (temp1
);
5335 temp1
= parameter_list_remove_pattern (varname
[0], pattern
, patspec
, quoted
);
5336 if (temp1
&& quoted
== 0 && ifs_is_null
)
5338 /* Posix interp 888 */
5340 else if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
5342 val
= quote_escapes (temp1
);
5349 this_command_name
= oname
;
5355 #if defined (PROCESS_SUBSTITUTION)
5357 static void reap_some_procsubs
PARAMS((int));
5359 /*****************************************************************/
5361 /* Hacking Process Substitution */
5363 /*****************************************************************/
5365 #if !defined (HAVE_DEV_FD)
5366 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
5367 of FIFOs the shell has open. unlink_fifo_list will walk the list and
5368 unlink the ones that don't have a living process on the other end.
5369 unlink_all_fifos will walk the list and unconditionally unlink them, trying
5370 to open and close the FIFO first to release any child processes sleeping on
5371 the FIFO. add_fifo_list adds the name of an open FIFO to the list.
5372 NFIFO is a count of the number of FIFOs in the list. */
5373 #define FIFO_INCR 20
5375 /* PROC value of -1 means the process has been reaped and the FIFO needs to
5376 be removed. PROC value of 0 means the slot is unused. */
5382 static struct temp_fifo
*fifo_list
= (struct temp_fifo
*)NULL
;
5384 static int fifo_list_size
;
5391 for (i
= 0; i
< fifo_list_size
; i
++)
5393 if (fifo_list
[i
].file
)
5394 free (fifo_list
[i
].file
);
5395 fifo_list
[i
].file
= NULL
;
5396 fifo_list
[i
].proc
= 0;
5402 copy_fifo_list (sizep
)
5407 return (void *)NULL
;
5411 add_fifo_list (pathname
)
5416 if (nfifo
>= fifo_list_size
- 1)
5418 osize
= fifo_list_size
;
5419 fifo_list_size
+= FIFO_INCR
;
5420 fifo_list
= (struct temp_fifo
*)xrealloc (fifo_list
,
5421 fifo_list_size
* sizeof (struct temp_fifo
));
5422 for (i
= osize
; i
< fifo_list_size
; i
++)
5424 fifo_list
[i
].file
= (char *)NULL
;
5425 fifo_list
[i
].proc
= 0; /* unused */
5429 fifo_list
[nfifo
].file
= savestring (pathname
);
5437 if ((fifo_list
[i
].proc
== (pid_t
)-1) || (fifo_list
[i
].proc
> 0 && (kill(fifo_list
[i
].proc
, 0) == -1)))
5439 unlink (fifo_list
[i
].file
);
5440 free (fifo_list
[i
].file
);
5441 fifo_list
[i
].file
= (char *)NULL
;
5442 fifo_list
[i
].proc
= 0;
5454 for (i
= saved
= 0; i
< nfifo
; i
++)
5456 if ((fifo_list
[i
].proc
== (pid_t
)-1) || (fifo_list
[i
].proc
> 0 && (kill(fifo_list
[i
].proc
, 0) == -1)))
5458 unlink (fifo_list
[i
].file
);
5459 free (fifo_list
[i
].file
);
5460 fifo_list
[i
].file
= (char *)NULL
;
5461 fifo_list
[i
].proc
= 0;
5467 /* If we didn't remove some of the FIFOs, compact the list. */
5470 for (i
= j
= 0; i
< nfifo
; i
++)
5471 if (fifo_list
[i
].file
)
5475 fifo_list
[j
].file
= fifo_list
[i
].file
;
5476 fifo_list
[j
].proc
= fifo_list
[i
].proc
;
5477 fifo_list
[i
].file
= (char *)NULL
;
5478 fifo_list
[i
].proc
= 0;
5496 for (i
= 0; i
< nfifo
; i
++)
5498 fifo_list
[i
].proc
= (pid_t
)-1;
5499 fd
= open (fifo_list
[i
].file
, O_RDWR
|O_NONBLOCK
);
5508 /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list
5509 from some point in the past, and close all open FIFOs in fifo_list
5510 that are not marked as active in LIST. If LIST is NULL, close
5511 everything in fifo_list. LSIZE is the number of elements in LIST, in
5512 case it's larger than fifo_list_size (size of fifo_list). */
5514 close_new_fifos (list
, lsize
)
5523 unlink_fifo_list ();
5527 for (plist
= (char *)list
, i
= 0; i
< lsize
; i
++)
5528 if (plist
[i
] == 0 && i
< fifo_list_size
&& fifo_list
[i
].proc
!= -1)
5531 for (i
= lsize
; i
< fifo_list_size
; i
++)
5536 find_procsub_child (pid
)
5541 for (i
= 0; i
< nfifo
; i
++)
5542 if (fifo_list
[i
].proc
== pid
)
5548 set_procsub_status (ind
, pid
, status
)
5553 if (ind
>= 0 && ind
< nfifo
)
5554 fifo_list
[ind
].proc
= (pid_t
)-1; /* sentinel */
5557 /* If we've marked the process for this procsub as dead, close the
5558 associated file descriptor and delete the FIFO. */
5560 reap_some_procsubs (max
)
5565 for (i
= 0; i
< max
; i
++)
5566 if (fifo_list
[i
].proc
== (pid_t
)-1) /* reaped */
5573 reap_some_procsubs (nfifo
);
5583 for (i
= 0; i
< nfifo
; i
++)
5585 if (fifo_list
[i
].proc
!= (pid_t
)-1 && fifo_list
[i
].proc
> 0)
5587 r
= wait_for (fifo_list
[i
].proc
, 0);
5588 save_proc_status (fifo_list
[i
].proc
, r
);
5589 fifo_list
[i
].proc
= (pid_t
)-1;
5612 tname
= sh_mktmpname ("sh-np", MT_USERANDOM
|MT_USETMPDIR
);
5613 if (mkfifo (tname
, 0600) < 0)
5616 return ((char *)NULL
);
5619 add_fifo_list (tname
);
5623 #else /* HAVE_DEV_FD */
5625 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
5626 has open to children. NFDS is a count of the number of bits currently
5627 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
5629 /* dev_fd_list[I] value of -1 means the process has been reaped and file
5630 descriptor I needs to be closed. Value of 0 means the slot is unused. */
5632 static pid_t
*dev_fd_list
= (pid_t
*)NULL
;
5634 static int totfds
; /* The highest possible number of open files. */
5655 for (i
= 0; nfds
&& i
< totfds
; i
++)
5662 copy_fifo_list (sizep
)
5667 if (nfds
== 0 || totfds
== 0)
5671 return (void *)NULL
;
5676 ret
= xmalloc (totfds
* sizeof (pid_t
));
5677 return (memcpy (ret
, dev_fd_list
, totfds
* sizeof (pid_t
)));
5684 if (dev_fd_list
== 0 || fd
>= totfds
)
5689 totfds
= getdtablesize ();
5690 if (totfds
< 0 || totfds
> 256)
5695 dev_fd_list
= (pid_t
*)xrealloc (dev_fd_list
, totfds
* sizeof (dev_fd_list
[0]));
5696 /* XXX - might need a loop for this */
5697 memset (dev_fd_list
+ ofds
, '\0', (totfds
- ofds
) * sizeof (pid_t
));
5700 dev_fd_list
[fd
] = 1; /* marker; updated later */
5707 return 0; /* used for cleanup; not needed with /dev/fd */
5720 if (dev_fd_list
[fd
])
5723 dev_fd_list
[fd
] = 0;
5736 for (i
= totfds
-1; nfds
&& i
>= 0; i
--)
5745 unlink_fifo_list ();
5748 /* Take LIST, which is a snapshot copy of dev_fd_list from some point in
5749 the past, and close all open fds in dev_fd_list that are not marked
5750 as open in LIST. If LIST is NULL, close everything in dev_fd_list.
5751 LSIZE is the number of elements in LIST, in case it's larger than
5752 totfds (size of dev_fd_list). */
5754 close_new_fifos (list
, lsize
)
5763 unlink_fifo_list ();
5767 for (plist
= (pid_t
*)list
, i
= 0; i
< lsize
; i
++)
5768 if (plist
[i
] == 0 && i
< totfds
&& dev_fd_list
[i
])
5771 for (i
= lsize
; i
< totfds
; i
++)
5776 find_procsub_child (pid
)
5784 for (i
= 0; i
< totfds
; i
++)
5785 if (dev_fd_list
[i
] == pid
)
5792 set_procsub_status (ind
, pid
, status
)
5797 if (ind
>= 0 && ind
< totfds
)
5798 dev_fd_list
[ind
] = (pid_t
)-1; /* sentinel */
5801 /* If we've marked the process for this procsub as dead, close the
5802 associated file descriptor. */
5804 reap_some_procsubs (max
)
5809 for (i
= 0; nfds
> 0 && i
< max
; i
++)
5810 if (dev_fd_list
[i
] == (pid_t
)-1)
5817 reap_some_procsubs (totfds
);
5827 for (i
= 0; nfds
> 0 && i
< totfds
; i
++)
5829 if (dev_fd_list
[i
] != (pid_t
)-1 && dev_fd_list
[i
] > 0)
5831 r
= wait_for (dev_fd_list
[i
], 0);
5832 save_proc_status (dev_fd_list
[i
], r
);
5833 dev_fd_list
[i
] = (pid_t
)-1;
5839 #if defined (NOTDEF)
5840 print_dev_fd_list ()
5844 fprintf (stderr
, "pid %ld: dev_fd_list:", (long)getpid ());
5847 for (i
= 0; i
< totfds
; i
++)
5850 fprintf (stderr
, " %d", i
);
5852 fprintf (stderr
, "\n");
5857 make_dev_fd_filename (fd
)
5860 char *ret
, intbuf
[INT_STRLEN_BOUND (int) + 1], *p
;
5862 ret
= (char *)xmalloc (sizeof (DEV_FD_PREFIX
) + 8);
5864 strcpy (ret
, DEV_FD_PREFIX
);
5865 p
= inttostr (fd
, intbuf
, sizeof (intbuf
));
5866 strcpy (ret
+ sizeof (DEV_FD_PREFIX
) - 1, p
);
5872 #endif /* HAVE_DEV_FD */
5874 /* Return a filename that will open a connection to the process defined by
5875 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
5876 a filename in /dev/fd corresponding to a descriptor that is one of the
5877 ends of the pipe. If not defined, we use named pipes on systems that have
5878 them. Systems without /dev/fd and named pipes are out of luck.
5880 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
5881 use the read end of the pipe and dup that file descriptor to fd 0 in
5882 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
5883 writing or use the write end of the pipe in the child, and dup that
5884 file descriptor to fd 1 in the child. The parent does the opposite. */
5887 process_substitute (string
, open_for_read_in_child
)
5889 int open_for_read_in_child
;
5892 int fd
, result
, rc
, function_value
;
5894 #if defined (HAVE_DEV_FD)
5895 int parent_pipe_fd
, child_pipe_fd
;
5897 #endif /* HAVE_DEV_FD */
5898 #if defined (JOB_CONTROL)
5899 pid_t old_pipeline_pgrp
;
5902 if (!string
|| !*string
|| wordexp_only
)
5903 return ((char *)NULL
);
5905 #if !defined (HAVE_DEV_FD)
5906 pathname
= make_named_pipe ();
5907 #else /* HAVE_DEV_FD */
5908 if (pipe (fildes
) < 0)
5910 sys_error ("%s", _("cannot make pipe for process substitution"));
5911 return ((char *)NULL
);
5913 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
5914 the pipe in the parent, otherwise the read end. */
5915 parent_pipe_fd
= fildes
[open_for_read_in_child
];
5916 child_pipe_fd
= fildes
[1 - open_for_read_in_child
];
5917 /* Move the parent end of the pipe to some high file descriptor, to
5918 avoid clashes with FDs used by the script. */
5919 parent_pipe_fd
= move_to_high_fd (parent_pipe_fd
, 1, 64);
5921 pathname
= make_dev_fd_filename (parent_pipe_fd
);
5922 #endif /* HAVE_DEV_FD */
5926 sys_error ("%s", _("cannot make pipe for process substitution"));
5927 return ((char *)NULL
);
5930 old_pid
= last_made_pid
;
5932 #if defined (JOB_CONTROL)
5933 old_pipeline_pgrp
= pipeline_pgrp
;
5934 if (pipeline_pgrp
== 0 || (subshell_environment
& (SUBSHELL_PIPE
|SUBSHELL_FORK
|SUBSHELL_ASYNC
)) == 0)
5935 pipeline_pgrp
= shell_pgrp
;
5937 #endif /* JOB_CONTROL */
5939 pid
= make_child ((char *)NULL
, FORK_ASYNC
);
5943 int old_interactive
;
5945 old_interactive
= interactive
;
5947 /* The currently-executing shell is not interactive */
5950 reset_terminating_signals (); /* XXX */
5951 free_pushed_string_input ();
5952 /* Cancel traps, in trap.c. */
5953 restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */
5954 subshell_environment
&= ~SUBSHELL_IGNTRAP
;
5955 QUIT
; /* catch any interrupts we got post-fork */
5956 setup_async_signals ();
5958 if (open_for_read_in_child
== 0 && old_interactive
&& (bash_input
.type
== st_stdin
|| bash_input
.type
== st_stream
))
5959 async_redirect_stdin ();
5962 subshell_environment
|= SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
|SUBSHELL_ASYNC
;
5964 /* We don't inherit the verbose option for command substitutions now, so
5965 let's try it for process substitutions. */
5966 change_flag ('v', FLAG_OFF
);
5968 /* if we're expanding a redirection, we shouldn't have access to the
5969 temporary environment, but commands in the subshell should have
5970 access to their own temporary environment. */
5971 if (expanding_redir
)
5972 flush_temporary_env ();
5975 #if defined (JOB_CONTROL)
5976 set_sigchld_handler ();
5977 stop_making_children ();
5978 /* XXX - should we only do this in the parent? (as in command subst) */
5979 pipeline_pgrp
= old_pipeline_pgrp
;
5981 stop_making_children ();
5982 #endif /* JOB_CONTROL */
5986 sys_error ("%s", _("cannot make child for process substitution"));
5988 #if defined (HAVE_DEV_FD)
5989 close (parent_pipe_fd
);
5990 close (child_pipe_fd
);
5991 #endif /* HAVE_DEV_FD */
5992 #if defined (JOB_CONTROL)
5993 restore_pipeline (1);
5995 return ((char *)NULL
);
6000 #if defined (JOB_CONTROL)
6001 last_procsub_child
= restore_pipeline (0);
6002 /* We assume that last_procsub_child->next == last_procsub_child because
6003 of how jobs.c:add_process() works. */
6004 last_procsub_child
->next
= 0;
6005 procsub_add (last_procsub_child
);
6008 #if defined (HAVE_DEV_FD)
6009 dev_fd_list
[parent_pipe_fd
] = pid
;
6011 fifo_list
[nfifo
-1].proc
= pid
;
6014 last_made_pid
= old_pid
;
6016 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
6018 #endif /* JOB_CONTROL && PGRP_PIPE */
6020 #if defined (HAVE_DEV_FD)
6021 close (child_pipe_fd
);
6022 #endif /* HAVE_DEV_FD */
6027 set_sigint_handler ();
6029 #if defined (JOB_CONTROL)
6030 /* make sure we don't have any job control */
6031 set_job_control (0);
6033 /* Clear out any existing list of process substitutions */
6036 /* The idea is that we want all the jobs we start from an async process
6037 substitution to be in the same process group, but not the same pgrp
6038 as our parent shell, since we don't want to affect our parent shell's
6039 jobs if we get a SIGHUP and end up calling hangup_all_jobs, for example.
6040 If pipeline_pgrp != shell_pgrp, we assume that there is a job control
6041 shell somewhere in our parent process chain (since make_child initializes
6042 pipeline_pgrp to shell_pgrp if job_control == 0). What we do in this
6043 case is to set pipeline_pgrp to our PID, so all jobs started by this
6044 process have that same pgrp and we are basically the process group leader.
6045 This should not have negative effects on child processes surviving
6046 after we exit, since we wait for the children we create, but that is
6047 something to watch for. */
6049 if (pipeline_pgrp
!= shell_pgrp
)
6050 pipeline_pgrp
= getpid ();
6051 #endif /* JOB_CONTROL */
6053 #if !defined (HAVE_DEV_FD)
6054 /* Open the named pipe in the child. */
6055 fd
= open (pathname
, open_for_read_in_child
? O_RDONLY
: O_WRONLY
);
6058 /* Two separate strings for ease of translation. */
6059 if (open_for_read_in_child
)
6060 sys_error (_("cannot open named pipe %s for reading"), pathname
);
6062 sys_error (_("cannot open named pipe %s for writing"), pathname
);
6066 if (open_for_read_in_child
)
6068 if (sh_unset_nodelay_mode (fd
) < 0)
6070 sys_error (_("cannot reset nodelay mode for fd %d"), fd
);
6074 #else /* HAVE_DEV_FD */
6076 #endif /* HAVE_DEV_FD */
6078 /* Discard buffered stdio output before replacing the underlying file
6080 if (open_for_read_in_child
== 0)
6083 if (dup2 (fd
, open_for_read_in_child
? 0 : 1) < 0)
6085 sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname
,
6086 open_for_read_in_child
? 0 : 1);
6090 if (fd
!= (open_for_read_in_child
? 0 : 1))
6093 /* Need to close any files that this process has open to pipes inherited
6095 if (current_fds_to_close
)
6097 close_fd_bitmap (current_fds_to_close
);
6098 current_fds_to_close
= (struct fd_bitmap
*)NULL
;
6101 #if defined (HAVE_DEV_FD)
6102 /* Make sure we close the parent's end of the pipe and clear the slot
6103 in the fd list so it is not closed later, if reallocated by, for
6104 instance, pipe(2). */
6105 close (parent_pipe_fd
);
6106 dev_fd_list
[parent_pipe_fd
] = 0;
6107 #endif /* HAVE_DEV_FD */
6109 /* subshells shouldn't have this flag, which controls using the temporary
6110 environment for variable lookups. We have already flushed the temporary
6111 environment above in the case we're expanding a redirection, so processes
6112 executed by this command need to be able to set it independently of their
6114 expanding_redir
= 0;
6116 remove_quoted_escapes (string
);
6118 #if 0 /* TAG: bash-5.2 */
6119 startup_state
= 2; /* see if we can avoid a fork */
6120 parse_and_execute_level
= 0;
6123 /* Give process substitution a place to jump back to on failure,
6124 so we don't go back up to main (). */
6125 result
= setjmp_nosigs (top_level
);
6127 /* If we're running a process substitution inside a shell function,
6128 trap `return' so we don't return from the function in the subshell
6129 and go off to never-never land. */
6130 if (result
== 0 && return_catch_flag
)
6131 function_value
= setjmp_nosigs (return_catch
);
6135 if (result
== ERREXIT
)
6136 rc
= last_command_exit_value
;
6137 else if (result
== EXITPROG
)
6138 rc
= last_command_exit_value
;
6140 rc
= EXECUTION_FAILURE
;
6141 else if (function_value
)
6142 rc
= return_catch_value
;
6146 rc
= parse_and_execute (string
, "process substitution", (SEVAL_NONINT
|SEVAL_NOHIST
));
6147 /* leave subshell level intact for any exit trap */
6150 #if !defined (HAVE_DEV_FD)
6151 /* Make sure we close the named pipe in the child before we exit. */
6152 close (open_for_read_in_child
? 0 : 1);
6153 #endif /* !HAVE_DEV_FD */
6155 last_command_exit_value
= rc
;
6156 rc
= run_exit_trap ();
6160 #endif /* PROCESS_SUBSTITUTION */
6162 /***********************************/
6164 /* Command Substitution */
6166 /***********************************/
6169 read_comsub (fd
, quoted
, flags
, rflag
)
6170 int fd
, quoted
, flags
;
6173 char *istring
, buf
[512], *bufp
;
6174 int istring_index
, c
, tflag
, skip_ctlesc
, skip_ctlnul
;
6176 size_t istring_size
;
6179 #if defined (HANDLE_MULTIBYTE)
6186 istring
= (char *)NULL
;
6187 istring_index
= istring_size
= bufn
= tflag
= 0;
6189 skip_ctlesc
= ifs_cmap
[CTLESC
];
6190 skip_ctlnul
= ifs_cmap
[CTLNUL
];
6192 mb_cur_max
= MB_CUR_MAX
;
6195 /* Read the output of the command through the pipe. */
6202 bufn
= zread (fd
, buf
, sizeof (buf
));
6214 internal_warning ("%s", _("command substitution: ignored null byte in input"));
6221 /* Add the character to ISTRING, possibly after resizing it. */
6222 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, mb_cur_max
+1, istring_size
, 512);
6224 /* This is essentially quote_string inline */
6225 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) /* || c == CTLESC || c == CTLNUL */)
6226 istring
[istring_index
++] = CTLESC
;
6227 else if ((flags
& PF_ASSIGNRHS
) && skip_ctlesc
&& c
== CTLESC
)
6228 istring
[istring_index
++] = CTLESC
;
6229 /* Escape CTLESC and CTLNUL in the output to protect those characters
6230 from the rest of the word expansions (word splitting and globbing.)
6231 This is essentially quote_escapes inline. */
6232 else if (skip_ctlesc
== 0 && c
== CTLESC
)
6233 istring
[istring_index
++] = CTLESC
;
6234 else if ((skip_ctlnul
== 0 && c
== CTLNUL
) || (c
== ' ' && (ifs_value
&& *ifs_value
== 0)))
6235 istring
[istring_index
++] = CTLESC
;
6237 #if defined (HANDLE_MULTIBYTE)
6238 if ((locale_utf8locale
&& (c
& 0x80)) ||
6239 (locale_utf8locale
== 0 && mb_cur_max
> 1 && (unsigned char)c
> 127))
6241 /* read a multibyte character from buf */
6242 /* punt on the hard case for now */
6243 memset (&ps
, '\0', sizeof (mbstate_t));
6244 mblen
= mbrtowc (&wc
, bufp
-1, bufn
+1, &ps
);
6245 if (MB_INVALIDCH (mblen
) || mblen
== 0 || mblen
== 1)
6246 istring
[istring_index
++] = c
;
6249 istring
[istring_index
++] = c
;
6250 for (i
= 0; i
< mblen
-1; i
++)
6251 istring
[istring_index
++] = *bufp
++;
6258 istring
[istring_index
++] = c
;
6262 istring
[istring_index
] = '\0';
6264 /* If we read no output, just return now and save ourselves some
6266 if (istring_index
== 0)
6271 return (char *)NULL
;
6274 /* Strip trailing newlines from the output of the command. */
6275 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
6277 while (istring_index
> 0)
6279 if (istring
[istring_index
- 1] == '\n')
6283 /* If the newline was quoted, remove the quoting char. */
6284 if (istring
[istring_index
- 1] == CTLESC
)
6290 istring
[istring_index
] = '\0';
6293 strip_trailing (istring
, istring_index
- 1, 1);
6300 /* Perform command substitution on STRING. This returns a WORD_DESC * with the
6301 contained string possibly quoted. */
6303 command_substitute (string
, quoted
, flags
)
6308 pid_t pid
, old_pid
, old_pipeline_pgrp
, old_async_pid
;
6310 int result
, fildes
[2], function_value
, pflags
, rc
, tflag
, fork_flags
;
6314 istring
= (char *)NULL
;
6316 /* Don't fork () if there is no need to. In the case of no command to
6317 run, just return NULL. */
6319 for (s
= string
; s
&& *s
&& (shellblank (*s
) || *s
== '\n'); s
++)
6321 if (s
== 0 || *s
== 0)
6322 return ((WORD_DESC
*)NULL
);
6324 if (!string
|| !*string
|| (string
[0] == '\n' && !string
[1]))
6325 return ((WORD_DESC
*)NULL
);
6328 if (wordexp_only
&& read_but_dont_execute
)
6330 last_command_exit_value
= EX_WEXPCOMSUB
;
6331 jump_to_top_level (EXITPROG
);
6334 /* We're making the assumption here that the command substitution will
6335 eventually run a command from the file system. Since we'll run
6336 maybe_make_export_env in this subshell before executing that command,
6337 the parent shell and any other shells it starts will have to remake
6338 the environment. If we make it before we fork, other shells won't
6339 have to. Don't bother if we have any temporary variable assignments,
6340 though, because the export environment will be remade after this
6341 command completes anyway, but do it if all the words to be expanded
6342 are variable assignments. */
6343 if (subst_assign_varlist
== 0 || garglist
== 0)
6344 maybe_make_export_env (); /* XXX */
6346 /* Flags to pass to parse_and_execute() */
6347 pflags
= (interactive
&& sourcelevel
== 0) ? SEVAL_RESETLINE
: 0;
6349 old_pid
= last_made_pid
;
6351 /* Pipe the output of executing STRING into the current shell. */
6352 if (pipe (fildes
) < 0)
6354 sys_error ("%s", _("cannot make pipe for command substitution"));
6358 #if defined (JOB_CONTROL)
6359 old_pipeline_pgrp
= pipeline_pgrp
;
6360 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline or
6361 we've already forked to run a disk command (and are expanding redirections,
6363 if ((subshell_environment
& (SUBSHELL_FORK
|SUBSHELL_PIPE
)) == 0)
6364 pipeline_pgrp
= shell_pgrp
;
6365 cleanup_the_pipeline ();
6366 #endif /* JOB_CONTROL */
6368 old_async_pid
= last_asynchronous_pid
;
6369 fork_flags
= (subshell_environment
&SUBSHELL_ASYNC
) ? FORK_ASYNC
: 0;
6370 pid
= make_child ((char *)NULL
, fork_flags
|FORK_NOTERM
);
6371 last_asynchronous_pid
= old_async_pid
;
6375 /* Reset the signal handlers in the child, but don't free the
6376 trap strings. Set a flag noting that we have to free the
6377 trap strings if we run trap to change a signal disposition. */
6378 reset_signal_handlers ();
6381 kill (getpid (), SIGINT
);
6382 CLRINTERRUPT
; /* if we're ignoring SIGINT somehow */
6384 QUIT
; /* catch any interrupts we got post-fork */
6385 subshell_environment
|= SUBSHELL_RESETTRAP
;
6386 subshell_environment
&= ~SUBSHELL_IGNTRAP
;
6389 #if defined (JOB_CONTROL)
6390 /* XXX DO THIS ONLY IN PARENT ? XXX */
6391 set_sigchld_handler ();
6392 stop_making_children ();
6394 pipeline_pgrp
= old_pipeline_pgrp
;
6396 stop_making_children ();
6397 #endif /* JOB_CONTROL */
6401 sys_error (_("cannot make child for command substitution"));
6404 last_made_pid
= old_pid
;
6409 return ((WORD_DESC
*)NULL
);
6414 /* The currently executing shell is not interactive. */
6417 #if defined (JOB_CONTROL)
6418 /* Invariant: in child processes started to run command substitutions,
6419 pipeline_pgrp == shell_pgrp. Other parts of the shell assume this. */
6420 if (pipeline_pgrp
> 0 && pipeline_pgrp
!= shell_pgrp
)
6421 shell_pgrp
= pipeline_pgrp
;
6424 set_sigint_handler (); /* XXX */
6426 free_pushed_string_input ();
6428 /* Discard buffered stdio output before replacing the underlying file
6432 if (dup2 (fildes
[1], 1) < 0)
6434 sys_error ("%s", _("command_substitute: cannot duplicate pipe as fd 1"));
6435 exit (EXECUTION_FAILURE
);
6438 /* If standard output is closed in the parent shell
6439 (such as after `exec >&-'), file descriptor 1 will be
6440 the lowest available file descriptor, and end up in
6441 fildes[0]. This can happen for stdin and stderr as well,
6442 but stdout is more important -- it will cause no output
6443 to be generated from this command. */
6444 if ((fildes
[1] != fileno (stdin
)) &&
6445 (fildes
[1] != fileno (stdout
)) &&
6446 (fildes
[1] != fileno (stderr
)))
6449 if ((fildes
[0] != fileno (stdin
)) &&
6450 (fildes
[0] != fileno (stdout
)) &&
6451 (fildes
[0] != fileno (stderr
)))
6455 /* Let stdio know the fd may have changed from text to binary mode, and
6456 make sure to preserve stdout line buffering. */
6457 freopen (NULL
, "w", stdout
);
6458 sh_setlinebuf (stdout
);
6459 #endif /* __CYGWIN__ */
6461 /* This is a subshell environment. */
6462 subshell_environment
|= SUBSHELL_COMSUB
;
6464 /* Many shells do not appear to inherit the -v option for command
6466 change_flag ('v', FLAG_OFF
);
6468 /* When inherit_errexit option is not enabled, command substitution does
6469 not inherit the -e flag. It is enabled when Posix mode is enabled */
6470 if (inherit_errexit
== 0)
6472 builtin_ignoring_errexit
= 0;
6473 change_flag ('e', FLAG_OFF
);
6477 /* If we are expanding a redirection, we can dispose of any temporary
6478 environment we received, since redirections are not supposed to have
6479 access to the temporary environment. We will have to see whether this
6480 affects temporary environments supplied to `eval', but the temporary
6481 environment gets copied to builtin_env at some point. */
6482 if (expanding_redir
)
6484 flush_temporary_env ();
6485 expanding_redir
= 0;
6488 remove_quoted_escapes (string
);
6490 startup_state
= 2; /* see if we can avoid a fork */
6491 parse_and_execute_level
= 0;
6493 /* Give command substitution a place to jump back to on failure,
6494 so we don't go back up to main (). */
6495 result
= setjmp_nosigs (top_level
);
6497 /* If we're running a command substitution inside a shell function,
6498 trap `return' so we don't return from the function in the subshell
6499 and go off to never-never land. */
6500 if (result
== 0 && return_catch_flag
)
6501 function_value
= setjmp_nosigs (return_catch
);
6505 if (result
== ERREXIT
)
6506 rc
= last_command_exit_value
;
6507 else if (result
== EXITPROG
)
6508 rc
= last_command_exit_value
;
6510 rc
= EXECUTION_FAILURE
;
6511 else if (function_value
)
6512 rc
= return_catch_value
;
6516 rc
= parse_and_execute (string
, "command substitution", pflags
|SEVAL_NOHIST
);
6517 /* leave subshell level intact for any exit trap */
6520 last_command_exit_value
= rc
;
6521 rc
= run_exit_trap ();
6522 #if defined (PROCESS_SUBSTITUTION)
6523 unlink_fifo_list ();
6531 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
6533 #endif /* JOB_CONTROL && PGRP_PIPE */
6537 begin_unwind_frame ("read-comsub");
6538 dummyfd
= fildes
[0];
6539 add_unwind_protect (close
, dummyfd
);
6541 /* Block SIGINT while we're reading from the pipe. If the child
6542 process gets a SIGINT, it will either handle it or die, and the
6543 read will return. */
6544 BLOCK_SIGNAL (SIGINT
, set
, oset
);
6546 istring
= read_comsub (fildes
[0], quoted
, flags
, &tflag
);
6549 discard_unwind_frame ("read-comsub");
6550 UNBLOCK_SIGNAL (oset
);
6552 current_command_subst_pid
= pid
;
6553 last_command_exit_value
= wait_for (pid
, JWAIT_NOTERM
);
6554 last_command_subst_pid
= pid
;
6555 last_made_pid
= old_pid
;
6557 #if defined (JOB_CONTROL)
6558 /* If last_command_exit_value > 128, then the substituted command
6559 was terminated by a signal. If that signal was SIGINT, then send
6560 SIGINT to ourselves. This will break out of loops, for instance. */
6561 if (last_command_exit_value
== (128 + SIGINT
) && last_command_exit_signal
== SIGINT
)
6562 kill (getpid (), SIGINT
);
6563 #endif /* JOB_CONTROL */
6565 ret
= alloc_word_desc ();
6566 ret
->word
= istring
;
6573 /********************************************************
6575 * Utility functions for parameter expansion *
6577 ********************************************************/
6579 #if defined (ARRAY_VARS)
6582 array_length_reference (s
)
6593 var
= array_variable_part (s
, 0, &t
, &len
);
6595 /* If unbound variables should generate an error, report one and return
6597 if ((var
== 0 || invisible_p (var
) || (assoc_p (var
) == 0 && array_p (var
) == 0)) && unbound_vars_is_error
)
6601 set_exit_status (EXECUTION_FAILURE
);
6606 else if (var
== 0 || invisible_p (var
))
6609 /* We support a couple of expansions for variables that are not arrays.
6610 We'll return the length of the value for v[0], and 1 for v[@] or
6611 v[*]. Return 0 for everything else. */
6613 array
= array_p (var
) ? array_cell (var
) : (ARRAY
*)NULL
;
6614 h
= assoc_p (var
) ? assoc_cell (var
) : (HASH_TABLE
*)NULL
;
6616 if (ALL_ELEMENT_SUB (t
[0]) && t
[1] == RBRACK
)
6619 return (h
? assoc_num_elements (h
) : 0);
6620 else if (array_p (var
))
6621 return (array
? array_num_elements (array
) : 0);
6623 return (var_isset (var
) ? 1 : 0);
6629 akey
= expand_assignment_string_to_string (t
, 0); /* [ */
6630 t
[len
- 1] = RBRACK
;
6631 if (akey
== 0 || *akey
== 0)
6633 err_badarraysub (t
);
6637 t
= assoc_reference (assoc_cell (var
), akey
);
6642 ind
= array_expand_index (var
, t
, len
, 0);
6643 /* negative subscripts to indexed arrays count back from end */
6644 if (var
&& array_p (var
) && ind
< 0)
6645 ind
= array_max_index (array_cell (var
)) + 1 + ind
;
6648 err_badarraysub (t
);
6652 t
= array_reference (array
, ind
);
6654 t
= (ind
== 0) ? value_cell (var
) : (char *)NULL
;
6657 len
= MB_STRLEN (t
);
6660 #endif /* ARRAY_VARS */
6663 valid_brace_expansion_word (name
, var_is_special
)
6667 if (DIGIT (*name
) && all_digits (name
))
6669 else if (var_is_special
)
6671 #if defined (ARRAY_VARS)
6672 else if (valid_array_reference (name
, 0))
6674 #endif /* ARRAY_VARS */
6675 else if (legal_identifier (name
))
6682 chk_atstar (name
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
)
6685 int *quoted_dollar_atp
, *contains_dollar_at
;
6691 if (quoted_dollar_atp
)
6692 *quoted_dollar_atp
= 0;
6693 if (contains_dollar_at
)
6694 *contains_dollar_at
= 0;
6698 /* check for $@ and $* */
6699 if (name
[0] == '@' && name
[1] == 0)
6701 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
6702 *quoted_dollar_atp
= 1;
6703 if (contains_dollar_at
)
6704 *contains_dollar_at
= 1;
6707 else if (name
[0] == '*' && name
[1] == '\0' && quoted
== 0)
6709 /* Need more checks here that parallel what string_list_pos_params and
6710 param_expand do. Check expand_no_split_dollar_star and ??? */
6711 if (contains_dollar_at
&& expand_no_split_dollar_star
== 0)
6712 *contains_dollar_at
= 1;
6716 /* Now check for ${array[@]} and ${array[*]} */
6717 #if defined (ARRAY_VARS)
6718 else if (valid_array_reference (name
, 0))
6720 temp1
= mbschr (name
, LBRACK
);
6721 if (temp1
&& temp1
[1] == '@' && temp1
[2] == RBRACK
)
6723 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
6724 *quoted_dollar_atp
= 1;
6725 if (contains_dollar_at
)
6726 *contains_dollar_at
= 1;
6729 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
6730 which should result in separate words even when IFS is unset. */
6731 if (temp1
&& temp1
[1] == '*' && temp1
[2] == RBRACK
&& quoted
== 0)
6733 if (contains_dollar_at
)
6734 *contains_dollar_at
= 1;
6742 /* Parameter expand NAME, and return a new string which is the expansion,
6743 or NULL if there was no expansion. NAME is as given in ${NAMEcWORD}.
6744 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
6745 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
6746 NAME was found inside of a double-quoted expression. */
6748 parameter_brace_expand_word (name
, var_is_special
, quoted
, pflags
, indp
)
6750 int var_is_special
, quoted
, pflags
;
6767 /* Handle multiple digit arguments, as in ${11}. */
6768 if (legal_number (name
, &arg_index
))
6770 tt
= get_dollar_var_value (arg_index
);
6772 temp
= (*tt
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6774 : quote_escapes (tt
);
6776 temp
= (char *)NULL
;
6779 else if (var_is_special
) /* ${@} */
6782 tt
= (char *)xmalloc (2 + strlen (name
));
6783 tt
[sindex
= 0] = '$';
6784 strcpy (tt
+ 1, name
);
6786 ret
= param_expand (tt
, &sindex
, quoted
, (int *)NULL
, (int *)NULL
,
6787 (int *)NULL
, (int *)NULL
, pflags
);
6790 #if defined (ARRAY_VARS)
6791 else if (valid_array_reference (name
, 0))
6794 var
= array_variable_part (name
, 0, &tt
, (int *)0);
6795 /* These are the cases where word splitting will not be performed */
6796 if (pflags
& PF_ASSIGNRHS
)
6798 if (ALL_ELEMENT_SUB (tt
[0]) && tt
[1] == RBRACK
)
6800 /* Only treat as double quoted if array variable */
6801 if (var
&& (array_p (var
) || assoc_p (var
)))
6802 temp
= array_value (name
, quoted
|Q_DOUBLE_QUOTES
, AV_ASSIGNRHS
, &atype
, &ind
);
6804 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6807 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6809 /* Posix interp 888 */
6810 else if (pflags
& PF_NOSPLIT2
)
6812 /* Special cases, then general case, for each of A[@], A[*], A[n] */
6813 #if defined (HANDLE_MULTIBYTE)
6814 if (tt
[0] == '@' && tt
[1] == RBRACK
&& var
&& quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && ifs_firstc
[0] != ' ')
6816 if (tt
[0] == '@' && tt
[1] == RBRACK
&& var
&& quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && ifs_firstc
!= ' ')
6818 temp
= array_value (name
, Q_DOUBLE_QUOTES
, AV_ASSIGNRHS
, &atype
, &ind
);
6819 else if (tt
[0] == '@' && tt
[1] == RBRACK
)
6820 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6821 else if (tt
[0] == '*' && tt
[1] == RBRACK
&& expand_no_split_dollar_star
&& ifs_is_null
)
6822 temp
= array_value (name
, Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
, 0, &atype
, &ind
);
6823 else if (tt
[0] == '*' && tt
[1] == RBRACK
)
6824 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6826 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6828 else if (tt
[0] == '*' && tt
[1] == RBRACK
&& expand_no_split_dollar_star
&& ifs_is_null
)
6829 temp
= array_value (name
, Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
, 0, &atype
, &ind
);
6831 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6832 if (atype
== 0 && temp
)
6834 temp
= (*temp
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6835 ? quote_string (temp
)
6836 : quote_escapes (temp
);
6837 rflags
|= W_ARRAYIND
;
6841 else if (atype
== 1 && temp
&& QUOTED_NULL (temp
) && (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6842 rflags
|= W_HASQUOTEDNULL
;
6845 else if (var
= find_variable (name
))
6847 if (var_isset (var
) && invisible_p (var
) == 0)
6849 #if defined (ARRAY_VARS)
6850 /* We avoid a memory leak by saving TT as the memory allocated by
6851 assoc_to_string or array_to_string and leaving it 0 otherwise,
6852 then freeing TT after quoting temp. */
6854 if ((pflags
& PF_ALLINDS
) && assoc_p (var
))
6855 tt
= temp
= assoc_empty (assoc_cell (var
)) ? (char *)NULL
: assoc_to_string (assoc_cell (var
), " ", quoted
);
6856 else if ((pflags
& PF_ALLINDS
) && array_p (var
))
6857 tt
= temp
= array_empty (array_cell (var
)) ? (char *)NULL
: array_to_string (array_cell (var
), " ", quoted
);
6858 else if (assoc_p (var
))
6859 temp
= assoc_reference (assoc_cell (var
), "0");
6860 else if (array_p (var
))
6861 temp
= array_reference (array_cell (var
), 0);
6863 temp
= value_cell (var
);
6865 temp
= value_cell (var
);
6869 temp
= (*temp
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6870 ? quote_string (temp
)
6871 : ((pflags
& PF_ASSIGNRHS
) ? quote_rhs (temp
)
6872 : quote_escapes (temp
));
6876 temp
= (char *)NULL
;
6878 else if (var
= find_variable_last_nameref (name
, 0))
6880 temp
= nameref_cell (var
);
6881 #if defined (ARRAY_VARS)
6882 /* Handle expanding nameref whose value is x[n] */
6883 if (temp
&& *temp
&& valid_array_reference (temp
, 0))
6886 goto expand_arrayref
;
6890 /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */
6891 if (temp
&& *temp
&& legal_identifier (temp
) == 0)
6893 set_exit_status (EXECUTION_FAILURE
);
6894 report_error (_("%s: invalid variable name for name reference"), temp
);
6895 temp
= &expand_param_error
;
6898 temp
= (char *)NULL
;
6901 temp
= (char *)NULL
;
6905 ret
= alloc_word_desc ();
6907 ret
->flags
|= rflags
;
6913 parameter_brace_find_indir (name
, var_is_special
, quoted
, find_nameref
)
6915 int var_is_special
, quoted
, find_nameref
;
6922 if (find_nameref
&& var_is_special
== 0 && (v
= find_variable_last_nameref (name
, 0)) &&
6923 nameref_p (v
) && (t
= nameref_cell (v
)) && *t
)
6924 return (savestring (t
));
6926 /* If var_is_special == 0, and name is not an array reference, this does
6927 more expansion than necessary. It should really look up the variable's
6928 value and not try to expand it. */
6929 pflags
= PF_IGNUNBOUND
;
6930 /* Note that we're not going to be doing word splitting here */
6933 pflags
|= PF_ASSIGNRHS
; /* suppresses word splitting */
6934 oldex
= expand_no_split_dollar_star
;
6935 expand_no_split_dollar_star
= 1;
6937 w
= parameter_brace_expand_word (name
, var_is_special
, quoted
, pflags
, 0);
6939 expand_no_split_dollar_star
= oldex
;
6942 /* Have to dequote here if necessary */
6945 temp
= ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) || var_is_special
)
6946 ? dequote_string (t
)
6947 : dequote_escapes (t
);
6951 dispose_word_desc (w
);
6956 /* Expand an indirect reference to a variable: ${!NAME} expands to the
6957 value of the variable whose name is the value of NAME. */
6959 parameter_brace_expand_indir (name
, var_is_special
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
)
6961 int var_is_special
, quoted
, pflags
;
6962 int *quoted_dollar_atp
, *contains_dollar_at
;
6968 /* See if it's a nameref first, behave in ksh93-compatible fashion.
6969 There is at least one incompatibility: given ${!foo[0]} where foo=bar,
6970 bash performs an indirect lookup on foo[0] and expands the result;
6971 ksh93 expands bar[0]. We could do that here -- there are enough usable
6972 primitives to do that -- but do not at this point. */
6973 if (var_is_special
== 0 && (v
= find_variable_last_nameref (name
, 0)))
6975 if (nameref_p (v
) && (t
= nameref_cell (v
)) && *t
)
6977 w
= alloc_word_desc ();
6978 w
->word
= savestring (t
);
6984 /* An indirect reference to a positional parameter or a special parameter
6985 is ok. Indirect references to array references, as explained above, are
6986 ok (currently). Only references to unset variables are errors at this
6988 if (legal_identifier (name
) && v
== 0)
6990 report_error (_("%s: invalid indirect expansion"), name
);
6991 w
= alloc_word_desc ();
6992 w
->word
= &expand_param_error
;
6997 t
= parameter_brace_find_indir (name
, var_is_special
, quoted
, 0);
6999 chk_atstar (t
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
);
7001 #if defined (ARRAY_VARS)
7002 /* Array references to unset variables are also an error */
7003 if (t
== 0 && valid_array_reference (name
, 0))
7005 v
= array_variable_part (name
, 0, (char **)0, (int *)0);
7008 report_error (_("%s: invalid indirect expansion"), name
);
7009 w
= alloc_word_desc ();
7010 w
->word
= &expand_param_error
;
7015 return (WORD_DESC
*)NULL
;
7020 return (WORD_DESC
*)NULL
;
7022 if (valid_brace_expansion_word (t
, SPECIAL_VAR (t
, 0)) == 0)
7024 report_error (_("%s: invalid variable name"), t
);
7026 w
= alloc_word_desc ();
7027 w
->word
= &expand_param_error
;
7032 w
= parameter_brace_expand_word (t
, SPECIAL_VAR(t
, 0), quoted
, pflags
, 0);
7038 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
7039 depending on the value of C, the separating character. C can be one of
7040 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
7041 between double quotes. */
7043 parameter_brace_expand_rhs (name
, value
, op
, quoted
, pflags
, qdollaratp
, hasdollarat
)
7045 int op
, quoted
, pflags
, *qdollaratp
, *hasdollarat
;
7049 char *t
, *t1
, *temp
, *vname
;
7050 int l_hasdollat
, sindex
;
7053 /*itrace("parameter_brace_expand_rhs: %s:%s pflags = %d", name, value, pflags);*/
7054 /* If the entire expression is between double quotes, we want to treat
7055 the value as a double-quoted string, with the exception that we strip
7056 embedded unescaped double quotes (for sh backwards compatibility). */
7057 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *value
)
7060 temp
= string_extract_double_quoted (value
, &sindex
, SX_STRIPDQ
);
7065 w
= alloc_word_desc ();
7067 l
= *temp
? expand_string_for_rhs (temp
, quoted
, op
, pflags
, &l_hasdollat
, (int *)NULL
)
7070 *hasdollarat
= l_hasdollat
|| (l
&& l
->next
);
7074 /* list_string takes multiple CTLNULs and turns them into an empty word
7075 with W_SAWQUOTEDNULL set. Turn it back into a single CTLNUL for the
7076 rest of this function and the caller. */
7077 for (tl
= l
; tl
; tl
= tl
->next
)
7079 if (tl
->word
&& (tl
->word
->word
== 0 || tl
->word
->word
[0] == 0) &&
7080 (tl
->word
->flags
| W_SAWQUOTEDNULL
))
7082 t
= make_quoted_char ('\0');
7083 FREE (tl
->word
->word
);
7085 tl
->word
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
7086 tl
->word
->flags
&= ~W_SAWQUOTEDNULL
;
7092 /* If l->next is not null, we know that TEMP contained "$@", since that
7093 is the only expansion that creates more than one word. */
7094 if (qdollaratp
&& ((l_hasdollat
&& quoted
) || l
->next
))
7096 /*itrace("parameter_brace_expand_rhs: %s:%s: l != NULL, set *qdollaratp", name, value);*/
7100 /* The expansion of TEMP returned something. We need to treat things
7101 slightly differently if L_HASDOLLAT is non-zero. If we have "$@",
7102 the individual words have already been quoted. We need to turn them
7103 into a string with the words separated by the first character of
7104 $IFS without any additional quoting, so string_list_dollar_at won't
7105 do the right thing. If IFS is null, we want "$@" to split into
7106 separate arguments, not be concatenated, so we use string_list_internal
7107 and mark the word to be split on spaces later. We use
7108 string_list_dollar_star for "$@" otherwise. */
7109 if (l
->next
&& ifs_is_null
)
7111 temp
= string_list_internal (l
, " ");
7112 w
->flags
|= W_SPLITSPACE
;
7114 else if (l_hasdollat
|| l
->next
)
7115 temp
= string_list_dollar_star (l
, quoted
, 0);
7118 temp
= string_list (l
);
7119 if (temp
&& (QUOTED_NULL (temp
) == 0) && (l
->word
->flags
& W_SAWQUOTEDNULL
))
7120 w
->flags
|= W_SAWQUOTEDNULL
; /* XXX */
7123 /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is
7124 a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the
7125 flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the
7126 expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7127 (which is more paranoia than anything else), we need to return the
7128 quoted null string and set the flags to indicate it. */
7129 if (l
->next
== 0 && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && QUOTED_NULL (temp
) && QUOTED_NULL (l
->word
->word
) && (l
->word
->flags
& W_HASQUOTEDNULL
))
7131 w
->flags
|= W_HASQUOTEDNULL
;
7132 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null, turning off qdollaratp", name, value);*/
7133 /* If we return a quoted null with L_HASDOLLARAT, we either have a
7134 construct like "${@-$@}" or "${@-${@-$@}}" with no positional
7135 parameters or a quoted expansion of "$@" with $1 == ''. In either
7136 case, we don't want to enable special handling of $@. */
7137 if (qdollaratp
&& l_hasdollat
)
7142 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && l_hasdollat
)
7144 /* Posix interp 221 changed the rules on this. The idea is that
7145 something like "$xxx$@" should expand the same as "${foo-$xxx$@}"
7146 when foo and xxx are unset. The problem is that it's not in any
7147 way backwards compatible and few other shells do it. We're eventually
7148 going to try and split the difference (heh) a little bit here. */
7149 /* l_hasdollat == 1 means we saw a quoted dollar at. */
7151 /* The brace expansion occurred between double quotes and there was
7152 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
7153 it does not expand to anything. In this case, we want to return
7154 a quoted empty string. Posix interp 888 */
7155 temp
= make_quoted_char ('\0');
7156 w
->flags
|= W_HASQUOTEDNULL
;
7157 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null", name, value);*/
7160 temp
= (char *)NULL
;
7162 if (op
== '-' || op
== '+')
7169 t1
= temp
? dequote_string (temp
) : savestring ("");
7175 (legal_variable_starter ((unsigned char)name
[1]) || DIGIT (name
[1]) || VALID_INDIR_PARAM (name
[1])))
7177 vname
= parameter_brace_find_indir (name
+ 1, SPECIAL_VAR (name
, 1), quoted
, 1);
7178 if (vname
== 0 || *vname
== 0)
7180 report_error (_("%s: invalid indirect expansion"), name
);
7184 return &expand_wdesc_error
;
7186 if (legal_identifier (vname
) == 0)
7188 report_error (_("%s: invalid variable name"), vname
);
7192 return &expand_wdesc_error
;
7196 #if defined (ARRAY_VARS)
7197 if (valid_array_reference (vname
, 0))
7198 v
= assign_array_element (vname
, t1
, 0);
7200 #endif /* ARRAY_VARS */
7201 v
= bind_variable (vname
, t1
, 0);
7203 if (v
== 0 || readonly_p (v
) || noassign_p (v
)) /* expansion error */
7205 if ((v
== 0 || readonly_p (v
)) && interactive_shell
== 0 && posixly_correct
)
7207 last_command_exit_value
= EXECUTION_FAILURE
;
7208 exp_jump_to_top_level (FORCE_EOF
);
7214 last_command_exit_value
= EX_BADUSAGE
;
7215 exp_jump_to_top_level (DISCARD
);
7219 stupidly_hack_special_variables (vname
);
7224 /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */
7226 /* If we are double-quoted or if we are not going to be performing word
7227 splitting, we want to quote the value we return appropriately, like
7228 the other expansions this function handles. */
7229 w
->word
= (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) ? quote_string (t1
) : quote_escapes (t1
);
7230 /* If we have something that's non-null, that's not a quoted null string,
7231 and we're not going to be performing word splitting (we know we're not
7232 because the operator is `='), we can forget we saw a quoted null. */
7233 if (w
->word
&& w
->word
[0] && QUOTED_NULL (w
->word
) == 0)
7234 w
->flags
&= ~W_SAWQUOTEDNULL
;
7237 /* If we convert a null string into a quoted null, make sure the caller
7239 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) && QUOTED_NULL (w
->word
))
7240 w
->flags
|= W_HASQUOTEDNULL
;
7245 /* Deal with the right hand side of a ${name:?value} expansion in the case
7246 that NAME is null or not set. If VALUE is non-null it is expanded and
7247 used as the error message to print, otherwise a standard message is
7250 parameter_brace_expand_error (name
, value
, check_null
)
7257 set_exit_status (EXECUTION_FAILURE
); /* ensure it's non-zero */
7258 if (value
&& *value
)
7260 l
= expand_string (value
, 0);
7261 temp
= string_list (l
);
7262 report_error ("%s: %s", name
, temp
? temp
: ""); /* XXX was value not "" */
7266 else if (check_null
== 0)
7267 report_error (_("%s: parameter not set"), name
);
7269 report_error (_("%s: parameter null or not set"), name
);
7271 /* Free the data we have allocated during this expansion, since we
7272 are about to longjmp out. */
7277 /* Return 1 if NAME is something for which parameter_brace_expand_length is
7280 valid_length_expression (name
)
7283 return (name
[1] == '\0' || /* ${#} */
7284 ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0') || /* special param */
7285 (DIGIT (name
[1]) && all_digits (name
+ 1)) || /* ${#11} */
7286 #if defined (ARRAY_VARS)
7287 valid_array_reference (name
+ 1, 0) || /* ${#a[7]} */
7289 legal_identifier (name
+ 1)); /* ${#PS1} */
7292 /* Handle the parameter brace expansion that requires us to return the
7293 length of a parameter. */
7295 parameter_brace_expand_length (name
)
7299 intmax_t number
, arg_index
;
7303 var
= (SHELL_VAR
*)NULL
;
7305 if (name
[1] == '\0') /* ${#} */
7306 number
= number_of_args ();
7307 else if (DOLLAR_AT_STAR (name
[1]) && name
[2] == '\0') /* ${#@}, ${#*} */
7308 number
= number_of_args ();
7309 else if ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0')
7311 /* Take the lengths of some of the shell's special parameters. */
7315 t
= which_set_flags ();
7318 t
= itos (last_command_exit_value
);
7321 t
= itos (dollar_dollar_pid
);
7324 if (last_asynchronous_pid
== NO_PID
)
7325 t
= (char *)NULL
; /* XXX - error if set -u set? */
7327 t
= itos (last_asynchronous_pid
);
7330 t
= itos (number_of_args ());
7333 number
= STRLEN (t
);
7336 #if defined (ARRAY_VARS)
7337 else if (valid_array_reference (name
+ 1, 0))
7338 number
= array_length_reference (name
+ 1);
7339 #endif /* ARRAY_VARS */
7344 if (legal_number (name
+ 1, &arg_index
)) /* ${#1} */
7346 t
= get_dollar_var_value (arg_index
);
7347 if (t
== 0 && unbound_vars_is_error
)
7349 number
= MB_STRLEN (t
);
7352 #if defined (ARRAY_VARS)
7353 else if ((var
= find_variable (name
+ 1)) && (invisible_p (var
) == 0) && (array_p (var
) || assoc_p (var
)))
7356 t
= assoc_reference (assoc_cell (var
), "0");
7358 t
= array_reference (array_cell (var
), 0);
7359 if (t
== 0 && unbound_vars_is_error
)
7361 number
= MB_STRLEN (t
);
7364 /* Fast path for the common case of taking the length of a non-dynamic
7365 scalar variable value. */
7366 else if ((var
|| (var
= find_variable (name
+ 1))) &&
7367 invisible_p (var
) == 0 &&
7368 array_p (var
) == 0 && assoc_p (var
) == 0 &&
7369 var
->dynamic_value
== 0)
7370 number
= value_cell (var
) ? MB_STRLEN (value_cell (var
)) : 0;
7371 else if (var
== 0 && unbound_vars_is_error
== 0)
7375 newname
= savestring (name
);
7377 list
= expand_string (newname
, Q_DOUBLE_QUOTES
);
7378 t
= list
? string_list (list
) : (char *)NULL
;
7381 dispose_words (list
);
7383 number
= t
? MB_STRLEN (t
) : 0;
7391 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
7392 so we do some ad-hoc parsing of an arithmetic expression to find
7393 the first DELIM, instead of using strchr(3). Two rules:
7394 1. If the substring contains a `(', read until closing `)'.
7395 2. If the substring contains a `?', read past one `:' for each `?'.
7396 The SD_ARITHEXP flag to skip_to_delim takes care of doing this.
7400 skiparith (substr
, delim
)
7410 i
= skip_to_delim (substr
, 0, delims
, SD_ARITHEXP
);
7411 return (substr
+ i
);
7414 /* Verify and limit the start and end of the desired substring. If
7415 VTYPE == 0, a regular shell variable is being used; if it is 1,
7416 then the positional parameters are being used; if it is 2, then
7417 VALUE is really a pointer to an array variable that should be used.
7418 Return value is 1 if both values were OK, 0 if there was a problem
7419 with an invalid expression, or -1 if the values were out of range. */
7421 verify_substring_values (v
, value
, substr
, vtype
, e1p
, e2p
)
7423 char *value
, *substr
;
7425 intmax_t *e1p
, *e2p
;
7427 char *t
, *temp1
, *temp2
;
7430 #if defined (ARRAY_VARS)
7435 /* duplicate behavior of strchr(3) */
7436 t
= skiparith (substr
, ':');
7437 if (*t
&& *t
== ':')
7442 temp1
= expand_arith_string (substr
, Q_DOUBLE_QUOTES
);
7443 *e1p
= evalexp (temp1
, 0, &expok
); /* XXX - EXP_EXPANDED? */
7448 len
= -1; /* paranoia */
7452 case VT_ARRAYMEMBER
:
7453 len
= MB_STRLEN (value
);
7456 len
= number_of_args () + 1;
7458 len
++; /* add one arg if counting from $0 */
7460 #if defined (ARRAY_VARS)
7462 /* For arrays, the first value deals with array indices. Negative
7463 offsets count from one past the array's maximum index. Associative
7464 arrays treat the number of elements as the maximum index. */
7468 len
= assoc_num_elements (h
) + (*e1p
< 0);
7473 len
= array_max_index (a
) + (*e1p
< 0); /* arrays index from 0 to n - 1 */
7479 if (len
== -1) /* paranoia */
7482 if (*e1p
< 0) /* negative offsets count from end */
7485 if (*e1p
> len
|| *e1p
< 0)
7488 #if defined (ARRAY_VARS)
7489 /* For arrays, the second offset deals with the number of elements. */
7490 if (vtype
== VT_ARRAYVAR
)
7491 len
= assoc_p (v
) ? assoc_num_elements (h
) : array_num_elements (a
);
7497 temp2
= savestring (t
);
7498 temp1
= expand_arith_string (temp2
, Q_DOUBLE_QUOTES
);
7501 *e2p
= evalexp (temp1
, 0, &expok
); /* XXX - EXP_EXPANDED? */
7506 /* Should we allow positional parameter length < 0 to count backwards
7507 from end of positional parameters? */
7509 if ((vtype
== VT_ARRAYVAR
|| vtype
== VT_POSPARMS
) && *e2p
< 0)
7510 #else /* TAG: bash-5.2 */
7511 if (vtype
== VT_ARRAYVAR
&& *e2p
< 0)
7514 internal_error (_("%s: substring expression < 0"), t
);
7517 #if defined (ARRAY_VARS)
7518 /* In order to deal with sparse arrays, push the intelligence about how
7519 to deal with the number of elements desired down to the array-
7520 specific functions. */
7521 if (vtype
!= VT_ARRAYVAR
)
7527 if (*e2p
< 0 || *e2p
< *e1p
)
7529 internal_error (_("%s: substring expression < 0"), t
);
7534 *e2p
+= *e1p
; /* want E2 chars starting at E1 */
7545 /* Return the type of variable specified by VARNAME (simple variable,
7546 positional param, or array variable). Also return the value specified
7547 by VARNAME (value of a variable or a reference to an array element).
7548 QUOTED is the standard description of quoting state, using Q_* defines.
7549 FLAGS is currently a set of flags to pass to array_value. If IND is
7550 non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is
7551 passed to array_value so the array index is not computed again.
7552 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
7553 characters in the value are quoted with CTLESC and takes appropriate
7554 steps. For convenience, *VALP is set to the dequoted VALUE. */
7556 get_var_and_type (varname
, value
, ind
, quoted
, flags
, varp
, valp
)
7557 char *varname
, *value
;
7563 int vtype
, want_indir
;
7568 want_indir
= *varname
== '!' &&
7569 (legal_variable_starter ((unsigned char)varname
[1]) || DIGIT (varname
[1])
7570 || VALID_INDIR_PARAM (varname
[1]));
7572 vname
= parameter_brace_find_indir (varname
+1, SPECIAL_VAR (varname
, 1), quoted
, 1);
7573 /* XXX - what if vname == 0 || *vname == 0 ? */
7579 vtype
= VT_VARIABLE
;
7580 *varp
= (SHELL_VAR
*)NULL
;
7581 *valp
= (char *)NULL
;
7585 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
7586 vtype
= STR_DOLLAR_AT_STAR (vname
);
7587 if (vtype
== VT_POSPARMS
&& vname
[0] == '*')
7588 vtype
|= VT_STARSUB
;
7589 *varp
= (SHELL_VAR
*)NULL
;
7591 #if defined (ARRAY_VARS)
7592 if (valid_array_reference (vname
, 0))
7594 v
= array_variable_part (vname
, 0, &temp
, (int *)0);
7595 /* If we want to signal array_value to use an already-computed index,
7596 set LIND to that index */
7597 lind
= (ind
!= INTMAX_MIN
&& (flags
& AV_USEIND
)) ? ind
: 0;
7598 if (v
&& invisible_p (v
))
7600 vtype
= VT_ARRAYMEMBER
;
7601 *varp
= (SHELL_VAR
*)NULL
;
7602 *valp
= (char *)NULL
;
7604 if (v
&& (array_p (v
) || assoc_p (v
)))
7606 if (ALL_ELEMENT_SUB (temp
[0]) && temp
[1] == RBRACK
)
7608 /* Callers have to differentiate between indexed and associative */
7609 vtype
= VT_ARRAYVAR
;
7611 vtype
|= VT_STARSUB
;
7612 *valp
= array_p (v
) ? (char *)array_cell (v
) : (char *)assoc_cell (v
);
7616 vtype
= VT_ARRAYMEMBER
;
7617 *valp
= array_value (vname
, Q_DOUBLE_QUOTES
, flags
, (int *)NULL
, &lind
);
7621 else if (v
&& (ALL_ELEMENT_SUB (temp
[0]) && temp
[1] == RBRACK
))
7623 vtype
= VT_VARIABLE
;
7625 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
7626 *valp
= value
? dequote_string (value
) : (char *)NULL
;
7628 *valp
= value
? dequote_escapes (value
) : (char *)NULL
;
7632 vtype
= VT_ARRAYMEMBER
;
7634 *valp
= array_value (vname
, Q_DOUBLE_QUOTES
, flags
, (int *)NULL
, &lind
);
7637 else if ((v
= find_variable (vname
)) && (invisible_p (v
) == 0) && (assoc_p (v
) || array_p (v
)))
7639 vtype
= VT_ARRAYMEMBER
;
7641 *valp
= assoc_p (v
) ? assoc_reference (assoc_cell (v
), "0") : array_reference (array_cell (v
), 0);
7646 if (value
&& vtype
== VT_VARIABLE
)
7648 *varp
= find_variable (vname
);
7649 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
7650 *valp
= dequote_string (value
);
7652 *valp
= dequote_escapes (value
);
7664 /***********************************************************/
7666 /* Functions to perform transformations on variable values */
7668 /***********************************************************/
7671 string_var_assignment (v
, s
)
7675 char flags
[MAX_ATTRIBUTES
], *ret
, *val
;
7678 val
= (v
&& (invisible_p (v
) || var_isset (v
) == 0)) ? (char *)NULL
: sh_quote_reusable (s
, 0);
7679 i
= var_attribute_string (v
, 0, flags
);
7680 if (i
== 0 && val
== 0)
7681 return (char *)NULL
;
7683 ret
= (char *)xmalloc (i
+ STRLEN (val
) + strlen (v
->name
) + 16 + MAX_ATTRIBUTES
);
7684 if (i
> 0 && val
== 0)
7685 sprintf (ret
, "declare -%s %s", flags
, v
->name
);
7687 sprintf (ret
, "declare -%s %s=%s", flags
, v
->name
, val
);
7689 sprintf (ret
, "%s=%s", v
->name
, val
);
7694 #if defined (ARRAY_VARS)
7696 array_var_assignment (v
, itype
, quoted
, atype
)
7698 int itype
, quoted
, atype
;
7700 char *ret
, *val
, flags
[MAX_ATTRIBUTES
];
7704 return (char *)NULL
;
7706 val
= array_p (v
) ? array_to_kvpair (array_cell (v
), 0)
7707 : assoc_to_kvpair (assoc_cell (v
), 0);
7709 val
= array_p (v
) ? array_to_assign (array_cell (v
), 0)
7710 : assoc_to_assign (assoc_cell (v
), 0);
7712 if (val
== 0 && (invisible_p (v
) || var_isset (v
) == 0))
7716 val
= (char *)xmalloc (3);
7723 ret
= (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) ? quote_string (val
) : quote_escapes (val
);
7731 i
= var_attribute_string (v
, 0, flags
);
7732 ret
= (char *)xmalloc (i
+ STRLEN (val
) + strlen (v
->name
) + 16);
7734 sprintf (ret
, "declare -%s %s=%s", flags
, v
->name
, val
);
7736 sprintf (ret
, "declare -%s %s", flags
, v
->name
);
7743 pos_params_assignment (list
, itype
, quoted
)
7750 /* first, we transform the list to quote each word. */
7751 temp
= list_transform ('Q', (SHELL_VAR
*)0, list
, itype
, quoted
);
7752 ret
= (char *)xmalloc (strlen (temp
) + 8);
7753 strcpy (ret
, "set -- ");
7754 strcpy (ret
+ 7, temp
);
7760 string_transform (xc
, v
, s
)
7765 char *ret
, flags
[MAX_ATTRIBUTES
], *t
;
7768 if (((xc
== 'A' || xc
== 'a') && v
== 0))
7769 return (char *)NULL
;
7770 else if (xc
!= 'a' && xc
!= 'A' && s
== 0)
7771 return (char *)NULL
;
7775 /* Transformations that interrogate the variable */
7777 i
= var_attribute_string (v
, 0, flags
);
7778 ret
= (i
> 0) ? savestring (flags
) : (char *)NULL
;
7781 ret
= string_var_assignment (v
, s
);
7784 ret
= sh_quote_reusable (s
, 0);
7786 /* Transformations that modify the variable's value */
7788 t
= ansiexpand (s
, 0, strlen (s
), (int *)0);
7789 ret
= dequote_escapes (t
);
7793 ret
= decode_prompt_string (s
);
7796 ret
= sh_quote_reusable (s
, 0);
7799 ret
= sh_modcase (s
, 0, CASE_UPPER
);
7802 ret
= sh_modcase (s
, 0, CASE_UPFIRST
); /* capitalize */
7805 ret
= sh_modcase (s
, 0, CASE_LOWER
);
7815 list_transform (xc
, v
, list
, itype
, quoted
)
7826 for (new = (WORD_LIST
*)NULL
, l
= list
; l
; l
= l
->next
)
7828 tword
= string_transform (xc
, v
, l
->word
->word
);
7829 w
= alloc_word_desc ();
7830 w
->word
= tword
? tword
: savestring (""); /* XXX */
7831 new = make_word_list (w
, new);
7833 l
= REVERSE_LIST (new, WORD_LIST
*);
7836 /* If we are expanding in a context where word splitting will not be
7837 performed, treat as quoted. This changes how $* will be expanded. */
7838 if (itype
== '*' && expand_no_split_dollar_star
&& ifs_is_null
)
7839 qflags
|= Q_DOUBLE_QUOTES
; /* Posix interp 888 */
7841 tword
= string_list_pos_params (itype
, l
, qflags
, 0);
7848 parameter_list_transform (xc
, itype
, quoted
)
7856 list
= list_rest_of_args ();
7858 return ((char *)NULL
);
7860 ret
= pos_params_assignment (list
, itype
, quoted
);
7862 ret
= list_transform (xc
, (SHELL_VAR
*)0, list
, itype
, quoted
);
7863 dispose_words (list
);
7867 #if defined (ARRAY_VARS)
7869 array_transform (xc
, var
, starsub
, quoted
)
7872 int starsub
; /* so we can figure out how it's indexed */
7882 v
= var
; /* XXX - for now */
7884 itype
= starsub
? '*' : '@';
7887 return (array_var_assignment (v
, itype
, quoted
, 1));
7889 return (array_var_assignment (v
, itype
, quoted
, 2));
7891 /* special case for unset arrays and attributes */
7892 if (xc
== 'a' && (invisible_p (v
) || var_isset (v
) == 0))
7894 char flags
[MAX_ATTRIBUTES
];
7897 i
= var_attribute_string (v
, 0, flags
);
7898 return ((i
> 0) ? savestring (flags
) : (char *)NULL
);
7901 a
= (v
&& array_p (v
)) ? array_cell (v
) : 0;
7902 h
= (v
&& assoc_p (v
)) ? assoc_cell (v
) : 0;
7904 list
= a
? array_to_word_list (a
) : (h
? assoc_to_word_list (h
) : 0);
7906 return ((char *)NULL
);
7907 ret
= list_transform (xc
, v
, list
, itype
, quoted
);
7908 dispose_words (list
);
7912 #endif /* ARRAY_VARS */
7915 valid_parameter_transform (xform
)
7921 /* check for valid values of xform[0] */
7924 case 'a': /* expand to a string with just attributes */
7925 case 'A': /* expand as an assignment statement with attributes */
7926 case 'K': /* expand assoc array to list of key/value pairs */
7927 case 'E': /* expand like $'...' */
7928 case 'P': /* expand like prompt string */
7929 case 'Q': /* quote reusably */
7930 case 'U': /* transform to uppercase */
7931 case 'u': /* tranform by capitalizing */
7932 case 'L': /* transform to lowercase */
7940 parameter_brace_transform (varname
, value
, ind
, xform
, rtype
, quoted
, pflags
, flags
)
7941 char *varname
, *value
;
7944 int rtype
, quoted
, pflags
, flags
;
7946 int vtype
, xc
, starsub
;
7947 char *temp1
, *val
, *oname
;
7951 if (value
== 0 && xc
!= 'A' && xc
!= 'a')
7952 return ((char *)NULL
);
7954 oname
= this_command_name
;
7955 this_command_name
= varname
;
7957 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
7960 this_command_name
= oname
;
7961 return ((char *)NULL
);
7964 if (valid_parameter_transform (xform
) == 0)
7966 this_command_name
= oname
;
7967 #if 0 /* TAG: bash-5.2 Martin Schulte <gnu@schrader-schulte.de> 10/2020 */
7968 return (interactive_shell
? &expand_param_error
: &expand_param_fatal
);
7970 return &expand_param_error
;
7974 starsub
= vtype
& VT_STARSUB
;
7975 vtype
&= ~VT_STARSUB
;
7977 /* If we are asked to display the attributes of an unset variable, V will
7978 be NULL after the call to get_var_and_type. Double-check here. */
7979 if ((xc
== 'a' || xc
== 'A') && vtype
== VT_VARIABLE
&& varname
&& v
== 0)
7980 v
= find_variable (varname
);
7982 temp1
= (char *)NULL
; /* shut up gcc */
7986 case VT_ARRAYMEMBER
:
7987 temp1
= string_transform (xc
, v
, val
);
7988 if (vtype
== VT_VARIABLE
)
7992 val
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
7993 ? quote_string (temp1
)
7994 : quote_escapes (temp1
);
7999 #if defined (ARRAY_VARS)
8001 temp1
= array_transform (xc
, v
, starsub
, quoted
);
8002 if (temp1
&& quoted
== 0 && ifs_is_null
)
8004 /* Posix interp 888 */
8006 else if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
8008 val
= quote_escapes (temp1
);
8015 temp1
= parameter_list_transform (xc
, varname
[0], quoted
);
8016 if (temp1
&& quoted
== 0 && ifs_is_null
)
8018 /* Posix interp 888 */
8020 else if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
8022 val
= quote_escapes (temp1
);
8029 this_command_name
= oname
;
8033 /******************************************************/
8035 /* Functions to extract substrings of variable values */
8037 /******************************************************/
8039 #if defined (HANDLE_MULTIBYTE)
8040 /* Character-oriented rather than strictly byte-oriented substrings. S and
8041 E, rather being strict indices into STRING, indicate character (possibly
8042 multibyte character) positions that require calculation.
8043 Used by the ${param:offset[:length]} expansion. */
8045 mb_substring (string
, s
, e
)
8055 /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
8056 slen
= (MB_CUR_MAX
> 1) ? STRLEN (string
) : 0;
8059 while (string
[start
] && i
--)
8060 ADVANCE_CHAR (string
, slen
, start
);
8063 while (string
[stop
] && i
--)
8064 ADVANCE_CHAR (string
, slen
, stop
);
8065 tt
= substring (string
, start
, stop
);
8070 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
8071 is `@', use the positional parameters; otherwise, use the value of
8072 VARNAME. If VARNAME is an array variable, use the array elements. */
8075 parameter_brace_substring (varname
, value
, ind
, substr
, quoted
, pflags
, flags
)
8076 char *varname
, *value
;
8079 int quoted
, pflags
, flags
;
8082 int vtype
, r
, starsub
;
8083 char *temp
, *val
, *tt
, *oname
;
8086 if (value
== 0 && ((varname
[0] != '@' && varname
[0] != '*') || varname
[1]))
8087 return ((char *)NULL
);
8089 oname
= this_command_name
;
8090 this_command_name
= varname
;
8092 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
8095 this_command_name
= oname
;
8096 return ((char *)NULL
);
8099 starsub
= vtype
& VT_STARSUB
;
8100 vtype
&= ~VT_STARSUB
;
8102 r
= verify_substring_values (v
, val
, substr
, vtype
, &e1
, &e2
);
8103 this_command_name
= oname
;
8106 if (vtype
== VT_VARIABLE
)
8108 return ((r
== 0) ? &expand_param_error
: (char *)NULL
);
8114 case VT_ARRAYMEMBER
:
8115 #if defined (HANDLE_MULTIBYTE)
8117 tt
= mb_substring (val
, e1
, e2
);
8120 tt
= substring (val
, e1
, e2
);
8122 if (vtype
== VT_VARIABLE
)
8124 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
8125 temp
= quote_string (tt
);
8127 temp
= tt
? quote_escapes (tt
) : (char *)NULL
;
8132 if (vtype
== VT_POSPARMS
)
8133 tt
= pos_params (varname
, e1
, e2
, quoted
, pflags
);
8134 #if defined (ARRAY_VARS)
8135 /* assoc_subrange and array_subrange both call string_list_pos_params,
8136 so we can treat this case just like VT_POSPARAMS. */
8137 else if (assoc_p (v
))
8138 /* we convert to list and take first e2 elements starting at e1th
8139 element -- officially undefined for now */
8140 tt
= assoc_subrange (assoc_cell (v
), e1
, e2
, starsub
, quoted
, pflags
);
8142 /* We want E2 to be the number of elements desired (arrays can be
8143 sparse, so verify_substring_values just returns the numbers
8144 specified and we rely on array_subrange to understand how to
8146 tt
= array_subrange (array_cell (v
), e1
, e2
, starsub
, quoted
, pflags
);
8148 /* We want to leave this alone in every case where pos_params/
8149 string_list_pos_params quotes the list members */
8150 if (tt
&& quoted
== 0 && ifs_is_null
)
8152 temp
= tt
; /* Posix interp 888 */
8154 else if (tt
&& quoted
== 0 && (pflags
& PF_ASSIGNRHS
))
8156 temp
= tt
; /* Posix interp 888 */
8158 else if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) == 0)
8160 temp
= tt
? quote_escapes (tt
) : (char *)NULL
;
8168 temp
= (char *)NULL
;
8174 /****************************************************************/
8176 /* Functions to perform pattern substitution on variable values */
8178 /****************************************************************/
8180 #ifdef INCLUDE_UNUSED
8182 shouldexp_replacement (s
)
8187 for (p
= s
; p
&& *p
; p
++)
8199 pat_subst (string
, pat
, rep
, mflags
)
8200 char *string
, *pat
, *rep
;
8203 char *ret
, *s
, *e
, *str
, *rstr
, *mstr
, *send
;
8204 int rptr
, mtype
, rxpand
, mlen
;
8205 size_t rsize
, l
, replen
, rslen
;
8209 return (savestring (""));
8211 mtype
= mflags
& MATCH_TYPEMASK
;
8213 #if 0 /* TAG: bash-5.2? */
8214 rxpand
= (rep
&& *rep
) ? shouldexp_replacement (rep
) : 0;
8220 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
8221 * with REP and return the result.
8222 * 2. A null pattern with mtype == MATCH_END means to append REP to
8223 * STRING and return the result.
8224 * 3. A null STRING with a matching pattern means to append REP to
8225 * STRING and return the result.
8226 * These don't understand or process `&' in the replacement string.
8228 if ((pat
== 0 || *pat
== 0) && (mtype
== MATCH_BEG
|| mtype
== MATCH_END
))
8230 replen
= STRLEN (rep
);
8231 l
= STRLEN (string
);
8232 ret
= (char *)xmalloc (replen
+ l
+ 2);
8234 strcpy (ret
, string
);
8235 else if (mtype
== MATCH_BEG
)
8238 strcpy (ret
+ replen
, string
);
8242 strcpy (ret
, string
);
8243 strcpy (ret
+ l
, rep
);
8247 else if (*string
== 0 && (match_pattern (string
, pat
, mtype
, &s
, &e
) != 0))
8249 replen
= STRLEN (rep
);
8250 ret
= (char *)xmalloc (replen
+ 1);
8258 ret
= (char *)xmalloc (rsize
= 64);
8260 send
= string
+ strlen (string
);
8262 for (replen
= STRLEN (rep
), rptr
= 0, str
= string
; *str
;)
8264 if (match_pattern (str
, pat
, mtype
, &s
, &e
) == 0)
8272 mstr
= xmalloc (mlen
+ 1);
8273 for (x
= 0; x
< mlen
; x
++)
8276 rstr
= strcreplace (rep
, '&', mstr
, 0);
8278 rslen
= strlen (rstr
);
8286 RESIZE_MALLOCED_BUFFER (ret
, rptr
, (l
+ rslen
), rsize
, 64);
8288 /* OK, now copy the leading unmatched portion of the string (from
8289 str to s) to ret starting at rptr (the current offset). Then copy
8290 the replacement string at ret + rptr + (s - str). Increment
8291 rptr (if necessary) and str and go on. */
8294 strncpy (ret
+ rptr
, str
, l
);
8299 strncpy (ret
+ rptr
, rstr
, rslen
);
8302 str
= e
; /* e == end of match */
8307 if (((mflags
& MATCH_GLOBREP
) == 0) || mtype
!= MATCH_ANY
)
8312 /* On a zero-length match, make sure we copy one character, since
8313 we increment one character to avoid infinite recursion. */
8314 char *p
, *origp
, *origs
;
8317 RESIZE_MALLOCED_BUFFER (ret
, rptr
, locale_mb_cur_max
, rsize
, 64);
8318 #if defined (HANDLE_MULTIBYTE)
8319 p
= origp
= ret
+ rptr
;
8321 COPY_CHAR_P (p
, str
, send
);
8325 ret
[rptr
++] = *str
++;
8326 e
++; /* avoid infinite recursion on zero-length match */
8331 /* Now copy the unmatched portion of the input string */
8334 RESIZE_MALLOCED_BUFFER (ret
, rptr
, STRLEN(str
) + 1, rsize
, 64);
8335 strcpy (ret
+ rptr
, str
);
8343 /* Do pattern match and replacement on the positional parameters. */
8345 pos_params_pat_subst (string
, pat
, rep
, mflags
)
8346 char *string
, *pat
, *rep
;
8349 WORD_LIST
*save
, *params
;
8352 int pchar
, qflags
, pflags
;
8354 save
= params
= list_rest_of_args ();
8356 return ((char *)NULL
);
8358 for ( ; params
; params
= params
->next
)
8360 ret
= pat_subst (params
->word
->word
, pat
, rep
, mflags
);
8361 w
= alloc_word_desc ();
8362 w
->word
= ret
? ret
: savestring ("");
8363 dispose_word (params
->word
);
8367 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
8368 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
8369 pflags
= (mflags
& MATCH_ASSIGNRHS
) == MATCH_ASSIGNRHS
? PF_ASSIGNRHS
: 0;
8371 /* If we are expanding in a context where word splitting will not be
8372 performed, treat as quoted. This changes how $* will be expanded. */
8373 if (pchar
== '*' && (mflags
& MATCH_ASSIGNRHS
) && expand_no_split_dollar_star
&& ifs_is_null
)
8374 qflags
|= Q_DOUBLE_QUOTES
; /* Posix interp 888 */
8376 ret
= string_list_pos_params (pchar
, save
, qflags
, pflags
);
8377 dispose_words (save
);
8382 /* Perform pattern substitution on VALUE, which is the expansion of
8383 VARNAME. PATSUB is an expression supplying the pattern to match
8384 and the string to substitute. QUOTED is a flags word containing
8385 the type of quoting currently in effect. */
8387 parameter_brace_patsub (varname
, value
, ind
, patsub
, quoted
, pflags
, flags
)
8388 char *varname
, *value
;
8391 int quoted
, pflags
, flags
;
8393 int vtype
, mflags
, starsub
, delim
;
8394 char *val
, *temp
, *pat
, *rep
, *p
, *lpatsub
, *tt
, *oname
;
8398 return ((char *)NULL
);
8400 oname
= this_command_name
;
8401 this_command_name
= varname
; /* error messages */
8403 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
8406 this_command_name
= oname
;
8407 return ((char *)NULL
);
8410 starsub
= vtype
& VT_STARSUB
;
8411 vtype
&= ~VT_STARSUB
;
8414 /* PATSUB is never NULL when this is called. */
8417 mflags
|= MATCH_GLOBREP
;
8421 /* Malloc this because expand_string_if_necessary or one of the expansion
8422 functions in its call chain may free it on a substitution error. */
8423 lpatsub
= savestring (patsub
);
8425 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
8426 mflags
|= MATCH_QUOTED
;
8429 mflags
|= MATCH_STARSUB
;
8431 if (pflags
& PF_ASSIGNRHS
)
8432 mflags
|= MATCH_ASSIGNRHS
;
8434 /* If the pattern starts with a `/', make sure we skip over it when looking
8435 for the replacement delimiter. */
8436 delim
= skip_to_delim (lpatsub
, ((*patsub
== '/') ? 1 : 0), "/", 0);
8437 if (lpatsub
[delim
] == '/')
8440 rep
= lpatsub
+ delim
+ 1;
8445 if (rep
&& *rep
== '\0')
8448 /* Perform the same expansions on the pattern as performed by the
8449 pattern removal expansions. */
8450 pat
= getpattern (lpatsub
, quoted
, 1);
8454 /* We want to perform quote removal on the expanded replacement even if
8455 the entire expansion is double-quoted because the parser and string
8456 extraction functions treated quotes in the replacement string as
8457 special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */
8458 if (shell_compatibility_level
> 42)
8459 rep
= expand_string_if_necessary (rep
, quoted
& ~(Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
), expand_string_unsplit
);
8460 /* This is the bash-4.2 code. */
8461 else if ((mflags
& MATCH_QUOTED
) == 0)
8462 rep
= expand_string_if_necessary (rep
, quoted
, expand_string_unsplit
);
8464 rep
= expand_string_to_string_internal (rep
, quoted
, expand_string_unsplit
);
8467 /* ksh93 doesn't allow the match specifier to be a part of the expanded
8468 pattern. This is an extension. Make sure we don't anchor the pattern
8469 at the beginning or end of the string if we're doing global replacement,
8472 if (mflags
& MATCH_GLOBREP
)
8473 mflags
|= MATCH_ANY
;
8474 else if (pat
&& pat
[0] == '#')
8476 mflags
|= MATCH_BEG
;
8479 else if (pat
&& pat
[0] == '%')
8481 mflags
|= MATCH_END
;
8485 mflags
|= MATCH_ANY
;
8487 /* OK, we now want to substitute REP for PAT in VAL. If
8488 flags & MATCH_GLOBREP is non-zero, the substitution is done
8489 everywhere, otherwise only the first occurrence of PAT is
8490 replaced. The pattern matching code doesn't understand
8491 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
8492 values passed in (VT_VARIABLE) so the pattern substitution
8493 code works right. We need to requote special chars after
8494 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
8495 other cases if QUOTED == 0, since the posparams and arrays
8496 indexed by * or @ do special things when QUOTED != 0. */
8501 case VT_ARRAYMEMBER
:
8502 temp
= pat_subst (val
, p
, rep
, mflags
);
8503 if (vtype
== VT_VARIABLE
)
8507 tt
= (mflags
& MATCH_QUOTED
) ? quote_string (temp
) : quote_escapes (temp
);
8513 /* This does the right thing for the case where we are not performing
8514 word splitting. MATCH_STARSUB restricts it to ${* /foo/bar}, and
8515 pos_params_pat_subst/string_list_pos_params will do the right thing
8516 in turn for the case where ifs_is_null. Posix interp 888 */
8517 if ((pflags
& PF_NOSPLIT2
) && (mflags
& MATCH_STARSUB
))
8518 mflags
|= MATCH_ASSIGNRHS
;
8519 temp
= pos_params_pat_subst (val
, p
, rep
, mflags
);
8520 if (temp
&& quoted
== 0 && ifs_is_null
)
8522 /* Posix interp 888 */
8524 else if (temp
&& quoted
== 0 && (pflags
& PF_ASSIGNRHS
))
8526 /* Posix interp 888 */
8528 else if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
8530 tt
= quote_escapes (temp
);
8535 #if defined (ARRAY_VARS)
8537 /* If we are expanding in a context where word splitting will not be
8538 performed, treat as quoted. This changes how ${A[*]} will be
8539 expanded to make it identical to $*. */
8540 if ((mflags
& MATCH_STARSUB
) && (mflags
& MATCH_ASSIGNRHS
) && ifs_is_null
)
8541 mflags
|= MATCH_QUOTED
; /* Posix interp 888 */
8543 /* these eventually call string_list_pos_params */
8545 temp
= assoc_patsub (assoc_cell (v
), p
, rep
, mflags
);
8547 temp
= array_patsub (array_cell (v
), p
, rep
, mflags
);
8549 if (temp
&& quoted
== 0 && ifs_is_null
)
8551 /* Posix interp 888 */
8553 else if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
8555 tt
= quote_escapes (temp
);
8567 this_command_name
= oname
;
8572 /****************************************************************/
8574 /* Functions to perform case modification on variable values */
8576 /****************************************************************/
8578 /* Do case modification on the positional parameters. */
8581 pos_params_modcase (string
, pat
, modop
, mflags
)
8586 WORD_LIST
*save
, *params
;
8589 int pchar
, qflags
, pflags
;
8591 save
= params
= list_rest_of_args ();
8593 return ((char *)NULL
);
8595 for ( ; params
; params
= params
->next
)
8597 ret
= sh_modcase (params
->word
->word
, pat
, modop
);
8598 w
= alloc_word_desc ();
8599 w
->word
= ret
? ret
: savestring ("");
8600 dispose_word (params
->word
);
8604 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
8605 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
8606 pflags
= (mflags
& MATCH_ASSIGNRHS
) == MATCH_ASSIGNRHS
? PF_ASSIGNRHS
: 0;
8608 /* If we are expanding in a context where word splitting will not be
8609 performed, treat as quoted. This changes how $* will be expanded. */
8610 if (pchar
== '*' && (mflags
& MATCH_ASSIGNRHS
) && ifs_is_null
)
8611 qflags
|= Q_DOUBLE_QUOTES
; /* Posix interp 888 */
8613 ret
= string_list_pos_params (pchar
, save
, qflags
, pflags
);
8614 dispose_words (save
);
8619 /* Perform case modification on VALUE, which is the expansion of
8620 VARNAME. MODSPEC is an expression supplying the type of modification
8621 to perform. QUOTED is a flags word containing the type of quoting
8622 currently in effect. */
8624 parameter_brace_casemod (varname
, value
, ind
, modspec
, patspec
, quoted
, pflags
, flags
)
8625 char *varname
, *value
;
8628 int quoted
, pflags
, flags
;
8630 int vtype
, starsub
, modop
, mflags
, x
;
8631 char *val
, *temp
, *pat
, *p
, *lpat
, *tt
, *oname
;
8635 return ((char *)NULL
);
8637 oname
= this_command_name
;
8638 this_command_name
= varname
;
8640 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
8643 this_command_name
= oname
;
8644 return ((char *)NULL
);
8647 starsub
= vtype
& VT_STARSUB
;
8648 vtype
&= ~VT_STARSUB
;
8652 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
8653 mflags
|= MATCH_QUOTED
;
8655 mflags
|= MATCH_STARSUB
;
8656 if (pflags
& PF_ASSIGNRHS
)
8657 mflags
|= MATCH_ASSIGNRHS
;
8662 x
= p
&& p
[0] == modspec
;
8663 modop
= x
? CASE_UPPER
: CASE_UPFIRST
;
8666 else if (modspec
== ',')
8668 x
= p
&& p
[0] == modspec
;
8669 modop
= x
? CASE_LOWER
: CASE_LOWFIRST
;
8672 else if (modspec
== '~')
8674 x
= p
&& p
[0] == modspec
;
8675 modop
= x
? CASE_TOGGLEALL
: CASE_TOGGLE
;
8679 lpat
= p
? savestring (p
) : 0;
8680 /* Perform the same expansions on the pattern as performed by the
8681 pattern removal expansions. */
8682 pat
= lpat
? getpattern (lpat
, quoted
, 1) : 0;
8684 /* OK, now we do the case modification. */
8688 case VT_ARRAYMEMBER
:
8689 temp
= sh_modcase (val
, pat
, modop
);
8690 if (vtype
== VT_VARIABLE
)
8694 tt
= (mflags
& MATCH_QUOTED
) ? quote_string (temp
) : quote_escapes (temp
);
8701 temp
= pos_params_modcase (val
, pat
, modop
, mflags
);
8702 if (temp
&& quoted
== 0 && ifs_is_null
)
8704 /* Posix interp 888 */
8706 else if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
8708 tt
= quote_escapes (temp
);
8714 #if defined (ARRAY_VARS)
8716 /* If we are expanding in a context where word splitting will not be
8717 performed, treat as quoted. This changes how ${A[*]} will be
8718 expanded to make it identical to $*. */
8719 if ((mflags
& MATCH_STARSUB
) && (mflags
& MATCH_ASSIGNRHS
) && ifs_is_null
)
8720 mflags
|= MATCH_QUOTED
; /* Posix interp 888 */
8722 temp
= assoc_p (v
) ? assoc_modcase (assoc_cell (v
), pat
, modop
, mflags
)
8723 : array_modcase (array_cell (v
), pat
, modop
, mflags
);
8725 if (temp
&& quoted
== 0 && ifs_is_null
)
8727 /* Posix interp 888 */
8729 else if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
8731 tt
= quote_escapes (temp
);
8743 this_command_name
= oname
;
8748 /* Check for unbalanced parens in S, which is the contents of $(( ... )). If
8749 any occur, this must be a nested command substitution, so return 0.
8750 Otherwise, return 1. A valid arithmetic expression must always have a
8751 ( before a matching ), so any cases where there are more right parens
8752 means that this must not be an arithmetic expression, though the parser
8753 will not accept it without a balanced total number of parens. */
8755 chk_arithsub (s
, len
)
8767 else if (s
[i
] == RPAREN
)
8777 ADVANCE_CHAR (s
, len
, i
);
8783 ADVANCE_CHAR (s
, len
, i
);
8787 i
= skip_single_quoted (s
, len
, ++i
, 0);
8791 i
= skip_double_quoted ((char *)s
, len
, ++i
, 0);
8796 return (count
== 0);
8799 /****************************************************************/
8801 /* Functions to perform parameter expansion on a string */
8803 /****************************************************************/
8805 /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
8807 parameter_brace_expand (string
, indexp
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
)
8809 int *indexp
, quoted
, pflags
, *quoted_dollar_atp
, *contains_dollar_at
;
8811 int check_nullness
, var_is_set
, var_is_null
, var_is_special
;
8812 int want_substring
, want_indir
, want_patsub
, want_casemod
, want_attributes
;
8813 char *name
, *value
, *temp
, *temp1
;
8814 WORD_DESC
*tdesc
, *ret
;
8815 int t_index
, sindex
, c
, tflag
, modspec
, local_pflags
, all_element_arrayref
;
8819 temp
= temp1
= value
= (char *)NULL
;
8820 var_is_set
= var_is_null
= var_is_special
= check_nullness
= 0;
8821 want_substring
= want_indir
= want_patsub
= want_casemod
= want_attributes
= 0;
8824 all_element_arrayref
= 0;
8828 /* ${#var} doesn't have any of the other parameter expansions on it. */
8829 if (string
[t_index
] == '#' && legal_variable_starter (string
[t_index
+1])) /* {{ */
8830 name
= string_extract (string
, &t_index
, "}", SX_VARNAME
);
8832 #if defined (CASEMOD_EXPANSIONS)
8833 /* To enable case-toggling expansions using the `~' operator character
8834 define CASEMOD_TOGGLECASE in config-top.h */
8835 # if defined (CASEMOD_TOGGLECASE)
8836 name
= string_extract (string
, &t_index
, "#%^,~:-=?+/@}", SX_VARNAME
);
8838 name
= string_extract (string
, &t_index
, "#%^,:-=?+/@}", SX_VARNAME
);
8839 # endif /* CASEMOD_TOGGLECASE */
8841 name
= string_extract (string
, &t_index
, "#%:-=?+/@}", SX_VARNAME
);
8842 #endif /* CASEMOD_EXPANSIONS */
8844 /* Handle ${@[stuff]} now that @ is a word expansion operator. Not exactly
8845 the cleanest code ever. */
8846 if (*name
== 0 && sindex
== t_index
&& string
[sindex
] == '@')
8848 name
= (char *)xrealloc (name
, 2);
8853 else if (*name
== '!' && t_index
> sindex
&& string
[t_index
] == '@' && string
[t_index
+1] == RBRACE
)
8855 name
= (char *)xrealloc (name
, t_index
- sindex
+ 2);
8856 name
[t_index
- sindex
] = '@';
8857 name
[t_index
- sindex
+ 1] = '\0';
8866 /* If the name really consists of a special variable, then make sure
8867 that we have the entire name. We don't allow indirect references
8868 to special variables except `#', `?', `@' and `*'. This clause is
8869 designed to handle ${#SPECIAL} and ${!SPECIAL}, not anything more
8871 if ((sindex
== t_index
&& VALID_SPECIAL_LENGTH_PARAM (string
[t_index
])) ||
8872 (sindex
== t_index
&& string
[sindex
] == '#' && VALID_SPECIAL_LENGTH_PARAM (string
[sindex
+ 1])) ||
8873 (sindex
== t_index
- 1 && string
[sindex
] == '!' && VALID_INDIR_PARAM (string
[t_index
])))
8876 temp1
= string_extract (string
, &t_index
, "#%:-=?+/@}", 0);
8877 name
= (char *)xrealloc (name
, 3 + (strlen (temp1
)));
8878 *name
= string
[sindex
];
8879 if (string
[sindex
] == '!')
8881 /* indirect reference of $#, $?, $@, or $* */
8882 name
[1] = string
[sindex
+ 1];
8883 strcpy (name
+ 2, temp1
);
8886 strcpy (name
+ 1, temp1
);
8891 /* Find out what character ended the variable name. Then
8892 do the appropriate thing. */
8893 if (c
= string
[sindex
])
8896 /* If c is followed by one of the valid parameter expansion
8897 characters, move past it as normal. If not, assume that
8898 a substring specification is being given, and do not move
8900 if (c
== ':' && VALID_PARAM_EXPAND_CHAR (string
[sindex
]))
8903 if (c
= string
[sindex
])
8906 else if (c
== ':' && string
[sindex
] != RBRACE
)
8908 else if (c
== '/' /* && string[sindex] != RBRACE */) /* XXX */
8910 #if defined (CASEMOD_EXPANSIONS)
8911 else if (c
== '^' || c
== ',' || c
== '~')
8917 else if (c
== '@' && (string
[sindex
] == 'a' || string
[sindex
] == 'A') && string
[sindex
+1] == RBRACE
)
8919 /* special case because we do not want to shortcut foo as foo[0] here */
8920 want_attributes
= 1;
8921 local_pflags
|= PF_ALLINDS
;
8924 /* Catch the valid and invalid brace expressions that made it through the
8926 /* ${#-} is a valid expansion and means to take the length of $-.
8927 Similarly for ${#?} and ${##}... */
8928 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
8929 VALID_SPECIAL_LENGTH_PARAM (c
) && string
[sindex
] == RBRACE
)
8931 name
= (char *)xrealloc (name
, 3);
8934 c
= string
[sindex
++];
8937 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
8938 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
8939 member (c
, "%:=+/") && string
[sindex
] == RBRACE
)
8941 temp
= (char *)NULL
;
8942 goto bad_substitution
; /* XXX - substitution error */
8945 /* Indirect expansion begins with a `!'. A valid indirect expansion is
8946 either a variable name, one of the positional parameters or a special
8947 variable that expands to one of the positional parameters. */
8948 want_indir
= *name
== '!' &&
8949 (legal_variable_starter ((unsigned char)name
[1]) || DIGIT (name
[1])
8950 || VALID_INDIR_PARAM (name
[1]));
8952 /* Determine the value of this variable whose name is NAME. */
8954 /* Check for special variables, directly referenced. */
8955 if (SPECIAL_VAR (name
, want_indir
))
8958 /* Check for special expansion things, like the length of a parameter */
8959 if (*name
== '#' && name
[1])
8961 /* If we are not pointing at the character just after the
8962 closing brace, then we haven't gotten all of the name.
8963 Since it begins with a special character, this is a bad
8964 substitution. Also check NAME for validity before trying
8966 if (string
[sindex
- 1] != RBRACE
|| (valid_length_expression (name
) == 0))
8968 temp
= (char *)NULL
;
8969 goto bad_substitution
; /* substitution error */
8972 number
= parameter_brace_expand_length (name
);
8973 if (number
== INTMAX_MIN
&& unbound_vars_is_error
)
8975 set_exit_status (EXECUTION_FAILURE
);
8976 err_unboundvar (name
+1);
8978 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8984 return (&expand_wdesc_error
);
8987 ret
= alloc_word_desc ();
8988 ret
->word
= itos (number
);
8993 /* ${@} is identical to $@. */
8994 if (name
[0] == '@' && name
[1] == '\0')
8996 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
8997 *quoted_dollar_atp
= 1;
8999 if (contains_dollar_at
)
9000 *contains_dollar_at
= 1;
9002 tflag
|= W_DOLLARAT
;
9005 /* Process ${!PREFIX*} expansion. */
9006 if (want_indir
&& string
[sindex
- 1] == RBRACE
&&
9007 (string
[sindex
- 2] == '*' || string
[sindex
- 2] == '@') &&
9008 legal_variable_starter ((unsigned char) name
[1]))
9013 temp1
= savestring (name
+ 1);
9014 number
= strlen (temp1
);
9015 temp1
[number
- 1] = '\0';
9016 x
= all_variables_matching_prefix (temp1
);
9017 xlist
= strvec_to_word_list (x
, 0, 0);
9018 if (string
[sindex
- 2] == '*')
9019 temp
= string_list_dollar_star (xlist
, quoted
, 0);
9022 temp
= string_list_dollar_at (xlist
, quoted
, 0);
9023 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
9024 *quoted_dollar_atp
= 1;
9025 if (contains_dollar_at
)
9026 *contains_dollar_at
= 1;
9028 tflag
|= W_DOLLARAT
;
9031 dispose_words (xlist
);
9037 ret
= alloc_word_desc ();
9039 ret
->flags
= tflag
; /* XXX */
9043 #if defined (ARRAY_VARS)
9044 /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */
9045 if (want_indir
&& string
[sindex
- 1] == RBRACE
&&
9046 string
[sindex
- 2] == RBRACK
&& valid_array_reference (name
+1, 0))
9050 temp1
= savestring (name
+ 1);
9051 x
= array_variable_name (temp1
, 0, &x1
, (int *)0);
9053 if (ALL_ELEMENT_SUB (x1
[0]) && x1
[1] == RBRACK
)
9055 temp
= array_keys (temp1
, quoted
, pflags
); /* handles assoc vars too */
9058 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
9059 *quoted_dollar_atp
= 1;
9060 if (contains_dollar_at
)
9061 *contains_dollar_at
= 1;
9063 tflag
|= W_DOLLARAT
;
9070 ret
= alloc_word_desc ();
9072 ret
->flags
= tflag
; /* XXX */
9078 #endif /* ARRAY_VARS */
9080 /* Make sure that NAME is valid before trying to go on. */
9081 if (valid_brace_expansion_word (want_indir
? name
+ 1 : name
,
9082 var_is_special
) == 0)
9084 temp
= (char *)NULL
;
9085 goto bad_substitution
; /* substitution error */
9090 tdesc
= parameter_brace_expand_indir (name
+ 1, var_is_special
, quoted
, pflags
|local_pflags
, quoted_dollar_atp
, contains_dollar_at
);
9091 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
9093 temp
= (char *)NULL
;
9094 goto bad_substitution
;
9097 /* Turn off the W_ARRAYIND flag because there is no way for this function
9098 to return the index we're supposed to be using. */
9099 if (tdesc
&& tdesc
->flags
)
9100 tdesc
->flags
&= ~W_ARRAYIND
;
9104 local_pflags
|= PF_IGNUNBOUND
|(pflags
&(PF_NOSPLIT2
|PF_ASSIGNRHS
));
9105 tdesc
= parameter_brace_expand_word (name
, var_is_special
, quoted
, local_pflags
, &ind
);
9108 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
9117 tflag
= tdesc
->flags
;
9118 dispose_word_desc (tdesc
);
9123 if (temp
== &expand_param_error
|| temp
== &expand_param_fatal
)
9127 return (temp
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9130 #if defined (ARRAY_VARS)
9131 if (valid_array_reference (name
, 0))
9137 /* If in a context where word splitting will not take place, treat as
9138 if double-quoted. Has effects with $* and ${array[*]} */
9140 if (pflags
& PF_ASSIGNRHS
)
9141 qflags
|= Q_DOUBLE_QUOTES
;
9142 /* We duplicate a little code here */
9143 t
= mbschr (name
, LBRACK
);
9144 if (t
&& ALL_ELEMENT_SUB (t
[1]) && t
[2] == RBRACK
)
9146 all_element_arrayref
= 1;
9147 if (expand_no_split_dollar_star
&& t
[1] == '*') /* XXX */
9148 qflags
|= Q_DOUBLE_QUOTES
;
9150 chk_atstar (name
, qflags
, pflags
, quoted_dollar_atp
, contains_dollar_at
);
9154 var_is_set
= temp
!= (char *)0;
9155 var_is_null
= check_nullness
&& (var_is_set
== 0 || *temp
== 0);
9156 /* XXX - this may not need to be restricted to special variables */
9158 var_is_null
|= var_is_set
&& var_is_special
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && QUOTED_NULL (temp
);
9159 #if defined (ARRAY_VARS)
9161 var_is_null
|= var_is_set
&&
9162 (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) &&
9163 QUOTED_NULL (temp
) &&
9164 valid_array_reference (name
, 0) &&
9165 chk_atstar (name
, 0, 0, (int *)0, (int *)0);
9168 /* Get the rest of the stuff inside the braces. */
9169 if (c
&& c
!= RBRACE
)
9171 /* Extract the contents of the ${ ... } expansion
9172 according to the Posix.2 rules. */
9173 value
= extract_dollar_brace_string (string
, &sindex
, quoted
, (c
== '%' || c
== '#' || c
=='/' || c
== '^' || c
== ',' || c
==':') ? SX_POSIXEXP
|SX_WORD
: SX_WORD
);
9174 if (string
[sindex
] == RBRACE
)
9177 goto bad_substitution
; /* substitution error */
9180 value
= (char *)NULL
;
9184 /* All the cases where an expansion can possibly generate an unbound
9186 if (want_substring
|| want_patsub
|| want_casemod
|| c
== '@' || c
== '#' || c
== '%' || c
== RBRACE
)
9188 if (var_is_set
== 0 && unbound_vars_is_error
&& ((name
[0] != '@' && name
[0] != '*') || name
[1]) && all_element_arrayref
== 0)
9190 set_exit_status (EXECUTION_FAILURE
);
9191 err_unboundvar (name
);
9195 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9199 /* If this is a substring spec, process it and add the result. */
9202 temp1
= parameter_brace_substring (name
, temp
, ind
, value
, quoted
, pflags
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9206 if (temp1
== &expand_param_error
|| temp1
== &expand_param_fatal
)
9209 return (temp1
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9212 ret
= alloc_word_desc ();
9214 /* We test quoted_dollar_atp because we want variants with double-quoted
9215 "$@" to take a different code path. In fact, we make sure at the end
9216 of expand_word_internal that we're only looking at these flags if
9217 quoted_dollar_at == 0. */
9219 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
9220 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9221 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
9222 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 &&
9223 (pflags
& PF_ASSIGNRHS
))
9224 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9225 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9226 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 && ifs_is_null
)
9227 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9232 else if (want_patsub
)
9234 temp1
= parameter_brace_patsub (name
, temp
, ind
, value
, quoted
, pflags
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9238 if (temp1
== &expand_param_error
|| temp1
== &expand_param_fatal
)
9241 return (temp1
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9244 ret
= alloc_word_desc ();
9247 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
9248 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9249 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
9250 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9251 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 && ifs_is_null
)
9252 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9257 #if defined (CASEMOD_EXPANSIONS)
9258 else if (want_casemod
)
9260 temp1
= parameter_brace_casemod (name
, temp
, ind
, modspec
, value
, quoted
, pflags
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9264 if (temp1
== &expand_param_error
|| temp1
== &expand_param_fatal
)
9267 return (temp1
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9270 ret
= alloc_word_desc ();
9273 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
9274 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9275 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
9276 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9277 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 && ifs_is_null
)
9278 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9285 /* Do the right thing based on which character ended the variable name. */
9291 set_exit_status (EXECUTION_FAILURE
);
9292 report_error (_("%s: bad substitution"), string
? string
: "??");
9296 if (shell_compatibility_level
<= 43)
9297 return &expand_wdesc_error
;
9299 return ((posixly_correct
&& interactive_shell
== 0) ? &expand_wdesc_fatal
: &expand_wdesc_error
);
9305 temp1
= parameter_brace_transform (name
, temp
, ind
, value
, c
, quoted
, pflags
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9309 if (temp1
== &expand_param_error
|| temp1
== &expand_param_fatal
)
9312 set_exit_status (EXECUTION_FAILURE
);
9313 report_error (_("%s: bad substitution"), string
? string
: "??");
9314 return (temp1
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9317 ret
= alloc_word_desc ();
9319 if (temp1
&& QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9320 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
9321 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9322 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 && ifs_is_null
)
9323 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9328 case '#': /* ${param#[#]pattern} */
9329 case '%': /* ${param%[%]pattern} */
9330 if (value
== 0 || *value
== '\0' || temp
== 0 || *temp
== '\0')
9335 temp1
= parameter_brace_remove_pattern (name
, temp
, ind
, value
, c
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9339 ret
= alloc_word_desc ();
9341 if (temp1
&& QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9342 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
9343 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9344 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 && ifs_is_null
)
9345 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9354 if (var_is_set
&& var_is_null
== 0)
9356 /* If the operator is `+', we don't want the value of the named
9357 variable for anything, just the value of the right hand side. */
9360 /* XXX -- if we're double-quoted and the named variable is "$@",
9361 we want to turn off any special handling of "$@" --
9362 we're not using it, so whatever is on the rhs applies. */
9363 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
9364 *quoted_dollar_atp
= 0;
9365 if (contains_dollar_at
)
9366 *contains_dollar_at
= 0;
9371 /* From Posix discussion on austin-group list. Issue 221
9372 requires that backslashes escaping `}' inside
9373 double-quoted ${...} be removed. */
9374 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
9375 quoted
|= Q_DOLBRACE
;
9376 ret
= parameter_brace_expand_rhs (name
, value
, c
,
9380 contains_dollar_at
);
9381 /* XXX - fix up later, esp. noting presence of
9382 W_HASQUOTEDNULL in ret->flags */
9386 temp
= (char *)NULL
;
9392 /* Otherwise do nothing; just use the value in TEMP. */
9394 else /* VAR not set or VAR is NULL. */
9397 temp
= (char *)NULL
;
9398 if (c
== '=' && var_is_special
)
9400 set_exit_status (EXECUTION_FAILURE
);
9401 report_error (_("$%s: cannot assign in this way"), name
);
9404 return &expand_wdesc_error
;
9408 parameter_brace_expand_error (name
, value
, check_nullness
);
9409 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9413 /* XXX -- if we're double-quoted and the named variable is "$@",
9414 we want to turn off any special handling of "$@" --
9415 we're not using it, so whatever is on the rhs applies. */
9416 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
9417 *quoted_dollar_atp
= 0;
9418 if (contains_dollar_at
)
9419 *contains_dollar_at
= 0;
9421 /* From Posix discussion on austin-group list. Issue 221 requires
9422 that backslashes escaping `}' inside double-quoted ${...} be
9424 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
9425 quoted
|= Q_DOLBRACE
;
9426 ret
= parameter_brace_expand_rhs (name
, value
, c
, quoted
, pflags
,
9428 contains_dollar_at
);
9429 /* XXX - fix up later, esp. noting presence of
9430 W_HASQUOTEDNULL in tdesc->flags */
9441 ret
= alloc_word_desc ();
9448 /* Expand a single ${xxx} expansion. The braces are optional. When
9449 the braces are used, parameter_brace_expand() does the work,
9450 possibly calling param_expand recursively. */
9452 param_expand (string
, sindex
, quoted
, expanded_something
,
9453 contains_dollar_at
, quoted_dollar_at_p
, had_quoted_null_p
,
9456 int *sindex
, quoted
, *expanded_something
, *contains_dollar_at
;
9457 int *quoted_dollar_at_p
, *had_quoted_null_p
, pflags
;
9459 char *temp
, *temp1
, uerror
[3], *savecmd
;
9460 int zindex
, t_index
, expok
;
9464 WORD_LIST
*list
, *l
;
9465 WORD_DESC
*tdesc
, *ret
;
9468 /*itrace("param_expand: `%s' pflags = %d", string+*sindex, pflags);*/
9470 c
= string
[++zindex
];
9472 temp
= (char *)NULL
;
9473 ret
= tdesc
= (WORD_DESC
*)NULL
;
9476 /* Do simple cases first. Switch on what follows '$'. */
9490 temp1
= dollar_vars
[TODIGIT (c
)];
9491 /* This doesn't get called when (pflags&PF_IGNUNBOUND) != 0 */
9492 if (unbound_vars_is_error
&& temp1
== (char *)NULL
)
9497 set_exit_status (EXECUTION_FAILURE
);
9498 err_unboundvar (uerror
);
9499 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9502 temp
= (*temp1
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9503 ? quote_string (temp1
)
9504 : quote_escapes (temp1
);
9506 temp
= (char *)NULL
;
9510 /* $$ -- pid of the invoking shell. */
9512 temp
= itos (dollar_dollar_pid
);
9515 /* $# -- number of positional parameters. */
9517 temp
= itos (number_of_args ());
9520 /* $? -- return value of the last synchronous command. */
9522 temp
= itos (last_command_exit_value
);
9525 /* $- -- flags supplied to the shell on invocation or by `set'. */
9527 temp
= which_set_flags ();
9530 /* $! -- Pid of the last asynchronous command. */
9532 /* If no asynchronous pids have been created, expand to nothing.
9533 If `set -u' has been executed, and no async processes have
9534 been created, this is an expansion error. */
9535 if (last_asynchronous_pid
== NO_PID
)
9537 if (expanded_something
)
9538 *expanded_something
= 0;
9539 temp
= (char *)NULL
;
9540 if (unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
9545 set_exit_status (EXECUTION_FAILURE
);
9546 err_unboundvar (uerror
);
9547 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9551 temp
= itos (last_asynchronous_pid
);
9554 /* The only difference between this and $@ is when the arg is quoted. */
9555 case '*': /* `$*' */
9556 list
= list_rest_of_args ();
9559 /* According to austin-group posix proposal by Geoff Clare in
9560 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
9562 "The shell shall write a message to standard error and
9563 immediately exit when it tries to expand an unset parameter
9564 other than the '@' and '*' special parameters."
9567 if (list
== 0 && unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
9572 set_exit_status (EXECUTION_FAILURE
);
9573 err_unboundvar (uerror
);
9574 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9578 /* If there are no command-line arguments, this should just
9579 disappear if there are other characters in the expansion,
9580 even if it's quoted. */
9581 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && list
== 0)
9582 temp
= (char *)NULL
;
9583 else if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
|Q_PATQUOTE
))
9585 /* If we have "$*" we want to make a string of the positional
9586 parameters, separated by the first character of $IFS, and
9587 quote the whole string, including the separators. If IFS
9588 is unset, the parameters are separated by ' '; if $IFS is
9589 null, the parameters are concatenated. */
9590 temp
= (quoted
& (Q_DOUBLE_QUOTES
|Q_PATQUOTE
)) ? string_list_dollar_star (list
, quoted
, 0) : string_list (list
);
9593 temp1
= (quoted
& Q_DOUBLE_QUOTES
) ? quote_string (temp
) : temp
;
9595 tflag
|= W_HASQUOTEDNULL
;
9603 /* We check whether or not we're eventually going to split $* here,
9604 for example when IFS is empty and we are processing the rhs of
9605 an assignment statement. In that case, we don't separate the
9606 arguments at all. Otherwise, if the $* is not quoted it is
9608 if (expand_no_split_dollar_star
&& quoted
== 0 && ifs_is_set
== 0 && (pflags
& PF_ASSIGNRHS
))
9610 /* Posix interp 888: RHS of assignment, IFS unset: no splitting,
9611 separate with space */
9612 temp1
= string_list_dollar_star (list
, quoted
, pflags
);
9613 temp
= temp1
? quote_string (temp1
) : temp1
;
9614 /* XXX - tentative - note that we saw a quoted null here */
9615 if (temp1
&& *temp1
== 0 && QUOTED_NULL (temp
))
9616 tflag
|= W_SAWQUOTEDNULL
;
9619 else if (expand_no_split_dollar_star
&& quoted
== 0 && ifs_is_null
&& (pflags
& PF_ASSIGNRHS
))
9621 /* Posix interp 888: RHS of assignment, IFS set to '' */
9622 temp1
= string_list_dollar_star (list
, quoted
, pflags
);
9623 temp
= temp1
? quote_escapes (temp1
) : temp1
;
9626 else if (expand_no_split_dollar_star
&& quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && (pflags
& PF_ASSIGNRHS
))
9628 /* Posix interp 888: RHS of assignment, IFS set to non-null value */
9629 temp1
= string_list_dollar_star (list
, quoted
, pflags
);
9630 temp
= temp1
? quote_string (temp1
) : temp1
;
9632 /* XXX - tentative - note that we saw a quoted null here */
9633 if (temp1
&& *temp1
== 0 && QUOTED_NULL (temp
))
9634 tflag
|= W_SAWQUOTEDNULL
;
9637 /* XXX - should we check ifs_is_set here as well? */
9638 # if defined (HANDLE_MULTIBYTE)
9639 else if (expand_no_split_dollar_star
&& ifs_firstc
[0] == 0)
9641 else if (expand_no_split_dollar_star
&& ifs_firstc
== 0)
9643 /* Posix interp 888: not RHS, no splitting, IFS set to '' */
9644 temp
= string_list_dollar_star (list
, quoted
, 0);
9647 temp
= string_list_dollar_at (list
, quoted
, 0);
9648 /* Set W_SPLITSPACE to make sure the individual positional
9649 parameters are split into separate arguments */
9651 if (quoted
== 0 && (ifs_is_set
== 0 || ifs_is_null
))
9652 #else /* change with bash-5.0 */
9653 if (quoted
== 0 && ifs_is_null
)
9655 tflag
|= W_SPLITSPACE
;
9656 /* If we're not quoted but we still don't want word splitting, make
9657 we quote the IFS characters to protect them from splitting (e.g.,
9658 when $@ is in the string as well). */
9659 else if (temp
&& quoted
== 0 && ifs_is_set
&& (pflags
& PF_ASSIGNRHS
))
9661 temp1
= quote_string (temp
);
9667 if (expand_no_split_dollar_star
== 0 && contains_dollar_at
)
9668 *contains_dollar_at
= 1;
9671 dispose_words (list
);
9674 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
9675 means that we have to turn quoting off after we split into
9676 the individually quoted arguments so that the final split
9677 on the first character of $IFS is still done. */
9678 case '@': /* `$@' */
9679 list
= list_rest_of_args ();
9682 /* According to austin-group posix proposal by Geoff Clare in
9683 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
9685 "The shell shall write a message to standard error and
9686 immediately exit when it tries to expand an unset parameter
9687 other than the '@' and '*' special parameters."
9690 if (list
== 0 && unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
9695 set_exit_status (EXECUTION_FAILURE
);
9696 err_unboundvar (uerror
);
9697 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9701 for (nullarg
= 0, l
= list
; l
; l
= l
->next
)
9703 if (l
->word
&& (l
->word
->word
== 0 || l
->word
->word
[0] == 0))
9707 /* We want to flag the fact that we saw this. We can't turn
9708 off quoting entirely, because other characters in the
9709 string might need it (consider "\"$@\""), but we need some
9710 way to signal that the final split on the first character
9711 of $IFS should be done, even though QUOTED is 1. */
9712 /* XXX - should this test include Q_PATQUOTE? */
9713 if (quoted_dollar_at_p
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9714 *quoted_dollar_at_p
= 1;
9715 if (contains_dollar_at
)
9716 *contains_dollar_at
= 1;
9718 /* We want to separate the positional parameters with the first
9719 character of $IFS in case $IFS is something other than a space.
9720 We also want to make sure that splitting is done no matter what --
9721 according to POSIX.2, this expands to a list of the positional
9722 parameters no matter what IFS is set to. */
9723 /* XXX - what to do when in a context where word splitting is not
9724 performed? Even when IFS is not the default, posix seems to imply
9725 that we have to expand $@ to all the positional parameters and
9726 separate them with spaces, which are preserved because word splitting
9727 doesn't take place. See below for how we use PF_NOSPLIT2 here. */
9729 /* These are the cases where word splitting will not be performed. */
9730 if (pflags
& PF_ASSIGNRHS
)
9732 temp
= string_list_dollar_at (list
, (quoted
|Q_DOUBLE_QUOTES
), pflags
);
9734 tflag
|= W_HASQUOTEDNULL
; /* we know quoting produces quoted nulls */
9737 /* This needs to match what expand_word_internal does with non-quoted $@
9738 does with separating with spaces. Passing Q_DOUBLE_QUOTES means that
9739 the characters in LIST will be quoted, and PF_ASSIGNRHS ensures that
9740 they will separated by spaces. After doing this, we need the special
9741 handling for PF_NOSPLIT2 in expand_word_internal to remove the CTLESC
9743 else if (pflags
& PF_NOSPLIT2
)
9745 #if defined (HANDLE_MULTIBYTE)
9746 if (quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && ifs_firstc
[0] != ' ')
9748 if (quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && ifs_firstc
!= ' ')
9750 /* Posix interp 888 */
9751 temp
= string_list_dollar_at (list
, Q_DOUBLE_QUOTES
, pflags
);
9753 temp
= string_list_dollar_at (list
, quoted
, pflags
);
9756 temp
= string_list_dollar_at (list
, quoted
, pflags
);
9758 tflag
|= W_DOLLARAT
;
9759 dispose_words (list
);
9763 tdesc
= parameter_brace_expand (string
, &zindex
, quoted
, pflags
,
9765 contains_dollar_at
);
9767 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
9769 temp
= tdesc
? tdesc
->word
: (char *)0;
9772 /* Quoted nulls should be removed if there is anything else
9774 /* Note that we saw the quoted null so we can add one back at
9775 the end of this function if there are no other characters
9776 in the string, discard TEMP, and go on. The exception to
9777 this is when we have "${@}" and $1 is '', since $@ needs
9778 special handling. */
9779 if (tdesc
&& tdesc
->word
&& (tdesc
->flags
& W_HASQUOTEDNULL
) && QUOTED_NULL (temp
))
9781 if (had_quoted_null_p
)
9782 *had_quoted_null_p
= 1;
9783 if (*quoted_dollar_at_p
== 0)
9786 tdesc
->word
= temp
= (char *)NULL
;
9794 /* Do command or arithmetic substitution. */
9796 /* We have to extract the contents of this paren substitution. */
9797 t_index
= zindex
+ 1;
9798 /* XXX - might want to check for string[t_index+2] == LPAREN and parse
9799 as arithmetic substitution immediately. */
9800 temp
= extract_command_subst (string
, &t_index
, (pflags
&PF_COMPLETE
) ? SX_COMPLETE
: 0);
9803 /* For Posix.2-style `$(( ))' arithmetic substitution,
9804 extract the expression and pass it to the evaluator. */
9805 if (temp
&& *temp
== LPAREN
)
9809 temp2
= savestring (temp1
);
9810 t_index
= strlen (temp2
) - 1;
9812 if (temp2
[t_index
] != RPAREN
)
9818 /* Cut off ending `)' */
9819 temp2
[t_index
] = '\0';
9821 if (chk_arithsub (temp2
, t_index
) == 0)
9825 internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
9830 /* Expand variables found inside the expression. */
9831 temp1
= expand_arith_string (temp2
, Q_DOUBLE_QUOTES
|Q_ARITH
);
9835 /* No error messages. */
9836 savecmd
= this_command_name
;
9837 this_command_name
= (char *)NULL
;
9838 number
= evalexp (temp1
, EXP_EXPANDED
, &expok
);
9839 this_command_name
= savecmd
;
9844 if (interactive_shell
== 0 && posixly_correct
)
9846 set_exit_status (EXECUTION_FAILURE
);
9847 return (&expand_wdesc_fatal
);
9850 return (&expand_wdesc_error
);
9852 temp
= itos (number
);
9857 if (pflags
& PF_NOCOMSUB
)
9858 /* we need zindex+1 because string[zindex] == RPAREN */
9859 temp1
= substring (string
, *sindex
, zindex
+1);
9862 tdesc
= command_substitute (temp
, quoted
, pflags
&PF_ASSIGNRHS
);
9863 temp1
= tdesc
? tdesc
->word
: (char *)NULL
;
9865 dispose_word_desc (tdesc
);
9871 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
9872 away in a future bash release. */
9874 /* Extract the contents of this arithmetic substitution. */
9875 t_index
= zindex
+ 1;
9876 temp
= extract_arithmetic_subst (string
, &t_index
);
9880 temp
= savestring (string
);
9881 if (expanded_something
)
9882 *expanded_something
= 0;
9886 /* Do initial variable expansion. */
9887 temp1
= expand_arith_string (temp
, Q_DOUBLE_QUOTES
|Q_ARITH
);
9892 /* Find the variable in VARIABLE_LIST. */
9893 temp
= (char *)NULL
;
9895 for (t_index
= zindex
; (c
= string
[zindex
]) && legal_variable_char (c
); zindex
++)
9897 temp1
= (zindex
> t_index
) ? substring (string
, t_index
, zindex
) : (char *)NULL
;
9899 /* If this isn't a variable name, then just output the `$'. */
9900 if (temp1
== 0 || *temp1
== '\0')
9903 temp
= (char *)xmalloc (2);
9906 if (expanded_something
)
9907 *expanded_something
= 0;
9911 /* If the variable exists, return its value cell. */
9912 var
= find_variable (temp1
);
9914 if (var
&& invisible_p (var
) == 0 && var_isset (var
))
9916 #if defined (ARRAY_VARS)
9917 if (assoc_p (var
) || array_p (var
))
9919 temp
= array_p (var
) ? array_reference (array_cell (var
), 0)
9920 : assoc_reference (assoc_cell (var
), "0");
9922 temp
= (*temp
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9923 ? quote_string (temp
)
9924 : quote_escapes (temp
);
9925 else if (unbound_vars_is_error
)
9926 goto unbound_variable
;
9931 temp
= value_cell (var
);
9933 temp
= (*temp
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9934 ? quote_string (temp
)
9935 : ((pflags
& PF_ASSIGNRHS
) ? quote_rhs (temp
)
9936 : quote_escapes (temp
));
9943 else if (var
&& (invisible_p (var
) || var_isset (var
) == 0))
9944 temp
= (char *)NULL
;
9945 else if ((var
= find_variable_last_nameref (temp1
, 0)) && var_isset (var
) && invisible_p (var
) == 0)
9947 temp
= nameref_cell (var
);
9948 #if defined (ARRAY_VARS)
9949 if (temp
&& *temp
&& valid_array_reference (temp
, 0))
9951 tdesc
= parameter_brace_expand_word (temp
, SPECIAL_VAR (temp
, 0), quoted
, pflags
, (arrayind_t
*)NULL
);
9952 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
9959 /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */
9960 if (temp
&& *temp
&& legal_identifier (temp
) == 0)
9962 set_exit_status (EXECUTION_FAILURE
);
9963 report_error (_("%s: invalid variable name for name reference"), temp
);
9964 return (&expand_wdesc_error
); /* XXX */
9967 temp
= (char *)NULL
;
9970 temp
= (char *)NULL
;
9973 if (unbound_vars_is_error
)
9975 set_exit_status (EXECUTION_FAILURE
);
9976 err_unboundvar (temp1
);
9985 set_exit_status (EXECUTION_FAILURE
);
9986 return ((unbound_vars_is_error
&& interactive_shell
== 0)
9987 ? &expand_wdesc_fatal
9988 : &expand_wdesc_error
);
9999 ret
= alloc_word_desc ();
10000 ret
->flags
= tflag
; /* XXX */
10007 invalidate_cached_quoted_dollar_at ()
10009 dispose_words (cached_quoted_dollar_at
);
10010 cached_quoted_dollar_at
= 0;
10013 /* Make a word list which is the result of parameter and variable
10014 expansion, command substitution, arithmetic substitution, and
10015 quote removal of WORD. Return a pointer to a WORD_LIST which is
10016 the result of the expansion. If WORD contains a null word, the
10017 word list returned is also null.
10019 QUOTED contains flag values defined in shell.h.
10021 ISEXP is used to tell expand_word_internal that the word should be
10022 treated as the result of an expansion. This has implications for
10023 how IFS characters in the word are treated.
10025 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
10026 they point to an integer value which receives information about expansion.
10027 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
10028 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
10031 This only does word splitting in the case of $@ expansion. In that
10032 case, we split on ' '. */
10034 /* Values for the local variable quoted_state. */
10036 #define PARTIALLY_QUOTED 1
10037 #define WHOLLY_QUOTED 2
10040 expand_word_internal (word
, quoted
, isexp
, contains_dollar_at
, expanded_something
)
10043 int *contains_dollar_at
;
10044 int *expanded_something
;
10049 /* The intermediate string that we build while expanding. */
10052 /* The current size of the above object. */
10053 size_t istring_size
;
10055 /* Index into ISTRING. */
10058 /* Temporary string storage. */
10059 char *temp
, *temp1
;
10061 /* The text of WORD. */
10062 register char *string
;
10064 /* The size of STRING. */
10065 size_t string_size
;
10067 /* The index into STRING. */
10070 /* This gets 1 if we see a $@ while quoted. */
10071 int quoted_dollar_at
;
10073 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
10074 whether WORD contains no quoting characters, a partially quoted
10075 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
10079 int had_quoted_null
;
10080 int has_quoted_ifs
; /* did we add a quoted $IFS character here? */
10081 int has_dollar_at
, temp_has_dollar_at
;
10082 int split_on_spaces
;
10083 int local_expanded
;
10085 int pflags
; /* flags passed to param_expand */
10088 int assignoff
; /* If assignment, offset of `=' */
10090 register unsigned char c
; /* Current character. */
10091 int t_index
; /* For calls to string_extract_xxx. */
10097 /* OK, let's see if we can optimize a common idiom: "$@" */
10098 if (STREQ (word
->word
, "\"$@\"") &&
10099 (word
->flags
== (W_HASDOLLAR
|W_QUOTED
)) &&
10100 dollar_vars
[1]) /* XXX - check IFS here as well? */
10102 if (contains_dollar_at
)
10103 *contains_dollar_at
= 1;
10104 if (expanded_something
)
10105 *expanded_something
= 1;
10106 if (cached_quoted_dollar_at
)
10107 return (copy_word_list (cached_quoted_dollar_at
));
10108 list
= list_rest_of_args ();
10109 list
= quote_list (list
);
10110 cached_quoted_dollar_at
= copy_word_list (list
);
10114 istring
= (char *)xmalloc (istring_size
= DEFAULT_INITIAL_ARRAY_SIZE
);
10115 istring
[istring_index
= 0] = '\0';
10116 quoted_dollar_at
= had_quoted_null
= has_dollar_at
= 0;
10117 has_quoted_ifs
= 0;
10118 split_on_spaces
= 0;
10119 quoted_state
= UNQUOTED
;
10121 string
= word
->word
;
10123 goto finished_with_string
;
10124 mb_cur_max
= MB_CUR_MAX
;
10126 /* Don't need the string length for the SADD... and COPY_ macros unless
10127 multibyte characters are possible, but do need it for bounds checking. */
10128 string_size
= (mb_cur_max
> 1) ? strlen (string
) : 1;
10130 if (contains_dollar_at
)
10131 *contains_dollar_at
= 0;
10135 /* Begin the expansion. */
10137 for (sindex
= 0; ;)
10139 c
= string
[sindex
];
10141 /* Case on top-level character. */
10145 goto finished_with_string
;
10149 #if HANDLE_MULTIBYTE
10150 if (mb_cur_max
> 1 && string
[sindex
])
10152 SADD_MBQCHAR_BODY(temp
, string
, sindex
, string_size
);
10157 temp
= (char *)xmalloc (3);
10159 temp
[1] = c
= string
[sindex
];
10164 if (string
[sindex
])
10170 istring
= sub_append_string (temp
, istring
, &istring_index
, &istring_size
);
10176 #if defined (PROCESS_SUBSTITUTION)
10177 /* Process substitution. */
10181 /* XXX - technically this should only be expanded at the start
10183 if (string
[++sindex
] != LPAREN
|| (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (word
->flags
& (W_DQUOTE
|W_NOPROCSUB
)))
10185 sindex
--; /* add_character: label increments sindex */
10186 goto add_character
;
10189 t_index
= sindex
+ 1; /* skip past both '<' and LPAREN */
10191 temp1
= extract_process_subst (string
, (c
== '<') ? "<(" : ">(", &t_index
, 0); /*))*/
10194 /* If the process substitution specification is `<()', we want to
10195 open the pipe for writing in the child and produce output; if
10196 it is `>()', we want to open the pipe for reading in the child
10197 and consume input. */
10198 temp
= temp1
? process_substitute (temp1
, (c
== '>')) : (char *)0;
10202 goto dollar_add_string
;
10204 #endif /* PROCESS_SUBSTITUTION */
10207 /* Posix.2 section 3.6.1 says that tildes following `=' in words
10208 which are not assignment statements are not expanded. If the
10209 shell isn't in posix mode, though, we perform tilde expansion
10210 on `likely candidate' unquoted assignment statements (flags
10211 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
10212 contains an unquoted :~ or =~. Something to think about: we
10213 now have a flag that says to perform tilde expansion on arguments
10214 to `assignment builtins' like declare and export that look like
10215 assignment statements. We now do tilde expansion on such words
10216 even in POSIX mode. */
10217 if (word
->flags
& (W_ASSIGNRHS
|W_NOTILDE
))
10219 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
10220 goto add_ifs_character
;
10222 goto add_character
;
10224 /* If we're not in posix mode or forcing assignment-statement tilde
10225 expansion, note where the first `=' appears in the word and prepare
10226 to do tilde expansion following the first `='. We have to keep
10227 track of the first `=' (using assignoff) to avoid being confused
10228 by an `=' in the rhs of the assignment statement. */
10229 if ((word
->flags
& W_ASSIGNMENT
) &&
10230 (posixly_correct
== 0 || (word
->flags
& W_TILDEEXP
)) &&
10231 assignoff
== -1 && sindex
> 0)
10232 assignoff
= sindex
;
10233 if (sindex
== assignoff
&& string
[sindex
+1] == '~') /* XXX */
10234 word
->flags
|= W_ITILDE
;
10236 if (word
->flags
& W_ASSIGNARG
)
10237 word
->flags
|= W_ASSIGNRHS
; /* affects $@ */
10239 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
10242 goto add_ifs_character
;
10245 goto add_character
;
10248 if (word
->flags
& (W_NOTILDE
|W_NOASSNTILDE
))
10250 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
10251 goto add_ifs_character
;
10253 goto add_character
;
10256 if ((word
->flags
& (W_ASSIGNMENT
|W_ASSIGNRHS
)) &&
10257 (posixly_correct
== 0 || (word
->flags
& W_TILDEEXP
)) &&
10258 string
[sindex
+1] == '~')
10259 word
->flags
|= W_ITILDE
;
10261 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
10262 goto add_ifs_character
;
10264 goto add_character
;
10267 /* If the word isn't supposed to be tilde expanded, or we're not
10268 at the start of a word or after an unquoted : or = in an
10269 assignment statement, we don't do tilde expansion. We don't
10270 do tilde expansion if quoted or in an arithmetic context. */
10272 if ((word
->flags
& (W_NOTILDE
|W_DQUOTE
)) ||
10273 (sindex
> 0 && ((word
->flags
& W_ITILDE
) == 0)) ||
10274 (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
10276 word
->flags
&= ~W_ITILDE
;
10277 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
) && (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) == 0)
10278 goto add_ifs_character
;
10280 goto add_character
;
10283 if (word
->flags
& W_ASSIGNRHS
)
10285 else if (word
->flags
& (W_ASSIGNMENT
|W_TILDEEXP
))
10290 temp
= bash_tilde_find_word (string
+ sindex
, tflag
, &t_index
);
10292 word
->flags
&= ~W_ITILDE
;
10294 if (temp
&& *temp
&& t_index
> 0)
10296 temp1
= bash_tilde_expand (temp
, tflag
);
10297 if (temp1
&& *temp1
== '~' && STREQ (temp
, temp1
))
10301 goto add_character
; /* tilde expansion failed */
10306 goto add_quoted_string
; /* XXX was add_string */
10311 goto add_character
;
10315 if (expanded_something
)
10316 *expanded_something
= 1;
10317 local_expanded
= 1;
10319 temp_has_dollar_at
= 0;
10320 pflags
= (word
->flags
& W_NOCOMSUB
) ? PF_NOCOMSUB
: 0;
10321 if (word
->flags
& W_NOSPLIT2
)
10322 pflags
|= PF_NOSPLIT2
;
10323 if (word
->flags
& W_ASSIGNRHS
)
10324 pflags
|= PF_ASSIGNRHS
;
10325 if (word
->flags
& W_COMPLETE
)
10326 pflags
|= PF_COMPLETE
;
10328 tword
= param_expand (string
, &sindex
, quoted
, expanded_something
,
10329 &temp_has_dollar_at
, "ed_dollar_at
,
10330 &had_quoted_null
, pflags
);
10331 has_dollar_at
+= temp_has_dollar_at
;
10332 split_on_spaces
+= (tword
->flags
& W_SPLITSPACE
);
10334 if (tword
== &expand_wdesc_error
|| tword
== &expand_wdesc_fatal
)
10338 return ((tword
== &expand_wdesc_error
) ? &expand_word_error
10339 : &expand_word_fatal
);
10341 if (contains_dollar_at
&& has_dollar_at
)
10342 *contains_dollar_at
= 1;
10344 if (tword
&& (tword
->flags
& W_HASQUOTEDNULL
))
10345 had_quoted_null
= 1; /* note for later */
10346 if (tword
&& (tword
->flags
& W_SAWQUOTEDNULL
))
10347 had_quoted_null
= 1; /* XXX */
10349 temp
= tword
? tword
->word
: (char *)NULL
;
10350 dispose_word_desc (tword
);
10352 /* Kill quoted nulls; we will add them back at the end of
10353 expand_word_internal if nothing else in the string */
10354 if (had_quoted_null
&& temp
&& QUOTED_NULL (temp
))
10357 temp
= (char *)NULL
;
10363 case '`': /* Backquoted command substitution. */
10365 t_index
= sindex
++;
10367 temp
= string_extract (string
, &sindex
, "`", SX_REQMATCH
);
10368 /* The test of sindex against t_index is to allow bare instances of
10369 ` to pass through, for backwards compatibility. */
10370 if (temp
== &extract_string_error
|| temp
== &extract_string_fatal
)
10372 if (sindex
- 1 == t_index
)
10375 goto add_character
;
10377 set_exit_status (EXECUTION_FAILURE
);
10378 report_error (_("bad substitution: no closing \"`\" in %s") , string
+t_index
);
10381 return ((temp
== &extract_string_error
) ? &expand_word_error
10382 : &expand_word_fatal
);
10385 if (expanded_something
)
10386 *expanded_something
= 1;
10387 local_expanded
= 1;
10389 if (word
->flags
& W_NOCOMSUB
)
10390 /* sindex + 1 because string[sindex] == '`' */
10391 temp1
= substring (string
, t_index
, sindex
+ 1);
10394 de_backslash (temp
);
10395 tword
= command_substitute (temp
, quoted
, 0);
10396 temp1
= tword
? tword
->word
: (char *)NULL
;
10398 dispose_word_desc (tword
);
10402 goto dollar_add_string
;
10406 if (string
[sindex
+ 1] == '\n')
10412 c
= string
[++sindex
];
10414 /* "However, the double-quote character ( '"' ) shall not be treated
10415 specially within a here-document, except when the double-quote
10416 appears within "$()", "``", or "${}"." */
10417 if ((quoted
& Q_HERE_DOCUMENT
) && (quoted
& Q_DOLBRACE
) && c
== '"')
10418 tflag
= CBSDQUOTE
; /* special case */
10419 else if (quoted
& Q_HERE_DOCUMENT
)
10421 else if (quoted
& Q_DOUBLE_QUOTES
)
10426 /* From Posix discussion on austin-group list: Backslash escaping
10427 a } in ${...} is removed. Issue 0000221 */
10428 if ((quoted
& Q_DOLBRACE
) && c
== RBRACE
)
10430 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
10432 /* This is the fix for " $@\ " */
10433 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && ((sh_syntaxtab
[c
] & tflag
) == 0) && isexp
== 0 && isifs (c
))
10435 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
10436 DEFAULT_ARRAY_SIZE
);
10437 istring
[istring_index
++] = CTLESC
;
10438 istring
[istring_index
++] = '\\';
10439 istring
[istring_index
] = '\0';
10441 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
10443 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && c
== 0)
10445 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
10446 DEFAULT_ARRAY_SIZE
);
10447 istring
[istring_index
++] = CTLESC
;
10448 istring
[istring_index
++] = '\\';
10449 istring
[istring_index
] = '\0';
10452 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && ((sh_syntaxtab
[c
] & tflag
) == 0))
10454 SCOPY_CHAR_I (twochars
, '\\', c
, string
, sindex
, string_size
);
10459 sindex
--; /* add_character: label increments sindex */
10460 goto add_character
;
10464 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
10469 /* BEFORE jumping here, we need to increment sindex if appropriate */
10470 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
10471 DEFAULT_ARRAY_SIZE
);
10472 istring
[istring_index
++] = twochars
[0];
10473 istring
[istring_index
++] = twochars
[1];
10474 istring
[istring_index
] = '\0';
10479 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) && ((quoted
& Q_ARITH
) == 0))
10480 goto add_character
;
10482 t_index
= ++sindex
;
10483 temp
= string_extract_double_quoted (string
, &sindex
, (word
->flags
& W_COMPLETE
) ? SX_COMPLETE
: 0);
10485 /* If the quotes surrounded the entire string, then the
10486 whole word was quoted. */
10487 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
10489 : PARTIALLY_QUOTED
;
10493 tword
= alloc_word_desc ();
10494 tword
->word
= temp
;
10496 if (word
->flags
& W_ASSIGNARG
)
10497 tword
->flags
|= word
->flags
& (W_ASSIGNARG
|W_ASSIGNRHS
); /* affects $@ */
10498 if (word
->flags
& W_COMPLETE
)
10499 tword
->flags
|= W_COMPLETE
; /* for command substitutions */
10500 if (word
->flags
& W_NOCOMSUB
)
10501 tword
->flags
|= W_NOCOMSUB
;
10502 if (word
->flags
& W_NOPROCSUB
)
10503 tword
->flags
|= W_NOPROCSUB
;
10505 if (word
->flags
& W_ASSIGNRHS
)
10506 tword
->flags
|= W_ASSIGNRHS
;
10508 temp
= (char *)NULL
;
10510 temp_has_dollar_at
= 0; /* does this quoted (sub)string include $@? */
10511 /* Need to get W_HASQUOTEDNULL flag through this function. */
10512 list
= expand_word_internal (tword
, Q_DOUBLE_QUOTES
, 0, &temp_has_dollar_at
, (int *)NULL
);
10513 has_dollar_at
+= temp_has_dollar_at
;
10515 if (list
== &expand_word_error
|| list
== &expand_word_fatal
)
10519 /* expand_word_internal has already freed temp_word->word
10520 for us because of the way it prints error messages. */
10521 tword
->word
= (char *)NULL
;
10522 dispose_word (tword
);
10526 dispose_word (tword
);
10528 /* "$@" (a double-quoted dollar-at) expands into nothing,
10529 not even a NULL word, when there are no positional
10530 parameters. Posix interp 888 says that other parts of the
10531 word that expand to quoted nulls result in quoted nulls, so
10532 we can't just throw the entire word away if we have "$@"
10533 anywhere in it. We use had_quoted_null to keep track */
10534 if (list
== 0 && temp_has_dollar_at
) /* XXX - was has_dollar_at */
10536 quoted_dollar_at
++;
10540 /* If this list comes back with a quoted null from expansion,
10541 we have either "$x" or "$@" with $1 == ''. In either case,
10542 we need to make sure we add a quoted null argument and
10543 disable the special handling that "$@" gets. */
10544 if (list
&& list
->word
&& list
->next
== 0 && (list
->word
->flags
& W_HASQUOTEDNULL
))
10546 if (had_quoted_null
&& temp_has_dollar_at
)
10547 quoted_dollar_at
++;
10548 had_quoted_null
= 1; /* XXX */
10551 /* If we get "$@", we know we have expanded something, so we
10552 need to remember it for the final split on $IFS. This is
10553 a special case; it's the only case where a quoted string
10554 can expand into more than one word. It's going to come back
10555 from the above call to expand_word_internal as a list with
10558 dequote_list (list
);
10560 if (temp_has_dollar_at
) /* XXX - was has_dollar_at */
10562 quoted_dollar_at
++;
10563 if (contains_dollar_at
)
10564 *contains_dollar_at
= 1;
10565 if (expanded_something
)
10566 *expanded_something
= 1;
10567 local_expanded
= 1;
10572 /* What we have is "". This is a minor optimization. */
10574 list
= (WORD_LIST
*)NULL
;
10575 had_quoted_null
= 1; /* note for later */
10578 /* The code above *might* return a list (consider the case of "$@",
10579 where it returns "$1", "$2", etc.). We can't throw away the
10580 rest of the list, and we have to make sure each word gets added
10581 as quoted. We test on tresult->next: if it is non-NULL, we
10582 quote the whole list, save it to a string with string_list, and
10583 add that string. We don't need to quote the results of this
10584 (and it would be wrong, since that would quote the separators
10585 as well), so we go directly to add_string. */
10590 /* Testing quoted_dollar_at makes sure that "$@" is
10591 split correctly when $IFS does not contain a space. */
10592 temp
= quoted_dollar_at
10593 ? string_list_dollar_at (list
, Q_DOUBLE_QUOTES
, 0)
10594 : string_list (quote_list (list
));
10595 dispose_words (list
);
10600 temp
= savestring (list
->word
->word
);
10601 tflag
= list
->word
->flags
;
10602 dispose_words (list
);
10604 /* If the string is not a quoted null string, we want
10605 to remove any embedded unquoted CTLNUL characters.
10606 We do not want to turn quoted null strings back into
10607 the empty string, though. We do this because we
10608 want to remove any quoted nulls from expansions that
10609 contain other characters. For example, if we have
10610 x"$*"y or "x$*y" and there are no positional parameters,
10611 the $* should expand into nothing. */
10612 /* We use the W_HASQUOTEDNULL flag to differentiate the
10613 cases: a quoted null character as above and when
10614 CTLNUL is contained in the (non-null) expansion
10615 of some variable. We use the had_quoted_null flag to
10616 pass the value through this function to its caller. */
10617 if ((tflag
& W_HASQUOTEDNULL
) && QUOTED_NULL (temp
) == 0)
10618 remove_quoted_nulls (temp
); /* XXX */
10622 temp
= (char *)NULL
;
10624 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
)
10625 had_quoted_null
= 1; /* note for later */
10627 /* We do not want to add quoted nulls to strings that are only
10628 partially quoted; we can throw them away. The exception to
10629 this is when we are going to be performing word splitting,
10630 since we have to preserve a null argument if the next character
10631 will cause word splitting. */
10632 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
&& quoted
== 0 && (word
->flags
& (W_NOSPLIT
|W_EXPANDRHS
|W_ASSIGNRHS
)) == W_EXPANDRHS
)
10636 had_quoted_null
= 1;
10637 goto add_character
;
10639 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
&& (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)))
10647 temp
= quote_string (temp
);
10653 /* Add NULL arg. */
10655 sindex
--; /* add_character: label increments sindex */
10656 had_quoted_null
= 1; /* note for later */
10657 goto add_character
;
10663 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
10664 goto add_character
;
10666 t_index
= ++sindex
;
10667 temp
= string_extract_single_quoted (string
, &sindex
);
10669 /* If the entire STRING was surrounded by single quotes,
10670 then the string is wholly quoted. */
10671 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
10673 : PARTIALLY_QUOTED
;
10675 /* If all we had was '', it is a null expansion. */
10679 temp
= (char *)NULL
;
10682 remove_quoted_escapes (temp
); /* ??? */
10684 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
)
10685 had_quoted_null
= 1; /* note for later */
10687 /* We do not want to add quoted nulls to strings that are only
10688 partially quoted; such nulls are discarded. See above for the
10689 exception, which is when the string is going to be split.
10690 Posix interp 888/1129 */
10691 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
&& quoted
== 0 && (word
->flags
& (W_NOSPLIT
|W_EXPANDRHS
|W_ASSIGNRHS
)) == W_EXPANDRHS
)
10695 goto add_character
;
10698 if (temp
== 0 && (quoted_state
== PARTIALLY_QUOTED
) && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)))
10701 /* If we have a quoted null expansion, add a quoted NULL to istring. */
10705 sindex
--; /* add_character: label increments sindex */
10706 goto add_character
;
10709 goto add_quoted_string
;
10714 /* If we are in a context where the word is not going to be split, but
10715 we need to account for $@ and $* producing one word for each
10716 positional parameter, add quoted spaces so the spaces in the
10717 expansion of "$@", if any, behave correctly. We still may need to
10718 split if we are expanding the rhs of a word expansion. */
10719 if (ifs_is_null
|| split_on_spaces
|| ((word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
|W_ASSIGNRHS
)) && (word
->flags
& W_EXPANDRHS
) == 0))
10721 if (string
[sindex
])
10723 twochars
[0] = CTLESC
;
10730 /* This is the fix for " $@ " */
10732 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (isexp
== 0 && isifs (c
) && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0))
10734 if ((quoted
&(Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0)
10736 add_quoted_character
:
10737 if (string
[sindex
]) /* from old goto dollar_add_string */
10742 goto add_character
;
10746 #if HANDLE_MULTIBYTE
10747 /* XXX - should make sure that c is actually multibyte,
10748 otherwise we can use the twochars branch */
10749 if (mb_cur_max
> 1)
10752 if (mb_cur_max
> 1)
10754 SADD_MBQCHAR_BODY(temp
, string
, sindex
, string_size
);
10759 twochars
[0] = CTLESC
;
10766 SADD_MBCHAR (temp
, string
, sindex
, string_size
);
10769 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 1, istring_size
,
10770 DEFAULT_ARRAY_SIZE
);
10771 istring
[istring_index
++] = c
;
10772 istring
[istring_index
] = '\0';
10774 /* Next character. */
10779 finished_with_string
:
10780 /* OK, we're ready to return. If we have a quoted string, and
10781 quoted_dollar_at is not set, we do no splitting at all; otherwise
10782 we split on ' '. The routines that call this will handle what to
10783 do if nothing has been expanded. */
10785 /* Partially and wholly quoted strings which expand to the empty
10786 string are retained as an empty arguments. Unquoted strings
10787 which expand to the empty string are discarded. The single
10788 exception is the case of expanding "$@" when there are no
10789 positional parameters. In that case, we discard the expansion. */
10791 /* Because of how the code that handles "" and '' in partially
10792 quoted strings works, we need to make ISTRING into a QUOTED_NULL
10793 if we saw quoting characters, but the expansion was empty.
10794 "" and '' are tossed away before we get to this point when
10795 processing partially quoted strings. This makes "" and $xxx""
10796 equivalent when xxx is unset. We also look to see whether we
10797 saw a quoted null from a ${} expansion and add one back if we
10800 /* If we expand to nothing and there were no single or double quotes
10801 in the word, we throw it away. Otherwise, we return a NULL word.
10802 The single exception is for $@ surrounded by double quotes when
10803 there are no positional parameters. In that case, we also throw
10806 if (*istring
== '\0')
10808 if (quoted_dollar_at
== 0 && (had_quoted_null
|| quoted_state
== PARTIALLY_QUOTED
))
10810 istring
[0] = CTLNUL
;
10812 tword
= alloc_word_desc ();
10813 tword
->word
= istring
;
10814 istring
= 0; /* avoid later free() */
10815 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
10816 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
10817 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
10818 tword
->flags
|= W_QUOTED
;
10820 /* According to sh, ksh, and Posix.2, if a word expands into nothing
10821 and a double-quoted "$@" appears anywhere in it, then the entire
10822 word is removed. */
10823 /* XXX - exception appears to be that quoted null strings result in
10825 else if (quoted_state
== UNQUOTED
|| quoted_dollar_at
)
10826 list
= (WORD_LIST
*)NULL
;
10828 list
= (WORD_LIST
*)NULL
;
10830 else if (word
->flags
& W_NOSPLIT
)
10832 tword
= alloc_word_desc ();
10833 tword
->word
= istring
;
10834 if (had_quoted_null
&& QUOTED_NULL (istring
))
10835 tword
->flags
|= W_HASQUOTEDNULL
;
10836 istring
= 0; /* avoid later free() */
10837 if (word
->flags
& W_ASSIGNMENT
)
10838 tword
->flags
|= W_ASSIGNMENT
; /* XXX */
10839 if (word
->flags
& W_COMPASSIGN
)
10840 tword
->flags
|= W_COMPASSIGN
; /* XXX */
10841 if (word
->flags
& W_NOGLOB
)
10842 tword
->flags
|= W_NOGLOB
; /* XXX */
10843 if (word
->flags
& W_NOBRACE
)
10844 tword
->flags
|= W_NOBRACE
; /* XXX */
10845 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
10846 tword
->flags
|= W_QUOTED
;
10847 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
10849 else if (word
->flags
& W_ASSIGNRHS
)
10851 list
= list_string (istring
, "", quoted
);
10852 tword
= list
->word
;
10853 if (had_quoted_null
&& QUOTED_NULL (istring
))
10854 tword
->flags
|= W_HASQUOTEDNULL
;
10857 istring
= 0; /* avoid later free() */
10858 goto set_word_flags
;
10864 ifs_chars
= (quoted_dollar_at
|| has_dollar_at
) ? ifs_value
: (char *)NULL
;
10866 /* If we have $@, we need to split the results no matter what. If
10867 IFS is unset or NULL, string_list_dollar_at has separated the
10868 positional parameters with a space, so we split on space (we have
10869 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
10870 string_list_dollar_at has separated the positional parameters
10871 with the first character of $IFS, so we split on $IFS. If
10872 SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either
10873 unset or null, and we want to make sure that we split on spaces
10874 regardless of what else has happened to IFS since the expansion,
10875 or we expanded "$@" with IFS null and we need to split the positional
10876 parameters into separate words. */
10877 if (split_on_spaces
)
10879 /* If IFS is not set, and the word is not quoted, we want to split
10880 the individual words on $' \t\n'. We rely on previous steps to
10881 quote the portions of the word that should not be split */
10882 if (ifs_is_set
== 0)
10883 list
= list_string (istring
, " \t\n", 1); /* XXX quoted == 1? */
10885 list
= list_string (istring
, " ", 1); /* XXX quoted == 1? */
10888 /* If we have $@ (has_dollar_at != 0) and we are in a context where we
10889 don't want to split the result (W_NOSPLIT2), and we are not quoted,
10890 we have already separated the arguments with the first character of
10891 $IFS. In this case, we want to return a list with a single word
10892 with the separator possibly replaced with a space (it's what other
10893 shells seem to do).
10894 quoted_dollar_at is internal to this function and is set if we are
10895 passed an argument that is unquoted (quoted == 0) but we encounter a
10896 double-quoted $@ while expanding it. */
10897 else if (has_dollar_at
&& quoted_dollar_at
== 0 && ifs_chars
&& quoted
== 0 && (word
->flags
& W_NOSPLIT2
))
10899 tword
= alloc_word_desc ();
10900 /* Only split and rejoin if we have to */
10901 if (*ifs_chars
&& *ifs_chars
!= ' ')
10903 /* list_string dequotes CTLESCs in the string it's passed, so we
10904 need it to get the space separation right if space isn't the
10905 first character in IFS (but is present) and to remove the
10906 quoting we added back in param_expand(). */
10907 list
= list_string (istring
, *ifs_chars
? ifs_chars
: " ", 1);
10908 /* This isn't exactly right in the case where we're expanding
10909 the RHS of an expansion like ${var-$@} where IFS=: (for
10910 example). The W_NOSPLIT2 means we do the separation with :;
10911 the list_string removes the quotes and breaks the string into
10912 a list, and the string_list rejoins it on spaces. When we
10913 return, we expect to be able to split the results, but the
10914 space separation means the right split doesn't happen. */
10915 tword
->word
= string_list (list
);
10918 tword
->word
= istring
;
10919 if (had_quoted_null
&& QUOTED_NULL (istring
))
10920 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
10921 if (tword
->word
!= istring
)
10923 istring
= 0; /* avoid later free() */
10924 goto set_word_flags
;
10926 else if (has_dollar_at
&& ifs_chars
)
10927 list
= list_string (istring
, *ifs_chars
? ifs_chars
: " ", 1);
10930 tword
= alloc_word_desc ();
10931 if (expanded_something
&& *expanded_something
== 0 && has_quoted_ifs
)
10932 tword
->word
= remove_quoted_ifs (istring
);
10934 tword
->word
= istring
;
10935 if (had_quoted_null
&& QUOTED_NULL (istring
)) /* should check for more than one */
10936 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
10937 else if (had_quoted_null
)
10938 tword
->flags
|= W_SAWQUOTEDNULL
; /* XXX */
10939 if (tword
->word
!= istring
)
10941 istring
= 0; /* avoid later free() */
10943 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) || (quoted_state
== WHOLLY_QUOTED
))
10944 tword
->flags
|= W_QUOTED
;
10945 if (word
->flags
& W_ASSIGNMENT
)
10946 tword
->flags
|= W_ASSIGNMENT
;
10947 if (word
->flags
& W_COMPASSIGN
)
10948 tword
->flags
|= W_COMPASSIGN
;
10949 if (word
->flags
& W_NOGLOB
)
10950 tword
->flags
|= W_NOGLOB
;
10951 if (word
->flags
& W_NOBRACE
)
10952 tword
->flags
|= W_NOBRACE
;
10953 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
10961 /* **************************************************************** */
10963 /* Functions for Quote Removal */
10965 /* **************************************************************** */
10967 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
10968 backslash quoting rules for within double quotes or a here document. */
10970 string_quote_removal (string
, quoted
)
10975 char *r
, *result_string
, *temp
, *send
;
10976 int sindex
, tindex
, dquote
;
10980 /* The result can be no longer than the original string. */
10981 slen
= strlen (string
);
10982 send
= string
+ slen
;
10984 r
= result_string
= (char *)xmalloc (slen
+ 1);
10986 for (dquote
= sindex
= 0; c
= string
[sindex
];)
10991 c
= string
[++sindex
];
10997 if (((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
) && (sh_syntaxtab
[c
] & CBSDQUOTE
) == 0)
11002 SCOPY_CHAR_M (r
, string
, send
, sindex
);
11006 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
)
11012 tindex
= sindex
+ 1;
11013 temp
= string_extract_single_quoted (string
, &tindex
);
11024 dquote
= 1 - dquote
;
11030 return (result_string
);
11035 /* Perform quote removal on word WORD. This allocates and returns a new
11038 word_quote_removal (word
, quoted
)
11045 t
= string_quote_removal (word
->word
, quoted
);
11046 w
= alloc_word_desc ();
11047 w
->word
= t
? t
: savestring ("");
11051 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
11052 the members of the list are treated as if they are surrounded by
11053 double quotes. Return a new list, or NULL if LIST is NULL. */
11055 word_list_quote_removal (list
, quoted
)
11059 WORD_LIST
*result
, *t
, *tresult
, *e
;
11061 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
11063 tresult
= make_word_list (word_quote_removal (t
->word
, quoted
), (WORD_LIST
*)NULL
);
11065 result
= (WORD_LIST
*) list_append (result
, tresult
);
11068 result
= e
= tresult
;
11081 /*******************************************
11083 * Functions to perform word splitting *
11085 *******************************************/
11095 ifs_value
= (v
&& value_cell (v
)) ? value_cell (v
) : " \t\n";
11097 ifs_is_set
= ifs_var
!= 0;
11098 ifs_is_null
= ifs_is_set
&& (*ifs_value
== 0);
11100 /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
11101 handle multibyte chars in IFS */
11102 memset (ifs_cmap
, '\0', sizeof (ifs_cmap
));
11103 for (t
= ifs_value
; t
&& *t
; t
++)
11109 #if defined (HANDLE_MULTIBYTE)
11110 if (ifs_value
== 0)
11112 ifs_firstc
[0] = '\0'; /* XXX - ? */
11113 ifs_firstc_len
= 1;
11117 if (locale_utf8locale
&& UTF8_SINGLEBYTE (*ifs_value
))
11118 ifs_firstc_len
= (*ifs_value
!= 0) ? 1 : 0;
11122 ifs_len
= strnlen (ifs_value
, MB_CUR_MAX
);
11123 ifs_firstc_len
= MBLEN (ifs_value
, ifs_len
);
11125 if (ifs_firstc_len
== 1 || ifs_firstc_len
== 0 || MB_INVALIDCH (ifs_firstc_len
))
11127 ifs_firstc
[0] = ifs_value
[0];
11128 ifs_firstc
[1] = '\0';
11129 ifs_firstc_len
= 1;
11132 memcpy (ifs_firstc
, ifs_value
, ifs_firstc_len
);
11135 ifs_firstc
= ifs_value
? *ifs_value
: 0;
11145 /* This splits a single word into a WORD LIST on $IFS, but only if the word
11146 is not quoted. list_string () performs quote removal for us, even if we
11147 don't do any splitting. */
11149 word_split (w
, ifs_chars
)
11159 xifs
= ((w
->flags
& W_QUOTED
) || ifs_chars
== 0) ? "" : ifs_chars
;
11160 result
= list_string (w
->word
, xifs
, w
->flags
& W_QUOTED
);
11163 result
= (WORD_LIST
*)NULL
;
11168 /* Perform word splitting on LIST and return the RESULT. It is possible
11169 to return (WORD_LIST *)NULL. */
11171 word_list_split (list
)
11174 WORD_LIST
*result
, *t
, *tresult
, *e
;
11177 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
11179 tresult
= word_split (t
->word
, ifs_value
);
11180 /* POSIX 2.6: "If the complete expansion appropriate for a word results
11181 in an empty field, that empty field shall be deleted from the list
11182 of fields that form the completely expanded command, unless the
11183 original word contained single-quote or double-quote characters."
11184 This is where we handle these words that contain quoted null strings
11185 and other characters that expand to nothing after word splitting. */
11186 if (tresult
== 0 && t
->word
&& (t
->word
->flags
& W_SAWQUOTEDNULL
)) /* XXX */
11188 w
= alloc_word_desc ();
11189 w
->word
= (char *)xmalloc (1);
11191 tresult
= make_word_list (w
, (WORD_LIST
*)NULL
);
11194 result
= e
= tresult
;
11205 /**************************************************
11207 * Functions to expand an entire WORD_LIST *
11209 **************************************************/
11211 /* Do any word-expansion-specific cleanup and jump to top_level */
11213 exp_jump_to_top_level (v
)
11216 set_pipestatus_from_exit (last_command_exit_value
);
11218 /* Cleanup code goes here. */
11219 expand_no_split_dollar_star
= 0; /* XXX */
11220 if (expanding_redir
)
11221 undo_partial_redirects ();
11222 expanding_redir
= 0;
11223 assigning_in_environment
= 0;
11225 if (parse_and_execute_level
== 0)
11226 top_level_cleanup (); /* from sig.c */
11228 jump_to_top_level (v
);
11231 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
11232 ELIST, and set ELIST to the new list. */
11233 #define PREPEND_LIST(nlist, elist) \
11234 do { nlist->next = elist; elist = nlist; } while (0)
11236 /* Separate out any initial variable assignments from TLIST. If set -k has
11237 been executed, remove all assignment statements from TLIST. Initial
11238 variable assignments and other environment assignments are placed
11239 on SUBST_ASSIGN_VARLIST. */
11241 separate_out_assignments (tlist
)
11244 register WORD_LIST
*vp
, *lp
;
11247 return ((WORD_LIST
*)NULL
);
11249 if (subst_assign_varlist
)
11250 dispose_words (subst_assign_varlist
); /* Clean up after previous error */
11252 subst_assign_varlist
= (WORD_LIST
*)NULL
;
11255 /* Separate out variable assignments at the start of the command.
11256 Loop invariant: vp->next == lp
11257 Loop postcondition:
11258 lp = list of words left after assignment statements skipped
11259 tlist = original list of words
11261 while (lp
&& (lp
->word
->flags
& W_ASSIGNMENT
))
11267 /* If lp != tlist, we have some initial assignment statements.
11268 We make SUBST_ASSIGN_VARLIST point to the list of assignment
11269 words and TLIST point to the remaining words. */
11272 subst_assign_varlist
= tlist
;
11273 /* ASSERT(vp->next == lp); */
11274 vp
->next
= (WORD_LIST
*)NULL
; /* terminate variable list */
11275 tlist
= lp
; /* remainder of word list */
11278 /* vp == end of variable list */
11279 /* tlist == remainder of original word list without variable assignments */
11281 /* All the words in tlist were assignment statements */
11282 return ((WORD_LIST
*)NULL
);
11284 /* ASSERT(tlist != NULL); */
11285 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
11287 /* If the -k option is in effect, we need to go through the remaining
11288 words, separate out the assignment words, and place them on
11289 SUBST_ASSIGN_VARLIST. */
11290 if (place_keywords_in_env
)
11292 WORD_LIST
*tp
; /* tp == running pointer into tlist */
11297 /* Loop Invariant: tp->next == lp */
11298 /* Loop postcondition: tlist == word list without assignment statements */
11301 if (lp
->word
->flags
& W_ASSIGNMENT
)
11303 /* Found an assignment statement, add this word to end of
11304 subst_assign_varlist (vp). */
11305 if (!subst_assign_varlist
)
11306 subst_assign_varlist
= vp
= lp
;
11313 /* Remove the word pointed to by LP from TLIST. */
11314 tp
->next
= lp
->next
;
11315 /* ASSERT(vp == lp); */
11316 lp
->next
= (WORD_LIST
*)NULL
;
11329 #define WEXP_VARASSIGN 0x001
11330 #define WEXP_BRACEEXP 0x002
11331 #define WEXP_TILDEEXP 0x004
11332 #define WEXP_PARAMEXP 0x008
11333 #define WEXP_PATHEXP 0x010
11335 /* All of the expansions, including variable assignments at the start of
11337 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
11339 /* All of the expansions except variable assignments at the start of
11341 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
11343 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
11344 expansion, command substitution, arithmetic expansion, word splitting, and
11346 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
11348 /* Take the list of words in LIST and do the various substitutions. Return
11349 a new list of words which is the expanded list, and without things like
11350 variable assignments. */
11353 expand_words (list
)
11356 return (expand_word_list_internal (list
, WEXP_ALL
));
11359 /* Same as expand_words (), but doesn't hack variable or environment
11362 expand_words_no_vars (list
)
11365 return (expand_word_list_internal (list
, WEXP_NOVARS
));
11369 expand_words_shellexp (list
)
11372 return (expand_word_list_internal (list
, WEXP_SHELLEXP
));
11376 glob_expand_word_list (tlist
, eflags
)
11380 char **glob_array
, *temp_string
;
11381 register int glob_index
;
11382 WORD_LIST
*glob_list
, *output_list
, *disposables
, *next
;
11386 output_list
= disposables
= (WORD_LIST
*)NULL
;
11387 glob_array
= (char **)NULL
;
11390 /* For each word, either globbing is attempted or the word is
11391 added to orig_list. If globbing succeeds, the results are
11392 added to orig_list and the word (tlist) is added to the list
11393 of disposable words. If globbing fails and failed glob
11394 expansions are left unchanged (the shell default), the
11395 original word is added to orig_list. If globbing fails and
11396 failed glob expansions are removed, the original word is
11397 added to the list of disposable words. orig_list ends up
11398 in reverse order and requires a call to REVERSE_LIST to
11399 be set right. After all words are examined, the disposable
11400 words are freed. */
11401 next
= tlist
->next
;
11403 /* If the word isn't an assignment and contains an unquoted
11404 pattern matching character, then glob it. */
11405 if ((tlist
->word
->flags
& W_NOGLOB
) == 0 &&
11406 unquoted_glob_pattern_p (tlist
->word
->word
))
11408 glob_array
= shell_glob_filename (tlist
->word
->word
, QGLOB_CTLESC
); /* XXX */
11410 /* Handle error cases.
11411 I don't think we should report errors like "No such file
11412 or directory". However, I would like to report errors
11413 like "Read failed". */
11415 if (glob_array
== 0 || GLOB_FAILED (glob_array
))
11417 glob_array
= (char **)xmalloc (sizeof (char *));
11418 glob_array
[0] = (char *)NULL
;
11421 /* Dequote the current word in case we have to use it. */
11422 if (glob_array
[0] == NULL
)
11424 temp_string
= dequote_string (tlist
->word
->word
);
11425 free (tlist
->word
->word
);
11426 tlist
->word
->word
= temp_string
;
11429 /* Make the array into a word list. */
11430 glob_list
= (WORD_LIST
*)NULL
;
11431 for (glob_index
= 0; glob_array
[glob_index
]; glob_index
++)
11433 tword
= make_bare_word (glob_array
[glob_index
]);
11434 glob_list
= make_word_list (tword
, glob_list
);
11439 output_list
= (WORD_LIST
*)list_append (glob_list
, output_list
);
11440 PREPEND_LIST (tlist
, disposables
);
11442 else if (fail_glob_expansion
!= 0)
11444 last_command_exit_value
= EXECUTION_FAILURE
;
11445 report_error (_("no match: %s"), tlist
->word
->word
);
11446 exp_jump_to_top_level (DISCARD
);
11448 else if (allow_null_glob_expansion
== 0)
11450 /* Failed glob expressions are left unchanged. */
11451 PREPEND_LIST (tlist
, output_list
);
11455 /* Failed glob expressions are removed. */
11456 PREPEND_LIST (tlist
, disposables
);
11461 /* Dequote the string. */
11462 temp_string
= dequote_string (tlist
->word
->word
);
11463 free (tlist
->word
->word
);
11464 tlist
->word
->word
= temp_string
;
11465 PREPEND_LIST (tlist
, output_list
);
11468 strvec_dispose (glob_array
);
11469 glob_array
= (char **)NULL
;
11475 dispose_words (disposables
);
11478 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
11480 return (output_list
);
11483 #if defined (BRACE_EXPANSION)
11485 brace_expand_word_list (tlist
, eflags
)
11489 register char **expansions
;
11491 WORD_LIST
*disposables
, *output_list
, *next
;
11495 for (disposables
= output_list
= (WORD_LIST
*)NULL
; tlist
; tlist
= next
)
11497 next
= tlist
->next
;
11499 if (tlist
->word
->flags
& W_NOBRACE
)
11501 /*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/
11502 PREPEND_LIST (tlist
, output_list
);
11506 if ((tlist
->word
->flags
& (W_COMPASSIGN
|W_ASSIGNARG
)) == (W_COMPASSIGN
|W_ASSIGNARG
))
11508 /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
11509 PREPEND_LIST (tlist
, output_list
);
11513 /* Only do brace expansion if the word has a brace character. If
11514 not, just add the word list element to BRACES and continue. In
11515 the common case, at least when running shell scripts, this will
11516 degenerate to a bunch of calls to `mbschr', and then what is
11517 basically a reversal of TLIST into BRACES, which is corrected
11518 by a call to REVERSE_LIST () on BRACES when the end of TLIST
11520 if (mbschr (tlist
->word
->word
, LBRACE
))
11522 expansions
= brace_expand (tlist
->word
->word
);
11524 for (eindex
= 0; temp_string
= expansions
[eindex
]; eindex
++)
11526 w
= alloc_word_desc ();
11527 w
->word
= temp_string
;
11529 /* If brace expansion didn't change the word, preserve
11530 the flags. We may want to preserve the flags
11531 unconditionally someday -- XXX */
11532 if (STREQ (temp_string
, tlist
->word
->word
))
11533 w
->flags
= tlist
->word
->flags
;
11535 w
= make_word_flags (w
, temp_string
);
11537 output_list
= make_word_list (w
, output_list
);
11541 /* Add TLIST to the list of words to be freed after brace
11542 expansion has been performed. */
11543 PREPEND_LIST (tlist
, disposables
);
11546 PREPEND_LIST (tlist
, output_list
);
11550 dispose_words (disposables
);
11553 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
11555 return (output_list
);
11559 #if defined (ARRAY_VARS)
11560 /* Take WORD, a compound array assignment, and internally run (for example),
11561 'declare -A w', where W is the variable name portion of WORD. OPTION is
11562 the list of options to supply to `declare'. CMD is the declaration command
11563 we are expanding right now; it's unused currently. */
11565 make_internal_declare (word
, option
, cmd
)
11574 w
= make_word (word
);
11576 t
= assignment (w
->word
, 0);
11577 if (w
->word
[t
] == '=')
11580 if (w
->word
[t
- 1] == '+') /* cut off any append op */
11581 w
->word
[t
- 1] = '\0';
11584 wl
= make_word_list (w
, (WORD_LIST
*)NULL
);
11585 wl
= make_word_list (make_word (option
), wl
);
11587 r
= declare_builtin (wl
);
11589 dispose_words (wl
);
11593 /* Expand VALUE in NAME[+]=( VALUE ) to a list of words. FLAGS is 1 if NAME
11594 is an associative array.
11596 If we are processing an indexed array, expand_compound_array_assignment
11597 will expand all the individual words and quote_compound_array_list will
11598 single-quote them. If we are processing an associative array, we use
11599 parse_string_to_word_list to split VALUE into a list of words instead of
11600 faking up a shell variable and calling expand_compound_array_assignment.
11601 expand_and_quote_assoc_word expands and single-quotes each word in VALUE
11602 together so we don't have problems finding the end of the subscript when
11605 Words in VALUE can be individual words, which are expanded and single-quoted,
11606 or words of the form [IND]=VALUE, which end up as explained below, as
11607 ['expanded-ind']='expanded-value'. */
11610 expand_oneword (value
, flags
)
11620 /* Indexed array */
11621 l
= expand_compound_array_assignment ((SHELL_VAR
*)NULL
, value
, flags
);
11622 /* Now we quote the results of the expansion above to prevent double
11624 quote_compound_array_list (l
, flags
);
11629 /* Associative array */
11630 l
= parse_string_to_word_list (value
, 1, "array assign");
11631 #if ASSOC_KVPAIR_ASSIGNMENT
11632 kvpair
= kvpair_assignment_p (l
);
11635 /* For associative arrays, with their arbitrary subscripts, we have to
11636 expand and quote in one step so we don't have to search for the
11637 closing right bracket more than once. */
11638 for (nl
= l
; nl
; nl
= nl
->next
)
11640 #if ASSOC_KVPAIR_ASSIGNMENT
11642 /* keys and values undergo the same set of expansions */
11643 t
= expand_and_quote_kvpair_word (nl
->word
->word
);
11646 if ((nl
->word
->flags
& W_ASSIGNMENT
) == 0)
11647 t
= sh_single_quote (nl
->word
->word
? nl
->word
->word
: "");
11649 t
= expand_and_quote_assoc_word (nl
->word
->word
, flags
);
11650 free (nl
->word
->word
);
11651 nl
->word
->word
= t
;
11657 /* Expand a single compound assignment argument to a declaration builtin.
11658 This word takes the form NAME[+]=( VALUE ). The NAME[+]= is passed through
11659 unchanged. The VALUE is expanded and each word in the result is single-
11660 quoted. Words of the form [key]=value end up as
11661 ['expanded-key']='expanded-value'. Associative arrays have special
11662 handling, see expand_oneword() above. The return value is
11663 NAME[+]=( expanded-and-quoted-VALUE ). */
11665 expand_compound_assignment_word (tlist
, flags
)
11671 char *value
, *temp
;
11673 /*itrace("expand_compound_assignment_word: original word = -%s-", tlist->word->word);*/
11674 t
= assignment (tlist
->word
->word
, 0);
11676 /* value doesn't have the open and close parens */
11678 value
= extract_array_assignment_list (tlist
->word
->word
+ t
+ 1, &oind
);
11679 /* This performs one round of expansion on the index/key and value and
11680 single-quotes each word in the result. */
11681 l
= expand_oneword (value
, flags
);
11684 value
= string_list (l
);
11687 wlen
= STRLEN (value
);
11689 /* Now, let's rebuild the string */
11690 temp
= xmalloc (t
+ 3 + wlen
+ 1); /* name[+]=(value) */
11691 memcpy (temp
, tlist
->word
->word
, ++t
);
11694 memcpy (temp
+ t
, value
, wlen
);
11698 /*itrace("expand_compound_assignment_word: reconstructed word = -%s-", temp);*/
11700 free (tlist
->word
->word
);
11701 tlist
->word
->word
= temp
;
11706 /* Expand and process an argument to a declaration command. We have already
11707 set flags in TLIST->word->flags depending on the declaration command
11708 (declare, local, etc.) and the options supplied to it (-a, -A, etc.).
11709 TLIST->word->word is of the form NAME[+]=( VALUE ).
11711 This does several things, all using pieces of other functions to get the
11712 evaluation sequence right. It's called for compound array assignments with
11713 the W_ASSIGNMENT flag set (basically, valid identifier names on the lhs).
11714 It parses out which flags need to be set for declare to create the variable
11715 correctly, then calls declare internally (make_internal_declare) to make
11716 sure the variable exists with the correct attributes. Before the variable
11717 is created, it calls expand_compound_assignment_word to expand VALUE to a
11718 list of words, appropriately quoted for further evaluation. This preserves
11719 the semantics of word-expansion-before-calling-builtins. Finally, it calls
11720 do_word_assignment to perform the expansion and assignment with the same
11721 expansion semantics as a standalone assignment statement (no word splitting,
11722 etc.) even though the word is single-quoted so all that needs to happen is
11725 expand_declaration_argument (tlist
, wcmd
)
11726 WORD_LIST
*tlist
, *wcmd
;
11728 char opts
[16], omap
[128];
11729 int t
, opti
, oind
, skip
, inheriting
;
11732 inheriting
= localvar_inherit
;
11734 if (tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSNGLOBAL
|W_CHKLOCAL
|W_ASSIGNARRAY
))
11735 opts
[opti
++] = '-';
11737 if ((tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSNGLOBAL
)) == (W_ASSIGNASSOC
|W_ASSNGLOBAL
))
11739 opts
[opti
++] = 'g';
11740 opts
[opti
++] = 'A';
11742 else if (tlist
->word
->flags
& W_ASSIGNASSOC
)
11744 opts
[opti
++] = 'A';
11746 else if ((tlist
->word
->flags
& (W_ASSIGNARRAY
|W_ASSNGLOBAL
)) == (W_ASSIGNARRAY
|W_ASSNGLOBAL
))
11748 opts
[opti
++] = 'g';
11749 opts
[opti
++] = 'a';
11751 else if (tlist
->word
->flags
& W_ASSIGNARRAY
)
11753 opts
[opti
++] = 'a';
11755 else if (tlist
->word
->flags
& W_ASSNGLOBAL
)
11756 opts
[opti
++] = 'g';
11758 if (tlist
->word
->flags
& W_CHKLOCAL
)
11759 opts
[opti
++] = 'G';
11761 /* If we have special handling note the integer attribute and others
11762 that transform the value upon assignment. What we do is take all
11763 of the option arguments and scan through them looking for options
11764 that cause such transformations, and add them to the `opts' array. */
11766 memset (omap
, '\0', sizeof (omap
));
11767 for (l
= wcmd
->next
; l
!= tlist
; l
= l
->next
)
11769 if (l
->word
->word
[0] != '-')
11770 break; /* non-option argument */
11771 if (l
->word
->word
[0] == '-' && l
->word
->word
[1] == '-' && l
->word
->word
[2] == 0)
11772 break; /* -- signals end of options */
11773 for (oind
= 1; l
->word
->word
[oind
]; oind
++)
11774 switch (l
->word
->word
[oind
])
11782 omap
[l
->word
->word
[oind
]] = 1;
11784 opts
[opti
++] = '-';
11791 for (oind
= 0; oind
< sizeof (omap
); oind
++)
11793 opts
[opti
++] = oind
;
11795 /* If there are no -a/-A options, but we have a compound assignment,
11796 we have a choice: we can set opts[0]='-', opts[1]='a', since the
11797 default is to create an indexed array, and call
11798 make_internal_declare with that, or we can just skip the -a and let
11799 declare_builtin deal with it. Once we're here, we're better set
11800 up for the latter, since we don't want to deal with looking up
11801 any existing variable here -- better to let declare_builtin do it.
11802 We need the variable created, though, especially if it's local, so
11803 we get the scoping right before we call do_word_assignment.
11804 To ensure that make_local_declare gets called, we add `--' if there
11805 aren't any options. */
11806 if ((tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSIGNARRAY
)) == 0)
11810 opts
[opti
++] = '-';
11811 opts
[opti
++] = '-';
11816 /* This isn't perfect, but it's a start. Improvements later. We expand
11817 tlist->word->word and single-quote the results to avoid multiple
11818 expansions by, say, do_assignment_internal(). We have to weigh the
11819 cost of reconstructing the compound assignment string with its single
11820 quoting and letting the declare builtin handle it. The single quotes
11821 will prevent any unwanted additional expansion or word splitting. */
11822 expand_compound_assignment_word (tlist
, (tlist
->word
->flags
& W_ASSIGNASSOC
) ? 1 : 0);
11827 t
= make_internal_declare (tlist
->word
->word
, opts
, wcmd
? wcmd
->word
->word
: (char *)0);
11828 if (t
!= EXECUTION_SUCCESS
)
11830 last_command_exit_value
= t
;
11831 if (tlist
->word
->flags
& W_FORCELOCAL
) /* non-fatal error */
11834 exp_jump_to_top_level (DISCARD
);
11840 t
= do_word_assignment (tlist
->word
, 0);
11843 last_command_exit_value
= EXECUTION_FAILURE
;
11844 exp_jump_to_top_level (DISCARD
);
11848 /* Now transform the word as ksh93 appears to do and go on */
11849 t
= assignment (tlist
->word
->word
, 0);
11850 tlist
->word
->word
[t
] = '\0';
11851 if (tlist
->word
->word
[t
- 1] == '+')
11852 tlist
->word
->word
[t
- 1] = '\0'; /* cut off append op */
11853 tlist
->word
->flags
&= ~(W_ASSIGNMENT
|W_NOSPLIT
|W_COMPASSIGN
|W_ASSIGNARG
|W_ASSIGNASSOC
|W_ASSIGNARRAY
);
11857 #endif /* ARRAY_VARS */
11860 shell_expand_word_list (tlist
, eflags
)
11864 WORD_LIST
*expanded
, *orig_list
, *new_list
, *next
, *temp_list
, *wcmd
;
11865 int expanded_something
, has_dollar_at
;
11867 /* We do tilde expansion all the time. This is what 1003.2 says. */
11868 wcmd
= new_list
= (WORD_LIST
*)NULL
;
11870 for (orig_list
= tlist
; tlist
; tlist
= next
)
11872 if (wcmd
== 0 && (tlist
->word
->flags
& W_ASSNBLTIN
))
11875 next
= tlist
->next
;
11877 #if defined (ARRAY_VARS)
11878 /* If this is a compound array assignment to a builtin that accepts
11879 such assignments (e.g., `declare'), take the assignment and perform
11880 it separately, handling the semantics of declarations inside shell
11881 functions. This avoids the double-evaluation of such arguments,
11882 because `declare' does some evaluation of compound assignments on
11884 if ((tlist
->word
->flags
& (W_COMPASSIGN
|W_ASSIGNARG
)) == (W_COMPASSIGN
|W_ASSIGNARG
))
11885 expand_declaration_argument (tlist
, wcmd
);
11888 expanded_something
= 0;
11889 expanded
= expand_word_internal
11890 (tlist
->word
, 0, 0, &has_dollar_at
, &expanded_something
);
11892 if (expanded
== &expand_word_error
|| expanded
== &expand_word_fatal
)
11894 /* By convention, each time this error is returned,
11895 tlist->word->word has already been freed. */
11896 tlist
->word
->word
= (char *)NULL
;
11898 /* Dispose our copy of the original list. */
11899 dispose_words (orig_list
);
11900 /* Dispose the new list we're building. */
11901 dispose_words (new_list
);
11903 last_command_exit_value
= EXECUTION_FAILURE
;
11904 if (expanded
== &expand_word_error
)
11905 exp_jump_to_top_level (DISCARD
);
11907 exp_jump_to_top_level (FORCE_EOF
);
11910 /* Don't split words marked W_NOSPLIT. */
11911 if (expanded_something
&& (tlist
->word
->flags
& W_NOSPLIT
) == 0)
11913 temp_list
= word_list_split (expanded
);
11914 dispose_words (expanded
);
11918 /* If no parameter expansion, command substitution, process
11919 substitution, or arithmetic substitution took place, then
11920 do not do word splitting. We still have to remove quoted
11921 null characters from the result. */
11922 word_list_remove_quoted_nulls (expanded
);
11923 temp_list
= expanded
;
11926 expanded
= REVERSE_LIST (temp_list
, WORD_LIST
*);
11927 new_list
= (WORD_LIST
*)list_append (expanded
, new_list
);
11931 dispose_words (orig_list
);
11934 new_list
= REVERSE_LIST (new_list
, WORD_LIST
*);
11939 /* The workhorse for expand_words () and expand_words_no_vars ().
11940 First arg is LIST, a WORD_LIST of words.
11941 Second arg EFLAGS is a flags word controlling which expansions are
11944 This does all of the substitutions: brace expansion, tilde expansion,
11945 parameter expansion, command substitution, arithmetic expansion,
11946 process substitution, word splitting, and pathname expansion, according
11947 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
11948 set, or for which no expansion is done, do not undergo word splitting.
11949 Words with the W_NOGLOB bit set do not undergo pathname expansion; words
11950 with W_NOBRACE set do not undergo brace expansion (see
11951 brace_expand_word_list above). */
11953 expand_word_list_internal (list
, eflags
)
11957 WORD_LIST
*new_list
, *temp_list
;
11961 tempenv_assign_error
= 0;
11963 return ((WORD_LIST
*)NULL
);
11965 garglist
= new_list
= copy_word_list (list
);
11966 if (eflags
& WEXP_VARASSIGN
)
11968 garglist
= new_list
= separate_out_assignments (new_list
);
11971 if (subst_assign_varlist
)
11973 /* All the words were variable assignments, so they are placed
11974 into the shell's environment. */
11975 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
11977 savecmd
= this_command_name
;
11978 this_command_name
= (char *)NULL
; /* no arithmetic errors */
11979 tint
= do_word_assignment (temp_list
->word
, 0);
11980 this_command_name
= savecmd
;
11981 /* Variable assignment errors in non-interactive shells
11982 running in Posix.2 mode cause the shell to exit, unless
11983 they are being run by the `command' builtin. */
11986 last_command_exit_value
= EXECUTION_FAILURE
;
11987 if (interactive_shell
== 0 && posixly_correct
&& executing_command_builtin
== 0)
11988 exp_jump_to_top_level (FORCE_EOF
);
11990 exp_jump_to_top_level (DISCARD
);
11993 dispose_words (subst_assign_varlist
);
11994 subst_assign_varlist
= (WORD_LIST
*)NULL
;
11996 return ((WORD_LIST
*)NULL
);
12000 /* Begin expanding the words that remain. The expansions take place on
12001 things that aren't really variable assignments. */
12003 #if defined (BRACE_EXPANSION)
12004 /* Do brace expansion on this word if there are any brace characters
12006 if ((eflags
& WEXP_BRACEEXP
) && brace_expansion
&& new_list
)
12007 new_list
= brace_expand_word_list (new_list
, eflags
);
12008 #endif /* BRACE_EXPANSION */
12010 /* Perform the `normal' shell expansions: tilde expansion, parameter and
12011 variable substitution, command substitution, arithmetic expansion,
12012 and word splitting. */
12013 new_list
= shell_expand_word_list (new_list
, eflags
);
12015 /* Okay, we're almost done. Now let's just do some filename
12019 if ((eflags
& WEXP_PATHEXP
) && disallow_filename_globbing
== 0)
12020 /* Glob expand the word list unless globbing has been disabled. */
12021 new_list
= glob_expand_word_list (new_list
, eflags
);
12023 /* Dequote the words, because we're not performing globbing. */
12024 new_list
= dequote_list (new_list
);
12027 if ((eflags
& WEXP_VARASSIGN
) && subst_assign_varlist
)
12029 sh_wassign_func_t
*assign_func
;
12030 int is_special_builtin
, is_builtin_or_func
;
12032 /* If the remainder of the words expand to nothing, Posix.2 requires
12033 that the variable and environment assignments affect the shell's
12035 assign_func
= new_list
? assign_in_env
: do_word_assignment
;
12036 tempenv_assign_error
= 0;
12038 is_builtin_or_func
= (new_list
&& new_list
->word
&& (find_shell_builtin (new_list
->word
->word
) || find_function (new_list
->word
->word
)));
12039 /* Posix says that special builtins exit if a variable assignment error
12040 occurs in an assignment preceding it. */
12041 is_special_builtin
= (posixly_correct
&& new_list
&& new_list
->word
&& find_special_builtin (new_list
->word
->word
));
12043 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
12045 savecmd
= this_command_name
;
12046 this_command_name
= (char *)NULL
;
12047 assigning_in_environment
= (assign_func
== assign_in_env
);
12048 tint
= (*assign_func
) (temp_list
->word
, is_builtin_or_func
);
12049 assigning_in_environment
= 0;
12050 this_command_name
= savecmd
;
12051 /* Variable assignment errors in non-interactive shells running
12052 in Posix.2 mode cause the shell to exit. */
12055 if (assign_func
== do_word_assignment
)
12057 last_command_exit_value
= EXECUTION_FAILURE
;
12058 if (interactive_shell
== 0 && posixly_correct
)
12059 exp_jump_to_top_level (FORCE_EOF
);
12061 exp_jump_to_top_level (DISCARD
);
12063 else if (interactive_shell
== 0 && is_special_builtin
)
12065 last_command_exit_value
= EXECUTION_FAILURE
;
12066 exp_jump_to_top_level (FORCE_EOF
);
12069 tempenv_assign_error
++;
12073 dispose_words (subst_assign_varlist
);
12074 subst_assign_varlist
= (WORD_LIST
*)NULL
;