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));
305 static char *process_substitute
PARAMS((char *, int));
307 static char *read_comsub
PARAMS((int, int, int, int *));
310 static arrayind_t array_length_reference
PARAMS((char *));
313 static int valid_brace_expansion_word
PARAMS((char *, int));
314 static int chk_atstar
PARAMS((char *, int, int, int *, int *));
315 static int chk_arithsub
PARAMS((const char *, int));
317 static WORD_DESC
*parameter_brace_expand_word
PARAMS((char *, int, int, int, arrayind_t
*));
318 static char *parameter_brace_find_indir
PARAMS((char *, int, int, int));
319 static WORD_DESC
*parameter_brace_expand_indir
PARAMS((char *, int, int, int, int *, int *));
320 static WORD_DESC
*parameter_brace_expand_rhs
PARAMS((char *, char *, int, int, int, int *, int *));
321 static void parameter_brace_expand_error
PARAMS((char *, char *, int));
323 static int valid_length_expression
PARAMS((char *));
324 static intmax_t parameter_brace_expand_length
PARAMS((char *));
326 static char *skiparith
PARAMS((char *, int));
327 static int verify_substring_values
PARAMS((SHELL_VAR
*, char *, char *, int, intmax_t *, intmax_t *));
328 static int get_var_and_type
PARAMS((char *, char *, arrayind_t
, int, int, SHELL_VAR
**, char **));
329 static char *mb_substring
PARAMS((char *, int, int));
330 static char *parameter_brace_substring
PARAMS((char *, char *, int, char *, int, int, int));
332 static int shouldexp_replacement
PARAMS((char *));
334 static char *pos_params_pat_subst
PARAMS((char *, char *, char *, int));
336 static char *parameter_brace_patsub
PARAMS((char *, char *, int, char *, int, int, int));
338 static char *pos_params_casemod
PARAMS((char *, char *, int, int));
339 static char *parameter_brace_casemod
PARAMS((char *, char *, int, int, char *, int, int, int));
341 static WORD_DESC
*parameter_brace_expand
PARAMS((char *, int *, int, int, int *, int *));
342 static WORD_DESC
*param_expand
PARAMS((char *, int *, int, int *, int *, int *, int *, int));
344 static WORD_LIST
*expand_word_internal
PARAMS((WORD_DESC
*, int, int, int *, int *));
346 static WORD_LIST
*word_list_split
PARAMS((WORD_LIST
*));
348 static void exp_jump_to_top_level
PARAMS((int));
350 static WORD_LIST
*separate_out_assignments
PARAMS((WORD_LIST
*));
351 static WORD_LIST
*glob_expand_word_list
PARAMS((WORD_LIST
*, int));
352 #ifdef BRACE_EXPANSION
353 static WORD_LIST
*brace_expand_word_list
PARAMS((WORD_LIST
*, int));
355 #if defined (ARRAY_VARS)
356 static int make_internal_declare
PARAMS((char *, char *, char *));
357 static void expand_compound_assignment_word
PARAMS((WORD_LIST
*, int));
358 static WORD_LIST
*expand_declaration_argument
PARAMS((WORD_LIST
*, WORD_LIST
*));
360 static WORD_LIST
*shell_expand_word_list
PARAMS((WORD_LIST
*, int));
361 static WORD_LIST
*expand_word_list_internal
PARAMS((WORD_LIST
*, int));
363 /* **************************************************************** */
365 /* Utility Functions */
367 /* **************************************************************** */
371 dump_word_flags (flags
)
377 fprintf (stderr
, "%d -> ", f
);
381 fprintf (stderr
, "W_ARRAYIND%s", f
? "|" : "");
383 if (f
& W_ASSIGNASSOC
)
386 fprintf (stderr
, "W_ASSIGNASSOC%s", f
? "|" : "");
388 if (f
& W_ASSIGNARRAY
)
391 fprintf (stderr
, "W_ASSIGNARRAY%s", f
? "|" : "");
393 if (f
& W_SAWQUOTEDNULL
)
395 f
&= ~W_SAWQUOTEDNULL
;
396 fprintf (stderr
, "W_SAWQUOTEDNULL%s", f
? "|" : "");
401 fprintf (stderr
, "W_NOPROCSUB%s", f
? "|" : "");
406 fprintf (stderr
, "W_DQUOTE%s", f
? "|" : "");
408 if (f
& W_HASQUOTEDNULL
)
410 f
&= ~W_HASQUOTEDNULL
;
411 fprintf (stderr
, "W_HASQUOTEDNULL%s", f
? "|" : "");
416 fprintf (stderr
, "W_ASSIGNARG%s", f
? "|" : "");
421 fprintf (stderr
, "W_ASSNBLTIN%s", f
? "|" : "");
423 if (f
& W_ASSNGLOBAL
)
426 fprintf (stderr
, "W_ASSNGLOBAL%s", f
? "|" : "");
428 if (f
& W_COMPASSIGN
)
431 fprintf (stderr
, "W_COMPASSIGN%s", f
? "|" : "");
436 fprintf (stderr
, "W_EXPANDRHS%s", f
? "|" : "");
441 fprintf (stderr
, "W_ITILDE%s", f
? "|" : "");
446 fprintf (stderr
, "W_NOTILDE%s", f
? "|" : "");
451 fprintf (stderr
, "W_ASSIGNRHS%s", f
? "|" : "");
453 if (f
& W_NOASSNTILDE
)
456 fprintf (stderr
, "W_NOASSNTILDE%s", f
? "|" : "");
461 fprintf (stderr
, "W_NOCOMSUB%s", f
? "|" : "");
463 if (f
& W_DOLLARSTAR
)
466 fprintf (stderr
, "W_DOLLARSTAR%s", f
? "|" : "");
471 fprintf (stderr
, "W_DOLLARAT%s", f
? "|" : "");
476 fprintf (stderr
, "W_TILDEEXP%s", f
? "|" : "");
481 fprintf (stderr
, "W_NOSPLIT2%s", f
? "|" : "");
486 fprintf (stderr
, "W_NOSPLIT%s", f
? "|" : "");
491 fprintf (stderr
, "W_NOBRACE%s", f
? "|" : "");
496 fprintf (stderr
, "W_NOGLOB%s", f
? "|" : "");
498 if (f
& W_SPLITSPACE
)
501 fprintf (stderr
, "W_SPLITSPACE%s", f
? "|" : "");
503 if (f
& W_ASSIGNMENT
)
506 fprintf (stderr
, "W_ASSIGNMENT%s", f
? "|" : "");
511 fprintf (stderr
, "W_QUOTED%s", f
? "|" : "");
516 fprintf (stderr
, "W_HASDOLLAR%s", f
? "|" : "");
521 fprintf (stderr
, "W_COMPLETE%s", f
? "|" : "");
526 fprintf (stderr
, "W_CHKLOCAL%s", f
? "|" : "");
528 if (f
& W_FORCELOCAL
)
531 fprintf (stderr
, "W_FORCELOCAL%s", f
? "|" : "");
534 fprintf (stderr
, "\n");
539 #ifdef INCLUDE_UNUSED
541 quoted_substring (string
, start
, end
)
546 register char *result
, *s
, *r
;
550 /* Move to string[start], skipping quoted characters. */
551 for (s
= string
, l
= 0; *s
&& l
< start
; )
563 r
= result
= (char *)xmalloc (2*len
+ 1); /* save room for quotes */
565 /* Copy LEN characters, including quote characters. */
567 for (l
= 0; l
< len
; s
++)
581 #ifdef INCLUDE_UNUSED
582 /* Return the length of S, skipping over quoted characters */
606 #ifdef INCLUDE_UNUSED
607 /* Find the first occurrence of character C in string S, obeying shell
608 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
609 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
610 escaped with CTLESC are skipped. */
612 quoted_strchr (s
, c
, flags
)
620 if (((flags
& ST_BACKSL
) && *p
== '\\')
621 || ((flags
& ST_CTLESC
) && *p
== CTLESC
))
625 return ((char *)NULL
);
631 return ((char *)NULL
);
634 /* Return 1 if CHARACTER appears in an unquoted portion of
635 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
637 unquoted_member (character
, string
)
645 slen
= strlen (string
);
647 while (c
= string
[sindex
])
655 ADVANCE_CHAR (string
, slen
, sindex
);
661 ADVANCE_CHAR (string
, slen
, sindex
);
665 sindex
= skip_single_quoted (string
, slen
, ++sindex
, 0);
669 sindex
= skip_double_quoted (string
, slen
, ++sindex
, 0);
676 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
678 unquoted_substring (substr
, string
)
679 char *substr
, *string
;
682 int sindex
, c
, sublen
;
685 if (substr
== 0 || *substr
== '\0')
688 slen
= strlen (string
);
689 sublen
= strlen (substr
);
690 for (sindex
= 0; c
= string
[sindex
]; )
692 if (STREQN (string
+ sindex
, substr
, sublen
))
700 ADVANCE_CHAR (string
, slen
, sindex
);
704 sindex
= skip_single_quoted (string
, slen
, ++sindex
, 0);
708 sindex
= skip_double_quoted (string
, slen
, ++sindex
, 0);
712 ADVANCE_CHAR (string
, slen
, sindex
);
720 /* Most of the substitutions must be done in parallel. In order
721 to avoid using tons of unclear goto's, I have some functions
722 for manipulating malloc'ed strings. They all take INDX, a
723 pointer to an integer which is the offset into the string
724 where manipulation is taking place. They also take SIZE, a
725 pointer to an integer which is the current length of the
726 character array for this string. */
728 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
729 of space allocated to TARGET. SOURCE can be NULL, in which
730 case nothing happens. Gets rid of SOURCE by freeing it.
731 Returns TARGET in case the location has changed. */
733 sub_append_string (source
, target
, indx
, size
)
734 char *source
, *target
;
743 srclen
= STRLEN (source
);
744 if (srclen
>= (int)(*size
- *indx
))
747 n
= (n
+ DEFAULT_ARRAY_SIZE
) - (n
% DEFAULT_ARRAY_SIZE
);
748 target
= (char *)xrealloc (target
, (*size
= n
));
751 FASTCOPY (source
, target
+ *indx
, srclen
);
753 target
[*indx
] = '\0';
762 /* Append the textual representation of NUMBER to TARGET.
763 INDX and SIZE are as in SUB_APPEND_STRING. */
765 sub_append_number (number
, target
, indx
, size
)
773 temp
= itos (number
);
774 return (sub_append_string (temp
, target
, indx
, size
));
778 /* Extract a substring from STRING, starting at SINDEX and ending with
779 one of the characters in CHARLIST. Don't make the ending character
780 part of the string. Leave SINDEX pointing at the ending character.
781 Understand about backslashes in the string. If (flags & SX_VARNAME)
782 is non-zero, and array variables have been compiled into the shell,
783 everything between a `[' and a corresponding `]' is skipped over.
784 If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
785 update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
786 contain a closing character from CHARLIST. */
788 string_extract (string
, sindex
, charlist
, flags
)
800 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 0;
803 while (c
= string
[i
])
812 #if defined (ARRAY_VARS)
813 else if ((flags
& SX_VARNAME
) && c
== LBRACK
)
816 /* If this is an array subscript, skip over it and continue. */
817 ni
= skipsubscript (string
, i
, 0);
818 if (string
[ni
] == RBRACK
)
822 else if (MEMBER (c
, charlist
))
828 ADVANCE_CHAR (string
, slen
, i
);
831 /* If we had to have a matching delimiter and didn't find one, return an
832 error and let the caller deal with it. */
833 if ((flags
& SX_REQMATCH
) && found
== 0)
836 return (&extract_string_error
);
839 temp
= (flags
& SX_NOALLOC
) ? (char *)NULL
: substring (string
, *sindex
, i
);
845 /* Extract the contents of STRING as if it is enclosed in double quotes.
846 SINDEX, when passed in, is the offset of the character immediately
847 following the opening double quote; on exit, SINDEX is left pointing after
848 the closing double quote. If STRIPDQ is non-zero, unquoted double
849 quotes are stripped and the string is terminated by a null byte.
850 Backslashes between the embedded double quotes are processed. If STRIPDQ
851 is zero, an unquoted `"' terminates the string. */
853 string_extract_double_quoted (string
, sindex
, flags
)
861 char *temp
, *ret
; /* The new string we return. */
862 int pass_next
, backquote
, si
; /* State variables for the machine. */
867 slen
= strlen (string
+ *sindex
) + *sindex
;
868 send
= string
+ slen
;
870 stripdq
= (flags
& SX_STRIPDQ
);
872 pass_next
= backquote
= dquote
= 0;
873 temp
= (char *)xmalloc (1 + slen
- *sindex
);
877 while (c
= string
[i
])
879 /* Process a character that was quoted by a backslash. */
882 /* XXX - take another look at this in light of Interp 221 */
885 ``The backslash shall retain its special meaning as an escape
886 character only when followed by one of the characters:
889 If STRIPDQ is zero, we handle the double quotes here and let
890 expand_word_internal handle the rest. If STRIPDQ is non-zero,
891 we have already been through one round of backslash stripping,
892 and want to strip these backslashes only if DQUOTE is non-zero,
893 indicating that we are inside an embedded double-quoted string. */
895 /* If we are in an embedded quoted string, then don't strip
896 backslashes before characters for which the backslash
897 retains its special meaning, but remove backslashes in
898 front of other characters. If we are not in an
899 embedded quoted string, don't strip backslashes at all.
900 This mess is necessary because the string was already
901 surrounded by double quotes (and sh has some really weird
903 The returned string will be run through expansion as if
904 it were double-quoted. */
905 if ((stripdq
== 0 && c
!= '"') ||
906 (stripdq
&& ((dquote
&& (sh_syntaxtab
[c
] & CBSDQUOTE
)) || dquote
== 0)))
911 COPY_CHAR_I (temp
, j
, string
, send
, i
);
915 /* A backslash protects the next character. The code just above
916 handles preserving the backslash in front of any character but
925 /* Inside backquotes, ``the portion of the quoted string from the
926 initial backquote and the characters up to the next backquote
927 that is not preceded by a backslash, having escape characters
928 removed, defines that command''. */
933 temp
[j
++] = c
; /* COPY_CHAR_I? */
946 /* Pass everything between `$(' and the matching `)' or a quoted
947 ${ ... } pair through according to the Posix.2 specification. */
948 if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
953 if (string
[i
+ 1] == LPAREN
)
954 ret
= extract_command_subst (string
, &si
, (flags
& SX_COMPLETE
));
956 ret
= extract_dollar_brace_string (string
, &si
, Q_DOUBLE_QUOTES
, 0);
959 temp
[j
++] = string
[i
+ 1];
961 /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
963 if (ret
== 0 && no_longjmp_on_fatal_error
)
966 ret
= string
+ i
+ 2;
969 /* XXX - CHECK_STRING_OVERRUN here? */
970 for (t
= 0; ret
[t
]; t
++, j
++)
972 temp
[j
] = string
[si
];
974 if (si
< i
+ 2) /* we went back? */
989 /* Add any character but a double quote to the quoted string we're
992 goto add_one_character
;
1006 /* Point to after the closing quote. */
1014 /* This should really be another option to string_extract_double_quoted. */
1016 skip_double_quoted (string
, slen
, sind
, flags
)
1024 int pass_next
, backquote
, si
;
1027 pass_next
= backquote
= 0;
1029 while (c
= string
[i
])
1034 ADVANCE_CHAR (string
, slen
, i
);
1047 ADVANCE_CHAR (string
, slen
, i
);
1056 else if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
1059 if (string
[i
+ 1] == LPAREN
)
1060 ret
= extract_command_subst (string
, &si
, SX_NOALLOC
|(flags
&SX_COMPLETE
));
1062 ret
= extract_dollar_brace_string (string
, &si
, Q_DOUBLE_QUOTES
, SX_NOALLOC
);
1064 /* These can consume the entire string if they are unterminated */
1065 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1072 ADVANCE_CHAR (string
, slen
, i
);
1085 /* Extract the contents of STRING as if it is enclosed in single quotes.
1086 SINDEX, when passed in, is the offset of the character immediately
1087 following the opening single quote; on exit, SINDEX is left pointing after
1088 the closing single quote. */
1089 static inline char *
1090 string_extract_single_quoted (string
, sindex
)
1099 /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
1100 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 0;
1102 while (string
[i
] && string
[i
] != '\'')
1103 ADVANCE_CHAR (string
, slen
, i
);
1105 t
= substring (string
, *sindex
, i
);
1114 /* Skip over a single-quoted string. We overload the SX_COMPLETE flag to mean
1115 that we are splitting out words for completion and have encountered a $'...'
1116 string, which allows backslash-escaped single quotes. */
1118 skip_single_quoted (string
, slen
, sind
, flags
)
1128 while (string
[c
] && string
[c
] != '\'')
1130 if ((flags
& SX_COMPLETE
) && string
[c
] == '\\' && string
[c
+1] == '\'' && string
[c
+2])
1131 ADVANCE_CHAR (string
, slen
, c
);
1132 ADVANCE_CHAR (string
, slen
, c
);
1140 /* Just like string_extract, but doesn't hack backslashes or any of
1141 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
1143 string_extract_verbatim (string
, slen
, sindex
, charlist
, flags
)
1151 #if defined (HANDLE_MULTIBYTE)
1158 if ((flags
& SX_NOCTLESC
) && charlist
[0] == '\'' && charlist
[1] == '\0')
1160 temp
= string_extract_single_quoted (string
, sindex
);
1161 --*sindex
; /* leave *sindex at separator character */
1165 /* This can never be called with charlist == NULL. If *charlist == NULL,
1166 we can skip the loop and just return a copy of the string, updating
1170 temp
= string
+ *sindex
;
1171 c
= (*sindex
== 0) ? slen
: STRLEN (temp
);
1172 temp
= savestring (temp
);
1178 #if defined (HANDLE_MULTIBYTE)
1181 while (c
= string
[i
])
1183 #if defined (HANDLE_MULTIBYTE)
1186 if ((flags
& SX_NOCTLESC
) == 0 && c
== CTLESC
)
1189 CHECK_STRING_OVERRUN (i
, i
, slen
, c
);
1192 /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
1193 through, to protect the CTLNULs from later calls to
1194 remove_quoted_nulls. */
1195 else if ((flags
& SX_NOESCCTLNUL
) == 0 && c
== CTLESC
&& string
[i
+1] == CTLNUL
)
1198 CHECK_STRING_OVERRUN (i
, i
, slen
, c
);
1202 #if defined (HANDLE_MULTIBYTE)
1203 if (locale_utf8locale
&& slen
> i
&& UTF8_SINGLEBYTE (string
[i
]))
1204 mblength
= (string
[i
] != 0) ? 1 : 0;
1206 mblength
= MBLEN (string
+ i
, slen
- i
);
1210 mblength
= mbtowc (&wc
, string
+ i
, slen
- i
);
1211 if (MB_INVALIDCH (mblength
))
1213 if (MEMBER (c
, charlist
))
1221 len
= mbstowcs (wcharlist
, charlist
, 0);
1224 wcharlist
= (wchar_t *)xmalloc (sizeof (wchar_t) * (len
+ 1));
1225 mbstowcs (wcharlist
, charlist
, len
+ 1);
1228 if (wcschr (wcharlist
, wc
))
1234 if (MEMBER (c
, charlist
))
1237 ADVANCE_CHAR (string
, slen
, i
);
1240 #if defined (HANDLE_MULTIBYTE)
1244 temp
= substring (string
, *sindex
, i
);
1250 /* Extract the $( construct in STRING, and return a new string.
1251 Start extracting at (SINDEX) as if we had just seen "$(".
1252 Make (SINDEX) get the position of the matching ")". )
1253 XFLAGS is additional flags to pass to other extraction functions. */
1255 extract_command_subst (string
, sindex
, xflags
)
1262 if (string
[*sindex
] == LPAREN
|| (xflags
& SX_COMPLETE
))
1263 return (extract_delimited_string (string
, sindex
, "$(", "(", ")", xflags
|SX_COMMAND
)); /*)*/
1266 xflags
|= (no_longjmp_on_fatal_error
? SX_NOLONGJMP
: 0);
1267 ret
= xparse_dolparen (string
, string
+*sindex
, sindex
, xflags
);
1272 /* Extract the $[ construct in STRING, and return a new string. (])
1273 Start extracting at (SINDEX) as if we had just seen "$[".
1274 Make (SINDEX) get the position of the matching "]". */
1276 extract_arithmetic_subst (string
, sindex
)
1280 return (extract_delimited_string (string
, sindex
, "$[", "[", "]", 0)); /*]*/
1283 #if defined (PROCESS_SUBSTITUTION)
1284 /* Extract the <( or >( construct in STRING, and return a new string.
1285 Start extracting at (SINDEX) as if we had just seen "<(".
1286 Make (SINDEX) get the position of the matching ")". */ /*))*/
1288 extract_process_subst (string
, starter
, sindex
, xflags
)
1295 /* XXX - check xflags&SX_COMPLETE here? */
1296 return (extract_delimited_string (string
, sindex
, starter
, "(", ")", SX_COMMAND
));
1298 xflags
|= (no_longjmp_on_fatal_error
? SX_NOLONGJMP
: 0);
1299 return (xparse_dolparen (string
, string
+*sindex
, sindex
, xflags
));
1302 #endif /* PROCESS_SUBSTITUTION */
1304 #if defined (ARRAY_VARS)
1305 /* This can be fooled by unquoted right parens in the passed string. If
1306 each caller verifies that the last character in STRING is a right paren,
1307 we don't even need to call extract_delimited_string. */
1309 extract_array_assignment_list (string
, sindex
)
1316 slen
= strlen (string
);
1317 if (string
[slen
- 1] == RPAREN
)
1319 ret
= substring (string
, *sindex
, slen
- 1);
1327 /* Extract and create a new string from the contents of STRING, a
1328 character string delimited with OPENER and CLOSER. SINDEX is
1329 the address of an int describing the current offset in STRING;
1330 it should point to just after the first OPENER found. On exit,
1331 SINDEX gets the position of the last character of the matching CLOSER.
1332 If OPENER is more than a single character, ALT_OPENER, if non-null,
1333 contains a character string that can also match CLOSER and thus
1334 needs to be skipped. */
1336 extract_delimited_string (string
, sindex
, opener
, alt_opener
, closer
, flags
)
1339 char *opener
, *alt_opener
, *closer
;
1345 int pass_character
, nesting_level
, in_comment
;
1346 int len_closer
, len_opener
, len_alt_opener
;
1349 slen
= strlen (string
+ *sindex
) + *sindex
;
1350 len_opener
= STRLEN (opener
);
1351 len_alt_opener
= STRLEN (alt_opener
);
1352 len_closer
= STRLEN (closer
);
1354 pass_character
= in_comment
= 0;
1359 while (nesting_level
)
1363 /* If a recursive call or a call to ADVANCE_CHAR leaves the index beyond
1364 the end of the string, catch it and cut the loop. */
1368 c
= string
[i
= slen
];
1379 ADVANCE_CHAR (string
, slen
, i
);
1383 if (pass_character
) /* previous char was backslash */
1386 ADVANCE_CHAR (string
, slen
, i
);
1390 /* Not exactly right yet; should handle shell metacharacters and
1391 multibyte characters, too. See COMMENT_BEGIN define in parse.y */
1392 if ((flags
& SX_COMMAND
) && c
== '#' && (i
== 0 || string
[i
- 1] == '\n' || shellblank (string
[i
- 1])))
1395 ADVANCE_CHAR (string
, slen
, i
);
1399 if (c
== CTLESC
|| c
== '\\')
1406 /* Process a nested command substitution, but only if we're parsing an
1407 arithmetic substitution. */
1408 if ((flags
& SX_COMMAND
) && string
[i
] == '$' && string
[i
+1] == LPAREN
)
1411 t
= extract_command_subst (string
, &si
, flags
|SX_NOALLOC
);
1412 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1417 /* Process a nested OPENER. */
1418 if (STREQN (string
+ i
, opener
, len_opener
))
1420 si
= i
+ len_opener
;
1421 t
= extract_delimited_string (string
, &si
, opener
, alt_opener
, closer
, flags
|SX_NOALLOC
);
1422 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1427 /* Process a nested ALT_OPENER */
1428 if (len_alt_opener
&& STREQN (string
+ i
, alt_opener
, len_alt_opener
))
1430 si
= i
+ len_alt_opener
;
1431 t
= extract_delimited_string (string
, &si
, alt_opener
, alt_opener
, closer
, flags
|SX_NOALLOC
);
1432 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1437 /* If the current substring terminates the delimited string, decrement
1438 the nesting level. */
1439 if (STREQN (string
+ i
, closer
, len_closer
))
1441 i
+= len_closer
- 1; /* move to last byte of the closer */
1443 if (nesting_level
== 0)
1447 /* Pass old-style command substitution through verbatim. */
1451 t
= string_extract (string
, &si
, "`", flags
|SX_NOALLOC
);
1452 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1457 /* Pass single-quoted and double-quoted strings through verbatim. */
1458 if (c
== '\'' || c
== '"')
1461 i
= (c
== '\'') ? skip_single_quoted (string
, slen
, si
, 0)
1462 : skip_double_quoted (string
, slen
, si
, 0);
1466 /* move past this character, which was not special. */
1467 ADVANCE_CHAR (string
, slen
, i
);
1470 if (c
== 0 && nesting_level
)
1472 if (no_longjmp_on_fatal_error
== 0)
1474 last_command_exit_value
= EXECUTION_FAILURE
;
1475 report_error (_("bad substitution: no closing `%s' in %s"), closer
, string
);
1476 exp_jump_to_top_level (DISCARD
);
1481 return (char *)NULL
;
1485 si
= i
- *sindex
- len_closer
+ 1;
1486 if (flags
& SX_NOALLOC
)
1487 result
= (char *)NULL
;
1490 result
= (char *)xmalloc (1 + si
);
1491 strncpy (result
, string
+ *sindex
, si
);
1499 /* Extract a parameter expansion expression within ${ and } from STRING.
1500 Obey the Posix.2 rules for finding the ending `}': count braces while
1501 skipping over enclosed quoted strings and command substitutions.
1502 SINDEX is the address of an int describing the current offset in STRING;
1503 it should point to just after the first `{' found. On exit, SINDEX
1504 gets the position of the matching `}'. QUOTED is non-zero if this
1505 occurs inside double quotes. */
1506 /* XXX -- this is very similar to extract_delimited_string -- XXX */
1508 extract_dollar_brace_string (string
, sindex
, quoted
, flags
)
1510 int *sindex
, quoted
, flags
;
1514 int pass_character
, nesting_level
, si
, dolbrace_state
;
1520 slen
= strlen (string
+ *sindex
) + *sindex
;
1522 /* The handling of dolbrace_state needs to agree with the code in parse.y:
1523 parse_matched_pair(). The different initial value is to handle the
1524 case where this function is called to parse the word in
1525 ${param op word} (SX_WORD). */
1526 dolbrace_state
= (flags
& SX_WORD
) ? DOLBRACE_WORD
: DOLBRACE_PARAM
;
1527 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && (flags
& SX_POSIXEXP
))
1528 dolbrace_state
= DOLBRACE_QUOTE
;
1531 while (c
= string
[i
])
1536 ADVANCE_CHAR (string
, slen
, i
);
1540 /* CTLESCs and backslashes quote the next character. */
1541 if (c
== CTLESC
|| c
== '\\')
1548 if (string
[i
] == '$' && string
[i
+1] == LBRACE
)
1558 if (nesting_level
== 0)
1564 /* Pass the contents of old-style command substitutions through
1569 t
= string_extract (string
, &si
, "`", flags
|SX_NOALLOC
);
1571 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1577 /* Pass the contents of new-style command substitutions and
1578 arithmetic substitutions through verbatim. */
1579 if (string
[i
] == '$' && string
[i
+1] == LPAREN
)
1582 t
= extract_command_subst (string
, &si
, flags
|SX_NOALLOC
);
1584 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1590 #if defined (PROCESS_SUBSTITUTION)
1591 /* Technically this should only work at the start of a word */
1592 if ((string
[i
] == '<' || string
[i
] == '>') && string
[i
+1] == LPAREN
)
1595 t
= extract_process_subst (string
, (string
[i
] == '<' ? "<(" : ">)"), &si
, flags
|SX_NOALLOC
);
1597 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1604 /* Pass the contents of double-quoted strings through verbatim. */
1608 i
= skip_double_quoted (string
, slen
, si
, 0);
1609 /* skip_XXX_quoted leaves index one past close quote */
1615 /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/
1616 if (posixly_correct
&& shell_compatibility_level
> 42 && dolbrace_state
!= DOLBRACE_QUOTE
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
1617 ADVANCE_CHAR (string
, slen
, i
);
1621 i
= skip_single_quoted (string
, slen
, si
, 0);
1627 #if defined (ARRAY_VARS)
1628 if (c
== LBRACK
&& dolbrace_state
== DOLBRACE_PARAM
)
1630 si
= skipsubscript (string
, i
, 0);
1631 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1632 if (string
[si
] == RBRACK
)
1637 /* move past this character, which was not special. */
1638 ADVANCE_CHAR (string
, slen
, i
);
1640 /* This logic must agree with parse.y:parse_matched_pair, since they
1641 share the same defines. */
1642 if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '%' && (i
- *sindex
) > 1)
1643 dolbrace_state
= DOLBRACE_QUOTE
;
1644 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '#' && (i
- *sindex
) > 1)
1645 dolbrace_state
= DOLBRACE_QUOTE
;
1646 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '/' && (i
- *sindex
) > 1)
1647 dolbrace_state
= DOLBRACE_QUOTE2
; /* XXX */
1648 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '^' && (i
- *sindex
) > 1)
1649 dolbrace_state
= DOLBRACE_QUOTE
;
1650 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== ',' && (i
- *sindex
) > 1)
1651 dolbrace_state
= DOLBRACE_QUOTE
;
1652 /* This is intended to handle all of the [:]op expansions and the substring/
1653 length/pattern removal/pattern substitution expansions. */
1654 else if (dolbrace_state
== DOLBRACE_PARAM
&& strchr ("#%^,~:-=?+/", c
) != 0)
1655 dolbrace_state
= DOLBRACE_OP
;
1656 else if (dolbrace_state
== DOLBRACE_OP
&& strchr ("#%^,~:-=?+/", c
) == 0)
1657 dolbrace_state
= DOLBRACE_WORD
;
1660 if (c
== 0 && nesting_level
)
1662 if (no_longjmp_on_fatal_error
== 0)
1664 last_command_exit_value
= EXECUTION_FAILURE
;
1665 report_error (_("bad substitution: no closing `%s' in %s"), "}", string
);
1666 exp_jump_to_top_level (DISCARD
);
1671 return ((char *)NULL
);
1675 result
= (flags
& SX_NOALLOC
) ? (char *)NULL
: substring (string
, *sindex
, i
);
1681 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1682 STRING, and returns a pointer to it. */
1684 de_backslash (string
)
1687 register size_t slen
;
1688 register int i
, j
, prev_i
;
1691 slen
= strlen (string
);
1694 /* Loop copying string[i] to string[j], i >= j. */
1697 if (string
[i
] == '\\' && (string
[i
+ 1] == '`' || string
[i
+ 1] == '\\' ||
1698 string
[i
+ 1] == '$'))
1701 ADVANCE_CHAR (string
, slen
, i
);
1703 do string
[j
++] = string
[prev_i
++]; while (prev_i
< i
);
1714 /* Replace instances of \! in a string with !. */
1716 unquote_bang (string
)
1720 register char *temp
;
1722 temp
= (char *)xmalloc (1 + strlen (string
));
1724 for (i
= 0, j
= 0; (temp
[j
] = string
[i
]); i
++, j
++)
1726 if (string
[i
] == '\\' && string
[i
+ 1] == '!')
1732 strcpy (string
, temp
);
1737 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = oldjmp; return (x); } while (0)
1739 /* This function assumes s[i] == open; returns with s[ret] == close; used to
1740 parse array subscripts. FLAGS & 1 means to not attempt to skip over
1741 matched pairs of quotes or backquotes, or skip word expansions; it is
1742 intended to be used after expansion has been performed and during final
1743 assignment parsing (see arrayfunc.c:assign_compound_array_list()) or
1744 during execution by a builtin which has already undergone word expansion. */
1746 skip_matched_pair (string
, start
, open
, close
, flags
)
1748 int start
, open
, close
, flags
;
1750 int i
, pass_next
, backq
, si
, c
, count
, oldjmp
;
1755 slen
= strlen (string
+ start
) + start
;
1756 oldjmp
= no_longjmp_on_fatal_error
;
1757 no_longjmp_on_fatal_error
= 1;
1759 i
= start
+ 1; /* skip over leading bracket */
1761 pass_next
= backq
= 0;
1762 ss
= (char *)string
;
1763 while (c
= string
[i
])
1770 ADVANCE_CHAR (string
, slen
, i
);
1773 else if ((flags
& 1) == 0 && c
== '\\')
1783 ADVANCE_CHAR (string
, slen
, i
);
1786 else if ((flags
& 1) == 0 && c
== '`')
1792 else if ((flags
& 1) == 0 && c
== open
)
1798 else if (c
== close
)
1806 else if ((flags
& 1) == 0 && (c
== '\'' || c
== '"'))
1808 i
= (c
== '\'') ? skip_single_quoted (ss
, slen
, ++i
, 0)
1809 : skip_double_quoted (ss
, slen
, ++i
, 0);
1810 /* no increment, the skip functions increment past the closing quote. */
1812 else if ((flags
&1) == 0 && c
== '$' && (string
[i
+1] == LPAREN
|| string
[i
+1] == LBRACE
))
1815 if (string
[si
] == '\0')
1818 /* XXX - extract_command_subst here? */
1819 if (string
[i
+1] == LPAREN
)
1820 temp
= extract_delimited_string (ss
, &si
, "$(", "(", ")", SX_NOALLOC
|SX_COMMAND
); /* ) */
1822 temp
= extract_dollar_brace_string (ss
, &si
, 0, SX_NOALLOC
);
1824 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1827 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1833 ADVANCE_CHAR (string
, slen
, i
);
1839 #if defined (ARRAY_VARS)
1840 /* Flags has 1 as a reserved value, since skip_matched_pair uses it for
1841 skipping over quoted strings and taking the first instance of the
1842 closing character. */
1844 skipsubscript (string
, start
, flags
)
1848 return (skip_matched_pair (string
, start
, '[', ']', flags
));
1852 /* Skip characters in STRING until we find a character in DELIMS, and return
1853 the index of that character. START is the index into string at which we
1854 begin. This is similar in spirit to strpbrk, but it returns an index into
1855 STRING and takes a starting index. This little piece of code knows quite
1856 a lot of shell syntax. It's very similar to skip_double_quoted and other
1857 functions of that ilk. */
1859 skip_to_delim (string
, start
, delims
, flags
)
1865 int i
, pass_next
, backq
, dquote
, si
, c
, oldjmp
;
1866 int invert
, skipquote
, skipcmd
, noprocsub
, completeflag
;
1867 int arithexp
, skipcol
;
1869 char *temp
, open
[3];
1872 slen
= strlen (string
+ start
) + start
;
1873 oldjmp
= no_longjmp_on_fatal_error
;
1874 if (flags
& SD_NOJMP
)
1875 no_longjmp_on_fatal_error
= 1;
1876 invert
= (flags
& SD_INVERT
);
1877 skipcmd
= (flags
& SD_NOSKIPCMD
) == 0;
1878 noprocsub
= (flags
& SD_NOPROCSUB
);
1879 completeflag
= (flags
& SD_COMPLETE
) ? SX_COMPLETE
: 0;
1881 arithexp
= (flags
& SD_ARITHEXP
);
1885 pass_next
= backq
= dquote
= 0;
1886 while (c
= string
[i
])
1888 /* If this is non-zero, we should not let quote characters be delimiters
1889 and the current character is a single or double quote. We should not
1890 test whether or not it's a delimiter until after we skip single- or
1891 double-quoted strings. */
1892 skipquote
= ((flags
& SD_NOQUOTEDELIM
) && (c
== '\'' || c
=='"'));
1898 ADVANCE_CHAR (string
, slen
, i
);
1911 ADVANCE_CHAR (string
, slen
, i
);
1920 else if (arithexp
&& skipcol
&& c
== ':')
1926 else if (arithexp
&& c
== '?')
1932 else if (skipquote
== 0 && invert
== 0 && member (c
, delims
))
1934 /* the usual case is to use skip_xxx_quoted, but we don't skip over double
1935 quoted strings when looking for the history expansion character as a
1937 /* special case for programmable completion which takes place before
1938 parser converts backslash-escaped single quotes between $'...' to
1939 `regular' single-quoted strings. */
1940 else if (completeflag
&& i
> 0 && string
[i
-1] == '$' && c
== '\'')
1941 i
= skip_single_quoted (string
, slen
, ++i
, SX_COMPLETE
);
1943 i
= skip_single_quoted (string
, slen
, ++i
, 0);
1945 i
= skip_double_quoted (string
, slen
, ++i
, completeflag
);
1946 else if (c
== LPAREN
&& arithexp
)
1949 if (string
[si
] == '\0')
1952 temp
= extract_delimited_string (string
, &si
, "(", "(", ")", SX_NOALLOC
); /* ) */
1954 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1959 else if (c
== '$' && ((skipcmd
&& string
[i
+1] == LPAREN
) || string
[i
+1] == LBRACE
))
1962 if (string
[si
] == '\0')
1965 if (string
[i
+1] == LPAREN
)
1966 temp
= extract_delimited_string (string
, &si
, "$(", "(", ")", SX_NOALLOC
|SX_COMMAND
); /* ) */
1968 temp
= extract_dollar_brace_string (string
, &si
, 0, SX_NOALLOC
);
1969 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1971 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1976 #if defined (PROCESS_SUBSTITUTION)
1977 else if (skipcmd
&& noprocsub
== 0 && (c
== '<' || c
== '>') && string
[i
+1] == LPAREN
)
1980 if (string
[si
] == '\0')
1983 temp
= extract_delimited_string (string
, &si
, (c
== '<') ? "<(" : ">(", "(", ")", SX_COMMAND
|SX_NOALLOC
); /* )) */
1984 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1986 if (string
[i
] == '\0')
1991 #endif /* PROCESS_SUBSTITUTION */
1992 #if defined (EXTENDED_GLOB)
1993 else if ((flags
& SD_EXTGLOB
) && extended_glob
&& string
[i
+1] == LPAREN
&& member (c
, "?*+!@"))
1996 if (string
[si
] == '\0')
2002 temp
= extract_delimited_string (string
, &si
, open
, "(", ")", SX_NOALLOC
); /* ) */
2004 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
2006 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
2012 else if ((flags
& SD_GLOB
) && c
== LBRACK
)
2015 if (string
[si
] == '\0')
2018 temp
= extract_delimited_string (string
, &si
, "[", "[", "]", SX_NOALLOC
); /* ] */
2021 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
2026 else if ((skipquote
|| invert
) && (member (c
, delims
) == 0))
2029 ADVANCE_CHAR (string
, slen
, i
);
2035 #if defined (BANG_HISTORY)
2036 /* Skip to the history expansion character (delims[0]), paying attention to
2037 quoted strings and command and process substitution. This is a stripped-
2038 down version of skip_to_delims. The essential difference is that this
2039 resets the quoting state when starting a command substitution */
2041 skip_to_histexp (string
, start
, delims
, flags
)
2047 int i
, pass_next
, backq
, dquote
, c
, oldjmp
;
2048 int histexp_comsub
, histexp_backq
, old_dquote
;
2052 slen
= strlen (string
+ start
) + start
;
2053 oldjmp
= no_longjmp_on_fatal_error
;
2054 if (flags
& SD_NOJMP
)
2055 no_longjmp_on_fatal_error
= 1;
2057 histexp_comsub
= histexp_backq
= old_dquote
= 0;
2060 pass_next
= backq
= dquote
= 0;
2061 while (c
= string
[i
])
2068 ADVANCE_CHAR (string
, slen
, i
);
2077 else if (backq
&& c
== '`')
2081 dquote
= old_dquote
;
2089 old_dquote
= dquote
; /* simple - one level for now */
2094 /* When in double quotes, act as if the double quote is a member of
2095 history_no_expand_chars, like the history library does */
2096 else if (dquote
&& c
== delims
[0] && string
[i
+1] == '"')
2101 else if (c
== delims
[0])
2103 /* the usual case is to use skip_xxx_quoted, but we don't skip over double
2104 quoted strings when looking for the history expansion character as a
2106 else if (dquote
&& c
== '\'')
2112 i
= skip_single_quoted (string
, slen
, ++i
, 0);
2113 /* The posixly_correct test makes posix-mode shells allow double quotes
2114 to quote the history expansion character */
2115 else if (posixly_correct
== 0 && c
== '"')
2117 dquote
= 1 - dquote
;
2122 i
= skip_double_quoted (string
, slen
, ++i
, 0);
2123 #if defined (PROCESS_SUBSTITUTION)
2124 else if ((c
== '$' || c
== '<' || c
== '>') && string
[i
+1] == LPAREN
&& string
[i
+2] != LPAREN
)
2126 else if (c
== '$' && string
[i
+1] == LPAREN
&& string
[i
+2] != LPAREN
)
2129 if (string
[i
+2] == '\0')
2133 old_dquote
= dquote
;
2136 else if (histexp_comsub
&& c
== RPAREN
)
2139 dquote
= old_dquote
;
2143 else if (backq
) /* placeholder */
2145 ADVANCE_CHAR (string
, slen
, i
);
2149 ADVANCE_CHAR (string
, slen
, i
);
2154 #endif /* BANG_HISTORY */
2156 #if defined (READLINE)
2157 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
2158 an unclosed quoted string), or if the character at EINDEX is quoted
2159 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
2160 single and double-quoted string parsing functions should not return an
2161 error if there are unclosed quotes or braces. The characters that this
2162 recognizes need to be the same as the contents of
2163 rl_completer_quote_characters. */
2166 char_is_quoted (string
, eindex
)
2170 int i
, pass_next
, c
, oldjmp
;
2174 slen
= strlen (string
);
2175 oldjmp
= no_longjmp_on_fatal_error
;
2176 no_longjmp_on_fatal_error
= 1;
2185 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
2187 ADVANCE_CHAR (string
, slen
, i
);
2196 else if (c
== '$' && string
[i
+1] == '\'' && string
[i
+2])
2199 i
= skip_single_quoted (string
, slen
, i
, SX_COMPLETE
);
2203 else if (c
== '\'' || c
== '"')
2205 i
= (c
== '\'') ? skip_single_quoted (string
, slen
, ++i
, 0)
2206 : skip_double_quoted (string
, slen
, ++i
, SX_COMPLETE
);
2209 /* no increment, the skip_xxx functions go one past end */
2212 ADVANCE_CHAR (string
, slen
, i
);
2219 unclosed_pair (string
, eindex
, openstr
)
2224 int i
, pass_next
, openc
, olen
;
2228 slen
= strlen (string
);
2229 olen
= strlen (openstr
);
2230 i
= pass_next
= openc
= 0;
2236 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
2238 ADVANCE_CHAR (string
, slen
, i
);
2241 else if (string
[i
] == '\\')
2247 else if (STREQN (string
+ i
, openstr
, olen
))
2252 /* XXX - may want to handle $'...' specially here */
2253 else if (string
[i
] == '\'' || string
[i
] == '"')
2255 i
= (string
[i
] == '\'') ? skip_single_quoted (string
, slen
, i
, 0)
2256 : skip_double_quoted (string
, slen
, i
, SX_COMPLETE
);
2261 ADVANCE_CHAR (string
, slen
, i
);
2266 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
2267 individual words. If DELIMS is NULL, the current value of $IFS is used
2268 to split the string, and the function follows the shell field splitting
2269 rules. SENTINEL is an index to look for. NWP, if non-NULL,
2270 gets the number of words in the returned list. CWP, if non-NULL, gets
2271 the index of the word containing SENTINEL. Non-whitespace chars in
2272 DELIMS delimit separate fields. This is used by programmable completion. */
2274 split_at_delims (string
, slen
, delims
, sentinel
, flags
, nwp
, cwp
)
2278 int sentinel
, flags
;
2281 int ts
, te
, i
, nw
, cw
, ifs_split
, dflags
;
2282 char *token
, *d
, *d2
;
2283 WORD_LIST
*ret
, *tl
;
2285 if (string
== 0 || *string
== '\0')
2291 return ((WORD_LIST
*)NULL
);
2294 d
= (delims
== 0) ? ifs_value
: delims
;
2295 ifs_split
= delims
== 0;
2297 /* Make d2 the non-whitespace characters in delims */
2302 #if defined (HANDLE_MULTIBYTE)
2303 size_t mblength
= 1;
2307 slength
= strlen (delims
);
2308 d2
= (char *)xmalloc (slength
+ 1);
2312 #if defined (HANDLE_MULTIBYTE)
2313 mbstate_t state_bak
;
2315 mblength
= MBRLEN (delims
+ i
, slength
, &state
);
2316 if (MB_INVALIDCH (mblength
))
2318 else if (mblength
> 1)
2320 memcpy (d2
+ ts
, delims
+ i
, mblength
);
2323 slength
-= mblength
;
2327 if (whitespace (delims
[i
]) == 0)
2328 d2
[ts
++] = delims
[i
];
2336 ret
= (WORD_LIST
*)NULL
;
2338 /* Remove sequences of whitespace characters at the start of the string, as
2339 long as those characters are delimiters. */
2340 for (i
= 0; member (string
[i
], d
) && spctabnl (string
[i
]); i
++)
2342 if (string
[i
] == '\0')
2351 dflags
= flags
|SD_NOJMP
;
2354 te
= skip_to_delim (string
, ts
, d
, dflags
);
2356 /* If we have a non-whitespace delimiter character, use it to make a
2357 separate field. This is just about what $IFS splitting does and
2358 is closer to the behavior of the shell parser. */
2359 if (ts
== te
&& d2
&& member (string
[ts
], d2
))
2362 /* If we're using IFS splitting, the non-whitespace delimiter char
2363 and any additional IFS whitespace delimits a field. */
2365 while (member (string
[te
], d
) && spctabnl (string
[te
]) && ((flags
&SD_NOQUOTEDELIM
) == 0 || (string
[te
] != '\'' && string
[te
] != '"')))
2368 while (member (string
[te
], d2
) && ((flags
&SD_NOQUOTEDELIM
) == 0 || (string
[te
] != '\'' && string
[te
] != '"')))
2372 token
= substring (string
, ts
, te
);
2374 ret
= add_string_to_list (token
, ret
); /* XXX */
2378 if (sentinel
>= ts
&& sentinel
<= te
)
2381 /* If the cursor is at whitespace just before word start, set the
2382 sentinel word to the current word. */
2383 if (cwp
&& cw
== -1 && sentinel
== ts
-1)
2386 /* If the cursor is at whitespace between two words, make a new, empty
2387 word, add it before (well, after, since the list is in reverse order)
2388 the word we just added, and set the current word to that one. */
2389 if (cwp
&& cw
== -1 && sentinel
< ts
)
2391 tl
= make_word_list (make_word (""), ret
->next
);
2397 if (string
[te
] == 0)
2401 /* XXX - honor SD_NOQUOTEDELIM here */
2402 while (member (string
[i
], d
) && (ifs_split
|| spctabnl(string
[i
])) && ((flags
&SD_NOQUOTEDELIM
) == 0 || (string
[te
] != '\'' && string
[te
] != '"')))
2411 /* Special case for SENTINEL at the end of STRING. If we haven't found
2412 the word containing SENTINEL yet, and the index we're looking for is at
2413 the end of STRING (or past the end of the previously-found token,
2414 possible if the end of the line is composed solely of IFS whitespace)
2415 add an additional null argument and set the current word pointer to that. */
2416 if (cwp
&& cw
== -1 && (sentinel
>= slen
|| sentinel
>= te
))
2418 if (whitespace (string
[sentinel
- 1]))
2421 ret
= add_string_to_list (token
, ret
);
2434 return (REVERSE_LIST (ret
, WORD_LIST
*));
2436 #endif /* READLINE */
2440 /* Extract the name of the variable to bind to from the assignment string. */
2442 assignment_name (string
)
2448 offset
= assignment (string
, 0);
2450 return (char *)NULL
;
2451 temp
= substring (string
, 0, offset
);
2456 /* **************************************************************** */
2458 /* Functions to convert strings to WORD_LISTs and vice versa */
2460 /* **************************************************************** */
2462 /* Return a single string of all the words in LIST. SEP is the separator
2463 to put between individual elements of LIST in the output string. */
2465 string_list_internal (list
, sep
)
2469 register WORD_LIST
*t
;
2471 size_t word_len
, sep_len
, result_size
;
2474 return ((char *)NULL
);
2476 /* Short-circuit quickly if we don't need to separate anything. */
2477 if (list
->next
== 0)
2478 return (savestring (list
->word
->word
));
2480 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
2481 sep_len
= STRLEN (sep
);
2484 for (t
= list
; t
; t
= t
->next
)
2487 result_size
+= sep_len
;
2488 result_size
+= strlen (t
->word
->word
);
2491 r
= result
= (char *)xmalloc (result_size
+ 1);
2493 for (t
= list
; t
; t
= t
->next
)
2495 if (t
!= list
&& sep_len
)
2499 FASTCOPY (sep
, r
, sep_len
);
2506 word_len
= strlen (t
->word
->word
);
2507 FASTCOPY (t
->word
->word
, r
, word_len
);
2515 /* Return a single string of all the words present in LIST, separating
2516 each word with a space. */
2521 return (string_list_internal (list
, " "));
2524 /* An external interface that can be used by the rest of the shell to
2525 obtain a string containing the first character in $IFS. Handles all
2526 the multibyte complications. If LENP is non-null, it is set to the
2527 length of the returned string. */
2529 ifs_firstchar (lenp
)
2535 ret
= xmalloc (MB_LEN_MAX
+ 1);
2536 #if defined (HANDLE_MULTIBYTE)
2537 if (ifs_firstc_len
== 1)
2539 ret
[0] = ifs_firstc
[0];
2541 len
= ret
[0] ? 1 : 0;
2545 memcpy (ret
, ifs_firstc
, ifs_firstc_len
);
2546 ret
[len
= ifs_firstc_len
] = '\0';
2549 ret
[0] = ifs_firstc
;
2551 len
= ret
[0] ? 0 : 1;
2560 /* Return a single string of all the words present in LIST, obeying the
2561 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
2562 expansion [of $*] appears within a double quoted string, it expands
2563 to a single field with the value of each parameter separated by the
2564 first character of the IFS variable, or by a <space> if IFS is unset." */
2565 /* Posix interpretation 888 changes this when IFS is null by specifying
2566 that when unquoted, this expands to separate arguments */
2568 string_list_dollar_star (list
, quoted
, flags
)
2573 #if defined (HANDLE_MULTIBYTE)
2574 # if defined (__GNUC__)
2575 char sep
[MB_CUR_MAX
+ 1];
2583 #if defined (HANDLE_MULTIBYTE)
2584 # if !defined (__GNUC__)
2585 sep
= (char *)xmalloc (MB_CUR_MAX
+ 1);
2586 # endif /* !__GNUC__ */
2587 if (ifs_firstc_len
== 1)
2589 sep
[0] = ifs_firstc
[0];
2594 memcpy (sep
, ifs_firstc
, ifs_firstc_len
);
2595 sep
[ifs_firstc_len
] = '\0';
2598 sep
[0] = ifs_firstc
;
2602 ret
= string_list_internal (list
, sep
);
2603 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2609 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
2610 is non-zero, the $@ appears within double quotes, and we should quote
2611 the list before converting it into a string. If IFS is unset, and the
2612 word is not quoted, we just need to quote CTLESC and CTLNUL characters
2613 in the words in the list, because the default value of $IFS is
2614 <space><tab><newline>, IFS characters in the words in the list should
2615 also be split. If IFS is null, and the word is not quoted, we need
2616 to quote the words in the list to preserve the positional parameters
2618 Valid values for the FLAGS argument are the PF_ flags in command.h,
2619 the only one we care about is PF_ASSIGNRHS. $@ is supposed to expand
2620 to the positional parameters separated by spaces no matter what IFS is
2621 set to if in a context where word splitting is not performed. The only
2622 one that we didn't handle before is assignment statement arguments to
2623 declaration builtins like `declare'. */
2625 string_list_dollar_at (list
, quoted
, flags
)
2631 #if defined (HANDLE_MULTIBYTE)
2632 # if defined (__GNUC__)
2633 char sep
[MB_CUR_MAX
+ 1];
2636 # endif /* !__GNUC__ */
2642 /* XXX this could just be ifs = ifs_value; */
2643 ifs
= ifs_var
? value_cell (ifs_var
) : (char *)0;
2645 #if defined (HANDLE_MULTIBYTE)
2646 # if !defined (__GNUC__)
2647 sep
= (char *)xmalloc (MB_CUR_MAX
+ 1);
2648 # endif /* !__GNUC__ */
2649 /* XXX - testing PF_ASSIGNRHS to make sure positional parameters are
2650 separated with a space even when word splitting will not occur. */
2651 if (flags
& PF_ASSIGNRHS
)
2656 else if (ifs
&& *ifs
)
2658 if (ifs_firstc_len
== 1)
2660 sep
[0] = ifs_firstc
[0];
2665 memcpy (sep
, ifs_firstc
, ifs_firstc_len
);
2666 sep
[ifs_firstc_len
] = '\0';
2674 #else /* !HANDLE_MULTIBYTE */
2675 /* XXX - PF_ASSIGNRHS means no word splitting, so we want positional
2676 parameters separated by a space. */
2677 sep
[0] = ((flags
& PF_ASSIGNRHS
) || ifs
== 0 || *ifs
== 0) ? ' ' : *ifs
;
2679 #endif /* !HANDLE_MULTIBYTE */
2681 /* XXX -- why call quote_list if ifs == 0? we can get away without doing
2682 it now that quote_escapes quotes spaces */
2683 tlist
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
|Q_PATQUOTE
))
2685 : list_quote_escapes (list
);
2687 ret
= string_list_internal (tlist
, sep
);
2688 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2694 /* Turn the positional parameters into a string, understanding quoting and
2695 the various subtleties of using the first character of $IFS as the
2696 separator. Calls string_list_dollar_at, string_list_dollar_star, and
2697 string_list as appropriate. */
2698 /* This needs to fully understand the additional contexts where word
2699 splitting does not occur (W_ASSIGNRHS, etc.) */
2701 string_list_pos_params (pchar
, list
, quoted
, pflags
)
2709 if (pchar
== '*' && (quoted
& Q_DOUBLE_QUOTES
))
2711 tlist
= quote_list (list
);
2712 word_list_remove_quoted_nulls (tlist
);
2713 ret
= string_list_dollar_star (tlist
, 0, 0);
2715 else if (pchar
== '*' && (quoted
& Q_HERE_DOCUMENT
))
2717 tlist
= quote_list (list
);
2718 word_list_remove_quoted_nulls (tlist
);
2719 ret
= string_list (tlist
);
2721 else if (pchar
== '*' && quoted
== 0 && ifs_is_null
) /* XXX */
2722 ret
= expand_no_split_dollar_star
? string_list_dollar_star (list
, quoted
, 0) : string_list_dollar_at (list
, quoted
, 0); /* Posix interp 888 */
2723 else if (pchar
== '*' && quoted
== 0 && (pflags
& PF_ASSIGNRHS
)) /* XXX */
2724 ret
= expand_no_split_dollar_star
? string_list_dollar_star (list
, quoted
, 0) : string_list_dollar_at (list
, quoted
, 0); /* Posix interp 888 */
2725 else if (pchar
== '*')
2727 /* Even when unquoted, string_list_dollar_star does the right thing
2728 making sure that the first character of $IFS is used as the
2730 ret
= string_list_dollar_star (list
, quoted
, 0);
2732 else if (pchar
== '@' && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
2733 /* We use string_list_dollar_at, but only if the string is quoted, since
2734 that quotes the escapes if it's not, which we don't want. We could
2735 use string_list (the old code did), but that doesn't do the right
2736 thing if the first character of $IFS is not a space. We use
2737 string_list_dollar_star if the string is unquoted so we make sure that
2738 the elements of $@ are separated by the first character of $IFS for
2740 ret
= string_list_dollar_at (list
, quoted
, 0);
2741 else if (pchar
== '@' && quoted
== 0 && ifs_is_null
) /* XXX */
2742 ret
= string_list_dollar_at (list
, quoted
, 0); /* Posix interp 888 */
2743 else if (pchar
== '@' && quoted
== 0 && (pflags
& PF_ASSIGNRHS
))
2744 ret
= string_list_dollar_at (list
, quoted
, pflags
); /* Posix interp 888 */
2745 else if (pchar
== '@')
2746 ret
= string_list_dollar_star (list
, quoted
, 0);
2748 ret
= string_list ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? quote_list (list
) : list
);
2753 /* Return the list of words present in STRING. Separate the string into
2754 words at any of the characters found in SEPARATORS. If QUOTED is
2755 non-zero then word in the list will have its quoted flag set, otherwise
2756 the quoted flag is left as make_word () deemed fit.
2758 This obeys the P1003.2 word splitting semantics. If `separators' is
2759 exactly <space><tab><newline>, then the splitting algorithm is that of
2760 the Bourne shell, which treats any sequence of characters from `separators'
2761 as a delimiter. If IFS is unset, which results in `separators' being set
2762 to "", no splitting occurs. If separators has some other value, the
2763 following rules are applied (`IFS white space' means zero or more
2764 occurrences of <space>, <tab>, or <newline>, as long as those characters
2765 are in `separators'):
2767 1) IFS white space is ignored at the start and the end of the
2769 2) Each occurrence of a character in `separators' that is not
2770 IFS white space, along with any adjacent occurrences of
2771 IFS white space delimits a field.
2772 3) Any nonzero-length sequence of IFS white space delimits a field.
2775 /* BEWARE! list_string strips null arguments. Don't call it twice and
2776 expect to have "" preserved! */
2778 /* This performs word splitting and quoted null character removal on
2781 (((separators)[0]) ? ((separators)[1] ? isifs(c) \
2782 : (c) == (separators)[0]) \
2785 /* member of the space character class in the current locale */
2786 #define ifs_whitespace(c) ISSPACE(c)
2788 /* "adjacent IFS white space" */
2789 #define ifs_whitesep(c) ((sh_style_split || separators == 0) ? spctabnl (c) \
2790 : ifs_whitespace (c))
2793 list_string (string
, separators
, quoted
)
2794 register char *string
, *separators
;
2799 char *current_word
, *s
;
2800 int sindex
, sh_style_split
, whitesep
, xflags
, free_word
;
2803 if (!string
|| !*string
)
2804 return ((WORD_LIST
*)NULL
);
2806 sh_style_split
= separators
&& separators
[0] == ' ' &&
2807 separators
[1] == '\t' &&
2808 separators
[2] == '\n' &&
2809 separators
[3] == '\0';
2810 for (xflags
= 0, s
= ifs_value
; s
&& *s
; s
++)
2812 if (*s
== CTLESC
) xflags
|= SX_NOCTLESC
;
2813 else if (*s
== CTLNUL
) xflags
|= SX_NOESCCTLNUL
;
2817 /* Remove sequences of whitespace at the beginning of STRING, as
2818 long as those characters appear in IFS. Do not do this if
2819 STRING is quoted or if there are no separator characters. We use the
2820 Posix definition of whitespace as a member of the space character
2821 class in the current locale. */
2823 if (!quoted
|| !separators
|| !*separators
)
2825 /* issep() requires that separators be non-null, and always returns 0 if
2826 separator is the empty string, so don't bother if we get an empty string
2827 for separators. We already returned NULL above if STRING is empty. */
2828 if (!quoted
&& separators
&& *separators
)
2831 for (s
= string
; *s
&& issep (*s
) && ifs_whitespace (*s
); s
++);
2834 return ((WORD_LIST
*)NULL
);
2839 /* OK, now STRING points to a word that does not begin with white space.
2840 The splitting algorithm is:
2841 extract a word, stopping at a separator
2842 skip sequences of whitespace characters as long as they are separators
2843 This obeys the field splitting rules in Posix.2. */
2844 slen
= STRLEN (string
);
2845 for (result
= (WORD_LIST
*)NULL
, sindex
= 0; string
[sindex
]; )
2847 /* Don't need string length in ADVANCE_CHAR unless multibyte chars are
2848 possible, but need it in string_extract_verbatim for bounds checking */
2849 current_word
= string_extract_verbatim (string
, slen
, &sindex
, separators
, xflags
);
2850 if (current_word
== 0)
2853 free_word
= 1; /* If non-zero, we free current_word */
2855 /* If we have a quoted empty string, add a quoted null argument. We
2856 want to preserve the quoted null character iff this is a quoted
2857 empty string; otherwise the quoted null characters are removed
2859 if (QUOTED_NULL (current_word
))
2861 t
= alloc_word_desc ();
2862 t
->word
= make_quoted_char ('\0');
2863 t
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
2864 result
= make_word_list (t
, result
);
2866 else if (current_word
[0] != '\0')
2868 /* If we have something, then add it regardless. However,
2869 perform quoted null character removal on the current word. */
2870 remove_quoted_nulls (current_word
);
2872 /* We don't want to set the word flags based on the string contents
2873 here -- that's mostly for the parser -- so we just allocate a
2874 WORD_DESC *, assign current_word (noting that we don't want to
2875 free it), and skip all of make_word. */
2876 t
= alloc_word_desc ();
2877 t
->word
= current_word
;
2878 result
= make_word_list (t
, result
);
2880 result
->word
->flags
&= ~W_HASQUOTEDNULL
; /* just to be sure */
2881 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
2882 result
->word
->flags
|= W_QUOTED
;
2883 /* If removing quoted null characters leaves an empty word, note
2884 that we saw this for the caller to act on. */
2885 if (current_word
== 0 || current_word
[0] == '\0')
2886 result
->word
->flags
|= W_SAWQUOTEDNULL
;
2889 /* If we're not doing sequences of separators in the traditional
2890 Bourne shell style, then add a quoted null argument. */
2891 else if (!sh_style_split
&& !ifs_whitespace (string
[sindex
]))
2893 t
= alloc_word_desc ();
2894 t
->word
= make_quoted_char ('\0');
2895 t
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
2896 result
= make_word_list (t
, result
);
2900 free (current_word
);
2902 /* Note whether or not the separator is IFS whitespace, used later. */
2903 whitesep
= string
[sindex
] && ifs_whitesep (string
[sindex
]);
2905 /* Move past the current separator character. */
2909 ADVANCE_CHAR (string
, slen
, sindex
);
2912 /* Now skip sequences of whitespace characters if they are
2913 in the list of separators. */
2914 while (string
[sindex
] && ifs_whitesep (string
[sindex
]) && issep (string
[sindex
]))
2917 /* If the first separator was IFS whitespace and the current character
2918 is a non-whitespace IFS character, it should be part of the current
2919 field delimiter, not a separate delimiter that would result in an
2920 empty field. Look at POSIX.2, 3.6.5, (3)(b). */
2921 if (string
[sindex
] && whitesep
&& issep (string
[sindex
]) && !ifs_whitesep (string
[sindex
]))
2924 /* An IFS character that is not IFS white space, along with any
2925 adjacent IFS white space, shall delimit a field. (SUSv3) */
2926 while (string
[sindex
] && ifs_whitesep (string
[sindex
]) && isifs (string
[sindex
]))
2930 return (REVERSE_LIST (result
, WORD_LIST
*));
2933 /* Parse a single word from STRING, using SEPARATORS to separate fields.
2934 ENDPTR is set to the first character after the word. This is used by
2937 This is never called with SEPARATORS != $IFS, and takes advantage of that.
2939 XXX - this function is very similar to list_string; they should be
2942 /* character is in $IFS */
2943 #define islocalsep(c) (local_cmap[(unsigned char)(c)] != 0)
2946 get_word_from_string (stringp
, separators
, endptr
)
2947 char **stringp
, *separators
, **endptr
;
2951 int sindex
, sh_style_split
, whitesep
, xflags
;
2952 unsigned char local_cmap
[UCHAR_MAX
+1]; /* really only need single-byte chars here */
2955 if (!stringp
|| !*stringp
|| !**stringp
)
2956 return ((char *)NULL
);
2958 sh_style_split
= separators
&& separators
[0] == ' ' &&
2959 separators
[1] == '\t' &&
2960 separators
[2] == '\n' &&
2961 separators
[3] == '\0';
2962 memset (local_cmap
, '\0', sizeof (local_cmap
));
2963 for (xflags
= 0, s
= separators
; s
&& *s
; s
++)
2965 if (*s
== CTLESC
) xflags
|= SX_NOCTLESC
;
2966 if (*s
== CTLNUL
) xflags
|= SX_NOESCCTLNUL
;
2967 local_cmap
[(unsigned char)*s
] = 1; /* local charmap of separators */
2973 /* Remove sequences of whitespace at the beginning of STRING, as
2974 long as those characters appear in SEPARATORS. This happens if
2975 SEPARATORS == $' \t\n' or if IFS is unset. */
2976 if (sh_style_split
|| separators
== 0)
2977 for (; *s
&& spctabnl (*s
) && islocalsep (*s
); s
++);
2979 for (; *s
&& ifs_whitespace (*s
) && islocalsep (*s
); s
++);
2981 /* If the string is nothing but whitespace, update it and return. */
2987 return ((char *)NULL
);
2990 /* OK, S points to a word that does not begin with white space.
2991 Now extract a word, stopping at a separator, save a pointer to
2992 the first character after the word, then skip sequences of spc,
2993 tab, or nl as long as they are separators.
2995 This obeys the field splitting rules in Posix.2. */
2997 /* Don't need string length in ADVANCE_CHAR unless multibyte chars are
2998 possible, but need it in string_extract_verbatim for bounds checking */
3000 current_word
= string_extract_verbatim (s
, slen
, &sindex
, separators
, xflags
);
3002 /* Set ENDPTR to the first character after the end of the word. */
3004 *endptr
= s
+ sindex
;
3006 /* Note whether or not the separator is IFS whitespace, used later. */
3007 whitesep
= s
[sindex
] && ifs_whitesep (s
[sindex
]);
3009 /* Move past the current separator character. */
3013 ADVANCE_CHAR (s
, slen
, sindex
);
3016 /* Now skip sequences of space, tab, or newline characters if they are
3017 in the list of separators. */
3018 while (s
[sindex
] && spctabnl (s
[sindex
]) && islocalsep (s
[sindex
]))
3021 /* If the first separator was IFS whitespace and the current character is
3022 a non-whitespace IFS character, it should be part of the current field
3023 delimiter, not a separate delimiter that would result in an empty field.
3024 Look at POSIX.2, 3.6.5, (3)(b). */
3025 if (s
[sindex
] && whitesep
&& islocalsep (s
[sindex
]) && !ifs_whitesep (s
[sindex
]))
3028 /* An IFS character that is not IFS white space, along with any adjacent
3029 IFS white space, shall delimit a field. */
3030 while (s
[sindex
] && ifs_whitesep (s
[sindex
]) && islocalsep(s
[sindex
]))
3034 /* Update STRING to point to the next field. */
3035 *stringp
= s
+ sindex
;
3036 return (current_word
);
3039 /* Remove IFS white space at the end of STRING. Start at the end
3040 of the string and walk backwards until the beginning of the string
3041 or we find a character that's not IFS white space and not CTLESC.
3042 Only let CTLESC escape a white space character if SAW_ESCAPE is
3045 strip_trailing_ifs_whitespace (string
, separators
, saw_escape
)
3046 char *string
, *separators
;
3051 s
= string
+ STRLEN (string
) - 1;
3052 while (s
> string
&& ((spctabnl (*s
) && isifs (*s
)) ||
3053 (saw_escape
&& *s
== CTLESC
&& spctabnl (s
[1]))))
3061 /* Split STRING into words at whitespace. Obeys shell-style quoting with
3062 backslashes, single and double quotes. */
3064 list_string_with_quotes (string
)
3070 int c
, i
, tokstart
, len
;
3072 for (s
= string
; s
&& *s
&& spctabnl (*s
); s
++)
3074 if (s
== 0 || *s
== 0)
3075 return ((WORD_LIST
*)NULL
);
3079 list
= (WORD_LIST
*)NULL
;
3090 i
= skip_single_quoted (s
, s_len
, ++i
, 0);
3092 i
= skip_double_quoted (s
, s_len
, ++i
, 0);
3093 else if (c
== 0 || spctabnl (c
))
3095 /* We have found the end of a token. Make a word out of it and
3096 add it to the word list. */
3097 token
= substring (s
, tokstart
, i
);
3098 list
= add_string_to_list (token
, list
);
3100 while (spctabnl (s
[i
]))
3108 i
++; /* normal character */
3110 return (REVERSE_LIST (list
, WORD_LIST
*));
3114 /********************************************************/
3116 /* Functions to perform assignment statements */
3118 /********************************************************/
3120 #if defined (ARRAY_VARS)
3122 do_compound_assignment (name
, value
, flags
)
3127 int mklocal
, mkassoc
, mkglobal
, chklocal
;
3129 char *newname
; /* used for local nameref references */
3131 mklocal
= flags
& ASS_MKLOCAL
;
3132 mkassoc
= flags
& ASS_MKASSOC
;
3133 mkglobal
= flags
& ASS_MKGLOBAL
;
3134 chklocal
= flags
& ASS_CHKLOCAL
;
3136 if (mklocal
&& variable_context
)
3138 v
= find_variable (name
); /* follows namerefs */
3139 newname
= (v
== 0) ? nameref_transform_name (name
, flags
) : v
->name
;
3140 if (v
&& ((readonly_p (v
) && (flags
& ASS_FORCE
) == 0) || noassign_p (v
)))
3143 err_readonly (name
);
3144 return (v
); /* XXX */
3146 list
= expand_compound_array_assignment (v
, value
, flags
);
3148 v
= make_local_assoc_variable (newname
, 0);
3149 else if (v
== 0 || (array_p (v
) == 0 && assoc_p (v
) == 0) || v
->context
!= variable_context
)
3150 v
= make_local_array_variable (newname
, 0);
3152 assign_compound_array_list (v
, list
, flags
);
3154 dispose_words (list
);
3156 /* In a function but forcing assignment in global context. CHKLOCAL means to
3157 check for an existing local variable first. */
3158 else if (mkglobal
&& variable_context
)
3160 v
= chklocal
? find_variable (name
) : 0;
3161 if (v
&& (local_p (v
) == 0 || v
->context
!= variable_context
))
3164 v
= find_global_variable (name
);
3165 if (v
&& ((readonly_p (v
) && (flags
& ASS_FORCE
) == 0) || noassign_p (v
)))
3168 err_readonly (name
);
3169 return (v
); /* XXX */
3172 newname
= (v
== 0) ? nameref_transform_name (name
, flags
) : name
;
3173 list
= expand_compound_array_assignment (v
, value
, flags
);
3174 if (v
== 0 && mkassoc
)
3175 v
= make_new_assoc_variable (newname
);
3176 else if (v
&& mkassoc
&& assoc_p (v
) == 0)
3177 v
= convert_var_to_assoc (v
);
3179 v
= make_new_array_variable (newname
);
3180 else if (v
&& mkassoc
== 0 && array_p (v
) == 0)
3181 v
= convert_var_to_array (v
);
3183 assign_compound_array_list (v
, list
, flags
);
3185 dispose_words (list
);
3189 v
= assign_array_from_string (name
, value
, flags
);
3190 if (v
&& ((readonly_p (v
) && (flags
& ASS_FORCE
) == 0) || noassign_p (v
)))
3193 err_readonly (name
);
3194 return (v
); /* XXX */
3202 /* Given STRING, an assignment string, get the value of the right side
3203 of the `=', and bind it to the left side. If EXPAND is true, then
3204 perform parameter expansion, command substitution, and arithmetic
3205 expansion on the right-hand side. Perform tilde expansion in any
3206 case. Do not perform word splitting on the result of expansion. */
3208 do_assignment_internal (word
, expand
)
3209 const WORD_DESC
*word
;
3212 int offset
, appendop
, assign_list
, aflags
, retval
;
3213 char *name
, *value
, *temp
;
3215 #if defined (ARRAY_VARS)
3221 if (word
== 0 || word
->word
== 0)
3224 appendop
= assign_list
= aflags
= 0;
3225 string
= word
->word
;
3226 offset
= assignment (string
, 0);
3227 name
= savestring (string
);
3228 value
= (char *)NULL
;
3230 if (name
[offset
] == '=')
3232 if (name
[offset
- 1] == '+')
3235 name
[offset
- 1] = '\0';
3238 name
[offset
] = 0; /* might need this set later */
3239 temp
= name
+ offset
+ 1;
3241 #if defined (ARRAY_VARS)
3242 if (expand
&& (word
->flags
& W_COMPASSIGN
))
3244 assign_list
= ni
= 1;
3245 value
= extract_array_assignment_list (temp
, &ni
);
3249 if (expand
&& temp
[0])
3250 value
= expand_string_if_necessary (temp
, 0, expand_string_assignment
);
3252 value
= savestring (temp
);
3257 value
= (char *)xmalloc (1);
3261 if (echo_command_at_execute
)
3264 name
[offset
- 1] = '+';
3265 xtrace_print_assignment (name
, value
, assign_list
, 1);
3267 name
[offset
- 1] = '\0';
3270 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
3273 aflags
|= ASS_APPEND
;
3275 #if defined (ARRAY_VARS)
3276 if (t
= mbschr (name
, LBRACK
))
3280 report_error (_("%s: cannot assign list to array member"), name
);
3283 entry
= assign_array_element (name
, value
, aflags
);
3287 else if (assign_list
)
3289 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_CHKLOCAL
))
3290 aflags
|= ASS_CHKLOCAL
;
3291 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_ASSNGLOBAL
) == 0)
3292 aflags
|= ASS_MKLOCAL
;
3293 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_ASSNGLOBAL
))
3294 aflags
|= ASS_MKGLOBAL
;
3295 if (word
->flags
& W_ASSIGNASSOC
)
3296 aflags
|= ASS_MKASSOC
;
3297 entry
= do_compound_assignment (name
, value
, aflags
);
3300 #endif /* ARRAY_VARS */
3301 entry
= bind_variable (name
, value
, aflags
);
3303 stupidly_hack_special_variables (name
);
3305 /* Return 1 if the assignment seems to have been performed correctly. */
3306 if (entry
== 0 || readonly_p (entry
))
3307 retval
= 0; /* assignment failure */
3308 else if (noassign_p (entry
))
3310 set_exit_status (EXECUTION_FAILURE
);
3311 retval
= 1; /* error status, but not assignment failure */
3316 if (entry
&& retval
!= 0 && noassign_p (entry
) == 0)
3317 VUNSETATTR (entry
, att_invisible
);
3319 ASSIGN_RETURN (retval
);
3322 /* Perform the assignment statement in STRING, and expand the
3323 right side by doing tilde, command and parameter expansion. */
3325 do_assignment (string
)
3330 td
.flags
= W_ASSIGNMENT
;
3333 return do_assignment_internal (&td
, 1);
3337 do_word_assignment (word
, flags
)
3341 return do_assignment_internal (word
, 1);
3344 /* Given STRING, an assignment string, get the value of the right side
3345 of the `=', and bind it to the left side. Do not perform any word
3346 expansions on the right hand side. */
3348 do_assignment_no_expand (string
)
3353 td
.flags
= W_ASSIGNMENT
;
3356 return (do_assignment_internal (&td
, 0));
3359 /***************************************************
3361 * Functions to manage the positional parameters *
3363 ***************************************************/
3365 /* Return the word list that corresponds to `$*'. */
3367 list_rest_of_args ()
3369 register WORD_LIST
*list
, *args
;
3372 /* Break out of the loop as soon as one of the dollar variables is null. */
3373 for (i
= 1, list
= (WORD_LIST
*)NULL
; i
< 10 && dollar_vars
[i
]; i
++)
3374 list
= make_word_list (make_bare_word (dollar_vars
[i
]), list
);
3376 for (args
= rest_of_args
; args
; args
= args
->next
)
3377 list
= make_word_list (make_bare_word (args
->word
->word
), list
);
3379 return (REVERSE_LIST (list
, WORD_LIST
*));
3382 /* Return the value of a positional parameter. This handles values > 10. */
3384 get_dollar_var_value (ind
)
3391 temp
= dollar_vars
[ind
] ? savestring (dollar_vars
[ind
]) : (char *)NULL
;
3392 else /* We want something like ${11} */
3395 for (p
= rest_of_args
; p
&& ind
--; p
= p
->next
)
3397 temp
= p
? savestring (p
->word
->word
) : (char *)NULL
;
3402 /* Make a single large string out of the dollar digit variables,
3403 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
3404 case of "$*" with respect to IFS. */
3406 string_rest_of_args (dollar_star
)
3409 register WORD_LIST
*list
;
3412 list
= list_rest_of_args ();
3413 string
= dollar_star
? string_list_dollar_star (list
, 0, 0) : string_list (list
);
3414 dispose_words (list
);
3418 /* Return a string containing the positional parameters from START to
3419 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
3420 which only makes a difference if QUOTED is non-zero. If QUOTED includes
3421 Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
3422 no quoting chars are added. */
3424 pos_params (string
, start
, end
, quoted
, pflags
)
3426 int start
, end
, quoted
, pflags
;
3428 WORD_LIST
*save
, *params
, *h
, *t
;
3432 /* see if we can short-circuit. if start == end, we want 0 parameters. */
3434 return ((char *)NULL
);
3436 save
= params
= list_rest_of_args ();
3437 if (save
== 0 && start
> 0)
3438 return ((char *)NULL
);
3440 if (start
== 0) /* handle ${@:0[:x]} specially */
3442 t
= make_word_list (make_word (dollar_vars
[0]), params
);
3446 for (i
= start
? 1 : 0; params
&& i
< start
; i
++)
3447 params
= params
->next
;
3450 dispose_words (save
);
3451 return ((char *)NULL
);
3453 for (h
= t
= params
; params
&& i
< end
; i
++)
3456 params
= params
->next
;
3458 t
->next
= (WORD_LIST
*)NULL
;
3460 ret
= string_list_pos_params (string
[0], h
, quoted
, pflags
);
3465 dispose_words (save
);
3469 /******************************************************************/
3471 /* Functions to expand strings to strings or WORD_LISTs */
3473 /******************************************************************/
3475 #if defined (PROCESS_SUBSTITUTION)
3476 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
3478 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
3481 /* If there are any characters in STRING that require full expansion,
3482 then call FUNC to expand STRING; otherwise just perform quote
3483 removal if necessary. This returns a new string. */
3485 expand_string_if_necessary (string
, quoted
, func
)
3496 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3497 slen
= (MB_CUR_MAX
> 1) ? strlen (string
) : 0;
3501 if (EXP_CHAR (string
[i
]))
3503 else if (string
[i
] == '\'' || string
[i
] == '\\' || string
[i
] == '"')
3505 ADVANCE_CHAR (string
, slen
, i
);
3510 list
= (*func
) (string
, quoted
);
3513 ret
= string_list (list
);
3514 dispose_words (list
);
3519 else if (saw_quote
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
3520 ret
= string_quote_removal (string
, quoted
);
3522 ret
= savestring (string
);
3527 static inline char *
3528 expand_string_to_string_internal (string
, quoted
, func
)
3536 if (string
== 0 || *string
== '\0')
3537 return ((char *)NULL
);
3539 list
= (*func
) (string
, quoted
);
3542 ret
= string_list (list
);
3543 dispose_words (list
);
3552 expand_string_to_string (string
, quoted
)
3556 return (expand_string_to_string_internal (string
, quoted
, expand_string
));
3560 expand_string_unsplit_to_string (string
, quoted
)
3564 return (expand_string_to_string_internal (string
, quoted
, expand_string_unsplit
));
3568 expand_assignment_string_to_string (string
, quoted
)
3572 return (expand_string_to_string_internal (string
, quoted
, expand_string_assignment
));
3576 expand_arith_string (string
, quoted
)
3581 WORD_LIST
*list
, *tlist
;
3587 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3588 slen
= (MB_CUR_MAX
> 1) ? strlen (string
) : 0;
3592 if (EXP_CHAR (string
[i
]))
3594 else if (string
[i
] == '\'' || string
[i
] == '\\' || string
[i
] == '"')
3596 ADVANCE_CHAR (string
, slen
, i
);
3601 /* This is expanded version of expand_string_internal as it's called by
3602 expand_string_leave_quoted */
3603 td
.flags
= W_NOPROCSUB
|W_NOTILDE
; /* don't want process substitution or tilde expansion */
3604 #if 0 /* TAG:bash-5.1 */
3605 if (quoted
& Q_ARRAYSUB
)
3606 td
.flags
|= W_NOCOMSUB
;
3608 td
.word
= savestring (string
);
3609 list
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3610 /* This takes care of the calls from expand_string_leave_quoted and
3614 tlist
= word_list_split (list
);
3615 dispose_words (list
);
3618 dequote_list (list
);
3620 /* This comes from expand_string_if_necessary */
3623 ret
= string_list (list
);
3624 dispose_words (list
);
3630 else if (saw_quote
&& (quoted
& Q_ARITH
))
3631 ret
= string_quote_removal (string
, quoted
);
3632 else if (saw_quote
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
3633 ret
= string_quote_removal (string
, quoted
);
3635 ret
= savestring (string
);
3640 #if defined (COND_COMMAND)
3641 /* Just remove backslashes in STRING. Returns a new string. */
3643 remove_backslashes (string
)
3648 r
= ret
= (char *)xmalloc (strlen (string
) + 1);
3649 for (s
= string
; s
&& *s
; )
3661 /* This needs better error handling. */
3662 /* Expand W for use as an argument to a unary or binary operator in a
3663 [[...]] expression. If SPECIAL is 1, this is the rhs argument
3664 to the != or == operator, and should be treated as a pattern. In
3665 this case, we quote the string specially for the globbing code. If
3666 SPECIAL is 2, this is an rhs argument for the =~ operator, and should
3667 be quoted appropriately for regcomp/regexec. The caller is responsible
3668 for removing the backslashes if the unquoted word is needed later. In
3669 any case, since we don't perform word splitting, we need to do quoted
3670 null character removal. */
3672 cond_expand_word (w
, special
)
3680 if (w
->word
== 0 || w
->word
[0] == '\0')
3681 return ((char *)NULL
);
3683 expand_no_split_dollar_star
= 1;
3684 w
->flags
|= W_NOSPLIT2
;
3685 l
= call_expand_word_internal (w
, 0, 0, (int *)0, (int *)0);
3686 expand_no_split_dollar_star
= 0;
3689 if (special
== 0) /* LHS */
3692 word_list_remove_quoted_nulls (l
);
3694 r
= string_list (l
);
3698 /* Need to figure out whether or not we should call dequote_escapes
3699 or a new dequote_ctlnul function here, and under what
3701 qflags
= QGLOB_CVTNULL
|QGLOB_CTLESC
;
3703 qflags
|= QGLOB_REGEXP
;
3704 word_list_remove_quoted_nulls (l
);
3705 p
= string_list (l
);
3706 r
= quote_string_for_globbing (p
, qflags
);
3718 /* Call expand_word_internal to expand W and handle error returns.
3719 A convenience function for functions that don't want to handle
3720 any errors or free any memory before aborting. */
3722 call_expand_word_internal (w
, q
, i
, c
, e
)
3728 result
= expand_word_internal (w
, q
, i
, c
, e
);
3729 if (result
== &expand_word_error
|| result
== &expand_word_fatal
)
3731 /* By convention, each time this error is returned, w->word has
3732 already been freed (it sometimes may not be in the fatal case,
3733 but that doesn't result in a memory leak because we're going
3734 to exit in most cases). */
3735 w
->word
= (char *)NULL
;
3736 last_command_exit_value
= EXECUTION_FAILURE
;
3737 exp_jump_to_top_level ((result
== &expand_word_error
) ? DISCARD
: FORCE_EOF
);
3745 /* Perform parameter expansion, command substitution, and arithmetic
3746 expansion on STRING, as if it were a word. Leave the result quoted.
3747 Since this does not perform word splitting, it leaves quoted nulls
3750 expand_string_internal (string
, quoted
)
3757 if (string
== 0 || *string
== 0)
3758 return ((WORD_LIST
*)NULL
);
3761 td
.word
= savestring (string
);
3763 tresult
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3769 /* Expand STRING by performing parameter expansion, command substitution,
3770 and arithmetic expansion. Dequote the resulting WORD_LIST before
3771 returning it, but do not perform word splitting. The call to
3772 remove_quoted_nulls () is in here because word splitting normally
3773 takes care of quote removal. */
3775 expand_string_unsplit (string
, quoted
)
3781 if (string
== 0 || *string
== '\0')
3782 return ((WORD_LIST
*)NULL
);
3784 expand_no_split_dollar_star
= 1;
3785 value
= expand_string_internal (string
, quoted
);
3786 expand_no_split_dollar_star
= 0;
3792 remove_quoted_nulls (value
->word
->word
); /* XXX */
3793 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3795 dequote_list (value
);
3800 /* Expand the rhs of an assignment statement */
3802 expand_string_assignment (string
, quoted
)
3809 if (string
== 0 || *string
== '\0')
3810 return ((WORD_LIST
*)NULL
);
3812 expand_no_split_dollar_star
= 1;
3815 /* Other shells (ksh93) do it this way, which affects how $@ is expanded
3816 in constructs like bar=${@#0} (preserves the spaces resulting from the
3817 expansion of $@ in a context where you don't do word splitting); Posix
3818 interp 888 makes the expansion of $@ in contexts where word splitting
3819 is not performed unspecified. */
3820 td
.flags
= W_ASSIGNRHS
|W_NOSPLIT2
; /* Posix interp 888 */
3822 td
.flags
= W_ASSIGNRHS
;
3824 td
.word
= savestring (string
);
3825 value
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3828 expand_no_split_dollar_star
= 0;
3834 remove_quoted_nulls (value
->word
->word
); /* XXX */
3835 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3837 dequote_list (value
);
3843 /* Expand one of the PS? prompt strings. This is a sort of combination of
3844 expand_string_unsplit and expand_string_internal, but returns the
3845 passed string when an error occurs. Might want to trap other calls
3846 to jump_to_top_level here so we don't endlessly loop. */
3848 expand_prompt_string (string
, quoted
, wflags
)
3856 if (string
== 0 || *string
== 0)
3857 return ((WORD_LIST
*)NULL
);
3860 td
.word
= savestring (string
);
3862 no_longjmp_on_fatal_error
= 1;
3863 value
= expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3864 no_longjmp_on_fatal_error
= 0;
3866 if (value
== &expand_word_error
|| value
== &expand_word_fatal
)
3868 value
= make_word_list (make_bare_word (string
), (WORD_LIST
*)NULL
);
3876 remove_quoted_nulls (value
->word
->word
); /* XXX */
3877 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3879 dequote_list (value
);
3884 /* Expand STRING just as if you were expanding a word, but do not dequote
3885 the resultant WORD_LIST. This is called only from within this file,
3886 and is used to correctly preserve quoted characters when expanding
3887 things like ${1+"$@"}. This does parameter expansion, command
3888 substitution, arithmetic expansion, and word splitting. */
3890 expand_string_leave_quoted (string
, quoted
)
3897 if (string
== 0 || *string
== '\0')
3898 return ((WORD_LIST
*)NULL
);
3900 tlist
= expand_string_internal (string
, quoted
);
3904 tresult
= word_list_split (tlist
);
3905 dispose_words (tlist
);
3908 return ((WORD_LIST
*)NULL
);
3911 /* This does not perform word splitting or dequote the WORD_LIST
3914 expand_string_for_rhs (string
, quoted
, op
, pflags
, dollar_at_p
, expanded_p
)
3916 int quoted
, op
, pflags
;
3917 int *dollar_at_p
, *expanded_p
;
3923 if (string
== 0 || *string
== '\0')
3924 return (WORD_LIST
*)NULL
;
3926 /* We want field splitting to be determined by what is going to be done with
3927 the entire ${parameterOPword} expansion, so we don't want to split the RHS
3928 we expand here. However, the expansion of $* is determined by whether we
3929 are going to eventually perform word splitting, so we want to set this
3930 depending on whether or not are are going to be splitting: if the expansion
3931 is quoted, if the OP is `=', or if IFS is set to the empty string, we
3932 are not going to be splitting, so we set expand_no_split_dollar_star to
3933 note this to callees.
3934 We pass through PF_ASSIGNRHS as W_ASSIGNRHS if this is on the RHS of an
3935 assignment statement. */
3936 /* The updated treatment of $* is the result of Posix interp 888 */
3937 /* This was further clarified on the austin-group list in March, 2017 and
3938 in Posix bug 1129 */
3939 old_nosplit
= expand_no_split_dollar_star
;
3940 expand_no_split_dollar_star
= (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) || op
== '=' || ifs_is_null
== 0; /* XXX - was 1 */
3941 td
.flags
= W_EXPANDRHS
; /* expanding RHS of ${paramOPword} */
3942 td
.flags
|= W_NOSPLIT2
; /* no splitting, remove "" and '' */
3943 if (pflags
& PF_ASSIGNRHS
) /* pass through */
3944 td
.flags
|= W_ASSIGNRHS
;
3947 td
.flags
|= W_ASSIGNRHS
; /* expand b in ${a=b} like assignment */
3949 td
.flags
|= W_ASSIGNRHS
|W_NOASSNTILDE
; /* expand b in ${a=b} like assignment */
3952 tresult
= call_expand_word_internal (&td
, quoted
, 1, dollar_at_p
, expanded_p
);
3953 expand_no_split_dollar_star
= old_nosplit
;
3958 /* This does not perform word splitting or dequote the WORD_LIST
3959 it returns and it treats $* as if it were quoted. */
3961 expand_string_for_pat (string
, quoted
, dollar_at_p
, expanded_p
)
3963 int quoted
, *dollar_at_p
, *expanded_p
;
3969 if (string
== 0 || *string
== '\0')
3970 return (WORD_LIST
*)NULL
;
3972 oexp
= expand_no_split_dollar_star
;
3973 expand_no_split_dollar_star
= 1;
3974 td
.flags
= W_NOSPLIT2
; /* no splitting, remove "" and '' */
3976 tresult
= call_expand_word_internal (&td
, quoted
, 1, dollar_at_p
, expanded_p
);
3977 expand_no_split_dollar_star
= oexp
;
3982 /* Expand STRING just as if you were expanding a word. This also returns
3983 a list of words. Note that filename globbing is *NOT* done for word
3984 or string expansion, just when the shell is expanding a command. This
3985 does parameter expansion, command substitution, arithmetic expansion,
3986 and word splitting. Dequote the resultant WORD_LIST before returning. */
3988 expand_string (string
, quoted
)
3994 if (string
== 0 || *string
== '\0')
3995 return ((WORD_LIST
*)NULL
);
3997 result
= expand_string_leave_quoted (string
, quoted
);
3998 return (result
? dequote_list (result
) : result
);
4001 /*******************************************
4003 * Functions to expand WORD_DESCs *
4005 *******************************************/
4007 /* Expand WORD, performing word splitting on the result. This does
4008 parameter expansion, command substitution, arithmetic expansion,
4009 word splitting, and quote removal. */
4012 expand_word (word
, quoted
)
4016 WORD_LIST
*result
, *tresult
;
4018 tresult
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
4019 result
= word_list_split (tresult
);
4020 dispose_words (tresult
);
4021 return (result
? dequote_list (result
) : result
);
4024 /* Expand WORD, but do not perform word splitting on the result. This
4025 does parameter expansion, command substitution, arithmetic expansion,
4026 and quote removal. */
4028 expand_word_unsplit (word
, quoted
)
4034 result
= expand_word_leave_quoted (word
, quoted
);
4035 return (result
? dequote_list (result
) : result
);
4038 /* Perform shell expansions on WORD, but do not perform word splitting or
4039 quote removal on the result. Virtually identical to expand_word_unsplit;
4040 could be combined if implementations don't diverge. */
4042 expand_word_leave_quoted (word
, quoted
)
4048 expand_no_split_dollar_star
= 1;
4050 word
->flags
|= W_NOSPLIT
;
4051 word
->flags
|= W_NOSPLIT2
;
4052 result
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
4053 expand_no_split_dollar_star
= 0;
4058 /***************************************************
4060 * Functions to handle quoting chars *
4062 ***************************************************/
4066 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
4067 The parser passes CTLNUL as CTLESC CTLNUL. */
4069 /* Quote escape characters in string s, but no other characters. This is
4070 used to protect CTLESC and CTLNUL in variable values from the rest of
4071 the word expansion process after the variable is expanded (word splitting
4072 and filename generation). If IFS is null, we quote spaces as well, just
4073 in case we split on spaces later (in the case of unquoted $@, we will
4074 eventually attempt to split the entire word on spaces). Corresponding
4075 code exists in dequote_escapes. Even if we don't end up splitting on
4076 spaces, quoting spaces is not a problem. This should never be called on
4077 a string that is quoted with single or double quotes or part of a here
4078 document (effectively double-quoted).
4079 FLAGS says whether or not we are going to split the result. If we are not,
4080 and there is a CTLESC or CTLNUL in IFS, we need to quote CTLESC and CTLNUL,
4081 respectively, to prevent them from being removed as part of dequoting. */
4083 quote_escapes_internal (string
, flags
)
4087 const char *s
, *send
;
4090 int quote_spaces
, skip_ctlesc
, skip_ctlnul
, nosplit
;
4093 slen
= strlen (string
);
4094 send
= string
+ slen
;
4096 quote_spaces
= (ifs_value
&& *ifs_value
== 0);
4097 nosplit
= (flags
& PF_NOSPLIT2
);
4099 for (skip_ctlesc
= skip_ctlnul
= 0, s
= ifs_value
; s
&& *s
; s
++)
4101 skip_ctlesc
|= (nosplit
== 0 && *s
== CTLESC
);
4102 skip_ctlnul
|= (nosplit
== 0 && *s
== CTLNUL
);
4105 t
= result
= (char *)xmalloc ((slen
* 2) + 1);
4110 if ((skip_ctlesc
== 0 && *s
== CTLESC
) || (skip_ctlnul
== 0 && *s
== CTLNUL
) || (quote_spaces
&& *s
== ' '))
4112 COPY_CHAR_P (t
, s
, send
);
4120 quote_escapes (string
)
4123 return (quote_escapes_internal (string
, 0));
4130 return (quote_escapes_internal (string
, PF_NOSPLIT2
));
4134 list_quote_escapes (list
)
4137 register WORD_LIST
*w
;
4140 for (w
= list
; w
; w
= w
->next
)
4143 w
->word
->word
= quote_escapes (t
);
4149 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
4151 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
4152 This is necessary to make unquoted CTLESC and CTLNUL characters in the
4153 data stream pass through properly.
4155 We need to remove doubled CTLESC characters inside quoted strings before
4156 quoting the entire string, so we do not double the number of CTLESC
4159 Also used by parts of the pattern substitution code. */
4161 dequote_escapes (string
)
4164 const char *s
, *send
;
4173 slen
= strlen (string
);
4174 send
= string
+ slen
;
4176 t
= result
= (char *)xmalloc (slen
+ 1);
4178 if (strchr (string
, CTLESC
) == 0)
4179 return (strcpy (result
, string
));
4181 quote_spaces
= (ifs_value
&& *ifs_value
== 0);
4186 if (*s
== CTLESC
&& (s
[1] == CTLESC
|| s
[1] == CTLNUL
|| (quote_spaces
&& s
[1] == ' ')))
4192 COPY_CHAR_P (t
, s
, send
);
4199 #if defined (INCLUDE_UNUSED)
4201 list_dequote_escapes (list
)
4204 register WORD_LIST
*w
;
4207 for (w
= list
; w
; w
= w
->next
)
4210 w
->word
->word
= dequote_escapes (t
);
4217 /* Return a new string with the quoted representation of character C.
4218 This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
4219 set in any resultant WORD_DESC where this value is the word. */
4221 make_quoted_char (c
)
4226 temp
= (char *)xmalloc (3);
4241 /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
4242 the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
4243 this value is the word. */
4245 quote_string (string
)
4250 char *result
, *send
;
4254 result
= (char *)xmalloc (2);
4262 slen
= strlen (string
);
4263 send
= string
+ slen
;
4265 result
= (char *)xmalloc ((slen
* 2) + 1);
4267 for (t
= result
; string
< send
; )
4270 COPY_CHAR_P (t
, string
, send
);
4277 /* De-quote quoted characters in STRING. */
4279 dequote_string (string
)
4282 register char *s
, *t
;
4284 char *result
, *send
;
4288 if (string
[0] == CTLESC
&& string
[1] == 0)
4289 internal_inform ("dequote_string: string with bare CTLESC");
4292 slen
= STRLEN (string
);
4294 t
= result
= (char *)xmalloc (slen
+ 1);
4296 if (QUOTED_NULL (string
))
4302 /* A string consisting of only a single CTLESC should pass through unchanged */
4303 if (string
[0] == CTLESC
&& string
[1] == 0)
4310 /* If no character in the string can be quoted, don't bother examining
4311 each character. Just return a copy of the string passed to us. */
4312 if (strchr (string
, CTLESC
) == NULL
)
4313 return (strcpy (result
, string
));
4315 send
= string
+ slen
;
4325 COPY_CHAR_P (t
, s
, send
);
4332 /* Quote the entire WORD_LIST list. */
4337 register WORD_LIST
*w
;
4340 for (w
= list
; w
; w
= w
->next
)
4343 w
->word
->word
= quote_string (t
);
4345 w
->word
->flags
|= W_HASQUOTEDNULL
; /* XXX - turn on W_HASQUOTEDNULL here? */
4346 w
->word
->flags
|= W_QUOTED
;
4358 s
= dequote_string (word
->word
);
4359 if (QUOTED_NULL (word
->word
))
4360 word
->flags
&= ~W_HASQUOTEDNULL
;
4367 /* De-quote quoted characters in each word in LIST. */
4373 register WORD_LIST
*tlist
;
4375 for (tlist
= list
; tlist
; tlist
= tlist
->next
)
4377 s
= dequote_string (tlist
->word
->word
);
4378 if (QUOTED_NULL (tlist
->word
->word
))
4379 tlist
->word
->flags
&= ~W_HASQUOTEDNULL
;
4380 free (tlist
->word
->word
);
4381 tlist
->word
->word
= s
;
4386 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
4389 remove_quoted_escapes (string
)
4396 t
= dequote_escapes (string
);
4404 /* Remove quoted $IFS characters from STRING. Quoted IFS characters are
4405 added to protect them from word splitting, but we need to remove them
4406 if no word splitting takes place. This returns newly-allocated memory,
4407 so callers can use it to replace savestring(). */
4409 remove_quoted_ifs (string
)
4412 register size_t slen
;
4417 slen
= strlen (string
);
4418 send
= string
+ slen
;
4421 ret
= (char *)xmalloc (slen
+ 1);
4425 if (string
[i
] == CTLESC
)
4428 if (string
[i
] == 0 || isifs (string
[i
]) == 0)
4434 COPY_CHAR_I (ret
, j
, string
, send
, i
);
4442 remove_quoted_nulls (string
)
4445 register size_t slen
;
4446 register int i
, j
, prev_i
;
4449 if (strchr (string
, CTLNUL
) == 0) /* XXX */
4450 return string
; /* XXX */
4452 slen
= strlen (string
);
4457 if (string
[i
] == CTLESC
)
4459 /* Old code had j++, but we cannot assume that i == j at this
4460 point -- what if a CTLNUL has already been removed from the
4461 string? We don't want to drop the CTLESC or recopy characters
4462 that we've already copied down. */
4464 string
[j
++] = CTLESC
;
4468 else if (string
[i
] == CTLNUL
)
4475 ADVANCE_CHAR (string
, slen
, i
); /* COPY_CHAR_I? */
4478 do string
[j
++] = string
[prev_i
++]; while (prev_i
< i
);
4488 /* Perform quoted null character removal on each element of LIST.
4489 This modifies LIST. */
4491 word_list_remove_quoted_nulls (list
)
4494 register WORD_LIST
*t
;
4496 for (t
= list
; t
; t
= t
->next
)
4498 remove_quoted_nulls (t
->word
->word
);
4499 t
->word
->flags
&= ~W_HASQUOTEDNULL
;
4503 /* **************************************************************** */
4505 /* Functions for Matching and Removing Patterns */
4507 /* **************************************************************** */
4509 #if defined (HANDLE_MULTIBYTE)
4510 # ifdef INCLUDE_UNUSED
4511 static unsigned char *
4512 mb_getcharlens (string
, len
)
4516 int i
, offset
, last
;
4523 ret
= (unsigned char *)xmalloc (len
);
4524 memset (ret
, 0, len
);
4525 while (string
[last
])
4527 ADVANCE_CHAR (string
, len
, offset
);
4528 ret
[last
] = offset
- last
;
4536 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
4537 can have one of 4 values:
4538 RP_LONG_LEFT remove longest matching portion at start of PARAM
4539 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
4540 RP_LONG_RIGHT remove longest matching portion at end of PARAM
4541 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
4544 #define RP_LONG_LEFT 1
4545 #define RP_SHORT_LEFT 2
4546 #define RP_LONG_RIGHT 3
4547 #define RP_SHORT_RIGHT 4
4549 /* Returns its first argument if nothing matched; new memory otherwise */
4551 remove_upattern (param
, pattern
, op
)
4552 char *param
, *pattern
;
4555 register size_t len
;
4557 register char *p
, *ret
, c
;
4559 len
= STRLEN (param
);
4564 case RP_LONG_LEFT
: /* remove longest match at start */
4565 for (p
= end
; p
>= param
; p
--)
4568 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4571 return (savestring (p
));
4578 case RP_SHORT_LEFT
: /* remove shortest match at start */
4579 for (p
= param
; p
<= end
; p
++)
4582 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4585 return (savestring (p
));
4591 case RP_LONG_RIGHT
: /* remove longest match at end */
4592 for (p
= param
; p
<= end
; p
++)
4594 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4597 ret
= savestring (param
);
4604 case RP_SHORT_RIGHT
: /* remove shortest match at end */
4605 for (p
= end
; p
>= param
; p
--)
4607 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4610 ret
= savestring (param
);
4618 return (param
); /* no match, return original string */
4621 #if defined (HANDLE_MULTIBYTE)
4622 /* Returns its first argument if nothing matched; new memory otherwise */
4624 remove_wpattern (wparam
, wstrlen
, wpattern
, op
)
4635 case RP_LONG_LEFT
: /* remove longest match at start */
4636 for (n
= wstrlen
; n
>= 0; n
--)
4638 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4639 if (wcsmatch (wpattern
, wparam
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4642 return (wcsdup (wparam
+ n
));
4648 case RP_SHORT_LEFT
: /* remove shortest match at start */
4649 for (n
= 0; n
<= wstrlen
; n
++)
4651 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4652 if (wcsmatch (wpattern
, wparam
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4655 return (wcsdup (wparam
+ n
));
4661 case RP_LONG_RIGHT
: /* remove longest match at end */
4662 for (n
= 0; n
<= wstrlen
; n
++)
4664 if (wcsmatch (wpattern
, wparam
+ n
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4666 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4667 ret
= wcsdup (wparam
);
4674 case RP_SHORT_RIGHT
: /* remove shortest match at end */
4675 for (n
= wstrlen
; n
>= 0; n
--)
4677 if (wcsmatch (wpattern
, wparam
+ n
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4679 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4680 ret
= wcsdup (wparam
);
4688 return (wparam
); /* no match, return original string */
4690 #endif /* HANDLE_MULTIBYTE */
4693 remove_pattern (param
, pattern
, op
)
4694 char *param
, *pattern
;
4701 if (*param
== '\0' || pattern
== NULL
|| *pattern
== '\0') /* minor optimization */
4702 return (savestring (param
));
4704 #if defined (HANDLE_MULTIBYTE)
4707 wchar_t *ret
, *oret
;
4709 wchar_t *wparam
, *wpattern
;
4712 /* XXX - could optimize here by checking param and pattern for multibyte
4713 chars with mbsmbchar and calling remove_upattern. */
4715 n
= xdupmbstowcs (&wpattern
, NULL
, pattern
);
4716 if (n
== (size_t)-1)
4718 xret
= remove_upattern (param
, pattern
, op
);
4719 return ((xret
== param
) ? savestring (param
) : xret
);
4721 n
= xdupmbstowcs (&wparam
, NULL
, param
);
4723 if (n
== (size_t)-1)
4726 xret
= remove_upattern (param
, pattern
, op
);
4727 return ((xret
== param
) ? savestring (param
) : xret
);
4729 oret
= ret
= remove_wpattern (wparam
, n
, wpattern
, op
);
4730 /* Don't bother to convert wparam back to multibyte string if nothing
4731 matched; just return copy of original string */
4736 return (savestring (param
));
4743 xret
= (char *)xmalloc (n
+ 1);
4744 memset (&ps
, '\0', sizeof (mbstate_t));
4745 n
= wcsrtombs (xret
, (const wchar_t **)&ret
, n
, &ps
);
4746 xret
[n
] = '\0'; /* just to make sure */
4753 xret
= remove_upattern (param
, pattern
, op
);
4754 return ((xret
== param
) ? savestring (param
) : xret
);
4758 /* Match PAT anywhere in STRING and return the match boundaries.
4759 This returns 1 in case of a successful match, 0 otherwise. SP
4760 and EP are pointers into the string where the match begins and
4761 ends, respectively. MTYPE controls what kind of match is attempted.
4762 MATCH_BEG and MATCH_END anchor the match at the beginning and end
4763 of the string, respectively. The longest match is returned. */
4765 match_upattern (string
, pat
, mtype
, sp
, ep
)
4772 register char *p
, *p1
, *npat
;
4775 /* If the pattern doesn't match anywhere in the string, go ahead and
4776 short-circuit right away. A minor optimization, saves a bunch of
4777 unnecessary calls to strmatch (up to N calls for a string of N
4778 characters) if the match is unsuccessful. To preserve the semantics
4779 of the substring matches below, we make sure that the pattern has
4780 `*' as first and last character, making a new pattern if necessary. */
4781 /* XXX - check this later if I ever implement `**' with special meaning,
4782 since this will potentially result in `**' at the beginning or end */
4784 if (pat
[0] != '*' || (pat
[0] == '*' && pat
[1] == LPAREN
&& extended_glob
) || pat
[len
- 1] != '*')
4786 int unescaped_backslash
;
4789 p
= npat
= (char *)xmalloc (len
+ 3);
4791 if ((mtype
!= MATCH_BEG
) && (*p1
!= '*' || (*p1
== '*' && p1
[1] == LPAREN
&& extended_glob
)))
4796 /* Need to also handle a pattern that ends with an unescaped backslash.
4797 For right now, we ignore it because the pattern matching code will
4798 fail the match anyway */
4799 /* If the pattern ends with a `*' we leave it alone if it's preceded by
4800 an even number of backslashes, but if it's escaped by a backslash
4801 we need to add another `*'. */
4802 if ((mtype
!= MATCH_END
) && (p1
[-1] == '*' && (unescaped_backslash
= p1
[-2] == '\\')))
4805 while (pp
>= pat
&& *pp
-- == '\\')
4806 unescaped_backslash
= 1 - unescaped_backslash
;
4807 if (unescaped_backslash
)
4810 else if (mtype
!= MATCH_END
&& p1
[-1] != '*')
4813 if (p1
[-1] != '*' || p1
[-2] == '\\')
4820 c
= strmatch (npat
, string
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
);
4823 if (c
== FNM_NOMATCH
)
4826 len
= STRLEN (string
);
4829 mlen
= umatchlen (pat
, len
);
4830 if (mlen
> (int)len
)
4836 for (p
= string
; p
<= end
; p
++)
4838 if (match_pattern_char (pat
, p
, FNMATCH_IGNCASE
))
4840 p1
= (mlen
== -1) ? end
: p
+ mlen
;
4841 /* p1 - p = length of portion of string to be considered
4842 p = current position in string
4843 mlen = number of characters consumed by match (-1 for entire string)
4845 we want to break immediately if the potential match len
4846 is greater than the number of characters remaining in the
4851 for ( ; p1
>= p
; p1
--)
4853 c
= *p1
; *p1
= '\0';
4854 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4863 /* If MLEN != -1, we have a fixed length pattern. */
4874 if (match_pattern_char (pat
, string
, FNMATCH_IGNCASE
) == 0)
4877 for (p
= (mlen
== -1) ? end
: string
+ mlen
; p
>= string
; p
--)
4880 if (strmatch (pat
, string
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4888 /* If MLEN != -1, we have a fixed length pattern. */
4896 for (p
= end
- ((mlen
== -1) ? len
: mlen
); p
<= end
; p
++)
4898 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4904 /* If MLEN != -1, we have a fixed length pattern. */
4915 #if defined (HANDLE_MULTIBYTE)
4917 #define WFOLD(c) (match_ignore_case && iswupper (c) ? towlower (c) : (c))
4919 /* Match WPAT anywhere in WSTRING and return the match boundaries.
4920 This returns 1 in case of a successful match, 0 otherwise. Wide
4921 character version. */
4923 match_wpattern (wstring
, indices
, wstrlen
, wpat
, mtype
, sp
, ep
)
4931 wchar_t wc
, *wp
, *nwpat
, *wp1
;
4934 int n
, n1
, n2
, simple
;
4936 simple
= (wpat
[0] != L
'\\' && wpat
[0] != L
'*' && wpat
[0] != L
'?' && wpat
[0] != L
'[');
4937 #if defined (EXTENDED_GLOB)
4939 simple
&= (wpat
[1] != L
'(' || (wpat
[0] != L
'*' && wpat
[0] != L
'?' && wpat
[0] != L
'+' && wpat
[0] != L
'!' && wpat
[0] != L
'@')); /*)*/
4942 /* If the pattern doesn't match anywhere in the string, go ahead and
4943 short-circuit right away. A minor optimization, saves a bunch of
4944 unnecessary calls to strmatch (up to N calls for a string of N
4945 characters) if the match is unsuccessful. To preserve the semantics
4946 of the substring matches below, we make sure that the pattern has
4947 `*' as first and last character, making a new pattern if necessary. */
4948 len
= wcslen (wpat
);
4949 if (wpat
[0] != L
'*' || (wpat
[0] == L
'*' && wpat
[1] == WLPAREN
&& extended_glob
) || wpat
[len
- 1] != L
'*')
4951 int unescaped_backslash
;
4954 wp
= nwpat
= (wchar_t *)xmalloc ((len
+ 3) * sizeof (wchar_t));
4956 if (*wp1
!= L
'*' || (*wp1
== '*' && wp1
[1] == WLPAREN
&& extended_glob
))
4958 while (*wp1
!= L
'\0')
4961 /* See comments above in match_upattern. */
4962 if (wp1
[-1] == L
'*' && (unescaped_backslash
= wp1
[-2] == L
'\\'))
4965 while (wpp
>= wpat
&& *wpp
-- == L
'\\')
4966 unescaped_backslash
= 1 - unescaped_backslash
;
4967 if (unescaped_backslash
)
4970 else if (wp1
[-1] != L
'*')
4973 if (wp1
[-1] != L
'*' || wp1
[-2] == L
'\\')
4980 len
= wcsmatch (nwpat
, wstring
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
);
4983 if (len
== FNM_NOMATCH
)
4986 mlen
= wmatchlen (wpat
, wstrlen
);
4987 if (mlen
> (int)wstrlen
)
4990 /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */
4994 for (n
= 0; n
<= wstrlen
; n
++)
4996 n2
= simple
? (WFOLD(*wpat
) == WFOLD(wstring
[n
])) : match_pattern_wchar (wpat
, wstring
+ n
, FNMATCH_IGNCASE
);
4999 n1
= (mlen
== -1) ? wstrlen
: n
+ mlen
;
5003 for ( ; n1
>= n
; n1
--)
5005 wc
= wstring
[n1
]; wstring
[n1
] = L
'\0';
5006 if (wcsmatch (wpat
, wstring
+ n
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
5014 /* If MLEN != -1, we have a fixed length pattern. */
5024 if (match_pattern_wchar (wpat
, wstring
, FNMATCH_IGNCASE
) == 0)
5027 for (n
= (mlen
== -1) ? wstrlen
: mlen
; n
>= 0; n
--)
5029 wc
= wstring
[n
]; wstring
[n
] = L
'\0';
5030 if (wcsmatch (wpat
, wstring
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
5038 /* If MLEN != -1, we have a fixed length pattern. */
5046 for (n
= wstrlen
- ((mlen
== -1) ? wstrlen
: mlen
); n
<= wstrlen
; n
++)
5048 if (wcsmatch (wpat
, wstring
+ n
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
5051 *ep
= indices
[wstrlen
];
5054 /* If MLEN != -1, we have a fixed length pattern. */
5065 #endif /* HANDLE_MULTIBYTE */
5068 match_pattern (string
, pat
, mtype
, sp
, ep
)
5073 #if defined (HANDLE_MULTIBYTE)
5076 wchar_t *wstring
, *wpat
;
5080 if (string
== 0 || pat
== 0 || *pat
== 0)
5083 #if defined (HANDLE_MULTIBYTE)
5086 if (mbsmbchar (string
) == 0 && mbsmbchar (pat
) == 0)
5087 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
5089 n
= xdupmbstowcs (&wpat
, NULL
, pat
);
5090 if (n
== (size_t)-1)
5091 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
5092 n
= xdupmbstowcs (&wstring
, &indices
, string
);
5093 if (n
== (size_t)-1)
5096 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
5098 ret
= match_wpattern (wstring
, indices
, n
, wpat
, mtype
, sp
, ep
);
5108 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
5112 getpatspec (c
, value
)
5117 return ((*value
== '#') ? RP_LONG_LEFT
: RP_SHORT_LEFT
);
5119 return ((*value
== '%') ? RP_LONG_RIGHT
: RP_SHORT_RIGHT
);
5122 /* Posix.2 says that the WORD should be run through tilde expansion,
5123 parameter expansion, command substitution and arithmetic expansion.
5124 This leaves the result quoted, so quote_string_for_globbing () has
5125 to be called to fix it up for strmatch (). If QUOTED is non-zero,
5126 it means that the entire expression was enclosed in double quotes.
5127 This means that quoting characters in the pattern do not make any
5128 special pattern characters quoted. For example, the `*' in the
5129 following retains its special meaning: "${foo#'*'}". */
5131 getpattern (value
, quoted
, expandpat
)
5133 int quoted
, expandpat
;
5140 /* There is a problem here: how to handle single or double quotes in the
5141 pattern string when the whole expression is between double quotes?
5142 POSIX.2 says that enclosing double quotes do not cause the pattern to
5143 be quoted, but does that leave us a problem with @ and array[@] and their
5144 expansions inside a pattern? */
5146 if (expandpat
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *tword
)
5149 pat
= string_extract_double_quoted (tword
, &i
, SX_STRIPDQ
);
5155 /* expand_string_for_pat () leaves WORD quoted and does not perform
5157 l
= *value
? expand_string_for_pat (value
,
5158 (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? Q_PATQUOTE
: quoted
,
5159 (int *)NULL
, (int *)NULL
)
5162 word_list_remove_quoted_nulls (l
);
5163 pat
= string_list (l
);
5167 tword
= quote_string_for_globbing (pat
, QGLOB_CVTNULL
);
5175 /* Handle removing a pattern from a string as a result of ${name%[%]value}
5176 or ${name#[#]value}. */
5178 variable_remove_pattern (value
, pattern
, patspec
, quoted
)
5179 char *value
, *pattern
;
5180 int patspec
, quoted
;
5184 tword
= remove_pattern (value
, pattern
, patspec
);
5191 list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
)
5194 int patspec
, itype
, quoted
;
5200 for (new = (WORD_LIST
*)NULL
, l
= list
; l
; l
= l
->next
)
5202 tword
= remove_pattern (l
->word
->word
, pattern
, patspec
);
5203 w
= alloc_word_desc ();
5204 w
->word
= tword
? tword
: savestring ("");
5205 new = make_word_list (w
, new);
5208 l
= REVERSE_LIST (new, WORD_LIST
*);
5209 tword
= string_list_pos_params (itype
, l
, quoted
, 0);
5216 parameter_list_remove_pattern (itype
, pattern
, patspec
, quoted
)
5219 int patspec
, quoted
;
5224 list
= list_rest_of_args ();
5226 return ((char *)NULL
);
5227 ret
= list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
);
5228 dispose_words (list
);
5232 #if defined (ARRAY_VARS)
5234 array_remove_pattern (var
, pattern
, patspec
, starsub
, quoted
)
5238 int starsub
; /* so we can figure out how it's indexed */
5248 v
= var
; /* XXX - for now */
5250 itype
= starsub
? '*' : '@';
5252 a
= (v
&& array_p (v
)) ? array_cell (v
) : 0;
5253 h
= (v
&& assoc_p (v
)) ? assoc_cell (v
) : 0;
5255 list
= a
? array_to_word_list (a
) : (h
? assoc_to_word_list (h
) : 0);
5257 return ((char *)NULL
);
5258 ret
= list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
);
5259 dispose_words (list
);
5263 #endif /* ARRAY_VARS */
5266 parameter_brace_remove_pattern (varname
, value
, ind
, patstr
, rtype
, quoted
, flags
)
5267 char *varname
, *value
;
5270 int rtype
, quoted
, flags
;
5272 int vtype
, patspec
, starsub
;
5273 char *temp1
, *val
, *pattern
, *oname
;
5277 return ((char *)NULL
);
5279 oname
= this_command_name
;
5280 this_command_name
= varname
;
5282 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
5285 this_command_name
= oname
;
5286 return ((char *)NULL
);
5289 starsub
= vtype
& VT_STARSUB
;
5290 vtype
&= ~VT_STARSUB
;
5292 patspec
= getpatspec (rtype
, patstr
);
5293 if (patspec
== RP_LONG_LEFT
|| patspec
== RP_LONG_RIGHT
)
5296 /* Need to pass getpattern newly-allocated memory in case of expansion --
5297 the expansion code will free the passed string on an error. */
5298 temp1
= savestring (patstr
);
5299 pattern
= getpattern (temp1
, quoted
, 1);
5302 temp1
= (char *)NULL
; /* shut up gcc */
5306 case VT_ARRAYMEMBER
:
5307 temp1
= remove_pattern (val
, pattern
, patspec
);
5308 if (vtype
== VT_VARIABLE
)
5312 val
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
5313 ? quote_string (temp1
)
5314 : quote_escapes (temp1
);
5319 #if defined (ARRAY_VARS)
5321 temp1
= array_remove_pattern (v
, pattern
, patspec
, starsub
, quoted
);
5322 if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
5324 val
= quote_escapes (temp1
);
5331 temp1
= parameter_list_remove_pattern (varname
[0], pattern
, patspec
, quoted
);
5332 if (temp1
&& quoted
== 0 && ifs_is_null
)
5334 /* Posix interp 888 */
5336 else if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
5338 val
= quote_escapes (temp1
);
5345 this_command_name
= oname
;
5351 #if defined (PROCESS_SUBSTITUTION)
5353 static void reap_some_procsubs
PARAMS((int));
5355 /*****************************************************************/
5357 /* Hacking Process Substitution */
5359 /*****************************************************************/
5361 #if !defined (HAVE_DEV_FD)
5362 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
5363 of FIFOs the shell has open. unlink_fifo_list will walk the list and
5364 unlink all of them. add_fifo_list adds the name of an open FIFO to the
5365 list. NFIFO is a count of the number of FIFOs in the list. */
5366 #define FIFO_INCR 20
5368 /* PROC value of -1 means the process has been reaped and the FIFO needs to
5369 be removed. PROC value of 0 means the slot is unused. */
5375 static struct temp_fifo
*fifo_list
= (struct temp_fifo
*)NULL
;
5377 static int fifo_list_size
;
5384 for (i
= 0; i
< fifo_list_size
; i
++)
5386 if (fifo_list
[i
].file
)
5387 free (fifo_list
[i
].file
);
5388 fifo_list
[i
].file
= NULL
;
5389 fifo_list
[i
].proc
= 0;
5395 copy_fifo_list (sizep
)
5400 return (void *)NULL
;
5404 add_fifo_list (pathname
)
5409 if (nfifo
>= fifo_list_size
- 1)
5411 osize
= fifo_list_size
;
5412 fifo_list_size
+= FIFO_INCR
;
5413 fifo_list
= (struct temp_fifo
*)xrealloc (fifo_list
,
5414 fifo_list_size
* sizeof (struct temp_fifo
));
5415 for (i
= osize
; i
< fifo_list_size
; i
++)
5417 fifo_list
[i
].file
= (char *)NULL
;
5418 fifo_list
[i
].proc
= 0; /* unused */
5422 fifo_list
[nfifo
].file
= savestring (pathname
);
5430 if ((fifo_list
[i
].proc
== (pid_t
)-1) || (fifo_list
[i
].proc
> 0 && (kill(fifo_list
[i
].proc
, 0) == -1)))
5432 unlink (fifo_list
[i
].file
);
5433 free (fifo_list
[i
].file
);
5434 fifo_list
[i
].file
= (char *)NULL
;
5435 fifo_list
[i
].proc
= 0;
5447 for (i
= saved
= 0; i
< nfifo
; i
++)
5449 if ((fifo_list
[i
].proc
== (pid_t
)-1) || (fifo_list
[i
].proc
> 0 && (kill(fifo_list
[i
].proc
, 0) == -1)))
5451 unlink (fifo_list
[i
].file
);
5452 free (fifo_list
[i
].file
);
5453 fifo_list
[i
].file
= (char *)NULL
;
5454 fifo_list
[i
].proc
= 0;
5460 /* If we didn't remove some of the FIFOs, compact the list. */
5463 for (i
= j
= 0; i
< nfifo
; i
++)
5464 if (fifo_list
[i
].file
)
5468 fifo_list
[j
].file
= fifo_list
[i
].file
;
5469 fifo_list
[j
].proc
= fifo_list
[i
].proc
;
5470 fifo_list
[i
].file
= (char *)NULL
;
5471 fifo_list
[i
].proc
= 0;
5481 /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list
5482 from some point in the past, and close all open FIFOs in fifo_list
5483 that are not marked as active in LIST. If LIST is NULL, close
5484 everything in fifo_list. LSIZE is the number of elements in LIST, in
5485 case it's larger than fifo_list_size (size of fifo_list). */
5487 close_new_fifos (list
, lsize
)
5496 unlink_fifo_list ();
5500 for (plist
= (char *)list
, i
= 0; i
< lsize
; i
++)
5501 if (plist
[i
] == 0 && i
< fifo_list_size
&& fifo_list
[i
].proc
!= -1)
5504 for (i
= lsize
; i
< fifo_list_size
; i
++)
5509 find_procsub_child (pid
)
5514 for (i
= 0; i
< nfifo
; i
++)
5515 if (fifo_list
[i
].proc
== pid
)
5521 set_procsub_status (ind
, pid
, status
)
5526 if (ind
>= 0 && ind
< nfifo
)
5527 fifo_list
[ind
].proc
= (pid_t
)-1; /* sentinel */
5530 /* If we've marked the process for this procsub as dead, close the
5531 associated file descriptor and delete the FIFO. */
5533 reap_some_procsubs (max
)
5538 for (i
= 0; i
< max
; i
++)
5539 if (fifo_list
[i
].proc
== (pid_t
)-1) /* reaped */
5546 reap_some_procsubs (nfifo
);
5556 for (i
= 0; i
< nfifo
; i
++)
5558 if (fifo_list
[i
].proc
!= (pid_t
)-1 && fifo_list
[i
].proc
> 0)
5560 r
= wait_for (fifo_list
[i
].proc
, 0);
5561 save_proc_status (fifo_list
[i
].proc
, r
);
5562 fifo_list
[i
].proc
= (pid_t
)-1;
5585 tname
= sh_mktmpname ("sh-np", MT_USERANDOM
|MT_USETMPDIR
);
5586 if (mkfifo (tname
, 0600) < 0)
5589 return ((char *)NULL
);
5592 add_fifo_list (tname
);
5596 #else /* HAVE_DEV_FD */
5598 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
5599 has open to children. NFDS is a count of the number of bits currently
5600 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
5602 /* dev_fd_list[I] value of -1 means the process has been reaped and file
5603 descriptor I needs to be closed. Value of 0 means the slot is unused. */
5605 static pid_t
*dev_fd_list
= (pid_t
*)NULL
;
5607 static int totfds
; /* The highest possible number of open files. */
5628 for (i
= 0; nfds
&& i
< totfds
; i
++)
5635 copy_fifo_list (sizep
)
5640 if (nfds
== 0 || totfds
== 0)
5644 return (void *)NULL
;
5649 ret
= xmalloc (totfds
* sizeof (pid_t
));
5650 return (memcpy (ret
, dev_fd_list
, totfds
* sizeof (pid_t
)));
5657 if (dev_fd_list
== 0 || fd
>= totfds
)
5662 totfds
= getdtablesize ();
5663 if (totfds
< 0 || totfds
> 256)
5668 dev_fd_list
= (pid_t
*)xrealloc (dev_fd_list
, totfds
* sizeof (dev_fd_list
[0]));
5669 /* XXX - might need a loop for this */
5670 memset (dev_fd_list
+ ofds
, '\0', (totfds
- ofds
) * sizeof (pid_t
));
5673 dev_fd_list
[fd
] = 1; /* marker; updated later */
5680 return 0; /* used for cleanup; not needed with /dev/fd */
5693 if (dev_fd_list
[fd
])
5696 dev_fd_list
[fd
] = 0;
5709 for (i
= totfds
-1; nfds
&& i
>= 0; i
--)
5715 /* Take LIST, which is a snapshot copy of dev_fd_list from some point in
5716 the past, and close all open fds in dev_fd_list that are not marked
5717 as open in LIST. If LIST is NULL, close everything in dev_fd_list.
5718 LSIZE is the number of elements in LIST, in case it's larger than
5719 totfds (size of dev_fd_list). */
5721 close_new_fifos (list
, lsize
)
5730 unlink_fifo_list ();
5734 for (plist
= (pid_t
*)list
, i
= 0; i
< lsize
; i
++)
5735 if (plist
[i
] == 0 && i
< totfds
&& dev_fd_list
[i
])
5738 for (i
= lsize
; i
< totfds
; i
++)
5743 find_procsub_child (pid
)
5751 for (i
= 0; i
< totfds
; i
++)
5752 if (dev_fd_list
[i
] == pid
)
5759 set_procsub_status (ind
, pid
, status
)
5764 if (ind
>= 0 && ind
< totfds
)
5765 dev_fd_list
[ind
] = (pid_t
)-1; /* sentinel */
5768 /* If we've marked the process for this procsub as dead, close the
5769 associated file descriptor. */
5771 reap_some_procsubs (max
)
5776 for (i
= 0; nfds
> 0 && i
< max
; i
++)
5777 if (dev_fd_list
[i
] == (pid_t
)-1)
5784 reap_some_procsubs (totfds
);
5794 for (i
= 0; nfds
> 0 && i
< totfds
; i
++)
5796 if (dev_fd_list
[i
] != (pid_t
)-1 && dev_fd_list
[i
] > 0)
5798 r
= wait_for (dev_fd_list
[i
], 0);
5799 save_proc_status (dev_fd_list
[i
], r
);
5800 dev_fd_list
[i
] = (pid_t
)-1;
5806 #if defined (NOTDEF)
5807 print_dev_fd_list ()
5811 fprintf (stderr
, "pid %ld: dev_fd_list:", (long)getpid ());
5814 for (i
= 0; i
< totfds
; i
++)
5817 fprintf (stderr
, " %d", i
);
5819 fprintf (stderr
, "\n");
5824 make_dev_fd_filename (fd
)
5827 char *ret
, intbuf
[INT_STRLEN_BOUND (int) + 1], *p
;
5829 ret
= (char *)xmalloc (sizeof (DEV_FD_PREFIX
) + 8);
5831 strcpy (ret
, DEV_FD_PREFIX
);
5832 p
= inttostr (fd
, intbuf
, sizeof (intbuf
));
5833 strcpy (ret
+ sizeof (DEV_FD_PREFIX
) - 1, p
);
5839 #endif /* HAVE_DEV_FD */
5841 /* Return a filename that will open a connection to the process defined by
5842 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
5843 a filename in /dev/fd corresponding to a descriptor that is one of the
5844 ends of the pipe. If not defined, we use named pipes on systems that have
5845 them. Systems without /dev/fd and named pipes are out of luck.
5847 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
5848 use the read end of the pipe and dup that file descriptor to fd 0 in
5849 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
5850 writing or use the write end of the pipe in the child, and dup that
5851 file descriptor to fd 1 in the child. The parent does the opposite. */
5854 process_substitute (string
, open_for_read_in_child
)
5856 int open_for_read_in_child
;
5859 int fd
, result
, rc
, function_value
;
5861 #if defined (HAVE_DEV_FD)
5862 int parent_pipe_fd
, child_pipe_fd
;
5864 #endif /* HAVE_DEV_FD */
5865 #if defined (JOB_CONTROL)
5866 pid_t old_pipeline_pgrp
;
5869 if (!string
|| !*string
|| wordexp_only
)
5870 return ((char *)NULL
);
5872 #if !defined (HAVE_DEV_FD)
5873 pathname
= make_named_pipe ();
5874 #else /* HAVE_DEV_FD */
5875 if (pipe (fildes
) < 0)
5877 sys_error ("%s", _("cannot make pipe for process substitution"));
5878 return ((char *)NULL
);
5880 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
5881 the pipe in the parent, otherwise the read end. */
5882 parent_pipe_fd
= fildes
[open_for_read_in_child
];
5883 child_pipe_fd
= fildes
[1 - open_for_read_in_child
];
5884 /* Move the parent end of the pipe to some high file descriptor, to
5885 avoid clashes with FDs used by the script. */
5886 parent_pipe_fd
= move_to_high_fd (parent_pipe_fd
, 1, 64);
5888 pathname
= make_dev_fd_filename (parent_pipe_fd
);
5889 #endif /* HAVE_DEV_FD */
5893 sys_error ("%s", _("cannot make pipe for process substitution"));
5894 return ((char *)NULL
);
5897 old_pid
= last_made_pid
;
5899 #if defined (JOB_CONTROL)
5900 old_pipeline_pgrp
= pipeline_pgrp
;
5901 if (pipeline_pgrp
== 0 || (subshell_environment
& (SUBSHELL_PIPE
|SUBSHELL_FORK
|SUBSHELL_ASYNC
)) == 0)
5902 pipeline_pgrp
= shell_pgrp
;
5904 #endif /* JOB_CONTROL */
5906 pid
= make_child ((char *)NULL
, FORK_ASYNC
);
5909 /* The currently-executing shell is not interactive */
5912 reset_terminating_signals (); /* XXX */
5913 free_pushed_string_input ();
5914 /* Cancel traps, in trap.c. */
5915 restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */
5916 QUIT
; /* catch any interrupts we got post-fork */
5917 setup_async_signals ();
5918 if (open_for_read_in_child
== 0)
5919 async_redirect_stdin ();
5920 subshell_environment
|= SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
;
5922 /* We don't inherit the verbose option for command substitutions now, so
5923 let's try it for process substitutions. */
5924 change_flag ('v', FLAG_OFF
);
5926 /* if we're expanding a redirection, we shouldn't have access to the
5927 temporary environment, but commands in the subshell should have
5928 access to their own temporary environment. */
5929 if (expanding_redir
)
5930 flush_temporary_env ();
5933 #if defined (JOB_CONTROL)
5934 set_sigchld_handler ();
5935 stop_making_children ();
5936 /* XXX - should we only do this in the parent? (as in command subst) */
5937 pipeline_pgrp
= old_pipeline_pgrp
;
5939 stop_making_children ();
5940 #endif /* JOB_CONTROL */
5944 sys_error ("%s", _("cannot make child for process substitution"));
5946 #if defined (HAVE_DEV_FD)
5947 close (parent_pipe_fd
);
5948 close (child_pipe_fd
);
5949 #endif /* HAVE_DEV_FD */
5950 #if defined (JOB_CONTROL)
5951 restore_pipeline (1);
5953 return ((char *)NULL
);
5958 #if defined (JOB_CONTROL)
5959 last_procsub_child
= restore_pipeline (0);
5960 /* We assume that last_procsub_child->next == last_procsub_child because
5961 of how jobs.c:add_process() works. */
5962 last_procsub_child
->next
= 0;
5963 procsub_add (last_procsub_child
);
5966 #if defined (HAVE_DEV_FD)
5967 dev_fd_list
[parent_pipe_fd
] = pid
;
5969 fifo_list
[nfifo
-1].proc
= pid
;
5972 last_made_pid
= old_pid
;
5974 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
5976 #endif /* JOB_CONTROL && PGRP_PIPE */
5978 #if defined (HAVE_DEV_FD)
5979 close (child_pipe_fd
);
5980 #endif /* HAVE_DEV_FD */
5985 set_sigint_handler ();
5987 #if defined (JOB_CONTROL)
5988 /* make sure we don't have any job control */
5989 set_job_control (0);
5991 /* Clear out any existing list of process substitutions */
5994 /* The idea is that we want all the jobs we start from an async process
5995 substitution to be in the same process group, but not the same pgrp
5996 as our parent shell, since we don't want to affect our parent shell's
5997 jobs if we get a SIGHUP and end up calling hangup_all_jobs, for example.
5998 If pipeline_pgrp != shell_pgrp, we assume that there is a job control
5999 shell somewhere in our parent process chain (since make_child initializes
6000 pipeline_pgrp to shell_pgrp if job_control == 0). What we do in this
6001 case is to set pipeline_pgrp to our PID, so all jobs started by this
6002 process have that same pgrp and we are basically the process group leader.
6003 This should not have negative effects on child processes surviving
6004 after we exit, since we wait for the children we create, but that is
6005 something to watch for. */
6007 if (pipeline_pgrp
!= shell_pgrp
)
6008 pipeline_pgrp
= getpid ();
6009 #endif /* JOB_CONTROL */
6011 #if !defined (HAVE_DEV_FD)
6012 /* Open the named pipe in the child. */
6013 fd
= open (pathname
, open_for_read_in_child
? O_RDONLY
: O_WRONLY
);
6016 /* Two separate strings for ease of translation. */
6017 if (open_for_read_in_child
)
6018 sys_error (_("cannot open named pipe %s for reading"), pathname
);
6020 sys_error (_("cannot open named pipe %s for writing"), pathname
);
6024 if (open_for_read_in_child
)
6026 if (sh_unset_nodelay_mode (fd
) < 0)
6028 sys_error (_("cannot reset nodelay mode for fd %d"), fd
);
6032 #else /* HAVE_DEV_FD */
6034 #endif /* HAVE_DEV_FD */
6036 /* Discard buffered stdio output before replacing the underlying file
6038 if (open_for_read_in_child
== 0)
6041 if (dup2 (fd
, open_for_read_in_child
? 0 : 1) < 0)
6043 sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname
,
6044 open_for_read_in_child
? 0 : 1);
6048 if (fd
!= (open_for_read_in_child
? 0 : 1))
6051 /* Need to close any files that this process has open to pipes inherited
6053 if (current_fds_to_close
)
6055 close_fd_bitmap (current_fds_to_close
);
6056 current_fds_to_close
= (struct fd_bitmap
*)NULL
;
6059 #if defined (HAVE_DEV_FD)
6060 /* Make sure we close the parent's end of the pipe and clear the slot
6061 in the fd list so it is not closed later, if reallocated by, for
6062 instance, pipe(2). */
6063 close (parent_pipe_fd
);
6064 dev_fd_list
[parent_pipe_fd
] = 0;
6065 #endif /* HAVE_DEV_FD */
6067 /* subshells shouldn't have this flag, which controls using the temporary
6068 environment for variable lookups. We have already flushed the temporary
6069 environment above in the case we're expanding a redirection, so processes
6070 executed by this command need to be able to set it independently of their
6072 expanding_redir
= 0;
6074 remove_quoted_escapes (string
);
6076 /* Give process substitution a place to jump back to on failure,
6077 so we don't go back up to main (). */
6078 result
= setjmp_nosigs (top_level
);
6080 /* If we're running a process substitution inside a shell function,
6081 trap `return' so we don't return from the function in the subshell
6082 and go off to never-never land. */
6083 if (result
== 0 && return_catch_flag
)
6084 function_value
= setjmp_nosigs (return_catch
);
6088 if (result
== ERREXIT
)
6089 rc
= last_command_exit_value
;
6090 else if (result
== EXITPROG
)
6091 rc
= last_command_exit_value
;
6093 rc
= EXECUTION_FAILURE
;
6094 else if (function_value
)
6095 rc
= return_catch_value
;
6099 rc
= parse_and_execute (string
, "process substitution", (SEVAL_NONINT
|SEVAL_NOHIST
));
6100 /* leave subshell level intact for any exit trap */
6103 #if !defined (HAVE_DEV_FD)
6104 /* Make sure we close the named pipe in the child before we exit. */
6105 close (open_for_read_in_child
? 0 : 1);
6106 #endif /* !HAVE_DEV_FD */
6108 last_command_exit_value
= rc
;
6109 rc
= run_exit_trap ();
6113 #endif /* PROCESS_SUBSTITUTION */
6115 /***********************************/
6117 /* Command Substitution */
6119 /***********************************/
6122 read_comsub (fd
, quoted
, flags
, rflag
)
6123 int fd
, quoted
, flags
;
6126 char *istring
, buf
[512], *bufp
;
6127 int istring_index
, c
, tflag
, skip_ctlesc
, skip_ctlnul
;
6129 size_t istring_size
;
6132 #if defined (HANDLE_MULTIBYTE)
6139 istring
= (char *)NULL
;
6140 istring_index
= istring_size
= bufn
= tflag
= 0;
6142 skip_ctlesc
= ifs_cmap
[CTLESC
];
6143 skip_ctlnul
= ifs_cmap
[CTLNUL
];
6145 mb_cur_max
= MB_CUR_MAX
;
6148 /* Read the output of the command through the pipe. */
6155 bufn
= zread (fd
, buf
, sizeof (buf
));
6167 internal_warning ("%s", _("command substitution: ignored null byte in input"));
6174 /* Add the character to ISTRING, possibly after resizing it. */
6175 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, mb_cur_max
+1, istring_size
, 512);
6177 /* This is essentially quote_string inline */
6178 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) /* || c == CTLESC || c == CTLNUL */)
6179 istring
[istring_index
++] = CTLESC
;
6180 else if ((flags
& PF_ASSIGNRHS
) && skip_ctlesc
&& c
== CTLESC
)
6181 istring
[istring_index
++] = CTLESC
;
6182 /* Escape CTLESC and CTLNUL in the output to protect those characters
6183 from the rest of the word expansions (word splitting and globbing.)
6184 This is essentially quote_escapes inline. */
6185 else if (skip_ctlesc
== 0 && c
== CTLESC
)
6186 istring
[istring_index
++] = CTLESC
;
6187 else if ((skip_ctlnul
== 0 && c
== CTLNUL
) || (c
== ' ' && (ifs_value
&& *ifs_value
== 0)))
6188 istring
[istring_index
++] = CTLESC
;
6190 #if defined (HANDLE_MULTIBYTE)
6191 if ((locale_utf8locale
&& (c
& 0x80)) ||
6192 (locale_utf8locale
== 0 && mb_cur_max
> 1 && (unsigned char)c
> 127))
6194 /* read a multibyte character from buf */
6195 /* punt on the hard case for now */
6196 memset (&ps
, '\0', sizeof (mbstate_t));
6197 mblen
= mbrtowc (&wc
, bufp
-1, bufn
+1, &ps
);
6198 if (MB_INVALIDCH (mblen
) || mblen
== 0 || mblen
== 1)
6199 istring
[istring_index
++] = c
;
6202 istring
[istring_index
++] = c
;
6203 for (i
= 0; i
< mblen
-1; i
++)
6204 istring
[istring_index
++] = *bufp
++;
6211 istring
[istring_index
++] = c
;
6215 istring
[istring_index
] = '\0';
6217 /* If we read no output, just return now and save ourselves some
6219 if (istring_index
== 0)
6224 return (char *)NULL
;
6227 /* Strip trailing newlines from the output of the command. */
6228 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
6230 while (istring_index
> 0)
6232 if (istring
[istring_index
- 1] == '\n')
6236 /* If the newline was quoted, remove the quoting char. */
6237 if (istring
[istring_index
- 1] == CTLESC
)
6243 istring
[istring_index
] = '\0';
6246 strip_trailing (istring
, istring_index
- 1, 1);
6253 /* Perform command substitution on STRING. This returns a WORD_DESC * with the
6254 contained string possibly quoted. */
6256 command_substitute (string
, quoted
, flags
)
6261 pid_t pid
, old_pid
, old_pipeline_pgrp
, old_async_pid
;
6263 int result
, fildes
[2], function_value
, pflags
, rc
, tflag
, fork_flags
;
6267 istring
= (char *)NULL
;
6269 /* Don't fork () if there is no need to. In the case of no command to
6270 run, just return NULL. */
6272 for (s
= string
; s
&& *s
&& (shellblank (*s
) || *s
== '\n'); s
++)
6274 if (s
== 0 || *s
== 0)
6275 return ((WORD_DESC
*)NULL
);
6277 if (!string
|| !*string
|| (string
[0] == '\n' && !string
[1]))
6278 return ((WORD_DESC
*)NULL
);
6281 if (wordexp_only
&& read_but_dont_execute
)
6283 last_command_exit_value
= EX_WEXPCOMSUB
;
6284 jump_to_top_level (EXITPROG
);
6287 /* We're making the assumption here that the command substitution will
6288 eventually run a command from the file system. Since we'll run
6289 maybe_make_export_env in this subshell before executing that command,
6290 the parent shell and any other shells it starts will have to remake
6291 the environment. If we make it before we fork, other shells won't
6292 have to. Don't bother if we have any temporary variable assignments,
6293 though, because the export environment will be remade after this
6294 command completes anyway, but do it if all the words to be expanded
6295 are variable assignments. */
6296 if (subst_assign_varlist
== 0 || garglist
== 0)
6297 maybe_make_export_env (); /* XXX */
6299 /* Flags to pass to parse_and_execute() */
6300 pflags
= (interactive
&& sourcelevel
== 0) ? SEVAL_RESETLINE
: 0;
6302 old_pid
= last_made_pid
;
6304 /* Pipe the output of executing STRING into the current shell. */
6305 if (pipe (fildes
) < 0)
6307 sys_error ("%s", _("cannot make pipe for command substitution"));
6311 #if defined (JOB_CONTROL)
6312 old_pipeline_pgrp
= pipeline_pgrp
;
6313 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
6314 if ((subshell_environment
& SUBSHELL_PIPE
) == 0)
6315 pipeline_pgrp
= shell_pgrp
;
6316 cleanup_the_pipeline ();
6317 #endif /* JOB_CONTROL */
6319 old_async_pid
= last_asynchronous_pid
;
6320 fork_flags
= (subshell_environment
&SUBSHELL_ASYNC
) ? FORK_ASYNC
: 0;
6321 pid
= make_child ((char *)NULL
, fork_flags
|FORK_NOTERM
);
6322 last_asynchronous_pid
= old_async_pid
;
6326 /* Reset the signal handlers in the child, but don't free the
6327 trap strings. Set a flag noting that we have to free the
6328 trap strings if we run trap to change a signal disposition. */
6329 reset_signal_handlers ();
6332 kill (getpid (), SIGINT
);
6333 CLRINTERRUPT
; /* if we're ignoring SIGINT somehow */
6335 QUIT
; /* catch any interrupts we got post-fork */
6336 subshell_environment
|= SUBSHELL_RESETTRAP
;
6339 #if defined (JOB_CONTROL)
6340 /* XXX DO THIS ONLY IN PARENT ? XXX */
6341 set_sigchld_handler ();
6342 stop_making_children ();
6344 pipeline_pgrp
= old_pipeline_pgrp
;
6346 stop_making_children ();
6347 #endif /* JOB_CONTROL */
6351 sys_error (_("cannot make child for command substitution"));
6354 last_made_pid
= old_pid
;
6359 return ((WORD_DESC
*)NULL
);
6364 /* The currently executing shell is not interactive. */
6367 set_sigint_handler (); /* XXX */
6369 free_pushed_string_input ();
6371 /* Discard buffered stdio output before replacing the underlying file
6375 if (dup2 (fildes
[1], 1) < 0)
6377 sys_error ("%s", _("command_substitute: cannot duplicate pipe as fd 1"));
6378 exit (EXECUTION_FAILURE
);
6381 /* If standard output is closed in the parent shell
6382 (such as after `exec >&-'), file descriptor 1 will be
6383 the lowest available file descriptor, and end up in
6384 fildes[0]. This can happen for stdin and stderr as well,
6385 but stdout is more important -- it will cause no output
6386 to be generated from this command. */
6387 if ((fildes
[1] != fileno (stdin
)) &&
6388 (fildes
[1] != fileno (stdout
)) &&
6389 (fildes
[1] != fileno (stderr
)))
6392 if ((fildes
[0] != fileno (stdin
)) &&
6393 (fildes
[0] != fileno (stdout
)) &&
6394 (fildes
[0] != fileno (stderr
)))
6398 /* Let stdio know the fd may have changed from text to binary mode, and
6399 make sure to preserve stdout line buffering. */
6400 freopen (NULL
, "w", stdout
);
6401 sh_setlinebuf (stdout
);
6402 #endif /* __CYGWIN__ */
6404 /* This is a subshell environment. */
6405 subshell_environment
|= SUBSHELL_COMSUB
;
6407 /* Many shells do not appear to inherit the -v option for command
6409 change_flag ('v', FLAG_OFF
);
6411 /* When inherit_errexit option is not enabled, command substitution does
6412 not inherit the -e flag. It is enabled when Posix mode is enabled */
6413 if (inherit_errexit
== 0)
6415 builtin_ignoring_errexit
= 0;
6416 change_flag ('e', FLAG_OFF
);
6420 /* If we are expanding a redirection, we can dispose of any temporary
6421 environment we received, since redirections are not supposed to have
6422 access to the temporary environment. We will have to see whether this
6423 affects temporary environments supplied to `eval', but the temporary
6424 environment gets copied to builtin_env at some point. */
6425 if (expanding_redir
)
6427 flush_temporary_env ();
6428 expanding_redir
= 0;
6431 remove_quoted_escapes (string
);
6433 startup_state
= 2; /* see if we can avoid a fork */
6434 parse_and_execute_level
= 0;
6436 /* Give command substitution a place to jump back to on failure,
6437 so we don't go back up to main (). */
6438 result
= setjmp_nosigs (top_level
);
6440 /* If we're running a command substitution inside a shell function,
6441 trap `return' so we don't return from the function in the subshell
6442 and go off to never-never land. */
6443 if (result
== 0 && return_catch_flag
)
6444 function_value
= setjmp_nosigs (return_catch
);
6448 if (result
== ERREXIT
)
6449 rc
= last_command_exit_value
;
6450 else if (result
== EXITPROG
)
6451 rc
= last_command_exit_value
;
6453 rc
= EXECUTION_FAILURE
;
6454 else if (function_value
)
6455 rc
= return_catch_value
;
6459 rc
= parse_and_execute (string
, "command substitution", pflags
|SEVAL_NOHIST
);
6460 /* leave subshell level intact for any exit trap */
6463 last_command_exit_value
= rc
;
6464 rc
= run_exit_trap ();
6465 #if defined (PROCESS_SUBSTITUTION)
6466 unlink_fifo_list ();
6474 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
6476 #endif /* JOB_CONTROL && PGRP_PIPE */
6480 begin_unwind_frame ("read-comsub");
6481 dummyfd
= fildes
[0];
6482 add_unwind_protect (close
, dummyfd
);
6484 /* Block SIGINT while we're reading from the pipe. If the child
6485 process gets a SIGINT, it will either handle it or die, and the
6486 read will return. */
6487 BLOCK_SIGNAL (SIGINT
, set
, oset
);
6489 istring
= read_comsub (fildes
[0], quoted
, flags
, &tflag
);
6492 discard_unwind_frame ("read-comsub");
6493 UNBLOCK_SIGNAL (oset
);
6495 current_command_subst_pid
= pid
;
6496 last_command_exit_value
= wait_for (pid
, JWAIT_NOTERM
);
6497 last_command_subst_pid
= pid
;
6498 last_made_pid
= old_pid
;
6500 #if defined (JOB_CONTROL)
6501 /* If last_command_exit_value > 128, then the substituted command
6502 was terminated by a signal. If that signal was SIGINT, then send
6503 SIGINT to ourselves. This will break out of loops, for instance. */
6504 if (last_command_exit_value
== (128 + SIGINT
) && last_command_exit_signal
== SIGINT
)
6505 kill (getpid (), SIGINT
);
6506 #endif /* JOB_CONTROL */
6508 ret
= alloc_word_desc ();
6509 ret
->word
= istring
;
6516 /********************************************************
6518 * Utility functions for parameter expansion *
6520 ********************************************************/
6522 #if defined (ARRAY_VARS)
6525 array_length_reference (s
)
6536 var
= array_variable_part (s
, 0, &t
, &len
);
6538 /* If unbound variables should generate an error, report one and return
6540 if ((var
== 0 || invisible_p (var
) || (assoc_p (var
) == 0 && array_p (var
) == 0)) && unbound_vars_is_error
)
6544 set_exit_status (EXECUTION_FAILURE
);
6549 else if (var
== 0 || invisible_p (var
))
6552 /* We support a couple of expansions for variables that are not arrays.
6553 We'll return the length of the value for v[0], and 1 for v[@] or
6554 v[*]. Return 0 for everything else. */
6556 array
= array_p (var
) ? array_cell (var
) : (ARRAY
*)NULL
;
6557 h
= assoc_p (var
) ? assoc_cell (var
) : (HASH_TABLE
*)NULL
;
6559 if (ALL_ELEMENT_SUB (t
[0]) && t
[1] == RBRACK
)
6562 return (h
? assoc_num_elements (h
) : 0);
6563 else if (array_p (var
))
6564 return (array
? array_num_elements (array
) : 0);
6566 return (var_isset (var
) ? 1 : 0);
6572 akey
= expand_assignment_string_to_string (t
, 0); /* [ */
6573 t
[len
- 1] = RBRACK
;
6574 if (akey
== 0 || *akey
== 0)
6576 err_badarraysub (t
);
6580 t
= assoc_reference (assoc_cell (var
), akey
);
6585 ind
= array_expand_index (var
, t
, len
, 0);
6586 /* negative subscripts to indexed arrays count back from end */
6587 if (var
&& array_p (var
) && ind
< 0)
6588 ind
= array_max_index (array_cell (var
)) + 1 + ind
;
6591 err_badarraysub (t
);
6595 t
= array_reference (array
, ind
);
6597 t
= (ind
== 0) ? value_cell (var
) : (char *)NULL
;
6600 len
= MB_STRLEN (t
);
6603 #endif /* ARRAY_VARS */
6606 valid_brace_expansion_word (name
, var_is_special
)
6610 if (DIGIT (*name
) && all_digits (name
))
6612 else if (var_is_special
)
6614 #if defined (ARRAY_VARS)
6615 else if (valid_array_reference (name
, 0))
6617 #endif /* ARRAY_VARS */
6618 else if (legal_identifier (name
))
6625 chk_atstar (name
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
)
6628 int *quoted_dollar_atp
, *contains_dollar_at
;
6634 if (quoted_dollar_atp
)
6635 *quoted_dollar_atp
= 0;
6636 if (contains_dollar_at
)
6637 *contains_dollar_at
= 0;
6641 /* check for $@ and $* */
6642 if (name
[0] == '@' && name
[1] == 0)
6644 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
6645 *quoted_dollar_atp
= 1;
6646 if (contains_dollar_at
)
6647 *contains_dollar_at
= 1;
6650 else if (name
[0] == '*' && name
[1] == '\0' && quoted
== 0)
6652 /* Need more checks here that parallel what string_list_pos_params and
6653 param_expand do. Check expand_no_split_dollar_star and ??? */
6654 if (contains_dollar_at
&& expand_no_split_dollar_star
== 0)
6655 *contains_dollar_at
= 1;
6659 /* Now check for ${array[@]} and ${array[*]} */
6660 #if defined (ARRAY_VARS)
6661 else if (valid_array_reference (name
, 0))
6663 temp1
= mbschr (name
, LBRACK
);
6664 if (temp1
&& temp1
[1] == '@' && temp1
[2] == RBRACK
)
6666 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
6667 *quoted_dollar_atp
= 1;
6668 if (contains_dollar_at
)
6669 *contains_dollar_at
= 1;
6672 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
6673 which should result in separate words even when IFS is unset. */
6674 if (temp1
&& temp1
[1] == '*' && temp1
[2] == RBRACK
&& quoted
== 0)
6676 if (contains_dollar_at
)
6677 *contains_dollar_at
= 1;
6685 /* Parameter expand NAME, and return a new string which is the expansion,
6686 or NULL if there was no expansion. NAME is as given in ${NAMEcWORD}.
6687 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
6688 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
6689 NAME was found inside of a double-quoted expression. */
6691 parameter_brace_expand_word (name
, var_is_special
, quoted
, pflags
, indp
)
6693 int var_is_special
, quoted
, pflags
;
6710 /* Handle multiple digit arguments, as in ${11}. */
6711 if (legal_number (name
, &arg_index
))
6713 tt
= get_dollar_var_value (arg_index
);
6715 temp
= (*tt
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6717 : quote_escapes (tt
);
6719 temp
= (char *)NULL
;
6722 else if (var_is_special
) /* ${@} */
6725 tt
= (char *)xmalloc (2 + strlen (name
));
6726 tt
[sindex
= 0] = '$';
6727 strcpy (tt
+ 1, name
);
6729 ret
= param_expand (tt
, &sindex
, quoted
, (int *)NULL
, (int *)NULL
,
6730 (int *)NULL
, (int *)NULL
, pflags
);
6733 #if defined (ARRAY_VARS)
6734 else if (valid_array_reference (name
, 0))
6737 var
= array_variable_part (name
, 0, &tt
, (int *)0);
6738 /* These are the cases where word splitting will not be performed */
6739 if (pflags
& PF_ASSIGNRHS
)
6741 if (ALL_ELEMENT_SUB (tt
[0]) && tt
[1] == RBRACK
)
6743 /* Only treat as double quoted if array variable */
6744 if (var
&& (array_p (var
) || assoc_p (var
)))
6745 temp
= array_value (name
, quoted
|Q_DOUBLE_QUOTES
, AV_ASSIGNRHS
, &atype
, &ind
);
6747 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6750 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6752 /* Posix interp 888 */
6753 else if (pflags
& PF_NOSPLIT2
)
6755 /* Special cases, then general case, for each of A[@], A[*], A[n] */
6756 #if defined (HANDLE_MULTIBYTE)
6757 if (tt
[0] == '@' && tt
[1] == RBRACK
&& var
&& quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && ifs_firstc
[0] != ' ')
6759 if (tt
[0] == '@' && tt
[1] == RBRACK
&& var
&& quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && ifs_firstc
!= ' ')
6761 temp
= array_value (name
, Q_DOUBLE_QUOTES
, AV_ASSIGNRHS
, &atype
, &ind
);
6762 else if (tt
[0] == '@' && tt
[1] == RBRACK
)
6763 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6764 else if (tt
[0] == '*' && tt
[1] == RBRACK
&& expand_no_split_dollar_star
&& ifs_is_null
)
6765 temp
= array_value (name
, Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
, 0, &atype
, &ind
);
6766 else if (tt
[0] == '*' && tt
[1] == RBRACK
)
6767 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6769 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6771 else if (tt
[0] == '*' && tt
[1] == RBRACK
&& expand_no_split_dollar_star
&& ifs_is_null
)
6772 temp
= array_value (name
, Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
, 0, &atype
, &ind
);
6774 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6775 if (atype
== 0 && temp
)
6777 temp
= (*temp
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6778 ? quote_string (temp
)
6779 : quote_escapes (temp
);
6780 rflags
|= W_ARRAYIND
;
6784 else if (atype
== 1 && temp
&& QUOTED_NULL (temp
) && (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6785 rflags
|= W_HASQUOTEDNULL
;
6788 else if (var
= find_variable (name
))
6790 if (var_isset (var
) && invisible_p (var
) == 0)
6792 #if defined (ARRAY_VARS)
6794 temp
= assoc_reference (assoc_cell (var
), "0");
6795 else if (array_p (var
))
6796 temp
= array_reference (array_cell (var
), 0);
6798 temp
= value_cell (var
);
6800 temp
= value_cell (var
);
6804 temp
= (*temp
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6805 ? quote_string (temp
)
6806 : ((pflags
& PF_ASSIGNRHS
) ? quote_rhs (temp
)
6807 : quote_escapes (temp
));
6810 temp
= (char *)NULL
;
6812 else if (var
= find_variable_last_nameref (name
, 0))
6814 temp
= nameref_cell (var
);
6815 #if defined (ARRAY_VARS)
6816 /* Handle expanding nameref whose value is x[n] */
6817 if (temp
&& *temp
&& valid_array_reference (temp
, 0))
6820 goto expand_arrayref
;
6824 /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */
6825 if (temp
&& *temp
&& legal_identifier (temp
) == 0)
6827 set_exit_status (EXECUTION_FAILURE
);
6828 report_error (_("%s: invalid variable name for name reference"), temp
);
6829 temp
= &expand_param_error
;
6832 temp
= (char *)NULL
;
6835 temp
= (char *)NULL
;
6839 ret
= alloc_word_desc ();
6841 ret
->flags
|= rflags
;
6847 parameter_brace_find_indir (name
, var_is_special
, quoted
, find_nameref
)
6849 int var_is_special
, quoted
, find_nameref
;
6856 if (find_nameref
&& var_is_special
== 0 && (v
= find_variable_last_nameref (name
, 0)) &&
6857 nameref_p (v
) && (t
= nameref_cell (v
)) && *t
)
6858 return (savestring (t
));
6860 /* If var_is_special == 0, and name is not an array reference, this does
6861 more expansion than necessary. It should really look up the variable's
6862 value and not try to expand it. */
6863 pflags
= PF_IGNUNBOUND
;
6864 /* Note that we're not going to be doing word splitting here */
6867 pflags
|= PF_ASSIGNRHS
; /* suppresses word splitting */
6868 oldex
= expand_no_split_dollar_star
;
6869 expand_no_split_dollar_star
= 1;
6871 w
= parameter_brace_expand_word (name
, var_is_special
, quoted
, pflags
, 0);
6873 expand_no_split_dollar_star
= oldex
;
6876 /* Have to dequote here if necessary */
6879 temp
= ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) || var_is_special
)
6880 ? dequote_string (t
)
6881 : dequote_escapes (t
);
6885 dispose_word_desc (w
);
6890 /* Expand an indirect reference to a variable: ${!NAME} expands to the
6891 value of the variable whose name is the value of NAME. */
6893 parameter_brace_expand_indir (name
, var_is_special
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
)
6895 int var_is_special
, quoted
, pflags
;
6896 int *quoted_dollar_atp
, *contains_dollar_at
;
6902 /* See if it's a nameref first, behave in ksh93-compatible fashion.
6903 There is at least one incompatibility: given ${!foo[0]} where foo=bar,
6904 bash performs an indirect lookup on foo[0] and expands the result;
6905 ksh93 expands bar[0]. We could do that here -- there are enough usable
6906 primitives to do that -- but do not at this point. */
6907 if (var_is_special
== 0 && (v
= find_variable_last_nameref (name
, 0)))
6909 if (nameref_p (v
) && (t
= nameref_cell (v
)) && *t
)
6911 w
= alloc_word_desc ();
6912 w
->word
= savestring (t
);
6918 /* An indirect reference to a positional parameter or a special parameter
6919 is ok. Indirect references to array references, as explained above, are
6920 ok (currently). Only references to unset variables are errors at this
6922 if (legal_identifier (name
) && v
== 0)
6924 report_error (_("%s: invalid indirect expansion"), name
);
6925 w
= alloc_word_desc ();
6926 w
->word
= &expand_param_error
;
6931 t
= parameter_brace_find_indir (name
, var_is_special
, quoted
, 0);
6933 chk_atstar (t
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
);
6935 #if defined (ARRAY_VARS)
6936 /* Array references to unset variables are also an error */
6937 if (t
== 0 && valid_array_reference (name
, 0))
6939 v
= array_variable_part (name
, 0, (char **)0, (int *)0);
6942 report_error (_("%s: invalid indirect expansion"), name
);
6943 w
= alloc_word_desc ();
6944 w
->word
= &expand_param_error
;
6949 return (WORD_DESC
*)NULL
;
6954 return (WORD_DESC
*)NULL
;
6956 if (valid_brace_expansion_word (t
, SPECIAL_VAR (t
, 0)) == 0)
6958 report_error (_("%s: invalid variable name"), t
);
6960 w
= alloc_word_desc ();
6961 w
->word
= &expand_param_error
;
6966 w
= parameter_brace_expand_word (t
, SPECIAL_VAR(t
, 0), quoted
, pflags
, 0);
6972 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
6973 depending on the value of C, the separating character. C can be one of
6974 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
6975 between double quotes. */
6977 parameter_brace_expand_rhs (name
, value
, op
, quoted
, pflags
, qdollaratp
, hasdollarat
)
6979 int op
, quoted
, pflags
, *qdollaratp
, *hasdollarat
;
6983 char *t
, *t1
, *temp
, *vname
;
6984 int l_hasdollat
, sindex
;
6987 /*itrace("parameter_brace_expand_rhs: %s:%s pflags = %d", name, value, pflags);*/
6988 /* If the entire expression is between double quotes, we want to treat
6989 the value as a double-quoted string, with the exception that we strip
6990 embedded unescaped double quotes (for sh backwards compatibility). */
6991 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *value
)
6994 temp
= string_extract_double_quoted (value
, &sindex
, SX_STRIPDQ
);
6999 w
= alloc_word_desc ();
7001 l
= *temp
? expand_string_for_rhs (temp
, quoted
, op
, pflags
, &l_hasdollat
, (int *)NULL
)
7004 *hasdollarat
= l_hasdollat
|| (l
&& l
->next
);
7008 /* list_string takes multiple CTLNULs and turns them into an empty word
7009 with W_SAWQUOTEDNULL set. Turn it back into a single CTLNUL for the
7010 rest of this function and the caller. */
7011 for (tl
= l
; tl
; tl
= tl
->next
)
7013 if (tl
->word
&& (tl
->word
->word
== 0 || tl
->word
->word
[0] == 0) &&
7014 (tl
->word
->flags
| W_SAWQUOTEDNULL
))
7016 t
= make_quoted_char ('\0');
7017 FREE (tl
->word
->word
);
7019 tl
->word
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
7020 tl
->word
->flags
&= ~W_SAWQUOTEDNULL
;
7026 /* If l->next is not null, we know that TEMP contained "$@", since that
7027 is the only expansion that creates more than one word. */
7028 if (qdollaratp
&& ((l_hasdollat
&& quoted
) || l
->next
))
7030 /*itrace("parameter_brace_expand_rhs: %s:%s: l != NULL, set *qdollaratp", name, value);*/
7034 /* The expansion of TEMP returned something. We need to treat things
7035 slightly differently if L_HASDOLLAT is non-zero. If we have "$@",
7036 the individual words have already been quoted. We need to turn them
7037 into a string with the words separated by the first character of
7038 $IFS without any additional quoting, so string_list_dollar_at won't
7039 do the right thing. If IFS is null, we want "$@" to split into
7040 separate arguments, not be concatenated, so we use string_list_internal
7041 and mark the word to be split on spaces later. We use
7042 string_list_dollar_star for "$@" otherwise. */
7043 if (l
->next
&& ifs_is_null
)
7045 temp
= string_list_internal (l
, " ");
7046 w
->flags
|= W_SPLITSPACE
;
7048 else if (l_hasdollat
|| l
->next
)
7049 temp
= string_list_dollar_star (l
, quoted
, 0);
7052 temp
= string_list (l
);
7053 if (temp
&& (QUOTED_NULL (temp
) == 0) && (l
->word
->flags
& W_SAWQUOTEDNULL
))
7054 w
->flags
|= W_SAWQUOTEDNULL
; /* XXX */
7057 /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is
7058 a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the
7059 flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the
7060 expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7061 (which is more paranoia than anything else), we need to return the
7062 quoted null string and set the flags to indicate it. */
7063 if (l
->next
== 0 && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && QUOTED_NULL (temp
) && QUOTED_NULL (l
->word
->word
) && (l
->word
->flags
& W_HASQUOTEDNULL
))
7065 w
->flags
|= W_HASQUOTEDNULL
;
7066 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null, turning off qdollaratp", name, value);*/
7067 /* If we return a quoted null with L_HASDOLLARAT, we either have a
7068 construct like "${@-$@}" or "${@-${@-$@}}" with no positional
7069 parameters or a quoted expansion of "$@" with $1 == ''. In either
7070 case, we don't want to enable special handling of $@. */
7071 if (qdollaratp
&& l_hasdollat
)
7076 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && l_hasdollat
)
7078 /* Posix interp 221 changed the rules on this. The idea is that
7079 something like "$xxx$@" should expand the same as "${foo-$xxx$@}"
7080 when foo and xxx are unset. The problem is that it's not in any
7081 way backwards compatible and few other shells do it. We're eventually
7082 going to try and split the difference (heh) a little bit here. */
7083 /* l_hasdollat == 1 means we saw a quoted dollar at. */
7085 /* The brace expansion occurred between double quotes and there was
7086 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
7087 it does not expand to anything. In this case, we want to return
7088 a quoted empty string. Posix interp 888 */
7089 temp
= make_quoted_char ('\0');
7090 w
->flags
|= W_HASQUOTEDNULL
;
7091 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null", name, value);*/
7094 temp
= (char *)NULL
;
7096 if (op
== '-' || op
== '+')
7103 t1
= temp
? dequote_string (temp
) : savestring ("");
7109 (legal_variable_starter ((unsigned char)name
[1]) || DIGIT (name
[1]) || VALID_INDIR_PARAM (name
[1])))
7111 vname
= parameter_brace_find_indir (name
+ 1, SPECIAL_VAR (name
, 1), quoted
, 1);
7112 if (vname
== 0 || *vname
== 0)
7114 report_error (_("%s: invalid indirect expansion"), name
);
7118 return &expand_wdesc_error
;
7120 if (legal_identifier (vname
) == 0)
7122 report_error (_("%s: invalid variable name"), vname
);
7126 return &expand_wdesc_error
;
7130 #if defined (ARRAY_VARS)
7131 if (valid_array_reference (vname
, 0))
7132 v
= assign_array_element (vname
, t1
, 0);
7134 #endif /* ARRAY_VARS */
7135 v
= bind_variable (vname
, t1
, 0);
7137 if (v
== 0 || readonly_p (v
) || noassign_p (v
)) /* expansion error */
7139 if ((v
== 0 || readonly_p (v
)) && interactive_shell
== 0 && posixly_correct
)
7141 last_command_exit_value
= EXECUTION_FAILURE
;
7142 exp_jump_to_top_level (FORCE_EOF
);
7148 last_command_exit_value
= EX_BADUSAGE
;
7149 exp_jump_to_top_level (DISCARD
);
7153 stupidly_hack_special_variables (vname
);
7158 /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */
7160 /* If we are double-quoted or if we are not going to be performing word
7161 splitting, we want to quote the value we return appropriately, like
7162 the other expansions this function handles. */
7163 w
->word
= (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) ? quote_string (t1
) : quote_escapes (t1
);
7164 /* If we have something that's non-null, that's not a quoted null string,
7165 and we're not going to be performing word splitting (we know we're not
7166 because the operator is `='), we can forget we saw a quoted null. */
7167 if (w
->word
&& w
->word
[0] && QUOTED_NULL (w
->word
) == 0)
7168 w
->flags
&= ~W_SAWQUOTEDNULL
;
7171 /* If we convert a null string into a quoted null, make sure the caller
7173 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) && QUOTED_NULL (w
->word
))
7174 w
->flags
|= W_HASQUOTEDNULL
;
7179 /* Deal with the right hand side of a ${name:?value} expansion in the case
7180 that NAME is null or not set. If VALUE is non-null it is expanded and
7181 used as the error message to print, otherwise a standard message is
7184 parameter_brace_expand_error (name
, value
, check_null
)
7191 set_exit_status (EXECUTION_FAILURE
); /* ensure it's non-zero */
7192 if (value
&& *value
)
7194 l
= expand_string (value
, 0);
7195 temp
= string_list (l
);
7196 report_error ("%s: %s", name
, temp
? temp
: ""); /* XXX was value not "" */
7200 else if (check_null
== 0)
7201 report_error (_("%s: parameter not set"), name
);
7203 report_error (_("%s: parameter null or not set"), name
);
7205 /* Free the data we have allocated during this expansion, since we
7206 are about to longjmp out. */
7211 /* Return 1 if NAME is something for which parameter_brace_expand_length is
7214 valid_length_expression (name
)
7217 return (name
[1] == '\0' || /* ${#} */
7218 ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0') || /* special param */
7219 (DIGIT (name
[1]) && all_digits (name
+ 1)) || /* ${#11} */
7220 #if defined (ARRAY_VARS)
7221 valid_array_reference (name
+ 1, 0) || /* ${#a[7]} */
7223 legal_identifier (name
+ 1)); /* ${#PS1} */
7226 /* Handle the parameter brace expansion that requires us to return the
7227 length of a parameter. */
7229 parameter_brace_expand_length (name
)
7233 intmax_t number
, arg_index
;
7237 var
= (SHELL_VAR
*)NULL
;
7239 if (name
[1] == '\0') /* ${#} */
7240 number
= number_of_args ();
7241 else if (DOLLAR_AT_STAR (name
[1]) && name
[2] == '\0') /* ${#@}, ${#*} */
7242 number
= number_of_args ();
7243 else if ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0')
7245 /* Take the lengths of some of the shell's special parameters. */
7249 t
= which_set_flags ();
7252 t
= itos (last_command_exit_value
);
7255 t
= itos (dollar_dollar_pid
);
7258 if (last_asynchronous_pid
== NO_PID
)
7259 t
= (char *)NULL
; /* XXX - error if set -u set? */
7261 t
= itos (last_asynchronous_pid
);
7264 t
= itos (number_of_args ());
7267 number
= STRLEN (t
);
7270 #if defined (ARRAY_VARS)
7271 else if (valid_array_reference (name
+ 1, 0))
7272 number
= array_length_reference (name
+ 1);
7273 #endif /* ARRAY_VARS */
7278 if (legal_number (name
+ 1, &arg_index
)) /* ${#1} */
7280 t
= get_dollar_var_value (arg_index
);
7281 if (t
== 0 && unbound_vars_is_error
)
7283 number
= MB_STRLEN (t
);
7286 #if defined (ARRAY_VARS)
7287 else if ((var
= find_variable (name
+ 1)) && (invisible_p (var
) == 0) && (array_p (var
) || assoc_p (var
)))
7290 t
= assoc_reference (assoc_cell (var
), "0");
7292 t
= array_reference (array_cell (var
), 0);
7293 if (t
== 0 && unbound_vars_is_error
)
7295 number
= MB_STRLEN (t
);
7298 /* Fast path for the common case of taking the length of a non-dynamic
7299 scalar variable value. */
7300 else if ((var
|| (var
= find_variable (name
+ 1))) &&
7301 invisible_p (var
) == 0 &&
7302 array_p (var
) == 0 && assoc_p (var
) == 0 &&
7303 var
->dynamic_value
== 0)
7304 number
= value_cell (var
) ? MB_STRLEN (value_cell (var
)) : 0;
7305 else if (var
== 0 && unbound_vars_is_error
== 0)
7309 newname
= savestring (name
);
7311 list
= expand_string (newname
, Q_DOUBLE_QUOTES
);
7312 t
= list
? string_list (list
) : (char *)NULL
;
7315 dispose_words (list
);
7317 number
= t
? MB_STRLEN (t
) : 0;
7325 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
7326 so we do some ad-hoc parsing of an arithmetic expression to find
7327 the first DELIM, instead of using strchr(3). Two rules:
7328 1. If the substring contains a `(', read until closing `)'.
7329 2. If the substring contains a `?', read past one `:' for each `?'.
7330 The SD_ARITHEXP flag to skip_to_delim takes care of doing this.
7334 skiparith (substr
, delim
)
7344 i
= skip_to_delim (substr
, 0, delims
, SD_ARITHEXP
);
7345 return (substr
+ i
);
7348 /* Verify and limit the start and end of the desired substring. If
7349 VTYPE == 0, a regular shell variable is being used; if it is 1,
7350 then the positional parameters are being used; if it is 2, then
7351 VALUE is really a pointer to an array variable that should be used.
7352 Return value is 1 if both values were OK, 0 if there was a problem
7353 with an invalid expression, or -1 if the values were out of range. */
7355 verify_substring_values (v
, value
, substr
, vtype
, e1p
, e2p
)
7357 char *value
, *substr
;
7359 intmax_t *e1p
, *e2p
;
7361 char *t
, *temp1
, *temp2
;
7364 #if defined (ARRAY_VARS)
7369 /* duplicate behavior of strchr(3) */
7370 t
= skiparith (substr
, ':');
7371 if (*t
&& *t
== ':')
7376 temp1
= expand_arith_string (substr
, Q_DOUBLE_QUOTES
);
7377 *e1p
= evalexp (temp1
, 0, &expok
); /* XXX - EXP_EXPANDED? */
7382 len
= -1; /* paranoia */
7386 case VT_ARRAYMEMBER
:
7387 len
= MB_STRLEN (value
);
7390 len
= number_of_args () + 1;
7392 len
++; /* add one arg if counting from $0 */
7394 #if defined (ARRAY_VARS)
7396 /* For arrays, the first value deals with array indices. Negative
7397 offsets count from one past the array's maximum index. Associative
7398 arrays treat the number of elements as the maximum index. */
7402 len
= assoc_num_elements (h
) + (*e1p
< 0);
7407 len
= array_max_index (a
) + (*e1p
< 0); /* arrays index from 0 to n - 1 */
7413 if (len
== -1) /* paranoia */
7416 if (*e1p
< 0) /* negative offsets count from end */
7419 if (*e1p
> len
|| *e1p
< 0)
7422 #if defined (ARRAY_VARS)
7423 /* For arrays, the second offset deals with the number of elements. */
7424 if (vtype
== VT_ARRAYVAR
)
7425 len
= assoc_p (v
) ? assoc_num_elements (h
) : array_num_elements (a
);
7431 temp2
= savestring (t
);
7432 temp1
= expand_arith_string (temp2
, Q_DOUBLE_QUOTES
);
7435 *e2p
= evalexp (temp1
, 0, &expok
); /* XXX - EXP_EXPANDED? */
7440 /* Should we allow positional parameter length < 0 to count backwards
7441 from end of positional parameters? */
7443 if ((vtype
== VT_ARRAYVAR
|| vtype
== VT_POSPARMS
) && *e2p
< 0)
7444 #else /* XXX - TAG: bash-5.1 */
7445 if (vtype
== VT_ARRAYVAR
&& *e2p
< 0)
7448 internal_error (_("%s: substring expression < 0"), t
);
7451 #if defined (ARRAY_VARS)
7452 /* In order to deal with sparse arrays, push the intelligence about how
7453 to deal with the number of elements desired down to the array-
7454 specific functions. */
7455 if (vtype
!= VT_ARRAYVAR
)
7461 if (*e2p
< 0 || *e2p
< *e1p
)
7463 internal_error (_("%s: substring expression < 0"), t
);
7468 *e2p
+= *e1p
; /* want E2 chars starting at E1 */
7479 /* Return the type of variable specified by VARNAME (simple variable,
7480 positional param, or array variable). Also return the value specified
7481 by VARNAME (value of a variable or a reference to an array element).
7482 QUOTED is the standard description of quoting state, using Q_* defines.
7483 FLAGS is currently a set of flags to pass to array_value. If IND is
7484 non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is
7485 passed to array_value so the array index is not computed again.
7486 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
7487 characters in the value are quoted with CTLESC and takes appropriate
7488 steps. For convenience, *VALP is set to the dequoted VALUE. */
7490 get_var_and_type (varname
, value
, ind
, quoted
, flags
, varp
, valp
)
7491 char *varname
, *value
;
7497 int vtype
, want_indir
;
7502 want_indir
= *varname
== '!' &&
7503 (legal_variable_starter ((unsigned char)varname
[1]) || DIGIT (varname
[1])
7504 || VALID_INDIR_PARAM (varname
[1]));
7506 vname
= parameter_brace_find_indir (varname
+1, SPECIAL_VAR (varname
, 1), quoted
, 1);
7507 /* XXX - what if vname == 0 || *vname == 0 ? */
7513 vtype
= VT_VARIABLE
;
7514 *varp
= (SHELL_VAR
*)NULL
;
7515 *valp
= (char *)NULL
;
7519 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
7520 vtype
= STR_DOLLAR_AT_STAR (vname
);
7521 if (vtype
== VT_POSPARMS
&& vname
[0] == '*')
7522 vtype
|= VT_STARSUB
;
7523 *varp
= (SHELL_VAR
*)NULL
;
7525 #if defined (ARRAY_VARS)
7526 if (valid_array_reference (vname
, 0))
7528 v
= array_variable_part (vname
, 0, &temp
, (int *)0);
7529 /* If we want to signal array_value to use an already-computed index,
7530 set LIND to that index */
7531 lind
= (ind
!= INTMAX_MIN
&& (flags
& AV_USEIND
)) ? ind
: 0;
7532 if (v
&& invisible_p (v
))
7534 vtype
= VT_ARRAYMEMBER
;
7535 *varp
= (SHELL_VAR
*)NULL
;
7536 *valp
= (char *)NULL
;
7538 if (v
&& (array_p (v
) || assoc_p (v
)))
7540 if (ALL_ELEMENT_SUB (temp
[0]) && temp
[1] == RBRACK
)
7542 /* Callers have to differentiate between indexed and associative */
7543 vtype
= VT_ARRAYVAR
;
7545 vtype
|= VT_STARSUB
;
7546 *valp
= array_p (v
) ? (char *)array_cell (v
) : (char *)assoc_cell (v
);
7550 vtype
= VT_ARRAYMEMBER
;
7551 *valp
= array_value (vname
, Q_DOUBLE_QUOTES
, flags
, (int *)NULL
, &lind
);
7555 else if (v
&& (ALL_ELEMENT_SUB (temp
[0]) && temp
[1] == RBRACK
))
7557 vtype
= VT_VARIABLE
;
7559 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
7560 *valp
= value
? dequote_string (value
) : (char *)NULL
;
7562 *valp
= value
? dequote_escapes (value
) : (char *)NULL
;
7566 vtype
= VT_ARRAYMEMBER
;
7568 *valp
= array_value (vname
, Q_DOUBLE_QUOTES
, flags
, (int *)NULL
, &lind
);
7571 else if ((v
= find_variable (vname
)) && (invisible_p (v
) == 0) && (assoc_p (v
) || array_p (v
)))
7573 vtype
= VT_ARRAYMEMBER
;
7575 *valp
= assoc_p (v
) ? assoc_reference (assoc_cell (v
), "0") : array_reference (array_cell (v
), 0);
7580 if (value
&& vtype
== VT_VARIABLE
)
7582 *varp
= find_variable (vname
);
7583 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
7584 *valp
= dequote_string (value
);
7586 *valp
= dequote_escapes (value
);
7598 /***********************************************************/
7600 /* Functions to perform transformations on variable values */
7602 /***********************************************************/
7605 string_var_assignment (v
, s
)
7609 char flags
[MAX_ATTRIBUTES
], *ret
, *val
;
7612 val
= (v
&& (invisible_p (v
) || var_isset (v
) == 0)) ? (char *)NULL
: sh_quote_reusable (s
, 0);
7613 i
= var_attribute_string (v
, 0, flags
);
7614 if (i
== 0 && val
== 0)
7615 return (char *)NULL
;
7617 ret
= (char *)xmalloc (i
+ STRLEN (val
) + strlen (v
->name
) + 16 + MAX_ATTRIBUTES
);
7618 if (i
> 0 && val
== 0)
7619 sprintf (ret
, "declare -%s %s", flags
, v
->name
);
7621 sprintf (ret
, "declare -%s %s=%s", flags
, v
->name
, val
);
7623 sprintf (ret
, "%s=%s", v
->name
, val
);
7628 #if defined (ARRAY_VARS)
7630 array_var_assignment (v
, itype
, quoted
, atype
)
7632 int itype
, quoted
, atype
;
7634 char *ret
, *val
, flags
[MAX_ATTRIBUTES
];
7638 return (char *)NULL
;
7640 val
= array_p (v
) ? array_to_kvpair (array_cell (v
), 0)
7641 : assoc_to_kvpair (assoc_cell (v
), 0);
7643 val
= array_p (v
) ? array_to_assign (array_cell (v
), 0)
7644 : assoc_to_assign (assoc_cell (v
), 0);
7646 if (val
== 0 && (invisible_p (v
) || var_isset (v
) == 0))
7650 val
= (char *)xmalloc (3);
7657 ret
= (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) ? quote_string (val
) : quote_escapes (val
);
7665 i
= var_attribute_string (v
, 0, flags
);
7666 ret
= (char *)xmalloc (i
+ STRLEN (val
) + strlen (v
->name
) + 16);
7668 sprintf (ret
, "declare -%s %s=%s", flags
, v
->name
, val
);
7670 sprintf (ret
, "declare -%s %s", flags
, v
->name
);
7677 pos_params_assignment (list
, itype
, quoted
)
7684 /* first, we transform the list to quote each word. */
7685 temp
= list_transform ('Q', (SHELL_VAR
*)0, list
, itype
, quoted
);
7686 ret
= (char *)xmalloc (strlen (temp
) + 8);
7687 strcpy (ret
, "set -- ");
7688 strcpy (ret
+ 7, temp
);
7694 string_transform (xc
, v
, s
)
7699 char *ret
, flags
[MAX_ATTRIBUTES
], *t
;
7702 if (((xc
== 'A' || xc
== 'a') && v
== 0))
7703 return (char *)NULL
;
7704 else if (xc
!= 'a' && xc
!= 'A' && s
== 0)
7705 return (char *)NULL
;
7709 /* Transformations that interrogate the variable */
7711 i
= var_attribute_string (v
, 0, flags
);
7712 ret
= (i
> 0) ? savestring (flags
) : (char *)NULL
;
7715 ret
= string_var_assignment (v
, s
);
7718 ret
= sh_quote_reusable (s
, 0);
7720 /* Transformations that modify the variable's value */
7722 t
= ansiexpand (s
, 0, strlen (s
), (int *)0);
7723 ret
= dequote_escapes (t
);
7727 ret
= decode_prompt_string (s
);
7730 ret
= sh_quote_reusable (s
, 0);
7733 ret
= sh_modcase (s
, 0, CASE_UPPER
);
7736 ret
= sh_modcase (s
, 0, CASE_UPFIRST
); /* capitalize */
7739 ret
= sh_modcase (s
, 0, CASE_LOWER
);
7749 list_transform (xc
, v
, list
, itype
, quoted
)
7760 for (new = (WORD_LIST
*)NULL
, l
= list
; l
; l
= l
->next
)
7762 tword
= string_transform (xc
, v
, l
->word
->word
);
7763 w
= alloc_word_desc ();
7764 w
->word
= tword
? tword
: savestring (""); /* XXX */
7765 new = make_word_list (w
, new);
7767 l
= REVERSE_LIST (new, WORD_LIST
*);
7770 /* If we are expanding in a context where word splitting will not be
7771 performed, treat as quoted. This changes how $* will be expanded. */
7772 if (itype
== '*' && expand_no_split_dollar_star
&& ifs_is_null
)
7773 qflags
|= Q_DOUBLE_QUOTES
; /* Posix interp 888 */
7775 tword
= string_list_pos_params (itype
, l
, qflags
, 0);
7782 parameter_list_transform (xc
, itype
, quoted
)
7790 list
= list_rest_of_args ();
7792 return ((char *)NULL
);
7794 ret
= pos_params_assignment (list
, itype
, quoted
);
7796 ret
= list_transform (xc
, (SHELL_VAR
*)0, list
, itype
, quoted
);
7797 dispose_words (list
);
7801 #if defined (ARRAY_VARS)
7803 array_transform (xc
, var
, starsub
, quoted
)
7806 int starsub
; /* so we can figure out how it's indexed */
7816 v
= var
; /* XXX - for now */
7818 itype
= starsub
? '*' : '@';
7821 return (array_var_assignment (v
, itype
, quoted
, 1));
7823 return (array_var_assignment (v
, itype
, quoted
, 2));
7825 /* special case for unset arrays and attributes */
7826 if (xc
== 'a' && (invisible_p (v
) || var_isset (v
) == 0))
7828 char flags
[MAX_ATTRIBUTES
];
7831 i
= var_attribute_string (v
, 0, flags
);
7832 return ((i
> 0) ? savestring (flags
) : (char *)NULL
);
7835 a
= (v
&& array_p (v
)) ? array_cell (v
) : 0;
7836 h
= (v
&& assoc_p (v
)) ? assoc_cell (v
) : 0;
7838 list
= a
? array_to_word_list (a
) : (h
? assoc_to_word_list (h
) : 0);
7840 return ((char *)NULL
);
7841 ret
= list_transform (xc
, v
, list
, itype
, quoted
);
7842 dispose_words (list
);
7846 #endif /* ARRAY_VARS */
7849 parameter_brace_transform (varname
, value
, ind
, xform
, rtype
, quoted
, pflags
, flags
)
7850 char *varname
, *value
;
7853 int rtype
, quoted
, pflags
, flags
;
7855 int vtype
, xc
, starsub
;
7856 char *temp1
, *val
, *oname
;
7860 if (value
== 0 && xc
!= 'A' && xc
!= 'a')
7861 return ((char *)NULL
);
7863 oname
= this_command_name
;
7864 this_command_name
= varname
;
7866 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
7869 this_command_name
= oname
;
7870 return ((char *)NULL
);
7873 /* check for valid values of xc */
7876 case 'a': /* expand to a string with just attributes */
7877 case 'A': /* expand as an assignment statement with attributes */
7878 case 'K': /* expand assoc array to list of key/value pairs */
7879 case 'E': /* expand like $'...' */
7880 case 'P': /* expand like prompt string */
7881 case 'Q': /* quote reusably */
7882 case 'U': /* transform to uppercase */
7883 case 'u': /* tranform by capitalizing */
7884 case 'L': /* transform to lowercase */
7887 this_command_name
= oname
;
7888 return &expand_param_error
;
7891 starsub
= vtype
& VT_STARSUB
;
7892 vtype
&= ~VT_STARSUB
;
7894 /* If we are asked to display the attributes of an unset variable, V will
7895 be NULL after the call to get_var_and_type. Double-check here. */
7896 if ((xc
== 'a' || xc
== 'A') && vtype
== VT_VARIABLE
&& varname
&& v
== 0)
7897 v
= find_variable (varname
);
7899 temp1
= (char *)NULL
; /* shut up gcc */
7903 case VT_ARRAYMEMBER
:
7904 temp1
= string_transform (xc
, v
, val
);
7905 if (vtype
== VT_VARIABLE
)
7909 val
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
7910 ? quote_string (temp1
)
7911 : quote_escapes (temp1
);
7916 #if defined (ARRAY_VARS)
7918 temp1
= array_transform (xc
, v
, starsub
, quoted
);
7919 if (temp1
&& quoted
== 0 && ifs_is_null
)
7921 /* Posix interp 888 */
7923 else if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
7925 val
= quote_escapes (temp1
);
7932 temp1
= parameter_list_transform (xc
, varname
[0], quoted
);
7933 if (temp1
&& quoted
== 0 && ifs_is_null
)
7935 /* Posix interp 888 */
7937 else if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
7939 val
= quote_escapes (temp1
);
7946 this_command_name
= oname
;
7950 /******************************************************/
7952 /* Functions to extract substrings of variable values */
7954 /******************************************************/
7956 #if defined (HANDLE_MULTIBYTE)
7957 /* Character-oriented rather than strictly byte-oriented substrings. S and
7958 E, rather being strict indices into STRING, indicate character (possibly
7959 multibyte character) positions that require calculation.
7960 Used by the ${param:offset[:length]} expansion. */
7962 mb_substring (string
, s
, e
)
7972 /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
7973 slen
= (MB_CUR_MAX
> 1) ? STRLEN (string
) : 0;
7976 while (string
[start
] && i
--)
7977 ADVANCE_CHAR (string
, slen
, start
);
7980 while (string
[stop
] && i
--)
7981 ADVANCE_CHAR (string
, slen
, stop
);
7982 tt
= substring (string
, start
, stop
);
7987 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
7988 is `@', use the positional parameters; otherwise, use the value of
7989 VARNAME. If VARNAME is an array variable, use the array elements. */
7992 parameter_brace_substring (varname
, value
, ind
, substr
, quoted
, pflags
, flags
)
7993 char *varname
, *value
;
7996 int quoted
, pflags
, flags
;
7999 int vtype
, r
, starsub
;
8000 char *temp
, *val
, *tt
, *oname
;
8003 if (value
== 0 && ((varname
[0] != '@' && varname
[0] != '*') || varname
[1]))
8004 return ((char *)NULL
);
8006 oname
= this_command_name
;
8007 this_command_name
= varname
;
8009 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
8012 this_command_name
= oname
;
8013 return ((char *)NULL
);
8016 starsub
= vtype
& VT_STARSUB
;
8017 vtype
&= ~VT_STARSUB
;
8019 r
= verify_substring_values (v
, val
, substr
, vtype
, &e1
, &e2
);
8020 this_command_name
= oname
;
8023 if (vtype
== VT_VARIABLE
)
8025 return ((r
== 0) ? &expand_param_error
: (char *)NULL
);
8031 case VT_ARRAYMEMBER
:
8032 #if defined (HANDLE_MULTIBYTE)
8034 tt
= mb_substring (val
, e1
, e2
);
8037 tt
= substring (val
, e1
, e2
);
8039 if (vtype
== VT_VARIABLE
)
8041 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
8042 temp
= quote_string (tt
);
8044 temp
= tt
? quote_escapes (tt
) : (char *)NULL
;
8049 if (vtype
== VT_POSPARMS
)
8050 tt
= pos_params (varname
, e1
, e2
, quoted
, pflags
);
8051 #if defined (ARRAY_VARS)
8052 /* assoc_subrange and array_subrange both call string_list_pos_params,
8053 so we can treat this case just like VT_POSPARAMS. */
8054 else if (assoc_p (v
))
8055 /* we convert to list and take first e2 elements starting at e1th
8056 element -- officially undefined for now */
8057 tt
= assoc_subrange (assoc_cell (v
), e1
, e2
, starsub
, quoted
, pflags
);
8059 /* We want E2 to be the number of elements desired (arrays can be
8060 sparse, so verify_substring_values just returns the numbers
8061 specified and we rely on array_subrange to understand how to
8063 tt
= array_subrange (array_cell (v
), e1
, e2
, starsub
, quoted
, pflags
);
8065 /* We want to leave this alone in every case where pos_params/
8066 string_list_pos_params quotes the list members */
8067 if (tt
&& quoted
== 0 && ifs_is_null
)
8069 temp
= tt
; /* Posix interp 888 */
8071 else if (tt
&& quoted
== 0 && (pflags
& PF_ASSIGNRHS
))
8073 temp
= tt
; /* Posix interp 888 */
8075 else if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) == 0)
8077 temp
= tt
? quote_escapes (tt
) : (char *)NULL
;
8085 temp
= (char *)NULL
;
8091 /****************************************************************/
8093 /* Functions to perform pattern substitution on variable values */
8095 /****************************************************************/
8097 #ifdef INCLUDE_UNUSED
8099 shouldexp_replacement (s
)
8104 for (p
= s
; p
&& *p
; p
++)
8116 pat_subst (string
, pat
, rep
, mflags
)
8117 char *string
, *pat
, *rep
;
8120 char *ret
, *s
, *e
, *str
, *rstr
, *mstr
, *send
;
8121 int rptr
, mtype
, rxpand
, mlen
;
8122 size_t rsize
, l
, replen
, rslen
;
8126 return (savestring (""));
8128 mtype
= mflags
& MATCH_TYPEMASK
;
8130 #if 0 /* TAG: bash-5.2? */
8131 rxpand
= (rep
&& *rep
) ? shouldexp_replacement (rep
) : 0;
8137 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
8138 * with REP and return the result.
8139 * 2. A null pattern with mtype == MATCH_END means to append REP to
8140 * STRING and return the result.
8141 * 3. A null STRING with a matching pattern means to append REP to
8142 * STRING and return the result.
8143 * These don't understand or process `&' in the replacement string.
8145 if ((pat
== 0 || *pat
== 0) && (mtype
== MATCH_BEG
|| mtype
== MATCH_END
))
8147 replen
= STRLEN (rep
);
8148 l
= STRLEN (string
);
8149 ret
= (char *)xmalloc (replen
+ l
+ 2);
8151 strcpy (ret
, string
);
8152 else if (mtype
== MATCH_BEG
)
8155 strcpy (ret
+ replen
, string
);
8159 strcpy (ret
, string
);
8160 strcpy (ret
+ l
, rep
);
8164 else if (*string
== 0 && (match_pattern (string
, pat
, mtype
, &s
, &e
) != 0))
8166 replen
= STRLEN (rep
);
8167 ret
= (char *)xmalloc (replen
+ 1);
8175 ret
= (char *)xmalloc (rsize
= 64);
8177 send
= string
+ strlen (string
);
8179 for (replen
= STRLEN (rep
), rptr
= 0, str
= string
; *str
;)
8181 if (match_pattern (str
, pat
, mtype
, &s
, &e
) == 0)
8189 mstr
= xmalloc (mlen
+ 1);
8190 for (x
= 0; x
< mlen
; x
++)
8193 rstr
= strcreplace (rep
, '&', mstr
, 0);
8195 rslen
= strlen (rstr
);
8203 RESIZE_MALLOCED_BUFFER (ret
, rptr
, (l
+ rslen
), rsize
, 64);
8205 /* OK, now copy the leading unmatched portion of the string (from
8206 str to s) to ret starting at rptr (the current offset). Then copy
8207 the replacement string at ret + rptr + (s - str). Increment
8208 rptr (if necessary) and str and go on. */
8211 strncpy (ret
+ rptr
, str
, l
);
8216 strncpy (ret
+ rptr
, rstr
, rslen
);
8219 str
= e
; /* e == end of match */
8224 if (((mflags
& MATCH_GLOBREP
) == 0) || mtype
!= MATCH_ANY
)
8229 /* On a zero-length match, make sure we copy one character, since
8230 we increment one character to avoid infinite recursion. */
8231 char *p
, *origp
, *origs
;
8234 RESIZE_MALLOCED_BUFFER (ret
, rptr
, locale_mb_cur_max
, rsize
, 64);
8235 #if defined (HANDLE_MULTIBYTE)
8236 p
= origp
= ret
+ rptr
;
8238 COPY_CHAR_P (p
, str
, send
);
8242 ret
[rptr
++] = *str
++;
8243 e
++; /* avoid infinite recursion on zero-length match */
8248 /* Now copy the unmatched portion of the input string */
8251 RESIZE_MALLOCED_BUFFER (ret
, rptr
, STRLEN(str
) + 1, rsize
, 64);
8252 strcpy (ret
+ rptr
, str
);
8260 /* Do pattern match and replacement on the positional parameters. */
8262 pos_params_pat_subst (string
, pat
, rep
, mflags
)
8263 char *string
, *pat
, *rep
;
8266 WORD_LIST
*save
, *params
;
8269 int pchar
, qflags
, pflags
;
8271 save
= params
= list_rest_of_args ();
8273 return ((char *)NULL
);
8275 for ( ; params
; params
= params
->next
)
8277 ret
= pat_subst (params
->word
->word
, pat
, rep
, mflags
);
8278 w
= alloc_word_desc ();
8279 w
->word
= ret
? ret
: savestring ("");
8280 dispose_word (params
->word
);
8284 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
8285 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
8286 pflags
= (mflags
& MATCH_ASSIGNRHS
) == MATCH_ASSIGNRHS
? PF_ASSIGNRHS
: 0;
8288 /* If we are expanding in a context where word splitting will not be
8289 performed, treat as quoted. This changes how $* will be expanded. */
8290 if (pchar
== '*' && (mflags
& MATCH_ASSIGNRHS
) && expand_no_split_dollar_star
&& ifs_is_null
)
8291 qflags
|= Q_DOUBLE_QUOTES
; /* Posix interp 888 */
8293 ret
= string_list_pos_params (pchar
, save
, qflags
, pflags
);
8294 dispose_words (save
);
8299 /* Perform pattern substitution on VALUE, which is the expansion of
8300 VARNAME. PATSUB is an expression supplying the pattern to match
8301 and the string to substitute. QUOTED is a flags word containing
8302 the type of quoting currently in effect. */
8304 parameter_brace_patsub (varname
, value
, ind
, patsub
, quoted
, pflags
, flags
)
8305 char *varname
, *value
;
8308 int quoted
, pflags
, flags
;
8310 int vtype
, mflags
, starsub
, delim
;
8311 char *val
, *temp
, *pat
, *rep
, *p
, *lpatsub
, *tt
, *oname
;
8315 return ((char *)NULL
);
8317 oname
= this_command_name
;
8318 this_command_name
= varname
; /* error messages */
8320 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
8323 this_command_name
= oname
;
8324 return ((char *)NULL
);
8327 starsub
= vtype
& VT_STARSUB
;
8328 vtype
&= ~VT_STARSUB
;
8331 /* PATSUB is never NULL when this is called. */
8334 mflags
|= MATCH_GLOBREP
;
8338 /* Malloc this because expand_string_if_necessary or one of the expansion
8339 functions in its call chain may free it on a substitution error. */
8340 lpatsub
= savestring (patsub
);
8342 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
8343 mflags
|= MATCH_QUOTED
;
8346 mflags
|= MATCH_STARSUB
;
8348 if (pflags
& PF_ASSIGNRHS
)
8349 mflags
|= MATCH_ASSIGNRHS
;
8351 /* If the pattern starts with a `/', make sure we skip over it when looking
8352 for the replacement delimiter. */
8353 delim
= skip_to_delim (lpatsub
, ((*patsub
== '/') ? 1 : 0), "/", 0);
8354 if (lpatsub
[delim
] == '/')
8357 rep
= lpatsub
+ delim
+ 1;
8362 if (rep
&& *rep
== '\0')
8365 /* Perform the same expansions on the pattern as performed by the
8366 pattern removal expansions. */
8367 pat
= getpattern (lpatsub
, quoted
, 1);
8371 /* We want to perform quote removal on the expanded replacement even if
8372 the entire expansion is double-quoted because the parser and string
8373 extraction functions treated quotes in the replacement string as
8374 special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */
8375 if (shell_compatibility_level
> 42)
8376 rep
= expand_string_if_necessary (rep
, quoted
& ~(Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
), expand_string_unsplit
);
8377 /* This is the bash-4.2 code. */
8378 else if ((mflags
& MATCH_QUOTED
) == 0)
8379 rep
= expand_string_if_necessary (rep
, quoted
, expand_string_unsplit
);
8381 rep
= expand_string_to_string_internal (rep
, quoted
, expand_string_unsplit
);
8384 /* ksh93 doesn't allow the match specifier to be a part of the expanded
8385 pattern. This is an extension. Make sure we don't anchor the pattern
8386 at the beginning or end of the string if we're doing global replacement,
8389 if (mflags
& MATCH_GLOBREP
)
8390 mflags
|= MATCH_ANY
;
8391 else if (pat
&& pat
[0] == '#')
8393 mflags
|= MATCH_BEG
;
8396 else if (pat
&& pat
[0] == '%')
8398 mflags
|= MATCH_END
;
8402 mflags
|= MATCH_ANY
;
8404 /* OK, we now want to substitute REP for PAT in VAL. If
8405 flags & MATCH_GLOBREP is non-zero, the substitution is done
8406 everywhere, otherwise only the first occurrence of PAT is
8407 replaced. The pattern matching code doesn't understand
8408 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
8409 values passed in (VT_VARIABLE) so the pattern substitution
8410 code works right. We need to requote special chars after
8411 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
8412 other cases if QUOTED == 0, since the posparams and arrays
8413 indexed by * or @ do special things when QUOTED != 0. */
8418 case VT_ARRAYMEMBER
:
8419 temp
= pat_subst (val
, p
, rep
, mflags
);
8420 if (vtype
== VT_VARIABLE
)
8424 tt
= (mflags
& MATCH_QUOTED
) ? quote_string (temp
) : quote_escapes (temp
);
8430 /* This does the right thing for the case where we are not performing
8431 word splitting. MATCH_STARSUB restricts it to ${* /foo/bar}, and
8432 pos_params_pat_subst/string_list_pos_params will do the right thing
8433 in turn for the case where ifs_is_null. Posix interp 888 */
8434 if ((pflags
& PF_NOSPLIT2
) && (mflags
& MATCH_STARSUB
))
8435 mflags
|= MATCH_ASSIGNRHS
;
8436 temp
= pos_params_pat_subst (val
, p
, rep
, mflags
);
8437 if (temp
&& quoted
== 0 && ifs_is_null
)
8439 /* Posix interp 888 */
8441 else if (temp
&& quoted
== 0 && (pflags
& PF_ASSIGNRHS
))
8443 /* Posix interp 888 */
8445 else if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
8447 tt
= quote_escapes (temp
);
8452 #if defined (ARRAY_VARS)
8454 /* If we are expanding in a context where word splitting will not be
8455 performed, treat as quoted. This changes how ${A[*]} will be
8456 expanded to make it identical to $*. */
8457 if ((mflags
& MATCH_STARSUB
) && (mflags
& MATCH_ASSIGNRHS
) && ifs_is_null
)
8458 mflags
|= MATCH_QUOTED
; /* Posix interp 888 */
8460 /* these eventually call string_list_pos_params */
8462 temp
= assoc_patsub (assoc_cell (v
), p
, rep
, mflags
);
8464 temp
= array_patsub (array_cell (v
), p
, rep
, mflags
);
8466 if (temp
&& quoted
== 0 && ifs_is_null
)
8468 /* Posix interp 888 */
8470 else if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
8472 tt
= quote_escapes (temp
);
8484 this_command_name
= oname
;
8489 /****************************************************************/
8491 /* Functions to perform case modification on variable values */
8493 /****************************************************************/
8495 /* Do case modification on the positional parameters. */
8498 pos_params_modcase (string
, pat
, modop
, mflags
)
8503 WORD_LIST
*save
, *params
;
8506 int pchar
, qflags
, pflags
;
8508 save
= params
= list_rest_of_args ();
8510 return ((char *)NULL
);
8512 for ( ; params
; params
= params
->next
)
8514 ret
= sh_modcase (params
->word
->word
, pat
, modop
);
8515 w
= alloc_word_desc ();
8516 w
->word
= ret
? ret
: savestring ("");
8517 dispose_word (params
->word
);
8521 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
8522 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
8523 pflags
= (mflags
& MATCH_ASSIGNRHS
) == MATCH_ASSIGNRHS
? PF_ASSIGNRHS
: 0;
8525 /* If we are expanding in a context where word splitting will not be
8526 performed, treat as quoted. This changes how $* will be expanded. */
8527 if (pchar
== '*' && (mflags
& MATCH_ASSIGNRHS
) && ifs_is_null
)
8528 qflags
|= Q_DOUBLE_QUOTES
; /* Posix interp 888 */
8530 ret
= string_list_pos_params (pchar
, save
, qflags
, pflags
);
8531 dispose_words (save
);
8536 /* Perform case modification on VALUE, which is the expansion of
8537 VARNAME. MODSPEC is an expression supplying the type of modification
8538 to perform. QUOTED is a flags word containing the type of quoting
8539 currently in effect. */
8541 parameter_brace_casemod (varname
, value
, ind
, modspec
, patspec
, quoted
, pflags
, flags
)
8542 char *varname
, *value
;
8545 int quoted
, pflags
, flags
;
8547 int vtype
, starsub
, modop
, mflags
, x
;
8548 char *val
, *temp
, *pat
, *p
, *lpat
, *tt
, *oname
;
8552 return ((char *)NULL
);
8554 oname
= this_command_name
;
8555 this_command_name
= varname
;
8557 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
8560 this_command_name
= oname
;
8561 return ((char *)NULL
);
8564 starsub
= vtype
& VT_STARSUB
;
8565 vtype
&= ~VT_STARSUB
;
8569 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
8570 mflags
|= MATCH_QUOTED
;
8572 mflags
|= MATCH_STARSUB
;
8573 if (pflags
& PF_ASSIGNRHS
)
8574 mflags
|= MATCH_ASSIGNRHS
;
8579 x
= p
&& p
[0] == modspec
;
8580 modop
= x
? CASE_UPPER
: CASE_UPFIRST
;
8583 else if (modspec
== ',')
8585 x
= p
&& p
[0] == modspec
;
8586 modop
= x
? CASE_LOWER
: CASE_LOWFIRST
;
8589 else if (modspec
== '~')
8591 x
= p
&& p
[0] == modspec
;
8592 modop
= x
? CASE_TOGGLEALL
: CASE_TOGGLE
;
8596 lpat
= p
? savestring (p
) : 0;
8597 /* Perform the same expansions on the pattern as performed by the
8598 pattern removal expansions. */
8599 pat
= lpat
? getpattern (lpat
, quoted
, 1) : 0;
8601 /* OK, now we do the case modification. */
8605 case VT_ARRAYMEMBER
:
8606 temp
= sh_modcase (val
, pat
, modop
);
8607 if (vtype
== VT_VARIABLE
)
8611 tt
= (mflags
& MATCH_QUOTED
) ? quote_string (temp
) : quote_escapes (temp
);
8618 temp
= pos_params_modcase (val
, pat
, modop
, mflags
);
8619 if (temp
&& quoted
== 0 && ifs_is_null
)
8621 /* Posix interp 888 */
8623 else if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
8625 tt
= quote_escapes (temp
);
8631 #if defined (ARRAY_VARS)
8633 /* If we are expanding in a context where word splitting will not be
8634 performed, treat as quoted. This changes how ${A[*]} will be
8635 expanded to make it identical to $*. */
8636 if ((mflags
& MATCH_STARSUB
) && (mflags
& MATCH_ASSIGNRHS
) && ifs_is_null
)
8637 mflags
|= MATCH_QUOTED
; /* Posix interp 888 */
8639 temp
= assoc_p (v
) ? assoc_modcase (assoc_cell (v
), pat
, modop
, mflags
)
8640 : array_modcase (array_cell (v
), pat
, modop
, mflags
);
8642 if (temp
&& quoted
== 0 && ifs_is_null
)
8644 /* Posix interp 888 */
8646 else if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
8648 tt
= quote_escapes (temp
);
8660 this_command_name
= oname
;
8665 /* Check for unbalanced parens in S, which is the contents of $(( ... )). If
8666 any occur, this must be a nested command substitution, so return 0.
8667 Otherwise, return 1. A valid arithmetic expression must always have a
8668 ( before a matching ), so any cases where there are more right parens
8669 means that this must not be an arithmetic expression, though the parser
8670 will not accept it without a balanced total number of parens. */
8672 chk_arithsub (s
, len
)
8684 else if (s
[i
] == RPAREN
)
8694 ADVANCE_CHAR (s
, len
, i
);
8700 ADVANCE_CHAR (s
, len
, i
);
8704 i
= skip_single_quoted (s
, len
, ++i
, 0);
8708 i
= skip_double_quoted ((char *)s
, len
, ++i
, 0);
8713 return (count
== 0);
8716 /****************************************************************/
8718 /* Functions to perform parameter expansion on a string */
8720 /****************************************************************/
8722 /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
8724 parameter_brace_expand (string
, indexp
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
)
8726 int *indexp
, quoted
, pflags
, *quoted_dollar_atp
, *contains_dollar_at
;
8728 int check_nullness
, var_is_set
, var_is_null
, var_is_special
;
8729 int want_substring
, want_indir
, want_patsub
, want_casemod
;
8730 char *name
, *value
, *temp
, *temp1
;
8731 WORD_DESC
*tdesc
, *ret
;
8732 int t_index
, sindex
, c
, tflag
, modspec
, all_element_arrayref
;
8736 temp
= temp1
= value
= (char *)NULL
;
8737 var_is_set
= var_is_null
= var_is_special
= check_nullness
= 0;
8738 want_substring
= want_indir
= want_patsub
= want_casemod
= 0;
8740 all_element_arrayref
= 0;
8744 /* ${#var} doesn't have any of the other parameter expansions on it. */
8745 if (string
[t_index
] == '#' && legal_variable_starter (string
[t_index
+1])) /* {{ */
8746 name
= string_extract (string
, &t_index
, "}", SX_VARNAME
);
8748 #if defined (CASEMOD_EXPANSIONS)
8749 /* To enable case-toggling expansions using the `~' operator character
8750 change the 1 to 0. */
8751 # if defined (CASEMOD_CAPCASE)
8752 name
= string_extract (string
, &t_index
, "#%^,~:-=?+/@}", SX_VARNAME
);
8754 name
= string_extract (string
, &t_index
, "#%^,:-=?+/@}", SX_VARNAME
);
8755 # endif /* CASEMOD_CAPCASE */
8757 name
= string_extract (string
, &t_index
, "#%:-=?+/@}", SX_VARNAME
);
8758 #endif /* CASEMOD_EXPANSIONS */
8760 /* Handle ${@[stuff]} now that @ is a word expansion operator. Not exactly
8761 the cleanest code ever. */
8762 if (*name
== 0 && sindex
== t_index
&& string
[sindex
] == '@')
8764 name
= (char *)xrealloc (name
, 2);
8769 else if (*name
== '!' && t_index
> sindex
&& string
[t_index
] == '@' && string
[t_index
+1] == RBRACE
)
8771 name
= (char *)xrealloc (name
, t_index
- sindex
+ 2);
8772 name
[t_index
- sindex
] = '@';
8773 name
[t_index
- sindex
+ 1] = '\0';
8782 /* If the name really consists of a special variable, then make sure
8783 that we have the entire name. We don't allow indirect references
8784 to special variables except `#', `?', `@' and `*'. This clause is
8785 designed to handle ${#SPECIAL} and ${!SPECIAL}, not anything more
8787 if ((sindex
== t_index
&& VALID_SPECIAL_LENGTH_PARAM (string
[t_index
])) ||
8788 (sindex
== t_index
&& string
[sindex
] == '#' && VALID_SPECIAL_LENGTH_PARAM (string
[sindex
+ 1])) ||
8789 (sindex
== t_index
- 1 && string
[sindex
] == '!' && VALID_INDIR_PARAM (string
[t_index
])))
8792 temp1
= string_extract (string
, &t_index
, "#%:-=?+/@}", 0);
8793 name
= (char *)xrealloc (name
, 3 + (strlen (temp1
)));
8794 *name
= string
[sindex
];
8795 if (string
[sindex
] == '!')
8797 /* indirect reference of $#, $?, $@, or $* */
8798 name
[1] = string
[sindex
+ 1];
8799 strcpy (name
+ 2, temp1
);
8802 strcpy (name
+ 1, temp1
);
8807 /* Find out what character ended the variable name. Then
8808 do the appropriate thing. */
8809 if (c
= string
[sindex
])
8812 /* If c is followed by one of the valid parameter expansion
8813 characters, move past it as normal. If not, assume that
8814 a substring specification is being given, and do not move
8816 if (c
== ':' && VALID_PARAM_EXPAND_CHAR (string
[sindex
]))
8819 if (c
= string
[sindex
])
8822 else if (c
== ':' && string
[sindex
] != RBRACE
)
8824 else if (c
== '/' /* && string[sindex] != RBRACE */) /* XXX */
8826 #if defined (CASEMOD_EXPANSIONS)
8827 else if (c
== '^' || c
== ',' || c
== '~')
8834 /* Catch the valid and invalid brace expressions that made it through the
8836 /* ${#-} is a valid expansion and means to take the length of $-.
8837 Similarly for ${#?} and ${##}... */
8838 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
8839 VALID_SPECIAL_LENGTH_PARAM (c
) && string
[sindex
] == RBRACE
)
8841 name
= (char *)xrealloc (name
, 3);
8844 c
= string
[sindex
++];
8847 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
8848 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
8849 member (c
, "%:=+/") && string
[sindex
] == RBRACE
)
8851 temp
= (char *)NULL
;
8852 goto bad_substitution
; /* XXX - substitution error */
8855 /* Indirect expansion begins with a `!'. A valid indirect expansion is
8856 either a variable name, one of the positional parameters or a special
8857 variable that expands to one of the positional parameters. */
8858 want_indir
= *name
== '!' &&
8859 (legal_variable_starter ((unsigned char)name
[1]) || DIGIT (name
[1])
8860 || VALID_INDIR_PARAM (name
[1]));
8862 /* Determine the value of this variable whose name is NAME. */
8864 /* Check for special variables, directly referenced. */
8865 if (SPECIAL_VAR (name
, want_indir
))
8868 /* Check for special expansion things, like the length of a parameter */
8869 if (*name
== '#' && name
[1])
8871 /* If we are not pointing at the character just after the
8872 closing brace, then we haven't gotten all of the name.
8873 Since it begins with a special character, this is a bad
8874 substitution. Also check NAME for validity before trying
8876 if (string
[sindex
- 1] != RBRACE
|| (valid_length_expression (name
) == 0))
8878 temp
= (char *)NULL
;
8879 goto bad_substitution
; /* substitution error */
8882 number
= parameter_brace_expand_length (name
);
8883 if (number
== INTMAX_MIN
&& unbound_vars_is_error
)
8885 set_exit_status (EXECUTION_FAILURE
);
8886 err_unboundvar (name
+1);
8888 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8894 return (&expand_wdesc_error
);
8897 ret
= alloc_word_desc ();
8898 ret
->word
= itos (number
);
8903 /* ${@} is identical to $@. */
8904 if (name
[0] == '@' && name
[1] == '\0')
8906 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
8907 *quoted_dollar_atp
= 1;
8909 if (contains_dollar_at
)
8910 *contains_dollar_at
= 1;
8912 tflag
|= W_DOLLARAT
;
8915 /* Process ${!PREFIX*} expansion. */
8916 if (want_indir
&& string
[sindex
- 1] == RBRACE
&&
8917 (string
[sindex
- 2] == '*' || string
[sindex
- 2] == '@') &&
8918 legal_variable_starter ((unsigned char) name
[1]))
8923 temp1
= savestring (name
+ 1);
8924 number
= strlen (temp1
);
8925 temp1
[number
- 1] = '\0';
8926 x
= all_variables_matching_prefix (temp1
);
8927 xlist
= strvec_to_word_list (x
, 0, 0);
8928 if (string
[sindex
- 2] == '*')
8929 temp
= string_list_dollar_star (xlist
, quoted
, 0);
8932 temp
= string_list_dollar_at (xlist
, quoted
, 0);
8933 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
8934 *quoted_dollar_atp
= 1;
8935 if (contains_dollar_at
)
8936 *contains_dollar_at
= 1;
8938 tflag
|= W_DOLLARAT
;
8941 dispose_words (xlist
);
8947 ret
= alloc_word_desc ();
8949 ret
->flags
= tflag
; /* XXX */
8953 #if defined (ARRAY_VARS)
8954 /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */
8955 if (want_indir
&& string
[sindex
- 1] == RBRACE
&&
8956 string
[sindex
- 2] == RBRACK
&& valid_array_reference (name
+1, 0))
8960 temp1
= savestring (name
+ 1);
8961 x
= array_variable_name (temp1
, 0, &x1
, (int *)0);
8963 if (ALL_ELEMENT_SUB (x1
[0]) && x1
[1] == RBRACK
)
8965 temp
= array_keys (temp1
, quoted
, pflags
); /* handles assoc vars too */
8968 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
8969 *quoted_dollar_atp
= 1;
8970 if (contains_dollar_at
)
8971 *contains_dollar_at
= 1;
8973 tflag
|= W_DOLLARAT
;
8980 ret
= alloc_word_desc ();
8982 ret
->flags
= tflag
; /* XXX */
8988 #endif /* ARRAY_VARS */
8990 /* Make sure that NAME is valid before trying to go on. */
8991 if (valid_brace_expansion_word (want_indir
? name
+ 1 : name
,
8992 var_is_special
) == 0)
8994 temp
= (char *)NULL
;
8995 goto bad_substitution
; /* substitution error */
9000 tdesc
= parameter_brace_expand_indir (name
+ 1, var_is_special
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
);
9001 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
9003 temp
= (char *)NULL
;
9004 goto bad_substitution
;
9007 /* Turn off the W_ARRAYIND flag because there is no way for this function
9008 to return the index we're supposed to be using. */
9009 if (tdesc
&& tdesc
->flags
)
9010 tdesc
->flags
&= ~W_ARRAYIND
;
9013 tdesc
= parameter_brace_expand_word (name
, var_is_special
, quoted
, PF_IGNUNBOUND
|(pflags
&(PF_NOSPLIT2
|PF_ASSIGNRHS
)), &ind
);
9015 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
9024 tflag
= tdesc
->flags
;
9025 dispose_word_desc (tdesc
);
9030 if (temp
== &expand_param_error
|| temp
== &expand_param_fatal
)
9034 return (temp
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9037 #if defined (ARRAY_VARS)
9038 if (valid_array_reference (name
, 0))
9044 /* If in a context where word splitting will not take place, treat as
9045 if double-quoted. Has effects with $* and ${array[*]} */
9047 if (pflags
& PF_ASSIGNRHS
)
9048 qflags
|= Q_DOUBLE_QUOTES
;
9049 /* We duplicate a little code here */
9050 t
= mbschr (name
, LBRACK
);
9051 if (t
&& ALL_ELEMENT_SUB (t
[1]) && t
[2] == RBRACK
)
9053 all_element_arrayref
= 1;
9054 if (expand_no_split_dollar_star
&& t
[1] == '*') /* XXX */
9055 qflags
|= Q_DOUBLE_QUOTES
;
9057 chk_atstar (name
, qflags
, pflags
, quoted_dollar_atp
, contains_dollar_at
);
9061 var_is_set
= temp
!= (char *)0;
9062 var_is_null
= check_nullness
&& (var_is_set
== 0 || *temp
== 0);
9063 /* XXX - this may not need to be restricted to special variables */
9065 var_is_null
|= var_is_set
&& var_is_special
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && QUOTED_NULL (temp
);
9066 #if defined (ARRAY_VARS)
9068 var_is_null
|= var_is_set
&&
9069 (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) &&
9070 QUOTED_NULL (temp
) &&
9071 valid_array_reference (name
, 0) &&
9072 chk_atstar (name
, 0, 0, (int *)0, (int *)0);
9075 /* Get the rest of the stuff inside the braces. */
9076 if (c
&& c
!= RBRACE
)
9078 /* Extract the contents of the ${ ... } expansion
9079 according to the Posix.2 rules. */
9080 value
= extract_dollar_brace_string (string
, &sindex
, quoted
, (c
== '%' || c
== '#' || c
=='/' || c
== '^' || c
== ',' || c
==':') ? SX_POSIXEXP
|SX_WORD
: SX_WORD
);
9081 if (string
[sindex
] == RBRACE
)
9084 goto bad_substitution
; /* substitution error */
9087 value
= (char *)NULL
;
9091 /* All the cases where an expansion can possibly generate an unbound
9093 if (want_substring
|| want_patsub
|| want_casemod
|| c
== '@' || c
== '#' || c
== '%' || c
== RBRACE
)
9095 if (var_is_set
== 0 && unbound_vars_is_error
&& ((name
[0] != '@' && name
[0] != '*') || name
[1]) && all_element_arrayref
== 0)
9097 set_exit_status (EXECUTION_FAILURE
);
9098 err_unboundvar (name
);
9102 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9106 /* If this is a substring spec, process it and add the result. */
9109 temp1
= parameter_brace_substring (name
, temp
, ind
, value
, quoted
, pflags
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9113 if (temp1
== &expand_param_error
|| temp1
== &expand_param_fatal
)
9116 return (temp1
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9119 ret
= alloc_word_desc ();
9121 /* We test quoted_dollar_atp because we want variants with double-quoted
9122 "$@" to take a different code path. In fact, we make sure at the end
9123 of expand_word_internal that we're only looking at these flags if
9124 quoted_dollar_at == 0. */
9126 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
9127 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9128 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
9129 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 &&
9130 (pflags
& PF_ASSIGNRHS
))
9131 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9132 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9133 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 && ifs_is_null
)
9134 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9139 else if (want_patsub
)
9141 temp1
= parameter_brace_patsub (name
, temp
, ind
, value
, quoted
, pflags
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9145 if (temp1
== &expand_param_error
|| temp1
== &expand_param_fatal
)
9148 return (temp1
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9151 ret
= alloc_word_desc ();
9154 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
9155 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9156 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
9157 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9158 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 && ifs_is_null
)
9159 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9164 #if defined (CASEMOD_EXPANSIONS)
9165 else if (want_casemod
)
9167 temp1
= parameter_brace_casemod (name
, temp
, ind
, modspec
, value
, quoted
, pflags
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9171 if (temp1
== &expand_param_error
|| temp1
== &expand_param_fatal
)
9174 return (temp1
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9177 ret
= alloc_word_desc ();
9180 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
9181 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9182 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
9183 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9184 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 && ifs_is_null
)
9185 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9192 /* Do the right thing based on which character ended the variable name. */
9198 set_exit_status (EXECUTION_FAILURE
);
9199 report_error (_("%s: bad substitution"), string
? string
: "??");
9203 if (shell_compatibility_level
<= 43)
9204 return &expand_wdesc_error
;
9206 return ((posixly_correct
&& interactive_shell
== 0) ? &expand_wdesc_fatal
: &expand_wdesc_error
);
9212 temp1
= parameter_brace_transform (name
, temp
, ind
, value
, c
, quoted
, pflags
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9216 if (temp1
== &expand_param_error
|| temp1
== &expand_param_fatal
)
9219 set_exit_status (EXECUTION_FAILURE
);
9220 report_error (_("%s: bad substitution"), string
? string
: "??");
9221 return (temp1
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9224 ret
= alloc_word_desc ();
9226 if (temp1
&& QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9227 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
9228 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9229 else if (temp1
&& (name
[0] == '*' && name
[1] == 0) && quoted
== 0 && ifs_is_null
)
9230 ret
->flags
|= W_SPLITSPACE
; /* Posix interp 888 */
9235 case '#': /* ${param#[#]pattern} */
9236 case '%': /* ${param%[%]pattern} */
9237 if (value
== 0 || *value
== '\0' || temp
== 0 || *temp
== '\0')
9242 temp1
= parameter_brace_remove_pattern (name
, temp
, ind
, value
, c
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
9246 ret
= alloc_word_desc ();
9248 if (temp1
&& 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 */
9261 if (var_is_set
&& var_is_null
== 0)
9263 /* If the operator is `+', we don't want the value of the named
9264 variable for anything, just the value of the right hand side. */
9267 /* XXX -- if we're double-quoted and the named variable is "$@",
9268 we want to turn off any special handling of "$@" --
9269 we're not using it, so whatever is on the rhs applies. */
9270 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
9271 *quoted_dollar_atp
= 0;
9272 if (contains_dollar_at
)
9273 *contains_dollar_at
= 0;
9278 /* From Posix discussion on austin-group list. Issue 221
9279 requires that backslashes escaping `}' inside
9280 double-quoted ${...} be removed. */
9281 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
9282 quoted
|= Q_DOLBRACE
;
9283 ret
= parameter_brace_expand_rhs (name
, value
, c
,
9287 contains_dollar_at
);
9288 /* XXX - fix up later, esp. noting presence of
9289 W_HASQUOTEDNULL in ret->flags */
9293 temp
= (char *)NULL
;
9299 /* Otherwise do nothing; just use the value in TEMP. */
9301 else /* VAR not set or VAR is NULL. */
9304 temp
= (char *)NULL
;
9305 if (c
== '=' && var_is_special
)
9307 set_exit_status (EXECUTION_FAILURE
);
9308 report_error (_("$%s: cannot assign in this way"), name
);
9311 return &expand_wdesc_error
;
9315 parameter_brace_expand_error (name
, value
, check_nullness
);
9316 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9320 /* XXX -- if we're double-quoted and the named variable is "$@",
9321 we want to turn off any special handling of "$@" --
9322 we're not using it, so whatever is on the rhs applies. */
9323 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
9324 *quoted_dollar_atp
= 0;
9325 if (contains_dollar_at
)
9326 *contains_dollar_at
= 0;
9328 /* From Posix discussion on austin-group list. Issue 221 requires
9329 that backslashes escaping `}' inside double-quoted ${...} be
9331 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
9332 quoted
|= Q_DOLBRACE
;
9333 ret
= parameter_brace_expand_rhs (name
, value
, c
, quoted
, pflags
,
9335 contains_dollar_at
);
9336 /* XXX - fix up later, esp. noting presence of
9337 W_HASQUOTEDNULL in tdesc->flags */
9348 ret
= alloc_word_desc ();
9355 /* Expand a single ${xxx} expansion. The braces are optional. When
9356 the braces are used, parameter_brace_expand() does the work,
9357 possibly calling param_expand recursively. */
9359 param_expand (string
, sindex
, quoted
, expanded_something
,
9360 contains_dollar_at
, quoted_dollar_at_p
, had_quoted_null_p
,
9363 int *sindex
, quoted
, *expanded_something
, *contains_dollar_at
;
9364 int *quoted_dollar_at_p
, *had_quoted_null_p
, pflags
;
9366 char *temp
, *temp1
, uerror
[3], *savecmd
;
9367 int zindex
, t_index
, expok
;
9371 WORD_LIST
*list
, *l
;
9372 WORD_DESC
*tdesc
, *ret
;
9375 /*itrace("param_expand: `%s' pflags = %d", string+*sindex, pflags);*/
9377 c
= string
[++zindex
];
9379 temp
= (char *)NULL
;
9380 ret
= tdesc
= (WORD_DESC
*)NULL
;
9383 /* Do simple cases first. Switch on what follows '$'. */
9397 temp1
= dollar_vars
[TODIGIT (c
)];
9398 /* This doesn't get called when (pflags&PF_IGNUNBOUND) != 0 */
9399 if (unbound_vars_is_error
&& temp1
== (char *)NULL
)
9404 set_exit_status (EXECUTION_FAILURE
);
9405 err_unboundvar (uerror
);
9406 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9409 temp
= (*temp1
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9410 ? quote_string (temp1
)
9411 : quote_escapes (temp1
);
9413 temp
= (char *)NULL
;
9417 /* $$ -- pid of the invoking shell. */
9419 temp
= itos (dollar_dollar_pid
);
9422 /* $# -- number of positional parameters. */
9424 temp
= itos (number_of_args ());
9427 /* $? -- return value of the last synchronous command. */
9429 temp
= itos (last_command_exit_value
);
9432 /* $- -- flags supplied to the shell on invocation or by `set'. */
9434 temp
= which_set_flags ();
9437 /* $! -- Pid of the last asynchronous command. */
9439 /* If no asynchronous pids have been created, expand to nothing.
9440 If `set -u' has been executed, and no async processes have
9441 been created, this is an expansion error. */
9442 if (last_asynchronous_pid
== NO_PID
)
9444 if (expanded_something
)
9445 *expanded_something
= 0;
9446 temp
= (char *)NULL
;
9447 if (unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
9452 set_exit_status (EXECUTION_FAILURE
);
9453 err_unboundvar (uerror
);
9454 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9458 temp
= itos (last_asynchronous_pid
);
9461 /* The only difference between this and $@ is when the arg is quoted. */
9462 case '*': /* `$*' */
9463 list
= list_rest_of_args ();
9466 /* According to austin-group posix proposal by Geoff Clare in
9467 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
9469 "The shell shall write a message to standard error and
9470 immediately exit when it tries to expand an unset parameter
9471 other than the '@' and '*' special parameters."
9474 if (list
== 0 && unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
9479 set_exit_status (EXECUTION_FAILURE
);
9480 err_unboundvar (uerror
);
9481 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9485 /* If there are no command-line arguments, this should just
9486 disappear if there are other characters in the expansion,
9487 even if it's quoted. */
9488 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && list
== 0)
9489 temp
= (char *)NULL
;
9490 else if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
|Q_PATQUOTE
))
9492 /* If we have "$*" we want to make a string of the positional
9493 parameters, separated by the first character of $IFS, and
9494 quote the whole string, including the separators. If IFS
9495 is unset, the parameters are separated by ' '; if $IFS is
9496 null, the parameters are concatenated. */
9497 temp
= (quoted
& (Q_DOUBLE_QUOTES
|Q_PATQUOTE
)) ? string_list_dollar_star (list
, quoted
, 0) : string_list (list
);
9500 temp1
= (quoted
& Q_DOUBLE_QUOTES
) ? quote_string (temp
) : temp
;
9502 tflag
|= W_HASQUOTEDNULL
;
9510 /* We check whether or not we're eventually going to split $* here,
9511 for example when IFS is empty and we are processing the rhs of
9512 an assignment statement. In that case, we don't separate the
9513 arguments at all. Otherwise, if the $* is not quoted it is
9515 if (expand_no_split_dollar_star
&& quoted
== 0 && ifs_is_set
== 0 && (pflags
& PF_ASSIGNRHS
))
9517 /* Posix interp 888: RHS of assignment, IFS unset: no splitting,
9518 separate with space */
9519 temp1
= string_list_dollar_star (list
, quoted
, pflags
);
9520 temp
= temp1
? quote_string (temp1
) : temp1
;
9521 /* XXX - tentative - note that we saw a quoted null here */
9522 if (temp1
&& *temp1
== 0 && QUOTED_NULL (temp
))
9523 tflag
|= W_SAWQUOTEDNULL
;
9526 else if (expand_no_split_dollar_star
&& quoted
== 0 && ifs_is_null
&& (pflags
& PF_ASSIGNRHS
))
9528 /* Posix interp 888: RHS of assignment, IFS set to '' */
9529 temp1
= string_list_dollar_star (list
, quoted
, pflags
);
9530 temp
= temp1
? quote_escapes (temp1
) : temp1
;
9533 else if (expand_no_split_dollar_star
&& quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && (pflags
& PF_ASSIGNRHS
))
9535 /* Posix interp 888: RHS of assignment, IFS set to non-null value */
9536 temp1
= string_list_dollar_star (list
, quoted
, pflags
);
9537 temp
= temp1
? quote_string (temp1
) : temp1
;
9539 /* XXX - tentative - note that we saw a quoted null here */
9540 if (temp1
&& *temp1
== 0 && QUOTED_NULL (temp
))
9541 tflag
|= W_SAWQUOTEDNULL
;
9544 /* XXX - should we check ifs_is_set here as well? */
9545 # if defined (HANDLE_MULTIBYTE)
9546 else if (expand_no_split_dollar_star
&& ifs_firstc
[0] == 0)
9548 else if (expand_no_split_dollar_star
&& ifs_firstc
== 0)
9550 /* Posix interp 888: not RHS, no splitting, IFS set to '' */
9551 temp
= string_list_dollar_star (list
, quoted
, 0);
9554 temp
= string_list_dollar_at (list
, quoted
, 0);
9555 /* Set W_SPLITSPACE to make sure the individual positional
9556 parameters are split into separate arguments */
9558 if (quoted
== 0 && (ifs_is_set
== 0 || ifs_is_null
))
9559 #else /* change with bash-5.0 */
9560 if (quoted
== 0 && ifs_is_null
)
9562 tflag
|= W_SPLITSPACE
;
9563 /* If we're not quoted but we still don't want word splitting, make
9564 we quote the IFS characters to protect them from splitting (e.g.,
9565 when $@ is in the string as well). */
9566 else if (temp
&& quoted
== 0 && ifs_is_set
&& (pflags
& PF_ASSIGNRHS
))
9568 temp1
= quote_string (temp
);
9574 if (expand_no_split_dollar_star
== 0 && contains_dollar_at
)
9575 *contains_dollar_at
= 1;
9578 dispose_words (list
);
9581 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
9582 means that we have to turn quoting off after we split into
9583 the individually quoted arguments so that the final split
9584 on the first character of $IFS is still done. */
9585 case '@': /* `$@' */
9586 list
= list_rest_of_args ();
9589 /* According to austin-group posix proposal by Geoff Clare in
9590 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
9592 "The shell shall write a message to standard error and
9593 immediately exit when it tries to expand an unset parameter
9594 other than the '@' and '*' special parameters."
9597 if (list
== 0 && unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
9602 set_exit_status (EXECUTION_FAILURE
);
9603 err_unboundvar (uerror
);
9604 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
9608 for (nullarg
= 0, l
= list
; l
; l
= l
->next
)
9610 if (l
->word
&& (l
->word
->word
== 0 || l
->word
->word
[0] == 0))
9614 /* We want to flag the fact that we saw this. We can't turn
9615 off quoting entirely, because other characters in the
9616 string might need it (consider "\"$@\""), but we need some
9617 way to signal that the final split on the first character
9618 of $IFS should be done, even though QUOTED is 1. */
9619 /* XXX - should this test include Q_PATQUOTE? */
9620 if (quoted_dollar_at_p
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9621 *quoted_dollar_at_p
= 1;
9622 if (contains_dollar_at
)
9623 *contains_dollar_at
= 1;
9625 /* We want to separate the positional parameters with the first
9626 character of $IFS in case $IFS is something other than a space.
9627 We also want to make sure that splitting is done no matter what --
9628 according to POSIX.2, this expands to a list of the positional
9629 parameters no matter what IFS is set to. */
9630 /* XXX - what to do when in a context where word splitting is not
9631 performed? Even when IFS is not the default, posix seems to imply
9632 that we have to expand $@ to all the positional parameters and
9633 separate them with spaces, which are preserved because word splitting
9634 doesn't take place. See below for how we use PF_NOSPLIT2 here. */
9636 /* These are the cases where word splitting will not be performed. */
9637 if (pflags
& PF_ASSIGNRHS
)
9639 temp
= string_list_dollar_at (list
, (quoted
|Q_DOUBLE_QUOTES
), pflags
);
9641 tflag
|= W_HASQUOTEDNULL
; /* we know quoting produces quoted nulls */
9644 /* This needs to match what expand_word_internal does with non-quoted $@
9645 does with separating with spaces. Passing Q_DOUBLE_QUOTES means that
9646 the characters in LIST will be quoted, and PF_ASSIGNRHS ensures that
9647 they will separated by spaces. After doing this, we need the special
9648 handling for PF_NOSPLIT2 in expand_word_internal to remove the CTLESC
9650 else if (pflags
& PF_NOSPLIT2
)
9652 #if defined (HANDLE_MULTIBYTE)
9653 if (quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && ifs_firstc
[0] != ' ')
9655 if (quoted
== 0 && ifs_is_set
&& ifs_is_null
== 0 && ifs_firstc
!= ' ')
9657 /* Posix interp 888 */
9658 temp
= string_list_dollar_at (list
, Q_DOUBLE_QUOTES
, pflags
);
9660 temp
= string_list_dollar_at (list
, quoted
, pflags
);
9663 temp
= string_list_dollar_at (list
, quoted
, pflags
);
9665 tflag
|= W_DOLLARAT
;
9666 dispose_words (list
);
9670 tdesc
= parameter_brace_expand (string
, &zindex
, quoted
, pflags
,
9672 contains_dollar_at
);
9674 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
9676 temp
= tdesc
? tdesc
->word
: (char *)0;
9679 /* Quoted nulls should be removed if there is anything else
9681 /* Note that we saw the quoted null so we can add one back at
9682 the end of this function if there are no other characters
9683 in the string, discard TEMP, and go on. The exception to
9684 this is when we have "${@}" and $1 is '', since $@ needs
9685 special handling. */
9686 if (tdesc
&& tdesc
->word
&& (tdesc
->flags
& W_HASQUOTEDNULL
) && QUOTED_NULL (temp
))
9688 if (had_quoted_null_p
)
9689 *had_quoted_null_p
= 1;
9690 if (*quoted_dollar_at_p
== 0)
9693 tdesc
->word
= temp
= (char *)NULL
;
9701 /* Do command or arithmetic substitution. */
9703 /* We have to extract the contents of this paren substitution. */
9704 t_index
= zindex
+ 1;
9705 /* XXX - might want to check for string[t_index+2] == LPAREN and parse
9706 as arithmetic substitution immediately. */
9707 temp
= extract_command_subst (string
, &t_index
, (pflags
&PF_COMPLETE
) ? SX_COMPLETE
: 0);
9710 /* For Posix.2-style `$(( ))' arithmetic substitution,
9711 extract the expression and pass it to the evaluator. */
9712 if (temp
&& *temp
== LPAREN
)
9716 temp2
= savestring (temp1
);
9717 t_index
= strlen (temp2
) - 1;
9719 if (temp2
[t_index
] != RPAREN
)
9725 /* Cut off ending `)' */
9726 temp2
[t_index
] = '\0';
9728 if (chk_arithsub (temp2
, t_index
) == 0)
9732 internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
9737 /* Expand variables found inside the expression. */
9738 temp1
= expand_arith_string (temp2
, Q_DOUBLE_QUOTES
|Q_ARITH
);
9742 /* No error messages. */
9743 savecmd
= this_command_name
;
9744 this_command_name
= (char *)NULL
;
9745 number
= evalexp (temp1
, EXP_EXPANDED
, &expok
);
9746 this_command_name
= savecmd
;
9751 if (interactive_shell
== 0 && posixly_correct
)
9753 set_exit_status (EXECUTION_FAILURE
);
9754 return (&expand_wdesc_fatal
);
9757 return (&expand_wdesc_error
);
9759 temp
= itos (number
);
9764 if (pflags
& PF_NOCOMSUB
)
9765 /* we need zindex+1 because string[zindex] == RPAREN */
9766 temp1
= substring (string
, *sindex
, zindex
+1);
9769 tdesc
= command_substitute (temp
, quoted
, pflags
&PF_ASSIGNRHS
);
9770 temp1
= tdesc
? tdesc
->word
: (char *)NULL
;
9772 dispose_word_desc (tdesc
);
9778 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
9779 away in a future bash release. */
9781 /* Extract the contents of this arithmetic substitution. */
9782 t_index
= zindex
+ 1;
9783 temp
= extract_arithmetic_subst (string
, &t_index
);
9787 temp
= savestring (string
);
9788 if (expanded_something
)
9789 *expanded_something
= 0;
9793 /* Do initial variable expansion. */
9794 temp1
= expand_arith_string (temp
, Q_DOUBLE_QUOTES
|Q_ARITH
);
9799 /* Find the variable in VARIABLE_LIST. */
9800 temp
= (char *)NULL
;
9802 for (t_index
= zindex
; (c
= string
[zindex
]) && legal_variable_char (c
); zindex
++)
9804 temp1
= (zindex
> t_index
) ? substring (string
, t_index
, zindex
) : (char *)NULL
;
9806 /* If this isn't a variable name, then just output the `$'. */
9807 if (temp1
== 0 || *temp1
== '\0')
9810 temp
= (char *)xmalloc (2);
9813 if (expanded_something
)
9814 *expanded_something
= 0;
9818 /* If the variable exists, return its value cell. */
9819 var
= find_variable (temp1
);
9821 if (var
&& invisible_p (var
) == 0 && var_isset (var
))
9823 #if defined (ARRAY_VARS)
9824 if (assoc_p (var
) || array_p (var
))
9826 temp
= array_p (var
) ? array_reference (array_cell (var
), 0)
9827 : assoc_reference (assoc_cell (var
), "0");
9829 temp
= (*temp
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9830 ? quote_string (temp
)
9831 : quote_escapes (temp
);
9832 else if (unbound_vars_is_error
)
9833 goto unbound_variable
;
9838 temp
= value_cell (var
);
9840 temp
= (*temp
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
9841 ? quote_string (temp
)
9842 : ((pflags
& PF_ASSIGNRHS
) ? quote_rhs (temp
)
9843 : quote_escapes (temp
));
9850 else if (var
&& (invisible_p (var
) || var_isset (var
) == 0))
9851 temp
= (char *)NULL
;
9852 else if ((var
= find_variable_last_nameref (temp1
, 0)) && var_isset (var
) && invisible_p (var
) == 0)
9854 temp
= nameref_cell (var
);
9855 #if defined (ARRAY_VARS)
9856 if (temp
&& *temp
&& valid_array_reference (temp
, 0))
9858 tdesc
= parameter_brace_expand_word (temp
, SPECIAL_VAR (temp
, 0), quoted
, pflags
, (arrayind_t
*)NULL
);
9859 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
9866 /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */
9867 if (temp
&& *temp
&& legal_identifier (temp
) == 0)
9869 set_exit_status (EXECUTION_FAILURE
);
9870 report_error (_("%s: invalid variable name for name reference"), temp
);
9871 return (&expand_wdesc_error
); /* XXX */
9874 temp
= (char *)NULL
;
9877 temp
= (char *)NULL
;
9880 if (unbound_vars_is_error
)
9882 set_exit_status (EXECUTION_FAILURE
);
9883 err_unboundvar (temp1
);
9892 set_exit_status (EXECUTION_FAILURE
);
9893 return ((unbound_vars_is_error
&& interactive_shell
== 0)
9894 ? &expand_wdesc_fatal
9895 : &expand_wdesc_error
);
9906 ret
= alloc_word_desc ();
9907 ret
->flags
= tflag
; /* XXX */
9914 invalidate_cached_quoted_dollar_at ()
9916 dispose_words (cached_quoted_dollar_at
);
9917 cached_quoted_dollar_at
= 0;
9920 /* Make a word list which is the result of parameter and variable
9921 expansion, command substitution, arithmetic substitution, and
9922 quote removal of WORD. Return a pointer to a WORD_LIST which is
9923 the result of the expansion. If WORD contains a null word, the
9924 word list returned is also null.
9926 QUOTED contains flag values defined in shell.h.
9928 ISEXP is used to tell expand_word_internal that the word should be
9929 treated as the result of an expansion. This has implications for
9930 how IFS characters in the word are treated.
9932 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
9933 they point to an integer value which receives information about expansion.
9934 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
9935 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
9938 This only does word splitting in the case of $@ expansion. In that
9939 case, we split on ' '. */
9941 /* Values for the local variable quoted_state. */
9943 #define PARTIALLY_QUOTED 1
9944 #define WHOLLY_QUOTED 2
9947 expand_word_internal (word
, quoted
, isexp
, contains_dollar_at
, expanded_something
)
9950 int *contains_dollar_at
;
9951 int *expanded_something
;
9956 /* The intermediate string that we build while expanding. */
9959 /* The current size of the above object. */
9960 size_t istring_size
;
9962 /* Index into ISTRING. */
9965 /* Temporary string storage. */
9968 /* The text of WORD. */
9969 register char *string
;
9971 /* The size of STRING. */
9974 /* The index into STRING. */
9977 /* This gets 1 if we see a $@ while quoted. */
9978 int quoted_dollar_at
;
9980 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
9981 whether WORD contains no quoting characters, a partially quoted
9982 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
9986 int had_quoted_null
;
9987 int has_quoted_ifs
; /* did we add a quoted $IFS character here? */
9988 int has_dollar_at
, temp_has_dollar_at
;
9989 int split_on_spaces
;
9992 int pflags
; /* flags passed to param_expand */
9995 int assignoff
; /* If assignment, offset of `=' */
9997 register unsigned char c
; /* Current character. */
9998 int t_index
; /* For calls to string_extract_xxx. */
10004 /* OK, let's see if we can optimize a common idiom: "$@" */
10005 if (STREQ (word
->word
, "\"$@\"") &&
10006 (word
->flags
== (W_HASDOLLAR
|W_QUOTED
)) &&
10007 dollar_vars
[1]) /* XXX - check IFS here as well? */
10009 if (contains_dollar_at
)
10010 *contains_dollar_at
= 1;
10011 if (expanded_something
)
10012 *expanded_something
= 1;
10013 if (cached_quoted_dollar_at
)
10014 return (copy_word_list (cached_quoted_dollar_at
));
10015 list
= list_rest_of_args ();
10016 list
= quote_list (list
);
10017 cached_quoted_dollar_at
= copy_word_list (list
);
10021 istring
= (char *)xmalloc (istring_size
= DEFAULT_INITIAL_ARRAY_SIZE
);
10022 istring
[istring_index
= 0] = '\0';
10023 quoted_dollar_at
= had_quoted_null
= has_dollar_at
= 0;
10024 has_quoted_ifs
= 0;
10025 split_on_spaces
= 0;
10026 quoted_state
= UNQUOTED
;
10028 string
= word
->word
;
10030 goto finished_with_string
;
10031 mb_cur_max
= MB_CUR_MAX
;
10033 /* Don't need the string length for the SADD... and COPY_ macros unless
10034 multibyte characters are possible, but do need it for bounds checking. */
10035 string_size
= (mb_cur_max
> 1) ? strlen (string
) : 1;
10037 if (contains_dollar_at
)
10038 *contains_dollar_at
= 0;
10042 /* Begin the expansion. */
10044 for (sindex
= 0; ;)
10046 c
= string
[sindex
];
10048 /* Case on top-level character. */
10052 goto finished_with_string
;
10056 #if HANDLE_MULTIBYTE
10057 if (mb_cur_max
> 1 && string
[sindex
])
10059 SADD_MBQCHAR_BODY(temp
, string
, sindex
, string_size
);
10064 temp
= (char *)xmalloc (3);
10066 temp
[1] = c
= string
[sindex
];
10071 if (string
[sindex
])
10077 istring
= sub_append_string (temp
, istring
, &istring_index
, &istring_size
);
10083 #if defined (PROCESS_SUBSTITUTION)
10084 /* Process substitution. */
10088 /* XXX - technically this should only be expanded at the start
10090 if (string
[++sindex
] != LPAREN
|| (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (word
->flags
& (W_DQUOTE
|W_NOPROCSUB
)))
10092 sindex
--; /* add_character: label increments sindex */
10093 goto add_character
;
10096 t_index
= sindex
+ 1; /* skip past both '<' and LPAREN */
10098 temp1
= extract_process_subst (string
, (c
== '<') ? "<(" : ">(", &t_index
, 0); /*))*/
10101 /* If the process substitution specification is `<()', we want to
10102 open the pipe for writing in the child and produce output; if
10103 it is `>()', we want to open the pipe for reading in the child
10104 and consume input. */
10105 temp
= temp1
? process_substitute (temp1
, (c
== '>')) : (char *)0;
10109 goto dollar_add_string
;
10111 #endif /* PROCESS_SUBSTITUTION */
10114 /* Posix.2 section 3.6.1 says that tildes following `=' in words
10115 which are not assignment statements are not expanded. If the
10116 shell isn't in posix mode, though, we perform tilde expansion
10117 on `likely candidate' unquoted assignment statements (flags
10118 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
10119 contains an unquoted :~ or =~. Something to think about: we
10120 now have a flag that says to perform tilde expansion on arguments
10121 to `assignment builtins' like declare and export that look like
10122 assignment statements. We now do tilde expansion on such words
10123 even in POSIX mode. */
10124 if (word
->flags
& (W_ASSIGNRHS
|W_NOTILDE
))
10126 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
10127 goto add_ifs_character
;
10129 goto add_character
;
10131 /* If we're not in posix mode or forcing assignment-statement tilde
10132 expansion, note where the `=' appears in the word and prepare to
10133 do tilde expansion following the first `='. */
10134 if ((word
->flags
& W_ASSIGNMENT
) &&
10135 (posixly_correct
== 0 || (word
->flags
& W_TILDEEXP
)) &&
10136 assignoff
== -1 && sindex
> 0)
10137 assignoff
= sindex
;
10138 if (sindex
== assignoff
&& string
[sindex
+1] == '~') /* XXX */
10139 word
->flags
|= W_ITILDE
;
10141 else if ((word
->flags
& W_ASSIGNMENT
) &&
10142 (posixly_correct
== 0 || (word
->flags
& W_TILDEEXP
)) &&
10143 string
[sindex
+1] == '~')
10144 word
->flags
|= W_ITILDE
;
10147 if (word
->flags
& W_ASSIGNARG
)
10148 word
->flags
|= W_ASSIGNRHS
; /* affects $@ */
10150 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
10153 goto add_ifs_character
;
10156 goto add_character
;
10159 if (word
->flags
& (W_NOTILDE
|W_NOASSNTILDE
))
10161 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
10162 goto add_ifs_character
;
10164 goto add_character
;
10167 if ((word
->flags
& (W_ASSIGNMENT
|W_ASSIGNRHS
|W_TILDEEXP
)) &&
10168 string
[sindex
+1] == '~')
10169 word
->flags
|= W_ITILDE
;
10171 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
10172 goto add_ifs_character
;
10174 goto add_character
;
10177 /* If the word isn't supposed to be tilde expanded, or we're not
10178 at the start of a word or after an unquoted : or = in an
10179 assignment statement, we don't do tilde expansion. We don't
10180 do tilde expansion if quoted or in an arithmetic context. */
10182 if ((word
->flags
& (W_NOTILDE
|W_DQUOTE
)) ||
10183 (sindex
> 0 && ((word
->flags
& W_ITILDE
) == 0)) ||
10184 (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
10186 word
->flags
&= ~W_ITILDE
;
10187 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
) && (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) == 0)
10188 goto add_ifs_character
;
10190 goto add_character
;
10193 if (word
->flags
& W_ASSIGNRHS
)
10195 else if (word
->flags
& (W_ASSIGNMENT
|W_TILDEEXP
))
10200 temp
= bash_tilde_find_word (string
+ sindex
, tflag
, &t_index
);
10202 word
->flags
&= ~W_ITILDE
;
10204 if (temp
&& *temp
&& t_index
> 0)
10206 temp1
= bash_tilde_expand (temp
, tflag
);
10207 if (temp1
&& *temp1
== '~' && STREQ (temp
, temp1
))
10211 goto add_character
; /* tilde expansion failed */
10216 goto add_quoted_string
; /* XXX was add_string */
10221 goto add_character
;
10225 if (expanded_something
)
10226 *expanded_something
= 1;
10227 local_expanded
= 1;
10229 temp_has_dollar_at
= 0;
10230 pflags
= (word
->flags
& W_NOCOMSUB
) ? PF_NOCOMSUB
: 0;
10231 if (word
->flags
& W_NOSPLIT2
)
10232 pflags
|= PF_NOSPLIT2
;
10233 if (word
->flags
& W_ASSIGNRHS
)
10234 pflags
|= PF_ASSIGNRHS
;
10235 if (word
->flags
& W_COMPLETE
)
10236 pflags
|= PF_COMPLETE
;
10238 tword
= param_expand (string
, &sindex
, quoted
, expanded_something
,
10239 &temp_has_dollar_at
, "ed_dollar_at
,
10240 &had_quoted_null
, pflags
);
10241 has_dollar_at
+= temp_has_dollar_at
;
10242 split_on_spaces
+= (tword
->flags
& W_SPLITSPACE
);
10244 if (tword
== &expand_wdesc_error
|| tword
== &expand_wdesc_fatal
)
10248 return ((tword
== &expand_wdesc_error
) ? &expand_word_error
10249 : &expand_word_fatal
);
10251 if (contains_dollar_at
&& has_dollar_at
)
10252 *contains_dollar_at
= 1;
10254 if (tword
&& (tword
->flags
& W_HASQUOTEDNULL
))
10255 had_quoted_null
= 1; /* note for later */
10256 if (tword
&& (tword
->flags
& W_SAWQUOTEDNULL
))
10257 had_quoted_null
= 1; /* XXX */
10259 temp
= tword
? tword
->word
: (char *)NULL
;
10260 dispose_word_desc (tword
);
10262 /* Kill quoted nulls; we will add them back at the end of
10263 expand_word_internal if nothing else in the string */
10264 if (had_quoted_null
&& temp
&& QUOTED_NULL (temp
))
10267 temp
= (char *)NULL
;
10273 case '`': /* Backquoted command substitution. */
10275 t_index
= sindex
++;
10277 temp
= string_extract (string
, &sindex
, "`", SX_REQMATCH
);
10278 /* The test of sindex against t_index is to allow bare instances of
10279 ` to pass through, for backwards compatibility. */
10280 if (temp
== &extract_string_error
|| temp
== &extract_string_fatal
)
10282 if (sindex
- 1 == t_index
)
10285 goto add_character
;
10287 set_exit_status (EXECUTION_FAILURE
);
10288 report_error (_("bad substitution: no closing \"`\" in %s") , string
+t_index
);
10291 return ((temp
== &extract_string_error
) ? &expand_word_error
10292 : &expand_word_fatal
);
10295 if (expanded_something
)
10296 *expanded_something
= 1;
10297 local_expanded
= 1;
10299 if (word
->flags
& W_NOCOMSUB
)
10300 /* sindex + 1 because string[sindex] == '`' */
10301 temp1
= substring (string
, t_index
, sindex
+ 1);
10304 de_backslash (temp
);
10305 tword
= command_substitute (temp
, quoted
, 0);
10306 temp1
= tword
? tword
->word
: (char *)NULL
;
10308 dispose_word_desc (tword
);
10312 goto dollar_add_string
;
10316 if (string
[sindex
+ 1] == '\n')
10322 c
= string
[++sindex
];
10324 if (quoted
& Q_HERE_DOCUMENT
)
10326 else if (quoted
& Q_DOUBLE_QUOTES
)
10331 /* From Posix discussion on austin-group list: Backslash escaping
10332 a } in ${...} is removed. Issue 0000221 */
10333 if ((quoted
& Q_DOLBRACE
) && c
== RBRACE
)
10335 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
10337 /* This is the fix for " $@\ " */
10338 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && ((sh_syntaxtab
[c
] & tflag
) == 0) && isexp
== 0 && isifs (c
))
10340 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
10341 DEFAULT_ARRAY_SIZE
);
10342 istring
[istring_index
++] = CTLESC
;
10343 istring
[istring_index
++] = '\\';
10344 istring
[istring_index
] = '\0';
10346 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
10348 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && c
== 0)
10350 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
10351 DEFAULT_ARRAY_SIZE
);
10352 istring
[istring_index
++] = CTLESC
;
10353 istring
[istring_index
++] = '\\';
10354 istring
[istring_index
] = '\0';
10357 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && ((sh_syntaxtab
[c
] & tflag
) == 0))
10359 SCOPY_CHAR_I (twochars
, '\\', c
, string
, sindex
, string_size
);
10364 sindex
--; /* add_character: label increments sindex */
10365 goto add_character
;
10369 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
10374 /* BEFORE jumping here, we need to increment sindex if appropriate */
10375 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
10376 DEFAULT_ARRAY_SIZE
);
10377 istring
[istring_index
++] = twochars
[0];
10378 istring
[istring_index
++] = twochars
[1];
10379 istring
[istring_index
] = '\0';
10384 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) && ((quoted
& Q_ARITH
) == 0))
10385 goto add_character
;
10387 t_index
= ++sindex
;
10388 temp
= string_extract_double_quoted (string
, &sindex
, (word
->flags
& W_COMPLETE
) ? SX_COMPLETE
: 0);
10390 /* If the quotes surrounded the entire string, then the
10391 whole word was quoted. */
10392 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
10394 : PARTIALLY_QUOTED
;
10398 tword
= alloc_word_desc ();
10399 tword
->word
= temp
;
10401 if (word
->flags
& W_ASSIGNARG
)
10402 tword
->flags
|= word
->flags
& (W_ASSIGNARG
|W_ASSIGNRHS
); /* affects $@ */
10403 if (word
->flags
& W_COMPLETE
)
10404 tword
->flags
|= W_COMPLETE
; /* for command substitutions */
10405 if (word
->flags
& W_NOCOMSUB
)
10406 tword
->flags
|= W_NOCOMSUB
;
10407 if (word
->flags
& W_NOPROCSUB
)
10408 tword
->flags
|= W_NOPROCSUB
;
10410 if (word
->flags
& W_ASSIGNRHS
)
10411 tword
->flags
|= W_ASSIGNRHS
;
10413 temp
= (char *)NULL
;
10415 temp_has_dollar_at
= 0; /* does this quoted (sub)string include $@? */
10416 /* Need to get W_HASQUOTEDNULL flag through this function. */
10417 list
= expand_word_internal (tword
, Q_DOUBLE_QUOTES
, 0, &temp_has_dollar_at
, (int *)NULL
);
10418 has_dollar_at
+= temp_has_dollar_at
;
10420 if (list
== &expand_word_error
|| list
== &expand_word_fatal
)
10424 /* expand_word_internal has already freed temp_word->word
10425 for us because of the way it prints error messages. */
10426 tword
->word
= (char *)NULL
;
10427 dispose_word (tword
);
10431 dispose_word (tword
);
10433 /* "$@" (a double-quoted dollar-at) expands into nothing,
10434 not even a NULL word, when there are no positional
10435 parameters. Posix interp 888 says that other parts of the
10436 word that expand to quoted nulls result in quoted nulls, so
10437 we can't just throw the entire word away if we have "$@"
10438 anywhere in it. We use had_quoted_null to keep track */
10439 if (list
== 0 && temp_has_dollar_at
) /* XXX - was has_dollar_at */
10441 quoted_dollar_at
++;
10445 /* If this list comes back with a quoted null from expansion,
10446 we have either "$x" or "$@" with $1 == ''. In either case,
10447 we need to make sure we add a quoted null argument and
10448 disable the special handling that "$@" gets. */
10449 if (list
&& list
->word
&& list
->next
== 0 && (list
->word
->flags
& W_HASQUOTEDNULL
))
10451 if (had_quoted_null
&& temp_has_dollar_at
)
10452 quoted_dollar_at
++;
10453 had_quoted_null
= 1; /* XXX */
10456 /* If we get "$@", we know we have expanded something, so we
10457 need to remember it for the final split on $IFS. This is
10458 a special case; it's the only case where a quoted string
10459 can expand into more than one word. It's going to come back
10460 from the above call to expand_word_internal as a list with
10463 dequote_list (list
);
10465 if (temp_has_dollar_at
) /* XXX - was has_dollar_at */
10467 quoted_dollar_at
++;
10468 if (contains_dollar_at
)
10469 *contains_dollar_at
= 1;
10470 if (expanded_something
)
10471 *expanded_something
= 1;
10472 local_expanded
= 1;
10477 /* What we have is "". This is a minor optimization. */
10479 list
= (WORD_LIST
*)NULL
;
10480 had_quoted_null
= 1; /* note for later */
10483 /* The code above *might* return a list (consider the case of "$@",
10484 where it returns "$1", "$2", etc.). We can't throw away the
10485 rest of the list, and we have to make sure each word gets added
10486 as quoted. We test on tresult->next: if it is non-NULL, we
10487 quote the whole list, save it to a string with string_list, and
10488 add that string. We don't need to quote the results of this
10489 (and it would be wrong, since that would quote the separators
10490 as well), so we go directly to add_string. */
10495 /* Testing quoted_dollar_at makes sure that "$@" is
10496 split correctly when $IFS does not contain a space. */
10497 temp
= quoted_dollar_at
10498 ? string_list_dollar_at (list
, Q_DOUBLE_QUOTES
, 0)
10499 : string_list (quote_list (list
));
10500 dispose_words (list
);
10505 temp
= savestring (list
->word
->word
);
10506 tflag
= list
->word
->flags
;
10507 dispose_words (list
);
10509 /* If the string is not a quoted null string, we want
10510 to remove any embedded unquoted CTLNUL characters.
10511 We do not want to turn quoted null strings back into
10512 the empty string, though. We do this because we
10513 want to remove any quoted nulls from expansions that
10514 contain other characters. For example, if we have
10515 x"$*"y or "x$*y" and there are no positional parameters,
10516 the $* should expand into nothing. */
10517 /* We use the W_HASQUOTEDNULL flag to differentiate the
10518 cases: a quoted null character as above and when
10519 CTLNUL is contained in the (non-null) expansion
10520 of some variable. We use the had_quoted_null flag to
10521 pass the value through this function to its caller. */
10522 if ((tflag
& W_HASQUOTEDNULL
) && QUOTED_NULL (temp
) == 0)
10523 remove_quoted_nulls (temp
); /* XXX */
10527 temp
= (char *)NULL
;
10529 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
)
10530 had_quoted_null
= 1; /* note for later */
10532 /* We do not want to add quoted nulls to strings that are only
10533 partially quoted; we can throw them away. The exception to
10534 this is when we are going to be performing word splitting,
10535 since we have to preserve a null argument if the next character
10536 will cause word splitting. */
10537 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
&& quoted
== 0 && (word
->flags
& (W_NOSPLIT
|W_EXPANDRHS
|W_ASSIGNRHS
)) == W_EXPANDRHS
)
10541 had_quoted_null
= 1;
10542 goto add_character
;
10544 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
&& (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)))
10552 temp
= quote_string (temp
);
10558 /* Add NULL arg. */
10560 sindex
--; /* add_character: label increments sindex */
10561 had_quoted_null
= 1; /* note for later */
10562 goto add_character
;
10568 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
10569 goto add_character
;
10571 t_index
= ++sindex
;
10572 temp
= string_extract_single_quoted (string
, &sindex
);
10574 /* If the entire STRING was surrounded by single quotes,
10575 then the string is wholly quoted. */
10576 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
10578 : PARTIALLY_QUOTED
;
10580 /* If all we had was '', it is a null expansion. */
10584 temp
= (char *)NULL
;
10587 remove_quoted_escapes (temp
); /* ??? */
10589 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
)
10590 had_quoted_null
= 1; /* note for later */
10592 /* We do not want to add quoted nulls to strings that are only
10593 partially quoted; such nulls are discarded. See above for the
10594 exception, which is when the string is going to be split.
10595 Posix interp 888/1129 */
10596 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
&& quoted
== 0 && (word
->flags
& (W_NOSPLIT
|W_EXPANDRHS
|W_ASSIGNRHS
)) == W_EXPANDRHS
)
10600 goto add_character
;
10603 if (temp
== 0 && (quoted_state
== PARTIALLY_QUOTED
) && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)))
10606 /* If we have a quoted null expansion, add a quoted NULL to istring. */
10610 sindex
--; /* add_character: label increments sindex */
10611 goto add_character
;
10614 goto add_quoted_string
;
10619 /* If we are in a context where the word is not going to be split, but
10620 we need to account for $@ and $* producing one word for each
10621 positional parameter, add quoted spaces so the spaces in the
10622 expansion of "$@", if any, behave correctly. We still may need to
10623 split if we are expanding the rhs of a word expansion. */
10624 if (ifs_is_null
|| split_on_spaces
|| ((word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
|W_ASSIGNRHS
)) && (word
->flags
& W_EXPANDRHS
) == 0))
10626 if (string
[sindex
])
10628 twochars
[0] = CTLESC
;
10635 /* This is the fix for " $@ " */
10637 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (isexp
== 0 && isifs (c
) && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0))
10639 if ((quoted
&(Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0)
10641 add_quoted_character
:
10642 if (string
[sindex
]) /* from old goto dollar_add_string */
10647 goto add_character
;
10651 #if HANDLE_MULTIBYTE
10652 /* XXX - should make sure that c is actually multibyte,
10653 otherwise we can use the twochars branch */
10654 if (mb_cur_max
> 1)
10657 if (mb_cur_max
> 1)
10659 SADD_MBQCHAR_BODY(temp
, string
, sindex
, string_size
);
10664 twochars
[0] = CTLESC
;
10671 SADD_MBCHAR (temp
, string
, sindex
, string_size
);
10674 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 1, istring_size
,
10675 DEFAULT_ARRAY_SIZE
);
10676 istring
[istring_index
++] = c
;
10677 istring
[istring_index
] = '\0';
10679 /* Next character. */
10684 finished_with_string
:
10685 /* OK, we're ready to return. If we have a quoted string, and
10686 quoted_dollar_at is not set, we do no splitting at all; otherwise
10687 we split on ' '. The routines that call this will handle what to
10688 do if nothing has been expanded. */
10690 /* Partially and wholly quoted strings which expand to the empty
10691 string are retained as an empty arguments. Unquoted strings
10692 which expand to the empty string are discarded. The single
10693 exception is the case of expanding "$@" when there are no
10694 positional parameters. In that case, we discard the expansion. */
10696 /* Because of how the code that handles "" and '' in partially
10697 quoted strings works, we need to make ISTRING into a QUOTED_NULL
10698 if we saw quoting characters, but the expansion was empty.
10699 "" and '' are tossed away before we get to this point when
10700 processing partially quoted strings. This makes "" and $xxx""
10701 equivalent when xxx is unset. We also look to see whether we
10702 saw a quoted null from a ${} expansion and add one back if we
10705 /* If we expand to nothing and there were no single or double quotes
10706 in the word, we throw it away. Otherwise, we return a NULL word.
10707 The single exception is for $@ surrounded by double quotes when
10708 there are no positional parameters. In that case, we also throw
10711 if (*istring
== '\0')
10713 if (quoted_dollar_at
== 0 && (had_quoted_null
|| quoted_state
== PARTIALLY_QUOTED
))
10715 istring
[0] = CTLNUL
;
10717 tword
= alloc_word_desc ();
10718 tword
->word
= istring
;
10719 istring
= 0; /* avoid later free() */
10720 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
10721 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
10722 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
10723 tword
->flags
|= W_QUOTED
;
10725 /* According to sh, ksh, and Posix.2, if a word expands into nothing
10726 and a double-quoted "$@" appears anywhere in it, then the entire
10727 word is removed. */
10728 /* XXX - exception appears to be that quoted null strings result in
10730 else if (quoted_state
== UNQUOTED
|| quoted_dollar_at
)
10731 list
= (WORD_LIST
*)NULL
;
10733 list
= (WORD_LIST
*)NULL
;
10735 else if (word
->flags
& W_NOSPLIT
)
10737 tword
= alloc_word_desc ();
10738 tword
->word
= istring
;
10739 if (had_quoted_null
&& QUOTED_NULL (istring
))
10740 tword
->flags
|= W_HASQUOTEDNULL
;
10741 istring
= 0; /* avoid later free() */
10742 if (word
->flags
& W_ASSIGNMENT
)
10743 tword
->flags
|= W_ASSIGNMENT
; /* XXX */
10744 if (word
->flags
& W_COMPASSIGN
)
10745 tword
->flags
|= W_COMPASSIGN
; /* XXX */
10746 if (word
->flags
& W_NOGLOB
)
10747 tword
->flags
|= W_NOGLOB
; /* XXX */
10748 if (word
->flags
& W_NOBRACE
)
10749 tword
->flags
|= W_NOBRACE
; /* XXX */
10750 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
10751 tword
->flags
|= W_QUOTED
;
10752 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
10754 else if (word
->flags
& W_ASSIGNRHS
)
10756 list
= list_string (istring
, "", quoted
);
10757 tword
= list
->word
;
10758 if (had_quoted_null
&& QUOTED_NULL (istring
))
10759 tword
->flags
|= W_HASQUOTEDNULL
;
10762 istring
= 0; /* avoid later free() */
10763 goto set_word_flags
;
10769 ifs_chars
= (quoted_dollar_at
|| has_dollar_at
) ? ifs_value
: (char *)NULL
;
10771 /* If we have $@, we need to split the results no matter what. If
10772 IFS is unset or NULL, string_list_dollar_at has separated the
10773 positional parameters with a space, so we split on space (we have
10774 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
10775 string_list_dollar_at has separated the positional parameters
10776 with the first character of $IFS, so we split on $IFS. If
10777 SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either
10778 unset or null, and we want to make sure that we split on spaces
10779 regardless of what else has happened to IFS since the expansion,
10780 or we expanded "$@" with IFS null and we need to split the positional
10781 parameters into separate words. */
10782 if (split_on_spaces
)
10784 /* If IFS is not set, and the word is not quoted, we want to split
10785 the individual words on $' \t\n'. We rely on previous steps to
10786 quote the portions of the word that should not be split */
10787 if (ifs_is_set
== 0)
10788 list
= list_string (istring
, " \t\n", 1); /* XXX quoted == 1? */
10790 list
= list_string (istring
, " ", 1); /* XXX quoted == 1? */
10793 /* If we have $@ (has_dollar_at != 0) and we are in a context where we
10794 don't want to split the result (W_NOSPLIT2), and we are not quoted,
10795 we have already separated the arguments with the first character of
10796 $IFS. In this case, we want to return a list with a single word
10797 with the separator possibly replaced with a space (it's what other
10798 shells seem to do).
10799 quoted_dollar_at is internal to this function and is set if we are
10800 passed an argument that is unquoted (quoted == 0) but we encounter a
10801 double-quoted $@ while expanding it. */
10802 else if (has_dollar_at
&& quoted_dollar_at
== 0 && ifs_chars
&& quoted
== 0 && (word
->flags
& W_NOSPLIT2
))
10804 tword
= alloc_word_desc ();
10805 /* Only split and rejoin if we have to */
10806 if (*ifs_chars
&& *ifs_chars
!= ' ')
10808 /* list_string dequotes CTLESCs in the string it's passed, so we
10809 need it to get the space separation right if space isn't the
10810 first character in IFS (but is present) and to remove the
10811 quoting we added back in param_expand(). */
10812 list
= list_string (istring
, *ifs_chars
? ifs_chars
: " ", 1);
10813 /* This isn't exactly right in the case where we're expanding
10814 the RHS of an expansion like ${var-$@} where IFS=: (for
10815 example). The W_NOSPLIT2 means we do the separation with :;
10816 the list_string removes the quotes and breaks the string into
10817 a list, and the string_list rejoins it on spaces. When we
10818 return, we expect to be able to split the results, but the
10819 space separation means the right split doesn't happen. */
10820 tword
->word
= string_list (list
);
10823 tword
->word
= istring
;
10824 if (had_quoted_null
&& QUOTED_NULL (istring
))
10825 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
10826 if (tword
->word
!= istring
)
10828 istring
= 0; /* avoid later free() */
10829 goto set_word_flags
;
10831 else if (has_dollar_at
&& ifs_chars
)
10832 list
= list_string (istring
, *ifs_chars
? ifs_chars
: " ", 1);
10835 tword
= alloc_word_desc ();
10836 if (expanded_something
&& *expanded_something
== 0 && has_quoted_ifs
)
10837 tword
->word
= remove_quoted_ifs (istring
);
10839 tword
->word
= istring
;
10840 if (had_quoted_null
&& QUOTED_NULL (istring
)) /* should check for more than one */
10841 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
10842 else if (had_quoted_null
)
10843 tword
->flags
|= W_SAWQUOTEDNULL
; /* XXX */
10844 if (tword
->word
!= istring
)
10846 istring
= 0; /* avoid later free() */
10848 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) || (quoted_state
== WHOLLY_QUOTED
))
10849 tword
->flags
|= W_QUOTED
;
10850 if (word
->flags
& W_ASSIGNMENT
)
10851 tword
->flags
|= W_ASSIGNMENT
;
10852 if (word
->flags
& W_COMPASSIGN
)
10853 tword
->flags
|= W_COMPASSIGN
;
10854 if (word
->flags
& W_NOGLOB
)
10855 tword
->flags
|= W_NOGLOB
;
10856 if (word
->flags
& W_NOBRACE
)
10857 tword
->flags
|= W_NOBRACE
;
10858 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
10866 /* **************************************************************** */
10868 /* Functions for Quote Removal */
10870 /* **************************************************************** */
10872 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
10873 backslash quoting rules for within double quotes or a here document. */
10875 string_quote_removal (string
, quoted
)
10880 char *r
, *result_string
, *temp
, *send
;
10881 int sindex
, tindex
, dquote
;
10885 /* The result can be no longer than the original string. */
10886 slen
= strlen (string
);
10887 send
= string
+ slen
;
10889 r
= result_string
= (char *)xmalloc (slen
+ 1);
10891 for (dquote
= sindex
= 0; c
= string
[sindex
];)
10896 c
= string
[++sindex
];
10902 if (((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
) && (sh_syntaxtab
[c
] & CBSDQUOTE
) == 0)
10907 SCOPY_CHAR_M (r
, string
, send
, sindex
);
10911 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
)
10917 tindex
= sindex
+ 1;
10918 temp
= string_extract_single_quoted (string
, &tindex
);
10929 dquote
= 1 - dquote
;
10935 return (result_string
);
10940 /* Perform quote removal on word WORD. This allocates and returns a new
10943 word_quote_removal (word
, quoted
)
10950 t
= string_quote_removal (word
->word
, quoted
);
10951 w
= alloc_word_desc ();
10952 w
->word
= t
? t
: savestring ("");
10956 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
10957 the members of the list are treated as if they are surrounded by
10958 double quotes. Return a new list, or NULL if LIST is NULL. */
10960 word_list_quote_removal (list
, quoted
)
10964 WORD_LIST
*result
, *t
, *tresult
, *e
;
10966 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
10968 tresult
= make_word_list (word_quote_removal (t
->word
, quoted
), (WORD_LIST
*)NULL
);
10970 result
= (WORD_LIST
*) list_append (result
, tresult
);
10973 result
= e
= tresult
;
10986 /*******************************************
10988 * Functions to perform word splitting *
10990 *******************************************/
11000 ifs_value
= (v
&& value_cell (v
)) ? value_cell (v
) : " \t\n";
11002 ifs_is_set
= ifs_var
!= 0;
11003 ifs_is_null
= ifs_is_set
&& (*ifs_value
== 0);
11005 /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
11006 handle multibyte chars in IFS */
11007 memset (ifs_cmap
, '\0', sizeof (ifs_cmap
));
11008 for (t
= ifs_value
; t
&& *t
; t
++)
11014 #if defined (HANDLE_MULTIBYTE)
11015 if (ifs_value
== 0)
11017 ifs_firstc
[0] = '\0'; /* XXX - ? */
11018 ifs_firstc_len
= 1;
11022 if (locale_utf8locale
&& UTF8_SINGLEBYTE (*ifs_value
))
11023 ifs_firstc_len
= (*ifs_value
!= 0) ? 1 : 0;
11027 ifs_len
= strnlen (ifs_value
, MB_CUR_MAX
);
11028 ifs_firstc_len
= MBLEN (ifs_value
, ifs_len
);
11030 if (ifs_firstc_len
== 1 || ifs_firstc_len
== 0 || MB_INVALIDCH (ifs_firstc_len
))
11032 ifs_firstc
[0] = ifs_value
[0];
11033 ifs_firstc
[1] = '\0';
11034 ifs_firstc_len
= 1;
11037 memcpy (ifs_firstc
, ifs_value
, ifs_firstc_len
);
11040 ifs_firstc
= ifs_value
? *ifs_value
: 0;
11050 /* This splits a single word into a WORD LIST on $IFS, but only if the word
11051 is not quoted. list_string () performs quote removal for us, even if we
11052 don't do any splitting. */
11054 word_split (w
, ifs_chars
)
11064 xifs
= ((w
->flags
& W_QUOTED
) || ifs_chars
== 0) ? "" : ifs_chars
;
11065 result
= list_string (w
->word
, xifs
, w
->flags
& W_QUOTED
);
11068 result
= (WORD_LIST
*)NULL
;
11073 /* Perform word splitting on LIST and return the RESULT. It is possible
11074 to return (WORD_LIST *)NULL. */
11076 word_list_split (list
)
11079 WORD_LIST
*result
, *t
, *tresult
, *e
;
11082 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
11084 tresult
= word_split (t
->word
, ifs_value
);
11085 /* POSIX 2.6: "If the complete expansion appropriate for a word results
11086 in an empty field, that empty field shall be deleted from the list
11087 of fields that form the completely expanded command, unless the
11088 original word contained single-quote or double-quote characters."
11089 This is where we handle these words that contain quoted null strings
11090 and other characters that expand to nothing after word splitting. */
11091 if (tresult
== 0 && t
->word
&& (t
->word
->flags
& W_SAWQUOTEDNULL
)) /* XXX */
11093 w
= alloc_word_desc ();
11094 w
->word
= (char *)xmalloc (1);
11096 tresult
= make_word_list (w
, (WORD_LIST
*)NULL
);
11099 result
= e
= tresult
;
11110 /**************************************************
11112 * Functions to expand an entire WORD_LIST *
11114 **************************************************/
11116 /* Do any word-expansion-specific cleanup and jump to top_level */
11118 exp_jump_to_top_level (v
)
11121 set_pipestatus_from_exit (last_command_exit_value
);
11123 /* Cleanup code goes here. */
11124 expand_no_split_dollar_star
= 0; /* XXX */
11125 if (expanding_redir
)
11126 undo_partial_redirects ();
11127 expanding_redir
= 0;
11128 assigning_in_environment
= 0;
11130 if (parse_and_execute_level
== 0)
11131 top_level_cleanup (); /* from sig.c */
11133 jump_to_top_level (v
);
11136 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
11137 ELIST, and set ELIST to the new list. */
11138 #define PREPEND_LIST(nlist, elist) \
11139 do { nlist->next = elist; elist = nlist; } while (0)
11141 /* Separate out any initial variable assignments from TLIST. If set -k has
11142 been executed, remove all assignment statements from TLIST. Initial
11143 variable assignments and other environment assignments are placed
11144 on SUBST_ASSIGN_VARLIST. */
11146 separate_out_assignments (tlist
)
11149 register WORD_LIST
*vp
, *lp
;
11152 return ((WORD_LIST
*)NULL
);
11154 if (subst_assign_varlist
)
11155 dispose_words (subst_assign_varlist
); /* Clean up after previous error */
11157 subst_assign_varlist
= (WORD_LIST
*)NULL
;
11160 /* Separate out variable assignments at the start of the command.
11161 Loop invariant: vp->next == lp
11162 Loop postcondition:
11163 lp = list of words left after assignment statements skipped
11164 tlist = original list of words
11166 while (lp
&& (lp
->word
->flags
& W_ASSIGNMENT
))
11172 /* If lp != tlist, we have some initial assignment statements.
11173 We make SUBST_ASSIGN_VARLIST point to the list of assignment
11174 words and TLIST point to the remaining words. */
11177 subst_assign_varlist
= tlist
;
11178 /* ASSERT(vp->next == lp); */
11179 vp
->next
= (WORD_LIST
*)NULL
; /* terminate variable list */
11180 tlist
= lp
; /* remainder of word list */
11183 /* vp == end of variable list */
11184 /* tlist == remainder of original word list without variable assignments */
11186 /* All the words in tlist were assignment statements */
11187 return ((WORD_LIST
*)NULL
);
11189 /* ASSERT(tlist != NULL); */
11190 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
11192 /* If the -k option is in effect, we need to go through the remaining
11193 words, separate out the assignment words, and place them on
11194 SUBST_ASSIGN_VARLIST. */
11195 if (place_keywords_in_env
)
11197 WORD_LIST
*tp
; /* tp == running pointer into tlist */
11202 /* Loop Invariant: tp->next == lp */
11203 /* Loop postcondition: tlist == word list without assignment statements */
11206 if (lp
->word
->flags
& W_ASSIGNMENT
)
11208 /* Found an assignment statement, add this word to end of
11209 subst_assign_varlist (vp). */
11210 if (!subst_assign_varlist
)
11211 subst_assign_varlist
= vp
= lp
;
11218 /* Remove the word pointed to by LP from TLIST. */
11219 tp
->next
= lp
->next
;
11220 /* ASSERT(vp == lp); */
11221 lp
->next
= (WORD_LIST
*)NULL
;
11234 #define WEXP_VARASSIGN 0x001
11235 #define WEXP_BRACEEXP 0x002
11236 #define WEXP_TILDEEXP 0x004
11237 #define WEXP_PARAMEXP 0x008
11238 #define WEXP_PATHEXP 0x010
11240 /* All of the expansions, including variable assignments at the start of
11242 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
11244 /* All of the expansions except variable assignments at the start of
11246 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
11248 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
11249 expansion, command substitution, arithmetic expansion, word splitting, and
11251 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
11253 /* Take the list of words in LIST and do the various substitutions. Return
11254 a new list of words which is the expanded list, and without things like
11255 variable assignments. */
11258 expand_words (list
)
11261 return (expand_word_list_internal (list
, WEXP_ALL
));
11264 /* Same as expand_words (), but doesn't hack variable or environment
11267 expand_words_no_vars (list
)
11270 return (expand_word_list_internal (list
, WEXP_NOVARS
));
11274 expand_words_shellexp (list
)
11277 return (expand_word_list_internal (list
, WEXP_SHELLEXP
));
11281 glob_expand_word_list (tlist
, eflags
)
11285 char **glob_array
, *temp_string
;
11286 register int glob_index
;
11287 WORD_LIST
*glob_list
, *output_list
, *disposables
, *next
;
11291 output_list
= disposables
= (WORD_LIST
*)NULL
;
11292 glob_array
= (char **)NULL
;
11295 /* For each word, either globbing is attempted or the word is
11296 added to orig_list. If globbing succeeds, the results are
11297 added to orig_list and the word (tlist) is added to the list
11298 of disposable words. If globbing fails and failed glob
11299 expansions are left unchanged (the shell default), the
11300 original word is added to orig_list. If globbing fails and
11301 failed glob expansions are removed, the original word is
11302 added to the list of disposable words. orig_list ends up
11303 in reverse order and requires a call to REVERSE_LIST to
11304 be set right. After all words are examined, the disposable
11305 words are freed. */
11306 next
= tlist
->next
;
11308 /* If the word isn't an assignment and contains an unquoted
11309 pattern matching character, then glob it. */
11310 if ((tlist
->word
->flags
& W_NOGLOB
) == 0 &&
11311 unquoted_glob_pattern_p (tlist
->word
->word
))
11313 glob_array
= shell_glob_filename (tlist
->word
->word
, QGLOB_CTLESC
); /* XXX */
11315 /* Handle error cases.
11316 I don't think we should report errors like "No such file
11317 or directory". However, I would like to report errors
11318 like "Read failed". */
11320 if (glob_array
== 0 || GLOB_FAILED (glob_array
))
11322 glob_array
= (char **)xmalloc (sizeof (char *));
11323 glob_array
[0] = (char *)NULL
;
11326 /* Dequote the current word in case we have to use it. */
11327 if (glob_array
[0] == NULL
)
11329 temp_string
= dequote_string (tlist
->word
->word
);
11330 free (tlist
->word
->word
);
11331 tlist
->word
->word
= temp_string
;
11334 /* Make the array into a word list. */
11335 glob_list
= (WORD_LIST
*)NULL
;
11336 for (glob_index
= 0; glob_array
[glob_index
]; glob_index
++)
11338 tword
= make_bare_word (glob_array
[glob_index
]);
11339 glob_list
= make_word_list (tword
, glob_list
);
11344 output_list
= (WORD_LIST
*)list_append (glob_list
, output_list
);
11345 PREPEND_LIST (tlist
, disposables
);
11347 else if (fail_glob_expansion
!= 0)
11349 last_command_exit_value
= EXECUTION_FAILURE
;
11350 report_error (_("no match: %s"), tlist
->word
->word
);
11351 exp_jump_to_top_level (DISCARD
);
11353 else if (allow_null_glob_expansion
== 0)
11355 /* Failed glob expressions are left unchanged. */
11356 PREPEND_LIST (tlist
, output_list
);
11360 /* Failed glob expressions are removed. */
11361 PREPEND_LIST (tlist
, disposables
);
11366 /* Dequote the string. */
11367 temp_string
= dequote_string (tlist
->word
->word
);
11368 free (tlist
->word
->word
);
11369 tlist
->word
->word
= temp_string
;
11370 PREPEND_LIST (tlist
, output_list
);
11373 strvec_dispose (glob_array
);
11374 glob_array
= (char **)NULL
;
11380 dispose_words (disposables
);
11383 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
11385 return (output_list
);
11388 #if defined (BRACE_EXPANSION)
11390 brace_expand_word_list (tlist
, eflags
)
11394 register char **expansions
;
11396 WORD_LIST
*disposables
, *output_list
, *next
;
11400 for (disposables
= output_list
= (WORD_LIST
*)NULL
; tlist
; tlist
= next
)
11402 next
= tlist
->next
;
11404 if (tlist
->word
->flags
& W_NOBRACE
)
11406 /*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/
11407 PREPEND_LIST (tlist
, output_list
);
11411 if ((tlist
->word
->flags
& (W_COMPASSIGN
|W_ASSIGNARG
)) == (W_COMPASSIGN
|W_ASSIGNARG
))
11413 /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
11414 PREPEND_LIST (tlist
, output_list
);
11418 /* Only do brace expansion if the word has a brace character. If
11419 not, just add the word list element to BRACES and continue. In
11420 the common case, at least when running shell scripts, this will
11421 degenerate to a bunch of calls to `mbschr', and then what is
11422 basically a reversal of TLIST into BRACES, which is corrected
11423 by a call to REVERSE_LIST () on BRACES when the end of TLIST
11425 if (mbschr (tlist
->word
->word
, LBRACE
))
11427 expansions
= brace_expand (tlist
->word
->word
);
11429 for (eindex
= 0; temp_string
= expansions
[eindex
]; eindex
++)
11431 w
= alloc_word_desc ();
11432 w
->word
= temp_string
;
11434 /* If brace expansion didn't change the word, preserve
11435 the flags. We may want to preserve the flags
11436 unconditionally someday -- XXX */
11437 if (STREQ (temp_string
, tlist
->word
->word
))
11438 w
->flags
= tlist
->word
->flags
;
11440 w
= make_word_flags (w
, temp_string
);
11442 output_list
= make_word_list (w
, output_list
);
11446 /* Add TLIST to the list of words to be freed after brace
11447 expansion has been performed. */
11448 PREPEND_LIST (tlist
, disposables
);
11451 PREPEND_LIST (tlist
, output_list
);
11455 dispose_words (disposables
);
11458 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
11460 return (output_list
);
11464 #if defined (ARRAY_VARS)
11465 /* Take WORD, a compound array assignment, and internally run (for example),
11466 'declare -A w', where W is the variable name portion of WORD. OPTION is
11467 the list of options to supply to `declare'. CMD is the declaration command
11468 we are expanding right now; it's unused currently. */
11470 make_internal_declare (word
, option
, cmd
)
11479 w
= make_word (word
);
11481 t
= assignment (w
->word
, 0);
11482 if (w
->word
[t
] == '=')
11485 if (w
->word
[t
- 1] == '+') /* cut off any append op */
11486 w
->word
[t
- 1] = '\0';
11489 wl
= make_word_list (w
, (WORD_LIST
*)NULL
);
11490 wl
= make_word_list (make_word (option
), wl
);
11492 r
= declare_builtin (wl
);
11494 dispose_words (wl
);
11498 /* Expand VALUE in NAME[+]=( VALUE ) to a list of words. FLAGS is 1 if NAME
11499 is an associative array.
11501 If we are processing an indexed array, expand_compound_array_assignment
11502 will expand all the individual words and quote_compound_array_list will
11503 single-quote them. If we are processing an associative array, we use
11504 parse_string_to_word_list to split VALUE into a list of words instead of
11505 faking up a shell variable and calling expand_compound_array_assignment.
11506 expand_and_quote_assoc_word expands and single-quotes each word in VALUE
11507 together so we don't have problems finding the end of the subscript when
11510 Words in VALUE can be individual words, which are expanded and single-quoted,
11511 or words of the form [IND]=VALUE, which end up as explained below, as
11512 ['expanded-ind']='expanded-value'. */
11515 expand_oneword (value
, flags
)
11524 /* Indexed array */
11525 l
= expand_compound_array_assignment ((SHELL_VAR
*)NULL
, value
, flags
);
11526 /* Now we quote the results of the expansion above to prevent double
11528 quote_compound_array_list (l
, flags
);
11533 /* Associative array */
11534 l
= parse_string_to_word_list (value
, 1, "array assign");
11535 /* For associative arrays, with their arbitrary subscripts, we have to
11536 expand and quote in one step so we don't have to search for the
11537 closing right bracket more than once. */
11538 for (nl
= l
; nl
; nl
= nl
->next
)
11540 if ((nl
->word
->flags
& W_ASSIGNMENT
) == 0)
11541 t
= sh_single_quote (nl
->word
->word
? nl
->word
->word
: "");
11543 t
= expand_and_quote_assoc_word (nl
->word
->word
, flags
);
11544 free (nl
->word
->word
);
11545 nl
->word
->word
= t
;
11551 /* Expand a single compound assignment argument to a declaration builtin.
11552 This word takes the form NAME[+]=( VALUE ). The NAME[+]= is passed through
11553 unchanged. The VALUE is expanded and each word in the result is single-
11554 quoted. Words of the form [key]=value end up as
11555 ['expanded-key']='expanded-value'. Associative arrays have special
11556 handling, see expand_oneword() above. The return value is
11557 NAME[+]=( expanded-and-quoted-VALUE ). */
11559 expand_compound_assignment_word (tlist
, flags
)
11565 char *value
, *temp
;
11567 /*itrace("expand_compound_assignment_word: original word = -%s-", tlist->word->word);*/
11568 t
= assignment (tlist
->word
->word
, 0);
11570 /* value doesn't have the open and close parens */
11572 value
= extract_array_assignment_list (tlist
->word
->word
+ t
+ 1, &oind
);
11573 /* This performs one round of expansion on the index/key and value and
11574 single-quotes each word in the result. */
11575 l
= expand_oneword (value
, flags
);
11578 value
= string_list (l
);
11579 wlen
= STRLEN (value
);
11581 /* Now, let's rebuild the string */
11582 temp
= xmalloc (t
+ 3 + wlen
+ 1); /* name[+]=(value) */
11583 memcpy (temp
, tlist
->word
->word
, ++t
);
11586 memcpy (temp
+ t
, value
, wlen
);
11590 /*itrace("expand_compound_assignment_word: reconstructed word = -%s-", temp);*/
11592 free (tlist
->word
->word
);
11593 tlist
->word
->word
= temp
;
11598 /* Expand and process an argument to a declaration command. We have already
11599 set flags in TLIST->word->flags depending on the declaration command
11600 (declare, local, etc.) and the options supplied to it (-a, -A, etc.).
11601 TLIST->word->word is of the form NAME[+]=( VALUE ).
11603 This does several things, all using pieces of other functions to get the
11604 evaluation sequence right. It's called for compound array assignments with
11605 the W_ASSIGNMENT flag set (basically, valid identifier names on the lhs).
11606 It parses out which flags need to be set for declare to create the variable
11607 correctly, then calls declare internally (make_internal_declare) to make
11608 sure the variable exists with the correct attributes. Before the variable
11609 is created, it calls expand_compound_assignment_word to expand VALUE to a
11610 list of words, appropriately quoted for further evaluation. This preserves
11611 the semantics of word-expansion-before-calling-builtins. Finally, it calls
11612 do_word_assignment to perform the expansion and assignment with the same
11613 expansion semantics as a standalone assignment statement (no word splitting,
11614 etc.) even though the word is single-quoted so all that needs to happen is
11617 expand_declaration_argument (tlist
, wcmd
)
11618 WORD_LIST
*tlist
, *wcmd
;
11620 char opts
[16], omap
[128];
11621 int t
, opti
, oind
, skip
, inheriting
;
11624 inheriting
= localvar_inherit
;
11626 if (tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSNGLOBAL
|W_CHKLOCAL
|W_ASSIGNARRAY
))
11627 opts
[opti
++] = '-';
11629 if ((tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSNGLOBAL
)) == (W_ASSIGNASSOC
|W_ASSNGLOBAL
))
11631 opts
[opti
++] = 'g';
11632 opts
[opti
++] = 'A';
11634 else if (tlist
->word
->flags
& W_ASSIGNASSOC
)
11636 opts
[opti
++] = 'A';
11638 else if ((tlist
->word
->flags
& (W_ASSIGNARRAY
|W_ASSNGLOBAL
)) == (W_ASSIGNARRAY
|W_ASSNGLOBAL
))
11640 opts
[opti
++] = 'g';
11641 opts
[opti
++] = 'a';
11643 else if (tlist
->word
->flags
& W_ASSIGNARRAY
)
11645 opts
[opti
++] = 'a';
11647 else if (tlist
->word
->flags
& W_ASSNGLOBAL
)
11648 opts
[opti
++] = 'g';
11650 if (tlist
->word
->flags
& W_CHKLOCAL
)
11651 opts
[opti
++] = 'G';
11653 /* If we have special handling note the integer attribute and others
11654 that transform the value upon assignment. What we do is take all
11655 of the option arguments and scan through them looking for options
11656 that cause such transformations, and add them to the `opts' array. */
11658 memset (omap
, '\0', sizeof (omap
));
11659 for (l
= wcmd
->next
; l
!= tlist
; l
= l
->next
)
11661 if (l
->word
->word
[0] != '-')
11662 break; /* non-option argument */
11663 if (l
->word
->word
[0] == '-' && l
->word
->word
[1] == '-' && l
->word
->word
[2] == 0)
11664 break; /* -- signals end of options */
11665 for (oind
= 1; l
->word
->word
[oind
]; oind
++)
11666 switch (l
->word
->word
[oind
])
11674 omap
[l
->word
->word
[oind
]] = 1;
11676 opts
[opti
++] = '-';
11683 for (oind
= 0; oind
< sizeof (omap
); oind
++)
11685 opts
[opti
++] = oind
;
11687 /* If there are no -a/-A options, but we have a compound assignment,
11688 we have a choice: we can set opts[0]='-', opts[1]='a', since the
11689 default is to create an indexed array, and call
11690 make_internal_declare with that, or we can just skip the -a and let
11691 declare_builtin deal with it. Once we're here, we're better set
11692 up for the latter, since we don't want to deal with looking up
11693 any existing variable here -- better to let declare_builtin do it.
11694 We need the variable created, though, especially if it's local, so
11695 we get the scoping right before we call do_word_assignment.
11696 To ensure that make_local_declare gets called, we add `--' if there
11697 aren't any options. */
11698 if ((tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSIGNARRAY
)) == 0)
11702 opts
[opti
++] = '-';
11703 opts
[opti
++] = '-';
11708 /* This isn't perfect, but it's a start. Improvements later. We expand
11709 tlist->word->word and single-quote the results to avoid multiple
11710 expansions by, say, do_assignment_internal(). We have to weigh the
11711 cost of reconstructing the compound assignment string with its single
11712 quoting and letting the declare builtin handle it. The single quotes
11713 will prevent any unwanted additional expansion or word splitting. */
11714 expand_compound_assignment_word (tlist
, (tlist
->word
->flags
& W_ASSIGNASSOC
) ? 1 : 0);
11719 t
= make_internal_declare (tlist
->word
->word
, opts
, wcmd
? wcmd
->word
->word
: (char *)0);
11720 if (t
!= EXECUTION_SUCCESS
)
11722 last_command_exit_value
= t
;
11723 if (tlist
->word
->flags
& W_FORCELOCAL
) /* non-fatal error */
11726 exp_jump_to_top_level (DISCARD
);
11732 t
= do_word_assignment (tlist
->word
, 0);
11735 last_command_exit_value
= EXECUTION_FAILURE
;
11736 exp_jump_to_top_level (DISCARD
);
11740 /* Now transform the word as ksh93 appears to do and go on */
11741 t
= assignment (tlist
->word
->word
, 0);
11742 tlist
->word
->word
[t
] = '\0';
11743 if (tlist
->word
->word
[t
- 1] == '+')
11744 tlist
->word
->word
[t
- 1] = '\0'; /* cut off append op */
11745 tlist
->word
->flags
&= ~(W_ASSIGNMENT
|W_NOSPLIT
|W_COMPASSIGN
|W_ASSIGNARG
|W_ASSIGNASSOC
|W_ASSIGNARRAY
);
11749 #endif /* ARRAY_VARS */
11752 shell_expand_word_list (tlist
, eflags
)
11756 WORD_LIST
*expanded
, *orig_list
, *new_list
, *next
, *temp_list
, *wcmd
;
11757 int expanded_something
, has_dollar_at
;
11759 /* We do tilde expansion all the time. This is what 1003.2 says. */
11760 wcmd
= new_list
= (WORD_LIST
*)NULL
;
11762 for (orig_list
= tlist
; tlist
; tlist
= next
)
11764 if (wcmd
== 0 && (tlist
->word
->flags
& W_ASSNBLTIN
))
11767 next
= tlist
->next
;
11769 #if defined (ARRAY_VARS)
11770 /* If this is a compound array assignment to a builtin that accepts
11771 such assignments (e.g., `declare'), take the assignment and perform
11772 it separately, handling the semantics of declarations inside shell
11773 functions. This avoids the double-evaluation of such arguments,
11774 because `declare' does some evaluation of compound assignments on
11776 if ((tlist
->word
->flags
& (W_COMPASSIGN
|W_ASSIGNARG
)) == (W_COMPASSIGN
|W_ASSIGNARG
))
11777 expand_declaration_argument (tlist
, wcmd
);
11780 expanded_something
= 0;
11781 expanded
= expand_word_internal
11782 (tlist
->word
, 0, 0, &has_dollar_at
, &expanded_something
);
11784 if (expanded
== &expand_word_error
|| expanded
== &expand_word_fatal
)
11786 /* By convention, each time this error is returned,
11787 tlist->word->word has already been freed. */
11788 tlist
->word
->word
= (char *)NULL
;
11790 /* Dispose our copy of the original list. */
11791 dispose_words (orig_list
);
11792 /* Dispose the new list we're building. */
11793 dispose_words (new_list
);
11795 last_command_exit_value
= EXECUTION_FAILURE
;
11796 if (expanded
== &expand_word_error
)
11797 exp_jump_to_top_level (DISCARD
);
11799 exp_jump_to_top_level (FORCE_EOF
);
11802 /* Don't split words marked W_NOSPLIT. */
11803 if (expanded_something
&& (tlist
->word
->flags
& W_NOSPLIT
) == 0)
11805 temp_list
= word_list_split (expanded
);
11806 dispose_words (expanded
);
11810 /* If no parameter expansion, command substitution, process
11811 substitution, or arithmetic substitution took place, then
11812 do not do word splitting. We still have to remove quoted
11813 null characters from the result. */
11814 word_list_remove_quoted_nulls (expanded
);
11815 temp_list
= expanded
;
11818 expanded
= REVERSE_LIST (temp_list
, WORD_LIST
*);
11819 new_list
= (WORD_LIST
*)list_append (expanded
, new_list
);
11823 dispose_words (orig_list
);
11826 new_list
= REVERSE_LIST (new_list
, WORD_LIST
*);
11831 /* The workhorse for expand_words () and expand_words_no_vars ().
11832 First arg is LIST, a WORD_LIST of words.
11833 Second arg EFLAGS is a flags word controlling which expansions are
11836 This does all of the substitutions: brace expansion, tilde expansion,
11837 parameter expansion, command substitution, arithmetic expansion,
11838 process substitution, word splitting, and pathname expansion, according
11839 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
11840 set, or for which no expansion is done, do not undergo word splitting.
11841 Words with the W_NOGLOB bit set do not undergo pathname expansion; words
11842 with W_NOBRACE set do not undergo brace expansion (see
11843 brace_expand_word_list above). */
11845 expand_word_list_internal (list
, eflags
)
11849 WORD_LIST
*new_list
, *temp_list
;
11853 tempenv_assign_error
= 0;
11855 return ((WORD_LIST
*)NULL
);
11857 garglist
= new_list
= copy_word_list (list
);
11858 if (eflags
& WEXP_VARASSIGN
)
11860 garglist
= new_list
= separate_out_assignments (new_list
);
11863 if (subst_assign_varlist
)
11865 /* All the words were variable assignments, so they are placed
11866 into the shell's environment. */
11867 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
11869 savecmd
= this_command_name
;
11870 this_command_name
= (char *)NULL
; /* no arithmetic errors */
11871 tint
= do_word_assignment (temp_list
->word
, 0);
11872 this_command_name
= savecmd
;
11873 /* Variable assignment errors in non-interactive shells
11874 running in Posix.2 mode cause the shell to exit, unless
11875 they are being run by the `command' builtin. */
11878 last_command_exit_value
= EXECUTION_FAILURE
;
11879 if (interactive_shell
== 0 && posixly_correct
&& executing_command_builtin
== 0)
11880 exp_jump_to_top_level (FORCE_EOF
);
11882 exp_jump_to_top_level (DISCARD
);
11885 dispose_words (subst_assign_varlist
);
11886 subst_assign_varlist
= (WORD_LIST
*)NULL
;
11888 return ((WORD_LIST
*)NULL
);
11892 /* Begin expanding the words that remain. The expansions take place on
11893 things that aren't really variable assignments. */
11895 #if defined (BRACE_EXPANSION)
11896 /* Do brace expansion on this word if there are any brace characters
11898 if ((eflags
& WEXP_BRACEEXP
) && brace_expansion
&& new_list
)
11899 new_list
= brace_expand_word_list (new_list
, eflags
);
11900 #endif /* BRACE_EXPANSION */
11902 /* Perform the `normal' shell expansions: tilde expansion, parameter and
11903 variable substitution, command substitution, arithmetic expansion,
11904 and word splitting. */
11905 new_list
= shell_expand_word_list (new_list
, eflags
);
11907 /* Okay, we're almost done. Now let's just do some filename
11911 if ((eflags
& WEXP_PATHEXP
) && disallow_filename_globbing
== 0)
11912 /* Glob expand the word list unless globbing has been disabled. */
11913 new_list
= glob_expand_word_list (new_list
, eflags
);
11915 /* Dequote the words, because we're not performing globbing. */
11916 new_list
= dequote_list (new_list
);
11919 if ((eflags
& WEXP_VARASSIGN
) && subst_assign_varlist
)
11921 sh_wassign_func_t
*assign_func
;
11922 int is_special_builtin
, is_builtin_or_func
;
11924 /* If the remainder of the words expand to nothing, Posix.2 requires
11925 that the variable and environment assignments affect the shell's
11927 assign_func
= new_list
? assign_in_env
: do_word_assignment
;
11928 tempenv_assign_error
= 0;
11930 is_builtin_or_func
= (new_list
&& new_list
->word
&& (find_shell_builtin (new_list
->word
->word
) || find_function (new_list
->word
->word
)));
11931 /* Posix says that special builtins exit if a variable assignment error
11932 occurs in an assignment preceding it. */
11933 is_special_builtin
= (posixly_correct
&& new_list
&& new_list
->word
&& find_special_builtin (new_list
->word
->word
));
11935 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
11937 savecmd
= this_command_name
;
11938 this_command_name
= (char *)NULL
;
11939 assigning_in_environment
= (assign_func
== assign_in_env
);
11940 tint
= (*assign_func
) (temp_list
->word
, is_builtin_or_func
);
11941 assigning_in_environment
= 0;
11942 this_command_name
= savecmd
;
11943 /* Variable assignment errors in non-interactive shells running
11944 in Posix.2 mode cause the shell to exit. */
11947 if (assign_func
== do_word_assignment
)
11949 last_command_exit_value
= EXECUTION_FAILURE
;
11950 if (interactive_shell
== 0 && posixly_correct
)
11951 exp_jump_to_top_level (FORCE_EOF
);
11953 exp_jump_to_top_level (DISCARD
);
11955 else if (interactive_shell
== 0 && is_special_builtin
)
11957 last_command_exit_value
= EXECUTION_FAILURE
;
11958 exp_jump_to_top_level (FORCE_EOF
);
11961 tempenv_assign_error
++;
11965 dispose_words (subst_assign_varlist
);
11966 subst_assign_varlist
= (WORD_LIST
*)NULL
;