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-2016 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) */
135 #define CHECK_STRING_OVERRUN(oind, ind, len, ch) \
144 /* An expansion function that takes a string and a quoted flag and returns
145 a WORD_LIST *. Used as the type of the third argument to
146 expand_string_if_necessary(). */
147 typedef WORD_LIST
*EXPFUNC
__P((char *, int));
149 /* Process ID of the last command executed within command substitution. */
150 pid_t last_command_subst_pid
= NO_PID
;
151 pid_t current_command_subst_pid
= NO_PID
;
153 /* Variables used to keep track of the characters in IFS. */
156 unsigned char ifs_cmap
[UCHAR_MAX
+ 1];
157 int ifs_is_set
, ifs_is_null
;
159 #if defined (HANDLE_MULTIBYTE)
160 unsigned char ifs_firstc
[MB_LEN_MAX
];
161 size_t ifs_firstc_len
;
163 unsigned char ifs_firstc
;
166 /* If non-zero, command substitution inherits the value of errexit option */
167 int inherit_errexit
= 0;
169 /* Sentinel to tell when we are performing variable assignments preceding a
170 command name and putting them into the environment. Used to make sure
171 we use the temporary environment when looking up variable values. */
172 int assigning_in_environment
;
174 /* Used to hold a list of variable assignments preceding a command. Global
175 so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
176 SIGCHLD trap and so it can be saved and restored by the trap handlers. */
177 WORD_LIST
*subst_assign_varlist
= (WORD_LIST
*)NULL
;
179 /* Tell the expansion functions to not longjmp back to top_level on fatal
180 errors. Enabled when doing completion and prompt string expansion. */
181 int no_longjmp_on_fatal_error
= 0;
183 /* Extern functions and variables from different files. */
184 extern int last_command_exit_value
, last_command_exit_signal
;
185 extern int subshell_environment
, running_in_background
;
186 extern int subshell_level
, parse_and_execute_level
, sourcelevel
;
187 extern int eof_encountered
, line_number
;
188 extern int return_catch_flag
, return_catch_value
;
189 extern pid_t dollar_dollar_pid
;
190 extern int posixly_correct
;
191 extern char *this_command_name
;
192 extern struct fd_bitmap
*current_fds_to_close
;
193 extern int wordexp_only
;
194 extern int expanding_redir
;
195 extern int tempenv_assign_error
;
196 extern int builtin_ignoring_errexit
;
198 #if defined (JOB_CONTROL) && defined (PROCESS_SUBSTITUTION)
199 extern PROCESS
*last_procsub_child
;
202 #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
203 extern wchar_t *wcsdup
__P((const wchar_t *));
206 /* Non-zero means to allow unmatched globbed filenames to expand to
208 int allow_null_glob_expansion
;
210 /* Non-zero means to throw an error when globbing fails to match anything. */
211 int fail_glob_expansion
;
214 /* Variables to keep track of which words in an expanded word list (the
215 output of expand_word_list_internal) are the result of globbing
216 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
217 (CURRENTLY UNUSED). */
218 char *glob_argv_flags
;
219 static int glob_argv_flags_size
;
222 static WORD_LIST
*cached_quoted_dollar_at
= 0;
224 static WORD_LIST expand_word_error
, expand_word_fatal
;
225 static WORD_DESC expand_wdesc_error
, expand_wdesc_fatal
;
226 static char expand_param_error
, expand_param_fatal
;
227 static char extract_string_error
, extract_string_fatal
;
229 /* Set by expand_word_unsplit; used to inhibit splitting and re-joining
230 $* on $IFS, primarily when doing assignment statements. */
231 static int expand_no_split_dollar_star
= 0;
233 /* A WORD_LIST of words to be expanded by expand_word_list_internal,
234 without any leading variable assignments. */
235 static WORD_LIST
*garglist
= (WORD_LIST
*)NULL
;
237 static char *quoted_substring
__P((char *, int, int));
238 static int quoted_strlen
__P((char *));
239 static char *quoted_strchr
__P((char *, int, int));
241 static char *expand_string_if_necessary
__P((char *, int, EXPFUNC
*));
242 static inline char *expand_string_to_string_internal
__P((char *, int, EXPFUNC
*));
243 static WORD_LIST
*call_expand_word_internal
__P((WORD_DESC
*, int, int, int *, int *));
244 static WORD_LIST
*expand_string_internal
__P((char *, int));
245 static WORD_LIST
*expand_string_leave_quoted
__P((char *, int));
246 static WORD_LIST
*expand_string_for_rhs
__P((char *, int, int *, int *));
248 static WORD_LIST
*list_quote_escapes
__P((WORD_LIST
*));
249 static WORD_LIST
*list_dequote_escapes
__P((WORD_LIST
*));
251 static char *make_quoted_char
__P((int));
252 static WORD_LIST
*quote_list
__P((WORD_LIST
*));
254 static int unquoted_substring
__P((char *, char *));
255 static int unquoted_member
__P((int, char *));
257 #if defined (ARRAY_VARS)
258 static SHELL_VAR
*do_compound_assignment
__P((char *, char *, int));
260 static int do_assignment_internal
__P((const WORD_DESC
*, int));
262 static char *string_extract_verbatim
__P((char *, size_t, int *, char *, int));
263 static char *string_extract
__P((char *, int *, char *, int));
264 static char *string_extract_double_quoted
__P((char *, int *, int));
265 static inline char *string_extract_single_quoted
__P((char *, int *));
266 static inline int skip_single_quoted
__P((const char *, size_t, int, int));
267 static int skip_double_quoted
__P((char *, size_t, int, int));
268 static char *extract_delimited_string
__P((char *, int *, char *, char *, char *, int));
269 static char *extract_dollar_brace_string
__P((char *, int *, int, int));
270 static int skip_matched_pair
__P((const char *, int, int, int, int));
272 static char *pos_params
__P((char *, int, int, int));
274 static unsigned char *mb_getcharlens
__P((char *, int));
276 static char *remove_upattern
__P((char *, char *, int));
277 #if defined (HANDLE_MULTIBYTE)
278 static wchar_t *remove_wpattern
__P((wchar_t *, size_t, wchar_t *, int));
280 static char *remove_pattern
__P((char *, char *, int));
282 static int match_upattern
__P((char *, char *, int, char **, char **));
283 #if defined (HANDLE_MULTIBYTE)
284 static int match_wpattern
__P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
286 static int match_pattern
__P((char *, char *, int, char **, char **));
287 static int getpatspec
__P((int, char *));
288 static char *getpattern
__P((char *, int, int));
289 static char *variable_remove_pattern
__P((char *, char *, int, int));
290 static char *list_remove_pattern
__P((WORD_LIST
*, char *, int, int, int));
291 static char *parameter_list_remove_pattern
__P((int, char *, int, int));
293 static char *array_remove_pattern
__P((SHELL_VAR
*, char *, int, char *, int));
295 static char *parameter_brace_remove_pattern
__P((char *, char *, int, char *, int, int, int));
297 static char *string_var_assignment
__P((SHELL_VAR
*, char *));
298 #if defined (ARRAY_VARS)
299 static char *array_var_assignment
__P((SHELL_VAR
*, int, int));
301 static char *pos_params_assignment
__P((WORD_LIST
*, int, int));
302 static char *string_transform
__P((int, SHELL_VAR
*, char *));
303 static char *list_transform
__P((int, SHELL_VAR
*, WORD_LIST
*, int, int));
304 static char *parameter_list_transform
__P((int, int, int));
305 #if defined ARRAY_VARS
306 static char *array_transform
__P((int, SHELL_VAR
*, char *, int));
308 static char *parameter_brace_transform
__P((char *, char *, int, char *, int, int, int));
310 static char *process_substitute
__P((char *, int));
312 static char *read_comsub
__P((int, int, int *));
315 static arrayind_t array_length_reference
__P((char *));
318 static int valid_brace_expansion_word
__P((char *, int));
319 static int chk_atstar
__P((char *, int, int *, int *));
320 static int chk_arithsub
__P((const char *, int));
322 static WORD_DESC
*parameter_brace_expand_word
__P((char *, int, int, int, arrayind_t
*));
323 static char *parameter_brace_find_indir
__P((char *, int, int, int));
324 static WORD_DESC
*parameter_brace_expand_indir
__P((char *, int, int, int *, int *));
325 static WORD_DESC
*parameter_brace_expand_rhs
__P((char *, char *, int, int, int, int *, int *));
326 static void parameter_brace_expand_error
__P((char *, char *));
328 static int valid_length_expression
__P((char *));
329 static intmax_t parameter_brace_expand_length
__P((char *));
331 static char *skiparith
__P((char *, int));
332 static int verify_substring_values
__P((SHELL_VAR
*, char *, char *, int, intmax_t *, intmax_t *));
333 static int get_var_and_type
__P((char *, char *, arrayind_t
, int, int, SHELL_VAR
**, char **));
334 static char *mb_substring
__P((char *, int, int));
335 static char *parameter_brace_substring
__P((char *, char *, int, char *, int, int));
337 static int shouldexp_replacement
__P((char *));
339 static char *pos_params_pat_subst
__P((char *, char *, char *, int));
341 static char *parameter_brace_patsub
__P((char *, char *, int, char *, int, int, int));
343 static char *pos_params_casemod
__P((char *, char *, int, int));
344 static char *parameter_brace_casemod
__P((char *, char *, int, int, char *, int, int));
346 static WORD_DESC
*parameter_brace_expand
__P((char *, int *, int, int, int *, int *));
347 static WORD_DESC
*param_expand
__P((char *, int *, int, int *, int *, int *, int *, int));
349 static WORD_LIST
*expand_word_internal
__P((WORD_DESC
*, int, int, int *, int *));
351 static WORD_LIST
*word_list_split
__P((WORD_LIST
*));
353 static void exp_jump_to_top_level
__P((int));
355 static WORD_LIST
*separate_out_assignments
__P((WORD_LIST
*));
356 static WORD_LIST
*glob_expand_word_list
__P((WORD_LIST
*, int));
357 #ifdef BRACE_EXPANSION
358 static WORD_LIST
*brace_expand_word_list
__P((WORD_LIST
*, int));
360 #if defined (ARRAY_VARS)
361 static int make_internal_declare
__P((char *, char *, char *));
363 static WORD_LIST
*shell_expand_word_list
__P((WORD_LIST
*, int));
364 static WORD_LIST
*expand_word_list_internal
__P((WORD_LIST
*, int));
366 /* **************************************************************** */
368 /* Utility Functions */
370 /* **************************************************************** */
374 dump_word_flags (flags
)
380 fprintf (stderr
, "%d -> ", f
);
384 fprintf (stderr
, "W_ARRAYIND%s", f
? "|" : "");
386 if (f
& W_ASSIGNASSOC
)
389 fprintf (stderr
, "W_ASSIGNASSOC%s", f
? "|" : "");
391 if (f
& W_ASSIGNARRAY
)
394 fprintf (stderr
, "W_ASSIGNARRAY%s", f
? "|" : "");
399 fprintf (stderr
, "W_HASCTLESC%s", f
? "|" : "");
404 fprintf (stderr
, "W_NOPROCSUB%s", f
? "|" : "");
409 fprintf (stderr
, "W_DQUOTE%s", f
? "|" : "");
411 if (f
& W_HASQUOTEDNULL
)
413 f
&= ~W_HASQUOTEDNULL
;
414 fprintf (stderr
, "W_HASQUOTEDNULL%s", f
? "|" : "");
419 fprintf (stderr
, "W_ASSIGNARG%s", f
? "|" : "");
424 fprintf (stderr
, "W_ASSNBLTIN%s", f
? "|" : "");
426 if (f
& W_ASSNGLOBAL
)
429 fprintf (stderr
, "W_ASSNGLOBAL%s", f
? "|" : "");
431 if (f
& W_COMPASSIGN
)
434 fprintf (stderr
, "W_COMPASSIGN%s", f
? "|" : "");
439 fprintf (stderr
, "W_NOEXPAND%s", f
? "|" : "");
444 fprintf (stderr
, "W_ITILDE%s", f
? "|" : "");
449 fprintf (stderr
, "W_NOTILDE%s", f
? "|" : "");
454 fprintf (stderr
, "W_ASSIGNRHS%s", f
? "|" : "");
459 fprintf (stderr
, "W_NOCOMSUB%s", f
? "|" : "");
461 if (f
& W_DOLLARSTAR
)
464 fprintf (stderr
, "W_DOLLARSTAR%s", f
? "|" : "");
469 fprintf (stderr
, "W_DOLLARAT%s", f
? "|" : "");
474 fprintf (stderr
, "W_TILDEEXP%s", f
? "|" : "");
479 fprintf (stderr
, "W_NOSPLIT2%s", f
? "|" : "");
484 fprintf (stderr
, "W_NOSPLIT%s", f
? "|" : "");
489 fprintf (stderr
, "W_NOBRACE%s", f
? "|" : "");
494 fprintf (stderr
, "W_NOGLOB%s", f
? "|" : "");
496 if (f
& W_SPLITSPACE
)
499 fprintf (stderr
, "W_SPLITSPACE%s", f
? "|" : "");
501 if (f
& W_ASSIGNMENT
)
504 fprintf (stderr
, "W_ASSIGNMENT%s", f
? "|" : "");
509 fprintf (stderr
, "W_QUOTED%s", f
? "|" : "");
514 fprintf (stderr
, "W_HASDOLLAR%s", f
? "|" : "");
519 fprintf (stderr
, "W_COMPLETE%s", f
? "|" : "");
522 fprintf (stderr
, "\n");
527 #ifdef INCLUDE_UNUSED
529 quoted_substring (string
, start
, end
)
534 register char *result
, *s
, *r
;
538 /* Move to string[start], skipping quoted characters. */
539 for (s
= string
, l
= 0; *s
&& l
< start
; )
551 r
= result
= (char *)xmalloc (2*len
+ 1); /* save room for quotes */
553 /* Copy LEN characters, including quote characters. */
555 for (l
= 0; l
< len
; s
++)
569 #ifdef INCLUDE_UNUSED
570 /* Return the length of S, skipping over quoted characters */
594 /* Find the first occurrence of character C in string S, obeying shell
595 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
596 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
597 escaped with CTLESC are skipped. */
599 quoted_strchr (s
, c
, flags
)
607 if (((flags
& ST_BACKSL
) && *p
== '\\')
608 || ((flags
& ST_CTLESC
) && *p
== CTLESC
))
612 return ((char *)NULL
);
618 return ((char *)NULL
);
621 #if defined (INCLUDE_UNUSED)
622 /* Return 1 if CHARACTER appears in an unquoted portion of
623 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
625 unquoted_member (character
, string
)
633 slen
= strlen (string
);
635 while (c
= string
[sindex
])
643 ADVANCE_CHAR (string
, slen
, sindex
);
649 ADVANCE_CHAR (string
, slen
, sindex
);
653 sindex
= skip_single_quoted (string
, slen
, ++sindex
, 0);
657 sindex
= skip_double_quoted (string
, slen
, ++sindex
, 0);
664 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
666 unquoted_substring (substr
, string
)
667 char *substr
, *string
;
670 int sindex
, c
, sublen
;
673 if (substr
== 0 || *substr
== '\0')
676 slen
= strlen (string
);
677 sublen
= strlen (substr
);
678 for (sindex
= 0; c
= string
[sindex
]; )
680 if (STREQN (string
+ sindex
, substr
, sublen
))
688 ADVANCE_CHAR (string
, slen
, sindex
);
692 sindex
= skip_single_quoted (string
, slen
, ++sindex
, 0);
696 sindex
= skip_double_quoted (string
, slen
, ++sindex
, 0);
700 ADVANCE_CHAR (string
, slen
, sindex
);
708 /* Most of the substitutions must be done in parallel. In order
709 to avoid using tons of unclear goto's, I have some functions
710 for manipulating malloc'ed strings. They all take INDX, a
711 pointer to an integer which is the offset into the string
712 where manipulation is taking place. They also take SIZE, a
713 pointer to an integer which is the current length of the
714 character array for this string. */
716 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
717 of space allocated to TARGET. SOURCE can be NULL, in which
718 case nothing happens. Gets rid of SOURCE by freeing it.
719 Returns TARGET in case the location has changed. */
721 sub_append_string (source
, target
, indx
, size
)
722 char *source
, *target
;
731 srclen
= STRLEN (source
);
732 if (srclen
>= (int)(*size
- *indx
))
735 n
= (n
+ DEFAULT_ARRAY_SIZE
) - (n
% DEFAULT_ARRAY_SIZE
);
736 target
= (char *)xrealloc (target
, (*size
= n
));
739 FASTCOPY (source
, target
+ *indx
, srclen
);
741 target
[*indx
] = '\0';
750 /* Append the textual representation of NUMBER to TARGET.
751 INDX and SIZE are as in SUB_APPEND_STRING. */
753 sub_append_number (number
, target
, indx
, size
)
761 temp
= itos (number
);
762 return (sub_append_string (temp
, target
, indx
, size
));
766 /* Extract a substring from STRING, starting at SINDEX and ending with
767 one of the characters in CHARLIST. Don't make the ending character
768 part of the string. Leave SINDEX pointing at the ending character.
769 Understand about backslashes in the string. If (flags & SX_VARNAME)
770 is non-zero, and array variables have been compiled into the shell,
771 everything between a `[' and a corresponding `]' is skipped over.
772 If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
773 update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
774 contain a closing character from CHARLIST. */
776 string_extract (string
, sindex
, charlist
, flags
)
788 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 0;
791 while (c
= string
[i
])
800 #if defined (ARRAY_VARS)
801 else if ((flags
& SX_VARNAME
) && c
== '[')
804 /* If this is an array subscript, skip over it and continue. */
805 ni
= skipsubscript (string
, i
, 0);
806 if (string
[ni
] == ']')
810 else if (MEMBER (c
, charlist
))
816 ADVANCE_CHAR (string
, slen
, i
);
819 /* If we had to have a matching delimiter and didn't find one, return an
820 error and let the caller deal with it. */
821 if ((flags
& SX_REQMATCH
) && found
== 0)
824 return (&extract_string_error
);
827 temp
= (flags
& SX_NOALLOC
) ? (char *)NULL
: substring (string
, *sindex
, i
);
833 /* Extract the contents of STRING as if it is enclosed in double quotes.
834 SINDEX, when passed in, is the offset of the character immediately
835 following the opening double quote; on exit, SINDEX is left pointing after
836 the closing double quote. If STRIPDQ is non-zero, unquoted double
837 quotes are stripped and the string is terminated by a null byte.
838 Backslashes between the embedded double quotes are processed. If STRIPDQ
839 is zero, an unquoted `"' terminates the string. */
841 string_extract_double_quoted (string
, sindex
, flags
)
849 char *temp
, *ret
; /* The new string we return. */
850 int pass_next
, backquote
, si
; /* State variables for the machine. */
855 slen
= strlen (string
+ *sindex
) + *sindex
;
856 send
= string
+ slen
;
858 stripdq
= (flags
& SX_STRIPDQ
);
860 pass_next
= backquote
= dquote
= 0;
861 temp
= (char *)xmalloc (1 + slen
- *sindex
);
865 while (c
= string
[i
])
867 /* Process a character that was quoted by a backslash. */
870 /* XXX - take another look at this in light of Interp 221 */
873 ``The backslash shall retain its special meaning as an escape
874 character only when followed by one of the characters:
877 If STRIPDQ is zero, we handle the double quotes here and let
878 expand_word_internal handle the rest. If STRIPDQ is non-zero,
879 we have already been through one round of backslash stripping,
880 and want to strip these backslashes only if DQUOTE is non-zero,
881 indicating that we are inside an embedded double-quoted string. */
883 /* If we are in an embedded quoted string, then don't strip
884 backslashes before characters for which the backslash
885 retains its special meaning, but remove backslashes in
886 front of other characters. If we are not in an
887 embedded quoted string, don't strip backslashes at all.
888 This mess is necessary because the string was already
889 surrounded by double quotes (and sh has some really weird
891 The returned string will be run through expansion as if
892 it were double-quoted. */
893 if ((stripdq
== 0 && c
!= '"') ||
894 (stripdq
&& ((dquote
&& (sh_syntaxtab
[c
] & CBSDQUOTE
)) || dquote
== 0)))
899 COPY_CHAR_I (temp
, j
, string
, send
, i
);
903 /* A backslash protects the next character. The code just above
904 handles preserving the backslash in front of any character but
913 /* Inside backquotes, ``the portion of the quoted string from the
914 initial backquote and the characters up to the next backquote
915 that is not preceded by a backslash, having escape characters
916 removed, defines that command''. */
934 /* Pass everything between `$(' and the matching `)' or a quoted
935 ${ ... } pair through according to the Posix.2 specification. */
936 if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
941 if (string
[i
+ 1] == LPAREN
)
942 ret
= extract_command_subst (string
, &si
, (flags
& SX_COMPLETE
));
944 ret
= extract_dollar_brace_string (string
, &si
, Q_DOUBLE_QUOTES
, 0);
947 temp
[j
++] = string
[i
+ 1];
949 /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
951 if (ret
== 0 && no_longjmp_on_fatal_error
)
954 ret
= string
+ i
+ 2;
957 for (t
= 0; ret
[t
]; t
++, j
++)
959 temp
[j
] = string
[si
];
974 /* Add any character but a double quote to the quoted string we're
977 goto add_one_character
;
991 /* Point to after the closing quote. */
999 /* This should really be another option to string_extract_double_quoted. */
1001 skip_double_quoted (string
, slen
, sind
, flags
)
1009 int pass_next
, backquote
, si
;
1012 pass_next
= backquote
= 0;
1014 while (c
= string
[i
])
1019 ADVANCE_CHAR (string
, slen
, i
);
1032 ADVANCE_CHAR (string
, slen
, i
);
1041 else if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
1044 if (string
[i
+ 1] == LPAREN
)
1045 ret
= extract_command_subst (string
, &si
, SX_NOALLOC
|(flags
&SX_COMPLETE
));
1047 ret
= extract_dollar_brace_string (string
, &si
, Q_DOUBLE_QUOTES
, SX_NOALLOC
);
1049 /* These can consume the entire string if they are unterminated */
1050 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1057 ADVANCE_CHAR (string
, slen
, i
);
1070 /* Extract the contents of STRING as if it is enclosed in single quotes.
1071 SINDEX, when passed in, is the offset of the character immediately
1072 following the opening single quote; on exit, SINDEX is left pointing after
1073 the closing single quote. */
1074 static inline char *
1075 string_extract_single_quoted (string
, sindex
)
1084 /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
1085 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 0;
1087 while (string
[i
] && string
[i
] != '\'')
1088 ADVANCE_CHAR (string
, slen
, i
);
1090 t
= substring (string
, *sindex
, i
);
1099 /* Skip over a single-quoted string. We overload the SX_COMPLETE flag to mean
1100 that we are splitting out words for completion and have encountered a $'...'
1101 string, which allows backslash-escaped single quotes. */
1103 skip_single_quoted (string
, slen
, sind
, flags
)
1113 while (string
[c
] && string
[c
] != '\'')
1115 if ((flags
& SX_COMPLETE
) && string
[c
] == '\\' && string
[c
+1] == '\'' && string
[c
+2])
1116 ADVANCE_CHAR (string
, slen
, c
);
1117 ADVANCE_CHAR (string
, slen
, c
);
1125 /* Just like string_extract, but doesn't hack backslashes or any of
1126 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
1128 string_extract_verbatim (string
, slen
, sindex
, charlist
, flags
)
1136 #if defined (HANDLE_MULTIBYTE)
1144 if ((flags
& SX_NOCTLESC
) && charlist
[0] == '\'' && charlist
[1] == '\0')
1146 temp
= string_extract_single_quoted (string
, sindex
);
1147 --*sindex
; /* leave *sindex at separator character */
1153 /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need
1154 this only if MB_CUR_MAX > 1. */
1155 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 1;
1157 #if defined (HANDLE_MULTIBYTE)
1158 clen
= strlen (charlist
);
1161 while (c
= string
[i
])
1163 #if defined (HANDLE_MULTIBYTE)
1166 if ((flags
& SX_NOCTLESC
) == 0 && c
== CTLESC
)
1171 /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
1172 through, to protect the CTLNULs from later calls to
1173 remove_quoted_nulls. */
1174 else if ((flags
& SX_NOESCCTLNUL
) == 0 && c
== CTLESC
&& string
[i
+1] == CTLNUL
)
1180 #if defined (HANDLE_MULTIBYTE)
1181 mblength
= MBLEN (string
+ i
, slen
- i
);
1185 mblength
= mbtowc (&wc
, string
+ i
, slen
- i
);
1186 if (MB_INVALIDCH (mblength
))
1188 if (MEMBER (c
, charlist
))
1196 len
= mbstowcs (wcharlist
, charlist
, 0);
1199 wcharlist
= (wchar_t *)xmalloc (sizeof (wchar_t) * (len
+ 1));
1200 mbstowcs (wcharlist
, charlist
, len
+ 1);
1203 if (wcschr (wcharlist
, wc
))
1209 if (MEMBER (c
, charlist
))
1212 ADVANCE_CHAR (string
, slen
, i
);
1215 #if defined (HANDLE_MULTIBYTE)
1219 temp
= substring (string
, *sindex
, i
);
1225 /* Extract the $( construct in STRING, and return a new string.
1226 Start extracting at (SINDEX) as if we had just seen "$(".
1227 Make (SINDEX) get the position of the matching ")". )
1228 XFLAGS is additional flags to pass to other extraction functions. */
1230 extract_command_subst (string
, sindex
, xflags
)
1237 if (string
[*sindex
] == LPAREN
|| (xflags
& SX_COMPLETE
))
1238 return (extract_delimited_string (string
, sindex
, "$(", "(", ")", xflags
|SX_COMMAND
)); /*)*/
1241 xflags
|= (no_longjmp_on_fatal_error
? SX_NOLONGJMP
: 0);
1242 ret
= xparse_dolparen (string
, string
+*sindex
, sindex
, xflags
);
1247 /* Extract the $[ construct in STRING, and return a new string. (])
1248 Start extracting at (SINDEX) as if we had just seen "$[".
1249 Make (SINDEX) get the position of the matching "]". */
1251 extract_arithmetic_subst (string
, sindex
)
1255 return (extract_delimited_string (string
, sindex
, "$[", "[", "]", 0)); /*]*/
1258 #if defined (PROCESS_SUBSTITUTION)
1259 /* Extract the <( or >( construct in STRING, and return a new string.
1260 Start extracting at (SINDEX) as if we had just seen "<(".
1261 Make (SINDEX) get the position of the matching ")". */ /*))*/
1263 extract_process_subst (string
, starter
, sindex
, xflags
)
1270 return (extract_delimited_string (string
, sindex
, starter
, "(", ")", SX_COMMAND
));
1272 xflags
|= (no_longjmp_on_fatal_error
? SX_NOLONGJMP
: 0);
1273 return (xparse_dolparen (string
, string
+*sindex
, sindex
, xflags
));
1276 #endif /* PROCESS_SUBSTITUTION */
1278 #if defined (ARRAY_VARS)
1279 /* This can be fooled by unquoted right parens in the passed string. If
1280 each caller verifies that the last character in STRING is a right paren,
1281 we don't even need to call extract_delimited_string. */
1283 extract_array_assignment_list (string
, sindex
)
1290 slen
= strlen (string
); /* ( */
1291 if (string
[slen
- 1] == ')')
1293 ret
= substring (string
, *sindex
, slen
- 1);
1301 /* Extract and create a new string from the contents of STRING, a
1302 character string delimited with OPENER and CLOSER. SINDEX is
1303 the address of an int describing the current offset in STRING;
1304 it should point to just after the first OPENER found. On exit,
1305 SINDEX gets the position of the last character of the matching CLOSER.
1306 If OPENER is more than a single character, ALT_OPENER, if non-null,
1307 contains a character string that can also match CLOSER and thus
1308 needs to be skipped. */
1310 extract_delimited_string (string
, sindex
, opener
, alt_opener
, closer
, flags
)
1313 char *opener
, *alt_opener
, *closer
;
1319 int pass_character
, nesting_level
, in_comment
;
1320 int len_closer
, len_opener
, len_alt_opener
;
1323 slen
= strlen (string
+ *sindex
) + *sindex
;
1324 len_opener
= STRLEN (opener
);
1325 len_alt_opener
= STRLEN (alt_opener
);
1326 len_closer
= STRLEN (closer
);
1328 pass_character
= in_comment
= 0;
1333 while (nesting_level
)
1337 /* If a recursive call or a call to ADVANCE_CHAR leaves the index beyond
1338 the end of the string, catch it and cut the loop. */
1342 c
= string
[i
= slen
];
1353 ADVANCE_CHAR (string
, slen
, i
);
1357 if (pass_character
) /* previous char was backslash */
1360 ADVANCE_CHAR (string
, slen
, i
);
1364 /* Not exactly right yet; should handle shell metacharacters and
1365 multibyte characters, too. See COMMENT_BEGIN define in parse.y */
1366 if ((flags
& SX_COMMAND
) && c
== '#' && (i
== 0 || string
[i
- 1] == '\n' || shellblank (string
[i
- 1])))
1369 ADVANCE_CHAR (string
, slen
, i
);
1373 if (c
== CTLESC
|| c
== '\\')
1380 /* Process a nested command substitution, but only if we're parsing an
1381 arithmetic substitution. */
1382 if ((flags
& SX_COMMAND
) && string
[i
] == '$' && string
[i
+1] == LPAREN
)
1385 t
= extract_command_subst (string
, &si
, flags
|SX_NOALLOC
);
1390 /* Process a nested OPENER. */
1391 if (STREQN (string
+ i
, opener
, len_opener
))
1393 si
= i
+ len_opener
;
1394 t
= extract_delimited_string (string
, &si
, opener
, alt_opener
, closer
, flags
|SX_NOALLOC
);
1399 /* Process a nested ALT_OPENER */
1400 if (len_alt_opener
&& STREQN (string
+ i
, alt_opener
, len_alt_opener
))
1402 si
= i
+ len_alt_opener
;
1403 t
= extract_delimited_string (string
, &si
, alt_opener
, alt_opener
, closer
, flags
|SX_NOALLOC
);
1408 /* If the current substring terminates the delimited string, decrement
1409 the nesting level. */
1410 if (STREQN (string
+ i
, closer
, len_closer
))
1412 i
+= len_closer
- 1; /* move to last byte of the closer */
1414 if (nesting_level
== 0)
1418 /* Pass old-style command substitution through verbatim. */
1422 t
= string_extract (string
, &si
, "`", flags
|SX_NOALLOC
);
1427 /* Pass single-quoted and double-quoted strings through verbatim. */
1428 if (c
== '\'' || c
== '"')
1431 i
= (c
== '\'') ? skip_single_quoted (string
, slen
, si
, 0)
1432 : skip_double_quoted (string
, slen
, si
, 0);
1436 /* move past this character, which was not special. */
1437 ADVANCE_CHAR (string
, slen
, i
);
1440 if (c
== 0 && nesting_level
)
1442 if (no_longjmp_on_fatal_error
== 0)
1444 last_command_exit_value
= EXECUTION_FAILURE
;
1445 report_error (_("bad substitution: no closing `%s' in %s"), closer
, string
);
1446 exp_jump_to_top_level (DISCARD
);
1451 return (char *)NULL
;
1455 si
= i
- *sindex
- len_closer
+ 1;
1456 if (flags
& SX_NOALLOC
)
1457 result
= (char *)NULL
;
1460 result
= (char *)xmalloc (1 + si
);
1461 strncpy (result
, string
+ *sindex
, si
);
1469 /* Extract a parameter expansion expression within ${ and } from STRING.
1470 Obey the Posix.2 rules for finding the ending `}': count braces while
1471 skipping over enclosed quoted strings and command substitutions.
1472 SINDEX is the address of an int describing the current offset in STRING;
1473 it should point to just after the first `{' found. On exit, SINDEX
1474 gets the position of the matching `}'. QUOTED is non-zero if this
1475 occurs inside double quotes. */
1476 /* XXX -- this is very similar to extract_delimited_string -- XXX */
1478 extract_dollar_brace_string (string
, sindex
, quoted
, flags
)
1480 int *sindex
, quoted
, flags
;
1484 int pass_character
, nesting_level
, si
, dolbrace_state
;
1490 slen
= strlen (string
+ *sindex
) + *sindex
;
1492 /* The handling of dolbrace_state needs to agree with the code in parse.y:
1493 parse_matched_pair(). The different initial value is to handle the
1494 case where this function is called to parse the word in
1495 ${param op word} (SX_WORD). */
1496 dolbrace_state
= (flags
& SX_WORD
) ? DOLBRACE_WORD
: DOLBRACE_PARAM
;
1497 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && (flags
& SX_POSIXEXP
))
1498 dolbrace_state
= DOLBRACE_QUOTE
;
1501 while (c
= string
[i
])
1506 ADVANCE_CHAR (string
, slen
, i
);
1510 /* CTLESCs and backslashes quote the next character. */
1511 if (c
== CTLESC
|| c
== '\\')
1518 if (string
[i
] == '$' && string
[i
+1] == LBRACE
)
1528 if (nesting_level
== 0)
1534 /* Pass the contents of old-style command substitutions through
1539 t
= string_extract (string
, &si
, "`", flags
|SX_NOALLOC
);
1541 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1547 /* Pass the contents of new-style command substitutions and
1548 arithmetic substitutions through verbatim. */
1549 if (string
[i
] == '$' && string
[i
+1] == LPAREN
)
1552 t
= extract_command_subst (string
, &si
, flags
|SX_NOALLOC
);
1557 /* Pass the contents of double-quoted strings through verbatim. */
1561 i
= skip_double_quoted (string
, slen
, si
, 0);
1562 /* skip_XXX_quoted leaves index one past close quote */
1568 /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/
1569 if (posixly_correct
&& shell_compatibility_level
> 42 && dolbrace_state
!= DOLBRACE_QUOTE
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
1570 ADVANCE_CHAR (string
, slen
, i
);
1574 i
= skip_single_quoted (string
, slen
, si
, 0);
1580 /* move past this character, which was not special. */
1581 ADVANCE_CHAR (string
, slen
, i
);
1583 /* This logic must agree with parse.y:parse_matched_pair, since they
1584 share the same defines. */
1585 if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '%' && (i
- *sindex
) > 1)
1586 dolbrace_state
= DOLBRACE_QUOTE
;
1587 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '#' && (i
- *sindex
) > 1)
1588 dolbrace_state
= DOLBRACE_QUOTE
;
1589 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '/' && (i
- *sindex
) > 1)
1590 dolbrace_state
= DOLBRACE_QUOTE2
; /* XXX */
1591 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '^' && (i
- *sindex
) > 1)
1592 dolbrace_state
= DOLBRACE_QUOTE
;
1593 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== ',' && (i
- *sindex
) > 1)
1594 dolbrace_state
= DOLBRACE_QUOTE
;
1595 /* This is intended to handle all of the [:]op expansions and the substring/
1596 length/pattern removal/pattern substitution expansions. */
1597 else if (dolbrace_state
== DOLBRACE_PARAM
&& strchr ("#%^,~:-=?+/", c
) != 0)
1598 dolbrace_state
= DOLBRACE_OP
;
1599 else if (dolbrace_state
== DOLBRACE_OP
&& strchr ("#%^,~:-=?+/", c
) == 0)
1600 dolbrace_state
= DOLBRACE_WORD
;
1603 if (c
== 0 && nesting_level
)
1605 if (no_longjmp_on_fatal_error
== 0)
1607 last_command_exit_value
= EXECUTION_FAILURE
;
1608 report_error (_("bad substitution: no closing `%s' in %s"), "}", string
);
1609 exp_jump_to_top_level (DISCARD
);
1614 return ((char *)NULL
);
1618 result
= (flags
& SX_NOALLOC
) ? (char *)NULL
: substring (string
, *sindex
, i
);
1624 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1625 STRING, and returns a pointer to it. */
1627 de_backslash (string
)
1630 register size_t slen
;
1631 register int i
, j
, prev_i
;
1634 slen
= strlen (string
);
1637 /* Loop copying string[i] to string[j], i >= j. */
1640 if (string
[i
] == '\\' && (string
[i
+ 1] == '`' || string
[i
+ 1] == '\\' ||
1641 string
[i
+ 1] == '$'))
1644 ADVANCE_CHAR (string
, slen
, i
);
1646 do string
[j
++] = string
[prev_i
++]; while (prev_i
< i
);
1657 /* Replace instances of \! in a string with !. */
1659 unquote_bang (string
)
1663 register char *temp
;
1665 temp
= (char *)xmalloc (1 + strlen (string
));
1667 for (i
= 0, j
= 0; (temp
[j
] = string
[i
]); i
++, j
++)
1669 if (string
[i
] == '\\' && string
[i
+ 1] == '!')
1675 strcpy (string
, temp
);
1680 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = oldjmp; return (x); } while (0)
1682 /* This function assumes s[i] == open; returns with s[ret] == close; used to
1683 parse array subscripts. FLAGS & 1 means to not attempt to skip over
1684 matched pairs of quotes or backquotes, or skip word expansions; it is
1685 intended to be used after expansion has been performed and during final
1686 assignment parsing (see arrayfunc.c:assign_compound_array_list()). */
1688 skip_matched_pair (string
, start
, open
, close
, flags
)
1690 int start
, open
, close
, flags
;
1692 int i
, pass_next
, backq
, si
, c
, count
, oldjmp
;
1697 slen
= strlen (string
+ start
) + start
;
1698 oldjmp
= no_longjmp_on_fatal_error
;
1699 no_longjmp_on_fatal_error
= 1;
1701 i
= start
+ 1; /* skip over leading bracket */
1703 pass_next
= backq
= 0;
1704 ss
= (char *)string
;
1705 while (c
= string
[i
])
1712 ADVANCE_CHAR (string
, slen
, i
);
1725 ADVANCE_CHAR (string
, slen
, i
);
1728 else if ((flags
& 1) == 0 && c
== '`')
1734 else if ((flags
& 1) == 0 && c
== open
)
1740 else if (c
== close
)
1748 else if ((flags
& 1) == 0 && (c
== '\'' || c
== '"'))
1750 i
= (c
== '\'') ? skip_single_quoted (ss
, slen
, ++i
, 0)
1751 : skip_double_quoted (ss
, slen
, ++i
, 0);
1752 /* no increment, the skip functions increment past the closing quote. */
1754 else if ((flags
&1) == 0 && c
== '$' && (string
[i
+1] == LPAREN
|| string
[i
+1] == LBRACE
))
1757 if (string
[si
] == '\0')
1760 if (string
[i
+1] == LPAREN
)
1761 temp
= extract_delimited_string (ss
, &si
, "$(", "(", ")", SX_NOALLOC
|SX_COMMAND
); /* ) */
1763 temp
= extract_dollar_brace_string (ss
, &si
, 0, SX_NOALLOC
);
1765 CHECK_STRING_OVERRUN (i
, si
, slen
, c
);
1768 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1774 ADVANCE_CHAR (string
, slen
, i
);
1780 #if defined (ARRAY_VARS)
1782 skipsubscript (string
, start
, flags
)
1786 return (skip_matched_pair (string
, start
, '[', ']', flags
));
1790 /* Skip characters in STRING until we find a character in DELIMS, and return
1791 the index of that character. START is the index into string at which we
1792 begin. This is similar in spirit to strpbrk, but it returns an index into
1793 STRING and takes a starting index. This little piece of code knows quite
1794 a lot of shell syntax. It's very similar to skip_double_quoted and other
1795 functions of that ilk. */
1797 skip_to_delim (string
, start
, delims
, flags
)
1803 int i
, pass_next
, backq
, dquote
, si
, c
, oldjmp
;
1804 int invert
, skipquote
, skipcmd
, noprocsub
, completeflag
;
1805 int arithexp
, skipcol
;
1807 char *temp
, open
[3];
1810 slen
= strlen (string
+ start
) + start
;
1811 oldjmp
= no_longjmp_on_fatal_error
;
1812 if (flags
& SD_NOJMP
)
1813 no_longjmp_on_fatal_error
= 1;
1814 invert
= (flags
& SD_INVERT
);
1815 skipcmd
= (flags
& SD_NOSKIPCMD
) == 0;
1816 noprocsub
= (flags
& SD_NOPROCSUB
);
1817 completeflag
= (flags
& SD_COMPLETE
) ? SX_COMPLETE
: 0;
1819 arithexp
= (flags
& SD_ARITHEXP
);
1823 pass_next
= backq
= dquote
= 0;
1824 while (c
= string
[i
])
1826 /* If this is non-zero, we should not let quote characters be delimiters
1827 and the current character is a single or double quote. We should not
1828 test whether or not it's a delimiter until after we skip single- or
1829 double-quoted strings. */
1830 skipquote
= ((flags
& SD_NOQUOTEDELIM
) && (c
== '\'' || c
=='"'));
1836 ADVANCE_CHAR (string
, slen
, i
);
1849 ADVANCE_CHAR (string
, slen
, i
);
1858 else if (arithexp
&& skipcol
&& c
== ':')
1864 else if (arithexp
&& c
== '?')
1870 else if (skipquote
== 0 && invert
== 0 && member (c
, delims
))
1872 /* the usual case is to use skip_xxx_quoted, but we don't skip over double
1873 quoted strings when looking for the history expansion character as a
1875 /* special case for programmable completion which takes place before
1876 parser converts backslash-escaped single quotes between $'...' to
1877 `regular' single-quoted strings. */
1878 else if (completeflag
&& i
> 0 && string
[i
-1] == '$' && c
== '\'')
1879 i
= skip_single_quoted (string
, slen
, ++i
, SX_COMPLETE
);
1881 i
= skip_single_quoted (string
, slen
, ++i
, 0);
1883 i
= skip_double_quoted (string
, slen
, ++i
, completeflag
);
1884 else if (c
== LPAREN
&& arithexp
)
1887 if (string
[si
] == '\0')
1890 temp
= extract_delimited_string (string
, &si
, "(", "(", ")", SX_NOALLOC
); /* ) */
1892 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1897 else if (c
== '$' && ((skipcmd
&& string
[i
+1] == LPAREN
) || string
[i
+1] == LBRACE
))
1900 if (string
[si
] == '\0')
1903 if (string
[i
+1] == LPAREN
)
1904 temp
= extract_delimited_string (string
, &si
, "$(", "(", ")", SX_NOALLOC
|SX_COMMAND
); /* ) */
1906 temp
= extract_dollar_brace_string (string
, &si
, 0, SX_NOALLOC
);
1908 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1913 #if defined (PROCESS_SUBSTITUTION)
1914 else if (skipcmd
&& noprocsub
== 0 && (c
== '<' || c
== '>') && string
[i
+1] == LPAREN
)
1917 if (string
[si
] == '\0')
1920 temp
= extract_delimited_string (string
, &si
, (c
== '<') ? "<(" : ">(", "(", ")", SX_COMMAND
|SX_NOALLOC
); /* )) */
1922 temp
= extract_process_subst (string
, (c
== '<') ? "<(" : ">(", &si
, 0);
1923 free (temp
); /* XXX - not using SX_NOALLOC here yet */
1926 if (string
[i
] == '\0')
1931 #endif /* PROCESS_SUBSTITUTION */
1932 #if defined (EXTENDED_GLOB)
1933 else if ((flags
& SD_EXTGLOB
) && extended_glob
&& string
[i
+1] == LPAREN
&& member (c
, "?*+!@"))
1936 if (string
[si
] == '\0')
1942 temp
= extract_delimited_string (string
, &si
, open
, "(", ")", SX_NOALLOC
); /* ) */
1945 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1951 else if ((flags
& SD_GLOB
) && c
== LBRACK
)
1954 if (string
[si
] == '\0')
1957 temp
= extract_delimited_string (string
, &si
, "[", "[", "]", SX_NOALLOC
); /* ] */
1960 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1965 else if ((skipquote
|| invert
) && (member (c
, delims
) == 0))
1968 ADVANCE_CHAR (string
, slen
, i
);
1974 #if defined (BANG_HISTORY)
1975 /* Skip to the history expansion character (delims[0]), paying attention to
1976 quoted strings and command and process substitution. This is a stripped-
1977 down version of skip_to_delims. The essential difference is that this
1978 resets the quoting state when starting a command substitution */
1980 skip_to_histexp (string
, start
, delims
, flags
)
1986 int i
, pass_next
, backq
, dquote
, si
, c
, oldjmp
;
1987 int histexp_comsub
, histexp_backq
, old_dquote
;
1989 char *temp
, open
[3];
1992 slen
= strlen (string
+ start
) + start
;
1993 oldjmp
= no_longjmp_on_fatal_error
;
1994 if (flags
& SD_NOJMP
)
1995 no_longjmp_on_fatal_error
= 1;
1997 histexp_comsub
= histexp_backq
= old_dquote
= 0;
2000 pass_next
= backq
= dquote
= 0;
2001 while (c
= string
[i
])
2008 ADVANCE_CHAR (string
, slen
, i
);
2017 else if (backq
&& c
== '`')
2021 dquote
= old_dquote
;
2029 old_dquote
= dquote
; /* simple - one level for now */
2034 /* When in double quotes, act as if the double quote is a member of
2035 history_no_expand_chars, like the history library does */
2036 else if (dquote
&& c
== delims
[0] && string
[i
+1] == '"')
2041 else if (c
== delims
[0])
2043 /* the usual case is to use skip_xxx_quoted, but we don't skip over double
2044 quoted strings when looking for the history expansion character as a
2046 else if (dquote
&& c
== '\'')
2052 i
= skip_single_quoted (string
, slen
, ++i
, 0);
2053 /* The posixly_correct test makes posix-mode shells allow double quotes
2054 to quote the history expansion character */
2055 else if (posixly_correct
== 0 && c
== '"')
2057 dquote
= 1 - dquote
;
2062 i
= skip_double_quoted (string
, slen
, ++i
, 0);
2063 #if defined (PROCESS_SUBSTITUTION)
2064 else if ((c
== '$' || c
== '<' || c
== '>') && string
[i
+1] == LPAREN
&& string
[i
+2] != LPAREN
)
2066 else if (c
== '$' && string
[i
+1] == LPAREN
&& string
[i
+2] != LPAREN
)
2069 if (string
[i
+2] == '\0')
2073 old_dquote
= dquote
;
2076 else if (histexp_comsub
&& c
== RPAREN
)
2079 dquote
= old_dquote
;
2083 else if (backq
) /* placeholder */
2085 ADVANCE_CHAR (string
, slen
, i
);
2089 ADVANCE_CHAR (string
, slen
, i
);
2094 #endif /* BANG_HISTORY */
2096 #if defined (READLINE)
2097 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
2098 an unclosed quoted string), or if the character at EINDEX is quoted
2099 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
2100 single and double-quoted string parsing functions should not return an
2101 error if there are unclosed quotes or braces. The characters that this
2102 recognizes need to be the same as the contents of
2103 rl_completer_quote_characters. */
2106 char_is_quoted (string
, eindex
)
2110 int i
, pass_next
, c
, oldjmp
;
2114 slen
= strlen (string
);
2115 oldjmp
= no_longjmp_on_fatal_error
;
2116 no_longjmp_on_fatal_error
= 1;
2125 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
2127 ADVANCE_CHAR (string
, slen
, i
);
2136 else if (c
== '$' && string
[i
+1] == '\'' && string
[i
+2])
2139 i
= skip_single_quoted (string
, slen
, i
, SX_COMPLETE
);
2143 else if (c
== '\'' || c
== '"')
2145 i
= (c
== '\'') ? skip_single_quoted (string
, slen
, ++i
, 0)
2146 : skip_double_quoted (string
, slen
, ++i
, SX_COMPLETE
);
2149 /* no increment, the skip_xxx functions go one past end */
2152 ADVANCE_CHAR (string
, slen
, i
);
2159 unclosed_pair (string
, eindex
, openstr
)
2164 int i
, pass_next
, openc
, olen
;
2168 slen
= strlen (string
);
2169 olen
= strlen (openstr
);
2170 i
= pass_next
= openc
= 0;
2176 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
2178 ADVANCE_CHAR (string
, slen
, i
);
2181 else if (string
[i
] == '\\')
2187 else if (STREQN (string
+ i
, openstr
, olen
))
2192 /* XXX - may want to handle $'...' specially here */
2193 else if (string
[i
] == '\'' || string
[i
] == '"')
2195 i
= (string
[i
] == '\'') ? skip_single_quoted (string
, slen
, i
, 0)
2196 : skip_double_quoted (string
, slen
, i
, SX_COMPLETE
);
2201 ADVANCE_CHAR (string
, slen
, i
);
2206 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
2207 individual words. If DELIMS is NULL, the current value of $IFS is used
2208 to split the string, and the function follows the shell field splitting
2209 rules. SENTINEL is an index to look for. NWP, if non-NULL,
2210 gets the number of words in the returned list. CWP, if non-NULL, gets
2211 the index of the word containing SENTINEL. Non-whitespace chars in
2212 DELIMS delimit separate fields. */
2214 split_at_delims (string
, slen
, delims
, sentinel
, flags
, nwp
, cwp
)
2218 int sentinel
, flags
;
2221 int ts
, te
, i
, nw
, cw
, ifs_split
, dflags
;
2222 char *token
, *d
, *d2
;
2223 WORD_LIST
*ret
, *tl
;
2225 if (string
== 0 || *string
== '\0')
2231 return ((WORD_LIST
*)NULL
);
2234 d
= (delims
== 0) ? ifs_value
: delims
;
2235 ifs_split
= delims
== 0;
2237 /* Make d2 the non-whitespace characters in delims */
2242 #if defined (HANDLE_MULTIBYTE)
2243 size_t mblength
= 1;
2247 slength
= strlen (delims
);
2248 d2
= (char *)xmalloc (slength
+ 1);
2252 #if defined (HANDLE_MULTIBYTE)
2253 mbstate_t state_bak
;
2255 mblength
= MBRLEN (delims
+ i
, slength
, &state
);
2256 if (MB_INVALIDCH (mblength
))
2258 else if (mblength
> 1)
2260 memcpy (d2
+ ts
, delims
+ i
, mblength
);
2263 slength
-= mblength
;
2267 if (whitespace (delims
[i
]) == 0)
2268 d2
[ts
++] = delims
[i
];
2276 ret
= (WORD_LIST
*)NULL
;
2278 /* Remove sequences of whitespace characters at the start of the string, as
2279 long as those characters are delimiters. */
2280 for (i
= 0; member (string
[i
], d
) && spctabnl (string
[i
]); i
++)
2282 if (string
[i
] == '\0')
2291 dflags
= flags
|SD_NOJMP
;
2294 te
= skip_to_delim (string
, ts
, d
, dflags
);
2296 /* If we have a non-whitespace delimiter character, use it to make a
2297 separate field. This is just about what $IFS splitting does and
2298 is closer to the behavior of the shell parser. */
2299 if (ts
== te
&& d2
&& member (string
[ts
], d2
))
2302 /* If we're using IFS splitting, the non-whitespace delimiter char
2303 and any additional IFS whitespace delimits a field. */
2305 while (member (string
[te
], d
) && spctabnl (string
[te
]))
2308 while (member (string
[te
], d2
))
2312 token
= substring (string
, ts
, te
);
2314 ret
= add_string_to_list (token
, ret
);
2318 if (sentinel
>= ts
&& sentinel
<= te
)
2321 /* If the cursor is at whitespace just before word start, set the
2322 sentinel word to the current word. */
2323 if (cwp
&& cw
== -1 && sentinel
== ts
-1)
2326 /* If the cursor is at whitespace between two words, make a new, empty
2327 word, add it before (well, after, since the list is in reverse order)
2328 the word we just added, and set the current word to that one. */
2329 if (cwp
&& cw
== -1 && sentinel
< ts
)
2331 tl
= make_word_list (make_word (""), ret
->next
);
2337 if (string
[te
] == 0)
2341 while (member (string
[i
], d
) && (ifs_split
|| spctabnl(string
[i
])))
2350 /* Special case for SENTINEL at the end of STRING. If we haven't found
2351 the word containing SENTINEL yet, and the index we're looking for is at
2352 the end of STRING (or past the end of the previously-found token,
2353 possible if the end of the line is composed solely of IFS whitespace)
2354 add an additional null argument and set the current word pointer to that. */
2355 if (cwp
&& cw
== -1 && (sentinel
>= slen
|| sentinel
>= te
))
2357 if (whitespace (string
[sentinel
- 1]))
2360 ret
= add_string_to_list (token
, ret
);
2373 return (REVERSE_LIST (ret
, WORD_LIST
*));
2375 #endif /* READLINE */
2379 /* Extract the name of the variable to bind to from the assignment string. */
2381 assignment_name (string
)
2387 offset
= assignment (string
, 0);
2389 return (char *)NULL
;
2390 temp
= substring (string
, 0, offset
);
2395 /* **************************************************************** */
2397 /* Functions to convert strings to WORD_LISTs and vice versa */
2399 /* **************************************************************** */
2401 /* Return a single string of all the words in LIST. SEP is the separator
2402 to put between individual elements of LIST in the output string. */
2404 string_list_internal (list
, sep
)
2408 register WORD_LIST
*t
;
2410 size_t word_len
, sep_len
, result_size
;
2413 return ((char *)NULL
);
2415 /* Short-circuit quickly if we don't need to separate anything. */
2416 if (list
->next
== 0)
2417 return (savestring (list
->word
->word
));
2419 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
2420 sep_len
= STRLEN (sep
);
2423 for (t
= list
; t
; t
= t
->next
)
2426 result_size
+= sep_len
;
2427 result_size
+= strlen (t
->word
->word
);
2430 r
= result
= (char *)xmalloc (result_size
+ 1);
2432 for (t
= list
; t
; t
= t
->next
)
2434 if (t
!= list
&& sep_len
)
2438 FASTCOPY (sep
, r
, sep_len
);
2445 word_len
= strlen (t
->word
->word
);
2446 FASTCOPY (t
->word
->word
, r
, word_len
);
2454 /* Return a single string of all the words present in LIST, separating
2455 each word with a space. */
2460 return (string_list_internal (list
, " "));
2463 /* An external interface that can be used by the rest of the shell to
2464 obtain a string containing the first character in $IFS. Handles all
2465 the multibyte complications. If LENP is non-null, it is set to the
2466 length of the returned string. */
2468 ifs_firstchar (lenp
)
2474 ret
= xmalloc (MB_LEN_MAX
+ 1);
2475 #if defined (HANDLE_MULTIBYTE)
2476 if (ifs_firstc_len
== 1)
2478 ret
[0] = ifs_firstc
[0];
2480 len
= ret
[0] ? 1 : 0;
2484 memcpy (ret
, ifs_firstc
, ifs_firstc_len
);
2485 ret
[len
= ifs_firstc_len
] = '\0';
2488 ret
[0] = ifs_firstc
;
2490 len
= ret
[0] ? 0 : 1;
2499 /* Return a single string of all the words present in LIST, obeying the
2500 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
2501 expansion [of $*] appears within a double quoted string, it expands
2502 to a single field with the value of each parameter separated by the
2503 first character of the IFS variable, or by a <space> if IFS is unset." */
2505 string_list_dollar_star (list
)
2509 #if defined (HANDLE_MULTIBYTE)
2510 # if defined (__GNUC__)
2511 char sep
[MB_CUR_MAX
+ 1];
2519 #if defined (HANDLE_MULTIBYTE)
2520 # if !defined (__GNUC__)
2521 sep
= (char *)xmalloc (MB_CUR_MAX
+ 1);
2522 # endif /* !__GNUC__ */
2523 if (ifs_firstc_len
== 1)
2525 sep
[0] = ifs_firstc
[0];
2530 memcpy (sep
, ifs_firstc
, ifs_firstc_len
);
2531 sep
[ifs_firstc_len
] = '\0';
2534 sep
[0] = ifs_firstc
;
2538 ret
= string_list_internal (list
, sep
);
2539 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2545 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
2546 is non-zero, the $@ appears within double quotes, and we should quote
2547 the list before converting it into a string. If IFS is unset, and the
2548 word is not quoted, we just need to quote CTLESC and CTLNUL characters
2549 in the words in the list, because the default value of $IFS is
2550 <space><tab><newline>, IFS characters in the words in the list should
2551 also be split. If IFS is null, and the word is not quoted, we need
2552 to quote the words in the list to preserve the positional parameters
2554 Valid values for the FLAGS argument are the PF_ flags in command.h,
2555 the only one we care about is PF_ASSIGNRHS. $@ is supposed to expand
2556 to the positional parameters separated by spaces no matter what IFS is
2557 set to if in a context where word splitting is not performed. The only
2558 one that we didn't handle before is assignment statement arguments to
2559 declaration builtins like `declare'. */
2561 string_list_dollar_at (list
, quoted
, flags
)
2567 #if defined (HANDLE_MULTIBYTE)
2568 # if defined (__GNUC__)
2569 char sep
[MB_CUR_MAX
+ 1];
2572 # endif /* !__GNUC__ */
2578 /* XXX this could just be ifs = ifs_value; */
2579 ifs
= ifs_var
? value_cell (ifs_var
) : (char *)0;
2581 #if defined (HANDLE_MULTIBYTE)
2582 # if !defined (__GNUC__)
2583 sep
= (char *)xmalloc (MB_CUR_MAX
+ 1);
2584 # endif /* !__GNUC__ */
2585 /* XXX - bash-4.4/bash-5.0 testing PF_ASSIGNRHS */
2586 if (flags
& PF_ASSIGNRHS
)
2591 else if (ifs
&& *ifs
)
2593 if (ifs_firstc_len
== 1)
2595 sep
[0] = ifs_firstc
[0];
2600 memcpy (sep
, ifs_firstc
, ifs_firstc_len
);
2601 sep
[ifs_firstc_len
] = '\0';
2610 /* XXX - bash-4.4/bash-5.0 test PF_ASSIGNRHS */
2611 sep
[0] = ((flags
& PF_ASSIGNRHS
) || ifs
== 0 || *ifs
== 0) ? ' ' : *ifs
;
2615 /* XXX -- why call quote_list if ifs == 0? we can get away without doing
2616 it now that quote_escapes quotes spaces */
2617 tlist
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
|Q_PATQUOTE
))
2619 : list_quote_escapes (list
);
2621 ret
= string_list_internal (tlist
, sep
);
2622 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2628 /* Turn the positional parameters into a string, understanding quoting and
2629 the various subtleties of using the first character of $IFS as the
2630 separator. Calls string_list_dollar_at, string_list_dollar_star, and
2631 string_list as appropriate. */
2633 string_list_pos_params (pchar
, list
, quoted
)
2641 if (pchar
== '*' && (quoted
& Q_DOUBLE_QUOTES
))
2643 tlist
= quote_list (list
);
2644 word_list_remove_quoted_nulls (tlist
);
2645 ret
= string_list_dollar_star (tlist
);
2647 else if (pchar
== '*' && (quoted
& Q_HERE_DOCUMENT
))
2649 tlist
= quote_list (list
);
2650 word_list_remove_quoted_nulls (tlist
);
2651 ret
= string_list (tlist
);
2653 else if (pchar
== '*')
2655 /* Even when unquoted, string_list_dollar_star does the right thing
2656 making sure that the first character of $IFS is used as the
2658 ret
= string_list_dollar_star (list
);
2660 else if (pchar
== '@' && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
2661 /* We use string_list_dollar_at, but only if the string is quoted, since
2662 that quotes the escapes if it's not, which we don't want. We could
2663 use string_list (the old code did), but that doesn't do the right
2664 thing if the first character of $IFS is not a space. We use
2665 string_list_dollar_star if the string is unquoted so we make sure that
2666 the elements of $@ are separated by the first character of $IFS for
2668 ret
= string_list_dollar_at (list
, quoted
, 0);
2669 else if (pchar
== '@')
2670 ret
= string_list_dollar_star (list
);
2672 ret
= string_list ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? quote_list (list
) : list
);
2677 /* Return the list of words present in STRING. Separate the string into
2678 words at any of the characters found in SEPARATORS. If QUOTED is
2679 non-zero then word in the list will have its quoted flag set, otherwise
2680 the quoted flag is left as make_word () deemed fit.
2682 This obeys the P1003.2 word splitting semantics. If `separators' is
2683 exactly <space><tab><newline>, then the splitting algorithm is that of
2684 the Bourne shell, which treats any sequence of characters from `separators'
2685 as a delimiter. If IFS is unset, which results in `separators' being set
2686 to "", no splitting occurs. If separators has some other value, the
2687 following rules are applied (`IFS white space' means zero or more
2688 occurrences of <space>, <tab>, or <newline>, as long as those characters
2689 are in `separators'):
2691 1) IFS white space is ignored at the start and the end of the
2693 2) Each occurrence of a character in `separators' that is not
2694 IFS white space, along with any adjacent occurrences of
2695 IFS white space delimits a field.
2696 3) Any nonzero-length sequence of IFS white space delimits a field.
2699 /* BEWARE! list_string strips null arguments. Don't call it twice and
2700 expect to have "" preserved! */
2702 /* This performs word splitting and quoted null character removal on
2705 (((separators)[0]) ? ((separators)[1] ? isifs(c) \
2706 : (c) == (separators)[0]) \
2710 list_string (string
, separators
, quoted
)
2711 register char *string
, *separators
;
2716 char *current_word
, *s
;
2717 int sindex
, sh_style_split
, whitesep
, xflags
;
2720 if (!string
|| !*string
)
2721 return ((WORD_LIST
*)NULL
);
2723 sh_style_split
= separators
&& separators
[0] == ' ' &&
2724 separators
[1] == '\t' &&
2725 separators
[2] == '\n' &&
2726 separators
[3] == '\0';
2727 for (xflags
= 0, s
= ifs_value
; s
&& *s
; s
++)
2729 if (*s
== CTLESC
) xflags
|= SX_NOCTLESC
;
2730 else if (*s
== CTLNUL
) xflags
|= SX_NOESCCTLNUL
;
2734 /* Remove sequences of whitespace at the beginning of STRING, as
2735 long as those characters appear in IFS. Do not do this if
2736 STRING is quoted or if there are no separator characters. */
2737 if (!quoted
|| !separators
|| !*separators
)
2739 for (s
= string
; *s
&& spctabnl (*s
) && issep (*s
); s
++);
2742 return ((WORD_LIST
*)NULL
);
2747 /* OK, now STRING points to a word that does not begin with white space.
2748 The splitting algorithm is:
2749 extract a word, stopping at a separator
2750 skip sequences of spc, tab, or nl as long as they are separators
2751 This obeys the field splitting rules in Posix.2. */
2752 slen
= (MB_CUR_MAX
> 1) ? STRLEN (string
) : 1;
2753 for (result
= (WORD_LIST
*)NULL
, sindex
= 0; string
[sindex
]; )
2755 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2756 unless multibyte chars are possible. */
2757 current_word
= string_extract_verbatim (string
, slen
, &sindex
, separators
, xflags
);
2758 if (current_word
== 0)
2761 /* If we have a quoted empty string, add a quoted null argument. We
2762 want to preserve the quoted null character iff this is a quoted
2763 empty string; otherwise the quoted null characters are removed
2765 if (QUOTED_NULL (current_word
))
2767 t
= alloc_word_desc ();
2768 t
->word
= make_quoted_char ('\0');
2769 t
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
2770 result
= make_word_list (t
, result
);
2772 else if (current_word
[0] != '\0')
2774 /* If we have something, then add it regardless. However,
2775 perform quoted null character removal on the current word. */
2776 remove_quoted_nulls (current_word
);
2777 result
= add_string_to_list (current_word
, result
);
2778 result
->word
->flags
&= ~W_HASQUOTEDNULL
; /* just to be sure */
2779 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
2780 result
->word
->flags
|= W_QUOTED
;
2783 /* If we're not doing sequences of separators in the traditional
2784 Bourne shell style, then add a quoted null argument. */
2785 else if (!sh_style_split
&& !spctabnl (string
[sindex
]))
2787 t
= alloc_word_desc ();
2788 t
->word
= make_quoted_char ('\0');
2789 t
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
2790 result
= make_word_list (t
, result
);
2793 free (current_word
);
2795 /* Note whether or not the separator is IFS whitespace, used later. */
2796 whitesep
= string
[sindex
] && spctabnl (string
[sindex
]);
2798 /* Move past the current separator character. */
2802 ADVANCE_CHAR (string
, slen
, sindex
);
2805 /* Now skip sequences of space, tab, or newline characters if they are
2806 in the list of separators. */
2807 while (string
[sindex
] && spctabnl (string
[sindex
]) && issep (string
[sindex
]))
2810 /* If the first separator was IFS whitespace and the current character
2811 is a non-whitespace IFS character, it should be part of the current
2812 field delimiter, not a separate delimiter that would result in an
2813 empty field. Look at POSIX.2, 3.6.5, (3)(b). */
2814 if (string
[sindex
] && whitesep
&& issep (string
[sindex
]) && !spctabnl (string
[sindex
]))
2817 /* An IFS character that is not IFS white space, along with any
2818 adjacent IFS white space, shall delimit a field. (SUSv3) */
2819 while (string
[sindex
] && spctabnl (string
[sindex
]) && isifs (string
[sindex
]))
2823 return (REVERSE_LIST (result
, WORD_LIST
*));
2826 /* Parse a single word from STRING, using SEPARATORS to separate fields.
2827 ENDPTR is set to the first character after the word. This is used by
2828 the `read' builtin. This is never called with SEPARATORS != $IFS;
2829 it should be simplified.
2831 XXX - this function is very similar to list_string; they should be
2834 get_word_from_string (stringp
, separators
, endptr
)
2835 char **stringp
, *separators
, **endptr
;
2839 int sindex
, sh_style_split
, whitesep
, xflags
;
2842 if (!stringp
|| !*stringp
|| !**stringp
)
2843 return ((char *)NULL
);
2845 sh_style_split
= separators
&& separators
[0] == ' ' &&
2846 separators
[1] == '\t' &&
2847 separators
[2] == '\n' &&
2848 separators
[3] == '\0';
2849 for (xflags
= 0, s
= ifs_value
; s
&& *s
; s
++)
2851 if (*s
== CTLESC
) xflags
|= SX_NOCTLESC
;
2852 if (*s
== CTLNUL
) xflags
|= SX_NOESCCTLNUL
;
2858 /* Remove sequences of whitespace at the beginning of STRING, as
2859 long as those characters appear in IFS. */
2860 if (sh_style_split
|| !separators
|| !*separators
)
2862 for (; *s
&& spctabnl (*s
) && isifs (*s
); s
++);
2864 /* If the string is nothing but whitespace, update it and return. */
2870 return ((char *)NULL
);
2874 /* OK, S points to a word that does not begin with white space.
2875 Now extract a word, stopping at a separator, save a pointer to
2876 the first character after the word, then skip sequences of spc,
2877 tab, or nl as long as they are separators.
2879 This obeys the field splitting rules in Posix.2. */
2881 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2882 unless multibyte chars are possible. */
2883 slen
= (MB_CUR_MAX
> 1) ? STRLEN (s
) : 1;
2884 current_word
= string_extract_verbatim (s
, slen
, &sindex
, separators
, xflags
);
2886 /* Set ENDPTR to the first character after the end of the word. */
2888 *endptr
= s
+ sindex
;
2890 /* Note whether or not the separator is IFS whitespace, used later. */
2891 whitesep
= s
[sindex
] && spctabnl (s
[sindex
]);
2893 /* Move past the current separator character. */
2897 ADVANCE_CHAR (s
, slen
, sindex
);
2900 /* Now skip sequences of space, tab, or newline characters if they are
2901 in the list of separators. */
2902 while (s
[sindex
] && spctabnl (s
[sindex
]) && isifs (s
[sindex
]))
2905 /* If the first separator was IFS whitespace and the current character is
2906 a non-whitespace IFS character, it should be part of the current field
2907 delimiter, not a separate delimiter that would result in an empty field.
2908 Look at POSIX.2, 3.6.5, (3)(b). */
2909 if (s
[sindex
] && whitesep
&& isifs (s
[sindex
]) && !spctabnl (s
[sindex
]))
2912 /* An IFS character that is not IFS white space, along with any adjacent
2913 IFS white space, shall delimit a field. */
2914 while (s
[sindex
] && spctabnl (s
[sindex
]) && isifs (s
[sindex
]))
2918 /* Update STRING to point to the next field. */
2919 *stringp
= s
+ sindex
;
2920 return (current_word
);
2923 /* Remove IFS white space at the end of STRING. Start at the end
2924 of the string and walk backwards until the beginning of the string
2925 or we find a character that's not IFS white space and not CTLESC.
2926 Only let CTLESC escape a white space character if SAW_ESCAPE is
2929 strip_trailing_ifs_whitespace (string
, separators
, saw_escape
)
2930 char *string
, *separators
;
2935 s
= string
+ STRLEN (string
) - 1;
2936 while (s
> string
&& ((spctabnl (*s
) && isifs (*s
)) ||
2937 (saw_escape
&& *s
== CTLESC
&& spctabnl (s
[1]))))
2945 /* Split STRING into words at whitespace. Obeys shell-style quoting with
2946 backslashes, single and double quotes. */
2948 list_string_with_quotes (string
)
2954 int c
, i
, tokstart
, len
;
2956 for (s
= string
; s
&& *s
&& spctabnl (*s
); s
++)
2958 if (s
== 0 || *s
== 0)
2959 return ((WORD_LIST
*)NULL
);
2963 list
= (WORD_LIST
*)NULL
;
2974 i
= skip_single_quoted (s
, s_len
, ++i
, 0);
2976 i
= skip_double_quoted (s
, s_len
, ++i
, 0);
2977 else if (c
== 0 || spctabnl (c
))
2979 /* We have found the end of a token. Make a word out of it and
2980 add it to the word list. */
2981 token
= substring (s
, tokstart
, i
);
2982 list
= add_string_to_list (token
, list
);
2984 while (spctabnl (s
[i
]))
2992 i
++; /* normal character */
2994 return (REVERSE_LIST (list
, WORD_LIST
*));
2998 /********************************************************/
3000 /* Functions to perform assignment statements */
3002 /********************************************************/
3004 #if defined (ARRAY_VARS)
3006 do_compound_assignment (name
, value
, flags
)
3011 int mklocal
, mkassoc
, mkglobal
;
3014 mklocal
= flags
& ASS_MKLOCAL
;
3015 mkassoc
= flags
& ASS_MKASSOC
;
3016 mkglobal
= flags
& ASS_MKGLOBAL
;
3018 if (mklocal
&& variable_context
)
3020 v
= find_variable (name
);
3021 if (v
&& ((readonly_p (v
) && (flags
& ASS_FORCE
) == 0) || noassign_p (v
)))
3023 if (v
&& readonly_p (v
))
3024 err_readonly (name
);
3025 return (v
); /* XXX */
3027 list
= expand_compound_array_assignment (v
, value
, flags
);
3029 v
= make_local_assoc_variable (name
);
3030 else if (v
== 0 || (array_p (v
) == 0 && assoc_p (v
) == 0) || v
->context
!= variable_context
)
3031 v
= make_local_array_variable (name
, 0);
3033 assign_compound_array_list (v
, list
, flags
);
3035 dispose_words (list
);
3037 /* In a function but forcing assignment in global context */
3038 else if (mkglobal
&& variable_context
)
3040 v
= find_global_variable (name
);
3041 if (v
&& ((readonly_p (v
) && (flags
& ASS_FORCE
) == 0) || noassign_p (v
)))
3043 if (v
&& readonly_p (v
))
3044 err_readonly (name
);
3045 return (v
); /* XXX */
3047 list
= expand_compound_array_assignment (v
, value
, flags
);
3048 if (v
== 0 && mkassoc
)
3049 v
= make_new_assoc_variable (name
);
3050 else if (v
&& mkassoc
&& assoc_p (v
) == 0)
3051 v
= convert_var_to_assoc (v
);
3053 v
= make_new_array_variable (name
);
3054 else if (v
&& mkassoc
== 0 && array_p (v
) == 0)
3055 v
= convert_var_to_array (v
);
3057 assign_compound_array_list (v
, list
, flags
);
3059 dispose_words (list
);
3063 v
= assign_array_from_string (name
, value
, flags
);
3064 if (v
&& ((readonly_p (v
) && (flags
& ASS_FORCE
) == 0) || noassign_p (v
)))
3066 if (v
&& readonly_p (v
))
3067 err_readonly (name
);
3068 return (v
); /* XXX */
3076 /* Given STRING, an assignment string, get the value of the right side
3077 of the `=', and bind it to the left side. If EXPAND is true, then
3078 perform parameter expansion, command substitution, and arithmetic
3079 expansion on the right-hand side. Perform tilde expansion in any
3080 case. Do not perform word splitting on the result of expansion. */
3082 do_assignment_internal (word
, expand
)
3083 const WORD_DESC
*word
;
3086 int offset
, appendop
, assign_list
, aflags
, retval
;
3087 char *name
, *value
, *temp
;
3089 #if defined (ARRAY_VARS)
3095 if (word
== 0 || word
->word
== 0)
3098 appendop
= assign_list
= aflags
= 0;
3099 string
= word
->word
;
3100 offset
= assignment (string
, 0);
3101 name
= savestring (string
);
3102 value
= (char *)NULL
;
3104 if (name
[offset
] == '=')
3106 if (name
[offset
- 1] == '+')
3109 name
[offset
- 1] = '\0';
3112 name
[offset
] = 0; /* might need this set later */
3113 temp
= name
+ offset
+ 1;
3115 #if defined (ARRAY_VARS)
3116 if (expand
&& (word
->flags
& W_COMPASSIGN
))
3118 assign_list
= ni
= 1;
3119 value
= extract_array_assignment_list (temp
, &ni
);
3123 if (expand
&& temp
[0])
3124 value
= expand_string_if_necessary (temp
, 0, expand_string_assignment
);
3126 value
= savestring (temp
);
3131 value
= (char *)xmalloc (1);
3135 if (echo_command_at_execute
)
3138 name
[offset
- 1] = '+';
3139 xtrace_print_assignment (name
, value
, assign_list
, 1);
3141 name
[offset
- 1] = '\0';
3144 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
3147 aflags
|= ASS_APPEND
;
3149 #if defined (ARRAY_VARS)
3150 if (t
= mbschr (name
, '[')) /*]*/
3154 report_error (_("%s: cannot assign list to array member"), name
);
3157 entry
= assign_array_element (name
, value
, aflags
);
3161 else if (assign_list
)
3163 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_ASSNGLOBAL
) == 0)
3164 aflags
|= ASS_MKLOCAL
;
3165 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_ASSNGLOBAL
))
3166 aflags
|= ASS_MKGLOBAL
;
3167 if (word
->flags
& W_ASSIGNASSOC
)
3168 aflags
|= ASS_MKASSOC
;
3169 entry
= do_compound_assignment (name
, value
, aflags
);
3172 #endif /* ARRAY_VARS */
3173 entry
= bind_variable (name
, value
, aflags
);
3175 stupidly_hack_special_variables (name
);
3177 /* Return 1 if the assignment seems to have been performed correctly. */
3178 if (entry
== 0 || readonly_p (entry
))
3179 retval
= 0; /* assignment failure */
3180 else if (noassign_p (entry
))
3182 last_command_exit_value
= EXECUTION_FAILURE
;
3183 retval
= 1; /* error status, but not assignment failure */
3188 if (entry
&& retval
!= 0 && noassign_p (entry
) == 0)
3189 VUNSETATTR (entry
, att_invisible
);
3191 ASSIGN_RETURN (retval
);
3194 /* Perform the assignment statement in STRING, and expand the
3195 right side by doing tilde, command and parameter expansion. */
3197 do_assignment (string
)
3202 td
.flags
= W_ASSIGNMENT
;
3205 return do_assignment_internal (&td
, 1);
3209 do_word_assignment (word
, flags
)
3213 return do_assignment_internal (word
, 1);
3216 /* Given STRING, an assignment string, get the value of the right side
3217 of the `=', and bind it to the left side. Do not perform any word
3218 expansions on the right hand side. */
3220 do_assignment_no_expand (string
)
3225 td
.flags
= W_ASSIGNMENT
;
3228 return (do_assignment_internal (&td
, 0));
3231 /***************************************************
3233 * Functions to manage the positional parameters *
3235 ***************************************************/
3237 /* Return the word list that corresponds to `$*'. */
3239 list_rest_of_args ()
3241 register WORD_LIST
*list
, *args
;
3244 /* Break out of the loop as soon as one of the dollar variables is null. */
3245 for (i
= 1, list
= (WORD_LIST
*)NULL
; i
< 10 && dollar_vars
[i
]; i
++)
3246 list
= make_word_list (make_bare_word (dollar_vars
[i
]), list
);
3248 for (args
= rest_of_args
; args
; args
= args
->next
)
3249 list
= make_word_list (make_bare_word (args
->word
->word
), list
);
3251 return (REVERSE_LIST (list
, WORD_LIST
*));
3257 register WORD_LIST
*list
;
3260 for (n
= 0; n
< 9 && dollar_vars
[n
+1]; n
++)
3262 for (list
= rest_of_args
; list
; list
= list
->next
)
3267 /* Return the value of a positional parameter. This handles values > 10. */
3269 get_dollar_var_value (ind
)
3276 temp
= dollar_vars
[ind
] ? savestring (dollar_vars
[ind
]) : (char *)NULL
;
3277 else /* We want something like ${11} */
3280 for (p
= rest_of_args
; p
&& ind
--; p
= p
->next
)
3282 temp
= p
? savestring (p
->word
->word
) : (char *)NULL
;
3287 /* Make a single large string out of the dollar digit variables,
3288 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
3289 case of "$*" with respect to IFS. */
3291 string_rest_of_args (dollar_star
)
3294 register WORD_LIST
*list
;
3297 list
= list_rest_of_args ();
3298 string
= dollar_star
? string_list_dollar_star (list
) : string_list (list
);
3299 dispose_words (list
);
3303 /* Return a string containing the positional parameters from START to
3304 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
3305 which only makes a difference if QUOTED is non-zero. If QUOTED includes
3306 Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
3307 no quoting chars are added. */
3309 pos_params (string
, start
, end
, quoted
)
3311 int start
, end
, quoted
;
3313 WORD_LIST
*save
, *params
, *h
, *t
;
3317 /* see if we can short-circuit. if start == end, we want 0 parameters. */
3319 return ((char *)NULL
);
3321 save
= params
= list_rest_of_args ();
3322 if (save
== 0 && start
> 0)
3323 return ((char *)NULL
);
3325 if (start
== 0) /* handle ${@:0[:x]} specially */
3327 t
= make_word_list (make_word (dollar_vars
[0]), params
);
3331 for (i
= start
? 1 : 0; params
&& i
< start
; i
++)
3332 params
= params
->next
;
3335 dispose_words (save
);
3336 return ((char *)NULL
);
3338 for (h
= t
= params
; params
&& i
< end
; i
++)
3341 params
= params
->next
;
3344 t
->next
= (WORD_LIST
*)NULL
;
3346 ret
= string_list_pos_params (string
[0], h
, quoted
);
3351 dispose_words (save
);
3355 /******************************************************************/
3357 /* Functions to expand strings to strings or WORD_LISTs */
3359 /******************************************************************/
3361 #if defined (PROCESS_SUBSTITUTION)
3362 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
3364 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
3367 /* If there are any characters in STRING that require full expansion,
3368 then call FUNC to expand STRING; otherwise just perform quote
3369 removal if necessary. This returns a new string. */
3371 expand_string_if_necessary (string
, quoted
, func
)
3382 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3383 slen
= (MB_CUR_MAX
> 1) ? strlen (string
) : 0;
3387 if (EXP_CHAR (string
[i
]))
3389 else if (string
[i
] == '\'' || string
[i
] == '\\' || string
[i
] == '"')
3391 ADVANCE_CHAR (string
, slen
, i
);
3396 list
= (*func
) (string
, quoted
);
3399 ret
= string_list (list
);
3400 dispose_words (list
);
3405 else if (saw_quote
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
3406 ret
= string_quote_removal (string
, quoted
);
3408 ret
= savestring (string
);
3413 static inline char *
3414 expand_string_to_string_internal (string
, quoted
, func
)
3422 if (string
== 0 || *string
== '\0')
3423 return ((char *)NULL
);
3425 list
= (*func
) (string
, quoted
);
3428 ret
= string_list (list
);
3429 dispose_words (list
);
3438 expand_string_to_string (string
, quoted
)
3442 return (expand_string_to_string_internal (string
, quoted
, expand_string
));
3446 expand_string_unsplit_to_string (string
, quoted
)
3450 return (expand_string_to_string_internal (string
, quoted
, expand_string_unsplit
));
3454 expand_assignment_string_to_string (string
, quoted
)
3458 return (expand_string_to_string_internal (string
, quoted
, expand_string_assignment
));
3462 expand_arith_string (string
, quoted
)
3467 WORD_LIST
*list
, *tlist
;
3473 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3474 slen
= (MB_CUR_MAX
> 1) ? strlen (string
) : 0;
3478 if (EXP_CHAR (string
[i
]))
3480 else if (string
[i
] == '\'' || string
[i
] == '\\' || string
[i
] == '"')
3482 ADVANCE_CHAR (string
, slen
, i
);
3487 /* This is expanded version of expand_string_internal as it's called by
3488 expand_string_leave_quoted */
3489 td
.flags
= W_NOPROCSUB
; /* don't want process substitution */
3490 td
.word
= savestring (string
);
3491 list
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3492 /* This takes care of the calls from expand_string_leave_quoted and
3496 tlist
= word_list_split (list
);
3497 dispose_words (list
);
3500 dequote_list (list
);
3502 /* This comes from expand_string_if_necessary */
3505 ret
= string_list (list
);
3506 dispose_words (list
);
3512 else if (saw_quote
&& (quoted
& Q_ARITH
))
3513 ret
= string_quote_removal (string
, quoted
);
3514 else if (saw_quote
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
3515 ret
= string_quote_removal (string
, quoted
);
3517 ret
= savestring (string
);
3522 #if defined (COND_COMMAND)
3523 /* Just remove backslashes in STRING. Returns a new string. */
3525 remove_backslashes (string
)
3530 r
= ret
= (char *)xmalloc (strlen (string
) + 1);
3531 for (s
= string
; s
&& *s
; )
3543 /* This needs better error handling. */
3544 /* Expand W for use as an argument to a unary or binary operator in a
3545 [[...]] expression. If SPECIAL is 1, this is the rhs argument
3546 to the != or == operator, and should be treated as a pattern. In
3547 this case, we quote the string specially for the globbing code. If
3548 SPECIAL is 2, this is an rhs argument for the =~ operator, and should
3549 be quoted appropriately for regcomp/regexec. The caller is responsible
3550 for removing the backslashes if the unquoted word is needed later. */
3552 cond_expand_word (w
, special
)
3560 if (w
->word
== 0 || w
->word
[0] == '\0')
3561 return ((char *)NULL
);
3563 expand_no_split_dollar_star
= 1;
3564 w
->flags
|= W_NOSPLIT2
;
3565 l
= call_expand_word_internal (w
, 0, 0, (int *)0, (int *)0);
3566 expand_no_split_dollar_star
= 0;
3569 if (special
== 0) /* LHS */
3572 r
= string_list (l
);
3576 /* Need to figure out whether or not we should call dequote_escapes
3577 or a new dequote_ctlnul function here, and under what
3579 qflags
= QGLOB_CVTNULL
;
3581 qflags
|= QGLOB_REGEXP
;
3582 word_list_remove_quoted_nulls (l
);
3583 p
= string_list (l
);
3584 r
= quote_string_for_globbing (p
, qflags
);
3596 /* Call expand_word_internal to expand W and handle error returns.
3597 A convenience function for functions that don't want to handle
3598 any errors or free any memory before aborting. */
3600 call_expand_word_internal (w
, q
, i
, c
, e
)
3606 result
= expand_word_internal (w
, q
, i
, c
, e
);
3607 if (result
== &expand_word_error
|| result
== &expand_word_fatal
)
3609 /* By convention, each time this error is returned, w->word has
3610 already been freed (it sometimes may not be in the fatal case,
3611 but that doesn't result in a memory leak because we're going
3612 to exit in most cases). */
3613 w
->word
= (char *)NULL
;
3614 last_command_exit_value
= EXECUTION_FAILURE
;
3615 exp_jump_to_top_level ((result
== &expand_word_error
) ? DISCARD
: FORCE_EOF
);
3623 /* Perform parameter expansion, command substitution, and arithmetic
3624 expansion on STRING, as if it were a word. Leave the result quoted.
3625 Since this does not perform word splitting, it leaves quoted nulls
3628 expand_string_internal (string
, quoted
)
3635 if (string
== 0 || *string
== 0)
3636 return ((WORD_LIST
*)NULL
);
3639 td
.word
= savestring (string
);
3641 tresult
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3647 /* Expand STRING by performing parameter expansion, command substitution,
3648 and arithmetic expansion. Dequote the resulting WORD_LIST before
3649 returning it, but do not perform word splitting. The call to
3650 remove_quoted_nulls () is in here because word splitting normally
3651 takes care of quote removal. */
3653 expand_string_unsplit (string
, quoted
)
3659 if (string
== 0 || *string
== '\0')
3660 return ((WORD_LIST
*)NULL
);
3662 expand_no_split_dollar_star
= 1;
3663 value
= expand_string_internal (string
, quoted
);
3664 expand_no_split_dollar_star
= 0;
3670 remove_quoted_nulls (value
->word
->word
);
3671 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3673 dequote_list (value
);
3678 /* Expand the rhs of an assignment statement */
3680 expand_string_assignment (string
, quoted
)
3687 if (string
== 0 || *string
== '\0')
3688 return ((WORD_LIST
*)NULL
);
3690 expand_no_split_dollar_star
= 1;
3692 td
.flags
= W_ASSIGNRHS
;
3693 td
.word
= savestring (string
);
3694 value
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3697 expand_no_split_dollar_star
= 0;
3703 remove_quoted_nulls (value
->word
->word
);
3704 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3706 dequote_list (value
);
3712 /* Expand one of the PS? prompt strings. This is a sort of combination of
3713 expand_string_unsplit and expand_string_internal, but returns the
3714 passed string when an error occurs. Might want to trap other calls
3715 to jump_to_top_level here so we don't endlessly loop. */
3717 expand_prompt_string (string
, quoted
, wflags
)
3725 if (string
== 0 || *string
== 0)
3726 return ((WORD_LIST
*)NULL
);
3729 td
.word
= savestring (string
);
3731 no_longjmp_on_fatal_error
= 1;
3732 value
= expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3733 no_longjmp_on_fatal_error
= 0;
3735 if (value
== &expand_word_error
|| value
== &expand_word_fatal
)
3737 value
= make_word_list (make_bare_word (string
), (WORD_LIST
*)NULL
);
3745 remove_quoted_nulls (value
->word
->word
);
3746 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3748 dequote_list (value
);
3753 /* Expand STRING just as if you were expanding a word, but do not dequote
3754 the resultant WORD_LIST. This is called only from within this file,
3755 and is used to correctly preserve quoted characters when expanding
3756 things like ${1+"$@"}. This does parameter expansion, command
3757 substitution, arithmetic expansion, and word splitting. */
3759 expand_string_leave_quoted (string
, quoted
)
3766 if (string
== 0 || *string
== '\0')
3767 return ((WORD_LIST
*)NULL
);
3769 tlist
= expand_string_internal (string
, quoted
);
3773 tresult
= word_list_split (tlist
);
3774 dispose_words (tlist
);
3777 return ((WORD_LIST
*)NULL
);
3780 /* This does not perform word splitting or dequote the WORD_LIST
3783 expand_string_for_rhs (string
, quoted
, dollar_at_p
, expanded_p
)
3785 int quoted
, *dollar_at_p
, *expanded_p
;
3790 if (string
== 0 || *string
== '\0')
3791 return (WORD_LIST
*)NULL
;
3793 expand_no_split_dollar_star
= 1;
3794 td
.flags
= W_NOSPLIT2
; /* no splitting, remove "" and '' */
3796 tresult
= call_expand_word_internal (&td
, quoted
, 1, dollar_at_p
, expanded_p
);
3797 expand_no_split_dollar_star
= 0;
3802 /* Expand STRING just as if you were expanding a word. This also returns
3803 a list of words. Note that filename globbing is *NOT* done for word
3804 or string expansion, just when the shell is expanding a command. This
3805 does parameter expansion, command substitution, arithmetic expansion,
3806 and word splitting. Dequote the resultant WORD_LIST before returning. */
3808 expand_string (string
, quoted
)
3814 if (string
== 0 || *string
== '\0')
3815 return ((WORD_LIST
*)NULL
);
3817 result
= expand_string_leave_quoted (string
, quoted
);
3818 return (result
? dequote_list (result
) : result
);
3821 /***************************************************
3823 * Functions to handle quoting chars *
3825 ***************************************************/
3829 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
3830 The parser passes CTLNUL as CTLESC CTLNUL. */
3832 /* Quote escape characters in string s, but no other characters. This is
3833 used to protect CTLESC and CTLNUL in variable values from the rest of
3834 the word expansion process after the variable is expanded (word splitting
3835 and filename generation). If IFS is null, we quote spaces as well, just
3836 in case we split on spaces later (in the case of unquoted $@, we will
3837 eventually attempt to split the entire word on spaces). Corresponding
3838 code exists in dequote_escapes. Even if we don't end up splitting on
3839 spaces, quoting spaces is not a problem. This should never be called on
3840 a string that is quoted with single or double quotes or part of a here
3841 document (effectively double-quoted). */
3843 quote_escapes (string
)
3846 register char *s
, *t
;
3848 char *result
, *send
;
3849 int quote_spaces
, skip_ctlesc
, skip_ctlnul
;
3852 slen
= strlen (string
);
3853 send
= string
+ slen
;
3855 quote_spaces
= (ifs_value
&& *ifs_value
== 0);
3857 for (skip_ctlesc
= skip_ctlnul
= 0, s
= ifs_value
; s
&& *s
; s
++)
3858 skip_ctlesc
|= *s
== CTLESC
, skip_ctlnul
|= *s
== CTLNUL
;
3860 t
= result
= (char *)xmalloc ((slen
* 2) + 1);
3865 if ((skip_ctlesc
== 0 && *s
== CTLESC
) || (skip_ctlnul
== 0 && *s
== CTLNUL
) || (quote_spaces
&& *s
== ' '))
3867 COPY_CHAR_P (t
, s
, send
);
3875 list_quote_escapes (list
)
3878 register WORD_LIST
*w
;
3881 for (w
= list
; w
; w
= w
->next
)
3884 w
->word
->word
= quote_escapes (t
);
3890 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
3892 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
3893 This is necessary to make unquoted CTLESC and CTLNUL characters in the
3894 data stream pass through properly.
3896 We need to remove doubled CTLESC characters inside quoted strings before
3897 quoting the entire string, so we do not double the number of CTLESC
3900 Also used by parts of the pattern substitution code. */
3902 dequote_escapes (string
)
3905 register char *s
, *t
, *s1
;
3907 char *result
, *send
;
3914 slen
= strlen (string
);
3915 send
= string
+ slen
;
3917 t
= result
= (char *)xmalloc (slen
+ 1);
3919 if (strchr (string
, CTLESC
) == 0)
3920 return (strcpy (result
, string
));
3922 quote_spaces
= (ifs_value
&& *ifs_value
== 0);
3927 if (*s
== CTLESC
&& (s
[1] == CTLESC
|| s
[1] == CTLNUL
|| (quote_spaces
&& s
[1] == ' ')))
3933 COPY_CHAR_P (t
, s
, send
);
3941 list_dequote_escapes (list
)
3944 register WORD_LIST
*w
;
3947 for (w
= list
; w
; w
= w
->next
)
3950 w
->word
->word
= dequote_escapes (t
);
3956 /* Return a new string with the quoted representation of character C.
3957 This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
3958 set in any resultant WORD_DESC where this value is the word. */
3960 make_quoted_char (c
)
3965 temp
= (char *)xmalloc (3);
3980 /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
3981 the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
3982 this value is the word. */
3984 quote_string (string
)
3989 char *result
, *send
;
3993 result
= (char *)xmalloc (2);
4001 slen
= strlen (string
);
4002 send
= string
+ slen
;
4004 result
= (char *)xmalloc ((slen
* 2) + 1);
4006 for (t
= result
; string
< send
; )
4009 COPY_CHAR_P (t
, string
, send
);
4016 /* De-quote quoted characters in STRING. */
4018 dequote_string (string
)
4021 register char *s
, *t
;
4023 char *result
, *send
;
4027 if (string
[0] == CTLESC
&& string
[1] == 0)
4028 internal_inform ("dequote_string: string with bare CTLESC");
4031 slen
= strlen (string
);
4033 t
= result
= (char *)xmalloc (slen
+ 1);
4035 if (QUOTED_NULL (string
))
4041 /* A string consisting of only a single CTLESC should pass through unchanged */
4042 if (string
[0] == CTLESC
&& string
[1] == 0)
4049 /* If no character in the string can be quoted, don't bother examining
4050 each character. Just return a copy of the string passed to us. */
4051 if (strchr (string
, CTLESC
) == NULL
)
4052 return (strcpy (result
, string
));
4054 send
= string
+ slen
;
4064 COPY_CHAR_P (t
, s
, send
);
4071 /* Quote the entire WORD_LIST list. */
4076 register WORD_LIST
*w
;
4079 for (w
= list
; w
; w
= w
->next
)
4082 w
->word
->word
= quote_string (t
);
4084 w
->word
->flags
|= W_HASQUOTEDNULL
; /* XXX - turn on W_HASQUOTEDNULL here? */
4085 w
->word
->flags
|= W_QUOTED
;
4091 /* De-quote quoted characters in each word in LIST. */
4097 register WORD_LIST
*tlist
;
4099 for (tlist
= list
; tlist
; tlist
= tlist
->next
)
4101 s
= dequote_string (tlist
->word
->word
);
4102 if (QUOTED_NULL (tlist
->word
->word
))
4103 tlist
->word
->flags
&= ~W_HASQUOTEDNULL
;
4104 free (tlist
->word
->word
);
4105 tlist
->word
->word
= s
;
4110 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
4113 remove_quoted_escapes (string
)
4120 t
= dequote_escapes (string
);
4128 /* Perform quoted null character removal on STRING. We don't allow any
4129 quoted null characters in the middle or at the ends of strings because
4130 of how expand_word_internal works. remove_quoted_nulls () turns
4131 STRING into an empty string iff it only consists of a quoted null,
4132 and removes all unquoted CTLNUL characters. */
4134 remove_quoted_nulls (string
)
4137 register size_t slen
;
4138 register int i
, j
, prev_i
;
4141 if (strchr (string
, CTLNUL
) == 0) /* XXX */
4142 return string
; /* XXX */
4144 slen
= strlen (string
);
4149 if (string
[i
] == CTLESC
)
4151 /* Old code had j++, but we cannot assume that i == j at this
4152 point -- what if a CTLNUL has already been removed from the
4153 string? We don't want to drop the CTLESC or recopy characters
4154 that we've already copied down. */
4155 i
++; string
[j
++] = CTLESC
;
4159 else if (string
[i
] == CTLNUL
)
4166 ADVANCE_CHAR (string
, slen
, i
);
4169 do string
[j
++] = string
[prev_i
++]; while (prev_i
< i
);
4179 /* Perform quoted null character removal on each element of LIST.
4180 This modifies LIST. */
4182 word_list_remove_quoted_nulls (list
)
4185 register WORD_LIST
*t
;
4187 for (t
= list
; t
; t
= t
->next
)
4189 remove_quoted_nulls (t
->word
->word
);
4190 t
->word
->flags
&= ~W_HASQUOTEDNULL
;
4194 /* **************************************************************** */
4196 /* Functions for Matching and Removing Patterns */
4198 /* **************************************************************** */
4200 #if defined (HANDLE_MULTIBYTE)
4201 #if 0 /* Currently unused */
4202 static unsigned char *
4203 mb_getcharlens (string
, len
)
4207 int i
, offset
, last
;
4214 ret
= (unsigned char *)xmalloc (len
);
4215 memset (ret
, 0, len
);
4216 while (string
[last
])
4218 ADVANCE_CHAR (string
, len
, offset
);
4219 ret
[last
] = offset
- last
;
4227 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
4228 can have one of 4 values:
4229 RP_LONG_LEFT remove longest matching portion at start of PARAM
4230 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
4231 RP_LONG_RIGHT remove longest matching portion at end of PARAM
4232 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
4235 #define RP_LONG_LEFT 1
4236 #define RP_SHORT_LEFT 2
4237 #define RP_LONG_RIGHT 3
4238 #define RP_SHORT_RIGHT 4
4240 /* Returns its first argument if nothing matched; new memory otherwise */
4242 remove_upattern (param
, pattern
, op
)
4243 char *param
, *pattern
;
4246 register size_t len
;
4248 register char *p
, *ret
, c
;
4250 len
= STRLEN (param
);
4255 case RP_LONG_LEFT
: /* remove longest match at start */
4256 for (p
= end
; p
>= param
; p
--)
4259 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4262 return (savestring (p
));
4269 case RP_SHORT_LEFT
: /* remove shortest match at start */
4270 for (p
= param
; p
<= end
; p
++)
4273 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4276 return (savestring (p
));
4282 case RP_LONG_RIGHT
: /* remove longest match at end */
4283 for (p
= param
; p
<= end
; p
++)
4285 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4288 ret
= savestring (param
);
4295 case RP_SHORT_RIGHT
: /* remove shortest match at end */
4296 for (p
= end
; p
>= param
; p
--)
4298 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4301 ret
= savestring (param
);
4309 return (param
); /* no match, return original string */
4312 #if defined (HANDLE_MULTIBYTE)
4313 /* Returns its first argument if nothing matched; new memory otherwise */
4315 remove_wpattern (wparam
, wstrlen
, wpattern
, op
)
4326 case RP_LONG_LEFT
: /* remove longest match at start */
4327 for (n
= wstrlen
; n
>= 0; n
--)
4329 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4330 if (wcsmatch (wpattern
, wparam
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4333 return (wcsdup (wparam
+ n
));
4339 case RP_SHORT_LEFT
: /* remove shortest match at start */
4340 for (n
= 0; n
<= wstrlen
; n
++)
4342 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4343 if (wcsmatch (wpattern
, wparam
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4346 return (wcsdup (wparam
+ n
));
4352 case RP_LONG_RIGHT
: /* remove longest match at end */
4353 for (n
= 0; n
<= wstrlen
; n
++)
4355 if (wcsmatch (wpattern
, wparam
+ n
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4357 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4358 ret
= wcsdup (wparam
);
4365 case RP_SHORT_RIGHT
: /* remove shortest match at end */
4366 for (n
= wstrlen
; n
>= 0; n
--)
4368 if (wcsmatch (wpattern
, wparam
+ n
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4370 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4371 ret
= wcsdup (wparam
);
4379 return (wparam
); /* no match, return original string */
4381 #endif /* HANDLE_MULTIBYTE */
4384 remove_pattern (param
, pattern
, op
)
4385 char *param
, *pattern
;
4392 if (*param
== '\0' || pattern
== NULL
|| *pattern
== '\0') /* minor optimization */
4393 return (savestring (param
));
4395 #if defined (HANDLE_MULTIBYTE)
4398 wchar_t *ret
, *oret
;
4400 wchar_t *wparam
, *wpattern
;
4403 n
= xdupmbstowcs (&wpattern
, NULL
, pattern
);
4404 if (n
== (size_t)-1)
4406 xret
= remove_upattern (param
, pattern
, op
);
4407 return ((xret
== param
) ? savestring (param
) : xret
);
4409 n
= xdupmbstowcs (&wparam
, NULL
, param
);
4411 if (n
== (size_t)-1)
4414 xret
= remove_upattern (param
, pattern
, op
);
4415 return ((xret
== param
) ? savestring (param
) : xret
);
4417 oret
= ret
= remove_wpattern (wparam
, n
, wpattern
, op
);
4418 /* Don't bother to convert wparam back to multibyte string if nothing
4419 matched; just return copy of original string */
4424 return (savestring (param
));
4431 xret
= (char *)xmalloc (n
+ 1);
4432 memset (&ps
, '\0', sizeof (mbstate_t));
4433 n
= wcsrtombs (xret
, (const wchar_t **)&ret
, n
, &ps
);
4434 xret
[n
] = '\0'; /* just to make sure */
4441 xret
= remove_upattern (param
, pattern
, op
);
4442 return ((xret
== param
) ? savestring (param
) : xret
);
4446 /* Match PAT anywhere in STRING and return the match boundaries.
4447 This returns 1 in case of a successful match, 0 otherwise. SP
4448 and EP are pointers into the string where the match begins and
4449 ends, respectively. MTYPE controls what kind of match is attempted.
4450 MATCH_BEG and MATCH_END anchor the match at the beginning and end
4451 of the string, respectively. The longest match is returned. */
4453 match_upattern (string
, pat
, mtype
, sp
, ep
)
4460 register char *p
, *p1
, *npat
;
4464 /* If the pattern doesn't match anywhere in the string, go ahead and
4465 short-circuit right away. A minor optimization, saves a bunch of
4466 unnecessary calls to strmatch (up to N calls for a string of N
4467 characters) if the match is unsuccessful. To preserve the semantics
4468 of the substring matches below, we make sure that the pattern has
4469 `*' as first and last character, making a new pattern if necessary. */
4470 /* XXX - check this later if I ever implement `**' with special meaning,
4471 since this will potentially result in `**' at the beginning or end */
4473 if (pat
[0] != '*' || (pat
[0] == '*' && pat
[1] == LPAREN
&& extended_glob
) || pat
[len
- 1] != '*')
4475 int unescaped_backslash
;
4478 p
= npat
= (char *)xmalloc (len
+ 3);
4480 if (*p1
!= '*' || (*p1
== '*' && p1
[1] == LPAREN
&& extended_glob
))
4485 /* Need to also handle a pattern that ends with an unescaped backslash.
4486 For right now, we ignore it because the pattern matching code will
4487 fail the match anyway */
4488 /* If the pattern ends with a `*' we leave it alone if it's preceded by
4489 an even number of backslashes, but if it's escaped by a backslash
4490 we need to add another `*'. */
4491 if (p1
[-1] == '*' && (unescaped_backslash
= p1
[-2] == '\\'))
4494 while (pp
>= pat
&& *pp
-- == '\\')
4495 unescaped_backslash
= 1 - unescaped_backslash
;
4496 if (unescaped_backslash
)
4499 else if (p1
[-1] != '*')
4502 if (p1
[-1] != '*' || p1
[-2] == '\\')
4509 c
= strmatch (npat
, string
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
);
4512 if (c
== FNM_NOMATCH
)
4515 len
= STRLEN (string
);
4518 mlen
= umatchlen (pat
, len
);
4523 for (p
= string
; p
<= end
; p
++)
4525 if (match_pattern_char (pat
, p
, FNMATCH_IGNCASE
))
4527 p1
= (mlen
== -1) ? end
: p
+ mlen
;
4528 /* p1 - p = length of portion of string to be considered
4529 p = current position in string
4530 mlen = number of characters consumed by match (-1 for entire string)
4532 we want to break immediately if the potential match len
4533 is greater than the number of characters remaining in the
4538 for ( ; p1
>= p
; p1
--)
4540 c
= *p1
; *p1
= '\0';
4541 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4550 /* If MLEN != -1, we have a fixed length pattern. */
4561 if (match_pattern_char (pat
, string
, FNMATCH_IGNCASE
) == 0)
4564 for (p
= (mlen
== -1) ? end
: string
+ mlen
; p
>= string
; p
--)
4567 if (strmatch (pat
, string
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4575 /* If MLEN != -1, we have a fixed length pattern. */
4583 for (p
= end
- ((mlen
== -1) ? len
: mlen
); p
<= end
; p
++)
4585 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4591 /* If MLEN != -1, we have a fixed length pattern. */
4602 #if defined (HANDLE_MULTIBYTE)
4604 #define WFOLD(c) (match_ignore_case && iswupper (c) ? towlower (c) : (c))
4606 /* Match WPAT anywhere in WSTRING and return the match boundaries.
4607 This returns 1 in case of a successful match, 0 otherwise. Wide
4608 character version. */
4610 match_wpattern (wstring
, indices
, wstrlen
, wpat
, mtype
, sp
, ep
)
4618 wchar_t wc
, *wp
, *nwpat
, *wp1
;
4621 int n
, n1
, n2
, simple
;
4623 simple
= (wpat
[0] != L
'\\' && wpat
[0] != L
'*' && wpat
[0] != L
'?' && wpat
[0] != L
'[');
4624 #if defined (EXTENDED_GLOB)
4626 simple
&= (wpat
[1] != L
'(' || (wpat
[0] != L
'*' && wpat
[0] != L
'?' && wpat
[0] != L
'+' && wpat
[0] != L
'!' && wpat
[0] != L
'@')); /*)*/
4629 /* If the pattern doesn't match anywhere in the string, go ahead and
4630 short-circuit right away. A minor optimization, saves a bunch of
4631 unnecessary calls to strmatch (up to N calls for a string of N
4632 characters) if the match is unsuccessful. To preserve the semantics
4633 of the substring matches below, we make sure that the pattern has
4634 `*' as first and last character, making a new pattern if necessary. */
4635 len
= wcslen (wpat
);
4636 if (wpat
[0] != L
'*' || (wpat
[0] == L
'*' && wpat
[1] == WLPAREN
&& extended_glob
) || wpat
[len
- 1] != L
'*')
4638 int unescaped_backslash
;
4641 wp
= nwpat
= (wchar_t *)xmalloc ((len
+ 3) * sizeof (wchar_t));
4643 if (*wp1
!= L
'*' || (*wp1
== '*' && wp1
[1] == WLPAREN
&& extended_glob
))
4645 while (*wp1
!= L
'\0')
4648 /* See comments above in match_upattern. */
4649 if (wp1
[-1] == L
'*' && (unescaped_backslash
= wp1
[-2] == L
'\\'))
4652 while (wpp
>= wpat
&& *wpp
-- == L
'\\')
4653 unescaped_backslash
= 1 - unescaped_backslash
;
4654 if (unescaped_backslash
)
4657 else if (wp1
[-1] != L
'*')
4660 if (wp1
[-1] != L
'*' || wp1
[-2] == L
'\\')
4667 len
= wcsmatch (nwpat
, wstring
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
);
4670 if (len
== FNM_NOMATCH
)
4673 mlen
= wmatchlen (wpat
, wstrlen
);
4675 /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */
4679 for (n
= 0; n
<= wstrlen
; n
++)
4681 n2
= simple
? (WFOLD(*wpat
) == WFOLD(wstring
[n
])) : match_pattern_wchar (wpat
, wstring
+ n
, FNMATCH_IGNCASE
);
4684 n1
= (mlen
== -1) ? wstrlen
: n
+ mlen
;
4688 for ( ; n1
>= n
; n1
--)
4690 wc
= wstring
[n1
]; wstring
[n1
] = L
'\0';
4691 if (wcsmatch (wpat
, wstring
+ n
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4699 /* If MLEN != -1, we have a fixed length pattern. */
4709 if (match_pattern_wchar (wpat
, wstring
, FNMATCH_IGNCASE
) == 0)
4712 for (n
= (mlen
== -1) ? wstrlen
: mlen
; n
>= 0; n
--)
4714 wc
= wstring
[n
]; wstring
[n
] = L
'\0';
4715 if (wcsmatch (wpat
, wstring
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4723 /* If MLEN != -1, we have a fixed length pattern. */
4731 for (n
= wstrlen
- ((mlen
== -1) ? wstrlen
: mlen
); n
<= wstrlen
; n
++)
4733 if (wcsmatch (wpat
, wstring
+ n
, FNMATCH_EXTFLAG
| FNMATCH_IGNCASE
) == 0)
4736 *ep
= indices
[wstrlen
];
4739 /* If MLEN != -1, we have a fixed length pattern. */
4750 #endif /* HANDLE_MULTIBYTE */
4753 match_pattern (string
, pat
, mtype
, sp
, ep
)
4758 #if defined (HANDLE_MULTIBYTE)
4761 wchar_t *wstring
, *wpat
;
4763 size_t slen
, plen
, mslen
, mplen
;
4766 if (string
== 0 || pat
== 0 || *pat
== 0)
4769 #if defined (HANDLE_MULTIBYTE)
4772 if (mbsmbchar (string
) == 0 && mbsmbchar (pat
) == 0)
4773 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
4775 n
= xdupmbstowcs (&wpat
, NULL
, pat
);
4776 if (n
== (size_t)-1)
4777 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
4778 n
= xdupmbstowcs (&wstring
, &indices
, string
);
4779 if (n
== (size_t)-1)
4782 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
4784 ret
= match_wpattern (wstring
, indices
, n
, wpat
, mtype
, sp
, ep
);
4794 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
4798 getpatspec (c
, value
)
4803 return ((*value
== '#') ? RP_LONG_LEFT
: RP_SHORT_LEFT
);
4805 return ((*value
== '%') ? RP_LONG_RIGHT
: RP_SHORT_RIGHT
);
4808 /* Posix.2 says that the WORD should be run through tilde expansion,
4809 parameter expansion, command substitution and arithmetic expansion.
4810 This leaves the result quoted, so quote_string_for_globbing () has
4811 to be called to fix it up for strmatch (). If QUOTED is non-zero,
4812 it means that the entire expression was enclosed in double quotes.
4813 This means that quoting characters in the pattern do not make any
4814 special pattern characters quoted. For example, the `*' in the
4815 following retains its special meaning: "${foo#'*'}". */
4817 getpattern (value
, quoted
, expandpat
)
4819 int quoted
, expandpat
;
4826 /* There is a problem here: how to handle single or double quotes in the
4827 pattern string when the whole expression is between double quotes?
4828 POSIX.2 says that enclosing double quotes do not cause the pattern to
4829 be quoted, but does that leave us a problem with @ and array[@] and their
4830 expansions inside a pattern? */
4832 if (expandpat
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *tword
)
4835 pat
= string_extract_double_quoted (tword
, &i
, SX_STRIPDQ
);
4841 /* expand_string_for_rhs () leaves WORD quoted and does not perform
4843 l
= *value
? expand_string_for_rhs (value
,
4844 (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? Q_PATQUOTE
: quoted
,
4845 (int *)NULL
, (int *)NULL
)
4847 pat
= string_list (l
);
4851 tword
= quote_string_for_globbing (pat
, QGLOB_CVTNULL
);
4859 /* Handle removing a pattern from a string as a result of ${name%[%]value}
4860 or ${name#[#]value}. */
4862 variable_remove_pattern (value
, pattern
, patspec
, quoted
)
4863 char *value
, *pattern
;
4864 int patspec
, quoted
;
4868 tword
= remove_pattern (value
, pattern
, patspec
);
4875 list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
)
4878 int patspec
, itype
, quoted
;
4884 for (new = (WORD_LIST
*)NULL
, l
= list
; l
; l
= l
->next
)
4886 tword
= remove_pattern (l
->word
->word
, pattern
, patspec
);
4887 w
= alloc_word_desc ();
4888 w
->word
= tword
? tword
: savestring ("");
4889 new = make_word_list (w
, new);
4892 l
= REVERSE_LIST (new, WORD_LIST
*);
4893 tword
= string_list_pos_params (itype
, l
, quoted
);
4900 parameter_list_remove_pattern (itype
, pattern
, patspec
, quoted
)
4903 int patspec
, quoted
;
4908 list
= list_rest_of_args ();
4910 return ((char *)NULL
);
4911 ret
= list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
);
4912 dispose_words (list
);
4916 #if defined (ARRAY_VARS)
4918 array_remove_pattern (var
, pattern
, patspec
, varname
, quoted
)
4922 char *varname
; /* so we can figure out how it's indexed */
4932 /* compute itype from varname here */
4933 v
= array_variable_part (varname
, &ret
, 0);
4936 if (v
&& invisible_p (v
))
4937 return ((char *)NULL
);
4941 a
= (v
&& array_p (v
)) ? array_cell (v
) : 0;
4942 h
= (v
&& assoc_p (v
)) ? assoc_cell (v
) : 0;
4944 list
= a
? array_to_word_list (a
) : (h
? assoc_to_word_list (h
) : 0);
4946 return ((char *)NULL
);
4947 ret
= list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
);
4948 dispose_words (list
);
4952 #endif /* ARRAY_VARS */
4955 parameter_brace_remove_pattern (varname
, value
, ind
, patstr
, rtype
, quoted
, flags
)
4956 char *varname
, *value
;
4959 int rtype
, quoted
, flags
;
4961 int vtype
, patspec
, starsub
;
4962 char *temp1
, *val
, *pattern
;
4966 return ((char *)NULL
);
4968 this_command_name
= varname
;
4970 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
4972 return ((char *)NULL
);
4974 starsub
= vtype
& VT_STARSUB
;
4975 vtype
&= ~VT_STARSUB
;
4977 patspec
= getpatspec (rtype
, patstr
);
4978 if (patspec
== RP_LONG_LEFT
|| patspec
== RP_LONG_RIGHT
)
4981 /* Need to pass getpattern newly-allocated memory in case of expansion --
4982 the expansion code will free the passed string on an error. */
4983 temp1
= savestring (patstr
);
4984 pattern
= getpattern (temp1
, quoted
, 1);
4987 temp1
= (char *)NULL
; /* shut up gcc */
4991 case VT_ARRAYMEMBER
:
4992 temp1
= remove_pattern (val
, pattern
, patspec
);
4993 if (vtype
== VT_VARIABLE
)
4997 val
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
4998 ? quote_string (temp1
)
4999 : quote_escapes (temp1
);
5004 #if defined (ARRAY_VARS)
5006 temp1
= array_remove_pattern (v
, pattern
, patspec
, varname
, quoted
);
5007 if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
5009 val
= quote_escapes (temp1
);
5016 temp1
= parameter_list_remove_pattern (varname
[0], pattern
, patspec
, quoted
);
5017 if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
5019 val
= quote_escapes (temp1
);
5031 string_var_assignment (v
, s
)
5035 char flags
[MAX_ATTRIBUTES
], *ret
, *val
;
5038 val
= sh_quote_reusable (s
, 0);
5039 i
= var_attribute_string (v
, 0, flags
);
5040 ret
= (char *)xmalloc (i
+ strlen (val
) + strlen (v
->name
) + 16 + MAX_ATTRIBUTES
);
5042 sprintf (ret
, "declare -%s %s=%s", flags
, v
->name
, val
);
5044 sprintf (ret
, "%s=%s", v
->name
, val
);
5049 #if defined (ARRAY_VARS)
5051 array_var_assignment (v
, itype
, quoted
)
5055 char *ret
, *val
, flags
[MAX_ATTRIBUTES
];
5059 return (char *)NULL
;
5060 val
= array_p (v
) ? array_to_assign (array_cell (v
), 0)
5061 : assoc_to_assign (assoc_cell (v
), 0);
5064 val
= (char *)xmalloc (3);
5071 ret
= (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) ? quote_string (val
) : quote_escapes (val
);
5075 i
= var_attribute_string (v
, 0, flags
);
5076 ret
= (char *)xmalloc (i
+ strlen (val
) + strlen (v
->name
) + 16);
5077 sprintf (ret
, "declare -%s %s=%s", flags
, v
->name
, val
);
5084 pos_params_assignment (list
, itype
, quoted
)
5091 /* first, we transform the list to quote each word. */
5092 temp
= list_transform ('Q', (SHELL_VAR
*)0, list
, itype
, quoted
);
5093 ret
= (char *)xmalloc (strlen (temp
) + 8);
5094 strcpy (ret
, "set -- ");
5095 strcpy (ret
+ 7, temp
);
5101 string_transform (xc
, v
, s
)
5106 char *ret
, flags
[MAX_ATTRIBUTES
];
5109 if (((xc
== 'A' || xc
== 'a') && v
== 0) || (xc
!= 'a' && s
== 0))
5110 return (char *)NULL
;
5114 /* Transformations that interrogate the variable */
5116 i
= var_attribute_string (v
, 0, flags
);
5117 ret
= (i
> 0) ? savestring (flags
) : (char *)NULL
;
5120 ret
= string_var_assignment (v
, s
);
5122 /* Transformations that modify the variable's value */
5124 ret
= ansiexpand (s
, 0, strlen (s
), (int *)0);
5127 ret
= decode_prompt_string (s
);
5130 ret
= sh_quote_reusable (s
, 0);
5140 list_transform (xc
, v
, list
, itype
, quoted
)
5150 for (new = (WORD_LIST
*)NULL
, l
= list
; l
; l
= l
->next
)
5152 tword
= string_transform (xc
, v
, l
->word
->word
);
5153 w
= alloc_word_desc ();
5154 w
->word
= tword
? tword
: savestring (""); /* XXX */
5155 new = make_word_list (w
, new);
5158 l
= REVERSE_LIST (new, WORD_LIST
*);
5159 tword
= string_list_pos_params (itype
, l
, quoted
);
5166 parameter_list_transform (xc
, itype
, quoted
)
5174 list
= list_rest_of_args ();
5176 return ((char *)NULL
);
5178 return (pos_params_assignment (list
, itype
, quoted
));
5179 ret
= list_transform (xc
, (SHELL_VAR
*)0, list
, itype
, quoted
);
5180 dispose_words (list
);
5184 #if defined (ARRAY_VARS)
5186 array_transform (xc
, var
, varname
, quoted
)
5189 char *varname
; /* so we can figure out how it's indexed */
5199 /* compute itype from varname here */
5200 v
= array_variable_part (varname
, &ret
, 0);
5203 if (v
&& invisible_p (v
))
5204 return ((char *)NULL
);
5209 return (array_var_assignment (v
, itype
, quoted
));
5211 a
= (v
&& array_p (v
)) ? array_cell (v
) : 0;
5212 h
= (v
&& assoc_p (v
)) ? assoc_cell (v
) : 0;
5214 list
= a
? array_to_word_list (a
) : (h
? assoc_to_word_list (h
) : 0);
5216 return ((char *)NULL
);
5217 ret
= list_transform (xc
, v
, list
, itype
, quoted
);
5218 dispose_words (list
);
5222 #endif /* ARRAY_VARS */
5225 parameter_brace_transform (varname
, value
, ind
, xform
, rtype
, quoted
, flags
)
5226 char *varname
, *value
;
5229 int rtype
, quoted
, flags
;
5236 if (value
== 0 && xc
!= 'A' && xc
!= 'a')
5237 return ((char *)NULL
);
5239 this_command_name
= varname
;
5241 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
5243 return ((char *)NULL
);
5245 /* check for valid values of xc */
5248 case 'a': /* expand to a string with just attributes */
5249 case 'A': /* expand as an assignment statement with attributes */
5250 case 'E': /* expand like $'...' */
5251 case 'P': /* expand like prompt string */
5252 case 'Q': /* quote reusably */
5255 return &expand_param_error
;
5258 temp1
= (char *)NULL
; /* shut up gcc */
5259 switch (vtype
& ~VT_STARSUB
)
5262 case VT_ARRAYMEMBER
:
5263 temp1
= string_transform (xc
, v
, val
);
5264 if (vtype
== VT_VARIABLE
)
5268 val
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
5269 ? quote_string (temp1
)
5270 : quote_escapes (temp1
);
5275 #if defined (ARRAY_VARS)
5277 temp1
= array_transform (xc
, v
, varname
, quoted
);
5278 if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
5280 val
= quote_escapes (temp1
);
5287 temp1
= parameter_list_transform (xc
, varname
[0], quoted
);
5288 if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
5290 val
= quote_escapes (temp1
);
5300 /*******************************************
5302 * Functions to expand WORD_DESCs *
5304 *******************************************/
5306 /* Expand WORD, performing word splitting on the result. This does
5307 parameter expansion, command substitution, arithmetic expansion,
5308 word splitting, and quote removal. */
5311 expand_word (word
, quoted
)
5315 WORD_LIST
*result
, *tresult
;
5317 tresult
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
5318 result
= word_list_split (tresult
);
5319 dispose_words (tresult
);
5320 return (result
? dequote_list (result
) : result
);
5323 /* Expand WORD, but do not perform word splitting on the result. This
5324 does parameter expansion, command substitution, arithmetic expansion,
5325 and quote removal. */
5327 expand_word_unsplit (word
, quoted
)
5333 expand_no_split_dollar_star
= 1;
5334 #if defined (HANDLE_MULTIBYTE)
5335 if (ifs_firstc
[0] == 0)
5337 if (ifs_firstc
== 0)
5339 word
->flags
|= W_NOSPLIT
;
5340 word
->flags
|= W_NOSPLIT2
;
5341 result
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
5342 expand_no_split_dollar_star
= 0;
5344 return (result
? dequote_list (result
) : result
);
5347 /* Perform shell expansions on WORD, but do not perform word splitting or
5348 quote removal on the result. Virtually identical to expand_word_unsplit;
5349 could be combined if implementations don't diverge. */
5351 expand_word_leave_quoted (word
, quoted
)
5357 expand_no_split_dollar_star
= 1;
5358 #if defined (HANDLE_MULTIBYTE)
5359 if (ifs_firstc
[0] == 0)
5361 if (ifs_firstc
== 0)
5363 word
->flags
|= W_NOSPLIT
;
5364 word
->flags
|= W_NOSPLIT2
;
5365 result
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
5366 expand_no_split_dollar_star
= 0;
5371 #if defined (PROCESS_SUBSTITUTION)
5373 /*****************************************************************/
5375 /* Hacking Process Substitution */
5377 /*****************************************************************/
5379 #if !defined (HAVE_DEV_FD)
5380 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
5381 of FIFOs the shell has open. unlink_fifo_list will walk the list and
5382 unlink all of them. add_fifo_list adds the name of an open FIFO to the
5383 list. NFIFO is a count of the number of FIFOs in the list. */
5384 #define FIFO_INCR 20
5391 static struct temp_fifo
*fifo_list
= (struct temp_fifo
*)NULL
;
5393 static int fifo_list_size
;
5401 copy_fifo_list (sizep
)
5406 return (char *)NULL
;
5410 add_fifo_list (pathname
)
5413 if (nfifo
>= fifo_list_size
- 1)
5415 fifo_list_size
+= FIFO_INCR
;
5416 fifo_list
= (struct temp_fifo
*)xrealloc (fifo_list
,
5417 fifo_list_size
* sizeof (struct temp_fifo
));
5420 fifo_list
[nfifo
].file
= savestring (pathname
);
5428 if ((fifo_list
[i
].proc
== -1) || (kill(fifo_list
[i
].proc
, 0) == -1))
5430 unlink (fifo_list
[i
].file
);
5431 free (fifo_list
[i
].file
);
5432 fifo_list
[i
].file
= (char *)NULL
;
5433 fifo_list
[i
].proc
= -1;
5445 for (i
= saved
= 0; i
< nfifo
; i
++)
5447 if ((fifo_list
[i
].proc
== -1) || (kill(fifo_list
[i
].proc
, 0) == -1))
5449 unlink (fifo_list
[i
].file
);
5450 free (fifo_list
[i
].file
);
5451 fifo_list
[i
].file
= (char *)NULL
;
5452 fifo_list
[i
].proc
= -1;
5458 /* If we didn't remove some of the FIFOs, compact the list. */
5461 for (i
= j
= 0; i
< nfifo
; i
++)
5462 if (fifo_list
[i
].file
)
5464 fifo_list
[j
].file
= fifo_list
[i
].file
;
5465 fifo_list
[j
].proc
= fifo_list
[i
].proc
;
5474 /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list
5475 from some point in the past, and close all open FIFOs in fifo_list
5476 that are not marked as active in LIST. If LIST is NULL, close
5477 everything in fifo_list. LSIZE is the number of elements in LIST, in
5478 case it's larger than fifo_list_size (size of fifo_list). */
5480 close_new_fifos (list
, lsize
)
5488 unlink_fifo_list ();
5492 for (i
= 0; i
< lsize
; i
++)
5493 if (list
[i
] == 0 && i
< fifo_list_size
&& fifo_list
[i
].proc
!= -1)
5496 for (i
= lsize
; i
< fifo_list_size
; i
++)
5517 tname
= sh_mktmpname ("sh-np", MT_USERANDOM
|MT_USETMPDIR
);
5518 if (mkfifo (tname
, 0600) < 0)
5521 return ((char *)NULL
);
5524 add_fifo_list (tname
);
5528 #else /* HAVE_DEV_FD */
5530 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
5531 has open to children. NFDS is a count of the number of bits currently
5532 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
5534 static char *dev_fd_list
= (char *)NULL
;
5536 static int totfds
; /* The highest possible number of open files. */
5557 for (i
= 0; nfds
&& i
< totfds
; i
++)
5564 copy_fifo_list (sizep
)
5569 if (nfds
== 0 || totfds
== 0)
5573 return (char *)NULL
;
5578 ret
= (char *)xmalloc (totfds
);
5579 return (memcpy (ret
, dev_fd_list
, totfds
));
5586 if (dev_fd_list
== 0 || fd
>= totfds
)
5591 totfds
= getdtablesize ();
5592 if (totfds
< 0 || totfds
> 256)
5597 dev_fd_list
= (char *)xrealloc (dev_fd_list
, totfds
);
5598 memset (dev_fd_list
+ ofds
, '\0', totfds
- ofds
);
5601 dev_fd_list
[fd
] = 1;
5608 return 0; /* used for cleanup; not needed with /dev/fd */
5621 if (dev_fd_list
[fd
])
5624 dev_fd_list
[fd
] = 0;
5637 for (i
= 0; nfds
&& i
< totfds
; i
++)
5643 /* Take LIST, which is a snapshot copy of dev_fd_list from some point in
5644 the past, and close all open fds in dev_fd_list that are not marked
5645 as open in LIST. If LIST is NULL, close everything in dev_fd_list.
5646 LSIZE is the number of elements in LIST, in case it's larger than
5647 totfds (size of dev_fd_list). */
5649 close_new_fifos (list
, lsize
)
5657 unlink_fifo_list ();
5661 for (i
= 0; i
< lsize
; i
++)
5662 if (list
[i
] == 0 && i
< totfds
&& dev_fd_list
[i
])
5665 for (i
= lsize
; i
< totfds
; i
++)
5669 #if defined (NOTDEF)
5670 print_dev_fd_list ()
5674 fprintf (stderr
, "pid %ld: dev_fd_list:", (long)getpid ());
5677 for (i
= 0; i
< totfds
; i
++)
5680 fprintf (stderr
, " %d", i
);
5682 fprintf (stderr
, "\n");
5687 make_dev_fd_filename (fd
)
5690 char *ret
, intbuf
[INT_STRLEN_BOUND (int) + 1], *p
;
5692 ret
= (char *)xmalloc (sizeof (DEV_FD_PREFIX
) + 8);
5694 strcpy (ret
, DEV_FD_PREFIX
);
5695 p
= inttostr (fd
, intbuf
, sizeof (intbuf
));
5696 strcpy (ret
+ sizeof (DEV_FD_PREFIX
) - 1, p
);
5702 #endif /* HAVE_DEV_FD */
5704 /* Return a filename that will open a connection to the process defined by
5705 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
5706 a filename in /dev/fd corresponding to a descriptor that is one of the
5707 ends of the pipe. If not defined, we use named pipes on systems that have
5708 them. Systems without /dev/fd and named pipes are out of luck.
5710 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
5711 use the read end of the pipe and dup that file descriptor to fd 0 in
5712 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
5713 writing or use the write end of the pipe in the child, and dup that
5714 file descriptor to fd 1 in the child. The parent does the opposite. */
5717 process_substitute (string
, open_for_read_in_child
)
5719 int open_for_read_in_child
;
5724 #if defined (HAVE_DEV_FD)
5725 int parent_pipe_fd
, child_pipe_fd
;
5727 #endif /* HAVE_DEV_FD */
5728 #if defined (JOB_CONTROL)
5729 pid_t old_pipeline_pgrp
;
5732 if (!string
|| !*string
|| wordexp_only
)
5733 return ((char *)NULL
);
5735 #if !defined (HAVE_DEV_FD)
5736 pathname
= make_named_pipe ();
5737 #else /* HAVE_DEV_FD */
5738 if (pipe (fildes
) < 0)
5740 sys_error ("%s", _("cannot make pipe for process substitution"));
5741 return ((char *)NULL
);
5743 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
5744 the pipe in the parent, otherwise the read end. */
5745 parent_pipe_fd
= fildes
[open_for_read_in_child
];
5746 child_pipe_fd
= fildes
[1 - open_for_read_in_child
];
5747 /* Move the parent end of the pipe to some high file descriptor, to
5748 avoid clashes with FDs used by the script. */
5749 parent_pipe_fd
= move_to_high_fd (parent_pipe_fd
, 1, 64);
5751 pathname
= make_dev_fd_filename (parent_pipe_fd
);
5752 #endif /* HAVE_DEV_FD */
5756 sys_error ("%s", _("cannot make pipe for process substitution"));
5757 return ((char *)NULL
);
5760 old_pid
= last_made_pid
;
5762 #if defined (JOB_CONTROL)
5763 old_pipeline_pgrp
= pipeline_pgrp
;
5764 if (pipeline_pgrp
== 0 || (subshell_environment
& (SUBSHELL_PIPE
|SUBSHELL_FORK
|SUBSHELL_ASYNC
)) == 0)
5765 pipeline_pgrp
= shell_pgrp
;
5767 #endif /* JOB_CONTROL */
5769 pid
= make_child ((char *)NULL
, 1);
5772 reset_terminating_signals (); /* XXX */
5773 free_pushed_string_input ();
5774 /* Cancel traps, in trap.c. */
5775 restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */
5776 QUIT
; /* catch any interrupts we got post-fork */
5777 setup_async_signals ();
5778 subshell_environment
|= SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
;
5780 /* if we're expanding a redirection, we shouldn't have access to the
5781 temporary environment, but commands in the subshell should have
5782 access to their own temporary environment. */
5783 if (expanding_redir
)
5784 flush_temporary_env ();
5787 #if defined (JOB_CONTROL)
5788 set_sigchld_handler ();
5789 stop_making_children ();
5790 /* XXX - should we only do this in the parent? (as in command subst) */
5791 pipeline_pgrp
= old_pipeline_pgrp
;
5793 stop_making_children ();
5794 #endif /* JOB_CONTROL */
5798 sys_error ("%s", _("cannot make child for process substitution"));
5800 #if defined (HAVE_DEV_FD)
5801 close (parent_pipe_fd
);
5802 close (child_pipe_fd
);
5803 #endif /* HAVE_DEV_FD */
5804 return ((char *)NULL
);
5809 #if defined (JOB_CONTROL)
5810 if (last_procsub_child
)
5812 discard_pipeline (last_procsub_child
);
5813 last_procsub_child
= (PROCESS
*)NULL
;
5815 last_procsub_child
= restore_pipeline (0);
5818 #if !defined (HAVE_DEV_FD)
5819 fifo_list
[nfifo
-1].proc
= pid
;
5822 last_made_pid
= old_pid
;
5824 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
5826 #endif /* JOB_CONTROL && PGRP_PIPE */
5828 #if defined (HAVE_DEV_FD)
5829 close (child_pipe_fd
);
5830 #endif /* HAVE_DEV_FD */
5835 set_sigint_handler ();
5837 #if defined (JOB_CONTROL)
5838 set_job_control (0);
5839 #endif /* JOB_CONTROL */
5841 #if !defined (HAVE_DEV_FD)
5842 /* Open the named pipe in the child. */
5843 fd
= open (pathname
, open_for_read_in_child
? O_RDONLY
: O_WRONLY
);
5846 /* Two separate strings for ease of translation. */
5847 if (open_for_read_in_child
)
5848 sys_error (_("cannot open named pipe %s for reading"), pathname
);
5850 sys_error (_("cannot open named pipe %s for writing"), pathname
);
5854 if (open_for_read_in_child
)
5856 if (sh_unset_nodelay_mode (fd
) < 0)
5858 sys_error (_("cannot reset nodelay mode for fd %d"), fd
);
5862 #else /* HAVE_DEV_FD */
5864 #endif /* HAVE_DEV_FD */
5866 /* Discard buffered stdio output before replacing the underlying file
5868 if (open_for_read_in_child
== 0)
5871 if (dup2 (fd
, open_for_read_in_child
? 0 : 1) < 0)
5873 sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname
,
5874 open_for_read_in_child
? 0 : 1);
5878 if (fd
!= (open_for_read_in_child
? 0 : 1))
5881 /* Need to close any files that this process has open to pipes inherited
5883 if (current_fds_to_close
)
5885 close_fd_bitmap (current_fds_to_close
);
5886 current_fds_to_close
= (struct fd_bitmap
*)NULL
;
5889 #if defined (HAVE_DEV_FD)
5890 /* Make sure we close the parent's end of the pipe and clear the slot
5891 in the fd list so it is not closed later, if reallocated by, for
5892 instance, pipe(2). */
5893 close (parent_pipe_fd
);
5894 dev_fd_list
[parent_pipe_fd
] = 0;
5895 #endif /* HAVE_DEV_FD */
5897 /* subshells shouldn't have this flag, which controls using the temporary
5898 environment for variable lookups. We have already flushed the temporary
5899 environment above in the case we're expanding a redirection, so processes
5900 executed by this command need to be able to set it independently of their
5902 expanding_redir
= 0;
5905 result
= parse_and_execute (string
, "process substitution", (SEVAL_NONINT
|SEVAL_NOHIST
));
5908 #if !defined (HAVE_DEV_FD)
5909 /* Make sure we close the named pipe in the child before we exit. */
5910 close (open_for_read_in_child
? 0 : 1);
5911 #endif /* !HAVE_DEV_FD */
5913 last_command_exit_value
= result
;
5914 result
= run_exit_trap ();
5918 #endif /* PROCESS_SUBSTITUTION */
5920 /***********************************/
5922 /* Command Substitution */
5924 /***********************************/
5927 read_comsub (fd
, quoted
, rflag
)
5931 char *istring
, buf
[128], *bufp
, *s
;
5932 int istring_index
, istring_size
, c
, tflag
, skip_ctlesc
, skip_ctlnul
;
5935 istring
= (char *)NULL
;
5936 istring_index
= istring_size
= bufn
= tflag
= 0;
5938 for (skip_ctlesc
= skip_ctlnul
= 0, s
= ifs_value
; s
&& *s
; s
++)
5939 skip_ctlesc
|= *s
== CTLESC
, skip_ctlnul
|= *s
== CTLNUL
;
5941 /* Read the output of the command through the pipe. This may need to be
5942 changed to understand multibyte characters in the future. */
5949 bufn
= zread (fd
, buf
, sizeof (buf
));
5959 internal_warning ("%s", _("command substitution: ignored null byte in input"));
5964 /* Add the character to ISTRING, possibly after resizing it. */
5965 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
, DEFAULT_ARRAY_SIZE
);
5967 /* This is essentially quote_string inline */
5968 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) /* || c == CTLESC || c == CTLNUL */)
5969 istring
[istring_index
++] = CTLESC
;
5970 /* Escape CTLESC and CTLNUL in the output to protect those characters
5971 from the rest of the word expansions (word splitting and globbing.)
5972 This is essentially quote_escapes inline. */
5973 else if (skip_ctlesc
== 0 && c
== CTLESC
)
5975 tflag
|= W_HASCTLESC
;
5976 istring
[istring_index
++] = CTLESC
;
5978 else if ((skip_ctlnul
== 0 && c
== CTLNUL
) || (c
== ' ' && (ifs_value
&& *ifs_value
== 0)))
5979 istring
[istring_index
++] = CTLESC
;
5981 istring
[istring_index
++] = c
;
5984 #if defined (__CYGWIN__)
5985 if (c
== '\n' && istring_index
> 1 && istring
[istring_index
- 2] == '\r')
5988 istring
[istring_index
- 1] = '\n';
5995 istring
[istring_index
] = '\0';
5997 /* If we read no output, just return now and save ourselves some
5999 if (istring_index
== 0)
6004 return (char *)NULL
;
6007 /* Strip trailing newlines from the output of the command. */
6008 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
6010 while (istring_index
> 0)
6012 if (istring
[istring_index
- 1] == '\n')
6016 /* If the newline was quoted, remove the quoting char. */
6017 if (istring
[istring_index
- 1] == CTLESC
)
6023 istring
[istring_index
] = '\0';
6026 strip_trailing (istring
, istring_index
- 1, 1);
6033 /* Perform command substitution on STRING. This returns a WORD_DESC * with the
6034 contained string possibly quoted. */
6036 command_substitute (string
, quoted
)
6040 pid_t pid
, old_pid
, old_pipeline_pgrp
, old_async_pid
;
6042 int result
, fildes
[2], function_value
, pflags
, rc
, tflag
;
6045 istring
= (char *)NULL
;
6047 /* Don't fork () if there is no need to. In the case of no command to
6048 run, just return NULL. */
6050 for (s
= string
; s
&& *s
&& (shellblank (*s
) || *s
== '\n'); s
++)
6052 if (s
== 0 || *s
== 0)
6053 return ((WORD_DESC
*)NULL
);
6055 if (!string
|| !*string
|| (string
[0] == '\n' && !string
[1]))
6056 return ((WORD_DESC
*)NULL
);
6059 if (wordexp_only
&& read_but_dont_execute
)
6061 last_command_exit_value
= EX_WEXPCOMSUB
;
6062 jump_to_top_level (EXITPROG
);
6065 /* We're making the assumption here that the command substitution will
6066 eventually run a command from the file system. Since we'll run
6067 maybe_make_export_env in this subshell before executing that command,
6068 the parent shell and any other shells it starts will have to remake
6069 the environment. If we make it before we fork, other shells won't
6070 have to. Don't bother if we have any temporary variable assignments,
6071 though, because the export environment will be remade after this
6072 command completes anyway, but do it if all the words to be expanded
6073 are variable assignments. */
6074 if (subst_assign_varlist
== 0 || garglist
== 0)
6075 maybe_make_export_env (); /* XXX */
6077 /* Flags to pass to parse_and_execute() */
6078 pflags
= (interactive
&& sourcelevel
== 0) ? SEVAL_RESETLINE
: 0;
6080 /* Pipe the output of executing STRING into the current shell. */
6081 if (pipe (fildes
) < 0)
6083 sys_error ("%s", _("cannot make pipe for command substitution"));
6087 old_pid
= last_made_pid
;
6088 #if defined (JOB_CONTROL)
6089 old_pipeline_pgrp
= pipeline_pgrp
;
6090 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
6091 if ((subshell_environment
& SUBSHELL_PIPE
) == 0)
6092 pipeline_pgrp
= shell_pgrp
;
6093 cleanup_the_pipeline ();
6094 #endif /* JOB_CONTROL */
6096 old_async_pid
= last_asynchronous_pid
;
6097 pid
= make_child ((char *)NULL
, subshell_environment
&SUBSHELL_ASYNC
);
6098 last_asynchronous_pid
= old_async_pid
;
6102 /* Reset the signal handlers in the child, but don't free the
6103 trap strings. Set a flag noting that we have to free the
6104 trap strings if we run trap to change a signal disposition. */
6105 reset_signal_handlers ();
6108 kill (getpid (), SIGINT
);
6109 CLRINTERRUPT
; /* if we're ignoring SIGINT somehow */
6111 QUIT
; /* catch any interrupts we got post-fork */
6112 subshell_environment
|= SUBSHELL_RESETTRAP
;
6115 #if defined (JOB_CONTROL)
6116 /* XXX DO THIS ONLY IN PARENT ? XXX */
6117 set_sigchld_handler ();
6118 stop_making_children ();
6120 pipeline_pgrp
= old_pipeline_pgrp
;
6122 stop_making_children ();
6123 #endif /* JOB_CONTROL */
6127 sys_error (_("cannot make child for command substitution"));
6130 last_made_pid
= old_pid
;
6135 return ((WORD_DESC
*)NULL
);
6140 /* The currently executing shell is not interactive. */
6143 set_sigint_handler (); /* XXX */
6145 free_pushed_string_input ();
6147 /* Discard buffered stdio output before replacing the underlying file
6151 if (dup2 (fildes
[1], 1) < 0)
6153 sys_error ("%s", _("command_substitute: cannot duplicate pipe as fd 1"));
6154 exit (EXECUTION_FAILURE
);
6157 /* If standard output is closed in the parent shell
6158 (such as after `exec >&-'), file descriptor 1 will be
6159 the lowest available file descriptor, and end up in
6160 fildes[0]. This can happen for stdin and stderr as well,
6161 but stdout is more important -- it will cause no output
6162 to be generated from this command. */
6163 if ((fildes
[1] != fileno (stdin
)) &&
6164 (fildes
[1] != fileno (stdout
)) &&
6165 (fildes
[1] != fileno (stderr
)))
6168 if ((fildes
[0] != fileno (stdin
)) &&
6169 (fildes
[0] != fileno (stdout
)) &&
6170 (fildes
[0] != fileno (stderr
)))
6174 /* Let stdio know the fd may have changed from text to binary mode, and
6175 make sure to preserve stdout line buffering. */
6176 freopen (NULL
, "w", stdout
);
6177 sh_setlinebuf (stdout
);
6178 #endif /* __CYGWIN__ */
6180 /* This is a subshell environment. */
6181 subshell_environment
|= SUBSHELL_COMSUB
;
6183 /* Many shells do not appear to inherit the -v option for command
6185 change_flag ('v', FLAG_OFF
);
6187 /* When inherit_errexit option is not enabled, command substitution does
6188 not inherit the -e flag. It is enabled when Posix mode is enabled */
6189 if (inherit_errexit
== 0)
6191 builtin_ignoring_errexit
= 0;
6192 change_flag ('e', FLAG_OFF
);
6196 /* If we are expanding a redirection, we can dispose of any temporary
6197 environment we received, since redirections are not supposed to have
6198 access to the temporary environment. We will have to see whether this
6199 affects temporary environments supplied to `eval', but the temporary
6200 environment gets copied to builtin_env at some point. */
6201 if (expanding_redir
)
6203 flush_temporary_env ();
6204 expanding_redir
= 0;
6207 remove_quoted_escapes (string
);
6209 startup_state
= 2; /* see if we can avoid a fork */
6210 /* Give command substitution a place to jump back to on failure,
6211 so we don't go back up to main (). */
6212 result
= setjmp_nosigs (top_level
);
6214 /* If we're running a command substitution inside a shell function,
6215 trap `return' so we don't return from the function in the subshell
6216 and go off to never-never land. */
6217 if (result
== 0 && return_catch_flag
)
6218 function_value
= setjmp_nosigs (return_catch
);
6222 if (result
== ERREXIT
)
6223 rc
= last_command_exit_value
;
6224 else if (result
== EXITPROG
)
6225 rc
= last_command_exit_value
;
6227 rc
= EXECUTION_FAILURE
;
6228 else if (function_value
)
6229 rc
= return_catch_value
;
6233 rc
= parse_and_execute (string
, "command substitution", pflags
|SEVAL_NOHIST
);
6237 last_command_exit_value
= rc
;
6238 rc
= run_exit_trap ();
6239 #if defined (PROCESS_SUBSTITUTION)
6240 unlink_fifo_list ();
6246 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
6248 #endif /* JOB_CONTROL && PGRP_PIPE */
6253 istring
= read_comsub (fildes
[0], quoted
, &tflag
);
6257 current_command_subst_pid
= pid
;
6258 last_command_exit_value
= wait_for (pid
);
6259 last_command_subst_pid
= pid
;
6260 last_made_pid
= old_pid
;
6262 #if defined (JOB_CONTROL)
6263 /* If last_command_exit_value > 128, then the substituted command
6264 was terminated by a signal. If that signal was SIGINT, then send
6265 SIGINT to ourselves. This will break out of loops, for instance. */
6266 if (last_command_exit_value
== (128 + SIGINT
) && last_command_exit_signal
== SIGINT
)
6267 kill (getpid (), SIGINT
);
6269 /* wait_for gives the terminal back to shell_pgrp. If some other
6270 process group should have it, give it away to that group here.
6271 pipeline_pgrp is non-zero only while we are constructing a
6272 pipeline, so what we are concerned about is whether or not that
6273 pipeline was started in the background. A pipeline started in
6274 the background should never get the tty back here. We duplicate
6275 the conditions that wait_for tests to make sure we only give
6276 the terminal back to pipeline_pgrp under the conditions that wait_for
6277 gave it to shell_pgrp. If wait_for doesn't mess with the terminal
6278 pgrp, we should not either. */
6279 if (interactive
&& pipeline_pgrp
!= (pid_t
)0 && running_in_background
== 0 &&
6280 (subshell_environment
& (SUBSHELL_ASYNC
|SUBSHELL_PIPE
)) == 0)
6281 give_terminal_to (pipeline_pgrp
, 0);
6282 #endif /* JOB_CONTROL */
6284 ret
= alloc_word_desc ();
6285 ret
->word
= istring
;
6292 /********************************************************
6294 * Utility functions for parameter expansion *
6296 ********************************************************/
6298 #if defined (ARRAY_VARS)
6301 array_length_reference (s
)
6312 var
= array_variable_part (s
, &t
, &len
);
6314 /* If unbound variables should generate an error, report one and return
6316 if ((var
== 0 || invisible_p (var
) || (assoc_p (var
) == 0 && array_p (var
) == 0)) && unbound_vars_is_error
)
6320 last_command_exit_value
= EXECUTION_FAILURE
;
6325 else if (var
== 0 || invisible_p (var
))
6328 /* We support a couple of expansions for variables that are not arrays.
6329 We'll return the length of the value for v[0], and 1 for v[@] or
6330 v[*]. Return 0 for everything else. */
6332 array
= array_p (var
) ? array_cell (var
) : (ARRAY
*)NULL
;
6333 h
= assoc_p (var
) ? assoc_cell (var
) : (HASH_TABLE
*)NULL
;
6335 if (ALL_ELEMENT_SUB (t
[0]) && t
[1] == ']')
6338 return (h
? assoc_num_elements (h
) : 0);
6339 else if (array_p (var
))
6340 return (array
? array_num_elements (array
) : 0);
6342 return (var_isset (var
) ? 1 : 0);
6348 akey
= expand_assignment_string_to_string (t
, 0); /* [ */
6350 if (akey
== 0 || *akey
== 0)
6352 err_badarraysub (t
);
6356 t
= assoc_reference (assoc_cell (var
), akey
);
6361 ind
= array_expand_index (var
, t
, len
);
6362 /* negative subscripts to indexed arrays count back from end */
6363 if (var
&& array_p (var
) && ind
< 0)
6364 ind
= array_max_index (array_cell (var
)) + 1 + ind
;
6367 err_badarraysub (t
);
6371 t
= array_reference (array
, ind
);
6373 t
= (ind
== 0) ? value_cell (var
) : (char *)NULL
;
6376 len
= MB_STRLEN (t
);
6379 #endif /* ARRAY_VARS */
6382 valid_brace_expansion_word (name
, var_is_special
)
6386 if (DIGIT (*name
) && all_digits (name
))
6388 else if (var_is_special
)
6390 #if defined (ARRAY_VARS)
6391 else if (valid_array_reference (name
, 0))
6393 #endif /* ARRAY_VARS */
6394 else if (legal_identifier (name
))
6401 chk_atstar (name
, quoted
, quoted_dollar_atp
, contains_dollar_at
)
6404 int *quoted_dollar_atp
, *contains_dollar_at
;
6410 if (quoted_dollar_atp
)
6411 *quoted_dollar_atp
= 0;
6412 if (contains_dollar_at
)
6413 *contains_dollar_at
= 0;
6417 /* check for $@ and $* */
6418 if (name
[0] == '@' && name
[1] == 0)
6420 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
6421 *quoted_dollar_atp
= 1;
6422 if (contains_dollar_at
)
6423 *contains_dollar_at
= 1;
6426 else if (name
[0] == '*' && name
[1] == '\0' && quoted
== 0)
6428 if (contains_dollar_at
)
6429 *contains_dollar_at
= 1;
6433 /* Now check for ${array[@]} and ${array[*]} */
6434 #if defined (ARRAY_VARS)
6435 else if (valid_array_reference (name
, 0))
6437 temp1
= mbschr (name
, '[');
6438 if (temp1
&& temp1
[1] == '@' && temp1
[2] == ']')
6440 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
6441 *quoted_dollar_atp
= 1;
6442 if (contains_dollar_at
)
6443 *contains_dollar_at
= 1;
6446 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
6447 which should result in separate words even when IFS is unset. */
6448 if (temp1
&& temp1
[1] == '*' && temp1
[2] == ']' && quoted
== 0)
6450 if (contains_dollar_at
)
6451 *contains_dollar_at
= 1;
6459 /* Parameter expand NAME, and return a new string which is the expansion,
6460 or NULL if there was no expansion.
6461 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
6462 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
6463 NAME was found inside of a double-quoted expression. */
6465 parameter_brace_expand_word (name
, var_is_special
, quoted
, pflags
, indp
)
6467 int var_is_special
, quoted
, pflags
;
6484 /* Handle multiple digit arguments, as in ${11}. */
6485 if (legal_number (name
, &arg_index
))
6487 tt
= get_dollar_var_value (arg_index
);
6489 temp
= (*tt
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6491 : quote_escapes (tt
);
6493 temp
= (char *)NULL
;
6496 else if (var_is_special
) /* ${@} */
6499 tt
= (char *)xmalloc (2 + strlen (name
));
6500 tt
[sindex
= 0] = '$';
6501 strcpy (tt
+ 1, name
);
6503 ret
= param_expand (tt
, &sindex
, quoted
, (int *)NULL
, (int *)NULL
,
6504 (int *)NULL
, (int *)NULL
, pflags
);
6507 #if defined (ARRAY_VARS)
6508 else if (valid_array_reference (name
, 0))
6511 if (pflags
& PF_ASSIGNRHS
)
6513 var
= array_variable_part (name
, &tt
, (int *)0);
6514 if (ALL_ELEMENT_SUB (tt
[0]) && tt
[1] == ']')
6516 /* Only treat as double quoted if array variable */
6517 if (var
&& (array_p (var
) || assoc_p (var
)))
6518 /* XXX - bash-4.4/bash-5.0 pass AV_ASSIGNRHS */
6519 temp
= array_value (name
, quoted
|Q_DOUBLE_QUOTES
, AV_ASSIGNRHS
, &atype
, &ind
);
6521 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6524 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6527 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
6528 if (atype
== 0 && temp
)
6530 temp
= (*temp
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6531 ? quote_string (temp
)
6532 : quote_escapes (temp
);
6533 rflags
|= W_ARRAYIND
;
6537 else if (atype
== 1 && temp
&& QUOTED_NULL (temp
) && (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6538 rflags
|= W_HASQUOTEDNULL
;
6541 else if (var
= find_variable (name
))
6543 if (var_isset (var
) && invisible_p (var
) == 0)
6545 #if defined (ARRAY_VARS)
6547 temp
= assoc_reference (assoc_cell (var
), "0");
6548 else if (array_p (var
))
6549 temp
= array_reference (array_cell (var
), 0);
6551 temp
= value_cell (var
);
6553 temp
= value_cell (var
);
6557 temp
= (*temp
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
6558 ? quote_string (temp
)
6559 : quote_escapes (temp
);
6562 temp
= (char *)NULL
;
6564 else if (var
= find_variable_last_nameref (name
, 0))
6566 temp
= nameref_cell (var
);
6567 #if defined (ARRAY_VARS)
6568 /* Handle expanding nameref whose value is x[n] */
6569 if (temp
&& *temp
&& valid_array_reference (temp
, 0))
6572 goto expand_arrayref
;
6576 /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */
6577 if (temp
&& *temp
&& legal_identifier (temp
) == 0)
6579 last_command_exit_value
= EXECUTION_FAILURE
;
6580 report_error (_("%s: invalid variable name for name reference"), temp
);
6581 temp
= &expand_param_error
;
6584 temp
= (char *)NULL
;
6587 temp
= (char *)NULL
;
6591 ret
= alloc_word_desc ();
6593 ret
->flags
|= rflags
;
6599 parameter_brace_find_indir (name
, var_is_special
, quoted
, find_nameref
)
6601 int var_is_special
, quoted
, find_nameref
;
6607 if (find_nameref
&& var_is_special
== 0 && (v
= find_variable_last_nameref (name
, 0)) &&
6608 nameref_p (v
) && (t
= nameref_cell (v
)) && *t
)
6609 return (savestring (t
));
6611 /* If var_is_special == 0, and name is not an array reference, this does
6612 more expansion than necessary. It should really look up the variable's
6613 value and not try to expand it. */
6614 w
= parameter_brace_expand_word (name
, var_is_special
, quoted
, PF_IGNUNBOUND
, 0);
6616 /* Have to dequote here if necessary */
6619 temp
= (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
6620 ? dequote_string (t
)
6621 : dequote_escapes (t
);
6625 dispose_word_desc (w
);
6630 /* Expand an indirect reference to a variable: ${!NAME} expands to the
6631 value of the variable whose name is the value of NAME. */
6633 parameter_brace_expand_indir (name
, var_is_special
, quoted
, quoted_dollar_atp
, contains_dollar_at
)
6635 int var_is_special
, quoted
;
6636 int *quoted_dollar_atp
, *contains_dollar_at
;
6642 /* See if it's a nameref first, behave in ksh93-compatible fashion.
6643 There is at least one incompatibility: given ${!foo[0]} where foo=bar,
6644 bash performs an indirect lookup on foo[0] and expands the result;
6645 ksh93 expands bar[0]. We could do that here -- there are enough usable
6646 primitives to do that -- but do not at this point. */
6647 if (var_is_special
== 0 && (v
= find_variable_last_nameref (name
, 0)))
6649 if (nameref_p (v
) && (t
= nameref_cell (v
)) && *t
)
6651 w
= alloc_word_desc ();
6652 w
->word
= savestring (t
);
6658 t
= parameter_brace_find_indir (name
, var_is_special
, quoted
, 0);
6660 chk_atstar (t
, quoted
, quoted_dollar_atp
, contains_dollar_at
);
6662 return (WORD_DESC
*)NULL
;
6664 if (valid_brace_expansion_word (t
, SPECIAL_VAR (t
, 0)) == 0)
6666 report_error (_("%s: bad substitution"), t
);
6668 w
= alloc_word_desc ();
6669 w
->word
= &expand_param_error
;
6674 w
= parameter_brace_expand_word (t
, SPECIAL_VAR(t
, 0), quoted
, 0, 0);
6680 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
6681 depending on the value of C, the separating character. C can be one of
6682 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
6683 between double quotes. */
6685 parameter_brace_expand_rhs (name
, value
, c
, quoted
, pflags
, qdollaratp
, hasdollarat
)
6687 int c
, quoted
, pflags
, *qdollaratp
, *hasdollarat
;
6691 char *t
, *t1
, *temp
, *vname
;
6692 int l_hasdollat
, sindex
;
6694 /*itrace("parameter_brace_expand_rhs: %s:%s pflags = %d", name, value, pflags);*/
6695 /* If the entire expression is between double quotes, we want to treat
6696 the value as a double-quoted string, with the exception that we strip
6697 embedded unescaped double quotes (for sh backwards compatibility). */
6698 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *value
)
6701 temp
= string_extract_double_quoted (value
, &sindex
, SX_STRIPDQ
);
6706 w
= alloc_word_desc ();
6708 /* XXX was 0 not quoted */
6709 l
= *temp
? expand_string_for_rhs (temp
, quoted
, &l_hasdollat
, (int *)NULL
)
6712 *hasdollarat
= l_hasdollat
|| (l
&& l
->next
);
6717 /* If l->next is not null, we know that TEMP contained "$@", since that
6718 is the only expansion that creates more than one word. */
6719 if (qdollaratp
&& ((l_hasdollat
&& quoted
) || l
->next
))
6721 /*itrace("parameter_brace_expand_rhs: %s:%s: l != NULL, set *qdollaratp", name, value);*/
6725 /* The expansion of TEMP returned something. We need to treat things
6726 slightly differently if L_HASDOLLAT is non-zero. If we have "$@",
6727 the individual words have already been quoted. We need to turn them
6728 into a string with the words separated by the first character of
6729 $IFS without any additional quoting, so string_list_dollar_at won't
6730 do the right thing. If IFS is null, we want "$@" to split into
6731 separate arguments, not be concatenated, so we use string_list_internal
6732 and mark the word to be split on spaces later. We use
6733 string_list_dollar_star for "$@" otherwise. */
6734 if (l
->next
&& ifs_is_null
)
6736 temp
= string_list_internal (l
, " ");
6737 w
->flags
|= W_SPLITSPACE
;
6740 temp
= (l_hasdollat
|| l
->next
) ? string_list_dollar_star (l
) : string_list (l
);
6742 /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is
6743 a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the
6744 flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the
6745 expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6746 (which is more paranoia than anything else), we need to return the
6747 quoted null string and set the flags to indicate it. */
6748 if (l
->next
== 0 && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && QUOTED_NULL (temp
) && QUOTED_NULL (l
->word
->word
) && (l
->word
->flags
& W_HASQUOTEDNULL
))
6750 w
->flags
|= W_HASQUOTEDNULL
;
6751 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null, turning off qdollaratp", name, value);*/
6752 /* If we return a quoted null with L_HASDOLLARAT, we either have a
6753 construct like "${@-$@}" or "${@-${@-$@}}" with no positional
6754 parameters or a quoted expansion of "$@" with $1 == ''. In either
6755 case, we don't want to enable special handling of $@. */
6756 if (qdollaratp
&& l_hasdollat
)
6761 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && l_hasdollat
)
6763 /* Posix interp 221 changed the rules on this. The idea is that
6764 something like "$xxx$@" should expand the same as "${foo-$xxx$@}"
6765 when foo and xxx are unset. The problem is that it's not in any
6766 way backwards compatible and few other shells do it. We're eventually
6767 going to try and split the difference (heh) a little bit here. */
6768 /* l_hasdollat == 1 means we saw a quoted dollar at. */
6770 /* The brace expansion occurred between double quotes and there was
6771 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
6772 it does not expand to anything. In this case, we want to return
6773 a quoted empty string. Posix interp 888 */
6774 temp
= make_quoted_char ('\0');
6775 w
->flags
|= W_HASQUOTEDNULL
;
6776 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null", name, value);*/
6779 temp
= (char *)NULL
;
6781 if (c
== '-' || c
== '+')
6788 t
= temp
? savestring (temp
) : savestring ("");
6789 t1
= dequote_string (t
);
6795 (legal_variable_starter ((unsigned char)name
[1]) || DIGIT (name
[1]) || VALID_INDIR_PARAM (name
[1])))
6797 vname
= parameter_brace_find_indir (name
+ 1, SPECIAL_VAR (name
, 1), quoted
, 1);
6798 if (vname
== 0 || *vname
== 0)
6800 report_error (_("%s: invalid indirect expansion"), name
);
6803 return &expand_wdesc_error
;
6805 if (legal_identifier (vname
) == 0)
6807 report_error (_("%s: invalid variable name"), vname
);
6810 return &expand_wdesc_error
;
6814 #if defined (ARRAY_VARS)
6815 if (valid_array_reference (vname
, 0))
6816 assign_array_element (vname
, t1
, 0);
6818 #endif /* ARRAY_VARS */
6819 bind_variable (vname
, t1
, 0);
6821 stupidly_hack_special_variables (vname
);
6826 /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */
6833 /* Deal with the right hand side of a ${name:?value} expansion in the case
6834 that NAME is null or not set. If VALUE is non-null it is expanded and
6835 used as the error message to print, otherwise a standard message is
6838 parameter_brace_expand_error (name
, value
)
6844 last_command_exit_value
= EXECUTION_FAILURE
; /* ensure it's non-zero */
6845 if (value
&& *value
)
6847 l
= expand_string (value
, 0);
6848 temp
= string_list (l
);
6849 report_error ("%s: %s", name
, temp
? temp
: ""); /* XXX was value not "" */
6854 report_error (_("%s: parameter null or not set"), name
);
6856 /* Free the data we have allocated during this expansion, since we
6857 are about to longjmp out. */
6862 /* Return 1 if NAME is something for which parameter_brace_expand_length is
6865 valid_length_expression (name
)
6868 return (name
[1] == '\0' || /* ${#} */
6869 ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0') || /* special param */
6870 (DIGIT (name
[1]) && all_digits (name
+ 1)) || /* ${#11} */
6871 #if defined (ARRAY_VARS)
6872 valid_array_reference (name
+ 1, 0) || /* ${#a[7]} */
6874 legal_identifier (name
+ 1)); /* ${#PS1} */
6877 /* Handle the parameter brace expansion that requires us to return the
6878 length of a parameter. */
6880 parameter_brace_expand_length (name
)
6884 intmax_t number
, arg_index
;
6886 #if defined (ARRAY_VARS)
6890 if (name
[1] == '\0') /* ${#} */
6891 number
= number_of_args ();
6892 else if (DOLLAR_AT_STAR (name
[1]) && name
[2] == '\0') /* ${#@}, ${#*} */
6893 number
= number_of_args ();
6894 else if ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0')
6896 /* Take the lengths of some of the shell's special parameters. */
6900 t
= which_set_flags ();
6903 t
= itos (last_command_exit_value
);
6906 t
= itos (dollar_dollar_pid
);
6909 if (last_asynchronous_pid
== NO_PID
)
6910 t
= (char *)NULL
; /* XXX - error if set -u set? */
6912 t
= itos (last_asynchronous_pid
);
6915 t
= itos (number_of_args ());
6918 number
= STRLEN (t
);
6921 #if defined (ARRAY_VARS)
6922 else if (valid_array_reference (name
+ 1, 0))
6923 number
= array_length_reference (name
+ 1);
6924 #endif /* ARRAY_VARS */
6929 if (legal_number (name
+ 1, &arg_index
)) /* ${#1} */
6931 t
= get_dollar_var_value (arg_index
);
6932 if (t
== 0 && unbound_vars_is_error
)
6934 number
= MB_STRLEN (t
);
6937 #if defined (ARRAY_VARS)
6938 else if ((var
= find_variable (name
+ 1)) && (invisible_p (var
) == 0) && (array_p (var
) || assoc_p (var
)))
6941 t
= assoc_reference (assoc_cell (var
), "0");
6943 t
= array_reference (array_cell (var
), 0);
6944 if (t
== 0 && unbound_vars_is_error
)
6946 number
= MB_STRLEN (t
);
6951 newname
= savestring (name
);
6953 list
= expand_string (newname
, Q_DOUBLE_QUOTES
);
6954 t
= list
? string_list (list
) : (char *)NULL
;
6957 dispose_words (list
);
6959 number
= t
? MB_STRLEN (t
) : 0;
6967 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
6968 so we do some ad-hoc parsing of an arithmetic expression to find
6969 the first DELIM, instead of using strchr(3). Two rules:
6970 1. If the substring contains a `(', read until closing `)'.
6971 2. If the substring contains a `?', read past one `:' for each `?'.
6972 The SD_ARITHEXP flag to skip_to_delim takes care of doing this.
6976 skiparith (substr
, delim
)
6986 i
= skip_to_delim (substr
, 0, delims
, SD_ARITHEXP
);
6987 return (substr
+ i
);
6990 /* Verify and limit the start and end of the desired substring. If
6991 VTYPE == 0, a regular shell variable is being used; if it is 1,
6992 then the positional parameters are being used; if it is 2, then
6993 VALUE is really a pointer to an array variable that should be used.
6994 Return value is 1 if both values were OK, 0 if there was a problem
6995 with an invalid expression, or -1 if the values were out of range. */
6997 verify_substring_values (v
, value
, substr
, vtype
, e1p
, e2p
)
6999 char *value
, *substr
;
7001 intmax_t *e1p
, *e2p
;
7003 char *t
, *temp1
, *temp2
;
7006 #if defined (ARRAY_VARS)
7011 /* duplicate behavior of strchr(3) */
7012 t
= skiparith (substr
, ':');
7013 if (*t
&& *t
== ':')
7018 temp1
= expand_arith_string (substr
, Q_DOUBLE_QUOTES
);
7019 *e1p
= evalexp (temp1
, &expok
);
7024 len
= -1; /* paranoia */
7028 case VT_ARRAYMEMBER
:
7029 len
= MB_STRLEN (value
);
7032 len
= number_of_args () + 1;
7034 len
++; /* add one arg if counting from $0 */
7036 #if defined (ARRAY_VARS)
7038 /* For arrays, the first value deals with array indices. Negative
7039 offsets count from one past the array's maximum index. Associative
7040 arrays treat the number of elements as the maximum index. */
7044 len
= assoc_num_elements (h
) + (*e1p
< 0);
7049 len
= array_max_index (a
) + (*e1p
< 0); /* arrays index from 0 to n - 1 */
7055 if (len
== -1) /* paranoia */
7058 if (*e1p
< 0) /* negative offsets count from end */
7061 if (*e1p
> len
|| *e1p
< 0)
7064 #if defined (ARRAY_VARS)
7065 /* For arrays, the second offset deals with the number of elements. */
7066 if (vtype
== VT_ARRAYVAR
)
7067 len
= assoc_p (v
) ? assoc_num_elements (h
) : array_num_elements (a
);
7073 temp2
= savestring (t
);
7074 temp1
= expand_arith_string (temp2
, Q_DOUBLE_QUOTES
);
7077 *e2p
= evalexp (temp1
, &expok
);
7082 if ((vtype
== VT_ARRAYVAR
|| vtype
== VT_POSPARMS
) && *e2p
< 0)
7084 /* bash-4.3: allow positional parameter length < 0 to count backwards
7085 from end of positional parameters */
7086 if (vtype
== VT_ARRAYVAR
&& *e2p
< 0)
7089 internal_error (_("%s: substring expression < 0"), t
);
7092 #if defined (ARRAY_VARS)
7093 /* In order to deal with sparse arrays, push the intelligence about how
7094 to deal with the number of elements desired down to the array-
7095 specific functions. */
7096 if (vtype
!= VT_ARRAYVAR
)
7102 if (*e2p
< 0 || *e2p
< *e1p
)
7104 internal_error (_("%s: substring expression < 0"), t
);
7109 *e2p
+= *e1p
; /* want E2 chars starting at E1 */
7120 /* Return the type of variable specified by VARNAME (simple variable,
7121 positional param, or array variable). Also return the value specified
7122 by VARNAME (value of a variable or a reference to an array element).
7123 QUOTED is the standard description of quoting state, using Q_* defines.
7124 FLAGS is currently a set of flags to pass to array_value. If IND is
7125 non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is
7126 passed to array_value so the array index is not computed again.
7127 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
7128 characters in the value are quoted with CTLESC and takes appropriate
7129 steps. For convenience, *VALP is set to the dequoted VALUE. */
7131 get_var_and_type (varname
, value
, ind
, quoted
, flags
, varp
, valp
)
7132 char *varname
, *value
;
7138 int vtype
, want_indir
;
7144 want_indir
= *varname
== '!' &&
7145 (legal_variable_starter ((unsigned char)varname
[1]) || DIGIT (varname
[1])
7146 || VALID_INDIR_PARAM (varname
[1]));
7148 vname
= parameter_brace_find_indir (varname
+1, SPECIAL_VAR (varname
, 1), quoted
, 1);
7149 /* XXX - what if vname == 0 || *vname == 0 ? */
7155 vtype
= VT_VARIABLE
;
7156 *varp
= (SHELL_VAR
*)NULL
;
7157 *valp
= (char *)NULL
;
7161 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
7162 vtype
= STR_DOLLAR_AT_STAR (vname
);
7163 if (vtype
== VT_POSPARMS
&& vname
[0] == '*')
7164 vtype
|= VT_STARSUB
;
7165 *varp
= (SHELL_VAR
*)NULL
;
7167 #if defined (ARRAY_VARS)
7168 if (valid_array_reference (vname
, 0))
7170 v
= array_variable_part (vname
, &temp
, (int *)0);
7171 /* If we want to signal array_value to use an already-computed index,
7172 set LIND to that index */
7173 lind
= (ind
!= INTMAX_MIN
&& (flags
& AV_USEIND
)) ? ind
: 0;
7174 if (v
&& invisible_p (v
))
7176 vtype
= VT_ARRAYMEMBER
;
7177 *varp
= (SHELL_VAR
*)NULL
;
7178 *valp
= (char *)NULL
;
7180 if (v
&& (array_p (v
) || assoc_p (v
)))
7182 if (ALL_ELEMENT_SUB (temp
[0]) && temp
[1] == ']')
7184 /* Callers have to differentiate between indexed and associative */
7185 vtype
= VT_ARRAYVAR
;
7187 vtype
|= VT_STARSUB
;
7188 *valp
= array_p (v
) ? (char *)array_cell (v
) : (char *)assoc_cell (v
);
7192 vtype
= VT_ARRAYMEMBER
;
7193 *valp
= array_value (vname
, Q_DOUBLE_QUOTES
, flags
, (int *)NULL
, &lind
);
7197 else if (v
&& (ALL_ELEMENT_SUB (temp
[0]) && temp
[1] == ']'))
7199 vtype
= VT_VARIABLE
;
7201 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
7202 *valp
= dequote_string (value
);
7204 *valp
= dequote_escapes (value
);
7208 vtype
= VT_ARRAYMEMBER
;
7210 *valp
= array_value (vname
, Q_DOUBLE_QUOTES
, flags
, (int *)NULL
, &lind
);
7213 else if ((v
= find_variable (vname
)) && (invisible_p (v
) == 0) && (assoc_p (v
) || array_p (v
)))
7215 vtype
= VT_ARRAYMEMBER
;
7217 *valp
= assoc_p (v
) ? assoc_reference (assoc_cell (v
), "0") : array_reference (array_cell (v
), 0);
7222 if (value
&& vtype
== VT_VARIABLE
)
7224 *varp
= find_variable (vname
);
7225 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
7226 *valp
= dequote_string (value
);
7228 *valp
= dequote_escapes (value
);
7240 /******************************************************/
7242 /* Functions to extract substrings of variable values */
7244 /******************************************************/
7246 #if defined (HANDLE_MULTIBYTE)
7247 /* Character-oriented rather than strictly byte-oriented substrings. S and
7248 E, rather being strict indices into STRING, indicate character (possibly
7249 multibyte character) positions that require calculation.
7250 Used by the ${param:offset[:length]} expansion. */
7252 mb_substring (string
, s
, e
)
7262 /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
7263 slen
= (MB_CUR_MAX
> 1) ? STRLEN (string
) : 0;
7266 while (string
[start
] && i
--)
7267 ADVANCE_CHAR (string
, slen
, start
);
7270 while (string
[stop
] && i
--)
7271 ADVANCE_CHAR (string
, slen
, stop
);
7272 tt
= substring (string
, start
, stop
);
7277 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
7278 is `@', use the positional parameters; otherwise, use the value of
7279 VARNAME. If VARNAME is an array variable, use the array elements. */
7282 parameter_brace_substring (varname
, value
, ind
, substr
, quoted
, flags
)
7283 char *varname
, *value
;
7289 int vtype
, r
, starsub
;
7290 char *temp
, *val
, *tt
, *oname
;
7293 if (value
== 0 && ((varname
[0] != '@' && varname
[0] != '*') || varname
[1]))
7294 return ((char *)NULL
);
7296 oname
= this_command_name
;
7297 this_command_name
= varname
;
7299 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
7302 this_command_name
= oname
;
7303 return ((char *)NULL
);
7306 starsub
= vtype
& VT_STARSUB
;
7307 vtype
&= ~VT_STARSUB
;
7309 r
= verify_substring_values (v
, val
, substr
, vtype
, &e1
, &e2
);
7310 this_command_name
= oname
;
7313 if (vtype
== VT_VARIABLE
)
7315 return ((r
== 0) ? &expand_param_error
: (char *)NULL
);
7321 case VT_ARRAYMEMBER
:
7322 #if defined (HANDLE_MULTIBYTE)
7324 tt
= mb_substring (val
, e1
, e2
);
7327 tt
= substring (val
, e1
, e2
);
7329 if (vtype
== VT_VARIABLE
)
7331 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
7332 temp
= quote_string (tt
);
7334 temp
= tt
? quote_escapes (tt
) : (char *)NULL
;
7338 tt
= pos_params (varname
, e1
, e2
, quoted
);
7339 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) == 0)
7341 temp
= tt
? quote_escapes (tt
) : (char *)NULL
;
7347 #if defined (ARRAY_VARS)
7350 /* we convert to list and take first e2 elements starting at e1th
7351 element -- officially undefined for now */
7352 temp
= assoc_subrange (assoc_cell (v
), e1
, e2
, starsub
, quoted
);
7354 /* We want E2 to be the number of elements desired (arrays can be sparse,
7355 so verify_substring_values just returns the numbers specified and we
7356 rely on array_subrange to understand how to deal with them). */
7357 temp
= array_subrange (array_cell (v
), e1
, e2
, starsub
, quoted
);
7358 /* array_subrange now calls array_quote_escapes as appropriate, so the
7359 caller no longer needs to. */
7363 temp
= (char *)NULL
;
7369 /****************************************************************/
7371 /* Functions to perform pattern substitution on variable values */
7373 /****************************************************************/
7377 shouldexp_replacement (s
)
7382 for (p
= s
; p
&& *p
; p
++)
7394 pat_subst (string
, pat
, rep
, mflags
)
7395 char *string
, *pat
, *rep
;
7398 char *ret
, *s
, *e
, *str
, *rstr
, *mstr
;
7399 int rptr
, mtype
, rxpand
, mlen
;
7400 size_t rsize
, l
, replen
, rslen
;
7403 return (savestring (""));
7405 mtype
= mflags
& MATCH_TYPEMASK
;
7407 #if 0 /* bash-4.2 ? */
7408 rxpand
= (rep
&& *rep
) ? shouldexp_replacement (rep
) : 0;
7414 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
7415 * with REP and return the result.
7416 * 2. A null pattern with mtype == MATCH_END means to append REP to
7417 * STRING and return the result.
7418 * 3. A null STRING with a matching pattern means to append REP to
7419 * STRING and return the result.
7420 * These don't understand or process `&' in the replacement string.
7422 if ((pat
== 0 || *pat
== 0) && (mtype
== MATCH_BEG
|| mtype
== MATCH_END
))
7424 replen
= STRLEN (rep
);
7425 l
= STRLEN (string
);
7426 ret
= (char *)xmalloc (replen
+ l
+ 2);
7428 strcpy (ret
, string
);
7429 else if (mtype
== MATCH_BEG
)
7432 strcpy (ret
+ replen
, string
);
7436 strcpy (ret
, string
);
7437 strcpy (ret
+ l
, rep
);
7441 else if (*string
== 0 && (match_pattern (string
, pat
, mtype
, &s
, &e
) != 0))
7443 replen
= STRLEN (rep
);
7444 ret
= (char *)xmalloc (replen
+ 1);
7452 ret
= (char *)xmalloc (rsize
= 64);
7455 for (replen
= STRLEN (rep
), rptr
= 0, str
= string
; *str
;)
7457 if (match_pattern (str
, pat
, mtype
, &s
, &e
) == 0)
7465 mstr
= xmalloc (mlen
+ 1);
7466 for (x
= 0; x
< mlen
; x
++)
7469 rstr
= strcreplace (rep
, '&', mstr
, 0);
7471 rslen
= strlen (rstr
);
7479 RESIZE_MALLOCED_BUFFER (ret
, rptr
, (l
+ rslen
), rsize
, 64);
7481 /* OK, now copy the leading unmatched portion of the string (from
7482 str to s) to ret starting at rptr (the current offset). Then copy
7483 the replacement string at ret + rptr + (s - str). Increment
7484 rptr (if necessary) and str and go on. */
7487 strncpy (ret
+ rptr
, str
, l
);
7492 strncpy (ret
+ rptr
, rstr
, rslen
);
7495 str
= e
; /* e == end of match */
7500 if (((mflags
& MATCH_GLOBREP
) == 0) || mtype
!= MATCH_ANY
)
7505 /* On a zero-length match, make sure we copy one character, since
7506 we increment one character to avoid infinite recursion. */
7507 RESIZE_MALLOCED_BUFFER (ret
, rptr
, 1, rsize
, 64);
7508 ret
[rptr
++] = *str
++;
7509 e
++; /* avoid infinite recursion on zero-length match */
7513 /* Now copy the unmatched portion of the input string */
7516 RESIZE_MALLOCED_BUFFER (ret
, rptr
, STRLEN(str
) + 1, rsize
, 64);
7517 strcpy (ret
+ rptr
, str
);
7525 /* Do pattern match and replacement on the positional parameters. */
7527 pos_params_pat_subst (string
, pat
, rep
, mflags
)
7528 char *string
, *pat
, *rep
;
7531 WORD_LIST
*save
, *params
;
7536 save
= params
= list_rest_of_args ();
7538 return ((char *)NULL
);
7540 for ( ; params
; params
= params
->next
)
7542 ret
= pat_subst (params
->word
->word
, pat
, rep
, mflags
);
7543 w
= alloc_word_desc ();
7544 w
->word
= ret
? ret
: savestring ("");
7545 dispose_word (params
->word
);
7549 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
7550 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
7552 ret
= string_list_pos_params (pchar
, save
, qflags
);
7554 dispose_words (save
);
7559 /* Perform pattern substitution on VALUE, which is the expansion of
7560 VARNAME. PATSUB is an expression supplying the pattern to match
7561 and the string to substitute. QUOTED is a flags word containing
7562 the type of quoting currently in effect. */
7564 parameter_brace_patsub (varname
, value
, ind
, patsub
, quoted
, pflags
, flags
)
7565 char *varname
, *value
;
7568 int quoted
, pflags
, flags
;
7570 int vtype
, mflags
, starsub
, delim
;
7571 char *val
, *temp
, *pat
, *rep
, *p
, *lpatsub
, *tt
;
7575 return ((char *)NULL
);
7577 this_command_name
= varname
;
7579 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
7581 return ((char *)NULL
);
7583 starsub
= vtype
& VT_STARSUB
;
7584 vtype
&= ~VT_STARSUB
;
7587 /* PATSUB is never NULL when this is called. */
7590 mflags
|= MATCH_GLOBREP
;
7594 /* Malloc this because expand_string_if_necessary or one of the expansion
7595 functions in its call chain may free it on a substitution error. */
7596 lpatsub
= savestring (patsub
);
7598 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
7599 mflags
|= MATCH_QUOTED
;
7602 mflags
|= MATCH_STARSUB
;
7604 if (pflags
& PF_ASSIGNRHS
)
7605 mflags
|= MATCH_ASSIGNRHS
;
7607 /* If the pattern starts with a `/', make sure we skip over it when looking
7608 for the replacement delimiter. */
7609 delim
= skip_to_delim (lpatsub
, ((*patsub
== '/') ? 1 : 0), "/", 0);
7610 if (lpatsub
[delim
] == '/')
7613 rep
= lpatsub
+ delim
+ 1;
7618 if (rep
&& *rep
== '\0')
7621 /* Perform the same expansions on the pattern as performed by the
7622 pattern removal expansions. */
7623 pat
= getpattern (lpatsub
, quoted
, 1);
7627 /* We want to perform quote removal on the expanded replacement even if
7628 the entire expansion is double-quoted because the parser and string
7629 extraction functions treated quotes in the replacement string as
7630 special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */
7631 if (shell_compatibility_level
> 42)
7632 rep
= expand_string_if_necessary (rep
, quoted
& ~(Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
), expand_string_unsplit
);
7633 /* This is the bash-4.2 code. */
7634 else if ((mflags
& MATCH_QUOTED
) == 0)
7635 rep
= expand_string_if_necessary (rep
, quoted
, expand_string_unsplit
);
7637 rep
= expand_string_to_string_internal (rep
, quoted
, expand_string_unsplit
);
7640 /* ksh93 doesn't allow the match specifier to be a part of the expanded
7641 pattern. This is an extension. Make sure we don't anchor the pattern
7642 at the beginning or end of the string if we're doing global replacement,
7645 if (mflags
& MATCH_GLOBREP
)
7646 mflags
|= MATCH_ANY
;
7647 else if (pat
&& pat
[0] == '#')
7649 mflags
|= MATCH_BEG
;
7652 else if (pat
&& pat
[0] == '%')
7654 mflags
|= MATCH_END
;
7658 mflags
|= MATCH_ANY
;
7660 /* OK, we now want to substitute REP for PAT in VAL. If
7661 flags & MATCH_GLOBREP is non-zero, the substitution is done
7662 everywhere, otherwise only the first occurrence of PAT is
7663 replaced. The pattern matching code doesn't understand
7664 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
7665 values passed in (VT_VARIABLE) so the pattern substitution
7666 code works right. We need to requote special chars after
7667 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
7668 other cases if QUOTED == 0, since the posparams and arrays
7669 indexed by * or @ do special things when QUOTED != 0. */
7674 case VT_ARRAYMEMBER
:
7675 temp
= pat_subst (val
, p
, rep
, mflags
);
7676 if (vtype
== VT_VARIABLE
)
7680 tt
= (mflags
& MATCH_QUOTED
) ? quote_string (temp
) : quote_escapes (temp
);
7686 temp
= pos_params_pat_subst (val
, p
, rep
, mflags
);
7687 if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
7689 tt
= quote_escapes (temp
);
7694 #if defined (ARRAY_VARS)
7696 temp
= assoc_p (v
) ? assoc_patsub (assoc_cell (v
), p
, rep
, mflags
)
7697 : array_patsub (array_cell (v
), p
, rep
, mflags
);
7698 /* Don't call quote_escapes anymore; array_patsub calls
7699 array_quote_escapes as appropriate before adding the
7700 space separators; ditto for assoc_patsub. */
7712 /****************************************************************/
7714 /* Functions to perform case modification on variable values */
7716 /****************************************************************/
7718 /* Do case modification on the positional parameters. */
7721 pos_params_modcase (string
, pat
, modop
, mflags
)
7726 WORD_LIST
*save
, *params
;
7731 save
= params
= list_rest_of_args ();
7733 return ((char *)NULL
);
7735 for ( ; params
; params
= params
->next
)
7737 ret
= sh_modcase (params
->word
->word
, pat
, modop
);
7738 w
= alloc_word_desc ();
7739 w
->word
= ret
? ret
: savestring ("");
7740 dispose_word (params
->word
);
7744 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
7745 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
7747 ret
= string_list_pos_params (pchar
, save
, qflags
);
7748 dispose_words (save
);
7753 /* Perform case modification on VALUE, which is the expansion of
7754 VARNAME. MODSPEC is an expression supplying the type of modification
7755 to perform. QUOTED is a flags word containing the type of quoting
7756 currently in effect. */
7758 parameter_brace_casemod (varname
, value
, ind
, modspec
, patspec
, quoted
, flags
)
7759 char *varname
, *value
;
7764 int vtype
, starsub
, modop
, mflags
, x
;
7765 char *val
, *temp
, *pat
, *p
, *lpat
, *tt
;
7769 return ((char *)NULL
);
7771 this_command_name
= varname
;
7773 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
7775 return ((char *)NULL
);
7777 starsub
= vtype
& VT_STARSUB
;
7778 vtype
&= ~VT_STARSUB
;
7782 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
7783 mflags
|= MATCH_QUOTED
;
7785 mflags
|= MATCH_STARSUB
;
7790 x
= p
&& p
[0] == modspec
;
7791 modop
= x
? CASE_UPPER
: CASE_UPFIRST
;
7794 else if (modspec
== ',')
7796 x
= p
&& p
[0] == modspec
;
7797 modop
= x
? CASE_LOWER
: CASE_LOWFIRST
;
7800 else if (modspec
== '~')
7802 x
= p
&& p
[0] == modspec
;
7803 modop
= x
? CASE_TOGGLEALL
: CASE_TOGGLE
;
7807 lpat
= p
? savestring (p
) : 0;
7808 /* Perform the same expansions on the pattern as performed by the
7809 pattern removal expansions. FOR LATER */
7810 pat
= lpat
? getpattern (lpat
, quoted
, 1) : 0;
7812 /* OK, now we do the case modification. */
7816 case VT_ARRAYMEMBER
:
7817 temp
= sh_modcase (val
, pat
, modop
);
7818 if (vtype
== VT_VARIABLE
)
7822 tt
= (mflags
& MATCH_QUOTED
) ? quote_string (temp
) : quote_escapes (temp
);
7829 temp
= pos_params_modcase (val
, pat
, modop
, mflags
);
7830 if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
7832 tt
= quote_escapes (temp
);
7838 #if defined (ARRAY_VARS)
7840 temp
= assoc_p (v
) ? assoc_modcase (assoc_cell (v
), pat
, modop
, mflags
)
7841 : array_modcase (array_cell (v
), pat
, modop
, mflags
);
7842 /* Don't call quote_escapes; array_modcase calls array_quote_escapes
7843 as appropriate before adding the space separators; ditto for
7855 /* Check for unbalanced parens in S, which is the contents of $(( ... )). If
7856 any occur, this must be a nested command substitution, so return 0.
7857 Otherwise, return 1. A valid arithmetic expression must always have a
7858 ( before a matching ), so any cases where there are more right parens
7859 means that this must not be an arithmetic expression, though the parser
7860 will not accept it without a balanced total number of parens. */
7862 chk_arithsub (s
, len
)
7874 else if (s
[i
] == RPAREN
)
7884 ADVANCE_CHAR (s
, len
, i
);
7890 ADVANCE_CHAR (s
, len
, i
);
7894 i
= skip_single_quoted (s
, len
, ++i
, 0);
7898 i
= skip_double_quoted ((char *)s
, len
, ++i
, 0);
7903 return (count
== 0);
7906 /****************************************************************/
7908 /* Functions to perform parameter expansion on a string */
7910 /****************************************************************/
7912 /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
7914 parameter_brace_expand (string
, indexp
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
)
7916 int *indexp
, quoted
, pflags
, *quoted_dollar_atp
, *contains_dollar_at
;
7918 int check_nullness
, var_is_set
, var_is_null
, var_is_special
;
7919 int want_substring
, want_indir
, want_patsub
, want_casemod
;
7920 char *name
, *value
, *temp
, *temp1
;
7921 WORD_DESC
*tdesc
, *ret
;
7922 int t_index
, sindex
, c
, tflag
, modspec
, all_element_arrayref
;
7926 temp
= temp1
= value
= (char *)NULL
;
7927 var_is_set
= var_is_null
= var_is_special
= check_nullness
= 0;
7928 want_substring
= want_indir
= want_patsub
= want_casemod
= 0;
7930 all_element_arrayref
= 0;
7934 /* ${#var} doesn't have any of the other parameter expansions on it. */
7935 if (string
[t_index
] == '#' && legal_variable_starter (string
[t_index
+1])) /* {{ */
7936 name
= string_extract (string
, &t_index
, "}", SX_VARNAME
);
7938 #if defined (CASEMOD_EXPANSIONS)
7939 /* To enable case-toggling expansions using the `~' operator character
7940 change the 1 to 0. */
7941 # if defined (CASEMOD_CAPCASE)
7942 name
= string_extract (string
, &t_index
, "#%^,~:-=?+/@}", SX_VARNAME
);
7944 name
= string_extract (string
, &t_index
, "#%^,:-=?+/@}", SX_VARNAME
);
7945 # endif /* CASEMOD_CAPCASE */
7947 name
= string_extract (string
, &t_index
, "#%:-=?+/@}", SX_VARNAME
);
7948 #endif /* CASEMOD_EXPANSIONS */
7950 /* Handle ${@[stuff]} now that @ is a word expansion operator. Not exactly
7951 the cleanest code ever. */
7952 if (*name
== 0 && sindex
== t_index
&& string
[sindex
] == '@')
7954 name
= (char *)xrealloc (name
, 2);
7959 else if (*name
== '!' && t_index
> sindex
&& string
[t_index
] == '@' && string
[t_index
+1] == '}')
7961 name
= (char *)xrealloc (name
, t_index
- sindex
+ 2);
7962 name
[t_index
- sindex
] = '@';
7963 name
[t_index
- sindex
+ 1] = '\0';
7972 /* If the name really consists of a special variable, then make sure
7973 that we have the entire name. We don't allow indirect references
7974 to special variables except `#', `?', `@' and `*'. This clause is
7975 designed to handle ${#SPECIAL} and ${!SPECIAL}, not anything more
7977 if ((sindex
== t_index
&& VALID_SPECIAL_LENGTH_PARAM (string
[t_index
])) ||
7978 (sindex
== t_index
&& string
[sindex
] == '#' && VALID_SPECIAL_LENGTH_PARAM (string
[sindex
+ 1])) ||
7979 (sindex
== t_index
- 1 && string
[sindex
] == '!' && VALID_INDIR_PARAM (string
[t_index
])))
7982 temp1
= string_extract (string
, &t_index
, "#%:-=?+/@}", 0);
7983 name
= (char *)xrealloc (name
, 3 + (strlen (temp1
)));
7984 *name
= string
[sindex
];
7985 if (string
[sindex
] == '!')
7987 /* indirect reference of $#, $?, $@, or $* */
7988 name
[1] = string
[sindex
+ 1];
7989 strcpy (name
+ 2, temp1
);
7992 strcpy (name
+ 1, temp1
);
7997 /* Find out what character ended the variable name. Then
7998 do the appropriate thing. */
7999 if (c
= string
[sindex
])
8002 /* If c is followed by one of the valid parameter expansion
8003 characters, move past it as normal. If not, assume that
8004 a substring specification is being given, and do not move
8006 if (c
== ':' && VALID_PARAM_EXPAND_CHAR (string
[sindex
]))
8009 if (c
= string
[sindex
])
8012 else if (c
== ':' && string
[sindex
] != RBRACE
)
8014 else if (c
== '/' /* && string[sindex] != RBRACE */) /* XXX */
8016 #if defined (CASEMOD_EXPANSIONS)
8017 else if (c
== '^' || c
== ',' || c
== '~')
8024 /* Catch the valid and invalid brace expressions that made it through the
8026 /* ${#-} is a valid expansion and means to take the length of $-.
8027 Similarly for ${#?} and ${##}... */
8028 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
8029 VALID_SPECIAL_LENGTH_PARAM (c
) && string
[sindex
] == RBRACE
)
8031 name
= (char *)xrealloc (name
, 3);
8034 c
= string
[sindex
++];
8037 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
8038 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
8039 member (c
, "%:=+/") && string
[sindex
] == RBRACE
)
8041 temp
= (char *)NULL
;
8042 goto bad_substitution
; /* XXX - substitution error */
8045 /* Indirect expansion begins with a `!'. A valid indirect expansion is
8046 either a variable name, one of the positional parameters or a special
8047 variable that expands to one of the positional parameters. */
8048 want_indir
= *name
== '!' &&
8049 (legal_variable_starter ((unsigned char)name
[1]) || DIGIT (name
[1])
8050 || VALID_INDIR_PARAM (name
[1]));
8052 /* Determine the value of this variable. */
8054 /* Check for special variables, directly referenced. */
8055 if (SPECIAL_VAR (name
, want_indir
))
8058 /* Check for special expansion things, like the length of a parameter */
8059 if (*name
== '#' && name
[1])
8061 /* If we are not pointing at the character just after the
8062 closing brace, then we haven't gotten all of the name.
8063 Since it begins with a special character, this is a bad
8064 substitution. Also check NAME for validity before trying
8066 if (string
[sindex
- 1] != RBRACE
|| (valid_length_expression (name
) == 0))
8068 temp
= (char *)NULL
;
8069 goto bad_substitution
; /* substitution error */
8072 number
= parameter_brace_expand_length (name
);
8073 if (number
== INTMAX_MIN
&& unbound_vars_is_error
)
8075 last_command_exit_value
= EXECUTION_FAILURE
;
8076 err_unboundvar (name
+1);
8078 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8084 return (&expand_wdesc_error
);
8087 ret
= alloc_word_desc ();
8088 ret
->word
= itos (number
);
8093 /* ${@} is identical to $@. */
8094 if (name
[0] == '@' && name
[1] == '\0')
8096 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
8097 *quoted_dollar_atp
= 1;
8099 if (contains_dollar_at
)
8100 *contains_dollar_at
= 1;
8102 tflag
|= W_DOLLARAT
;
8105 /* Process ${!PREFIX*} expansion. */
8106 if (want_indir
&& string
[sindex
- 1] == RBRACE
&&
8107 (string
[sindex
- 2] == '*' || string
[sindex
- 2] == '@') &&
8108 legal_variable_starter ((unsigned char) name
[1]))
8113 temp1
= savestring (name
+ 1);
8114 number
= strlen (temp1
);
8115 temp1
[number
- 1] = '\0';
8116 x
= all_variables_matching_prefix (temp1
);
8117 xlist
= strvec_to_word_list (x
, 0, 0);
8118 if (string
[sindex
- 2] == '*')
8119 temp
= string_list_dollar_star (xlist
);
8122 temp
= string_list_dollar_at (xlist
, quoted
, 0);
8123 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
8124 *quoted_dollar_atp
= 1;
8125 if (contains_dollar_at
)
8126 *contains_dollar_at
= 1;
8128 tflag
|= W_DOLLARAT
;
8131 dispose_words (xlist
);
8137 ret
= alloc_word_desc ();
8139 ret
->flags
= tflag
; /* XXX */
8143 #if defined (ARRAY_VARS)
8144 /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */
8145 if (want_indir
&& string
[sindex
- 1] == RBRACE
&&
8146 string
[sindex
- 2] == ']' && valid_array_reference (name
+1, 0))
8150 temp1
= savestring (name
+ 1);
8151 x
= array_variable_name (temp1
, &x1
, (int *)0); /* [ */
8153 if (ALL_ELEMENT_SUB (x1
[0]) && x1
[1] == ']')
8155 temp
= array_keys (temp1
, quoted
); /* handles assoc vars too */
8158 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
8159 *quoted_dollar_atp
= 1;
8160 if (contains_dollar_at
)
8161 *contains_dollar_at
= 1;
8163 tflag
|= W_DOLLARAT
;
8170 ret
= alloc_word_desc ();
8172 ret
->flags
= tflag
; /* XXX */
8178 #endif /* ARRAY_VARS */
8180 /* Make sure that NAME is valid before trying to go on. */
8181 if (valid_brace_expansion_word (want_indir
? name
+ 1 : name
,
8182 var_is_special
) == 0)
8184 temp
= (char *)NULL
;
8185 goto bad_substitution
; /* substitution error */
8190 tdesc
= parameter_brace_expand_indir (name
+ 1, var_is_special
, quoted
, quoted_dollar_atp
, contains_dollar_at
);
8191 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
8193 temp
= (char *)NULL
;
8194 goto bad_substitution
;
8196 /* Turn off the W_ARRAYIND flag because there is no way for this function
8197 to return the index we're supposed to be using. */
8198 if (tdesc
&& tdesc
->flags
)
8199 tdesc
->flags
&= ~W_ARRAYIND
;
8202 tdesc
= parameter_brace_expand_word (name
, var_is_special
, quoted
, PF_IGNUNBOUND
|(pflags
&(PF_NOSPLIT2
|PF_ASSIGNRHS
)), &ind
);
8207 tflag
= tdesc
->flags
;
8208 dispose_word_desc (tdesc
);
8213 if (temp
== &expand_param_error
|| temp
== &expand_param_fatal
)
8217 return (temp
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8220 #if defined (ARRAY_VARS)
8221 if (valid_array_reference (name
, 0))
8227 /* If in a context where word splitting will not take place, treat as
8228 if double-quoted. Has effects with $* and ${array[*]} */
8229 if (pflags
& PF_ASSIGNRHS
)
8230 qflags
|= Q_DOUBLE_QUOTES
;
8231 chk_atstar (name
, qflags
, quoted_dollar_atp
, contains_dollar_at
);
8232 /* We duplicate a little code here */
8233 t
= mbschr (name
, '[');
8234 if (t
&& ALL_ELEMENT_SUB (t
[1]) && t
[2] == ']')
8235 all_element_arrayref
= 1;
8239 var_is_set
= temp
!= (char *)0;
8240 var_is_null
= check_nullness
&& (var_is_set
== 0 || *temp
== 0);
8241 /* XXX - this may not need to be restricted to special variables */
8243 var_is_null
|= var_is_set
&& var_is_special
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && QUOTED_NULL (temp
);
8245 /* Get the rest of the stuff inside the braces. */
8246 if (c
&& c
!= RBRACE
)
8248 /* Extract the contents of the ${ ... } expansion
8249 according to the Posix.2 rules. */
8250 value
= extract_dollar_brace_string (string
, &sindex
, quoted
, (c
== '%' || c
== '#' || c
=='/' || c
== '^' || c
== ',' || c
==':') ? SX_POSIXEXP
|SX_WORD
: SX_WORD
);
8251 if (string
[sindex
] == RBRACE
)
8254 goto bad_substitution
; /* substitution error */
8257 value
= (char *)NULL
;
8261 /* All the cases where an expansion can possibly generate an unbound
8263 if (want_substring
|| want_patsub
|| want_casemod
|| c
== '#' || c
== '%' || c
== RBRACE
)
8265 if (var_is_set
== 0 && unbound_vars_is_error
&& ((name
[0] != '@' && name
[0] != '*') || name
[1]) && all_element_arrayref
== 0)
8267 last_command_exit_value
= EXECUTION_FAILURE
;
8268 err_unboundvar (name
);
8272 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8276 /* If this is a substring spec, process it and add the result. */
8279 temp1
= parameter_brace_substring (name
, temp
, ind
, value
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
8284 if (temp1
== &expand_param_error
)
8285 return (&expand_wdesc_error
);
8286 else if (temp1
== &expand_param_fatal
)
8287 return (&expand_wdesc_fatal
);
8289 ret
= alloc_word_desc ();
8291 /* We test quoted_dollar_atp because we want variants with double-quoted
8292 "$@" to take a different code path. In fact, we make sure at the end
8293 of expand_word_internal that we're only looking at these flags if
8294 quoted_dollar_at == 0. */
8296 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
8297 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8298 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
8301 else if (want_patsub
)
8303 temp1
= parameter_brace_patsub (name
, temp
, ind
, value
, quoted
, pflags
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
8308 if (temp1
== &expand_param_error
)
8309 return (&expand_wdesc_error
);
8310 else if (temp1
== &expand_param_fatal
)
8311 return (&expand_wdesc_fatal
);
8313 ret
= alloc_word_desc ();
8316 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
8317 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8318 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
8321 #if defined (CASEMOD_EXPANSIONS)
8322 else if (want_casemod
)
8324 temp1
= parameter_brace_casemod (name
, temp
, ind
, modspec
, value
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
8329 if (temp1
== &expand_param_error
)
8330 return (&expand_wdesc_error
);
8331 else if (temp1
== &expand_param_fatal
)
8332 return (&expand_wdesc_fatal
);
8334 ret
= alloc_word_desc ();
8337 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
8338 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8339 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
8344 /* Do the right thing based on which character ended the variable name. */
8350 last_command_exit_value
= EXECUTION_FAILURE
;
8351 report_error (_("%s: bad substitution"), string
? string
: "??");
8355 if (shell_compatibility_level
<= 43)
8356 return &expand_wdesc_error
;
8358 return ((posixly_correct
&& interactive_shell
== 0) ? &expand_wdesc_fatal
: &expand_wdesc_error
);
8364 temp1
= parameter_brace_transform (name
, temp
, ind
, value
, c
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
8368 if (temp1
== &expand_param_error
|| temp1
== &expand_param_fatal
)
8370 last_command_exit_value
= EXECUTION_FAILURE
;
8371 report_error (_("%s: bad substitution"), string
? string
: "??");
8372 return (temp1
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8375 ret
= alloc_word_desc ();
8377 if (temp1
&& QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8378 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
8381 case '#': /* ${param#[#]pattern} */
8382 case '%': /* ${param%[%]pattern} */
8383 if (value
== 0 || *value
== '\0' || temp
== 0 || *temp
== '\0')
8388 temp1
= parameter_brace_remove_pattern (name
, temp
, ind
, value
, c
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
8393 ret
= alloc_word_desc ();
8395 if (temp1
&& QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8396 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
8403 if (var_is_set
&& var_is_null
== 0)
8405 /* If the operator is `+', we don't want the value of the named
8406 variable for anything, just the value of the right hand side. */
8409 /* XXX -- if we're double-quoted and the named variable is "$@",
8410 we want to turn off any special handling of "$@" --
8411 we're not using it, so whatever is on the rhs applies. */
8412 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
8413 *quoted_dollar_atp
= 0;
8414 if (contains_dollar_at
)
8415 *contains_dollar_at
= 0;
8420 /* From Posix discussion on austin-group list. Issue 221
8421 requires that backslashes escaping `}' inside
8422 double-quoted ${...} be removed. */
8423 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
8424 quoted
|= Q_DOLBRACE
;
8425 ret
= parameter_brace_expand_rhs (name
, value
, c
,
8429 contains_dollar_at
);
8430 /* XXX - fix up later, esp. noting presence of
8431 W_HASQUOTEDNULL in ret->flags */
8435 temp
= (char *)NULL
;
8441 /* Otherwise do nothing; just use the value in TEMP. */
8443 else /* VAR not set or VAR is NULL. */
8446 temp
= (char *)NULL
;
8447 if (c
== '=' && var_is_special
)
8449 last_command_exit_value
= EXECUTION_FAILURE
;
8450 report_error (_("$%s: cannot assign in this way"), name
);
8453 return &expand_wdesc_error
;
8457 parameter_brace_expand_error (name
, value
);
8458 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8462 /* XXX -- if we're double-quoted and the named variable is "$@",
8463 we want to turn off any special handling of "$@" --
8464 we're not using it, so whatever is on the rhs applies. */
8465 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
8466 *quoted_dollar_atp
= 0;
8467 if (contains_dollar_at
)
8468 *contains_dollar_at
= 0;
8470 /* From Posix discussion on austin-group list. Issue 221 requires
8471 that backslashes escaping `}' inside double-quoted ${...} be
8473 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
8474 quoted
|= Q_DOLBRACE
;
8475 ret
= parameter_brace_expand_rhs (name
, value
, c
, quoted
, pflags
,
8477 contains_dollar_at
);
8478 /* XXX - fix up later, esp. noting presence of
8479 W_HASQUOTEDNULL in tdesc->flags */
8490 ret
= alloc_word_desc ();
8497 /* Expand a single ${xxx} expansion. The braces are optional. When
8498 the braces are used, parameter_brace_expand() does the work,
8499 possibly calling param_expand recursively. */
8501 param_expand (string
, sindex
, quoted
, expanded_something
,
8502 contains_dollar_at
, quoted_dollar_at_p
, had_quoted_null_p
,
8505 int *sindex
, quoted
, *expanded_something
, *contains_dollar_at
;
8506 int *quoted_dollar_at_p
, *had_quoted_null_p
, pflags
;
8508 char *temp
, *temp1
, uerror
[3], *savecmd
;
8509 int zindex
, t_index
, expok
;
8514 WORD_DESC
*tdesc
, *ret
;
8517 /*itrace("param_expand: `%s' pflags = %d", string+*sindex, pflags);*/
8519 c
= string
[++zindex
];
8521 temp
= (char *)NULL
;
8522 ret
= tdesc
= (WORD_DESC
*)NULL
;
8525 /* Do simple cases first. Switch on what follows '$'. */
8539 temp1
= dollar_vars
[TODIGIT (c
)];
8540 if (unbound_vars_is_error
&& temp1
== (char *)NULL
)
8545 last_command_exit_value
= EXECUTION_FAILURE
;
8546 err_unboundvar (uerror
);
8547 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8550 temp
= (*temp1
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8551 ? quote_string (temp1
)
8552 : quote_escapes (temp1
);
8554 temp
= (char *)NULL
;
8558 /* $$ -- pid of the invoking shell. */
8560 temp
= itos (dollar_dollar_pid
);
8563 /* $# -- number of positional parameters. */
8565 temp
= itos (number_of_args ());
8568 /* $? -- return value of the last synchronous command. */
8570 temp
= itos (last_command_exit_value
);
8573 /* $- -- flags supplied to the shell on invocation or by `set'. */
8575 temp
= which_set_flags ();
8578 /* $! -- Pid of the last asynchronous command. */
8580 /* If no asynchronous pids have been created, expand to nothing.
8581 If `set -u' has been executed, and no async processes have
8582 been created, this is an expansion error. */
8583 if (last_asynchronous_pid
== NO_PID
)
8585 if (expanded_something
)
8586 *expanded_something
= 0;
8587 temp
= (char *)NULL
;
8588 if (unbound_vars_is_error
)
8593 last_command_exit_value
= EXECUTION_FAILURE
;
8594 err_unboundvar (uerror
);
8595 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8599 temp
= itos (last_asynchronous_pid
);
8602 /* The only difference between this and $@ is when the arg is quoted. */
8603 case '*': /* `$*' */
8604 list
= list_rest_of_args ();
8607 /* According to austin-group posix proposal by Geoff Clare in
8608 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
8610 "The shell shall write a message to standard error and
8611 immediately exit when it tries to expand an unset parameter
8612 other than the '@' and '*' special parameters."
8615 if (list
== 0 && unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
8620 last_command_exit_value
= EXECUTION_FAILURE
;
8621 err_unboundvar (uerror
);
8622 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8626 /* If there are no command-line arguments, this should just
8627 disappear if there are other characters in the expansion,
8628 even if it's quoted. */
8629 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && list
== 0)
8630 temp
= (char *)NULL
;
8631 else if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
|Q_PATQUOTE
))
8633 /* If we have "$*" we want to make a string of the positional
8634 parameters, separated by the first character of $IFS, and
8635 quote the whole string, including the separators. If IFS
8636 is unset, the parameters are separated by ' '; if $IFS is
8637 null, the parameters are concatenated. */
8638 temp
= (quoted
& (Q_DOUBLE_QUOTES
|Q_PATQUOTE
)) ? string_list_dollar_star (list
) : string_list (list
);
8641 temp1
= (quoted
& Q_DOUBLE_QUOTES
) ? quote_string (temp
) : temp
;
8643 tflag
|= W_HASQUOTEDNULL
;
8651 /* We check whether or not we're eventually going to split $* here,
8652 for example when IFS is empty and we are processing the rhs of
8653 an assignment statement. In that case, we don't separate the
8654 arguments at all. Otherwise, if the $* is not quoted it is
8656 # if defined (HANDLE_MULTIBYTE)
8657 if (expand_no_split_dollar_star
&& ifs_firstc
[0] == 0)
8659 if (expand_no_split_dollar_star
&& ifs_firstc
== 0)
8661 temp
= string_list_dollar_star (list
);
8664 temp
= string_list_dollar_at (list
, quoted
, 0);
8665 if (quoted
== 0 && (ifs_is_set
== 0 || ifs_is_null
))
8666 tflag
|= W_SPLITSPACE
;
8667 /* If we're not quoted but we still don't want word splitting, make
8668 we quote the IFS characters to protect them from splitting (e.g.,
8669 when $@ is in the string as well). */
8670 else if (temp
&& quoted
== 0 && ifs_is_set
&& (pflags
& PF_ASSIGNRHS
))
8672 temp1
= quote_string (temp
);
8678 if (expand_no_split_dollar_star
== 0 && contains_dollar_at
)
8679 *contains_dollar_at
= 1;
8682 dispose_words (list
);
8685 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
8686 means that we have to turn quoting off after we split into
8687 the individually quoted arguments so that the final split
8688 on the first character of $IFS is still done. */
8689 case '@': /* `$@' */
8690 list
= list_rest_of_args ();
8693 /* According to austin-group posix proposal by Geoff Clare in
8694 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
8696 "The shell shall write a message to standard error and
8697 immediately exit when it tries to expand an unset parameter
8698 other than the '@' and '*' special parameters."
8701 if (list
== 0 && unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
8706 last_command_exit_value
= EXECUTION_FAILURE
;
8707 err_unboundvar (uerror
);
8708 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
8712 /* We want to flag the fact that we saw this. We can't turn
8713 off quoting entirely, because other characters in the
8714 string might need it (consider "\"$@\""), but we need some
8715 way to signal that the final split on the first character
8716 of $IFS should be done, even though QUOTED is 1. */
8717 /* XXX - should this test include Q_PATQUOTE? */
8718 if (quoted_dollar_at_p
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8719 *quoted_dollar_at_p
= 1;
8720 if (contains_dollar_at
)
8721 *contains_dollar_at
= 1;
8723 /* We want to separate the positional parameters with the first
8724 character of $IFS in case $IFS is something other than a space.
8725 We also want to make sure that splitting is done no matter what --
8726 according to POSIX.2, this expands to a list of the positional
8727 parameters no matter what IFS is set to. */
8728 /* XXX - what to do when in a context where word splitting is not
8729 performed? Even when IFS is not the default, posix seems to imply
8730 that we behave like unquoted $* ? Maybe we should use PF_NOSPLIT2
8732 /* XXX - bash-4.4/bash-5.0 passing PFLAGS */
8733 temp
= string_list_dollar_at (list
, (pflags
& PF_ASSIGNRHS
) ? (quoted
|Q_DOUBLE_QUOTES
) : quoted
, pflags
);
8735 tflag
|= W_DOLLARAT
;
8736 dispose_words (list
);
8740 tdesc
= parameter_brace_expand (string
, &zindex
, quoted
, pflags
,
8742 contains_dollar_at
);
8744 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
8746 temp
= tdesc
? tdesc
->word
: (char *)0;
8749 /* Quoted nulls should be removed if there is anything else
8751 /* Note that we saw the quoted null so we can add one back at
8752 the end of this function if there are no other characters
8753 in the string, discard TEMP, and go on. The exception to
8754 this is when we have "${@}" and $1 is '', since $@ needs
8755 special handling. */
8756 if (tdesc
&& tdesc
->word
&& (tdesc
->flags
& W_HASQUOTEDNULL
) && QUOTED_NULL (temp
))
8758 if (had_quoted_null_p
)
8759 *had_quoted_null_p
= 1;
8760 if (*quoted_dollar_at_p
== 0)
8763 tdesc
->word
= temp
= (char *)NULL
;
8771 /* Do command or arithmetic substitution. */
8773 /* We have to extract the contents of this paren substitution. */
8774 t_index
= zindex
+ 1;
8775 /* XXX - might want to check for string[t_index+2] == LPAREN and parse
8776 as arithmetic substitution immediately. */
8777 temp
= extract_command_subst (string
, &t_index
, (pflags
&PF_COMPLETE
) ? SX_COMPLETE
: 0);
8780 /* For Posix.2-style `$(( ))' arithmetic substitution,
8781 extract the expression and pass it to the evaluator. */
8782 if (temp
&& *temp
== LPAREN
)
8786 temp2
= savestring (temp1
);
8787 t_index
= strlen (temp2
) - 1;
8789 if (temp2
[t_index
] != RPAREN
)
8795 /* Cut off ending `)' */
8796 temp2
[t_index
] = '\0';
8798 if (chk_arithsub (temp2
, t_index
) == 0)
8802 internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
8807 /* Expand variables found inside the expression. */
8808 temp1
= expand_arith_string (temp2
, Q_DOUBLE_QUOTES
|Q_ARITH
);
8812 /* No error messages. */
8813 savecmd
= this_command_name
;
8814 this_command_name
= (char *)NULL
;
8815 number
= evalexp (temp1
, &expok
);
8816 this_command_name
= savecmd
;
8821 if (interactive_shell
== 0 && posixly_correct
)
8823 last_command_exit_value
= EXECUTION_FAILURE
;
8824 return (&expand_wdesc_fatal
);
8827 return (&expand_wdesc_error
);
8829 temp
= itos (number
);
8834 if (pflags
& PF_NOCOMSUB
)
8835 /* we need zindex+1 because string[zindex] == RPAREN */
8836 temp1
= substring (string
, *sindex
, zindex
+1);
8839 tdesc
= command_substitute (temp
, quoted
);
8840 temp1
= tdesc
? tdesc
->word
: (char *)NULL
;
8842 dispose_word_desc (tdesc
);
8848 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
8849 away in a future bash release. */
8851 /* Extract the contents of this arithmetic substitution. */
8852 t_index
= zindex
+ 1;
8853 temp
= extract_arithmetic_subst (string
, &t_index
);
8857 temp
= savestring (string
);
8858 if (expanded_something
)
8859 *expanded_something
= 0;
8863 /* Do initial variable expansion. */
8864 temp1
= expand_arith_string (temp
, Q_DOUBLE_QUOTES
|Q_ARITH
);
8869 /* Find the variable in VARIABLE_LIST. */
8870 temp
= (char *)NULL
;
8872 for (t_index
= zindex
; (c
= string
[zindex
]) && legal_variable_char (c
); zindex
++)
8874 temp1
= (zindex
> t_index
) ? substring (string
, t_index
, zindex
) : (char *)NULL
;
8876 /* If this isn't a variable name, then just output the `$'. */
8877 if (temp1
== 0 || *temp1
== '\0')
8880 temp
= (char *)xmalloc (2);
8883 if (expanded_something
)
8884 *expanded_something
= 0;
8888 /* If the variable exists, return its value cell. */
8889 var
= find_variable (temp1
);
8891 if (var
&& invisible_p (var
) == 0 && var_isset (var
))
8893 #if defined (ARRAY_VARS)
8894 if (assoc_p (var
) || array_p (var
))
8896 temp
= array_p (var
) ? array_reference (array_cell (var
), 0)
8897 : assoc_reference (assoc_cell (var
), "0");
8899 temp
= (*temp
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8900 ? quote_string (temp
)
8901 : quote_escapes (temp
);
8902 else if (unbound_vars_is_error
)
8903 goto unbound_variable
;
8908 temp
= value_cell (var
);
8910 temp
= (*temp
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8911 ? quote_string (temp
)
8912 : quote_escapes (temp
);
8919 else if (var
&& (invisible_p (var
) || var_isset (var
) == 0))
8920 temp
= (char *)NULL
;
8921 else if ((var
= find_variable_last_nameref (temp1
, 0)) && var_isset (var
) && invisible_p (var
) == 0)
8923 temp
= nameref_cell (var
);
8924 #if defined (ARRAY_VARS)
8925 if (temp
&& *temp
&& valid_array_reference (temp
, 0))
8927 tdesc
= parameter_brace_expand_word (temp
, SPECIAL_VAR (temp
, 0), quoted
, pflags
, (arrayind_t
*)NULL
);
8928 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
8935 /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */
8936 if (temp
&& *temp
&& legal_identifier (temp
) == 0)
8938 last_command_exit_value
= EXECUTION_FAILURE
;
8939 report_error (_("%s: invalid variable name for name reference"), temp
);
8940 return (&expand_wdesc_error
); /* XXX */
8943 temp
= (char *)NULL
;
8946 temp
= (char *)NULL
;
8949 if (unbound_vars_is_error
)
8951 last_command_exit_value
= EXECUTION_FAILURE
;
8952 err_unboundvar (temp1
);
8961 last_command_exit_value
= EXECUTION_FAILURE
;
8962 return ((unbound_vars_is_error
&& interactive_shell
== 0)
8963 ? &expand_wdesc_fatal
8964 : &expand_wdesc_error
);
8975 ret
= alloc_word_desc ();
8976 ret
->flags
= tflag
; /* XXX */
8983 invalidate_cached_quoted_dollar_at ()
8985 dispose_words (cached_quoted_dollar_at
);
8986 cached_quoted_dollar_at
= 0;
8989 /* Make a word list which is the result of parameter and variable
8990 expansion, command substitution, arithmetic substitution, and
8991 quote removal of WORD. Return a pointer to a WORD_LIST which is
8992 the result of the expansion. If WORD contains a null word, the
8993 word list returned is also null.
8995 QUOTED contains flag values defined in shell.h.
8997 ISEXP is used to tell expand_word_internal that the word should be
8998 treated as the result of an expansion. This has implications for
8999 how IFS characters in the word are treated.
9001 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
9002 they point to an integer value which receives information about expansion.
9003 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
9004 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
9007 This only does word splitting in the case of $@ expansion. In that
9008 case, we split on ' '. */
9010 /* Values for the local variable quoted_state. */
9012 #define PARTIALLY_QUOTED 1
9013 #define WHOLLY_QUOTED 2
9016 expand_word_internal (word
, quoted
, isexp
, contains_dollar_at
, expanded_something
)
9019 int *contains_dollar_at
;
9020 int *expanded_something
;
9025 /* The intermediate string that we build while expanding. */
9028 /* The current size of the above object. */
9029 size_t istring_size
;
9031 /* Index into ISTRING. */
9034 /* Temporary string storage. */
9037 /* The text of WORD. */
9038 register char *string
;
9040 /* The size of STRING. */
9043 /* The index into STRING. */
9046 /* This gets 1 if we see a $@ while quoted. */
9047 int quoted_dollar_at
;
9049 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
9050 whether WORD contains no quoting characters, a partially quoted
9051 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
9055 int had_quoted_null
;
9056 int has_dollar_at
, temp_has_dollar_at
;
9057 int split_on_spaces
;
9059 int pflags
; /* flags passed to param_expand */
9062 int assignoff
; /* If assignment, offset of `=' */
9064 register unsigned char c
; /* Current character. */
9065 int t_index
; /* For calls to string_extract_xxx. */
9071 /* OK, let's see if we can optimize a common idiom: "$@" */
9072 if (STREQ (word
->word
, "\"$@\"") &&
9073 (word
->flags
== (W_HASDOLLAR
|W_QUOTED
)) &&
9074 dollar_vars
[1]) /* XXX - check IFS here as well? */
9076 if (contains_dollar_at
)
9077 *contains_dollar_at
= 1;
9078 if (expanded_something
)
9079 *expanded_something
= 1;
9080 if (cached_quoted_dollar_at
)
9081 return (copy_word_list (cached_quoted_dollar_at
));
9082 list
= list_rest_of_args ();
9083 list
= quote_list (list
);
9084 cached_quoted_dollar_at
= copy_word_list (list
);
9088 istring
= (char *)xmalloc (istring_size
= DEFAULT_INITIAL_ARRAY_SIZE
);
9089 istring
[istring_index
= 0] = '\0';
9090 quoted_dollar_at
= had_quoted_null
= has_dollar_at
= 0;
9091 split_on_spaces
= 0;
9092 quoted_state
= UNQUOTED
;
9094 string
= word
->word
;
9096 goto finished_with_string
;
9097 mb_cur_max
= MB_CUR_MAX
;
9099 /* Don't need the string length for the SADD... and COPY_ macros unless
9100 multibyte characters are possible. */
9101 string_size
= (mb_cur_max
> 1) ? strlen (string
) : 1;
9103 if (contains_dollar_at
)
9104 *contains_dollar_at
= 0;
9108 /* Begin the expansion. */
9114 /* Case on top-level character. */
9118 goto finished_with_string
;
9122 #if HANDLE_MULTIBYTE
9123 if (mb_cur_max
> 1 && string
[sindex
])
9125 SADD_MBQCHAR_BODY(temp
, string
, sindex
, string_size
);
9130 temp
= (char *)xmalloc (3);
9132 temp
[1] = c
= string
[sindex
];
9143 istring
= sub_append_string (temp
, istring
, &istring_index
, &istring_size
);
9149 #if defined (PROCESS_SUBSTITUTION)
9150 /* Process substitution. */
9154 /* bash-4.4/bash-5.0
9155 XXX - technically this should only be expanded at the start
9157 if (string
[++sindex
] != LPAREN
|| (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (word
->flags
& (W_DQUOTE
|W_NOPROCSUB
)) || posixly_correct
)
9159 sindex
--; /* add_character: label increments sindex */
9163 t_index
= sindex
+ 1; /* skip past both '<' and LPAREN */
9165 temp1
= extract_process_subst (string
, (c
== '<') ? "<(" : ">(", &t_index
, 0); /*))*/
9168 /* If the process substitution specification is `<()', we want to
9169 open the pipe for writing in the child and produce output; if
9170 it is `>()', we want to open the pipe for reading in the child
9171 and consume input. */
9172 temp
= temp1
? process_substitute (temp1
, (c
== '>')) : (char *)0;
9176 goto dollar_add_string
;
9178 #endif /* PROCESS_SUBSTITUTION */
9181 /* Posix.2 section 3.6.1 says that tildes following `=' in words
9182 which are not assignment statements are not expanded. If the
9183 shell isn't in posix mode, though, we perform tilde expansion
9184 on `likely candidate' unquoted assignment statements (flags
9185 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
9186 contains an unquoted :~ or =~. Something to think about: we
9187 now have a flag that says to perform tilde expansion on arguments
9188 to `assignment builtins' like declare and export that look like
9189 assignment statements. We now do tilde expansion on such words
9190 even in POSIX mode. */
9191 if (word
->flags
& (W_ASSIGNRHS
|W_NOTILDE
))
9193 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
9194 goto add_ifs_character
;
9198 /* If we're not in posix mode or forcing assignment-statement tilde
9199 expansion, note where the `=' appears in the word and prepare to
9200 do tilde expansion following the first `='. */
9201 if ((word
->flags
& W_ASSIGNMENT
) &&
9202 (posixly_correct
== 0 || (word
->flags
& W_TILDEEXP
)) &&
9203 assignoff
== -1 && sindex
> 0)
9205 if (sindex
== assignoff
&& string
[sindex
+1] == '~') /* XXX */
9206 word
->flags
|= W_ITILDE
;
9208 else if ((word
->flags
& W_ASSIGNMENT
) &&
9209 (posixly_correct
== 0 || (word
->flags
& W_TILDEEXP
)) &&
9210 string
[sindex
+1] == '~')
9211 word
->flags
|= W_ITILDE
;
9214 /* XXX - bash-4.4/bash-5.0 */
9215 if (word
->flags
& W_ASSIGNARG
)
9216 word
->flags
|= W_ASSIGNRHS
; /* affects $@ */
9218 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
9219 goto add_ifs_character
;
9224 if (word
->flags
& W_NOTILDE
)
9226 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
9227 goto add_ifs_character
;
9232 if ((word
->flags
& (W_ASSIGNMENT
|W_ASSIGNRHS
|W_TILDEEXP
)) &&
9233 string
[sindex
+1] == '~')
9234 word
->flags
|= W_ITILDE
;
9236 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
9237 goto add_ifs_character
;
9242 /* If the word isn't supposed to be tilde expanded, or we're not
9243 at the start of a word or after an unquoted : or = in an
9244 assignment statement, we don't do tilde expansion. If we don't want
9245 tilde expansion when expanding words to be passed to the arithmetic
9246 evaluator, remove the check for Q_ARITH. */
9247 if ((word
->flags
& (W_NOTILDE
|W_DQUOTE
)) ||
9248 (sindex
> 0 && ((word
->flags
& W_ITILDE
) == 0)) ||
9249 ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) && ((quoted
& Q_ARITH
) == 0)))
9251 word
->flags
&= ~W_ITILDE
;
9252 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
) && (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) == 0)
9253 goto add_ifs_character
;
9258 if (word
->flags
& W_ASSIGNRHS
)
9260 else if (word
->flags
& (W_ASSIGNMENT
|W_TILDEEXP
))
9265 temp
= bash_tilde_find_word (string
+ sindex
, tflag
, &t_index
);
9267 word
->flags
&= ~W_ITILDE
;
9269 if (temp
&& *temp
&& t_index
> 0)
9271 temp1
= bash_tilde_expand (temp
, tflag
);
9272 if (temp1
&& *temp1
== '~' && STREQ (temp
, temp1
))
9276 goto add_character
; /* tilde expansion failed */
9281 goto add_quoted_string
; /* XXX was add_string */
9290 if (expanded_something
)
9291 *expanded_something
= 1;
9293 temp_has_dollar_at
= 0;
9294 pflags
= (word
->flags
& W_NOCOMSUB
) ? PF_NOCOMSUB
: 0;
9295 if (word
->flags
& W_NOSPLIT2
)
9296 pflags
|= PF_NOSPLIT2
;
9297 if (word
->flags
& W_ASSIGNRHS
)
9298 pflags
|= PF_ASSIGNRHS
;
9299 if (word
->flags
& W_COMPLETE
)
9300 pflags
|= PF_COMPLETE
;
9301 tword
= param_expand (string
, &sindex
, quoted
, expanded_something
,
9302 &temp_has_dollar_at
, "ed_dollar_at
,
9303 &had_quoted_null
, pflags
);
9304 has_dollar_at
+= temp_has_dollar_at
;
9305 split_on_spaces
+= (tword
->flags
& W_SPLITSPACE
);
9307 if (tword
== &expand_wdesc_error
|| tword
== &expand_wdesc_fatal
)
9311 return ((tword
== &expand_wdesc_error
) ? &expand_word_error
9312 : &expand_word_fatal
);
9314 if (contains_dollar_at
&& has_dollar_at
)
9315 *contains_dollar_at
= 1;
9317 if (tword
&& (tword
->flags
& W_HASQUOTEDNULL
))
9318 had_quoted_null
= 1;
9320 temp
= tword
? tword
->word
: (char *)NULL
;
9321 dispose_word_desc (tword
);
9323 /* Kill quoted nulls; we will add them back at the end of
9324 expand_word_internal if nothing else in the string */
9325 if (had_quoted_null
&& temp
&& QUOTED_NULL (temp
))
9328 temp
= (char *)NULL
;
9334 case '`': /* Backquoted command substitution. */
9338 temp
= string_extract (string
, &sindex
, "`", SX_REQMATCH
);
9339 /* The test of sindex against t_index is to allow bare instances of
9340 ` to pass through, for backwards compatibility. */
9341 if (temp
== &extract_string_error
|| temp
== &extract_string_fatal
)
9343 if (sindex
- 1 == t_index
)
9348 last_command_exit_value
= EXECUTION_FAILURE
;
9349 report_error (_("bad substitution: no closing \"`\" in %s") , string
+t_index
);
9352 return ((temp
== &extract_string_error
) ? &expand_word_error
9353 : &expand_word_fatal
);
9356 if (expanded_something
)
9357 *expanded_something
= 1;
9359 if (word
->flags
& W_NOCOMSUB
)
9360 /* sindex + 1 because string[sindex] == '`' */
9361 temp1
= substring (string
, t_index
, sindex
+ 1);
9364 de_backslash (temp
);
9365 tword
= command_substitute (temp
, quoted
);
9366 temp1
= tword
? tword
->word
: (char *)NULL
;
9368 dispose_word_desc (tword
);
9372 goto dollar_add_string
;
9376 if (string
[sindex
+ 1] == '\n')
9382 c
= string
[++sindex
];
9384 if (quoted
& Q_HERE_DOCUMENT
)
9386 else if (quoted
& Q_DOUBLE_QUOTES
)
9391 /* From Posix discussion on austin-group list: Backslash escaping
9392 a } in ${...} is removed. Issue 0000221 */
9393 if ((quoted
& Q_DOLBRACE
) && c
== RBRACE
)
9395 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
9397 /* This is the fix for " $@\ " */
9398 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && ((sh_syntaxtab
[c
] & tflag
) == 0) && isexp
== 0 && isifs (c
))
9400 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
9401 DEFAULT_ARRAY_SIZE
);
9402 istring
[istring_index
++] = CTLESC
;
9403 istring
[istring_index
++] = '\\';
9404 istring
[istring_index
] = '\0';
9406 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
9408 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && ((sh_syntaxtab
[c
] & tflag
) == 0))
9410 SCOPY_CHAR_I (twochars
, '\\', c
, string
, sindex
, string_size
);
9415 sindex
--; /* add_character: label increments sindex */
9420 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
9425 /* BEFORE jumping here, we need to increment sindex if appropriate */
9426 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
9427 DEFAULT_ARRAY_SIZE
);
9428 istring
[istring_index
++] = twochars
[0];
9429 istring
[istring_index
++] = twochars
[1];
9430 istring
[istring_index
] = '\0';
9435 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) && ((quoted
& Q_ARITH
) == 0))
9439 temp
= string_extract_double_quoted (string
, &sindex
, (word
->flags
& W_COMPLETE
) ? SX_COMPLETE
: 0);
9441 /* If the quotes surrounded the entire string, then the
9442 whole word was quoted. */
9443 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
9449 tword
= alloc_word_desc ();
9452 /* XXX - bash-4.4/bash-5.0 */
9453 if (word
->flags
& W_ASSIGNARG
)
9454 tword
->flags
|= word
->flags
& (W_ASSIGNARG
|W_ASSIGNRHS
); /* affects $@ */
9455 if (word
->flags
& W_COMPLETE
)
9456 tword
->flags
|= W_COMPLETE
; /* for command substitutions */
9458 temp
= (char *)NULL
;
9460 temp_has_dollar_at
= 0; /* XXX */
9461 /* Need to get W_HASQUOTEDNULL flag through this function. */
9462 list
= expand_word_internal (tword
, Q_DOUBLE_QUOTES
, 0, &temp_has_dollar_at
, (int *)NULL
);
9463 has_dollar_at
+= temp_has_dollar_at
;
9465 if (list
== &expand_word_error
|| list
== &expand_word_fatal
)
9469 /* expand_word_internal has already freed temp_word->word
9470 for us because of the way it prints error messages. */
9471 tword
->word
= (char *)NULL
;
9472 dispose_word (tword
);
9476 dispose_word (tword
);
9478 /* "$@" (a double-quoted dollar-at) expands into nothing,
9479 not even a NULL word, when there are no positional
9481 if (list
== 0 && temp_has_dollar_at
) /* XXX - was has_dollar_at */
9487 /* If we get "$@", we know we have expanded something, so we
9488 need to remember it for the final split on $IFS. This is
9489 a special case; it's the only case where a quoted string
9490 can expand into more than one word. It's going to come back
9491 from the above call to expand_word_internal as a list with
9492 a single word, in which all characters are quoted and
9493 separated by blanks. What we want to do is to turn it back
9494 into a list for the next piece of code. */
9496 dequote_list (list
);
9498 if (list
&& list
->word
&& (list
->word
->flags
& W_HASQUOTEDNULL
))
9499 had_quoted_null
= 1; /* XXX */
9501 if (temp_has_dollar_at
) /* XXX - was has_dollar_at */
9504 if (contains_dollar_at
)
9505 *contains_dollar_at
= 1;
9506 if (expanded_something
)
9507 *expanded_something
= 1;
9512 /* What we have is "". This is a minor optimization. */
9514 list
= (WORD_LIST
*)NULL
;
9517 /* The code above *might* return a list (consider the case of "$@",
9518 where it returns "$1", "$2", etc.). We can't throw away the
9519 rest of the list, and we have to make sure each word gets added
9520 as quoted. We test on tresult->next: if it is non-NULL, we
9521 quote the whole list, save it to a string with string_list, and
9522 add that string. We don't need to quote the results of this
9523 (and it would be wrong, since that would quote the separators
9524 as well), so we go directly to add_string. */
9529 /* Testing quoted_dollar_at makes sure that "$@" is
9530 split correctly when $IFS does not contain a space. */
9531 temp
= quoted_dollar_at
9532 ? string_list_dollar_at (list
, Q_DOUBLE_QUOTES
, 0)
9533 : string_list (quote_list (list
));
9534 dispose_words (list
);
9539 temp
= savestring (list
->word
->word
);
9540 tflag
= list
->word
->flags
;
9541 dispose_words (list
);
9543 /* If the string is not a quoted null string, we want
9544 to remove any embedded unquoted CTLNUL characters.
9545 We do not want to turn quoted null strings back into
9546 the empty string, though. We do this because we
9547 want to remove any quoted nulls from expansions that
9548 contain other characters. For example, if we have
9549 x"$*"y or "x$*y" and there are no positional parameters,
9550 the $* should expand into nothing. */
9551 /* We use the W_HASQUOTEDNULL flag to differentiate the
9552 cases: a quoted null character as above and when
9553 CTLNUL is contained in the (non-null) expansion
9554 of some variable. We use the had_quoted_null flag to
9555 pass the value through this function to its caller. */
9556 if ((tflag
& W_HASQUOTEDNULL
) && QUOTED_NULL (temp
) == 0)
9557 remove_quoted_nulls (temp
); /* XXX */
9561 temp
= (char *)NULL
;
9563 /* We do not want to add quoted nulls to strings that are only
9564 partially quoted; we can throw them away. The exception to
9565 this is when we are going to be performing word splitting,
9566 since we have to preserve a null argument if the next character
9567 will cause word splitting. */
9568 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
&& (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)))
9576 temp
= quote_string (temp
);
9584 sindex
--; /* add_character: label increments sindex */
9591 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
9595 temp
= string_extract_single_quoted (string
, &sindex
);
9597 /* If the entire STRING was surrounded by single quotes,
9598 then the string is wholly quoted. */
9599 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
9603 /* If all we had was '', it is a null expansion. */
9607 temp
= (char *)NULL
;
9610 remove_quoted_escapes (temp
); /* ??? */
9612 /* We do not want to add quoted nulls to strings that are only
9613 partially quoted; such nulls are discarded. See above for the
9614 exception, which is when the string is going to be split. */
9615 if (temp
== 0 && (quoted_state
== PARTIALLY_QUOTED
) && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)))
9618 /* If we have a quoted null expansion, add a quoted NULL to istring. */
9622 sindex
--; /* add_character: label increments sindex */
9626 goto add_quoted_string
;
9631 /* This is the fix for " $@ " */
9633 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (isexp
== 0 && isifs (c
) && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0))
9635 if (string
[sindex
]) /* from old goto dollar_add_string */
9644 #if HANDLE_MULTIBYTE
9650 SADD_MBQCHAR_BODY(temp
, string
, sindex
, string_size
);
9655 twochars
[0] = CTLESC
;
9662 SADD_MBCHAR (temp
, string
, sindex
, string_size
);
9665 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 1, istring_size
,
9666 DEFAULT_ARRAY_SIZE
);
9667 istring
[istring_index
++] = c
;
9668 istring
[istring_index
] = '\0';
9670 /* Next character. */
9675 finished_with_string
:
9676 /* OK, we're ready to return. If we have a quoted string, and
9677 quoted_dollar_at is not set, we do no splitting at all; otherwise
9678 we split on ' '. The routines that call this will handle what to
9679 do if nothing has been expanded. */
9681 /* Partially and wholly quoted strings which expand to the empty
9682 string are retained as an empty arguments. Unquoted strings
9683 which expand to the empty string are discarded. The single
9684 exception is the case of expanding "$@" when there are no
9685 positional parameters. In that case, we discard the expansion. */
9687 /* Because of how the code that handles "" and '' in partially
9688 quoted strings works, we need to make ISTRING into a QUOTED_NULL
9689 if we saw quoting characters, but the expansion was empty.
9690 "" and '' are tossed away before we get to this point when
9691 processing partially quoted strings. This makes "" and $xxx""
9692 equivalent when xxx is unset. We also look to see whether we
9693 saw a quoted null from a ${} expansion and add one back if we
9696 /* If we expand to nothing and there were no single or double quotes
9697 in the word, we throw it away. Otherwise, we return a NULL word.
9698 The single exception is for $@ surrounded by double quotes when
9699 there are no positional parameters. In that case, we also throw
9702 if (*istring
== '\0')
9704 if (quoted_dollar_at
== 0 && (had_quoted_null
|| quoted_state
== PARTIALLY_QUOTED
))
9706 istring
[0] = CTLNUL
;
9708 tword
= make_bare_word (istring
);
9709 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
9710 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
9711 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
9712 tword
->flags
|= W_QUOTED
;
9714 /* According to sh, ksh, and Posix.2, if a word expands into nothing
9715 and a double-quoted "$@" appears anywhere in it, then the entire
9717 /* XXX - exception appears to be that quoted null strings result in
9719 else if (quoted_state
== UNQUOTED
|| quoted_dollar_at
)
9720 list
= (WORD_LIST
*)NULL
;
9724 tword
= make_bare_word (istring
);
9725 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
9726 tword
->flags
|= W_QUOTED
;
9727 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
9731 list
= (WORD_LIST
*)NULL
;
9734 else if (word
->flags
& W_NOSPLIT
)
9736 tword
= make_bare_word (istring
);
9737 if (word
->flags
& W_ASSIGNMENT
)
9738 tword
->flags
|= W_ASSIGNMENT
; /* XXX */
9739 if (word
->flags
& W_COMPASSIGN
)
9740 tword
->flags
|= W_COMPASSIGN
; /* XXX */
9741 if (word
->flags
& W_NOGLOB
)
9742 tword
->flags
|= W_NOGLOB
; /* XXX */
9743 if (word
->flags
& W_NOBRACE
)
9744 tword
->flags
|= W_NOBRACE
; /* XXX */
9745 if (word
->flags
& W_NOEXPAND
)
9746 tword
->flags
|= W_NOEXPAND
; /* XXX */
9747 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
9748 tword
->flags
|= W_QUOTED
;
9749 if (had_quoted_null
&& QUOTED_NULL (istring
))
9750 tword
->flags
|= W_HASQUOTEDNULL
;
9751 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
9758 ifs_chars
= (quoted_dollar_at
|| has_dollar_at
) ? ifs_value
: (char *)NULL
;
9760 /* If we have $@, we need to split the results no matter what. If
9761 IFS is unset or NULL, string_list_dollar_at has separated the
9762 positional parameters with a space, so we split on space (we have
9763 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
9764 string_list_dollar_at has separated the positional parameters
9765 with the first character of $IFS, so we split on $IFS. If
9766 SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either
9767 unset or null, and we want to make sure that we split on spaces
9768 regardless of what else has happened to IFS since the expansion,
9769 or we expanded "$@" with IFS null and we need to split the positional
9770 parameters into separate words. */
9771 if (split_on_spaces
)
9772 list
= list_string (istring
, " ", 1); /* XXX quoted == 1? */
9773 /* If we have $@ (has_dollar_at != 0) and we are in a context where we
9774 don't want to split the result (W_NOSPLIT2), and we are not quoted,
9775 we have already separated the arguments with the first character of
9776 $IFS. In this case, we want to return a list with a single word
9777 with the separator possibly replaced with a space (it's what other
9779 quoted_dollar_at is internal to this function and is set if we are
9780 passed an argument that is unquoted (quoted == 0) but we encounter a
9781 double-quoted $@ while expanding it. */
9782 else if (has_dollar_at
&& quoted_dollar_at
== 0 && ifs_chars
&& quoted
== 0 && (word
->flags
& W_NOSPLIT2
))
9784 /* Only split and rejoin if we have to */
9785 if (*ifs_chars
&& *ifs_chars
!= ' ')
9787 list
= list_string (istring
, *ifs_chars
? ifs_chars
: " ", 1);
9788 tstring
= string_list (list
);
9792 tword
= make_bare_word (tstring
);
9793 if (tstring
!= istring
)
9795 goto set_word_flags
;
9797 /* This is the attempt to make $* in an assignment context (a=$*) and
9798 array variables subscripted with * in an assignment context (a=${foo[*]})
9799 behave similarly. It has side effects that, though they increase
9800 compatibility with other shells, are not backwards compatible. */
9802 else if (has_dollar_at
&& quoted
== 0 && ifs_chars
&& (word
->flags
& W_ASSIGNRHS
))
9804 tword
= make_bare_word (istring
);
9805 goto set_word_flags
;
9808 else if (has_dollar_at
&& ifs_chars
)
9809 list
= list_string (istring
, *ifs_chars
? ifs_chars
: " ", 1);
9812 tword
= make_bare_word (istring
);
9814 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) || (quoted_state
== WHOLLY_QUOTED
))
9815 tword
->flags
|= W_QUOTED
;
9816 if (word
->flags
& W_ASSIGNMENT
)
9817 tword
->flags
|= W_ASSIGNMENT
;
9818 if (word
->flags
& W_COMPASSIGN
)
9819 tword
->flags
|= W_COMPASSIGN
;
9820 if (word
->flags
& W_NOGLOB
)
9821 tword
->flags
|= W_NOGLOB
;
9822 if (word
->flags
& W_NOBRACE
)
9823 tword
->flags
|= W_NOBRACE
;
9824 if (word
->flags
& W_NOEXPAND
)
9825 tword
->flags
|= W_NOEXPAND
;
9826 if (had_quoted_null
&& QUOTED_NULL (istring
))
9827 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
9828 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
9836 /* **************************************************************** */
9838 /* Functions for Quote Removal */
9840 /* **************************************************************** */
9842 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
9843 backslash quoting rules for within double quotes or a here document. */
9845 string_quote_removal (string
, quoted
)
9850 char *r
, *result_string
, *temp
, *send
;
9851 int sindex
, tindex
, dquote
;
9855 /* The result can be no longer than the original string. */
9856 slen
= strlen (string
);
9857 send
= string
+ slen
;
9859 r
= result_string
= (char *)xmalloc (slen
+ 1);
9861 for (dquote
= sindex
= 0; c
= string
[sindex
];)
9866 c
= string
[++sindex
];
9872 if (((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
) && (sh_syntaxtab
[c
] & CBSDQUOTE
) == 0)
9877 SCOPY_CHAR_M (r
, string
, send
, sindex
);
9881 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
)
9887 tindex
= sindex
+ 1;
9888 temp
= string_extract_single_quoted (string
, &tindex
);
9899 dquote
= 1 - dquote
;
9905 return (result_string
);
9910 /* Perform quote removal on word WORD. This allocates and returns a new
9913 word_quote_removal (word
, quoted
)
9920 t
= string_quote_removal (word
->word
, quoted
);
9921 w
= alloc_word_desc ();
9922 w
->word
= t
? t
: savestring ("");
9926 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
9927 the members of the list are treated as if they are surrounded by
9928 double quotes. Return a new list, or NULL if LIST is NULL. */
9930 word_list_quote_removal (list
, quoted
)
9934 WORD_LIST
*result
, *t
, *tresult
, *e
;
9936 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
9938 tresult
= make_word_list (word_quote_removal (t
->word
, quoted
), (WORD_LIST
*)NULL
);
9940 result
= (WORD_LIST
*) list_append (result
, tresult
);
9943 result
= e
= tresult
;
9956 /*******************************************
9958 * Functions to perform word splitting *
9960 *******************************************/
9970 ifs_value
= (v
&& value_cell (v
)) ? value_cell (v
) : " \t\n";
9972 ifs_is_set
= ifs_var
!= 0;
9973 ifs_is_null
= ifs_is_set
&& (*ifs_value
== 0);
9975 /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
9976 handle multibyte chars in IFS */
9977 memset (ifs_cmap
, '\0', sizeof (ifs_cmap
));
9978 for (t
= ifs_value
; t
&& *t
; t
++)
9984 #if defined (HANDLE_MULTIBYTE)
9987 ifs_firstc
[0] = '\0';
9993 ifs_len
= strnlen (ifs_value
, MB_CUR_MAX
);
9994 ifs_firstc_len
= MBLEN (ifs_value
, ifs_len
);
9995 if (ifs_firstc_len
== 1 || ifs_firstc_len
== 0 || MB_INVALIDCH (ifs_firstc_len
))
9997 ifs_firstc
[0] = ifs_value
[0];
9998 ifs_firstc
[1] = '\0';
10002 memcpy (ifs_firstc
, ifs_value
, ifs_firstc_len
);
10005 ifs_firstc
= ifs_value
? *ifs_value
: 0;
10015 /* This splits a single word into a WORD LIST on $IFS, but only if the word
10016 is not quoted. list_string () performs quote removal for us, even if we
10017 don't do any splitting. */
10019 word_split (w
, ifs_chars
)
10029 xifs
= ((w
->flags
& W_QUOTED
) || ifs_chars
== 0) ? "" : ifs_chars
;
10030 result
= list_string (w
->word
, xifs
, w
->flags
& W_QUOTED
);
10033 result
= (WORD_LIST
*)NULL
;
10038 /* Perform word splitting on LIST and return the RESULT. It is possible
10039 to return (WORD_LIST *)NULL. */
10041 word_list_split (list
)
10044 WORD_LIST
*result
, *t
, *tresult
, *e
;
10046 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
10048 tresult
= word_split (t
->word
, ifs_value
);
10050 result
= e
= tresult
;
10061 /**************************************************
10063 * Functions to expand an entire WORD_LIST *
10065 **************************************************/
10067 /* Do any word-expansion-specific cleanup and jump to top_level */
10069 exp_jump_to_top_level (v
)
10072 set_pipestatus_from_exit (last_command_exit_value
);
10074 /* Cleanup code goes here. */
10075 expand_no_split_dollar_star
= 0; /* XXX */
10076 expanding_redir
= 0;
10077 assigning_in_environment
= 0;
10079 if (parse_and_execute_level
== 0)
10080 top_level_cleanup (); /* from sig.c */
10082 jump_to_top_level (v
);
10085 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
10086 ELIST, and set ELIST to the new list. */
10087 #define PREPEND_LIST(nlist, elist) \
10088 do { nlist->next = elist; elist = nlist; } while (0)
10090 /* Separate out any initial variable assignments from TLIST. If set -k has
10091 been executed, remove all assignment statements from TLIST. Initial
10092 variable assignments and other environment assignments are placed
10093 on SUBST_ASSIGN_VARLIST. */
10095 separate_out_assignments (tlist
)
10098 register WORD_LIST
*vp
, *lp
;
10101 return ((WORD_LIST
*)NULL
);
10103 if (subst_assign_varlist
)
10104 dispose_words (subst_assign_varlist
); /* Clean up after previous error */
10106 subst_assign_varlist
= (WORD_LIST
*)NULL
;
10109 /* Separate out variable assignments at the start of the command.
10110 Loop invariant: vp->next == lp
10111 Loop postcondition:
10112 lp = list of words left after assignment statements skipped
10113 tlist = original list of words
10115 while (lp
&& (lp
->word
->flags
& W_ASSIGNMENT
))
10121 /* If lp != tlist, we have some initial assignment statements.
10122 We make SUBST_ASSIGN_VARLIST point to the list of assignment
10123 words and TLIST point to the remaining words. */
10126 subst_assign_varlist
= tlist
;
10127 /* ASSERT(vp->next == lp); */
10128 vp
->next
= (WORD_LIST
*)NULL
; /* terminate variable list */
10129 tlist
= lp
; /* remainder of word list */
10132 /* vp == end of variable list */
10133 /* tlist == remainder of original word list without variable assignments */
10135 /* All the words in tlist were assignment statements */
10136 return ((WORD_LIST
*)NULL
);
10138 /* ASSERT(tlist != NULL); */
10139 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
10141 /* If the -k option is in effect, we need to go through the remaining
10142 words, separate out the assignment words, and place them on
10143 SUBST_ASSIGN_VARLIST. */
10144 if (place_keywords_in_env
)
10146 WORD_LIST
*tp
; /* tp == running pointer into tlist */
10151 /* Loop Invariant: tp->next == lp */
10152 /* Loop postcondition: tlist == word list without assignment statements */
10155 if (lp
->word
->flags
& W_ASSIGNMENT
)
10157 /* Found an assignment statement, add this word to end of
10158 subst_assign_varlist (vp). */
10159 if (!subst_assign_varlist
)
10160 subst_assign_varlist
= vp
= lp
;
10167 /* Remove the word pointed to by LP from TLIST. */
10168 tp
->next
= lp
->next
;
10169 /* ASSERT(vp == lp); */
10170 lp
->next
= (WORD_LIST
*)NULL
;
10183 #define WEXP_VARASSIGN 0x001
10184 #define WEXP_BRACEEXP 0x002
10185 #define WEXP_TILDEEXP 0x004
10186 #define WEXP_PARAMEXP 0x008
10187 #define WEXP_PATHEXP 0x010
10189 /* All of the expansions, including variable assignments at the start of
10191 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
10193 /* All of the expansions except variable assignments at the start of
10195 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
10197 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
10198 expansion, command substitution, arithmetic expansion, word splitting, and
10200 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
10202 /* Take the list of words in LIST and do the various substitutions. Return
10203 a new list of words which is the expanded list, and without things like
10204 variable assignments. */
10207 expand_words (list
)
10210 return (expand_word_list_internal (list
, WEXP_ALL
));
10213 /* Same as expand_words (), but doesn't hack variable or environment
10216 expand_words_no_vars (list
)
10219 return (expand_word_list_internal (list
, WEXP_NOVARS
));
10223 expand_words_shellexp (list
)
10226 return (expand_word_list_internal (list
, WEXP_SHELLEXP
));
10230 glob_expand_word_list (tlist
, eflags
)
10234 char **glob_array
, *temp_string
;
10235 register int glob_index
;
10236 WORD_LIST
*glob_list
, *output_list
, *disposables
, *next
;
10239 output_list
= disposables
= (WORD_LIST
*)NULL
;
10240 glob_array
= (char **)NULL
;
10243 /* For each word, either globbing is attempted or the word is
10244 added to orig_list. If globbing succeeds, the results are
10245 added to orig_list and the word (tlist) is added to the list
10246 of disposable words. If globbing fails and failed glob
10247 expansions are left unchanged (the shell default), the
10248 original word is added to orig_list. If globbing fails and
10249 failed glob expansions are removed, the original word is
10250 added to the list of disposable words. orig_list ends up
10251 in reverse order and requires a call to REVERSE_LIST to
10252 be set right. After all words are examined, the disposable
10253 words are freed. */
10254 next
= tlist
->next
;
10256 /* If the word isn't an assignment and contains an unquoted
10257 pattern matching character, then glob it. */
10258 if ((tlist
->word
->flags
& W_NOGLOB
) == 0 &&
10259 unquoted_glob_pattern_p (tlist
->word
->word
))
10261 glob_array
= shell_glob_filename (tlist
->word
->word
);
10263 /* Handle error cases.
10264 I don't think we should report errors like "No such file
10265 or directory". However, I would like to report errors
10266 like "Read failed". */
10268 if (glob_array
== 0 || GLOB_FAILED (glob_array
))
10270 glob_array
= (char **)xmalloc (sizeof (char *));
10271 glob_array
[0] = (char *)NULL
;
10274 /* Dequote the current word in case we have to use it. */
10275 if (glob_array
[0] == NULL
)
10277 temp_string
= dequote_string (tlist
->word
->word
);
10278 free (tlist
->word
->word
);
10279 tlist
->word
->word
= temp_string
;
10282 /* Make the array into a word list. */
10283 glob_list
= (WORD_LIST
*)NULL
;
10284 for (glob_index
= 0; glob_array
[glob_index
]; glob_index
++)
10286 tword
= make_bare_word (glob_array
[glob_index
]);
10287 glob_list
= make_word_list (tword
, glob_list
);
10292 output_list
= (WORD_LIST
*)list_append (glob_list
, output_list
);
10293 PREPEND_LIST (tlist
, disposables
);
10295 else if (fail_glob_expansion
!= 0)
10297 last_command_exit_value
= EXECUTION_FAILURE
;
10298 report_error (_("no match: %s"), tlist
->word
->word
);
10299 exp_jump_to_top_level (DISCARD
);
10301 else if (allow_null_glob_expansion
== 0)
10303 /* Failed glob expressions are left unchanged. */
10304 PREPEND_LIST (tlist
, output_list
);
10308 /* Failed glob expressions are removed. */
10309 PREPEND_LIST (tlist
, disposables
);
10314 /* Dequote the string. */
10315 temp_string
= dequote_string (tlist
->word
->word
);
10316 free (tlist
->word
->word
);
10317 tlist
->word
->word
= temp_string
;
10318 PREPEND_LIST (tlist
, output_list
);
10321 strvec_dispose (glob_array
);
10322 glob_array
= (char **)NULL
;
10328 dispose_words (disposables
);
10331 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
10333 return (output_list
);
10336 #if defined (BRACE_EXPANSION)
10338 brace_expand_word_list (tlist
, eflags
)
10342 register char **expansions
;
10344 WORD_LIST
*disposables
, *output_list
, *next
;
10348 for (disposables
= output_list
= (WORD_LIST
*)NULL
; tlist
; tlist
= next
)
10350 next
= tlist
->next
;
10352 if (tlist
->word
->flags
& W_NOBRACE
)
10354 /*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/
10355 PREPEND_LIST (tlist
, output_list
);
10359 if ((tlist
->word
->flags
& (W_COMPASSIGN
|W_ASSIGNARG
)) == (W_COMPASSIGN
|W_ASSIGNARG
))
10361 /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
10362 PREPEND_LIST (tlist
, output_list
);
10366 /* Only do brace expansion if the word has a brace character. If
10367 not, just add the word list element to BRACES and continue. In
10368 the common case, at least when running shell scripts, this will
10369 degenerate to a bunch of calls to `mbschr', and then what is
10370 basically a reversal of TLIST into BRACES, which is corrected
10371 by a call to REVERSE_LIST () on BRACES when the end of TLIST
10373 if (mbschr (tlist
->word
->word
, LBRACE
))
10375 expansions
= brace_expand (tlist
->word
->word
);
10377 for (eindex
= 0; temp_string
= expansions
[eindex
]; eindex
++)
10379 w
= alloc_word_desc ();
10380 w
->word
= temp_string
;
10382 /* If brace expansion didn't change the word, preserve
10383 the flags. We may want to preserve the flags
10384 unconditionally someday -- XXX */
10385 if (STREQ (temp_string
, tlist
->word
->word
))
10386 w
->flags
= tlist
->word
->flags
;
10388 w
= make_word_flags (w
, temp_string
);
10390 output_list
= make_word_list (w
, output_list
);
10394 /* Add TLIST to the list of words to be freed after brace
10395 expansion has been performed. */
10396 PREPEND_LIST (tlist
, disposables
);
10399 PREPEND_LIST (tlist
, output_list
);
10403 dispose_words (disposables
);
10406 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
10408 return (output_list
);
10412 #if defined (ARRAY_VARS)
10413 /* Take WORD, a compound associative array assignment, and internally run
10414 'declare -A w', where W is the variable name portion of WORD. */
10416 make_internal_declare (word
, option
, cmd
)
10425 w
= make_word (word
);
10427 t
= assignment (w
->word
, 0);
10428 if (w
->word
[t
] == '=')
10431 if (w
->word
[t
- 1] == '+') /* cut off any append op */
10432 w
->word
[t
- 1] = '\0';
10435 wl
= make_word_list (w
, (WORD_LIST
*)NULL
);
10436 wl
= make_word_list (make_word (option
), wl
);
10438 r
= declare_builtin (wl
);
10440 dispose_words (wl
);
10446 shell_expand_word_list (tlist
, eflags
)
10450 WORD_LIST
*expanded
, *orig_list
, *new_list
, *next
, *temp_list
, *wcmd
;
10451 int expanded_something
, has_dollar_at
;
10454 /* We do tilde expansion all the time. This is what 1003.2 says. */
10455 new_list
= (WORD_LIST
*)NULL
;
10456 for (wcmd
= tlist
; wcmd
; wcmd
= wcmd
->next
)
10457 if (wcmd
->word
->flags
& W_ASSNBLTIN
)
10460 for (orig_list
= tlist
; tlist
; tlist
= next
)
10462 temp_string
= tlist
->word
->word
;
10464 next
= tlist
->next
;
10466 #if defined (ARRAY_VARS)
10467 /* If this is a compound array assignment to a builtin that accepts
10468 such assignments (e.g., `declare'), take the assignment and perform
10469 it separately, handling the semantics of declarations inside shell
10470 functions. This avoids the double-evaluation of such arguments,
10471 because `declare' does some evaluation of compound assignments on
10473 if ((tlist
->word
->flags
& (W_COMPASSIGN
|W_ASSIGNARG
)) == (W_COMPASSIGN
|W_ASSIGNARG
))
10476 char opts
[16], opti
;
10479 if (tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSNGLOBAL
|W_ASSIGNARRAY
))
10480 opts
[opti
++] = '-';
10482 if ((tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSNGLOBAL
)) == (W_ASSIGNASSOC
|W_ASSNGLOBAL
))
10484 opts
[opti
++] = 'g';
10485 opts
[opti
++] = 'A';
10487 else if (tlist
->word
->flags
& W_ASSIGNASSOC
)
10488 opts
[opti
++] = 'A';
10489 else if ((tlist
->word
->flags
& (W_ASSIGNARRAY
|W_ASSNGLOBAL
)) == (W_ASSIGNARRAY
|W_ASSNGLOBAL
))
10491 opts
[opti
++] = 'g';
10492 opts
[opti
++] = 'a';
10494 else if (tlist
->word
->flags
& W_ASSIGNARRAY
)
10495 opts
[opti
++] = 'a';
10496 else if (tlist
->word
->flags
& W_ASSNGLOBAL
)
10497 opts
[opti
++] = 'g';
10499 /* If we have special handling note the integer attribute and others
10500 that transform the value upon assignment. What we do is take all
10501 of the option arguments and scan through them looking for options
10502 that cause such transformations, and add them to the `opts' array. */
10503 /* if (opti > 0) */
10509 memset (omap
, '\0', sizeof (omap
));
10510 for (l
= orig_list
->next
; l
!= tlist
; l
= l
->next
)
10512 if (l
->word
->word
[0] != '-')
10513 break; /* non-option argument */
10514 if (l
->word
->word
[0] == '-' && l
->word
->word
[1] == '-' && l
->word
->word
[2] == 0)
10515 break; /* -- signals end of options */
10516 for (oind
= 1; l
->word
->word
[oind
]; oind
++)
10517 switch (l
->word
->word
[oind
])
10523 omap
[l
->word
->word
[oind
]] = 1;
10525 opts
[opti
++] = '-';
10532 for (oind
= 0; oind
< sizeof (omap
); oind
++)
10534 opts
[opti
++] = oind
;
10540 t
= make_internal_declare (tlist
->word
->word
, opts
, wcmd
? wcmd
->word
->word
: (char *)0);
10541 if (t
!= EXECUTION_SUCCESS
)
10543 last_command_exit_value
= t
;
10544 exp_jump_to_top_level (DISCARD
);
10548 t
= do_word_assignment (tlist
->word
, 0);
10551 last_command_exit_value
= EXECUTION_FAILURE
;
10552 exp_jump_to_top_level (DISCARD
);
10555 /* Now transform the word as ksh93 appears to do and go on */
10556 t
= assignment (tlist
->word
->word
, 0);
10557 tlist
->word
->word
[t
] = '\0';
10558 if (tlist
->word
->word
[t
- 1] == '+')
10559 tlist
->word
->word
[t
- 1] = '\0'; /* cut off append op */
10560 tlist
->word
->flags
&= ~(W_ASSIGNMENT
|W_NOSPLIT
|W_COMPASSIGN
|W_ASSIGNARG
|W_ASSIGNASSOC
|W_ASSIGNARRAY
);
10564 expanded_something
= 0;
10565 expanded
= expand_word_internal
10566 (tlist
->word
, 0, 0, &has_dollar_at
, &expanded_something
);
10568 if (expanded
== &expand_word_error
|| expanded
== &expand_word_fatal
)
10570 /* By convention, each time this error is returned,
10571 tlist->word->word has already been freed. */
10572 tlist
->word
->word
= (char *)NULL
;
10574 /* Dispose our copy of the original list. */
10575 dispose_words (orig_list
);
10576 /* Dispose the new list we're building. */
10577 dispose_words (new_list
);
10579 last_command_exit_value
= EXECUTION_FAILURE
;
10580 if (expanded
== &expand_word_error
)
10581 exp_jump_to_top_level (DISCARD
);
10583 exp_jump_to_top_level (FORCE_EOF
);
10586 /* Don't split words marked W_NOSPLIT. */
10587 if (expanded_something
&& (tlist
->word
->flags
& W_NOSPLIT
) == 0)
10589 temp_list
= word_list_split (expanded
);
10590 dispose_words (expanded
);
10594 /* If no parameter expansion, command substitution, process
10595 substitution, or arithmetic substitution took place, then
10596 do not do word splitting. We still have to remove quoted
10597 null characters from the result. */
10598 word_list_remove_quoted_nulls (expanded
);
10599 temp_list
= expanded
;
10602 expanded
= REVERSE_LIST (temp_list
, WORD_LIST
*);
10603 new_list
= (WORD_LIST
*)list_append (expanded
, new_list
);
10607 dispose_words (orig_list
);
10610 new_list
= REVERSE_LIST (new_list
, WORD_LIST
*);
10615 /* The workhorse for expand_words () and expand_words_no_vars ().
10616 First arg is LIST, a WORD_LIST of words.
10617 Second arg EFLAGS is a flags word controlling which expansions are
10620 This does all of the substitutions: brace expansion, tilde expansion,
10621 parameter expansion, command substitution, arithmetic expansion,
10622 process substitution, word splitting, and pathname expansion, according
10623 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
10624 set, or for which no expansion is done, do not undergo word splitting.
10625 Words with the W_NOGLOB bit set do not undergo pathname expansion; words
10626 with W_NOBRACE set do not undergo brace expansion (see
10627 brace_expand_word_list above). */
10629 expand_word_list_internal (list
, eflags
)
10633 WORD_LIST
*new_list
, *temp_list
;
10637 tempenv_assign_error
= 0;
10639 return ((WORD_LIST
*)NULL
);
10641 garglist
= new_list
= copy_word_list (list
);
10642 if (eflags
& WEXP_VARASSIGN
)
10644 garglist
= new_list
= separate_out_assignments (new_list
);
10647 if (subst_assign_varlist
)
10649 /* All the words were variable assignments, so they are placed
10650 into the shell's environment. */
10651 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
10653 savecmd
= this_command_name
;
10654 this_command_name
= (char *)NULL
; /* no arithmetic errors */
10655 tint
= do_word_assignment (temp_list
->word
, 0);
10656 this_command_name
= savecmd
;
10657 /* Variable assignment errors in non-interactive shells
10658 running in Posix.2 mode cause the shell to exit. */
10661 last_command_exit_value
= EXECUTION_FAILURE
;
10662 if (interactive_shell
== 0 && posixly_correct
)
10663 exp_jump_to_top_level (FORCE_EOF
);
10665 exp_jump_to_top_level (DISCARD
);
10668 dispose_words (subst_assign_varlist
);
10669 subst_assign_varlist
= (WORD_LIST
*)NULL
;
10671 return ((WORD_LIST
*)NULL
);
10675 /* Begin expanding the words that remain. The expansions take place on
10676 things that aren't really variable assignments. */
10678 #if defined (BRACE_EXPANSION)
10679 /* Do brace expansion on this word if there are any brace characters
10681 if ((eflags
& WEXP_BRACEEXP
) && brace_expansion
&& new_list
)
10682 new_list
= brace_expand_word_list (new_list
, eflags
);
10683 #endif /* BRACE_EXPANSION */
10685 /* Perform the `normal' shell expansions: tilde expansion, parameter and
10686 variable substitution, command substitution, arithmetic expansion,
10687 and word splitting. */
10688 new_list
= shell_expand_word_list (new_list
, eflags
);
10690 /* Okay, we're almost done. Now let's just do some filename
10694 if ((eflags
& WEXP_PATHEXP
) && disallow_filename_globbing
== 0)
10695 /* Glob expand the word list unless globbing has been disabled. */
10696 new_list
= glob_expand_word_list (new_list
, eflags
);
10698 /* Dequote the words, because we're not performing globbing. */
10699 new_list
= dequote_list (new_list
);
10702 if ((eflags
& WEXP_VARASSIGN
) && subst_assign_varlist
)
10704 sh_wassign_func_t
*assign_func
;
10705 int is_special_builtin
, is_builtin_or_func
;
10707 /* If the remainder of the words expand to nothing, Posix.2 requires
10708 that the variable and environment assignments affect the shell's
10710 assign_func
= new_list
? assign_in_env
: do_word_assignment
;
10711 tempenv_assign_error
= 0;
10713 is_builtin_or_func
= (new_list
&& new_list
->word
&& (find_shell_builtin (new_list
->word
->word
) || find_function (new_list
->word
->word
)));
10714 /* Posix says that special builtins exit if a variable assignment error
10715 occurs in an assignment preceding it. */
10716 is_special_builtin
= (posixly_correct
&& new_list
&& new_list
->word
&& find_special_builtin (new_list
->word
->word
));
10718 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
10720 savecmd
= this_command_name
;
10721 this_command_name
= (char *)NULL
;
10722 assigning_in_environment
= (assign_func
== assign_in_env
);
10723 tint
= (*assign_func
) (temp_list
->word
, is_builtin_or_func
);
10724 assigning_in_environment
= 0;
10725 this_command_name
= savecmd
;
10726 /* Variable assignment errors in non-interactive shells running
10727 in Posix.2 mode cause the shell to exit. */
10730 if (assign_func
== do_word_assignment
)
10732 last_command_exit_value
= EXECUTION_FAILURE
;
10733 if (interactive_shell
== 0 && posixly_correct
&& is_special_builtin
)
10734 exp_jump_to_top_level (FORCE_EOF
);
10736 exp_jump_to_top_level (DISCARD
);
10739 tempenv_assign_error
++;
10743 dispose_words (subst_assign_varlist
);
10744 subst_assign_varlist
= (WORD_LIST
*)NULL
;