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-2013 Free Software Foundation, Inc.
9 This file is part of GNU Bash, the Bourne Again SHell.
11 Bash is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 Bash is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with Bash. If not, see <http://www.gnu.org/licenses/>.
27 #include "bashtypes.h"
29 #include "chartypes.h"
30 #if defined (HAVE_PWD_H)
36 #if defined (HAVE_UNISTD_H)
41 #include "posixstat.h"
48 #include "execute_cmd.h"
52 #include "mailcheck.h"
57 #include "builtins/getopt.h"
58 #include "builtins/common.h"
60 #include "builtins/builtext.h"
62 #include <tilde/tilde.h>
63 #include <glob/strmatch.h>
69 /* The size that strings change by. */
70 #define DEFAULT_INITIAL_ARRAY_SIZE 112
71 #define DEFAULT_ARRAY_SIZE 128
77 #define VT_ARRAYMEMBER 3
80 #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */
82 /* Flags for quoted_strchr */
83 #define ST_BACKSL 0x01
84 #define ST_CTLESC 0x02
85 #define ST_SQUOTE 0x04 /* unused yet */
86 #define ST_DQUOTE 0x08 /* unused yet */
88 /* Flags for the `pflags' argument to param_expand() */
89 #define PF_NOCOMSUB 0x01 /* Do not perform command substitution */
90 #define PF_IGNUNBOUND 0x02 /* ignore unbound vars even if -u set */
91 #define PF_NOSPLIT2 0x04 /* same as W_NOSPLIT2 */
92 #define PF_ASSIGNRHS 0x08 /* same as W_ASSIGNRHS */
94 /* These defs make it easier to use the editor. */
102 #if defined (HANDLE_MULTIBYTE)
107 /* Evaluates to 1 if C is one of the shell's special parameters whose length
108 can be taken, but is also one of the special expansion characters. */
109 #define VALID_SPECIAL_LENGTH_PARAM(c) \
110 ((c) == '-' || (c) == '?' || (c) == '#')
112 /* Evaluates to 1 if C is one of the shell's special parameters for which an
113 indirect variable reference may be made. */
114 #define VALID_INDIR_PARAM(c) \
115 ((posixly_correct == 0 && (c) == '#') || (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*')
117 /* Evaluates to 1 if C is one of the OP characters that follows the parameter
118 in ${parameter[:]OPword}. */
119 #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
121 /* Evaluates to 1 if this is one of the shell's special variables. */
122 #define SPECIAL_VAR(name, wi) \
123 ((DIGIT (*name) && all_digits (name)) || \
124 (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
125 (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))
127 /* An expansion function that takes a string and a quoted flag and returns
128 a WORD_LIST *. Used as the type of the third argument to
129 expand_string_if_necessary(). */
130 typedef WORD_LIST
*EXPFUNC
__P((char *, int));
132 /* Process ID of the last command executed within command substitution. */
133 pid_t last_command_subst_pid
= NO_PID
;
134 pid_t current_command_subst_pid
= NO_PID
;
136 /* Variables used to keep track of the characters in IFS. */
139 unsigned char ifs_cmap
[UCHAR_MAX
+ 1];
140 int ifs_is_set
, ifs_is_null
;
142 #if defined (HANDLE_MULTIBYTE)
143 unsigned char ifs_firstc
[MB_LEN_MAX
];
144 size_t ifs_firstc_len
;
146 unsigned char ifs_firstc
;
149 /* Sentinel to tell when we are performing variable assignments preceding a
150 command name and putting them into the environment. Used to make sure
151 we use the temporary environment when looking up variable values. */
152 int assigning_in_environment
;
154 /* Used to hold a list of variable assignments preceding a command. Global
155 so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
156 SIGCHLD trap and so it can be saved and restored by the trap handlers. */
157 WORD_LIST
*subst_assign_varlist
= (WORD_LIST
*)NULL
;
159 /* Extern functions and variables from different files. */
160 extern int last_command_exit_value
, last_command_exit_signal
;
161 extern int subshell_environment
, line_number
;
162 extern int subshell_level
, parse_and_execute_level
, sourcelevel
;
163 extern int eof_encountered
;
164 extern int return_catch_flag
, return_catch_value
;
165 extern pid_t dollar_dollar_pid
;
166 extern int posixly_correct
;
167 extern char *this_command_name
;
168 extern struct fd_bitmap
*current_fds_to_close
;
169 extern int wordexp_only
;
170 extern int expanding_redir
;
171 extern int tempenv_assign_error
;
172 extern int builtin_ignoring_errexit
;
174 #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
175 extern wchar_t *wcsdup
__P((const wchar_t *));
178 /* Non-zero means to allow unmatched globbed filenames to expand to
180 int allow_null_glob_expansion
;
182 /* Non-zero means to throw an error when globbing fails to match anything. */
183 int fail_glob_expansion
;
186 /* Variables to keep track of which words in an expanded word list (the
187 output of expand_word_list_internal) are the result of globbing
188 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
189 (CURRENTLY UNUSED). */
190 char *glob_argv_flags
;
191 static int glob_argv_flags_size
;
194 static WORD_LIST expand_word_error
, expand_word_fatal
;
195 static WORD_DESC expand_wdesc_error
, expand_wdesc_fatal
;
196 static char expand_param_error
, expand_param_fatal
;
197 static char extract_string_error
, extract_string_fatal
;
199 /* Tell the expansion functions to not longjmp back to top_level on fatal
200 errors. Enabled when doing completion and prompt string expansion. */
201 static int no_longjmp_on_fatal_error
= 0;
203 /* Set by expand_word_unsplit; used to inhibit splitting and re-joining
204 $* on $IFS, primarily when doing assignment statements. */
205 static int expand_no_split_dollar_star
= 0;
207 /* A WORD_LIST of words to be expanded by expand_word_list_internal,
208 without any leading variable assignments. */
209 static WORD_LIST
*garglist
= (WORD_LIST
*)NULL
;
211 static char *quoted_substring
__P((char *, int, int));
212 static int quoted_strlen
__P((char *));
213 static char *quoted_strchr
__P((char *, int, int));
215 static char *expand_string_if_necessary
__P((char *, int, EXPFUNC
*));
216 static inline char *expand_string_to_string_internal
__P((char *, int, EXPFUNC
*));
217 static WORD_LIST
*call_expand_word_internal
__P((WORD_DESC
*, int, int, int *, int *));
218 static WORD_LIST
*expand_string_internal
__P((char *, int));
219 static WORD_LIST
*expand_string_leave_quoted
__P((char *, int));
220 static WORD_LIST
*expand_string_for_rhs
__P((char *, int, int *, int *));
222 static WORD_LIST
*list_quote_escapes
__P((WORD_LIST
*));
223 static WORD_LIST
*list_dequote_escapes
__P((WORD_LIST
*));
225 static char *make_quoted_char
__P((int));
226 static WORD_LIST
*quote_list
__P((WORD_LIST
*));
228 static int unquoted_substring
__P((char *, char *));
229 static int unquoted_member
__P((int, char *));
231 #if defined (ARRAY_VARS)
232 static SHELL_VAR
*do_compound_assignment
__P((char *, char *, int));
234 static int do_assignment_internal
__P((const WORD_DESC
*, int));
236 static char *string_extract_verbatim
__P((char *, size_t, int *, char *, int));
237 static char *string_extract
__P((char *, int *, char *, int));
238 static char *string_extract_double_quoted
__P((char *, int *, int));
239 static inline char *string_extract_single_quoted
__P((char *, int *));
240 static inline int skip_single_quoted
__P((const char *, size_t, int));
241 static int skip_double_quoted
__P((char *, size_t, int));
242 static char *extract_delimited_string
__P((char *, int *, char *, char *, char *, int));
243 static char *extract_dollar_brace_string
__P((char *, int *, int, int));
244 static int skip_matched_pair
__P((const char *, int, int, int, int));
246 static char *pos_params
__P((char *, int, int, int));
248 static unsigned char *mb_getcharlens
__P((char *, int));
250 static char *remove_upattern
__P((char *, char *, int));
251 #if defined (HANDLE_MULTIBYTE)
252 static wchar_t *remove_wpattern
__P((wchar_t *, size_t, wchar_t *, int));
254 static char *remove_pattern
__P((char *, char *, int));
256 static int match_upattern
__P((char *, char *, int, char **, char **));
257 #if defined (HANDLE_MULTIBYTE)
258 static int match_wpattern
__P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
260 static int match_pattern
__P((char *, char *, int, char **, char **));
261 static int getpatspec
__P((int, char *));
262 static char *getpattern
__P((char *, int, int));
263 static char *variable_remove_pattern
__P((char *, char *, int, int));
264 static char *list_remove_pattern
__P((WORD_LIST
*, char *, int, int, int));
265 static char *parameter_list_remove_pattern
__P((int, char *, int, int));
267 static char *array_remove_pattern
__P((SHELL_VAR
*, char *, int, char *, int));
269 static char *parameter_brace_remove_pattern
__P((char *, char *, int, char *, int, int, int));
271 static char *process_substitute
__P((char *, int));
273 static char *read_comsub
__P((int, int, int *));
276 static arrayind_t array_length_reference
__P((char *));
279 static int valid_brace_expansion_word
__P((char *, int));
280 static int chk_atstar
__P((char *, int, int *, int *));
281 static int chk_arithsub
__P((const char *, int));
283 static WORD_DESC
*parameter_brace_expand_word
__P((char *, int, int, int, arrayind_t
*));
284 static char *parameter_brace_find_indir
__P((char *, int, int, int));
285 static WORD_DESC
*parameter_brace_expand_indir
__P((char *, int, int, int *, int *));
286 static WORD_DESC
*parameter_brace_expand_rhs
__P((char *, char *, int, int, int *, int *));
287 static void parameter_brace_expand_error
__P((char *, char *));
289 static int valid_length_expression
__P((char *));
290 static intmax_t parameter_brace_expand_length
__P((char *));
292 static char *skiparith
__P((char *, int));
293 static int verify_substring_values
__P((SHELL_VAR
*, char *, char *, int, intmax_t *, intmax_t *));
294 static int get_var_and_type
__P((char *, char *, arrayind_t
, int, int, SHELL_VAR
**, char **));
295 static char *mb_substring
__P((char *, int, int));
296 static char *parameter_brace_substring
__P((char *, char *, int, char *, int, int));
298 static int shouldexp_replacement
__P((char *));
300 static char *pos_params_pat_subst
__P((char *, char *, char *, int));
302 static char *parameter_brace_patsub
__P((char *, char *, int, char *, int, int));
304 static char *pos_params_casemod
__P((char *, char *, int, int));
305 static char *parameter_brace_casemod
__P((char *, char *, int, int, char *, int, int));
307 static WORD_DESC
*parameter_brace_expand
__P((char *, int *, int, int, int *, int *));
308 static WORD_DESC
*param_expand
__P((char *, int *, int, int *, int *, int *, int *, int));
310 static WORD_LIST
*expand_word_internal
__P((WORD_DESC
*, int, int, int *, int *));
312 static WORD_LIST
*word_list_split
__P((WORD_LIST
*));
314 static void exp_jump_to_top_level
__P((int));
316 static WORD_LIST
*separate_out_assignments
__P((WORD_LIST
*));
317 static WORD_LIST
*glob_expand_word_list
__P((WORD_LIST
*, int));
318 #ifdef BRACE_EXPANSION
319 static WORD_LIST
*brace_expand_word_list
__P((WORD_LIST
*, int));
321 #if defined (ARRAY_VARS)
322 static int make_internal_declare
__P((char *, char *));
324 static WORD_LIST
*shell_expand_word_list
__P((WORD_LIST
*, int));
325 static WORD_LIST
*expand_word_list_internal
__P((WORD_LIST
*, int));
327 /* **************************************************************** */
329 /* Utility Functions */
331 /* **************************************************************** */
335 dump_word_flags (flags
)
341 fprintf (stderr
, "%d -> ", f
);
345 fprintf (stderr
, "W_ARRAYIND%s", f
? "|" : "");
347 if (f
& W_ASSIGNASSOC
)
350 fprintf (stderr
, "W_ASSIGNASSOC%s", f
? "|" : "");
352 if (f
& W_ASSIGNARRAY
)
355 fprintf (stderr
, "W_ASSIGNARRAY%s", f
? "|" : "");
360 fprintf (stderr
, "W_HASCTLESC%s", f
? "|" : "");
365 fprintf (stderr
, "W_NOPROCSUB%s", f
? "|" : "");
370 fprintf (stderr
, "W_DQUOTE%s", f
? "|" : "");
372 if (f
& W_HASQUOTEDNULL
)
374 f
&= ~W_HASQUOTEDNULL
;
375 fprintf (stderr
, "W_HASQUOTEDNULL%s", f
? "|" : "");
380 fprintf (stderr
, "W_ASSIGNARG%s", f
? "|" : "");
385 fprintf (stderr
, "W_ASSNBLTIN%s", f
? "|" : "");
387 if (f
& W_ASSNGLOBAL
)
390 fprintf (stderr
, "W_ASSNGLOBAL%s", f
? "|" : "");
395 fprintf (stderr
, "W_ASSIGNINT%s", f
? "|" : "");
397 if (f
& W_COMPASSIGN
)
400 fprintf (stderr
, "W_COMPASSIGN%s", f
? "|" : "");
405 fprintf (stderr
, "W_NOEXPAND%s", f
? "|" : "");
410 fprintf (stderr
, "W_ITILDE%s", f
? "|" : "");
415 fprintf (stderr
, "W_NOTILDE%s", f
? "|" : "");
420 fprintf (stderr
, "W_ASSIGNRHS%s", f
? "|" : "");
425 fprintf (stderr
, "W_NOCOMSUB%s", f
? "|" : "");
427 if (f
& W_DOLLARSTAR
)
430 fprintf (stderr
, "W_DOLLARSTAR%s", f
? "|" : "");
435 fprintf (stderr
, "W_DOLLARAT%s", f
? "|" : "");
440 fprintf (stderr
, "W_TILDEEXP%s", f
? "|" : "");
445 fprintf (stderr
, "W_NOSPLIT2%s", f
? "|" : "");
450 fprintf (stderr
, "W_NOSPLIT%s", f
? "|" : "");
455 fprintf (stderr
, "W_NOBRACE%s", f
? "|" : "");
460 fprintf (stderr
, "W_NOGLOB%s", f
? "|" : "");
462 if (f
& W_SPLITSPACE
)
465 fprintf (stderr
, "W_SPLITSPACE%s", f
? "|" : "");
467 if (f
& W_ASSIGNMENT
)
470 fprintf (stderr
, "W_ASSIGNMENT%s", f
? "|" : "");
475 fprintf (stderr
, "W_QUOTED%s", f
? "|" : "");
480 fprintf (stderr
, "W_HASDOLLAR%s", f
? "|" : "");
482 fprintf (stderr
, "\n");
487 #ifdef INCLUDE_UNUSED
489 quoted_substring (string
, start
, end
)
494 register char *result
, *s
, *r
;
498 /* Move to string[start], skipping quoted characters. */
499 for (s
= string
, l
= 0; *s
&& l
< start
; )
511 r
= result
= (char *)xmalloc (2*len
+ 1); /* save room for quotes */
513 /* Copy LEN characters, including quote characters. */
515 for (l
= 0; l
< len
; s
++)
529 #ifdef INCLUDE_UNUSED
530 /* Return the length of S, skipping over quoted characters */
554 /* Find the first occurrence of character C in string S, obeying shell
555 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
556 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
557 escaped with CTLESC are skipped. */
559 quoted_strchr (s
, c
, flags
)
567 if (((flags
& ST_BACKSL
) && *p
== '\\')
568 || ((flags
& ST_CTLESC
) && *p
== CTLESC
))
572 return ((char *)NULL
);
578 return ((char *)NULL
);
581 /* Return 1 if CHARACTER appears in an unquoted portion of
582 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
584 unquoted_member (character
, string
)
592 slen
= strlen (string
);
594 while (c
= string
[sindex
])
602 ADVANCE_CHAR (string
, slen
, sindex
);
608 ADVANCE_CHAR (string
, slen
, sindex
);
612 sindex
= skip_single_quoted (string
, slen
, ++sindex
);
616 sindex
= skip_double_quoted (string
, slen
, ++sindex
);
623 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
625 unquoted_substring (substr
, string
)
626 char *substr
, *string
;
629 int sindex
, c
, sublen
;
632 if (substr
== 0 || *substr
== '\0')
635 slen
= strlen (string
);
636 sublen
= strlen (substr
);
637 for (sindex
= 0; c
= string
[sindex
]; )
639 if (STREQN (string
+ sindex
, substr
, sublen
))
647 ADVANCE_CHAR (string
, slen
, sindex
);
651 sindex
= skip_single_quoted (string
, slen
, ++sindex
);
655 sindex
= skip_double_quoted (string
, slen
, ++sindex
);
659 ADVANCE_CHAR (string
, slen
, sindex
);
666 /* Most of the substitutions must be done in parallel. In order
667 to avoid using tons of unclear goto's, I have some functions
668 for manipulating malloc'ed strings. They all take INDX, a
669 pointer to an integer which is the offset into the string
670 where manipulation is taking place. They also take SIZE, a
671 pointer to an integer which is the current length of the
672 character array for this string. */
674 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
675 of space allocated to TARGET. SOURCE can be NULL, in which
676 case nothing happens. Gets rid of SOURCE by freeing it.
677 Returns TARGET in case the location has changed. */
679 sub_append_string (source
, target
, indx
, size
)
680 char *source
, *target
;
687 srclen
= STRLEN (source
);
688 if (srclen
>= (int)(*size
- *indx
))
691 n
= (n
+ DEFAULT_ARRAY_SIZE
) - (n
% DEFAULT_ARRAY_SIZE
);
692 target
= (char *)xrealloc (target
, (*size
= n
));
695 FASTCOPY (source
, target
+ *indx
, srclen
);
697 target
[*indx
] = '\0';
706 /* Append the textual representation of NUMBER to TARGET.
707 INDX and SIZE are as in SUB_APPEND_STRING. */
709 sub_append_number (number
, target
, indx
, size
)
716 temp
= itos (number
);
717 return (sub_append_string (temp
, target
, indx
, size
));
721 /* Extract a substring from STRING, starting at SINDEX and ending with
722 one of the characters in CHARLIST. Don't make the ending character
723 part of the string. Leave SINDEX pointing at the ending character.
724 Understand about backslashes in the string. If (flags & SX_VARNAME)
725 is non-zero, and array variables have been compiled into the shell,
726 everything between a `[' and a corresponding `]' is skipped over.
727 If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
728 update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
729 contain a closing character from CHARLIST. */
731 string_extract (string
, sindex
, charlist
, flags
)
743 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 0;
746 while (c
= string
[i
])
755 #if defined (ARRAY_VARS)
756 else if ((flags
& SX_VARNAME
) && c
== '[')
759 /* If this is an array subscript, skip over it and continue. */
760 ni
= skipsubscript (string
, i
, 0);
761 if (string
[ni
] == ']')
765 else if (MEMBER (c
, charlist
))
771 ADVANCE_CHAR (string
, slen
, i
);
774 /* If we had to have a matching delimiter and didn't find one, return an
775 error and let the caller deal with it. */
776 if ((flags
& SX_REQMATCH
) && found
== 0)
779 return (&extract_string_error
);
782 temp
= (flags
& SX_NOALLOC
) ? (char *)NULL
: substring (string
, *sindex
, i
);
788 /* Extract the contents of STRING as if it is enclosed in double quotes.
789 SINDEX, when passed in, is the offset of the character immediately
790 following the opening double quote; on exit, SINDEX is left pointing after
791 the closing double quote. If STRIPDQ is non-zero, unquoted double
792 quotes are stripped and the string is terminated by a null byte.
793 Backslashes between the embedded double quotes are processed. If STRIPDQ
794 is zero, an unquoted `"' terminates the string. */
796 string_extract_double_quoted (string
, sindex
, stripdq
)
798 int *sindex
, stripdq
;
804 char *temp
, *ret
; /* The new string we return. */
805 int pass_next
, backquote
, si
; /* State variables for the machine. */
809 slen
= strlen (string
+ *sindex
) + *sindex
;
810 send
= string
+ slen
;
812 pass_next
= backquote
= dquote
= 0;
813 temp
= (char *)xmalloc (1 + slen
- *sindex
);
817 while (c
= string
[i
])
819 /* Process a character that was quoted by a backslash. */
822 /* XXX - take another look at this in light of Interp 221 */
825 ``The backslash shall retain its special meaning as an escape
826 character only when followed by one of the characters:
829 If STRIPDQ is zero, we handle the double quotes here and let
830 expand_word_internal handle the rest. If STRIPDQ is non-zero,
831 we have already been through one round of backslash stripping,
832 and want to strip these backslashes only if DQUOTE is non-zero,
833 indicating that we are inside an embedded double-quoted string. */
835 /* If we are in an embedded quoted string, then don't strip
836 backslashes before characters for which the backslash
837 retains its special meaning, but remove backslashes in
838 front of other characters. If we are not in an
839 embedded quoted string, don't strip backslashes at all.
840 This mess is necessary because the string was already
841 surrounded by double quotes (and sh has some really weird
843 The returned string will be run through expansion as if
844 it were double-quoted. */
845 if ((stripdq
== 0 && c
!= '"') ||
846 (stripdq
&& ((dquote
&& (sh_syntaxtab
[c
] & CBSDQUOTE
)) || dquote
== 0)))
851 COPY_CHAR_I (temp
, j
, string
, send
, i
);
855 /* A backslash protects the next character. The code just above
856 handles preserving the backslash in front of any character but
865 /* Inside backquotes, ``the portion of the quoted string from the
866 initial backquote and the characters up to the next backquote
867 that is not preceded by a backslash, having escape characters
868 removed, defines that command''. */
886 /* Pass everything between `$(' and the matching `)' or a quoted
887 ${ ... } pair through according to the Posix.2 specification. */
888 if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
893 if (string
[i
+ 1] == LPAREN
)
894 ret
= extract_command_subst (string
, &si
, 0);
896 ret
= extract_dollar_brace_string (string
, &si
, Q_DOUBLE_QUOTES
, 0);
899 temp
[j
++] = string
[i
+ 1];
901 /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
903 if (ret
== 0 && no_longjmp_on_fatal_error
)
906 ret
= string
+ i
+ 2;
909 for (t
= 0; ret
[t
]; t
++, j
++)
911 temp
[j
] = string
[si
];
926 /* Add any character but a double quote to the quoted string we're
929 goto add_one_character
;
943 /* Point to after the closing quote. */
951 /* This should really be another option to string_extract_double_quoted. */
953 skip_double_quoted (string
, slen
, sind
)
960 int pass_next
, backquote
, si
;
963 pass_next
= backquote
= 0;
965 while (c
= string
[i
])
970 ADVANCE_CHAR (string
, slen
, i
);
983 ADVANCE_CHAR (string
, slen
, i
);
992 else if (c
== '$' && ((string
[i
+ 1] == LPAREN
) || (string
[i
+ 1] == LBRACE
)))
995 if (string
[i
+ 1] == LPAREN
)
996 ret
= extract_command_subst (string
, &si
, SX_NOALLOC
);
998 ret
= extract_dollar_brace_string (string
, &si
, Q_DOUBLE_QUOTES
, SX_NOALLOC
);
1005 ADVANCE_CHAR (string
, slen
, i
);
1018 /* Extract the contents of STRING as if it is enclosed in single quotes.
1019 SINDEX, when passed in, is the offset of the character immediately
1020 following the opening single quote; on exit, SINDEX is left pointing after
1021 the closing single quote. */
1022 static inline char *
1023 string_extract_single_quoted (string
, sindex
)
1032 /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
1033 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 0;
1035 while (string
[i
] && string
[i
] != '\'')
1036 ADVANCE_CHAR (string
, slen
, i
);
1038 t
= substring (string
, *sindex
, i
);
1048 skip_single_quoted (string
, slen
, sind
)
1057 while (string
[c
] && string
[c
] != '\'')
1058 ADVANCE_CHAR (string
, slen
, c
);
1065 /* Just like string_extract, but doesn't hack backslashes or any of
1066 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
1068 string_extract_verbatim (string
, slen
, sindex
, charlist
, flags
)
1076 #if defined (HANDLE_MULTIBYTE)
1084 if (charlist
[0] == '\'' && charlist
[1] == '\0')
1086 temp
= string_extract_single_quoted (string
, sindex
);
1087 --*sindex
; /* leave *sindex at separator character */
1093 /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need
1094 this only if MB_CUR_MAX > 1. */
1095 slen
= (MB_CUR_MAX
> 1) ? strlen (string
+ *sindex
) + *sindex
: 1;
1097 #if defined (HANDLE_MULTIBYTE)
1098 clen
= strlen (charlist
);
1101 while (c
= string
[i
])
1103 #if defined (HANDLE_MULTIBYTE)
1106 if ((flags
& SX_NOCTLESC
) == 0 && c
== CTLESC
)
1111 /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
1112 through, to protect the CTLNULs from later calls to
1113 remove_quoted_nulls. */
1114 else if ((flags
& SX_NOESCCTLNUL
) == 0 && c
== CTLESC
&& string
[i
+1] == CTLNUL
)
1120 #if defined (HANDLE_MULTIBYTE)
1121 mblength
= MBLEN (string
+ i
, slen
- i
);
1125 mblength
= mbtowc (&wc
, string
+ i
, slen
- i
);
1126 if (MB_INVALIDCH (mblength
))
1128 if (MEMBER (c
, charlist
))
1136 len
= mbstowcs (wcharlist
, charlist
, 0);
1139 wcharlist
= (wchar_t *)xmalloc (sizeof (wchar_t) * (len
+ 1));
1140 mbstowcs (wcharlist
, charlist
, len
+ 1);
1143 if (wcschr (wcharlist
, wc
))
1149 if (MEMBER (c
, charlist
))
1152 ADVANCE_CHAR (string
, slen
, i
);
1155 #if defined (HANDLE_MULTIBYTE)
1159 temp
= substring (string
, *sindex
, i
);
1165 /* Extract the $( construct in STRING, and return a new string.
1166 Start extracting at (SINDEX) as if we had just seen "$(".
1167 Make (SINDEX) get the position of the matching ")". )
1168 XFLAGS is additional flags to pass to other extraction functions. */
1170 extract_command_subst (string
, sindex
, xflags
)
1175 if (string
[*sindex
] == LPAREN
)
1176 return (extract_delimited_string (string
, sindex
, "$(", "(", ")", xflags
|SX_COMMAND
)); /*)*/
1179 xflags
|= (no_longjmp_on_fatal_error
? SX_NOLONGJMP
: 0);
1180 return (xparse_dolparen (string
, string
+*sindex
, sindex
, xflags
));
1184 /* Extract the $[ construct in STRING, and return a new string. (])
1185 Start extracting at (SINDEX) as if we had just seen "$[".
1186 Make (SINDEX) get the position of the matching "]". */
1188 extract_arithmetic_subst (string
, sindex
)
1192 return (extract_delimited_string (string
, sindex
, "$[", "[", "]", 0)); /*]*/
1195 #if defined (PROCESS_SUBSTITUTION)
1196 /* Extract the <( or >( construct in STRING, and return a new string.
1197 Start extracting at (SINDEX) as if we had just seen "<(".
1198 Make (SINDEX) get the position of the matching ")". */ /*))*/
1200 extract_process_subst (string
, starter
, sindex
, xflags
)
1207 return (extract_delimited_string (string
, sindex
, starter
, "(", ")", SX_COMMAND
));
1209 xflags
|= (no_longjmp_on_fatal_error
? SX_NOLONGJMP
: 0);
1210 return (xparse_dolparen (string
, string
+*sindex
, sindex
, xflags
));
1213 #endif /* PROCESS_SUBSTITUTION */
1215 #if defined (ARRAY_VARS)
1216 /* This can be fooled by unquoted right parens in the passed string. If
1217 each caller verifies that the last character in STRING is a right paren,
1218 we don't even need to call extract_delimited_string. */
1220 extract_array_assignment_list (string
, sindex
)
1227 slen
= strlen (string
); /* ( */
1228 if (string
[slen
- 1] == ')')
1230 ret
= substring (string
, *sindex
, slen
- 1);
1238 /* Extract and create a new string from the contents of STRING, a
1239 character string delimited with OPENER and CLOSER. SINDEX is
1240 the address of an int describing the current offset in STRING;
1241 it should point to just after the first OPENER found. On exit,
1242 SINDEX gets the position of the last character of the matching CLOSER.
1243 If OPENER is more than a single character, ALT_OPENER, if non-null,
1244 contains a character string that can also match CLOSER and thus
1245 needs to be skipped. */
1247 extract_delimited_string (string
, sindex
, opener
, alt_opener
, closer
, flags
)
1250 char *opener
, *alt_opener
, *closer
;
1256 int pass_character
, nesting_level
, in_comment
;
1257 int len_closer
, len_opener
, len_alt_opener
;
1260 slen
= strlen (string
+ *sindex
) + *sindex
;
1261 len_opener
= STRLEN (opener
);
1262 len_alt_opener
= STRLEN (alt_opener
);
1263 len_closer
= STRLEN (closer
);
1265 pass_character
= in_comment
= 0;
1270 while (nesting_level
)
1281 ADVANCE_CHAR (string
, slen
, i
);
1285 if (pass_character
) /* previous char was backslash */
1288 ADVANCE_CHAR (string
, slen
, i
);
1292 /* Not exactly right yet; should handle shell metacharacters and
1293 multibyte characters, too. See COMMENT_BEGIN define in parse.y */
1294 if ((flags
& SX_COMMAND
) && c
== '#' && (i
== 0 || string
[i
- 1] == '\n' || shellblank (string
[i
- 1])))
1297 ADVANCE_CHAR (string
, slen
, i
);
1301 if (c
== CTLESC
|| c
== '\\')
1308 /* Process a nested command substitution, but only if we're parsing an
1309 arithmetic substitution. */
1310 if ((flags
& SX_COMMAND
) && string
[i
] == '$' && string
[i
+1] == LPAREN
)
1313 t
= extract_command_subst (string
, &si
, flags
|SX_NOALLOC
);
1318 /* Process a nested OPENER. */
1319 if (STREQN (string
+ i
, opener
, len_opener
))
1321 si
= i
+ len_opener
;
1322 t
= extract_delimited_string (string
, &si
, opener
, alt_opener
, closer
, flags
|SX_NOALLOC
);
1327 /* Process a nested ALT_OPENER */
1328 if (len_alt_opener
&& STREQN (string
+ i
, alt_opener
, len_alt_opener
))
1330 si
= i
+ len_alt_opener
;
1331 t
= extract_delimited_string (string
, &si
, alt_opener
, alt_opener
, closer
, flags
|SX_NOALLOC
);
1336 /* If the current substring terminates the delimited string, decrement
1337 the nesting level. */
1338 if (STREQN (string
+ i
, closer
, len_closer
))
1340 i
+= len_closer
- 1; /* move to last byte of the closer */
1342 if (nesting_level
== 0)
1346 /* Pass old-style command substitution through verbatim. */
1350 t
= string_extract (string
, &si
, "`", flags
|SX_NOALLOC
);
1355 /* Pass single-quoted and double-quoted strings through verbatim. */
1356 if (c
== '\'' || c
== '"')
1359 i
= (c
== '\'') ? skip_single_quoted (string
, slen
, si
)
1360 : skip_double_quoted (string
, slen
, si
);
1364 /* move past this character, which was not special. */
1365 ADVANCE_CHAR (string
, slen
, i
);
1368 if (c
== 0 && nesting_level
)
1370 if (no_longjmp_on_fatal_error
== 0)
1372 last_command_exit_value
= EXECUTION_FAILURE
;
1373 report_error (_("bad substitution: no closing `%s' in %s"), closer
, string
);
1374 exp_jump_to_top_level (DISCARD
);
1379 return (char *)NULL
;
1383 si
= i
- *sindex
- len_closer
+ 1;
1384 if (flags
& SX_NOALLOC
)
1385 result
= (char *)NULL
;
1388 result
= (char *)xmalloc (1 + si
);
1389 strncpy (result
, string
+ *sindex
, si
);
1397 /* Extract a parameter expansion expression within ${ and } from STRING.
1398 Obey the Posix.2 rules for finding the ending `}': count braces while
1399 skipping over enclosed quoted strings and command substitutions.
1400 SINDEX is the address of an int describing the current offset in STRING;
1401 it should point to just after the first `{' found. On exit, SINDEX
1402 gets the position of the matching `}'. QUOTED is non-zero if this
1403 occurs inside double quotes. */
1404 /* XXX -- this is very similar to extract_delimited_string -- XXX */
1406 extract_dollar_brace_string (string
, sindex
, quoted
, flags
)
1408 int *sindex
, quoted
, flags
;
1412 int pass_character
, nesting_level
, si
, dolbrace_state
;
1418 slen
= strlen (string
+ *sindex
) + *sindex
;
1420 /* The handling of dolbrace_state needs to agree with the code in parse.y:
1421 parse_matched_pair(). The different initial value is to handle the
1422 case where this function is called to parse the word in
1423 ${param op word} (SX_WORD). */
1424 dolbrace_state
= (flags
& SX_WORD
) ? DOLBRACE_WORD
: DOLBRACE_PARAM
;
1425 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && (flags
& SX_POSIXEXP
))
1426 dolbrace_state
= DOLBRACE_QUOTE
;
1429 while (c
= string
[i
])
1434 ADVANCE_CHAR (string
, slen
, i
);
1438 /* CTLESCs and backslashes quote the next character. */
1439 if (c
== CTLESC
|| c
== '\\')
1446 if (string
[i
] == '$' && string
[i
+1] == LBRACE
)
1456 if (nesting_level
== 0)
1462 /* Pass the contents of old-style command substitutions through
1467 t
= string_extract (string
, &si
, "`", flags
|SX_NOALLOC
);
1472 /* Pass the contents of new-style command substitutions and
1473 arithmetic substitutions through verbatim. */
1474 if (string
[i
] == '$' && string
[i
+1] == LPAREN
)
1477 t
= extract_command_subst (string
, &si
, flags
|SX_NOALLOC
);
1482 /* Pass the contents of double-quoted strings through verbatim. */
1486 i
= skip_double_quoted (string
, slen
, si
);
1487 /* skip_XXX_quoted leaves index one past close quote */
1493 /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/
1494 if (posixly_correct
&& shell_compatibility_level
> 42 && dolbrace_state
!= DOLBRACE_QUOTE
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
1495 ADVANCE_CHAR (string
, slen
, i
);
1499 i
= skip_single_quoted (string
, slen
, si
);
1505 /* move past this character, which was not special. */
1506 ADVANCE_CHAR (string
, slen
, i
);
1508 /* This logic must agree with parse.y:parse_matched_pair, since they
1509 share the same defines. */
1510 if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '%' && (i
- *sindex
) > 1)
1511 dolbrace_state
= DOLBRACE_QUOTE
;
1512 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '#' && (i
- *sindex
) > 1)
1513 dolbrace_state
= DOLBRACE_QUOTE
;
1514 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '/' && (i
- *sindex
) > 1)
1515 dolbrace_state
= DOLBRACE_QUOTE2
; /* XXX */
1516 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== '^' && (i
- *sindex
) > 1)
1517 dolbrace_state
= DOLBRACE_QUOTE
;
1518 else if (dolbrace_state
== DOLBRACE_PARAM
&& c
== ',' && (i
- *sindex
) > 1)
1519 dolbrace_state
= DOLBRACE_QUOTE
;
1520 else if (dolbrace_state
== DOLBRACE_PARAM
&& strchr ("#%^,~:-=?+/", c
) != 0)
1521 dolbrace_state
= DOLBRACE_OP
;
1522 else if (dolbrace_state
== DOLBRACE_OP
&& strchr ("#%^,~:-=?+/", c
) == 0)
1523 dolbrace_state
= DOLBRACE_WORD
;
1526 if (c
== 0 && nesting_level
)
1528 if (no_longjmp_on_fatal_error
== 0)
1530 last_command_exit_value
= EXECUTION_FAILURE
;
1531 report_error (_("bad substitution: no closing `%s' in %s"), "}", string
);
1532 exp_jump_to_top_level (DISCARD
);
1537 return ((char *)NULL
);
1541 result
= (flags
& SX_NOALLOC
) ? (char *)NULL
: substring (string
, *sindex
, i
);
1547 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1548 STRING, and returns a pointer to it. */
1550 de_backslash (string
)
1553 register size_t slen
;
1554 register int i
, j
, prev_i
;
1557 slen
= strlen (string
);
1560 /* Loop copying string[i] to string[j], i >= j. */
1563 if (string
[i
] == '\\' && (string
[i
+ 1] == '`' || string
[i
+ 1] == '\\' ||
1564 string
[i
+ 1] == '$'))
1567 ADVANCE_CHAR (string
, slen
, i
);
1569 do string
[j
++] = string
[prev_i
++]; while (prev_i
< i
);
1580 /* Replace instances of \! in a string with !. */
1582 unquote_bang (string
)
1586 register char *temp
;
1588 temp
= (char *)xmalloc (1 + strlen (string
));
1590 for (i
= 0, j
= 0; (temp
[j
] = string
[i
]); i
++, j
++)
1592 if (string
[i
] == '\\' && string
[i
+ 1] == '!')
1598 strcpy (string
, temp
);
1603 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0)
1605 /* This function assumes s[i] == open; returns with s[ret] == close; used to
1606 parse array subscripts. FLAGS & 1 means to not attempt to skip over
1607 matched pairs of quotes or backquotes, or skip word expansions; it is
1608 intended to be used after expansion has been performed and during final
1609 assignment parsing (see arrayfunc.c:assign_compound_array_list()). */
1611 skip_matched_pair (string
, start
, open
, close
, flags
)
1613 int start
, open
, close
, flags
;
1615 int i
, pass_next
, backq
, si
, c
, count
;
1620 slen
= strlen (string
+ start
) + start
;
1621 no_longjmp_on_fatal_error
= 1;
1623 i
= start
+ 1; /* skip over leading bracket */
1625 pass_next
= backq
= 0;
1626 ss
= (char *)string
;
1627 while (c
= string
[i
])
1634 ADVANCE_CHAR (string
, slen
, i
);
1647 ADVANCE_CHAR (string
, slen
, i
);
1650 else if ((flags
& 1) == 0 && c
== '`')
1656 else if ((flags
& 1) == 0 && c
== open
)
1662 else if (c
== close
)
1670 else if ((flags
& 1) == 0 && (c
== '\'' || c
== '"'))
1672 i
= (c
== '\'') ? skip_single_quoted (ss
, slen
, ++i
)
1673 : skip_double_quoted (ss
, slen
, ++i
);
1674 /* no increment, the skip functions increment past the closing quote. */
1676 else if ((flags
&1) == 0 && c
== '$' && (string
[i
+1] == LPAREN
|| string
[i
+1] == LBRACE
))
1679 if (string
[si
] == '\0')
1682 if (string
[i
+1] == LPAREN
)
1683 temp
= extract_delimited_string (ss
, &si
, "$(", "(", ")", SX_NOALLOC
|SX_COMMAND
); /* ) */
1685 temp
= extract_dollar_brace_string (ss
, &si
, 0, SX_NOALLOC
);
1687 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1693 ADVANCE_CHAR (string
, slen
, i
);
1699 #if defined (ARRAY_VARS)
1701 skipsubscript (string
, start
, flags
)
1705 return (skip_matched_pair (string
, start
, '[', ']', flags
));
1709 /* Skip characters in STRING until we find a character in DELIMS, and return
1710 the index of that character. START is the index into string at which we
1711 begin. This is similar in spirit to strpbrk, but it returns an index into
1712 STRING and takes a starting index. This little piece of code knows quite
1713 a lot of shell syntax. It's very similar to skip_double_quoted and other
1714 functions of that ilk. */
1716 skip_to_delim (string
, start
, delims
, flags
)
1722 int i
, pass_next
, backq
, si
, c
, invert
, skipquote
, skipcmd
, noprocsub
;
1724 char *temp
, open
[3];
1727 slen
= strlen (string
+ start
) + start
;
1728 if (flags
& SD_NOJMP
)
1729 no_longjmp_on_fatal_error
= 1;
1730 invert
= (flags
& SD_INVERT
);
1731 skipcmd
= (flags
& SD_NOSKIPCMD
) == 0;
1732 noprocsub
= (flags
& SD_NOPROCSUB
);
1735 pass_next
= backq
= 0;
1736 while (c
= string
[i
])
1738 /* If this is non-zero, we should not let quote characters be delimiters
1739 and the current character is a single or double quote. We should not
1740 test whether or not it's a delimiter until after we skip single- or
1741 double-quoted strings. */
1742 skipquote
= ((flags
& SD_NOQUOTEDELIM
) && (c
== '\'' || c
=='"'));
1748 ADVANCE_CHAR (string
, slen
, i
);
1761 ADVANCE_CHAR (string
, slen
, i
);
1770 else if (skipquote
== 0 && invert
== 0 && member (c
, delims
))
1772 else if (c
== '\'' || c
== '"')
1774 i
= (c
== '\'') ? skip_single_quoted (string
, slen
, ++i
)
1775 : skip_double_quoted (string
, slen
, ++i
);
1776 /* no increment, the skip functions increment past the closing quote. */
1778 else if (c
== '$' && ((skipcmd
&& string
[i
+1] == LPAREN
) || string
[i
+1] == LBRACE
))
1781 if (string
[si
] == '\0')
1784 if (string
[i
+1] == LPAREN
)
1785 temp
= extract_delimited_string (string
, &si
, "$(", "(", ")", SX_NOALLOC
|SX_COMMAND
); /* ) */
1787 temp
= extract_dollar_brace_string (string
, &si
, 0, SX_NOALLOC
);
1789 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1794 #if defined (PROCESS_SUBSTITUTION)
1795 else if (skipcmd
&& noprocsub
== 0 && (c
== '<' || c
== '>') && string
[i
+1] == LPAREN
)
1798 if (string
[si
] == '\0')
1800 temp
= extract_process_subst (string
, (c
== '<') ? "<(" : ">(", &si
, 0);
1801 free (temp
); /* XXX - not using SX_ALLOC here yet */
1803 if (string
[i
] == '\0')
1808 #endif /* PROCESS_SUBSTITUTION */
1809 #if defined (EXTENDED_GLOB)
1810 else if ((flags
& SD_EXTGLOB
) && extended_glob
&& string
[i
+1] == LPAREN
&& member (c
, "?*+!@"))
1813 if (string
[si
] == '\0')
1819 temp
= extract_delimited_string (string
, &si
, open
, "(", ")", SX_NOALLOC
); /* ) */
1822 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1828 else if ((flags
& SD_GLOB
) && c
== LBRACK
)
1831 if (string
[si
] == '\0')
1834 temp
= extract_delimited_string (string
, &si
, "[", "[", "]", SX_NOALLOC
); /* ] */
1837 if (string
[i
] == '\0') /* don't increment i past EOS in loop */
1842 else if ((skipquote
|| invert
) && (member (c
, delims
) == 0))
1845 ADVANCE_CHAR (string
, slen
, i
);
1851 #if defined (READLINE)
1852 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
1853 an unclosed quoted string), or if the character at EINDEX is quoted
1854 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
1855 single and double-quoted string parsing functions should not return an
1856 error if there are unclosed quotes or braces. The characters that this
1857 recognizes need to be the same as the contents of
1858 rl_completer_quote_characters. */
1861 char_is_quoted (string
, eindex
)
1865 int i
, pass_next
, c
;
1869 slen
= strlen (string
);
1870 no_longjmp_on_fatal_error
= 1;
1879 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
1881 ADVANCE_CHAR (string
, slen
, i
);
1890 else if (c
== '\'' || c
== '"')
1892 i
= (c
== '\'') ? skip_single_quoted (string
, slen
, ++i
)
1893 : skip_double_quoted (string
, slen
, ++i
);
1896 /* no increment, the skip_xxx functions go one past end */
1899 ADVANCE_CHAR (string
, slen
, i
);
1906 unclosed_pair (string
, eindex
, openstr
)
1911 int i
, pass_next
, openc
, olen
;
1915 slen
= strlen (string
);
1916 olen
= strlen (openstr
);
1917 i
= pass_next
= openc
= 0;
1923 if (i
>= eindex
) /* XXX was if (i >= eindex - 1) */
1925 ADVANCE_CHAR (string
, slen
, i
);
1928 else if (string
[i
] == '\\')
1934 else if (STREQN (string
+ i
, openstr
, olen
))
1939 else if (string
[i
] == '\'' || string
[i
] == '"')
1941 i
= (string
[i
] == '\'') ? skip_single_quoted (string
, slen
, i
)
1942 : skip_double_quoted (string
, slen
, i
);
1947 ADVANCE_CHAR (string
, slen
, i
);
1952 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
1953 individual words. If DELIMS is NULL, the current value of $IFS is used
1954 to split the string, and the function follows the shell field splitting
1955 rules. SENTINEL is an index to look for. NWP, if non-NULL,
1956 gets the number of words in the returned list. CWP, if non-NULL, gets
1957 the index of the word containing SENTINEL. Non-whitespace chars in
1958 DELIMS delimit separate fields. */
1960 split_at_delims (string
, slen
, delims
, sentinel
, flags
, nwp
, cwp
)
1964 int sentinel
, flags
;
1967 int ts
, te
, i
, nw
, cw
, ifs_split
, dflags
;
1968 char *token
, *d
, *d2
;
1969 WORD_LIST
*ret
, *tl
;
1971 if (string
== 0 || *string
== '\0')
1977 return ((WORD_LIST
*)NULL
);
1980 d
= (delims
== 0) ? ifs_value
: delims
;
1981 ifs_split
= delims
== 0;
1983 /* Make d2 the non-whitespace characters in delims */
1988 #if defined (HANDLE_MULTIBYTE)
1989 size_t mblength
= 1;
1993 slength
= strlen (delims
);
1994 d2
= (char *)xmalloc (slength
+ 1);
1998 #if defined (HANDLE_MULTIBYTE)
1999 mbstate_t state_bak
;
2001 mblength
= MBRLEN (delims
+ i
, slength
, &state
);
2002 if (MB_INVALIDCH (mblength
))
2004 else if (mblength
> 1)
2006 memcpy (d2
+ ts
, delims
+ i
, mblength
);
2009 slength
-= mblength
;
2013 if (whitespace (delims
[i
]) == 0)
2014 d2
[ts
++] = delims
[i
];
2022 ret
= (WORD_LIST
*)NULL
;
2024 /* Remove sequences of whitespace characters at the start of the string, as
2025 long as those characters are delimiters. */
2026 for (i
= 0; member (string
[i
], d
) && spctabnl (string
[i
]); i
++)
2028 if (string
[i
] == '\0')
2034 dflags
= flags
|SD_NOJMP
;
2037 te
= skip_to_delim (string
, ts
, d
, dflags
);
2039 /* If we have a non-whitespace delimiter character, use it to make a
2040 separate field. This is just about what $IFS splitting does and
2041 is closer to the behavior of the shell parser. */
2042 if (ts
== te
&& d2
&& member (string
[ts
], d2
))
2045 /* If we're using IFS splitting, the non-whitespace delimiter char
2046 and any additional IFS whitespace delimits a field. */
2048 while (member (string
[te
], d
) && spctabnl (string
[te
]))
2051 while (member (string
[te
], d2
))
2055 token
= substring (string
, ts
, te
);
2057 ret
= add_string_to_list (token
, ret
);
2061 if (sentinel
>= ts
&& sentinel
<= te
)
2064 /* If the cursor is at whitespace just before word start, set the
2065 sentinel word to the current word. */
2066 if (cwp
&& cw
== -1 && sentinel
== ts
-1)
2069 /* If the cursor is at whitespace between two words, make a new, empty
2070 word, add it before (well, after, since the list is in reverse order)
2071 the word we just added, and set the current word to that one. */
2072 if (cwp
&& cw
== -1 && sentinel
< ts
)
2074 tl
= make_word_list (make_word (""), ret
->next
);
2080 if (string
[te
] == 0)
2084 while (member (string
[i
], d
) && (ifs_split
|| spctabnl(string
[i
])))
2093 /* Special case for SENTINEL at the end of STRING. If we haven't found
2094 the word containing SENTINEL yet, and the index we're looking for is at
2095 the end of STRING (or past the end of the previously-found token,
2096 possible if the end of the line is composed solely of IFS whitespace)
2097 add an additional null argument and set the current word pointer to that. */
2098 if (cwp
&& cw
== -1 && (sentinel
>= slen
|| sentinel
>= te
))
2100 if (whitespace (string
[sentinel
- 1]))
2103 ret
= add_string_to_list (token
, ret
);
2116 return (REVERSE_LIST (ret
, WORD_LIST
*));
2118 #endif /* READLINE */
2122 /* Extract the name of the variable to bind to from the assignment string. */
2124 assignment_name (string
)
2130 offset
= assignment (string
, 0);
2132 return (char *)NULL
;
2133 temp
= substring (string
, 0, offset
);
2138 /* **************************************************************** */
2140 /* Functions to convert strings to WORD_LISTs and vice versa */
2142 /* **************************************************************** */
2144 /* Return a single string of all the words in LIST. SEP is the separator
2145 to put between individual elements of LIST in the output string. */
2147 string_list_internal (list
, sep
)
2151 register WORD_LIST
*t
;
2153 int word_len
, sep_len
, result_size
;
2156 return ((char *)NULL
);
2158 /* Short-circuit quickly if we don't need to separate anything. */
2159 if (list
->next
== 0)
2160 return (savestring (list
->word
->word
));
2162 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
2163 sep_len
= STRLEN (sep
);
2166 for (t
= list
; t
; t
= t
->next
)
2169 result_size
+= sep_len
;
2170 result_size
+= strlen (t
->word
->word
);
2173 r
= result
= (char *)xmalloc (result_size
+ 1);
2175 for (t
= list
; t
; t
= t
->next
)
2177 if (t
!= list
&& sep_len
)
2181 FASTCOPY (sep
, r
, sep_len
);
2188 word_len
= strlen (t
->word
->word
);
2189 FASTCOPY (t
->word
->word
, r
, word_len
);
2197 /* Return a single string of all the words present in LIST, separating
2198 each word with a space. */
2203 return (string_list_internal (list
, " "));
2206 /* An external interface that can be used by the rest of the shell to
2207 obtain a string containing the first character in $IFS. Handles all
2208 the multibyte complications. If LENP is non-null, it is set to the
2209 length of the returned string. */
2211 ifs_firstchar (lenp
)
2217 ret
= xmalloc (MB_LEN_MAX
+ 1);
2218 #if defined (HANDLE_MULTIBYTE)
2219 if (ifs_firstc_len
== 1)
2221 ret
[0] = ifs_firstc
[0];
2223 len
= ret
[0] ? 1 : 0;
2227 memcpy (ret
, ifs_firstc
, ifs_firstc_len
);
2228 ret
[len
= ifs_firstc_len
] = '\0';
2231 ret
[0] = ifs_firstc
;
2233 len
= ret
[0] ? 0 : 1;
2242 /* Return a single string of all the words present in LIST, obeying the
2243 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
2244 expansion [of $*] appears within a double quoted string, it expands
2245 to a single field with the value of each parameter separated by the
2246 first character of the IFS variable, or by a <space> if IFS is unset." */
2248 string_list_dollar_star (list
)
2252 #if defined (HANDLE_MULTIBYTE)
2253 # if defined (__GNUC__)
2254 char sep
[MB_CUR_MAX
+ 1];
2262 #if defined (HANDLE_MULTIBYTE)
2263 # if !defined (__GNUC__)
2264 sep
= (char *)xmalloc (MB_CUR_MAX
+ 1);
2265 # endif /* !__GNUC__ */
2266 if (ifs_firstc_len
== 1)
2268 sep
[0] = ifs_firstc
[0];
2273 memcpy (sep
, ifs_firstc
, ifs_firstc_len
);
2274 sep
[ifs_firstc_len
] = '\0';
2277 sep
[0] = ifs_firstc
;
2281 ret
= string_list_internal (list
, sep
);
2282 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2288 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
2289 is non-zero, the $@ appears within double quotes, and we should quote
2290 the list before converting it into a string. If IFS is unset, and the
2291 word is not quoted, we just need to quote CTLESC and CTLNUL characters
2292 in the words in the list, because the default value of $IFS is
2293 <space><tab><newline>, IFS characters in the words in the list should
2294 also be split. If IFS is null, and the word is not quoted, we need
2295 to quote the words in the list to preserve the positional parameters
2298 string_list_dollar_at (list
, quoted
)
2303 #if defined (HANDLE_MULTIBYTE)
2304 # if defined (__GNUC__)
2305 char sep
[MB_CUR_MAX
+ 1];
2308 # endif /* !__GNUC__ */
2314 /* XXX this could just be ifs = ifs_value; */
2315 ifs
= ifs_var
? value_cell (ifs_var
) : (char *)0;
2317 #if defined (HANDLE_MULTIBYTE)
2318 # if !defined (__GNUC__)
2319 sep
= (char *)xmalloc (MB_CUR_MAX
+ 1);
2320 # endif /* !__GNUC__ */
2323 if (ifs_firstc_len
== 1)
2325 sep
[0] = ifs_firstc
[0];
2330 memcpy (sep
, ifs_firstc
, ifs_firstc_len
);
2331 sep
[ifs_firstc_len
] = '\0';
2340 sep
[0] = (ifs
== 0 || *ifs
== 0) ? ' ' : *ifs
;
2344 /* XXX -- why call quote_list if ifs == 0? we can get away without doing
2345 it now that quote_escapes quotes spaces */
2346 tlist
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
|Q_PATQUOTE
))
2348 : list_quote_escapes (list
);
2350 ret
= string_list_internal (tlist
, sep
);
2351 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2357 /* Turn the positional parameters into a string, understanding quoting and
2358 the various subtleties of using the first character of $IFS as the
2359 separator. Calls string_list_dollar_at, string_list_dollar_star, and
2360 string_list as appropriate. */
2362 string_list_pos_params (pchar
, list
, quoted
)
2370 if (pchar
== '*' && (quoted
& Q_DOUBLE_QUOTES
))
2372 tlist
= quote_list (list
);
2373 word_list_remove_quoted_nulls (tlist
);
2374 ret
= string_list_dollar_star (tlist
);
2376 else if (pchar
== '*' && (quoted
& Q_HERE_DOCUMENT
))
2378 tlist
= quote_list (list
);
2379 word_list_remove_quoted_nulls (tlist
);
2380 ret
= string_list (tlist
);
2382 else if (pchar
== '*')
2384 /* Even when unquoted, string_list_dollar_star does the right thing
2385 making sure that the first character of $IFS is used as the
2387 ret
= string_list_dollar_star (list
);
2389 else if (pchar
== '@' && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
2390 /* We use string_list_dollar_at, but only if the string is quoted, since
2391 that quotes the escapes if it's not, which we don't want. We could
2392 use string_list (the old code did), but that doesn't do the right
2393 thing if the first character of $IFS is not a space. We use
2394 string_list_dollar_star if the string is unquoted so we make sure that
2395 the elements of $@ are separated by the first character of $IFS for
2397 ret
= string_list_dollar_at (list
, quoted
);
2398 else if (pchar
== '@')
2399 ret
= string_list_dollar_star (list
);
2401 ret
= string_list ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? quote_list (list
) : list
);
2406 /* Return the list of words present in STRING. Separate the string into
2407 words at any of the characters found in SEPARATORS. If QUOTED is
2408 non-zero then word in the list will have its quoted flag set, otherwise
2409 the quoted flag is left as make_word () deemed fit.
2411 This obeys the P1003.2 word splitting semantics. If `separators' is
2412 exactly <space><tab><newline>, then the splitting algorithm is that of
2413 the Bourne shell, which treats any sequence of characters from `separators'
2414 as a delimiter. If IFS is unset, which results in `separators' being set
2415 to "", no splitting occurs. If separators has some other value, the
2416 following rules are applied (`IFS white space' means zero or more
2417 occurrences of <space>, <tab>, or <newline>, as long as those characters
2418 are in `separators'):
2420 1) IFS white space is ignored at the start and the end of the
2422 2) Each occurrence of a character in `separators' that is not
2423 IFS white space, along with any adjacent occurrences of
2424 IFS white space delimits a field.
2425 3) Any nonzero-length sequence of IFS white space delimits a field.
2428 /* BEWARE! list_string strips null arguments. Don't call it twice and
2429 expect to have "" preserved! */
2431 /* This performs word splitting and quoted null character removal on
2434 (((separators)[0]) ? ((separators)[1] ? isifs(c) \
2435 : (c) == (separators)[0]) \
2439 list_string (string
, separators
, quoted
)
2440 register char *string
, *separators
;
2445 char *current_word
, *s
;
2446 int sindex
, sh_style_split
, whitesep
, xflags
;
2449 if (!string
|| !*string
)
2450 return ((WORD_LIST
*)NULL
);
2452 sh_style_split
= separators
&& separators
[0] == ' ' &&
2453 separators
[1] == '\t' &&
2454 separators
[2] == '\n' &&
2455 separators
[3] == '\0';
2456 for (xflags
= 0, s
= ifs_value
; s
&& *s
; s
++)
2458 if (*s
== CTLESC
) xflags
|= SX_NOCTLESC
;
2459 else if (*s
== CTLNUL
) xflags
|= SX_NOESCCTLNUL
;
2463 /* Remove sequences of whitespace at the beginning of STRING, as
2464 long as those characters appear in IFS. Do not do this if
2465 STRING is quoted or if there are no separator characters. */
2466 if (!quoted
|| !separators
|| !*separators
)
2468 for (s
= string
; *s
&& spctabnl (*s
) && issep (*s
); s
++);
2471 return ((WORD_LIST
*)NULL
);
2476 /* OK, now STRING points to a word that does not begin with white space.
2477 The splitting algorithm is:
2478 extract a word, stopping at a separator
2479 skip sequences of spc, tab, or nl as long as they are separators
2480 This obeys the field splitting rules in Posix.2. */
2481 slen
= (MB_CUR_MAX
> 1) ? strlen (string
) : 1;
2482 for (result
= (WORD_LIST
*)NULL
, sindex
= 0; string
[sindex
]; )
2484 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2485 unless multibyte chars are possible. */
2486 current_word
= string_extract_verbatim (string
, slen
, &sindex
, separators
, xflags
);
2487 if (current_word
== 0)
2490 /* If we have a quoted empty string, add a quoted null argument. We
2491 want to preserve the quoted null character iff this is a quoted
2492 empty string; otherwise the quoted null characters are removed
2494 if (QUOTED_NULL (current_word
))
2496 t
= alloc_word_desc ();
2497 t
->word
= make_quoted_char ('\0');
2498 t
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
2499 result
= make_word_list (t
, result
);
2501 else if (current_word
[0] != '\0')
2503 /* If we have something, then add it regardless. However,
2504 perform quoted null character removal on the current word. */
2505 remove_quoted_nulls (current_word
);
2506 result
= add_string_to_list (current_word
, result
);
2507 result
->word
->flags
&= ~W_HASQUOTEDNULL
; /* just to be sure */
2508 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
2509 result
->word
->flags
|= W_QUOTED
;
2512 /* If we're not doing sequences of separators in the traditional
2513 Bourne shell style, then add a quoted null argument. */
2514 else if (!sh_style_split
&& !spctabnl (string
[sindex
]))
2516 t
= alloc_word_desc ();
2517 t
->word
= make_quoted_char ('\0');
2518 t
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
2519 result
= make_word_list (t
, result
);
2522 free (current_word
);
2524 /* Note whether or not the separator is IFS whitespace, used later. */
2525 whitesep
= string
[sindex
] && spctabnl (string
[sindex
]);
2527 /* Move past the current separator character. */
2531 ADVANCE_CHAR (string
, slen
, sindex
);
2534 /* Now skip sequences of space, tab, or newline characters if they are
2535 in the list of separators. */
2536 while (string
[sindex
] && spctabnl (string
[sindex
]) && issep (string
[sindex
]))
2539 /* If the first separator was IFS whitespace and the current character
2540 is a non-whitespace IFS character, it should be part of the current
2541 field delimiter, not a separate delimiter that would result in an
2542 empty field. Look at POSIX.2, 3.6.5, (3)(b). */
2543 if (string
[sindex
] && whitesep
&& issep (string
[sindex
]) && !spctabnl (string
[sindex
]))
2546 /* An IFS character that is not IFS white space, along with any
2547 adjacent IFS white space, shall delimit a field. (SUSv3) */
2548 while (string
[sindex
] && spctabnl (string
[sindex
]) && isifs (string
[sindex
]))
2552 return (REVERSE_LIST (result
, WORD_LIST
*));
2555 /* Parse a single word from STRING, using SEPARATORS to separate fields.
2556 ENDPTR is set to the first character after the word. This is used by
2557 the `read' builtin. This is never called with SEPARATORS != $IFS;
2558 it should be simplified.
2560 XXX - this function is very similar to list_string; they should be
2563 get_word_from_string (stringp
, separators
, endptr
)
2564 char **stringp
, *separators
, **endptr
;
2568 int sindex
, sh_style_split
, whitesep
, xflags
;
2571 if (!stringp
|| !*stringp
|| !**stringp
)
2572 return ((char *)NULL
);
2574 sh_style_split
= separators
&& separators
[0] == ' ' &&
2575 separators
[1] == '\t' &&
2576 separators
[2] == '\n' &&
2577 separators
[3] == '\0';
2578 for (xflags
= 0, s
= ifs_value
; s
&& *s
; s
++)
2580 if (*s
== CTLESC
) xflags
|= SX_NOCTLESC
;
2581 if (*s
== CTLNUL
) xflags
|= SX_NOESCCTLNUL
;
2587 /* Remove sequences of whitespace at the beginning of STRING, as
2588 long as those characters appear in IFS. */
2589 if (sh_style_split
|| !separators
|| !*separators
)
2591 for (; *s
&& spctabnl (*s
) && isifs (*s
); s
++);
2593 /* If the string is nothing but whitespace, update it and return. */
2599 return ((char *)NULL
);
2603 /* OK, S points to a word that does not begin with white space.
2604 Now extract a word, stopping at a separator, save a pointer to
2605 the first character after the word, then skip sequences of spc,
2606 tab, or nl as long as they are separators.
2608 This obeys the field splitting rules in Posix.2. */
2610 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2611 unless multibyte chars are possible. */
2612 slen
= (MB_CUR_MAX
> 1) ? strlen (s
) : 1;
2613 current_word
= string_extract_verbatim (s
, slen
, &sindex
, separators
, xflags
);
2615 /* Set ENDPTR to the first character after the end of the word. */
2617 *endptr
= s
+ sindex
;
2619 /* Note whether or not the separator is IFS whitespace, used later. */
2620 whitesep
= s
[sindex
] && spctabnl (s
[sindex
]);
2622 /* Move past the current separator character. */
2626 ADVANCE_CHAR (s
, slen
, sindex
);
2629 /* Now skip sequences of space, tab, or newline characters if they are
2630 in the list of separators. */
2631 while (s
[sindex
] && spctabnl (s
[sindex
]) && isifs (s
[sindex
]))
2634 /* If the first separator was IFS whitespace and the current character is
2635 a non-whitespace IFS character, it should be part of the current field
2636 delimiter, not a separate delimiter that would result in an empty field.
2637 Look at POSIX.2, 3.6.5, (3)(b). */
2638 if (s
[sindex
] && whitesep
&& isifs (s
[sindex
]) && !spctabnl (s
[sindex
]))
2641 /* An IFS character that is not IFS white space, along with any adjacent
2642 IFS white space, shall delimit a field. */
2643 while (s
[sindex
] && spctabnl (s
[sindex
]) && isifs (s
[sindex
]))
2647 /* Update STRING to point to the next field. */
2648 *stringp
= s
+ sindex
;
2649 return (current_word
);
2652 /* Remove IFS white space at the end of STRING. Start at the end
2653 of the string and walk backwards until the beginning of the string
2654 or we find a character that's not IFS white space and not CTLESC.
2655 Only let CTLESC escape a white space character if SAW_ESCAPE is
2658 strip_trailing_ifs_whitespace (string
, separators
, saw_escape
)
2659 char *string
, *separators
;
2664 s
= string
+ STRLEN (string
) - 1;
2665 while (s
> string
&& ((spctabnl (*s
) && isifs (*s
)) ||
2666 (saw_escape
&& *s
== CTLESC
&& spctabnl (s
[1]))))
2674 /* Split STRING into words at whitespace. Obeys shell-style quoting with
2675 backslashes, single and double quotes. */
2677 list_string_with_quotes (string
)
2683 int c
, i
, tokstart
, len
;
2685 for (s
= string
; s
&& *s
&& spctabnl (*s
); s
++)
2687 if (s
== 0 || *s
== 0)
2688 return ((WORD_LIST
*)NULL
);
2692 list
= (WORD_LIST
*)NULL
;
2703 i
= skip_single_quoted (s
, s_len
, ++i
);
2705 i
= skip_double_quoted (s
, s_len
, ++i
);
2706 else if (c
== 0 || spctabnl (c
))
2708 /* We have found the end of a token. Make a word out of it and
2709 add it to the word list. */
2710 token
= substring (s
, tokstart
, i
);
2711 list
= add_string_to_list (token
, list
);
2713 while (spctabnl (s
[i
]))
2721 i
++; /* normal character */
2723 return (REVERSE_LIST (list
, WORD_LIST
*));
2727 /********************************************************/
2729 /* Functions to perform assignment statements */
2731 /********************************************************/
2733 #if defined (ARRAY_VARS)
2735 do_compound_assignment (name
, value
, flags
)
2740 int mklocal
, mkassoc
, mkglobal
;
2743 mklocal
= flags
& ASS_MKLOCAL
;
2744 mkassoc
= flags
& ASS_MKASSOC
;
2745 mkglobal
= flags
& ASS_MKGLOBAL
;
2747 if (mklocal
&& variable_context
)
2749 v
= find_variable (name
);
2750 list
= expand_compound_array_assignment (v
, value
, flags
);
2752 v
= make_local_assoc_variable (name
);
2753 else if (v
== 0 || (array_p (v
) == 0 && assoc_p (v
) == 0) || v
->context
!= variable_context
)
2754 v
= make_local_array_variable (name
, 0);
2756 assign_compound_array_list (v
, list
, flags
);
2758 /* In a function but forcing assignment in global context */
2759 else if (mkglobal
&& variable_context
)
2761 v
= find_global_variable (name
);
2762 list
= expand_compound_array_assignment (v
, value
, flags
);
2763 if (v
== 0 && mkassoc
)
2764 v
= make_new_assoc_variable (name
);
2765 else if (v
&& mkassoc
&& assoc_p (v
) == 0)
2766 v
= convert_var_to_assoc (v
);
2768 v
= make_new_array_variable (name
);
2769 else if (v
&& mkassoc
== 0 && array_p (v
) == 0)
2770 v
= convert_var_to_array (v
);
2772 assign_compound_array_list (v
, list
, flags
);
2775 v
= assign_array_from_string (name
, value
, flags
);
2781 /* Given STRING, an assignment string, get the value of the right side
2782 of the `=', and bind it to the left side. If EXPAND is true, then
2783 perform parameter expansion, command substitution, and arithmetic
2784 expansion on the right-hand side. Perform tilde expansion in any
2785 case. Do not perform word splitting on the result of expansion. */
2787 do_assignment_internal (word
, expand
)
2788 const WORD_DESC
*word
;
2791 int offset
, appendop
, assign_list
, aflags
, retval
;
2792 char *name
, *value
, *temp
;
2794 #if defined (ARRAY_VARS)
2800 if (word
== 0 || word
->word
== 0)
2803 appendop
= assign_list
= aflags
= 0;
2804 string
= word
->word
;
2805 offset
= assignment (string
, 0);
2806 name
= savestring (string
);
2807 value
= (char *)NULL
;
2809 if (name
[offset
] == '=')
2811 if (name
[offset
- 1] == '+')
2814 name
[offset
- 1] = '\0';
2817 name
[offset
] = 0; /* might need this set later */
2818 temp
= name
+ offset
+ 1;
2820 #if defined (ARRAY_VARS)
2821 if (expand
&& (word
->flags
& W_COMPASSIGN
))
2823 assign_list
= ni
= 1;
2824 value
= extract_array_assignment_list (temp
, &ni
);
2828 if (expand
&& temp
[0])
2829 value
= expand_string_if_necessary (temp
, 0, expand_string_assignment
);
2831 value
= savestring (temp
);
2836 value
= (char *)xmalloc (1);
2840 if (echo_command_at_execute
)
2843 name
[offset
- 1] = '+';
2844 xtrace_print_assignment (name
, value
, assign_list
, 1);
2846 name
[offset
- 1] = '\0';
2849 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
2852 aflags
|= ASS_APPEND
;
2854 #if defined (ARRAY_VARS)
2855 if (t
= mbschr (name
, '[')) /*]*/
2859 report_error (_("%s: cannot assign list to array member"), name
);
2862 entry
= assign_array_element (name
, value
, aflags
);
2866 else if (assign_list
)
2868 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_ASSNGLOBAL
) == 0)
2869 aflags
|= ASS_MKLOCAL
;
2870 if ((word
->flags
& W_ASSIGNARG
) && (word
->flags
& W_ASSNGLOBAL
))
2871 aflags
|= ASS_MKGLOBAL
;
2872 if (word
->flags
& W_ASSIGNASSOC
)
2873 aflags
|= ASS_MKASSOC
;
2874 entry
= do_compound_assignment (name
, value
, aflags
);
2877 #endif /* ARRAY_VARS */
2878 entry
= bind_variable (name
, value
, aflags
);
2880 stupidly_hack_special_variables (name
);
2882 /* Return 1 if the assignment seems to have been performed correctly. */
2883 if (entry
== 0 || readonly_p (entry
))
2884 retval
= 0; /* assignment failure */
2885 else if (noassign_p (entry
))
2887 last_command_exit_value
= EXECUTION_FAILURE
;
2888 retval
= 1; /* error status, but not assignment failure */
2893 if (entry
&& retval
!= 0 && noassign_p (entry
) == 0)
2894 VUNSETATTR (entry
, att_invisible
);
2896 ASSIGN_RETURN (retval
);
2899 /* Perform the assignment statement in STRING, and expand the
2900 right side by doing tilde, command and parameter expansion. */
2902 do_assignment (string
)
2907 td
.flags
= W_ASSIGNMENT
;
2910 return do_assignment_internal (&td
, 1);
2914 do_word_assignment (word
, flags
)
2918 return do_assignment_internal (word
, 1);
2921 /* Given STRING, an assignment string, get the value of the right side
2922 of the `=', and bind it to the left side. Do not perform any word
2923 expansions on the right hand side. */
2925 do_assignment_no_expand (string
)
2930 td
.flags
= W_ASSIGNMENT
;
2933 return (do_assignment_internal (&td
, 0));
2936 /***************************************************
2938 * Functions to manage the positional parameters *
2940 ***************************************************/
2942 /* Return the word list that corresponds to `$*'. */
2944 list_rest_of_args ()
2946 register WORD_LIST
*list
, *args
;
2949 /* Break out of the loop as soon as one of the dollar variables is null. */
2950 for (i
= 1, list
= (WORD_LIST
*)NULL
; i
< 10 && dollar_vars
[i
]; i
++)
2951 list
= make_word_list (make_bare_word (dollar_vars
[i
]), list
);
2953 for (args
= rest_of_args
; args
; args
= args
->next
)
2954 list
= make_word_list (make_bare_word (args
->word
->word
), list
);
2956 return (REVERSE_LIST (list
, WORD_LIST
*));
2962 register WORD_LIST
*list
;
2965 for (n
= 0; n
< 9 && dollar_vars
[n
+1]; n
++)
2967 for (list
= rest_of_args
; list
; list
= list
->next
)
2972 /* Return the value of a positional parameter. This handles values > 10. */
2974 get_dollar_var_value (ind
)
2981 temp
= dollar_vars
[ind
] ? savestring (dollar_vars
[ind
]) : (char *)NULL
;
2982 else /* We want something like ${11} */
2985 for (p
= rest_of_args
; p
&& ind
--; p
= p
->next
)
2987 temp
= p
? savestring (p
->word
->word
) : (char *)NULL
;
2992 /* Make a single large string out of the dollar digit variables,
2993 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
2994 case of "$*" with respect to IFS. */
2996 string_rest_of_args (dollar_star
)
2999 register WORD_LIST
*list
;
3002 list
= list_rest_of_args ();
3003 string
= dollar_star
? string_list_dollar_star (list
) : string_list (list
);
3004 dispose_words (list
);
3008 /* Return a string containing the positional parameters from START to
3009 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
3010 which only makes a difference if QUOTED is non-zero. If QUOTED includes
3011 Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
3012 no quoting chars are added. */
3014 pos_params (string
, start
, end
, quoted
)
3016 int start
, end
, quoted
;
3018 WORD_LIST
*save
, *params
, *h
, *t
;
3022 /* see if we can short-circuit. if start == end, we want 0 parameters. */
3024 return ((char *)NULL
);
3026 save
= params
= list_rest_of_args ();
3027 if (save
== 0 && start
> 0)
3028 return ((char *)NULL
);
3030 if (start
== 0) /* handle ${@:0[:x]} specially */
3032 t
= make_word_list (make_word (dollar_vars
[0]), params
);
3036 for (i
= start
? 1 : 0; params
&& i
< start
; i
++)
3037 params
= params
->next
;
3040 dispose_words (save
);
3041 return ((char *)NULL
);
3043 for (h
= t
= params
; params
&& i
< end
; i
++)
3046 params
= params
->next
;
3049 t
->next
= (WORD_LIST
*)NULL
;
3051 ret
= string_list_pos_params (string
[0], h
, quoted
);
3056 dispose_words (save
);
3060 /******************************************************************/
3062 /* Functions to expand strings to strings or WORD_LISTs */
3064 /******************************************************************/
3066 #if defined (PROCESS_SUBSTITUTION)
3067 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
3069 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
3072 /* If there are any characters in STRING that require full expansion,
3073 then call FUNC to expand STRING; otherwise just perform quote
3074 removal if necessary. This returns a new string. */
3076 expand_string_if_necessary (string
, quoted
, func
)
3087 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3088 slen
= (MB_CUR_MAX
> 1) ? strlen (string
) : 0;
3092 if (EXP_CHAR (string
[i
]))
3094 else if (string
[i
] == '\'' || string
[i
] == '\\' || string
[i
] == '"')
3096 ADVANCE_CHAR (string
, slen
, i
);
3101 list
= (*func
) (string
, quoted
);
3104 ret
= string_list (list
);
3105 dispose_words (list
);
3110 else if (saw_quote
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
3111 ret
= string_quote_removal (string
, quoted
);
3113 ret
= savestring (string
);
3118 static inline char *
3119 expand_string_to_string_internal (string
, quoted
, func
)
3127 if (string
== 0 || *string
== '\0')
3128 return ((char *)NULL
);
3130 list
= (*func
) (string
, quoted
);
3133 ret
= string_list (list
);
3134 dispose_words (list
);
3143 expand_string_to_string (string
, quoted
)
3147 return (expand_string_to_string_internal (string
, quoted
, expand_string
));
3151 expand_string_unsplit_to_string (string
, quoted
)
3155 return (expand_string_to_string_internal (string
, quoted
, expand_string_unsplit
));
3159 expand_assignment_string_to_string (string
, quoted
)
3163 return (expand_string_to_string_internal (string
, quoted
, expand_string_assignment
));
3167 expand_arith_string (string
, quoted
)
3172 WORD_LIST
*list
, *tlist
;
3178 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3179 slen
= (MB_CUR_MAX
> 1) ? strlen (string
) : 0;
3183 if (EXP_CHAR (string
[i
]))
3185 else if (string
[i
] == '\'' || string
[i
] == '\\' || string
[i
] == '"')
3187 ADVANCE_CHAR (string
, slen
, i
);
3192 /* This is expanded version of expand_string_internal as it's called by
3193 expand_string_leave_quoted */
3194 td
.flags
= W_NOPROCSUB
; /* don't want process substitution */
3195 td
.word
= savestring (string
);
3196 list
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3197 /* This takes care of the calls from expand_string_leave_quoted and
3201 tlist
= word_list_split (list
);
3202 dispose_words (list
);
3205 dequote_list (list
);
3207 /* This comes from expand_string_if_necessary */
3210 ret
= string_list (list
);
3211 dispose_words (list
);
3217 else if (saw_quote
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
3218 ret
= string_quote_removal (string
, quoted
);
3220 ret
= savestring (string
);
3225 #if defined (COND_COMMAND)
3226 /* Just remove backslashes in STRING. Returns a new string. */
3228 remove_backslashes (string
)
3233 r
= ret
= (char *)xmalloc (strlen (string
) + 1);
3234 for (s
= string
; s
&& *s
; )
3246 /* This needs better error handling. */
3247 /* Expand W for use as an argument to a unary or binary operator in a
3248 [[...]] expression. If SPECIAL is 1, this is the rhs argument
3249 to the != or == operator, and should be treated as a pattern. In
3250 this case, we quote the string specially for the globbing code. If
3251 SPECIAL is 2, this is an rhs argument for the =~ operator, and should
3252 be quoted appropriately for regcomp/regexec. The caller is responsible
3253 for removing the backslashes if the unquoted word is needed later. */
3255 cond_expand_word (w
, special
)
3263 if (w
->word
== 0 || w
->word
[0] == '\0')
3264 return ((char *)NULL
);
3266 expand_no_split_dollar_star
= 1;
3267 w
->flags
|= W_NOSPLIT2
;
3268 l
= call_expand_word_internal (w
, 0, 0, (int *)0, (int *)0);
3269 expand_no_split_dollar_star
= 0;
3272 if (special
== 0) /* LHS */
3275 r
= string_list (l
);
3279 /* Need to figure out whether or not we should call dequote_escapes
3280 or a new dequote_ctlnul function here, and under what
3282 qflags
= QGLOB_CVTNULL
;
3284 qflags
|= QGLOB_REGEXP
;
3285 p
= string_list (l
);
3286 r
= quote_string_for_globbing (p
, qflags
);
3298 /* Call expand_word_internal to expand W and handle error returns.
3299 A convenience function for functions that don't want to handle
3300 any errors or free any memory before aborting. */
3302 call_expand_word_internal (w
, q
, i
, c
, e
)
3308 result
= expand_word_internal (w
, q
, i
, c
, e
);
3309 if (result
== &expand_word_error
|| result
== &expand_word_fatal
)
3311 /* By convention, each time this error is returned, w->word has
3312 already been freed (it sometimes may not be in the fatal case,
3313 but that doesn't result in a memory leak because we're going
3314 to exit in most cases). */
3315 w
->word
= (char *)NULL
;
3316 last_command_exit_value
= EXECUTION_FAILURE
;
3317 exp_jump_to_top_level ((result
== &expand_word_error
) ? DISCARD
: FORCE_EOF
);
3325 /* Perform parameter expansion, command substitution, and arithmetic
3326 expansion on STRING, as if it were a word. Leave the result quoted.
3327 Since this does not perform word splitting, it leaves quoted nulls
3330 expand_string_internal (string
, quoted
)
3337 if (string
== 0 || *string
== 0)
3338 return ((WORD_LIST
*)NULL
);
3341 td
.word
= savestring (string
);
3343 tresult
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3349 /* Expand STRING by performing parameter expansion, command substitution,
3350 and arithmetic expansion. Dequote the resulting WORD_LIST before
3351 returning it, but do not perform word splitting. The call to
3352 remove_quoted_nulls () is in here because word splitting normally
3353 takes care of quote removal. */
3355 expand_string_unsplit (string
, quoted
)
3361 if (string
== 0 || *string
== '\0')
3362 return ((WORD_LIST
*)NULL
);
3364 expand_no_split_dollar_star
= 1;
3365 value
= expand_string_internal (string
, quoted
);
3366 expand_no_split_dollar_star
= 0;
3372 remove_quoted_nulls (value
->word
->word
);
3373 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3375 dequote_list (value
);
3380 /* Expand the rhs of an assignment statement */
3382 expand_string_assignment (string
, quoted
)
3389 if (string
== 0 || *string
== '\0')
3390 return ((WORD_LIST
*)NULL
);
3392 expand_no_split_dollar_star
= 1;
3394 td
.flags
= W_ASSIGNRHS
;
3395 td
.word
= savestring (string
);
3396 value
= call_expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3399 expand_no_split_dollar_star
= 0;
3405 remove_quoted_nulls (value
->word
->word
);
3406 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3408 dequote_list (value
);
3414 /* Expand one of the PS? prompt strings. This is a sort of combination of
3415 expand_string_unsplit and expand_string_internal, but returns the
3416 passed string when an error occurs. Might want to trap other calls
3417 to jump_to_top_level here so we don't endlessly loop. */
3419 expand_prompt_string (string
, quoted
, wflags
)
3427 if (string
== 0 || *string
== 0)
3428 return ((WORD_LIST
*)NULL
);
3431 td
.word
= savestring (string
);
3433 no_longjmp_on_fatal_error
= 1;
3434 value
= expand_word_internal (&td
, quoted
, 0, (int *)NULL
, (int *)NULL
);
3435 no_longjmp_on_fatal_error
= 0;
3437 if (value
== &expand_word_error
|| value
== &expand_word_fatal
)
3439 value
= make_word_list (make_bare_word (string
), (WORD_LIST
*)NULL
);
3447 remove_quoted_nulls (value
->word
->word
);
3448 value
->word
->flags
&= ~W_HASQUOTEDNULL
;
3450 dequote_list (value
);
3455 /* Expand STRING just as if you were expanding a word, but do not dequote
3456 the resultant WORD_LIST. This is called only from within this file,
3457 and is used to correctly preserve quoted characters when expanding
3458 things like ${1+"$@"}. This does parameter expansion, command
3459 substitution, arithmetic expansion, and word splitting. */
3461 expand_string_leave_quoted (string
, quoted
)
3468 if (string
== 0 || *string
== '\0')
3469 return ((WORD_LIST
*)NULL
);
3471 tlist
= expand_string_internal (string
, quoted
);
3475 tresult
= word_list_split (tlist
);
3476 dispose_words (tlist
);
3479 return ((WORD_LIST
*)NULL
);
3482 /* This does not perform word splitting or dequote the WORD_LIST
3485 expand_string_for_rhs (string
, quoted
, dollar_at_p
, has_dollar_at
)
3487 int quoted
, *dollar_at_p
, *has_dollar_at
;
3492 if (string
== 0 || *string
== '\0')
3493 return (WORD_LIST
*)NULL
;
3495 td
.flags
= W_NOSPLIT2
; /* no splitting, remove "" and '' */
3497 tresult
= call_expand_word_internal (&td
, quoted
, 1, dollar_at_p
, has_dollar_at
);
3501 /* Expand STRING just as if you were expanding a word. This also returns
3502 a list of words. Note that filename globbing is *NOT* done for word
3503 or string expansion, just when the shell is expanding a command. This
3504 does parameter expansion, command substitution, arithmetic expansion,
3505 and word splitting. Dequote the resultant WORD_LIST before returning. */
3507 expand_string (string
, quoted
)
3513 if (string
== 0 || *string
== '\0')
3514 return ((WORD_LIST
*)NULL
);
3516 result
= expand_string_leave_quoted (string
, quoted
);
3517 return (result
? dequote_list (result
) : result
);
3520 /***************************************************
3522 * Functions to handle quoting chars *
3524 ***************************************************/
3528 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
3529 The parser passes CTLNUL as CTLESC CTLNUL. */
3531 /* Quote escape characters in string s, but no other characters. This is
3532 used to protect CTLESC and CTLNUL in variable values from the rest of
3533 the word expansion process after the variable is expanded (word splitting
3534 and filename generation). If IFS is null, we quote spaces as well, just
3535 in case we split on spaces later (in the case of unquoted $@, we will
3536 eventually attempt to split the entire word on spaces). Corresponding
3537 code exists in dequote_escapes. Even if we don't end up splitting on
3538 spaces, quoting spaces is not a problem. This should never be called on
3539 a string that is quoted with single or double quotes or part of a here
3540 document (effectively double-quoted). */
3542 quote_escapes (string
)
3545 register char *s
, *t
;
3547 char *result
, *send
;
3548 int quote_spaces
, skip_ctlesc
, skip_ctlnul
;
3551 slen
= strlen (string
);
3552 send
= string
+ slen
;
3554 quote_spaces
= (ifs_value
&& *ifs_value
== 0);
3556 for (skip_ctlesc
= skip_ctlnul
= 0, s
= ifs_value
; s
&& *s
; s
++)
3557 skip_ctlesc
|= *s
== CTLESC
, skip_ctlnul
|= *s
== CTLNUL
;
3559 t
= result
= (char *)xmalloc ((slen
* 2) + 1);
3564 if ((skip_ctlesc
== 0 && *s
== CTLESC
) || (skip_ctlnul
== 0 && *s
== CTLNUL
) || (quote_spaces
&& *s
== ' '))
3566 COPY_CHAR_P (t
, s
, send
);
3574 list_quote_escapes (list
)
3577 register WORD_LIST
*w
;
3580 for (w
= list
; w
; w
= w
->next
)
3583 w
->word
->word
= quote_escapes (t
);
3589 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
3591 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
3592 This is necessary to make unquoted CTLESC and CTLNUL characters in the
3593 data stream pass through properly.
3595 We need to remove doubled CTLESC characters inside quoted strings before
3596 quoting the entire string, so we do not double the number of CTLESC
3599 Also used by parts of the pattern substitution code. */
3601 dequote_escapes (string
)
3604 register char *s
, *t
, *s1
;
3606 char *result
, *send
;
3613 slen
= strlen (string
);
3614 send
= string
+ slen
;
3616 t
= result
= (char *)xmalloc (slen
+ 1);
3618 if (strchr (string
, CTLESC
) == 0)
3619 return (strcpy (result
, string
));
3621 quote_spaces
= (ifs_value
&& *ifs_value
== 0);
3626 if (*s
== CTLESC
&& (s
[1] == CTLESC
|| s
[1] == CTLNUL
|| (quote_spaces
&& s
[1] == ' ')))
3632 COPY_CHAR_P (t
, s
, send
);
3640 list_dequote_escapes (list
)
3643 register WORD_LIST
*w
;
3646 for (w
= list
; w
; w
= w
->next
)
3649 w
->word
->word
= dequote_escapes (t
);
3655 /* Return a new string with the quoted representation of character C.
3656 This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
3657 set in any resultant WORD_DESC where this value is the word. */
3659 make_quoted_char (c
)
3664 temp
= (char *)xmalloc (3);
3679 /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
3680 the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
3681 this value is the word. */
3683 quote_string (string
)
3688 char *result
, *send
;
3692 result
= (char *)xmalloc (2);
3700 slen
= strlen (string
);
3701 send
= string
+ slen
;
3703 result
= (char *)xmalloc ((slen
* 2) + 1);
3705 for (t
= result
; string
< send
; )
3708 COPY_CHAR_P (t
, string
, send
);
3715 /* De-quote quoted characters in STRING. */
3717 dequote_string (string
)
3720 register char *s
, *t
;
3722 char *result
, *send
;
3725 slen
= strlen (string
);
3727 t
= result
= (char *)xmalloc (slen
+ 1);
3729 if (QUOTED_NULL (string
))
3735 /* If no character in the string can be quoted, don't bother examining
3736 each character. Just return a copy of the string passed to us. */
3737 if (strchr (string
, CTLESC
) == NULL
)
3738 return (strcpy (result
, string
));
3740 send
= string
+ slen
;
3750 COPY_CHAR_P (t
, s
, send
);
3757 /* Quote the entire WORD_LIST list. */
3762 register WORD_LIST
*w
;
3765 for (w
= list
; w
; w
= w
->next
)
3768 w
->word
->word
= quote_string (t
);
3770 w
->word
->flags
|= W_HASQUOTEDNULL
; /* XXX - turn on W_HASQUOTEDNULL here? */
3771 w
->word
->flags
|= W_QUOTED
;
3777 /* De-quote quoted characters in each word in LIST. */
3783 register WORD_LIST
*tlist
;
3785 for (tlist
= list
; tlist
; tlist
= tlist
->next
)
3787 s
= dequote_string (tlist
->word
->word
);
3788 if (QUOTED_NULL (tlist
->word
->word
))
3789 tlist
->word
->flags
&= ~W_HASQUOTEDNULL
;
3790 free (tlist
->word
->word
);
3791 tlist
->word
->word
= s
;
3796 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
3799 remove_quoted_escapes (string
)
3806 t
= dequote_escapes (string
);
3814 /* Perform quoted null character removal on STRING. We don't allow any
3815 quoted null characters in the middle or at the ends of strings because
3816 of how expand_word_internal works. remove_quoted_nulls () turns
3817 STRING into an empty string iff it only consists of a quoted null,
3818 and removes all unquoted CTLNUL characters. */
3820 remove_quoted_nulls (string
)
3823 register size_t slen
;
3824 register int i
, j
, prev_i
;
3827 if (strchr (string
, CTLNUL
) == 0) /* XXX */
3828 return string
; /* XXX */
3830 slen
= strlen (string
);
3835 if (string
[i
] == CTLESC
)
3837 /* Old code had j++, but we cannot assume that i == j at this
3838 point -- what if a CTLNUL has already been removed from the
3839 string? We don't want to drop the CTLESC or recopy characters
3840 that we've already copied down. */
3841 i
++; string
[j
++] = CTLESC
;
3845 else if (string
[i
] == CTLNUL
)
3852 ADVANCE_CHAR (string
, slen
, i
);
3855 do string
[j
++] = string
[prev_i
++]; while (prev_i
< i
);
3865 /* Perform quoted null character removal on each element of LIST.
3866 This modifies LIST. */
3868 word_list_remove_quoted_nulls (list
)
3871 register WORD_LIST
*t
;
3873 for (t
= list
; t
; t
= t
->next
)
3875 remove_quoted_nulls (t
->word
->word
);
3876 t
->word
->flags
&= ~W_HASQUOTEDNULL
;
3880 /* **************************************************************** */
3882 /* Functions for Matching and Removing Patterns */
3884 /* **************************************************************** */
3886 #if defined (HANDLE_MULTIBYTE)
3887 #if 0 /* Currently unused */
3888 static unsigned char *
3889 mb_getcharlens (string
, len
)
3893 int i
, offset
, last
;
3900 ret
= (unsigned char *)xmalloc (len
);
3901 memset (ret
, 0, len
);
3902 while (string
[last
])
3904 ADVANCE_CHAR (string
, len
, offset
);
3905 ret
[last
] = offset
- last
;
3913 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
3914 can have one of 4 values:
3915 RP_LONG_LEFT remove longest matching portion at start of PARAM
3916 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
3917 RP_LONG_RIGHT remove longest matching portion at end of PARAM
3918 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
3921 #define RP_LONG_LEFT 1
3922 #define RP_SHORT_LEFT 2
3923 #define RP_LONG_RIGHT 3
3924 #define RP_SHORT_RIGHT 4
3926 /* Returns its first argument if nothing matched; new memory otherwise */
3928 remove_upattern (param
, pattern
, op
)
3929 char *param
, *pattern
;
3934 register char *p
, *ret
, c
;
3936 len
= STRLEN (param
);
3941 case RP_LONG_LEFT
: /* remove longest match at start */
3942 for (p
= end
; p
>= param
; p
--)
3945 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
3948 return (savestring (p
));
3955 case RP_SHORT_LEFT
: /* remove shortest match at start */
3956 for (p
= param
; p
<= end
; p
++)
3959 if (strmatch (pattern
, param
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
3962 return (savestring (p
));
3968 case RP_LONG_RIGHT
: /* remove longest match at end */
3969 for (p
= param
; p
<= end
; p
++)
3971 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
3974 ret
= savestring (param
);
3981 case RP_SHORT_RIGHT
: /* remove shortest match at end */
3982 for (p
= end
; p
>= param
; p
--)
3984 if (strmatch (pattern
, p
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
3987 ret
= savestring (param
);
3995 return (param
); /* no match, return original string */
3998 #if defined (HANDLE_MULTIBYTE)
3999 /* Returns its first argument if nothing matched; new memory otherwise */
4001 remove_wpattern (wparam
, wstrlen
, wpattern
, op
)
4012 case RP_LONG_LEFT
: /* remove longest match at start */
4013 for (n
= wstrlen
; n
>= 0; n
--)
4015 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4016 if (wcsmatch (wpattern
, wparam
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4019 return (wcsdup (wparam
+ n
));
4025 case RP_SHORT_LEFT
: /* remove shortest match at start */
4026 for (n
= 0; n
<= wstrlen
; n
++)
4028 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4029 if (wcsmatch (wpattern
, wparam
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4032 return (wcsdup (wparam
+ n
));
4038 case RP_LONG_RIGHT
: /* remove longest match at end */
4039 for (n
= 0; n
<= wstrlen
; n
++)
4041 if (wcsmatch (wpattern
, wparam
+ n
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4043 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4044 ret
= wcsdup (wparam
);
4051 case RP_SHORT_RIGHT
: /* remove shortest match at end */
4052 for (n
= wstrlen
; n
>= 0; n
--)
4054 if (wcsmatch (wpattern
, wparam
+ n
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
)
4056 wc
= wparam
[n
]; wparam
[n
] = L
'\0';
4057 ret
= wcsdup (wparam
);
4065 return (wparam
); /* no match, return original string */
4067 #endif /* HANDLE_MULTIBYTE */
4070 remove_pattern (param
, pattern
, op
)
4071 char *param
, *pattern
;
4078 if (*param
== '\0' || pattern
== NULL
|| *pattern
== '\0') /* minor optimization */
4079 return (savestring (param
));
4081 #if defined (HANDLE_MULTIBYTE)
4084 wchar_t *ret
, *oret
;
4086 wchar_t *wparam
, *wpattern
;
4089 n
= xdupmbstowcs (&wpattern
, NULL
, pattern
);
4090 if (n
== (size_t)-1)
4092 xret
= remove_upattern (param
, pattern
, op
);
4093 return ((xret
== param
) ? savestring (param
) : xret
);
4095 n
= xdupmbstowcs (&wparam
, NULL
, param
);
4097 if (n
== (size_t)-1)
4100 xret
= remove_upattern (param
, pattern
, op
);
4101 return ((xret
== param
) ? savestring (param
) : xret
);
4103 oret
= ret
= remove_wpattern (wparam
, n
, wpattern
, op
);
4104 /* Don't bother to convert wparam back to multibyte string if nothing
4105 matched; just return copy of original string */
4110 return (savestring (param
));
4117 xret
= (char *)xmalloc (n
+ 1);
4118 memset (&ps
, '\0', sizeof (mbstate_t));
4119 n
= wcsrtombs (xret
, (const wchar_t **)&ret
, n
, &ps
);
4120 xret
[n
] = '\0'; /* just to make sure */
4127 xret
= remove_upattern (param
, pattern
, op
);
4128 return ((xret
== param
) ? savestring (param
) : xret
);
4132 /* Match PAT anywhere in STRING and return the match boundaries.
4133 This returns 1 in case of a successful match, 0 otherwise. SP
4134 and EP are pointers into the string where the match begins and
4135 ends, respectively. MTYPE controls what kind of match is attempted.
4136 MATCH_BEG and MATCH_END anchor the match at the beginning and end
4137 of the string, respectively. The longest match is returned. */
4139 match_upattern (string
, pat
, mtype
, sp
, ep
)
4145 register char *p
, *p1
, *npat
;
4149 /* If the pattern doesn't match anywhere in the string, go ahead and
4150 short-circuit right away. A minor optimization, saves a bunch of
4151 unnecessary calls to strmatch (up to N calls for a string of N
4152 characters) if the match is unsuccessful. To preserve the semantics
4153 of the substring matches below, we make sure that the pattern has
4154 `*' as first and last character, making a new pattern if necessary. */
4155 /* XXX - check this later if I ever implement `**' with special meaning,
4156 since this will potentially result in `**' at the beginning or end */
4158 if (pat
[0] != '*' || (pat
[0] == '*' && pat
[1] == LPAREN
&& extended_glob
) || pat
[len
- 1] != '*')
4160 p
= npat
= (char *)xmalloc (len
+ 3);
4162 if (*p1
!= '*' || (*p1
== '*' && p1
[1] == LPAREN
&& extended_glob
))
4166 if (p1
[-1] != '*' || p
[-2] == '\\')
4172 c
= strmatch (npat
, string
, FNMATCH_EXTFLAG
);
4175 if (c
== FNM_NOMATCH
)
4178 len
= STRLEN (string
);
4181 mlen
= umatchlen (pat
, len
);
4186 for (p
= string
; p
<= end
; p
++)
4188 if (match_pattern_char (pat
, p
))
4190 p1
= (mlen
== -1) ? end
: p
+ mlen
;
4191 /* p1 - p = length of portion of string to be considered
4192 p = current position in string
4193 mlen = number of characters consumed by match (-1 for entire string)
4195 we want to break immediately if the potential match len
4196 is greater than the number of characters remaining in the
4201 for ( ; p1
>= p
; p1
--)
4203 c
= *p1
; *p1
= '\0';
4204 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
) == 0)
4213 /* If MLEN != -1, we have a fixed length pattern. */
4224 if (match_pattern_char (pat
, string
) == 0)
4227 for (p
= (mlen
== -1) ? end
: string
+ mlen
; p
>= string
; p
--)
4230 if (strmatch (pat
, string
, FNMATCH_EXTFLAG
) == 0)
4238 /* If MLEN != -1, we have a fixed length pattern. */
4246 for (p
= end
- ((mlen
== -1) ? len
: mlen
); p
<= end
; p
++)
4248 if (strmatch (pat
, p
, FNMATCH_EXTFLAG
) == 0)
4254 /* If MLEN != -1, we have a fixed length pattern. */
4265 #if defined (HANDLE_MULTIBYTE)
4266 /* Match WPAT anywhere in WSTRING and return the match boundaries.
4267 This returns 1 in case of a successful match, 0 otherwise. Wide
4268 character version. */
4270 match_wpattern (wstring
, indices
, wstrlen
, wpat
, mtype
, sp
, ep
)
4278 wchar_t wc
, *wp
, *nwpat
, *wp1
;
4281 int n
, n1
, n2
, simple
;
4283 simple
= (wpat
[0] != L
'\\' && wpat
[0] != L
'*' && wpat
[0] != L
'?' && wpat
[0] != L
'[');
4284 #if defined (EXTENDED_GLOB)
4286 simple
&= (wpat
[1] != L
'(' || (wpat
[0] != L
'*' && wpat
[0] != L
'?' && wpat
[0] != L
'+' && wpat
[0] != L
'!' && wpat
[0] != L
'@')); /*)*/
4289 /* If the pattern doesn't match anywhere in the string, go ahead and
4290 short-circuit right away. A minor optimization, saves a bunch of
4291 unnecessary calls to strmatch (up to N calls for a string of N
4292 characters) if the match is unsuccessful. To preserve the semantics
4293 of the substring matches below, we make sure that the pattern has
4294 `*' as first and last character, making a new pattern if necessary. */
4295 len
= wcslen (wpat
);
4296 if (wpat
[0] != L
'*' || (wpat
[0] == L
'*' && wpat
[1] == WLPAREN
&& extended_glob
) || wpat
[len
- 1] != L
'*')
4298 wp
= nwpat
= (wchar_t *)xmalloc ((len
+ 3) * sizeof (wchar_t));
4300 if (*wp1
!= L
'*' || (*wp1
== '*' && wp1
[1] == WLPAREN
&& extended_glob
))
4302 while (*wp1
!= L
'\0')
4304 if (wp1
[-1] != L
'*' || wp1
[-2] == L
'\\')
4310 len
= wcsmatch (nwpat
, wstring
, FNMATCH_EXTFLAG
);
4313 if (len
== FNM_NOMATCH
)
4316 mlen
= wmatchlen (wpat
, wstrlen
);
4318 /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */
4322 for (n
= 0; n
<= wstrlen
; n
++)
4324 n2
= simple
? (*wpat
== wstring
[n
]) : match_pattern_wchar (wpat
, wstring
+ n
);
4327 n1
= (mlen
== -1) ? wstrlen
: n
+ mlen
;
4331 for ( ; n1
>= n
; n1
--)
4333 wc
= wstring
[n1
]; wstring
[n1
] = L
'\0';
4334 if (wcsmatch (wpat
, wstring
+ n
, FNMATCH_EXTFLAG
) == 0)
4342 /* If MLEN != -1, we have a fixed length pattern. */
4352 if (match_pattern_wchar (wpat
, wstring
) == 0)
4355 for (n
= (mlen
== -1) ? wstrlen
: mlen
; n
>= 0; n
--)
4357 wc
= wstring
[n
]; wstring
[n
] = L
'\0';
4358 if (wcsmatch (wpat
, wstring
, FNMATCH_EXTFLAG
) == 0)
4366 /* If MLEN != -1, we have a fixed length pattern. */
4374 for (n
= wstrlen
- ((mlen
== -1) ? wstrlen
: mlen
); n
<= wstrlen
; n
++)
4376 if (wcsmatch (wpat
, wstring
+ n
, FNMATCH_EXTFLAG
) == 0)
4379 *ep
= indices
[wstrlen
];
4382 /* If MLEN != -1, we have a fixed length pattern. */
4392 #endif /* HANDLE_MULTIBYTE */
4395 match_pattern (string
, pat
, mtype
, sp
, ep
)
4400 #if defined (HANDLE_MULTIBYTE)
4403 wchar_t *wstring
, *wpat
;
4405 size_t slen
, plen
, mslen
, mplen
;
4408 if (string
== 0 || *string
== 0 || pat
== 0 || *pat
== 0)
4411 #if defined (HANDLE_MULTIBYTE)
4414 if (mbsmbchar (string
) == 0 && mbsmbchar (pat
) == 0)
4415 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
4417 n
= xdupmbstowcs (&wpat
, NULL
, pat
);
4418 if (n
== (size_t)-1)
4419 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
4420 n
= xdupmbstowcs (&wstring
, &indices
, string
);
4421 if (n
== (size_t)-1)
4424 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
4426 ret
= match_wpattern (wstring
, indices
, n
, wpat
, mtype
, sp
, ep
);
4436 return (match_upattern (string
, pat
, mtype
, sp
, ep
));
4440 getpatspec (c
, value
)
4445 return ((*value
== '#') ? RP_LONG_LEFT
: RP_SHORT_LEFT
);
4447 return ((*value
== '%') ? RP_LONG_RIGHT
: RP_SHORT_RIGHT
);
4450 /* Posix.2 says that the WORD should be run through tilde expansion,
4451 parameter expansion, command substitution and arithmetic expansion.
4452 This leaves the result quoted, so quote_string_for_globbing () has
4453 to be called to fix it up for strmatch (). If QUOTED is non-zero,
4454 it means that the entire expression was enclosed in double quotes.
4455 This means that quoting characters in the pattern do not make any
4456 special pattern characters quoted. For example, the `*' in the
4457 following retains its special meaning: "${foo#'*'}". */
4459 getpattern (value
, quoted
, expandpat
)
4461 int quoted
, expandpat
;
4468 /* There is a problem here: how to handle single or double quotes in the
4469 pattern string when the whole expression is between double quotes?
4470 POSIX.2 says that enclosing double quotes do not cause the pattern to
4471 be quoted, but does that leave us a problem with @ and array[@] and their
4472 expansions inside a pattern? */
4474 if (expandpat
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *tword
)
4477 pat
= string_extract_double_quoted (tword
, &i
, 1);
4483 /* expand_string_for_rhs () leaves WORD quoted and does not perform
4485 l
= *value
? expand_string_for_rhs (value
,
4486 (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) ? Q_PATQUOTE
: quoted
,
4487 (int *)NULL
, (int *)NULL
)
4489 pat
= string_list (l
);
4493 tword
= quote_string_for_globbing (pat
, QGLOB_CVTNULL
);
4501 /* Handle removing a pattern from a string as a result of ${name%[%]value}
4502 or ${name#[#]value}. */
4504 variable_remove_pattern (value
, pattern
, patspec
, quoted
)
4505 char *value
, *pattern
;
4506 int patspec
, quoted
;
4510 tword
= remove_pattern (value
, pattern
, patspec
);
4517 list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
)
4520 int patspec
, itype
, quoted
;
4526 for (new = (WORD_LIST
*)NULL
, l
= list
; l
; l
= l
->next
)
4528 tword
= remove_pattern (l
->word
->word
, pattern
, patspec
);
4529 w
= alloc_word_desc ();
4530 w
->word
= tword
? tword
: savestring ("");
4531 new = make_word_list (w
, new);
4534 l
= REVERSE_LIST (new, WORD_LIST
*);
4535 tword
= string_list_pos_params (itype
, l
, quoted
);
4542 parameter_list_remove_pattern (itype
, pattern
, patspec
, quoted
)
4545 int patspec
, quoted
;
4550 list
= list_rest_of_args ();
4552 return ((char *)NULL
);
4553 ret
= list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
);
4554 dispose_words (list
);
4558 #if defined (ARRAY_VARS)
4560 array_remove_pattern (var
, pattern
, patspec
, varname
, quoted
)
4564 char *varname
; /* so we can figure out how it's indexed */
4574 /* compute itype from varname here */
4575 v
= array_variable_part (varname
, &ret
, 0);
4578 if (v
&& invisible_p (var
))
4579 return ((char *)NULL
);
4583 a
= (v
&& array_p (v
)) ? array_cell (v
) : 0;
4584 h
= (v
&& assoc_p (v
)) ? assoc_cell (v
) : 0;
4586 list
= a
? array_to_word_list (a
) : (h
? assoc_to_word_list (h
) : 0);
4588 return ((char *)NULL
);
4589 ret
= list_remove_pattern (list
, pattern
, patspec
, itype
, quoted
);
4590 dispose_words (list
);
4594 #endif /* ARRAY_VARS */
4597 parameter_brace_remove_pattern (varname
, value
, ind
, patstr
, rtype
, quoted
, flags
)
4598 char *varname
, *value
;
4601 int rtype
, quoted
, flags
;
4603 int vtype
, patspec
, starsub
;
4604 char *temp1
, *val
, *pattern
;
4608 return ((char *)NULL
);
4610 this_command_name
= varname
;
4612 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
4614 return ((char *)NULL
);
4616 starsub
= vtype
& VT_STARSUB
;
4617 vtype
&= ~VT_STARSUB
;
4619 patspec
= getpatspec (rtype
, patstr
);
4620 if (patspec
== RP_LONG_LEFT
|| patspec
== RP_LONG_RIGHT
)
4623 /* Need to pass getpattern newly-allocated memory in case of expansion --
4624 the expansion code will free the passed string on an error. */
4625 temp1
= savestring (patstr
);
4626 pattern
= getpattern (temp1
, quoted
, 1);
4629 temp1
= (char *)NULL
; /* shut up gcc */
4633 case VT_ARRAYMEMBER
:
4634 temp1
= remove_pattern (val
, pattern
, patspec
);
4635 if (vtype
== VT_VARIABLE
)
4639 val
= (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
4640 ? quote_string (temp1
)
4641 : quote_escapes (temp1
);
4646 #if defined (ARRAY_VARS)
4648 temp1
= array_remove_pattern (v
, pattern
, patspec
, varname
, quoted
);
4649 if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
4651 val
= quote_escapes (temp1
);
4658 temp1
= parameter_list_remove_pattern (varname
[0], pattern
, patspec
, quoted
);
4659 if (temp1
&& ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) == 0))
4661 val
= quote_escapes (temp1
);
4672 /*******************************************
4674 * Functions to expand WORD_DESCs *
4676 *******************************************/
4678 /* Expand WORD, performing word splitting on the result. This does
4679 parameter expansion, command substitution, arithmetic expansion,
4680 word splitting, and quote removal. */
4683 expand_word (word
, quoted
)
4687 WORD_LIST
*result
, *tresult
;
4689 tresult
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
4690 result
= word_list_split (tresult
);
4691 dispose_words (tresult
);
4692 return (result
? dequote_list (result
) : result
);
4695 /* Expand WORD, but do not perform word splitting on the result. This
4696 does parameter expansion, command substitution, arithmetic expansion,
4697 and quote removal. */
4699 expand_word_unsplit (word
, quoted
)
4705 expand_no_split_dollar_star
= 1;
4706 #if defined (HANDLE_MULTIBYTE)
4707 if (ifs_firstc
[0] == 0)
4709 if (ifs_firstc
== 0)
4711 word
->flags
|= W_NOSPLIT
;
4712 word
->flags
|= W_NOSPLIT2
;
4713 result
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
4714 expand_no_split_dollar_star
= 0;
4716 return (result
? dequote_list (result
) : result
);
4719 /* Perform shell expansions on WORD, but do not perform word splitting or
4720 quote removal on the result. Virtually identical to expand_word_unsplit;
4721 could be combined if implementations don't diverge. */
4723 expand_word_leave_quoted (word
, quoted
)
4729 expand_no_split_dollar_star
= 1;
4730 #if defined (HANDLE_MULTIBYTE)
4731 if (ifs_firstc
[0] == 0)
4733 if (ifs_firstc
== 0)
4735 word
->flags
|= W_NOSPLIT
;
4736 word
->flags
|= W_NOSPLIT2
;
4737 result
= call_expand_word_internal (word
, quoted
, 0, (int *)NULL
, (int *)NULL
);
4738 expand_no_split_dollar_star
= 0;
4743 #if defined (PROCESS_SUBSTITUTION)
4745 /*****************************************************************/
4747 /* Hacking Process Substitution */
4749 /*****************************************************************/
4751 #if !defined (HAVE_DEV_FD)
4752 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
4753 of FIFOs the shell has open. unlink_fifo_list will walk the list and
4754 unlink all of them. add_fifo_list adds the name of an open FIFO to the
4755 list. NFIFO is a count of the number of FIFOs in the list. */
4756 #define FIFO_INCR 20
4763 static struct temp_fifo
*fifo_list
= (struct temp_fifo
*)NULL
;
4765 static int fifo_list_size
;
4773 copy_fifo_list (sizep
)
4778 return (char *)NULL
;
4782 add_fifo_list (pathname
)
4785 if (nfifo
>= fifo_list_size
- 1)
4787 fifo_list_size
+= FIFO_INCR
;
4788 fifo_list
= (struct temp_fifo
*)xrealloc (fifo_list
,
4789 fifo_list_size
* sizeof (struct temp_fifo
));
4792 fifo_list
[nfifo
].file
= savestring (pathname
);
4800 if ((fifo_list
[i
].proc
== -1) || (kill(fifo_list
[i
].proc
, 0) == -1))
4802 unlink (fifo_list
[i
].file
);
4803 free (fifo_list
[i
].file
);
4804 fifo_list
[i
].file
= (char *)NULL
;
4805 fifo_list
[i
].proc
= -1;
4817 for (i
= saved
= 0; i
< nfifo
; i
++)
4819 if ((fifo_list
[i
].proc
== -1) || (kill(fifo_list
[i
].proc
, 0) == -1))
4821 unlink (fifo_list
[i
].file
);
4822 free (fifo_list
[i
].file
);
4823 fifo_list
[i
].file
= (char *)NULL
;
4824 fifo_list
[i
].proc
= -1;
4830 /* If we didn't remove some of the FIFOs, compact the list. */
4833 for (i
= j
= 0; i
< nfifo
; i
++)
4834 if (fifo_list
[i
].file
)
4836 fifo_list
[j
].file
= fifo_list
[i
].file
;
4837 fifo_list
[j
].proc
= fifo_list
[i
].proc
;
4846 /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list
4847 from some point in the past, and close all open FIFOs in fifo_list
4848 that are not marked as active in LIST. If LIST is NULL, close
4849 everything in fifo_list. LSIZE is the number of elements in LIST, in
4850 case it's larger than fifo_list_size (size of fifo_list). */
4852 close_new_fifos (list
, lsize
)
4860 unlink_fifo_list ();
4864 for (i
= 0; i
< lsize
; i
++)
4865 if (list
[i
] == 0 && i
< fifo_list_size
&& fifo_list
[i
].proc
!= -1)
4868 for (i
= lsize
; i
< fifo_list_size
; i
++)
4889 tname
= sh_mktmpname ("sh-np", MT_USERANDOM
|MT_USETMPDIR
);
4890 if (mkfifo (tname
, 0600) < 0)
4893 return ((char *)NULL
);
4896 add_fifo_list (tname
);
4900 #else /* HAVE_DEV_FD */
4902 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
4903 has open to children. NFDS is a count of the number of bits currently
4904 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
4906 static char *dev_fd_list
= (char *)NULL
;
4908 static int totfds
; /* The highest possible number of open files. */
4929 for (i
= 0; nfds
&& i
< totfds
; i
++)
4936 copy_fifo_list (sizep
)
4941 if (nfds
== 0 || totfds
== 0)
4945 return (char *)NULL
;
4950 ret
= (char *)xmalloc (totfds
);
4951 return (memcpy (ret
, dev_fd_list
, totfds
));
4958 if (dev_fd_list
== 0 || fd
>= totfds
)
4963 totfds
= getdtablesize ();
4964 if (totfds
< 0 || totfds
> 256)
4969 dev_fd_list
= (char *)xrealloc (dev_fd_list
, totfds
);
4970 memset (dev_fd_list
+ ofds
, '\0', totfds
- ofds
);
4973 dev_fd_list
[fd
] = 1;
4980 return 0; /* used for cleanup; not needed with /dev/fd */
4993 if (dev_fd_list
[fd
])
4996 dev_fd_list
[fd
] = 0;
5009 for (i
= 0; nfds
&& i
< totfds
; i
++)
5015 /* Take LIST, which is a snapshot copy of dev_fd_list from some point in
5016 the past, and close all open fds in dev_fd_list that are not marked
5017 as open in LIST. If LIST is NULL, close everything in dev_fd_list.
5018 LSIZE is the number of elements in LIST, in case it's larger than
5019 totfds (size of dev_fd_list). */
5021 close_new_fifos (list
, lsize
)
5029 unlink_fifo_list ();
5033 for (i
= 0; i
< lsize
; i
++)
5034 if (list
[i
] == 0 && i
< totfds
&& dev_fd_list
[i
])
5037 for (i
= lsize
; i
< totfds
; i
++)
5041 #if defined (NOTDEF)
5042 print_dev_fd_list ()
5046 fprintf (stderr
, "pid %ld: dev_fd_list:", (long)getpid ());
5049 for (i
= 0; i
< totfds
; i
++)
5052 fprintf (stderr
, " %d", i
);
5054 fprintf (stderr
, "\n");
5059 make_dev_fd_filename (fd
)
5062 char *ret
, intbuf
[INT_STRLEN_BOUND (int) + 1], *p
;
5064 ret
= (char *)xmalloc (sizeof (DEV_FD_PREFIX
) + 8);
5066 strcpy (ret
, DEV_FD_PREFIX
);
5067 p
= inttostr (fd
, intbuf
, sizeof (intbuf
));
5068 strcpy (ret
+ sizeof (DEV_FD_PREFIX
) - 1, p
);
5074 #endif /* HAVE_DEV_FD */
5076 /* Return a filename that will open a connection to the process defined by
5077 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
5078 a filename in /dev/fd corresponding to a descriptor that is one of the
5079 ends of the pipe. If not defined, we use named pipes on systems that have
5080 them. Systems without /dev/fd and named pipes are out of luck.
5082 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
5083 use the read end of the pipe and dup that file descriptor to fd 0 in
5084 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
5085 writing or use the write end of the pipe in the child, and dup that
5086 file descriptor to fd 1 in the child. The parent does the opposite. */
5089 process_substitute (string
, open_for_read_in_child
)
5091 int open_for_read_in_child
;
5096 #if defined (HAVE_DEV_FD)
5097 int parent_pipe_fd
, child_pipe_fd
;
5099 #endif /* HAVE_DEV_FD */
5100 #if defined (JOB_CONTROL)
5101 pid_t old_pipeline_pgrp
;
5104 if (!string
|| !*string
|| wordexp_only
)
5105 return ((char *)NULL
);
5107 #if !defined (HAVE_DEV_FD)
5108 pathname
= make_named_pipe ();
5109 #else /* HAVE_DEV_FD */
5110 if (pipe (fildes
) < 0)
5112 sys_error (_("cannot make pipe for process substitution"));
5113 return ((char *)NULL
);
5115 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
5116 the pipe in the parent, otherwise the read end. */
5117 parent_pipe_fd
= fildes
[open_for_read_in_child
];
5118 child_pipe_fd
= fildes
[1 - open_for_read_in_child
];
5119 /* Move the parent end of the pipe to some high file descriptor, to
5120 avoid clashes with FDs used by the script. */
5121 parent_pipe_fd
= move_to_high_fd (parent_pipe_fd
, 1, 64);
5123 pathname
= make_dev_fd_filename (parent_pipe_fd
);
5124 #endif /* HAVE_DEV_FD */
5128 sys_error (_("cannot make pipe for process substitution"));
5129 return ((char *)NULL
);
5132 old_pid
= last_made_pid
;
5134 #if defined (JOB_CONTROL)
5135 old_pipeline_pgrp
= pipeline_pgrp
;
5136 pipeline_pgrp
= shell_pgrp
;
5138 #endif /* JOB_CONTROL */
5140 pid
= make_child ((char *)NULL
, 1);
5143 reset_terminating_signals (); /* XXX */
5144 free_pushed_string_input ();
5145 /* Cancel traps, in trap.c. */
5146 restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */
5147 setup_async_signals ();
5148 subshell_environment
|= SUBSHELL_COMSUB
|SUBSHELL_PROCSUB
;
5151 #if defined (JOB_CONTROL)
5152 set_sigchld_handler ();
5153 stop_making_children ();
5154 /* XXX - should we only do this in the parent? (as in command subst) */
5155 pipeline_pgrp
= old_pipeline_pgrp
;
5156 #endif /* JOB_CONTROL */
5160 sys_error (_("cannot make child for process substitution"));
5162 #if defined (HAVE_DEV_FD)
5163 close (parent_pipe_fd
);
5164 close (child_pipe_fd
);
5165 #endif /* HAVE_DEV_FD */
5166 return ((char *)NULL
);
5171 #if defined (JOB_CONTROL)
5172 restore_pipeline (1);
5175 #if !defined (HAVE_DEV_FD)
5176 fifo_list
[nfifo
-1].proc
= pid
;
5179 last_made_pid
= old_pid
;
5181 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
5183 #endif /* JOB_CONTROL && PGRP_PIPE */
5185 #if defined (HAVE_DEV_FD)
5186 close (child_pipe_fd
);
5187 #endif /* HAVE_DEV_FD */
5192 set_sigint_handler ();
5194 #if defined (JOB_CONTROL)
5195 set_job_control (0);
5196 #endif /* JOB_CONTROL */
5198 #if !defined (HAVE_DEV_FD)
5199 /* Open the named pipe in the child. */
5200 fd
= open (pathname
, open_for_read_in_child
? O_RDONLY
: O_WRONLY
);
5203 /* Two separate strings for ease of translation. */
5204 if (open_for_read_in_child
)
5205 sys_error (_("cannot open named pipe %s for reading"), pathname
);
5207 sys_error (_("cannot open named pipe %s for writing"), pathname
);
5211 if (open_for_read_in_child
)
5213 if (sh_unset_nodelay_mode (fd
) < 0)
5215 sys_error (_("cannot reset nodelay mode for fd %d"), fd
);
5219 #else /* HAVE_DEV_FD */
5221 #endif /* HAVE_DEV_FD */
5223 if (dup2 (fd
, open_for_read_in_child
? 0 : 1) < 0)
5225 sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname
,
5226 open_for_read_in_child
? 0 : 1);
5230 if (fd
!= (open_for_read_in_child
? 0 : 1))
5233 /* Need to close any files that this process has open to pipes inherited
5235 if (current_fds_to_close
)
5237 close_fd_bitmap (current_fds_to_close
);
5238 current_fds_to_close
= (struct fd_bitmap
*)NULL
;
5241 #if defined (HAVE_DEV_FD)
5242 /* Make sure we close the parent's end of the pipe and clear the slot
5243 in the fd list so it is not closed later, if reallocated by, for
5244 instance, pipe(2). */
5245 close (parent_pipe_fd
);
5246 dev_fd_list
[parent_pipe_fd
] = 0;
5247 #endif /* HAVE_DEV_FD */
5249 /* subshells shouldn't have this flag, which controls using the temporary
5250 environment for variable lookups. */
5251 expanding_redir
= 0;
5253 result
= parse_and_execute (string
, "process substitution", (SEVAL_NONINT
|SEVAL_NOHIST
));
5255 #if !defined (HAVE_DEV_FD)
5256 /* Make sure we close the named pipe in the child before we exit. */
5257 close (open_for_read_in_child
? 0 : 1);
5258 #endif /* !HAVE_DEV_FD */
5260 last_command_exit_value
= result
;
5261 result
= run_exit_trap ();
5265 #endif /* PROCESS_SUBSTITUTION */
5267 /***********************************/
5269 /* Command Substitution */
5271 /***********************************/
5274 read_comsub (fd
, quoted
, rflag
)
5278 char *istring
, buf
[128], *bufp
, *s
;
5279 int istring_index
, istring_size
, c
, tflag
, skip_ctlesc
, skip_ctlnul
;
5282 istring
= (char *)NULL
;
5283 istring_index
= istring_size
= bufn
= tflag
= 0;
5285 for (skip_ctlesc
= skip_ctlnul
= 0, s
= ifs_value
; s
&& *s
; s
++)
5286 skip_ctlesc
|= *s
== CTLESC
, skip_ctlnul
|= *s
== CTLNUL
;
5288 /* Read the output of the command through the pipe. This may need to be
5289 changed to understand multibyte characters in the future. */
5296 bufn
= zread (fd
, buf
, sizeof (buf
));
5306 internal_warning ("read_comsub: ignored null byte in input");
5311 /* Add the character to ISTRING, possibly after resizing it. */
5312 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
, DEFAULT_ARRAY_SIZE
);
5314 /* This is essentially quote_string inline */
5315 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) /* || c == CTLESC || c == CTLNUL */)
5316 istring
[istring_index
++] = CTLESC
;
5317 /* Escape CTLESC and CTLNUL in the output to protect those characters
5318 from the rest of the word expansions (word splitting and globbing.)
5319 This is essentially quote_escapes inline. */
5320 else if (skip_ctlesc
== 0 && c
== CTLESC
)
5322 tflag
|= W_HASCTLESC
;
5323 istring
[istring_index
++] = CTLESC
;
5325 else if ((skip_ctlnul
== 0 && c
== CTLNUL
) || (c
== ' ' && (ifs_value
&& *ifs_value
== 0)))
5326 istring
[istring_index
++] = CTLESC
;
5328 istring
[istring_index
++] = c
;
5331 #if defined (__CYGWIN__)
5332 if (c
== '\n' && istring_index
> 1 && istring
[istring_index
- 2] == '\r')
5335 istring
[istring_index
- 1] = '\n';
5342 istring
[istring_index
] = '\0';
5344 /* If we read no output, just return now and save ourselves some
5346 if (istring_index
== 0)
5351 return (char *)NULL
;
5354 /* Strip trailing newlines from the output of the command. */
5355 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
5357 while (istring_index
> 0)
5359 if (istring
[istring_index
- 1] == '\n')
5363 /* If the newline was quoted, remove the quoting char. */
5364 if (istring
[istring_index
- 1] == CTLESC
)
5370 istring
[istring_index
] = '\0';
5373 strip_trailing (istring
, istring_index
- 1, 1);
5380 /* Perform command substitution on STRING. This returns a WORD_DESC * with the
5381 contained string possibly quoted. */
5383 command_substitute (string
, quoted
)
5387 pid_t pid
, old_pid
, old_pipeline_pgrp
, old_async_pid
;
5389 int result
, fildes
[2], function_value
, pflags
, rc
, tflag
;
5392 istring
= (char *)NULL
;
5394 /* Don't fork () if there is no need to. In the case of no command to
5395 run, just return NULL. */
5396 if (!string
|| !*string
|| (string
[0] == '\n' && !string
[1]))
5397 return ((WORD_DESC
*)NULL
);
5399 if (wordexp_only
&& read_but_dont_execute
)
5401 last_command_exit_value
= EX_WEXPCOMSUB
;
5402 jump_to_top_level (EXITPROG
);
5405 /* We're making the assumption here that the command substitution will
5406 eventually run a command from the file system. Since we'll run
5407 maybe_make_export_env in this subshell before executing that command,
5408 the parent shell and any other shells it starts will have to remake
5409 the environment. If we make it before we fork, other shells won't
5410 have to. Don't bother if we have any temporary variable assignments,
5411 though, because the export environment will be remade after this
5412 command completes anyway, but do it if all the words to be expanded
5413 are variable assignments. */
5414 if (subst_assign_varlist
== 0 || garglist
== 0)
5415 maybe_make_export_env (); /* XXX */
5417 /* Flags to pass to parse_and_execute() */
5418 pflags
= (interactive
&& sourcelevel
== 0) ? SEVAL_RESETLINE
: 0;
5420 /* Pipe the output of executing STRING into the current shell. */
5421 if (pipe (fildes
) < 0)
5423 sys_error (_("cannot make pipe for command substitution"));
5427 old_pid
= last_made_pid
;
5428 #if defined (JOB_CONTROL)
5429 old_pipeline_pgrp
= pipeline_pgrp
;
5430 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
5431 if ((subshell_environment
& SUBSHELL_PIPE
) == 0)
5432 pipeline_pgrp
= shell_pgrp
;
5433 cleanup_the_pipeline ();
5434 #endif /* JOB_CONTROL */
5436 old_async_pid
= last_asynchronous_pid
;
5437 pid
= make_child ((char *)NULL
, subshell_environment
&SUBSHELL_ASYNC
);
5438 last_asynchronous_pid
= old_async_pid
;
5442 /* Reset the signal handlers in the child, but don't free the
5443 trap strings. Set a flag noting that we have to free the
5444 trap strings if we run trap to change a signal disposition. */
5445 reset_signal_handlers ();
5446 subshell_environment
|= SUBSHELL_RESETTRAP
;
5449 #if defined (JOB_CONTROL)
5450 /* XXX DO THIS ONLY IN PARENT ? XXX */
5451 set_sigchld_handler ();
5452 stop_making_children ();
5454 pipeline_pgrp
= old_pipeline_pgrp
;
5456 stop_making_children ();
5457 #endif /* JOB_CONTROL */
5461 sys_error (_("cannot make child for command substitution"));
5464 last_made_pid
= old_pid
;
5469 return ((WORD_DESC
*)NULL
);
5474 set_sigint_handler (); /* XXX */
5476 free_pushed_string_input ();
5478 if (dup2 (fildes
[1], 1) < 0)
5480 sys_error (_("command_substitute: cannot duplicate pipe as fd 1"));
5481 exit (EXECUTION_FAILURE
);
5484 /* If standard output is closed in the parent shell
5485 (such as after `exec >&-'), file descriptor 1 will be
5486 the lowest available file descriptor, and end up in
5487 fildes[0]. This can happen for stdin and stderr as well,
5488 but stdout is more important -- it will cause no output
5489 to be generated from this command. */
5490 if ((fildes
[1] != fileno (stdin
)) &&
5491 (fildes
[1] != fileno (stdout
)) &&
5492 (fildes
[1] != fileno (stderr
)))
5495 if ((fildes
[0] != fileno (stdin
)) &&
5496 (fildes
[0] != fileno (stdout
)) &&
5497 (fildes
[0] != fileno (stderr
)))
5501 /* Let stdio know the fd may have changed from text to binary mode, and
5502 make sure to preserve stdout line buffering. */
5503 freopen (NULL
, "w", stdout
);
5504 sh_setlinebuf (stdout
);
5505 #endif /* __CYGWIN__ */
5507 /* The currently executing shell is not interactive. */
5510 /* This is a subshell environment. */
5511 subshell_environment
|= SUBSHELL_COMSUB
;
5513 /* Many shells do not appear to inherit the -v option for command
5515 change_flag ('v', FLAG_OFF
);
5517 /* When not in POSIX mode, command substitution does not inherit
5519 if (posixly_correct
== 0)
5521 builtin_ignoring_errexit
= 0;
5522 change_flag ('e', FLAG_OFF
);
5526 remove_quoted_escapes (string
);
5528 startup_state
= 2; /* see if we can avoid a fork */
5529 /* Give command substitution a place to jump back to on failure,
5530 so we don't go back up to main (). */
5531 result
= setjmp_nosigs (top_level
);
5533 /* If we're running a command substitution inside a shell function,
5534 trap `return' so we don't return from the function in the subshell
5535 and go off to never-never land. */
5536 if (result
== 0 && return_catch_flag
)
5537 function_value
= setjmp_nosigs (return_catch
);
5541 if (result
== ERREXIT
)
5542 rc
= last_command_exit_value
;
5543 else if (result
== EXITPROG
)
5544 rc
= last_command_exit_value
;
5546 rc
= EXECUTION_FAILURE
;
5547 else if (function_value
)
5548 rc
= return_catch_value
;
5552 rc
= parse_and_execute (string
, "command substitution", pflags
|SEVAL_NOHIST
);
5556 last_command_exit_value
= rc
;
5557 rc
= run_exit_trap ();
5558 #if defined (PROCESS_SUBSTITUTION)
5559 unlink_fifo_list ();
5565 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
5567 #endif /* JOB_CONTROL && PGRP_PIPE */
5572 istring
= read_comsub (fildes
[0], quoted
, &tflag
);
5576 current_command_subst_pid
= pid
;
5577 last_command_exit_value
= wait_for (pid
);
5578 last_command_subst_pid
= pid
;
5579 last_made_pid
= old_pid
;
5581 #if defined (JOB_CONTROL)
5582 /* If last_command_exit_value > 128, then the substituted command
5583 was terminated by a signal. If that signal was SIGINT, then send
5584 SIGINT to ourselves. This will break out of loops, for instance. */
5585 if (last_command_exit_value
== (128 + SIGINT
) && last_command_exit_signal
== SIGINT
)
5586 kill (getpid (), SIGINT
);
5588 /* wait_for gives the terminal back to shell_pgrp. If some other
5589 process group should have it, give it away to that group here.
5590 pipeline_pgrp is non-zero only while we are constructing a
5591 pipeline, so what we are concerned about is whether or not that
5592 pipeline was started in the background. A pipeline started in
5593 the background should never get the tty back here. */
5594 if (interactive
&& pipeline_pgrp
!= (pid_t
)0 && (subshell_environment
& SUBSHELL_ASYNC
) == 0)
5595 give_terminal_to (pipeline_pgrp
, 0);
5596 #endif /* JOB_CONTROL */
5598 ret
= alloc_word_desc ();
5599 ret
->word
= istring
;
5606 /********************************************************
5608 * Utility functions for parameter expansion *
5610 ********************************************************/
5612 #if defined (ARRAY_VARS)
5615 array_length_reference (s
)
5626 var
= array_variable_part (s
, &t
, &len
);
5628 /* If unbound variables should generate an error, report one and return
5630 if ((var
== 0 || invisible_p (var
) || (assoc_p (var
) == 0 && array_p (var
) == 0)) && unbound_vars_is_error
)
5634 last_command_exit_value
= EXECUTION_FAILURE
;
5639 else if (var
== 0 || invisible_p (var
))
5642 /* We support a couple of expansions for variables that are not arrays.
5643 We'll return the length of the value for v[0], and 1 for v[@] or
5644 v[*]. Return 0 for everything else. */
5646 array
= array_p (var
) ? array_cell (var
) : (ARRAY
*)NULL
;
5647 h
= assoc_p (var
) ? assoc_cell (var
) : (HASH_TABLE
*)NULL
;
5649 if (ALL_ELEMENT_SUB (t
[0]) && t
[1] == ']')
5652 return (h
? assoc_num_elements (h
) : 0);
5653 else if (array_p (var
))
5654 return (array
? array_num_elements (array
) : 0);
5656 return (var_isset (var
) ? 1 : 0);
5662 akey
= expand_assignment_string_to_string (t
, 0); /* [ */
5664 if (akey
== 0 || *akey
== 0)
5666 err_badarraysub (t
);
5670 t
= assoc_reference (assoc_cell (var
), akey
);
5675 ind
= array_expand_index (var
, t
, len
);
5676 /* negative subscripts to indexed arrays count back from end */
5677 if (var
&& array_p (var
) && ind
< 0)
5678 ind
= array_max_index (array_cell (var
)) + 1 + ind
;
5681 err_badarraysub (t
);
5685 t
= array_reference (array
, ind
);
5687 t
= (ind
== 0) ? value_cell (var
) : (char *)NULL
;
5690 len
= MB_STRLEN (t
);
5693 #endif /* ARRAY_VARS */
5696 valid_brace_expansion_word (name
, var_is_special
)
5700 if (DIGIT (*name
) && all_digits (name
))
5702 else if (var_is_special
)
5704 #if defined (ARRAY_VARS)
5705 else if (valid_array_reference (name
))
5707 #endif /* ARRAY_VARS */
5708 else if (legal_identifier (name
))
5715 chk_atstar (name
, quoted
, quoted_dollar_atp
, contains_dollar_at
)
5718 int *quoted_dollar_atp
, *contains_dollar_at
;
5724 if (quoted_dollar_atp
)
5725 *quoted_dollar_atp
= 0;
5726 if (contains_dollar_at
)
5727 *contains_dollar_at
= 0;
5731 /* check for $@ and $* */
5732 if (name
[0] == '@' && name
[1] == 0)
5734 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
5735 *quoted_dollar_atp
= 1;
5736 if (contains_dollar_at
)
5737 *contains_dollar_at
= 1;
5740 else if (name
[0] == '*' && name
[1] == '\0' && quoted
== 0)
5742 if (contains_dollar_at
)
5743 *contains_dollar_at
= 1;
5747 /* Now check for ${array[@]} and ${array[*]} */
5748 #if defined (ARRAY_VARS)
5749 else if (valid_array_reference (name
))
5751 temp1
= mbschr (name
, '[');
5752 if (temp1
&& temp1
[1] == '@' && temp1
[2] == ']')
5754 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
5755 *quoted_dollar_atp
= 1;
5756 if (contains_dollar_at
)
5757 *contains_dollar_at
= 1;
5760 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
5761 which should result in separate words even when IFS is unset. */
5762 if (temp1
&& temp1
[1] == '*' && temp1
[2] == ']' && quoted
== 0)
5764 if (contains_dollar_at
)
5765 *contains_dollar_at
= 1;
5773 /* Parameter expand NAME, and return a new string which is the expansion,
5774 or NULL if there was no expansion.
5775 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
5776 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
5777 NAME was found inside of a double-quoted expression. */
5779 parameter_brace_expand_word (name
, var_is_special
, quoted
, pflags
, indp
)
5781 int var_is_special
, quoted
, pflags
;
5798 /* Handle multiple digit arguments, as in ${11}. */
5799 if (legal_number (name
, &arg_index
))
5801 tt
= get_dollar_var_value (arg_index
);
5803 temp
= (*tt
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
5805 : quote_escapes (tt
);
5807 temp
= (char *)NULL
;
5810 else if (var_is_special
) /* ${@} */
5813 tt
= (char *)xmalloc (2 + strlen (name
));
5814 tt
[sindex
= 0] = '$';
5815 strcpy (tt
+ 1, name
);
5817 ret
= param_expand (tt
, &sindex
, quoted
, (int *)NULL
, (int *)NULL
,
5818 (int *)NULL
, (int *)NULL
, pflags
);
5821 #if defined (ARRAY_VARS)
5822 else if (valid_array_reference (name
))
5825 /* XXX - does this leak if name[@] or name[*]? */
5826 if (pflags
& PF_ASSIGNRHS
)
5828 temp
= array_variable_name (name
, &tt
, (int *)0);
5829 if (ALL_ELEMENT_SUB (tt
[0]) && tt
[1] == ']')
5830 temp
= array_value (name
, quoted
|Q_DOUBLE_QUOTES
, 0, &atype
, &ind
);
5832 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
5835 temp
= array_value (name
, quoted
, 0, &atype
, &ind
);
5836 if (atype
== 0 && temp
)
5838 temp
= (*temp
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
5839 ? quote_string (temp
)
5840 : quote_escapes (temp
);
5841 rflags
|= W_ARRAYIND
;
5845 else if (atype
== 1 && temp
&& QUOTED_NULL (temp
) && (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
5846 rflags
|= W_HASQUOTEDNULL
;
5849 else if (var
= find_variable (name
))
5851 if (var_isset (var
) && invisible_p (var
) == 0)
5853 #if defined (ARRAY_VARS)
5855 temp
= assoc_reference (assoc_cell (var
), "0");
5856 else if (array_p (var
))
5857 temp
= array_reference (array_cell (var
), 0);
5859 temp
= value_cell (var
);
5861 temp
= value_cell (var
);
5865 temp
= (*temp
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
5866 ? quote_string (temp
)
5867 : quote_escapes (temp
);
5870 temp
= (char *)NULL
;
5872 else if (var
= find_variable_last_nameref (name
))
5874 temp
= nameref_cell (var
);
5875 #if defined (ARRAY_VARS)
5876 /* Handle expanding nameref whose value is x[n] */
5877 if (temp
&& *temp
&& valid_array_reference (temp
))
5880 goto expand_arrayref
;
5884 /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */
5885 if (temp
&& *temp
&& legal_identifier (temp
) == 0)
5887 last_command_exit_value
= EXECUTION_FAILURE
;
5888 report_error (_("%s: invalid variable name for name reference"), temp
);
5889 temp
= &expand_param_error
;
5892 temp
= (char *)NULL
;
5895 temp
= (char *)NULL
;
5899 ret
= alloc_word_desc ();
5901 ret
->flags
|= rflags
;
5907 parameter_brace_find_indir (name
, var_is_special
, quoted
, find_nameref
)
5909 int var_is_special
, quoted
, find_nameref
;
5915 if (find_nameref
&& var_is_special
== 0 && (v
= find_variable_last_nameref (name
)) &&
5916 nameref_p (v
) && (t
= nameref_cell (v
)) && *t
)
5917 return (savestring (t
));
5919 /* If var_is_special == 0, and name is not an array reference, this does
5920 more expansion than necessary. It should really look up the variable's
5921 value and not try to expand it. */
5922 w
= parameter_brace_expand_word (name
, var_is_special
, quoted
, PF_IGNUNBOUND
, 0);
5924 /* Have to dequote here if necessary */
5927 temp
= (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
5928 ? dequote_string (t
)
5929 : dequote_escapes (t
);
5933 dispose_word_desc (w
);
5938 /* Expand an indirect reference to a variable: ${!NAME} expands to the
5939 value of the variable whose name is the value of NAME. */
5941 parameter_brace_expand_indir (name
, var_is_special
, quoted
, quoted_dollar_atp
, contains_dollar_at
)
5943 int var_is_special
, quoted
;
5944 int *quoted_dollar_atp
, *contains_dollar_at
;
5950 /* See if it's a nameref first, behave in ksh93-compatible fashion.
5951 There is at least one incompatibility: given ${!foo[0]} where foo=bar,
5952 bash performs an indirect lookup on foo[0] and expands the result;
5953 ksh93 expands bar[0]. We could do that here -- there are enough usable
5954 primitives to do that -- but do not at this point. */
5955 if (var_is_special
== 0 && (v
= find_variable_last_nameref (name
)))
5957 if (nameref_p (v
) && (t
= nameref_cell (v
)) && *t
)
5959 w
= alloc_word_desc ();
5960 w
->word
= savestring (t
);
5966 t
= parameter_brace_find_indir (name
, var_is_special
, quoted
, 0);
5968 chk_atstar (t
, quoted
, quoted_dollar_atp
, contains_dollar_at
);
5970 return (WORD_DESC
*)NULL
;
5972 w
= parameter_brace_expand_word (t
, SPECIAL_VAR(t
, 0), quoted
, 0, 0);
5978 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
5979 depending on the value of C, the separating character. C can be one of
5980 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
5981 between double quotes. */
5983 parameter_brace_expand_rhs (name
, value
, c
, quoted
, qdollaratp
, hasdollarat
)
5985 int c
, quoted
, *qdollaratp
, *hasdollarat
;
5989 char *t
, *t1
, *temp
, *vname
;
5992 /* If the entire expression is between double quotes, we want to treat
5993 the value as a double-quoted string, with the exception that we strip
5994 embedded unescaped double quotes (for sh backwards compatibility). */
5995 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && *value
)
5998 temp
= string_extract_double_quoted (value
, &hasdol
, 1);
6003 w
= alloc_word_desc ();
6005 /* XXX was 0 not quoted */
6006 l
= *temp
? expand_string_for_rhs (temp
, quoted
, &hasdol
, (int *)NULL
)
6009 *hasdollarat
= hasdol
|| (l
&& l
->next
);
6014 /* The expansion of TEMP returned something. We need to treat things
6015 slightly differently if HASDOL is non-zero. If we have "$@", the
6016 individual words have already been quoted. We need to turn them
6017 into a string with the words separated by the first character of
6018 $IFS without any additional quoting, so string_list_dollar_at won't
6019 do the right thing. We use string_list_dollar_star instead. */
6020 temp
= (hasdol
|| l
->next
) ? string_list_dollar_star (l
) : string_list (l
);
6022 /* If l->next is not null, we know that TEMP contained "$@", since that
6023 is the only expansion that creates more than one word. */
6024 if (qdollaratp
&& ((hasdol
&& quoted
) || l
->next
))
6026 /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is
6027 a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the
6028 flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the
6029 expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6030 (which is more paranoia than anything else), we need to return the
6031 quoted null string and set the flags to indicate it. */
6032 if (l
->next
== 0 && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && QUOTED_NULL (temp
) && QUOTED_NULL (l
->word
->word
) && (l
->word
->flags
& W_HASQUOTEDNULL
))
6034 w
->flags
|= W_HASQUOTEDNULL
;
6038 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && hasdol
)
6040 /* The brace expansion occurred between double quotes and there was
6041 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
6042 it does not expand to anything. In this case, we want to return
6043 a quoted empty string. */
6044 temp
= make_quoted_char ('\0');
6045 w
->flags
|= W_HASQUOTEDNULL
;
6048 temp
= (char *)NULL
;
6050 if (c
== '-' || c
== '+')
6057 t
= temp
? savestring (temp
) : savestring ("");
6058 t1
= dequote_string (t
);
6064 (legal_variable_starter ((unsigned char)name
[1]) || DIGIT (name
[1]) || VALID_INDIR_PARAM (name
[1])))
6066 vname
= parameter_brace_find_indir (name
+ 1, SPECIAL_VAR (name
, 1), quoted
, 1);
6067 if (vname
== 0 || *vname
== 0)
6069 report_error (_("%s: invalid indirect expansion"), name
);
6072 return &expand_wdesc_error
;
6074 if (legal_identifier (vname
) == 0)
6076 report_error (_("%s: invalid variable name"), vname
);
6079 return &expand_wdesc_error
;
6083 #if defined (ARRAY_VARS)
6084 if (valid_array_reference (vname
))
6085 assign_array_element (vname
, t1
, 0);
6087 #endif /* ARRAY_VARS */
6088 bind_variable (vname
, t1
, 0);
6090 if (STREQ (vname
, "IFS") == 0)
6092 stupidly_hack_special_variables (vname
);
6097 /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */
6104 /* Deal with the right hand side of a ${name:?value} expansion in the case
6105 that NAME is null or not set. If VALUE is non-null it is expanded and
6106 used as the error message to print, otherwise a standard message is
6109 parameter_brace_expand_error (name
, value
)
6115 last_command_exit_value
= EXECUTION_FAILURE
; /* ensure it's non-zero */
6116 if (value
&& *value
)
6118 l
= expand_string (value
, 0);
6119 temp
= string_list (l
);
6120 report_error ("%s: %s", name
, temp
? temp
: ""); /* XXX was value not "" */
6125 report_error (_("%s: parameter null or not set"), name
);
6127 /* Free the data we have allocated during this expansion, since we
6128 are about to longjmp out. */
6133 /* Return 1 if NAME is something for which parameter_brace_expand_length is
6136 valid_length_expression (name
)
6139 return (name
[1] == '\0' || /* ${#} */
6140 ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0') || /* special param */
6141 (DIGIT (name
[1]) && all_digits (name
+ 1)) || /* ${#11} */
6142 #if defined (ARRAY_VARS)
6143 valid_array_reference (name
+ 1) || /* ${#a[7]} */
6145 legal_identifier (name
+ 1)); /* ${#PS1} */
6148 /* Handle the parameter brace expansion that requires us to return the
6149 length of a parameter. */
6151 parameter_brace_expand_length (name
)
6155 intmax_t number
, arg_index
;
6157 #if defined (ARRAY_VARS)
6161 if (name
[1] == '\0') /* ${#} */
6162 number
= number_of_args ();
6163 else if ((name
[1] == '@' || name
[1] == '*') && name
[2] == '\0') /* ${#@}, ${#*} */
6164 number
= number_of_args ();
6165 else if ((sh_syntaxtab
[(unsigned char) name
[1]] & CSPECVAR
) && name
[2] == '\0')
6167 /* Take the lengths of some of the shell's special parameters. */
6171 t
= which_set_flags ();
6174 t
= itos (last_command_exit_value
);
6177 t
= itos (dollar_dollar_pid
);
6180 if (last_asynchronous_pid
== NO_PID
)
6181 t
= (char *)NULL
; /* XXX - error if set -u set? */
6183 t
= itos (last_asynchronous_pid
);
6186 t
= itos (number_of_args ());
6189 number
= STRLEN (t
);
6192 #if defined (ARRAY_VARS)
6193 else if (valid_array_reference (name
+ 1))
6194 number
= array_length_reference (name
+ 1);
6195 #endif /* ARRAY_VARS */
6200 if (legal_number (name
+ 1, &arg_index
)) /* ${#1} */
6202 t
= get_dollar_var_value (arg_index
);
6203 if (t
== 0 && unbound_vars_is_error
)
6205 number
= MB_STRLEN (t
);
6208 #if defined (ARRAY_VARS)
6209 else if ((var
= find_variable (name
+ 1)) && (invisible_p (var
) == 0) && (array_p (var
) || assoc_p (var
)))
6212 t
= assoc_reference (assoc_cell (var
), "0");
6214 t
= array_reference (array_cell (var
), 0);
6215 if (t
== 0 && unbound_vars_is_error
)
6217 number
= MB_STRLEN (t
);
6222 newname
= savestring (name
);
6224 list
= expand_string (newname
, Q_DOUBLE_QUOTES
);
6225 t
= list
? string_list (list
) : (char *)NULL
;
6228 dispose_words (list
);
6230 number
= t
? MB_STRLEN (t
) : 0;
6238 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
6239 so we do some ad-hoc parsing of an arithmetic expression to find
6240 the first DELIM, instead of using strchr(3). Two rules:
6241 1. If the substring contains a `(', read until closing `)'.
6242 2. If the substring contains a `?', read past one `:' for each `?'.
6246 skiparith (substr
, delim
)
6251 int skipcol
, pcount
, i
;
6254 sublen
= strlen (substr
);
6255 i
= skipcol
= pcount
= 0;
6258 /* Balance parens */
6259 if (substr
[i
] == LPAREN
)
6265 if (substr
[i
] == RPAREN
&& pcount
)
6273 ADVANCE_CHAR (substr
, sublen
, i
);
6277 /* Skip one `:' for each `?' */
6278 if (substr
[i
] == ':' && skipcol
)
6284 if (substr
[i
] == delim
)
6286 if (substr
[i
] == '?')
6292 ADVANCE_CHAR (substr
, sublen
, i
);
6295 return (substr
+ i
);
6298 /* Verify and limit the start and end of the desired substring. If
6299 VTYPE == 0, a regular shell variable is being used; if it is 1,
6300 then the positional parameters are being used; if it is 2, then
6301 VALUE is really a pointer to an array variable that should be used.
6302 Return value is 1 if both values were OK, 0 if there was a problem
6303 with an invalid expression, or -1 if the values were out of range. */
6305 verify_substring_values (v
, value
, substr
, vtype
, e1p
, e2p
)
6307 char *value
, *substr
;
6309 intmax_t *e1p
, *e2p
;
6311 char *t
, *temp1
, *temp2
;
6314 #if defined (ARRAY_VARS)
6319 /* duplicate behavior of strchr(3) */
6320 t
= skiparith (substr
, ':');
6321 if (*t
&& *t
== ':')
6326 temp1
= expand_arith_string (substr
, Q_DOUBLE_QUOTES
);
6327 *e1p
= evalexp (temp1
, &expok
);
6332 len
= -1; /* paranoia */
6336 case VT_ARRAYMEMBER
:
6337 len
= MB_STRLEN (value
);
6340 len
= number_of_args () + 1;
6342 len
++; /* add one arg if counting from $0 */
6344 #if defined (ARRAY_VARS)
6346 /* For arrays, the first value deals with array indices. Negative
6347 offsets count from one past the array's maximum index. Associative
6348 arrays treat the number of elements as the maximum index. */
6352 len
= assoc_num_elements (h
) + (*e1p
< 0);
6357 len
= array_max_index (a
) + (*e1p
< 0); /* arrays index from 0 to n - 1 */
6363 if (len
== -1) /* paranoia */
6366 if (*e1p
< 0) /* negative offsets count from end */
6369 if (*e1p
> len
|| *e1p
< 0)
6372 #if defined (ARRAY_VARS)
6373 /* For arrays, the second offset deals with the number of elements. */
6374 if (vtype
== VT_ARRAYVAR
)
6375 len
= assoc_p (v
) ? assoc_num_elements (h
) : array_num_elements (a
);
6381 temp2
= savestring (t
);
6382 temp1
= expand_arith_string (temp2
, Q_DOUBLE_QUOTES
);
6385 *e2p
= evalexp (temp1
, &expok
);
6390 if ((vtype
== VT_ARRAYVAR
|| vtype
== VT_POSPARMS
) && *e2p
< 0)
6392 /* bash-4.3: allow positional parameter length < 0 to count backwards
6393 from end of positional parameters */
6394 if (vtype
== VT_ARRAYVAR
&& *e2p
< 0)
6397 internal_error (_("%s: substring expression < 0"), t
);
6400 #if defined (ARRAY_VARS)
6401 /* In order to deal with sparse arrays, push the intelligence about how
6402 to deal with the number of elements desired down to the array-
6403 specific functions. */
6404 if (vtype
!= VT_ARRAYVAR
)
6410 if (*e2p
< 0 || *e2p
< *e1p
)
6412 internal_error (_("%s: substring expression < 0"), t
);
6417 *e2p
+= *e1p
; /* want E2 chars starting at E1 */
6428 /* Return the type of variable specified by VARNAME (simple variable,
6429 positional param, or array variable). Also return the value specified
6430 by VARNAME (value of a variable or a reference to an array element).
6431 QUOTED is the standard description of quoting state, using Q_* defines.
6432 FLAGS is currently a set of flags to pass to array_value. If IND is
6433 non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is
6434 passed to array_value so the array index is not computed again.
6435 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
6436 characters in the value are quoted with CTLESC and takes appropriate
6437 steps. For convenience, *VALP is set to the dequoted VALUE. */
6439 get_var_and_type (varname
, value
, ind
, quoted
, flags
, varp
, valp
)
6440 char *varname
, *value
;
6446 int vtype
, want_indir
;
6449 #if defined (ARRAY_VARS)
6454 want_indir
= *varname
== '!' &&
6455 (legal_variable_starter ((unsigned char)varname
[1]) || DIGIT (varname
[1])
6456 || VALID_INDIR_PARAM (varname
[1]));
6458 vname
= parameter_brace_find_indir (varname
+1, SPECIAL_VAR (varname
, 1), quoted
, 1);
6459 /* XXX - what if vname == 0 || *vname == 0 ? */
6463 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
6464 vtype
= (vname
[0] == '@' || vname
[0] == '*') && vname
[1] == '\0';
6465 if (vtype
== VT_POSPARMS
&& vname
[0] == '*')
6466 vtype
|= VT_STARSUB
;
6467 *varp
= (SHELL_VAR
*)NULL
;
6469 #if defined (ARRAY_VARS)
6470 if (valid_array_reference (vname
))
6472 v
= array_variable_part (vname
, &temp
, (int *)0);
6473 /* If we want to signal array_value to use an already-computed index,
6474 set LIND to that index */
6475 lind
= (ind
!= INTMAX_MIN
&& (flags
& AV_USEIND
)) ? ind
: 0;
6476 if (v
&& invisible_p (v
))
6478 vtype
= VT_ARRAYMEMBER
;
6479 *varp
= (SHELL_VAR
*)NULL
;
6480 *valp
= (char *)NULL
;
6482 if (v
&& (array_p (v
) || assoc_p (v
)))
6484 if (ALL_ELEMENT_SUB (temp
[0]) && temp
[1] == ']')
6486 /* Callers have to differentiate between indexed and associative */
6487 vtype
= VT_ARRAYVAR
;
6489 vtype
|= VT_STARSUB
;
6490 *valp
= array_p (v
) ? (char *)array_cell (v
) : (char *)assoc_cell (v
);
6494 vtype
= VT_ARRAYMEMBER
;
6495 *valp
= array_value (vname
, Q_DOUBLE_QUOTES
, flags
, (int *)NULL
, &lind
);
6499 else if (v
&& (ALL_ELEMENT_SUB (temp
[0]) && temp
[1] == ']'))
6501 vtype
= VT_VARIABLE
;
6503 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
6504 *valp
= dequote_string (value
);
6506 *valp
= dequote_escapes (value
);
6510 vtype
= VT_ARRAYMEMBER
;
6512 *valp
= array_value (vname
, Q_DOUBLE_QUOTES
, flags
, (int *)NULL
, &lind
);
6515 else if ((v
= find_variable (vname
)) && (invisible_p (v
) == 0) && (assoc_p (v
) || array_p (v
)))
6517 vtype
= VT_ARRAYMEMBER
;
6519 *valp
= assoc_p (v
) ? assoc_reference (assoc_cell (v
), "0") : array_reference (array_cell (v
), 0);
6524 if (value
&& vtype
== VT_VARIABLE
)
6526 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
6527 *valp
= dequote_string (value
);
6529 *valp
= dequote_escapes (value
);
6541 /******************************************************/
6543 /* Functions to extract substrings of variable values */
6545 /******************************************************/
6547 #if defined (HANDLE_MULTIBYTE)
6548 /* Character-oriented rather than strictly byte-oriented substrings. S and
6549 E, rather being strict indices into STRING, indicate character (possibly
6550 multibyte character) positions that require calculation.
6551 Used by the ${param:offset[:length]} expansion. */
6553 mb_substring (string
, s
, e
)
6558 int start
, stop
, i
, slen
;
6562 /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
6563 slen
= (MB_CUR_MAX
> 1) ? STRLEN (string
) : 0;
6566 while (string
[start
] && i
--)
6567 ADVANCE_CHAR (string
, slen
, start
);
6570 while (string
[stop
] && i
--)
6571 ADVANCE_CHAR (string
, slen
, stop
);
6572 tt
= substring (string
, start
, stop
);
6577 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
6578 is `@', use the positional parameters; otherwise, use the value of
6579 VARNAME. If VARNAME is an array variable, use the array elements. */
6582 parameter_brace_substring (varname
, value
, ind
, substr
, quoted
, flags
)
6583 char *varname
, *value
;
6589 int vtype
, r
, starsub
;
6590 char *temp
, *val
, *tt
, *oname
;
6593 if (value
== 0 && ((varname
[0] != '@' && varname
[0] != '*') || varname
[1]))
6594 return ((char *)NULL
);
6596 oname
= this_command_name
;
6597 this_command_name
= varname
;
6599 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
6602 this_command_name
= oname
;
6603 return ((char *)NULL
);
6606 starsub
= vtype
& VT_STARSUB
;
6607 vtype
&= ~VT_STARSUB
;
6609 r
= verify_substring_values (v
, val
, substr
, vtype
, &e1
, &e2
);
6610 this_command_name
= oname
;
6613 if (vtype
== VT_VARIABLE
)
6615 return ((r
== 0) ? &expand_param_error
: (char *)NULL
);
6621 case VT_ARRAYMEMBER
:
6622 #if defined (HANDLE_MULTIBYTE)
6624 tt
= mb_substring (val
, e1
, e2
);
6627 tt
= substring (val
, e1
, e2
);
6629 if (vtype
== VT_VARIABLE
)
6631 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
6632 temp
= quote_string (tt
);
6634 temp
= tt
? quote_escapes (tt
) : (char *)NULL
;
6638 tt
= pos_params (varname
, e1
, e2
, quoted
);
6639 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) == 0)
6641 temp
= tt
? quote_escapes (tt
) : (char *)NULL
;
6647 #if defined (ARRAY_VARS)
6650 /* we convert to list and take first e2 elements starting at e1th
6651 element -- officially undefined for now */
6652 temp
= assoc_subrange (assoc_cell (v
), e1
, e2
, starsub
, quoted
);
6654 /* We want E2 to be the number of elements desired (arrays can be sparse,
6655 so verify_substring_values just returns the numbers specified and we
6656 rely on array_subrange to understand how to deal with them). */
6657 temp
= array_subrange (array_cell (v
), e1
, e2
, starsub
, quoted
);
6658 /* array_subrange now calls array_quote_escapes as appropriate, so the
6659 caller no longer needs to. */
6663 temp
= (char *)NULL
;
6669 /****************************************************************/
6671 /* Functions to perform pattern substitution on variable values */
6673 /****************************************************************/
6676 shouldexp_replacement (s
)
6681 for (p
= s
; p
&& *p
; p
++)
6692 pat_subst (string
, pat
, rep
, mflags
)
6693 char *string
, *pat
, *rep
;
6696 char *ret
, *s
, *e
, *str
, *rstr
, *mstr
;
6697 int rsize
, rptr
, l
, replen
, mtype
, rxpand
, rslen
, mlen
;
6700 return (savestring (""));
6702 mtype
= mflags
& MATCH_TYPEMASK
;
6704 #if 0 /* bash-4.2 ? */
6705 rxpand
= (rep
&& *rep
) ? shouldexp_replacement (rep
) : 0;
6711 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
6712 * with REP and return the result.
6713 * 2. A null pattern with mtype == MATCH_END means to append REP to
6714 * STRING and return the result.
6715 * These don't understand or process `&' in the replacement string.
6717 if ((pat
== 0 || *pat
== 0) && (mtype
== MATCH_BEG
|| mtype
== MATCH_END
))
6719 replen
= STRLEN (rep
);
6720 l
= STRLEN (string
);
6721 ret
= (char *)xmalloc (replen
+ l
+ 2);
6723 strcpy (ret
, string
);
6724 else if (mtype
== MATCH_BEG
)
6727 strcpy (ret
+ replen
, string
);
6731 strcpy (ret
, string
);
6732 strcpy (ret
+ l
, rep
);
6737 ret
= (char *)xmalloc (rsize
= 64);
6740 for (replen
= STRLEN (rep
), rptr
= 0, str
= string
;;)
6742 if (match_pattern (str
, pat
, mtype
, &s
, &e
) == 0)
6750 mstr
= xmalloc (mlen
+ 1);
6751 for (x
= 0; x
< mlen
; x
++)
6754 rstr
= strcreplace (rep
, '&', mstr
, 0);
6755 rslen
= strlen (rstr
);
6763 RESIZE_MALLOCED_BUFFER (ret
, rptr
, (l
+ rslen
), rsize
, 64);
6765 /* OK, now copy the leading unmatched portion of the string (from
6766 str to s) to ret starting at rptr (the current offset). Then copy
6767 the replacement string at ret + rptr + (s - str). Increment
6768 rptr (if necessary) and str and go on. */
6771 strncpy (ret
+ rptr
, str
, l
);
6776 strncpy (ret
+ rptr
, rstr
, rslen
);
6779 str
= e
; /* e == end of match */
6784 if (((mflags
& MATCH_GLOBREP
) == 0) || mtype
!= MATCH_ANY
)
6789 /* On a zero-length match, make sure we copy one character, since
6790 we increment one character to avoid infinite recursion. */
6791 RESIZE_MALLOCED_BUFFER (ret
, rptr
, 1, rsize
, 64);
6792 ret
[rptr
++] = *str
++;
6793 e
++; /* avoid infinite recursion on zero-length match */
6797 /* Now copy the unmatched portion of the input string */
6800 RESIZE_MALLOCED_BUFFER (ret
, rptr
, STRLEN(str
) + 1, rsize
, 64);
6801 strcpy (ret
+ rptr
, str
);
6809 /* Do pattern match and replacement on the positional parameters. */
6811 pos_params_pat_subst (string
, pat
, rep
, mflags
)
6812 char *string
, *pat
, *rep
;
6815 WORD_LIST
*save
, *params
;
6820 save
= params
= list_rest_of_args ();
6822 return ((char *)NULL
);
6824 for ( ; params
; params
= params
->next
)
6826 ret
= pat_subst (params
->word
->word
, pat
, rep
, mflags
);
6827 w
= alloc_word_desc ();
6828 w
->word
= ret
? ret
: savestring ("");
6829 dispose_word (params
->word
);
6833 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
6834 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
6836 ret
= string_list_pos_params (pchar
, save
, qflags
);
6838 dispose_words (save
);
6843 /* Perform pattern substitution on VALUE, which is the expansion of
6844 VARNAME. PATSUB is an expression supplying the pattern to match
6845 and the string to substitute. QUOTED is a flags word containing
6846 the type of quoting currently in effect. */
6848 parameter_brace_patsub (varname
, value
, ind
, patsub
, quoted
, flags
)
6849 char *varname
, *value
;
6854 int vtype
, mflags
, starsub
, delim
;
6855 char *val
, *temp
, *pat
, *rep
, *p
, *lpatsub
, *tt
;
6859 return ((char *)NULL
);
6861 this_command_name
= varname
;
6863 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
6865 return ((char *)NULL
);
6867 starsub
= vtype
& VT_STARSUB
;
6868 vtype
&= ~VT_STARSUB
;
6871 /* PATSUB is never NULL when this is called. */
6874 mflags
|= MATCH_GLOBREP
;
6878 /* Malloc this because expand_string_if_necessary or one of the expansion
6879 functions in its call chain may free it on a substitution error. */
6880 lpatsub
= savestring (patsub
);
6882 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
6883 mflags
|= MATCH_QUOTED
;
6886 mflags
|= MATCH_STARSUB
;
6888 /* If the pattern starts with a `/', make sure we skip over it when looking
6889 for the replacement delimiter. */
6890 delim
= skip_to_delim (lpatsub
, ((*patsub
== '/') ? 1 : 0), "/", 0);
6891 if (lpatsub
[delim
] == '/')
6894 rep
= lpatsub
+ delim
+ 1;
6899 if (rep
&& *rep
== '\0')
6902 /* Perform the same expansions on the pattern as performed by the
6903 pattern removal expansions. */
6904 pat
= getpattern (lpatsub
, quoted
, 1);
6908 /* We want to perform quote removal on the expanded replacement even if
6909 the entire expansion is double-quoted because the parser and string
6910 extraction functions treated quotes in the replacement string as
6911 special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */
6912 if (shell_compatibility_level
> 42)
6913 rep
= expand_string_if_necessary (rep
, quoted
& ~(Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
), expand_string_unsplit
);
6914 /* This is the bash-4.2 code. */
6915 else if ((mflags
& MATCH_QUOTED
) == 0)
6916 rep
= expand_string_if_necessary (rep
, quoted
, expand_string_unsplit
);
6918 rep
= expand_string_to_string_internal (rep
, quoted
, expand_string_unsplit
);
6921 /* ksh93 doesn't allow the match specifier to be a part of the expanded
6922 pattern. This is an extension. Make sure we don't anchor the pattern
6923 at the beginning or end of the string if we're doing global replacement,
6926 if (mflags
& MATCH_GLOBREP
)
6927 mflags
|= MATCH_ANY
;
6928 else if (pat
&& pat
[0] == '#')
6930 mflags
|= MATCH_BEG
;
6933 else if (pat
&& pat
[0] == '%')
6935 mflags
|= MATCH_END
;
6939 mflags
|= MATCH_ANY
;
6941 /* OK, we now want to substitute REP for PAT in VAL. If
6942 flags & MATCH_GLOBREP is non-zero, the substitution is done
6943 everywhere, otherwise only the first occurrence of PAT is
6944 replaced. The pattern matching code doesn't understand
6945 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
6946 values passed in (VT_VARIABLE) so the pattern substitution
6947 code works right. We need to requote special chars after
6948 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
6949 other cases if QUOTED == 0, since the posparams and arrays
6950 indexed by * or @ do special things when QUOTED != 0. */
6955 case VT_ARRAYMEMBER
:
6956 temp
= pat_subst (val
, p
, rep
, mflags
);
6957 if (vtype
== VT_VARIABLE
)
6961 tt
= (mflags
& MATCH_QUOTED
) ? quote_string (temp
) : quote_escapes (temp
);
6967 temp
= pos_params_pat_subst (val
, p
, rep
, mflags
);
6968 if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
6970 tt
= quote_escapes (temp
);
6975 #if defined (ARRAY_VARS)
6977 temp
= assoc_p (v
) ? assoc_patsub (assoc_cell (v
), p
, rep
, mflags
)
6978 : array_patsub (array_cell (v
), p
, rep
, mflags
);
6979 /* Don't call quote_escapes anymore; array_patsub calls
6980 array_quote_escapes as appropriate before adding the
6981 space separators; ditto for assoc_patsub. */
6993 /****************************************************************/
6995 /* Functions to perform case modification on variable values */
6997 /****************************************************************/
6999 /* Do case modification on the positional parameters. */
7002 pos_params_modcase (string
, pat
, modop
, mflags
)
7007 WORD_LIST
*save
, *params
;
7012 save
= params
= list_rest_of_args ();
7014 return ((char *)NULL
);
7016 for ( ; params
; params
= params
->next
)
7018 ret
= sh_modcase (params
->word
->word
, pat
, modop
);
7019 w
= alloc_word_desc ();
7020 w
->word
= ret
? ret
: savestring ("");
7021 dispose_word (params
->word
);
7025 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
7026 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
7028 ret
= string_list_pos_params (pchar
, save
, qflags
);
7029 dispose_words (save
);
7034 /* Perform case modification on VALUE, which is the expansion of
7035 VARNAME. MODSPEC is an expression supplying the type of modification
7036 to perform. QUOTED is a flags word containing the type of quoting
7037 currently in effect. */
7039 parameter_brace_casemod (varname
, value
, ind
, modspec
, patspec
, quoted
, flags
)
7040 char *varname
, *value
;
7045 int vtype
, starsub
, modop
, mflags
, x
;
7046 char *val
, *temp
, *pat
, *p
, *lpat
, *tt
;
7050 return ((char *)NULL
);
7052 this_command_name
= varname
;
7054 vtype
= get_var_and_type (varname
, value
, ind
, quoted
, flags
, &v
, &val
);
7056 return ((char *)NULL
);
7058 starsub
= vtype
& VT_STARSUB
;
7059 vtype
&= ~VT_STARSUB
;
7063 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
7064 mflags
|= MATCH_QUOTED
;
7066 mflags
|= MATCH_STARSUB
;
7071 x
= p
&& p
[0] == modspec
;
7072 modop
= x
? CASE_UPPER
: CASE_UPFIRST
;
7075 else if (modspec
== ',')
7077 x
= p
&& p
[0] == modspec
;
7078 modop
= x
? CASE_LOWER
: CASE_LOWFIRST
;
7081 else if (modspec
== '~')
7083 x
= p
&& p
[0] == modspec
;
7084 modop
= x
? CASE_TOGGLEALL
: CASE_TOGGLE
;
7088 lpat
= p
? savestring (p
) : 0;
7089 /* Perform the same expansions on the pattern as performed by the
7090 pattern removal expansions. FOR LATER */
7091 pat
= lpat
? getpattern (lpat
, quoted
, 1) : 0;
7093 /* OK, now we do the case modification. */
7097 case VT_ARRAYMEMBER
:
7098 temp
= sh_modcase (val
, pat
, modop
);
7099 if (vtype
== VT_VARIABLE
)
7103 tt
= (mflags
& MATCH_QUOTED
) ? quote_string (temp
) : quote_escapes (temp
);
7110 temp
= pos_params_modcase (val
, pat
, modop
, mflags
);
7111 if (temp
&& (mflags
& MATCH_QUOTED
) == 0)
7113 tt
= quote_escapes (temp
);
7119 #if defined (ARRAY_VARS)
7121 temp
= assoc_p (v
) ? assoc_modcase (assoc_cell (v
), pat
, modop
, mflags
)
7122 : array_modcase (array_cell (v
), pat
, modop
, mflags
);
7123 /* Don't call quote_escapes; array_modcase calls array_quote_escapes
7124 as appropriate before adding the space separators; ditto for
7136 /* Check for unbalanced parens in S, which is the contents of $(( ... )). If
7137 any occur, this must be a nested command substitution, so return 0.
7138 Otherwise, return 1. A valid arithmetic expression must always have a
7139 ( before a matching ), so any cases where there are more right parens
7140 means that this must not be an arithmetic expression, though the parser
7141 will not accept it without a balanced total number of parens. */
7143 chk_arithsub (s
, len
)
7155 else if (s
[i
] == RPAREN
)
7165 ADVANCE_CHAR (s
, len
, i
);
7171 ADVANCE_CHAR (s
, len
, i
);
7175 i
= skip_single_quoted (s
, len
, ++i
);
7179 i
= skip_double_quoted ((char *)s
, len
, ++i
);
7184 return (count
== 0);
7187 /****************************************************************/
7189 /* Functions to perform parameter expansion on a string */
7191 /****************************************************************/
7193 /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
7195 parameter_brace_expand (string
, indexp
, quoted
, pflags
, quoted_dollar_atp
, contains_dollar_at
)
7197 int *indexp
, quoted
, *quoted_dollar_atp
, *contains_dollar_at
, pflags
;
7199 int check_nullness
, var_is_set
, var_is_null
, var_is_special
;
7200 int want_substring
, want_indir
, want_patsub
, want_casemod
;
7201 char *name
, *value
, *temp
, *temp1
;
7202 WORD_DESC
*tdesc
, *ret
;
7203 int t_index
, sindex
, c
, tflag
, modspec
;
7207 temp
= temp1
= value
= (char *)NULL
;
7208 var_is_set
= var_is_null
= var_is_special
= check_nullness
= 0;
7209 want_substring
= want_indir
= want_patsub
= want_casemod
= 0;
7213 /* ${#var} doesn't have any of the other parameter expansions on it. */
7214 if (string
[t_index
] == '#' && legal_variable_starter (string
[t_index
+1])) /* {{ */
7215 name
= string_extract (string
, &t_index
, "}", SX_VARNAME
);
7217 #if defined (CASEMOD_EXPANSIONS)
7218 /* To enable case-toggling expansions using the `~' operator character
7219 change the 1 to 0. */
7220 # if defined (CASEMOD_CAPCASE)
7221 name
= string_extract (string
, &t_index
, "#%^,~:-=?+/}", SX_VARNAME
);
7223 name
= string_extract (string
, &t_index
, "#%^,:-=?+/}", SX_VARNAME
);
7224 # endif /* CASEMOD_CAPCASE */
7226 name
= string_extract (string
, &t_index
, "#%:-=?+/}", SX_VARNAME
);
7227 #endif /* CASEMOD_EXPANSIONS */
7234 /* If the name really consists of a special variable, then make sure
7235 that we have the entire name. We don't allow indirect references
7236 to special variables except `#', `?', `@' and `*'. */
7237 if ((sindex
== t_index
&& VALID_SPECIAL_LENGTH_PARAM (string
[t_index
])) ||
7238 (sindex
== t_index
- 1 && string
[sindex
] == '!' && VALID_INDIR_PARAM (string
[t_index
])))
7241 temp1
= string_extract (string
, &t_index
, "#%:-=?+/}", 0);
7242 name
= (char *)xrealloc (name
, 3 + (strlen (temp1
)));
7243 *name
= string
[sindex
];
7244 if (string
[sindex
] == '!')
7246 /* indirect reference of $#, $?, $@, or $* */
7247 name
[1] = string
[sindex
+ 1];
7248 strcpy (name
+ 2, temp1
);
7251 strcpy (name
+ 1, temp1
);
7256 /* Find out what character ended the variable name. Then
7257 do the appropriate thing. */
7258 if (c
= string
[sindex
])
7261 /* If c is followed by one of the valid parameter expansion
7262 characters, move past it as normal. If not, assume that
7263 a substring specification is being given, and do not move
7265 if (c
== ':' && VALID_PARAM_EXPAND_CHAR (string
[sindex
]))
7268 if (c
= string
[sindex
])
7271 else if (c
== ':' && string
[sindex
] != RBRACE
)
7273 else if (c
== '/' /* && string[sindex] != RBRACE */) /* XXX */
7275 #if defined (CASEMOD_EXPANSIONS)
7276 else if (c
== '^' || c
== ',' || c
== '~')
7283 /* Catch the valid and invalid brace expressions that made it through the
7285 /* ${#-} is a valid expansion and means to take the length of $-.
7286 Similarly for ${#?} and ${##}... */
7287 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
7288 VALID_SPECIAL_LENGTH_PARAM (c
) && string
[sindex
] == RBRACE
)
7290 name
= (char *)xrealloc (name
, 3);
7293 c
= string
[sindex
++];
7296 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
7297 if (name
[0] == '#' && name
[1] == '\0' && check_nullness
== 0 &&
7298 member (c
, "%:=+/") && string
[sindex
] == RBRACE
)
7300 temp
= (char *)NULL
;
7301 goto bad_substitution
;
7304 /* Indirect expansion begins with a `!'. A valid indirect expansion is
7305 either a variable name, one of the positional parameters or a special
7306 variable that expands to one of the positional parameters. */
7307 want_indir
= *name
== '!' &&
7308 (legal_variable_starter ((unsigned char)name
[1]) || DIGIT (name
[1])
7309 || VALID_INDIR_PARAM (name
[1]));
7311 /* Determine the value of this variable. */
7313 /* Check for special variables, directly referenced. */
7314 if (SPECIAL_VAR (name
, want_indir
))
7317 /* Check for special expansion things, like the length of a parameter */
7318 if (*name
== '#' && name
[1])
7320 /* If we are not pointing at the character just after the
7321 closing brace, then we haven't gotten all of the name.
7322 Since it begins with a special character, this is a bad
7323 substitution. Also check NAME for validity before trying
7325 if (string
[sindex
- 1] != RBRACE
|| (valid_length_expression (name
) == 0))
7327 temp
= (char *)NULL
;
7328 goto bad_substitution
;
7331 number
= parameter_brace_expand_length (name
);
7332 if (number
== INTMAX_MIN
&& unbound_vars_is_error
)
7334 last_command_exit_value
= EXECUTION_FAILURE
;
7335 err_unboundvar (name
+1);
7337 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
7343 return (&expand_wdesc_error
);
7346 ret
= alloc_word_desc ();
7347 ret
->word
= itos (number
);
7352 /* ${@} is identical to $@. */
7353 if (name
[0] == '@' && name
[1] == '\0')
7355 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
7356 *quoted_dollar_atp
= 1;
7358 if (contains_dollar_at
)
7359 *contains_dollar_at
= 1;
7361 tflag
|= W_DOLLARAT
;
7364 /* Process ${!PREFIX*} expansion. */
7365 if (want_indir
&& string
[sindex
- 1] == RBRACE
&&
7366 (string
[sindex
- 2] == '*' || string
[sindex
- 2] == '@') &&
7367 legal_variable_starter ((unsigned char) name
[1]))
7372 temp1
= savestring (name
+ 1);
7373 number
= strlen (temp1
);
7374 temp1
[number
- 1] = '\0';
7375 x
= all_variables_matching_prefix (temp1
);
7376 xlist
= strvec_to_word_list (x
, 0, 0);
7377 if (string
[sindex
- 2] == '*')
7378 temp
= string_list_dollar_star (xlist
);
7381 temp
= string_list_dollar_at (xlist
, quoted
);
7382 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
7383 *quoted_dollar_atp
= 1;
7384 if (contains_dollar_at
)
7385 *contains_dollar_at
= 1;
7387 tflag
|= W_DOLLARAT
;
7390 dispose_words (xlist
);
7396 ret
= alloc_word_desc ();
7398 ret
->flags
= tflag
; /* XXX */
7402 #if defined (ARRAY_VARS)
7403 /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */
7404 if (want_indir
&& string
[sindex
- 1] == RBRACE
&&
7405 string
[sindex
- 2] == ']' && valid_array_reference (name
+1))
7409 temp1
= savestring (name
+ 1);
7410 x
= array_variable_name (temp1
, &x1
, (int *)0); /* [ */
7412 if (ALL_ELEMENT_SUB (x1
[0]) && x1
[1] == ']')
7414 temp
= array_keys (temp1
, quoted
); /* handles assoc vars too */
7417 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
7418 *quoted_dollar_atp
= 1;
7419 if (contains_dollar_at
)
7420 *contains_dollar_at
= 1;
7422 tflag
|= W_DOLLARAT
;
7428 ret
= alloc_word_desc ();
7430 ret
->flags
= tflag
; /* XXX */
7436 #endif /* ARRAY_VARS */
7438 /* Make sure that NAME is valid before trying to go on. */
7439 if (valid_brace_expansion_word (want_indir
? name
+ 1 : name
,
7440 var_is_special
) == 0)
7442 temp
= (char *)NULL
;
7443 goto bad_substitution
;
7448 tdesc
= parameter_brace_expand_indir (name
+ 1, var_is_special
, quoted
, quoted_dollar_atp
, contains_dollar_at
);
7449 /* Turn off the W_ARRAYIND flag because there is no way for this function
7450 to return the index we're supposed to be using. */
7451 if (tdesc
&& tdesc
->flags
)
7452 tdesc
->flags
&= ~W_ARRAYIND
;
7455 tdesc
= parameter_brace_expand_word (name
, var_is_special
, quoted
, PF_IGNUNBOUND
|(pflags
&(PF_NOSPLIT2
|PF_ASSIGNRHS
)), &ind
);
7460 tflag
= tdesc
->flags
;
7461 dispose_word_desc (tdesc
);
7466 if (temp
== &expand_param_error
|| temp
== &expand_param_fatal
)
7470 return (temp
== &expand_param_error
? &expand_wdesc_error
: &expand_wdesc_fatal
);
7473 #if defined (ARRAY_VARS)
7474 if (valid_array_reference (name
))
7475 chk_atstar (name
, quoted
, quoted_dollar_atp
, contains_dollar_at
);
7478 var_is_set
= temp
!= (char *)0;
7479 var_is_null
= check_nullness
&& (var_is_set
== 0 || *temp
== 0);
7480 /* XXX - this may not need to be restricted to special variables */
7482 var_is_null
|= var_is_set
&& var_is_special
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && QUOTED_NULL (temp
);
7484 /* Get the rest of the stuff inside the braces. */
7485 if (c
&& c
!= RBRACE
)
7487 /* Extract the contents of the ${ ... } expansion
7488 according to the Posix.2 rules. */
7489 value
= extract_dollar_brace_string (string
, &sindex
, quoted
, (c
== '%' || c
== '#' || c
=='/' || c
== '^' || c
== ',' || c
==':') ? SX_POSIXEXP
|SX_WORD
: SX_WORD
);
7490 if (string
[sindex
] == RBRACE
)
7493 goto bad_substitution
;
7496 value
= (char *)NULL
;
7500 /* All the cases where an expansion can possibly generate an unbound
7502 if (want_substring
|| want_patsub
|| want_casemod
|| c
== '#' || c
== '%' || c
== RBRACE
)
7504 if (var_is_set
== 0 && unbound_vars_is_error
&& ((name
[0] != '@' && name
[0] != '*') || name
[1]))
7506 last_command_exit_value
= EXECUTION_FAILURE
;
7507 err_unboundvar (name
);
7511 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
7515 /* If this is a substring spec, process it and add the result. */
7518 temp1
= parameter_brace_substring (name
, temp
, ind
, value
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
7523 if (temp1
== &expand_param_error
)
7524 return (&expand_wdesc_error
);
7525 else if (temp1
== &expand_param_fatal
)
7526 return (&expand_wdesc_fatal
);
7528 ret
= alloc_word_desc ();
7530 /* We test quoted_dollar_atp because we want variants with double-quoted
7531 "$@" to take a different code path. In fact, we make sure at the end
7532 of expand_word_internal that we're only looking at these flags if
7533 quoted_dollar_at == 0. */
7535 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
7536 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
7537 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
7540 else if (want_patsub
)
7542 temp1
= parameter_brace_patsub (name
, temp
, ind
, value
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
7547 if (temp1
== &expand_param_error
)
7548 return (&expand_wdesc_error
);
7549 else if (temp1
== &expand_param_fatal
)
7550 return (&expand_wdesc_fatal
);
7552 ret
= alloc_word_desc ();
7555 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
7556 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
7557 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
7560 #if defined (CASEMOD_EXPANSIONS)
7561 else if (want_casemod
)
7563 temp1
= parameter_brace_casemod (name
, temp
, ind
, modspec
, value
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
7568 if (temp1
== &expand_param_error
)
7569 return (&expand_wdesc_error
);
7570 else if (temp1
== &expand_param_fatal
)
7571 return (&expand_wdesc_fatal
);
7573 ret
= alloc_word_desc ();
7576 (quoted_dollar_atp
== 0 || *quoted_dollar_atp
== 0) &&
7577 QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
7578 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
7583 /* Do the right thing based on which character ended the variable name. */
7589 last_command_exit_value
= EXECUTION_FAILURE
;
7590 report_error (_("%s: bad substitution"), string
? string
: "??");
7594 return &expand_wdesc_error
;
7599 case '#': /* ${param#[#]pattern} */
7600 case '%': /* ${param%[%]pattern} */
7601 if (value
== 0 || *value
== '\0' || temp
== 0 || *temp
== '\0')
7606 temp1
= parameter_brace_remove_pattern (name
, temp
, ind
, value
, c
, quoted
, (tflag
& W_ARRAYIND
) ? AV_USEIND
: 0);
7611 ret
= alloc_word_desc ();
7613 if (temp1
&& QUOTED_NULL (temp1
) && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
7614 ret
->flags
|= W_QUOTED
|W_HASQUOTEDNULL
;
7621 if (var_is_set
&& var_is_null
== 0)
7623 /* If the operator is `+', we don't want the value of the named
7624 variable for anything, just the value of the right hand side. */
7627 /* XXX -- if we're double-quoted and the named variable is "$@",
7628 we want to turn off any special handling of "$@" --
7629 we're not using it, so whatever is on the rhs applies. */
7630 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
7631 *quoted_dollar_atp
= 0;
7632 if (contains_dollar_at
)
7633 *contains_dollar_at
= 0;
7638 /* From Posix discussion on austin-group list. Issue 221
7639 requires that backslashes escaping `}' inside
7640 double-quoted ${...} be removed. */
7641 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
7642 quoted
|= Q_DOLBRACE
;
7643 ret
= parameter_brace_expand_rhs (name
, value
, c
,
7646 contains_dollar_at
);
7647 /* XXX - fix up later, esp. noting presence of
7648 W_HASQUOTEDNULL in ret->flags */
7652 temp
= (char *)NULL
;
7658 /* Otherwise do nothing; just use the value in TEMP. */
7660 else /* VAR not set or VAR is NULL. */
7663 temp
= (char *)NULL
;
7664 if (c
== '=' && var_is_special
)
7666 last_command_exit_value
= EXECUTION_FAILURE
;
7667 report_error (_("$%s: cannot assign in this way"), name
);
7670 return &expand_wdesc_error
;
7674 parameter_brace_expand_error (name
, value
);
7675 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
7679 /* XXX -- if we're double-quoted and the named variable is "$@",
7680 we want to turn off any special handling of "$@" --
7681 we're not using it, so whatever is on the rhs applies. */
7682 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && quoted_dollar_atp
)
7683 *quoted_dollar_atp
= 0;
7684 if (contains_dollar_at
)
7685 *contains_dollar_at
= 0;
7687 /* From Posix discussion on austin-group list. Issue 221 requires
7688 that backslashes escaping `}' inside double-quoted ${...} be
7690 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
7691 quoted
|= Q_DOLBRACE
;
7692 ret
= parameter_brace_expand_rhs (name
, value
, c
, quoted
,
7694 contains_dollar_at
);
7695 /* XXX - fix up later, esp. noting presence of
7696 W_HASQUOTEDNULL in tdesc->flags */
7707 ret
= alloc_word_desc ();
7714 /* Expand a single ${xxx} expansion. The braces are optional. When
7715 the braces are used, parameter_brace_expand() does the work,
7716 possibly calling param_expand recursively. */
7718 param_expand (string
, sindex
, quoted
, expanded_something
,
7719 contains_dollar_at
, quoted_dollar_at_p
, had_quoted_null_p
,
7722 int *sindex
, quoted
, *expanded_something
, *contains_dollar_at
;
7723 int *quoted_dollar_at_p
, *had_quoted_null_p
, pflags
;
7725 char *temp
, *temp1
, uerror
[3];
7726 int zindex
, t_index
, expok
;
7731 WORD_DESC
*tdesc
, *ret
;
7735 c
= string
[++zindex
];
7737 temp
= (char *)NULL
;
7738 ret
= tdesc
= (WORD_DESC
*)NULL
;
7741 /* Do simple cases first. Switch on what follows '$'. */
7755 temp1
= dollar_vars
[TODIGIT (c
)];
7756 if (unbound_vars_is_error
&& temp1
== (char *)NULL
)
7761 last_command_exit_value
= EXECUTION_FAILURE
;
7762 err_unboundvar (uerror
);
7763 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
7766 temp
= (*temp1
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
7767 ? quote_string (temp1
)
7768 : quote_escapes (temp1
);
7770 temp
= (char *)NULL
;
7774 /* $$ -- pid of the invoking shell. */
7776 temp
= itos (dollar_dollar_pid
);
7779 /* $# -- number of positional parameters. */
7781 temp
= itos (number_of_args ());
7784 /* $? -- return value of the last synchronous command. */
7786 temp
= itos (last_command_exit_value
);
7789 /* $- -- flags supplied to the shell on invocation or by `set'. */
7791 temp
= which_set_flags ();
7794 /* $! -- Pid of the last asynchronous command. */
7796 /* If no asynchronous pids have been created, expand to nothing.
7797 If `set -u' has been executed, and no async processes have
7798 been created, this is an expansion error. */
7799 if (last_asynchronous_pid
== NO_PID
)
7801 if (expanded_something
)
7802 *expanded_something
= 0;
7803 temp
= (char *)NULL
;
7804 if (unbound_vars_is_error
)
7809 last_command_exit_value
= EXECUTION_FAILURE
;
7810 err_unboundvar (uerror
);
7811 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
7815 temp
= itos (last_asynchronous_pid
);
7818 /* The only difference between this and $@ is when the arg is quoted. */
7819 case '*': /* `$*' */
7820 list
= list_rest_of_args ();
7823 /* According to austin-group posix proposal by Geoff Clare in
7824 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
7826 "The shell shall write a message to standard error and
7827 immediately exit when it tries to expand an unset parameter
7828 other than the '@' and '*' special parameters."
7831 if (list
== 0 && unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
7836 last_command_exit_value
= EXECUTION_FAILURE
;
7837 err_unboundvar (uerror
);
7838 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
7842 /* If there are no command-line arguments, this should just
7843 disappear if there are other characters in the expansion,
7844 even if it's quoted. */
7845 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && list
== 0)
7846 temp
= (char *)NULL
;
7847 else if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
|Q_PATQUOTE
))
7849 /* If we have "$*" we want to make a string of the positional
7850 parameters, separated by the first character of $IFS, and
7851 quote the whole string, including the separators. If IFS
7852 is unset, the parameters are separated by ' '; if $IFS is
7853 null, the parameters are concatenated. */
7854 temp
= (quoted
& (Q_DOUBLE_QUOTES
|Q_PATQUOTE
)) ? string_list_dollar_star (list
) : string_list (list
);
7857 temp1
= quote_string (temp
);
7859 tflag
|= W_HASQUOTEDNULL
;
7866 /* We check whether or not we're eventually going to split $* here,
7867 for example when IFS is empty and we are processing the rhs of
7868 an assignment statement. In that case, we don't separate the
7869 arguments at all. Otherwise, if the $* is not quoted it is
7871 # if defined (HANDLE_MULTIBYTE)
7872 if (expand_no_split_dollar_star
&& ifs_firstc
[0] == 0)
7874 if (expand_no_split_dollar_star
&& ifs_firstc
== 0)
7876 temp
= string_list_dollar_star (list
);
7879 temp
= string_list_dollar_at (list
, quoted
);
7880 if (quoted
== 0 && (ifs_is_set
== 0 || ifs_is_null
))
7881 tflag
|= W_SPLITSPACE
;
7884 if (expand_no_split_dollar_star
== 0 && contains_dollar_at
)
7885 *contains_dollar_at
= 1;
7888 dispose_words (list
);
7891 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
7892 means that we have to turn quoting off after we split into
7893 the individually quoted arguments so that the final split
7894 on the first character of $IFS is still done. */
7895 case '@': /* `$@' */
7896 list
= list_rest_of_args ();
7899 /* According to austin-group posix proposal by Geoff Clare in
7900 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
7902 "The shell shall write a message to standard error and
7903 immediately exit when it tries to expand an unset parameter
7904 other than the '@' and '*' special parameters."
7907 if (list
== 0 && unbound_vars_is_error
&& (pflags
& PF_IGNUNBOUND
) == 0)
7912 last_command_exit_value
= EXECUTION_FAILURE
;
7913 err_unboundvar (uerror
);
7914 return (interactive_shell
? &expand_wdesc_error
: &expand_wdesc_fatal
);
7918 /* We want to flag the fact that we saw this. We can't turn
7919 off quoting entirely, because other characters in the
7920 string might need it (consider "\"$@\""), but we need some
7921 way to signal that the final split on the first character
7922 of $IFS should be done, even though QUOTED is 1. */
7923 /* XXX - should this test include Q_PATQUOTE? */
7924 if (quoted_dollar_at_p
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
7925 *quoted_dollar_at_p
= 1;
7926 if (contains_dollar_at
)
7927 *contains_dollar_at
= 1;
7929 /* We want to separate the positional parameters with the first
7930 character of $IFS in case $IFS is something other than a space.
7931 We also want to make sure that splitting is done no matter what --
7932 according to POSIX.2, this expands to a list of the positional
7933 parameters no matter what IFS is set to. */
7934 /* XXX - what to do when in a context where word splitting is not
7935 performed? Even when IFS is not the default, posix seems to imply
7936 that we behave like unquoted $* ? Maybe we should use PF_NOSPLIT2
7938 temp
= string_list_dollar_at (list
, (pflags
& PF_ASSIGNRHS
) ? (quoted
|Q_DOUBLE_QUOTES
) : quoted
);
7940 tflag
|= W_DOLLARAT
;
7941 dispose_words (list
);
7945 tdesc
= parameter_brace_expand (string
, &zindex
, quoted
, pflags
,
7947 contains_dollar_at
);
7949 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
7951 temp
= tdesc
? tdesc
->word
: (char *)0;
7954 /* Quoted nulls should be removed if there is anything else
7956 /* Note that we saw the quoted null so we can add one back at
7957 the end of this function if there are no other characters
7958 in the string, discard TEMP, and go on. The exception to
7959 this is when we have "${@}" and $1 is '', since $@ needs
7960 special handling. */
7961 if (tdesc
&& tdesc
->word
&& (tdesc
->flags
& W_HASQUOTEDNULL
) && QUOTED_NULL (temp
))
7963 if (had_quoted_null_p
)
7964 *had_quoted_null_p
= 1;
7965 if (*quoted_dollar_at_p
== 0)
7968 tdesc
->word
= temp
= (char *)NULL
;
7976 /* Do command or arithmetic substitution. */
7978 /* We have to extract the contents of this paren substitution. */
7979 t_index
= zindex
+ 1;
7980 temp
= extract_command_subst (string
, &t_index
, 0);
7983 /* For Posix.2-style `$(( ))' arithmetic substitution,
7984 extract the expression and pass it to the evaluator. */
7985 if (temp
&& *temp
== LPAREN
)
7989 temp2
= savestring (temp1
);
7990 t_index
= strlen (temp2
) - 1;
7992 if (temp2
[t_index
] != RPAREN
)
7998 /* Cut off ending `)' */
7999 temp2
[t_index
] = '\0';
8001 if (chk_arithsub (temp2
, t_index
) == 0)
8005 internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
8010 /* Expand variables found inside the expression. */
8011 temp1
= expand_arith_string (temp2
, Q_DOUBLE_QUOTES
);
8015 /* No error messages. */
8016 this_command_name
= (char *)NULL
;
8017 number
= evalexp (temp1
, &expok
);
8022 if (interactive_shell
== 0 && posixly_correct
)
8024 last_command_exit_value
= EXECUTION_FAILURE
;
8025 return (&expand_wdesc_fatal
);
8028 return (&expand_wdesc_error
);
8030 temp
= itos (number
);
8035 if (pflags
& PF_NOCOMSUB
)
8036 /* we need zindex+1 because string[zindex] == RPAREN */
8037 temp1
= substring (string
, *sindex
, zindex
+1);
8040 tdesc
= command_substitute (temp
, quoted
);
8041 temp1
= tdesc
? tdesc
->word
: (char *)NULL
;
8043 dispose_word_desc (tdesc
);
8049 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
8050 away in a future bash release. */
8052 /* Extract the contents of this arithmetic substitution. */
8053 t_index
= zindex
+ 1;
8054 temp
= extract_arithmetic_subst (string
, &t_index
);
8058 temp
= savestring (string
);
8059 if (expanded_something
)
8060 *expanded_something
= 0;
8064 /* Do initial variable expansion. */
8065 temp1
= expand_arith_string (temp
, Q_DOUBLE_QUOTES
);
8070 /* Find the variable in VARIABLE_LIST. */
8071 temp
= (char *)NULL
;
8073 for (t_index
= zindex
; (c
= string
[zindex
]) && legal_variable_char (c
); zindex
++)
8075 temp1
= (zindex
> t_index
) ? substring (string
, t_index
, zindex
) : (char *)NULL
;
8077 /* If this isn't a variable name, then just output the `$'. */
8078 if (temp1
== 0 || *temp1
== '\0')
8081 temp
= (char *)xmalloc (2);
8084 if (expanded_something
)
8085 *expanded_something
= 0;
8089 /* If the variable exists, return its value cell. */
8090 var
= find_variable (temp1
);
8092 if (var
&& invisible_p (var
) == 0 && var_isset (var
))
8094 #if defined (ARRAY_VARS)
8095 if (assoc_p (var
) || array_p (var
))
8097 temp
= array_p (var
) ? array_reference (array_cell (var
), 0)
8098 : assoc_reference (assoc_cell (var
), "0");
8100 temp
= (*temp
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8101 ? quote_string (temp
)
8102 : quote_escapes (temp
);
8103 else if (unbound_vars_is_error
)
8104 goto unbound_variable
;
8109 temp
= value_cell (var
);
8111 temp
= (*temp
&& (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
8112 ? quote_string (temp
)
8113 : quote_escapes (temp
);
8120 else if (var
&& (invisible_p (var
) || var_isset (var
) == 0))
8121 temp
= (char *)NULL
;
8122 else if ((var
= find_variable_last_nameref (temp1
)) && var_isset (var
) && invisible_p (var
) == 0)
8124 temp
= nameref_cell (var
);
8125 #if defined (ARRAY_VARS)
8126 if (temp
&& *temp
&& valid_array_reference (temp
))
8128 tdesc
= parameter_brace_expand_word (temp
, SPECIAL_VAR (temp
, 0), quoted
, pflags
, (arrayind_t
*)NULL
);
8129 if (tdesc
== &expand_wdesc_error
|| tdesc
== &expand_wdesc_fatal
)
8136 /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */
8137 if (temp
&& *temp
&& legal_identifier (temp
) == 0)
8139 last_command_exit_value
= EXECUTION_FAILURE
;
8140 report_error (_("%s: invalid variable name for name reference"), temp
);
8141 return (&expand_wdesc_error
); /* XXX */
8144 temp
= (char *)NULL
;
8147 temp
= (char *)NULL
;
8150 if (unbound_vars_is_error
)
8152 last_command_exit_value
= EXECUTION_FAILURE
;
8153 err_unboundvar (temp1
);
8162 last_command_exit_value
= EXECUTION_FAILURE
;
8163 return ((unbound_vars_is_error
&& interactive_shell
== 0)
8164 ? &expand_wdesc_fatal
8165 : &expand_wdesc_error
);
8176 ret
= alloc_word_desc ();
8177 ret
->flags
= tflag
; /* XXX */
8183 /* Make a word list which is the result of parameter and variable
8184 expansion, command substitution, arithmetic substitution, and
8185 quote removal of WORD. Return a pointer to a WORD_LIST which is
8186 the result of the expansion. If WORD contains a null word, the
8187 word list returned is also null.
8189 QUOTED contains flag values defined in shell.h.
8191 ISEXP is used to tell expand_word_internal that the word should be
8192 treated as the result of an expansion. This has implications for
8193 how IFS characters in the word are treated.
8195 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
8196 they point to an integer value which receives information about expansion.
8197 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
8198 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
8201 This only does word splitting in the case of $@ expansion. In that
8202 case, we split on ' '. */
8204 /* Values for the local variable quoted_state. */
8206 #define PARTIALLY_QUOTED 1
8207 #define WHOLLY_QUOTED 2
8210 expand_word_internal (word
, quoted
, isexp
, contains_dollar_at
, expanded_something
)
8213 int *contains_dollar_at
;
8214 int *expanded_something
;
8219 /* The intermediate string that we build while expanding. */
8222 /* The current size of the above object. */
8225 /* Index into ISTRING. */
8228 /* Temporary string storage. */
8231 /* The text of WORD. */
8232 register char *string
;
8234 /* The size of STRING. */
8237 /* The index into STRING. */
8240 /* This gets 1 if we see a $@ while quoted. */
8241 int quoted_dollar_at
;
8243 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
8244 whether WORD contains no quoting characters, a partially quoted
8245 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
8249 int had_quoted_null
;
8250 int has_dollar_at
, temp_has_dollar_at
;
8251 int split_on_spaces
;
8253 int pflags
; /* flags passed to param_expand */
8255 int assignoff
; /* If assignment, offset of `=' */
8257 register unsigned char c
; /* Current character. */
8258 int t_index
; /* For calls to string_extract_xxx. */
8264 istring
= (char *)xmalloc (istring_size
= DEFAULT_INITIAL_ARRAY_SIZE
);
8265 istring
[istring_index
= 0] = '\0';
8266 quoted_dollar_at
= had_quoted_null
= has_dollar_at
= 0;
8267 split_on_spaces
= 0;
8268 quoted_state
= UNQUOTED
;
8270 string
= word
->word
;
8272 goto finished_with_string
;
8273 /* Don't need the string length for the SADD... and COPY_ macros unless
8274 multibyte characters are possible. */
8275 string_size
= (MB_CUR_MAX
> 1) ? strlen (string
) : 1;
8277 if (contains_dollar_at
)
8278 *contains_dollar_at
= 0;
8282 /* Begin the expansion. */
8288 /* Case on top-level character. */
8292 goto finished_with_string
;
8296 #if HANDLE_MULTIBYTE
8297 if (MB_CUR_MAX
> 1 && string
[sindex
])
8299 SADD_MBQCHAR_BODY(temp
, string
, sindex
, string_size
);
8304 temp
= (char *)xmalloc (3);
8306 temp
[1] = c
= string
[sindex
];
8317 istring
= sub_append_string (temp
, istring
, &istring_index
, &istring_size
);
8323 #if defined (PROCESS_SUBSTITUTION)
8324 /* Process substitution. */
8328 /* bash-4.4/bash-5.0
8329 XXX - technically this should only be expanded at the start
8331 if (string
[++sindex
] != LPAREN
|| (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (word
->flags
& (W_DQUOTE
|W_NOPROCSUB
)) || posixly_correct
)
8333 sindex
--; /* add_character: label increments sindex */
8337 t_index
= sindex
+ 1; /* skip past both '<' and LPAREN */
8339 temp1
= extract_process_subst (string
, (c
== '<') ? "<(" : ">(", &t_index
, 0); /*))*/
8342 /* If the process substitution specification is `<()', we want to
8343 open the pipe for writing in the child and produce output; if
8344 it is `>()', we want to open the pipe for reading in the child
8345 and consume input. */
8346 temp
= temp1
? process_substitute (temp1
, (c
== '>')) : (char *)0;
8350 goto dollar_add_string
;
8352 #endif /* PROCESS_SUBSTITUTION */
8355 /* Posix.2 section 3.6.1 says that tildes following `=' in words
8356 which are not assignment statements are not expanded. If the
8357 shell isn't in posix mode, though, we perform tilde expansion
8358 on `likely candidate' unquoted assignment statements (flags
8359 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
8360 contains an unquoted :~ or =~. Something to think about: we
8361 now have a flag that says to perform tilde expansion on arguments
8362 to `assignment builtins' like declare and export that look like
8363 assignment statements. We now do tilde expansion on such words
8364 even in POSIX mode. */
8365 if (word
->flags
& (W_ASSIGNRHS
|W_NOTILDE
))
8367 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
8368 goto add_ifs_character
;
8372 /* If we're not in posix mode or forcing assignment-statement tilde
8373 expansion, note where the `=' appears in the word and prepare to
8374 do tilde expansion following the first `='. */
8375 if ((word
->flags
& W_ASSIGNMENT
) &&
8376 (posixly_correct
== 0 || (word
->flags
& W_TILDEEXP
)) &&
8377 assignoff
== -1 && sindex
> 0)
8379 if (sindex
== assignoff
&& string
[sindex
+1] == '~') /* XXX */
8380 word
->flags
|= W_ITILDE
;
8382 else if ((word
->flags
& W_ASSIGNMENT
) &&
8383 (posixly_correct
== 0 || (word
->flags
& W_TILDEEXP
)) &&
8384 string
[sindex
+1] == '~')
8385 word
->flags
|= W_ITILDE
;
8387 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
8388 goto add_ifs_character
;
8393 if (word
->flags
& W_NOTILDE
)
8395 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
8396 goto add_ifs_character
;
8401 if ((word
->flags
& (W_ASSIGNMENT
|W_ASSIGNRHS
|W_TILDEEXP
)) &&
8402 string
[sindex
+1] == '~')
8403 word
->flags
|= W_ITILDE
;
8405 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
))
8406 goto add_ifs_character
;
8411 /* If the word isn't supposed to be tilde expanded, or we're not
8412 at the start of a word or after an unquoted : or = in an
8413 assignment statement, we don't do tilde expansion. */
8414 if ((word
->flags
& (W_NOTILDE
|W_DQUOTE
)) ||
8415 (sindex
> 0 && ((word
->flags
& W_ITILDE
) == 0)) ||
8416 (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
8418 word
->flags
&= ~W_ITILDE
;
8419 if (isexp
== 0 && (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)) == 0 && isifs (c
) && (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) == 0)
8420 goto add_ifs_character
;
8425 if (word
->flags
& W_ASSIGNRHS
)
8427 else if (word
->flags
& (W_ASSIGNMENT
|W_TILDEEXP
))
8432 temp
= bash_tilde_find_word (string
+ sindex
, tflag
, &t_index
);
8434 word
->flags
&= ~W_ITILDE
;
8436 if (temp
&& *temp
&& t_index
> 0)
8438 temp1
= bash_tilde_expand (temp
, tflag
);
8439 if (temp1
&& *temp1
== '~' && STREQ (temp
, temp1
))
8443 goto add_character
; /* tilde expansion failed */
8448 goto add_quoted_string
; /* XXX was add_string */
8457 if (expanded_something
)
8458 *expanded_something
= 1;
8460 temp_has_dollar_at
= 0;
8461 pflags
= (word
->flags
& W_NOCOMSUB
) ? PF_NOCOMSUB
: 0;
8462 if (word
->flags
& W_NOSPLIT2
)
8463 pflags
|= PF_NOSPLIT2
;
8464 if (word
->flags
& W_ASSIGNRHS
)
8465 pflags
|= PF_ASSIGNRHS
;
8466 tword
= param_expand (string
, &sindex
, quoted
, expanded_something
,
8467 &temp_has_dollar_at
, "ed_dollar_at
,
8468 &had_quoted_null
, pflags
);
8469 has_dollar_at
+= temp_has_dollar_at
;
8470 split_on_spaces
+= (tword
->flags
& W_SPLITSPACE
);
8472 if (tword
== &expand_wdesc_error
|| tword
== &expand_wdesc_fatal
)
8476 return ((tword
== &expand_wdesc_error
) ? &expand_word_error
8477 : &expand_word_fatal
);
8479 if (contains_dollar_at
&& has_dollar_at
)
8480 *contains_dollar_at
= 1;
8482 if (tword
&& (tword
->flags
& W_HASQUOTEDNULL
))
8483 had_quoted_null
= 1;
8485 temp
= tword
? tword
->word
: (char *)NULL
;
8486 dispose_word_desc (tword
);
8488 /* Kill quoted nulls; we will add them back at the end of
8489 expand_word_internal if nothing else in the string */
8490 if (had_quoted_null
&& temp
&& QUOTED_NULL (temp
))
8493 temp
= (char *)NULL
;
8499 case '`': /* Backquoted command substitution. */
8503 temp
= string_extract (string
, &sindex
, "`", SX_REQMATCH
);
8504 /* The test of sindex against t_index is to allow bare instances of
8505 ` to pass through, for backwards compatibility. */
8506 if (temp
== &extract_string_error
|| temp
== &extract_string_fatal
)
8508 if (sindex
- 1 == t_index
)
8513 last_command_exit_value
= EXECUTION_FAILURE
;
8514 report_error (_("bad substitution: no closing \"`\" in %s") , string
+t_index
);
8517 return ((temp
== &extract_string_error
) ? &expand_word_error
8518 : &expand_word_fatal
);
8521 if (expanded_something
)
8522 *expanded_something
= 1;
8524 if (word
->flags
& W_NOCOMSUB
)
8525 /* sindex + 1 because string[sindex] == '`' */
8526 temp1
= substring (string
, t_index
, sindex
+ 1);
8529 de_backslash (temp
);
8530 tword
= command_substitute (temp
, quoted
);
8531 temp1
= tword
? tword
->word
: (char *)NULL
;
8533 dispose_word_desc (tword
);
8537 goto dollar_add_string
;
8541 if (string
[sindex
+ 1] == '\n')
8547 c
= string
[++sindex
];
8549 if (quoted
& Q_HERE_DOCUMENT
)
8551 else if (quoted
& Q_DOUBLE_QUOTES
)
8556 /* From Posix discussion on austin-group list: Backslash escaping
8557 a } in ${...} is removed. Issue 0000221 */
8558 if ((quoted
& Q_DOLBRACE
) && c
== RBRACE
)
8560 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
8562 /* This is the fix for " $@\ " */
8563 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && ((sh_syntaxtab
[c
] & tflag
) == 0) && isexp
== 0 && isifs (c
))
8565 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
8566 DEFAULT_ARRAY_SIZE
);
8567 istring
[istring_index
++] = CTLESC
;
8568 istring
[istring_index
++] = '\\';
8569 istring
[istring_index
] = '\0';
8571 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
8573 else if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) && ((sh_syntaxtab
[c
] & tflag
) == 0))
8575 SCOPY_CHAR_I (twochars
, '\\', c
, string
, sindex
, string_size
);
8580 sindex
--; /* add_character: label increments sindex */
8585 SCOPY_CHAR_I (twochars
, CTLESC
, c
, string
, sindex
, string_size
);
8590 /* BEFORE jumping here, we need to increment sindex if appropriate */
8591 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 2, istring_size
,
8592 DEFAULT_ARRAY_SIZE
);
8593 istring
[istring_index
++] = twochars
[0];
8594 istring
[istring_index
++] = twochars
[1];
8595 istring
[istring_index
] = '\0';
8600 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
8604 temp
= string_extract_double_quoted (string
, &sindex
, 0);
8606 /* If the quotes surrounded the entire string, then the
8607 whole word was quoted. */
8608 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
8614 tword
= alloc_word_desc ();
8617 temp
= (char *)NULL
;
8619 temp_has_dollar_at
= 0; /* XXX */
8620 /* Need to get W_HASQUOTEDNULL flag through this function. */
8621 list
= expand_word_internal (tword
, Q_DOUBLE_QUOTES
, 0, &temp_has_dollar_at
, (int *)NULL
);
8622 has_dollar_at
+= temp_has_dollar_at
;
8624 if (list
== &expand_word_error
|| list
== &expand_word_fatal
)
8628 /* expand_word_internal has already freed temp_word->word
8629 for us because of the way it prints error messages. */
8630 tword
->word
= (char *)NULL
;
8631 dispose_word (tword
);
8635 dispose_word (tword
);
8637 /* "$@" (a double-quoted dollar-at) expands into nothing,
8638 not even a NULL word, when there are no positional
8640 if (list
== 0 && has_dollar_at
)
8646 /* If we get "$@", we know we have expanded something, so we
8647 need to remember it for the final split on $IFS. This is
8648 a special case; it's the only case where a quoted string
8649 can expand into more than one word. It's going to come back
8650 from the above call to expand_word_internal as a list with
8651 a single word, in which all characters are quoted and
8652 separated by blanks. What we want to do is to turn it back
8653 into a list for the next piece of code. */
8655 dequote_list (list
);
8657 if (list
&& list
->word
&& (list
->word
->flags
& W_HASQUOTEDNULL
))
8658 had_quoted_null
= 1; /* XXX */
8663 if (contains_dollar_at
)
8664 *contains_dollar_at
= 1;
8665 if (expanded_something
)
8666 *expanded_something
= 1;
8671 /* What we have is "". This is a minor optimization. */
8673 list
= (WORD_LIST
*)NULL
;
8676 /* The code above *might* return a list (consider the case of "$@",
8677 where it returns "$1", "$2", etc.). We can't throw away the
8678 rest of the list, and we have to make sure each word gets added
8679 as quoted. We test on tresult->next: if it is non-NULL, we
8680 quote the whole list, save it to a string with string_list, and
8681 add that string. We don't need to quote the results of this
8682 (and it would be wrong, since that would quote the separators
8683 as well), so we go directly to add_string. */
8688 /* Testing quoted_dollar_at makes sure that "$@" is
8689 split correctly when $IFS does not contain a space. */
8690 temp
= quoted_dollar_at
8691 ? string_list_dollar_at (list
, Q_DOUBLE_QUOTES
)
8692 : string_list (quote_list (list
));
8693 dispose_words (list
);
8698 temp
= savestring (list
->word
->word
);
8699 tflag
= list
->word
->flags
;
8700 dispose_words (list
);
8702 /* If the string is not a quoted null string, we want
8703 to remove any embedded unquoted CTLNUL characters.
8704 We do not want to turn quoted null strings back into
8705 the empty string, though. We do this because we
8706 want to remove any quoted nulls from expansions that
8707 contain other characters. For example, if we have
8708 x"$*"y or "x$*y" and there are no positional parameters,
8709 the $* should expand into nothing. */
8710 /* We use the W_HASQUOTEDNULL flag to differentiate the
8711 cases: a quoted null character as above and when
8712 CTLNUL is contained in the (non-null) expansion
8713 of some variable. We use the had_quoted_null flag to
8714 pass the value through this function to its caller. */
8715 if ((tflag
& W_HASQUOTEDNULL
) && QUOTED_NULL (temp
) == 0)
8716 remove_quoted_nulls (temp
); /* XXX */
8720 temp
= (char *)NULL
;
8722 /* We do not want to add quoted nulls to strings that are only
8723 partially quoted; we can throw them away. The exception to
8724 this is when we are going to be performing word splitting,
8725 since we have to preserve a null argument if the next character
8726 will cause word splitting. */
8727 if (temp
== 0 && quoted_state
== PARTIALLY_QUOTED
&& (word
->flags
& (W_NOSPLIT
|W_NOSPLIT2
)))
8735 temp
= quote_string (temp
);
8743 sindex
--; /* add_character: label increments sindex */
8750 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)))
8754 temp
= string_extract_single_quoted (string
, &sindex
);
8756 /* If the entire STRING was surrounded by single quotes,
8757 then the string is wholly quoted. */
8758 quoted_state
= (t_index
== 1 && string
[sindex
] == '\0')
8762 /* If all we had was '', it is a null expansion. */
8766 temp
= (char *)NULL
;
8769 remove_quoted_escapes (temp
); /* ??? */
8771 /* We do not want to add quoted nulls to strings that are only
8772 partially quoted; such nulls are discarded. */
8773 if (temp
== 0 && (quoted_state
== PARTIALLY_QUOTED
))
8776 /* If we have a quoted null expansion, add a quoted NULL to istring. */
8780 sindex
--; /* add_character: label increments sindex */
8784 goto add_quoted_string
;
8789 /* This is the fix for " $@ " */
8791 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || (isexp
== 0 && isifs (c
)))
8793 if (string
[sindex
]) /* from old goto dollar_add_string */
8802 #if HANDLE_MULTIBYTE
8808 SADD_MBQCHAR_BODY(temp
, string
, sindex
, string_size
);
8813 twochars
[0] = CTLESC
;
8820 SADD_MBCHAR (temp
, string
, sindex
, string_size
);
8823 RESIZE_MALLOCED_BUFFER (istring
, istring_index
, 1, istring_size
,
8824 DEFAULT_ARRAY_SIZE
);
8825 istring
[istring_index
++] = c
;
8826 istring
[istring_index
] = '\0';
8828 /* Next character. */
8833 finished_with_string
:
8834 /* OK, we're ready to return. If we have a quoted string, and
8835 quoted_dollar_at is not set, we do no splitting at all; otherwise
8836 we split on ' '. The routines that call this will handle what to
8837 do if nothing has been expanded. */
8839 /* Partially and wholly quoted strings which expand to the empty
8840 string are retained as an empty arguments. Unquoted strings
8841 which expand to the empty string are discarded. The single
8842 exception is the case of expanding "$@" when there are no
8843 positional parameters. In that case, we discard the expansion. */
8845 /* Because of how the code that handles "" and '' in partially
8846 quoted strings works, we need to make ISTRING into a QUOTED_NULL
8847 if we saw quoting characters, but the expansion was empty.
8848 "" and '' are tossed away before we get to this point when
8849 processing partially quoted strings. This makes "" and $xxx""
8850 equivalent when xxx is unset. We also look to see whether we
8851 saw a quoted null from a ${} expansion and add one back if we
8854 /* If we expand to nothing and there were no single or double quotes
8855 in the word, we throw it away. Otherwise, we return a NULL word.
8856 The single exception is for $@ surrounded by double quotes when
8857 there are no positional parameters. In that case, we also throw
8860 if (*istring
== '\0')
8862 if (quoted_dollar_at
== 0 && (had_quoted_null
|| quoted_state
== PARTIALLY_QUOTED
))
8864 istring
[0] = CTLNUL
;
8866 tword
= make_bare_word (istring
);
8867 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
8868 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
8869 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
8870 tword
->flags
|= W_QUOTED
;
8872 /* According to sh, ksh, and Posix.2, if a word expands into nothing
8873 and a double-quoted "$@" appears anywhere in it, then the entire
8875 else if (quoted_state
== UNQUOTED
|| quoted_dollar_at
)
8876 list
= (WORD_LIST
*)NULL
;
8880 tword
= make_bare_word (istring
);
8881 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
8882 tword
->flags
|= W_QUOTED
;
8883 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
8887 list
= (WORD_LIST
*)NULL
;
8890 else if (word
->flags
& W_NOSPLIT
)
8892 tword
= make_bare_word (istring
);
8893 if (word
->flags
& W_ASSIGNMENT
)
8894 tword
->flags
|= W_ASSIGNMENT
; /* XXX */
8895 if (word
->flags
& W_COMPASSIGN
)
8896 tword
->flags
|= W_COMPASSIGN
; /* XXX */
8897 if (word
->flags
& W_NOGLOB
)
8898 tword
->flags
|= W_NOGLOB
; /* XXX */
8899 if (word
->flags
& W_NOBRACE
)
8900 tword
->flags
|= W_NOBRACE
; /* XXX */
8901 if (word
->flags
& W_NOEXPAND
)
8902 tword
->flags
|= W_NOEXPAND
; /* XXX */
8903 if (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
))
8904 tword
->flags
|= W_QUOTED
;
8905 if (had_quoted_null
&& QUOTED_NULL (istring
))
8906 tword
->flags
|= W_HASQUOTEDNULL
;
8907 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
8914 ifs_chars
= (quoted_dollar_at
|| has_dollar_at
) ? ifs_value
: (char *)NULL
;
8916 /* If we have $@, we need to split the results no matter what. If
8917 IFS is unset or NULL, string_list_dollar_at has separated the
8918 positional parameters with a space, so we split on space (we have
8919 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
8920 string_list_dollar_at has separated the positional parameters
8921 with the first character of $IFS, so we split on $IFS. If
8922 SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either
8923 unset or null, and we want to make sure that we split on spaces
8924 regardless of what else has happened to IFS since the expansion. */
8925 if (split_on_spaces
)
8926 list
= list_string (istring
, " ", 1); /* XXX quoted == 1? */
8927 /* If we have $@ (has_dollar_at != 0) and we are in a context where we
8928 don't want to split the result (W_NOSPLIT2), and we are not quoted,
8929 we have already separated the arguments with the first character of
8930 $IFS. In this case, we want to return a list with a single word
8931 with the separator possibly replaced with a space (it's what other
8933 quoted_dollar_at is internal to this function and is set if we are
8934 passed an argument that is unquoted (quoted == 0) but we encounter a
8935 double-quoted $@ while expanding it. */
8936 else if (has_dollar_at
&& quoted_dollar_at
== 0 && ifs_chars
&& quoted
== 0 && (word
->flags
& W_NOSPLIT2
))
8938 /* Only split and rejoin if we have to */
8939 if (*ifs_chars
&& *ifs_chars
!= ' ')
8941 list
= list_string (istring
, *ifs_chars
? ifs_chars
: " ", 1);
8942 tstring
= string_list (list
);
8946 tword
= make_bare_word (tstring
);
8947 if (tstring
!= istring
)
8949 goto set_word_flags
;
8951 else if (has_dollar_at
&& ifs_chars
)
8952 list
= list_string (istring
, *ifs_chars
? ifs_chars
: " ", 1);
8955 tword
= make_bare_word (istring
);
8957 if ((quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) || (quoted_state
== WHOLLY_QUOTED
))
8958 tword
->flags
|= W_QUOTED
;
8959 if (word
->flags
& W_ASSIGNMENT
)
8960 tword
->flags
|= W_ASSIGNMENT
;
8961 if (word
->flags
& W_COMPASSIGN
)
8962 tword
->flags
|= W_COMPASSIGN
;
8963 if (word
->flags
& W_NOGLOB
)
8964 tword
->flags
|= W_NOGLOB
;
8965 if (word
->flags
& W_NOBRACE
)
8966 tword
->flags
|= W_NOBRACE
;
8967 if (word
->flags
& W_NOEXPAND
)
8968 tword
->flags
|= W_NOEXPAND
;
8969 if (had_quoted_null
&& QUOTED_NULL (istring
))
8970 tword
->flags
|= W_HASQUOTEDNULL
; /* XXX */
8971 list
= make_word_list (tword
, (WORD_LIST
*)NULL
);
8979 /* **************************************************************** */
8981 /* Functions for Quote Removal */
8983 /* **************************************************************** */
8985 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
8986 backslash quoting rules for within double quotes or a here document. */
8988 string_quote_removal (string
, quoted
)
8993 char *r
, *result_string
, *temp
, *send
;
8994 int sindex
, tindex
, dquote
;
8998 /* The result can be no longer than the original string. */
8999 slen
= strlen (string
);
9000 send
= string
+ slen
;
9002 r
= result_string
= (char *)xmalloc (slen
+ 1);
9004 for (dquote
= sindex
= 0; c
= string
[sindex
];)
9009 c
= string
[++sindex
];
9015 if (((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
) && (sh_syntaxtab
[c
] & CBSDQUOTE
) == 0)
9020 SCOPY_CHAR_M (r
, string
, send
, sindex
);
9024 if ((quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)) || dquote
)
9030 tindex
= sindex
+ 1;
9031 temp
= string_extract_single_quoted (string
, &tindex
);
9042 dquote
= 1 - dquote
;
9048 return (result_string
);
9053 /* Perform quote removal on word WORD. This allocates and returns a new
9056 word_quote_removal (word
, quoted
)
9063 t
= string_quote_removal (word
->word
, quoted
);
9064 w
= alloc_word_desc ();
9065 w
->word
= t
? t
: savestring ("");
9069 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
9070 the members of the list are treated as if they are surrounded by
9071 double quotes. Return a new list, or NULL if LIST is NULL. */
9073 word_list_quote_removal (list
, quoted
)
9077 WORD_LIST
*result
, *t
, *tresult
, *e
;
9079 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
9081 tresult
= make_word_list (word_quote_removal (t
->word
, quoted
), (WORD_LIST
*)NULL
);
9083 result
= (WORD_LIST
*) list_append (result
, tresult
);
9086 result
= e
= tresult
;
9099 /*******************************************
9101 * Functions to perform word splitting *
9103 *******************************************/
9113 ifs_value
= (v
&& value_cell (v
)) ? value_cell (v
) : " \t\n";
9115 ifs_is_set
= ifs_var
!= 0;
9116 ifs_is_null
= ifs_is_set
&& (*ifs_value
== 0);
9118 /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
9119 handle multibyte chars in IFS */
9120 memset (ifs_cmap
, '\0', sizeof (ifs_cmap
));
9121 for (t
= ifs_value
; t
&& *t
; t
++)
9127 #if defined (HANDLE_MULTIBYTE)
9130 ifs_firstc
[0] = '\0';
9136 ifs_len
= strnlen (ifs_value
, MB_CUR_MAX
);
9137 ifs_firstc_len
= MBLEN (ifs_value
, ifs_len
);
9138 if (ifs_firstc_len
== 1 || ifs_firstc_len
== 0 || MB_INVALIDCH (ifs_firstc_len
))
9140 ifs_firstc
[0] = ifs_value
[0];
9141 ifs_firstc
[1] = '\0';
9145 memcpy (ifs_firstc
, ifs_value
, ifs_firstc_len
);
9148 ifs_firstc
= ifs_value
? *ifs_value
: 0;
9158 /* This splits a single word into a WORD LIST on $IFS, but only if the word
9159 is not quoted. list_string () performs quote removal for us, even if we
9160 don't do any splitting. */
9162 word_split (w
, ifs_chars
)
9172 xifs
= ((w
->flags
& W_QUOTED
) || ifs_chars
== 0) ? "" : ifs_chars
;
9173 result
= list_string (w
->word
, xifs
, w
->flags
& W_QUOTED
);
9176 result
= (WORD_LIST
*)NULL
;
9181 /* Perform word splitting on LIST and return the RESULT. It is possible
9182 to return (WORD_LIST *)NULL. */
9184 word_list_split (list
)
9187 WORD_LIST
*result
, *t
, *tresult
, *e
;
9189 for (t
= list
, result
= (WORD_LIST
*)NULL
; t
; t
= t
->next
)
9191 tresult
= word_split (t
->word
, ifs_value
);
9193 result
= e
= tresult
;
9204 /**************************************************
9206 * Functions to expand an entire WORD_LIST *
9208 **************************************************/
9210 /* Do any word-expansion-specific cleanup and jump to top_level */
9212 exp_jump_to_top_level (v
)
9215 set_pipestatus_from_exit (last_command_exit_value
);
9217 /* Cleanup code goes here. */
9218 expand_no_split_dollar_star
= 0; /* XXX */
9219 expanding_redir
= 0;
9220 assigning_in_environment
= 0;
9222 if (parse_and_execute_level
== 0)
9223 top_level_cleanup (); /* from sig.c */
9225 jump_to_top_level (v
);
9228 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
9229 ELIST, and set ELIST to the new list. */
9230 #define PREPEND_LIST(nlist, elist) \
9231 do { nlist->next = elist; elist = nlist; } while (0)
9233 /* Separate out any initial variable assignments from TLIST. If set -k has
9234 been executed, remove all assignment statements from TLIST. Initial
9235 variable assignments and other environment assignments are placed
9236 on SUBST_ASSIGN_VARLIST. */
9238 separate_out_assignments (tlist
)
9241 register WORD_LIST
*vp
, *lp
;
9244 return ((WORD_LIST
*)NULL
);
9246 if (subst_assign_varlist
)
9247 dispose_words (subst_assign_varlist
); /* Clean up after previous error */
9249 subst_assign_varlist
= (WORD_LIST
*)NULL
;
9252 /* Separate out variable assignments at the start of the command.
9253 Loop invariant: vp->next == lp
9255 lp = list of words left after assignment statements skipped
9256 tlist = original list of words
9258 while (lp
&& (lp
->word
->flags
& W_ASSIGNMENT
))
9264 /* If lp != tlist, we have some initial assignment statements.
9265 We make SUBST_ASSIGN_VARLIST point to the list of assignment
9266 words and TLIST point to the remaining words. */
9269 subst_assign_varlist
= tlist
;
9270 /* ASSERT(vp->next == lp); */
9271 vp
->next
= (WORD_LIST
*)NULL
; /* terminate variable list */
9272 tlist
= lp
; /* remainder of word list */
9275 /* vp == end of variable list */
9276 /* tlist == remainder of original word list without variable assignments */
9278 /* All the words in tlist were assignment statements */
9279 return ((WORD_LIST
*)NULL
);
9281 /* ASSERT(tlist != NULL); */
9282 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
9284 /* If the -k option is in effect, we need to go through the remaining
9285 words, separate out the assignment words, and place them on
9286 SUBST_ASSIGN_VARLIST. */
9287 if (place_keywords_in_env
)
9289 WORD_LIST
*tp
; /* tp == running pointer into tlist */
9294 /* Loop Invariant: tp->next == lp */
9295 /* Loop postcondition: tlist == word list without assignment statements */
9298 if (lp
->word
->flags
& W_ASSIGNMENT
)
9300 /* Found an assignment statement, add this word to end of
9301 subst_assign_varlist (vp). */
9302 if (!subst_assign_varlist
)
9303 subst_assign_varlist
= vp
= lp
;
9310 /* Remove the word pointed to by LP from TLIST. */
9311 tp
->next
= lp
->next
;
9312 /* ASSERT(vp == lp); */
9313 lp
->next
= (WORD_LIST
*)NULL
;
9326 #define WEXP_VARASSIGN 0x001
9327 #define WEXP_BRACEEXP 0x002
9328 #define WEXP_TILDEEXP 0x004
9329 #define WEXP_PARAMEXP 0x008
9330 #define WEXP_PATHEXP 0x010
9332 /* All of the expansions, including variable assignments at the start of
9334 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
9336 /* All of the expansions except variable assignments at the start of
9338 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
9340 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
9341 expansion, command substitution, arithmetic expansion, word splitting, and
9343 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
9345 /* Take the list of words in LIST and do the various substitutions. Return
9346 a new list of words which is the expanded list, and without things like
9347 variable assignments. */
9353 return (expand_word_list_internal (list
, WEXP_ALL
));
9356 /* Same as expand_words (), but doesn't hack variable or environment
9359 expand_words_no_vars (list
)
9362 return (expand_word_list_internal (list
, WEXP_NOVARS
));
9366 expand_words_shellexp (list
)
9369 return (expand_word_list_internal (list
, WEXP_SHELLEXP
));
9373 glob_expand_word_list (tlist
, eflags
)
9377 char **glob_array
, *temp_string
;
9378 register int glob_index
;
9379 WORD_LIST
*glob_list
, *output_list
, *disposables
, *next
;
9382 output_list
= disposables
= (WORD_LIST
*)NULL
;
9383 glob_array
= (char **)NULL
;
9386 /* For each word, either globbing is attempted or the word is
9387 added to orig_list. If globbing succeeds, the results are
9388 added to orig_list and the word (tlist) is added to the list
9389 of disposable words. If globbing fails and failed glob
9390 expansions are left unchanged (the shell default), the
9391 original word is added to orig_list. If globbing fails and
9392 failed glob expansions are removed, the original word is
9393 added to the list of disposable words. orig_list ends up
9394 in reverse order and requires a call to REVERSE_LIST to
9395 be set right. After all words are examined, the disposable
9399 /* If the word isn't an assignment and contains an unquoted
9400 pattern matching character, then glob it. */
9401 if ((tlist
->word
->flags
& W_NOGLOB
) == 0 &&
9402 unquoted_glob_pattern_p (tlist
->word
->word
))
9404 glob_array
= shell_glob_filename (tlist
->word
->word
);
9406 /* Handle error cases.
9407 I don't think we should report errors like "No such file
9408 or directory". However, I would like to report errors
9409 like "Read failed". */
9411 if (glob_array
== 0 || GLOB_FAILED (glob_array
))
9413 glob_array
= (char **)xmalloc (sizeof (char *));
9414 glob_array
[0] = (char *)NULL
;
9417 /* Dequote the current word in case we have to use it. */
9418 if (glob_array
[0] == NULL
)
9420 temp_string
= dequote_string (tlist
->word
->word
);
9421 free (tlist
->word
->word
);
9422 tlist
->word
->word
= temp_string
;
9425 /* Make the array into a word list. */
9426 glob_list
= (WORD_LIST
*)NULL
;
9427 for (glob_index
= 0; glob_array
[glob_index
]; glob_index
++)
9429 tword
= make_bare_word (glob_array
[glob_index
]);
9430 glob_list
= make_word_list (tword
, glob_list
);
9435 output_list
= (WORD_LIST
*)list_append (glob_list
, output_list
);
9436 PREPEND_LIST (tlist
, disposables
);
9438 else if (fail_glob_expansion
!= 0)
9440 last_command_exit_value
= EXECUTION_FAILURE
;
9441 report_error (_("no match: %s"), tlist
->word
->word
);
9442 exp_jump_to_top_level (DISCARD
);
9444 else if (allow_null_glob_expansion
== 0)
9446 /* Failed glob expressions are left unchanged. */
9447 PREPEND_LIST (tlist
, output_list
);
9451 /* Failed glob expressions are removed. */
9452 PREPEND_LIST (tlist
, disposables
);
9457 /* Dequote the string. */
9458 temp_string
= dequote_string (tlist
->word
->word
);
9459 free (tlist
->word
->word
);
9460 tlist
->word
->word
= temp_string
;
9461 PREPEND_LIST (tlist
, output_list
);
9464 strvec_dispose (glob_array
);
9465 glob_array
= (char **)NULL
;
9471 dispose_words (disposables
);
9474 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
9476 return (output_list
);
9479 #if defined (BRACE_EXPANSION)
9481 brace_expand_word_list (tlist
, eflags
)
9485 register char **expansions
;
9487 WORD_LIST
*disposables
, *output_list
, *next
;
9491 for (disposables
= output_list
= (WORD_LIST
*)NULL
; tlist
; tlist
= next
)
9495 if (tlist
->word
->flags
& W_NOBRACE
)
9497 /*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/
9498 PREPEND_LIST (tlist
, output_list
);
9502 if ((tlist
->word
->flags
& (W_COMPASSIGN
|W_ASSIGNARG
)) == (W_COMPASSIGN
|W_ASSIGNARG
))
9504 /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
9505 PREPEND_LIST (tlist
, output_list
);
9509 /* Only do brace expansion if the word has a brace character. If
9510 not, just add the word list element to BRACES and continue. In
9511 the common case, at least when running shell scripts, this will
9512 degenerate to a bunch of calls to `mbschr', and then what is
9513 basically a reversal of TLIST into BRACES, which is corrected
9514 by a call to REVERSE_LIST () on BRACES when the end of TLIST
9516 if (mbschr (tlist
->word
->word
, LBRACE
))
9518 expansions
= brace_expand (tlist
->word
->word
);
9520 for (eindex
= 0; temp_string
= expansions
[eindex
]; eindex
++)
9522 w
= alloc_word_desc ();
9523 w
->word
= temp_string
;
9525 /* If brace expansion didn't change the word, preserve
9526 the flags. We may want to preserve the flags
9527 unconditionally someday -- XXX */
9528 if (STREQ (temp_string
, tlist
->word
->word
))
9529 w
->flags
= tlist
->word
->flags
;
9531 w
= make_word_flags (w
, temp_string
);
9533 output_list
= make_word_list (w
, output_list
);
9537 /* Add TLIST to the list of words to be freed after brace
9538 expansion has been performed. */
9539 PREPEND_LIST (tlist
, disposables
);
9542 PREPEND_LIST (tlist
, output_list
);
9546 dispose_words (disposables
);
9549 output_list
= REVERSE_LIST (output_list
, WORD_LIST
*);
9551 return (output_list
);
9555 #if defined (ARRAY_VARS)
9556 /* Take WORD, a compound associative array assignment, and internally run
9557 'declare -A w', where W is the variable name portion of WORD. */
9559 make_internal_declare (word
, option
)
9567 w
= make_word (word
);
9569 t
= assignment (w
->word
, 0);
9572 wl
= make_word_list (w
, (WORD_LIST
*)NULL
);
9573 wl
= make_word_list (make_word (option
), wl
);
9575 return (declare_builtin (wl
));
9580 shell_expand_word_list (tlist
, eflags
)
9584 WORD_LIST
*expanded
, *orig_list
, *new_list
, *next
, *temp_list
;
9585 int expanded_something
, has_dollar_at
;
9588 /* We do tilde expansion all the time. This is what 1003.2 says. */
9589 new_list
= (WORD_LIST
*)NULL
;
9590 for (orig_list
= tlist
; tlist
; tlist
= next
)
9592 temp_string
= tlist
->word
->word
;
9596 #if defined (ARRAY_VARS)
9597 /* If this is a compound array assignment to a builtin that accepts
9598 such assignments (e.g., `declare'), take the assignment and perform
9599 it separately, handling the semantics of declarations inside shell
9600 functions. This avoids the double-evaluation of such arguments,
9601 because `declare' does some evaluation of compound assignments on
9603 if ((tlist
->word
->flags
& (W_COMPASSIGN
|W_ASSIGNARG
)) == (W_COMPASSIGN
|W_ASSIGNARG
))
9609 if (tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSNGLOBAL
|W_ASSIGNARRAY
))
9612 if ((tlist
->word
->flags
& (W_ASSIGNASSOC
|W_ASSNGLOBAL
)) == (W_ASSIGNASSOC
|W_ASSNGLOBAL
))
9617 else if (tlist
->word
->flags
& W_ASSIGNASSOC
)
9619 else if ((tlist
->word
->flags
& (W_ASSIGNARRAY
|W_ASSNGLOBAL
)) == (W_ASSIGNARRAY
|W_ASSNGLOBAL
))
9624 else if (tlist
->word
->flags
& W_ASSIGNARRAY
)
9626 else if (tlist
->word
->flags
& W_ASSNGLOBAL
)
9630 /* If we have special handling note the integer attribute */
9631 if (opti
> 0 && (tlist
->word
->flags
& W_ASSIGNINT
))
9637 make_internal_declare (tlist
->word
->word
, opts
);
9639 t
= do_word_assignment (tlist
->word
, 0);
9642 last_command_exit_value
= EXECUTION_FAILURE
;
9643 exp_jump_to_top_level (DISCARD
);
9646 /* Now transform the word as ksh93 appears to do and go on */
9647 t
= assignment (tlist
->word
->word
, 0);
9648 tlist
->word
->word
[t
] = '\0';
9649 tlist
->word
->flags
&= ~(W_ASSIGNMENT
|W_NOSPLIT
|W_COMPASSIGN
|W_ASSIGNARG
|W_ASSIGNASSOC
|W_ASSIGNARRAY
);
9653 expanded_something
= 0;
9654 expanded
= expand_word_internal
9655 (tlist
->word
, 0, 0, &has_dollar_at
, &expanded_something
);
9657 if (expanded
== &expand_word_error
|| expanded
== &expand_word_fatal
)
9659 /* By convention, each time this error is returned,
9660 tlist->word->word has already been freed. */
9661 tlist
->word
->word
= (char *)NULL
;
9663 /* Dispose our copy of the original list. */
9664 dispose_words (orig_list
);
9665 /* Dispose the new list we're building. */
9666 dispose_words (new_list
);
9668 last_command_exit_value
= EXECUTION_FAILURE
;
9669 if (expanded
== &expand_word_error
)
9670 exp_jump_to_top_level (DISCARD
);
9672 exp_jump_to_top_level (FORCE_EOF
);
9675 /* Don't split words marked W_NOSPLIT. */
9676 if (expanded_something
&& (tlist
->word
->flags
& W_NOSPLIT
) == 0)
9678 temp_list
= word_list_split (expanded
);
9679 dispose_words (expanded
);
9683 /* If no parameter expansion, command substitution, process
9684 substitution, or arithmetic substitution took place, then
9685 do not do word splitting. We still have to remove quoted
9686 null characters from the result. */
9687 word_list_remove_quoted_nulls (expanded
);
9688 temp_list
= expanded
;
9691 expanded
= REVERSE_LIST (temp_list
, WORD_LIST
*);
9692 new_list
= (WORD_LIST
*)list_append (expanded
, new_list
);
9696 dispose_words (orig_list
);
9699 new_list
= REVERSE_LIST (new_list
, WORD_LIST
*);
9704 /* The workhorse for expand_words () and expand_words_no_vars ().
9705 First arg is LIST, a WORD_LIST of words.
9706 Second arg EFLAGS is a flags word controlling which expansions are
9709 This does all of the substitutions: brace expansion, tilde expansion,
9710 parameter expansion, command substitution, arithmetic expansion,
9711 process substitution, word splitting, and pathname expansion, according
9712 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
9713 set, or for which no expansion is done, do not undergo word splitting.
9714 Words with the W_NOGLOB bit set do not undergo pathname expansion; words
9715 with W_NOBRACE set do not undergo brace expansion (see
9716 brace_expand_word_list above). */
9718 expand_word_list_internal (list
, eflags
)
9722 WORD_LIST
*new_list
, *temp_list
;
9725 tempenv_assign_error
= 0;
9727 return ((WORD_LIST
*)NULL
);
9729 garglist
= new_list
= copy_word_list (list
);
9730 if (eflags
& WEXP_VARASSIGN
)
9732 garglist
= new_list
= separate_out_assignments (new_list
);
9735 if (subst_assign_varlist
)
9737 /* All the words were variable assignments, so they are placed
9738 into the shell's environment. */
9739 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
9741 this_command_name
= (char *)NULL
; /* no arithmetic errors */
9742 tint
= do_word_assignment (temp_list
->word
, 0);
9743 /* Variable assignment errors in non-interactive shells
9744 running in Posix.2 mode cause the shell to exit. */
9747 last_command_exit_value
= EXECUTION_FAILURE
;
9748 if (interactive_shell
== 0 && posixly_correct
)
9749 exp_jump_to_top_level (FORCE_EOF
);
9751 exp_jump_to_top_level (DISCARD
);
9754 dispose_words (subst_assign_varlist
);
9755 subst_assign_varlist
= (WORD_LIST
*)NULL
;
9757 return ((WORD_LIST
*)NULL
);
9761 /* Begin expanding the words that remain. The expansions take place on
9762 things that aren't really variable assignments. */
9764 #if defined (BRACE_EXPANSION)
9765 /* Do brace expansion on this word if there are any brace characters
9767 if ((eflags
& WEXP_BRACEEXP
) && brace_expansion
&& new_list
)
9768 new_list
= brace_expand_word_list (new_list
, eflags
);
9769 #endif /* BRACE_EXPANSION */
9771 /* Perform the `normal' shell expansions: tilde expansion, parameter and
9772 variable substitution, command substitution, arithmetic expansion,
9773 and word splitting. */
9774 new_list
= shell_expand_word_list (new_list
, eflags
);
9776 /* Okay, we're almost done. Now let's just do some filename
9780 if ((eflags
& WEXP_PATHEXP
) && disallow_filename_globbing
== 0)
9781 /* Glob expand the word list unless globbing has been disabled. */
9782 new_list
= glob_expand_word_list (new_list
, eflags
);
9784 /* Dequote the words, because we're not performing globbing. */
9785 new_list
= dequote_list (new_list
);
9788 if ((eflags
& WEXP_VARASSIGN
) && subst_assign_varlist
)
9790 sh_wassign_func_t
*assign_func
;
9791 int is_special_builtin
, is_builtin_or_func
;
9793 /* If the remainder of the words expand to nothing, Posix.2 requires
9794 that the variable and environment assignments affect the shell's
9796 assign_func
= new_list
? assign_in_env
: do_word_assignment
;
9797 tempenv_assign_error
= 0;
9799 is_builtin_or_func
= (new_list
&& new_list
->word
&& (find_shell_builtin (new_list
->word
->word
) || find_function (new_list
->word
->word
)));
9800 /* Posix says that special builtins exit if a variable assignment error
9801 occurs in an assignment preceding it. */
9802 is_special_builtin
= (posixly_correct
&& new_list
&& new_list
->word
&& find_special_builtin (new_list
->word
->word
));
9804 for (temp_list
= subst_assign_varlist
; temp_list
; temp_list
= temp_list
->next
)
9806 this_command_name
= (char *)NULL
;
9807 assigning_in_environment
= (assign_func
== assign_in_env
);
9808 tint
= (*assign_func
) (temp_list
->word
, is_builtin_or_func
);
9809 assigning_in_environment
= 0;
9810 /* Variable assignment errors in non-interactive shells running
9811 in Posix.2 mode cause the shell to exit. */
9814 if (assign_func
== do_word_assignment
)
9816 last_command_exit_value
= EXECUTION_FAILURE
;
9817 if (interactive_shell
== 0 && posixly_correct
&& is_special_builtin
)
9818 exp_jump_to_top_level (FORCE_EOF
);
9820 exp_jump_to_top_level (DISCARD
);
9823 tempenv_assign_error
++;
9827 dispose_words (subst_assign_varlist
);
9828 subst_assign_varlist
= (WORD_LIST
*)NULL
;