]>
Commit | Line | Data |
---|---|---|
95732b49 JA |
1 | /* subst.c -- The part of the shell that does parameter, command, arithmetic, |
2 | and globbing substitutions. */ | |
726f6388 | 3 | |
bb70624e JA |
4 | /* ``Have a little faith, there's magic in the night. You ain't a |
5 | beauty, but, hey, you're alright.'' */ | |
6 | ||
a0c0a00f | 7 | /* Copyright (C) 1987-2016 Free Software Foundation, Inc. |
726f6388 JA |
8 | |
9 | This file is part of GNU Bash, the Bourne Again SHell. | |
10 | ||
3185942a JA |
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. | |
726f6388 | 15 | |
3185942a JA |
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. | |
726f6388 | 20 | |
3185942a JA |
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/>. | |
23 | */ | |
726f6388 | 24 | |
ccc6cda3 JA |
25 | #include "config.h" |
26 | ||
726f6388 JA |
27 | #include "bashtypes.h" |
28 | #include <stdio.h> | |
f73dda09 | 29 | #include "chartypes.h" |
3185942a JA |
30 | #if defined (HAVE_PWD_H) |
31 | # include <pwd.h> | |
32 | #endif | |
726f6388 JA |
33 | #include <signal.h> |
34 | #include <errno.h> | |
ccc6cda3 JA |
35 | |
36 | #if defined (HAVE_UNISTD_H) | |
37 | # include <unistd.h> | |
38 | #endif | |
726f6388 | 39 | |
a0c0a00f CR |
40 | #define NEED_FPURGE_DECL |
41 | ||
726f6388 JA |
42 | #include "bashansi.h" |
43 | #include "posixstat.h" | |
b80f6443 | 44 | #include "bashintl.h" |
726f6388 JA |
45 | |
46 | #include "shell.h" | |
495aee44 | 47 | #include "parser.h" |
726f6388 JA |
48 | #include "flags.h" |
49 | #include "jobs.h" | |
50 | #include "execute_cmd.h" | |
51 | #include "filecntl.h" | |
ccc6cda3 JA |
52 | #include "trap.h" |
53 | #include "pathexp.h" | |
54 | #include "mailcheck.h" | |
55 | ||
7117c2d2 | 56 | #include "shmbutil.h" |
a0c0a00f CR |
57 | #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR) |
58 | # include <mbstr.h> /* mbschr */ | |
59 | #endif | |
495aee44 | 60 | #include "typemax.h" |
7117c2d2 | 61 | |
ccc6cda3 JA |
62 | #include "builtins/getopt.h" |
63 | #include "builtins/common.h" | |
726f6388 | 64 | |
3185942a JA |
65 | #include "builtins/builtext.h" |
66 | ||
cce855bc | 67 | #include <tilde/tilde.h> |
f73dda09 | 68 | #include <glob/strmatch.h> |
ccc6cda3 JA |
69 | |
70 | #if !defined (errno) | |
71 | extern int errno; | |
72 | #endif /* !errno */ | |
726f6388 JA |
73 | |
74 | /* The size that strings change by. */ | |
d166f048 | 75 | #define DEFAULT_INITIAL_ARRAY_SIZE 112 |
ccc6cda3 JA |
76 | #define DEFAULT_ARRAY_SIZE 128 |
77 | ||
78 | /* Variable types. */ | |
79 | #define VT_VARIABLE 0 | |
80 | #define VT_POSPARMS 1 | |
81 | #define VT_ARRAYVAR 2 | |
d166f048 | 82 | #define VT_ARRAYMEMBER 3 |
3185942a | 83 | #define VT_ASSOCVAR 4 |
726f6388 | 84 | |
b80f6443 JA |
85 | #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */ |
86 | ||
ccc6cda3 JA |
87 | /* Flags for quoted_strchr */ |
88 | #define ST_BACKSL 0x01 | |
89 | #define ST_CTLESC 0x02 | |
7117c2d2 JA |
90 | #define ST_SQUOTE 0x04 /* unused yet */ |
91 | #define ST_DQUOTE 0x08 /* unused yet */ | |
92 | ||
cce855bc JA |
93 | /* These defs make it easier to use the editor. */ |
94 | #define LBRACE '{' | |
95 | #define RBRACE '}' | |
96 | #define LPAREN '(' | |
97 | #define RPAREN ')' | |
ac50fbac CR |
98 | #define LBRACK '[' |
99 | #define RBRACK ']' | |
726f6388 | 100 | |
0001803f CR |
101 | #if defined (HANDLE_MULTIBYTE) |
102 | #define WLPAREN L'(' | |
103 | #define WRPAREN L')' | |
104 | #endif | |
105 | ||
a0c0a00f CR |
106 | #define DOLLAR_AT_STAR(c) ((c) == '@' || (c) == '*') |
107 | #define STR_DOLLAR_AT_STAR(s) (DOLLAR_AT_STAR ((s)[0]) && (s)[1] == '\0') | |
108 | ||
28ef6c31 JA |
109 | /* Evaluates to 1 if C is one of the shell's special parameters whose length |
110 | can be taken, but is also one of the special expansion characters. */ | |
111 | #define VALID_SPECIAL_LENGTH_PARAM(c) \ | |
a0c0a00f | 112 | ((c) == '-' || (c) == '?' || (c) == '#' || (c) == '@') |
28ef6c31 JA |
113 | |
114 | /* Evaluates to 1 if C is one of the shell's special parameters for which an | |
115 | indirect variable reference may be made. */ | |
116 | #define VALID_INDIR_PARAM(c) \ | |
495aee44 | 117 | ((posixly_correct == 0 && (c) == '#') || (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*') |
28ef6c31 JA |
118 | |
119 | /* Evaluates to 1 if C is one of the OP characters that follows the parameter | |
120 | in ${parameter[:]OPword}. */ | |
7117c2d2 | 121 | #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP) |
28ef6c31 | 122 | |
bb70624e JA |
123 | /* Evaluates to 1 if this is one of the shell's special variables. */ |
124 | #define SPECIAL_VAR(name, wi) \ | |
a0c0a00f | 125 | (*name && ((DIGIT (*name) && all_digits (name)) || \ |
f73dda09 | 126 | (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \ |
a0c0a00f CR |
127 | (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))) |
128 | ||
129 | /* This can be used by all of the *_extract_* functions that have a similar | |
130 | structure. It can't just be wrapped in a do...while(0) loop because of | |
131 | the embedded `break'. The dangling else accommodates a trailing semicolon; | |
132 | we could also put in a do ; while (0) */ | |
133 | ||
134 | ||
135 | #define CHECK_STRING_OVERRUN(oind, ind, len, ch) \ | |
136 | if (ind >= len) \ | |
137 | { \ | |
138 | oind = len; \ | |
139 | ch = 0; \ | |
140 | break; \ | |
141 | } \ | |
142 | else \ | |
bb70624e | 143 | |
f73dda09 JA |
144 | /* An expansion function that takes a string and a quoted flag and returns |
145 | a WORD_LIST *. Used as the type of the third argument to | |
146 | expand_string_if_necessary(). */ | |
147 | typedef WORD_LIST *EXPFUNC __P((char *, int)); | |
148 | ||
726f6388 JA |
149 | /* Process ID of the last command executed within command substitution. */ |
150 | pid_t last_command_subst_pid = NO_PID; | |
b72432fd | 151 | pid_t current_command_subst_pid = NO_PID; |
726f6388 | 152 | |
7117c2d2 JA |
153 | /* Variables used to keep track of the characters in IFS. */ |
154 | SHELL_VAR *ifs_var; | |
155 | char *ifs_value; | |
156 | unsigned char ifs_cmap[UCHAR_MAX + 1]; | |
ac50fbac | 157 | int ifs_is_set, ifs_is_null; |
95732b49 JA |
158 | |
159 | #if defined (HANDLE_MULTIBYTE) | |
160 | unsigned char ifs_firstc[MB_LEN_MAX]; | |
161 | size_t ifs_firstc_len; | |
162 | #else | |
7117c2d2 | 163 | unsigned char ifs_firstc; |
95732b49 | 164 | #endif |
7117c2d2 | 165 | |
a0c0a00f CR |
166 | /* If non-zero, command substitution inherits the value of errexit option */ |
167 | int inherit_errexit = 0; | |
168 | ||
0001803f CR |
169 | /* Sentinel to tell when we are performing variable assignments preceding a |
170 | command name and putting them into the environment. Used to make sure | |
171 | we use the temporary environment when looking up variable values. */ | |
3185942a JA |
172 | int assigning_in_environment; |
173 | ||
0001803f CR |
174 | /* Used to hold a list of variable assignments preceding a command. Global |
175 | so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a | |
176 | SIGCHLD trap and so it can be saved and restored by the trap handlers. */ | |
177 | WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL; | |
178 | ||
a0c0a00f CR |
179 | /* Tell the expansion functions to not longjmp back to top_level on fatal |
180 | errors. Enabled when doing completion and prompt string expansion. */ | |
181 | int no_longjmp_on_fatal_error = 0; | |
182 | ||
726f6388 | 183 | /* Extern functions and variables from different files. */ |
b80f6443 | 184 | extern int last_command_exit_value, last_command_exit_signal; |
a0c0a00f | 185 | extern int subshell_environment, running_in_background; |
0001803f | 186 | extern int subshell_level, parse_and_execute_level, sourcelevel; |
a0c0a00f | 187 | extern int eof_encountered, line_number; |
bb70624e | 188 | extern int return_catch_flag, return_catch_value; |
f73dda09 | 189 | extern pid_t dollar_dollar_pid; |
726f6388 | 190 | extern int posixly_correct; |
726f6388 | 191 | extern char *this_command_name; |
ccc6cda3 | 192 | extern struct fd_bitmap *current_fds_to_close; |
cce855bc | 193 | extern int wordexp_only; |
b80f6443 JA |
194 | extern int expanding_redir; |
195 | extern int tempenv_assign_error; | |
ac50fbac | 196 | extern int builtin_ignoring_errexit; |
726f6388 | 197 | |
a0c0a00f CR |
198 | #if defined (JOB_CONTROL) && defined (PROCESS_SUBSTITUTION) |
199 | extern PROCESS *last_procsub_child; | |
200 | #endif | |
201 | ||
0628567a JA |
202 | #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE) |
203 | extern wchar_t *wcsdup __P((const wchar_t *)); | |
204 | #endif | |
205 | ||
ccc6cda3 JA |
206 | /* Non-zero means to allow unmatched globbed filenames to expand to |
207 | a null file. */ | |
208 | int allow_null_glob_expansion; | |
209 | ||
b80f6443 JA |
210 | /* Non-zero means to throw an error when globbing fails to match anything. */ |
211 | int fail_glob_expansion; | |
212 | ||
f73dda09 | 213 | #if 0 |
ccc6cda3 JA |
214 | /* Variables to keep track of which words in an expanded word list (the |
215 | output of expand_word_list_internal) are the result of globbing | |
f73dda09 JA |
216 | expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c. |
217 | (CURRENTLY UNUSED). */ | |
ccc6cda3 JA |
218 | char *glob_argv_flags; |
219 | static int glob_argv_flags_size; | |
f73dda09 | 220 | #endif |
726f6388 | 221 | |
a0c0a00f CR |
222 | static WORD_LIST *cached_quoted_dollar_at = 0; |
223 | ||
726f6388 | 224 | static WORD_LIST expand_word_error, expand_word_fatal; |
95732b49 | 225 | static WORD_DESC expand_wdesc_error, expand_wdesc_fatal; |
726f6388 | 226 | static char expand_param_error, expand_param_fatal; |
95732b49 | 227 | static char extract_string_error, extract_string_fatal; |
726f6388 | 228 | |
28ef6c31 JA |
229 | /* Set by expand_word_unsplit; used to inhibit splitting and re-joining |
230 | $* on $IFS, primarily when doing assignment statements. */ | |
231 | static int expand_no_split_dollar_star = 0; | |
bb70624e | 232 | |
bb70624e JA |
233 | /* A WORD_LIST of words to be expanded by expand_word_list_internal, |
234 | without any leading variable assignments. */ | |
235 | static WORD_LIST *garglist = (WORD_LIST *)NULL; | |
b72432fd | 236 | |
f73dda09 | 237 | static char *quoted_substring __P((char *, int, int)); |
7117c2d2 JA |
238 | static int quoted_strlen __P((char *)); |
239 | static char *quoted_strchr __P((char *, int, int)); | |
f73dda09 JA |
240 | |
241 | static char *expand_string_if_necessary __P((char *, int, EXPFUNC *)); | |
242 | static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *)); | |
243 | static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *)); | |
244 | static WORD_LIST *expand_string_internal __P((char *, int)); | |
245 | static WORD_LIST *expand_string_leave_quoted __P((char *, int)); | |
246 | static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *)); | |
247 | ||
f73dda09 | 248 | static WORD_LIST *list_quote_escapes __P((WORD_LIST *)); |
ac50fbac CR |
249 | static WORD_LIST *list_dequote_escapes __P((WORD_LIST *)); |
250 | ||
f73dda09 JA |
251 | static char *make_quoted_char __P((int)); |
252 | static WORD_LIST *quote_list __P((WORD_LIST *)); | |
f73dda09 JA |
253 | |
254 | static int unquoted_substring __P((char *, char *)); | |
255 | static int unquoted_member __P((int, char *)); | |
256 | ||
95732b49 JA |
257 | #if defined (ARRAY_VARS) |
258 | static SHELL_VAR *do_compound_assignment __P((char *, char *, int)); | |
259 | #endif | |
260 | static int do_assignment_internal __P((const WORD_DESC *, int)); | |
f73dda09 | 261 | |
3185942a | 262 | static char *string_extract_verbatim __P((char *, size_t, int *, char *, int)); |
f73dda09 JA |
263 | static char *string_extract __P((char *, int *, char *, int)); |
264 | static char *string_extract_double_quoted __P((char *, int *, int)); | |
7117c2d2 | 265 | static inline char *string_extract_single_quoted __P((char *, int *)); |
a0c0a00f CR |
266 | static inline int skip_single_quoted __P((const char *, size_t, int, int)); |
267 | static int skip_double_quoted __P((char *, size_t, int, int)); | |
7117c2d2 JA |
268 | static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int)); |
269 | static char *extract_dollar_brace_string __P((char *, int *, int, int)); | |
89a92869 | 270 | static int skip_matched_pair __P((const char *, int, int, int, int)); |
f73dda09 | 271 | |
f73dda09 JA |
272 | static char *pos_params __P((char *, int, int, int)); |
273 | ||
b80f6443 JA |
274 | static unsigned char *mb_getcharlens __P((char *, int)); |
275 | ||
276 | static char *remove_upattern __P((char *, char *, int)); | |
0628567a | 277 | #if defined (HANDLE_MULTIBYTE) |
b80f6443 JA |
278 | static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int)); |
279 | #endif | |
f73dda09 | 280 | static char *remove_pattern __P((char *, char *, int)); |
b80f6443 | 281 | |
b80f6443 JA |
282 | static int match_upattern __P((char *, char *, int, char **, char **)); |
283 | #if defined (HANDLE_MULTIBYTE) | |
b80f6443 JA |
284 | static int match_wpattern __P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **)); |
285 | #endif | |
f73dda09 JA |
286 | static int match_pattern __P((char *, char *, int, char **, char **)); |
287 | static int getpatspec __P((int, char *)); | |
288 | static char *getpattern __P((char *, int, int)); | |
7117c2d2 | 289 | static char *variable_remove_pattern __P((char *, char *, int, int)); |
f73dda09 | 290 | static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int)); |
7117c2d2 | 291 | static char *parameter_list_remove_pattern __P((int, char *, int, int)); |
f73dda09 | 292 | #ifdef ARRAY_VARS |
3185942a | 293 | static char *array_remove_pattern __P((SHELL_VAR *, char *, int, char *, int)); |
f73dda09 | 294 | #endif |
495aee44 | 295 | static char *parameter_brace_remove_pattern __P((char *, char *, int, char *, int, int, int)); |
f73dda09 | 296 | |
a0c0a00f CR |
297 | static char *string_var_assignment __P((SHELL_VAR *, char *)); |
298 | #if defined (ARRAY_VARS) | |
299 | static char *array_var_assignment __P((SHELL_VAR *, int, int)); | |
300 | #endif | |
301 | static char *pos_params_assignment __P((WORD_LIST *, int, int)); | |
302 | static char *string_transform __P((int, SHELL_VAR *, char *)); | |
303 | static char *list_transform __P((int, SHELL_VAR *, WORD_LIST *, int, int)); | |
304 | static char *parameter_list_transform __P((int, int, int)); | |
305 | #if defined ARRAY_VARS | |
306 | static char *array_transform __P((int, SHELL_VAR *, char *, int)); | |
307 | #endif | |
308 | static char *parameter_brace_transform __P((char *, char *, int, char *, int, int, int)); | |
309 | ||
f73dda09 JA |
310 | static char *process_substitute __P((char *, int)); |
311 | ||
3185942a | 312 | static char *read_comsub __P((int, int, int *)); |
f73dda09 JA |
313 | |
314 | #ifdef ARRAY_VARS | |
315 | static arrayind_t array_length_reference __P((char *)); | |
316 | #endif | |
317 | ||
318 | static int valid_brace_expansion_word __P((char *, int)); | |
b80f6443 | 319 | static int chk_atstar __P((char *, int, int *, int *)); |
0628567a | 320 | static int chk_arithsub __P((const char *, int)); |
b80f6443 | 321 | |
495aee44 | 322 | static WORD_DESC *parameter_brace_expand_word __P((char *, int, int, int, arrayind_t *)); |
ac50fbac | 323 | static char *parameter_brace_find_indir __P((char *, int, int, int)); |
95732b49 | 324 | static WORD_DESC *parameter_brace_expand_indir __P((char *, int, int, int *, int *)); |
a0c0a00f | 325 | static WORD_DESC *parameter_brace_expand_rhs __P((char *, char *, int, int, int, int *, int *)); |
f73dda09 JA |
326 | static void parameter_brace_expand_error __P((char *, char *)); |
327 | ||
328 | static int valid_length_expression __P((char *)); | |
7117c2d2 | 329 | static intmax_t parameter_brace_expand_length __P((char *)); |
f73dda09 JA |
330 | |
331 | static char *skiparith __P((char *, int)); | |
3185942a | 332 | static int verify_substring_values __P((SHELL_VAR *, char *, char *, int, intmax_t *, intmax_t *)); |
495aee44 | 333 | static int get_var_and_type __P((char *, char *, arrayind_t, int, int, SHELL_VAR **, char **)); |
b80f6443 | 334 | static char *mb_substring __P((char *, int, int)); |
495aee44 CR |
335 | static char *parameter_brace_substring __P((char *, char *, int, char *, int, int)); |
336 | ||
337 | static int shouldexp_replacement __P((char *)); | |
f73dda09 JA |
338 | |
339 | static char *pos_params_pat_subst __P((char *, char *, char *, int)); | |
340 | ||
a0c0a00f | 341 | static char *parameter_brace_patsub __P((char *, char *, int, char *, int, int, int)); |
f73dda09 | 342 | |
3185942a | 343 | static char *pos_params_casemod __P((char *, char *, int, int)); |
495aee44 | 344 | static char *parameter_brace_casemod __P((char *, char *, int, int, char *, int, int)); |
3185942a | 345 | |
0001803f | 346 | static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int, int *, int *)); |
95732b49 | 347 | static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int)); |
f73dda09 JA |
348 | |
349 | static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *)); | |
350 | ||
f73dda09 JA |
351 | static WORD_LIST *word_list_split __P((WORD_LIST *)); |
352 | ||
b80f6443 JA |
353 | static void exp_jump_to_top_level __P((int)); |
354 | ||
f73dda09 JA |
355 | static WORD_LIST *separate_out_assignments __P((WORD_LIST *)); |
356 | static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int)); | |
357 | #ifdef BRACE_EXPANSION | |
358 | static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int)); | |
359 | #endif | |
3185942a | 360 | #if defined (ARRAY_VARS) |
a0c0a00f | 361 | static int make_internal_declare __P((char *, char *, char *)); |
3185942a | 362 | #endif |
f73dda09 JA |
363 | static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int)); |
364 | static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int)); | |
726f6388 JA |
365 | |
366 | /* **************************************************************** */ | |
367 | /* */ | |
368 | /* Utility Functions */ | |
369 | /* */ | |
370 | /* **************************************************************** */ | |
371 | ||
0001803f CR |
372 | #if defined (DEBUG) |
373 | void | |
374 | dump_word_flags (flags) | |
375 | int flags; | |
376 | { | |
377 | int f; | |
378 | ||
379 | f = flags; | |
380 | fprintf (stderr, "%d -> ", f); | |
a0c0a00f CR |
381 | if (f & W_ARRAYIND) |
382 | { | |
383 | f &= ~W_ARRAYIND; | |
384 | fprintf (stderr, "W_ARRAYIND%s", f ? "|" : ""); | |
385 | } | |
0001803f CR |
386 | if (f & W_ASSIGNASSOC) |
387 | { | |
388 | f &= ~W_ASSIGNASSOC; | |
389 | fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : ""); | |
390 | } | |
ac50fbac CR |
391 | if (f & W_ASSIGNARRAY) |
392 | { | |
393 | f &= ~W_ASSIGNARRAY; | |
394 | fprintf (stderr, "W_ASSIGNARRAY%s", f ? "|" : ""); | |
395 | } | |
0001803f CR |
396 | if (f & W_HASCTLESC) |
397 | { | |
398 | f &= ~W_HASCTLESC; | |
399 | fprintf (stderr, "W_HASCTLESC%s", f ? "|" : ""); | |
400 | } | |
401 | if (f & W_NOPROCSUB) | |
402 | { | |
403 | f &= ~W_NOPROCSUB; | |
404 | fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : ""); | |
405 | } | |
406 | if (f & W_DQUOTE) | |
407 | { | |
408 | f &= ~W_DQUOTE; | |
409 | fprintf (stderr, "W_DQUOTE%s", f ? "|" : ""); | |
410 | } | |
411 | if (f & W_HASQUOTEDNULL) | |
412 | { | |
413 | f &= ~W_HASQUOTEDNULL; | |
414 | fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : ""); | |
415 | } | |
416 | if (f & W_ASSIGNARG) | |
417 | { | |
418 | f &= ~W_ASSIGNARG; | |
419 | fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : ""); | |
420 | } | |
421 | if (f & W_ASSNBLTIN) | |
422 | { | |
423 | f &= ~W_ASSNBLTIN; | |
424 | fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : ""); | |
425 | } | |
6d41b715 CR |
426 | if (f & W_ASSNGLOBAL) |
427 | { | |
428 | f &= ~W_ASSNGLOBAL; | |
429 | fprintf (stderr, "W_ASSNGLOBAL%s", f ? "|" : ""); | |
430 | } | |
0001803f CR |
431 | if (f & W_COMPASSIGN) |
432 | { | |
433 | f &= ~W_COMPASSIGN; | |
434 | fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : ""); | |
435 | } | |
436 | if (f & W_NOEXPAND) | |
437 | { | |
438 | f &= ~W_NOEXPAND; | |
439 | fprintf (stderr, "W_NOEXPAND%s", f ? "|" : ""); | |
440 | } | |
441 | if (f & W_ITILDE) | |
442 | { | |
443 | f &= ~W_ITILDE; | |
444 | fprintf (stderr, "W_ITILDE%s", f ? "|" : ""); | |
445 | } | |
446 | if (f & W_NOTILDE) | |
447 | { | |
448 | f &= ~W_NOTILDE; | |
449 | fprintf (stderr, "W_NOTILDE%s", f ? "|" : ""); | |
450 | } | |
451 | if (f & W_ASSIGNRHS) | |
452 | { | |
453 | f &= ~W_ASSIGNRHS; | |
454 | fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : ""); | |
455 | } | |
456 | if (f & W_NOCOMSUB) | |
457 | { | |
458 | f &= ~W_NOCOMSUB; | |
459 | fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : ""); | |
460 | } | |
461 | if (f & W_DOLLARSTAR) | |
462 | { | |
463 | f &= ~W_DOLLARSTAR; | |
464 | fprintf (stderr, "W_DOLLARSTAR%s", f ? "|" : ""); | |
465 | } | |
466 | if (f & W_DOLLARAT) | |
467 | { | |
468 | f &= ~W_DOLLARAT; | |
469 | fprintf (stderr, "W_DOLLARAT%s", f ? "|" : ""); | |
470 | } | |
471 | if (f & W_TILDEEXP) | |
472 | { | |
473 | f &= ~W_TILDEEXP; | |
474 | fprintf (stderr, "W_TILDEEXP%s", f ? "|" : ""); | |
475 | } | |
476 | if (f & W_NOSPLIT2) | |
477 | { | |
478 | f &= ~W_NOSPLIT2; | |
479 | fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : ""); | |
480 | } | |
0001803f CR |
481 | if (f & W_NOSPLIT) |
482 | { | |
483 | f &= ~W_NOSPLIT; | |
484 | fprintf (stderr, "W_NOSPLIT%s", f ? "|" : ""); | |
485 | } | |
ac50fbac | 486 | if (f & W_NOBRACE) |
0001803f | 487 | { |
ac50fbac CR |
488 | f &= ~W_NOBRACE; |
489 | fprintf (stderr, "W_NOBRACE%s", f ? "|" : ""); | |
490 | } | |
491 | if (f & W_NOGLOB) | |
492 | { | |
493 | f &= ~W_NOGLOB; | |
494 | fprintf (stderr, "W_NOGLOB%s", f ? "|" : ""); | |
495 | } | |
496 | if (f & W_SPLITSPACE) | |
497 | { | |
498 | f &= ~W_SPLITSPACE; | |
499 | fprintf (stderr, "W_SPLITSPACE%s", f ? "|" : ""); | |
0001803f CR |
500 | } |
501 | if (f & W_ASSIGNMENT) | |
502 | { | |
503 | f &= ~W_ASSIGNMENT; | |
504 | fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : ""); | |
505 | } | |
506 | if (f & W_QUOTED) | |
507 | { | |
508 | f &= ~W_QUOTED; | |
509 | fprintf (stderr, "W_QUOTED%s", f ? "|" : ""); | |
510 | } | |
511 | if (f & W_HASDOLLAR) | |
512 | { | |
513 | f &= ~W_HASDOLLAR; | |
514 | fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : ""); | |
515 | } | |
a0c0a00f CR |
516 | if (f & W_COMPLETE) |
517 | { | |
518 | f &= ~W_COMPLETE; | |
519 | fprintf (stderr, "W_COMPLETE%s", f ? "|" : ""); | |
520 | } | |
521 | ||
0001803f CR |
522 | fprintf (stderr, "\n"); |
523 | fflush (stderr); | |
524 | } | |
525 | #endif | |
526 | ||
7117c2d2 | 527 | #ifdef INCLUDE_UNUSED |
ccc6cda3 JA |
528 | static char * |
529 | quoted_substring (string, start, end) | |
530 | char *string; | |
531 | int start, end; | |
532 | { | |
533 | register int len, l; | |
534 | register char *result, *s, *r; | |
535 | ||
536 | len = end - start; | |
537 | ||
538 | /* Move to string[start], skipping quoted characters. */ | |
539 | for (s = string, l = 0; *s && l < start; ) | |
540 | { | |
541 | if (*s == CTLESC) | |
542 | { | |
28ef6c31 JA |
543 | s++; |
544 | continue; | |
ccc6cda3 JA |
545 | } |
546 | l++; | |
547 | if (*s == 0) | |
28ef6c31 | 548 | break; |
ccc6cda3 JA |
549 | } |
550 | ||
f73dda09 | 551 | r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */ |
ccc6cda3 JA |
552 | |
553 | /* Copy LEN characters, including quote characters. */ | |
554 | s = string + l; | |
555 | for (l = 0; l < len; s++) | |
556 | { | |
557 | if (*s == CTLESC) | |
28ef6c31 | 558 | *r++ = *s++; |
ccc6cda3 JA |
559 | *r++ = *s; |
560 | l++; | |
561 | if (*s == 0) | |
28ef6c31 | 562 | break; |
ccc6cda3 JA |
563 | } |
564 | *r = '\0'; | |
565 | return result; | |
566 | } | |
7117c2d2 JA |
567 | #endif |
568 | ||
569 | #ifdef INCLUDE_UNUSED | |
570 | /* Return the length of S, skipping over quoted characters */ | |
571 | static int | |
572 | quoted_strlen (s) | |
573 | char *s; | |
574 | { | |
575 | register char *p; | |
576 | int i; | |
577 | ||
578 | i = 0; | |
579 | for (p = s; *p; p++) | |
580 | { | |
581 | if (*p == CTLESC) | |
582 | { | |
583 | p++; | |
584 | if (*p == 0) | |
585 | return (i + 1); | |
586 | } | |
587 | i++; | |
588 | } | |
589 | ||
590 | return i; | |
591 | } | |
592 | #endif | |
ccc6cda3 JA |
593 | |
594 | /* Find the first occurrence of character C in string S, obeying shell | |
595 | quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped | |
596 | characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters | |
597 | escaped with CTLESC are skipped. */ | |
7117c2d2 | 598 | static char * |
ccc6cda3 JA |
599 | quoted_strchr (s, c, flags) |
600 | char *s; | |
601 | int c, flags; | |
602 | { | |
603 | register char *p; | |
604 | ||
605 | for (p = s; *p; p++) | |
606 | { | |
607 | if (((flags & ST_BACKSL) && *p == '\\') | |
608 | || ((flags & ST_CTLESC) && *p == CTLESC)) | |
609 | { | |
610 | p++; | |
611 | if (*p == '\0') | |
612 | return ((char *)NULL); | |
613 | continue; | |
614 | } | |
615 | else if (*p == c) | |
616 | return p; | |
617 | } | |
618 | return ((char *)NULL); | |
619 | } | |
620 | ||
a0c0a00f | 621 | #if defined (INCLUDE_UNUSED) |
cce855bc | 622 | /* Return 1 if CHARACTER appears in an unquoted portion of |
7117c2d2 | 623 | STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */ |
cce855bc JA |
624 | static int |
625 | unquoted_member (character, string) | |
626 | int character; | |
726f6388 JA |
627 | char *string; |
628 | { | |
7117c2d2 | 629 | size_t slen; |
cce855bc | 630 | int sindex, c; |
7117c2d2 | 631 | DECLARE_MBSTATE; |
726f6388 | 632 | |
7117c2d2 JA |
633 | slen = strlen (string); |
634 | sindex = 0; | |
635 | while (c = string[sindex]) | |
726f6388 | 636 | { |
cce855bc JA |
637 | if (c == character) |
638 | return (1); | |
639 | ||
640 | switch (c) | |
ccc6cda3 | 641 | { |
cce855bc | 642 | default: |
7117c2d2 | 643 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
644 | break; |
645 | ||
646 | case '\\': | |
647 | sindex++; | |
648 | if (string[sindex]) | |
7117c2d2 | 649 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
650 | break; |
651 | ||
652 | case '\'': | |
a0c0a00f | 653 | sindex = skip_single_quoted (string, slen, ++sindex, 0); |
cce855bc JA |
654 | break; |
655 | ||
656 | case '"': | |
a0c0a00f | 657 | sindex = skip_double_quoted (string, slen, ++sindex, 0); |
cce855bc | 658 | break; |
ccc6cda3 | 659 | } |
726f6388 | 660 | } |
cce855bc | 661 | return (0); |
726f6388 JA |
662 | } |
663 | ||
cce855bc JA |
664 | /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */ |
665 | static int | |
666 | unquoted_substring (substr, string) | |
667 | char *substr, *string; | |
726f6388 | 668 | { |
7117c2d2 | 669 | size_t slen; |
cce855bc | 670 | int sindex, c, sublen; |
7117c2d2 | 671 | DECLARE_MBSTATE; |
726f6388 | 672 | |
cce855bc JA |
673 | if (substr == 0 || *substr == '\0') |
674 | return (0); | |
675 | ||
7117c2d2 | 676 | slen = strlen (string); |
cce855bc JA |
677 | sublen = strlen (substr); |
678 | for (sindex = 0; c = string[sindex]; ) | |
726f6388 | 679 | { |
cce855bc JA |
680 | if (STREQN (string + sindex, substr, sublen)) |
681 | return (1); | |
682 | ||
683 | switch (c) | |
684 | { | |
685 | case '\\': | |
686 | sindex++; | |
cce855bc | 687 | if (string[sindex]) |
7117c2d2 | 688 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
689 | break; |
690 | ||
691 | case '\'': | |
a0c0a00f | 692 | sindex = skip_single_quoted (string, slen, ++sindex, 0); |
cce855bc JA |
693 | break; |
694 | ||
695 | case '"': | |
a0c0a00f | 696 | sindex = skip_double_quoted (string, slen, ++sindex, 0); |
cce855bc JA |
697 | break; |
698 | ||
699 | default: | |
7117c2d2 | 700 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
701 | break; |
702 | } | |
726f6388 | 703 | } |
cce855bc | 704 | return (0); |
ccc6cda3 | 705 | } |
a0c0a00f | 706 | #endif |
726f6388 | 707 | |
cce855bc JA |
708 | /* Most of the substitutions must be done in parallel. In order |
709 | to avoid using tons of unclear goto's, I have some functions | |
710 | for manipulating malloc'ed strings. They all take INDX, a | |
711 | pointer to an integer which is the offset into the string | |
712 | where manipulation is taking place. They also take SIZE, a | |
713 | pointer to an integer which is the current length of the | |
714 | character array for this string. */ | |
726f6388 | 715 | |
cce855bc JA |
716 | /* Append SOURCE to TARGET at INDEX. SIZE is the current amount |
717 | of space allocated to TARGET. SOURCE can be NULL, in which | |
718 | case nothing happens. Gets rid of SOURCE by freeing it. | |
719 | Returns TARGET in case the location has changed. */ | |
7117c2d2 | 720 | INLINE char * |
cce855bc JA |
721 | sub_append_string (source, target, indx, size) |
722 | char *source, *target; | |
a0c0a00f CR |
723 | int *indx; |
724 | size_t *size; | |
cce855bc JA |
725 | { |
726 | if (source) | |
726f6388 | 727 | { |
a0c0a00f CR |
728 | int n; |
729 | size_t srclen; | |
cce855bc JA |
730 | |
731 | srclen = STRLEN (source); | |
732 | if (srclen >= (int)(*size - *indx)) | |
726f6388 | 733 | { |
cce855bc JA |
734 | n = srclen + *indx; |
735 | n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE); | |
f73dda09 | 736 | target = (char *)xrealloc (target, (*size = n)); |
726f6388 | 737 | } |
cce855bc JA |
738 | |
739 | FASTCOPY (source, target + *indx, srclen); | |
740 | *indx += srclen; | |
741 | target[*indx] = '\0'; | |
742 | ||
743 | free (source); | |
726f6388 | 744 | } |
cce855bc JA |
745 | return (target); |
746 | } | |
747 | ||
748 | #if 0 | |
749 | /* UNUSED */ | |
750 | /* Append the textual representation of NUMBER to TARGET. | |
751 | INDX and SIZE are as in SUB_APPEND_STRING. */ | |
752 | char * | |
753 | sub_append_number (number, target, indx, size) | |
7117c2d2 | 754 | intmax_t number; |
cce855bc | 755 | char *target; |
a0c0a00f CR |
756 | int *indx; |
757 | size_t *size; | |
cce855bc JA |
758 | { |
759 | char *temp; | |
760 | ||
761 | temp = itos (number); | |
762 | return (sub_append_string (temp, target, indx, size)); | |
726f6388 | 763 | } |
d166f048 | 764 | #endif |
726f6388 JA |
765 | |
766 | /* Extract a substring from STRING, starting at SINDEX and ending with | |
767 | one of the characters in CHARLIST. Don't make the ending character | |
768 | part of the string. Leave SINDEX pointing at the ending character. | |
3185942a | 769 | Understand about backslashes in the string. If (flags & SX_VARNAME) |
7117c2d2 JA |
770 | is non-zero, and array variables have been compiled into the shell, |
771 | everything between a `[' and a corresponding `]' is skipped over. | |
3185942a JA |
772 | If (flags & SX_NOALLOC) is non-zero, don't return the substring, just |
773 | update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must | |
95732b49 | 774 | contain a closing character from CHARLIST. */ |
726f6388 | 775 | static char * |
7117c2d2 | 776 | string_extract (string, sindex, charlist, flags) |
f73dda09 JA |
777 | char *string; |
778 | int *sindex; | |
779 | char *charlist; | |
7117c2d2 | 780 | int flags; |
726f6388 | 781 | { |
ccc6cda3 | 782 | register int c, i; |
95732b49 | 783 | int found; |
7117c2d2 | 784 | size_t slen; |
726f6388 | 785 | char *temp; |
7117c2d2 | 786 | DECLARE_MBSTATE; |
726f6388 | 787 | |
95732b49 | 788 | slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0; |
7117c2d2 | 789 | i = *sindex; |
95732b49 | 790 | found = 0; |
7117c2d2 | 791 | while (c = string[i]) |
726f6388 JA |
792 | { |
793 | if (c == '\\') | |
7117c2d2 JA |
794 | { |
795 | if (string[i + 1]) | |
796 | i++; | |
797 | else | |
798 | break; | |
799 | } | |
ccc6cda3 | 800 | #if defined (ARRAY_VARS) |
3185942a | 801 | else if ((flags & SX_VARNAME) && c == '[') |
ccc6cda3 JA |
802 | { |
803 | int ni; | |
804 | /* If this is an array subscript, skip over it and continue. */ | |
0001803f | 805 | ni = skipsubscript (string, i, 0); |
ccc6cda3 JA |
806 | if (string[ni] == ']') |
807 | i = ni; | |
808 | } | |
809 | #endif | |
810 | else if (MEMBER (c, charlist)) | |
95732b49 JA |
811 | { |
812 | found = 1; | |
726f6388 | 813 | break; |
95732b49 | 814 | } |
7117c2d2 JA |
815 | |
816 | ADVANCE_CHAR (string, slen, i); | |
726f6388 | 817 | } |
bb70624e | 818 | |
95732b49 JA |
819 | /* If we had to have a matching delimiter and didn't find one, return an |
820 | error and let the caller deal with it. */ | |
3185942a | 821 | if ((flags & SX_REQMATCH) && found == 0) |
95732b49 JA |
822 | { |
823 | *sindex = i; | |
824 | return (&extract_string_error); | |
825 | } | |
826 | ||
3185942a | 827 | temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i); |
726f6388 | 828 | *sindex = i; |
95732b49 | 829 | |
726f6388 JA |
830 | return (temp); |
831 | } | |
832 | ||
ccc6cda3 JA |
833 | /* Extract the contents of STRING as if it is enclosed in double quotes. |
834 | SINDEX, when passed in, is the offset of the character immediately | |
835 | following the opening double quote; on exit, SINDEX is left pointing after | |
836 | the closing double quote. If STRIPDQ is non-zero, unquoted double | |
837 | quotes are stripped and the string is terminated by a null byte. | |
838 | Backslashes between the embedded double quotes are processed. If STRIPDQ | |
839 | is zero, an unquoted `"' terminates the string. */ | |
7117c2d2 | 840 | static char * |
a0c0a00f | 841 | string_extract_double_quoted (string, sindex, flags) |
726f6388 | 842 | char *string; |
a0c0a00f | 843 | int *sindex, flags; |
726f6388 | 844 | { |
7117c2d2 JA |
845 | size_t slen; |
846 | char *send; | |
f73dda09 JA |
847 | int j, i, t; |
848 | unsigned char c; | |
ccc6cda3 JA |
849 | char *temp, *ret; /* The new string we return. */ |
850 | int pass_next, backquote, si; /* State variables for the machine. */ | |
851 | int dquote; | |
a0c0a00f | 852 | int stripdq; |
7117c2d2 JA |
853 | DECLARE_MBSTATE; |
854 | ||
855 | slen = strlen (string + *sindex) + *sindex; | |
856 | send = string + slen; | |
726f6388 | 857 | |
a0c0a00f CR |
858 | stripdq = (flags & SX_STRIPDQ); |
859 | ||
ccc6cda3 | 860 | pass_next = backquote = dquote = 0; |
7117c2d2 | 861 | temp = (char *)xmalloc (1 + slen - *sindex); |
726f6388 | 862 | |
7117c2d2 JA |
863 | j = 0; |
864 | i = *sindex; | |
865 | while (c = string[i]) | |
726f6388 | 866 | { |
ccc6cda3 JA |
867 | /* Process a character that was quoted by a backslash. */ |
868 | if (pass_next) | |
726f6388 | 869 | { |
495aee44 | 870 | /* XXX - take another look at this in light of Interp 221 */ |
ccc6cda3 | 871 | /* Posix.2 sez: |
726f6388 | 872 | |
ccc6cda3 JA |
873 | ``The backslash shall retain its special meaning as an escape |
874 | character only when followed by one of the characters: | |
7117c2d2 | 875 | $ ` " \ <newline>''. |
726f6388 | 876 | |
ccc6cda3 JA |
877 | If STRIPDQ is zero, we handle the double quotes here and let |
878 | expand_word_internal handle the rest. If STRIPDQ is non-zero, | |
879 | we have already been through one round of backslash stripping, | |
880 | and want to strip these backslashes only if DQUOTE is non-zero, | |
881 | indicating that we are inside an embedded double-quoted string. */ | |
882 | ||
883 | /* If we are in an embedded quoted string, then don't strip | |
884 | backslashes before characters for which the backslash | |
885 | retains its special meaning, but remove backslashes in | |
886 | front of other characters. If we are not in an | |
887 | embedded quoted string, don't strip backslashes at all. | |
888 | This mess is necessary because the string was already | |
889 | surrounded by double quotes (and sh has some really weird | |
890 | quoting rules). | |
891 | The returned string will be run through expansion as if | |
892 | it were double-quoted. */ | |
893 | if ((stripdq == 0 && c != '"') || | |
28ef6c31 | 894 | (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0))) |
ccc6cda3 | 895 | temp[j++] = '\\'; |
ccc6cda3 | 896 | pass_next = 0; |
7117c2d2 JA |
897 | |
898 | add_one_character: | |
899 | COPY_CHAR_I (temp, j, string, send, i); | |
ccc6cda3 JA |
900 | continue; |
901 | } | |
726f6388 | 902 | |
ccc6cda3 JA |
903 | /* A backslash protects the next character. The code just above |
904 | handles preserving the backslash in front of any character but | |
905 | a double quote. */ | |
906 | if (c == '\\') | |
726f6388 | 907 | { |
ccc6cda3 | 908 | pass_next++; |
7117c2d2 | 909 | i++; |
726f6388 JA |
910 | continue; |
911 | } | |
912 | ||
ccc6cda3 JA |
913 | /* Inside backquotes, ``the portion of the quoted string from the |
914 | initial backquote and the characters up to the next backquote | |
915 | that is not preceded by a backslash, having escape characters | |
916 | removed, defines that command''. */ | |
917 | if (backquote) | |
726f6388 | 918 | { |
ccc6cda3 JA |
919 | if (c == '`') |
920 | backquote = 0; | |
921 | temp[j++] = c; | |
7117c2d2 | 922 | i++; |
726f6388 JA |
923 | continue; |
924 | } | |
925 | ||
ccc6cda3 | 926 | if (c == '`') |
726f6388 | 927 | { |
ccc6cda3 JA |
928 | temp[j++] = c; |
929 | backquote++; | |
7117c2d2 | 930 | i++; |
ccc6cda3 | 931 | continue; |
726f6388 JA |
932 | } |
933 | ||
ccc6cda3 JA |
934 | /* Pass everything between `$(' and the matching `)' or a quoted |
935 | ${ ... } pair through according to the Posix.2 specification. */ | |
cce855bc | 936 | if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE))) |
726f6388 | 937 | { |
b80f6443 JA |
938 | int free_ret = 1; |
939 | ||
ccc6cda3 | 940 | si = i + 2; |
cce855bc | 941 | if (string[i + 1] == LPAREN) |
a0c0a00f | 942 | ret = extract_command_subst (string, &si, (flags & SX_COMPLETE)); |
ccc6cda3 | 943 | else |
495aee44 | 944 | ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, 0); |
726f6388 | 945 | |
ccc6cda3 JA |
946 | temp[j++] = '$'; |
947 | temp[j++] = string[i + 1]; | |
726f6388 | 948 | |
b80f6443 JA |
949 | /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error |
950 | is set. */ | |
951 | if (ret == 0 && no_longjmp_on_fatal_error) | |
952 | { | |
953 | free_ret = 0; | |
954 | ret = string + i + 2; | |
955 | } | |
956 | ||
ccc6cda3 JA |
957 | for (t = 0; ret[t]; t++, j++) |
958 | temp[j] = ret[t]; | |
b80f6443 | 959 | temp[j] = string[si]; |
726f6388 | 960 | |
b80f6443 JA |
961 | if (string[si]) |
962 | { | |
963 | j++; | |
964 | i = si + 1; | |
965 | } | |
966 | else | |
967 | i = si; | |
968 | ||
969 | if (free_ret) | |
970 | free (ret); | |
ccc6cda3 | 971 | continue; |
726f6388 JA |
972 | } |
973 | ||
ccc6cda3 | 974 | /* Add any character but a double quote to the quoted string we're |
28ef6c31 | 975 | accumulating. */ |
ccc6cda3 | 976 | if (c != '"') |
7117c2d2 | 977 | goto add_one_character; |
ccc6cda3 JA |
978 | |
979 | /* c == '"' */ | |
980 | if (stripdq) | |
726f6388 | 981 | { |
ccc6cda3 | 982 | dquote ^= 1; |
7117c2d2 | 983 | i++; |
ccc6cda3 | 984 | continue; |
726f6388 | 985 | } |
ccc6cda3 JA |
986 | |
987 | break; | |
726f6388 | 988 | } |
ccc6cda3 | 989 | temp[j] = '\0'; |
726f6388 | 990 | |
ccc6cda3 JA |
991 | /* Point to after the closing quote. */ |
992 | if (c) | |
993 | i++; | |
726f6388 JA |
994 | *sindex = i; |
995 | ||
ccc6cda3 JA |
996 | return (temp); |
997 | } | |
998 | ||
999 | /* This should really be another option to string_extract_double_quoted. */ | |
f73dda09 | 1000 | static int |
a0c0a00f | 1001 | skip_double_quoted (string, slen, sind, flags) |
ccc6cda3 | 1002 | char *string; |
7117c2d2 | 1003 | size_t slen; |
ccc6cda3 | 1004 | int sind; |
a0c0a00f | 1005 | int flags; |
ccc6cda3 | 1006 | { |
f73dda09 | 1007 | int c, i; |
ccc6cda3 JA |
1008 | char *ret; |
1009 | int pass_next, backquote, si; | |
7117c2d2 | 1010 | DECLARE_MBSTATE; |
ccc6cda3 JA |
1011 | |
1012 | pass_next = backquote = 0; | |
7117c2d2 JA |
1013 | i = sind; |
1014 | while (c = string[i]) | |
726f6388 | 1015 | { |
ccc6cda3 JA |
1016 | if (pass_next) |
1017 | { | |
1018 | pass_next = 0; | |
7117c2d2 | 1019 | ADVANCE_CHAR (string, slen, i); |
ccc6cda3 JA |
1020 | continue; |
1021 | } | |
1022 | else if (c == '\\') | |
1023 | { | |
1024 | pass_next++; | |
7117c2d2 | 1025 | i++; |
ccc6cda3 JA |
1026 | continue; |
1027 | } | |
1028 | else if (backquote) | |
1029 | { | |
1030 | if (c == '`') | |
1031 | backquote = 0; | |
7117c2d2 | 1032 | ADVANCE_CHAR (string, slen, i); |
ccc6cda3 JA |
1033 | continue; |
1034 | } | |
1035 | else if (c == '`') | |
1036 | { | |
1037 | backquote++; | |
7117c2d2 | 1038 | i++; |
ccc6cda3 JA |
1039 | continue; |
1040 | } | |
cce855bc | 1041 | else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE))) |
ccc6cda3 JA |
1042 | { |
1043 | si = i + 2; | |
cce855bc | 1044 | if (string[i + 1] == LPAREN) |
a0c0a00f | 1045 | ret = extract_command_subst (string, &si, SX_NOALLOC|(flags&SX_COMPLETE)); |
ccc6cda3 | 1046 | else |
495aee44 | 1047 | ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, SX_NOALLOC); |
ccc6cda3 | 1048 | |
a0c0a00f CR |
1049 | /* These can consume the entire string if they are unterminated */ |
1050 | CHECK_STRING_OVERRUN (i, si, slen, c); | |
1051 | ||
7117c2d2 | 1052 | i = si + 1; |
ccc6cda3 JA |
1053 | continue; |
1054 | } | |
1055 | else if (c != '"') | |
7117c2d2 JA |
1056 | { |
1057 | ADVANCE_CHAR (string, slen, i); | |
1058 | continue; | |
1059 | } | |
ccc6cda3 JA |
1060 | else |
1061 | break; | |
726f6388 | 1062 | } |
ccc6cda3 JA |
1063 | |
1064 | if (c) | |
1065 | i++; | |
1066 | ||
1067 | return (i); | |
726f6388 JA |
1068 | } |
1069 | ||
ccc6cda3 JA |
1070 | /* Extract the contents of STRING as if it is enclosed in single quotes. |
1071 | SINDEX, when passed in, is the offset of the character immediately | |
1072 | following the opening single quote; on exit, SINDEX is left pointing after | |
1073 | the closing single quote. */ | |
1074 | static inline char * | |
1075 | string_extract_single_quoted (string, sindex) | |
1076 | char *string; | |
1077 | int *sindex; | |
1078 | { | |
f73dda09 | 1079 | register int i; |
7117c2d2 | 1080 | size_t slen; |
ccc6cda3 | 1081 | char *t; |
7117c2d2 | 1082 | DECLARE_MBSTATE; |
ccc6cda3 | 1083 | |
95732b49 JA |
1084 | /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */ |
1085 | slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0; | |
7117c2d2 JA |
1086 | i = *sindex; |
1087 | while (string[i] && string[i] != '\'') | |
1088 | ADVANCE_CHAR (string, slen, i); | |
ccc6cda3 | 1089 | |
bb70624e | 1090 | t = substring (string, *sindex, i); |
ccc6cda3 JA |
1091 | |
1092 | if (string[i]) | |
1093 | i++; | |
1094 | *sindex = i; | |
1095 | ||
1096 | return (t); | |
1097 | } | |
1098 | ||
a0c0a00f CR |
1099 | /* Skip over a single-quoted string. We overload the SX_COMPLETE flag to mean |
1100 | that we are splitting out words for completion and have encountered a $'...' | |
1101 | string, which allows backslash-escaped single quotes. */ | |
ccc6cda3 | 1102 | static inline int |
a0c0a00f | 1103 | skip_single_quoted (string, slen, sind, flags) |
0628567a | 1104 | const char *string; |
7117c2d2 | 1105 | size_t slen; |
ccc6cda3 | 1106 | int sind; |
a0c0a00f | 1107 | int flags; |
ccc6cda3 | 1108 | { |
28ef6c31 | 1109 | register int c; |
7117c2d2 JA |
1110 | DECLARE_MBSTATE; |
1111 | ||
1112 | c = sind; | |
1113 | while (string[c] && string[c] != '\'') | |
a0c0a00f CR |
1114 | { |
1115 | if ((flags & SX_COMPLETE) && string[c] == '\\' && string[c+1] == '\'' && string[c+2]) | |
1116 | ADVANCE_CHAR (string, slen, c); | |
1117 | ADVANCE_CHAR (string, slen, c); | |
1118 | } | |
ccc6cda3 | 1119 | |
28ef6c31 JA |
1120 | if (string[c]) |
1121 | c++; | |
1122 | return c; | |
ccc6cda3 JA |
1123 | } |
1124 | ||
1125 | /* Just like string_extract, but doesn't hack backslashes or any of | |
bb70624e | 1126 | that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */ |
726f6388 | 1127 | static char * |
3185942a | 1128 | string_extract_verbatim (string, slen, sindex, charlist, flags) |
f73dda09 | 1129 | char *string; |
95732b49 | 1130 | size_t slen; |
ccc6cda3 | 1131 | int *sindex; |
f73dda09 | 1132 | char *charlist; |
3185942a | 1133 | int flags; |
ccc6cda3 | 1134 | { |
0001803f | 1135 | register int i; |
95732b49 JA |
1136 | #if defined (HANDLE_MULTIBYTE) |
1137 | size_t clen; | |
1138 | wchar_t *wcharlist; | |
1139 | #endif | |
ccc6cda3 JA |
1140 | int c; |
1141 | char *temp; | |
95732b49 | 1142 | DECLARE_MBSTATE; |
ccc6cda3 | 1143 | |
a0c0a00f | 1144 | if ((flags & SX_NOCTLESC) && charlist[0] == '\'' && charlist[1] == '\0') |
ccc6cda3 JA |
1145 | { |
1146 | temp = string_extract_single_quoted (string, sindex); | |
1147 | --*sindex; /* leave *sindex at separator character */ | |
1148 | return temp; | |
1149 | } | |
1150 | ||
95732b49 JA |
1151 | i = *sindex; |
1152 | #if 0 | |
1153 | /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need | |
1154 | this only if MB_CUR_MAX > 1. */ | |
1155 | slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 1; | |
1156 | #endif | |
1157 | #if defined (HANDLE_MULTIBYTE) | |
1158 | clen = strlen (charlist); | |
1159 | wcharlist = 0; | |
1160 | #endif | |
1161 | while (c = string[i]) | |
ccc6cda3 | 1162 | { |
95732b49 JA |
1163 | #if defined (HANDLE_MULTIBYTE) |
1164 | size_t mblength; | |
1165 | #endif | |
3185942a JA |
1166 | if ((flags & SX_NOCTLESC) == 0 && c == CTLESC) |
1167 | { | |
1168 | i += 2; | |
1169 | continue; | |
1170 | } | |
1171 | /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL | |
1172 | through, to protect the CTLNULs from later calls to | |
1173 | remove_quoted_nulls. */ | |
1174 | else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC && string[i+1] == CTLNUL) | |
ccc6cda3 | 1175 | { |
95732b49 | 1176 | i += 2; |
ccc6cda3 JA |
1177 | continue; |
1178 | } | |
1179 | ||
95732b49 JA |
1180 | #if defined (HANDLE_MULTIBYTE) |
1181 | mblength = MBLEN (string + i, slen - i); | |
1182 | if (mblength > 1) | |
1183 | { | |
1184 | wchar_t wc; | |
1185 | mblength = mbtowc (&wc, string + i, slen - i); | |
1186 | if (MB_INVALIDCH (mblength)) | |
1187 | { | |
1188 | if (MEMBER (c, charlist)) | |
1189 | break; | |
1190 | } | |
1191 | else | |
1192 | { | |
1193 | if (wcharlist == 0) | |
1194 | { | |
1195 | size_t len; | |
1196 | len = mbstowcs (wcharlist, charlist, 0); | |
1197 | if (len == -1) | |
1198 | len = 0; | |
0628567a JA |
1199 | wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1)); |
1200 | mbstowcs (wcharlist, charlist, len + 1); | |
95732b49 JA |
1201 | } |
1202 | ||
1203 | if (wcschr (wcharlist, wc)) | |
1204 | break; | |
1205 | } | |
1206 | } | |
1207 | else | |
1208 | #endif | |
ccc6cda3 JA |
1209 | if (MEMBER (c, charlist)) |
1210 | break; | |
95732b49 JA |
1211 | |
1212 | ADVANCE_CHAR (string, slen, i); | |
ccc6cda3 JA |
1213 | } |
1214 | ||
95732b49 JA |
1215 | #if defined (HANDLE_MULTIBYTE) |
1216 | FREE (wcharlist); | |
1217 | #endif | |
1218 | ||
bb70624e | 1219 | temp = substring (string, *sindex, i); |
ccc6cda3 JA |
1220 | *sindex = i; |
1221 | ||
1222 | return (temp); | |
1223 | } | |
1224 | ||
1225 | /* Extract the $( construct in STRING, and return a new string. | |
1226 | Start extracting at (SINDEX) as if we had just seen "$(". | |
3185942a | 1227 | Make (SINDEX) get the position of the matching ")". ) |
0001803f | 1228 | XFLAGS is additional flags to pass to other extraction functions. */ |
ccc6cda3 | 1229 | char * |
3185942a | 1230 | extract_command_subst (string, sindex, xflags) |
726f6388 JA |
1231 | char *string; |
1232 | int *sindex; | |
3185942a | 1233 | int xflags; |
726f6388 | 1234 | { |
a0c0a00f CR |
1235 | char *ret; |
1236 | ||
1237 | if (string[*sindex] == LPAREN || (xflags & SX_COMPLETE)) | |
3185942a JA |
1238 | return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/ |
1239 | else | |
1240 | { | |
1241 | xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0); | |
a0c0a00f CR |
1242 | ret = xparse_dolparen (string, string+*sindex, sindex, xflags); |
1243 | return ret; | |
3185942a | 1244 | } |
ccc6cda3 JA |
1245 | } |
1246 | ||
28ef6c31 | 1247 | /* Extract the $[ construct in STRING, and return a new string. (]) |
ccc6cda3 JA |
1248 | Start extracting at (SINDEX) as if we had just seen "$[". |
1249 | Make (SINDEX) get the position of the matching "]". */ | |
1250 | char * | |
1251 | extract_arithmetic_subst (string, sindex) | |
1252 | char *string; | |
1253 | int *sindex; | |
1254 | { | |
7117c2d2 | 1255 | return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/ |
ccc6cda3 JA |
1256 | } |
1257 | ||
1258 | #if defined (PROCESS_SUBSTITUTION) | |
1259 | /* Extract the <( or >( construct in STRING, and return a new string. | |
1260 | Start extracting at (SINDEX) as if we had just seen "<(". | |
cce855bc | 1261 | Make (SINDEX) get the position of the matching ")". */ /*))*/ |
ccc6cda3 | 1262 | char * |
85b94814 | 1263 | extract_process_subst (string, starter, sindex, xflags) |
ccc6cda3 JA |
1264 | char *string; |
1265 | char *starter; | |
1266 | int *sindex; | |
85b94814 | 1267 | int xflags; |
ccc6cda3 | 1268 | { |
85b94814 | 1269 | #if 0 |
ac50fbac | 1270 | return (extract_delimited_string (string, sindex, starter, "(", ")", SX_COMMAND)); |
85b94814 CR |
1271 | #else |
1272 | xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0); | |
1273 | return (xparse_dolparen (string, string+*sindex, sindex, xflags)); | |
1274 | #endif | |
ccc6cda3 JA |
1275 | } |
1276 | #endif /* PROCESS_SUBSTITUTION */ | |
1277 | ||
1278 | #if defined (ARRAY_VARS) | |
95732b49 JA |
1279 | /* This can be fooled by unquoted right parens in the passed string. If |
1280 | each caller verifies that the last character in STRING is a right paren, | |
1281 | we don't even need to call extract_delimited_string. */ | |
ccc6cda3 JA |
1282 | char * |
1283 | extract_array_assignment_list (string, sindex) | |
1284 | char *string; | |
1285 | int *sindex; | |
1286 | { | |
95732b49 JA |
1287 | int slen; |
1288 | char *ret; | |
1289 | ||
1290 | slen = strlen (string); /* ( */ | |
1291 | if (string[slen - 1] == ')') | |
1292 | { | |
1293 | ret = substring (string, *sindex, slen - 1); | |
1294 | *sindex = slen - 1; | |
1295 | return ret; | |
1296 | } | |
1297 | return 0; | |
ccc6cda3 JA |
1298 | } |
1299 | #endif | |
1300 | ||
1301 | /* Extract and create a new string from the contents of STRING, a | |
1302 | character string delimited with OPENER and CLOSER. SINDEX is | |
1303 | the address of an int describing the current offset in STRING; | |
1304 | it should point to just after the first OPENER found. On exit, | |
1305 | SINDEX gets the position of the last character of the matching CLOSER. | |
1306 | If OPENER is more than a single character, ALT_OPENER, if non-null, | |
1307 | contains a character string that can also match CLOSER and thus | |
1308 | needs to be skipped. */ | |
1309 | static char * | |
7117c2d2 | 1310 | extract_delimited_string (string, sindex, opener, alt_opener, closer, flags) |
ccc6cda3 JA |
1311 | char *string; |
1312 | int *sindex; | |
1313 | char *opener, *alt_opener, *closer; | |
7117c2d2 | 1314 | int flags; |
ccc6cda3 JA |
1315 | { |
1316 | int i, c, si; | |
7117c2d2 | 1317 | size_t slen; |
ccc6cda3 | 1318 | char *t, *result; |
0628567a | 1319 | int pass_character, nesting_level, in_comment; |
ccc6cda3 | 1320 | int len_closer, len_opener, len_alt_opener; |
7117c2d2 | 1321 | DECLARE_MBSTATE; |
ccc6cda3 | 1322 | |
7117c2d2 | 1323 | slen = strlen (string + *sindex) + *sindex; |
ccc6cda3 JA |
1324 | len_opener = STRLEN (opener); |
1325 | len_alt_opener = STRLEN (alt_opener); | |
1326 | len_closer = STRLEN (closer); | |
726f6388 | 1327 | |
0628567a | 1328 | pass_character = in_comment = 0; |
726f6388 JA |
1329 | |
1330 | nesting_level = 1; | |
ccc6cda3 | 1331 | i = *sindex; |
726f6388 | 1332 | |
ccc6cda3 | 1333 | while (nesting_level) |
726f6388 | 1334 | { |
ccc6cda3 JA |
1335 | c = string[i]; |
1336 | ||
a0c0a00f CR |
1337 | /* If a recursive call or a call to ADVANCE_CHAR leaves the index beyond |
1338 | the end of the string, catch it and cut the loop. */ | |
1339 | if (i > slen) | |
1340 | { | |
1341 | i = slen; | |
1342 | c = string[i = slen]; | |
1343 | break; | |
1344 | } | |
1345 | ||
ccc6cda3 | 1346 | if (c == 0) |
28ef6c31 | 1347 | break; |
ccc6cda3 | 1348 | |
0628567a JA |
1349 | if (in_comment) |
1350 | { | |
1351 | if (c == '\n') | |
1352 | in_comment = 0; | |
1353 | ADVANCE_CHAR (string, slen, i); | |
1354 | continue; | |
1355 | } | |
1356 | ||
ccc6cda3 | 1357 | if (pass_character) /* previous char was backslash */ |
726f6388 JA |
1358 | { |
1359 | pass_character = 0; | |
7117c2d2 | 1360 | ADVANCE_CHAR (string, slen, i); |
726f6388 JA |
1361 | continue; |
1362 | } | |
1363 | ||
0628567a | 1364 | /* Not exactly right yet; should handle shell metacharacters and |
0001803f | 1365 | multibyte characters, too. See COMMENT_BEGIN define in parse.y */ |
3185942a | 1366 | if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1]))) |
0628567a JA |
1367 | { |
1368 | in_comment = 1; | |
1369 | ADVANCE_CHAR (string, slen, i); | |
1370 | continue; | |
1371 | } | |
1372 | ||
7117c2d2 | 1373 | if (c == CTLESC || c == '\\') |
726f6388 | 1374 | { |
ccc6cda3 JA |
1375 | pass_character++; |
1376 | i++; | |
1377 | continue; | |
726f6388 JA |
1378 | } |
1379 | ||
495aee44 CR |
1380 | /* Process a nested command substitution, but only if we're parsing an |
1381 | arithmetic substitution. */ | |
0001803f CR |
1382 | if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN) |
1383 | { | |
1384 | si = i + 2; | |
495aee44 | 1385 | t = extract_command_subst (string, &si, flags|SX_NOALLOC); |
0001803f CR |
1386 | i = si + 1; |
1387 | continue; | |
1388 | } | |
0001803f | 1389 | |
ccc6cda3 JA |
1390 | /* Process a nested OPENER. */ |
1391 | if (STREQN (string + i, opener, len_opener)) | |
726f6388 | 1392 | { |
ccc6cda3 | 1393 | si = i + len_opener; |
3185942a | 1394 | t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC); |
ccc6cda3 | 1395 | i = si + 1; |
ccc6cda3 | 1396 | continue; |
726f6388 JA |
1397 | } |
1398 | ||
ccc6cda3 JA |
1399 | /* Process a nested ALT_OPENER */ |
1400 | if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener)) | |
726f6388 | 1401 | { |
ccc6cda3 | 1402 | si = i + len_alt_opener; |
3185942a | 1403 | t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC); |
ccc6cda3 | 1404 | i = si + 1; |
726f6388 JA |
1405 | continue; |
1406 | } | |
ccc6cda3 JA |
1407 | |
1408 | /* If the current substring terminates the delimited string, decrement | |
1409 | the nesting level. */ | |
1410 | if (STREQN (string + i, closer, len_closer)) | |
726f6388 | 1411 | { |
7117c2d2 | 1412 | i += len_closer - 1; /* move to last byte of the closer */ |
ccc6cda3 JA |
1413 | nesting_level--; |
1414 | if (nesting_level == 0) | |
1415 | break; | |
726f6388 | 1416 | } |
ccc6cda3 JA |
1417 | |
1418 | /* Pass old-style command substitution through verbatim. */ | |
1419 | if (c == '`') | |
28ef6c31 JA |
1420 | { |
1421 | si = i + 1; | |
3185942a | 1422 | t = string_extract (string, &si, "`", flags|SX_NOALLOC); |
28ef6c31 | 1423 | i = si + 1; |
28ef6c31 JA |
1424 | continue; |
1425 | } | |
ccc6cda3 | 1426 | |
7117c2d2 JA |
1427 | /* Pass single-quoted and double-quoted strings through verbatim. */ |
1428 | if (c == '\'' || c == '"') | |
28ef6c31 JA |
1429 | { |
1430 | si = i + 1; | |
a0c0a00f CR |
1431 | i = (c == '\'') ? skip_single_quoted (string, slen, si, 0) |
1432 | : skip_double_quoted (string, slen, si, 0); | |
28ef6c31 JA |
1433 | continue; |
1434 | } | |
ccc6cda3 | 1435 | |
7117c2d2 JA |
1436 | /* move past this character, which was not special. */ |
1437 | ADVANCE_CHAR (string, slen, i); | |
726f6388 JA |
1438 | } |
1439 | ||
b80f6443 | 1440 | if (c == 0 && nesting_level) |
726f6388 | 1441 | { |
b80f6443 JA |
1442 | if (no_longjmp_on_fatal_error == 0) |
1443 | { | |
b80f6443 | 1444 | last_command_exit_value = EXECUTION_FAILURE; |
ac50fbac | 1445 | report_error (_("bad substitution: no closing `%s' in %s"), closer, string); |
b80f6443 JA |
1446 | exp_jump_to_top_level (DISCARD); |
1447 | } | |
1448 | else | |
1449 | { | |
1450 | *sindex = i; | |
1451 | return (char *)NULL; | |
1452 | } | |
726f6388 | 1453 | } |
ccc6cda3 | 1454 | |
cce855bc | 1455 | si = i - *sindex - len_closer + 1; |
3185942a | 1456 | if (flags & SX_NOALLOC) |
7117c2d2 JA |
1457 | result = (char *)NULL; |
1458 | else | |
1459 | { | |
1460 | result = (char *)xmalloc (1 + si); | |
1461 | strncpy (result, string + *sindex, si); | |
1462 | result[si] = '\0'; | |
1463 | } | |
cce855bc JA |
1464 | *sindex = i; |
1465 | ||
726f6388 JA |
1466 | return (result); |
1467 | } | |
1468 | ||
ccc6cda3 JA |
1469 | /* Extract a parameter expansion expression within ${ and } from STRING. |
1470 | Obey the Posix.2 rules for finding the ending `}': count braces while | |
1471 | skipping over enclosed quoted strings and command substitutions. | |
1472 | SINDEX is the address of an int describing the current offset in STRING; | |
1473 | it should point to just after the first `{' found. On exit, SINDEX | |
1474 | gets the position of the matching `}'. QUOTED is non-zero if this | |
1475 | occurs inside double quotes. */ | |
1476 | /* XXX -- this is very similar to extract_delimited_string -- XXX */ | |
726f6388 | 1477 | static char * |
7117c2d2 | 1478 | extract_dollar_brace_string (string, sindex, quoted, flags) |
726f6388 | 1479 | char *string; |
7117c2d2 | 1480 | int *sindex, quoted, flags; |
726f6388 | 1481 | { |
f73dda09 | 1482 | register int i, c; |
7117c2d2 | 1483 | size_t slen; |
495aee44 | 1484 | int pass_character, nesting_level, si, dolbrace_state; |
ccc6cda3 | 1485 | char *result, *t; |
7117c2d2 | 1486 | DECLARE_MBSTATE; |
726f6388 | 1487 | |
ccc6cda3 | 1488 | pass_character = 0; |
ccc6cda3 | 1489 | nesting_level = 1; |
7117c2d2 | 1490 | slen = strlen (string + *sindex) + *sindex; |
ccc6cda3 | 1491 | |
495aee44 | 1492 | /* The handling of dolbrace_state needs to agree with the code in parse.y: |
49ed961b CR |
1493 | parse_matched_pair(). The different initial value is to handle the |
1494 | case where this function is called to parse the word in | |
1495 | ${param op word} (SX_WORD). */ | |
1496 | dolbrace_state = (flags & SX_WORD) ? DOLBRACE_WORD : DOLBRACE_PARAM; | |
1497 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && (flags & SX_POSIXEXP)) | |
1498 | dolbrace_state = DOLBRACE_QUOTE; | |
495aee44 | 1499 | |
7117c2d2 JA |
1500 | i = *sindex; |
1501 | while (c = string[i]) | |
726f6388 | 1502 | { |
ccc6cda3 | 1503 | if (pass_character) |
726f6388 | 1504 | { |
ccc6cda3 | 1505 | pass_character = 0; |
7117c2d2 | 1506 | ADVANCE_CHAR (string, slen, i); |
ccc6cda3 JA |
1507 | continue; |
1508 | } | |
726f6388 | 1509 | |
cce855bc JA |
1510 | /* CTLESCs and backslashes quote the next character. */ |
1511 | if (c == CTLESC || c == '\\') | |
726f6388 | 1512 | { |
ccc6cda3 | 1513 | pass_character++; |
7117c2d2 | 1514 | i++; |
726f6388 JA |
1515 | continue; |
1516 | } | |
1517 | ||
cce855bc | 1518 | if (string[i] == '$' && string[i+1] == LBRACE) |
726f6388 | 1519 | { |
ccc6cda3 | 1520 | nesting_level++; |
7117c2d2 | 1521 | i += 2; |
726f6388 JA |
1522 | continue; |
1523 | } | |
1524 | ||
cce855bc | 1525 | if (c == RBRACE) |
726f6388 | 1526 | { |
ccc6cda3 JA |
1527 | nesting_level--; |
1528 | if (nesting_level == 0) | |
1529 | break; | |
7117c2d2 | 1530 | i++; |
726f6388 JA |
1531 | continue; |
1532 | } | |
1533 | ||
ccc6cda3 JA |
1534 | /* Pass the contents of old-style command substitutions through |
1535 | verbatim. */ | |
1536 | if (c == '`') | |
726f6388 | 1537 | { |
ccc6cda3 | 1538 | si = i + 1; |
3185942a | 1539 | t = string_extract (string, &si, "`", flags|SX_NOALLOC); |
a0c0a00f CR |
1540 | |
1541 | CHECK_STRING_OVERRUN (i, si, slen, c); | |
1542 | ||
7117c2d2 | 1543 | i = si + 1; |
ccc6cda3 JA |
1544 | continue; |
1545 | } | |
726f6388 | 1546 | |
cce855bc JA |
1547 | /* Pass the contents of new-style command substitutions and |
1548 | arithmetic substitutions through verbatim. */ | |
1549 | if (string[i] == '$' && string[i+1] == LPAREN) | |
ccc6cda3 | 1550 | { |
726f6388 | 1551 | si = i + 2; |
3185942a | 1552 | t = extract_command_subst (string, &si, flags|SX_NOALLOC); |
7117c2d2 | 1553 | i = si + 1; |
726f6388 JA |
1554 | continue; |
1555 | } | |
1556 | ||
495aee44 CR |
1557 | /* Pass the contents of double-quoted strings through verbatim. */ |
1558 | if (c == '"') | |
1559 | { | |
1560 | si = i + 1; | |
a0c0a00f | 1561 | i = skip_double_quoted (string, slen, si, 0); |
495aee44 CR |
1562 | /* skip_XXX_quoted leaves index one past close quote */ |
1563 | continue; | |
1564 | } | |
1565 | ||
1566 | if (c == '\'') | |
1567 | { | |
1568 | /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/ | |
ac50fbac | 1569 | if (posixly_correct && shell_compatibility_level > 42 && dolbrace_state != DOLBRACE_QUOTE && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
495aee44 CR |
1570 | ADVANCE_CHAR (string, slen, i); |
1571 | else | |
1572 | { | |
1573 | si = i + 1; | |
a0c0a00f | 1574 | i = skip_single_quoted (string, slen, si, 0); |
495aee44 CR |
1575 | } |
1576 | ||
1577 | continue; | |
1578 | } | |
7117c2d2 JA |
1579 | |
1580 | /* move past this character, which was not special. */ | |
1581 | ADVANCE_CHAR (string, slen, i); | |
495aee44 CR |
1582 | |
1583 | /* This logic must agree with parse.y:parse_matched_pair, since they | |
1584 | share the same defines. */ | |
1585 | if (dolbrace_state == DOLBRACE_PARAM && c == '%' && (i - *sindex) > 1) | |
1586 | dolbrace_state = DOLBRACE_QUOTE; | |
1587 | else if (dolbrace_state == DOLBRACE_PARAM && c == '#' && (i - *sindex) > 1) | |
1588 | dolbrace_state = DOLBRACE_QUOTE; | |
1589 | else if (dolbrace_state == DOLBRACE_PARAM && c == '/' && (i - *sindex) > 1) | |
ac50fbac | 1590 | dolbrace_state = DOLBRACE_QUOTE2; /* XXX */ |
495aee44 CR |
1591 | else if (dolbrace_state == DOLBRACE_PARAM && c == '^' && (i - *sindex) > 1) |
1592 | dolbrace_state = DOLBRACE_QUOTE; | |
1593 | else if (dolbrace_state == DOLBRACE_PARAM && c == ',' && (i - *sindex) > 1) | |
1594 | dolbrace_state = DOLBRACE_QUOTE; | |
a0c0a00f CR |
1595 | /* This is intended to handle all of the [:]op expansions and the substring/ |
1596 | length/pattern removal/pattern substitution expansions. */ | |
495aee44 CR |
1597 | else if (dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", c) != 0) |
1598 | dolbrace_state = DOLBRACE_OP; | |
1599 | else if (dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", c) == 0) | |
1600 | dolbrace_state = DOLBRACE_WORD; | |
cce855bc | 1601 | } |
726f6388 | 1602 | |
b80f6443 | 1603 | if (c == 0 && nesting_level) |
cce855bc | 1604 | { |
b80f6443 JA |
1605 | if (no_longjmp_on_fatal_error == 0) |
1606 | { /* { */ | |
b80f6443 | 1607 | last_command_exit_value = EXECUTION_FAILURE; |
ac50fbac | 1608 | report_error (_("bad substitution: no closing `%s' in %s"), "}", string); |
b80f6443 JA |
1609 | exp_jump_to_top_level (DISCARD); |
1610 | } | |
1611 | else | |
1612 | { | |
1613 | *sindex = i; | |
1614 | return ((char *)NULL); | |
1615 | } | |
726f6388 | 1616 | } |
726f6388 | 1617 | |
3185942a | 1618 | result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i); |
726f6388 JA |
1619 | *sindex = i; |
1620 | ||
ccc6cda3 | 1621 | return (result); |
726f6388 JA |
1622 | } |
1623 | ||
ccc6cda3 JA |
1624 | /* Remove backslashes which are quoting backquotes from STRING. Modifies |
1625 | STRING, and returns a pointer to it. */ | |
1626 | char * | |
1627 | de_backslash (string) | |
726f6388 | 1628 | char *string; |
ccc6cda3 | 1629 | { |
7117c2d2 JA |
1630 | register size_t slen; |
1631 | register int i, j, prev_i; | |
1632 | DECLARE_MBSTATE; | |
726f6388 | 1633 | |
7117c2d2 JA |
1634 | slen = strlen (string); |
1635 | i = j = 0; | |
1636 | ||
1637 | /* Loop copying string[i] to string[j], i >= j. */ | |
1638 | while (i < slen) | |
1639 | { | |
1640 | if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' || | |
ccc6cda3 | 1641 | string[i + 1] == '$')) |
7117c2d2 JA |
1642 | i++; |
1643 | prev_i = i; | |
1644 | ADVANCE_CHAR (string, slen, i); | |
1645 | if (j < prev_i) | |
b80f6443 | 1646 | do string[j++] = string[prev_i++]; while (prev_i < i); |
7117c2d2 | 1647 | else |
b80f6443 | 1648 | j = i; |
7117c2d2 JA |
1649 | } |
1650 | string[j] = '\0'; | |
1651 | ||
ccc6cda3 JA |
1652 | return (string); |
1653 | } | |
726f6388 | 1654 | |
ccc6cda3 | 1655 | #if 0 |
cce855bc | 1656 | /*UNUSED*/ |
ccc6cda3 JA |
1657 | /* Replace instances of \! in a string with !. */ |
1658 | void | |
1659 | unquote_bang (string) | |
1660 | char *string; | |
1661 | { | |
1662 | register int i, j; | |
1663 | register char *temp; | |
726f6388 | 1664 | |
f73dda09 | 1665 | temp = (char *)xmalloc (1 + strlen (string)); |
726f6388 | 1666 | |
ccc6cda3 JA |
1667 | for (i = 0, j = 0; (temp[j] = string[i]); i++, j++) |
1668 | { | |
1669 | if (string[i] == '\\' && string[i + 1] == '!') | |
1670 | { | |
1671 | temp[j] = '!'; | |
1672 | i++; | |
1673 | } | |
1674 | } | |
1675 | strcpy (string, temp); | |
1676 | free (temp); | |
726f6388 | 1677 | } |
ccc6cda3 | 1678 | #endif |
726f6388 | 1679 | |
a0c0a00f | 1680 | #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = oldjmp; return (x); } while (0) |
3185942a | 1681 | |
89a92869 | 1682 | /* This function assumes s[i] == open; returns with s[ret] == close; used to |
0001803f CR |
1683 | parse array subscripts. FLAGS & 1 means to not attempt to skip over |
1684 | matched pairs of quotes or backquotes, or skip word expansions; it is | |
1685 | intended to be used after expansion has been performed and during final | |
1686 | assignment parsing (see arrayfunc.c:assign_compound_array_list()). */ | |
89a92869 CR |
1687 | static int |
1688 | skip_matched_pair (string, start, open, close, flags) | |
1689 | const char *string; | |
1690 | int start, open, close, flags; | |
1691 | { | |
a0c0a00f | 1692 | int i, pass_next, backq, si, c, count, oldjmp; |
89a92869 CR |
1693 | size_t slen; |
1694 | char *temp, *ss; | |
1695 | DECLARE_MBSTATE; | |
1696 | ||
1697 | slen = strlen (string + start) + start; | |
a0c0a00f | 1698 | oldjmp = no_longjmp_on_fatal_error; |
89a92869 CR |
1699 | no_longjmp_on_fatal_error = 1; |
1700 | ||
1701 | i = start + 1; /* skip over leading bracket */ | |
1702 | count = 1; | |
1703 | pass_next = backq = 0; | |
1704 | ss = (char *)string; | |
1705 | while (c = string[i]) | |
1706 | { | |
1707 | if (pass_next) | |
1708 | { | |
1709 | pass_next = 0; | |
1710 | if (c == 0) | |
1711 | CQ_RETURN(i); | |
1712 | ADVANCE_CHAR (string, slen, i); | |
1713 | continue; | |
1714 | } | |
1715 | else if (c == '\\') | |
1716 | { | |
1717 | pass_next = 1; | |
1718 | i++; | |
1719 | continue; | |
1720 | } | |
1721 | else if (backq) | |
1722 | { | |
1723 | if (c == '`') | |
1724 | backq = 0; | |
1725 | ADVANCE_CHAR (string, slen, i); | |
1726 | continue; | |
1727 | } | |
0001803f | 1728 | else if ((flags & 1) == 0 && c == '`') |
89a92869 CR |
1729 | { |
1730 | backq = 1; | |
1731 | i++; | |
1732 | continue; | |
1733 | } | |
0001803f | 1734 | else if ((flags & 1) == 0 && c == open) |
89a92869 CR |
1735 | { |
1736 | count++; | |
1737 | i++; | |
1738 | continue; | |
1739 | } | |
1740 | else if (c == close) | |
1741 | { | |
1742 | count--; | |
1743 | if (count == 0) | |
1744 | break; | |
1745 | i++; | |
1746 | continue; | |
1747 | } | |
0001803f | 1748 | else if ((flags & 1) == 0 && (c == '\'' || c == '"')) |
89a92869 | 1749 | { |
a0c0a00f CR |
1750 | i = (c == '\'') ? skip_single_quoted (ss, slen, ++i, 0) |
1751 | : skip_double_quoted (ss, slen, ++i, 0); | |
89a92869 CR |
1752 | /* no increment, the skip functions increment past the closing quote. */ |
1753 | } | |
0001803f | 1754 | else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE)) |
89a92869 CR |
1755 | { |
1756 | si = i + 2; | |
1757 | if (string[si] == '\0') | |
1758 | CQ_RETURN(si); | |
1759 | ||
1760 | if (string[i+1] == LPAREN) | |
1761 | temp = extract_delimited_string (ss, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */ | |
1762 | else | |
1763 | temp = extract_dollar_brace_string (ss, &si, 0, SX_NOALLOC); | |
a0c0a00f CR |
1764 | |
1765 | CHECK_STRING_OVERRUN (i, si, slen, c); | |
1766 | ||
89a92869 CR |
1767 | i = si; |
1768 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1769 | break; | |
1770 | i++; | |
1771 | continue; | |
1772 | } | |
1773 | else | |
1774 | ADVANCE_CHAR (string, slen, i); | |
1775 | } | |
1776 | ||
1777 | CQ_RETURN(i); | |
1778 | } | |
1779 | ||
1780 | #if defined (ARRAY_VARS) | |
1781 | int | |
0001803f | 1782 | skipsubscript (string, start, flags) |
89a92869 | 1783 | const char *string; |
0001803f | 1784 | int start, flags; |
89a92869 | 1785 | { |
0001803f | 1786 | return (skip_matched_pair (string, start, '[', ']', flags)); |
89a92869 CR |
1787 | } |
1788 | #endif | |
1789 | ||
3185942a JA |
1790 | /* Skip characters in STRING until we find a character in DELIMS, and return |
1791 | the index of that character. START is the index into string at which we | |
1792 | begin. This is similar in spirit to strpbrk, but it returns an index into | |
1793 | STRING and takes a starting index. This little piece of code knows quite | |
1794 | a lot of shell syntax. It's very similar to skip_double_quoted and other | |
1795 | functions of that ilk. */ | |
1796 | int | |
1797 | skip_to_delim (string, start, delims, flags) | |
1798 | char *string; | |
1799 | int start; | |
1800 | char *delims; | |
1801 | int flags; | |
1802 | { | |
a0c0a00f CR |
1803 | int i, pass_next, backq, dquote, si, c, oldjmp; |
1804 | int invert, skipquote, skipcmd, noprocsub, completeflag; | |
1805 | int arithexp, skipcol; | |
3185942a | 1806 | size_t slen; |
495aee44 | 1807 | char *temp, open[3]; |
3185942a JA |
1808 | DECLARE_MBSTATE; |
1809 | ||
1810 | slen = strlen (string + start) + start; | |
a0c0a00f | 1811 | oldjmp = no_longjmp_on_fatal_error; |
3185942a JA |
1812 | if (flags & SD_NOJMP) |
1813 | no_longjmp_on_fatal_error = 1; | |
1814 | invert = (flags & SD_INVERT); | |
0001803f | 1815 | skipcmd = (flags & SD_NOSKIPCMD) == 0; |
a0c0a00f CR |
1816 | noprocsub = (flags & SD_NOPROCSUB); |
1817 | completeflag = (flags & SD_COMPLETE) ? SX_COMPLETE : 0; | |
1818 | ||
1819 | arithexp = (flags & SD_ARITHEXP); | |
1820 | skipcol = 0; | |
3185942a JA |
1821 | |
1822 | i = start; | |
a0c0a00f | 1823 | pass_next = backq = dquote = 0; |
3185942a JA |
1824 | while (c = string[i]) |
1825 | { | |
0001803f CR |
1826 | /* If this is non-zero, we should not let quote characters be delimiters |
1827 | and the current character is a single or double quote. We should not | |
1828 | test whether or not it's a delimiter until after we skip single- or | |
1829 | double-quoted strings. */ | |
1830 | skipquote = ((flags & SD_NOQUOTEDELIM) && (c == '\'' || c =='"')); | |
3185942a JA |
1831 | if (pass_next) |
1832 | { | |
1833 | pass_next = 0; | |
1834 | if (c == 0) | |
1835 | CQ_RETURN(i); | |
1836 | ADVANCE_CHAR (string, slen, i); | |
1837 | continue; | |
1838 | } | |
1839 | else if (c == '\\') | |
1840 | { | |
1841 | pass_next = 1; | |
1842 | i++; | |
1843 | continue; | |
1844 | } | |
1845 | else if (backq) | |
1846 | { | |
1847 | if (c == '`') | |
1848 | backq = 0; | |
1849 | ADVANCE_CHAR (string, slen, i); | |
1850 | continue; | |
1851 | } | |
1852 | else if (c == '`') | |
1853 | { | |
1854 | backq = 1; | |
1855 | i++; | |
1856 | continue; | |
1857 | } | |
a0c0a00f | 1858 | else if (arithexp && skipcol && c == ':') |
3185942a | 1859 | { |
a0c0a00f CR |
1860 | skipcol--; |
1861 | i++; | |
1862 | continue; | |
3185942a | 1863 | } |
a0c0a00f CR |
1864 | else if (arithexp && c == '?') |
1865 | { | |
1866 | skipcol++; | |
1867 | i++; | |
1868 | continue; | |
1869 | } | |
1870 | else if (skipquote == 0 && invert == 0 && member (c, delims)) | |
1871 | break; | |
1872 | /* the usual case is to use skip_xxx_quoted, but we don't skip over double | |
1873 | quoted strings when looking for the history expansion character as a | |
1874 | delimiter. */ | |
1875 | /* special case for programmable completion which takes place before | |
1876 | parser converts backslash-escaped single quotes between $'...' to | |
1877 | `regular' single-quoted strings. */ | |
1878 | else if (completeflag && i > 0 && string[i-1] == '$' && c == '\'') | |
1879 | i = skip_single_quoted (string, slen, ++i, SX_COMPLETE); | |
1880 | else if (c == '\'') | |
1881 | i = skip_single_quoted (string, slen, ++i, 0); | |
1882 | else if (c == '"') | |
1883 | i = skip_double_quoted (string, slen, ++i, completeflag); | |
1884 | else if (c == LPAREN && arithexp) | |
1885 | { | |
1886 | si = i + 1; | |
1887 | if (string[si] == '\0') | |
1888 | CQ_RETURN(si); | |
1889 | ||
1890 | temp = extract_delimited_string (string, &si, "(", "(", ")", SX_NOALLOC); /* ) */ | |
1891 | i = si; | |
1892 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1893 | break; | |
1894 | i++; | |
1895 | continue; | |
1896 | } | |
0001803f | 1897 | else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE)) |
3185942a JA |
1898 | { |
1899 | si = i + 2; | |
1900 | if (string[si] == '\0') | |
1901 | CQ_RETURN(si); | |
1902 | ||
1903 | if (string[i+1] == LPAREN) | |
1904 | temp = extract_delimited_string (string, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */ | |
1905 | else | |
1906 | temp = extract_dollar_brace_string (string, &si, 0, SX_NOALLOC); | |
1907 | i = si; | |
1908 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1909 | break; | |
1910 | i++; | |
1911 | continue; | |
1912 | } | |
0001803f | 1913 | #if defined (PROCESS_SUBSTITUTION) |
a0c0a00f | 1914 | else if (skipcmd && noprocsub == 0 && (c == '<' || c == '>') && string[i+1] == LPAREN) |
0001803f CR |
1915 | { |
1916 | si = i + 2; | |
1917 | if (string[si] == '\0') | |
1918 | CQ_RETURN(si); | |
a0c0a00f CR |
1919 | #if 1 |
1920 | temp = extract_delimited_string (string, &si, (c == '<') ? "<(" : ">(", "(", ")", SX_COMMAND|SX_NOALLOC); /* )) */ | |
1921 | #else | |
85b94814 | 1922 | temp = extract_process_subst (string, (c == '<') ? "<(" : ">(", &si, 0); |
a0c0a00f CR |
1923 | free (temp); /* XXX - not using SX_NOALLOC here yet */ |
1924 | #endif | |
0001803f CR |
1925 | i = si; |
1926 | if (string[i] == '\0') | |
1927 | break; | |
1928 | i++; | |
1929 | continue; | |
1930 | } | |
1931 | #endif /* PROCESS_SUBSTITUTION */ | |
495aee44 CR |
1932 | #if defined (EXTENDED_GLOB) |
1933 | else if ((flags & SD_EXTGLOB) && extended_glob && string[i+1] == LPAREN && member (c, "?*+!@")) | |
1934 | { | |
1935 | si = i + 2; | |
1936 | if (string[si] == '\0') | |
1937 | CQ_RETURN(si); | |
1938 | ||
1939 | open[0] = c; | |
1940 | open[1] = LPAREN; | |
1941 | open[2] = '\0'; | |
1942 | temp = extract_delimited_string (string, &si, open, "(", ")", SX_NOALLOC); /* ) */ | |
1943 | ||
1944 | i = si; | |
1945 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1946 | break; | |
1947 | i++; | |
1948 | continue; | |
1949 | } | |
1950 | #endif | |
ac50fbac CR |
1951 | else if ((flags & SD_GLOB) && c == LBRACK) |
1952 | { | |
1953 | si = i + 1; | |
1954 | if (string[si] == '\0') | |
1955 | CQ_RETURN(si); | |
1956 | ||
1957 | temp = extract_delimited_string (string, &si, "[", "[", "]", SX_NOALLOC); /* ] */ | |
1958 | ||
1959 | i = si; | |
1960 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1961 | break; | |
1962 | i++; | |
1963 | continue; | |
1964 | } | |
0001803f | 1965 | else if ((skipquote || invert) && (member (c, delims) == 0)) |
3185942a JA |
1966 | break; |
1967 | else | |
1968 | ADVANCE_CHAR (string, slen, i); | |
1969 | } | |
1970 | ||
1971 | CQ_RETURN(i); | |
1972 | } | |
1973 | ||
a0c0a00f CR |
1974 | #if defined (BANG_HISTORY) |
1975 | /* Skip to the history expansion character (delims[0]), paying attention to | |
1976 | quoted strings and command and process substitution. This is a stripped- | |
1977 | down version of skip_to_delims. The essential difference is that this | |
1978 | resets the quoting state when starting a command substitution */ | |
1979 | int | |
1980 | skip_to_histexp (string, start, delims, flags) | |
1981 | char *string; | |
1982 | int start; | |
1983 | char *delims; | |
1984 | int flags; | |
1985 | { | |
1986 | int i, pass_next, backq, dquote, si, c, oldjmp; | |
1987 | int histexp_comsub, histexp_backq, old_dquote; | |
1988 | size_t slen; | |
1989 | char *temp, open[3]; | |
1990 | DECLARE_MBSTATE; | |
1991 | ||
1992 | slen = strlen (string + start) + start; | |
1993 | oldjmp = no_longjmp_on_fatal_error; | |
1994 | if (flags & SD_NOJMP) | |
1995 | no_longjmp_on_fatal_error = 1; | |
1996 | ||
1997 | histexp_comsub = histexp_backq = old_dquote = 0; | |
1998 | ||
1999 | i = start; | |
2000 | pass_next = backq = dquote = 0; | |
2001 | while (c = string[i]) | |
2002 | { | |
2003 | if (pass_next) | |
2004 | { | |
2005 | pass_next = 0; | |
2006 | if (c == 0) | |
2007 | CQ_RETURN(i); | |
2008 | ADVANCE_CHAR (string, slen, i); | |
2009 | continue; | |
2010 | } | |
2011 | else if (c == '\\') | |
2012 | { | |
2013 | pass_next = 1; | |
2014 | i++; | |
2015 | continue; | |
2016 | } | |
2017 | else if (backq && c == '`') | |
2018 | { | |
2019 | backq = 0; | |
2020 | histexp_backq--; | |
2021 | dquote = old_dquote; | |
2022 | i++; | |
2023 | continue; | |
2024 | } | |
2025 | else if (c == '`') | |
2026 | { | |
2027 | backq = 1; | |
2028 | histexp_backq++; | |
2029 | old_dquote = dquote; /* simple - one level for now */ | |
2030 | dquote = 0; | |
2031 | i++; | |
2032 | continue; | |
2033 | } | |
2034 | /* When in double quotes, act as if the double quote is a member of | |
2035 | history_no_expand_chars, like the history library does */ | |
2036 | else if (dquote && c == delims[0] && string[i+1] == '"') | |
2037 | { | |
2038 | i++; | |
2039 | continue; | |
2040 | } | |
2041 | else if (c == delims[0]) | |
2042 | break; | |
2043 | /* the usual case is to use skip_xxx_quoted, but we don't skip over double | |
2044 | quoted strings when looking for the history expansion character as a | |
2045 | delimiter. */ | |
2046 | else if (dquote && c == '\'') | |
2047 | { | |
2048 | i++; | |
2049 | continue; | |
2050 | } | |
2051 | else if (c == '\'') | |
2052 | i = skip_single_quoted (string, slen, ++i, 0); | |
2053 | /* The posixly_correct test makes posix-mode shells allow double quotes | |
2054 | to quote the history expansion character */ | |
2055 | else if (posixly_correct == 0 && c == '"') | |
2056 | { | |
2057 | dquote = 1 - dquote; | |
2058 | i++; | |
2059 | continue; | |
2060 | } | |
2061 | else if (c == '"') | |
2062 | i = skip_double_quoted (string, slen, ++i, 0); | |
2063 | #if defined (PROCESS_SUBSTITUTION) | |
2064 | else if ((c == '$' || c == '<' || c == '>') && string[i+1] == LPAREN && string[i+2] != LPAREN) | |
2065 | #else | |
2066 | else if (c == '$' && string[i+1] == LPAREN && string[i+2] != LPAREN) | |
2067 | #endif | |
2068 | { | |
2069 | if (string[i+2] == '\0') | |
2070 | CQ_RETURN(i+2); | |
2071 | i += 2; | |
2072 | histexp_comsub++; | |
2073 | old_dquote = dquote; | |
2074 | dquote = 0; | |
2075 | } | |
2076 | else if (histexp_comsub && c == RPAREN) | |
2077 | { | |
2078 | histexp_comsub--; | |
2079 | dquote = old_dquote; | |
2080 | i++; | |
2081 | continue; | |
2082 | } | |
2083 | else if (backq) /* placeholder */ | |
2084 | { | |
2085 | ADVANCE_CHAR (string, slen, i); | |
2086 | continue; | |
2087 | } | |
2088 | else | |
2089 | ADVANCE_CHAR (string, slen, i); | |
2090 | } | |
2091 | ||
2092 | CQ_RETURN(i); | |
2093 | } | |
2094 | #endif /* BANG_HISTORY */ | |
2095 | ||
ccc6cda3 | 2096 | #if defined (READLINE) |
726f6388 JA |
2097 | /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is |
2098 | an unclosed quoted string), or if the character at EINDEX is quoted | |
28ef6c31 | 2099 | by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various |
b72432fd | 2100 | single and double-quoted string parsing functions should not return an |
7117c2d2 JA |
2101 | error if there are unclosed quotes or braces. The characters that this |
2102 | recognizes need to be the same as the contents of | |
2103 | rl_completer_quote_characters. */ | |
b72432fd | 2104 | |
726f6388 JA |
2105 | int |
2106 | char_is_quoted (string, eindex) | |
2107 | char *string; | |
2108 | int eindex; | |
2109 | { | |
a0c0a00f | 2110 | int i, pass_next, c, oldjmp; |
7117c2d2 JA |
2111 | size_t slen; |
2112 | DECLARE_MBSTATE; | |
726f6388 | 2113 | |
7117c2d2 | 2114 | slen = strlen (string); |
a0c0a00f | 2115 | oldjmp = no_longjmp_on_fatal_error; |
28ef6c31 | 2116 | no_longjmp_on_fatal_error = 1; |
7117c2d2 JA |
2117 | i = pass_next = 0; |
2118 | while (i <= eindex) | |
726f6388 | 2119 | { |
7117c2d2 JA |
2120 | c = string[i]; |
2121 | ||
726f6388 JA |
2122 | if (pass_next) |
2123 | { | |
2124 | pass_next = 0; | |
2125 | if (i >= eindex) /* XXX was if (i >= eindex - 1) */ | |
b72432fd | 2126 | CQ_RETURN(1); |
7117c2d2 | 2127 | ADVANCE_CHAR (string, slen, i); |
726f6388 JA |
2128 | continue; |
2129 | } | |
7117c2d2 | 2130 | else if (c == '\\') |
ccc6cda3 JA |
2131 | { |
2132 | pass_next = 1; | |
7117c2d2 | 2133 | i++; |
ccc6cda3 JA |
2134 | continue; |
2135 | } | |
a0c0a00f CR |
2136 | else if (c == '$' && string[i+1] == '\'' && string[i+2]) |
2137 | { | |
2138 | i += 2; | |
2139 | i = skip_single_quoted (string, slen, i, SX_COMPLETE); | |
2140 | if (i > eindex) | |
2141 | CQ_RETURN (i); | |
2142 | } | |
7117c2d2 JA |
2143 | else if (c == '\'' || c == '"') |
2144 | { | |
a0c0a00f CR |
2145 | i = (c == '\'') ? skip_single_quoted (string, slen, ++i, 0) |
2146 | : skip_double_quoted (string, slen, ++i, SX_COMPLETE); | |
7117c2d2 JA |
2147 | if (i > eindex) |
2148 | CQ_RETURN(1); | |
2149 | /* no increment, the skip_xxx functions go one past end */ | |
2150 | } | |
2151 | else | |
2152 | ADVANCE_CHAR (string, slen, i); | |
726f6388 | 2153 | } |
7117c2d2 | 2154 | |
b72432fd | 2155 | CQ_RETURN(0); |
726f6388 JA |
2156 | } |
2157 | ||
726f6388 JA |
2158 | int |
2159 | unclosed_pair (string, eindex, openstr) | |
2160 | char *string; | |
2161 | int eindex; | |
2162 | char *openstr; | |
2163 | { | |
ccc6cda3 | 2164 | int i, pass_next, openc, olen; |
7117c2d2 JA |
2165 | size_t slen; |
2166 | DECLARE_MBSTATE; | |
726f6388 | 2167 | |
7117c2d2 | 2168 | slen = strlen (string); |
726f6388 | 2169 | olen = strlen (openstr); |
7117c2d2 JA |
2170 | i = pass_next = openc = 0; |
2171 | while (i <= eindex) | |
726f6388 JA |
2172 | { |
2173 | if (pass_next) | |
2174 | { | |
2175 | pass_next = 0; | |
2176 | if (i >= eindex) /* XXX was if (i >= eindex - 1) */ | |
2177 | return 0; | |
7117c2d2 JA |
2178 | ADVANCE_CHAR (string, slen, i); |
2179 | continue; | |
2180 | } | |
2181 | else if (string[i] == '\\') | |
2182 | { | |
2183 | pass_next = 1; | |
2184 | i++; | |
726f6388 JA |
2185 | continue; |
2186 | } | |
2187 | else if (STREQN (string + i, openstr, olen)) | |
2188 | { | |
2189 | openc = 1 - openc; | |
7117c2d2 | 2190 | i += olen; |
726f6388 | 2191 | } |
a0c0a00f | 2192 | /* XXX - may want to handle $'...' specially here */ |
ccc6cda3 | 2193 | else if (string[i] == '\'' || string[i] == '"') |
726f6388 | 2194 | { |
a0c0a00f CR |
2195 | i = (string[i] == '\'') ? skip_single_quoted (string, slen, i, 0) |
2196 | : skip_double_quoted (string, slen, i, SX_COMPLETE); | |
726f6388 JA |
2197 | if (i > eindex) |
2198 | return 0; | |
2199 | } | |
7117c2d2 JA |
2200 | else |
2201 | ADVANCE_CHAR (string, slen, i); | |
726f6388 JA |
2202 | } |
2203 | return (openc); | |
2204 | } | |
bb70624e | 2205 | |
bb70624e JA |
2206 | /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the |
2207 | individual words. If DELIMS is NULL, the current value of $IFS is used | |
b80f6443 JA |
2208 | to split the string, and the function follows the shell field splitting |
2209 | rules. SENTINEL is an index to look for. NWP, if non-NULL, | |
bb70624e JA |
2210 | gets the number of words in the returned list. CWP, if non-NULL, gets |
2211 | the index of the word containing SENTINEL. Non-whitespace chars in | |
2212 | DELIMS delimit separate fields. */ | |
2213 | WORD_LIST * | |
0001803f | 2214 | split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp) |
bb70624e JA |
2215 | char *string; |
2216 | int slen; | |
2217 | char *delims; | |
0001803f | 2218 | int sentinel, flags; |
bb70624e JA |
2219 | int *nwp, *cwp; |
2220 | { | |
0001803f | 2221 | int ts, te, i, nw, cw, ifs_split, dflags; |
f73dda09 | 2222 | char *token, *d, *d2; |
bb70624e JA |
2223 | WORD_LIST *ret, *tl; |
2224 | ||
2225 | if (string == 0 || *string == '\0') | |
2226 | { | |
2227 | if (nwp) | |
2228 | *nwp = 0; | |
2229 | if (cwp) | |
2230 | *cwp = 0; | |
2231 | return ((WORD_LIST *)NULL); | |
2232 | } | |
2233 | ||
7117c2d2 | 2234 | d = (delims == 0) ? ifs_value : delims; |
b80f6443 | 2235 | ifs_split = delims == 0; |
bb70624e JA |
2236 | |
2237 | /* Make d2 the non-whitespace characters in delims */ | |
2238 | d2 = 0; | |
2239 | if (delims) | |
2240 | { | |
95732b49 JA |
2241 | size_t slength; |
2242 | #if defined (HANDLE_MULTIBYTE) | |
2243 | size_t mblength = 1; | |
2244 | #endif | |
2245 | DECLARE_MBSTATE; | |
2246 | ||
2247 | slength = strlen (delims); | |
2248 | d2 = (char *)xmalloc (slength + 1); | |
2249 | i = ts = 0; | |
2250 | while (delims[i]) | |
bb70624e | 2251 | { |
95732b49 | 2252 | #if defined (HANDLE_MULTIBYTE) |
0628567a JA |
2253 | mbstate_t state_bak; |
2254 | state_bak = state; | |
95732b49 JA |
2255 | mblength = MBRLEN (delims + i, slength, &state); |
2256 | if (MB_INVALIDCH (mblength)) | |
2257 | state = state_bak; | |
2258 | else if (mblength > 1) | |
2259 | { | |
2260 | memcpy (d2 + ts, delims + i, mblength); | |
2261 | ts += mblength; | |
2262 | i += mblength; | |
2263 | slength -= mblength; | |
2264 | continue; | |
2265 | } | |
2266 | #endif | |
2267 | if (whitespace (delims[i]) == 0) | |
bb70624e | 2268 | d2[ts++] = delims[i]; |
95732b49 JA |
2269 | |
2270 | i++; | |
2271 | slength--; | |
bb70624e JA |
2272 | } |
2273 | d2[ts] = '\0'; | |
2274 | } | |
2275 | ||
2276 | ret = (WORD_LIST *)NULL; | |
2277 | ||
0001803f | 2278 | /* Remove sequences of whitespace characters at the start of the string, as |
b80f6443 JA |
2279 | long as those characters are delimiters. */ |
2280 | for (i = 0; member (string[i], d) && spctabnl (string[i]); i++) | |
bb70624e JA |
2281 | ; |
2282 | if (string[i] == '\0') | |
a0c0a00f CR |
2283 | { |
2284 | FREE (d2); | |
2285 | return (ret); | |
2286 | } | |
bb70624e JA |
2287 | |
2288 | ts = i; | |
2289 | nw = 0; | |
2290 | cw = -1; | |
0001803f | 2291 | dflags = flags|SD_NOJMP; |
bb70624e JA |
2292 | while (1) |
2293 | { | |
0001803f | 2294 | te = skip_to_delim (string, ts, d, dflags); |
bb70624e JA |
2295 | |
2296 | /* If we have a non-whitespace delimiter character, use it to make a | |
2297 | separate field. This is just about what $IFS splitting does and | |
2298 | is closer to the behavior of the shell parser. */ | |
28ef6c31 | 2299 | if (ts == te && d2 && member (string[ts], d2)) |
bb70624e JA |
2300 | { |
2301 | te = ts + 1; | |
b80f6443 JA |
2302 | /* If we're using IFS splitting, the non-whitespace delimiter char |
2303 | and any additional IFS whitespace delimits a field. */ | |
2304 | if (ifs_split) | |
2305 | while (member (string[te], d) && spctabnl (string[te])) | |
2306 | te++; | |
2307 | else | |
2308 | while (member (string[te], d2)) | |
2309 | te++; | |
bb70624e JA |
2310 | } |
2311 | ||
2312 | token = substring (string, ts, te); | |
2313 | ||
2314 | ret = add_string_to_list (token, ret); | |
2315 | free (token); | |
2316 | nw++; | |
2317 | ||
2318 | if (sentinel >= ts && sentinel <= te) | |
2319 | cw = nw; | |
2320 | ||
2321 | /* If the cursor is at whitespace just before word start, set the | |
28ef6c31 | 2322 | sentinel word to the current word. */ |
bb70624e JA |
2323 | if (cwp && cw == -1 && sentinel == ts-1) |
2324 | cw = nw; | |
2325 | ||
2326 | /* If the cursor is at whitespace between two words, make a new, empty | |
28ef6c31 JA |
2327 | word, add it before (well, after, since the list is in reverse order) |
2328 | the word we just added, and set the current word to that one. */ | |
bb70624e | 2329 | if (cwp && cw == -1 && sentinel < ts) |
28ef6c31 | 2330 | { |
7117c2d2 | 2331 | tl = make_word_list (make_word (""), ret->next); |
28ef6c31 JA |
2332 | ret->next = tl; |
2333 | cw = nw; | |
2334 | nw++; | |
2335 | } | |
bb70624e JA |
2336 | |
2337 | if (string[te] == 0) | |
2338 | break; | |
2339 | ||
b80f6443 JA |
2340 | i = te; |
2341 | while (member (string[i], d) && (ifs_split || spctabnl(string[i]))) | |
bb70624e JA |
2342 | i++; |
2343 | ||
2344 | if (string[i]) | |
2345 | ts = i; | |
2346 | else | |
2347 | break; | |
2348 | } | |
2349 | ||
2350 | /* Special case for SENTINEL at the end of STRING. If we haven't found | |
2351 | the word containing SENTINEL yet, and the index we're looking for is at | |
0001803f CR |
2352 | the end of STRING (or past the end of the previously-found token, |
2353 | possible if the end of the line is composed solely of IFS whitespace) | |
2354 | add an additional null argument and set the current word pointer to that. */ | |
2355 | if (cwp && cw == -1 && (sentinel >= slen || sentinel >= te)) | |
bb70624e JA |
2356 | { |
2357 | if (whitespace (string[sentinel - 1])) | |
28ef6c31 JA |
2358 | { |
2359 | token = ""; | |
2360 | ret = add_string_to_list (token, ret); | |
2361 | nw++; | |
2362 | } | |
bb70624e JA |
2363 | cw = nw; |
2364 | } | |
2365 | ||
2366 | if (nwp) | |
2367 | *nwp = nw; | |
2368 | if (cwp) | |
2369 | *cwp = cw; | |
2370 | ||
ac50fbac CR |
2371 | FREE (d2); |
2372 | ||
bb70624e JA |
2373 | return (REVERSE_LIST (ret, WORD_LIST *)); |
2374 | } | |
726f6388 JA |
2375 | #endif /* READLINE */ |
2376 | ||
ccc6cda3 JA |
2377 | #if 0 |
2378 | /* UNUSED */ | |
726f6388 JA |
2379 | /* Extract the name of the variable to bind to from the assignment string. */ |
2380 | char * | |
2381 | assignment_name (string) | |
2382 | char *string; | |
2383 | { | |
ccc6cda3 | 2384 | int offset; |
726f6388 JA |
2385 | char *temp; |
2386 | ||
b80f6443 | 2387 | offset = assignment (string, 0); |
ccc6cda3 | 2388 | if (offset == 0) |
726f6388 | 2389 | return (char *)NULL; |
bb70624e | 2390 | temp = substring (string, 0, offset); |
726f6388 JA |
2391 | return (temp); |
2392 | } | |
ccc6cda3 | 2393 | #endif |
726f6388 | 2394 | |
cce855bc JA |
2395 | /* **************************************************************** */ |
2396 | /* */ | |
2397 | /* Functions to convert strings to WORD_LISTs and vice versa */ | |
2398 | /* */ | |
2399 | /* **************************************************************** */ | |
2400 | ||
726f6388 JA |
2401 | /* Return a single string of all the words in LIST. SEP is the separator |
2402 | to put between individual elements of LIST in the output string. */ | |
7117c2d2 | 2403 | char * |
726f6388 JA |
2404 | string_list_internal (list, sep) |
2405 | WORD_LIST *list; | |
2406 | char *sep; | |
2407 | { | |
2408 | register WORD_LIST *t; | |
2409 | char *result, *r; | |
a0c0a00f | 2410 | size_t word_len, sep_len, result_size; |
726f6388 | 2411 | |
ccc6cda3 | 2412 | if (list == 0) |
726f6388 JA |
2413 | return ((char *)NULL); |
2414 | ||
b80f6443 JA |
2415 | /* Short-circuit quickly if we don't need to separate anything. */ |
2416 | if (list->next == 0) | |
2417 | return (savestring (list->word->word)); | |
2418 | ||
726f6388 JA |
2419 | /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */ |
2420 | sep_len = STRLEN (sep); | |
2421 | result_size = 0; | |
2422 | ||
2423 | for (t = list; t; t = t->next) | |
2424 | { | |
2425 | if (t != list) | |
2426 | result_size += sep_len; | |
2427 | result_size += strlen (t->word->word); | |
2428 | } | |
2429 | ||
f73dda09 | 2430 | r = result = (char *)xmalloc (result_size + 1); |
726f6388 JA |
2431 | |
2432 | for (t = list; t; t = t->next) | |
2433 | { | |
2434 | if (t != list && sep_len) | |
2435 | { | |
ccc6cda3 JA |
2436 | if (sep_len > 1) |
2437 | { | |
2438 | FASTCOPY (sep, r, sep_len); | |
2439 | r += sep_len; | |
2440 | } | |
2441 | else | |
2442 | *r++ = sep[0]; | |
726f6388 JA |
2443 | } |
2444 | ||
2445 | word_len = strlen (t->word->word); | |
2446 | FASTCOPY (t->word->word, r, word_len); | |
2447 | r += word_len; | |
2448 | } | |
2449 | ||
ccc6cda3 | 2450 | *r = '\0'; |
726f6388 JA |
2451 | return (result); |
2452 | } | |
2453 | ||
2454 | /* Return a single string of all the words present in LIST, separating | |
2455 | each word with a space. */ | |
2456 | char * | |
2457 | string_list (list) | |
2458 | WORD_LIST *list; | |
2459 | { | |
2460 | return (string_list_internal (list, " ")); | |
2461 | } | |
2462 | ||
3185942a JA |
2463 | /* An external interface that can be used by the rest of the shell to |
2464 | obtain a string containing the first character in $IFS. Handles all | |
2465 | the multibyte complications. If LENP is non-null, it is set to the | |
2466 | length of the returned string. */ | |
2467 | char * | |
2468 | ifs_firstchar (lenp) | |
2469 | int *lenp; | |
2470 | { | |
2471 | char *ret; | |
2472 | int len; | |
2473 | ||
2474 | ret = xmalloc (MB_LEN_MAX + 1); | |
2475 | #if defined (HANDLE_MULTIBYTE) | |
2476 | if (ifs_firstc_len == 1) | |
2477 | { | |
2478 | ret[0] = ifs_firstc[0]; | |
2479 | ret[1] = '\0'; | |
2480 | len = ret[0] ? 1 : 0; | |
2481 | } | |
2482 | else | |
2483 | { | |
2484 | memcpy (ret, ifs_firstc, ifs_firstc_len); | |
2485 | ret[len = ifs_firstc_len] = '\0'; | |
2486 | } | |
2487 | #else | |
2488 | ret[0] = ifs_firstc; | |
2489 | ret[1] = '\0'; | |
2490 | len = ret[0] ? 0 : 1; | |
2491 | #endif | |
2492 | ||
2493 | if (lenp) | |
2494 | *lenp = len; | |
2495 | ||
2496 | return ret; | |
2497 | } | |
2498 | ||
726f6388 JA |
2499 | /* Return a single string of all the words present in LIST, obeying the |
2500 | quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the | |
2501 | expansion [of $*] appears within a double quoted string, it expands | |
2502 | to a single field with the value of each parameter separated by the | |
2503 | first character of the IFS variable, or by a <space> if IFS is unset." */ | |
f73dda09 | 2504 | char * |
726f6388 JA |
2505 | string_list_dollar_star (list) |
2506 | WORD_LIST *list; | |
2507 | { | |
0628567a | 2508 | char *ret; |
95732b49 | 2509 | #if defined (HANDLE_MULTIBYTE) |
0628567a | 2510 | # if defined (__GNUC__) |
95732b49 | 2511 | char sep[MB_CUR_MAX + 1]; |
0628567a JA |
2512 | # else |
2513 | char *sep = 0; | |
2514 | # endif | |
95732b49 | 2515 | #else |
7117c2d2 | 2516 | char sep[2]; |
95732b49 | 2517 | #endif |
726f6388 | 2518 | |
95732b49 | 2519 | #if defined (HANDLE_MULTIBYTE) |
0628567a JA |
2520 | # if !defined (__GNUC__) |
2521 | sep = (char *)xmalloc (MB_CUR_MAX + 1); | |
2522 | # endif /* !__GNUC__ */ | |
95732b49 JA |
2523 | if (ifs_firstc_len == 1) |
2524 | { | |
2525 | sep[0] = ifs_firstc[0]; | |
2526 | sep[1] = '\0'; | |
2527 | } | |
2528 | else | |
2529 | { | |
2530 | memcpy (sep, ifs_firstc, ifs_firstc_len); | |
2531 | sep[ifs_firstc_len] = '\0'; | |
2532 | } | |
2533 | #else | |
7117c2d2 | 2534 | sep[0] = ifs_firstc; |
726f6388 | 2535 | sep[1] = '\0'; |
95732b49 | 2536 | #endif |
726f6388 | 2537 | |
0628567a JA |
2538 | ret = string_list_internal (list, sep); |
2539 | #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__) | |
2540 | free (sep); | |
2541 | #endif | |
2542 | return ret; | |
726f6388 JA |
2543 | } |
2544 | ||
cce855bc JA |
2545 | /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
2546 | is non-zero, the $@ appears within double quotes, and we should quote | |
2547 | the list before converting it into a string. If IFS is unset, and the | |
2548 | word is not quoted, we just need to quote CTLESC and CTLNUL characters | |
2549 | in the words in the list, because the default value of $IFS is | |
2550 | <space><tab><newline>, IFS characters in the words in the list should | |
2551 | also be split. If IFS is null, and the word is not quoted, we need | |
2552 | to quote the words in the list to preserve the positional parameters | |
a0c0a00f CR |
2553 | exactly. |
2554 | Valid values for the FLAGS argument are the PF_ flags in command.h, | |
2555 | the only one we care about is PF_ASSIGNRHS. $@ is supposed to expand | |
2556 | to the positional parameters separated by spaces no matter what IFS is | |
2557 | set to if in a context where word splitting is not performed. The only | |
2558 | one that we didn't handle before is assignment statement arguments to | |
2559 | declaration builtins like `declare'. */ | |
f73dda09 | 2560 | char * |
a0c0a00f | 2561 | string_list_dollar_at (list, quoted, flags) |
cce855bc JA |
2562 | WORD_LIST *list; |
2563 | int quoted; | |
a0c0a00f | 2564 | int flags; |
cce855bc | 2565 | { |
95732b49 JA |
2566 | char *ifs, *ret; |
2567 | #if defined (HANDLE_MULTIBYTE) | |
0628567a | 2568 | # if defined (__GNUC__) |
95732b49 | 2569 | char sep[MB_CUR_MAX + 1]; |
0628567a JA |
2570 | # else |
2571 | char *sep = 0; | |
2572 | # endif /* !__GNUC__ */ | |
95732b49 JA |
2573 | #else |
2574 | char sep[2]; | |
2575 | #endif | |
cce855bc JA |
2576 | WORD_LIST *tlist; |
2577 | ||
7117c2d2 JA |
2578 | /* XXX this could just be ifs = ifs_value; */ |
2579 | ifs = ifs_var ? value_cell (ifs_var) : (char *)0; | |
cce855bc | 2580 | |
95732b49 | 2581 | #if defined (HANDLE_MULTIBYTE) |
0628567a JA |
2582 | # if !defined (__GNUC__) |
2583 | sep = (char *)xmalloc (MB_CUR_MAX + 1); | |
2584 | # endif /* !__GNUC__ */ | |
a0c0a00f CR |
2585 | /* XXX - bash-4.4/bash-5.0 testing PF_ASSIGNRHS */ |
2586 | if (flags & PF_ASSIGNRHS) | |
2587 | { | |
2588 | sep[0] = ' '; | |
2589 | sep[1] = '\0'; | |
2590 | } | |
2591 | else if (ifs && *ifs) | |
95732b49 JA |
2592 | { |
2593 | if (ifs_firstc_len == 1) | |
2594 | { | |
2595 | sep[0] = ifs_firstc[0]; | |
2596 | sep[1] = '\0'; | |
2597 | } | |
2598 | else | |
2599 | { | |
2600 | memcpy (sep, ifs_firstc, ifs_firstc_len); | |
2601 | sep[ifs_firstc_len] = '\0'; | |
2602 | } | |
2603 | } | |
2604 | else | |
2605 | { | |
2606 | sep[0] = ' '; | |
2607 | sep[1] = '\0'; | |
2608 | } | |
2609 | #else | |
a0c0a00f CR |
2610 | /* XXX - bash-4.4/bash-5.0 test PF_ASSIGNRHS */ |
2611 | sep[0] = ((flags & PF_ASSIGNRHS) || ifs == 0 || *ifs == 0) ? ' ' : *ifs; | |
cce855bc | 2612 | sep[1] = '\0'; |
95732b49 | 2613 | #endif |
cce855bc | 2614 | |
f1be666c JA |
2615 | /* XXX -- why call quote_list if ifs == 0? we can get away without doing |
2616 | it now that quote_escapes quotes spaces */ | |
0001803f | 2617 | tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE)) |
cce855bc JA |
2618 | ? quote_list (list) |
2619 | : list_quote_escapes (list); | |
0628567a JA |
2620 | |
2621 | ret = string_list_internal (tlist, sep); | |
2622 | #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__) | |
2623 | free (sep); | |
2624 | #endif | |
2625 | return ret; | |
cce855bc JA |
2626 | } |
2627 | ||
ac50fbac | 2628 | /* Turn the positional parameters into a string, understanding quoting and |
3185942a JA |
2629 | the various subtleties of using the first character of $IFS as the |
2630 | separator. Calls string_list_dollar_at, string_list_dollar_star, and | |
2631 | string_list as appropriate. */ | |
2632 | char * | |
2633 | string_list_pos_params (pchar, list, quoted) | |
2634 | int pchar; | |
2635 | WORD_LIST *list; | |
2636 | int quoted; | |
2637 | { | |
2638 | char *ret; | |
2639 | WORD_LIST *tlist; | |
2640 | ||
2641 | if (pchar == '*' && (quoted & Q_DOUBLE_QUOTES)) | |
2642 | { | |
2643 | tlist = quote_list (list); | |
2644 | word_list_remove_quoted_nulls (tlist); | |
2645 | ret = string_list_dollar_star (tlist); | |
2646 | } | |
2647 | else if (pchar == '*' && (quoted & Q_HERE_DOCUMENT)) | |
2648 | { | |
2649 | tlist = quote_list (list); | |
2650 | word_list_remove_quoted_nulls (tlist); | |
2651 | ret = string_list (tlist); | |
2652 | } | |
2653 | else if (pchar == '*') | |
2654 | { | |
2655 | /* Even when unquoted, string_list_dollar_star does the right thing | |
2656 | making sure that the first character of $IFS is used as the | |
2657 | separator. */ | |
2658 | ret = string_list_dollar_star (list); | |
2659 | } | |
2660 | else if (pchar == '@' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
2661 | /* We use string_list_dollar_at, but only if the string is quoted, since | |
2662 | that quotes the escapes if it's not, which we don't want. We could | |
2663 | use string_list (the old code did), but that doesn't do the right | |
2664 | thing if the first character of $IFS is not a space. We use | |
2665 | string_list_dollar_star if the string is unquoted so we make sure that | |
2666 | the elements of $@ are separated by the first character of $IFS for | |
2667 | later splitting. */ | |
a0c0a00f | 2668 | ret = string_list_dollar_at (list, quoted, 0); |
3185942a JA |
2669 | else if (pchar == '@') |
2670 | ret = string_list_dollar_star (list); | |
2671 | else | |
2672 | ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list); | |
2673 | ||
2674 | return ret; | |
2675 | } | |
2676 | ||
726f6388 JA |
2677 | /* Return the list of words present in STRING. Separate the string into |
2678 | words at any of the characters found in SEPARATORS. If QUOTED is | |
2679 | non-zero then word in the list will have its quoted flag set, otherwise | |
2680 | the quoted flag is left as make_word () deemed fit. | |
2681 | ||
2682 | This obeys the P1003.2 word splitting semantics. If `separators' is | |
2683 | exactly <space><tab><newline>, then the splitting algorithm is that of | |
2684 | the Bourne shell, which treats any sequence of characters from `separators' | |
2685 | as a delimiter. If IFS is unset, which results in `separators' being set | |
2686 | to "", no splitting occurs. If separators has some other value, the | |
2687 | following rules are applied (`IFS white space' means zero or more | |
2688 | occurrences of <space>, <tab>, or <newline>, as long as those characters | |
2689 | are in `separators'): | |
2690 | ||
2691 | 1) IFS white space is ignored at the start and the end of the | |
2692 | string. | |
2693 | 2) Each occurrence of a character in `separators' that is not | |
2694 | IFS white space, along with any adjacent occurrences of | |
2695 | IFS white space delimits a field. | |
2696 | 3) Any nonzero-length sequence of IFS white space delimits a field. | |
2697 | */ | |
2698 | ||
2699 | /* BEWARE! list_string strips null arguments. Don't call it twice and | |
2700 | expect to have "" preserved! */ | |
2701 | ||
726f6388 JA |
2702 | /* This performs word splitting and quoted null character removal on |
2703 | STRING. */ | |
b80f6443 JA |
2704 | #define issep(c) \ |
2705 | (((separators)[0]) ? ((separators)[1] ? isifs(c) \ | |
2706 | : (c) == (separators)[0]) \ | |
2707 | : 0) | |
726f6388 JA |
2708 | |
2709 | WORD_LIST * | |
2710 | list_string (string, separators, quoted) | |
2711 | register char *string, *separators; | |
2712 | int quoted; | |
2713 | { | |
ccc6cda3 JA |
2714 | WORD_LIST *result; |
2715 | WORD_DESC *t; | |
2716 | char *current_word, *s; | |
3185942a | 2717 | int sindex, sh_style_split, whitesep, xflags; |
95732b49 | 2718 | size_t slen; |
726f6388 JA |
2719 | |
2720 | if (!string || !*string) | |
2721 | return ((WORD_LIST *)NULL); | |
2722 | ||
7117c2d2 JA |
2723 | sh_style_split = separators && separators[0] == ' ' && |
2724 | separators[1] == '\t' && | |
2725 | separators[2] == '\n' && | |
2726 | separators[3] == '\0'; | |
3185942a JA |
2727 | for (xflags = 0, s = ifs_value; s && *s; s++) |
2728 | { | |
2729 | if (*s == CTLESC) xflags |= SX_NOCTLESC; | |
2730 | else if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL; | |
2731 | } | |
726f6388 | 2732 | |
95732b49 | 2733 | slen = 0; |
726f6388 JA |
2734 | /* Remove sequences of whitespace at the beginning of STRING, as |
2735 | long as those characters appear in IFS. Do not do this if | |
2736 | STRING is quoted or if there are no separator characters. */ | |
2737 | if (!quoted || !separators || !*separators) | |
2738 | { | |
2739 | for (s = string; *s && spctabnl (*s) && issep (*s); s++); | |
2740 | ||
2741 | if (!*s) | |
2742 | return ((WORD_LIST *)NULL); | |
2743 | ||
2744 | string = s; | |
2745 | } | |
2746 | ||
2747 | /* OK, now STRING points to a word that does not begin with white space. | |
2748 | The splitting algorithm is: | |
7117c2d2 JA |
2749 | extract a word, stopping at a separator |
2750 | skip sequences of spc, tab, or nl as long as they are separators | |
726f6388 | 2751 | This obeys the field splitting rules in Posix.2. */ |
a0c0a00f | 2752 | slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 1; |
ccc6cda3 | 2753 | for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; ) |
726f6388 | 2754 | { |
95732b49 JA |
2755 | /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim |
2756 | unless multibyte chars are possible. */ | |
3185942a | 2757 | current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags); |
ccc6cda3 | 2758 | if (current_word == 0) |
726f6388 JA |
2759 | break; |
2760 | ||
2761 | /* If we have a quoted empty string, add a quoted null argument. We | |
2762 | want to preserve the quoted null character iff this is a quoted | |
2763 | empty string; otherwise the quoted null characters are removed | |
2764 | below. */ | |
2765 | if (QUOTED_NULL (current_word)) | |
2766 | { | |
95732b49 | 2767 | t = alloc_word_desc (); |
726f6388 | 2768 | t->word = make_quoted_char ('\0'); |
95732b49 | 2769 | t->flags |= W_QUOTED|W_HASQUOTEDNULL; |
726f6388 JA |
2770 | result = make_word_list (t, result); |
2771 | } | |
ccc6cda3 | 2772 | else if (current_word[0] != '\0') |
726f6388 JA |
2773 | { |
2774 | /* If we have something, then add it regardless. However, | |
2775 | perform quoted null character removal on the current word. */ | |
2776 | remove_quoted_nulls (current_word); | |
cce855bc | 2777 | result = add_string_to_list (current_word, result); |
95732b49 | 2778 | result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */ |
ccc6cda3 JA |
2779 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) |
2780 | result->word->flags |= W_QUOTED; | |
726f6388 JA |
2781 | } |
2782 | ||
2783 | /* If we're not doing sequences of separators in the traditional | |
2784 | Bourne shell style, then add a quoted null argument. */ | |
726f6388 JA |
2785 | else if (!sh_style_split && !spctabnl (string[sindex])) |
2786 | { | |
95732b49 | 2787 | t = alloc_word_desc (); |
ccc6cda3 | 2788 | t->word = make_quoted_char ('\0'); |
95732b49 | 2789 | t->flags |= W_QUOTED|W_HASQUOTEDNULL; |
ccc6cda3 | 2790 | result = make_word_list (t, result); |
726f6388 JA |
2791 | } |
2792 | ||
2793 | free (current_word); | |
2794 | ||
28ef6c31 JA |
2795 | /* Note whether or not the separator is IFS whitespace, used later. */ |
2796 | whitesep = string[sindex] && spctabnl (string[sindex]); | |
2797 | ||
726f6388 JA |
2798 | /* Move past the current separator character. */ |
2799 | if (string[sindex]) | |
95732b49 JA |
2800 | { |
2801 | DECLARE_MBSTATE; | |
2802 | ADVANCE_CHAR (string, slen, sindex); | |
2803 | } | |
726f6388 JA |
2804 | |
2805 | /* Now skip sequences of space, tab, or newline characters if they are | |
2806 | in the list of separators. */ | |
2807 | while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex])) | |
2808 | sindex++; | |
28ef6c31 | 2809 | |
7117c2d2 JA |
2810 | /* If the first separator was IFS whitespace and the current character |
2811 | is a non-whitespace IFS character, it should be part of the current | |
2812 | field delimiter, not a separate delimiter that would result in an | |
2813 | empty field. Look at POSIX.2, 3.6.5, (3)(b). */ | |
28ef6c31 | 2814 | if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex])) |
95732b49 JA |
2815 | { |
2816 | sindex++; | |
2817 | /* An IFS character that is not IFS white space, along with any | |
2818 | adjacent IFS white space, shall delimit a field. (SUSv3) */ | |
0628567a | 2819 | while (string[sindex] && spctabnl (string[sindex]) && isifs (string[sindex])) |
95732b49 JA |
2820 | sindex++; |
2821 | } | |
726f6388 JA |
2822 | } |
2823 | return (REVERSE_LIST (result, WORD_LIST *)); | |
2824 | } | |
2825 | ||
2826 | /* Parse a single word from STRING, using SEPARATORS to separate fields. | |
2827 | ENDPTR is set to the first character after the word. This is used by | |
7117c2d2 JA |
2828 | the `read' builtin. This is never called with SEPARATORS != $IFS; |
2829 | it should be simplified. | |
2830 | ||
726f6388 JA |
2831 | XXX - this function is very similar to list_string; they should be |
2832 | combined - XXX */ | |
2833 | char * | |
2834 | get_word_from_string (stringp, separators, endptr) | |
2835 | char **stringp, *separators, **endptr; | |
2836 | { | |
2837 | register char *s; | |
2838 | char *current_word; | |
3185942a | 2839 | int sindex, sh_style_split, whitesep, xflags; |
95732b49 | 2840 | size_t slen; |
726f6388 JA |
2841 | |
2842 | if (!stringp || !*stringp || !**stringp) | |
2843 | return ((char *)NULL); | |
ccc6cda3 | 2844 | |
7117c2d2 JA |
2845 | sh_style_split = separators && separators[0] == ' ' && |
2846 | separators[1] == '\t' && | |
2847 | separators[2] == '\n' && | |
2848 | separators[3] == '\0'; | |
3185942a JA |
2849 | for (xflags = 0, s = ifs_value; s && *s; s++) |
2850 | { | |
2851 | if (*s == CTLESC) xflags |= SX_NOCTLESC; | |
2852 | if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL; | |
2853 | } | |
726f6388 | 2854 | |
3185942a | 2855 | s = *stringp; |
95732b49 JA |
2856 | slen = 0; |
2857 | ||
726f6388 JA |
2858 | /* Remove sequences of whitespace at the beginning of STRING, as |
2859 | long as those characters appear in IFS. */ | |
2860 | if (sh_style_split || !separators || !*separators) | |
2861 | { | |
7117c2d2 | 2862 | for (; *s && spctabnl (*s) && isifs (*s); s++); |
726f6388 JA |
2863 | |
2864 | /* If the string is nothing but whitespace, update it and return. */ | |
2865 | if (!*s) | |
2866 | { | |
2867 | *stringp = s; | |
2868 | if (endptr) | |
2869 | *endptr = s; | |
2870 | return ((char *)NULL); | |
2871 | } | |
2872 | } | |
2873 | ||
2874 | /* OK, S points to a word that does not begin with white space. | |
2875 | Now extract a word, stopping at a separator, save a pointer to | |
2876 | the first character after the word, then skip sequences of spc, | |
2877 | tab, or nl as long as they are separators. | |
ccc6cda3 | 2878 | |
726f6388 JA |
2879 | This obeys the field splitting rules in Posix.2. */ |
2880 | sindex = 0; | |
95732b49 JA |
2881 | /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim |
2882 | unless multibyte chars are possible. */ | |
a0c0a00f | 2883 | slen = (MB_CUR_MAX > 1) ? STRLEN (s) : 1; |
3185942a | 2884 | current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags); |
726f6388 JA |
2885 | |
2886 | /* Set ENDPTR to the first character after the end of the word. */ | |
2887 | if (endptr) | |
2888 | *endptr = s + sindex; | |
2889 | ||
28ef6c31 JA |
2890 | /* Note whether or not the separator is IFS whitespace, used later. */ |
2891 | whitesep = s[sindex] && spctabnl (s[sindex]); | |
2892 | ||
726f6388 JA |
2893 | /* Move past the current separator character. */ |
2894 | if (s[sindex]) | |
95732b49 JA |
2895 | { |
2896 | DECLARE_MBSTATE; | |
2897 | ADVANCE_CHAR (s, slen, sindex); | |
2898 | } | |
726f6388 JA |
2899 | |
2900 | /* Now skip sequences of space, tab, or newline characters if they are | |
2901 | in the list of separators. */ | |
7117c2d2 | 2902 | while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex])) |
726f6388 JA |
2903 | sindex++; |
2904 | ||
28ef6c31 JA |
2905 | /* If the first separator was IFS whitespace and the current character is |
2906 | a non-whitespace IFS character, it should be part of the current field | |
2907 | delimiter, not a separate delimiter that would result in an empty field. | |
2908 | Look at POSIX.2, 3.6.5, (3)(b). */ | |
7117c2d2 | 2909 | if (s[sindex] && whitesep && isifs (s[sindex]) && !spctabnl (s[sindex])) |
95732b49 JA |
2910 | { |
2911 | sindex++; | |
2912 | /* An IFS character that is not IFS white space, along with any adjacent | |
2913 | IFS white space, shall delimit a field. */ | |
2914 | while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex])) | |
2915 | sindex++; | |
2916 | } | |
28ef6c31 | 2917 | |
726f6388 JA |
2918 | /* Update STRING to point to the next field. */ |
2919 | *stringp = s + sindex; | |
2920 | return (current_word); | |
2921 | } | |
2922 | ||
2923 | /* Remove IFS white space at the end of STRING. Start at the end | |
2924 | of the string and walk backwards until the beginning of the string | |
2925 | or we find a character that's not IFS white space and not CTLESC. | |
2926 | Only let CTLESC escape a white space character if SAW_ESCAPE is | |
2927 | non-zero. */ | |
2928 | char * | |
2929 | strip_trailing_ifs_whitespace (string, separators, saw_escape) | |
2930 | char *string, *separators; | |
2931 | int saw_escape; | |
2932 | { | |
2933 | char *s; | |
ccc6cda3 | 2934 | |
726f6388 | 2935 | s = string + STRLEN (string) - 1; |
7117c2d2 | 2936 | while (s > string && ((spctabnl (*s) && isifs (*s)) || |
726f6388 JA |
2937 | (saw_escape && *s == CTLESC && spctabnl (s[1])))) |
2938 | s--; | |
2939 | *++s = '\0'; | |
2940 | return string; | |
2941 | } | |
2942 | ||
bb70624e JA |
2943 | #if 0 |
2944 | /* UNUSED */ | |
2945 | /* Split STRING into words at whitespace. Obeys shell-style quoting with | |
2946 | backslashes, single and double quotes. */ | |
ccc6cda3 JA |
2947 | WORD_LIST * |
2948 | list_string_with_quotes (string) | |
2949 | char *string; | |
2950 | { | |
2951 | WORD_LIST *list; | |
2952 | char *token, *s; | |
7117c2d2 | 2953 | size_t s_len; |
ccc6cda3 JA |
2954 | int c, i, tokstart, len; |
2955 | ||
2956 | for (s = string; s && *s && spctabnl (*s); s++) | |
2957 | ; | |
2958 | if (s == 0 || *s == 0) | |
2959 | return ((WORD_LIST *)NULL); | |
2960 | ||
7117c2d2 | 2961 | s_len = strlen (s); |
ccc6cda3 JA |
2962 | tokstart = i = 0; |
2963 | list = (WORD_LIST *)NULL; | |
2964 | while (1) | |
2965 | { | |
2966 | c = s[i]; | |
2967 | if (c == '\\') | |
2968 | { | |
2969 | i++; | |
2970 | if (s[i]) | |
2971 | i++; | |
2972 | } | |
2973 | else if (c == '\'') | |
a0c0a00f | 2974 | i = skip_single_quoted (s, s_len, ++i, 0); |
ccc6cda3 | 2975 | else if (c == '"') |
a0c0a00f | 2976 | i = skip_double_quoted (s, s_len, ++i, 0); |
ccc6cda3 JA |
2977 | else if (c == 0 || spctabnl (c)) |
2978 | { | |
2979 | /* We have found the end of a token. Make a word out of it and | |
2980 | add it to the word list. */ | |
bb70624e | 2981 | token = substring (s, tokstart, i); |
cce855bc | 2982 | list = add_string_to_list (token, list); |
ccc6cda3 JA |
2983 | free (token); |
2984 | while (spctabnl (s[i])) | |
2985 | i++; | |
2986 | if (s[i]) | |
2987 | tokstart = i; | |
2988 | else | |
2989 | break; | |
2990 | } | |
2991 | else | |
2992 | i++; /* normal character */ | |
2993 | } | |
2994 | return (REVERSE_LIST (list, WORD_LIST *)); | |
2995 | } | |
bb70624e | 2996 | #endif |
d166f048 | 2997 | |
cce855bc JA |
2998 | /********************************************************/ |
2999 | /* */ | |
3000 | /* Functions to perform assignment statements */ | |
3001 | /* */ | |
3002 | /********************************************************/ | |
d166f048 | 3003 | |
95732b49 JA |
3004 | #if defined (ARRAY_VARS) |
3005 | static SHELL_VAR * | |
3006 | do_compound_assignment (name, value, flags) | |
3007 | char *name, *value; | |
3008 | int flags; | |
3009 | { | |
3010 | SHELL_VAR *v; | |
ac50fbac | 3011 | int mklocal, mkassoc, mkglobal; |
0628567a | 3012 | WORD_LIST *list; |
95732b49 JA |
3013 | |
3014 | mklocal = flags & ASS_MKLOCAL; | |
3185942a | 3015 | mkassoc = flags & ASS_MKASSOC; |
ac50fbac | 3016 | mkglobal = flags & ASS_MKGLOBAL; |
95732b49 JA |
3017 | |
3018 | if (mklocal && variable_context) | |
3019 | { | |
3020 | v = find_variable (name); | |
a0c0a00f CR |
3021 | if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v))) |
3022 | { | |
3023 | if (v && readonly_p (v)) | |
3024 | err_readonly (name); | |
3025 | return (v); /* XXX */ | |
3026 | } | |
3185942a JA |
3027 | list = expand_compound_array_assignment (v, value, flags); |
3028 | if (mkassoc) | |
3029 | v = make_local_assoc_variable (name); | |
3030 | else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context) | |
ac50fbac CR |
3031 | v = make_local_array_variable (name, 0); |
3032 | if (v) | |
3033 | assign_compound_array_list (v, list, flags); | |
a0c0a00f CR |
3034 | if (list) |
3035 | dispose_words (list); | |
ac50fbac CR |
3036 | } |
3037 | /* In a function but forcing assignment in global context */ | |
3038 | else if (mkglobal && variable_context) | |
3039 | { | |
3040 | v = find_global_variable (name); | |
a0c0a00f CR |
3041 | if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v))) |
3042 | { | |
3043 | if (v && readonly_p (v)) | |
3044 | err_readonly (name); | |
3045 | return (v); /* XXX */ | |
3046 | } | |
ac50fbac CR |
3047 | list = expand_compound_array_assignment (v, value, flags); |
3048 | if (v == 0 && mkassoc) | |
3049 | v = make_new_assoc_variable (name); | |
3050 | else if (v && mkassoc && assoc_p (v) == 0) | |
3051 | v = convert_var_to_assoc (v); | |
3052 | else if (v == 0) | |
3053 | v = make_new_array_variable (name); | |
3054 | else if (v && mkassoc == 0 && array_p (v) == 0) | |
3055 | v = convert_var_to_array (v); | |
3056 | if (v) | |
3057 | assign_compound_array_list (v, list, flags); | |
a0c0a00f CR |
3058 | if (list) |
3059 | dispose_words (list); | |
95732b49 JA |
3060 | } |
3061 | else | |
a0c0a00f CR |
3062 | { |
3063 | v = assign_array_from_string (name, value, flags); | |
3064 | if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v))) | |
3065 | { | |
3066 | if (v && readonly_p (v)) | |
3067 | err_readonly (name); | |
3068 | return (v); /* XXX */ | |
3069 | } | |
3070 | } | |
95732b49 JA |
3071 | |
3072 | return (v); | |
3073 | } | |
3074 | #endif | |
3075 | ||
726f6388 JA |
3076 | /* Given STRING, an assignment string, get the value of the right side |
3077 | of the `=', and bind it to the left side. If EXPAND is true, then | |
3078 | perform parameter expansion, command substitution, and arithmetic | |
3079 | expansion on the right-hand side. Perform tilde expansion in any | |
3080 | case. Do not perform word splitting on the result of expansion. */ | |
3081 | static int | |
95732b49 JA |
3082 | do_assignment_internal (word, expand) |
3083 | const WORD_DESC *word; | |
726f6388 JA |
3084 | int expand; |
3085 | { | |
495aee44 CR |
3086 | int offset, appendop, assign_list, aflags, retval; |
3087 | char *name, *value, *temp; | |
ccc6cda3 JA |
3088 | SHELL_VAR *entry; |
3089 | #if defined (ARRAY_VARS) | |
3090 | char *t; | |
b80f6443 | 3091 | int ni; |
ccc6cda3 | 3092 | #endif |
95732b49 | 3093 | const char *string; |
ccc6cda3 | 3094 | |
95732b49 JA |
3095 | if (word == 0 || word->word == 0) |
3096 | return 0; | |
3097 | ||
3098 | appendop = assign_list = aflags = 0; | |
3099 | string = word->word; | |
b80f6443 | 3100 | offset = assignment (string, 0); |
ccc6cda3 JA |
3101 | name = savestring (string); |
3102 | value = (char *)NULL; | |
726f6388 JA |
3103 | |
3104 | if (name[offset] == '=') | |
3105 | { | |
95732b49 JA |
3106 | if (name[offset - 1] == '+') |
3107 | { | |
3108 | appendop = 1; | |
3109 | name[offset - 1] = '\0'; | |
3110 | } | |
3111 | ||
3112 | name[offset] = 0; /* might need this set later */ | |
726f6388 JA |
3113 | temp = name + offset + 1; |
3114 | ||
ccc6cda3 | 3115 | #if defined (ARRAY_VARS) |
95732b49 | 3116 | if (expand && (word->flags & W_COMPASSIGN)) |
726f6388 | 3117 | { |
ccc6cda3 | 3118 | assign_list = ni = 1; |
95732b49 | 3119 | value = extract_array_assignment_list (temp, &ni); |
ccc6cda3 JA |
3120 | } |
3121 | else | |
3122 | #endif | |
ccc6cda3 | 3123 | if (expand && temp[0]) |
95732b49 | 3124 | value = expand_string_if_necessary (temp, 0, expand_string_assignment); |
726f6388 JA |
3125 | else |
3126 | value = savestring (temp); | |
3127 | } | |
3128 | ||
3129 | if (value == 0) | |
d166f048 | 3130 | { |
f73dda09 | 3131 | value = (char *)xmalloc (1); |
d166f048 JA |
3132 | value[0] = '\0'; |
3133 | } | |
726f6388 | 3134 | |
726f6388 | 3135 | if (echo_command_at_execute) |
95732b49 JA |
3136 | { |
3137 | if (appendop) | |
3138 | name[offset - 1] = '+'; | |
3139 | xtrace_print_assignment (name, value, assign_list, 1); | |
3140 | if (appendop) | |
3141 | name[offset - 1] = '\0'; | |
3142 | } | |
726f6388 | 3143 | |
d166f048 | 3144 | #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0) |
ccc6cda3 | 3145 | |
95732b49 JA |
3146 | if (appendop) |
3147 | aflags |= ASS_APPEND; | |
3148 | ||
ccc6cda3 | 3149 | #if defined (ARRAY_VARS) |
0001803f | 3150 | if (t = mbschr (name, '[')) /*]*/ |
ccc6cda3 JA |
3151 | { |
3152 | if (assign_list) | |
3153 | { | |
b80f6443 | 3154 | report_error (_("%s: cannot assign list to array member"), name); |
ccc6cda3 JA |
3155 | ASSIGN_RETURN (0); |
3156 | } | |
95732b49 | 3157 | entry = assign_array_element (name, value, aflags); |
ccc6cda3 | 3158 | if (entry == 0) |
28ef6c31 | 3159 | ASSIGN_RETURN (0); |
ccc6cda3 JA |
3160 | } |
3161 | else if (assign_list) | |
95732b49 | 3162 | { |
6d41b715 | 3163 | if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL) == 0) |
95732b49 | 3164 | aflags |= ASS_MKLOCAL; |
ac50fbac CR |
3165 | if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL)) |
3166 | aflags |= ASS_MKGLOBAL; | |
3185942a JA |
3167 | if (word->flags & W_ASSIGNASSOC) |
3168 | aflags |= ASS_MKASSOC; | |
95732b49 JA |
3169 | entry = do_compound_assignment (name, value, aflags); |
3170 | } | |
ccc6cda3 JA |
3171 | else |
3172 | #endif /* ARRAY_VARS */ | |
95732b49 | 3173 | entry = bind_variable (name, value, aflags); |
ccc6cda3 | 3174 | |
726f6388 JA |
3175 | stupidly_hack_special_variables (name); |
3176 | ||
3185942a JA |
3177 | /* Return 1 if the assignment seems to have been performed correctly. */ |
3178 | if (entry == 0 || readonly_p (entry)) | |
3179 | retval = 0; /* assignment failure */ | |
3180 | else if (noassign_p (entry)) | |
3181 | { | |
3182 | last_command_exit_value = EXECUTION_FAILURE; | |
3183 | retval = 1; /* error status, but not assignment failure */ | |
3184 | } | |
3185 | else | |
3186 | retval = 1; | |
3187 | ||
3188 | if (entry && retval != 0 && noassign_p (entry) == 0) | |
3189 | VUNSETATTR (entry, att_invisible); | |
3190 | ||
3191 | ASSIGN_RETURN (retval); | |
726f6388 JA |
3192 | } |
3193 | ||
3194 | /* Perform the assignment statement in STRING, and expand the | |
95732b49 | 3195 | right side by doing tilde, command and parameter expansion. */ |
ccc6cda3 | 3196 | int |
726f6388 | 3197 | do_assignment (string) |
95732b49 | 3198 | char *string; |
726f6388 | 3199 | { |
95732b49 JA |
3200 | WORD_DESC td; |
3201 | ||
3202 | td.flags = W_ASSIGNMENT; | |
3203 | td.word = string; | |
3204 | ||
3205 | return do_assignment_internal (&td, 1); | |
3206 | } | |
3207 | ||
3208 | int | |
495aee44 | 3209 | do_word_assignment (word, flags) |
95732b49 | 3210 | WORD_DESC *word; |
495aee44 | 3211 | int flags; |
95732b49 JA |
3212 | { |
3213 | return do_assignment_internal (word, 1); | |
726f6388 JA |
3214 | } |
3215 | ||
3216 | /* Given STRING, an assignment string, get the value of the right side | |
95732b49 JA |
3217 | of the `=', and bind it to the left side. Do not perform any word |
3218 | expansions on the right hand side. */ | |
ccc6cda3 | 3219 | int |
726f6388 | 3220 | do_assignment_no_expand (string) |
95732b49 | 3221 | char *string; |
726f6388 | 3222 | { |
95732b49 JA |
3223 | WORD_DESC td; |
3224 | ||
3225 | td.flags = W_ASSIGNMENT; | |
3226 | td.word = string; | |
3227 | ||
3228 | return (do_assignment_internal (&td, 0)); | |
726f6388 JA |
3229 | } |
3230 | ||
cce855bc JA |
3231 | /*************************************************** |
3232 | * * | |
3233 | * Functions to manage the positional parameters * | |
3234 | * * | |
3235 | ***************************************************/ | |
726f6388 JA |
3236 | |
3237 | /* Return the word list that corresponds to `$*'. */ | |
3238 | WORD_LIST * | |
3239 | list_rest_of_args () | |
3240 | { | |
ccc6cda3 | 3241 | register WORD_LIST *list, *args; |
726f6388 JA |
3242 | int i; |
3243 | ||
3244 | /* Break out of the loop as soon as one of the dollar variables is null. */ | |
ccc6cda3 JA |
3245 | for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++) |
3246 | list = make_word_list (make_bare_word (dollar_vars[i]), list); | |
3247 | ||
3248 | for (args = rest_of_args; args; args = args->next) | |
3249 | list = make_word_list (make_bare_word (args->word->word), list); | |
726f6388 | 3250 | |
726f6388 JA |
3251 | return (REVERSE_LIST (list, WORD_LIST *)); |
3252 | } | |
3253 | ||
ccc6cda3 JA |
3254 | int |
3255 | number_of_args () | |
3256 | { | |
3257 | register WORD_LIST *list; | |
3258 | int n; | |
3259 | ||
3260 | for (n = 0; n < 9 && dollar_vars[n+1]; n++) | |
3261 | ; | |
3262 | for (list = rest_of_args; list; list = list->next) | |
3263 | n++; | |
3264 | return n; | |
3265 | } | |
3266 | ||
cce855bc JA |
3267 | /* Return the value of a positional parameter. This handles values > 10. */ |
3268 | char * | |
3269 | get_dollar_var_value (ind) | |
7117c2d2 | 3270 | intmax_t ind; |
cce855bc JA |
3271 | { |
3272 | char *temp; | |
3273 | WORD_LIST *p; | |
3274 | ||
3275 | if (ind < 10) | |
3276 | temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL; | |
3277 | else /* We want something like ${11} */ | |
3278 | { | |
3279 | ind -= 10; | |
3280 | for (p = rest_of_args; p && ind--; p = p->next) | |
28ef6c31 | 3281 | ; |
cce855bc JA |
3282 | temp = p ? savestring (p->word->word) : (char *)NULL; |
3283 | } | |
3284 | return (temp); | |
3285 | } | |
3286 | ||
726f6388 JA |
3287 | /* Make a single large string out of the dollar digit variables, |
3288 | and the rest_of_args. If DOLLAR_STAR is 1, then obey the special | |
3289 | case of "$*" with respect to IFS. */ | |
3290 | char * | |
3291 | string_rest_of_args (dollar_star) | |
3292 | int dollar_star; | |
3293 | { | |
ccc6cda3 | 3294 | register WORD_LIST *list; |
726f6388 JA |
3295 | char *string; |
3296 | ||
ccc6cda3 | 3297 | list = list_rest_of_args (); |
726f6388 JA |
3298 | string = dollar_star ? string_list_dollar_star (list) : string_list (list); |
3299 | dispose_words (list); | |
3300 | return (string); | |
3301 | } | |
3302 | ||
cce855bc JA |
3303 | /* Return a string containing the positional parameters from START to |
3304 | END, inclusive. If STRING[0] == '*', we obey the rules for $*, | |
7117c2d2 JA |
3305 | which only makes a difference if QUOTED is non-zero. If QUOTED includes |
3306 | Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise | |
3307 | no quoting chars are added. */ | |
cce855bc JA |
3308 | static char * |
3309 | pos_params (string, start, end, quoted) | |
3310 | char *string; | |
3311 | int start, end, quoted; | |
726f6388 | 3312 | { |
cce855bc JA |
3313 | WORD_LIST *save, *params, *h, *t; |
3314 | char *ret; | |
3315 | int i; | |
726f6388 | 3316 | |
bb70624e JA |
3317 | /* see if we can short-circuit. if start == end, we want 0 parameters. */ |
3318 | if (start == end) | |
3319 | return ((char *)NULL); | |
3320 | ||
cce855bc | 3321 | save = params = list_rest_of_args (); |
a0c0a00f | 3322 | if (save == 0 && start > 0) |
cce855bc JA |
3323 | return ((char *)NULL); |
3324 | ||
3185942a JA |
3325 | if (start == 0) /* handle ${@:0[:x]} specially */ |
3326 | { | |
3327 | t = make_word_list (make_word (dollar_vars[0]), params); | |
3328 | save = params = t; | |
3329 | } | |
3330 | ||
0001803f | 3331 | for (i = start ? 1 : 0; params && i < start; i++) |
cce855bc JA |
3332 | params = params->next; |
3333 | if (params == 0) | |
a0c0a00f CR |
3334 | { |
3335 | dispose_words (save); | |
3336 | return ((char *)NULL); | |
3337 | } | |
cce855bc | 3338 | for (h = t = params; params && i < end; i++) |
d166f048 | 3339 | { |
cce855bc JA |
3340 | t = params; |
3341 | params = params->next; | |
d166f048 | 3342 | } |
726f6388 | 3343 | |
cce855bc | 3344 | t->next = (WORD_LIST *)NULL; |
3185942a JA |
3345 | |
3346 | ret = string_list_pos_params (string[0], h, quoted); | |
3347 | ||
bb70624e JA |
3348 | if (t != params) |
3349 | t->next = params; | |
726f6388 | 3350 | |
cce855bc JA |
3351 | dispose_words (save); |
3352 | return (ret); | |
3353 | } | |
3354 | ||
3355 | /******************************************************************/ | |
3356 | /* */ | |
3357 | /* Functions to expand strings to strings or WORD_LISTs */ | |
3358 | /* */ | |
3359 | /******************************************************************/ | |
3360 | ||
3361 | #if defined (PROCESS_SUBSTITUTION) | |
95732b49 | 3362 | #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~') |
cce855bc | 3363 | #else |
95732b49 | 3364 | #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~') |
cce855bc JA |
3365 | #endif |
3366 | ||
3367 | /* If there are any characters in STRING that require full expansion, | |
3368 | then call FUNC to expand STRING; otherwise just perform quote | |
3369 | removal if necessary. This returns a new string. */ | |
3370 | static char * | |
f73dda09 | 3371 | expand_string_if_necessary (string, quoted, func) |
cce855bc JA |
3372 | char *string; |
3373 | int quoted; | |
f73dda09 | 3374 | EXPFUNC *func; |
cce855bc JA |
3375 | { |
3376 | WORD_LIST *list; | |
7117c2d2 | 3377 | size_t slen; |
cce855bc JA |
3378 | int i, saw_quote; |
3379 | char *ret; | |
7117c2d2 | 3380 | DECLARE_MBSTATE; |
cce855bc | 3381 | |
95732b49 JA |
3382 | /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */ |
3383 | slen = (MB_CUR_MAX > 1) ? strlen (string) : 0; | |
7117c2d2 JA |
3384 | i = saw_quote = 0; |
3385 | while (string[i]) | |
cce855bc JA |
3386 | { |
3387 | if (EXP_CHAR (string[i])) | |
3388 | break; | |
3389 | else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"') | |
3390 | saw_quote = 1; | |
7117c2d2 | 3391 | ADVANCE_CHAR (string, slen, i); |
cce855bc JA |
3392 | } |
3393 | ||
3394 | if (string[i]) | |
3395 | { | |
3396 | list = (*func) (string, quoted); | |
3397 | if (list) | |
3398 | { | |
3399 | ret = string_list (list); | |
3400 | dispose_words (list); | |
3401 | } | |
3402 | else | |
3403 | ret = (char *)NULL; | |
3404 | } | |
3405 | else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) | |
3406 | ret = string_quote_removal (string, quoted); | |
3407 | else | |
3408 | ret = savestring (string); | |
7117c2d2 | 3409 | |
cce855bc JA |
3410 | return ret; |
3411 | } | |
3412 | ||
3413 | static inline char * | |
f73dda09 | 3414 | expand_string_to_string_internal (string, quoted, func) |
cce855bc JA |
3415 | char *string; |
3416 | int quoted; | |
f73dda09 | 3417 | EXPFUNC *func; |
cce855bc JA |
3418 | { |
3419 | WORD_LIST *list; | |
3420 | char *ret; | |
3421 | ||
3422 | if (string == 0 || *string == '\0') | |
3423 | return ((char *)NULL); | |
3424 | ||
3425 | list = (*func) (string, quoted); | |
3426 | if (list) | |
3427 | { | |
3428 | ret = string_list (list); | |
3429 | dispose_words (list); | |
3430 | } | |
3431 | else | |
3432 | ret = (char *)NULL; | |
3433 | ||
3434 | return (ret); | |
3435 | } | |
3436 | ||
f73dda09 JA |
3437 | char * |
3438 | expand_string_to_string (string, quoted) | |
3439 | char *string; | |
3440 | int quoted; | |
3441 | { | |
3442 | return (expand_string_to_string_internal (string, quoted, expand_string)); | |
3443 | } | |
3444 | ||
3445 | char * | |
3446 | expand_string_unsplit_to_string (string, quoted) | |
3447 | char *string; | |
3448 | int quoted; | |
3449 | { | |
3450 | return (expand_string_to_string_internal (string, quoted, expand_string_unsplit)); | |
3451 | } | |
3452 | ||
95732b49 JA |
3453 | char * |
3454 | expand_assignment_string_to_string (string, quoted) | |
3455 | char *string; | |
3456 | int quoted; | |
3457 | { | |
3458 | return (expand_string_to_string_internal (string, quoted, expand_string_assignment)); | |
3459 | } | |
3460 | ||
0628567a JA |
3461 | char * |
3462 | expand_arith_string (string, quoted) | |
3463 | char *string; | |
3185942a | 3464 | int quoted; |
0628567a | 3465 | { |
ac50fbac CR |
3466 | WORD_DESC td; |
3467 | WORD_LIST *list, *tlist; | |
3468 | size_t slen; | |
3469 | int i, saw_quote; | |
3470 | char *ret; | |
3471 | DECLARE_MBSTATE; | |
3472 | ||
3473 | /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */ | |
3474 | slen = (MB_CUR_MAX > 1) ? strlen (string) : 0; | |
3475 | i = saw_quote = 0; | |
3476 | while (string[i]) | |
3477 | { | |
3478 | if (EXP_CHAR (string[i])) | |
3479 | break; | |
3480 | else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"') | |
3481 | saw_quote = 1; | |
3482 | ADVANCE_CHAR (string, slen, i); | |
3483 | } | |
3484 | ||
3485 | if (string[i]) | |
3486 | { | |
3487 | /* This is expanded version of expand_string_internal as it's called by | |
3488 | expand_string_leave_quoted */ | |
3489 | td.flags = W_NOPROCSUB; /* don't want process substitution */ | |
3490 | td.word = savestring (string); | |
3491 | list = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); | |
3492 | /* This takes care of the calls from expand_string_leave_quoted and | |
3493 | expand_string */ | |
3494 | if (list) | |
3495 | { | |
3496 | tlist = word_list_split (list); | |
3497 | dispose_words (list); | |
3498 | list = tlist; | |
3499 | if (list) | |
3500 | dequote_list (list); | |
3501 | } | |
3502 | /* This comes from expand_string_if_necessary */ | |
3503 | if (list) | |
3504 | { | |
3505 | ret = string_list (list); | |
3506 | dispose_words (list); | |
3507 | } | |
3508 | else | |
3509 | ret = (char *)NULL; | |
3510 | FREE (td.word); | |
3511 | } | |
a0c0a00f CR |
3512 | else if (saw_quote && (quoted & Q_ARITH)) |
3513 | ret = string_quote_removal (string, quoted); | |
ac50fbac CR |
3514 | else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) |
3515 | ret = string_quote_removal (string, quoted); | |
3516 | else | |
3517 | ret = savestring (string); | |
3518 | ||
3519 | return ret; | |
0628567a JA |
3520 | } |
3521 | ||
cce855bc JA |
3522 | #if defined (COND_COMMAND) |
3523 | /* Just remove backslashes in STRING. Returns a new string. */ | |
3524 | char * | |
3525 | remove_backslashes (string) | |
3526 | char *string; | |
3527 | { | |
3528 | char *r, *ret, *s; | |
3529 | ||
f73dda09 | 3530 | r = ret = (char *)xmalloc (strlen (string) + 1); |
cce855bc JA |
3531 | for (s = string; s && *s; ) |
3532 | { | |
3533 | if (*s == '\\') | |
28ef6c31 | 3534 | s++; |
cce855bc | 3535 | if (*s == 0) |
28ef6c31 | 3536 | break; |
cce855bc JA |
3537 | *r++ = *s++; |
3538 | } | |
3539 | *r = '\0'; | |
3540 | return ret; | |
3541 | } | |
3542 | ||
3543 | /* This needs better error handling. */ | |
3544 | /* Expand W for use as an argument to a unary or binary operator in a | |
f1be666c | 3545 | [[...]] expression. If SPECIAL is 1, this is the rhs argument |
cce855bc | 3546 | to the != or == operator, and should be treated as a pattern. In |
f1be666c JA |
3547 | this case, we quote the string specially for the globbing code. If |
3548 | SPECIAL is 2, this is an rhs argument for the =~ operator, and should | |
3549 | be quoted appropriately for regcomp/regexec. The caller is responsible | |
3550 | for removing the backslashes if the unquoted word is needed later. */ | |
cce855bc JA |
3551 | char * |
3552 | cond_expand_word (w, special) | |
3553 | WORD_DESC *w; | |
3554 | int special; | |
3555 | { | |
3556 | char *r, *p; | |
3557 | WORD_LIST *l; | |
f1be666c | 3558 | int qflags; |
cce855bc JA |
3559 | |
3560 | if (w->word == 0 || w->word[0] == '\0') | |
3561 | return ((char *)NULL); | |
3562 | ||
3b34f6e6 | 3563 | expand_no_split_dollar_star = 1; |
0001803f | 3564 | w->flags |= W_NOSPLIT2; |
b72432fd | 3565 | l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0); |
3b34f6e6 | 3566 | expand_no_split_dollar_star = 0; |
cce855bc JA |
3567 | if (l) |
3568 | { | |
ac50fbac | 3569 | if (special == 0) /* LHS */ |
cce855bc JA |
3570 | { |
3571 | dequote_list (l); | |
3572 | r = string_list (l); | |
3573 | } | |
3574 | else | |
28ef6c31 | 3575 | { |
ac50fbac CR |
3576 | /* Need to figure out whether or not we should call dequote_escapes |
3577 | or a new dequote_ctlnul function here, and under what | |
3578 | circumstances. */ | |
f1be666c JA |
3579 | qflags = QGLOB_CVTNULL; |
3580 | if (special == 2) | |
3581 | qflags |= QGLOB_REGEXP; | |
a0c0a00f | 3582 | word_list_remove_quoted_nulls (l); |
28ef6c31 | 3583 | p = string_list (l); |
f1be666c | 3584 | r = quote_string_for_globbing (p, qflags); |
28ef6c31 JA |
3585 | free (p); |
3586 | } | |
cce855bc JA |
3587 | dispose_words (l); |
3588 | } | |
3589 | else | |
3590 | r = (char *)NULL; | |
3591 | ||
3592 | return r; | |
3593 | } | |
3594 | #endif | |
3595 | ||
3596 | /* Call expand_word_internal to expand W and handle error returns. | |
3597 | A convenience function for functions that don't want to handle | |
3598 | any errors or free any memory before aborting. */ | |
3599 | static WORD_LIST * | |
b72432fd | 3600 | call_expand_word_internal (w, q, i, c, e) |
cce855bc | 3601 | WORD_DESC *w; |
b72432fd | 3602 | int q, i, *c, *e; |
cce855bc JA |
3603 | { |
3604 | WORD_LIST *result; | |
3605 | ||
b72432fd | 3606 | result = expand_word_internal (w, q, i, c, e); |
bb70624e | 3607 | if (result == &expand_word_error || result == &expand_word_fatal) |
cce855bc JA |
3608 | { |
3609 | /* By convention, each time this error is returned, w->word has | |
bb70624e JA |
3610 | already been freed (it sometimes may not be in the fatal case, |
3611 | but that doesn't result in a memory leak because we're going | |
3612 | to exit in most cases). */ | |
cce855bc | 3613 | w->word = (char *)NULL; |
28ef6c31 | 3614 | last_command_exit_value = EXECUTION_FAILURE; |
b80f6443 | 3615 | exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF); |
cce855bc | 3616 | /* NOTREACHED */ |
ac50fbac | 3617 | return (NULL); |
cce855bc | 3618 | } |
cce855bc JA |
3619 | else |
3620 | return (result); | |
3621 | } | |
3622 | ||
3623 | /* Perform parameter expansion, command substitution, and arithmetic | |
ac50fbac CR |
3624 | expansion on STRING, as if it were a word. Leave the result quoted. |
3625 | Since this does not perform word splitting, it leaves quoted nulls | |
3626 | in the result. */ | |
cce855bc JA |
3627 | static WORD_LIST * |
3628 | expand_string_internal (string, quoted) | |
3629 | char *string; | |
3630 | int quoted; | |
3631 | { | |
3632 | WORD_DESC td; | |
3633 | WORD_LIST *tresult; | |
3634 | ||
3635 | if (string == 0 || *string == 0) | |
3636 | return ((WORD_LIST *)NULL); | |
3637 | ||
28ef6c31 JA |
3638 | td.flags = 0; |
3639 | td.word = savestring (string); | |
3640 | ||
b72432fd | 3641 | tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); |
28ef6c31 JA |
3642 | |
3643 | FREE (td.word); | |
cce855bc | 3644 | return (tresult); |
726f6388 JA |
3645 | } |
3646 | ||
3647 | /* Expand STRING by performing parameter expansion, command substitution, | |
3648 | and arithmetic expansion. Dequote the resulting WORD_LIST before | |
3649 | returning it, but do not perform word splitting. The call to | |
3650 | remove_quoted_nulls () is in here because word splitting normally | |
3651 | takes care of quote removal. */ | |
3652 | WORD_LIST * | |
3653 | expand_string_unsplit (string, quoted) | |
3654 | char *string; | |
3655 | int quoted; | |
3656 | { | |
3657 | WORD_LIST *value; | |
3658 | ||
28ef6c31 | 3659 | if (string == 0 || *string == '\0') |
726f6388 JA |
3660 | return ((WORD_LIST *)NULL); |
3661 | ||
28ef6c31 | 3662 | expand_no_split_dollar_star = 1; |
726f6388 | 3663 | value = expand_string_internal (string, quoted); |
28ef6c31 JA |
3664 | expand_no_split_dollar_star = 0; |
3665 | ||
726f6388 JA |
3666 | if (value) |
3667 | { | |
3668 | if (value->word) | |
95732b49 JA |
3669 | { |
3670 | remove_quoted_nulls (value->word->word); | |
3671 | value->word->flags &= ~W_HASQUOTEDNULL; | |
3672 | } | |
3673 | dequote_list (value); | |
3674 | } | |
3675 | return (value); | |
3676 | } | |
3677 | ||
3678 | /* Expand the rhs of an assignment statement */ | |
3679 | WORD_LIST * | |
3680 | expand_string_assignment (string, quoted) | |
3681 | char *string; | |
3682 | int quoted; | |
3683 | { | |
3684 | WORD_DESC td; | |
3685 | WORD_LIST *value; | |
3686 | ||
3687 | if (string == 0 || *string == '\0') | |
3688 | return ((WORD_LIST *)NULL); | |
3689 | ||
3690 | expand_no_split_dollar_star = 1; | |
3691 | ||
3692 | td.flags = W_ASSIGNRHS; | |
3693 | td.word = savestring (string); | |
3694 | value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); | |
3695 | FREE (td.word); | |
3696 | ||
3697 | expand_no_split_dollar_star = 0; | |
3698 | ||
3699 | if (value) | |
3700 | { | |
3701 | if (value->word) | |
3702 | { | |
3703 | remove_quoted_nulls (value->word->word); | |
3704 | value->word->flags &= ~W_HASQUOTEDNULL; | |
3705 | } | |
726f6388 JA |
3706 | dequote_list (value); |
3707 | } | |
3708 | return (value); | |
3709 | } | |
3710 | ||
bb70624e JA |
3711 | |
3712 | /* Expand one of the PS? prompt strings. This is a sort of combination of | |
3713 | expand_string_unsplit and expand_string_internal, but returns the | |
3714 | passed string when an error occurs. Might want to trap other calls | |
3715 | to jump_to_top_level here so we don't endlessly loop. */ | |
3716 | WORD_LIST * | |
f1be666c | 3717 | expand_prompt_string (string, quoted, wflags) |
bb70624e JA |
3718 | char *string; |
3719 | int quoted; | |
f1be666c | 3720 | int wflags; |
bb70624e JA |
3721 | { |
3722 | WORD_LIST *value; | |
3723 | WORD_DESC td; | |
3724 | ||
3725 | if (string == 0 || *string == 0) | |
3726 | return ((WORD_LIST *)NULL); | |
3727 | ||
f1be666c | 3728 | td.flags = wflags; |
bb70624e | 3729 | td.word = savestring (string); |
28ef6c31 JA |
3730 | |
3731 | no_longjmp_on_fatal_error = 1; | |
bb70624e | 3732 | value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); |
28ef6c31 JA |
3733 | no_longjmp_on_fatal_error = 0; |
3734 | ||
bb70624e JA |
3735 | if (value == &expand_word_error || value == &expand_word_fatal) |
3736 | { | |
3737 | value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL); | |
3738 | return value; | |
3739 | } | |
3740 | FREE (td.word); | |
3741 | if (value) | |
3742 | { | |
3743 | if (value->word) | |
95732b49 JA |
3744 | { |
3745 | remove_quoted_nulls (value->word->word); | |
3746 | value->word->flags &= ~W_HASQUOTEDNULL; | |
3747 | } | |
bb70624e JA |
3748 | dequote_list (value); |
3749 | } | |
3750 | return (value); | |
3751 | } | |
3752 | ||
726f6388 JA |
3753 | /* Expand STRING just as if you were expanding a word, but do not dequote |
3754 | the resultant WORD_LIST. This is called only from within this file, | |
3755 | and is used to correctly preserve quoted characters when expanding | |
3756 | things like ${1+"$@"}. This does parameter expansion, command | |
b72432fd | 3757 | substitution, arithmetic expansion, and word splitting. */ |
726f6388 JA |
3758 | static WORD_LIST * |
3759 | expand_string_leave_quoted (string, quoted) | |
3760 | char *string; | |
3761 | int quoted; | |
3762 | { | |
3763 | WORD_LIST *tlist; | |
3764 | WORD_LIST *tresult; | |
3765 | ||
ccc6cda3 | 3766 | if (string == 0 || *string == '\0') |
726f6388 JA |
3767 | return ((WORD_LIST *)NULL); |
3768 | ||
3769 | tlist = expand_string_internal (string, quoted); | |
3770 | ||
3771 | if (tlist) | |
3772 | { | |
3773 | tresult = word_list_split (tlist); | |
3774 | dispose_words (tlist); | |
3775 | return (tresult); | |
3776 | } | |
3777 | return ((WORD_LIST *)NULL); | |
3778 | } | |
3779 | ||
ccc6cda3 JA |
3780 | /* This does not perform word splitting or dequote the WORD_LIST |
3781 | it returns. */ | |
3782 | static WORD_LIST * | |
a0c0a00f | 3783 | expand_string_for_rhs (string, quoted, dollar_at_p, expanded_p) |
ccc6cda3 | 3784 | char *string; |
a0c0a00f | 3785 | int quoted, *dollar_at_p, *expanded_p; |
ccc6cda3 JA |
3786 | { |
3787 | WORD_DESC td; | |
3788 | WORD_LIST *tresult; | |
3789 | ||
3790 | if (string == 0 || *string == '\0') | |
3791 | return (WORD_LIST *)NULL; | |
3792 | ||
a0c0a00f | 3793 | expand_no_split_dollar_star = 1; |
aeb26a67 | 3794 | td.flags = W_NOSPLIT2; /* no splitting, remove "" and '' */ |
ccc6cda3 | 3795 | td.word = string; |
a0c0a00f CR |
3796 | tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, expanded_p); |
3797 | expand_no_split_dollar_star = 0; | |
3798 | ||
ccc6cda3 JA |
3799 | return (tresult); |
3800 | } | |
3801 | ||
726f6388 JA |
3802 | /* Expand STRING just as if you were expanding a word. This also returns |
3803 | a list of words. Note that filename globbing is *NOT* done for word | |
3804 | or string expansion, just when the shell is expanding a command. This | |
3805 | does parameter expansion, command substitution, arithmetic expansion, | |
3806 | and word splitting. Dequote the resultant WORD_LIST before returning. */ | |
3807 | WORD_LIST * | |
3808 | expand_string (string, quoted) | |
3809 | char *string; | |
3810 | int quoted; | |
3811 | { | |
3812 | WORD_LIST *result; | |
3813 | ||
28ef6c31 | 3814 | if (string == 0 || *string == '\0') |
726f6388 JA |
3815 | return ((WORD_LIST *)NULL); |
3816 | ||
3817 | result = expand_string_leave_quoted (string, quoted); | |
ccc6cda3 | 3818 | return (result ? dequote_list (result) : result); |
726f6388 JA |
3819 | } |
3820 | ||
3821 | /*************************************************** | |
3822 | * * | |
3823 | * Functions to handle quoting chars * | |
3824 | * * | |
3825 | ***************************************************/ | |
3826 | ||
cce855bc JA |
3827 | /* Conventions: |
3828 | ||
3829 | A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string. | |
3830 | The parser passes CTLNUL as CTLESC CTLNUL. */ | |
3831 | ||
cce855bc JA |
3832 | /* Quote escape characters in string s, but no other characters. This is |
3833 | used to protect CTLESC and CTLNUL in variable values from the rest of | |
3185942a JA |
3834 | the word expansion process after the variable is expanded (word splitting |
3835 | and filename generation). If IFS is null, we quote spaces as well, just | |
3836 | in case we split on spaces later (in the case of unquoted $@, we will | |
3837 | eventually attempt to split the entire word on spaces). Corresponding | |
3838 | code exists in dequote_escapes. Even if we don't end up splitting on | |
3839 | spaces, quoting spaces is not a problem. This should never be called on | |
3840 | a string that is quoted with single or double quotes or part of a here | |
3841 | document (effectively double-quoted). */ | |
f73dda09 | 3842 | char * |
cce855bc JA |
3843 | quote_escapes (string) |
3844 | char *string; | |
3845 | { | |
3846 | register char *s, *t; | |
7117c2d2 JA |
3847 | size_t slen; |
3848 | char *result, *send; | |
3185942a | 3849 | int quote_spaces, skip_ctlesc, skip_ctlnul; |
7117c2d2 | 3850 | DECLARE_MBSTATE; |
cce855bc | 3851 | |
7117c2d2 JA |
3852 | slen = strlen (string); |
3853 | send = string + slen; | |
3854 | ||
f1be666c | 3855 | quote_spaces = (ifs_value && *ifs_value == 0); |
3185942a JA |
3856 | |
3857 | for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++) | |
3858 | skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL; | |
3859 | ||
7117c2d2 JA |
3860 | t = result = (char *)xmalloc ((slen * 2) + 1); |
3861 | s = string; | |
3862 | ||
3863 | while (*s) | |
cce855bc | 3864 | { |
3185942a | 3865 | if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' ')) |
cce855bc | 3866 | *t++ = CTLESC; |
7117c2d2 | 3867 | COPY_CHAR_P (t, s, send); |
cce855bc JA |
3868 | } |
3869 | *t = '\0'; | |
ac50fbac | 3870 | |
cce855bc JA |
3871 | return (result); |
3872 | } | |
3873 | ||
3874 | static WORD_LIST * | |
3875 | list_quote_escapes (list) | |
3876 | WORD_LIST *list; | |
3877 | { | |
3878 | register WORD_LIST *w; | |
3879 | char *t; | |
3880 | ||
3881 | for (w = list; w; w = w->next) | |
3882 | { | |
3883 | t = w->word->word; | |
3884 | w->word->word = quote_escapes (t); | |
3885 | free (t); | |
3886 | } | |
3887 | return list; | |
3888 | } | |
3889 | ||
7117c2d2 JA |
3890 | /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL. |
3891 | ||
3892 | The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL. | |
3893 | This is necessary to make unquoted CTLESC and CTLNUL characters in the | |
3894 | data stream pass through properly. | |
3895 | ||
3896 | We need to remove doubled CTLESC characters inside quoted strings before | |
3897 | quoting the entire string, so we do not double the number of CTLESC | |
3898 | characters. | |
3899 | ||
3900 | Also used by parts of the pattern substitution code. */ | |
3185942a | 3901 | char * |
cce855bc JA |
3902 | dequote_escapes (string) |
3903 | char *string; | |
3904 | { | |
3185942a | 3905 | register char *s, *t, *s1; |
7117c2d2 JA |
3906 | size_t slen; |
3907 | char *result, *send; | |
f1be666c | 3908 | int quote_spaces; |
7117c2d2 | 3909 | DECLARE_MBSTATE; |
cce855bc | 3910 | |
7117c2d2 JA |
3911 | if (string == 0) |
3912 | return string; | |
3913 | ||
3914 | slen = strlen (string); | |
3915 | send = string + slen; | |
3916 | ||
3917 | t = result = (char *)xmalloc (slen + 1); | |
7117c2d2 JA |
3918 | |
3919 | if (strchr (string, CTLESC) == 0) | |
3185942a | 3920 | return (strcpy (result, string)); |
7117c2d2 | 3921 | |
f1be666c | 3922 | quote_spaces = (ifs_value && *ifs_value == 0); |
3185942a JA |
3923 | |
3924 | s = string; | |
7117c2d2 | 3925 | while (*s) |
cce855bc | 3926 | { |
f1be666c | 3927 | if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' '))) |
cce855bc JA |
3928 | { |
3929 | s++; | |
3930 | if (*s == '\0') | |
3931 | break; | |
3932 | } | |
7117c2d2 | 3933 | COPY_CHAR_P (t, s, send); |
cce855bc JA |
3934 | } |
3935 | *t = '\0'; | |
ac50fbac | 3936 | |
cce855bc JA |
3937 | return result; |
3938 | } | |
726f6388 | 3939 | |
ac50fbac CR |
3940 | static WORD_LIST * |
3941 | list_dequote_escapes (list) | |
3942 | WORD_LIST *list; | |
3943 | { | |
3944 | register WORD_LIST *w; | |
3945 | char *t; | |
3946 | ||
3947 | for (w = list; w; w = w->next) | |
3948 | { | |
3949 | t = w->word->word; | |
3950 | w->word->word = dequote_escapes (t); | |
3951 | free (t); | |
3952 | } | |
3953 | return list; | |
3954 | } | |
3955 | ||
0628567a JA |
3956 | /* Return a new string with the quoted representation of character C. |
3957 | This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be | |
3958 | set in any resultant WORD_DESC where this value is the word. */ | |
726f6388 JA |
3959 | static char * |
3960 | make_quoted_char (c) | |
3961 | int c; | |
3962 | { | |
3963 | char *temp; | |
3964 | ||
f73dda09 | 3965 | temp = (char *)xmalloc (3); |
726f6388 JA |
3966 | if (c == 0) |
3967 | { | |
3968 | temp[0] = CTLNUL; | |
3969 | temp[1] = '\0'; | |
3970 | } | |
3971 | else | |
3972 | { | |
3973 | temp[0] = CTLESC; | |
3974 | temp[1] = c; | |
3975 | temp[2] = '\0'; | |
3976 | } | |
3977 | return (temp); | |
3978 | } | |
3979 | ||
0628567a JA |
3980 | /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so |
3981 | the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where | |
3982 | this value is the word. */ | |
ccc6cda3 | 3983 | char * |
726f6388 JA |
3984 | quote_string (string) |
3985 | char *string; | |
3986 | { | |
ccc6cda3 | 3987 | register char *t; |
7117c2d2 JA |
3988 | size_t slen; |
3989 | char *result, *send; | |
726f6388 | 3990 | |
ccc6cda3 | 3991 | if (*string == 0) |
726f6388 | 3992 | { |
f73dda09 | 3993 | result = (char *)xmalloc (2); |
726f6388 JA |
3994 | result[0] = CTLNUL; |
3995 | result[1] = '\0'; | |
3996 | } | |
3997 | else | |
3998 | { | |
7117c2d2 | 3999 | DECLARE_MBSTATE; |
726f6388 | 4000 | |
7117c2d2 JA |
4001 | slen = strlen (string); |
4002 | send = string + slen; | |
4003 | ||
4004 | result = (char *)xmalloc ((slen * 2) + 1); | |
4005 | ||
4006 | for (t = result; string < send; ) | |
726f6388 JA |
4007 | { |
4008 | *t++ = CTLESC; | |
7117c2d2 | 4009 | COPY_CHAR_P (t, string, send); |
726f6388 JA |
4010 | } |
4011 | *t = '\0'; | |
4012 | } | |
4013 | return (result); | |
4014 | } | |
4015 | ||
0628567a | 4016 | /* De-quote quoted characters in STRING. */ |
726f6388 JA |
4017 | char * |
4018 | dequote_string (string) | |
4019 | char *string; | |
4020 | { | |
7117c2d2 JA |
4021 | register char *s, *t; |
4022 | size_t slen; | |
4023 | char *result, *send; | |
4024 | DECLARE_MBSTATE; | |
726f6388 | 4025 | |
a0c0a00f CR |
4026 | #if defined (DEBUG) |
4027 | if (string[0] == CTLESC && string[1] == 0) | |
4028 | internal_inform ("dequote_string: string with bare CTLESC"); | |
4029 | #endif | |
4030 | ||
7117c2d2 JA |
4031 | slen = strlen (string); |
4032 | ||
4033 | t = result = (char *)xmalloc (slen + 1); | |
726f6388 JA |
4034 | |
4035 | if (QUOTED_NULL (string)) | |
4036 | { | |
4037 | result[0] = '\0'; | |
4038 | return (result); | |
4039 | } | |
4040 | ||
a0c0a00f CR |
4041 | /* A string consisting of only a single CTLESC should pass through unchanged */ |
4042 | if (string[0] == CTLESC && string[1] == 0) | |
4043 | { | |
4044 | result[0] = CTLESC; | |
4045 | result[1] = '\0'; | |
4046 | return (result); | |
4047 | } | |
4048 | ||
726f6388 JA |
4049 | /* If no character in the string can be quoted, don't bother examining |
4050 | each character. Just return a copy of the string passed to us. */ | |
7117c2d2 JA |
4051 | if (strchr (string, CTLESC) == NULL) |
4052 | return (strcpy (result, string)); | |
726f6388 | 4053 | |
7117c2d2 JA |
4054 | send = string + slen; |
4055 | s = string; | |
4056 | while (*s) | |
726f6388 | 4057 | { |
7117c2d2 | 4058 | if (*s == CTLESC) |
726f6388 | 4059 | { |
7117c2d2 JA |
4060 | s++; |
4061 | if (*s == '\0') | |
726f6388 JA |
4062 | break; |
4063 | } | |
7117c2d2 | 4064 | COPY_CHAR_P (t, s, send); |
726f6388 JA |
4065 | } |
4066 | ||
4067 | *t = '\0'; | |
4068 | return (result); | |
4069 | } | |
4070 | ||
4071 | /* Quote the entire WORD_LIST list. */ | |
ccc6cda3 | 4072 | static WORD_LIST * |
726f6388 JA |
4073 | quote_list (list) |
4074 | WORD_LIST *list; | |
4075 | { | |
4076 | register WORD_LIST *w; | |
ccc6cda3 | 4077 | char *t; |
726f6388 JA |
4078 | |
4079 | for (w = list; w; w = w->next) | |
4080 | { | |
ccc6cda3 | 4081 | t = w->word->word; |
726f6388 | 4082 | w->word->word = quote_string (t); |
3185942a JA |
4083 | if (*t == 0) |
4084 | w->word->flags |= W_HASQUOTEDNULL; /* XXX - turn on W_HASQUOTEDNULL here? */ | |
ccc6cda3 | 4085 | w->word->flags |= W_QUOTED; |
3185942a | 4086 | free (t); |
726f6388 | 4087 | } |
ccc6cda3 | 4088 | return list; |
726f6388 JA |
4089 | } |
4090 | ||
0628567a JA |
4091 | /* De-quote quoted characters in each word in LIST. */ |
4092 | WORD_LIST * | |
7117c2d2 JA |
4093 | dequote_list (list) |
4094 | WORD_LIST *list; | |
4095 | { | |
4096 | register char *s; | |
4097 | register WORD_LIST *tlist; | |
4098 | ||
4099 | for (tlist = list; tlist; tlist = tlist->next) | |
4100 | { | |
4101 | s = dequote_string (tlist->word->word); | |
3185942a JA |
4102 | if (QUOTED_NULL (tlist->word->word)) |
4103 | tlist->word->flags &= ~W_HASQUOTEDNULL; | |
7117c2d2 JA |
4104 | free (tlist->word->word); |
4105 | tlist->word->word = s; | |
4106 | } | |
4107 | return list; | |
4108 | } | |
4109 | ||
4110 | /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed | |
4111 | string. */ | |
3185942a | 4112 | char * |
7117c2d2 JA |
4113 | remove_quoted_escapes (string) |
4114 | char *string; | |
4115 | { | |
4116 | char *t; | |
4117 | ||
4118 | if (string) | |
4119 | { | |
4120 | t = dequote_escapes (string); | |
4121 | strcpy (string, t); | |
4122 | free (t); | |
4123 | } | |
4124 | ||
4125 | return (string); | |
4126 | } | |
4127 | ||
cce855bc JA |
4128 | /* Perform quoted null character removal on STRING. We don't allow any |
4129 | quoted null characters in the middle or at the ends of strings because | |
4130 | of how expand_word_internal works. remove_quoted_nulls () turns | |
4131 | STRING into an empty string iff it only consists of a quoted null, | |
4132 | and removes all unquoted CTLNUL characters. */ | |
3185942a | 4133 | char * |
cce855bc JA |
4134 | remove_quoted_nulls (string) |
4135 | char *string; | |
4136 | { | |
7117c2d2 JA |
4137 | register size_t slen; |
4138 | register int i, j, prev_i; | |
4139 | DECLARE_MBSTATE; | |
4140 | ||
4141 | if (strchr (string, CTLNUL) == 0) /* XXX */ | |
4142 | return string; /* XXX */ | |
4143 | ||
4144 | slen = strlen (string); | |
4145 | i = j = 0; | |
4146 | ||
4147 | while (i < slen) | |
4148 | { | |
4149 | if (string[i] == CTLESC) | |
b80f6443 JA |
4150 | { |
4151 | /* Old code had j++, but we cannot assume that i == j at this | |
4152 | point -- what if a CTLNUL has already been removed from the | |
4153 | string? We don't want to drop the CTLESC or recopy characters | |
4154 | that we've already copied down. */ | |
4155 | i++; string[j++] = CTLESC; | |
4156 | if (i == slen) | |
4157 | break; | |
4158 | } | |
7117c2d2 | 4159 | else if (string[i] == CTLNUL) |
a48a8ac3 CR |
4160 | { |
4161 | i++; | |
4162 | continue; | |
4163 | } | |
7117c2d2 JA |
4164 | |
4165 | prev_i = i; | |
4166 | ADVANCE_CHAR (string, slen, i); | |
4167 | if (j < prev_i) | |
cce855bc | 4168 | { |
7117c2d2 | 4169 | do string[j++] = string[prev_i++]; while (prev_i < i); |
cce855bc | 4170 | } |
7117c2d2 JA |
4171 | else |
4172 | j = i; | |
cce855bc | 4173 | } |
7117c2d2 JA |
4174 | string[j] = '\0'; |
4175 | ||
4176 | return (string); | |
cce855bc JA |
4177 | } |
4178 | ||
4179 | /* Perform quoted null character removal on each element of LIST. | |
4180 | This modifies LIST. */ | |
4181 | void | |
4182 | word_list_remove_quoted_nulls (list) | |
4183 | WORD_LIST *list; | |
4184 | { | |
4185 | register WORD_LIST *t; | |
4186 | ||
4187 | for (t = list; t; t = t->next) | |
95732b49 JA |
4188 | { |
4189 | remove_quoted_nulls (t->word->word); | |
4190 | t->word->flags &= ~W_HASQUOTEDNULL; | |
4191 | } | |
cce855bc JA |
4192 | } |
4193 | ||
4194 | /* **************************************************************** */ | |
4195 | /* */ | |
4196 | /* Functions for Matching and Removing Patterns */ | |
4197 | /* */ | |
4198 | /* **************************************************************** */ | |
4199 | ||
b80f6443 JA |
4200 | #if defined (HANDLE_MULTIBYTE) |
4201 | #if 0 /* Currently unused */ | |
4202 | static unsigned char * | |
4203 | mb_getcharlens (string, len) | |
4204 | char *string; | |
4205 | int len; | |
4206 | { | |
4207 | int i, offset, last; | |
4208 | unsigned char *ret; | |
4209 | char *p; | |
4210 | DECLARE_MBSTATE; | |
4211 | ||
4212 | i = offset = 0; | |
4213 | last = 0; | |
4214 | ret = (unsigned char *)xmalloc (len); | |
4215 | memset (ret, 0, len); | |
4216 | while (string[last]) | |
4217 | { | |
4218 | ADVANCE_CHAR (string, len, offset); | |
4219 | ret[last] = offset - last; | |
4220 | last = offset; | |
4221 | } | |
4222 | return ret; | |
4223 | } | |
4224 | #endif | |
4225 | #endif | |
4226 | ||
cce855bc JA |
4227 | /* Remove the portion of PARAM matched by PATTERN according to OP, where OP |
4228 | can have one of 4 values: | |
4229 | RP_LONG_LEFT remove longest matching portion at start of PARAM | |
726f6388 JA |
4230 | RP_SHORT_LEFT remove shortest matching portion at start of PARAM |
4231 | RP_LONG_RIGHT remove longest matching portion at end of PARAM | |
4232 | RP_SHORT_RIGHT remove shortest matching portion at end of PARAM | |
4233 | */ | |
4234 | ||
4235 | #define RP_LONG_LEFT 1 | |
4236 | #define RP_SHORT_LEFT 2 | |
4237 | #define RP_LONG_RIGHT 3 | |
4238 | #define RP_SHORT_RIGHT 4 | |
4239 | ||
495aee44 | 4240 | /* Returns its first argument if nothing matched; new memory otherwise */ |
726f6388 | 4241 | static char * |
b80f6443 | 4242 | remove_upattern (param, pattern, op) |
726f6388 JA |
4243 | char *param, *pattern; |
4244 | int op; | |
4245 | { | |
a0c0a00f | 4246 | register size_t len; |
ccc6cda3 | 4247 | register char *end; |
726f6388 JA |
4248 | register char *p, *ret, c; |
4249 | ||
ccc6cda3 JA |
4250 | len = STRLEN (param); |
4251 | end = param + len; | |
726f6388 JA |
4252 | |
4253 | switch (op) | |
4254 | { | |
4255 | case RP_LONG_LEFT: /* remove longest match at start */ | |
4256 | for (p = end; p >= param; p--) | |
4257 | { | |
4258 | c = *p; *p = '\0'; | |
f73dda09 | 4259 | if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
726f6388 JA |
4260 | { |
4261 | *p = c; | |
4262 | return (savestring (p)); | |
4263 | } | |
4264 | *p = c; | |
b80f6443 | 4265 | |
726f6388 JA |
4266 | } |
4267 | break; | |
4268 | ||
4269 | case RP_SHORT_LEFT: /* remove shortest match at start */ | |
4270 | for (p = param; p <= end; p++) | |
4271 | { | |
4272 | c = *p; *p = '\0'; | |
f73dda09 | 4273 | if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
726f6388 JA |
4274 | { |
4275 | *p = c; | |
4276 | return (savestring (p)); | |
4277 | } | |
4278 | *p = c; | |
4279 | } | |
4280 | break; | |
4281 | ||
ccc6cda3 JA |
4282 | case RP_LONG_RIGHT: /* remove longest match at end */ |
4283 | for (p = param; p <= end; p++) | |
4284 | { | |
f73dda09 | 4285 | if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
ccc6cda3 JA |
4286 | { |
4287 | c = *p; *p = '\0'; | |
4288 | ret = savestring (param); | |
4289 | *p = c; | |
4290 | return (ret); | |
4291 | } | |
4292 | } | |
4293 | break; | |
4294 | ||
4295 | case RP_SHORT_RIGHT: /* remove shortest match at end */ | |
4296 | for (p = end; p >= param; p--) | |
4297 | { | |
f73dda09 | 4298 | if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
ccc6cda3 JA |
4299 | { |
4300 | c = *p; *p = '\0'; | |
4301 | ret = savestring (param); | |
4302 | *p = c; | |
4303 | return (ret); | |
4304 | } | |
4305 | } | |
4306 | break; | |
4307 | } | |
b80f6443 | 4308 | |
495aee44 | 4309 | return (param); /* no match, return original string */ |
ccc6cda3 JA |
4310 | } |
4311 | ||
b80f6443 | 4312 | #if defined (HANDLE_MULTIBYTE) |
495aee44 | 4313 | /* Returns its first argument if nothing matched; new memory otherwise */ |
b80f6443 JA |
4314 | static wchar_t * |
4315 | remove_wpattern (wparam, wstrlen, wpattern, op) | |
4316 | wchar_t *wparam; | |
4317 | size_t wstrlen; | |
4318 | wchar_t *wpattern; | |
4319 | int op; | |
4320 | { | |
0628567a JA |
4321 | wchar_t wc, *ret; |
4322 | int n; | |
b80f6443 JA |
4323 | |
4324 | switch (op) | |
4325 | { | |
4326 | case RP_LONG_LEFT: /* remove longest match at start */ | |
4327 | for (n = wstrlen; n >= 0; n--) | |
4328 | { | |
4329 | wc = wparam[n]; wparam[n] = L'\0'; | |
4330 | if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4331 | { | |
4332 | wparam[n] = wc; | |
4333 | return (wcsdup (wparam + n)); | |
4334 | } | |
4335 | wparam[n] = wc; | |
4336 | } | |
4337 | break; | |
4338 | ||
4339 | case RP_SHORT_LEFT: /* remove shortest match at start */ | |
4340 | for (n = 0; n <= wstrlen; n++) | |
4341 | { | |
4342 | wc = wparam[n]; wparam[n] = L'\0'; | |
4343 | if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4344 | { | |
4345 | wparam[n] = wc; | |
4346 | return (wcsdup (wparam + n)); | |
4347 | } | |
4348 | wparam[n] = wc; | |
4349 | } | |
4350 | break; | |
4351 | ||
4352 | case RP_LONG_RIGHT: /* remove longest match at end */ | |
4353 | for (n = 0; n <= wstrlen; n++) | |
4354 | { | |
4355 | if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4356 | { | |
4357 | wc = wparam[n]; wparam[n] = L'\0'; | |
4358 | ret = wcsdup (wparam); | |
4359 | wparam[n] = wc; | |
4360 | return (ret); | |
4361 | } | |
4362 | } | |
4363 | break; | |
4364 | ||
4365 | case RP_SHORT_RIGHT: /* remove shortest match at end */ | |
4366 | for (n = wstrlen; n >= 0; n--) | |
4367 | { | |
4368 | if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4369 | { | |
4370 | wc = wparam[n]; wparam[n] = L'\0'; | |
4371 | ret = wcsdup (wparam); | |
4372 | wparam[n] = wc; | |
4373 | return (ret); | |
4374 | } | |
4375 | } | |
4376 | break; | |
4377 | } | |
4378 | ||
495aee44 | 4379 | return (wparam); /* no match, return original string */ |
b80f6443 JA |
4380 | } |
4381 | #endif /* HANDLE_MULTIBYTE */ | |
4382 | ||
4383 | static char * | |
4384 | remove_pattern (param, pattern, op) | |
4385 | char *param, *pattern; | |
4386 | int op; | |
4387 | { | |
495aee44 CR |
4388 | char *xret; |
4389 | ||
b80f6443 JA |
4390 | if (param == NULL) |
4391 | return (param); | |
4392 | if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */ | |
4393 | return (savestring (param)); | |
4394 | ||
4395 | #if defined (HANDLE_MULTIBYTE) | |
4396 | if (MB_CUR_MAX > 1) | |
4397 | { | |
4398 | wchar_t *ret, *oret; | |
4399 | size_t n; | |
4400 | wchar_t *wparam, *wpattern; | |
4401 | mbstate_t ps; | |
b80f6443 JA |
4402 | |
4403 | n = xdupmbstowcs (&wpattern, NULL, pattern); | |
4404 | if (n == (size_t)-1) | |
495aee44 CR |
4405 | { |
4406 | xret = remove_upattern (param, pattern, op); | |
4407 | return ((xret == param) ? savestring (param) : xret); | |
4408 | } | |
b80f6443 | 4409 | n = xdupmbstowcs (&wparam, NULL, param); |
ac50fbac | 4410 | |
b80f6443 JA |
4411 | if (n == (size_t)-1) |
4412 | { | |
4413 | free (wpattern); | |
495aee44 CR |
4414 | xret = remove_upattern (param, pattern, op); |
4415 | return ((xret == param) ? savestring (param) : xret); | |
b80f6443 JA |
4416 | } |
4417 | oret = ret = remove_wpattern (wparam, n, wpattern, op); | |
495aee44 CR |
4418 | /* Don't bother to convert wparam back to multibyte string if nothing |
4419 | matched; just return copy of original string */ | |
4420 | if (ret == wparam) | |
4421 | { | |
4422 | free (wparam); | |
4423 | free (wpattern); | |
4424 | return (savestring (param)); | |
4425 | } | |
b80f6443 JA |
4426 | |
4427 | free (wparam); | |
4428 | free (wpattern); | |
4429 | ||
4430 | n = strlen (param); | |
0628567a | 4431 | xret = (char *)xmalloc (n + 1); |
b80f6443 JA |
4432 | memset (&ps, '\0', sizeof (mbstate_t)); |
4433 | n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps); | |
4434 | xret[n] = '\0'; /* just to make sure */ | |
4435 | free (oret); | |
4436 | return xret; | |
4437 | } | |
4438 | else | |
4439 | #endif | |
ccc6cda3 | 4440 | { |
495aee44 CR |
4441 | xret = remove_upattern (param, pattern, op); |
4442 | return ((xret == param) ? savestring (param) : xret); | |
ccc6cda3 JA |
4443 | } |
4444 | } | |
4445 | ||
4446 | /* Match PAT anywhere in STRING and return the match boundaries. | |
4447 | This returns 1 in case of a successful match, 0 otherwise. SP | |
4448 | and EP are pointers into the string where the match begins and | |
4449 | ends, respectively. MTYPE controls what kind of match is attempted. | |
4450 | MATCH_BEG and MATCH_END anchor the match at the beginning and end | |
4451 | of the string, respectively. The longest match is returned. */ | |
4452 | static int | |
b80f6443 | 4453 | match_upattern (string, pat, mtype, sp, ep) |
ccc6cda3 JA |
4454 | char *string, *pat; |
4455 | int mtype; | |
4456 | char **sp, **ep; | |
4457 | { | |
a0c0a00f CR |
4458 | int c, mlen; |
4459 | size_t len; | |
95732b49 | 4460 | register char *p, *p1, *npat; |
ccc6cda3 | 4461 | char *end; |
495aee44 | 4462 | int n1; |
ccc6cda3 | 4463 | |
95732b49 JA |
4464 | /* If the pattern doesn't match anywhere in the string, go ahead and |
4465 | short-circuit right away. A minor optimization, saves a bunch of | |
4466 | unnecessary calls to strmatch (up to N calls for a string of N | |
4467 | characters) if the match is unsuccessful. To preserve the semantics | |
4468 | of the substring matches below, we make sure that the pattern has | |
4469 | `*' as first and last character, making a new pattern if necessary. */ | |
4470 | /* XXX - check this later if I ever implement `**' with special meaning, | |
4471 | since this will potentially result in `**' at the beginning or end */ | |
4472 | len = STRLEN (pat); | |
0001803f | 4473 | if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*') |
95732b49 | 4474 | { |
a0c0a00f CR |
4475 | int unescaped_backslash; |
4476 | char *pp; | |
4477 | ||
0628567a | 4478 | p = npat = (char *)xmalloc (len + 3); |
95732b49 | 4479 | p1 = pat; |
0001803f | 4480 | if (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob)) |
95732b49 JA |
4481 | *p++ = '*'; |
4482 | while (*p1) | |
4483 | *p++ = *p1++; | |
a0c0a00f CR |
4484 | #if 1 |
4485 | /* Need to also handle a pattern that ends with an unescaped backslash. | |
4486 | For right now, we ignore it because the pattern matching code will | |
4487 | fail the match anyway */ | |
4488 | /* If the pattern ends with a `*' we leave it alone if it's preceded by | |
4489 | an even number of backslashes, but if it's escaped by a backslash | |
4490 | we need to add another `*'. */ | |
4491 | if (p1[-1] == '*' && (unescaped_backslash = p1[-2] == '\\')) | |
4492 | { | |
4493 | pp = p1 - 3; | |
4494 | while (pp >= pat && *pp-- == '\\') | |
4495 | unescaped_backslash = 1 - unescaped_backslash; | |
4496 | if (unescaped_backslash) | |
4497 | *p++ = '*'; | |
4498 | } | |
4499 | else if (p1[-1] != '*') | |
95732b49 | 4500 | *p++ = '*'; |
a0c0a00f CR |
4501 | #else |
4502 | if (p1[-1] != '*' || p1[-2] == '\\') | |
4503 | *p++ = '*'; | |
4504 | #endif | |
95732b49 JA |
4505 | *p = '\0'; |
4506 | } | |
4507 | else | |
4508 | npat = pat; | |
a0c0a00f | 4509 | c = strmatch (npat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE); |
95732b49 JA |
4510 | if (npat != pat) |
4511 | free (npat); | |
4512 | if (c == FNM_NOMATCH) | |
4513 | return (0); | |
4514 | ||
b80f6443 JA |
4515 | len = STRLEN (string); |
4516 | end = string + len; | |
ccc6cda3 | 4517 | |
495aee44 CR |
4518 | mlen = umatchlen (pat, len); |
4519 | ||
ccc6cda3 JA |
4520 | switch (mtype) |
4521 | { | |
4522 | case MATCH_ANY: | |
4523 | for (p = string; p <= end; p++) | |
4524 | { | |
a0c0a00f | 4525 | if (match_pattern_char (pat, p, FNMATCH_IGNCASE)) |
ccc6cda3 | 4526 | { |
495aee44 CR |
4527 | p1 = (mlen == -1) ? end : p + mlen; |
4528 | /* p1 - p = length of portion of string to be considered | |
4529 | p = current position in string | |
4530 | mlen = number of characters consumed by match (-1 for entire string) | |
4531 | end = end of string | |
4532 | we want to break immediately if the potential match len | |
4533 | is greater than the number of characters remaining in the | |
4534 | string | |
4535 | */ | |
4536 | if (p1 > end) | |
4537 | break; | |
4538 | for ( ; p1 >= p; p1--) | |
ccc6cda3 JA |
4539 | { |
4540 | c = *p1; *p1 = '\0'; | |
a0c0a00f | 4541 | if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
ccc6cda3 JA |
4542 | { |
4543 | *p1 = c; | |
4544 | *sp = p; | |
4545 | *ep = p1; | |
4546 | return 1; | |
4547 | } | |
4548 | *p1 = c; | |
495aee44 CR |
4549 | #if 1 |
4550 | /* If MLEN != -1, we have a fixed length pattern. */ | |
4551 | if (mlen != -1) | |
4552 | break; | |
4553 | #endif | |
ccc6cda3 JA |
4554 | } |
4555 | } | |
4556 | } | |
b80f6443 | 4557 | |
ccc6cda3 JA |
4558 | return (0); |
4559 | ||
4560 | case MATCH_BEG: | |
a0c0a00f | 4561 | if (match_pattern_char (pat, string, FNMATCH_IGNCASE) == 0) |
28ef6c31 | 4562 | return (0); |
b80f6443 | 4563 | |
495aee44 | 4564 | for (p = (mlen == -1) ? end : string + mlen; p >= string; p--) |
ccc6cda3 JA |
4565 | { |
4566 | c = *p; *p = '\0'; | |
a0c0a00f | 4567 | if (strmatch (pat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
ccc6cda3 JA |
4568 | { |
4569 | *p = c; | |
4570 | *sp = string; | |
4571 | *ep = p; | |
4572 | return 1; | |
4573 | } | |
4574 | *p = c; | |
495aee44 CR |
4575 | /* If MLEN != -1, we have a fixed length pattern. */ |
4576 | if (mlen != -1) | |
4577 | break; | |
ccc6cda3 | 4578 | } |
b80f6443 | 4579 | |
ccc6cda3 | 4580 | return (0); |
726f6388 | 4581 | |
ccc6cda3 | 4582 | case MATCH_END: |
495aee44 | 4583 | for (p = end - ((mlen == -1) ? len : mlen); p <= end; p++) |
b80f6443 | 4584 | { |
a0c0a00f | 4585 | if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
4586 | { |
4587 | *sp = p; | |
4588 | *ep = end; | |
4589 | return 1; | |
4590 | } | |
495aee44 CR |
4591 | /* If MLEN != -1, we have a fixed length pattern. */ |
4592 | if (mlen != -1) | |
4593 | break; | |
b80f6443 JA |
4594 | } |
4595 | ||
4596 | return (0); | |
4597 | } | |
4598 | ||
4599 | return (0); | |
4600 | } | |
4601 | ||
4602 | #if defined (HANDLE_MULTIBYTE) | |
a0c0a00f CR |
4603 | |
4604 | #define WFOLD(c) (match_ignore_case && iswupper (c) ? towlower (c) : (c)) | |
4605 | ||
b80f6443 JA |
4606 | /* Match WPAT anywhere in WSTRING and return the match boundaries. |
4607 | This returns 1 in case of a successful match, 0 otherwise. Wide | |
4608 | character version. */ | |
4609 | static int | |
4610 | match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep) | |
4611 | wchar_t *wstring; | |
4612 | char **indices; | |
4613 | size_t wstrlen; | |
4614 | wchar_t *wpat; | |
4615 | int mtype; | |
4616 | char **sp, **ep; | |
4617 | { | |
95732b49 | 4618 | wchar_t wc, *wp, *nwpat, *wp1; |
495aee44 CR |
4619 | size_t len; |
4620 | int mlen; | |
4621 | int n, n1, n2, simple; | |
4622 | ||
4623 | simple = (wpat[0] != L'\\' && wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'['); | |
4624 | #if defined (EXTENDED_GLOB) | |
4625 | if (extended_glob) | |
91717ba3 | 4626 | simple &= (wpat[1] != L'(' || (wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'+' && wpat[0] != L'!' && wpat[0] != L'@')); /*)*/ |
b80f6443 JA |
4627 | #endif |
4628 | ||
95732b49 JA |
4629 | /* If the pattern doesn't match anywhere in the string, go ahead and |
4630 | short-circuit right away. A minor optimization, saves a bunch of | |
4631 | unnecessary calls to strmatch (up to N calls for a string of N | |
4632 | characters) if the match is unsuccessful. To preserve the semantics | |
4633 | of the substring matches below, we make sure that the pattern has | |
4634 | `*' as first and last character, making a new pattern if necessary. */ | |
95732b49 | 4635 | len = wcslen (wpat); |
0001803f | 4636 | if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*') |
95732b49 | 4637 | { |
a0c0a00f CR |
4638 | int unescaped_backslash; |
4639 | wchar_t *wpp; | |
4640 | ||
0628567a | 4641 | wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t)); |
95732b49 | 4642 | wp1 = wpat; |
0001803f | 4643 | if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob)) |
95732b49 JA |
4644 | *wp++ = L'*'; |
4645 | while (*wp1 != L'\0') | |
4646 | *wp++ = *wp1++; | |
a0c0a00f CR |
4647 | #if 1 |
4648 | /* See comments above in match_upattern. */ | |
4649 | if (wp1[-1] == L'*' && (unescaped_backslash = wp1[-2] == L'\\')) | |
4650 | { | |
4651 | wpp = wp1 - 3; | |
4652 | while (wpp >= wpat && *wpp-- == L'\\') | |
4653 | unescaped_backslash = 1 - unescaped_backslash; | |
4654 | if (unescaped_backslash) | |
4655 | *wp++ = L'*'; | |
4656 | } | |
4657 | else if (wp1[-1] != L'*') | |
4658 | *wp++ = L'*'; | |
4659 | #else | |
95732b49 JA |
4660 | if (wp1[-1] != L'*' || wp1[-2] == L'\\') |
4661 | *wp++ = L'*'; | |
a0c0a00f | 4662 | #endif |
95732b49 JA |
4663 | *wp = '\0'; |
4664 | } | |
4665 | else | |
4666 | nwpat = wpat; | |
a0c0a00f | 4667 | len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE); |
95732b49 JA |
4668 | if (nwpat != wpat) |
4669 | free (nwpat); | |
4670 | if (len == FNM_NOMATCH) | |
4671 | return (0); | |
4672 | ||
495aee44 CR |
4673 | mlen = wmatchlen (wpat, wstrlen); |
4674 | ||
4675 | /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */ | |
b80f6443 JA |
4676 | switch (mtype) |
4677 | { | |
4678 | case MATCH_ANY: | |
4679 | for (n = 0; n <= wstrlen; n++) | |
4680 | { | |
a0c0a00f | 4681 | n2 = simple ? (WFOLD(*wpat) == WFOLD(wstring[n])) : match_pattern_wchar (wpat, wstring + n, FNMATCH_IGNCASE); |
495aee44 | 4682 | if (n2) |
b80f6443 | 4683 | { |
495aee44 CR |
4684 | n1 = (mlen == -1) ? wstrlen : n + mlen; |
4685 | if (n1 > wstrlen) | |
4686 | break; | |
4687 | ||
4688 | for ( ; n1 >= n; n1--) | |
b80f6443 JA |
4689 | { |
4690 | wc = wstring[n1]; wstring[n1] = L'\0'; | |
a0c0a00f | 4691 | if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
4692 | { |
4693 | wstring[n1] = wc; | |
4694 | *sp = indices[n]; | |
4695 | *ep = indices[n1]; | |
4696 | return 1; | |
4697 | } | |
4698 | wstring[n1] = wc; | |
495aee44 CR |
4699 | /* If MLEN != -1, we have a fixed length pattern. */ |
4700 | if (mlen != -1) | |
4701 | break; | |
b80f6443 JA |
4702 | } |
4703 | } | |
4704 | } | |
4705 | ||
4706 | return (0); | |
4707 | ||
4708 | case MATCH_BEG: | |
a0c0a00f | 4709 | if (match_pattern_wchar (wpat, wstring, FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
4710 | return (0); |
4711 | ||
495aee44 | 4712 | for (n = (mlen == -1) ? wstrlen : mlen; n >= 0; n--) |
b80f6443 JA |
4713 | { |
4714 | wc = wstring[n]; wstring[n] = L'\0'; | |
a0c0a00f | 4715 | if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
4716 | { |
4717 | wstring[n] = wc; | |
4718 | *sp = indices[0]; | |
4719 | *ep = indices[n]; | |
4720 | return 1; | |
4721 | } | |
4722 | wstring[n] = wc; | |
495aee44 CR |
4723 | /* If MLEN != -1, we have a fixed length pattern. */ |
4724 | if (mlen != -1) | |
4725 | break; | |
b80f6443 JA |
4726 | } |
4727 | ||
4728 | return (0); | |
4729 | ||
4730 | case MATCH_END: | |
495aee44 | 4731 | for (n = wstrlen - ((mlen == -1) ? wstrlen : mlen); n <= wstrlen; n++) |
b80f6443 | 4732 | { |
a0c0a00f | 4733 | if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
4734 | { |
4735 | *sp = indices[n]; | |
4736 | *ep = indices[wstrlen]; | |
4737 | return 1; | |
4738 | } | |
495aee44 CR |
4739 | /* If MLEN != -1, we have a fixed length pattern. */ |
4740 | if (mlen != -1) | |
4741 | break; | |
b80f6443 JA |
4742 | } |
4743 | ||
ccc6cda3 | 4744 | return (0); |
726f6388 | 4745 | } |
ccc6cda3 JA |
4746 | |
4747 | return (0); | |
726f6388 | 4748 | } |
a0c0a00f | 4749 | #undef WFOLD |
b80f6443 JA |
4750 | #endif /* HANDLE_MULTIBYTE */ |
4751 | ||
4752 | static int | |
4753 | match_pattern (string, pat, mtype, sp, ep) | |
4754 | char *string, *pat; | |
4755 | int mtype; | |
4756 | char **sp, **ep; | |
4757 | { | |
4758 | #if defined (HANDLE_MULTIBYTE) | |
4759 | int ret; | |
4760 | size_t n; | |
4761 | wchar_t *wstring, *wpat; | |
4762 | char **indices; | |
495aee44 | 4763 | size_t slen, plen, mslen, mplen; |
b80f6443 JA |
4764 | #endif |
4765 | ||
a0c0a00f | 4766 | if (string == 0 || pat == 0 || *pat == 0) |
b80f6443 JA |
4767 | return (0); |
4768 | ||
4769 | #if defined (HANDLE_MULTIBYTE) | |
4770 | if (MB_CUR_MAX > 1) | |
4771 | { | |
495aee44 | 4772 | if (mbsmbchar (string) == 0 && mbsmbchar (pat) == 0) |
495aee44 CR |
4773 | return (match_upattern (string, pat, mtype, sp, ep)); |
4774 | ||
b80f6443 JA |
4775 | n = xdupmbstowcs (&wpat, NULL, pat); |
4776 | if (n == (size_t)-1) | |
4777 | return (match_upattern (string, pat, mtype, sp, ep)); | |
4778 | n = xdupmbstowcs (&wstring, &indices, string); | |
4779 | if (n == (size_t)-1) | |
4780 | { | |
4781 | free (wpat); | |
4782 | return (match_upattern (string, pat, mtype, sp, ep)); | |
4783 | } | |
4784 | ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep); | |
4785 | ||
4786 | free (wpat); | |
4787 | free (wstring); | |
4788 | free (indices); | |
4789 | ||
4790 | return (ret); | |
4791 | } | |
4792 | else | |
4793 | #endif | |
4794 | return (match_upattern (string, pat, mtype, sp, ep)); | |
4795 | } | |
726f6388 | 4796 | |
cce855bc JA |
4797 | static int |
4798 | getpatspec (c, value) | |
4799 | int c; | |
4800 | char *value; | |
4801 | { | |
4802 | if (c == '#') | |
4803 | return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT); | |
4804 | else /* c == '%' */ | |
4805 | return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT); | |
4806 | } | |
4807 | ||
4808 | /* Posix.2 says that the WORD should be run through tilde expansion, | |
4809 | parameter expansion, command substitution and arithmetic expansion. | |
4810 | This leaves the result quoted, so quote_string_for_globbing () has | |
f73dda09 | 4811 | to be called to fix it up for strmatch (). If QUOTED is non-zero, |
cce855bc JA |
4812 | it means that the entire expression was enclosed in double quotes. |
4813 | This means that quoting characters in the pattern do not make any | |
4814 | special pattern characters quoted. For example, the `*' in the | |
4815 | following retains its special meaning: "${foo#'*'}". */ | |
4816 | static char * | |
4817 | getpattern (value, quoted, expandpat) | |
4818 | char *value; | |
4819 | int quoted, expandpat; | |
4820 | { | |
4821 | char *pat, *tword; | |
4822 | WORD_LIST *l; | |
0628567a | 4823 | #if 0 |
cce855bc | 4824 | int i; |
0628567a | 4825 | #endif |
7117c2d2 JA |
4826 | /* There is a problem here: how to handle single or double quotes in the |
4827 | pattern string when the whole expression is between double quotes? | |
4828 | POSIX.2 says that enclosing double quotes do not cause the pattern to | |
4829 | be quoted, but does that leave us a problem with @ and array[@] and their | |
4830 | expansions inside a pattern? */ | |
4831 | #if 0 | |
cce855bc JA |
4832 | if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword) |
4833 | { | |
4834 | i = 0; | |
a0c0a00f | 4835 | pat = string_extract_double_quoted (tword, &i, SX_STRIPDQ); |
cce855bc JA |
4836 | free (tword); |
4837 | tword = pat; | |
4838 | } | |
7117c2d2 | 4839 | #endif |
cce855bc | 4840 | |
7117c2d2 JA |
4841 | /* expand_string_for_rhs () leaves WORD quoted and does not perform |
4842 | word splitting. */ | |
95732b49 | 4843 | l = *value ? expand_string_for_rhs (value, |
7117c2d2 | 4844 | (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted, |
cce855bc | 4845 | (int *)NULL, (int *)NULL) |
cce855bc | 4846 | : (WORD_LIST *)0; |
cce855bc JA |
4847 | pat = string_list (l); |
4848 | dispose_words (l); | |
4849 | if (pat) | |
4850 | { | |
4851 | tword = quote_string_for_globbing (pat, QGLOB_CVTNULL); | |
4852 | free (pat); | |
4853 | pat = tword; | |
4854 | } | |
4855 | return (pat); | |
4856 | } | |
4857 | ||
7117c2d2 | 4858 | #if 0 |
cce855bc JA |
4859 | /* Handle removing a pattern from a string as a result of ${name%[%]value} |
4860 | or ${name#[#]value}. */ | |
4861 | static char * | |
7117c2d2 JA |
4862 | variable_remove_pattern (value, pattern, patspec, quoted) |
4863 | char *value, *pattern; | |
4864 | int patspec, quoted; | |
cce855bc | 4865 | { |
7117c2d2 | 4866 | char *tword; |
cce855bc | 4867 | |
7117c2d2 | 4868 | tword = remove_pattern (value, pattern, patspec); |
cce855bc | 4869 | |
cce855bc JA |
4870 | return (tword); |
4871 | } | |
a0c0a00f CR |
4872 | #endif |
4873 | ||
4874 | static char * | |
4875 | list_remove_pattern (list, pattern, patspec, itype, quoted) | |
4876 | WORD_LIST *list; | |
4877 | char *pattern; | |
4878 | int patspec, itype, quoted; | |
4879 | { | |
4880 | WORD_LIST *new, *l; | |
4881 | WORD_DESC *w; | |
4882 | char *tword; | |
4883 | ||
4884 | for (new = (WORD_LIST *)NULL, l = list; l; l = l->next) | |
4885 | { | |
4886 | tword = remove_pattern (l->word->word, pattern, patspec); | |
4887 | w = alloc_word_desc (); | |
4888 | w->word = tword ? tword : savestring (""); | |
4889 | new = make_word_list (w, new); | |
4890 | } | |
4891 | ||
4892 | l = REVERSE_LIST (new, WORD_LIST *); | |
4893 | tword = string_list_pos_params (itype, l, quoted); | |
4894 | dispose_words (l); | |
4895 | ||
4896 | return (tword); | |
4897 | } | |
4898 | ||
4899 | static char * | |
4900 | parameter_list_remove_pattern (itype, pattern, patspec, quoted) | |
4901 | int itype; | |
4902 | char *pattern; | |
4903 | int patspec, quoted; | |
4904 | { | |
4905 | char *ret; | |
4906 | WORD_LIST *list; | |
4907 | ||
4908 | list = list_rest_of_args (); | |
4909 | if (list == 0) | |
4910 | return ((char *)NULL); | |
4911 | ret = list_remove_pattern (list, pattern, patspec, itype, quoted); | |
4912 | dispose_words (list); | |
4913 | return (ret); | |
4914 | } | |
4915 | ||
4916 | #if defined (ARRAY_VARS) | |
4917 | static char * | |
4918 | array_remove_pattern (var, pattern, patspec, varname, quoted) | |
4919 | SHELL_VAR *var; | |
4920 | char *pattern; | |
4921 | int patspec; | |
4922 | char *varname; /* so we can figure out how it's indexed */ | |
4923 | int quoted; | |
4924 | { | |
4925 | ARRAY *a; | |
4926 | HASH_TABLE *h; | |
4927 | int itype; | |
4928 | char *ret; | |
4929 | WORD_LIST *list; | |
4930 | SHELL_VAR *v; | |
4931 | ||
4932 | /* compute itype from varname here */ | |
4933 | v = array_variable_part (varname, &ret, 0); | |
4934 | ||
4935 | /* XXX */ | |
4936 | if (v && invisible_p (v)) | |
4937 | return ((char *)NULL); | |
4938 | ||
4939 | itype = ret[0]; | |
4940 | ||
4941 | a = (v && array_p (v)) ? array_cell (v) : 0; | |
4942 | h = (v && assoc_p (v)) ? assoc_cell (v) : 0; | |
4943 | ||
4944 | list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0); | |
4945 | if (list == 0) | |
4946 | return ((char *)NULL); | |
4947 | ret = list_remove_pattern (list, pattern, patspec, itype, quoted); | |
4948 | dispose_words (list); | |
4949 | ||
4950 | return ret; | |
4951 | } | |
4952 | #endif /* ARRAY_VARS */ | |
4953 | ||
4954 | static char * | |
4955 | parameter_brace_remove_pattern (varname, value, ind, patstr, rtype, quoted, flags) | |
4956 | char *varname, *value; | |
4957 | int ind; | |
4958 | char *patstr; | |
4959 | int rtype, quoted, flags; | |
4960 | { | |
4961 | int vtype, patspec, starsub; | |
4962 | char *temp1, *val, *pattern; | |
4963 | SHELL_VAR *v; | |
4964 | ||
4965 | if (value == 0) | |
4966 | return ((char *)NULL); | |
4967 | ||
4968 | this_command_name = varname; | |
4969 | ||
4970 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); | |
4971 | if (vtype == -1) | |
4972 | return ((char *)NULL); | |
4973 | ||
4974 | starsub = vtype & VT_STARSUB; | |
4975 | vtype &= ~VT_STARSUB; | |
4976 | ||
4977 | patspec = getpatspec (rtype, patstr); | |
4978 | if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT) | |
4979 | patstr++; | |
4980 | ||
4981 | /* Need to pass getpattern newly-allocated memory in case of expansion -- | |
4982 | the expansion code will free the passed string on an error. */ | |
4983 | temp1 = savestring (patstr); | |
4984 | pattern = getpattern (temp1, quoted, 1); | |
4985 | free (temp1); | |
4986 | ||
4987 | temp1 = (char *)NULL; /* shut up gcc */ | |
4988 | switch (vtype) | |
4989 | { | |
4990 | case VT_VARIABLE: | |
4991 | case VT_ARRAYMEMBER: | |
4992 | temp1 = remove_pattern (val, pattern, patspec); | |
4993 | if (vtype == VT_VARIABLE) | |
4994 | FREE (val); | |
4995 | if (temp1) | |
4996 | { | |
4997 | val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
4998 | ? quote_string (temp1) | |
4999 | : quote_escapes (temp1); | |
5000 | free (temp1); | |
5001 | temp1 = val; | |
5002 | } | |
5003 | break; | |
5004 | #if defined (ARRAY_VARS) | |
5005 | case VT_ARRAYVAR: | |
5006 | temp1 = array_remove_pattern (v, pattern, patspec, varname, quoted); | |
5007 | if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) | |
5008 | { | |
5009 | val = quote_escapes (temp1); | |
5010 | free (temp1); | |
5011 | temp1 = val; | |
5012 | } | |
5013 | break; | |
5014 | #endif | |
5015 | case VT_POSPARMS: | |
5016 | temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted); | |
5017 | if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) | |
5018 | { | |
5019 | val = quote_escapes (temp1); | |
5020 | free (temp1); | |
5021 | temp1 = val; | |
5022 | } | |
5023 | break; | |
5024 | } | |
5025 | ||
5026 | FREE (pattern); | |
5027 | return temp1; | |
5028 | } | |
5029 | ||
5030 | static char * | |
5031 | string_var_assignment (v, s) | |
5032 | SHELL_VAR *v; | |
5033 | char *s; | |
5034 | { | |
5035 | char flags[MAX_ATTRIBUTES], *ret, *val; | |
5036 | int i; | |
5037 | ||
5038 | val = sh_quote_reusable (s, 0); | |
5039 | i = var_attribute_string (v, 0, flags); | |
5040 | ret = (char *)xmalloc (i + strlen (val) + strlen (v->name) + 16 + MAX_ATTRIBUTES); | |
5041 | if (i > 0) | |
5042 | sprintf (ret, "declare -%s %s=%s", flags, v->name, val); | |
5043 | else | |
5044 | sprintf (ret, "%s=%s", v->name, val); | |
5045 | free (val); | |
5046 | return ret; | |
5047 | } | |
5048 | ||
5049 | #if defined (ARRAY_VARS) | |
5050 | static char * | |
5051 | array_var_assignment (v, itype, quoted) | |
5052 | SHELL_VAR *v; | |
5053 | int itype, quoted; | |
5054 | { | |
5055 | char *ret, *val, flags[MAX_ATTRIBUTES]; | |
5056 | int i; | |
5057 | ||
5058 | if (v == 0) | |
5059 | return (char *)NULL; | |
5060 | val = array_p (v) ? array_to_assign (array_cell (v), 0) | |
5061 | : assoc_to_assign (assoc_cell (v), 0); | |
5062 | if (val == 0) | |
5063 | { | |
5064 | val = (char *)xmalloc (3); | |
5065 | val[0] = '('; | |
5066 | val[1] = ')'; | |
5067 | val[2] = 0; | |
5068 | } | |
5069 | else | |
5070 | { | |
5071 | ret = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) ? quote_string (val) : quote_escapes (val); | |
5072 | free (val); | |
5073 | val = ret; | |
5074 | } | |
5075 | i = var_attribute_string (v, 0, flags); | |
5076 | ret = (char *)xmalloc (i + strlen (val) + strlen (v->name) + 16); | |
5077 | sprintf (ret, "declare -%s %s=%s", flags, v->name, val); | |
5078 | free (val); | |
5079 | return ret; | |
5080 | } | |
5081 | #endif | |
5082 | ||
5083 | static char * | |
5084 | pos_params_assignment (list, itype, quoted) | |
5085 | WORD_LIST *list; | |
5086 | int itype; | |
5087 | int quoted; | |
5088 | { | |
5089 | char *temp, *ret; | |
5090 | ||
5091 | /* first, we transform the list to quote each word. */ | |
5092 | temp = list_transform ('Q', (SHELL_VAR *)0, list, itype, quoted); | |
5093 | ret = (char *)xmalloc (strlen (temp) + 8); | |
5094 | strcpy (ret, "set -- "); | |
5095 | strcpy (ret + 7, temp); | |
5096 | free (temp); | |
5097 | return ret; | |
5098 | } | |
5099 | ||
5100 | static char * | |
5101 | string_transform (xc, v, s) | |
5102 | int xc; | |
5103 | SHELL_VAR *v; | |
5104 | char *s; | |
5105 | { | |
5106 | char *ret, flags[MAX_ATTRIBUTES]; | |
5107 | int i; | |
5108 | ||
5109 | if (((xc == 'A' || xc == 'a') && v == 0) || (xc != 'a' && s == 0)) | |
5110 | return (char *)NULL; | |
5111 | ||
5112 | switch (xc) | |
5113 | { | |
5114 | /* Transformations that interrogate the variable */ | |
5115 | case 'a': | |
5116 | i = var_attribute_string (v, 0, flags); | |
5117 | ret = (i > 0) ? savestring (flags) : (char *)NULL; | |
5118 | break; | |
5119 | case 'A': | |
5120 | ret = string_var_assignment (v, s); | |
5121 | break; | |
5122 | /* Transformations that modify the variable's value */ | |
5123 | case 'E': | |
5124 | ret = ansiexpand (s, 0, strlen (s), (int *)0); | |
5125 | break; | |
5126 | case 'P': | |
5127 | ret = decode_prompt_string (s); | |
5128 | break; | |
5129 | case 'Q': | |
5130 | ret = sh_quote_reusable (s, 0); | |
5131 | break; | |
5132 | default: | |
5133 | ret = (char *)NULL; | |
5134 | break; | |
5135 | } | |
5136 | return ret; | |
5137 | } | |
cce855bc JA |
5138 | |
5139 | static char * | |
a0c0a00f CR |
5140 | list_transform (xc, v, list, itype, quoted) |
5141 | int xc; | |
5142 | SHELL_VAR *v; | |
cce855bc | 5143 | WORD_LIST *list; |
a0c0a00f | 5144 | int itype, quoted; |
cce855bc JA |
5145 | { |
5146 | WORD_LIST *new, *l; | |
5147 | WORD_DESC *w; | |
5148 | char *tword; | |
5149 | ||
5150 | for (new = (WORD_LIST *)NULL, l = list; l; l = l->next) | |
5151 | { | |
a0c0a00f | 5152 | tword = string_transform (xc, v, l->word->word); |
95732b49 | 5153 | w = alloc_word_desc (); |
a0c0a00f | 5154 | w->word = tword ? tword : savestring (""); /* XXX */ |
cce855bc JA |
5155 | new = make_word_list (w, new); |
5156 | } | |
5157 | ||
5158 | l = REVERSE_LIST (new, WORD_LIST *); | |
3185942a | 5159 | tword = string_list_pos_params (itype, l, quoted); |
cce855bc | 5160 | dispose_words (l); |
3185942a | 5161 | |
cce855bc JA |
5162 | return (tword); |
5163 | } | |
5164 | ||
5165 | static char * | |
a0c0a00f CR |
5166 | parameter_list_transform (xc, itype, quoted) |
5167 | int xc; | |
7117c2d2 | 5168 | int itype; |
a0c0a00f | 5169 | int quoted; |
cce855bc | 5170 | { |
7117c2d2 | 5171 | char *ret; |
cce855bc JA |
5172 | WORD_LIST *list; |
5173 | ||
cce855bc | 5174 | list = list_rest_of_args (); |
7117c2d2 JA |
5175 | if (list == 0) |
5176 | return ((char *)NULL); | |
a0c0a00f CR |
5177 | if (xc == 'A') |
5178 | return (pos_params_assignment (list, itype, quoted)); | |
5179 | ret = list_transform (xc, (SHELL_VAR *)0, list, itype, quoted); | |
cce855bc | 5180 | dispose_words (list); |
cce855bc JA |
5181 | return (ret); |
5182 | } | |
5183 | ||
5184 | #if defined (ARRAY_VARS) | |
5185 | static char * | |
a0c0a00f CR |
5186 | array_transform (xc, var, varname, quoted) |
5187 | int xc; | |
3185942a | 5188 | SHELL_VAR *var; |
7117c2d2 JA |
5189 | char *varname; /* so we can figure out how it's indexed */ |
5190 | int quoted; | |
cce855bc | 5191 | { |
3185942a JA |
5192 | ARRAY *a; |
5193 | HASH_TABLE *h; | |
7117c2d2 JA |
5194 | int itype; |
5195 | char *ret; | |
5196 | WORD_LIST *list; | |
5197 | SHELL_VAR *v; | |
cce855bc | 5198 | |
7117c2d2 JA |
5199 | /* compute itype from varname here */ |
5200 | v = array_variable_part (varname, &ret, 0); | |
ac50fbac CR |
5201 | |
5202 | /* XXX */ | |
a0c0a00f | 5203 | if (v && invisible_p (v)) |
ac50fbac CR |
5204 | return ((char *)NULL); |
5205 | ||
7117c2d2 JA |
5206 | itype = ret[0]; |
5207 | ||
a0c0a00f CR |
5208 | if (xc == 'A') |
5209 | return (array_var_assignment (v, itype, quoted)); | |
5210 | ||
3185942a JA |
5211 | a = (v && array_p (v)) ? array_cell (v) : 0; |
5212 | h = (v && assoc_p (v)) ? assoc_cell (v) : 0; | |
5213 | ||
5214 | list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0); | |
7117c2d2 JA |
5215 | if (list == 0) |
5216 | return ((char *)NULL); | |
a0c0a00f | 5217 | ret = list_transform (xc, v, list, itype, quoted); |
7117c2d2 JA |
5218 | dispose_words (list); |
5219 | ||
5220 | return ret; | |
5221 | } | |
5222 | #endif /* ARRAY_VARS */ | |
5223 | ||
5224 | static char * | |
a0c0a00f | 5225 | parameter_brace_transform (varname, value, ind, xform, rtype, quoted, flags) |
495aee44 CR |
5226 | char *varname, *value; |
5227 | int ind; | |
a0c0a00f | 5228 | char *xform; |
495aee44 | 5229 | int rtype, quoted, flags; |
7117c2d2 | 5230 | { |
a0c0a00f CR |
5231 | int vtype, xc; |
5232 | char *temp1, *val; | |
7117c2d2 JA |
5233 | SHELL_VAR *v; |
5234 | ||
a0c0a00f CR |
5235 | xc = xform[0]; |
5236 | if (value == 0 && xc != 'A' && xc != 'a') | |
7117c2d2 JA |
5237 | return ((char *)NULL); |
5238 | ||
5239 | this_command_name = varname; | |
5240 | ||
495aee44 | 5241 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
7117c2d2 | 5242 | if (vtype == -1) |
cce855bc JA |
5243 | return ((char *)NULL); |
5244 | ||
a0c0a00f CR |
5245 | /* check for valid values of xc */ |
5246 | switch (xc) | |
5247 | { | |
5248 | case 'a': /* expand to a string with just attributes */ | |
5249 | case 'A': /* expand as an assignment statement with attributes */ | |
5250 | case 'E': /* expand like $'...' */ | |
5251 | case 'P': /* expand like prompt string */ | |
5252 | case 'Q': /* quote reusably */ | |
5253 | break; | |
5254 | default: | |
5255 | return &expand_param_error; | |
5256 | } | |
cce855bc | 5257 | |
7117c2d2 | 5258 | temp1 = (char *)NULL; /* shut up gcc */ |
a0c0a00f | 5259 | switch (vtype & ~VT_STARSUB) |
cce855bc | 5260 | { |
7117c2d2 JA |
5261 | case VT_VARIABLE: |
5262 | case VT_ARRAYMEMBER: | |
a0c0a00f | 5263 | temp1 = string_transform (xc, v, val); |
7117c2d2 JA |
5264 | if (vtype == VT_VARIABLE) |
5265 | FREE (val); | |
5266 | if (temp1) | |
28ef6c31 | 5267 | { |
3185942a JA |
5268 | val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
5269 | ? quote_string (temp1) | |
5270 | : quote_escapes (temp1); | |
7117c2d2 JA |
5271 | free (temp1); |
5272 | temp1 = val; | |
28ef6c31 | 5273 | } |
7117c2d2 JA |
5274 | break; |
5275 | #if defined (ARRAY_VARS) | |
5276 | case VT_ARRAYVAR: | |
a0c0a00f | 5277 | temp1 = array_transform (xc, v, varname, quoted); |
7117c2d2 | 5278 | if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) |
cce855bc | 5279 | { |
7117c2d2 JA |
5280 | val = quote_escapes (temp1); |
5281 | free (temp1); | |
5282 | temp1 = val; | |
cce855bc | 5283 | } |
7117c2d2 JA |
5284 | break; |
5285 | #endif | |
5286 | case VT_POSPARMS: | |
a0c0a00f | 5287 | temp1 = parameter_list_transform (xc, varname[0], quoted); |
7117c2d2 JA |
5288 | if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) |
5289 | { | |
5290 | val = quote_escapes (temp1); | |
5291 | free (temp1); | |
5292 | temp1 = val; | |
5293 | } | |
5294 | break; | |
cce855bc JA |
5295 | } |
5296 | ||
7117c2d2 | 5297 | return temp1; |
a0c0a00f | 5298 | } |
cce855bc | 5299 | |
726f6388 JA |
5300 | /******************************************* |
5301 | * * | |
5302 | * Functions to expand WORD_DESCs * | |
5303 | * * | |
5304 | *******************************************/ | |
5305 | ||
5306 | /* Expand WORD, performing word splitting on the result. This does | |
5307 | parameter expansion, command substitution, arithmetic expansion, | |
5308 | word splitting, and quote removal. */ | |
5309 | ||
5310 | WORD_LIST * | |
5311 | expand_word (word, quoted) | |
5312 | WORD_DESC *word; | |
5313 | int quoted; | |
5314 | { | |
5315 | WORD_LIST *result, *tresult; | |
5316 | ||
b72432fd | 5317 | tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL); |
726f6388 JA |
5318 | result = word_list_split (tresult); |
5319 | dispose_words (tresult); | |
ccc6cda3 | 5320 | return (result ? dequote_list (result) : result); |
726f6388 JA |
5321 | } |
5322 | ||
5323 | /* Expand WORD, but do not perform word splitting on the result. This | |
5324 | does parameter expansion, command substitution, arithmetic expansion, | |
5325 | and quote removal. */ | |
5326 | WORD_LIST * | |
28ef6c31 | 5327 | expand_word_unsplit (word, quoted) |
726f6388 JA |
5328 | WORD_DESC *word; |
5329 | int quoted; | |
5330 | { | |
5331 | WORD_LIST *result; | |
5332 | ||
28ef6c31 | 5333 | expand_no_split_dollar_star = 1; |
0001803f CR |
5334 | #if defined (HANDLE_MULTIBYTE) |
5335 | if (ifs_firstc[0] == 0) | |
5336 | #else | |
5337 | if (ifs_firstc == 0) | |
5338 | #endif | |
5339 | word->flags |= W_NOSPLIT; | |
1cc06898 | 5340 | word->flags |= W_NOSPLIT2; |
b72432fd | 5341 | result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL); |
28ef6c31 | 5342 | expand_no_split_dollar_star = 0; |
7117c2d2 | 5343 | |
ccc6cda3 | 5344 | return (result ? dequote_list (result) : result); |
726f6388 JA |
5345 | } |
5346 | ||
5347 | /* Perform shell expansions on WORD, but do not perform word splitting or | |
0001803f CR |
5348 | quote removal on the result. Virtually identical to expand_word_unsplit; |
5349 | could be combined if implementations don't diverge. */ | |
726f6388 JA |
5350 | WORD_LIST * |
5351 | expand_word_leave_quoted (word, quoted) | |
5352 | WORD_DESC *word; | |
5353 | int quoted; | |
5354 | { | |
0001803f CR |
5355 | WORD_LIST *result; |
5356 | ||
5357 | expand_no_split_dollar_star = 1; | |
5358 | #if defined (HANDLE_MULTIBYTE) | |
5359 | if (ifs_firstc[0] == 0) | |
5360 | #else | |
5361 | if (ifs_firstc == 0) | |
5362 | #endif | |
5363 | word->flags |= W_NOSPLIT; | |
5364 | word->flags |= W_NOSPLIT2; | |
5365 | result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL); | |
5366 | expand_no_split_dollar_star = 0; | |
5367 | ||
5368 | return result; | |
726f6388 JA |
5369 | } |
5370 | ||
726f6388 JA |
5371 | #if defined (PROCESS_SUBSTITUTION) |
5372 | ||
cce855bc JA |
5373 | /*****************************************************************/ |
5374 | /* */ | |
5375 | /* Hacking Process Substitution */ | |
5376 | /* */ | |
5377 | /*****************************************************************/ | |
726f6388 | 5378 | |
726f6388 JA |
5379 | #if !defined (HAVE_DEV_FD) |
5380 | /* Named pipes must be removed explicitly with `unlink'. This keeps a list | |
5381 | of FIFOs the shell has open. unlink_fifo_list will walk the list and | |
5382 | unlink all of them. add_fifo_list adds the name of an open FIFO to the | |
5383 | list. NFIFO is a count of the number of FIFOs in the list. */ | |
5384 | #define FIFO_INCR 20 | |
5385 | ||
f73dda09 JA |
5386 | struct temp_fifo { |
5387 | char *file; | |
5388 | pid_t proc; | |
5389 | }; | |
5390 | ||
5391 | static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL; | |
ccc6cda3 JA |
5392 | static int nfifo; |
5393 | static int fifo_list_size; | |
726f6388 | 5394 | |
a0c0a00f CR |
5395 | void |
5396 | clear_fifo_list () | |
5397 | { | |
5398 | } | |
5399 | ||
495aee44 CR |
5400 | char * |
5401 | copy_fifo_list (sizep) | |
5402 | int *sizep; | |
5403 | { | |
5404 | if (sizep) | |
5405 | *sizep = 0; | |
5406 | return (char *)NULL; | |
5407 | } | |
5408 | ||
726f6388 JA |
5409 | static void |
5410 | add_fifo_list (pathname) | |
5411 | char *pathname; | |
5412 | { | |
5413 | if (nfifo >= fifo_list_size - 1) | |
5414 | { | |
5415 | fifo_list_size += FIFO_INCR; | |
f73dda09 JA |
5416 | fifo_list = (struct temp_fifo *)xrealloc (fifo_list, |
5417 | fifo_list_size * sizeof (struct temp_fifo)); | |
726f6388 JA |
5418 | } |
5419 | ||
f73dda09 JA |
5420 | fifo_list[nfifo].file = savestring (pathname); |
5421 | nfifo++; | |
726f6388 JA |
5422 | } |
5423 | ||
495aee44 CR |
5424 | void |
5425 | unlink_fifo (i) | |
5426 | int i; | |
5427 | { | |
5428 | if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1)) | |
5429 | { | |
5430 | unlink (fifo_list[i].file); | |
5431 | free (fifo_list[i].file); | |
5432 | fifo_list[i].file = (char *)NULL; | |
5433 | fifo_list[i].proc = -1; | |
5434 | } | |
5435 | } | |
5436 | ||
726f6388 JA |
5437 | void |
5438 | unlink_fifo_list () | |
5439 | { | |
f73dda09 JA |
5440 | int saved, i, j; |
5441 | ||
ccc6cda3 | 5442 | if (nfifo == 0) |
726f6388 JA |
5443 | return; |
5444 | ||
f73dda09 | 5445 | for (i = saved = 0; i < nfifo; i++) |
726f6388 | 5446 | { |
f73dda09 JA |
5447 | if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1)) |
5448 | { | |
7117c2d2 JA |
5449 | unlink (fifo_list[i].file); |
5450 | free (fifo_list[i].file); | |
5451 | fifo_list[i].file = (char *)NULL; | |
5452 | fifo_list[i].proc = -1; | |
f73dda09 JA |
5453 | } |
5454 | else | |
7117c2d2 | 5455 | saved++; |
f73dda09 JA |
5456 | } |
5457 | ||
5458 | /* If we didn't remove some of the FIFOs, compact the list. */ | |
5459 | if (saved) | |
5460 | { | |
5461 | for (i = j = 0; i < nfifo; i++) | |
5462 | if (fifo_list[i].file) | |
5463 | { | |
5464 | fifo_list[j].file = fifo_list[i].file; | |
5465 | fifo_list[j].proc = fifo_list[i].proc; | |
5466 | j++; | |
5467 | } | |
5468 | nfifo = j; | |
726f6388 | 5469 | } |
f73dda09 JA |
5470 | else |
5471 | nfifo = 0; | |
726f6388 JA |
5472 | } |
5473 | ||
495aee44 CR |
5474 | /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list |
5475 | from some point in the past, and close all open FIFOs in fifo_list | |
5476 | that are not marked as active in LIST. If LIST is NULL, close | |
5477 | everything in fifo_list. LSIZE is the number of elements in LIST, in | |
5478 | case it's larger than fifo_list_size (size of fifo_list). */ | |
5479 | void | |
5480 | close_new_fifos (list, lsize) | |
5481 | char *list; | |
5482 | int lsize; | |
5483 | { | |
5484 | int i; | |
5485 | ||
5486 | if (list == 0) | |
5487 | { | |
5488 | unlink_fifo_list (); | |
5489 | return; | |
5490 | } | |
5491 | ||
5492 | for (i = 0; i < lsize; i++) | |
5493 | if (list[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1) | |
5494 | unlink_fifo (i); | |
5495 | ||
5496 | for (i = lsize; i < fifo_list_size; i++) | |
5497 | unlink_fifo (i); | |
5498 | } | |
5499 | ||
f1be666c JA |
5500 | int |
5501 | fifos_pending () | |
5502 | { | |
5503 | return nfifo; | |
5504 | } | |
5505 | ||
495aee44 CR |
5506 | int |
5507 | num_fifos () | |
5508 | { | |
5509 | return nfifo; | |
5510 | } | |
5511 | ||
726f6388 JA |
5512 | static char * |
5513 | make_named_pipe () | |
5514 | { | |
5515 | char *tname; | |
5516 | ||
3185942a | 5517 | tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR); |
726f6388 JA |
5518 | if (mkfifo (tname, 0600) < 0) |
5519 | { | |
5520 | free (tname); | |
5521 | return ((char *)NULL); | |
5522 | } | |
5523 | ||
5524 | add_fifo_list (tname); | |
5525 | return (tname); | |
5526 | } | |
5527 | ||
726f6388 JA |
5528 | #else /* HAVE_DEV_FD */ |
5529 | ||
5530 | /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell | |
5531 | has open to children. NFDS is a count of the number of bits currently | |
5532 | set in DEV_FD_LIST. TOTFDS is a count of the highest possible number | |
5533 | of open files. */ | |
5534 | static char *dev_fd_list = (char *)NULL; | |
ccc6cda3 | 5535 | static int nfds; |
726f6388 JA |
5536 | static int totfds; /* The highest possible number of open files. */ |
5537 | ||
a0c0a00f CR |
5538 | void |
5539 | clear_fifo (i) | |
5540 | int i; | |
5541 | { | |
5542 | if (dev_fd_list[i]) | |
5543 | { | |
5544 | dev_fd_list[i] = 0; | |
5545 | nfds--; | |
5546 | } | |
5547 | } | |
5548 | ||
5549 | void | |
5550 | clear_fifo_list () | |
5551 | { | |
5552 | register int i; | |
5553 | ||
5554 | if (nfds == 0) | |
5555 | return; | |
5556 | ||
5557 | for (i = 0; nfds && i < totfds; i++) | |
5558 | clear_fifo (i); | |
5559 | ||
5560 | nfds = 0; | |
5561 | } | |
5562 | ||
495aee44 CR |
5563 | char * |
5564 | copy_fifo_list (sizep) | |
5565 | int *sizep; | |
5566 | { | |
5567 | char *ret; | |
5568 | ||
5569 | if (nfds == 0 || totfds == 0) | |
5570 | { | |
5571 | if (sizep) | |
5572 | *sizep = 0; | |
5573 | return (char *)NULL; | |
5574 | } | |
5575 | ||
5576 | if (sizep) | |
5577 | *sizep = totfds; | |
5578 | ret = (char *)xmalloc (totfds); | |
5579 | return (memcpy (ret, dev_fd_list, totfds)); | |
5580 | } | |
5581 | ||
726f6388 JA |
5582 | static void |
5583 | add_fifo_list (fd) | |
5584 | int fd; | |
5585 | { | |
495aee44 | 5586 | if (dev_fd_list == 0 || fd >= totfds) |
726f6388 JA |
5587 | { |
5588 | int ofds; | |
5589 | ||
5590 | ofds = totfds; | |
5591 | totfds = getdtablesize (); | |
5592 | if (totfds < 0 || totfds > 256) | |
5593 | totfds = 256; | |
3185942a | 5594 | if (fd >= totfds) |
726f6388 JA |
5595 | totfds = fd + 2; |
5596 | ||
f73dda09 | 5597 | dev_fd_list = (char *)xrealloc (dev_fd_list, totfds); |
7117c2d2 | 5598 | memset (dev_fd_list + ofds, '\0', totfds - ofds); |
726f6388 JA |
5599 | } |
5600 | ||
5601 | dev_fd_list[fd] = 1; | |
5602 | nfds++; | |
5603 | } | |
5604 | ||
f1be666c JA |
5605 | int |
5606 | fifos_pending () | |
5607 | { | |
5608 | return 0; /* used for cleanup; not needed with /dev/fd */ | |
5609 | } | |
5610 | ||
495aee44 CR |
5611 | int |
5612 | num_fifos () | |
5613 | { | |
5614 | return nfds; | |
5615 | } | |
5616 | ||
5617 | void | |
5618 | unlink_fifo (fd) | |
5619 | int fd; | |
5620 | { | |
5621 | if (dev_fd_list[fd]) | |
5622 | { | |
5623 | close (fd); | |
5624 | dev_fd_list[fd] = 0; | |
5625 | nfds--; | |
5626 | } | |
5627 | } | |
5628 | ||
726f6388 JA |
5629 | void |
5630 | unlink_fifo_list () | |
5631 | { | |
5632 | register int i; | |
5633 | ||
ccc6cda3 | 5634 | if (nfds == 0) |
726f6388 JA |
5635 | return; |
5636 | ||
5637 | for (i = 0; nfds && i < totfds; i++) | |
495aee44 | 5638 | unlink_fifo (i); |
726f6388 JA |
5639 | |
5640 | nfds = 0; | |
5641 | } | |
5642 | ||
495aee44 CR |
5643 | /* Take LIST, which is a snapshot copy of dev_fd_list from some point in |
5644 | the past, and close all open fds in dev_fd_list that are not marked | |
5645 | as open in LIST. If LIST is NULL, close everything in dev_fd_list. | |
5646 | LSIZE is the number of elements in LIST, in case it's larger than | |
5647 | totfds (size of dev_fd_list). */ | |
5648 | void | |
5649 | close_new_fifos (list, lsize) | |
5650 | char *list; | |
5651 | int lsize; | |
5652 | { | |
5653 | int i; | |
5654 | ||
5655 | if (list == 0) | |
5656 | { | |
5657 | unlink_fifo_list (); | |
5658 | return; | |
5659 | } | |
5660 | ||
5661 | for (i = 0; i < lsize; i++) | |
5662 | if (list[i] == 0 && i < totfds && dev_fd_list[i]) | |
5663 | unlink_fifo (i); | |
5664 | ||
5665 | for (i = lsize; i < totfds; i++) | |
5666 | unlink_fifo (i); | |
5667 | } | |
5668 | ||
726f6388 JA |
5669 | #if defined (NOTDEF) |
5670 | print_dev_fd_list () | |
5671 | { | |
5672 | register int i; | |
5673 | ||
f73dda09 | 5674 | fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ()); |
726f6388 JA |
5675 | fflush (stderr); |
5676 | ||
5677 | for (i = 0; i < totfds; i++) | |
5678 | { | |
5679 | if (dev_fd_list[i]) | |
5680 | fprintf (stderr, " %d", i); | |
5681 | } | |
5682 | fprintf (stderr, "\n"); | |
5683 | } | |
5684 | #endif /* NOTDEF */ | |
5685 | ||
5686 | static char * | |
5687 | make_dev_fd_filename (fd) | |
5688 | int fd; | |
5689 | { | |
f73dda09 | 5690 | char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p; |
726f6388 | 5691 | |
17345e5a | 5692 | ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8); |
bb70624e JA |
5693 | |
5694 | strcpy (ret, DEV_FD_PREFIX); | |
5695 | p = inttostr (fd, intbuf, sizeof (intbuf)); | |
5696 | strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p); | |
5697 | ||
726f6388 JA |
5698 | add_fifo_list (fd); |
5699 | return (ret); | |
5700 | } | |
5701 | ||
5702 | #endif /* HAVE_DEV_FD */ | |
5703 | ||
5704 | /* Return a filename that will open a connection to the process defined by | |
5705 | executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return | |
5706 | a filename in /dev/fd corresponding to a descriptor that is one of the | |
5707 | ends of the pipe. If not defined, we use named pipes on systems that have | |
5708 | them. Systems without /dev/fd and named pipes are out of luck. | |
5709 | ||
5710 | OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or | |
5711 | use the read end of the pipe and dup that file descriptor to fd 0 in | |
5712 | the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for | |
5713 | writing or use the write end of the pipe in the child, and dup that | |
5714 | file descriptor to fd 1 in the child. The parent does the opposite. */ | |
5715 | ||
5716 | static char * | |
5717 | process_substitute (string, open_for_read_in_child) | |
5718 | char *string; | |
5719 | int open_for_read_in_child; | |
5720 | { | |
5721 | char *pathname; | |
5722 | int fd, result; | |
5723 | pid_t old_pid, pid; | |
5724 | #if defined (HAVE_DEV_FD) | |
5725 | int parent_pipe_fd, child_pipe_fd; | |
5726 | int fildes[2]; | |
5727 | #endif /* HAVE_DEV_FD */ | |
5728 | #if defined (JOB_CONTROL) | |
5729 | pid_t old_pipeline_pgrp; | |
ccc6cda3 | 5730 | #endif |
726f6388 | 5731 | |
cce855bc | 5732 | if (!string || !*string || wordexp_only) |
726f6388 JA |
5733 | return ((char *)NULL); |
5734 | ||
5735 | #if !defined (HAVE_DEV_FD) | |
5736 | pathname = make_named_pipe (); | |
5737 | #else /* HAVE_DEV_FD */ | |
5738 | if (pipe (fildes) < 0) | |
5739 | { | |
a0c0a00f | 5740 | sys_error ("%s", _("cannot make pipe for process substitution")); |
726f6388 JA |
5741 | return ((char *)NULL); |
5742 | } | |
5743 | /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of | |
5744 | the pipe in the parent, otherwise the read end. */ | |
5745 | parent_pipe_fd = fildes[open_for_read_in_child]; | |
5746 | child_pipe_fd = fildes[1 - open_for_read_in_child]; | |
d166f048 JA |
5747 | /* Move the parent end of the pipe to some high file descriptor, to |
5748 | avoid clashes with FDs used by the script. */ | |
5749 | parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64); | |
5750 | ||
726f6388 JA |
5751 | pathname = make_dev_fd_filename (parent_pipe_fd); |
5752 | #endif /* HAVE_DEV_FD */ | |
5753 | ||
3185942a | 5754 | if (pathname == 0) |
726f6388 | 5755 | { |
a0c0a00f | 5756 | sys_error ("%s", _("cannot make pipe for process substitution")); |
726f6388 JA |
5757 | return ((char *)NULL); |
5758 | } | |
5759 | ||
5760 | old_pid = last_made_pid; | |
5761 | ||
5762 | #if defined (JOB_CONTROL) | |
5763 | old_pipeline_pgrp = pipeline_pgrp; | |
a0c0a00f CR |
5764 | if (pipeline_pgrp == 0 || (subshell_environment & (SUBSHELL_PIPE|SUBSHELL_FORK|SUBSHELL_ASYNC)) == 0) |
5765 | pipeline_pgrp = shell_pgrp; | |
ccc6cda3 | 5766 | save_pipeline (1); |
ccc6cda3 JA |
5767 | #endif /* JOB_CONTROL */ |
5768 | ||
726f6388 JA |
5769 | pid = make_child ((char *)NULL, 1); |
5770 | if (pid == 0) | |
5771 | { | |
ccc6cda3 | 5772 | reset_terminating_signals (); /* XXX */ |
b80f6443 | 5773 | free_pushed_string_input (); |
726f6388 | 5774 | /* Cancel traps, in trap.c. */ |
495aee44 | 5775 | restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */ |
a0c0a00f | 5776 | QUIT; /* catch any interrupts we got post-fork */ |
726f6388 | 5777 | setup_async_signals (); |
3185942a | 5778 | subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB; |
a0c0a00f CR |
5779 | |
5780 | /* if we're expanding a redirection, we shouldn't have access to the | |
5781 | temporary environment, but commands in the subshell should have | |
5782 | access to their own temporary environment. */ | |
5783 | if (expanding_redir) | |
5784 | flush_temporary_env (); | |
726f6388 | 5785 | } |
ccc6cda3 JA |
5786 | |
5787 | #if defined (JOB_CONTROL) | |
726f6388 JA |
5788 | set_sigchld_handler (); |
5789 | stop_making_children (); | |
3185942a | 5790 | /* XXX - should we only do this in the parent? (as in command subst) */ |
726f6388 | 5791 | pipeline_pgrp = old_pipeline_pgrp; |
a0c0a00f CR |
5792 | #else |
5793 | stop_making_children (); | |
ccc6cda3 | 5794 | #endif /* JOB_CONTROL */ |
726f6388 JA |
5795 | |
5796 | if (pid < 0) | |
5797 | { | |
a0c0a00f | 5798 | sys_error ("%s", _("cannot make child for process substitution")); |
726f6388 JA |
5799 | free (pathname); |
5800 | #if defined (HAVE_DEV_FD) | |
5801 | close (parent_pipe_fd); | |
5802 | close (child_pipe_fd); | |
5803 | #endif /* HAVE_DEV_FD */ | |
5804 | return ((char *)NULL); | |
5805 | } | |
5806 | ||
5807 | if (pid > 0) | |
5808 | { | |
ccc6cda3 | 5809 | #if defined (JOB_CONTROL) |
a0c0a00f CR |
5810 | if (last_procsub_child) |
5811 | { | |
5812 | discard_pipeline (last_procsub_child); | |
5813 | last_procsub_child = (PROCESS *)NULL; | |
5814 | } | |
5815 | last_procsub_child = restore_pipeline (0); | |
ccc6cda3 JA |
5816 | #endif |
5817 | ||
f73dda09 JA |
5818 | #if !defined (HAVE_DEV_FD) |
5819 | fifo_list[nfifo-1].proc = pid; | |
5820 | #endif | |
5821 | ||
726f6388 JA |
5822 | last_made_pid = old_pid; |
5823 | ||
5824 | #if defined (JOB_CONTROL) && defined (PGRP_PIPE) | |
5825 | close_pgrp_pipe (); | |
5826 | #endif /* JOB_CONTROL && PGRP_PIPE */ | |
5827 | ||
5828 | #if defined (HAVE_DEV_FD) | |
5829 | close (child_pipe_fd); | |
5830 | #endif /* HAVE_DEV_FD */ | |
5831 | ||
5832 | return (pathname); | |
5833 | } | |
5834 | ||
5835 | set_sigint_handler (); | |
5836 | ||
5837 | #if defined (JOB_CONTROL) | |
5838 | set_job_control (0); | |
5839 | #endif /* JOB_CONTROL */ | |
5840 | ||
5841 | #if !defined (HAVE_DEV_FD) | |
5842 | /* Open the named pipe in the child. */ | |
ac50fbac | 5843 | fd = open (pathname, open_for_read_in_child ? O_RDONLY : O_WRONLY); |
726f6388 JA |
5844 | if (fd < 0) |
5845 | { | |
b80f6443 JA |
5846 | /* Two separate strings for ease of translation. */ |
5847 | if (open_for_read_in_child) | |
5848 | sys_error (_("cannot open named pipe %s for reading"), pathname); | |
5849 | else | |
5850 | sys_error (_("cannot open named pipe %s for writing"), pathname); | |
5851 | ||
726f6388 JA |
5852 | exit (127); |
5853 | } | |
bb70624e JA |
5854 | if (open_for_read_in_child) |
5855 | { | |
28ef6c31 | 5856 | if (sh_unset_nodelay_mode (fd) < 0) |
bb70624e | 5857 | { |
3185942a | 5858 | sys_error (_("cannot reset nodelay mode for fd %d"), fd); |
bb70624e JA |
5859 | exit (127); |
5860 | } | |
5861 | } | |
726f6388 JA |
5862 | #else /* HAVE_DEV_FD */ |
5863 | fd = child_pipe_fd; | |
5864 | #endif /* HAVE_DEV_FD */ | |
5865 | ||
a0c0a00f CR |
5866 | /* Discard buffered stdio output before replacing the underlying file |
5867 | descriptor. */ | |
5868 | if (open_for_read_in_child == 0) | |
5869 | fpurge (stdout); | |
5870 | ||
726f6388 JA |
5871 | if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0) |
5872 | { | |
b80f6443 | 5873 | sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname, |
ccc6cda3 | 5874 | open_for_read_in_child ? 0 : 1); |
726f6388 JA |
5875 | exit (127); |
5876 | } | |
5877 | ||
f73dda09 JA |
5878 | if (fd != (open_for_read_in_child ? 0 : 1)) |
5879 | close (fd); | |
726f6388 JA |
5880 | |
5881 | /* Need to close any files that this process has open to pipes inherited | |
5882 | from its parent. */ | |
5883 | if (current_fds_to_close) | |
5884 | { | |
5885 | close_fd_bitmap (current_fds_to_close); | |
5886 | current_fds_to_close = (struct fd_bitmap *)NULL; | |
5887 | } | |
5888 | ||
5889 | #if defined (HAVE_DEV_FD) | |
5890 | /* Make sure we close the parent's end of the pipe and clear the slot | |
5891 | in the fd list so it is not closed later, if reallocated by, for | |
5892 | instance, pipe(2). */ | |
5893 | close (parent_pipe_fd); | |
5894 | dev_fd_list[parent_pipe_fd] = 0; | |
5895 | #endif /* HAVE_DEV_FD */ | |
5896 | ||
8dea6e87 | 5897 | /* subshells shouldn't have this flag, which controls using the temporary |
a0c0a00f CR |
5898 | environment for variable lookups. We have already flushed the temporary |
5899 | environment above in the case we're expanding a redirection, so processes | |
5900 | executed by this command need to be able to set it independently of their | |
5901 | parent. */ | |
8dea6e87 CR |
5902 | expanding_redir = 0; |
5903 | ||
a0c0a00f | 5904 | subshell_level++; |
d166f048 | 5905 | result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST)); |
a0c0a00f | 5906 | subshell_level--; |
726f6388 JA |
5907 | |
5908 | #if !defined (HAVE_DEV_FD) | |
5909 | /* Make sure we close the named pipe in the child before we exit. */ | |
5910 | close (open_for_read_in_child ? 0 : 1); | |
5911 | #endif /* !HAVE_DEV_FD */ | |
5912 | ||
ac50fbac CR |
5913 | last_command_exit_value = result; |
5914 | result = run_exit_trap (); | |
726f6388 JA |
5915 | exit (result); |
5916 | /*NOTREACHED*/ | |
5917 | } | |
5918 | #endif /* PROCESS_SUBSTITUTION */ | |
5919 | ||
cce855bc JA |
5920 | /***********************************/ |
5921 | /* */ | |
5922 | /* Command Substitution */ | |
5923 | /* */ | |
5924 | /***********************************/ | |
5925 | ||
d166f048 | 5926 | static char * |
3185942a | 5927 | read_comsub (fd, quoted, rflag) |
d166f048 | 5928 | int fd, quoted; |
3185942a | 5929 | int *rflag; |
d166f048 | 5930 | { |
3185942a JA |
5931 | char *istring, buf[128], *bufp, *s; |
5932 | int istring_index, istring_size, c, tflag, skip_ctlesc, skip_ctlnul; | |
f73dda09 | 5933 | ssize_t bufn; |
d166f048 JA |
5934 | |
5935 | istring = (char *)NULL; | |
3185942a JA |
5936 | istring_index = istring_size = bufn = tflag = 0; |
5937 | ||
5938 | for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++) | |
5939 | skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL; | |
d166f048 | 5940 | |
3185942a JA |
5941 | /* Read the output of the command through the pipe. This may need to be |
5942 | changed to understand multibyte characters in the future. */ | |
d166f048 JA |
5943 | while (1) |
5944 | { | |
5945 | if (fd < 0) | |
28ef6c31 | 5946 | break; |
d166f048 JA |
5947 | if (--bufn <= 0) |
5948 | { | |
bb70624e | 5949 | bufn = zread (fd, buf, sizeof (buf)); |
d166f048 JA |
5950 | if (bufn <= 0) |
5951 | break; | |
5952 | bufp = buf; | |
5953 | } | |
5954 | c = *bufp++; | |
5955 | ||
28ef6c31 JA |
5956 | if (c == 0) |
5957 | { | |
a0c0a00f CR |
5958 | #if 1 |
5959 | internal_warning ("%s", _("command substitution: ignored null byte in input")); | |
28ef6c31 JA |
5960 | #endif |
5961 | continue; | |
5962 | } | |
5963 | ||
d166f048 JA |
5964 | /* Add the character to ISTRING, possibly after resizing it. */ |
5965 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE); | |
5966 | ||
f1be666c JA |
5967 | /* This is essentially quote_string inline */ |
5968 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */) | |
5969 | istring[istring_index++] = CTLESC; | |
5970 | /* Escape CTLESC and CTLNUL in the output to protect those characters | |
5971 | from the rest of the word expansions (word splitting and globbing.) | |
5972 | This is essentially quote_escapes inline. */ | |
3185942a JA |
5973 | else if (skip_ctlesc == 0 && c == CTLESC) |
5974 | { | |
5975 | tflag |= W_HASCTLESC; | |
5976 | istring[istring_index++] = CTLESC; | |
5977 | } | |
5978 | else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0))) | |
d166f048 JA |
5979 | istring[istring_index++] = CTLESC; |
5980 | ||
5981 | istring[istring_index++] = c; | |
28ef6c31 JA |
5982 | |
5983 | #if 0 | |
5984 | #if defined (__CYGWIN__) | |
5985 | if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r') | |
5986 | { | |
5987 | istring_index--; | |
5988 | istring[istring_index - 1] = '\n'; | |
5989 | } | |
5990 | #endif | |
5991 | #endif | |
d166f048 JA |
5992 | } |
5993 | ||
5994 | if (istring) | |
5995 | istring[istring_index] = '\0'; | |
5996 | ||
5997 | /* If we read no output, just return now and save ourselves some | |
5998 | trouble. */ | |
5999 | if (istring_index == 0) | |
6000 | { | |
6001 | FREE (istring); | |
3185942a JA |
6002 | if (rflag) |
6003 | *rflag = tflag; | |
d166f048 JA |
6004 | return (char *)NULL; |
6005 | } | |
6006 | ||
6007 | /* Strip trailing newlines from the output of the command. */ | |
6008 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
6009 | { | |
6010 | while (istring_index > 0) | |
6011 | { | |
6012 | if (istring[istring_index - 1] == '\n') | |
6013 | { | |
6014 | --istring_index; | |
6015 | ||
6016 | /* If the newline was quoted, remove the quoting char. */ | |
6017 | if (istring[istring_index - 1] == CTLESC) | |
6018 | --istring_index; | |
6019 | } | |
6020 | else | |
6021 | break; | |
6022 | } | |
6023 | istring[istring_index] = '\0'; | |
6024 | } | |
6025 | else | |
6026 | strip_trailing (istring, istring_index - 1, 1); | |
6027 | ||
3185942a JA |
6028 | if (rflag) |
6029 | *rflag = tflag; | |
d166f048 JA |
6030 | return istring; |
6031 | } | |
6032 | ||
3185942a JA |
6033 | /* Perform command substitution on STRING. This returns a WORD_DESC * with the |
6034 | contained string possibly quoted. */ | |
6035 | WORD_DESC * | |
726f6388 JA |
6036 | command_substitute (string, quoted) |
6037 | char *string; | |
6038 | int quoted; | |
6039 | { | |
95732b49 | 6040 | pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid; |
a0c0a00f | 6041 | char *istring, *s; |
3185942a JA |
6042 | int result, fildes[2], function_value, pflags, rc, tflag; |
6043 | WORD_DESC *ret; | |
726f6388 | 6044 | |
ccc6cda3 | 6045 | istring = (char *)NULL; |
726f6388 JA |
6046 | |
6047 | /* Don't fork () if there is no need to. In the case of no command to | |
6048 | run, just return NULL. */ | |
a0c0a00f CR |
6049 | #if 1 |
6050 | for (s = string; s && *s && (shellblank (*s) || *s == '\n'); s++) | |
6051 | ; | |
6052 | if (s == 0 || *s == 0) | |
6053 | return ((WORD_DESC *)NULL); | |
6054 | #else | |
726f6388 | 6055 | if (!string || !*string || (string[0] == '\n' && !string[1])) |
3185942a | 6056 | return ((WORD_DESC *)NULL); |
a0c0a00f | 6057 | #endif |
726f6388 | 6058 | |
cce855bc JA |
6059 | if (wordexp_only && read_but_dont_execute) |
6060 | { | |
0001803f | 6061 | last_command_exit_value = EX_WEXPCOMSUB; |
cce855bc JA |
6062 | jump_to_top_level (EXITPROG); |
6063 | } | |
6064 | ||
bb70624e JA |
6065 | /* We're making the assumption here that the command substitution will |
6066 | eventually run a command from the file system. Since we'll run | |
6067 | maybe_make_export_env in this subshell before executing that command, | |
6068 | the parent shell and any other shells it starts will have to remake | |
6069 | the environment. If we make it before we fork, other shells won't | |
6070 | have to. Don't bother if we have any temporary variable assignments, | |
6071 | though, because the export environment will be remade after this | |
6072 | command completes anyway, but do it if all the words to be expanded | |
6073 | are variable assignments. */ | |
6074 | if (subst_assign_varlist == 0 || garglist == 0) | |
6075 | maybe_make_export_env (); /* XXX */ | |
6076 | ||
b80f6443 | 6077 | /* Flags to pass to parse_and_execute() */ |
0001803f | 6078 | pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0; |
b80f6443 | 6079 | |
726f6388 JA |
6080 | /* Pipe the output of executing STRING into the current shell. */ |
6081 | if (pipe (fildes) < 0) | |
6082 | { | |
a0c0a00f | 6083 | sys_error ("%s", _("cannot make pipe for command substitution")); |
726f6388 JA |
6084 | goto error_exit; |
6085 | } | |
6086 | ||
6087 | old_pid = last_made_pid; | |
6088 | #if defined (JOB_CONTROL) | |
ccc6cda3 | 6089 | old_pipeline_pgrp = pipeline_pgrp; |
28ef6c31 JA |
6090 | /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */ |
6091 | if ((subshell_environment & SUBSHELL_PIPE) == 0) | |
6092 | pipeline_pgrp = shell_pgrp; | |
ccc6cda3 | 6093 | cleanup_the_pipeline (); |
95732b49 | 6094 | #endif /* JOB_CONTROL */ |
726f6388 | 6095 | |
95732b49 | 6096 | old_async_pid = last_asynchronous_pid; |
95732b49 | 6097 | pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC); |
95732b49 JA |
6098 | last_asynchronous_pid = old_async_pid; |
6099 | ||
726f6388 | 6100 | if (pid == 0) |
495aee44 CR |
6101 | { |
6102 | /* Reset the signal handlers in the child, but don't free the | |
6103 | trap strings. Set a flag noting that we have to free the | |
6104 | trap strings if we run trap to change a signal disposition. */ | |
6105 | reset_signal_handlers (); | |
a0c0a00f CR |
6106 | if (ISINTERRUPT) |
6107 | { | |
6108 | kill (getpid (), SIGINT); | |
6109 | CLRINTERRUPT; /* if we're ignoring SIGINT somehow */ | |
6110 | } | |
6111 | QUIT; /* catch any interrupts we got post-fork */ | |
495aee44 CR |
6112 | subshell_environment |= SUBSHELL_RESETTRAP; |
6113 | } | |
ccc6cda3 JA |
6114 | |
6115 | #if defined (JOB_CONTROL) | |
3185942a | 6116 | /* XXX DO THIS ONLY IN PARENT ? XXX */ |
ccc6cda3 JA |
6117 | set_sigchld_handler (); |
6118 | stop_making_children (); | |
f1be666c JA |
6119 | if (pid != 0) |
6120 | pipeline_pgrp = old_pipeline_pgrp; | |
f73dda09 JA |
6121 | #else |
6122 | stop_making_children (); | |
ccc6cda3 | 6123 | #endif /* JOB_CONTROL */ |
726f6388 JA |
6124 | |
6125 | if (pid < 0) | |
6126 | { | |
b80f6443 | 6127 | sys_error (_("cannot make child for command substitution")); |
726f6388 JA |
6128 | error_exit: |
6129 | ||
ac50fbac CR |
6130 | last_made_pid = old_pid; |
6131 | ||
726f6388 JA |
6132 | FREE (istring); |
6133 | close (fildes[0]); | |
6134 | close (fildes[1]); | |
3185942a | 6135 | return ((WORD_DESC *)NULL); |
726f6388 JA |
6136 | } |
6137 | ||
6138 | if (pid == 0) | |
6139 | { | |
a0c0a00f CR |
6140 | /* The currently executing shell is not interactive. */ |
6141 | interactive = 0; | |
6142 | ||
726f6388 | 6143 | set_sigint_handler (); /* XXX */ |
28ef6c31 | 6144 | |
b80f6443 JA |
6145 | free_pushed_string_input (); |
6146 | ||
a0c0a00f CR |
6147 | /* Discard buffered stdio output before replacing the underlying file |
6148 | descriptor. */ | |
6149 | fpurge (stdout); | |
6150 | ||
726f6388 JA |
6151 | if (dup2 (fildes[1], 1) < 0) |
6152 | { | |
a0c0a00f | 6153 | sys_error ("%s", _("command_substitute: cannot duplicate pipe as fd 1")); |
726f6388 JA |
6154 | exit (EXECUTION_FAILURE); |
6155 | } | |
6156 | ||
6157 | /* If standard output is closed in the parent shell | |
6158 | (such as after `exec >&-'), file descriptor 1 will be | |
6159 | the lowest available file descriptor, and end up in | |
6160 | fildes[0]. This can happen for stdin and stderr as well, | |
6161 | but stdout is more important -- it will cause no output | |
6162 | to be generated from this command. */ | |
6163 | if ((fildes[1] != fileno (stdin)) && | |
6164 | (fildes[1] != fileno (stdout)) && | |
6165 | (fildes[1] != fileno (stderr))) | |
6166 | close (fildes[1]); | |
6167 | ||
6168 | if ((fildes[0] != fileno (stdin)) && | |
6169 | (fildes[0] != fileno (stdout)) && | |
6170 | (fildes[0] != fileno (stderr))) | |
6171 | close (fildes[0]); | |
6172 | ||
495aee44 CR |
6173 | #ifdef __CYGWIN__ |
6174 | /* Let stdio know the fd may have changed from text to binary mode, and | |
6175 | make sure to preserve stdout line buffering. */ | |
6176 | freopen (NULL, "w", stdout); | |
6177 | sh_setlinebuf (stdout); | |
6178 | #endif /* __CYGWIN__ */ | |
6179 | ||
ccc6cda3 | 6180 | /* This is a subshell environment. */ |
28ef6c31 | 6181 | subshell_environment |= SUBSHELL_COMSUB; |
ccc6cda3 | 6182 | |
a0c0a00f CR |
6183 | /* Many shells do not appear to inherit the -v option for command |
6184 | substitutions. */ | |
6185 | change_flag ('v', FLAG_OFF); | |
6186 | ||
6187 | /* When inherit_errexit option is not enabled, command substitution does | |
6188 | not inherit the -e flag. It is enabled when Posix mode is enabled */ | |
6189 | if (inherit_errexit == 0) | |
ac50fbac CR |
6190 | { |
6191 | builtin_ignoring_errexit = 0; | |
6192 | change_flag ('e', FLAG_OFF); | |
ac50fbac | 6193 | } |
a0c0a00f CR |
6194 | set_shellopts (); |
6195 | ||
6196 | /* If we are expanding a redirection, we can dispose of any temporary | |
6197 | environment we received, since redirections are not supposed to have | |
6198 | access to the temporary environment. We will have to see whether this | |
6199 | affects temporary environments supplied to `eval', but the temporary | |
6200 | environment gets copied to builtin_env at some point. */ | |
6201 | if (expanding_redir) | |
6202 | { | |
6203 | flush_temporary_env (); | |
6204 | expanding_redir = 0; | |
6205 | } | |
726f6388 JA |
6206 | |
6207 | remove_quoted_escapes (string); | |
6208 | ||
ccc6cda3 | 6209 | startup_state = 2; /* see if we can avoid a fork */ |
726f6388 JA |
6210 | /* Give command substitution a place to jump back to on failure, |
6211 | so we don't go back up to main (). */ | |
ac50fbac | 6212 | result = setjmp_nosigs (top_level); |
726f6388 | 6213 | |
bb70624e JA |
6214 | /* If we're running a command substitution inside a shell function, |
6215 | trap `return' so we don't return from the function in the subshell | |
6216 | and go off to never-never land. */ | |
6217 | if (result == 0 && return_catch_flag) | |
ac50fbac | 6218 | function_value = setjmp_nosigs (return_catch); |
bb70624e JA |
6219 | else |
6220 | function_value = 0; | |
6221 | ||
b80f6443 JA |
6222 | if (result == ERREXIT) |
6223 | rc = last_command_exit_value; | |
6224 | else if (result == EXITPROG) | |
6225 | rc = last_command_exit_value; | |
726f6388 | 6226 | else if (result) |
b80f6443 | 6227 | rc = EXECUTION_FAILURE; |
bb70624e | 6228 | else if (function_value) |
b80f6443 | 6229 | rc = return_catch_value; |
726f6388 | 6230 | else |
b80f6443 JA |
6231 | { |
6232 | subshell_level++; | |
6233 | rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST); | |
6234 | subshell_level--; | |
6235 | } | |
6236 | ||
6237 | last_command_exit_value = rc; | |
6238 | rc = run_exit_trap (); | |
f1be666c JA |
6239 | #if defined (PROCESS_SUBSTITUTION) |
6240 | unlink_fifo_list (); | |
6241 | #endif | |
b80f6443 | 6242 | exit (rc); |
726f6388 JA |
6243 | } |
6244 | else | |
6245 | { | |
726f6388 JA |
6246 | #if defined (JOB_CONTROL) && defined (PGRP_PIPE) |
6247 | close_pgrp_pipe (); | |
6248 | #endif /* JOB_CONTROL && PGRP_PIPE */ | |
6249 | ||
6250 | close (fildes[1]); | |
6251 | ||
3185942a JA |
6252 | tflag = 0; |
6253 | istring = read_comsub (fildes[0], quoted, &tflag); | |
ccc6cda3 | 6254 | |
726f6388 JA |
6255 | close (fildes[0]); |
6256 | ||
b72432fd | 6257 | current_command_subst_pid = pid; |
726f6388 JA |
6258 | last_command_exit_value = wait_for (pid); |
6259 | last_command_subst_pid = pid; | |
6260 | last_made_pid = old_pid; | |
6261 | ||
6262 | #if defined (JOB_CONTROL) | |
6263 | /* If last_command_exit_value > 128, then the substituted command | |
6264 | was terminated by a signal. If that signal was SIGINT, then send | |
6265 | SIGINT to ourselves. This will break out of loops, for instance. */ | |
b80f6443 | 6266 | if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT) |
726f6388 JA |
6267 | kill (getpid (), SIGINT); |
6268 | ||
6269 | /* wait_for gives the terminal back to shell_pgrp. If some other | |
cce855bc JA |
6270 | process group should have it, give it away to that group here. |
6271 | pipeline_pgrp is non-zero only while we are constructing a | |
ac50fbac | 6272 | pipeline, so what we are concerned about is whether or not that |
cce855bc | 6273 | pipeline was started in the background. A pipeline started in |
a0c0a00f CR |
6274 | the background should never get the tty back here. We duplicate |
6275 | the conditions that wait_for tests to make sure we only give | |
6276 | the terminal back to pipeline_pgrp under the conditions that wait_for | |
6277 | gave it to shell_pgrp. If wait_for doesn't mess with the terminal | |
6278 | pgrp, we should not either. */ | |
6279 | if (interactive && pipeline_pgrp != (pid_t)0 && running_in_background == 0 && | |
6280 | (subshell_environment & (SUBSHELL_ASYNC|SUBSHELL_PIPE)) == 0) | |
28ef6c31 | 6281 | give_terminal_to (pipeline_pgrp, 0); |
726f6388 JA |
6282 | #endif /* JOB_CONTROL */ |
6283 | ||
3185942a JA |
6284 | ret = alloc_word_desc (); |
6285 | ret->word = istring; | |
6286 | ret->flags = tflag; | |
6287 | ||
6288 | return ret; | |
726f6388 JA |
6289 | } |
6290 | } | |
6291 | ||
6292 | /******************************************************** | |
6293 | * * | |
6294 | * Utility functions for parameter expansion * | |
6295 | * * | |
6296 | ********************************************************/ | |
6297 | ||
ccc6cda3 | 6298 | #if defined (ARRAY_VARS) |
ccc6cda3 | 6299 | |
f73dda09 | 6300 | static arrayind_t |
ccc6cda3 JA |
6301 | array_length_reference (s) |
6302 | char *s; | |
6303 | { | |
f73dda09 JA |
6304 | int len; |
6305 | arrayind_t ind; | |
3185942a | 6306 | char *akey; |
f73dda09 | 6307 | char *t, c; |
ccc6cda3 | 6308 | ARRAY *array; |
495aee44 | 6309 | HASH_TABLE *h; |
ccc6cda3 JA |
6310 | SHELL_VAR *var; |
6311 | ||
6312 | var = array_variable_part (s, &t, &len); | |
726f6388 | 6313 | |
ccc6cda3 JA |
6314 | /* If unbound variables should generate an error, report one and return |
6315 | failure. */ | |
ac50fbac | 6316 | if ((var == 0 || invisible_p (var) || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error) |
726f6388 | 6317 | { |
f73dda09 | 6318 | c = *--t; |
ccc6cda3 | 6319 | *t = '\0'; |
0001803f | 6320 | last_command_exit_value = EXECUTION_FAILURE; |
7117c2d2 | 6321 | err_unboundvar (s); |
f73dda09 | 6322 | *t = c; |
ccc6cda3 | 6323 | return (-1); |
726f6388 | 6324 | } |
ac50fbac | 6325 | else if (var == 0 || invisible_p (var)) |
ccc6cda3 | 6326 | return 0; |
726f6388 | 6327 | |
28ef6c31 JA |
6328 | /* We support a couple of expansions for variables that are not arrays. |
6329 | We'll return the length of the value for v[0], and 1 for v[@] or | |
6330 | v[*]. Return 0 for everything else. */ | |
6331 | ||
6332 | array = array_p (var) ? array_cell (var) : (ARRAY *)NULL; | |
495aee44 | 6333 | h = assoc_p (var) ? assoc_cell (var) : (HASH_TABLE *)NULL; |
726f6388 | 6334 | |
ccc6cda3 | 6335 | if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']') |
ccc6cda3 | 6336 | { |
3185942a | 6337 | if (assoc_p (var)) |
495aee44 | 6338 | return (h ? assoc_num_elements (h) : 0); |
3185942a | 6339 | else if (array_p (var)) |
495aee44 | 6340 | return (array ? array_num_elements (array) : 0); |
3185942a | 6341 | else |
495aee44 | 6342 | return (var_isset (var) ? 1 : 0); |
ccc6cda3 | 6343 | } |
ccc6cda3 | 6344 | |
3185942a JA |
6345 | if (assoc_p (var)) |
6346 | { | |
6347 | t[len - 1] = '\0'; | |
6348 | akey = expand_assignment_string_to_string (t, 0); /* [ */ | |
6349 | t[len - 1] = ']'; | |
6350 | if (akey == 0 || *akey == 0) | |
6351 | { | |
6352 | err_badarraysub (t); | |
ac50fbac | 6353 | FREE (akey); |
3185942a JA |
6354 | return (-1); |
6355 | } | |
6356 | t = assoc_reference (assoc_cell (var), akey); | |
ac50fbac | 6357 | free (akey); |
3185942a | 6358 | } |
28ef6c31 | 6359 | else |
3185942a | 6360 | { |
ac50fbac CR |
6361 | ind = array_expand_index (var, t, len); |
6362 | /* negative subscripts to indexed arrays count back from end */ | |
6363 | if (var && array_p (var) && ind < 0) | |
6364 | ind = array_max_index (array_cell (var)) + 1 + ind; | |
3185942a JA |
6365 | if (ind < 0) |
6366 | { | |
6367 | err_badarraysub (t); | |
6368 | return (-1); | |
6369 | } | |
6370 | if (array_p (var)) | |
6371 | t = array_reference (array, ind); | |
6372 | else | |
6373 | t = (ind == 0) ? value_cell (var) : (char *)NULL; | |
6374 | } | |
28ef6c31 | 6375 | |
f1be666c | 6376 | len = MB_STRLEN (t); |
ccc6cda3 | 6377 | return (len); |
726f6388 | 6378 | } |
ccc6cda3 | 6379 | #endif /* ARRAY_VARS */ |
726f6388 JA |
6380 | |
6381 | static int | |
6382 | valid_brace_expansion_word (name, var_is_special) | |
6383 | char *name; | |
6384 | int var_is_special; | |
6385 | { | |
f73dda09 | 6386 | if (DIGIT (*name) && all_digits (name)) |
726f6388 JA |
6387 | return 1; |
6388 | else if (var_is_special) | |
6389 | return 1; | |
ccc6cda3 | 6390 | #if defined (ARRAY_VARS) |
a0c0a00f | 6391 | else if (valid_array_reference (name, 0)) |
ccc6cda3 JA |
6392 | return 1; |
6393 | #endif /* ARRAY_VARS */ | |
726f6388 JA |
6394 | else if (legal_identifier (name)) |
6395 | return 1; | |
6396 | else | |
6397 | return 0; | |
6398 | } | |
ccc6cda3 | 6399 | |
b80f6443 JA |
6400 | static int |
6401 | chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at) | |
6402 | char *name; | |
6403 | int quoted; | |
6404 | int *quoted_dollar_atp, *contains_dollar_at; | |
6405 | { | |
6406 | char *temp1; | |
6407 | ||
6408 | if (name == 0) | |
6409 | { | |
6410 | if (quoted_dollar_atp) | |
6411 | *quoted_dollar_atp = 0; | |
6412 | if (contains_dollar_at) | |
6413 | *contains_dollar_at = 0; | |
6414 | return 0; | |
6415 | } | |
6416 | ||
6417 | /* check for $@ and $* */ | |
6418 | if (name[0] == '@' && name[1] == 0) | |
6419 | { | |
6420 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
6421 | *quoted_dollar_atp = 1; | |
6422 | if (contains_dollar_at) | |
6423 | *contains_dollar_at = 1; | |
6424 | return 1; | |
6425 | } | |
6426 | else if (name[0] == '*' && name[1] == '\0' && quoted == 0) | |
6427 | { | |
6428 | if (contains_dollar_at) | |
6429 | *contains_dollar_at = 1; | |
6430 | return 1; | |
6431 | } | |
6432 | ||
6433 | /* Now check for ${array[@]} and ${array[*]} */ | |
6434 | #if defined (ARRAY_VARS) | |
a0c0a00f | 6435 | else if (valid_array_reference (name, 0)) |
b80f6443 | 6436 | { |
0001803f | 6437 | temp1 = mbschr (name, '['); |
b80f6443 JA |
6438 | if (temp1 && temp1[1] == '@' && temp1[2] == ']') |
6439 | { | |
6440 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
6441 | *quoted_dollar_atp = 1; | |
6442 | if (contains_dollar_at) | |
6443 | *contains_dollar_at = 1; | |
6444 | return 1; | |
6445 | } /* [ */ | |
6446 | /* ${array[*]}, when unquoted, should be treated like ${array[@]}, | |
6447 | which should result in separate words even when IFS is unset. */ | |
6448 | if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0) | |
6449 | { | |
6450 | if (contains_dollar_at) | |
6451 | *contains_dollar_at = 1; | |
6452 | return 1; | |
6453 | } | |
6454 | } | |
6455 | #endif | |
6456 | return 0; | |
6457 | } | |
6458 | ||
726f6388 JA |
6459 | /* Parameter expand NAME, and return a new string which is the expansion, |
6460 | or NULL if there was no expansion. | |
6461 | VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in | |
6462 | the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that | |
6463 | NAME was found inside of a double-quoted expression. */ | |
95732b49 | 6464 | static WORD_DESC * |
495aee44 | 6465 | parameter_brace_expand_word (name, var_is_special, quoted, pflags, indp) |
726f6388 | 6466 | char *name; |
89a92869 | 6467 | int var_is_special, quoted, pflags; |
495aee44 | 6468 | arrayind_t *indp; |
726f6388 | 6469 | { |
95732b49 | 6470 | WORD_DESC *ret; |
ccc6cda3 | 6471 | char *temp, *tt; |
7117c2d2 | 6472 | intmax_t arg_index; |
ccc6cda3 | 6473 | SHELL_VAR *var; |
f1be666c | 6474 | int atype, rflags; |
495aee44 | 6475 | arrayind_t ind; |
726f6388 | 6476 | |
95732b49 JA |
6477 | ret = 0; |
6478 | temp = 0; | |
f1be666c | 6479 | rflags = 0; |
95732b49 | 6480 | |
495aee44 CR |
6481 | if (indp) |
6482 | *indp = INTMAX_MIN; | |
6483 | ||
95732b49 | 6484 | /* Handle multiple digit arguments, as in ${11}. */ |
f73dda09 | 6485 | if (legal_number (name, &arg_index)) |
7117c2d2 JA |
6486 | { |
6487 | tt = get_dollar_var_value (arg_index); | |
b80f6443 JA |
6488 | if (tt) |
6489 | temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) | |
6490 | ? quote_string (tt) | |
6491 | : quote_escapes (tt); | |
6492 | else | |
6493 | temp = (char *)NULL; | |
7117c2d2 JA |
6494 | FREE (tt); |
6495 | } | |
726f6388 JA |
6496 | else if (var_is_special) /* ${@} */ |
6497 | { | |
cce855bc | 6498 | int sindex; |
f73dda09 | 6499 | tt = (char *)xmalloc (2 + strlen (name)); |
cce855bc | 6500 | tt[sindex = 0] = '$'; |
726f6388 | 6501 | strcpy (tt + 1, name); |
7117c2d2 | 6502 | |
95732b49 | 6503 | ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL, |
89a92869 | 6504 | (int *)NULL, (int *)NULL, pflags); |
cce855bc | 6505 | free (tt); |
726f6388 | 6506 | } |
ccc6cda3 | 6507 | #if defined (ARRAY_VARS) |
a0c0a00f | 6508 | else if (valid_array_reference (name, 0)) |
ccc6cda3 | 6509 | { |
ac50fbac | 6510 | expand_arrayref: |
ac50fbac | 6511 | if (pflags & PF_ASSIGNRHS) |
a0c0a00f CR |
6512 | { |
6513 | var = array_variable_part (name, &tt, (int *)0); | |
6514 | if (ALL_ELEMENT_SUB (tt[0]) && tt[1] == ']') | |
6515 | { | |
6516 | /* Only treat as double quoted if array variable */ | |
6517 | if (var && (array_p (var) || assoc_p (var))) | |
6518 | /* XXX - bash-4.4/bash-5.0 pass AV_ASSIGNRHS */ | |
6519 | temp = array_value (name, quoted|Q_DOUBLE_QUOTES, AV_ASSIGNRHS, &atype, &ind); | |
6520 | else | |
6521 | temp = array_value (name, quoted, 0, &atype, &ind); | |
6522 | } | |
ac50fbac CR |
6523 | else |
6524 | temp = array_value (name, quoted, 0, &atype, &ind); | |
a0c0a00f | 6525 | } |
ac50fbac CR |
6526 | else |
6527 | temp = array_value (name, quoted, 0, &atype, &ind); | |
7117c2d2 | 6528 | if (atype == 0 && temp) |
495aee44 CR |
6529 | { |
6530 | temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) | |
6531 | ? quote_string (temp) | |
6532 | : quote_escapes (temp); | |
6533 | rflags |= W_ARRAYIND; | |
6534 | if (indp) | |
6535 | *indp = ind; | |
6536 | } | |
f1be666c JA |
6537 | else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
6538 | rflags |= W_HASQUOTEDNULL; | |
ccc6cda3 JA |
6539 | } |
6540 | #endif | |
6541 | else if (var = find_variable (name)) | |
6542 | { | |
7117c2d2 | 6543 | if (var_isset (var) && invisible_p (var) == 0) |
28ef6c31 | 6544 | { |
ccc6cda3 | 6545 | #if defined (ARRAY_VARS) |
3185942a JA |
6546 | if (assoc_p (var)) |
6547 | temp = assoc_reference (assoc_cell (var), "0"); | |
6548 | else if (array_p (var)) | |
6549 | temp = array_reference (array_cell (var), 0); | |
6550 | else | |
6551 | temp = value_cell (var); | |
ccc6cda3 JA |
6552 | #else |
6553 | temp = value_cell (var); | |
6554 | #endif | |
6555 | ||
6556 | if (temp) | |
b80f6443 JA |
6557 | temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
6558 | ? quote_string (temp) | |
6559 | : quote_escapes (temp); | |
28ef6c31 | 6560 | } |
ccc6cda3 JA |
6561 | else |
6562 | temp = (char *)NULL; | |
6563 | } | |
a0c0a00f | 6564 | else if (var = find_variable_last_nameref (name, 0)) |
ac50fbac CR |
6565 | { |
6566 | temp = nameref_cell (var); | |
6567 | #if defined (ARRAY_VARS) | |
6568 | /* Handle expanding nameref whose value is x[n] */ | |
a0c0a00f | 6569 | if (temp && *temp && valid_array_reference (temp, 0)) |
ac50fbac CR |
6570 | { |
6571 | name = temp; | |
6572 | goto expand_arrayref; | |
6573 | } | |
6574 | else | |
6575 | #endif | |
6576 | /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */ | |
6577 | if (temp && *temp && legal_identifier (temp) == 0) | |
6578 | { | |
6579 | last_command_exit_value = EXECUTION_FAILURE; | |
6580 | report_error (_("%s: invalid variable name for name reference"), temp); | |
6581 | temp = &expand_param_error; | |
6582 | } | |
6583 | else | |
6584 | temp = (char *)NULL; | |
6585 | } | |
726f6388 | 6586 | else |
ccc6cda3 | 6587 | temp = (char *)NULL; |
726f6388 | 6588 | |
95732b49 JA |
6589 | if (ret == 0) |
6590 | { | |
6591 | ret = alloc_word_desc (); | |
6592 | ret->word = temp; | |
f1be666c | 6593 | ret->flags |= rflags; |
95732b49 JA |
6594 | } |
6595 | return ret; | |
726f6388 JA |
6596 | } |
6597 | ||
ac50fbac CR |
6598 | static char * |
6599 | parameter_brace_find_indir (name, var_is_special, quoted, find_nameref) | |
ccc6cda3 | 6600 | char *name; |
ac50fbac | 6601 | int var_is_special, quoted, find_nameref; |
ccc6cda3 JA |
6602 | { |
6603 | char *temp, *t; | |
95732b49 | 6604 | WORD_DESC *w; |
ac50fbac CR |
6605 | SHELL_VAR *v; |
6606 | ||
a0c0a00f | 6607 | if (find_nameref && var_is_special == 0 && (v = find_variable_last_nameref (name, 0)) && |
ac50fbac CR |
6608 | nameref_p (v) && (t = nameref_cell (v)) && *t) |
6609 | return (savestring (t)); | |
ccc6cda3 | 6610 | |
ac50fbac CR |
6611 | /* If var_is_special == 0, and name is not an array reference, this does |
6612 | more expansion than necessary. It should really look up the variable's | |
6613 | value and not try to expand it. */ | |
495aee44 | 6614 | w = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND, 0); |
95732b49 | 6615 | t = w->word; |
b80f6443 JA |
6616 | /* Have to dequote here if necessary */ |
6617 | if (t) | |
6618 | { | |
6619 | temp = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
6620 | ? dequote_string (t) | |
6621 | : dequote_escapes (t); | |
6622 | free (t); | |
6623 | t = temp; | |
6624 | } | |
95732b49 JA |
6625 | dispose_word_desc (w); |
6626 | ||
ac50fbac CR |
6627 | return t; |
6628 | } | |
6629 | ||
6630 | /* Expand an indirect reference to a variable: ${!NAME} expands to the | |
6631 | value of the variable whose name is the value of NAME. */ | |
6632 | static WORD_DESC * | |
6633 | parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at) | |
6634 | char *name; | |
6635 | int var_is_special, quoted; | |
6636 | int *quoted_dollar_atp, *contains_dollar_at; | |
6637 | { | |
6638 | char *temp, *t; | |
6639 | WORD_DESC *w; | |
6640 | SHELL_VAR *v; | |
6641 | ||
6642 | /* See if it's a nameref first, behave in ksh93-compatible fashion. | |
6643 | There is at least one incompatibility: given ${!foo[0]} where foo=bar, | |
6644 | bash performs an indirect lookup on foo[0] and expands the result; | |
6645 | ksh93 expands bar[0]. We could do that here -- there are enough usable | |
6646 | primitives to do that -- but do not at this point. */ | |
a0c0a00f | 6647 | if (var_is_special == 0 && (v = find_variable_last_nameref (name, 0))) |
ac50fbac CR |
6648 | { |
6649 | if (nameref_p (v) && (t = nameref_cell (v)) && *t) | |
6650 | { | |
6651 | w = alloc_word_desc (); | |
6652 | w->word = savestring (t); | |
6653 | w->flags = 0; | |
6654 | return w; | |
6655 | } | |
6656 | } | |
6657 | ||
6658 | t = parameter_brace_find_indir (name, var_is_special, quoted, 0); | |
6659 | ||
b80f6443 | 6660 | chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at); |
ccc6cda3 | 6661 | if (t == 0) |
95732b49 JA |
6662 | return (WORD_DESC *)NULL; |
6663 | ||
a0c0a00f CR |
6664 | if (valid_brace_expansion_word (t, SPECIAL_VAR (t, 0)) == 0) |
6665 | { | |
6666 | report_error (_("%s: bad substitution"), t); | |
6667 | free (t); | |
6668 | w = alloc_word_desc (); | |
6669 | w->word = &expand_param_error; | |
6670 | w->flags = 0; | |
6671 | return (w); | |
6672 | } | |
6673 | ||
495aee44 | 6674 | w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, 0, 0); |
ccc6cda3 | 6675 | free (t); |
95732b49 JA |
6676 | |
6677 | return w; | |
ccc6cda3 JA |
6678 | } |
6679 | ||
726f6388 JA |
6680 | /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE}, |
6681 | depending on the value of C, the separating character. C can be one of | |
ccc6cda3 JA |
6682 | "-", "+", or "=". QUOTED is true if the entire brace expression occurs |
6683 | between double quotes. */ | |
95732b49 | 6684 | static WORD_DESC * |
a0c0a00f | 6685 | parameter_brace_expand_rhs (name, value, c, quoted, pflags, qdollaratp, hasdollarat) |
726f6388 | 6686 | char *name, *value; |
a0c0a00f | 6687 | int c, quoted, pflags, *qdollaratp, *hasdollarat; |
726f6388 | 6688 | { |
95732b49 | 6689 | WORD_DESC *w; |
726f6388 | 6690 | WORD_LIST *l; |
a0c0a00f CR |
6691 | char *t, *t1, *temp, *vname; |
6692 | int l_hasdollat, sindex; | |
726f6388 | 6693 | |
a0c0a00f | 6694 | /*itrace("parameter_brace_expand_rhs: %s:%s pflags = %d", name, value, pflags);*/ |
ccc6cda3 JA |
6695 | /* If the entire expression is between double quotes, we want to treat |
6696 | the value as a double-quoted string, with the exception that we strip | |
3185942a | 6697 | embedded unescaped double quotes (for sh backwards compatibility). */ |
95732b49 | 6698 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value) |
726f6388 | 6699 | { |
a0c0a00f CR |
6700 | sindex = 0; |
6701 | temp = string_extract_double_quoted (value, &sindex, SX_STRIPDQ); | |
726f6388 | 6702 | } |
95732b49 JA |
6703 | else |
6704 | temp = value; | |
ccc6cda3 | 6705 | |
95732b49 | 6706 | w = alloc_word_desc (); |
a0c0a00f | 6707 | l_hasdollat = 0; |
ccc6cda3 | 6708 | /* XXX was 0 not quoted */ |
a0c0a00f | 6709 | l = *temp ? expand_string_for_rhs (temp, quoted, &l_hasdollat, (int *)NULL) |
ccc6cda3 JA |
6710 | : (WORD_LIST *)0; |
6711 | if (hasdollarat) | |
a0c0a00f | 6712 | *hasdollarat = l_hasdollat || (l && l->next); |
95732b49 JA |
6713 | if (temp != value) |
6714 | free (temp); | |
726f6388 JA |
6715 | if (l) |
6716 | { | |
a0c0a00f CR |
6717 | /* If l->next is not null, we know that TEMP contained "$@", since that |
6718 | is the only expansion that creates more than one word. */ | |
6719 | if (qdollaratp && ((l_hasdollat && quoted) || l->next)) | |
6720 | { | |
6721 | /*itrace("parameter_brace_expand_rhs: %s:%s: l != NULL, set *qdollaratp", name, value);*/ | |
6722 | *qdollaratp = 1; | |
6723 | } | |
6724 | ||
ccc6cda3 | 6725 | /* The expansion of TEMP returned something. We need to treat things |
a0c0a00f CR |
6726 | slightly differently if L_HASDOLLAT is non-zero. If we have "$@", |
6727 | the individual words have already been quoted. We need to turn them | |
b80f6443 JA |
6728 | into a string with the words separated by the first character of |
6729 | $IFS without any additional quoting, so string_list_dollar_at won't | |
a0c0a00f CR |
6730 | do the right thing. If IFS is null, we want "$@" to split into |
6731 | separate arguments, not be concatenated, so we use string_list_internal | |
6732 | and mark the word to be split on spaces later. We use | |
6733 | string_list_dollar_star for "$@" otherwise. */ | |
6734 | if (l->next && ifs_is_null) | |
6735 | { | |
6736 | temp = string_list_internal (l, " "); | |
6737 | w->flags |= W_SPLITSPACE; | |
6738 | } | |
6739 | else | |
6740 | temp = (l_hasdollat || l->next) ? string_list_dollar_star (l) : string_list (l); | |
b80f6443 | 6741 | |
cd110fdf CR |
6742 | /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is |
6743 | a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the | |
6744 | flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the | |
6745 | expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
6746 | (which is more paranoia than anything else), we need to return the | |
6747 | quoted null string and set the flags to indicate it. */ | |
ac50fbac | 6748 | if (l->next == 0 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp) && QUOTED_NULL (l->word->word) && (l->word->flags & W_HASQUOTEDNULL)) |
cd110fdf CR |
6749 | { |
6750 | w->flags |= W_HASQUOTEDNULL; | |
a0c0a00f CR |
6751 | /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null, turning off qdollaratp", name, value);*/ |
6752 | /* If we return a quoted null with L_HASDOLLARAT, we either have a | |
6753 | construct like "${@-$@}" or "${@-${@-$@}}" with no positional | |
6754 | parameters or a quoted expansion of "$@" with $1 == ''. In either | |
6755 | case, we don't want to enable special handling of $@. */ | |
6756 | if (qdollaratp && l_hasdollat) | |
6757 | *qdollaratp = 0; | |
cd110fdf | 6758 | } |
726f6388 JA |
6759 | dispose_words (l); |
6760 | } | |
a0c0a00f | 6761 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && l_hasdollat) |
726f6388 | 6762 | { |
a0c0a00f CR |
6763 | /* Posix interp 221 changed the rules on this. The idea is that |
6764 | something like "$xxx$@" should expand the same as "${foo-$xxx$@}" | |
6765 | when foo and xxx are unset. The problem is that it's not in any | |
6766 | way backwards compatible and few other shells do it. We're eventually | |
6767 | going to try and split the difference (heh) a little bit here. */ | |
6768 | /* l_hasdollat == 1 means we saw a quoted dollar at. */ | |
6769 | ||
ccc6cda3 JA |
6770 | /* The brace expansion occurred between double quotes and there was |
6771 | a $@ in TEMP. It does not matter if the $@ is quoted, as long as | |
7117c2d2 | 6772 | it does not expand to anything. In this case, we want to return |
a0c0a00f | 6773 | a quoted empty string. Posix interp 888 */ |
0628567a | 6774 | temp = make_quoted_char ('\0'); |
95732b49 | 6775 | w->flags |= W_HASQUOTEDNULL; |
a0c0a00f | 6776 | /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null", name, value);*/ |
726f6388 JA |
6777 | } |
6778 | else | |
6779 | temp = (char *)NULL; | |
6780 | ||
6781 | if (c == '-' || c == '+') | |
95732b49 JA |
6782 | { |
6783 | w->word = temp; | |
6784 | return w; | |
6785 | } | |
726f6388 JA |
6786 | |
6787 | /* c == '=' */ | |
ccc6cda3 | 6788 | t = temp ? savestring (temp) : savestring (""); |
726f6388 JA |
6789 | t1 = dequote_string (t); |
6790 | free (t); | |
a0c0a00f CR |
6791 | |
6792 | /* bash-4.4/5.0 */ | |
6793 | vname = name; | |
6794 | if (*name == '!' && | |
6795 | (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) || VALID_INDIR_PARAM (name[1]))) | |
6796 | { | |
6797 | vname = parameter_brace_find_indir (name + 1, SPECIAL_VAR (name, 1), quoted, 1); | |
6798 | if (vname == 0 || *vname == 0) | |
6799 | { | |
6800 | report_error (_("%s: invalid indirect expansion"), name); | |
6801 | free (vname); | |
6802 | dispose_word (w); | |
6803 | return &expand_wdesc_error; | |
6804 | } | |
6805 | if (legal_identifier (vname) == 0) | |
6806 | { | |
6807 | report_error (_("%s: invalid variable name"), vname); | |
6808 | free (vname); | |
6809 | dispose_word (w); | |
6810 | return &expand_wdesc_error; | |
6811 | } | |
6812 | } | |
6813 | ||
b80f6443 | 6814 | #if defined (ARRAY_VARS) |
a0c0a00f CR |
6815 | if (valid_array_reference (vname, 0)) |
6816 | assign_array_element (vname, t1, 0); | |
b80f6443 JA |
6817 | else |
6818 | #endif /* ARRAY_VARS */ | |
a0c0a00f CR |
6819 | bind_variable (vname, t1, 0); |
6820 | ||
6821 | stupidly_hack_special_variables (vname); | |
6822 | ||
6823 | if (vname != name) | |
6824 | free (vname); | |
95732b49 | 6825 | |
495aee44 CR |
6826 | /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */ |
6827 | free (temp); | |
6828 | ||
6829 | w->word = t1; | |
95732b49 | 6830 | return w; |
726f6388 JA |
6831 | } |
6832 | ||
6833 | /* Deal with the right hand side of a ${name:?value} expansion in the case | |
6834 | that NAME is null or not set. If VALUE is non-null it is expanded and | |
6835 | used as the error message to print, otherwise a standard message is | |
6836 | printed. */ | |
6837 | static void | |
6838 | parameter_brace_expand_error (name, value) | |
6839 | char *name, *value; | |
6840 | { | |
ccc6cda3 JA |
6841 | WORD_LIST *l; |
6842 | char *temp; | |
6843 | ||
ac50fbac | 6844 | last_command_exit_value = EXECUTION_FAILURE; /* ensure it's non-zero */ |
726f6388 JA |
6845 | if (value && *value) |
6846 | { | |
95732b49 | 6847 | l = expand_string (value, 0); |
ccc6cda3 JA |
6848 | temp = string_list (l); |
6849 | report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */ | |
6850 | FREE (temp); | |
726f6388 JA |
6851 | dispose_words (l); |
6852 | } | |
6853 | else | |
b80f6443 | 6854 | report_error (_("%s: parameter null or not set"), name); |
726f6388 JA |
6855 | |
6856 | /* Free the data we have allocated during this expansion, since we | |
6857 | are about to longjmp out. */ | |
6858 | free (name); | |
6859 | FREE (value); | |
6860 | } | |
6861 | ||
6862 | /* Return 1 if NAME is something for which parameter_brace_expand_length is | |
6863 | OK to do. */ | |
6864 | static int | |
6865 | valid_length_expression (name) | |
6866 | char *name; | |
6867 | { | |
28ef6c31 | 6868 | return (name[1] == '\0' || /* ${#} */ |
f73dda09 JA |
6869 | ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */ |
6870 | (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */ | |
ccc6cda3 | 6871 | #if defined (ARRAY_VARS) |
a0c0a00f | 6872 | valid_array_reference (name + 1, 0) || /* ${#a[7]} */ |
ccc6cda3 | 6873 | #endif |
726f6388 JA |
6874 | legal_identifier (name + 1)); /* ${#PS1} */ |
6875 | } | |
6876 | ||
6877 | /* Handle the parameter brace expansion that requires us to return the | |
6878 | length of a parameter. */ | |
7117c2d2 | 6879 | static intmax_t |
726f6388 JA |
6880 | parameter_brace_expand_length (name) |
6881 | char *name; | |
6882 | { | |
ccc6cda3 | 6883 | char *t, *newname; |
7117c2d2 | 6884 | intmax_t number, arg_index; |
ccc6cda3 JA |
6885 | WORD_LIST *list; |
6886 | #if defined (ARRAY_VARS) | |
6887 | SHELL_VAR *var; | |
6888 | #endif | |
6889 | ||
6890 | if (name[1] == '\0') /* ${#} */ | |
6891 | number = number_of_args (); | |
a0c0a00f | 6892 | else if (DOLLAR_AT_STAR (name[1]) && name[2] == '\0') /* ${#@}, ${#*} */ |
cce855bc | 6893 | number = number_of_args (); |
f73dda09 | 6894 | else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') |
cce855bc JA |
6895 | { |
6896 | /* Take the lengths of some of the shell's special parameters. */ | |
6897 | switch (name[1]) | |
6898 | { | |
6899 | case '-': | |
6900 | t = which_set_flags (); | |
6901 | break; | |
6902 | case '?': | |
6903 | t = itos (last_command_exit_value); | |
6904 | break; | |
6905 | case '$': | |
6906 | t = itos (dollar_dollar_pid); | |
6907 | break; | |
6908 | case '!': | |
6909 | if (last_asynchronous_pid == NO_PID) | |
495aee44 | 6910 | t = (char *)NULL; /* XXX - error if set -u set? */ |
cce855bc | 6911 | else |
f73dda09 | 6912 | t = itos (last_asynchronous_pid); |
cce855bc JA |
6913 | break; |
6914 | case '#': | |
6915 | t = itos (number_of_args ()); | |
6916 | break; | |
6917 | } | |
6918 | number = STRLEN (t); | |
6919 | FREE (t); | |
6920 | } | |
ccc6cda3 | 6921 | #if defined (ARRAY_VARS) |
a0c0a00f | 6922 | else if (valid_array_reference (name + 1, 0)) |
ccc6cda3 JA |
6923 | number = array_length_reference (name + 1); |
6924 | #endif /* ARRAY_VARS */ | |
cce855bc | 6925 | else |
ccc6cda3 JA |
6926 | { |
6927 | number = 0; | |
6928 | ||
f73dda09 | 6929 | if (legal_number (name + 1, &arg_index)) /* ${#1} */ |
ccc6cda3 | 6930 | { |
f73dda09 | 6931 | t = get_dollar_var_value (arg_index); |
495aee44 CR |
6932 | if (t == 0 && unbound_vars_is_error) |
6933 | return INTMAX_MIN; | |
eb873671 | 6934 | number = MB_STRLEN (t); |
ccc6cda3 JA |
6935 | FREE (t); |
6936 | } | |
6937 | #if defined (ARRAY_VARS) | |
3185942a | 6938 | else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var))) |
ccc6cda3 | 6939 | { |
3185942a JA |
6940 | if (assoc_p (var)) |
6941 | t = assoc_reference (assoc_cell (var), "0"); | |
6942 | else | |
6943 | t = array_reference (array_cell (var), 0); | |
495aee44 CR |
6944 | if (t == 0 && unbound_vars_is_error) |
6945 | return INTMAX_MIN; | |
eb873671 | 6946 | number = MB_STRLEN (t); |
ccc6cda3 JA |
6947 | } |
6948 | #endif | |
6949 | else /* ${#PS1} */ | |
6950 | { | |
6951 | newname = savestring (name); | |
6952 | newname[0] = '$'; | |
6953 | list = expand_string (newname, Q_DOUBLE_QUOTES); | |
6954 | t = list ? string_list (list) : (char *)NULL; | |
6955 | free (newname); | |
6956 | if (list) | |
6957 | dispose_words (list); | |
6958 | ||
495aee44 | 6959 | number = t ? MB_STRLEN (t) : 0; |
ccc6cda3 JA |
6960 | FREE (t); |
6961 | } | |
6962 | } | |
ccc6cda3 JA |
6963 | |
6964 | return (number); | |
6965 | } | |
6966 | ||
28ef6c31 JA |
6967 | /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression, |
6968 | so we do some ad-hoc parsing of an arithmetic expression to find | |
6969 | the first DELIM, instead of using strchr(3). Two rules: | |
6970 | 1. If the substring contains a `(', read until closing `)'. | |
6971 | 2. If the substring contains a `?', read past one `:' for each `?'. | |
a0c0a00f | 6972 | The SD_ARITHEXP flag to skip_to_delim takes care of doing this. |
28ef6c31 JA |
6973 | */ |
6974 | ||
6975 | static char * | |
6976 | skiparith (substr, delim) | |
6977 | char *substr; | |
6978 | int delim; | |
6979 | { | |
a0c0a00f CR |
6980 | int i; |
6981 | char delims[2]; | |
28ef6c31 | 6982 | |
a0c0a00f CR |
6983 | delims[0] = delim; |
6984 | delims[1] = '\0'; | |
7117c2d2 | 6985 | |
a0c0a00f | 6986 | i = skip_to_delim (substr, 0, delims, SD_ARITHEXP); |
7117c2d2 | 6987 | return (substr + i); |
28ef6c31 JA |
6988 | } |
6989 | ||
ccc6cda3 JA |
6990 | /* Verify and limit the start and end of the desired substring. If |
6991 | VTYPE == 0, a regular shell variable is being used; if it is 1, | |
cce855bc | 6992 | then the positional parameters are being used; if it is 2, then |
e8ce775d JA |
6993 | VALUE is really a pointer to an array variable that should be used. |
6994 | Return value is 1 if both values were OK, 0 if there was a problem | |
6995 | with an invalid expression, or -1 if the values were out of range. */ | |
ccc6cda3 | 6996 | static int |
3185942a JA |
6997 | verify_substring_values (v, value, substr, vtype, e1p, e2p) |
6998 | SHELL_VAR *v; | |
ccc6cda3 | 6999 | char *value, *substr; |
f73dda09 | 7000 | int vtype; |
7117c2d2 | 7001 | intmax_t *e1p, *e2p; |
ccc6cda3 | 7002 | { |
bb70624e | 7003 | char *t, *temp1, *temp2; |
f73dda09 JA |
7004 | arrayind_t len; |
7005 | int expok; | |
ccc6cda3 JA |
7006 | #if defined (ARRAY_VARS) |
7007 | ARRAY *a; | |
3185942a | 7008 | HASH_TABLE *h; |
ccc6cda3 JA |
7009 | #endif |
7010 | ||
28ef6c31 JA |
7011 | /* duplicate behavior of strchr(3) */ |
7012 | t = skiparith (substr, ':'); | |
7013 | if (*t && *t == ':') | |
7117c2d2 | 7014 | *t = '\0'; |
28ef6c31 JA |
7015 | else |
7016 | t = (char *)0; | |
f73dda09 | 7017 | |
0628567a | 7018 | temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES); |
d166f048 | 7019 | *e1p = evalexp (temp1, &expok); |
ccc6cda3 | 7020 | free (temp1); |
d166f048 JA |
7021 | if (expok == 0) |
7022 | return (0); | |
ccc6cda3 | 7023 | |
f73dda09 | 7024 | len = -1; /* paranoia */ |
ccc6cda3 JA |
7025 | switch (vtype) |
7026 | { | |
7027 | case VT_VARIABLE: | |
d166f048 | 7028 | case VT_ARRAYMEMBER: |
eb873671 | 7029 | len = MB_STRLEN (value); |
ccc6cda3 JA |
7030 | break; |
7031 | case VT_POSPARMS: | |
7032 | len = number_of_args () + 1; | |
3185942a JA |
7033 | if (*e1p == 0) |
7034 | len++; /* add one arg if counting from $0 */ | |
ccc6cda3 JA |
7035 | break; |
7036 | #if defined (ARRAY_VARS) | |
7037 | case VT_ARRAYVAR: | |
eb873671 | 7038 | /* For arrays, the first value deals with array indices. Negative |
3185942a JA |
7039 | offsets count from one past the array's maximum index. Associative |
7040 | arrays treat the number of elements as the maximum index. */ | |
7041 | if (assoc_p (v)) | |
7042 | { | |
7043 | h = assoc_cell (v); | |
7044 | len = assoc_num_elements (h) + (*e1p < 0); | |
7045 | } | |
7046 | else | |
7047 | { | |
7048 | a = (ARRAY *)value; | |
7049 | len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */ | |
7050 | } | |
ccc6cda3 JA |
7051 | break; |
7052 | #endif | |
7053 | } | |
7054 | ||
f73dda09 JA |
7055 | if (len == -1) /* paranoia */ |
7056 | return -1; | |
7057 | ||
ccc6cda3 JA |
7058 | if (*e1p < 0) /* negative offsets count from end */ |
7059 | *e1p += len; | |
7060 | ||
eb873671 | 7061 | if (*e1p > len || *e1p < 0) |
e8ce775d | 7062 | return (-1); |
d166f048 | 7063 | |
b80f6443 JA |
7064 | #if defined (ARRAY_VARS) |
7065 | /* For arrays, the second offset deals with the number of elements. */ | |
7066 | if (vtype == VT_ARRAYVAR) | |
3185942a | 7067 | len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a); |
b80f6443 JA |
7068 | #endif |
7069 | ||
ccc6cda3 JA |
7070 | if (t) |
7071 | { | |
7072 | t++; | |
bb70624e | 7073 | temp2 = savestring (t); |
0628567a | 7074 | temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES); |
bb70624e | 7075 | free (temp2); |
ccc6cda3 | 7076 | t[-1] = ':'; |
d166f048 | 7077 | *e2p = evalexp (temp1, &expok); |
ccc6cda3 | 7078 | free (temp1); |
d166f048 | 7079 | if (expok == 0) |
28ef6c31 | 7080 | return (0); |
ac50fbac | 7081 | #if 1 |
495aee44 | 7082 | if ((vtype == VT_ARRAYVAR || vtype == VT_POSPARMS) && *e2p < 0) |
ac50fbac CR |
7083 | #else |
7084 | /* bash-4.3: allow positional parameter length < 0 to count backwards | |
7085 | from end of positional parameters */ | |
7086 | if (vtype == VT_ARRAYVAR && *e2p < 0) | |
7087 | #endif | |
28ef6c31 | 7088 | { |
b80f6443 | 7089 | internal_error (_("%s: substring expression < 0"), t); |
ccc6cda3 | 7090 | return (0); |
28ef6c31 | 7091 | } |
b80f6443 JA |
7092 | #if defined (ARRAY_VARS) |
7093 | /* In order to deal with sparse arrays, push the intelligence about how | |
7094 | to deal with the number of elements desired down to the array- | |
7095 | specific functions. */ | |
7096 | if (vtype != VT_ARRAYVAR) | |
7097 | #endif | |
7098 | { | |
495aee44 CR |
7099 | if (*e2p < 0) |
7100 | { | |
7101 | *e2p += len; | |
7102 | if (*e2p < 0 || *e2p < *e1p) | |
7103 | { | |
7104 | internal_error (_("%s: substring expression < 0"), t); | |
7105 | return (0); | |
7106 | } | |
7107 | } | |
7108 | else | |
7109 | *e2p += *e1p; /* want E2 chars starting at E1 */ | |
b80f6443 JA |
7110 | if (*e2p > len) |
7111 | *e2p = len; | |
7112 | } | |
ccc6cda3 JA |
7113 | } |
7114 | else | |
7115 | *e2p = len; | |
7116 | ||
7117 | return (1); | |
7118 | } | |
7119 | ||
ccc6cda3 | 7120 | /* Return the type of variable specified by VARNAME (simple variable, |
cce855bc | 7121 | positional param, or array variable). Also return the value specified |
7117c2d2 | 7122 | by VARNAME (value of a variable or a reference to an array element). |
495aee44 CR |
7123 | QUOTED is the standard description of quoting state, using Q_* defines. |
7124 | FLAGS is currently a set of flags to pass to array_value. If IND is | |
7125 | non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is | |
7126 | passed to array_value so the array index is not computed again. | |
7117c2d2 JA |
7127 | If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL |
7128 | characters in the value are quoted with CTLESC and takes appropriate | |
7129 | steps. For convenience, *VALP is set to the dequoted VALUE. */ | |
ccc6cda3 | 7130 | static int |
495aee44 | 7131 | get_var_and_type (varname, value, ind, quoted, flags, varp, valp) |
ccc6cda3 | 7132 | char *varname, *value; |
495aee44 CR |
7133 | arrayind_t ind; |
7134 | int quoted, flags; | |
ccc6cda3 JA |
7135 | SHELL_VAR **varp; |
7136 | char **valp; | |
7137 | { | |
ac50fbac CR |
7138 | int vtype, want_indir; |
7139 | char *temp, *vname; | |
7140 | WORD_DESC *wd; | |
ccc6cda3 | 7141 | SHELL_VAR *v; |
495aee44 | 7142 | arrayind_t lind; |
ccc6cda3 | 7143 | |
ac50fbac CR |
7144 | want_indir = *varname == '!' && |
7145 | (legal_variable_starter ((unsigned char)varname[1]) || DIGIT (varname[1]) | |
7146 | || VALID_INDIR_PARAM (varname[1])); | |
7147 | if (want_indir) | |
7148 | vname = parameter_brace_find_indir (varname+1, SPECIAL_VAR (varname, 1), quoted, 1); | |
a0c0a00f | 7149 | /* XXX - what if vname == 0 || *vname == 0 ? */ |
ac50fbac CR |
7150 | else |
7151 | vname = varname; | |
a0c0a00f CR |
7152 | |
7153 | if (vname == 0) | |
7154 | { | |
7155 | vtype = VT_VARIABLE; | |
7156 | *varp = (SHELL_VAR *)NULL; | |
7157 | *valp = (char *)NULL; | |
7158 | return (vtype); | |
7159 | } | |
7160 | ||
7117c2d2 | 7161 | /* This sets vtype to VT_VARIABLE or VT_POSPARMS */ |
a0c0a00f | 7162 | vtype = STR_DOLLAR_AT_STAR (vname); |
ac50fbac | 7163 | if (vtype == VT_POSPARMS && vname[0] == '*') |
b80f6443 | 7164 | vtype |= VT_STARSUB; |
ccc6cda3 JA |
7165 | *varp = (SHELL_VAR *)NULL; |
7166 | ||
7167 | #if defined (ARRAY_VARS) | |
a0c0a00f | 7168 | if (valid_array_reference (vname, 0)) |
ccc6cda3 | 7169 | { |
ac50fbac | 7170 | v = array_variable_part (vname, &temp, (int *)0); |
495aee44 CR |
7171 | /* If we want to signal array_value to use an already-computed index, |
7172 | set LIND to that index */ | |
7173 | lind = (ind != INTMAX_MIN && (flags & AV_USEIND)) ? ind : 0; | |
ac50fbac CR |
7174 | if (v && invisible_p (v)) |
7175 | { | |
7176 | vtype = VT_ARRAYMEMBER; | |
7177 | *varp = (SHELL_VAR *)NULL; | |
7178 | *valp = (char *)NULL; | |
7179 | } | |
3185942a | 7180 | if (v && (array_p (v) || assoc_p (v))) |
f73dda09 JA |
7181 | { /* [ */ |
7182 | if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']') | |
ccc6cda3 | 7183 | { |
ac50fbac | 7184 | /* Callers have to differentiate between indexed and associative */ |
ccc6cda3 | 7185 | vtype = VT_ARRAYVAR; |
b80f6443 JA |
7186 | if (temp[0] == '*') |
7187 | vtype |= VT_STARSUB; | |
3185942a | 7188 | *valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v); |
ccc6cda3 JA |
7189 | } |
7190 | else | |
7191 | { | |
d166f048 | 7192 | vtype = VT_ARRAYMEMBER; |
ac50fbac | 7193 | *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind); |
ccc6cda3 JA |
7194 | } |
7195 | *varp = v; | |
7196 | } | |
95732b49 JA |
7197 | else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')) |
7198 | { | |
7199 | vtype = VT_VARIABLE; | |
7200 | *varp = v; | |
7201 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
7202 | *valp = dequote_string (value); | |
7203 | else | |
7204 | *valp = dequote_escapes (value); | |
7205 | } | |
ccc6cda3 | 7206 | else |
3185942a JA |
7207 | { |
7208 | vtype = VT_ARRAYMEMBER; | |
7209 | *varp = v; | |
ac50fbac | 7210 | *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind); |
3185942a | 7211 | } |
ccc6cda3 | 7212 | } |
ac50fbac | 7213 | else if ((v = find_variable (vname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v))) |
ccc6cda3 | 7214 | { |
7117c2d2 | 7215 | vtype = VT_ARRAYMEMBER; |
ccc6cda3 | 7216 | *varp = v; |
3185942a | 7217 | *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0); |
ccc6cda3 JA |
7218 | } |
7219 | else | |
7220 | #endif | |
b80f6443 JA |
7221 | { |
7222 | if (value && vtype == VT_VARIABLE) | |
7223 | { | |
a0c0a00f | 7224 | *varp = find_variable (vname); |
b80f6443 JA |
7225 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) |
7226 | *valp = dequote_string (value); | |
7227 | else | |
7228 | *valp = dequote_escapes (value); | |
7229 | } | |
7230 | else | |
7231 | *valp = value; | |
7232 | } | |
ccc6cda3 | 7233 | |
ac50fbac CR |
7234 | if (want_indir) |
7235 | free (vname); | |
7236 | ||
ccc6cda3 JA |
7237 | return vtype; |
7238 | } | |
7239 | ||
cce855bc JA |
7240 | /******************************************************/ |
7241 | /* */ | |
7242 | /* Functions to extract substrings of variable values */ | |
7243 | /* */ | |
7244 | /******************************************************/ | |
7245 | ||
b80f6443 JA |
7246 | #if defined (HANDLE_MULTIBYTE) |
7247 | /* Character-oriented rather than strictly byte-oriented substrings. S and | |
7248 | E, rather being strict indices into STRING, indicate character (possibly | |
7249 | multibyte character) positions that require calculation. | |
7250 | Used by the ${param:offset[:length]} expansion. */ | |
7251 | static char * | |
7252 | mb_substring (string, s, e) | |
7253 | char *string; | |
7254 | int s, e; | |
7255 | { | |
7256 | char *tt; | |
a0c0a00f CR |
7257 | int start, stop, i; |
7258 | size_t slen; | |
b80f6443 JA |
7259 | DECLARE_MBSTATE; |
7260 | ||
7261 | start = 0; | |
95732b49 JA |
7262 | /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */ |
7263 | slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0; | |
b80f6443 JA |
7264 | |
7265 | i = s; | |
7266 | while (string[start] && i--) | |
7267 | ADVANCE_CHAR (string, slen, start); | |
7268 | stop = start; | |
7269 | i = e - s; | |
7270 | while (string[stop] && i--) | |
7271 | ADVANCE_CHAR (string, slen, stop); | |
7272 | tt = substring (string, start, stop); | |
7273 | return tt; | |
7274 | } | |
7275 | #endif | |
7276 | ||
ccc6cda3 JA |
7277 | /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME |
7278 | is `@', use the positional parameters; otherwise, use the value of | |
7279 | VARNAME. If VARNAME is an array variable, use the array elements. */ | |
7280 | ||
7281 | static char * | |
495aee44 CR |
7282 | parameter_brace_substring (varname, value, ind, substr, quoted, flags) |
7283 | char *varname, *value; | |
7284 | int ind; | |
7285 | char *substr; | |
7286 | int quoted, flags; | |
ccc6cda3 | 7287 | { |
7117c2d2 | 7288 | intmax_t e1, e2; |
b80f6443 | 7289 | int vtype, r, starsub; |
0628567a | 7290 | char *temp, *val, *tt, *oname; |
ccc6cda3 JA |
7291 | SHELL_VAR *v; |
7292 | ||
a0c0a00f | 7293 | if (value == 0 && ((varname[0] != '@' && varname[0] != '*') || varname[1])) |
ccc6cda3 JA |
7294 | return ((char *)NULL); |
7295 | ||
0628567a | 7296 | oname = this_command_name; |
ccc6cda3 JA |
7297 | this_command_name = varname; |
7298 | ||
495aee44 | 7299 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
ccc6cda3 | 7300 | if (vtype == -1) |
0628567a JA |
7301 | { |
7302 | this_command_name = oname; | |
7303 | return ((char *)NULL); | |
7304 | } | |
ccc6cda3 | 7305 | |
b80f6443 JA |
7306 | starsub = vtype & VT_STARSUB; |
7307 | vtype &= ~VT_STARSUB; | |
7308 | ||
3185942a | 7309 | r = verify_substring_values (v, val, substr, vtype, &e1, &e2); |
0628567a | 7310 | this_command_name = oname; |
e8ce775d | 7311 | if (r <= 0) |
ac50fbac CR |
7312 | { |
7313 | if (vtype == VT_VARIABLE) | |
7314 | FREE (val); | |
7315 | return ((r == 0) ? &expand_param_error : (char *)NULL); | |
7316 | } | |
ccc6cda3 JA |
7317 | |
7318 | switch (vtype) | |
7319 | { | |
7320 | case VT_VARIABLE: | |
d166f048 | 7321 | case VT_ARRAYMEMBER: |
b80f6443 JA |
7322 | #if defined (HANDLE_MULTIBYTE) |
7323 | if (MB_CUR_MAX > 1) | |
7324 | tt = mb_substring (val, e1, e2); | |
7325 | else | |
7326 | #endif | |
7117c2d2 | 7327 | tt = substring (val, e1, e2); |
b80f6443 | 7328 | |
7117c2d2 JA |
7329 | if (vtype == VT_VARIABLE) |
7330 | FREE (val); | |
7331 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
7332 | temp = quote_string (tt); | |
7333 | else | |
7334 | temp = tt ? quote_escapes (tt) : (char *)NULL; | |
7335 | FREE (tt); | |
ccc6cda3 JA |
7336 | break; |
7337 | case VT_POSPARMS: | |
7117c2d2 JA |
7338 | tt = pos_params (varname, e1, e2, quoted); |
7339 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0) | |
7340 | { | |
7341 | temp = tt ? quote_escapes (tt) : (char *)NULL; | |
7342 | FREE (tt); | |
7343 | } | |
7344 | else | |
7345 | temp = tt; | |
ccc6cda3 JA |
7346 | break; |
7347 | #if defined (ARRAY_VARS) | |
7348 | case VT_ARRAYVAR: | |
3185942a JA |
7349 | if (assoc_p (v)) |
7350 | /* we convert to list and take first e2 elements starting at e1th | |
7351 | element -- officially undefined for now */ | |
7352 | temp = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted); | |
7353 | else | |
b80f6443 JA |
7354 | /* We want E2 to be the number of elements desired (arrays can be sparse, |
7355 | so verify_substring_values just returns the numbers specified and we | |
7356 | rely on array_subrange to understand how to deal with them). */ | |
3185942a | 7357 | temp = array_subrange (array_cell (v), e1, e2, starsub, quoted); |
f1be666c JA |
7358 | /* array_subrange now calls array_quote_escapes as appropriate, so the |
7359 | caller no longer needs to. */ | |
ccc6cda3 JA |
7360 | break; |
7361 | #endif | |
f73dda09 JA |
7362 | default: |
7363 | temp = (char *)NULL; | |
ccc6cda3 JA |
7364 | } |
7365 | ||
7366 | return temp; | |
7367 | } | |
7368 | ||
cce855bc JA |
7369 | /****************************************************************/ |
7370 | /* */ | |
7371 | /* Functions to perform pattern substitution on variable values */ | |
7372 | /* */ | |
7373 | /****************************************************************/ | |
7374 | ||
a0c0a00f | 7375 | #if 0 /* Unused */ |
495aee44 CR |
7376 | static int |
7377 | shouldexp_replacement (s) | |
7378 | char *s; | |
7379 | { | |
7380 | register char *p; | |
7381 | ||
7382 | for (p = s; p && *p; p++) | |
7383 | { | |
7384 | if (*p == '\\') | |
7385 | p++; | |
7386 | else if (*p == '&') | |
7387 | return 1; | |
7388 | } | |
7389 | return 0; | |
7390 | } | |
a0c0a00f | 7391 | #endif |
495aee44 | 7392 | |
ccc6cda3 JA |
7393 | char * |
7394 | pat_subst (string, pat, rep, mflags) | |
7395 | char *string, *pat, *rep; | |
7396 | int mflags; | |
7397 | { | |
495aee44 | 7398 | char *ret, *s, *e, *str, *rstr, *mstr; |
a0c0a00f CR |
7399 | int rptr, mtype, rxpand, mlen; |
7400 | size_t rsize, l, replen, rslen; | |
495aee44 CR |
7401 | |
7402 | if (string == 0) | |
7403 | return (savestring ("")); | |
ccc6cda3 | 7404 | |
b72432fd JA |
7405 | mtype = mflags & MATCH_TYPEMASK; |
7406 | ||
495aee44 CR |
7407 | #if 0 /* bash-4.2 ? */ |
7408 | rxpand = (rep && *rep) ? shouldexp_replacement (rep) : 0; | |
7409 | #else | |
7410 | rxpand = 0; | |
7411 | #endif | |
7412 | ||
b72432fd JA |
7413 | /* Special cases: |
7414 | * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING | |
7415 | * with REP and return the result. | |
7416 | * 2. A null pattern with mtype == MATCH_END means to append REP to | |
7417 | * STRING and return the result. | |
a0c0a00f CR |
7418 | * 3. A null STRING with a matching pattern means to append REP to |
7419 | * STRING and return the result. | |
495aee44 | 7420 | * These don't understand or process `&' in the replacement string. |
b72432fd JA |
7421 | */ |
7422 | if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END)) | |
7423 | { | |
7424 | replen = STRLEN (rep); | |
495aee44 | 7425 | l = STRLEN (string); |
f73dda09 | 7426 | ret = (char *)xmalloc (replen + l + 2); |
bb70624e JA |
7427 | if (replen == 0) |
7428 | strcpy (ret, string); | |
7429 | else if (mtype == MATCH_BEG) | |
b72432fd JA |
7430 | { |
7431 | strcpy (ret, rep); | |
7432 | strcpy (ret + replen, string); | |
7433 | } | |
7434 | else | |
7435 | { | |
7436 | strcpy (ret, string); | |
7437 | strcpy (ret + l, rep); | |
7438 | } | |
7439 | return (ret); | |
7440 | } | |
a0c0a00f CR |
7441 | else if (*string == 0 && (match_pattern (string, pat, mtype, &s, &e) != 0)) |
7442 | { | |
7443 | replen = STRLEN (rep); | |
7444 | ret = (char *)xmalloc (replen + 1); | |
7445 | if (replen == 0) | |
7446 | ret[0] = '\0'; | |
7447 | else | |
7448 | strcpy (ret, rep); | |
7449 | return (ret); | |
7450 | } | |
b72432fd | 7451 | |
f73dda09 | 7452 | ret = (char *)xmalloc (rsize = 64); |
ccc6cda3 JA |
7453 | ret[0] = '\0'; |
7454 | ||
a0c0a00f | 7455 | for (replen = STRLEN (rep), rptr = 0, str = string; *str;) |
ccc6cda3 JA |
7456 | { |
7457 | if (match_pattern (str, pat, mtype, &s, &e) == 0) | |
7458 | break; | |
7459 | l = s - str; | |
495aee44 | 7460 | |
a0c0a00f | 7461 | if (rep && rxpand) |
495aee44 | 7462 | { |
a0c0a00f CR |
7463 | int x; |
7464 | mlen = e - s; | |
7465 | mstr = xmalloc (mlen + 1); | |
495aee44 CR |
7466 | for (x = 0; x < mlen; x++) |
7467 | mstr[x] = s[x]; | |
a0c0a00f CR |
7468 | mstr[mlen] = '\0'; |
7469 | rstr = strcreplace (rep, '&', mstr, 0); | |
7470 | free (mstr); | |
7471 | rslen = strlen (rstr); | |
495aee44 CR |
7472 | } |
7473 | else | |
a0c0a00f CR |
7474 | { |
7475 | rstr = rep; | |
7476 | rslen = replen; | |
7477 | } | |
495aee44 CR |
7478 | |
7479 | RESIZE_MALLOCED_BUFFER (ret, rptr, (l + rslen), rsize, 64); | |
ccc6cda3 JA |
7480 | |
7481 | /* OK, now copy the leading unmatched portion of the string (from | |
7482 | str to s) to ret starting at rptr (the current offset). Then copy | |
28ef6c31 JA |
7483 | the replacement string at ret + rptr + (s - str). Increment |
7484 | rptr (if necessary) and str and go on. */ | |
ccc6cda3 JA |
7485 | if (l) |
7486 | { | |
7487 | strncpy (ret + rptr, str, l); | |
7488 | rptr += l; | |
7489 | } | |
7490 | if (replen) | |
7491 | { | |
495aee44 CR |
7492 | strncpy (ret + rptr, rstr, rslen); |
7493 | rptr += rslen; | |
ccc6cda3 JA |
7494 | } |
7495 | str = e; /* e == end of match */ | |
b80f6443 | 7496 | |
495aee44 CR |
7497 | if (rstr != rep) |
7498 | free (rstr); | |
7499 | ||
ccc6cda3 | 7500 | if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY) |
28ef6c31 | 7501 | break; |
b80f6443 JA |
7502 | |
7503 | if (s == e) | |
0001803f CR |
7504 | { |
7505 | /* On a zero-length match, make sure we copy one character, since | |
7506 | we increment one character to avoid infinite recursion. */ | |
7507 | RESIZE_MALLOCED_BUFFER (ret, rptr, 1, rsize, 64); | |
7508 | ret[rptr++] = *str++; | |
7509 | e++; /* avoid infinite recursion on zero-length match */ | |
7510 | } | |
ccc6cda3 JA |
7511 | } |
7512 | ||
7513 | /* Now copy the unmatched portion of the input string */ | |
495aee44 | 7514 | if (str && *str) |
d166f048 JA |
7515 | { |
7516 | RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64); | |
7517 | strcpy (ret + rptr, str); | |
7518 | } | |
ccc6cda3 JA |
7519 | else |
7520 | ret[rptr] = '\0'; | |
7521 | ||
7522 | return ret; | |
7523 | } | |
7524 | ||
7525 | /* Do pattern match and replacement on the positional parameters. */ | |
7526 | static char * | |
7527 | pos_params_pat_subst (string, pat, rep, mflags) | |
7528 | char *string, *pat, *rep; | |
7529 | int mflags; | |
7530 | { | |
7531 | WORD_LIST *save, *params; | |
7532 | WORD_DESC *w; | |
0628567a | 7533 | char *ret; |
3185942a | 7534 | int pchar, qflags; |
ccc6cda3 JA |
7535 | |
7536 | save = params = list_rest_of_args (); | |
7537 | if (save == 0) | |
7538 | return ((char *)NULL); | |
7539 | ||
7540 | for ( ; params; params = params->next) | |
7541 | { | |
7542 | ret = pat_subst (params->word->word, pat, rep, mflags); | |
95732b49 JA |
7543 | w = alloc_word_desc (); |
7544 | w->word = ret ? ret : savestring (""); | |
ccc6cda3 JA |
7545 | dispose_word (params->word); |
7546 | params->word = w; | |
ccc6cda3 JA |
7547 | } |
7548 | ||
3185942a JA |
7549 | pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@'; |
7550 | qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0; | |
7551 | ||
3185942a | 7552 | ret = string_list_pos_params (pchar, save, qflags); |
3185942a | 7553 | |
ccc6cda3 JA |
7554 | dispose_words (save); |
7555 | ||
7556 | return (ret); | |
7557 | } | |
7558 | ||
cce855bc JA |
7559 | /* Perform pattern substitution on VALUE, which is the expansion of |
7560 | VARNAME. PATSUB is an expression supplying the pattern to match | |
7561 | and the string to substitute. QUOTED is a flags word containing | |
7562 | the type of quoting currently in effect. */ | |
ccc6cda3 | 7563 | static char * |
a0c0a00f | 7564 | parameter_brace_patsub (varname, value, ind, patsub, quoted, pflags, flags) |
495aee44 CR |
7565 | char *varname, *value; |
7566 | int ind; | |
7567 | char *patsub; | |
a0c0a00f | 7568 | int quoted, pflags, flags; |
ccc6cda3 | 7569 | { |
3185942a | 7570 | int vtype, mflags, starsub, delim; |
7117c2d2 | 7571 | char *val, *temp, *pat, *rep, *p, *lpatsub, *tt; |
ccc6cda3 JA |
7572 | SHELL_VAR *v; |
7573 | ||
7574 | if (value == 0) | |
7575 | return ((char *)NULL); | |
7576 | ||
7577 | this_command_name = varname; | |
7578 | ||
495aee44 | 7579 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
ccc6cda3 JA |
7580 | if (vtype == -1) |
7581 | return ((char *)NULL); | |
7582 | ||
b80f6443 JA |
7583 | starsub = vtype & VT_STARSUB; |
7584 | vtype &= ~VT_STARSUB; | |
7585 | ||
ccc6cda3 | 7586 | mflags = 0; |
ac50fbac CR |
7587 | /* PATSUB is never NULL when this is called. */ |
7588 | if (*patsub == '/') | |
f1be666c JA |
7589 | { |
7590 | mflags |= MATCH_GLOBREP; | |
7591 | patsub++; | |
7592 | } | |
7117c2d2 JA |
7593 | |
7594 | /* Malloc this because expand_string_if_necessary or one of the expansion | |
7595 | functions in its call chain may free it on a substitution error. */ | |
bb70624e | 7596 | lpatsub = savestring (patsub); |
ccc6cda3 JA |
7597 | |
7598 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
7599 | mflags |= MATCH_QUOTED; | |
7600 | ||
b80f6443 JA |
7601 | if (starsub) |
7602 | mflags |= MATCH_STARSUB; | |
7603 | ||
a0c0a00f CR |
7604 | if (pflags & PF_ASSIGNRHS) |
7605 | mflags |= MATCH_ASSIGNRHS; | |
7606 | ||
0628567a JA |
7607 | /* If the pattern starts with a `/', make sure we skip over it when looking |
7608 | for the replacement delimiter. */ | |
3185942a JA |
7609 | delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0); |
7610 | if (lpatsub[delim] == '/') | |
7611 | { | |
7612 | lpatsub[delim] = 0; | |
7613 | rep = lpatsub + delim + 1; | |
7614 | } | |
7615 | else | |
7616 | rep = (char *)NULL; | |
ccc6cda3 JA |
7617 | |
7618 | if (rep && *rep == '\0') | |
7619 | rep = (char *)NULL; | |
7620 | ||
b80f6443 JA |
7621 | /* Perform the same expansions on the pattern as performed by the |
7622 | pattern removal expansions. */ | |
7623 | pat = getpattern (lpatsub, quoted, 1); | |
bb70624e | 7624 | |
ccc6cda3 | 7625 | if (rep) |
d166f048 | 7626 | { |
ac50fbac CR |
7627 | /* We want to perform quote removal on the expanded replacement even if |
7628 | the entire expansion is double-quoted because the parser and string | |
7629 | extraction functions treated quotes in the replacement string as | |
7630 | special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */ | |
7631 | if (shell_compatibility_level > 42) | |
7632 | rep = expand_string_if_necessary (rep, quoted & ~(Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT), expand_string_unsplit); | |
7633 | /* This is the bash-4.2 code. */ | |
7634 | else if ((mflags & MATCH_QUOTED) == 0) | |
f73dda09 | 7635 | rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit); |
d166f048 | 7636 | else |
f73dda09 | 7637 | rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit); |
d166f048 | 7638 | } |
ccc6cda3 | 7639 | |
0628567a | 7640 | /* ksh93 doesn't allow the match specifier to be a part of the expanded |
f1be666c JA |
7641 | pattern. This is an extension. Make sure we don't anchor the pattern |
7642 | at the beginning or end of the string if we're doing global replacement, | |
7643 | though. */ | |
ccc6cda3 | 7644 | p = pat; |
f1be666c JA |
7645 | if (mflags & MATCH_GLOBREP) |
7646 | mflags |= MATCH_ANY; | |
0628567a | 7647 | else if (pat && pat[0] == '#') |
ccc6cda3 JA |
7648 | { |
7649 | mflags |= MATCH_BEG; | |
7650 | p++; | |
7651 | } | |
d166f048 | 7652 | else if (pat && pat[0] == '%') |
ccc6cda3 JA |
7653 | { |
7654 | mflags |= MATCH_END; | |
7655 | p++; | |
7656 | } | |
7657 | else | |
7658 | mflags |= MATCH_ANY; | |
7659 | ||
cce855bc JA |
7660 | /* OK, we now want to substitute REP for PAT in VAL. If |
7661 | flags & MATCH_GLOBREP is non-zero, the substitution is done | |
7662 | everywhere, otherwise only the first occurrence of PAT is | |
7117c2d2 JA |
7663 | replaced. The pattern matching code doesn't understand |
7664 | CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable | |
7665 | values passed in (VT_VARIABLE) so the pattern substitution | |
7666 | code works right. We need to requote special chars after | |
7667 | we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the | |
7668 | other cases if QUOTED == 0, since the posparams and arrays | |
7669 | indexed by * or @ do special things when QUOTED != 0. */ | |
7670 | ||
ccc6cda3 JA |
7671 | switch (vtype) |
7672 | { | |
7673 | case VT_VARIABLE: | |
d166f048 | 7674 | case VT_ARRAYMEMBER: |
ccc6cda3 | 7675 | temp = pat_subst (val, p, rep, mflags); |
7117c2d2 JA |
7676 | if (vtype == VT_VARIABLE) |
7677 | FREE (val); | |
7678 | if (temp) | |
7679 | { | |
3185942a | 7680 | tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp); |
7117c2d2 JA |
7681 | free (temp); |
7682 | temp = tt; | |
7683 | } | |
ccc6cda3 JA |
7684 | break; |
7685 | case VT_POSPARMS: | |
7686 | temp = pos_params_pat_subst (val, p, rep, mflags); | |
7117c2d2 JA |
7687 | if (temp && (mflags & MATCH_QUOTED) == 0) |
7688 | { | |
7689 | tt = quote_escapes (temp); | |
7690 | free (temp); | |
7691 | temp = tt; | |
7692 | } | |
ccc6cda3 JA |
7693 | break; |
7694 | #if defined (ARRAY_VARS) | |
7695 | case VT_ARRAYVAR: | |
3185942a JA |
7696 | temp = assoc_p (v) ? assoc_patsub (assoc_cell (v), p, rep, mflags) |
7697 | : array_patsub (array_cell (v), p, rep, mflags); | |
7698 | /* Don't call quote_escapes anymore; array_patsub calls | |
7699 | array_quote_escapes as appropriate before adding the | |
7700 | space separators; ditto for assoc_patsub. */ | |
7701 | break; | |
7702 | #endif | |
7703 | } | |
7704 | ||
7705 | FREE (pat); | |
7706 | FREE (rep); | |
7707 | free (lpatsub); | |
7708 | ||
7709 | return temp; | |
7710 | } | |
7711 | ||
7712 | /****************************************************************/ | |
7713 | /* */ | |
7714 | /* Functions to perform case modification on variable values */ | |
7715 | /* */ | |
7716 | /****************************************************************/ | |
7717 | ||
7718 | /* Do case modification on the positional parameters. */ | |
7719 | ||
7720 | static char * | |
7721 | pos_params_modcase (string, pat, modop, mflags) | |
7722 | char *string, *pat; | |
7723 | int modop; | |
7724 | int mflags; | |
7725 | { | |
7726 | WORD_LIST *save, *params; | |
7727 | WORD_DESC *w; | |
7728 | char *ret; | |
7729 | int pchar, qflags; | |
7730 | ||
7731 | save = params = list_rest_of_args (); | |
7732 | if (save == 0) | |
7733 | return ((char *)NULL); | |
7734 | ||
7735 | for ( ; params; params = params->next) | |
7736 | { | |
7737 | ret = sh_modcase (params->word->word, pat, modop); | |
7738 | w = alloc_word_desc (); | |
7739 | w->word = ret ? ret : savestring (""); | |
7740 | dispose_word (params->word); | |
7741 | params->word = w; | |
7742 | } | |
7743 | ||
7744 | pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@'; | |
7745 | qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0; | |
7746 | ||
7747 | ret = string_list_pos_params (pchar, save, qflags); | |
7748 | dispose_words (save); | |
7749 | ||
7750 | return (ret); | |
7751 | } | |
7752 | ||
7753 | /* Perform case modification on VALUE, which is the expansion of | |
7754 | VARNAME. MODSPEC is an expression supplying the type of modification | |
7755 | to perform. QUOTED is a flags word containing the type of quoting | |
7756 | currently in effect. */ | |
7757 | static char * | |
495aee44 | 7758 | parameter_brace_casemod (varname, value, ind, modspec, patspec, quoted, flags) |
3185942a | 7759 | char *varname, *value; |
495aee44 | 7760 | int ind, modspec; |
3185942a | 7761 | char *patspec; |
495aee44 | 7762 | int quoted, flags; |
3185942a JA |
7763 | { |
7764 | int vtype, starsub, modop, mflags, x; | |
7765 | char *val, *temp, *pat, *p, *lpat, *tt; | |
7766 | SHELL_VAR *v; | |
7767 | ||
7768 | if (value == 0) | |
7769 | return ((char *)NULL); | |
7770 | ||
7771 | this_command_name = varname; | |
7772 | ||
495aee44 | 7773 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
3185942a JA |
7774 | if (vtype == -1) |
7775 | return ((char *)NULL); | |
7776 | ||
7777 | starsub = vtype & VT_STARSUB; | |
7778 | vtype &= ~VT_STARSUB; | |
7779 | ||
7780 | modop = 0; | |
7781 | mflags = 0; | |
7782 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
7783 | mflags |= MATCH_QUOTED; | |
7784 | if (starsub) | |
7785 | mflags |= MATCH_STARSUB; | |
7786 | ||
7787 | p = patspec; | |
7788 | if (modspec == '^') | |
7789 | { | |
7790 | x = p && p[0] == modspec; | |
17345e5a | 7791 | modop = x ? CASE_UPPER : CASE_UPFIRST; |
3185942a JA |
7792 | p += x; |
7793 | } | |
7794 | else if (modspec == ',') | |
7795 | { | |
7796 | x = p && p[0] == modspec; | |
17345e5a | 7797 | modop = x ? CASE_LOWER : CASE_LOWFIRST; |
3185942a JA |
7798 | p += x; |
7799 | } | |
7800 | else if (modspec == '~') | |
7801 | { | |
7802 | x = p && p[0] == modspec; | |
7803 | modop = x ? CASE_TOGGLEALL : CASE_TOGGLE; | |
7804 | p += x; | |
7805 | } | |
7806 | ||
7807 | lpat = p ? savestring (p) : 0; | |
7808 | /* Perform the same expansions on the pattern as performed by the | |
7809 | pattern removal expansions. FOR LATER */ | |
7810 | pat = lpat ? getpattern (lpat, quoted, 1) : 0; | |
7811 | ||
7812 | /* OK, now we do the case modification. */ | |
7813 | switch (vtype) | |
7814 | { | |
7815 | case VT_VARIABLE: | |
7816 | case VT_ARRAYMEMBER: | |
7817 | temp = sh_modcase (val, pat, modop); | |
7818 | if (vtype == VT_VARIABLE) | |
7819 | FREE (val); | |
7820 | if (temp) | |
7821 | { | |
7822 | tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp); | |
7823 | free (temp); | |
7824 | temp = tt; | |
7825 | } | |
7826 | break; | |
7827 | ||
7828 | case VT_POSPARMS: | |
7829 | temp = pos_params_modcase (val, pat, modop, mflags); | |
7830 | if (temp && (mflags & MATCH_QUOTED) == 0) | |
7117c2d2 JA |
7831 | { |
7832 | tt = quote_escapes (temp); | |
7833 | free (temp); | |
7834 | temp = tt; | |
7835 | } | |
3185942a JA |
7836 | break; |
7837 | ||
7838 | #if defined (ARRAY_VARS) | |
7839 | case VT_ARRAYVAR: | |
7840 | temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags) | |
7841 | : array_modcase (array_cell (v), pat, modop, mflags); | |
7842 | /* Don't call quote_escapes; array_modcase calls array_quote_escapes | |
7843 | as appropriate before adding the space separators; ditto for | |
7844 | assoc_modcase. */ | |
ccc6cda3 JA |
7845 | break; |
7846 | #endif | |
7847 | } | |
7848 | ||
7849 | FREE (pat); | |
3185942a | 7850 | free (lpat); |
ccc6cda3 JA |
7851 | |
7852 | return temp; | |
7853 | } | |
7854 | ||
0628567a JA |
7855 | /* Check for unbalanced parens in S, which is the contents of $(( ... )). If |
7856 | any occur, this must be a nested command substitution, so return 0. | |
7857 | Otherwise, return 1. A valid arithmetic expression must always have a | |
7858 | ( before a matching ), so any cases where there are more right parens | |
7859 | means that this must not be an arithmetic expression, though the parser | |
7860 | will not accept it without a balanced total number of parens. */ | |
7861 | static int | |
7862 | chk_arithsub (s, len) | |
7863 | const char *s; | |
7864 | int len; | |
7865 | { | |
7866 | int i, count; | |
7867 | DECLARE_MBSTATE; | |
7868 | ||
7869 | i = count = 0; | |
7870 | while (i < len) | |
7871 | { | |
0001803f | 7872 | if (s[i] == LPAREN) |
0628567a | 7873 | count++; |
0001803f | 7874 | else if (s[i] == RPAREN) |
0628567a JA |
7875 | { |
7876 | count--; | |
7877 | if (count < 0) | |
7878 | return 0; | |
7879 | } | |
7880 | ||
7881 | switch (s[i]) | |
7882 | { | |
7883 | default: | |
7884 | ADVANCE_CHAR (s, len, i); | |
7885 | break; | |
7886 | ||
7887 | case '\\': | |
7888 | i++; | |
7889 | if (s[i]) | |
7890 | ADVANCE_CHAR (s, len, i); | |
7891 | break; | |
7892 | ||
7893 | case '\'': | |
a0c0a00f | 7894 | i = skip_single_quoted (s, len, ++i, 0); |
0628567a JA |
7895 | break; |
7896 | ||
7897 | case '"': | |
a0c0a00f | 7898 | i = skip_double_quoted ((char *)s, len, ++i, 0); |
0628567a JA |
7899 | break; |
7900 | } | |
7901 | } | |
7902 | ||
7903 | return (count == 0); | |
7904 | } | |
7905 | ||
cce855bc JA |
7906 | /****************************************************************/ |
7907 | /* */ | |
7908 | /* Functions to perform parameter expansion on a string */ | |
7909 | /* */ | |
7910 | /****************************************************************/ | |
7911 | ||
3185942a | 7912 | /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */ |
95732b49 | 7913 | static WORD_DESC * |
0001803f | 7914 | parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at) |
ccc6cda3 | 7915 | char *string; |
a0c0a00f | 7916 | int *indexp, quoted, pflags, *quoted_dollar_atp, *contains_dollar_at; |
ccc6cda3 JA |
7917 | { |
7918 | int check_nullness, var_is_set, var_is_null, var_is_special; | |
3185942a | 7919 | int want_substring, want_indir, want_patsub, want_casemod; |
ccc6cda3 | 7920 | char *name, *value, *temp, *temp1; |
95732b49 | 7921 | WORD_DESC *tdesc, *ret; |
a0c0a00f | 7922 | int t_index, sindex, c, tflag, modspec, all_element_arrayref; |
7117c2d2 | 7923 | intmax_t number; |
495aee44 | 7924 | arrayind_t ind; |
ccc6cda3 | 7925 | |
3185942a | 7926 | temp = temp1 = value = (char *)NULL; |
ccc6cda3 | 7927 | var_is_set = var_is_null = var_is_special = check_nullness = 0; |
3185942a | 7928 | want_substring = want_indir = want_patsub = want_casemod = 0; |
ccc6cda3 | 7929 | |
a0c0a00f CR |
7930 | all_element_arrayref = 0; |
7931 | ||
cce855bc JA |
7932 | sindex = *indexp; |
7933 | t_index = ++sindex; | |
0628567a JA |
7934 | /* ${#var} doesn't have any of the other parameter expansions on it. */ |
7935 | if (string[t_index] == '#' && legal_variable_starter (string[t_index+1])) /* {{ */ | |
3185942a | 7936 | name = string_extract (string, &t_index, "}", SX_VARNAME); |
0628567a | 7937 | else |
3185942a JA |
7938 | #if defined (CASEMOD_EXPANSIONS) |
7939 | /* To enable case-toggling expansions using the `~' operator character | |
7940 | change the 1 to 0. */ | |
7941 | # if defined (CASEMOD_CAPCASE) | |
a0c0a00f | 7942 | name = string_extract (string, &t_index, "#%^,~:-=?+/@}", SX_VARNAME); |
3185942a | 7943 | # else |
a0c0a00f | 7944 | name = string_extract (string, &t_index, "#%^,:-=?+/@}", SX_VARNAME); |
3185942a JA |
7945 | # endif /* CASEMOD_CAPCASE */ |
7946 | #else | |
a0c0a00f | 7947 | name = string_extract (string, &t_index, "#%:-=?+/@}", SX_VARNAME); |
3185942a | 7948 | #endif /* CASEMOD_EXPANSIONS */ |
cce855bc | 7949 | |
a0c0a00f CR |
7950 | /* Handle ${@[stuff]} now that @ is a word expansion operator. Not exactly |
7951 | the cleanest code ever. */ | |
7952 | if (*name == 0 && sindex == t_index && string[sindex] == '@') | |
7953 | { | |
7954 | name = (char *)xrealloc (name, 2); | |
7955 | name[0] = '@'; | |
7956 | name[1] = '\0'; | |
7957 | t_index++; | |
7958 | } | |
7959 | else if (*name == '!' && t_index > sindex && string[t_index] == '@' && string[t_index+1] == '}') | |
7960 | { | |
7961 | name = (char *)xrealloc (name, t_index - sindex + 2); | |
7962 | name[t_index - sindex] = '@'; | |
7963 | name[t_index - sindex + 1] = '\0'; | |
7964 | t_index++; | |
7965 | } | |
7966 | ||
95732b49 JA |
7967 | ret = 0; |
7968 | tflag = 0; | |
7969 | ||
495aee44 CR |
7970 | ind = INTMAX_MIN; |
7971 | ||
cce855bc JA |
7972 | /* If the name really consists of a special variable, then make sure |
7973 | that we have the entire name. We don't allow indirect references | |
a0c0a00f CR |
7974 | to special variables except `#', `?', `@' and `*'. This clause is |
7975 | designed to handle ${#SPECIAL} and ${!SPECIAL}, not anything more | |
7976 | general. */ | |
495aee44 | 7977 | if ((sindex == t_index && VALID_SPECIAL_LENGTH_PARAM (string[t_index])) || |
a0c0a00f | 7978 | (sindex == t_index && string[sindex] == '#' && VALID_SPECIAL_LENGTH_PARAM (string[sindex + 1])) || |
495aee44 | 7979 | (sindex == t_index - 1 && string[sindex] == '!' && VALID_INDIR_PARAM (string[t_index]))) |
ccc6cda3 JA |
7980 | { |
7981 | t_index++; | |
a0c0a00f | 7982 | temp1 = string_extract (string, &t_index, "#%:-=?+/@}", 0); |
ac50fbac | 7983 | name = (char *)xrealloc (name, 3 + (strlen (temp1))); |
ccc6cda3 JA |
7984 | *name = string[sindex]; |
7985 | if (string[sindex] == '!') | |
7986 | { | |
28ef6c31 JA |
7987 | /* indirect reference of $#, $?, $@, or $* */ |
7988 | name[1] = string[sindex + 1]; | |
7989 | strcpy (name + 2, temp1); | |
ccc6cda3 | 7990 | } |
cce855bc | 7991 | else |
ccc6cda3 JA |
7992 | strcpy (name + 1, temp1); |
7993 | free (temp1); | |
7994 | } | |
7995 | sindex = t_index; | |
7996 | ||
7997 | /* Find out what character ended the variable name. Then | |
7998 | do the appropriate thing. */ | |
7999 | if (c = string[sindex]) | |
8000 | sindex++; | |
8001 | ||
8002 | /* If c is followed by one of the valid parameter expansion | |
8003 | characters, move past it as normal. If not, assume that | |
8004 | a substring specification is being given, and do not move | |
8005 | past it. */ | |
28ef6c31 | 8006 | if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex])) |
ccc6cda3 JA |
8007 | { |
8008 | check_nullness++; | |
8009 | if (c = string[sindex]) | |
8010 | sindex++; | |
8011 | } | |
cce855bc | 8012 | else if (c == ':' && string[sindex] != RBRACE) |
ccc6cda3 | 8013 | want_substring = 1; |
ac50fbac | 8014 | else if (c == '/' /* && string[sindex] != RBRACE */) /* XXX */ |
ccc6cda3 | 8015 | want_patsub = 1; |
3185942a JA |
8016 | #if defined (CASEMOD_EXPANSIONS) |
8017 | else if (c == '^' || c == ',' || c == '~') | |
8018 | { | |
8019 | modspec = c; | |
8020 | want_casemod = 1; | |
8021 | } | |
8022 | #endif | |
ccc6cda3 | 8023 | |
cce855bc JA |
8024 | /* Catch the valid and invalid brace expressions that made it through the |
8025 | tests above. */ | |
8026 | /* ${#-} is a valid expansion and means to take the length of $-. | |
8027 | Similarly for ${#?} and ${##}... */ | |
8028 | if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 && | |
28ef6c31 | 8029 | VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE) |
cce855bc | 8030 | { |
f73dda09 | 8031 | name = (char *)xrealloc (name, 3); |
cce855bc JA |
8032 | name[1] = c; |
8033 | name[2] = '\0'; | |
8034 | c = string[sindex++]; | |
8035 | } | |
8036 | ||
8037 | /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */ | |
8038 | if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 && | |
8039 | member (c, "%:=+/") && string[sindex] == RBRACE) | |
8040 | { | |
8041 | temp = (char *)NULL; | |
a0c0a00f | 8042 | goto bad_substitution; /* XXX - substitution error */ |
cce855bc JA |
8043 | } |
8044 | ||
8045 | /* Indirect expansion begins with a `!'. A valid indirect expansion is | |
8046 | either a variable name, one of the positional parameters or a special | |
8047 | variable that expands to one of the positional parameters. */ | |
8048 | want_indir = *name == '!' && | |
f73dda09 | 8049 | (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) |
7117c2d2 | 8050 | || VALID_INDIR_PARAM (name[1])); |
ccc6cda3 JA |
8051 | |
8052 | /* Determine the value of this variable. */ | |
8053 | ||
cce855bc | 8054 | /* Check for special variables, directly referenced. */ |
bb70624e | 8055 | if (SPECIAL_VAR (name, want_indir)) |
ccc6cda3 JA |
8056 | var_is_special++; |
8057 | ||
cce855bc JA |
8058 | /* Check for special expansion things, like the length of a parameter */ |
8059 | if (*name == '#' && name[1]) | |
ccc6cda3 | 8060 | { |
cce855bc | 8061 | /* If we are not pointing at the character just after the |
28ef6c31 JA |
8062 | closing brace, then we haven't gotten all of the name. |
8063 | Since it begins with a special character, this is a bad | |
8064 | substitution. Also check NAME for validity before trying | |
8065 | to go on. */ | |
cce855bc | 8066 | if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0)) |
ccc6cda3 JA |
8067 | { |
8068 | temp = (char *)NULL; | |
a0c0a00f | 8069 | goto bad_substitution; /* substitution error */ |
ccc6cda3 JA |
8070 | } |
8071 | ||
8072 | number = parameter_brace_expand_length (name); | |
495aee44 CR |
8073 | if (number == INTMAX_MIN && unbound_vars_is_error) |
8074 | { | |
8075 | last_command_exit_value = EXECUTION_FAILURE; | |
8076 | err_unboundvar (name+1); | |
8077 | free (name); | |
8078 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); | |
8079 | } | |
ccc6cda3 JA |
8080 | free (name); |
8081 | ||
8082 | *indexp = sindex; | |
95732b49 JA |
8083 | if (number < 0) |
8084 | return (&expand_wdesc_error); | |
8085 | else | |
8086 | { | |
8087 | ret = alloc_word_desc (); | |
8088 | ret->word = itos (number); | |
8089 | return ret; | |
8090 | } | |
ccc6cda3 JA |
8091 | } |
8092 | ||
8093 | /* ${@} is identical to $@. */ | |
8094 | if (name[0] == '@' && name[1] == '\0') | |
8095 | { | |
8096 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
8097 | *quoted_dollar_atp = 1; | |
8098 | ||
8099 | if (contains_dollar_at) | |
8100 | *contains_dollar_at = 1; | |
ac50fbac CR |
8101 | |
8102 | tflag |= W_DOLLARAT; | |
ccc6cda3 JA |
8103 | } |
8104 | ||
b80f6443 | 8105 | /* Process ${!PREFIX*} expansion. */ |
bb70624e JA |
8106 | if (want_indir && string[sindex - 1] == RBRACE && |
8107 | (string[sindex - 2] == '*' || string[sindex - 2] == '@') && | |
f73dda09 | 8108 | legal_variable_starter ((unsigned char) name[1])) |
bb70624e JA |
8109 | { |
8110 | char **x; | |
8111 | WORD_LIST *xlist; | |
8112 | ||
8113 | temp1 = savestring (name + 1); | |
8114 | number = strlen (temp1); | |
8115 | temp1[number - 1] = '\0'; | |
8116 | x = all_variables_matching_prefix (temp1); | |
7117c2d2 | 8117 | xlist = strvec_to_word_list (x, 0, 0); |
28ef6c31 JA |
8118 | if (string[sindex - 2] == '*') |
8119 | temp = string_list_dollar_star (xlist); | |
8120 | else | |
8121 | { | |
a0c0a00f | 8122 | temp = string_list_dollar_at (xlist, quoted, 0); |
28ef6c31 JA |
8123 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) |
8124 | *quoted_dollar_atp = 1; | |
8125 | if (contains_dollar_at) | |
8126 | *contains_dollar_at = 1; | |
ac50fbac CR |
8127 | |
8128 | tflag |= W_DOLLARAT; | |
28ef6c31 | 8129 | } |
bb70624e | 8130 | free (x); |
89a92869 | 8131 | dispose_words (xlist); |
bb70624e JA |
8132 | free (temp1); |
8133 | *indexp = sindex; | |
95732b49 | 8134 | |
ac50fbac CR |
8135 | free (name); |
8136 | ||
95732b49 JA |
8137 | ret = alloc_word_desc (); |
8138 | ret->word = temp; | |
ac50fbac | 8139 | ret->flags = tflag; /* XXX */ |
95732b49 | 8140 | return ret; |
bb70624e | 8141 | } |
b80f6443 JA |
8142 | |
8143 | #if defined (ARRAY_VARS) | |
8144 | /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */ | |
8145 | if (want_indir && string[sindex - 1] == RBRACE && | |
a0c0a00f | 8146 | string[sindex - 2] == ']' && valid_array_reference (name+1, 0)) |
b80f6443 JA |
8147 | { |
8148 | char *x, *x1; | |
8149 | ||
8150 | temp1 = savestring (name + 1); | |
8151 | x = array_variable_name (temp1, &x1, (int *)0); /* [ */ | |
8152 | FREE (x); | |
8153 | if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == ']') | |
8154 | { | |
3185942a | 8155 | temp = array_keys (temp1, quoted); /* handles assoc vars too */ |
b80f6443 JA |
8156 | if (x1[0] == '@') |
8157 | { | |
8158 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
8159 | *quoted_dollar_atp = 1; | |
8160 | if (contains_dollar_at) | |
8161 | *contains_dollar_at = 1; | |
ac50fbac CR |
8162 | |
8163 | tflag |= W_DOLLARAT; | |
b80f6443 JA |
8164 | } |
8165 | ||
a0c0a00f | 8166 | free (name); |
b80f6443 JA |
8167 | free (temp1); |
8168 | *indexp = sindex; | |
95732b49 JA |
8169 | |
8170 | ret = alloc_word_desc (); | |
8171 | ret->word = temp; | |
ac50fbac | 8172 | ret->flags = tflag; /* XXX */ |
95732b49 | 8173 | return ret; |
b80f6443 JA |
8174 | } |
8175 | ||
8176 | free (temp1); | |
8177 | } | |
8178 | #endif /* ARRAY_VARS */ | |
bb70624e | 8179 | |
ccc6cda3 JA |
8180 | /* Make sure that NAME is valid before trying to go on. */ |
8181 | if (valid_brace_expansion_word (want_indir ? name + 1 : name, | |
8182 | var_is_special) == 0) | |
8183 | { | |
8184 | temp = (char *)NULL; | |
a0c0a00f | 8185 | goto bad_substitution; /* substitution error */ |
ccc6cda3 JA |
8186 | } |
8187 | ||
8188 | if (want_indir) | |
1a1f8b54 CR |
8189 | { |
8190 | tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at); | |
a0c0a00f CR |
8191 | if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal) |
8192 | { | |
8193 | temp = (char *)NULL; | |
8194 | goto bad_substitution; | |
8195 | } | |
1a1f8b54 CR |
8196 | /* Turn off the W_ARRAYIND flag because there is no way for this function |
8197 | to return the index we're supposed to be using. */ | |
8198 | if (tdesc && tdesc->flags) | |
8199 | tdesc->flags &= ~W_ARRAYIND; | |
8200 | } | |
95732b49 | 8201 | else |
ac50fbac | 8202 | tdesc = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND|(pflags&(PF_NOSPLIT2|PF_ASSIGNRHS)), &ind); |
95732b49 JA |
8203 | |
8204 | if (tdesc) | |
8205 | { | |
8206 | temp = tdesc->word; | |
8207 | tflag = tdesc->flags; | |
8208 | dispose_word_desc (tdesc); | |
8209 | } | |
ccc6cda3 | 8210 | else |
95732b49 | 8211 | temp = (char *)0; |
ccc6cda3 | 8212 | |
ac50fbac CR |
8213 | if (temp == &expand_param_error || temp == &expand_param_fatal) |
8214 | { | |
8215 | FREE (name); | |
8216 | FREE (value); | |
8217 | return (temp == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal); | |
8218 | } | |
8219 | ||
ccc6cda3 | 8220 | #if defined (ARRAY_VARS) |
a0c0a00f CR |
8221 | if (valid_array_reference (name, 0)) |
8222 | { | |
8223 | int qflags; | |
8224 | char *t; | |
8225 | ||
8226 | qflags = quoted; | |
8227 | /* If in a context where word splitting will not take place, treat as | |
8228 | if double-quoted. Has effects with $* and ${array[*]} */ | |
8229 | if (pflags & PF_ASSIGNRHS) | |
8230 | qflags |= Q_DOUBLE_QUOTES; | |
8231 | chk_atstar (name, qflags, quoted_dollar_atp, contains_dollar_at); | |
8232 | /* We duplicate a little code here */ | |
8233 | t = mbschr (name, '['); | |
8234 | if (t && ALL_ELEMENT_SUB (t[1]) && t[2] == ']') | |
8235 | all_element_arrayref = 1; | |
8236 | } | |
ccc6cda3 JA |
8237 | #endif |
8238 | ||
8239 | var_is_set = temp != (char *)0; | |
8240 | var_is_null = check_nullness && (var_is_set == 0 || *temp == 0); | |
ac50fbac CR |
8241 | /* XXX - this may not need to be restricted to special variables */ |
8242 | if (check_nullness) | |
8243 | var_is_null |= var_is_set && var_is_special && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp); | |
ccc6cda3 JA |
8244 | |
8245 | /* Get the rest of the stuff inside the braces. */ | |
cce855bc | 8246 | if (c && c != RBRACE) |
ccc6cda3 JA |
8247 | { |
8248 | /* Extract the contents of the ${ ... } expansion | |
28ef6c31 | 8249 | according to the Posix.2 rules. */ |
49ed961b | 8250 | value = extract_dollar_brace_string (string, &sindex, quoted, (c == '%' || c == '#' || c =='/' || c == '^' || c == ',' || c ==':') ? SX_POSIXEXP|SX_WORD : SX_WORD); |
cce855bc | 8251 | if (string[sindex] == RBRACE) |
28ef6c31 | 8252 | sindex++; |
ccc6cda3 | 8253 | else |
a0c0a00f | 8254 | goto bad_substitution; /* substitution error */ |
ccc6cda3 JA |
8255 | } |
8256 | else | |
8257 | value = (char *)NULL; | |
726f6388 | 8258 | |
ccc6cda3 JA |
8259 | *indexp = sindex; |
8260 | ||
495aee44 CR |
8261 | /* All the cases where an expansion can possibly generate an unbound |
8262 | variable error. */ | |
8263 | if (want_substring || want_patsub || want_casemod || c == '#' || c == '%' || c == RBRACE) | |
8264 | { | |
a0c0a00f | 8265 | if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1]) && all_element_arrayref == 0) |
495aee44 CR |
8266 | { |
8267 | last_command_exit_value = EXECUTION_FAILURE; | |
8268 | err_unboundvar (name); | |
8269 | FREE (value); | |
8270 | FREE (temp); | |
8271 | free (name); | |
8272 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); | |
8273 | } | |
8274 | } | |
8275 | ||
ccc6cda3 JA |
8276 | /* If this is a substring spec, process it and add the result. */ |
8277 | if (want_substring) | |
726f6388 | 8278 | { |
495aee44 | 8279 | temp1 = parameter_brace_substring (name, temp, ind, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
ccc6cda3 JA |
8280 | FREE (name); |
8281 | FREE (value); | |
8282 | FREE (temp); | |
95732b49 JA |
8283 | |
8284 | if (temp1 == &expand_param_error) | |
8285 | return (&expand_wdesc_error); | |
8286 | else if (temp1 == &expand_param_fatal) | |
8287 | return (&expand_wdesc_fatal); | |
8288 | ||
8289 | ret = alloc_word_desc (); | |
8290 | ret->word = temp1; | |
ac50fbac CR |
8291 | /* We test quoted_dollar_atp because we want variants with double-quoted |
8292 | "$@" to take a different code path. In fact, we make sure at the end | |
8293 | of expand_word_internal that we're only looking at these flags if | |
8294 | quoted_dollar_at == 0. */ | |
8295 | if (temp1 && | |
8296 | (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) && | |
8297 | QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
0628567a | 8298 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; |
95732b49 | 8299 | return ret; |
726f6388 | 8300 | } |
ccc6cda3 | 8301 | else if (want_patsub) |
726f6388 | 8302 | { |
a0c0a00f | 8303 | temp1 = parameter_brace_patsub (name, temp, ind, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
ccc6cda3 JA |
8304 | FREE (name); |
8305 | FREE (value); | |
8306 | FREE (temp); | |
95732b49 JA |
8307 | |
8308 | if (temp1 == &expand_param_error) | |
8309 | return (&expand_wdesc_error); | |
8310 | else if (temp1 == &expand_param_fatal) | |
8311 | return (&expand_wdesc_fatal); | |
8312 | ||
8313 | ret = alloc_word_desc (); | |
8314 | ret->word = temp1; | |
ac50fbac CR |
8315 | if (temp1 && |
8316 | (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) && | |
8317 | QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
3185942a | 8318 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; |
95732b49 | 8319 | return ret; |
ccc6cda3 | 8320 | } |
3185942a JA |
8321 | #if defined (CASEMOD_EXPANSIONS) |
8322 | else if (want_casemod) | |
8323 | { | |
495aee44 | 8324 | temp1 = parameter_brace_casemod (name, temp, ind, modspec, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
3185942a JA |
8325 | FREE (name); |
8326 | FREE (value); | |
8327 | FREE (temp); | |
8328 | ||
8329 | if (temp1 == &expand_param_error) | |
8330 | return (&expand_wdesc_error); | |
8331 | else if (temp1 == &expand_param_fatal) | |
8332 | return (&expand_wdesc_fatal); | |
8333 | ||
8334 | ret = alloc_word_desc (); | |
8335 | ret->word = temp1; | |
ac50fbac CR |
8336 | if (temp1 && |
8337 | (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) && | |
8338 | QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
3185942a JA |
8339 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; |
8340 | return ret; | |
8341 | } | |
8342 | #endif | |
726f6388 | 8343 | |
ccc6cda3 JA |
8344 | /* Do the right thing based on which character ended the variable name. */ |
8345 | switch (c) | |
8346 | { | |
8347 | default: | |
8348 | case '\0': | |
a0c0a00f | 8349 | bad_substitution: |
98043138 | 8350 | last_command_exit_value = EXECUTION_FAILURE; |
b80f6443 | 8351 | report_error (_("%s: bad substitution"), string ? string : "??"); |
ccc6cda3 JA |
8352 | FREE (value); |
8353 | FREE (temp); | |
8354 | free (name); | |
a0c0a00f CR |
8355 | if (shell_compatibility_level <= 43) |
8356 | return &expand_wdesc_error; | |
8357 | else | |
8358 | return ((posixly_correct && interactive_shell == 0) ? &expand_wdesc_fatal : &expand_wdesc_error); | |
ccc6cda3 | 8359 | |
cce855bc | 8360 | case RBRACE: |
ccc6cda3 | 8361 | break; |
726f6388 | 8362 | |
a0c0a00f CR |
8363 | case '@': |
8364 | temp1 = parameter_brace_transform (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0); | |
8365 | free (temp); | |
8366 | free (value); | |
8367 | free (name); | |
8368 | if (temp1 == &expand_param_error || temp1 == &expand_param_fatal) | |
8369 | { | |
8370 | last_command_exit_value = EXECUTION_FAILURE; | |
8371 | report_error (_("%s: bad substitution"), string ? string : "??"); | |
8372 | return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal); | |
8373 | } | |
8374 | ||
8375 | ret = alloc_word_desc (); | |
8376 | ret->word = temp1; | |
8377 | if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
8378 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; | |
8379 | return ret; | |
8380 | ||
ccc6cda3 JA |
8381 | case '#': /* ${param#[#]pattern} */ |
8382 | case '%': /* ${param%[%]pattern} */ | |
8383 | if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0') | |
28ef6c31 JA |
8384 | { |
8385 | FREE (value); | |
ccc6cda3 | 8386 | break; |
28ef6c31 | 8387 | } |
495aee44 | 8388 | temp1 = parameter_brace_remove_pattern (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
ccc6cda3 JA |
8389 | free (temp); |
8390 | free (value); | |
495aee44 | 8391 | free (name); |
3185942a JA |
8392 | |
8393 | ret = alloc_word_desc (); | |
8394 | ret->word = temp1; | |
8395 | if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
8396 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; | |
8397 | return ret; | |
ccc6cda3 JA |
8398 | |
8399 | case '-': | |
8400 | case '=': | |
8401 | case '?': | |
8402 | case '+': | |
8403 | if (var_is_set && var_is_null == 0) | |
28ef6c31 JA |
8404 | { |
8405 | /* If the operator is `+', we don't want the value of the named | |
8406 | variable for anything, just the value of the right hand side. */ | |
ccc6cda3 JA |
8407 | if (c == '+') |
8408 | { | |
28ef6c31 JA |
8409 | /* XXX -- if we're double-quoted and the named variable is "$@", |
8410 | we want to turn off any special handling of "$@" -- | |
8411 | we're not using it, so whatever is on the rhs applies. */ | |
8412 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
8413 | *quoted_dollar_atp = 0; | |
8414 | if (contains_dollar_at) | |
8415 | *contains_dollar_at = 0; | |
8416 | ||
ccc6cda3 JA |
8417 | FREE (temp); |
8418 | if (value) | |
28ef6c31 | 8419 | { |
495aee44 CR |
8420 | /* From Posix discussion on austin-group list. Issue 221 |
8421 | requires that backslashes escaping `}' inside | |
8422 | double-quoted ${...} be removed. */ | |
8423 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
8424 | quoted |= Q_DOLBRACE; | |
95732b49 JA |
8425 | ret = parameter_brace_expand_rhs (name, value, c, |
8426 | quoted, | |
a0c0a00f | 8427 | pflags, |
95732b49 JA |
8428 | quoted_dollar_atp, |
8429 | contains_dollar_at); | |
8430 | /* XXX - fix up later, esp. noting presence of | |
8431 | W_HASQUOTEDNULL in ret->flags */ | |
ccc6cda3 JA |
8432 | free (value); |
8433 | } | |
8434 | else | |
28ef6c31 | 8435 | temp = (char *)NULL; |
ccc6cda3 JA |
8436 | } |
8437 | else | |
8438 | { | |
8439 | FREE (value); | |
8440 | } | |
8441 | /* Otherwise do nothing; just use the value in TEMP. */ | |
726f6388 | 8442 | } |
ccc6cda3 | 8443 | else /* VAR not set or VAR is NULL. */ |
28ef6c31 | 8444 | { |
ccc6cda3 JA |
8445 | FREE (temp); |
8446 | temp = (char *)NULL; | |
8447 | if (c == '=' && var_is_special) | |
8448 | { | |
ac50fbac | 8449 | last_command_exit_value = EXECUTION_FAILURE; |
b80f6443 | 8450 | report_error (_("$%s: cannot assign in this way"), name); |
ccc6cda3 JA |
8451 | free (name); |
8452 | free (value); | |
95732b49 | 8453 | return &expand_wdesc_error; |
ccc6cda3 JA |
8454 | } |
8455 | else if (c == '?') | |
8456 | { | |
8457 | parameter_brace_expand_error (name, value); | |
95732b49 | 8458 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
ccc6cda3 JA |
8459 | } |
8460 | else if (c != '+') | |
28ef6c31 JA |
8461 | { |
8462 | /* XXX -- if we're double-quoted and the named variable is "$@", | |
8463 | we want to turn off any special handling of "$@" -- | |
8464 | we're not using it, so whatever is on the rhs applies. */ | |
8465 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
8466 | *quoted_dollar_atp = 0; | |
8467 | if (contains_dollar_at) | |
8468 | *contains_dollar_at = 0; | |
8469 | ||
495aee44 CR |
8470 | /* From Posix discussion on austin-group list. Issue 221 requires |
8471 | that backslashes escaping `}' inside double-quoted ${...} be | |
8472 | removed. */ | |
8473 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
8474 | quoted |= Q_DOLBRACE; | |
a0c0a00f | 8475 | ret = parameter_brace_expand_rhs (name, value, c, quoted, pflags, |
95732b49 JA |
8476 | quoted_dollar_atp, |
8477 | contains_dollar_at); | |
8478 | /* XXX - fix up later, esp. noting presence of | |
8479 | W_HASQUOTEDNULL in tdesc->flags */ | |
28ef6c31 | 8480 | } |
ccc6cda3 | 8481 | free (value); |
726f6388 | 8482 | } |
28ef6c31 | 8483 | |
ccc6cda3 | 8484 | break; |
726f6388 | 8485 | } |
ccc6cda3 | 8486 | free (name); |
95732b49 JA |
8487 | |
8488 | if (ret == 0) | |
8489 | { | |
8490 | ret = alloc_word_desc (); | |
8491 | ret->flags = tflag; | |
8492 | ret->word = temp; | |
8493 | } | |
8494 | return (ret); | |
726f6388 JA |
8495 | } |
8496 | ||
cce855bc JA |
8497 | /* Expand a single ${xxx} expansion. The braces are optional. When |
8498 | the braces are used, parameter_brace_expand() does the work, | |
8499 | possibly calling param_expand recursively. */ | |
95732b49 | 8500 | static WORD_DESC * |
cce855bc JA |
8501 | param_expand (string, sindex, quoted, expanded_something, |
8502 | contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p, | |
8503 | pflags) | |
8504 | char *string; | |
8505 | int *sindex, quoted, *expanded_something, *contains_dollar_at; | |
8506 | int *quoted_dollar_at_p, *had_quoted_null_p, pflags; | |
8507 | { | |
a0c0a00f | 8508 | char *temp, *temp1, uerror[3], *savecmd; |
f73dda09 JA |
8509 | int zindex, t_index, expok; |
8510 | unsigned char c; | |
7117c2d2 | 8511 | intmax_t number; |
cce855bc | 8512 | SHELL_VAR *var; |
f73dda09 | 8513 | WORD_LIST *list; |
95732b49 JA |
8514 | WORD_DESC *tdesc, *ret; |
8515 | int tflag; | |
cce855bc | 8516 | |
a0c0a00f | 8517 | /*itrace("param_expand: `%s' pflags = %d", string+*sindex, pflags);*/ |
cce855bc JA |
8518 | zindex = *sindex; |
8519 | c = string[++zindex]; | |
8520 | ||
8521 | temp = (char *)NULL; | |
95732b49 JA |
8522 | ret = tdesc = (WORD_DESC *)NULL; |
8523 | tflag = 0; | |
cce855bc JA |
8524 | |
8525 | /* Do simple cases first. Switch on what follows '$'. */ | |
8526 | switch (c) | |
8527 | { | |
8528 | /* $0 .. $9? */ | |
8529 | case '0': | |
8530 | case '1': | |
8531 | case '2': | |
8532 | case '3': | |
8533 | case '4': | |
8534 | case '5': | |
8535 | case '6': | |
8536 | case '7': | |
8537 | case '8': | |
8538 | case '9': | |
f73dda09 | 8539 | temp1 = dollar_vars[TODIGIT (c)]; |
cce855bc JA |
8540 | if (unbound_vars_is_error && temp1 == (char *)NULL) |
8541 | { | |
7117c2d2 JA |
8542 | uerror[0] = '$'; |
8543 | uerror[1] = c; | |
8544 | uerror[2] = '\0'; | |
cce855bc | 8545 | last_command_exit_value = EXECUTION_FAILURE; |
0001803f | 8546 | err_unboundvar (uerror); |
95732b49 | 8547 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
cce855bc | 8548 | } |
b80f6443 JA |
8549 | if (temp1) |
8550 | temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
8551 | ? quote_string (temp1) | |
8552 | : quote_escapes (temp1); | |
8553 | else | |
8554 | temp = (char *)NULL; | |
95732b49 | 8555 | |
cce855bc JA |
8556 | break; |
8557 | ||
8558 | /* $$ -- pid of the invoking shell. */ | |
8559 | case '$': | |
8560 | temp = itos (dollar_dollar_pid); | |
8561 | break; | |
8562 | ||
8563 | /* $# -- number of positional parameters. */ | |
8564 | case '#': | |
8565 | temp = itos (number_of_args ()); | |
8566 | break; | |
8567 | ||
8568 | /* $? -- return value of the last synchronous command. */ | |
8569 | case '?': | |
8570 | temp = itos (last_command_exit_value); | |
8571 | break; | |
8572 | ||
8573 | /* $- -- flags supplied to the shell on invocation or by `set'. */ | |
8574 | case '-': | |
8575 | temp = which_set_flags (); | |
8576 | break; | |
8577 | ||
8578 | /* $! -- Pid of the last asynchronous command. */ | |
8579 | case '!': | |
8580 | /* If no asynchronous pids have been created, expand to nothing. | |
8581 | If `set -u' has been executed, and no async processes have | |
8582 | been created, this is an expansion error. */ | |
8583 | if (last_asynchronous_pid == NO_PID) | |
8584 | { | |
8585 | if (expanded_something) | |
8586 | *expanded_something = 0; | |
8587 | temp = (char *)NULL; | |
8588 | if (unbound_vars_is_error) | |
8589 | { | |
7117c2d2 JA |
8590 | uerror[0] = '$'; |
8591 | uerror[1] = c; | |
8592 | uerror[2] = '\0'; | |
cce855bc | 8593 | last_command_exit_value = EXECUTION_FAILURE; |
0001803f | 8594 | err_unboundvar (uerror); |
95732b49 | 8595 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
cce855bc JA |
8596 | } |
8597 | } | |
8598 | else | |
f73dda09 | 8599 | temp = itos (last_asynchronous_pid); |
cce855bc JA |
8600 | break; |
8601 | ||
8602 | /* The only difference between this and $@ is when the arg is quoted. */ | |
8603 | case '*': /* `$*' */ | |
8604 | list = list_rest_of_args (); | |
8605 | ||
89a92869 CR |
8606 | #if 0 |
8607 | /* According to austin-group posix proposal by Geoff Clare in | |
8608 | <20090505091501.GA10097@squonk.masqnet> of 5 May 2009: | |
8609 | ||
8610 | "The shell shall write a message to standard error and | |
8611 | immediately exit when it tries to expand an unset parameter | |
8612 | other than the '@' and '*' special parameters." | |
8613 | */ | |
8614 | ||
8615 | if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0) | |
3185942a JA |
8616 | { |
8617 | uerror[0] = '$'; | |
8618 | uerror[1] = '*'; | |
8619 | uerror[2] = '\0'; | |
3185942a | 8620 | last_command_exit_value = EXECUTION_FAILURE; |
89a92869 | 8621 | err_unboundvar (uerror); |
3185942a JA |
8622 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
8623 | } | |
89a92869 | 8624 | #endif |
3185942a | 8625 | |
cce855bc JA |
8626 | /* If there are no command-line arguments, this should just |
8627 | disappear if there are other characters in the expansion, | |
8628 | even if it's quoted. */ | |
8629 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0) | |
8630 | temp = (char *)NULL; | |
0001803f | 8631 | else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE)) |
cce855bc JA |
8632 | { |
8633 | /* If we have "$*" we want to make a string of the positional | |
8634 | parameters, separated by the first character of $IFS, and | |
8635 | quote the whole string, including the separators. If IFS | |
8636 | is unset, the parameters are separated by ' '; if $IFS is | |
8637 | null, the parameters are concatenated. */ | |
0001803f | 8638 | temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list) : string_list (list); |
495aee44 CR |
8639 | if (temp) |
8640 | { | |
a0c0a00f | 8641 | temp1 = (quoted & Q_DOUBLE_QUOTES) ? quote_string (temp) : temp; |
495aee44 CR |
8642 | if (*temp == 0) |
8643 | tflag |= W_HASQUOTEDNULL; | |
a0c0a00f CR |
8644 | if (temp != temp1) |
8645 | free (temp); | |
495aee44 CR |
8646 | temp = temp1; |
8647 | } | |
cce855bc JA |
8648 | } |
8649 | else | |
28ef6c31 | 8650 | { |
95732b49 JA |
8651 | /* We check whether or not we're eventually going to split $* here, |
8652 | for example when IFS is empty and we are processing the rhs of | |
8653 | an assignment statement. In that case, we don't separate the | |
8654 | arguments at all. Otherwise, if the $* is not quoted it is | |
8655 | identical to $@ */ | |
95732b49 JA |
8656 | # if defined (HANDLE_MULTIBYTE) |
8657 | if (expand_no_split_dollar_star && ifs_firstc[0] == 0) | |
8658 | # else | |
8659 | if (expand_no_split_dollar_star && ifs_firstc == 0) | |
8660 | # endif | |
8661 | temp = string_list_dollar_star (list); | |
8662 | else | |
ac50fbac | 8663 | { |
a0c0a00f | 8664 | temp = string_list_dollar_at (list, quoted, 0); |
ac50fbac CR |
8665 | if (quoted == 0 && (ifs_is_set == 0 || ifs_is_null)) |
8666 | tflag |= W_SPLITSPACE; | |
a0c0a00f CR |
8667 | /* If we're not quoted but we still don't want word splitting, make |
8668 | we quote the IFS characters to protect them from splitting (e.g., | |
8669 | when $@ is in the string as well). */ | |
8670 | else if (temp && quoted == 0 && ifs_is_set && (pflags & PF_ASSIGNRHS)) | |
8671 | { | |
8672 | temp1 = quote_string (temp); | |
8673 | free (temp); | |
8674 | temp = temp1; | |
8675 | } | |
ac50fbac CR |
8676 | } |
8677 | ||
28ef6c31 JA |
8678 | if (expand_no_split_dollar_star == 0 && contains_dollar_at) |
8679 | *contains_dollar_at = 1; | |
8680 | } | |
cce855bc JA |
8681 | |
8682 | dispose_words (list); | |
8683 | break; | |
8684 | ||
8685 | /* When we have "$@" what we want is "$1" "$2" "$3" ... This | |
8686 | means that we have to turn quoting off after we split into | |
8687 | the individually quoted arguments so that the final split | |
8688 | on the first character of $IFS is still done. */ | |
8689 | case '@': /* `$@' */ | |
8690 | list = list_rest_of_args (); | |
8691 | ||
89a92869 CR |
8692 | #if 0 |
8693 | /* According to austin-group posix proposal by Geoff Clare in | |
8694 | <20090505091501.GA10097@squonk.masqnet> of 5 May 2009: | |
8695 | ||
8696 | "The shell shall write a message to standard error and | |
8697 | immediately exit when it tries to expand an unset parameter | |
8698 | other than the '@' and '*' special parameters." | |
8699 | */ | |
8700 | ||
8701 | if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0) | |
3185942a JA |
8702 | { |
8703 | uerror[0] = '$'; | |
8704 | uerror[1] = '@'; | |
8705 | uerror[2] = '\0'; | |
3185942a | 8706 | last_command_exit_value = EXECUTION_FAILURE; |
89a92869 | 8707 | err_unboundvar (uerror); |
3185942a JA |
8708 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
8709 | } | |
89a92869 | 8710 | #endif |
3185942a | 8711 | |
cce855bc JA |
8712 | /* We want to flag the fact that we saw this. We can't turn |
8713 | off quoting entirely, because other characters in the | |
8714 | string might need it (consider "\"$@\""), but we need some | |
8715 | way to signal that the final split on the first character | |
8716 | of $IFS should be done, even though QUOTED is 1. */ | |
0001803f | 8717 | /* XXX - should this test include Q_PATQUOTE? */ |
cce855bc JA |
8718 | if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
8719 | *quoted_dollar_at_p = 1; | |
8720 | if (contains_dollar_at) | |
8721 | *contains_dollar_at = 1; | |
8722 | ||
8723 | /* We want to separate the positional parameters with the first | |
8724 | character of $IFS in case $IFS is something other than a space. | |
8725 | We also want to make sure that splitting is done no matter what -- | |
8726 | according to POSIX.2, this expands to a list of the positional | |
8727 | parameters no matter what IFS is set to. */ | |
3b34f6e6 CR |
8728 | /* XXX - what to do when in a context where word splitting is not |
8729 | performed? Even when IFS is not the default, posix seems to imply | |
8730 | that we behave like unquoted $* ? Maybe we should use PF_NOSPLIT2 | |
8731 | here. */ | |
a0c0a00f CR |
8732 | /* XXX - bash-4.4/bash-5.0 passing PFLAGS */ |
8733 | temp = string_list_dollar_at (list, (pflags & PF_ASSIGNRHS) ? (quoted|Q_DOUBLE_QUOTES) : quoted, pflags); | |
cce855bc | 8734 | |
ac50fbac | 8735 | tflag |= W_DOLLARAT; |
cce855bc JA |
8736 | dispose_words (list); |
8737 | break; | |
8738 | ||
8739 | case LBRACE: | |
0001803f | 8740 | tdesc = parameter_brace_expand (string, &zindex, quoted, pflags, |
95732b49 JA |
8741 | quoted_dollar_at_p, |
8742 | contains_dollar_at); | |
8743 | ||
95732b49 JA |
8744 | if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal) |
8745 | return (tdesc); | |
8746 | temp = tdesc ? tdesc->word : (char *)0; | |
cce855bc JA |
8747 | |
8748 | /* XXX */ | |
bb70624e | 8749 | /* Quoted nulls should be removed if there is anything else |
cce855bc JA |
8750 | in the string. */ |
8751 | /* Note that we saw the quoted null so we can add one back at | |
8752 | the end of this function if there are no other characters | |
28ef6c31 JA |
8753 | in the string, discard TEMP, and go on. The exception to |
8754 | this is when we have "${@}" and $1 is '', since $@ needs | |
8755 | special handling. */ | |
95732b49 | 8756 | if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp)) |
cce855bc JA |
8757 | { |
8758 | if (had_quoted_null_p) | |
8759 | *had_quoted_null_p = 1; | |
28ef6c31 JA |
8760 | if (*quoted_dollar_at_p == 0) |
8761 | { | |
8762 | free (temp); | |
95732b49 | 8763 | tdesc->word = temp = (char *)NULL; |
28ef6c31 JA |
8764 | } |
8765 | ||
cce855bc JA |
8766 | } |
8767 | ||
95732b49 | 8768 | ret = tdesc; |
cce855bc JA |
8769 | goto return0; |
8770 | ||
8771 | /* Do command or arithmetic substitution. */ | |
8772 | case LPAREN: | |
8773 | /* We have to extract the contents of this paren substitution. */ | |
8774 | t_index = zindex + 1; | |
a0c0a00f CR |
8775 | /* XXX - might want to check for string[t_index+2] == LPAREN and parse |
8776 | as arithmetic substitution immediately. */ | |
8777 | temp = extract_command_subst (string, &t_index, (pflags&PF_COMPLETE) ? SX_COMPLETE : 0); | |
cce855bc JA |
8778 | zindex = t_index; |
8779 | ||
8780 | /* For Posix.2-style `$(( ))' arithmetic substitution, | |
28ef6c31 | 8781 | extract the expression and pass it to the evaluator. */ |
cce855bc JA |
8782 | if (temp && *temp == LPAREN) |
8783 | { | |
8784 | char *temp2; | |
8785 | temp1 = temp + 1; | |
8786 | temp2 = savestring (temp1); | |
8787 | t_index = strlen (temp2) - 1; | |
8788 | ||
8789 | if (temp2[t_index] != RPAREN) | |
8790 | { | |
8791 | free (temp2); | |
8792 | goto comsub; | |
8793 | } | |
8794 | ||
8795 | /* Cut off ending `)' */ | |
8796 | temp2[t_index] = '\0'; | |
8797 | ||
0628567a JA |
8798 | if (chk_arithsub (temp2, t_index) == 0) |
8799 | { | |
8800 | free (temp2); | |
0001803f CR |
8801 | #if 0 |
8802 | internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution")); | |
8803 | #endif | |
0628567a JA |
8804 | goto comsub; |
8805 | } | |
8806 | ||
cce855bc | 8807 | /* Expand variables found inside the expression. */ |
a0c0a00f | 8808 | temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES|Q_ARITH); |
cce855bc JA |
8809 | free (temp2); |
8810 | ||
8811 | arithsub: | |
8812 | /* No error messages. */ | |
a0c0a00f | 8813 | savecmd = this_command_name; |
cce855bc JA |
8814 | this_command_name = (char *)NULL; |
8815 | number = evalexp (temp1, &expok); | |
a0c0a00f | 8816 | this_command_name = savecmd; |
cce855bc JA |
8817 | free (temp); |
8818 | free (temp1); | |
8819 | if (expok == 0) | |
8820 | { | |
8821 | if (interactive_shell == 0 && posixly_correct) | |
8822 | { | |
8823 | last_command_exit_value = EXECUTION_FAILURE; | |
95732b49 | 8824 | return (&expand_wdesc_fatal); |
cce855bc JA |
8825 | } |
8826 | else | |
95732b49 | 8827 | return (&expand_wdesc_error); |
cce855bc JA |
8828 | } |
8829 | temp = itos (number); | |
8830 | break; | |
8831 | } | |
8832 | ||
8833 | comsub: | |
b80f6443 JA |
8834 | if (pflags & PF_NOCOMSUB) |
8835 | /* we need zindex+1 because string[zindex] == RPAREN */ | |
8836 | temp1 = substring (string, *sindex, zindex+1); | |
8837 | else | |
3185942a JA |
8838 | { |
8839 | tdesc = command_substitute (temp, quoted); | |
8840 | temp1 = tdesc ? tdesc->word : (char *)NULL; | |
8841 | if (tdesc) | |
8842 | dispose_word_desc (tdesc); | |
8843 | } | |
cce855bc JA |
8844 | FREE (temp); |
8845 | temp = temp1; | |
8846 | break; | |
8847 | ||
8848 | /* Do POSIX.2d9-style arithmetic substitution. This will probably go | |
8849 | away in a future bash release. */ | |
8850 | case '[': | |
bb70624e | 8851 | /* Extract the contents of this arithmetic substitution. */ |
cce855bc JA |
8852 | t_index = zindex + 1; |
8853 | temp = extract_arithmetic_subst (string, &t_index); | |
8854 | zindex = t_index; | |
3185942a JA |
8855 | if (temp == 0) |
8856 | { | |
8857 | temp = savestring (string); | |
8858 | if (expanded_something) | |
8859 | *expanded_something = 0; | |
8860 | goto return0; | |
8861 | } | |
cce855bc JA |
8862 | |
8863 | /* Do initial variable expansion. */ | |
a0c0a00f | 8864 | temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES|Q_ARITH); |
cce855bc JA |
8865 | |
8866 | goto arithsub; | |
8867 | ||
8868 | default: | |
8869 | /* Find the variable in VARIABLE_LIST. */ | |
8870 | temp = (char *)NULL; | |
8871 | ||
8872 | for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++) | |
8873 | ; | |
8874 | temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL; | |
8875 | ||
8876 | /* If this isn't a variable name, then just output the `$'. */ | |
8877 | if (temp1 == 0 || *temp1 == '\0') | |
8878 | { | |
8879 | FREE (temp1); | |
f73dda09 | 8880 | temp = (char *)xmalloc (2); |
cce855bc JA |
8881 | temp[0] = '$'; |
8882 | temp[1] = '\0'; | |
8883 | if (expanded_something) | |
8884 | *expanded_something = 0; | |
8885 | goto return0; | |
8886 | } | |
8887 | ||
8888 | /* If the variable exists, return its value cell. */ | |
8889 | var = find_variable (temp1); | |
8890 | ||
7117c2d2 | 8891 | if (var && invisible_p (var) == 0 && var_isset (var)) |
cce855bc JA |
8892 | { |
8893 | #if defined (ARRAY_VARS) | |
3185942a | 8894 | if (assoc_p (var) || array_p (var)) |
cce855bc | 8895 | { |
3185942a JA |
8896 | temp = array_p (var) ? array_reference (array_cell (var), 0) |
8897 | : assoc_reference (assoc_cell (var), "0"); | |
cce855bc | 8898 | if (temp) |
b80f6443 JA |
8899 | temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
8900 | ? quote_string (temp) | |
8901 | : quote_escapes (temp); | |
8902 | else if (unbound_vars_is_error) | |
8903 | goto unbound_variable; | |
cce855bc JA |
8904 | } |
8905 | else | |
8906 | #endif | |
b80f6443 JA |
8907 | { |
8908 | temp = value_cell (var); | |
8909 | ||
8910 | temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
8911 | ? quote_string (temp) | |
8912 | : quote_escapes (temp); | |
8913 | } | |
8914 | ||
cce855bc | 8915 | free (temp1); |
7117c2d2 | 8916 | |
cce855bc JA |
8917 | goto return0; |
8918 | } | |
b64a0e1d CR |
8919 | else if (var && (invisible_p (var) || var_isset (var) == 0)) |
8920 | temp = (char *)NULL; | |
a0c0a00f | 8921 | else if ((var = find_variable_last_nameref (temp1, 0)) && var_isset (var) && invisible_p (var) == 0) |
ac50fbac CR |
8922 | { |
8923 | temp = nameref_cell (var); | |
8924 | #if defined (ARRAY_VARS) | |
a0c0a00f | 8925 | if (temp && *temp && valid_array_reference (temp, 0)) |
ac50fbac CR |
8926 | { |
8927 | tdesc = parameter_brace_expand_word (temp, SPECIAL_VAR (temp, 0), quoted, pflags, (arrayind_t *)NULL); | |
8928 | if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal) | |
8929 | return (tdesc); | |
8930 | ret = tdesc; | |
8931 | goto return0; | |
8932 | } | |
8933 | else | |
8934 | #endif | |
8935 | /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */ | |
8936 | if (temp && *temp && legal_identifier (temp) == 0) | |
8937 | { | |
8938 | last_command_exit_value = EXECUTION_FAILURE; | |
8939 | report_error (_("%s: invalid variable name for name reference"), temp); | |
8940 | return (&expand_wdesc_error); /* XXX */ | |
8941 | } | |
8942 | else | |
8943 | temp = (char *)NULL; | |
8944 | } | |
cce855bc JA |
8945 | |
8946 | temp = (char *)NULL; | |
8947 | ||
b80f6443 | 8948 | unbound_variable: |
cce855bc | 8949 | if (unbound_vars_is_error) |
0001803f CR |
8950 | { |
8951 | last_command_exit_value = EXECUTION_FAILURE; | |
8952 | err_unboundvar (temp1); | |
8953 | } | |
cce855bc JA |
8954 | else |
8955 | { | |
8956 | free (temp1); | |
8957 | goto return0; | |
8958 | } | |
8959 | ||
8960 | free (temp1); | |
8961 | last_command_exit_value = EXECUTION_FAILURE; | |
8962 | return ((unbound_vars_is_error && interactive_shell == 0) | |
95732b49 JA |
8963 | ? &expand_wdesc_fatal |
8964 | : &expand_wdesc_error); | |
cce855bc JA |
8965 | } |
8966 | ||
8967 | if (string[zindex]) | |
8968 | zindex++; | |
8969 | ||
8970 | return0: | |
8971 | *sindex = zindex; | |
95732b49 JA |
8972 | |
8973 | if (ret == 0) | |
8974 | { | |
8975 | ret = alloc_word_desc (); | |
8976 | ret->flags = tflag; /* XXX */ | |
8977 | ret->word = temp; | |
8978 | } | |
8979 | return ret; | |
cce855bc JA |
8980 | } |
8981 | ||
a0c0a00f CR |
8982 | void |
8983 | invalidate_cached_quoted_dollar_at () | |
8984 | { | |
8985 | dispose_words (cached_quoted_dollar_at); | |
8986 | cached_quoted_dollar_at = 0; | |
8987 | } | |
8988 | ||
cce855bc JA |
8989 | /* Make a word list which is the result of parameter and variable |
8990 | expansion, command substitution, arithmetic substitution, and | |
8991 | quote removal of WORD. Return a pointer to a WORD_LIST which is | |
8992 | the result of the expansion. If WORD contains a null word, the | |
8993 | word list returned is also null. | |
726f6388 | 8994 | |
ccc6cda3 JA |
8995 | QUOTED contains flag values defined in shell.h. |
8996 | ||
b72432fd JA |
8997 | ISEXP is used to tell expand_word_internal that the word should be |
8998 | treated as the result of an expansion. This has implications for | |
8999 | how IFS characters in the word are treated. | |
9000 | ||
726f6388 JA |
9001 | CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null |
9002 | they point to an integer value which receives information about expansion. | |
9003 | CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero. | |
9004 | EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions, | |
9005 | else zero. | |
9006 | ||
9007 | This only does word splitting in the case of $@ expansion. In that | |
9008 | case, we split on ' '. */ | |
9009 | ||
9010 | /* Values for the local variable quoted_state. */ | |
9011 | #define UNQUOTED 0 | |
9012 | #define PARTIALLY_QUOTED 1 | |
9013 | #define WHOLLY_QUOTED 2 | |
9014 | ||
9015 | static WORD_LIST * | |
b72432fd | 9016 | expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something) |
726f6388 | 9017 | WORD_DESC *word; |
b72432fd | 9018 | int quoted, isexp; |
726f6388 JA |
9019 | int *contains_dollar_at; |
9020 | int *expanded_something; | |
9021 | { | |
ccc6cda3 JA |
9022 | WORD_LIST *list; |
9023 | WORD_DESC *tword; | |
726f6388 JA |
9024 | |
9025 | /* The intermediate string that we build while expanding. */ | |
ccc6cda3 | 9026 | char *istring; |
726f6388 JA |
9027 | |
9028 | /* The current size of the above object. */ | |
a0c0a00f | 9029 | size_t istring_size; |
726f6388 JA |
9030 | |
9031 | /* Index into ISTRING. */ | |
ccc6cda3 | 9032 | int istring_index; |
726f6388 JA |
9033 | |
9034 | /* Temporary string storage. */ | |
ccc6cda3 | 9035 | char *temp, *temp1; |
726f6388 JA |
9036 | |
9037 | /* The text of WORD. */ | |
ccc6cda3 | 9038 | register char *string; |
726f6388 | 9039 | |
7117c2d2 JA |
9040 | /* The size of STRING. */ |
9041 | size_t string_size; | |
9042 | ||
726f6388 | 9043 | /* The index into STRING. */ |
ccc6cda3 | 9044 | int sindex; |
726f6388 JA |
9045 | |
9046 | /* This gets 1 if we see a $@ while quoted. */ | |
ccc6cda3 | 9047 | int quoted_dollar_at; |
726f6388 JA |
9048 | |
9049 | /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on | |
9050 | whether WORD contains no quoting characters, a partially quoted | |
9051 | string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */ | |
ccc6cda3 JA |
9052 | int quoted_state; |
9053 | ||
95732b49 | 9054 | /* State flags */ |
ccc6cda3 | 9055 | int had_quoted_null; |
0b913689 | 9056 | int has_dollar_at, temp_has_dollar_at; |
ac50fbac | 9057 | int split_on_spaces; |
28ef6c31 | 9058 | int tflag; |
0001803f | 9059 | int pflags; /* flags passed to param_expand */ |
a0c0a00f | 9060 | int mb_cur_max; |
726f6388 | 9061 | |
95732b49 JA |
9062 | int assignoff; /* If assignment, offset of `=' */ |
9063 | ||
f73dda09 | 9064 | register unsigned char c; /* Current character. */ |
726f6388 | 9065 | int t_index; /* For calls to string_extract_xxx. */ |
726f6388 | 9066 | |
bb70624e | 9067 | char twochars[2]; |
b72432fd | 9068 | |
7117c2d2 JA |
9069 | DECLARE_MBSTATE; |
9070 | ||
a0c0a00f CR |
9071 | /* OK, let's see if we can optimize a common idiom: "$@" */ |
9072 | if (STREQ (word->word, "\"$@\"") && | |
9073 | (word->flags == (W_HASDOLLAR|W_QUOTED)) && | |
9074 | dollar_vars[1]) /* XXX - check IFS here as well? */ | |
9075 | { | |
9076 | if (contains_dollar_at) | |
9077 | *contains_dollar_at = 1; | |
9078 | if (expanded_something) | |
9079 | *expanded_something = 1; | |
9080 | if (cached_quoted_dollar_at) | |
9081 | return (copy_word_list (cached_quoted_dollar_at)); | |
9082 | list = list_rest_of_args (); | |
9083 | list = quote_list (list); | |
9084 | cached_quoted_dollar_at = copy_word_list (list); | |
9085 | return (list); | |
9086 | } | |
9087 | ||
f73dda09 | 9088 | istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE); |
ccc6cda3 | 9089 | istring[istring_index = 0] = '\0'; |
cce855bc | 9090 | quoted_dollar_at = had_quoted_null = has_dollar_at = 0; |
ac50fbac | 9091 | split_on_spaces = 0; |
ccc6cda3 JA |
9092 | quoted_state = UNQUOTED; |
9093 | ||
9094 | string = word->word; | |
9095 | if (string == 0) | |
9096 | goto finished_with_string; | |
a0c0a00f CR |
9097 | mb_cur_max = MB_CUR_MAX; |
9098 | ||
95732b49 JA |
9099 | /* Don't need the string length for the SADD... and COPY_ macros unless |
9100 | multibyte characters are possible. */ | |
a0c0a00f | 9101 | string_size = (mb_cur_max > 1) ? strlen (string) : 1; |
726f6388 JA |
9102 | |
9103 | if (contains_dollar_at) | |
9104 | *contains_dollar_at = 0; | |
9105 | ||
95732b49 JA |
9106 | assignoff = -1; |
9107 | ||
726f6388 JA |
9108 | /* Begin the expansion. */ |
9109 | ||
ccc6cda3 | 9110 | for (sindex = 0; ;) |
726f6388 JA |
9111 | { |
9112 | c = string[sindex]; | |
9113 | ||
ac50fbac | 9114 | /* Case on top-level character. */ |
726f6388 JA |
9115 | switch (c) |
9116 | { | |
9117 | case '\0': | |
9118 | goto finished_with_string; | |
9119 | ||
9120 | case CTLESC: | |
7117c2d2 JA |
9121 | sindex++; |
9122 | #if HANDLE_MULTIBYTE | |
a0c0a00f | 9123 | if (mb_cur_max > 1 && string[sindex]) |
7117c2d2 | 9124 | { |
b80f6443 | 9125 | SADD_MBQCHAR_BODY(temp, string, sindex, string_size); |
7117c2d2 JA |
9126 | } |
9127 | else | |
9128 | #endif | |
9129 | { | |
9130 | temp = (char *)xmalloc (3); | |
9131 | temp[0] = CTLESC; | |
9132 | temp[1] = c = string[sindex]; | |
9133 | temp[2] = '\0'; | |
9134 | } | |
726f6388 | 9135 | |
cce855bc | 9136 | dollar_add_string: |
726f6388 JA |
9137 | if (string[sindex]) |
9138 | sindex++; | |
9139 | ||
cce855bc JA |
9140 | add_string: |
9141 | if (temp) | |
9142 | { | |
9143 | istring = sub_append_string (temp, istring, &istring_index, &istring_size); | |
9144 | temp = (char *)0; | |
9145 | } | |
9146 | ||
9147 | break; | |
726f6388 JA |
9148 | |
9149 | #if defined (PROCESS_SUBSTITUTION) | |
9150 | /* Process substitution. */ | |
9151 | case '<': | |
9152 | case '>': | |
9153 | { | |
a0c0a00f CR |
9154 | /* bash-4.4/bash-5.0 |
9155 | XXX - technically this should only be expanded at the start | |
9156 | of a word */ | |
0628567a | 9157 | if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)) || posixly_correct) |
726f6388 | 9158 | { |
bb70624e | 9159 | sindex--; /* add_character: label increments sindex */ |
726f6388 JA |
9160 | goto add_character; |
9161 | } | |
9162 | else | |
cce855bc | 9163 | t_index = sindex + 1; /* skip past both '<' and LPAREN */ |
726f6388 | 9164 | |
85b94814 | 9165 | temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index, 0); /*))*/ |
ccc6cda3 | 9166 | sindex = t_index; |
726f6388 JA |
9167 | |
9168 | /* If the process substitution specification is `<()', we want to | |
9169 | open the pipe for writing in the child and produce output; if | |
9170 | it is `>()', we want to open the pipe for reading in the child | |
9171 | and consume input. */ | |
ccc6cda3 | 9172 | temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0; |
726f6388 JA |
9173 | |
9174 | FREE (temp1); | |
9175 | ||
9176 | goto dollar_add_string; | |
9177 | } | |
9178 | #endif /* PROCESS_SUBSTITUTION */ | |
9179 | ||
95732b49 JA |
9180 | case '=': |
9181 | /* Posix.2 section 3.6.1 says that tildes following `=' in words | |
9182 | which are not assignment statements are not expanded. If the | |
9183 | shell isn't in posix mode, though, we perform tilde expansion | |
9184 | on `likely candidate' unquoted assignment statements (flags | |
9185 | include W_ASSIGNMENT but not W_QUOTED). A likely candidate | |
9186 | contains an unquoted :~ or =~. Something to think about: we | |
9187 | now have a flag that says to perform tilde expansion on arguments | |
9188 | to `assignment builtins' like declare and export that look like | |
9189 | assignment statements. We now do tilde expansion on such words | |
9190 | even in POSIX mode. */ | |
9191 | if (word->flags & (W_ASSIGNRHS|W_NOTILDE)) | |
17345e5a | 9192 | { |
0001803f | 9193 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
17345e5a JA |
9194 | goto add_ifs_character; |
9195 | else | |
9196 | goto add_character; | |
9197 | } | |
95732b49 JA |
9198 | /* If we're not in posix mode or forcing assignment-statement tilde |
9199 | expansion, note where the `=' appears in the word and prepare to | |
9200 | do tilde expansion following the first `='. */ | |
9201 | if ((word->flags & W_ASSIGNMENT) && | |
9202 | (posixly_correct == 0 || (word->flags & W_TILDEEXP)) && | |
9203 | assignoff == -1 && sindex > 0) | |
9204 | assignoff = sindex; | |
9205 | if (sindex == assignoff && string[sindex+1] == '~') /* XXX */ | |
9206 | word->flags |= W_ITILDE; | |
9207 | #if 0 | |
9208 | else if ((word->flags & W_ASSIGNMENT) && | |
9209 | (posixly_correct == 0 || (word->flags & W_TILDEEXP)) && | |
9210 | string[sindex+1] == '~') | |
9211 | word->flags |= W_ITILDE; | |
9212 | #endif | |
a0c0a00f CR |
9213 | |
9214 | /* XXX - bash-4.4/bash-5.0 */ | |
9215 | if (word->flags & W_ASSIGNARG) | |
9216 | word->flags |= W_ASSIGNRHS; /* affects $@ */ | |
9217 | ||
0001803f | 9218 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
17345e5a JA |
9219 | goto add_ifs_character; |
9220 | else | |
9221 | goto add_character; | |
95732b49 JA |
9222 | |
9223 | case ':': | |
9224 | if (word->flags & W_NOTILDE) | |
17345e5a | 9225 | { |
0001803f | 9226 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
17345e5a JA |
9227 | goto add_ifs_character; |
9228 | else | |
9229 | goto add_character; | |
9230 | } | |
95732b49 JA |
9231 | |
9232 | if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) && | |
9233 | string[sindex+1] == '~') | |
9234 | word->flags |= W_ITILDE; | |
17345e5a | 9235 | |
0001803f | 9236 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
17345e5a JA |
9237 | goto add_ifs_character; |
9238 | else | |
9239 | goto add_character; | |
95732b49 JA |
9240 | |
9241 | case '~': | |
9242 | /* If the word isn't supposed to be tilde expanded, or we're not | |
9243 | at the start of a word or after an unquoted : or = in an | |
a0c0a00f CR |
9244 | assignment statement, we don't do tilde expansion. If we don't want |
9245 | tilde expansion when expanding words to be passed to the arithmetic | |
9246 | evaluator, remove the check for Q_ARITH. */ | |
95732b49 JA |
9247 | if ((word->flags & (W_NOTILDE|W_DQUOTE)) || |
9248 | (sindex > 0 && ((word->flags & W_ITILDE) == 0)) || | |
a0c0a00f | 9249 | ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && ((quoted & Q_ARITH) == 0))) |
95732b49 JA |
9250 | { |
9251 | word->flags &= ~W_ITILDE; | |
0001803f | 9252 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0) |
17345e5a JA |
9253 | goto add_ifs_character; |
9254 | else | |
9255 | goto add_character; | |
95732b49 JA |
9256 | } |
9257 | ||
9258 | if (word->flags & W_ASSIGNRHS) | |
9259 | tflag = 2; | |
9260 | else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP)) | |
9261 | tflag = 1; | |
9262 | else | |
9263 | tflag = 0; | |
9264 | ||
9265 | temp = bash_tilde_find_word (string + sindex, tflag, &t_index); | |
9266 | ||
9267 | word->flags &= ~W_ITILDE; | |
9268 | ||
9269 | if (temp && *temp && t_index > 0) | |
9270 | { | |
9271 | temp1 = bash_tilde_expand (temp, tflag); | |
0628567a JA |
9272 | if (temp1 && *temp1 == '~' && STREQ (temp, temp1)) |
9273 | { | |
9274 | FREE (temp); | |
9275 | FREE (temp1); | |
9276 | goto add_character; /* tilde expansion failed */ | |
9277 | } | |
95732b49 JA |
9278 | free (temp); |
9279 | temp = temp1; | |
9280 | sindex += t_index; | |
3185942a | 9281 | goto add_quoted_string; /* XXX was add_string */ |
95732b49 JA |
9282 | } |
9283 | else | |
9284 | { | |
9285 | FREE (temp); | |
9286 | goto add_character; | |
9287 | } | |
9288 | ||
726f6388 | 9289 | case '$': |
726f6388 JA |
9290 | if (expanded_something) |
9291 | *expanded_something = 1; | |
9292 | ||
0b913689 | 9293 | temp_has_dollar_at = 0; |
0001803f CR |
9294 | pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0; |
9295 | if (word->flags & W_NOSPLIT2) | |
9296 | pflags |= PF_NOSPLIT2; | |
ac50fbac CR |
9297 | if (word->flags & W_ASSIGNRHS) |
9298 | pflags |= PF_ASSIGNRHS; | |
a0c0a00f CR |
9299 | if (word->flags & W_COMPLETE) |
9300 | pflags |= PF_COMPLETE; | |
95732b49 | 9301 | tword = param_expand (string, &sindex, quoted, expanded_something, |
0b913689 | 9302 | &temp_has_dollar_at, "ed_dollar_at, |
0001803f | 9303 | &had_quoted_null, pflags); |
0b913689 | 9304 | has_dollar_at += temp_has_dollar_at; |
ac50fbac | 9305 | split_on_spaces += (tword->flags & W_SPLITSPACE); |
726f6388 | 9306 | |
95732b49 | 9307 | if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal) |
726f6388 | 9308 | { |
cce855bc JA |
9309 | free (string); |
9310 | free (istring); | |
95732b49 JA |
9311 | return ((tword == &expand_wdesc_error) ? &expand_word_error |
9312 | : &expand_word_fatal); | |
cce855bc JA |
9313 | } |
9314 | if (contains_dollar_at && has_dollar_at) | |
9315 | *contains_dollar_at = 1; | |
95732b49 JA |
9316 | |
9317 | if (tword && (tword->flags & W_HASQUOTEDNULL)) | |
9318 | had_quoted_null = 1; | |
9319 | ||
ac50fbac | 9320 | temp = tword ? tword->word : (char *)NULL; |
95732b49 JA |
9321 | dispose_word_desc (tword); |
9322 | ||
a601c749 CR |
9323 | /* Kill quoted nulls; we will add them back at the end of |
9324 | expand_word_internal if nothing else in the string */ | |
9325 | if (had_quoted_null && temp && QUOTED_NULL (temp)) | |
9326 | { | |
9327 | FREE (temp); | |
9328 | temp = (char *)NULL; | |
9329 | } | |
9330 | ||
cce855bc JA |
9331 | goto add_string; |
9332 | break; | |
726f6388 | 9333 | |
cce855bc JA |
9334 | case '`': /* Backquoted command substitution. */ |
9335 | { | |
b80f6443 | 9336 | t_index = sindex++; |
726f6388 | 9337 | |
3185942a | 9338 | temp = string_extract (string, &sindex, "`", SX_REQMATCH); |
95732b49 JA |
9339 | /* The test of sindex against t_index is to allow bare instances of |
9340 | ` to pass through, for backwards compatibility. */ | |
9341 | if (temp == &extract_string_error || temp == &extract_string_fatal) | |
9342 | { | |
9343 | if (sindex - 1 == t_index) | |
9344 | { | |
9345 | sindex = t_index; | |
9346 | goto add_character; | |
9347 | } | |
ac50fbac | 9348 | last_command_exit_value = EXECUTION_FAILURE; |
3185942a | 9349 | report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index); |
95732b49 JA |
9350 | free (string); |
9351 | free (istring); | |
9352 | return ((temp == &extract_string_error) ? &expand_word_error | |
9353 | : &expand_word_fatal); | |
9354 | } | |
9355 | ||
cce855bc JA |
9356 | if (expanded_something) |
9357 | *expanded_something = 1; | |
726f6388 | 9358 | |
b80f6443 JA |
9359 | if (word->flags & W_NOCOMSUB) |
9360 | /* sindex + 1 because string[sindex] == '`' */ | |
9361 | temp1 = substring (string, t_index, sindex + 1); | |
9362 | else | |
9363 | { | |
9364 | de_backslash (temp); | |
3185942a JA |
9365 | tword = command_substitute (temp, quoted); |
9366 | temp1 = tword ? tword->word : (char *)NULL; | |
9367 | if (tword) | |
9368 | dispose_word_desc (tword); | |
b80f6443 | 9369 | } |
cce855bc JA |
9370 | FREE (temp); |
9371 | temp = temp1; | |
9372 | goto dollar_add_string; | |
9373 | } | |
ccc6cda3 | 9374 | |
cce855bc JA |
9375 | case '\\': |
9376 | if (string[sindex + 1] == '\n') | |
9377 | { | |
9378 | sindex += 2; | |
9379 | continue; | |
9380 | } | |
726f6388 | 9381 | |
cce855bc | 9382 | c = string[++sindex]; |
726f6388 | 9383 | |
cce855bc | 9384 | if (quoted & Q_HERE_DOCUMENT) |
28ef6c31 | 9385 | tflag = CBSHDOC; |
cce855bc | 9386 | else if (quoted & Q_DOUBLE_QUOTES) |
28ef6c31 | 9387 | tflag = CBSDQUOTE; |
cce855bc | 9388 | else |
28ef6c31 JA |
9389 | tflag = 0; |
9390 | ||
495aee44 CR |
9391 | /* From Posix discussion on austin-group list: Backslash escaping |
9392 | a } in ${...} is removed. Issue 0000221 */ | |
9393 | if ((quoted & Q_DOLBRACE) && c == RBRACE) | |
9394 | { | |
ac50fbac CR |
9395 | SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size); |
9396 | } | |
9397 | /* This is the fix for " $@\ " */ | |
9398 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0) && isexp == 0 && isifs (c)) | |
9399 | { | |
9400 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, | |
9401 | DEFAULT_ARRAY_SIZE); | |
9402 | istring[istring_index++] = CTLESC; | |
9403 | istring[istring_index++] = '\\'; | |
9404 | istring[istring_index] = '\0'; | |
9405 | ||
495aee44 CR |
9406 | SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size); |
9407 | } | |
9408 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0)) | |
cce855bc | 9409 | { |
7117c2d2 | 9410 | SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size); |
bb70624e JA |
9411 | } |
9412 | else if (c == 0) | |
9413 | { | |
9414 | c = CTLNUL; | |
9415 | sindex--; /* add_character: label increments sindex */ | |
9416 | goto add_character; | |
cce855bc JA |
9417 | } |
9418 | else | |
bb70624e | 9419 | { |
7117c2d2 | 9420 | SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size); |
bb70624e | 9421 | } |
726f6388 | 9422 | |
bb70624e JA |
9423 | sindex++; |
9424 | add_twochars: | |
9425 | /* BEFORE jumping here, we need to increment sindex if appropriate */ | |
9426 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, | |
9427 | DEFAULT_ARRAY_SIZE); | |
9428 | istring[istring_index++] = twochars[0]; | |
9429 | istring[istring_index++] = twochars[1]; | |
9430 | istring[istring_index] = '\0'; | |
9431 | ||
9432 | break; | |
726f6388 | 9433 | |
cce855bc | 9434 | case '"': |
a0c0a00f | 9435 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && ((quoted & Q_ARITH) == 0)) |
cce855bc | 9436 | goto add_character; |
ccc6cda3 JA |
9437 | |
9438 | t_index = ++sindex; | |
a0c0a00f | 9439 | temp = string_extract_double_quoted (string, &sindex, (word->flags & W_COMPLETE) ? SX_COMPLETE : 0); |
ccc6cda3 JA |
9440 | |
9441 | /* If the quotes surrounded the entire string, then the | |
9442 | whole word was quoted. */ | |
9443 | quoted_state = (t_index == 1 && string[sindex] == '\0') | |
9444 | ? WHOLLY_QUOTED | |
7117c2d2 | 9445 | : PARTIALLY_QUOTED; |
ccc6cda3 JA |
9446 | |
9447 | if (temp && *temp) | |
726f6388 | 9448 | { |
95732b49 JA |
9449 | tword = alloc_word_desc (); |
9450 | tword->word = temp; | |
9451 | ||
a0c0a00f CR |
9452 | /* XXX - bash-4.4/bash-5.0 */ |
9453 | if (word->flags & W_ASSIGNARG) | |
9454 | tword->flags |= word->flags & (W_ASSIGNARG|W_ASSIGNRHS); /* affects $@ */ | |
9455 | if (word->flags & W_COMPLETE) | |
9456 | tword->flags |= W_COMPLETE; /* for command substitutions */ | |
9457 | ||
ccc6cda3 JA |
9458 | temp = (char *)NULL; |
9459 | ||
0b913689 | 9460 | temp_has_dollar_at = 0; /* XXX */ |
95732b49 | 9461 | /* Need to get W_HASQUOTEDNULL flag through this function. */ |
0b913689 CR |
9462 | list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &temp_has_dollar_at, (int *)NULL); |
9463 | has_dollar_at += temp_has_dollar_at; | |
726f6388 | 9464 | |
ccc6cda3 JA |
9465 | if (list == &expand_word_error || list == &expand_word_fatal) |
9466 | { | |
9467 | free (istring); | |
9468 | free (string); | |
9469 | /* expand_word_internal has already freed temp_word->word | |
9470 | for us because of the way it prints error messages. */ | |
9471 | tword->word = (char *)NULL; | |
9472 | dispose_word (tword); | |
9473 | return list; | |
9474 | } | |
726f6388 | 9475 | |
ccc6cda3 | 9476 | dispose_word (tword); |
726f6388 | 9477 | |
ccc6cda3 JA |
9478 | /* "$@" (a double-quoted dollar-at) expands into nothing, |
9479 | not even a NULL word, when there are no positional | |
9480 | parameters. */ | |
a0c0a00f | 9481 | if (list == 0 && temp_has_dollar_at) /* XXX - was has_dollar_at */ |
726f6388 | 9482 | { |
ccc6cda3 JA |
9483 | quoted_dollar_at++; |
9484 | break; | |
9485 | } | |
9486 | ||
9487 | /* If we get "$@", we know we have expanded something, so we | |
9488 | need to remember it for the final split on $IFS. This is | |
9489 | a special case; it's the only case where a quoted string | |
9490 | can expand into more than one word. It's going to come back | |
9491 | from the above call to expand_word_internal as a list with | |
9492 | a single word, in which all characters are quoted and | |
9493 | separated by blanks. What we want to do is to turn it back | |
9494 | into a list for the next piece of code. */ | |
9495 | if (list) | |
9496 | dequote_list (list); | |
9497 | ||
95732b49 | 9498 | if (list && list->word && (list->word->flags & W_HASQUOTEDNULL)) |
ac50fbac | 9499 | had_quoted_null = 1; /* XXX */ |
95732b49 | 9500 | |
a0c0a00f | 9501 | if (temp_has_dollar_at) /* XXX - was has_dollar_at */ |
ccc6cda3 JA |
9502 | { |
9503 | quoted_dollar_at++; | |
9504 | if (contains_dollar_at) | |
9505 | *contains_dollar_at = 1; | |
9506 | if (expanded_something) | |
9507 | *expanded_something = 1; | |
9508 | } | |
9509 | } | |
9510 | else | |
9511 | { | |
9512 | /* What we have is "". This is a minor optimization. */ | |
f73dda09 | 9513 | FREE (temp); |
ccc6cda3 JA |
9514 | list = (WORD_LIST *)NULL; |
9515 | } | |
9516 | ||
9517 | /* The code above *might* return a list (consider the case of "$@", | |
9518 | where it returns "$1", "$2", etc.). We can't throw away the | |
9519 | rest of the list, and we have to make sure each word gets added | |
9520 | as quoted. We test on tresult->next: if it is non-NULL, we | |
9521 | quote the whole list, save it to a string with string_list, and | |
9522 | add that string. We don't need to quote the results of this | |
9523 | (and it would be wrong, since that would quote the separators | |
9524 | as well), so we go directly to add_string. */ | |
9525 | if (list) | |
9526 | { | |
9527 | if (list->next) | |
9528 | { | |
bc4cd23c JA |
9529 | /* Testing quoted_dollar_at makes sure that "$@" is |
9530 | split correctly when $IFS does not contain a space. */ | |
9531 | temp = quoted_dollar_at | |
a0c0a00f | 9532 | ? string_list_dollar_at (list, Q_DOUBLE_QUOTES, 0) |
bc4cd23c | 9533 | : string_list (quote_list (list)); |
ccc6cda3 | 9534 | dispose_words (list); |
726f6388 JA |
9535 | goto add_string; |
9536 | } | |
9537 | else | |
9538 | { | |
ccc6cda3 | 9539 | temp = savestring (list->word->word); |
95732b49 | 9540 | tflag = list->word->flags; |
ccc6cda3 | 9541 | dispose_words (list); |
95732b49 | 9542 | |
cce855bc JA |
9543 | /* If the string is not a quoted null string, we want |
9544 | to remove any embedded unquoted CTLNUL characters. | |
9545 | We do not want to turn quoted null strings back into | |
9546 | the empty string, though. We do this because we | |
9547 | want to remove any quoted nulls from expansions that | |
9548 | contain other characters. For example, if we have | |
9549 | x"$*"y or "x$*y" and there are no positional parameters, | |
7117c2d2 | 9550 | the $* should expand into nothing. */ |
95732b49 JA |
9551 | /* We use the W_HASQUOTEDNULL flag to differentiate the |
9552 | cases: a quoted null character as above and when | |
9553 | CTLNUL is contained in the (non-null) expansion | |
9554 | of some variable. We use the had_quoted_null flag to | |
9555 | pass the value through this function to its caller. */ | |
9556 | if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0) | |
cce855bc | 9557 | remove_quoted_nulls (temp); /* XXX */ |
726f6388 JA |
9558 | } |
9559 | } | |
ccc6cda3 JA |
9560 | else |
9561 | temp = (char *)NULL; | |
726f6388 | 9562 | |
ccc6cda3 | 9563 | /* We do not want to add quoted nulls to strings that are only |
ac50fbac CR |
9564 | partially quoted; we can throw them away. The exception to |
9565 | this is when we are going to be performing word splitting, | |
9566 | since we have to preserve a null argument if the next character | |
9567 | will cause word splitting. */ | |
495aee44 | 9568 | if (temp == 0 && quoted_state == PARTIALLY_QUOTED && (word->flags & (W_NOSPLIT|W_NOSPLIT2))) |
cce855bc | 9569 | continue; |
726f6388 | 9570 | |
ccc6cda3 | 9571 | add_quoted_string: |
726f6388 | 9572 | |
ccc6cda3 JA |
9573 | if (temp) |
9574 | { | |
9575 | temp1 = temp; | |
9576 | temp = quote_string (temp); | |
9577 | free (temp1); | |
bb70624e | 9578 | goto add_string; |
ccc6cda3 JA |
9579 | } |
9580 | else | |
9581 | { | |
9582 | /* Add NULL arg. */ | |
bb70624e JA |
9583 | c = CTLNUL; |
9584 | sindex--; /* add_character: label increments sindex */ | |
9585 | goto add_character; | |
ccc6cda3 | 9586 | } |
bb70624e | 9587 | |
ccc6cda3 | 9588 | /* break; */ |
726f6388 | 9589 | |
ccc6cda3 | 9590 | case '\'': |
95732b49 | 9591 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
ccc6cda3 | 9592 | goto add_character; |
726f6388 | 9593 | |
ccc6cda3 JA |
9594 | t_index = ++sindex; |
9595 | temp = string_extract_single_quoted (string, &sindex); | |
726f6388 | 9596 | |
ccc6cda3 JA |
9597 | /* If the entire STRING was surrounded by single quotes, |
9598 | then the string is wholly quoted. */ | |
9599 | quoted_state = (t_index == 1 && string[sindex] == '\0') | |
9600 | ? WHOLLY_QUOTED | |
7117c2d2 | 9601 | : PARTIALLY_QUOTED; |
726f6388 | 9602 | |
ccc6cda3 JA |
9603 | /* If all we had was '', it is a null expansion. */ |
9604 | if (*temp == '\0') | |
9605 | { | |
9606 | free (temp); | |
9607 | temp = (char *)NULL; | |
9608 | } | |
9609 | else | |
7117c2d2 | 9610 | remove_quoted_escapes (temp); /* ??? */ |
726f6388 | 9611 | |
ccc6cda3 | 9612 | /* We do not want to add quoted nulls to strings that are only |
a0c0a00f CR |
9613 | partially quoted; such nulls are discarded. See above for the |
9614 | exception, which is when the string is going to be split. */ | |
9615 | if (temp == 0 && (quoted_state == PARTIALLY_QUOTED) && (word->flags & (W_NOSPLIT|W_NOSPLIT2))) | |
ccc6cda3 | 9616 | continue; |
726f6388 | 9617 | |
bb70624e JA |
9618 | /* If we have a quoted null expansion, add a quoted NULL to istring. */ |
9619 | if (temp == 0) | |
9620 | { | |
9621 | c = CTLNUL; | |
9622 | sindex--; /* add_character: label increments sindex */ | |
9623 | goto add_character; | |
9624 | } | |
9625 | else | |
9626 | goto add_quoted_string; | |
9627 | ||
ccc6cda3 | 9628 | /* break; */ |
726f6388 JA |
9629 | |
9630 | default: | |
726f6388 | 9631 | /* This is the fix for " $@ " */ |
17345e5a | 9632 | add_ifs_character: |
a0c0a00f | 9633 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0)) |
726f6388 | 9634 | { |
bb70624e JA |
9635 | if (string[sindex]) /* from old goto dollar_add_string */ |
9636 | sindex++; | |
9637 | if (c == 0) | |
9638 | { | |
9639 | c = CTLNUL; | |
9640 | goto add_character; | |
9641 | } | |
9642 | else | |
9643 | { | |
7117c2d2 | 9644 | #if HANDLE_MULTIBYTE |
a0c0a00f | 9645 | if (mb_cur_max > 1) |
b80f6443 JA |
9646 | sindex--; |
9647 | ||
a0c0a00f | 9648 | if (mb_cur_max > 1) |
7117c2d2 | 9649 | { |
b80f6443 | 9650 | SADD_MBQCHAR_BODY(temp, string, sindex, string_size); |
7117c2d2 JA |
9651 | } |
9652 | else | |
9653 | #endif | |
9654 | { | |
9655 | twochars[0] = CTLESC; | |
9656 | twochars[1] = c; | |
9657 | goto add_twochars; | |
9658 | } | |
bb70624e | 9659 | } |
726f6388 JA |
9660 | } |
9661 | ||
7117c2d2 JA |
9662 | SADD_MBCHAR (temp, string, sindex, string_size); |
9663 | ||
726f6388 | 9664 | add_character: |
ccc6cda3 JA |
9665 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size, |
9666 | DEFAULT_ARRAY_SIZE); | |
726f6388 JA |
9667 | istring[istring_index++] = c; |
9668 | istring[istring_index] = '\0'; | |
9669 | ||
9670 | /* Next character. */ | |
9671 | sindex++; | |
9672 | } | |
9673 | } | |
9674 | ||
9675 | finished_with_string: | |
726f6388 JA |
9676 | /* OK, we're ready to return. If we have a quoted string, and |
9677 | quoted_dollar_at is not set, we do no splitting at all; otherwise | |
9678 | we split on ' '. The routines that call this will handle what to | |
9679 | do if nothing has been expanded. */ | |
ccc6cda3 JA |
9680 | |
9681 | /* Partially and wholly quoted strings which expand to the empty | |
9682 | string are retained as an empty arguments. Unquoted strings | |
9683 | which expand to the empty string are discarded. The single | |
9684 | exception is the case of expanding "$@" when there are no | |
9685 | positional parameters. In that case, we discard the expansion. */ | |
9686 | ||
9687 | /* Because of how the code that handles "" and '' in partially | |
9688 | quoted strings works, we need to make ISTRING into a QUOTED_NULL | |
9689 | if we saw quoting characters, but the expansion was empty. | |
9690 | "" and '' are tossed away before we get to this point when | |
9691 | processing partially quoted strings. This makes "" and $xxx"" | |
9692 | equivalent when xxx is unset. We also look to see whether we | |
9693 | saw a quoted null from a ${} expansion and add one back if we | |
9694 | need to. */ | |
9695 | ||
9696 | /* If we expand to nothing and there were no single or double quotes | |
9697 | in the word, we throw it away. Otherwise, we return a NULL word. | |
9698 | The single exception is for $@ surrounded by double quotes when | |
9699 | there are no positional parameters. In that case, we also throw | |
9700 | the word away. */ | |
9701 | ||
9702 | if (*istring == '\0') | |
9703 | { | |
9704 | if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED)) | |
726f6388 | 9705 | { |
726f6388 JA |
9706 | istring[0] = CTLNUL; |
9707 | istring[1] = '\0'; | |
ccc6cda3 | 9708 | tword = make_bare_word (istring); |
95732b49 | 9709 | tword->flags |= W_HASQUOTEDNULL; /* XXX */ |
ccc6cda3 JA |
9710 | list = make_word_list (tword, (WORD_LIST *)NULL); |
9711 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
9712 | tword->flags |= W_QUOTED; | |
726f6388 | 9713 | } |
ccc6cda3 JA |
9714 | /* According to sh, ksh, and Posix.2, if a word expands into nothing |
9715 | and a double-quoted "$@" appears anywhere in it, then the entire | |
9716 | word is removed. */ | |
a0c0a00f CR |
9717 | /* XXX - exception appears to be that quoted null strings result in |
9718 | null arguments */ | |
ccc6cda3 JA |
9719 | else if (quoted_state == UNQUOTED || quoted_dollar_at) |
9720 | list = (WORD_LIST *)NULL; | |
9721 | #if 0 | |
9722 | else | |
726f6388 | 9723 | { |
ccc6cda3 | 9724 | tword = make_bare_word (istring); |
ccc6cda3 JA |
9725 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
9726 | tword->flags |= W_QUOTED; | |
95732b49 | 9727 | list = make_word_list (tword, (WORD_LIST *)NULL); |
726f6388 | 9728 | } |
f73dda09 JA |
9729 | #else |
9730 | else | |
9731 | list = (WORD_LIST *)NULL; | |
ccc6cda3 JA |
9732 | #endif |
9733 | } | |
9734 | else if (word->flags & W_NOSPLIT) | |
9735 | { | |
9736 | tword = make_bare_word (istring); | |
ccc6cda3 JA |
9737 | if (word->flags & W_ASSIGNMENT) |
9738 | tword->flags |= W_ASSIGNMENT; /* XXX */ | |
95732b49 JA |
9739 | if (word->flags & W_COMPASSIGN) |
9740 | tword->flags |= W_COMPASSIGN; /* XXX */ | |
b72432fd JA |
9741 | if (word->flags & W_NOGLOB) |
9742 | tword->flags |= W_NOGLOB; /* XXX */ | |
ac50fbac CR |
9743 | if (word->flags & W_NOBRACE) |
9744 | tword->flags |= W_NOBRACE; /* XXX */ | |
95732b49 JA |
9745 | if (word->flags & W_NOEXPAND) |
9746 | tword->flags |= W_NOEXPAND; /* XXX */ | |
ccc6cda3 | 9747 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
28ef6c31 | 9748 | tword->flags |= W_QUOTED; |
a601c749 | 9749 | if (had_quoted_null && QUOTED_NULL (istring)) |
95732b49 JA |
9750 | tword->flags |= W_HASQUOTEDNULL; |
9751 | list = make_word_list (tword, (WORD_LIST *)NULL); | |
ccc6cda3 JA |
9752 | } |
9753 | else | |
9754 | { | |
9755 | char *ifs_chars; | |
3b34f6e6 | 9756 | char *tstring; |
ccc6cda3 | 9757 | |
7117c2d2 | 9758 | ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL; |
726f6388 | 9759 | |
cce855bc JA |
9760 | /* If we have $@, we need to split the results no matter what. If |
9761 | IFS is unset or NULL, string_list_dollar_at has separated the | |
9762 | positional parameters with a space, so we split on space (we have | |
9763 | set ifs_chars to " \t\n" above if ifs is unset). If IFS is set, | |
9764 | string_list_dollar_at has separated the positional parameters | |
ac50fbac CR |
9765 | with the first character of $IFS, so we split on $IFS. If |
9766 | SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either | |
9767 | unset or null, and we want to make sure that we split on spaces | |
a0c0a00f CR |
9768 | regardless of what else has happened to IFS since the expansion, |
9769 | or we expanded "$@" with IFS null and we need to split the positional | |
9770 | parameters into separate words. */ | |
ac50fbac CR |
9771 | if (split_on_spaces) |
9772 | list = list_string (istring, " ", 1); /* XXX quoted == 1? */ | |
3b34f6e6 CR |
9773 | /* If we have $@ (has_dollar_at != 0) and we are in a context where we |
9774 | don't want to split the result (W_NOSPLIT2), and we are not quoted, | |
9775 | we have already separated the arguments with the first character of | |
9776 | $IFS. In this case, we want to return a list with a single word | |
9777 | with the separator possibly replaced with a space (it's what other | |
9778 | shells seem to do). | |
9779 | quoted_dollar_at is internal to this function and is set if we are | |
9780 | passed an argument that is unquoted (quoted == 0) but we encounter a | |
9781 | double-quoted $@ while expanding it. */ | |
9782 | else if (has_dollar_at && quoted_dollar_at == 0 && ifs_chars && quoted == 0 && (word->flags & W_NOSPLIT2)) | |
9783 | { | |
9784 | /* Only split and rejoin if we have to */ | |
9785 | if (*ifs_chars && *ifs_chars != ' ') | |
9786 | { | |
9787 | list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1); | |
9788 | tstring = string_list (list); | |
9789 | } | |
9790 | else | |
9791 | tstring = istring; | |
9792 | tword = make_bare_word (tstring); | |
9793 | if (tstring != istring) | |
9794 | free (tstring); | |
9795 | goto set_word_flags; | |
9796 | } | |
a0c0a00f CR |
9797 | /* This is the attempt to make $* in an assignment context (a=$*) and |
9798 | array variables subscripted with * in an assignment context (a=${foo[*]}) | |
9799 | behave similarly. It has side effects that, though they increase | |
9800 | compatibility with other shells, are not backwards compatible. */ | |
9801 | #if 0 | |
9802 | else if (has_dollar_at && quoted == 0 && ifs_chars && (word->flags & W_ASSIGNRHS)) | |
9803 | { | |
9804 | tword = make_bare_word (istring); | |
9805 | goto set_word_flags; | |
9806 | } | |
9807 | #endif | |
ac50fbac | 9808 | else if (has_dollar_at && ifs_chars) |
cce855bc | 9809 | list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1); |
ccc6cda3 JA |
9810 | else |
9811 | { | |
9812 | tword = make_bare_word (istring); | |
3b34f6e6 | 9813 | set_word_flags: |
ccc6cda3 JA |
9814 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED)) |
9815 | tword->flags |= W_QUOTED; | |
9816 | if (word->flags & W_ASSIGNMENT) | |
9817 | tword->flags |= W_ASSIGNMENT; | |
95732b49 JA |
9818 | if (word->flags & W_COMPASSIGN) |
9819 | tword->flags |= W_COMPASSIGN; | |
b72432fd JA |
9820 | if (word->flags & W_NOGLOB) |
9821 | tword->flags |= W_NOGLOB; | |
ac50fbac CR |
9822 | if (word->flags & W_NOBRACE) |
9823 | tword->flags |= W_NOBRACE; | |
95732b49 JA |
9824 | if (word->flags & W_NOEXPAND) |
9825 | tword->flags |= W_NOEXPAND; | |
a601c749 | 9826 | if (had_quoted_null && QUOTED_NULL (istring)) |
95732b49 JA |
9827 | tword->flags |= W_HASQUOTEDNULL; /* XXX */ |
9828 | list = make_word_list (tword, (WORD_LIST *)NULL); | |
726f6388 | 9829 | } |
726f6388 | 9830 | } |
726f6388 | 9831 | |
ccc6cda3 JA |
9832 | free (istring); |
9833 | return (list); | |
726f6388 JA |
9834 | } |
9835 | ||
9836 | /* **************************************************************** */ | |
9837 | /* */ | |
9838 | /* Functions for Quote Removal */ | |
9839 | /* */ | |
9840 | /* **************************************************************** */ | |
9841 | ||
9842 | /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the | |
7117c2d2 | 9843 | backslash quoting rules for within double quotes or a here document. */ |
726f6388 JA |
9844 | char * |
9845 | string_quote_removal (string, quoted) | |
9846 | char *string; | |
9847 | int quoted; | |
9848 | { | |
7117c2d2 JA |
9849 | size_t slen; |
9850 | char *r, *result_string, *temp, *send; | |
f73dda09 JA |
9851 | int sindex, tindex, dquote; |
9852 | unsigned char c; | |
7117c2d2 | 9853 | DECLARE_MBSTATE; |
726f6388 JA |
9854 | |
9855 | /* The result can be no longer than the original string. */ | |
7117c2d2 JA |
9856 | slen = strlen (string); |
9857 | send = string + slen; | |
9858 | ||
9859 | r = result_string = (char *)xmalloc (slen + 1); | |
726f6388 | 9860 | |
ccc6cda3 | 9861 | for (dquote = sindex = 0; c = string[sindex];) |
726f6388 JA |
9862 | { |
9863 | switch (c) | |
9864 | { | |
9865 | case '\\': | |
9866 | c = string[++sindex]; | |
3185942a JA |
9867 | if (c == 0) |
9868 | { | |
9869 | *r++ = '\\'; | |
9870 | break; | |
9871 | } | |
28ef6c31 | 9872 | if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0) |
726f6388 | 9873 | *r++ = '\\'; |
ccc6cda3 | 9874 | /* FALLTHROUGH */ |
726f6388 JA |
9875 | |
9876 | default: | |
7117c2d2 | 9877 | SCOPY_CHAR_M (r, string, send, sindex); |
726f6388 JA |
9878 | break; |
9879 | ||
9880 | case '\'': | |
ccc6cda3 | 9881 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) |
726f6388 JA |
9882 | { |
9883 | *r++ = c; | |
9884 | sindex++; | |
ccc6cda3 | 9885 | break; |
726f6388 | 9886 | } |
ccc6cda3 JA |
9887 | tindex = sindex + 1; |
9888 | temp = string_extract_single_quoted (string, &tindex); | |
9889 | if (temp) | |
726f6388 | 9890 | { |
ccc6cda3 JA |
9891 | strcpy (r, temp); |
9892 | r += strlen (r); | |
9893 | free (temp); | |
726f6388 | 9894 | } |
ccc6cda3 | 9895 | sindex = tindex; |
726f6388 JA |
9896 | break; |
9897 | ||
9898 | case '"': | |
9899 | dquote = 1 - dquote; | |
9900 | sindex++; | |
9901 | break; | |
9902 | } | |
9903 | } | |
9904 | *r = '\0'; | |
9905 | return (result_string); | |
9906 | } | |
9907 | ||
ccc6cda3 JA |
9908 | #if 0 |
9909 | /* UNUSED */ | |
726f6388 JA |
9910 | /* Perform quote removal on word WORD. This allocates and returns a new |
9911 | WORD_DESC *. */ | |
9912 | WORD_DESC * | |
9913 | word_quote_removal (word, quoted) | |
9914 | WORD_DESC *word; | |
9915 | int quoted; | |
9916 | { | |
9917 | WORD_DESC *w; | |
9918 | char *t; | |
9919 | ||
9920 | t = string_quote_removal (word->word, quoted); | |
95732b49 JA |
9921 | w = alloc_word_desc (); |
9922 | w->word = t ? t : savestring (""); | |
726f6388 JA |
9923 | return (w); |
9924 | } | |
9925 | ||
9926 | /* Perform quote removal on all words in LIST. If QUOTED is non-zero, | |
9927 | the members of the list are treated as if they are surrounded by | |
9928 | double quotes. Return a new list, or NULL if LIST is NULL. */ | |
9929 | WORD_LIST * | |
9930 | word_list_quote_removal (list, quoted) | |
9931 | WORD_LIST *list; | |
9932 | int quoted; | |
9933 | { | |
95732b49 | 9934 | WORD_LIST *result, *t, *tresult, *e; |
726f6388 | 9935 | |
ccc6cda3 | 9936 | for (t = list, result = (WORD_LIST *)NULL; t; t = t->next) |
726f6388 | 9937 | { |
7117c2d2 | 9938 | tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL); |
95732b49 | 9939 | #if 0 |
726f6388 | 9940 | result = (WORD_LIST *) list_append (result, tresult); |
95732b49 JA |
9941 | #else |
9942 | if (result == 0) | |
9943 | result = e = tresult; | |
9944 | else | |
9945 | { | |
9946 | e->next = tresult; | |
9947 | while (e->next) | |
9948 | e = e->next; | |
9949 | } | |
9950 | #endif | |
726f6388 JA |
9951 | } |
9952 | return (result); | |
9953 | } | |
ccc6cda3 | 9954 | #endif |
726f6388 | 9955 | |
726f6388 JA |
9956 | /******************************************* |
9957 | * * | |
9958 | * Functions to perform word splitting * | |
9959 | * * | |
9960 | *******************************************/ | |
9961 | ||
7117c2d2 JA |
9962 | void |
9963 | setifs (v) | |
9964 | SHELL_VAR *v; | |
b72432fd | 9965 | { |
7117c2d2 JA |
9966 | char *t; |
9967 | unsigned char uc; | |
9968 | ||
9969 | ifs_var = v; | |
95732b49 | 9970 | ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n"; |
b72432fd | 9971 | |
ac50fbac CR |
9972 | ifs_is_set = ifs_var != 0; |
9973 | ifs_is_null = ifs_is_set && (*ifs_value == 0); | |
9974 | ||
95732b49 JA |
9975 | /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet |
9976 | handle multibyte chars in IFS */ | |
7117c2d2 JA |
9977 | memset (ifs_cmap, '\0', sizeof (ifs_cmap)); |
9978 | for (t = ifs_value ; t && *t; t++) | |
9979 | { | |
9980 | uc = *t; | |
9981 | ifs_cmap[uc] = 1; | |
9982 | } | |
9983 | ||
95732b49 JA |
9984 | #if defined (HANDLE_MULTIBYTE) |
9985 | if (ifs_value == 0) | |
9986 | { | |
9987 | ifs_firstc[0] = '\0'; | |
9988 | ifs_firstc_len = 1; | |
9989 | } | |
9990 | else | |
9991 | { | |
9992 | size_t ifs_len; | |
9993 | ifs_len = strnlen (ifs_value, MB_CUR_MAX); | |
9994 | ifs_firstc_len = MBLEN (ifs_value, ifs_len); | |
9995 | if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len)) | |
9996 | { | |
9997 | ifs_firstc[0] = ifs_value[0]; | |
9998 | ifs_firstc[1] = '\0'; | |
9999 | ifs_firstc_len = 1; | |
10000 | } | |
10001 | else | |
10002 | memcpy (ifs_firstc, ifs_value, ifs_firstc_len); | |
10003 | } | |
10004 | #else | |
7117c2d2 | 10005 | ifs_firstc = ifs_value ? *ifs_value : 0; |
95732b49 | 10006 | #endif |
7117c2d2 JA |
10007 | } |
10008 | ||
10009 | char * | |
10010 | getifs () | |
10011 | { | |
10012 | return ifs_value; | |
b72432fd JA |
10013 | } |
10014 | ||
726f6388 JA |
10015 | /* This splits a single word into a WORD LIST on $IFS, but only if the word |
10016 | is not quoted. list_string () performs quote removal for us, even if we | |
10017 | don't do any splitting. */ | |
10018 | WORD_LIST * | |
7117c2d2 | 10019 | word_split (w, ifs_chars) |
726f6388 | 10020 | WORD_DESC *w; |
7117c2d2 | 10021 | char *ifs_chars; |
726f6388 JA |
10022 | { |
10023 | WORD_LIST *result; | |
10024 | ||
10025 | if (w) | |
10026 | { | |
7117c2d2 | 10027 | char *xifs; |
726f6388 | 10028 | |
7117c2d2 JA |
10029 | xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars; |
10030 | result = list_string (w->word, xifs, w->flags & W_QUOTED); | |
726f6388 JA |
10031 | } |
10032 | else | |
10033 | result = (WORD_LIST *)NULL; | |
ccc6cda3 | 10034 | |
726f6388 JA |
10035 | return (result); |
10036 | } | |
10037 | ||
10038 | /* Perform word splitting on LIST and return the RESULT. It is possible | |
10039 | to return (WORD_LIST *)NULL. */ | |
10040 | static WORD_LIST * | |
10041 | word_list_split (list) | |
10042 | WORD_LIST *list; | |
10043 | { | |
95732b49 | 10044 | WORD_LIST *result, *t, *tresult, *e; |
726f6388 | 10045 | |
ccc6cda3 | 10046 | for (t = list, result = (WORD_LIST *)NULL; t; t = t->next) |
726f6388 | 10047 | { |
7117c2d2 | 10048 | tresult = word_split (t->word, ifs_value); |
95732b49 JA |
10049 | if (result == 0) |
10050 | result = e = tresult; | |
10051 | else | |
10052 | { | |
10053 | e->next = tresult; | |
10054 | while (e->next) | |
10055 | e = e->next; | |
10056 | } | |
726f6388 JA |
10057 | } |
10058 | return (result); | |
10059 | } | |
10060 | ||
10061 | /************************************************** | |
10062 | * * | |
cce855bc | 10063 | * Functions to expand an entire WORD_LIST * |
726f6388 JA |
10064 | * * |
10065 | **************************************************/ | |
10066 | ||
b80f6443 JA |
10067 | /* Do any word-expansion-specific cleanup and jump to top_level */ |
10068 | static void | |
10069 | exp_jump_to_top_level (v) | |
10070 | int v; | |
10071 | { | |
3185942a JA |
10072 | set_pipestatus_from_exit (last_command_exit_value); |
10073 | ||
b80f6443 JA |
10074 | /* Cleanup code goes here. */ |
10075 | expand_no_split_dollar_star = 0; /* XXX */ | |
10076 | expanding_redir = 0; | |
3185942a | 10077 | assigning_in_environment = 0; |
b80f6443 | 10078 | |
f1be666c JA |
10079 | if (parse_and_execute_level == 0) |
10080 | top_level_cleanup (); /* from sig.c */ | |
10081 | ||
b80f6443 JA |
10082 | jump_to_top_level (v); |
10083 | } | |
10084 | ||
cce855bc JA |
10085 | /* Put NLIST (which is a WORD_LIST * of only one element) at the front of |
10086 | ELIST, and set ELIST to the new list. */ | |
10087 | #define PREPEND_LIST(nlist, elist) \ | |
10088 | do { nlist->next = elist; elist = nlist; } while (0) | |
10089 | ||
726f6388 JA |
10090 | /* Separate out any initial variable assignments from TLIST. If set -k has |
10091 | been executed, remove all assignment statements from TLIST. Initial | |
10092 | variable assignments and other environment assignments are placed | |
bb70624e | 10093 | on SUBST_ASSIGN_VARLIST. */ |
726f6388 JA |
10094 | static WORD_LIST * |
10095 | separate_out_assignments (tlist) | |
10096 | WORD_LIST *tlist; | |
10097 | { | |
10098 | register WORD_LIST *vp, *lp; | |
10099 | ||
0001803f | 10100 | if (tlist == 0) |
726f6388 JA |
10101 | return ((WORD_LIST *)NULL); |
10102 | ||
bb70624e JA |
10103 | if (subst_assign_varlist) |
10104 | dispose_words (subst_assign_varlist); /* Clean up after previous error */ | |
b72432fd | 10105 | |
bb70624e | 10106 | subst_assign_varlist = (WORD_LIST *)NULL; |
726f6388 JA |
10107 | vp = lp = tlist; |
10108 | ||
10109 | /* Separate out variable assignments at the start of the command. | |
10110 | Loop invariant: vp->next == lp | |
10111 | Loop postcondition: | |
7117c2d2 JA |
10112 | lp = list of words left after assignment statements skipped |
10113 | tlist = original list of words | |
726f6388 | 10114 | */ |
ccc6cda3 | 10115 | while (lp && (lp->word->flags & W_ASSIGNMENT)) |
726f6388 JA |
10116 | { |
10117 | vp = lp; | |
10118 | lp = lp->next; | |
10119 | } | |
10120 | ||
bb70624e JA |
10121 | /* If lp != tlist, we have some initial assignment statements. |
10122 | We make SUBST_ASSIGN_VARLIST point to the list of assignment | |
10123 | words and TLIST point to the remaining words. */ | |
726f6388 JA |
10124 | if (lp != tlist) |
10125 | { | |
bb70624e | 10126 | subst_assign_varlist = tlist; |
726f6388 JA |
10127 | /* ASSERT(vp->next == lp); */ |
10128 | vp->next = (WORD_LIST *)NULL; /* terminate variable list */ | |
10129 | tlist = lp; /* remainder of word list */ | |
10130 | } | |
10131 | ||
10132 | /* vp == end of variable list */ | |
10133 | /* tlist == remainder of original word list without variable assignments */ | |
10134 | if (!tlist) | |
10135 | /* All the words in tlist were assignment statements */ | |
10136 | return ((WORD_LIST *)NULL); | |
10137 | ||
10138 | /* ASSERT(tlist != NULL); */ | |
ccc6cda3 | 10139 | /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */ |
726f6388 JA |
10140 | |
10141 | /* If the -k option is in effect, we need to go through the remaining | |
bb70624e JA |
10142 | words, separate out the assignment words, and place them on |
10143 | SUBST_ASSIGN_VARLIST. */ | |
726f6388 JA |
10144 | if (place_keywords_in_env) |
10145 | { | |
10146 | WORD_LIST *tp; /* tp == running pointer into tlist */ | |
10147 | ||
10148 | tp = tlist; | |
10149 | lp = tlist->next; | |
10150 | ||
10151 | /* Loop Invariant: tp->next == lp */ | |
10152 | /* Loop postcondition: tlist == word list without assignment statements */ | |
10153 | while (lp) | |
10154 | { | |
ccc6cda3 | 10155 | if (lp->word->flags & W_ASSIGNMENT) |
726f6388 JA |
10156 | { |
10157 | /* Found an assignment statement, add this word to end of | |
bb70624e JA |
10158 | subst_assign_varlist (vp). */ |
10159 | if (!subst_assign_varlist) | |
10160 | subst_assign_varlist = vp = lp; | |
726f6388 JA |
10161 | else |
10162 | { | |
10163 | vp->next = lp; | |
10164 | vp = lp; | |
10165 | } | |
10166 | ||
10167 | /* Remove the word pointed to by LP from TLIST. */ | |
10168 | tp->next = lp->next; | |
10169 | /* ASSERT(vp == lp); */ | |
10170 | lp->next = (WORD_LIST *)NULL; | |
10171 | lp = tp->next; | |
10172 | } | |
10173 | else | |
10174 | { | |
10175 | tp = lp; | |
10176 | lp = lp->next; | |
10177 | } | |
10178 | } | |
10179 | } | |
10180 | return (tlist); | |
10181 | } | |
10182 | ||
cce855bc JA |
10183 | #define WEXP_VARASSIGN 0x001 |
10184 | #define WEXP_BRACEEXP 0x002 | |
10185 | #define WEXP_TILDEEXP 0x004 | |
10186 | #define WEXP_PARAMEXP 0x008 | |
10187 | #define WEXP_PATHEXP 0x010 | |
10188 | ||
10189 | /* All of the expansions, including variable assignments at the start of | |
10190 | the list. */ | |
10191 | #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP) | |
10192 | ||
10193 | /* All of the expansions except variable assignments at the start of | |
10194 | the list. */ | |
10195 | #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP) | |
10196 | ||
10197 | /* All of the `shell expansions': brace expansion, tilde expansion, parameter | |
10198 | expansion, command substitution, arithmetic expansion, word splitting, and | |
10199 | quote removal. */ | |
10200 | #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP) | |
10201 | ||
726f6388 JA |
10202 | /* Take the list of words in LIST and do the various substitutions. Return |
10203 | a new list of words which is the expanded list, and without things like | |
10204 | variable assignments. */ | |
10205 | ||
10206 | WORD_LIST * | |
10207 | expand_words (list) | |
10208 | WORD_LIST *list; | |
10209 | { | |
cce855bc | 10210 | return (expand_word_list_internal (list, WEXP_ALL)); |
726f6388 JA |
10211 | } |
10212 | ||
10213 | /* Same as expand_words (), but doesn't hack variable or environment | |
10214 | variables. */ | |
10215 | WORD_LIST * | |
10216 | expand_words_no_vars (list) | |
10217 | WORD_LIST *list; | |
10218 | { | |
cce855bc | 10219 | return (expand_word_list_internal (list, WEXP_NOVARS)); |
726f6388 JA |
10220 | } |
10221 | ||
cce855bc JA |
10222 | WORD_LIST * |
10223 | expand_words_shellexp (list) | |
726f6388 | 10224 | WORD_LIST *list; |
726f6388 | 10225 | { |
cce855bc JA |
10226 | return (expand_word_list_internal (list, WEXP_SHELLEXP)); |
10227 | } | |
726f6388 | 10228 | |
cce855bc JA |
10229 | static WORD_LIST * |
10230 | glob_expand_word_list (tlist, eflags) | |
10231 | WORD_LIST *tlist; | |
10232 | int eflags; | |
10233 | { | |
10234 | char **glob_array, *temp_string; | |
10235 | register int glob_index; | |
10236 | WORD_LIST *glob_list, *output_list, *disposables, *next; | |
10237 | WORD_DESC *tword; | |
726f6388 | 10238 | |
cce855bc JA |
10239 | output_list = disposables = (WORD_LIST *)NULL; |
10240 | glob_array = (char **)NULL; | |
10241 | while (tlist) | |
10242 | { | |
10243 | /* For each word, either globbing is attempted or the word is | |
10244 | added to orig_list. If globbing succeeds, the results are | |
10245 | added to orig_list and the word (tlist) is added to the list | |
10246 | of disposable words. If globbing fails and failed glob | |
10247 | expansions are left unchanged (the shell default), the | |
10248 | original word is added to orig_list. If globbing fails and | |
10249 | failed glob expansions are removed, the original word is | |
10250 | added to the list of disposable words. orig_list ends up | |
7117c2d2 | 10251 | in reverse order and requires a call to REVERSE_LIST to |
cce855bc JA |
10252 | be set right. After all words are examined, the disposable |
10253 | words are freed. */ | |
10254 | next = tlist->next; | |
726f6388 | 10255 | |
cce855bc | 10256 | /* If the word isn't an assignment and contains an unquoted |
28ef6c31 | 10257 | pattern matching character, then glob it. */ |
b72432fd | 10258 | if ((tlist->word->flags & W_NOGLOB) == 0 && |
cce855bc | 10259 | unquoted_glob_pattern_p (tlist->word->word)) |
726f6388 | 10260 | { |
cce855bc JA |
10261 | glob_array = shell_glob_filename (tlist->word->word); |
10262 | ||
10263 | /* Handle error cases. | |
10264 | I don't think we should report errors like "No such file | |
10265 | or directory". However, I would like to report errors | |
10266 | like "Read failed". */ | |
10267 | ||
b80f6443 | 10268 | if (glob_array == 0 || GLOB_FAILED (glob_array)) |
726f6388 | 10269 | { |
bb70624e | 10270 | glob_array = (char **)xmalloc (sizeof (char *)); |
cce855bc JA |
10271 | glob_array[0] = (char *)NULL; |
10272 | } | |
10273 | ||
10274 | /* Dequote the current word in case we have to use it. */ | |
10275 | if (glob_array[0] == NULL) | |
10276 | { | |
10277 | temp_string = dequote_string (tlist->word->word); | |
10278 | free (tlist->word->word); | |
10279 | tlist->word->word = temp_string; | |
10280 | } | |
10281 | ||
10282 | /* Make the array into a word list. */ | |
10283 | glob_list = (WORD_LIST *)NULL; | |
10284 | for (glob_index = 0; glob_array[glob_index]; glob_index++) | |
10285 | { | |
10286 | tword = make_bare_word (glob_array[glob_index]); | |
cce855bc JA |
10287 | glob_list = make_word_list (tword, glob_list); |
10288 | } | |
10289 | ||
10290 | if (glob_list) | |
10291 | { | |
10292 | output_list = (WORD_LIST *)list_append (glob_list, output_list); | |
10293 | PREPEND_LIST (tlist, disposables); | |
10294 | } | |
b80f6443 JA |
10295 | else if (fail_glob_expansion != 0) |
10296 | { | |
ac50fbac | 10297 | last_command_exit_value = EXECUTION_FAILURE; |
b80f6443 | 10298 | report_error (_("no match: %s"), tlist->word->word); |
f1be666c | 10299 | exp_jump_to_top_level (DISCARD); |
b80f6443 | 10300 | } |
cce855bc JA |
10301 | else if (allow_null_glob_expansion == 0) |
10302 | { | |
10303 | /* Failed glob expressions are left unchanged. */ | |
10304 | PREPEND_LIST (tlist, output_list); | |
10305 | } | |
10306 | else | |
10307 | { | |
10308 | /* Failed glob expressions are removed. */ | |
10309 | PREPEND_LIST (tlist, disposables); | |
726f6388 | 10310 | } |
726f6388 | 10311 | } |
cce855bc JA |
10312 | else |
10313 | { | |
10314 | /* Dequote the string. */ | |
10315 | temp_string = dequote_string (tlist->word->word); | |
10316 | free (tlist->word->word); | |
10317 | tlist->word->word = temp_string; | |
10318 | PREPEND_LIST (tlist, output_list); | |
10319 | } | |
10320 | ||
7117c2d2 | 10321 | strvec_dispose (glob_array); |
cce855bc JA |
10322 | glob_array = (char **)NULL; |
10323 | ||
10324 | tlist = next; | |
726f6388 JA |
10325 | } |
10326 | ||
cce855bc JA |
10327 | if (disposables) |
10328 | dispose_words (disposables); | |
10329 | ||
10330 | if (output_list) | |
10331 | output_list = REVERSE_LIST (output_list, WORD_LIST *); | |
10332 | ||
10333 | return (output_list); | |
10334 | } | |
726f6388 JA |
10335 | |
10336 | #if defined (BRACE_EXPANSION) | |
cce855bc JA |
10337 | static WORD_LIST * |
10338 | brace_expand_word_list (tlist, eflags) | |
10339 | WORD_LIST *tlist; | |
10340 | int eflags; | |
10341 | { | |
10342 | register char **expansions; | |
10343 | char *temp_string; | |
10344 | WORD_LIST *disposables, *output_list, *next; | |
10345 | WORD_DESC *w; | |
10346 | int eindex; | |
10347 | ||
10348 | for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next) | |
726f6388 | 10349 | { |
cce855bc | 10350 | next = tlist->next; |
726f6388 | 10351 | |
ac50fbac CR |
10352 | if (tlist->word->flags & W_NOBRACE) |
10353 | { | |
10354 | /*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/ | |
10355 | PREPEND_LIST (tlist, output_list); | |
10356 | continue; | |
10357 | } | |
10358 | ||
0001803f CR |
10359 | if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG)) |
10360 | { | |
10361 | /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/ | |
10362 | PREPEND_LIST (tlist, output_list); | |
10363 | continue; | |
10364 | } | |
ac50fbac | 10365 | |
cce855bc JA |
10366 | /* Only do brace expansion if the word has a brace character. If |
10367 | not, just add the word list element to BRACES and continue. In | |
10368 | the common case, at least when running shell scripts, this will | |
0001803f | 10369 | degenerate to a bunch of calls to `mbschr', and then what is |
cce855bc | 10370 | basically a reversal of TLIST into BRACES, which is corrected |
7117c2d2 | 10371 | by a call to REVERSE_LIST () on BRACES when the end of TLIST |
cce855bc | 10372 | is reached. */ |
0001803f | 10373 | if (mbschr (tlist->word->word, LBRACE)) |
726f6388 | 10374 | { |
cce855bc | 10375 | expansions = brace_expand (tlist->word->word); |
726f6388 | 10376 | |
cce855bc | 10377 | for (eindex = 0; temp_string = expansions[eindex]; eindex++) |
726f6388 | 10378 | { |
ac50fbac CR |
10379 | w = alloc_word_desc (); |
10380 | w->word = temp_string; | |
10381 | ||
cce855bc JA |
10382 | /* If brace expansion didn't change the word, preserve |
10383 | the flags. We may want to preserve the flags | |
10384 | unconditionally someday -- XXX */ | |
10385 | if (STREQ (temp_string, tlist->word->word)) | |
10386 | w->flags = tlist->word->flags; | |
ac50fbac CR |
10387 | else |
10388 | w = make_word_flags (w, temp_string); | |
10389 | ||
cce855bc | 10390 | output_list = make_word_list (w, output_list); |
726f6388 | 10391 | } |
cce855bc | 10392 | free (expansions); |
726f6388 | 10393 | |
cce855bc JA |
10394 | /* Add TLIST to the list of words to be freed after brace |
10395 | expansion has been performed. */ | |
10396 | PREPEND_LIST (tlist, disposables); | |
10397 | } | |
10398 | else | |
10399 | PREPEND_LIST (tlist, output_list); | |
726f6388 | 10400 | } |
cce855bc JA |
10401 | |
10402 | if (disposables) | |
10403 | dispose_words (disposables); | |
10404 | ||
10405 | if (output_list) | |
10406 | output_list = REVERSE_LIST (output_list, WORD_LIST *); | |
10407 | ||
10408 | return (output_list); | |
10409 | } | |
10410 | #endif | |
10411 | ||
3185942a JA |
10412 | #if defined (ARRAY_VARS) |
10413 | /* Take WORD, a compound associative array assignment, and internally run | |
10414 | 'declare -A w', where W is the variable name portion of WORD. */ | |
10415 | static int | |
a0c0a00f | 10416 | make_internal_declare (word, option, cmd) |
3185942a JA |
10417 | char *word; |
10418 | char *option; | |
a0c0a00f | 10419 | char *cmd; |
3185942a | 10420 | { |
a0c0a00f | 10421 | int t, r; |
3185942a JA |
10422 | WORD_LIST *wl; |
10423 | WORD_DESC *w; | |
10424 | ||
10425 | w = make_word (word); | |
10426 | ||
10427 | t = assignment (w->word, 0); | |
a0c0a00f CR |
10428 | if (w->word[t] == '=') |
10429 | { | |
10430 | w->word[t] = '\0'; | |
10431 | if (w->word[t - 1] == '+') /* cut off any append op */ | |
10432 | w->word[t - 1] = '\0'; | |
10433 | } | |
3185942a JA |
10434 | |
10435 | wl = make_word_list (w, (WORD_LIST *)NULL); | |
10436 | wl = make_word_list (make_word (option), wl); | |
10437 | ||
a0c0a00f CR |
10438 | r = declare_builtin (wl); |
10439 | ||
10440 | dispose_words (wl); | |
10441 | return r; | |
3185942a JA |
10442 | } |
10443 | #endif | |
10444 | ||
cce855bc JA |
10445 | static WORD_LIST * |
10446 | shell_expand_word_list (tlist, eflags) | |
10447 | WORD_LIST *tlist; | |
10448 | int eflags; | |
10449 | { | |
a0c0a00f | 10450 | WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list, *wcmd; |
cce855bc JA |
10451 | int expanded_something, has_dollar_at; |
10452 | char *temp_string; | |
726f6388 | 10453 | |
726f6388 | 10454 | /* We do tilde expansion all the time. This is what 1003.2 says. */ |
cce855bc | 10455 | new_list = (WORD_LIST *)NULL; |
a0c0a00f CR |
10456 | for (wcmd = tlist; wcmd; wcmd = wcmd->next) |
10457 | if (wcmd->word->flags & W_ASSNBLTIN) | |
10458 | break; | |
10459 | ||
cce855bc | 10460 | for (orig_list = tlist; tlist; tlist = next) |
726f6388 | 10461 | { |
ccc6cda3 | 10462 | temp_string = tlist->word->word; |
726f6388 JA |
10463 | |
10464 | next = tlist->next; | |
10465 | ||
95732b49 JA |
10466 | #if defined (ARRAY_VARS) |
10467 | /* If this is a compound array assignment to a builtin that accepts | |
10468 | such assignments (e.g., `declare'), take the assignment and perform | |
10469 | it separately, handling the semantics of declarations inside shell | |
10470 | functions. This avoids the double-evaluation of such arguments, | |
10471 | because `declare' does some evaluation of compound assignments on | |
10472 | its own. */ | |
10473 | if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG)) | |
10474 | { | |
10475 | int t; | |
a0c0a00f | 10476 | char opts[16], opti; |
ac50fbac CR |
10477 | |
10478 | opti = 0; | |
10479 | if (tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL|W_ASSIGNARRAY)) | |
10480 | opts[opti++] = '-'; | |
10481 | ||
10482 | if ((tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL)) == (W_ASSIGNASSOC|W_ASSNGLOBAL)) | |
10483 | { | |
10484 | opts[opti++] = 'g'; | |
10485 | opts[opti++] = 'A'; | |
10486 | } | |
10487 | else if (tlist->word->flags & W_ASSIGNASSOC) | |
10488 | opts[opti++] = 'A'; | |
10489 | else if ((tlist->word->flags & (W_ASSIGNARRAY|W_ASSNGLOBAL)) == (W_ASSIGNARRAY|W_ASSNGLOBAL)) | |
10490 | { | |
10491 | opts[opti++] = 'g'; | |
10492 | opts[opti++] = 'a'; | |
10493 | } | |
10494 | else if (tlist->word->flags & W_ASSIGNARRAY) | |
10495 | opts[opti++] = 'a'; | |
10496 | else if (tlist->word->flags & W_ASSNGLOBAL) | |
10497 | opts[opti++] = 'g'; | |
95732b49 | 10498 | |
a0c0a00f CR |
10499 | /* If we have special handling note the integer attribute and others |
10500 | that transform the value upon assignment. What we do is take all | |
10501 | of the option arguments and scan through them looking for options | |
10502 | that cause such transformations, and add them to the `opts' array. */ | |
10503 | /* if (opti > 0) */ | |
10504 | { | |
10505 | char omap[128]; | |
10506 | int oind; | |
10507 | WORD_LIST *l; | |
10508 | ||
10509 | memset (omap, '\0', sizeof (omap)); | |
10510 | for (l = orig_list->next; l != tlist; l = l->next) | |
10511 | { | |
10512 | if (l->word->word[0] != '-') | |
10513 | break; /* non-option argument */ | |
10514 | if (l->word->word[0] == '-' && l->word->word[1] == '-' && l->word->word[2] == 0) | |
10515 | break; /* -- signals end of options */ | |
10516 | for (oind = 1; l->word->word[oind]; oind++) | |
10517 | switch (l->word->word[oind]) | |
10518 | { | |
10519 | case 'i': | |
10520 | case 'l': | |
10521 | case 'u': | |
10522 | case 'c': | |
10523 | omap[l->word->word[oind]] = 1; | |
10524 | if (opti == 0) | |
10525 | opts[opti++] = '-'; | |
10526 | break; | |
10527 | default: | |
10528 | break; | |
10529 | } | |
10530 | } | |
10531 | ||
10532 | for (oind = 0; oind < sizeof (omap); oind++) | |
10533 | if (omap[oind]) | |
10534 | opts[opti++] = oind; | |
10535 | } | |
ac50fbac CR |
10536 | |
10537 | opts[opti] = '\0'; | |
10538 | if (opti > 0) | |
30a978b7 | 10539 | { |
a0c0a00f | 10540 | t = make_internal_declare (tlist->word->word, opts, wcmd ? wcmd->word->word : (char *)0); |
30a978b7 CR |
10541 | if (t != EXECUTION_SUCCESS) |
10542 | { | |
10543 | last_command_exit_value = t; | |
10544 | exp_jump_to_top_level (DISCARD); | |
10545 | } | |
10546 | } | |
3185942a | 10547 | |
495aee44 | 10548 | t = do_word_assignment (tlist->word, 0); |
95732b49 JA |
10549 | if (t == 0) |
10550 | { | |
10551 | last_command_exit_value = EXECUTION_FAILURE; | |
10552 | exp_jump_to_top_level (DISCARD); | |
10553 | } | |
10554 | ||
10555 | /* Now transform the word as ksh93 appears to do and go on */ | |
10556 | t = assignment (tlist->word->word, 0); | |
10557 | tlist->word->word[t] = '\0'; | |
a0c0a00f CR |
10558 | if (tlist->word->word[t - 1] == '+') |
10559 | tlist->word->word[t - 1] = '\0'; /* cut off append op */ | |
ac50fbac | 10560 | tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC|W_ASSIGNARRAY); |
726f6388 | 10561 | } |
95732b49 | 10562 | #endif |
726f6388 | 10563 | |
ccc6cda3 | 10564 | expanded_something = 0; |
726f6388 | 10565 | expanded = expand_word_internal |
b72432fd | 10566 | (tlist->word, 0, 0, &has_dollar_at, &expanded_something); |
726f6388 JA |
10567 | |
10568 | if (expanded == &expand_word_error || expanded == &expand_word_fatal) | |
10569 | { | |
10570 | /* By convention, each time this error is returned, | |
10571 | tlist->word->word has already been freed. */ | |
10572 | tlist->word->word = (char *)NULL; | |
ccc6cda3 | 10573 | |
726f6388 JA |
10574 | /* Dispose our copy of the original list. */ |
10575 | dispose_words (orig_list); | |
d166f048 | 10576 | /* Dispose the new list we're building. */ |
726f6388 JA |
10577 | dispose_words (new_list); |
10578 | ||
28ef6c31 | 10579 | last_command_exit_value = EXECUTION_FAILURE; |
726f6388 | 10580 | if (expanded == &expand_word_error) |
b80f6443 | 10581 | exp_jump_to_top_level (DISCARD); |
726f6388 | 10582 | else |
b80f6443 | 10583 | exp_jump_to_top_level (FORCE_EOF); |
726f6388 JA |
10584 | } |
10585 | ||
ccc6cda3 JA |
10586 | /* Don't split words marked W_NOSPLIT. */ |
10587 | if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0) | |
726f6388 | 10588 | { |
ccc6cda3 | 10589 | temp_list = word_list_split (expanded); |
726f6388 JA |
10590 | dispose_words (expanded); |
10591 | } | |
10592 | else | |
10593 | { | |
10594 | /* If no parameter expansion, command substitution, process | |
10595 | substitution, or arithmetic substitution took place, then | |
10596 | do not do word splitting. We still have to remove quoted | |
10597 | null characters from the result. */ | |
10598 | word_list_remove_quoted_nulls (expanded); | |
ccc6cda3 | 10599 | temp_list = expanded; |
726f6388 JA |
10600 | } |
10601 | ||
ccc6cda3 JA |
10602 | expanded = REVERSE_LIST (temp_list, WORD_LIST *); |
10603 | new_list = (WORD_LIST *)list_append (expanded, new_list); | |
726f6388 JA |
10604 | } |
10605 | ||
cce855bc JA |
10606 | if (orig_list) |
10607 | dispose_words (orig_list); | |
726f6388 | 10608 | |
726f6388 | 10609 | if (new_list) |
cce855bc | 10610 | new_list = REVERSE_LIST (new_list, WORD_LIST *); |
726f6388 | 10611 | |
cce855bc JA |
10612 | return (new_list); |
10613 | } | |
726f6388 | 10614 | |
cce855bc JA |
10615 | /* The workhorse for expand_words () and expand_words_no_vars (). |
10616 | First arg is LIST, a WORD_LIST of words. | |
b72432fd JA |
10617 | Second arg EFLAGS is a flags word controlling which expansions are |
10618 | performed. | |
726f6388 | 10619 | |
cce855bc JA |
10620 | This does all of the substitutions: brace expansion, tilde expansion, |
10621 | parameter expansion, command substitution, arithmetic expansion, | |
10622 | process substitution, word splitting, and pathname expansion, according | |
10623 | to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits | |
10624 | set, or for which no expansion is done, do not undergo word splitting. | |
ac50fbac CR |
10625 | Words with the W_NOGLOB bit set do not undergo pathname expansion; words |
10626 | with W_NOBRACE set do not undergo brace expansion (see | |
10627 | brace_expand_word_list above). */ | |
cce855bc JA |
10628 | static WORD_LIST * |
10629 | expand_word_list_internal (list, eflags) | |
10630 | WORD_LIST *list; | |
10631 | int eflags; | |
10632 | { | |
10633 | WORD_LIST *new_list, *temp_list; | |
10634 | int tint; | |
a0c0a00f | 10635 | char *savecmd; |
726f6388 | 10636 | |
ac50fbac | 10637 | tempenv_assign_error = 0; |
cce855bc JA |
10638 | if (list == 0) |
10639 | return ((WORD_LIST *)NULL); | |
726f6388 | 10640 | |
bb70624e | 10641 | garglist = new_list = copy_word_list (list); |
cce855bc JA |
10642 | if (eflags & WEXP_VARASSIGN) |
10643 | { | |
bb70624e | 10644 | garglist = new_list = separate_out_assignments (new_list); |
cce855bc JA |
10645 | if (new_list == 0) |
10646 | { | |
bb70624e | 10647 | if (subst_assign_varlist) |
cce855bc JA |
10648 | { |
10649 | /* All the words were variable assignments, so they are placed | |
10650 | into the shell's environment. */ | |
bb70624e | 10651 | for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next) |
cce855bc | 10652 | { |
a0c0a00f | 10653 | savecmd = this_command_name; |
cce855bc | 10654 | this_command_name = (char *)NULL; /* no arithmetic errors */ |
495aee44 | 10655 | tint = do_word_assignment (temp_list->word, 0); |
a0c0a00f | 10656 | this_command_name = savecmd; |
cce855bc JA |
10657 | /* Variable assignment errors in non-interactive shells |
10658 | running in Posix.2 mode cause the shell to exit. */ | |
28ef6c31 | 10659 | if (tint == 0) |
ccc6cda3 | 10660 | { |
cce855bc | 10661 | last_command_exit_value = EXECUTION_FAILURE; |
28ef6c31 | 10662 | if (interactive_shell == 0 && posixly_correct) |
b80f6443 | 10663 | exp_jump_to_top_level (FORCE_EOF); |
28ef6c31 | 10664 | else |
b80f6443 | 10665 | exp_jump_to_top_level (DISCARD); |
ccc6cda3 | 10666 | } |
726f6388 | 10667 | } |
bb70624e JA |
10668 | dispose_words (subst_assign_varlist); |
10669 | subst_assign_varlist = (WORD_LIST *)NULL; | |
cce855bc JA |
10670 | } |
10671 | return ((WORD_LIST *)NULL); | |
10672 | } | |
10673 | } | |
726f6388 | 10674 | |
cce855bc JA |
10675 | /* Begin expanding the words that remain. The expansions take place on |
10676 | things that aren't really variable assignments. */ | |
726f6388 | 10677 | |
cce855bc JA |
10678 | #if defined (BRACE_EXPANSION) |
10679 | /* Do brace expansion on this word if there are any brace characters | |
10680 | in the string. */ | |
10681 | if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list) | |
10682 | new_list = brace_expand_word_list (new_list, eflags); | |
10683 | #endif /* BRACE_EXPANSION */ | |
726f6388 | 10684 | |
cce855bc JA |
10685 | /* Perform the `normal' shell expansions: tilde expansion, parameter and |
10686 | variable substitution, command substitution, arithmetic expansion, | |
10687 | and word splitting. */ | |
10688 | new_list = shell_expand_word_list (new_list, eflags); | |
726f6388 | 10689 | |
cce855bc JA |
10690 | /* Okay, we're almost done. Now let's just do some filename |
10691 | globbing. */ | |
10692 | if (new_list) | |
10693 | { | |
10694 | if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0) | |
10695 | /* Glob expand the word list unless globbing has been disabled. */ | |
10696 | new_list = glob_expand_word_list (new_list, eflags); | |
726f6388 | 10697 | else |
cce855bc JA |
10698 | /* Dequote the words, because we're not performing globbing. */ |
10699 | new_list = dequote_list (new_list); | |
726f6388 JA |
10700 | } |
10701 | ||
bb70624e | 10702 | if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist) |
726f6388 | 10703 | { |
95732b49 | 10704 | sh_wassign_func_t *assign_func; |
495aee44 | 10705 | int is_special_builtin, is_builtin_or_func; |
726f6388 JA |
10706 | |
10707 | /* If the remainder of the words expand to nothing, Posix.2 requires | |
10708 | that the variable and environment assignments affect the shell's | |
10709 | environment. */ | |
95732b49 | 10710 | assign_func = new_list ? assign_in_env : do_word_assignment; |
b80f6443 | 10711 | tempenv_assign_error = 0; |
726f6388 | 10712 | |
495aee44 CR |
10713 | is_builtin_or_func = (new_list && new_list->word && (find_shell_builtin (new_list->word->word) || find_function (new_list->word->word))); |
10714 | /* Posix says that special builtins exit if a variable assignment error | |
10715 | occurs in an assignment preceding it. */ | |
10716 | is_special_builtin = (posixly_correct && new_list && new_list->word && find_special_builtin (new_list->word->word)); | |
10717 | ||
bb70624e | 10718 | for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next) |
726f6388 | 10719 | { |
a0c0a00f | 10720 | savecmd = this_command_name; |
ccc6cda3 | 10721 | this_command_name = (char *)NULL; |
3185942a | 10722 | assigning_in_environment = (assign_func == assign_in_env); |
495aee44 | 10723 | tint = (*assign_func) (temp_list->word, is_builtin_or_func); |
3185942a | 10724 | assigning_in_environment = 0; |
a0c0a00f | 10725 | this_command_name = savecmd; |
ccc6cda3 JA |
10726 | /* Variable assignment errors in non-interactive shells running |
10727 | in Posix.2 mode cause the shell to exit. */ | |
b80f6443 | 10728 | if (tint == 0) |
ccc6cda3 | 10729 | { |
95732b49 | 10730 | if (assign_func == do_word_assignment) |
b80f6443 JA |
10731 | { |
10732 | last_command_exit_value = EXECUTION_FAILURE; | |
495aee44 | 10733 | if (interactive_shell == 0 && posixly_correct && is_special_builtin) |
b80f6443 JA |
10734 | exp_jump_to_top_level (FORCE_EOF); |
10735 | else | |
10736 | exp_jump_to_top_level (DISCARD); | |
10737 | } | |
28ef6c31 | 10738 | else |
b80f6443 | 10739 | tempenv_assign_error++; |
ccc6cda3 | 10740 | } |
726f6388 | 10741 | } |
726f6388 | 10742 | |
bb70624e JA |
10743 | dispose_words (subst_assign_varlist); |
10744 | subst_assign_varlist = (WORD_LIST *)NULL; | |
726f6388 JA |
10745 | } |
10746 | ||
cce855bc | 10747 | return (new_list); |
ccc6cda3 | 10748 | } |