]>
Commit | Line | Data |
---|---|---|
cc87ba64 CR |
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 | ||
73a146be | 7 | /* Copyright (C) 1987-2013 Free Software Foundation, Inc. |
726f6388 JA |
8 | |
9 | This file is part of GNU Bash, the Bourne Again SHell. | |
10 | ||
2e4498b3 CR |
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. | |
15 | ||
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. | |
20 | ||
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" |
e77a3058 CR |
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 JA |
39 | |
40 | #include "bashansi.h" | |
41 | #include "posixstat.h" | |
5e13499c | 42 | #include "bashintl.h" |
726f6388 JA |
43 | |
44 | #include "shell.h" | |
9e51a74d | 45 | #include "parser.h" |
726f6388 JA |
46 | #include "flags.h" |
47 | #include "jobs.h" | |
48 | #include "execute_cmd.h" | |
49 | #include "filecntl.h" | |
ccc6cda3 JA |
50 | #include "trap.h" |
51 | #include "pathexp.h" | |
52 | #include "mailcheck.h" | |
53 | ||
7117c2d2 | 54 | #include "shmbutil.h" |
06dff54a | 55 | #include "typemax.h" |
7117c2d2 | 56 | |
ccc6cda3 JA |
57 | #include "builtins/getopt.h" |
58 | #include "builtins/common.h" | |
726f6388 | 59 | |
48ff5447 CR |
60 | #include "builtins/builtext.h" |
61 | ||
cce855bc | 62 | #include <tilde/tilde.h> |
f73dda09 | 63 | #include <glob/strmatch.h> |
ccc6cda3 JA |
64 | |
65 | #if !defined (errno) | |
66 | extern int errno; | |
67 | #endif /* !errno */ | |
726f6388 JA |
68 | |
69 | /* The size that strings change by. */ | |
d166f048 | 70 | #define DEFAULT_INITIAL_ARRAY_SIZE 112 |
ccc6cda3 JA |
71 | #define DEFAULT_ARRAY_SIZE 128 |
72 | ||
73 | /* Variable types. */ | |
74 | #define VT_VARIABLE 0 | |
75 | #define VT_POSPARMS 1 | |
76 | #define VT_ARRAYVAR 2 | |
d166f048 | 77 | #define VT_ARRAYMEMBER 3 |
e33f2203 | 78 | #define VT_ASSOCVAR 4 |
726f6388 | 79 | |
d3a24ed2 CR |
80 | #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */ |
81 | ||
ccc6cda3 JA |
82 | /* Flags for quoted_strchr */ |
83 | #define ST_BACKSL 0x01 | |
84 | #define ST_CTLESC 0x02 | |
7117c2d2 JA |
85 | #define ST_SQUOTE 0x04 /* unused yet */ |
86 | #define ST_DQUOTE 0x08 /* unused yet */ | |
87 | ||
d3a24ed2 CR |
88 | /* Flags for the `pflags' argument to param_expand() */ |
89 | #define PF_NOCOMSUB 0x01 /* Do not perform command substitution */ | |
1231ac47 | 90 | #define PF_IGNUNBOUND 0x02 /* ignore unbound vars even if -u set */ |
e1e48bba | 91 | #define PF_NOSPLIT2 0x04 /* same as W_NOSPLIT2 */ |
40647963 | 92 | #define PF_ASSIGNRHS 0x08 /* same as W_ASSIGNRHS */ |
d3a24ed2 | 93 | |
cce855bc JA |
94 | /* These defs make it easier to use the editor. */ |
95 | #define LBRACE '{' | |
96 | #define RBRACE '}' | |
97 | #define LPAREN '(' | |
98 | #define RPAREN ')' | |
bb579650 CR |
99 | #define LBRACK '[' |
100 | #define RBRACK ']' | |
726f6388 | 101 | |
176b12ee CR |
102 | #if defined (HANDLE_MULTIBYTE) |
103 | #define WLPAREN L'(' | |
104 | #define WRPAREN L')' | |
105 | #endif | |
106 | ||
28ef6c31 JA |
107 | /* Evaluates to 1 if C is one of the shell's special parameters whose length |
108 | can be taken, but is also one of the special expansion characters. */ | |
109 | #define VALID_SPECIAL_LENGTH_PARAM(c) \ | |
110 | ((c) == '-' || (c) == '?' || (c) == '#') | |
111 | ||
112 | /* Evaluates to 1 if C is one of the shell's special parameters for which an | |
113 | indirect variable reference may be made. */ | |
114 | #define VALID_INDIR_PARAM(c) \ | |
e05be32d | 115 | ((posixly_correct == 0 && (c) == '#') || (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*') |
28ef6c31 JA |
116 | |
117 | /* Evaluates to 1 if C is one of the OP characters that follows the parameter | |
118 | in ${parameter[:]OPword}. */ | |
7117c2d2 | 119 | #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP) |
28ef6c31 | 120 | |
bb70624e JA |
121 | /* Evaluates to 1 if this is one of the shell's special variables. */ |
122 | #define SPECIAL_VAR(name, wi) \ | |
f73dda09 JA |
123 | ((DIGIT (*name) && all_digits (name)) || \ |
124 | (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \ | |
28ef6c31 | 125 | (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1]))) |
bb70624e | 126 | |
f73dda09 JA |
127 | /* An expansion function that takes a string and a quoted flag and returns |
128 | a WORD_LIST *. Used as the type of the third argument to | |
129 | expand_string_if_necessary(). */ | |
130 | typedef WORD_LIST *EXPFUNC __P((char *, int)); | |
131 | ||
726f6388 JA |
132 | /* Process ID of the last command executed within command substitution. */ |
133 | pid_t last_command_subst_pid = NO_PID; | |
b72432fd | 134 | pid_t current_command_subst_pid = NO_PID; |
726f6388 | 135 | |
7117c2d2 JA |
136 | /* Variables used to keep track of the characters in IFS. */ |
137 | SHELL_VAR *ifs_var; | |
138 | char *ifs_value; | |
139 | unsigned char ifs_cmap[UCHAR_MAX + 1]; | |
f0c4de40 | 140 | int ifs_is_set, ifs_is_null; |
633e5c6d CR |
141 | |
142 | #if defined (HANDLE_MULTIBYTE) | |
143 | unsigned char ifs_firstc[MB_LEN_MAX]; | |
144 | size_t ifs_firstc_len; | |
145 | #else | |
7117c2d2 | 146 | unsigned char ifs_firstc; |
633e5c6d | 147 | #endif |
7117c2d2 | 148 | |
0527c903 CR |
149 | /* Sentinel to tell when we are performing variable assignments preceding a |
150 | command name and putting them into the environment. Used to make sure | |
151 | we use the temporary environment when looking up variable values. */ | |
d7f49990 CR |
152 | int assigning_in_environment; |
153 | ||
0527c903 CR |
154 | /* Used to hold a list of variable assignments preceding a command. Global |
155 | so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a | |
156 | SIGCHLD trap and so it can be saved and restored by the trap handlers. */ | |
157 | WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL; | |
158 | ||
726f6388 | 159 | /* Extern functions and variables from different files. */ |
d3a24ed2 | 160 | extern int last_command_exit_value, last_command_exit_signal; |
f486d0a1 CR |
161 | extern int subshell_environment, line_number; |
162 | extern int subshell_level, parse_and_execute_level, sourcelevel; | |
7117c2d2 | 163 | extern int eof_encountered; |
bb70624e | 164 | extern int return_catch_flag, return_catch_value; |
f73dda09 | 165 | extern pid_t dollar_dollar_pid; |
726f6388 | 166 | extern int posixly_correct; |
726f6388 | 167 | extern char *this_command_name; |
ccc6cda3 | 168 | extern struct fd_bitmap *current_fds_to_close; |
cce855bc | 169 | extern int wordexp_only; |
d3a24ed2 | 170 | extern int expanding_redir; |
56299fa5 | 171 | extern int tempenv_assign_error; |
77b3aacb | 172 | extern int builtin_ignoring_errexit; |
726f6388 | 173 | |
d3ad40de CR |
174 | #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE) |
175 | extern wchar_t *wcsdup __P((const wchar_t *)); | |
176 | #endif | |
177 | ||
ccc6cda3 JA |
178 | /* Non-zero means to allow unmatched globbed filenames to expand to |
179 | a null file. */ | |
180 | int allow_null_glob_expansion; | |
181 | ||
d3a24ed2 CR |
182 | /* Non-zero means to throw an error when globbing fails to match anything. */ |
183 | int fail_glob_expansion; | |
184 | ||
f73dda09 | 185 | #if 0 |
ccc6cda3 JA |
186 | /* Variables to keep track of which words in an expanded word list (the |
187 | output of expand_word_list_internal) are the result of globbing | |
f73dda09 JA |
188 | expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c. |
189 | (CURRENTLY UNUSED). */ | |
ccc6cda3 JA |
190 | char *glob_argv_flags; |
191 | static int glob_argv_flags_size; | |
f73dda09 | 192 | #endif |
726f6388 JA |
193 | |
194 | static WORD_LIST expand_word_error, expand_word_fatal; | |
227f982e | 195 | static WORD_DESC expand_wdesc_error, expand_wdesc_fatal; |
726f6388 | 196 | static char expand_param_error, expand_param_fatal; |
7027abcb | 197 | static char extract_string_error, extract_string_fatal; |
726f6388 | 198 | |
28ef6c31 JA |
199 | /* Tell the expansion functions to not longjmp back to top_level on fatal |
200 | errors. Enabled when doing completion and prompt string expansion. */ | |
201 | static int no_longjmp_on_fatal_error = 0; | |
202 | ||
203 | /* Set by expand_word_unsplit; used to inhibit splitting and re-joining | |
204 | $* on $IFS, primarily when doing assignment statements. */ | |
205 | static int expand_no_split_dollar_star = 0; | |
bb70624e | 206 | |
bb70624e JA |
207 | /* A WORD_LIST of words to be expanded by expand_word_list_internal, |
208 | without any leading variable assignments. */ | |
209 | static WORD_LIST *garglist = (WORD_LIST *)NULL; | |
b72432fd | 210 | |
f73dda09 | 211 | static char *quoted_substring __P((char *, int, int)); |
7117c2d2 JA |
212 | static int quoted_strlen __P((char *)); |
213 | static char *quoted_strchr __P((char *, int, int)); | |
f73dda09 JA |
214 | |
215 | static char *expand_string_if_necessary __P((char *, int, EXPFUNC *)); | |
216 | static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *)); | |
217 | static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *)); | |
218 | static WORD_LIST *expand_string_internal __P((char *, int)); | |
219 | static WORD_LIST *expand_string_leave_quoted __P((char *, int)); | |
220 | static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *)); | |
221 | ||
f73dda09 | 222 | static WORD_LIST *list_quote_escapes __P((WORD_LIST *)); |
4a2c75c6 CR |
223 | static WORD_LIST *list_dequote_escapes __P((WORD_LIST *)); |
224 | ||
f73dda09 JA |
225 | static char *make_quoted_char __P((int)); |
226 | static WORD_LIST *quote_list __P((WORD_LIST *)); | |
f73dda09 JA |
227 | |
228 | static int unquoted_substring __P((char *, char *)); | |
229 | static int unquoted_member __P((int, char *)); | |
230 | ||
43df7bbb CR |
231 | #if defined (ARRAY_VARS) |
232 | static SHELL_VAR *do_compound_assignment __P((char *, char *, int)); | |
233 | #endif | |
234 | static int do_assignment_internal __P((const WORD_DESC *, int)); | |
f73dda09 | 235 | |
d3ad40de | 236 | static char *string_extract_verbatim __P((char *, size_t, int *, char *, int)); |
f73dda09 JA |
237 | static char *string_extract __P((char *, int *, char *, int)); |
238 | static char *string_extract_double_quoted __P((char *, int *, int)); | |
7117c2d2 | 239 | static inline char *string_extract_single_quoted __P((char *, int *)); |
d3ad40de | 240 | static inline int skip_single_quoted __P((const char *, size_t, int)); |
7117c2d2 JA |
241 | static int skip_double_quoted __P((char *, size_t, int)); |
242 | static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int)); | |
243 | static char *extract_dollar_brace_string __P((char *, int *, int, int)); | |
6932f7f5 | 244 | static int skip_matched_pair __P((const char *, int, int, int, int)); |
f73dda09 | 245 | |
f73dda09 JA |
246 | static char *pos_params __P((char *, int, int, int)); |
247 | ||
545f34cf CR |
248 | static unsigned char *mb_getcharlens __P((char *, int)); |
249 | ||
704a1a2a | 250 | static char *remove_upattern __P((char *, char *, int)); |
d3ad40de | 251 | #if defined (HANDLE_MULTIBYTE) |
704a1a2a CR |
252 | static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int)); |
253 | #endif | |
f73dda09 | 254 | static char *remove_pattern __P((char *, char *, int)); |
704a1a2a | 255 | |
704a1a2a CR |
256 | static int match_upattern __P((char *, char *, int, char **, char **)); |
257 | #if defined (HANDLE_MULTIBYTE) | |
704a1a2a CR |
258 | static int match_wpattern __P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **)); |
259 | #endif | |
f73dda09 JA |
260 | static int match_pattern __P((char *, char *, int, char **, char **)); |
261 | static int getpatspec __P((int, char *)); | |
262 | static char *getpattern __P((char *, int, int)); | |
7117c2d2 | 263 | static char *variable_remove_pattern __P((char *, char *, int, int)); |
f73dda09 | 264 | static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int)); |
7117c2d2 | 265 | static char *parameter_list_remove_pattern __P((int, char *, int, int)); |
f73dda09 | 266 | #ifdef ARRAY_VARS |
fdf670ea | 267 | static char *array_remove_pattern __P((SHELL_VAR *, char *, int, char *, int)); |
f73dda09 | 268 | #endif |
5f8cde23 | 269 | static char *parameter_brace_remove_pattern __P((char *, char *, int, char *, int, int, int)); |
f73dda09 JA |
270 | |
271 | static char *process_substitute __P((char *, int)); | |
272 | ||
d3ad40de | 273 | static char *read_comsub __P((int, int, int *)); |
f73dda09 JA |
274 | |
275 | #ifdef ARRAY_VARS | |
276 | static arrayind_t array_length_reference __P((char *)); | |
277 | #endif | |
278 | ||
279 | static int valid_brace_expansion_word __P((char *, int)); | |
d3a24ed2 | 280 | static int chk_atstar __P((char *, int, int *, int *)); |
d3ad40de | 281 | static int chk_arithsub __P((const char *, int)); |
d3a24ed2 | 282 | |
5f8cde23 | 283 | static WORD_DESC *parameter_brace_expand_word __P((char *, int, int, int, arrayind_t *)); |
c111d992 | 284 | static char *parameter_brace_find_indir __P((char *, int, int, int)); |
227f982e CR |
285 | static WORD_DESC *parameter_brace_expand_indir __P((char *, int, int, int *, int *)); |
286 | static WORD_DESC *parameter_brace_expand_rhs __P((char *, char *, int, int, int *, int *)); | |
f73dda09 JA |
287 | static void parameter_brace_expand_error __P((char *, char *)); |
288 | ||
289 | static int valid_length_expression __P((char *)); | |
7117c2d2 | 290 | static intmax_t parameter_brace_expand_length __P((char *)); |
f73dda09 JA |
291 | |
292 | static char *skiparith __P((char *, int)); | |
09767ff0 | 293 | static int verify_substring_values __P((SHELL_VAR *, char *, char *, int, intmax_t *, intmax_t *)); |
5f8cde23 | 294 | static int get_var_and_type __P((char *, char *, arrayind_t, int, int, SHELL_VAR **, char **)); |
545f34cf | 295 | static char *mb_substring __P((char *, int, int)); |
5f8cde23 | 296 | static char *parameter_brace_substring __P((char *, char *, int, char *, int, int)); |
f73dda09 | 297 | |
ee56a865 CR |
298 | static int shouldexp_replacement __P((char *)); |
299 | ||
f73dda09 JA |
300 | static char *pos_params_pat_subst __P((char *, char *, char *, int)); |
301 | ||
5f8cde23 | 302 | static char *parameter_brace_patsub __P((char *, char *, int, char *, int, int)); |
f73dda09 | 303 | |
09767ff0 | 304 | static char *pos_params_casemod __P((char *, char *, int, int)); |
5f8cde23 | 305 | static char *parameter_brace_casemod __P((char *, char *, int, int, char *, int, int)); |
09767ff0 | 306 | |
e1e48bba | 307 | static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int, int *, int *)); |
227f982e | 308 | static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int)); |
f73dda09 JA |
309 | |
310 | static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *)); | |
311 | ||
f73dda09 JA |
312 | static WORD_LIST *word_list_split __P((WORD_LIST *)); |
313 | ||
d3a24ed2 CR |
314 | static void exp_jump_to_top_level __P((int)); |
315 | ||
f73dda09 JA |
316 | static WORD_LIST *separate_out_assignments __P((WORD_LIST *)); |
317 | static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int)); | |
318 | #ifdef BRACE_EXPANSION | |
319 | static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int)); | |
320 | #endif | |
fdf670ea CR |
321 | #if defined (ARRAY_VARS) |
322 | static int make_internal_declare __P((char *, char *)); | |
323 | #endif | |
f73dda09 JA |
324 | static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int)); |
325 | static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int)); | |
726f6388 JA |
326 | |
327 | /* **************************************************************** */ | |
328 | /* */ | |
329 | /* Utility Functions */ | |
330 | /* */ | |
331 | /* **************************************************************** */ | |
332 | ||
9dd88db7 CR |
333 | #if defined (DEBUG) |
334 | void | |
335 | dump_word_flags (flags) | |
336 | int flags; | |
337 | { | |
338 | int f; | |
339 | ||
340 | f = flags; | |
341 | fprintf (stderr, "%d -> ", f); | |
1034e10b CR |
342 | if (f & W_ARRAYIND) |
343 | { | |
344 | f &= ~W_ARRAYIND; | |
345 | fprintf (stderr, "W_ARRAYIND%s", f ? "|" : ""); | |
346 | } | |
9dd88db7 CR |
347 | if (f & W_ASSIGNASSOC) |
348 | { | |
349 | f &= ~W_ASSIGNASSOC; | |
350 | fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : ""); | |
351 | } | |
36eb585c CR |
352 | if (f & W_ASSIGNARRAY) |
353 | { | |
354 | f &= ~W_ASSIGNARRAY; | |
355 | fprintf (stderr, "W_ASSIGNARRAY%s", f ? "|" : ""); | |
356 | } | |
9dd88db7 CR |
357 | if (f & W_HASCTLESC) |
358 | { | |
359 | f &= ~W_HASCTLESC; | |
360 | fprintf (stderr, "W_HASCTLESC%s", f ? "|" : ""); | |
361 | } | |
362 | if (f & W_NOPROCSUB) | |
363 | { | |
364 | f &= ~W_NOPROCSUB; | |
365 | fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : ""); | |
366 | } | |
367 | if (f & W_DQUOTE) | |
368 | { | |
369 | f &= ~W_DQUOTE; | |
370 | fprintf (stderr, "W_DQUOTE%s", f ? "|" : ""); | |
371 | } | |
372 | if (f & W_HASQUOTEDNULL) | |
373 | { | |
374 | f &= ~W_HASQUOTEDNULL; | |
375 | fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : ""); | |
376 | } | |
377 | if (f & W_ASSIGNARG) | |
378 | { | |
379 | f &= ~W_ASSIGNARG; | |
380 | fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : ""); | |
381 | } | |
382 | if (f & W_ASSNBLTIN) | |
383 | { | |
384 | f &= ~W_ASSNBLTIN; | |
385 | fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : ""); | |
386 | } | |
ec860d76 CR |
387 | if (f & W_ASSNGLOBAL) |
388 | { | |
389 | f &= ~W_ASSNGLOBAL; | |
390 | fprintf (stderr, "W_ASSNGLOBAL%s", f ? "|" : ""); | |
391 | } | |
7571d3f4 CR |
392 | if (f & W_ASSIGNINT) |
393 | { | |
394 | f &= ~W_ASSIGNINT; | |
395 | fprintf (stderr, "W_ASSIGNINT%s", f ? "|" : ""); | |
396 | } | |
9dd88db7 CR |
397 | if (f & W_COMPASSIGN) |
398 | { | |
399 | f &= ~W_COMPASSIGN; | |
400 | fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : ""); | |
401 | } | |
402 | if (f & W_NOEXPAND) | |
403 | { | |
404 | f &= ~W_NOEXPAND; | |
405 | fprintf (stderr, "W_NOEXPAND%s", f ? "|" : ""); | |
406 | } | |
407 | if (f & W_ITILDE) | |
408 | { | |
409 | f &= ~W_ITILDE; | |
410 | fprintf (stderr, "W_ITILDE%s", f ? "|" : ""); | |
411 | } | |
412 | if (f & W_NOTILDE) | |
413 | { | |
414 | f &= ~W_NOTILDE; | |
415 | fprintf (stderr, "W_NOTILDE%s", f ? "|" : ""); | |
416 | } | |
417 | if (f & W_ASSIGNRHS) | |
418 | { | |
419 | f &= ~W_ASSIGNRHS; | |
420 | fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : ""); | |
421 | } | |
422 | if (f & W_NOCOMSUB) | |
423 | { | |
424 | f &= ~W_NOCOMSUB; | |
425 | fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : ""); | |
426 | } | |
427 | if (f & W_DOLLARSTAR) | |
428 | { | |
429 | f &= ~W_DOLLARSTAR; | |
430 | fprintf (stderr, "W_DOLLARSTAR%s", f ? "|" : ""); | |
431 | } | |
432 | if (f & W_DOLLARAT) | |
433 | { | |
434 | f &= ~W_DOLLARAT; | |
435 | fprintf (stderr, "W_DOLLARAT%s", f ? "|" : ""); | |
436 | } | |
437 | if (f & W_TILDEEXP) | |
438 | { | |
439 | f &= ~W_TILDEEXP; | |
440 | fprintf (stderr, "W_TILDEEXP%s", f ? "|" : ""); | |
441 | } | |
442 | if (f & W_NOSPLIT2) | |
443 | { | |
444 | f &= ~W_NOSPLIT2; | |
445 | fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : ""); | |
446 | } | |
9dd88db7 CR |
447 | if (f & W_NOSPLIT) |
448 | { | |
449 | f &= ~W_NOSPLIT; | |
450 | fprintf (stderr, "W_NOSPLIT%s", f ? "|" : ""); | |
451 | } | |
df0e4bfe CR |
452 | if (f & W_NOBRACE) |
453 | { | |
454 | f &= ~W_NOBRACE; | |
455 | fprintf (stderr, "W_NOBRACE%s", f ? "|" : ""); | |
456 | } | |
457 | if (f & W_NOGLOB) | |
458 | { | |
459 | f &= ~W_NOGLOB; | |
460 | fprintf (stderr, "W_NOGLOB%s", f ? "|" : ""); | |
461 | } | |
f0c4de40 | 462 | if (f & W_SPLITSPACE) |
9dd88db7 | 463 | { |
f0c4de40 CR |
464 | f &= ~W_SPLITSPACE; |
465 | fprintf (stderr, "W_SPLITSPACE%s", f ? "|" : ""); | |
9dd88db7 CR |
466 | } |
467 | if (f & W_ASSIGNMENT) | |
468 | { | |
469 | f &= ~W_ASSIGNMENT; | |
470 | fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : ""); | |
471 | } | |
472 | if (f & W_QUOTED) | |
473 | { | |
474 | f &= ~W_QUOTED; | |
475 | fprintf (stderr, "W_QUOTED%s", f ? "|" : ""); | |
476 | } | |
477 | if (f & W_HASDOLLAR) | |
478 | { | |
479 | f &= ~W_HASDOLLAR; | |
480 | fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : ""); | |
481 | } | |
482 | fprintf (stderr, "\n"); | |
483 | fflush (stderr); | |
484 | } | |
485 | #endif | |
486 | ||
7117c2d2 | 487 | #ifdef INCLUDE_UNUSED |
ccc6cda3 JA |
488 | static char * |
489 | quoted_substring (string, start, end) | |
490 | char *string; | |
491 | int start, end; | |
492 | { | |
493 | register int len, l; | |
494 | register char *result, *s, *r; | |
495 | ||
496 | len = end - start; | |
497 | ||
498 | /* Move to string[start], skipping quoted characters. */ | |
499 | for (s = string, l = 0; *s && l < start; ) | |
500 | { | |
501 | if (*s == CTLESC) | |
502 | { | |
28ef6c31 JA |
503 | s++; |
504 | continue; | |
ccc6cda3 JA |
505 | } |
506 | l++; | |
507 | if (*s == 0) | |
28ef6c31 | 508 | break; |
ccc6cda3 JA |
509 | } |
510 | ||
f73dda09 | 511 | r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */ |
ccc6cda3 JA |
512 | |
513 | /* Copy LEN characters, including quote characters. */ | |
514 | s = string + l; | |
515 | for (l = 0; l < len; s++) | |
516 | { | |
517 | if (*s == CTLESC) | |
28ef6c31 | 518 | *r++ = *s++; |
ccc6cda3 JA |
519 | *r++ = *s; |
520 | l++; | |
521 | if (*s == 0) | |
28ef6c31 | 522 | break; |
ccc6cda3 JA |
523 | } |
524 | *r = '\0'; | |
525 | return result; | |
526 | } | |
7117c2d2 JA |
527 | #endif |
528 | ||
529 | #ifdef INCLUDE_UNUSED | |
530 | /* Return the length of S, skipping over quoted characters */ | |
531 | static int | |
532 | quoted_strlen (s) | |
533 | char *s; | |
534 | { | |
535 | register char *p; | |
536 | int i; | |
537 | ||
538 | i = 0; | |
539 | for (p = s; *p; p++) | |
540 | { | |
541 | if (*p == CTLESC) | |
542 | { | |
543 | p++; | |
544 | if (*p == 0) | |
545 | return (i + 1); | |
546 | } | |
547 | i++; | |
548 | } | |
549 | ||
550 | return i; | |
551 | } | |
552 | #endif | |
ccc6cda3 JA |
553 | |
554 | /* Find the first occurrence of character C in string S, obeying shell | |
555 | quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped | |
556 | characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters | |
557 | escaped with CTLESC are skipped. */ | |
7117c2d2 | 558 | static char * |
ccc6cda3 JA |
559 | quoted_strchr (s, c, flags) |
560 | char *s; | |
561 | int c, flags; | |
562 | { | |
563 | register char *p; | |
564 | ||
565 | for (p = s; *p; p++) | |
566 | { | |
567 | if (((flags & ST_BACKSL) && *p == '\\') | |
568 | || ((flags & ST_CTLESC) && *p == CTLESC)) | |
569 | { | |
570 | p++; | |
571 | if (*p == '\0') | |
572 | return ((char *)NULL); | |
573 | continue; | |
574 | } | |
575 | else if (*p == c) | |
576 | return p; | |
577 | } | |
578 | return ((char *)NULL); | |
579 | } | |
580 | ||
cce855bc | 581 | /* Return 1 if CHARACTER appears in an unquoted portion of |
7117c2d2 | 582 | STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */ |
cce855bc JA |
583 | static int |
584 | unquoted_member (character, string) | |
585 | int character; | |
726f6388 JA |
586 | char *string; |
587 | { | |
7117c2d2 | 588 | size_t slen; |
cce855bc | 589 | int sindex, c; |
7117c2d2 | 590 | DECLARE_MBSTATE; |
726f6388 | 591 | |
7117c2d2 JA |
592 | slen = strlen (string); |
593 | sindex = 0; | |
594 | while (c = string[sindex]) | |
726f6388 | 595 | { |
cce855bc JA |
596 | if (c == character) |
597 | return (1); | |
598 | ||
599 | switch (c) | |
ccc6cda3 | 600 | { |
cce855bc | 601 | default: |
7117c2d2 | 602 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
603 | break; |
604 | ||
605 | case '\\': | |
606 | sindex++; | |
607 | if (string[sindex]) | |
7117c2d2 | 608 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
609 | break; |
610 | ||
611 | case '\'': | |
7117c2d2 | 612 | sindex = skip_single_quoted (string, slen, ++sindex); |
cce855bc JA |
613 | break; |
614 | ||
615 | case '"': | |
7117c2d2 | 616 | sindex = skip_double_quoted (string, slen, ++sindex); |
cce855bc | 617 | break; |
ccc6cda3 | 618 | } |
726f6388 | 619 | } |
cce855bc | 620 | return (0); |
726f6388 JA |
621 | } |
622 | ||
cce855bc JA |
623 | /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */ |
624 | static int | |
625 | unquoted_substring (substr, string) | |
626 | char *substr, *string; | |
726f6388 | 627 | { |
7117c2d2 | 628 | size_t slen; |
cce855bc | 629 | int sindex, c, sublen; |
7117c2d2 | 630 | DECLARE_MBSTATE; |
726f6388 | 631 | |
cce855bc JA |
632 | if (substr == 0 || *substr == '\0') |
633 | return (0); | |
634 | ||
7117c2d2 | 635 | slen = strlen (string); |
cce855bc JA |
636 | sublen = strlen (substr); |
637 | for (sindex = 0; c = string[sindex]; ) | |
726f6388 | 638 | { |
cce855bc JA |
639 | if (STREQN (string + sindex, substr, sublen)) |
640 | return (1); | |
641 | ||
642 | switch (c) | |
643 | { | |
644 | case '\\': | |
645 | sindex++; | |
cce855bc | 646 | if (string[sindex]) |
7117c2d2 | 647 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
648 | break; |
649 | ||
650 | case '\'': | |
7117c2d2 | 651 | sindex = skip_single_quoted (string, slen, ++sindex); |
cce855bc JA |
652 | break; |
653 | ||
654 | case '"': | |
7117c2d2 | 655 | sindex = skip_double_quoted (string, slen, ++sindex); |
cce855bc JA |
656 | break; |
657 | ||
658 | default: | |
7117c2d2 | 659 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
660 | break; |
661 | } | |
726f6388 | 662 | } |
cce855bc | 663 | return (0); |
ccc6cda3 | 664 | } |
726f6388 | 665 | |
cce855bc JA |
666 | /* Most of the substitutions must be done in parallel. In order |
667 | to avoid using tons of unclear goto's, I have some functions | |
668 | for manipulating malloc'ed strings. They all take INDX, a | |
669 | pointer to an integer which is the offset into the string | |
670 | where manipulation is taking place. They also take SIZE, a | |
671 | pointer to an integer which is the current length of the | |
672 | character array for this string. */ | |
726f6388 | 673 | |
cce855bc JA |
674 | /* Append SOURCE to TARGET at INDEX. SIZE is the current amount |
675 | of space allocated to TARGET. SOURCE can be NULL, in which | |
676 | case nothing happens. Gets rid of SOURCE by freeing it. | |
677 | Returns TARGET in case the location has changed. */ | |
7117c2d2 | 678 | INLINE char * |
cce855bc JA |
679 | sub_append_string (source, target, indx, size) |
680 | char *source, *target; | |
681 | int *indx, *size; | |
682 | { | |
683 | if (source) | |
726f6388 | 684 | { |
cce855bc JA |
685 | int srclen, n; |
686 | ||
687 | srclen = STRLEN (source); | |
688 | if (srclen >= (int)(*size - *indx)) | |
726f6388 | 689 | { |
cce855bc JA |
690 | n = srclen + *indx; |
691 | n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE); | |
f73dda09 | 692 | target = (char *)xrealloc (target, (*size = n)); |
726f6388 | 693 | } |
cce855bc JA |
694 | |
695 | FASTCOPY (source, target + *indx, srclen); | |
696 | *indx += srclen; | |
697 | target[*indx] = '\0'; | |
698 | ||
699 | free (source); | |
726f6388 | 700 | } |
cce855bc JA |
701 | return (target); |
702 | } | |
703 | ||
704 | #if 0 | |
705 | /* UNUSED */ | |
706 | /* Append the textual representation of NUMBER to TARGET. | |
707 | INDX and SIZE are as in SUB_APPEND_STRING. */ | |
708 | char * | |
709 | sub_append_number (number, target, indx, size) | |
7117c2d2 | 710 | intmax_t number; |
f73dda09 | 711 | int *indx, *size; |
cce855bc JA |
712 | char *target; |
713 | { | |
714 | char *temp; | |
715 | ||
716 | temp = itos (number); | |
717 | return (sub_append_string (temp, target, indx, size)); | |
726f6388 | 718 | } |
d166f048 | 719 | #endif |
726f6388 JA |
720 | |
721 | /* Extract a substring from STRING, starting at SINDEX and ending with | |
722 | one of the characters in CHARLIST. Don't make the ending character | |
723 | part of the string. Leave SINDEX pointing at the ending character. | |
e6598ba4 | 724 | Understand about backslashes in the string. If (flags & SX_VARNAME) |
7117c2d2 JA |
725 | is non-zero, and array variables have been compiled into the shell, |
726 | everything between a `[' and a corresponding `]' is skipped over. | |
e6598ba4 CR |
727 | If (flags & SX_NOALLOC) is non-zero, don't return the substring, just |
728 | update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must | |
7027abcb | 729 | contain a closing character from CHARLIST. */ |
726f6388 | 730 | static char * |
7117c2d2 | 731 | string_extract (string, sindex, charlist, flags) |
f73dda09 JA |
732 | char *string; |
733 | int *sindex; | |
734 | char *charlist; | |
7117c2d2 | 735 | int flags; |
726f6388 | 736 | { |
ccc6cda3 | 737 | register int c, i; |
7027abcb | 738 | int found; |
7117c2d2 | 739 | size_t slen; |
726f6388 | 740 | char *temp; |
7117c2d2 | 741 | DECLARE_MBSTATE; |
726f6388 | 742 | |
da713c23 | 743 | slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0; |
7117c2d2 | 744 | i = *sindex; |
7027abcb | 745 | found = 0; |
7117c2d2 | 746 | while (c = string[i]) |
726f6388 JA |
747 | { |
748 | if (c == '\\') | |
7117c2d2 JA |
749 | { |
750 | if (string[i + 1]) | |
751 | i++; | |
752 | else | |
753 | break; | |
754 | } | |
ccc6cda3 | 755 | #if defined (ARRAY_VARS) |
e6598ba4 | 756 | else if ((flags & SX_VARNAME) && c == '[') |
ccc6cda3 JA |
757 | { |
758 | int ni; | |
759 | /* If this is an array subscript, skip over it and continue. */ | |
3eb2d94a | 760 | ni = skipsubscript (string, i, 0); |
ccc6cda3 JA |
761 | if (string[ni] == ']') |
762 | i = ni; | |
763 | } | |
764 | #endif | |
765 | else if (MEMBER (c, charlist)) | |
7027abcb CR |
766 | { |
767 | found = 1; | |
726f6388 | 768 | break; |
7027abcb | 769 | } |
7117c2d2 JA |
770 | |
771 | ADVANCE_CHAR (string, slen, i); | |
726f6388 | 772 | } |
bb70624e | 773 | |
7027abcb CR |
774 | /* If we had to have a matching delimiter and didn't find one, return an |
775 | error and let the caller deal with it. */ | |
e6598ba4 | 776 | if ((flags & SX_REQMATCH) && found == 0) |
7027abcb CR |
777 | { |
778 | *sindex = i; | |
779 | return (&extract_string_error); | |
780 | } | |
781 | ||
e6598ba4 | 782 | temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i); |
726f6388 | 783 | *sindex = i; |
7027abcb | 784 | |
726f6388 JA |
785 | return (temp); |
786 | } | |
787 | ||
ccc6cda3 JA |
788 | /* Extract the contents of STRING as if it is enclosed in double quotes. |
789 | SINDEX, when passed in, is the offset of the character immediately | |
790 | following the opening double quote; on exit, SINDEX is left pointing after | |
791 | the closing double quote. If STRIPDQ is non-zero, unquoted double | |
792 | quotes are stripped and the string is terminated by a null byte. | |
793 | Backslashes between the embedded double quotes are processed. If STRIPDQ | |
794 | is zero, an unquoted `"' terminates the string. */ | |
7117c2d2 | 795 | static char * |
ccc6cda3 | 796 | string_extract_double_quoted (string, sindex, stripdq) |
726f6388 | 797 | char *string; |
ccc6cda3 | 798 | int *sindex, stripdq; |
726f6388 | 799 | { |
7117c2d2 JA |
800 | size_t slen; |
801 | char *send; | |
f73dda09 JA |
802 | int j, i, t; |
803 | unsigned char c; | |
ccc6cda3 JA |
804 | char *temp, *ret; /* The new string we return. */ |
805 | int pass_next, backquote, si; /* State variables for the machine. */ | |
806 | int dquote; | |
7117c2d2 JA |
807 | DECLARE_MBSTATE; |
808 | ||
809 | slen = strlen (string + *sindex) + *sindex; | |
810 | send = string + slen; | |
726f6388 | 811 | |
ccc6cda3 | 812 | pass_next = backquote = dquote = 0; |
7117c2d2 | 813 | temp = (char *)xmalloc (1 + slen - *sindex); |
726f6388 | 814 | |
7117c2d2 JA |
815 | j = 0; |
816 | i = *sindex; | |
817 | while (c = string[i]) | |
726f6388 | 818 | { |
ccc6cda3 JA |
819 | /* Process a character that was quoted by a backslash. */ |
820 | if (pass_next) | |
726f6388 | 821 | { |
9e51a74d | 822 | /* XXX - take another look at this in light of Interp 221 */ |
ccc6cda3 | 823 | /* Posix.2 sez: |
726f6388 | 824 | |
ccc6cda3 JA |
825 | ``The backslash shall retain its special meaning as an escape |
826 | character only when followed by one of the characters: | |
7117c2d2 | 827 | $ ` " \ <newline>''. |
726f6388 | 828 | |
ccc6cda3 JA |
829 | If STRIPDQ is zero, we handle the double quotes here and let |
830 | expand_word_internal handle the rest. If STRIPDQ is non-zero, | |
831 | we have already been through one round of backslash stripping, | |
832 | and want to strip these backslashes only if DQUOTE is non-zero, | |
833 | indicating that we are inside an embedded double-quoted string. */ | |
834 | ||
835 | /* If we are in an embedded quoted string, then don't strip | |
836 | backslashes before characters for which the backslash | |
837 | retains its special meaning, but remove backslashes in | |
838 | front of other characters. If we are not in an | |
839 | embedded quoted string, don't strip backslashes at all. | |
840 | This mess is necessary because the string was already | |
841 | surrounded by double quotes (and sh has some really weird | |
842 | quoting rules). | |
843 | The returned string will be run through expansion as if | |
844 | it were double-quoted. */ | |
845 | if ((stripdq == 0 && c != '"') || | |
28ef6c31 | 846 | (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0))) |
ccc6cda3 | 847 | temp[j++] = '\\'; |
ccc6cda3 | 848 | pass_next = 0; |
7117c2d2 JA |
849 | |
850 | add_one_character: | |
851 | COPY_CHAR_I (temp, j, string, send, i); | |
ccc6cda3 JA |
852 | continue; |
853 | } | |
726f6388 | 854 | |
ccc6cda3 JA |
855 | /* A backslash protects the next character. The code just above |
856 | handles preserving the backslash in front of any character but | |
857 | a double quote. */ | |
858 | if (c == '\\') | |
726f6388 | 859 | { |
ccc6cda3 | 860 | pass_next++; |
7117c2d2 | 861 | i++; |
726f6388 JA |
862 | continue; |
863 | } | |
864 | ||
ccc6cda3 JA |
865 | /* Inside backquotes, ``the portion of the quoted string from the |
866 | initial backquote and the characters up to the next backquote | |
867 | that is not preceded by a backslash, having escape characters | |
868 | removed, defines that command''. */ | |
869 | if (backquote) | |
726f6388 | 870 | { |
ccc6cda3 JA |
871 | if (c == '`') |
872 | backquote = 0; | |
873 | temp[j++] = c; | |
7117c2d2 | 874 | i++; |
726f6388 JA |
875 | continue; |
876 | } | |
877 | ||
ccc6cda3 | 878 | if (c == '`') |
726f6388 | 879 | { |
ccc6cda3 JA |
880 | temp[j++] = c; |
881 | backquote++; | |
7117c2d2 | 882 | i++; |
ccc6cda3 | 883 | continue; |
726f6388 JA |
884 | } |
885 | ||
ccc6cda3 JA |
886 | /* Pass everything between `$(' and the matching `)' or a quoted |
887 | ${ ... } pair through according to the Posix.2 specification. */ | |
cce855bc | 888 | if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE))) |
726f6388 | 889 | { |
61deeb13 CR |
890 | int free_ret = 1; |
891 | ||
ccc6cda3 | 892 | si = i + 2; |
cce855bc | 893 | if (string[i + 1] == LPAREN) |
e33f2203 | 894 | ret = extract_command_subst (string, &si, 0); |
ccc6cda3 | 895 | else |
64419627 | 896 | ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, 0); |
726f6388 | 897 | |
ccc6cda3 JA |
898 | temp[j++] = '$'; |
899 | temp[j++] = string[i + 1]; | |
726f6388 | 900 | |
61deeb13 CR |
901 | /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error |
902 | is set. */ | |
903 | if (ret == 0 && no_longjmp_on_fatal_error) | |
904 | { | |
905 | free_ret = 0; | |
906 | ret = string + i + 2; | |
907 | } | |
908 | ||
ccc6cda3 JA |
909 | for (t = 0; ret[t]; t++, j++) |
910 | temp[j] = ret[t]; | |
56299fa5 CR |
911 | temp[j] = string[si]; |
912 | ||
913 | if (string[si]) | |
914 | { | |
915 | j++; | |
916 | i = si + 1; | |
917 | } | |
918 | else | |
919 | i = si; | |
726f6388 | 920 | |
61deeb13 CR |
921 | if (free_ret) |
922 | free (ret); | |
ccc6cda3 | 923 | continue; |
726f6388 JA |
924 | } |
925 | ||
ccc6cda3 | 926 | /* Add any character but a double quote to the quoted string we're |
28ef6c31 | 927 | accumulating. */ |
ccc6cda3 | 928 | if (c != '"') |
7117c2d2 | 929 | goto add_one_character; |
ccc6cda3 JA |
930 | |
931 | /* c == '"' */ | |
932 | if (stripdq) | |
726f6388 | 933 | { |
ccc6cda3 | 934 | dquote ^= 1; |
7117c2d2 | 935 | i++; |
ccc6cda3 | 936 | continue; |
726f6388 | 937 | } |
ccc6cda3 JA |
938 | |
939 | break; | |
726f6388 | 940 | } |
ccc6cda3 | 941 | temp[j] = '\0'; |
726f6388 | 942 | |
ccc6cda3 JA |
943 | /* Point to after the closing quote. */ |
944 | if (c) | |
945 | i++; | |
726f6388 JA |
946 | *sindex = i; |
947 | ||
ccc6cda3 JA |
948 | return (temp); |
949 | } | |
950 | ||
951 | /* This should really be another option to string_extract_double_quoted. */ | |
f73dda09 | 952 | static int |
7117c2d2 | 953 | skip_double_quoted (string, slen, sind) |
ccc6cda3 | 954 | char *string; |
7117c2d2 | 955 | size_t slen; |
ccc6cda3 JA |
956 | int sind; |
957 | { | |
f73dda09 | 958 | int c, i; |
ccc6cda3 JA |
959 | char *ret; |
960 | int pass_next, backquote, si; | |
7117c2d2 | 961 | DECLARE_MBSTATE; |
ccc6cda3 JA |
962 | |
963 | pass_next = backquote = 0; | |
7117c2d2 JA |
964 | i = sind; |
965 | while (c = string[i]) | |
726f6388 | 966 | { |
ccc6cda3 JA |
967 | if (pass_next) |
968 | { | |
969 | pass_next = 0; | |
7117c2d2 | 970 | ADVANCE_CHAR (string, slen, i); |
ccc6cda3 JA |
971 | continue; |
972 | } | |
973 | else if (c == '\\') | |
974 | { | |
975 | pass_next++; | |
7117c2d2 | 976 | i++; |
ccc6cda3 JA |
977 | continue; |
978 | } | |
979 | else if (backquote) | |
980 | { | |
981 | if (c == '`') | |
982 | backquote = 0; | |
7117c2d2 | 983 | ADVANCE_CHAR (string, slen, i); |
ccc6cda3 JA |
984 | continue; |
985 | } | |
986 | else if (c == '`') | |
987 | { | |
988 | backquote++; | |
7117c2d2 | 989 | i++; |
ccc6cda3 JA |
990 | continue; |
991 | } | |
cce855bc | 992 | else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE))) |
ccc6cda3 JA |
993 | { |
994 | si = i + 2; | |
cce855bc | 995 | if (string[i + 1] == LPAREN) |
e33f2203 | 996 | ret = extract_command_subst (string, &si, SX_NOALLOC); |
ccc6cda3 | 997 | else |
9e51a74d | 998 | ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, SX_NOALLOC); |
ccc6cda3 | 999 | |
7117c2d2 | 1000 | i = si + 1; |
ccc6cda3 JA |
1001 | continue; |
1002 | } | |
1003 | else if (c != '"') | |
7117c2d2 JA |
1004 | { |
1005 | ADVANCE_CHAR (string, slen, i); | |
1006 | continue; | |
1007 | } | |
ccc6cda3 JA |
1008 | else |
1009 | break; | |
726f6388 | 1010 | } |
ccc6cda3 JA |
1011 | |
1012 | if (c) | |
1013 | i++; | |
1014 | ||
1015 | return (i); | |
726f6388 JA |
1016 | } |
1017 | ||
ccc6cda3 JA |
1018 | /* Extract the contents of STRING as if it is enclosed in single quotes. |
1019 | SINDEX, when passed in, is the offset of the character immediately | |
1020 | following the opening single quote; on exit, SINDEX is left pointing after | |
1021 | the closing single quote. */ | |
1022 | static inline char * | |
1023 | string_extract_single_quoted (string, sindex) | |
1024 | char *string; | |
1025 | int *sindex; | |
1026 | { | |
f73dda09 | 1027 | register int i; |
7117c2d2 | 1028 | size_t slen; |
ccc6cda3 | 1029 | char *t; |
7117c2d2 | 1030 | DECLARE_MBSTATE; |
ccc6cda3 | 1031 | |
da713c23 CR |
1032 | /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */ |
1033 | slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0; | |
7117c2d2 JA |
1034 | i = *sindex; |
1035 | while (string[i] && string[i] != '\'') | |
1036 | ADVANCE_CHAR (string, slen, i); | |
ccc6cda3 | 1037 | |
bb70624e | 1038 | t = substring (string, *sindex, i); |
ccc6cda3 JA |
1039 | |
1040 | if (string[i]) | |
1041 | i++; | |
1042 | *sindex = i; | |
1043 | ||
1044 | return (t); | |
1045 | } | |
1046 | ||
1047 | static inline int | |
7117c2d2 | 1048 | skip_single_quoted (string, slen, sind) |
d3ad40de | 1049 | const char *string; |
7117c2d2 | 1050 | size_t slen; |
ccc6cda3 JA |
1051 | int sind; |
1052 | { | |
28ef6c31 | 1053 | register int c; |
7117c2d2 JA |
1054 | DECLARE_MBSTATE; |
1055 | ||
1056 | c = sind; | |
1057 | while (string[c] && string[c] != '\'') | |
1058 | ADVANCE_CHAR (string, slen, c); | |
ccc6cda3 | 1059 | |
28ef6c31 JA |
1060 | if (string[c]) |
1061 | c++; | |
1062 | return c; | |
ccc6cda3 JA |
1063 | } |
1064 | ||
1065 | /* Just like string_extract, but doesn't hack backslashes or any of | |
bb70624e | 1066 | that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */ |
726f6388 | 1067 | static char * |
d3ad40de | 1068 | string_extract_verbatim (string, slen, sindex, charlist, flags) |
f73dda09 | 1069 | char *string; |
da713c23 | 1070 | size_t slen; |
ccc6cda3 | 1071 | int *sindex; |
f73dda09 | 1072 | char *charlist; |
d3ad40de | 1073 | int flags; |
ccc6cda3 | 1074 | { |
824dfe68 | 1075 | register int i; |
633e5c6d CR |
1076 | #if defined (HANDLE_MULTIBYTE) |
1077 | size_t clen; | |
1078 | wchar_t *wcharlist; | |
1079 | #endif | |
ccc6cda3 JA |
1080 | int c; |
1081 | char *temp; | |
633e5c6d | 1082 | DECLARE_MBSTATE; |
ccc6cda3 JA |
1083 | |
1084 | if (charlist[0] == '\'' && charlist[1] == '\0') | |
1085 | { | |
1086 | temp = string_extract_single_quoted (string, sindex); | |
1087 | --*sindex; /* leave *sindex at separator character */ | |
1088 | return temp; | |
1089 | } | |
1090 | ||
633e5c6d | 1091 | i = *sindex; |
da713c23 CR |
1092 | #if 0 |
1093 | /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need | |
1094 | this only if MB_CUR_MAX > 1. */ | |
1095 | slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 1; | |
1096 | #endif | |
633e5c6d CR |
1097 | #if defined (HANDLE_MULTIBYTE) |
1098 | clen = strlen (charlist); | |
1099 | wcharlist = 0; | |
1100 | #endif | |
1101 | while (c = string[i]) | |
ccc6cda3 | 1102 | { |
633e5c6d CR |
1103 | #if defined (HANDLE_MULTIBYTE) |
1104 | size_t mblength; | |
1105 | #endif | |
e6598ba4 CR |
1106 | if ((flags & SX_NOCTLESC) == 0 && c == CTLESC) |
1107 | { | |
1108 | i += 2; | |
1109 | continue; | |
1110 | } | |
1111 | /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL | |
1112 | through, to protect the CTLNULs from later calls to | |
1113 | remove_quoted_nulls. */ | |
1114 | else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC && string[i+1] == CTLNUL) | |
ccc6cda3 | 1115 | { |
633e5c6d | 1116 | i += 2; |
ccc6cda3 JA |
1117 | continue; |
1118 | } | |
e33f2203 | 1119 | |
633e5c6d | 1120 | #if defined (HANDLE_MULTIBYTE) |
da713c23 | 1121 | mblength = MBLEN (string + i, slen - i); |
633e5c6d CR |
1122 | if (mblength > 1) |
1123 | { | |
1124 | wchar_t wc; | |
1125 | mblength = mbtowc (&wc, string + i, slen - i); | |
1126 | if (MB_INVALIDCH (mblength)) | |
1127 | { | |
1128 | if (MEMBER (c, charlist)) | |
1129 | break; | |
1130 | } | |
1131 | else | |
1132 | { | |
1133 | if (wcharlist == 0) | |
1134 | { | |
1135 | size_t len; | |
1136 | len = mbstowcs (wcharlist, charlist, 0); | |
1137 | if (len == -1) | |
1138 | len = 0; | |
d3ad40de CR |
1139 | wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1)); |
1140 | mbstowcs (wcharlist, charlist, len + 1); | |
633e5c6d CR |
1141 | } |
1142 | ||
1143 | if (wcschr (wcharlist, wc)) | |
1144 | break; | |
1145 | } | |
1146 | } | |
1147 | else | |
1148 | #endif | |
ccc6cda3 JA |
1149 | if (MEMBER (c, charlist)) |
1150 | break; | |
633e5c6d CR |
1151 | |
1152 | ADVANCE_CHAR (string, slen, i); | |
ccc6cda3 JA |
1153 | } |
1154 | ||
633e5c6d CR |
1155 | #if defined (HANDLE_MULTIBYTE) |
1156 | FREE (wcharlist); | |
1157 | #endif | |
1158 | ||
bb70624e | 1159 | temp = substring (string, *sindex, i); |
ccc6cda3 JA |
1160 | *sindex = i; |
1161 | ||
1162 | return (temp); | |
1163 | } | |
1164 | ||
1165 | /* Extract the $( construct in STRING, and return a new string. | |
1166 | Start extracting at (SINDEX) as if we had just seen "$(". | |
e33f2203 | 1167 | Make (SINDEX) get the position of the matching ")". ) |
ecf57862 | 1168 | XFLAGS is additional flags to pass to other extraction functions. */ |
ccc6cda3 | 1169 | char * |
e33f2203 | 1170 | extract_command_subst (string, sindex, xflags) |
726f6388 JA |
1171 | char *string; |
1172 | int *sindex; | |
e33f2203 | 1173 | int xflags; |
726f6388 | 1174 | { |
176b12ee | 1175 | if (string[*sindex] == LPAREN) |
e33f2203 CR |
1176 | return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/ |
1177 | else | |
1178 | { | |
1179 | xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0); | |
1180 | return (xparse_dolparen (string, string+*sindex, sindex, xflags)); | |
1181 | } | |
ccc6cda3 JA |
1182 | } |
1183 | ||
28ef6c31 | 1184 | /* Extract the $[ construct in STRING, and return a new string. (]) |
ccc6cda3 JA |
1185 | Start extracting at (SINDEX) as if we had just seen "$[". |
1186 | Make (SINDEX) get the position of the matching "]". */ | |
1187 | char * | |
1188 | extract_arithmetic_subst (string, sindex) | |
1189 | char *string; | |
1190 | int *sindex; | |
1191 | { | |
7117c2d2 | 1192 | return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/ |
ccc6cda3 JA |
1193 | } |
1194 | ||
1195 | #if defined (PROCESS_SUBSTITUTION) | |
1196 | /* Extract the <( or >( construct in STRING, and return a new string. | |
1197 | Start extracting at (SINDEX) as if we had just seen "<(". | |
cce855bc | 1198 | Make (SINDEX) get the position of the matching ")". */ /*))*/ |
ccc6cda3 | 1199 | char * |
fbbc416f | 1200 | extract_process_subst (string, starter, sindex, xflags) |
ccc6cda3 JA |
1201 | char *string; |
1202 | char *starter; | |
1203 | int *sindex; | |
fbbc416f | 1204 | int xflags; |
ccc6cda3 | 1205 | { |
fbbc416f | 1206 | #if 0 |
f4f5e1c2 | 1207 | return (extract_delimited_string (string, sindex, starter, "(", ")", SX_COMMAND)); |
fbbc416f CR |
1208 | #else |
1209 | xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0); | |
1210 | return (xparse_dolparen (string, string+*sindex, sindex, xflags)); | |
1211 | #endif | |
ccc6cda3 JA |
1212 | } |
1213 | #endif /* PROCESS_SUBSTITUTION */ | |
1214 | ||
1215 | #if defined (ARRAY_VARS) | |
43df7bbb CR |
1216 | /* This can be fooled by unquoted right parens in the passed string. If |
1217 | each caller verifies that the last character in STRING is a right paren, | |
1218 | we don't even need to call extract_delimited_string. */ | |
ccc6cda3 JA |
1219 | char * |
1220 | extract_array_assignment_list (string, sindex) | |
1221 | char *string; | |
1222 | int *sindex; | |
1223 | { | |
43df7bbb CR |
1224 | int slen; |
1225 | char *ret; | |
1226 | ||
1227 | slen = strlen (string); /* ( */ | |
1228 | if (string[slen - 1] == ')') | |
1229 | { | |
1230 | ret = substring (string, *sindex, slen - 1); | |
1231 | *sindex = slen - 1; | |
1232 | return ret; | |
1233 | } | |
e225d5a9 | 1234 | return 0; |
ccc6cda3 JA |
1235 | } |
1236 | #endif | |
1237 | ||
1238 | /* Extract and create a new string from the contents of STRING, a | |
1239 | character string delimited with OPENER and CLOSER. SINDEX is | |
1240 | the address of an int describing the current offset in STRING; | |
1241 | it should point to just after the first OPENER found. On exit, | |
1242 | SINDEX gets the position of the last character of the matching CLOSER. | |
1243 | If OPENER is more than a single character, ALT_OPENER, if non-null, | |
1244 | contains a character string that can also match CLOSER and thus | |
1245 | needs to be skipped. */ | |
1246 | static char * | |
7117c2d2 | 1247 | extract_delimited_string (string, sindex, opener, alt_opener, closer, flags) |
ccc6cda3 JA |
1248 | char *string; |
1249 | int *sindex; | |
1250 | char *opener, *alt_opener, *closer; | |
7117c2d2 | 1251 | int flags; |
ccc6cda3 JA |
1252 | { |
1253 | int i, c, si; | |
7117c2d2 | 1254 | size_t slen; |
ccc6cda3 | 1255 | char *t, *result; |
d3ad40de | 1256 | int pass_character, nesting_level, in_comment; |
ccc6cda3 | 1257 | int len_closer, len_opener, len_alt_opener; |
7117c2d2 | 1258 | DECLARE_MBSTATE; |
ccc6cda3 | 1259 | |
7117c2d2 | 1260 | slen = strlen (string + *sindex) + *sindex; |
ccc6cda3 JA |
1261 | len_opener = STRLEN (opener); |
1262 | len_alt_opener = STRLEN (alt_opener); | |
1263 | len_closer = STRLEN (closer); | |
726f6388 | 1264 | |
d3ad40de | 1265 | pass_character = in_comment = 0; |
726f6388 JA |
1266 | |
1267 | nesting_level = 1; | |
ccc6cda3 | 1268 | i = *sindex; |
726f6388 | 1269 | |
ccc6cda3 | 1270 | while (nesting_level) |
726f6388 | 1271 | { |
ccc6cda3 JA |
1272 | c = string[i]; |
1273 | ||
1274 | if (c == 0) | |
28ef6c31 | 1275 | break; |
ccc6cda3 | 1276 | |
d3ad40de CR |
1277 | if (in_comment) |
1278 | { | |
1279 | if (c == '\n') | |
1280 | in_comment = 0; | |
1281 | ADVANCE_CHAR (string, slen, i); | |
1282 | continue; | |
1283 | } | |
1284 | ||
ccc6cda3 | 1285 | if (pass_character) /* previous char was backslash */ |
726f6388 JA |
1286 | { |
1287 | pass_character = 0; | |
7117c2d2 | 1288 | ADVANCE_CHAR (string, slen, i); |
726f6388 JA |
1289 | continue; |
1290 | } | |
1291 | ||
d3ad40de | 1292 | /* Not exactly right yet; should handle shell metacharacters and |
602bb739 | 1293 | multibyte characters, too. See COMMENT_BEGIN define in parse.y */ |
e33f2203 | 1294 | if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1]))) |
d3ad40de CR |
1295 | { |
1296 | in_comment = 1; | |
1297 | ADVANCE_CHAR (string, slen, i); | |
1298 | continue; | |
1299 | } | |
1300 | ||
7117c2d2 | 1301 | if (c == CTLESC || c == '\\') |
726f6388 | 1302 | { |
ccc6cda3 JA |
1303 | pass_character++; |
1304 | i++; | |
1305 | continue; | |
726f6388 JA |
1306 | } |
1307 | ||
6faad625 CR |
1308 | /* Process a nested command substitution, but only if we're parsing an |
1309 | arithmetic substitution. */ | |
ecf57862 CR |
1310 | if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN) |
1311 | { | |
1312 | si = i + 2; | |
3d8cce26 | 1313 | t = extract_command_subst (string, &si, flags|SX_NOALLOC); |
ecf57862 CR |
1314 | i = si + 1; |
1315 | continue; | |
1316 | } | |
ecf57862 | 1317 | |
ccc6cda3 JA |
1318 | /* Process a nested OPENER. */ |
1319 | if (STREQN (string + i, opener, len_opener)) | |
726f6388 | 1320 | { |
ccc6cda3 | 1321 | si = i + len_opener; |
e6598ba4 | 1322 | t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC); |
ccc6cda3 | 1323 | i = si + 1; |
ccc6cda3 | 1324 | continue; |
726f6388 JA |
1325 | } |
1326 | ||
ccc6cda3 JA |
1327 | /* Process a nested ALT_OPENER */ |
1328 | if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener)) | |
726f6388 | 1329 | { |
ccc6cda3 | 1330 | si = i + len_alt_opener; |
e6598ba4 | 1331 | t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC); |
ccc6cda3 | 1332 | i = si + 1; |
726f6388 JA |
1333 | continue; |
1334 | } | |
ccc6cda3 JA |
1335 | |
1336 | /* If the current substring terminates the delimited string, decrement | |
1337 | the nesting level. */ | |
1338 | if (STREQN (string + i, closer, len_closer)) | |
726f6388 | 1339 | { |
7117c2d2 | 1340 | i += len_closer - 1; /* move to last byte of the closer */ |
ccc6cda3 JA |
1341 | nesting_level--; |
1342 | if (nesting_level == 0) | |
1343 | break; | |
726f6388 | 1344 | } |
ccc6cda3 JA |
1345 | |
1346 | /* Pass old-style command substitution through verbatim. */ | |
1347 | if (c == '`') | |
28ef6c31 JA |
1348 | { |
1349 | si = i + 1; | |
e6598ba4 | 1350 | t = string_extract (string, &si, "`", flags|SX_NOALLOC); |
28ef6c31 | 1351 | i = si + 1; |
28ef6c31 JA |
1352 | continue; |
1353 | } | |
ccc6cda3 | 1354 | |
7117c2d2 JA |
1355 | /* Pass single-quoted and double-quoted strings through verbatim. */ |
1356 | if (c == '\'' || c == '"') | |
28ef6c31 JA |
1357 | { |
1358 | si = i + 1; | |
7117c2d2 JA |
1359 | i = (c == '\'') ? skip_single_quoted (string, slen, si) |
1360 | : skip_double_quoted (string, slen, si); | |
28ef6c31 JA |
1361 | continue; |
1362 | } | |
ccc6cda3 | 1363 | |
7117c2d2 JA |
1364 | /* move past this character, which was not special. */ |
1365 | ADVANCE_CHAR (string, slen, i); | |
726f6388 JA |
1366 | } |
1367 | ||
d3a24ed2 | 1368 | if (c == 0 && nesting_level) |
726f6388 | 1369 | { |
d3a24ed2 CR |
1370 | if (no_longjmp_on_fatal_error == 0) |
1371 | { | |
d3a24ed2 | 1372 | last_command_exit_value = EXECUTION_FAILURE; |
7f947b68 | 1373 | report_error (_("bad substitution: no closing `%s' in %s"), closer, string); |
d3a24ed2 CR |
1374 | exp_jump_to_top_level (DISCARD); |
1375 | } | |
1376 | else | |
1377 | { | |
1378 | *sindex = i; | |
1379 | return (char *)NULL; | |
1380 | } | |
726f6388 | 1381 | } |
ccc6cda3 | 1382 | |
cce855bc | 1383 | si = i - *sindex - len_closer + 1; |
e6598ba4 | 1384 | if (flags & SX_NOALLOC) |
7117c2d2 JA |
1385 | result = (char *)NULL; |
1386 | else | |
1387 | { | |
1388 | result = (char *)xmalloc (1 + si); | |
1389 | strncpy (result, string + *sindex, si); | |
1390 | result[si] = '\0'; | |
1391 | } | |
cce855bc JA |
1392 | *sindex = i; |
1393 | ||
726f6388 JA |
1394 | return (result); |
1395 | } | |
1396 | ||
ccc6cda3 JA |
1397 | /* Extract a parameter expansion expression within ${ and } from STRING. |
1398 | Obey the Posix.2 rules for finding the ending `}': count braces while | |
1399 | skipping over enclosed quoted strings and command substitutions. | |
1400 | SINDEX is the address of an int describing the current offset in STRING; | |
1401 | it should point to just after the first `{' found. On exit, SINDEX | |
1402 | gets the position of the matching `}'. QUOTED is non-zero if this | |
1403 | occurs inside double quotes. */ | |
1404 | /* XXX -- this is very similar to extract_delimited_string -- XXX */ | |
726f6388 | 1405 | static char * |
7117c2d2 | 1406 | extract_dollar_brace_string (string, sindex, quoted, flags) |
726f6388 | 1407 | char *string; |
7117c2d2 | 1408 | int *sindex, quoted, flags; |
726f6388 | 1409 | { |
f73dda09 | 1410 | register int i, c; |
7117c2d2 | 1411 | size_t slen; |
9e51a74d | 1412 | int pass_character, nesting_level, si, dolbrace_state; |
ccc6cda3 | 1413 | char *result, *t; |
7117c2d2 | 1414 | DECLARE_MBSTATE; |
726f6388 | 1415 | |
ccc6cda3 | 1416 | pass_character = 0; |
ccc6cda3 | 1417 | nesting_level = 1; |
7117c2d2 | 1418 | slen = strlen (string + *sindex) + *sindex; |
ccc6cda3 | 1419 | |
9e51a74d | 1420 | /* The handling of dolbrace_state needs to agree with the code in parse.y: |
e192f341 CR |
1421 | parse_matched_pair(). The different initial value is to handle the |
1422 | case where this function is called to parse the word in | |
1423 | ${param op word} (SX_WORD). */ | |
1424 | dolbrace_state = (flags & SX_WORD) ? DOLBRACE_WORD : DOLBRACE_PARAM; | |
1425 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && (flags & SX_POSIXEXP)) | |
1426 | dolbrace_state = DOLBRACE_QUOTE; | |
9e51a74d | 1427 | |
7117c2d2 JA |
1428 | i = *sindex; |
1429 | while (c = string[i]) | |
726f6388 | 1430 | { |
ccc6cda3 | 1431 | if (pass_character) |
726f6388 | 1432 | { |
ccc6cda3 | 1433 | pass_character = 0; |
7117c2d2 | 1434 | ADVANCE_CHAR (string, slen, i); |
ccc6cda3 JA |
1435 | continue; |
1436 | } | |
726f6388 | 1437 | |
cce855bc JA |
1438 | /* CTLESCs and backslashes quote the next character. */ |
1439 | if (c == CTLESC || c == '\\') | |
726f6388 | 1440 | { |
ccc6cda3 | 1441 | pass_character++; |
7117c2d2 | 1442 | i++; |
726f6388 JA |
1443 | continue; |
1444 | } | |
1445 | ||
cce855bc | 1446 | if (string[i] == '$' && string[i+1] == LBRACE) |
726f6388 | 1447 | { |
ccc6cda3 | 1448 | nesting_level++; |
7117c2d2 | 1449 | i += 2; |
726f6388 JA |
1450 | continue; |
1451 | } | |
1452 | ||
cce855bc | 1453 | if (c == RBRACE) |
726f6388 | 1454 | { |
ccc6cda3 JA |
1455 | nesting_level--; |
1456 | if (nesting_level == 0) | |
1457 | break; | |
7117c2d2 | 1458 | i++; |
726f6388 JA |
1459 | continue; |
1460 | } | |
1461 | ||
ccc6cda3 JA |
1462 | /* Pass the contents of old-style command substitutions through |
1463 | verbatim. */ | |
1464 | if (c == '`') | |
726f6388 | 1465 | { |
ccc6cda3 | 1466 | si = i + 1; |
e6598ba4 | 1467 | t = string_extract (string, &si, "`", flags|SX_NOALLOC); |
7117c2d2 | 1468 | i = si + 1; |
ccc6cda3 JA |
1469 | continue; |
1470 | } | |
726f6388 | 1471 | |
cce855bc JA |
1472 | /* Pass the contents of new-style command substitutions and |
1473 | arithmetic substitutions through verbatim. */ | |
1474 | if (string[i] == '$' && string[i+1] == LPAREN) | |
ccc6cda3 | 1475 | { |
726f6388 | 1476 | si = i + 2; |
e33f2203 | 1477 | t = extract_command_subst (string, &si, flags|SX_NOALLOC); |
7117c2d2 | 1478 | i = si + 1; |
726f6388 JA |
1479 | continue; |
1480 | } | |
1481 | ||
9e51a74d CR |
1482 | /* Pass the contents of double-quoted strings through verbatim. */ |
1483 | if (c == '"') | |
1484 | { | |
1485 | si = i + 1; | |
1486 | i = skip_double_quoted (string, slen, si); | |
1487 | /* skip_XXX_quoted leaves index one past close quote */ | |
1488 | continue; | |
1489 | } | |
1490 | ||
1491 | if (c == '\'') | |
1492 | { | |
1493 | /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/ | |
21af69d5 | 1494 | if (posixly_correct && shell_compatibility_level > 42 && dolbrace_state != DOLBRACE_QUOTE && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
9e51a74d CR |
1495 | ADVANCE_CHAR (string, slen, i); |
1496 | else | |
1497 | { | |
1498 | si = i + 1; | |
1499 | i = skip_single_quoted (string, slen, si); | |
1500 | } | |
1501 | ||
1502 | continue; | |
1503 | } | |
7117c2d2 JA |
1504 | |
1505 | /* move past this character, which was not special. */ | |
1506 | ADVANCE_CHAR (string, slen, i); | |
9e51a74d CR |
1507 | |
1508 | /* This logic must agree with parse.y:parse_matched_pair, since they | |
1509 | share the same defines. */ | |
3d35553a | 1510 | if (dolbrace_state == DOLBRACE_PARAM && c == '%' && (i - *sindex) > 1) |
9e51a74d CR |
1511 | dolbrace_state = DOLBRACE_QUOTE; |
1512 | else if (dolbrace_state == DOLBRACE_PARAM && c == '#' && (i - *sindex) > 1) | |
1513 | dolbrace_state = DOLBRACE_QUOTE; | |
3d35553a | 1514 | else if (dolbrace_state == DOLBRACE_PARAM && c == '/' && (i - *sindex) > 1) |
21af69d5 | 1515 | dolbrace_state = DOLBRACE_QUOTE2; /* XXX */ |
3d35553a CR |
1516 | else if (dolbrace_state == DOLBRACE_PARAM && c == '^' && (i - *sindex) > 1) |
1517 | dolbrace_state = DOLBRACE_QUOTE; | |
1518 | else if (dolbrace_state == DOLBRACE_PARAM && c == ',' && (i - *sindex) > 1) | |
1519 | dolbrace_state = DOLBRACE_QUOTE; | |
9e51a74d CR |
1520 | else if (dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", c) != 0) |
1521 | dolbrace_state = DOLBRACE_OP; | |
1522 | else if (dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", c) == 0) | |
1523 | dolbrace_state = DOLBRACE_WORD; | |
cce855bc | 1524 | } |
726f6388 | 1525 | |
d3a24ed2 | 1526 | if (c == 0 && nesting_level) |
cce855bc | 1527 | { |
d3a24ed2 CR |
1528 | if (no_longjmp_on_fatal_error == 0) |
1529 | { /* { */ | |
d3a24ed2 | 1530 | last_command_exit_value = EXECUTION_FAILURE; |
7f947b68 | 1531 | report_error (_("bad substitution: no closing `%s' in %s"), "}", string); |
d3a24ed2 CR |
1532 | exp_jump_to_top_level (DISCARD); |
1533 | } | |
1534 | else | |
1535 | { | |
1536 | *sindex = i; | |
1537 | return ((char *)NULL); | |
1538 | } | |
726f6388 | 1539 | } |
726f6388 | 1540 | |
e6598ba4 | 1541 | result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i); |
726f6388 JA |
1542 | *sindex = i; |
1543 | ||
ccc6cda3 | 1544 | return (result); |
726f6388 JA |
1545 | } |
1546 | ||
ccc6cda3 JA |
1547 | /* Remove backslashes which are quoting backquotes from STRING. Modifies |
1548 | STRING, and returns a pointer to it. */ | |
1549 | char * | |
1550 | de_backslash (string) | |
726f6388 | 1551 | char *string; |
ccc6cda3 | 1552 | { |
7117c2d2 JA |
1553 | register size_t slen; |
1554 | register int i, j, prev_i; | |
1555 | DECLARE_MBSTATE; | |
726f6388 | 1556 | |
7117c2d2 JA |
1557 | slen = strlen (string); |
1558 | i = j = 0; | |
1559 | ||
1560 | /* Loop copying string[i] to string[j], i >= j. */ | |
1561 | while (i < slen) | |
1562 | { | |
1563 | if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' || | |
ccc6cda3 | 1564 | string[i + 1] == '$')) |
7117c2d2 JA |
1565 | i++; |
1566 | prev_i = i; | |
1567 | ADVANCE_CHAR (string, slen, i); | |
1568 | if (j < prev_i) | |
704a1a2a | 1569 | do string[j++] = string[prev_i++]; while (prev_i < i); |
7117c2d2 | 1570 | else |
704a1a2a | 1571 | j = i; |
7117c2d2 JA |
1572 | } |
1573 | string[j] = '\0'; | |
1574 | ||
ccc6cda3 JA |
1575 | return (string); |
1576 | } | |
726f6388 | 1577 | |
ccc6cda3 | 1578 | #if 0 |
cce855bc | 1579 | /*UNUSED*/ |
ccc6cda3 JA |
1580 | /* Replace instances of \! in a string with !. */ |
1581 | void | |
1582 | unquote_bang (string) | |
1583 | char *string; | |
1584 | { | |
1585 | register int i, j; | |
1586 | register char *temp; | |
726f6388 | 1587 | |
f73dda09 | 1588 | temp = (char *)xmalloc (1 + strlen (string)); |
726f6388 | 1589 | |
ccc6cda3 JA |
1590 | for (i = 0, j = 0; (temp[j] = string[i]); i++, j++) |
1591 | { | |
1592 | if (string[i] == '\\' && string[i + 1] == '!') | |
1593 | { | |
1594 | temp[j] = '!'; | |
1595 | i++; | |
1596 | } | |
1597 | } | |
1598 | strcpy (string, temp); | |
1599 | free (temp); | |
726f6388 | 1600 | } |
ccc6cda3 | 1601 | #endif |
726f6388 | 1602 | |
4d8d005b CR |
1603 | #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0) |
1604 | ||
6932f7f5 | 1605 | /* This function assumes s[i] == open; returns with s[ret] == close; used to |
3eb2d94a CR |
1606 | parse array subscripts. FLAGS & 1 means to not attempt to skip over |
1607 | matched pairs of quotes or backquotes, or skip word expansions; it is | |
1608 | intended to be used after expansion has been performed and during final | |
1609 | assignment parsing (see arrayfunc.c:assign_compound_array_list()). */ | |
6932f7f5 CR |
1610 | static int |
1611 | skip_matched_pair (string, start, open, close, flags) | |
76af2125 | 1612 | const char *string; |
6932f7f5 | 1613 | int start, open, close, flags; |
76af2125 CR |
1614 | { |
1615 | int i, pass_next, backq, si, c, count; | |
1616 | size_t slen; | |
1617 | char *temp, *ss; | |
1618 | DECLARE_MBSTATE; | |
1619 | ||
1620 | slen = strlen (string + start) + start; | |
1621 | no_longjmp_on_fatal_error = 1; | |
1622 | ||
1623 | i = start + 1; /* skip over leading bracket */ | |
1624 | count = 1; | |
1625 | pass_next = backq = 0; | |
1626 | ss = (char *)string; | |
1627 | while (c = string[i]) | |
1628 | { | |
1629 | if (pass_next) | |
1630 | { | |
1631 | pass_next = 0; | |
1632 | if (c == 0) | |
1633 | CQ_RETURN(i); | |
1634 | ADVANCE_CHAR (string, slen, i); | |
1635 | continue; | |
1636 | } | |
1637 | else if (c == '\\') | |
1638 | { | |
1639 | pass_next = 1; | |
1640 | i++; | |
1641 | continue; | |
1642 | } | |
1643 | else if (backq) | |
1644 | { | |
1645 | if (c == '`') | |
1646 | backq = 0; | |
1647 | ADVANCE_CHAR (string, slen, i); | |
1648 | continue; | |
1649 | } | |
3eb2d94a | 1650 | else if ((flags & 1) == 0 && c == '`') |
76af2125 CR |
1651 | { |
1652 | backq = 1; | |
1653 | i++; | |
1654 | continue; | |
1655 | } | |
3eb2d94a | 1656 | else if ((flags & 1) == 0 && c == open) |
76af2125 CR |
1657 | { |
1658 | count++; | |
1659 | i++; | |
1660 | continue; | |
1661 | } | |
6932f7f5 | 1662 | else if (c == close) |
76af2125 CR |
1663 | { |
1664 | count--; | |
1665 | if (count == 0) | |
1666 | break; | |
1667 | i++; | |
1668 | continue; | |
1669 | } | |
3eb2d94a | 1670 | else if ((flags & 1) == 0 && (c == '\'' || c == '"')) |
76af2125 CR |
1671 | { |
1672 | i = (c == '\'') ? skip_single_quoted (ss, slen, ++i) | |
1673 | : skip_double_quoted (ss, slen, ++i); | |
1674 | /* no increment, the skip functions increment past the closing quote. */ | |
1675 | } | |
3eb2d94a | 1676 | else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE)) |
76af2125 CR |
1677 | { |
1678 | si = i + 2; | |
1679 | if (string[si] == '\0') | |
1680 | CQ_RETURN(si); | |
1681 | ||
1682 | if (string[i+1] == LPAREN) | |
1683 | temp = extract_delimited_string (ss, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */ | |
1684 | else | |
1685 | temp = extract_dollar_brace_string (ss, &si, 0, SX_NOALLOC); | |
1686 | i = si; | |
1687 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1688 | break; | |
1689 | i++; | |
1690 | continue; | |
1691 | } | |
1692 | else | |
1693 | ADVANCE_CHAR (string, slen, i); | |
1694 | } | |
1695 | ||
1696 | CQ_RETURN(i); | |
1697 | } | |
1698 | ||
6932f7f5 CR |
1699 | #if defined (ARRAY_VARS) |
1700 | int | |
3eb2d94a | 1701 | skipsubscript (string, start, flags) |
6932f7f5 | 1702 | const char *string; |
3eb2d94a | 1703 | int start, flags; |
6932f7f5 | 1704 | { |
3eb2d94a | 1705 | return (skip_matched_pair (string, start, '[', ']', flags)); |
6932f7f5 CR |
1706 | } |
1707 | #endif | |
1708 | ||
c2a47ea9 CR |
1709 | /* Skip characters in STRING until we find a character in DELIMS, and return |
1710 | the index of that character. START is the index into string at which we | |
1711 | begin. This is similar in spirit to strpbrk, but it returns an index into | |
1712 | STRING and takes a starting index. This little piece of code knows quite | |
1713 | a lot of shell syntax. It's very similar to skip_double_quoted and other | |
1714 | functions of that ilk. */ | |
1715 | int | |
4d8d005b | 1716 | skip_to_delim (string, start, delims, flags) |
c2a47ea9 CR |
1717 | char *string; |
1718 | int start; | |
1719 | char *delims; | |
4d8d005b | 1720 | int flags; |
c2a47ea9 | 1721 | { |
098cbb5c | 1722 | int i, pass_next, backq, si, c, invert, skipquote, skipcmd, noprocsub; |
c2a47ea9 | 1723 | size_t slen; |
5f8cde23 | 1724 | char *temp, open[3]; |
c2a47ea9 CR |
1725 | DECLARE_MBSTATE; |
1726 | ||
1727 | slen = strlen (string + start) + start; | |
4d8d005b CR |
1728 | if (flags & SD_NOJMP) |
1729 | no_longjmp_on_fatal_error = 1; | |
8c2fef19 | 1730 | invert = (flags & SD_INVERT); |
176b12ee | 1731 | skipcmd = (flags & SD_NOSKIPCMD) == 0; |
098cbb5c | 1732 | noprocsub = (flags & SD_NOPROCSUB); |
8c2fef19 | 1733 | |
c2a47ea9 CR |
1734 | i = start; |
1735 | pass_next = backq = 0; | |
1736 | while (c = string[i]) | |
1737 | { | |
a8fd3f3e CR |
1738 | /* If this is non-zero, we should not let quote characters be delimiters |
1739 | and the current character is a single or double quote. We should not | |
1740 | test whether or not it's a delimiter until after we skip single- or | |
1741 | double-quoted strings. */ | |
1742 | skipquote = ((flags & SD_NOQUOTEDELIM) && (c == '\'' || c =='"')); | |
c2a47ea9 CR |
1743 | if (pass_next) |
1744 | { | |
1745 | pass_next = 0; | |
1746 | if (c == 0) | |
1747 | CQ_RETURN(i); | |
1748 | ADVANCE_CHAR (string, slen, i); | |
1749 | continue; | |
1750 | } | |
1751 | else if (c == '\\') | |
1752 | { | |
1753 | pass_next = 1; | |
1754 | i++; | |
1755 | continue; | |
1756 | } | |
1757 | else if (backq) | |
1758 | { | |
1759 | if (c == '`') | |
1760 | backq = 0; | |
1761 | ADVANCE_CHAR (string, slen, i); | |
1762 | continue; | |
1763 | } | |
1764 | else if (c == '`') | |
1765 | { | |
1766 | backq = 1; | |
1767 | i++; | |
1768 | continue; | |
1769 | } | |
a8fd3f3e | 1770 | else if (skipquote == 0 && invert == 0 && member (c, delims)) |
8c2fef19 | 1771 | break; |
c2a47ea9 CR |
1772 | else if (c == '\'' || c == '"') |
1773 | { | |
1774 | i = (c == '\'') ? skip_single_quoted (string, slen, ++i) | |
1775 | : skip_double_quoted (string, slen, ++i); | |
1776 | /* no increment, the skip functions increment past the closing quote. */ | |
1777 | } | |
176b12ee | 1778 | else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE)) |
c2a47ea9 CR |
1779 | { |
1780 | si = i + 2; | |
1781 | if (string[si] == '\0') | |
1782 | CQ_RETURN(si); | |
1783 | ||
1784 | if (string[i+1] == LPAREN) | |
1785 | temp = extract_delimited_string (string, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */ | |
1786 | else | |
1787 | temp = extract_dollar_brace_string (string, &si, 0, SX_NOALLOC); | |
1788 | i = si; | |
1789 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1790 | break; | |
1791 | i++; | |
1792 | continue; | |
1793 | } | |
176b12ee | 1794 | #if defined (PROCESS_SUBSTITUTION) |
098cbb5c | 1795 | else if (skipcmd && noprocsub == 0 && (c == '<' || c == '>') && string[i+1] == LPAREN) |
176b12ee CR |
1796 | { |
1797 | si = i + 2; | |
1798 | if (string[si] == '\0') | |
1799 | CQ_RETURN(si); | |
fbbc416f CR |
1800 | temp = extract_process_subst (string, (c == '<') ? "<(" : ">(", &si, 0); |
1801 | free (temp); /* XXX - not using SX_ALLOC here yet */ | |
176b12ee CR |
1802 | i = si; |
1803 | if (string[i] == '\0') | |
1804 | break; | |
1805 | i++; | |
1806 | continue; | |
1807 | } | |
1808 | #endif /* PROCESS_SUBSTITUTION */ | |
5f8cde23 CR |
1809 | #if defined (EXTENDED_GLOB) |
1810 | else if ((flags & SD_EXTGLOB) && extended_glob && string[i+1] == LPAREN && member (c, "?*+!@")) | |
1811 | { | |
1812 | si = i + 2; | |
1813 | if (string[si] == '\0') | |
1814 | CQ_RETURN(si); | |
1815 | ||
1816 | open[0] = c; | |
1817 | open[1] = LPAREN; | |
1818 | open[2] = '\0'; | |
1819 | temp = extract_delimited_string (string, &si, open, "(", ")", SX_NOALLOC); /* ) */ | |
1820 | ||
1821 | i = si; | |
1822 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1823 | break; | |
1824 | i++; | |
1825 | continue; | |
1826 | } | |
1827 | #endif | |
bb579650 CR |
1828 | else if ((flags & SD_GLOB) && c == LBRACK) |
1829 | { | |
1830 | si = i + 1; | |
1831 | if (string[si] == '\0') | |
1832 | CQ_RETURN(si); | |
1833 | ||
c61bfbfd | 1834 | temp = extract_delimited_string (string, &si, "[", "[", "]", SX_NOALLOC); /* ] */ |
bb579650 CR |
1835 | |
1836 | i = si; | |
1837 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1838 | break; | |
1839 | i++; | |
1840 | continue; | |
1841 | } | |
a8fd3f3e | 1842 | else if ((skipquote || invert) && (member (c, delims) == 0)) |
c2a47ea9 CR |
1843 | break; |
1844 | else | |
1845 | ADVANCE_CHAR (string, slen, i); | |
1846 | } | |
1847 | ||
1848 | CQ_RETURN(i); | |
1849 | } | |
1850 | ||
ccc6cda3 | 1851 | #if defined (READLINE) |
726f6388 JA |
1852 | /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is |
1853 | an unclosed quoted string), or if the character at EINDEX is quoted | |
28ef6c31 | 1854 | by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various |
b72432fd | 1855 | single and double-quoted string parsing functions should not return an |
7117c2d2 JA |
1856 | error if there are unclosed quotes or braces. The characters that this |
1857 | recognizes need to be the same as the contents of | |
1858 | rl_completer_quote_characters. */ | |
b72432fd | 1859 | |
726f6388 JA |
1860 | int |
1861 | char_is_quoted (string, eindex) | |
1862 | char *string; | |
1863 | int eindex; | |
1864 | { | |
7117c2d2 JA |
1865 | int i, pass_next, c; |
1866 | size_t slen; | |
1867 | DECLARE_MBSTATE; | |
726f6388 | 1868 | |
7117c2d2 | 1869 | slen = strlen (string); |
28ef6c31 | 1870 | no_longjmp_on_fatal_error = 1; |
7117c2d2 JA |
1871 | i = pass_next = 0; |
1872 | while (i <= eindex) | |
726f6388 | 1873 | { |
7117c2d2 JA |
1874 | c = string[i]; |
1875 | ||
726f6388 JA |
1876 | if (pass_next) |
1877 | { | |
1878 | pass_next = 0; | |
1879 | if (i >= eindex) /* XXX was if (i >= eindex - 1) */ | |
b72432fd | 1880 | CQ_RETURN(1); |
7117c2d2 | 1881 | ADVANCE_CHAR (string, slen, i); |
726f6388 JA |
1882 | continue; |
1883 | } | |
7117c2d2 | 1884 | else if (c == '\\') |
ccc6cda3 JA |
1885 | { |
1886 | pass_next = 1; | |
7117c2d2 | 1887 | i++; |
ccc6cda3 JA |
1888 | continue; |
1889 | } | |
7117c2d2 JA |
1890 | else if (c == '\'' || c == '"') |
1891 | { | |
1892 | i = (c == '\'') ? skip_single_quoted (string, slen, ++i) | |
1893 | : skip_double_quoted (string, slen, ++i); | |
1894 | if (i > eindex) | |
1895 | CQ_RETURN(1); | |
1896 | /* no increment, the skip_xxx functions go one past end */ | |
1897 | } | |
1898 | else | |
1899 | ADVANCE_CHAR (string, slen, i); | |
726f6388 | 1900 | } |
7117c2d2 | 1901 | |
b72432fd | 1902 | CQ_RETURN(0); |
726f6388 JA |
1903 | } |
1904 | ||
726f6388 JA |
1905 | int |
1906 | unclosed_pair (string, eindex, openstr) | |
1907 | char *string; | |
1908 | int eindex; | |
1909 | char *openstr; | |
1910 | { | |
ccc6cda3 | 1911 | int i, pass_next, openc, olen; |
7117c2d2 JA |
1912 | size_t slen; |
1913 | DECLARE_MBSTATE; | |
726f6388 | 1914 | |
7117c2d2 | 1915 | slen = strlen (string); |
726f6388 | 1916 | olen = strlen (openstr); |
7117c2d2 JA |
1917 | i = pass_next = openc = 0; |
1918 | while (i <= eindex) | |
726f6388 JA |
1919 | { |
1920 | if (pass_next) | |
1921 | { | |
1922 | pass_next = 0; | |
1923 | if (i >= eindex) /* XXX was if (i >= eindex - 1) */ | |
1924 | return 0; | |
7117c2d2 JA |
1925 | ADVANCE_CHAR (string, slen, i); |
1926 | continue; | |
1927 | } | |
1928 | else if (string[i] == '\\') | |
1929 | { | |
1930 | pass_next = 1; | |
1931 | i++; | |
726f6388 JA |
1932 | continue; |
1933 | } | |
1934 | else if (STREQN (string + i, openstr, olen)) | |
1935 | { | |
1936 | openc = 1 - openc; | |
7117c2d2 | 1937 | i += olen; |
726f6388 | 1938 | } |
ccc6cda3 | 1939 | else if (string[i] == '\'' || string[i] == '"') |
726f6388 | 1940 | { |
7117c2d2 JA |
1941 | i = (string[i] == '\'') ? skip_single_quoted (string, slen, i) |
1942 | : skip_double_quoted (string, slen, i); | |
726f6388 JA |
1943 | if (i > eindex) |
1944 | return 0; | |
1945 | } | |
7117c2d2 JA |
1946 | else |
1947 | ADVANCE_CHAR (string, slen, i); | |
726f6388 JA |
1948 | } |
1949 | return (openc); | |
1950 | } | |
bb70624e | 1951 | |
bb70624e JA |
1952 | /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the |
1953 | individual words. If DELIMS is NULL, the current value of $IFS is used | |
d3a24ed2 CR |
1954 | to split the string, and the function follows the shell field splitting |
1955 | rules. SENTINEL is an index to look for. NWP, if non-NULL, | |
bb70624e JA |
1956 | gets the number of words in the returned list. CWP, if non-NULL, gets |
1957 | the index of the word containing SENTINEL. Non-whitespace chars in | |
1958 | DELIMS delimit separate fields. */ | |
1959 | WORD_LIST * | |
a8fd3f3e | 1960 | split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp) |
bb70624e JA |
1961 | char *string; |
1962 | int slen; | |
1963 | char *delims; | |
a8fd3f3e | 1964 | int sentinel, flags; |
bb70624e JA |
1965 | int *nwp, *cwp; |
1966 | { | |
a8fd3f3e | 1967 | int ts, te, i, nw, cw, ifs_split, dflags; |
f73dda09 | 1968 | char *token, *d, *d2; |
bb70624e JA |
1969 | WORD_LIST *ret, *tl; |
1970 | ||
1971 | if (string == 0 || *string == '\0') | |
1972 | { | |
1973 | if (nwp) | |
1974 | *nwp = 0; | |
1975 | if (cwp) | |
1976 | *cwp = 0; | |
1977 | return ((WORD_LIST *)NULL); | |
1978 | } | |
1979 | ||
7117c2d2 | 1980 | d = (delims == 0) ? ifs_value : delims; |
d3a24ed2 | 1981 | ifs_split = delims == 0; |
bb70624e JA |
1982 | |
1983 | /* Make d2 the non-whitespace characters in delims */ | |
1984 | d2 = 0; | |
1985 | if (delims) | |
1986 | { | |
633e5c6d CR |
1987 | size_t slength; |
1988 | #if defined (HANDLE_MULTIBYTE) | |
1989 | size_t mblength = 1; | |
1990 | #endif | |
1991 | DECLARE_MBSTATE; | |
1992 | ||
1993 | slength = strlen (delims); | |
1994 | d2 = (char *)xmalloc (slength + 1); | |
1995 | i = ts = 0; | |
1996 | while (delims[i]) | |
bb70624e | 1997 | { |
633e5c6d | 1998 | #if defined (HANDLE_MULTIBYTE) |
d3ad40de CR |
1999 | mbstate_t state_bak; |
2000 | state_bak = state; | |
633e5c6d CR |
2001 | mblength = MBRLEN (delims + i, slength, &state); |
2002 | if (MB_INVALIDCH (mblength)) | |
2003 | state = state_bak; | |
22e63b05 | 2004 | else if (mblength > 1) |
633e5c6d CR |
2005 | { |
2006 | memcpy (d2 + ts, delims + i, mblength); | |
2007 | ts += mblength; | |
2008 | i += mblength; | |
2009 | slength -= mblength; | |
2010 | continue; | |
2011 | } | |
2012 | #endif | |
2013 | if (whitespace (delims[i]) == 0) | |
bb70624e | 2014 | d2[ts++] = delims[i]; |
633e5c6d CR |
2015 | |
2016 | i++; | |
2017 | slength--; | |
bb70624e JA |
2018 | } |
2019 | d2[ts] = '\0'; | |
2020 | } | |
2021 | ||
2022 | ret = (WORD_LIST *)NULL; | |
2023 | ||
dfc21851 | 2024 | /* Remove sequences of whitespace characters at the start of the string, as |
d3a24ed2 CR |
2025 | long as those characters are delimiters. */ |
2026 | for (i = 0; member (string[i], d) && spctabnl (string[i]); i++) | |
bb70624e JA |
2027 | ; |
2028 | if (string[i] == '\0') | |
2029 | return (ret); | |
2030 | ||
2031 | ts = i; | |
2032 | nw = 0; | |
2033 | cw = -1; | |
a8fd3f3e | 2034 | dflags = flags|SD_NOJMP; |
bb70624e JA |
2035 | while (1) |
2036 | { | |
a8fd3f3e | 2037 | te = skip_to_delim (string, ts, d, dflags); |
bb70624e JA |
2038 | |
2039 | /* If we have a non-whitespace delimiter character, use it to make a | |
2040 | separate field. This is just about what $IFS splitting does and | |
2041 | is closer to the behavior of the shell parser. */ | |
28ef6c31 | 2042 | if (ts == te && d2 && member (string[ts], d2)) |
bb70624e JA |
2043 | { |
2044 | te = ts + 1; | |
d3a24ed2 CR |
2045 | /* If we're using IFS splitting, the non-whitespace delimiter char |
2046 | and any additional IFS whitespace delimits a field. */ | |
2047 | if (ifs_split) | |
2048 | while (member (string[te], d) && spctabnl (string[te])) | |
2049 | te++; | |
2050 | else | |
2051 | while (member (string[te], d2)) | |
2052 | te++; | |
bb70624e JA |
2053 | } |
2054 | ||
2055 | token = substring (string, ts, te); | |
2056 | ||
2057 | ret = add_string_to_list (token, ret); | |
2058 | free (token); | |
2059 | nw++; | |
2060 | ||
2061 | if (sentinel >= ts && sentinel <= te) | |
2062 | cw = nw; | |
2063 | ||
2064 | /* If the cursor is at whitespace just before word start, set the | |
28ef6c31 | 2065 | sentinel word to the current word. */ |
bb70624e JA |
2066 | if (cwp && cw == -1 && sentinel == ts-1) |
2067 | cw = nw; | |
2068 | ||
2069 | /* If the cursor is at whitespace between two words, make a new, empty | |
28ef6c31 JA |
2070 | word, add it before (well, after, since the list is in reverse order) |
2071 | the word we just added, and set the current word to that one. */ | |
bb70624e | 2072 | if (cwp && cw == -1 && sentinel < ts) |
28ef6c31 | 2073 | { |
7117c2d2 | 2074 | tl = make_word_list (make_word (""), ret->next); |
28ef6c31 JA |
2075 | ret->next = tl; |
2076 | cw = nw; | |
2077 | nw++; | |
2078 | } | |
bb70624e JA |
2079 | |
2080 | if (string[te] == 0) | |
2081 | break; | |
2082 | ||
d3a24ed2 CR |
2083 | i = te; |
2084 | while (member (string[i], d) && (ifs_split || spctabnl(string[i]))) | |
bb70624e JA |
2085 | i++; |
2086 | ||
2087 | if (string[i]) | |
2088 | ts = i; | |
2089 | else | |
2090 | break; | |
2091 | } | |
2092 | ||
2093 | /* Special case for SENTINEL at the end of STRING. If we haven't found | |
2094 | the word containing SENTINEL yet, and the index we're looking for is at | |
dfc21851 CR |
2095 | the end of STRING (or past the end of the previously-found token, |
2096 | possible if the end of the line is composed solely of IFS whitespace) | |
2097 | add an additional null argument and set the current word pointer to that. */ | |
2098 | if (cwp && cw == -1 && (sentinel >= slen || sentinel >= te)) | |
bb70624e JA |
2099 | { |
2100 | if (whitespace (string[sentinel - 1])) | |
28ef6c31 JA |
2101 | { |
2102 | token = ""; | |
2103 | ret = add_string_to_list (token, ret); | |
2104 | nw++; | |
2105 | } | |
bb70624e JA |
2106 | cw = nw; |
2107 | } | |
2108 | ||
2109 | if (nwp) | |
2110 | *nwp = nw; | |
2111 | if (cwp) | |
2112 | *cwp = cw; | |
2113 | ||
631b20c6 CR |
2114 | FREE (d2); |
2115 | ||
bb70624e JA |
2116 | return (REVERSE_LIST (ret, WORD_LIST *)); |
2117 | } | |
726f6388 JA |
2118 | #endif /* READLINE */ |
2119 | ||
ccc6cda3 JA |
2120 | #if 0 |
2121 | /* UNUSED */ | |
726f6388 JA |
2122 | /* Extract the name of the variable to bind to from the assignment string. */ |
2123 | char * | |
2124 | assignment_name (string) | |
2125 | char *string; | |
2126 | { | |
ccc6cda3 | 2127 | int offset; |
726f6388 JA |
2128 | char *temp; |
2129 | ||
5e13499c | 2130 | offset = assignment (string, 0); |
ccc6cda3 | 2131 | if (offset == 0) |
726f6388 | 2132 | return (char *)NULL; |
bb70624e | 2133 | temp = substring (string, 0, offset); |
726f6388 JA |
2134 | return (temp); |
2135 | } | |
ccc6cda3 | 2136 | #endif |
726f6388 | 2137 | |
cce855bc JA |
2138 | /* **************************************************************** */ |
2139 | /* */ | |
2140 | /* Functions to convert strings to WORD_LISTs and vice versa */ | |
2141 | /* */ | |
2142 | /* **************************************************************** */ | |
2143 | ||
726f6388 JA |
2144 | /* Return a single string of all the words in LIST. SEP is the separator |
2145 | to put between individual elements of LIST in the output string. */ | |
7117c2d2 | 2146 | char * |
726f6388 JA |
2147 | string_list_internal (list, sep) |
2148 | WORD_LIST *list; | |
2149 | char *sep; | |
2150 | { | |
2151 | register WORD_LIST *t; | |
2152 | char *result, *r; | |
2153 | int word_len, sep_len, result_size; | |
2154 | ||
ccc6cda3 | 2155 | if (list == 0) |
726f6388 JA |
2156 | return ((char *)NULL); |
2157 | ||
12d937f9 CR |
2158 | /* Short-circuit quickly if we don't need to separate anything. */ |
2159 | if (list->next == 0) | |
2160 | return (savestring (list->word->word)); | |
2161 | ||
726f6388 JA |
2162 | /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */ |
2163 | sep_len = STRLEN (sep); | |
2164 | result_size = 0; | |
2165 | ||
2166 | for (t = list; t; t = t->next) | |
2167 | { | |
2168 | if (t != list) | |
2169 | result_size += sep_len; | |
2170 | result_size += strlen (t->word->word); | |
2171 | } | |
2172 | ||
f73dda09 | 2173 | r = result = (char *)xmalloc (result_size + 1); |
726f6388 JA |
2174 | |
2175 | for (t = list; t; t = t->next) | |
2176 | { | |
2177 | if (t != list && sep_len) | |
2178 | { | |
ccc6cda3 JA |
2179 | if (sep_len > 1) |
2180 | { | |
2181 | FASTCOPY (sep, r, sep_len); | |
2182 | r += sep_len; | |
2183 | } | |
2184 | else | |
2185 | *r++ = sep[0]; | |
726f6388 JA |
2186 | } |
2187 | ||
2188 | word_len = strlen (t->word->word); | |
2189 | FASTCOPY (t->word->word, r, word_len); | |
2190 | r += word_len; | |
2191 | } | |
2192 | ||
ccc6cda3 | 2193 | *r = '\0'; |
726f6388 JA |
2194 | return (result); |
2195 | } | |
2196 | ||
2197 | /* Return a single string of all the words present in LIST, separating | |
2198 | each word with a space. */ | |
2199 | char * | |
2200 | string_list (list) | |
2201 | WORD_LIST *list; | |
2202 | { | |
2203 | return (string_list_internal (list, " ")); | |
2204 | } | |
2205 | ||
c2a47ea9 CR |
2206 | /* An external interface that can be used by the rest of the shell to |
2207 | obtain a string containing the first character in $IFS. Handles all | |
2208 | the multibyte complications. If LENP is non-null, it is set to the | |
2209 | length of the returned string. */ | |
2210 | char * | |
2211 | ifs_firstchar (lenp) | |
2212 | int *lenp; | |
2213 | { | |
2214 | char *ret; | |
2215 | int len; | |
2216 | ||
2217 | ret = xmalloc (MB_LEN_MAX + 1); | |
2218 | #if defined (HANDLE_MULTIBYTE) | |
2219 | if (ifs_firstc_len == 1) | |
2220 | { | |
2221 | ret[0] = ifs_firstc[0]; | |
2222 | ret[1] = '\0'; | |
2223 | len = ret[0] ? 1 : 0; | |
2224 | } | |
2225 | else | |
2226 | { | |
2227 | memcpy (ret, ifs_firstc, ifs_firstc_len); | |
2228 | ret[len = ifs_firstc_len] = '\0'; | |
2229 | } | |
2230 | #else | |
2231 | ret[0] = ifs_firstc; | |
2232 | ret[1] = '\0'; | |
2233 | len = ret[0] ? 0 : 1; | |
2234 | #endif | |
2235 | ||
2236 | if (lenp) | |
2237 | *lenp = len; | |
2238 | ||
2239 | return ret; | |
2240 | } | |
2241 | ||
726f6388 JA |
2242 | /* Return a single string of all the words present in LIST, obeying the |
2243 | quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the | |
2244 | expansion [of $*] appears within a double quoted string, it expands | |
2245 | to a single field with the value of each parameter separated by the | |
2246 | first character of the IFS variable, or by a <space> if IFS is unset." */ | |
f73dda09 | 2247 | char * |
726f6388 JA |
2248 | string_list_dollar_star (list) |
2249 | WORD_LIST *list; | |
2250 | { | |
dc8fbaf9 | 2251 | char *ret; |
633e5c6d | 2252 | #if defined (HANDLE_MULTIBYTE) |
dc8fbaf9 | 2253 | # if defined (__GNUC__) |
633e5c6d | 2254 | char sep[MB_CUR_MAX + 1]; |
dc8fbaf9 CR |
2255 | # else |
2256 | char *sep = 0; | |
2257 | # endif | |
633e5c6d | 2258 | #else |
7117c2d2 | 2259 | char sep[2]; |
633e5c6d CR |
2260 | #endif |
2261 | ||
633e5c6d | 2262 | #if defined (HANDLE_MULTIBYTE) |
dc8fbaf9 CR |
2263 | # if !defined (__GNUC__) |
2264 | sep = (char *)xmalloc (MB_CUR_MAX + 1); | |
2265 | # endif /* !__GNUC__ */ | |
633e5c6d CR |
2266 | if (ifs_firstc_len == 1) |
2267 | { | |
2268 | sep[0] = ifs_firstc[0]; | |
2269 | sep[1] = '\0'; | |
2270 | } | |
2271 | else | |
da713c23 CR |
2272 | { |
2273 | memcpy (sep, ifs_firstc, ifs_firstc_len); | |
2274 | sep[ifs_firstc_len] = '\0'; | |
2275 | } | |
633e5c6d | 2276 | #else |
7117c2d2 | 2277 | sep[0] = ifs_firstc; |
726f6388 | 2278 | sep[1] = '\0'; |
633e5c6d | 2279 | #endif |
726f6388 | 2280 | |
dc8fbaf9 CR |
2281 | ret = string_list_internal (list, sep); |
2282 | #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__) | |
2283 | free (sep); | |
2284 | #endif | |
2285 | return ret; | |
726f6388 JA |
2286 | } |
2287 | ||
cce855bc JA |
2288 | /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
2289 | is non-zero, the $@ appears within double quotes, and we should quote | |
2290 | the list before converting it into a string. If IFS is unset, and the | |
2291 | word is not quoted, we just need to quote CTLESC and CTLNUL characters | |
2292 | in the words in the list, because the default value of $IFS is | |
2293 | <space><tab><newline>, IFS characters in the words in the list should | |
2294 | also be split. If IFS is null, and the word is not quoted, we need | |
2295 | to quote the words in the list to preserve the positional parameters | |
2296 | exactly. */ | |
f73dda09 | 2297 | char * |
cce855bc JA |
2298 | string_list_dollar_at (list, quoted) |
2299 | WORD_LIST *list; | |
2300 | int quoted; | |
2301 | { | |
227f982e | 2302 | char *ifs, *ret; |
633e5c6d | 2303 | #if defined (HANDLE_MULTIBYTE) |
dc8fbaf9 | 2304 | # if defined (__GNUC__) |
633e5c6d | 2305 | char sep[MB_CUR_MAX + 1]; |
dc8fbaf9 CR |
2306 | # else |
2307 | char *sep = 0; | |
2308 | # endif /* !__GNUC__ */ | |
633e5c6d CR |
2309 | #else |
2310 | char sep[2]; | |
2311 | #endif | |
cce855bc JA |
2312 | WORD_LIST *tlist; |
2313 | ||
7117c2d2 JA |
2314 | /* XXX this could just be ifs = ifs_value; */ |
2315 | ifs = ifs_var ? value_cell (ifs_var) : (char *)0; | |
cce855bc | 2316 | |
633e5c6d | 2317 | #if defined (HANDLE_MULTIBYTE) |
dc8fbaf9 CR |
2318 | # if !defined (__GNUC__) |
2319 | sep = (char *)xmalloc (MB_CUR_MAX + 1); | |
2320 | # endif /* !__GNUC__ */ | |
633e5c6d CR |
2321 | if (ifs && *ifs) |
2322 | { | |
227f982e | 2323 | if (ifs_firstc_len == 1) |
633e5c6d | 2324 | { |
227f982e | 2325 | sep[0] = ifs_firstc[0]; |
633e5c6d CR |
2326 | sep[1] = '\0'; |
2327 | } | |
2328 | else | |
2329 | { | |
227f982e CR |
2330 | memcpy (sep, ifs_firstc, ifs_firstc_len); |
2331 | sep[ifs_firstc_len] = '\0'; | |
633e5c6d CR |
2332 | } |
2333 | } | |
2334 | else | |
2335 | { | |
2336 | sep[0] = ' '; | |
2337 | sep[1] = '\0'; | |
2338 | } | |
2339 | #else | |
cce855bc JA |
2340 | sep[0] = (ifs == 0 || *ifs == 0) ? ' ' : *ifs; |
2341 | sep[1] = '\0'; | |
633e5c6d | 2342 | #endif |
cce855bc | 2343 | |
d3ad40de CR |
2344 | /* XXX -- why call quote_list if ifs == 0? we can get away without doing |
2345 | it now that quote_escapes quotes spaces */ | |
1231ac47 | 2346 | tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE)) |
cce855bc JA |
2347 | ? quote_list (list) |
2348 | : list_quote_escapes (list); | |
dc8fbaf9 CR |
2349 | |
2350 | ret = string_list_internal (tlist, sep); | |
2351 | #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__) | |
2352 | free (sep); | |
2353 | #endif | |
2354 | return ret; | |
cce855bc JA |
2355 | } |
2356 | ||
39feef01 | 2357 | /* Turn the positional parameters into a string, understanding quoting and |
e33f2203 CR |
2358 | the various subtleties of using the first character of $IFS as the |
2359 | separator. Calls string_list_dollar_at, string_list_dollar_star, and | |
2360 | string_list as appropriate. */ | |
2361 | char * | |
2362 | string_list_pos_params (pchar, list, quoted) | |
2363 | int pchar; | |
2364 | WORD_LIST *list; | |
2365 | int quoted; | |
2366 | { | |
2367 | char *ret; | |
2368 | WORD_LIST *tlist; | |
2369 | ||
2370 | if (pchar == '*' && (quoted & Q_DOUBLE_QUOTES)) | |
2371 | { | |
2372 | tlist = quote_list (list); | |
2373 | word_list_remove_quoted_nulls (tlist); | |
2374 | ret = string_list_dollar_star (tlist); | |
2375 | } | |
2376 | else if (pchar == '*' && (quoted & Q_HERE_DOCUMENT)) | |
2377 | { | |
2378 | tlist = quote_list (list); | |
2379 | word_list_remove_quoted_nulls (tlist); | |
2380 | ret = string_list (tlist); | |
2381 | } | |
2382 | else if (pchar == '*') | |
2383 | { | |
2384 | /* Even when unquoted, string_list_dollar_star does the right thing | |
2385 | making sure that the first character of $IFS is used as the | |
2386 | separator. */ | |
2387 | ret = string_list_dollar_star (list); | |
2388 | } | |
2389 | else if (pchar == '@' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
2390 | /* We use string_list_dollar_at, but only if the string is quoted, since | |
2391 | that quotes the escapes if it's not, which we don't want. We could | |
2392 | use string_list (the old code did), but that doesn't do the right | |
2393 | thing if the first character of $IFS is not a space. We use | |
2394 | string_list_dollar_star if the string is unquoted so we make sure that | |
2395 | the elements of $@ are separated by the first character of $IFS for | |
2396 | later splitting. */ | |
2397 | ret = string_list_dollar_at (list, quoted); | |
2398 | else if (pchar == '@') | |
2399 | ret = string_list_dollar_star (list); | |
2400 | else | |
2401 | ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list); | |
2402 | ||
2403 | return ret; | |
2404 | } | |
2405 | ||
726f6388 JA |
2406 | /* Return the list of words present in STRING. Separate the string into |
2407 | words at any of the characters found in SEPARATORS. If QUOTED is | |
2408 | non-zero then word in the list will have its quoted flag set, otherwise | |
2409 | the quoted flag is left as make_word () deemed fit. | |
2410 | ||
2411 | This obeys the P1003.2 word splitting semantics. If `separators' is | |
2412 | exactly <space><tab><newline>, then the splitting algorithm is that of | |
2413 | the Bourne shell, which treats any sequence of characters from `separators' | |
2414 | as a delimiter. If IFS is unset, which results in `separators' being set | |
2415 | to "", no splitting occurs. If separators has some other value, the | |
2416 | following rules are applied (`IFS white space' means zero or more | |
2417 | occurrences of <space>, <tab>, or <newline>, as long as those characters | |
2418 | are in `separators'): | |
2419 | ||
2420 | 1) IFS white space is ignored at the start and the end of the | |
2421 | string. | |
2422 | 2) Each occurrence of a character in `separators' that is not | |
2423 | IFS white space, along with any adjacent occurrences of | |
2424 | IFS white space delimits a field. | |
2425 | 3) Any nonzero-length sequence of IFS white space delimits a field. | |
2426 | */ | |
2427 | ||
2428 | /* BEWARE! list_string strips null arguments. Don't call it twice and | |
2429 | expect to have "" preserved! */ | |
2430 | ||
726f6388 JA |
2431 | /* This performs word splitting and quoted null character removal on |
2432 | STRING. */ | |
d3a24ed2 CR |
2433 | #define issep(c) \ |
2434 | (((separators)[0]) ? ((separators)[1] ? isifs(c) \ | |
2435 | : (c) == (separators)[0]) \ | |
2436 | : 0) | |
726f6388 JA |
2437 | |
2438 | WORD_LIST * | |
2439 | list_string (string, separators, quoted) | |
2440 | register char *string, *separators; | |
2441 | int quoted; | |
2442 | { | |
ccc6cda3 JA |
2443 | WORD_LIST *result; |
2444 | WORD_DESC *t; | |
2445 | char *current_word, *s; | |
e6598ba4 | 2446 | int sindex, sh_style_split, whitesep, xflags; |
633e5c6d | 2447 | size_t slen; |
726f6388 JA |
2448 | |
2449 | if (!string || !*string) | |
2450 | return ((WORD_LIST *)NULL); | |
2451 | ||
7117c2d2 JA |
2452 | sh_style_split = separators && separators[0] == ' ' && |
2453 | separators[1] == '\t' && | |
2454 | separators[2] == '\n' && | |
2455 | separators[3] == '\0'; | |
e6598ba4 CR |
2456 | for (xflags = 0, s = ifs_value; s && *s; s++) |
2457 | { | |
2458 | if (*s == CTLESC) xflags |= SX_NOCTLESC; | |
2459 | else if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL; | |
2460 | } | |
726f6388 | 2461 | |
633e5c6d | 2462 | slen = 0; |
726f6388 JA |
2463 | /* Remove sequences of whitespace at the beginning of STRING, as |
2464 | long as those characters appear in IFS. Do not do this if | |
2465 | STRING is quoted or if there are no separator characters. */ | |
2466 | if (!quoted || !separators || !*separators) | |
2467 | { | |
2468 | for (s = string; *s && spctabnl (*s) && issep (*s); s++); | |
2469 | ||
2470 | if (!*s) | |
2471 | return ((WORD_LIST *)NULL); | |
2472 | ||
2473 | string = s; | |
2474 | } | |
2475 | ||
2476 | /* OK, now STRING points to a word that does not begin with white space. | |
2477 | The splitting algorithm is: | |
7117c2d2 JA |
2478 | extract a word, stopping at a separator |
2479 | skip sequences of spc, tab, or nl as long as they are separators | |
726f6388 | 2480 | This obeys the field splitting rules in Posix.2. */ |
da713c23 | 2481 | slen = (MB_CUR_MAX > 1) ? strlen (string) : 1; |
ccc6cda3 | 2482 | for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; ) |
726f6388 | 2483 | { |
da713c23 CR |
2484 | /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim |
2485 | unless multibyte chars are possible. */ | |
e6598ba4 | 2486 | current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags); |
ccc6cda3 | 2487 | if (current_word == 0) |
726f6388 JA |
2488 | break; |
2489 | ||
2490 | /* If we have a quoted empty string, add a quoted null argument. We | |
2491 | want to preserve the quoted null character iff this is a quoted | |
2492 | empty string; otherwise the quoted null characters are removed | |
2493 | below. */ | |
2494 | if (QUOTED_NULL (current_word)) | |
2495 | { | |
227f982e | 2496 | t = alloc_word_desc (); |
726f6388 | 2497 | t->word = make_quoted_char ('\0'); |
227f982e | 2498 | t->flags |= W_QUOTED|W_HASQUOTEDNULL; |
726f6388 JA |
2499 | result = make_word_list (t, result); |
2500 | } | |
ccc6cda3 | 2501 | else if (current_word[0] != '\0') |
726f6388 JA |
2502 | { |
2503 | /* If we have something, then add it regardless. However, | |
2504 | perform quoted null character removal on the current word. */ | |
2505 | remove_quoted_nulls (current_word); | |
cce855bc | 2506 | result = add_string_to_list (current_word, result); |
227f982e | 2507 | result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */ |
ccc6cda3 JA |
2508 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) |
2509 | result->word->flags |= W_QUOTED; | |
726f6388 JA |
2510 | } |
2511 | ||
2512 | /* If we're not doing sequences of separators in the traditional | |
2513 | Bourne shell style, then add a quoted null argument. */ | |
726f6388 JA |
2514 | else if (!sh_style_split && !spctabnl (string[sindex])) |
2515 | { | |
227f982e | 2516 | t = alloc_word_desc (); |
ccc6cda3 | 2517 | t->word = make_quoted_char ('\0'); |
227f982e | 2518 | t->flags |= W_QUOTED|W_HASQUOTEDNULL; |
ccc6cda3 | 2519 | result = make_word_list (t, result); |
726f6388 JA |
2520 | } |
2521 | ||
2522 | free (current_word); | |
2523 | ||
28ef6c31 JA |
2524 | /* Note whether or not the separator is IFS whitespace, used later. */ |
2525 | whitesep = string[sindex] && spctabnl (string[sindex]); | |
2526 | ||
726f6388 JA |
2527 | /* Move past the current separator character. */ |
2528 | if (string[sindex]) | |
633e5c6d CR |
2529 | { |
2530 | DECLARE_MBSTATE; | |
633e5c6d CR |
2531 | ADVANCE_CHAR (string, slen, sindex); |
2532 | } | |
726f6388 JA |
2533 | |
2534 | /* Now skip sequences of space, tab, or newline characters if they are | |
2535 | in the list of separators. */ | |
2536 | while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex])) | |
2537 | sindex++; | |
28ef6c31 | 2538 | |
7117c2d2 JA |
2539 | /* If the first separator was IFS whitespace and the current character |
2540 | is a non-whitespace IFS character, it should be part of the current | |
2541 | field delimiter, not a separate delimiter that would result in an | |
2542 | empty field. Look at POSIX.2, 3.6.5, (3)(b). */ | |
28ef6c31 | 2543 | if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex])) |
9d2b70f0 CR |
2544 | { |
2545 | sindex++; | |
2546 | /* An IFS character that is not IFS white space, along with any | |
2547 | adjacent IFS white space, shall delimit a field. (SUSv3) */ | |
d3ad40de | 2548 | while (string[sindex] && spctabnl (string[sindex]) && isifs (string[sindex])) |
9d2b70f0 CR |
2549 | sindex++; |
2550 | } | |
726f6388 JA |
2551 | } |
2552 | return (REVERSE_LIST (result, WORD_LIST *)); | |
2553 | } | |
2554 | ||
2555 | /* Parse a single word from STRING, using SEPARATORS to separate fields. | |
2556 | ENDPTR is set to the first character after the word. This is used by | |
7117c2d2 JA |
2557 | the `read' builtin. This is never called with SEPARATORS != $IFS; |
2558 | it should be simplified. | |
2559 | ||
726f6388 JA |
2560 | XXX - this function is very similar to list_string; they should be |
2561 | combined - XXX */ | |
2562 | char * | |
2563 | get_word_from_string (stringp, separators, endptr) | |
2564 | char **stringp, *separators, **endptr; | |
2565 | { | |
2566 | register char *s; | |
2567 | char *current_word; | |
e6598ba4 | 2568 | int sindex, sh_style_split, whitesep, xflags; |
633e5c6d | 2569 | size_t slen; |
726f6388 JA |
2570 | |
2571 | if (!stringp || !*stringp || !**stringp) | |
2572 | return ((char *)NULL); | |
ccc6cda3 | 2573 | |
7117c2d2 JA |
2574 | sh_style_split = separators && separators[0] == ' ' && |
2575 | separators[1] == '\t' && | |
2576 | separators[2] == '\n' && | |
2577 | separators[3] == '\0'; | |
e6598ba4 CR |
2578 | for (xflags = 0, s = ifs_value; s && *s; s++) |
2579 | { | |
2580 | if (*s == CTLESC) xflags |= SX_NOCTLESC; | |
2581 | if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL; | |
2582 | } | |
726f6388 | 2583 | |
e6598ba4 | 2584 | s = *stringp; |
633e5c6d CR |
2585 | slen = 0; |
2586 | ||
726f6388 JA |
2587 | /* Remove sequences of whitespace at the beginning of STRING, as |
2588 | long as those characters appear in IFS. */ | |
2589 | if (sh_style_split || !separators || !*separators) | |
2590 | { | |
7117c2d2 | 2591 | for (; *s && spctabnl (*s) && isifs (*s); s++); |
726f6388 JA |
2592 | |
2593 | /* If the string is nothing but whitespace, update it and return. */ | |
2594 | if (!*s) | |
2595 | { | |
2596 | *stringp = s; | |
2597 | if (endptr) | |
2598 | *endptr = s; | |
2599 | return ((char *)NULL); | |
2600 | } | |
2601 | } | |
2602 | ||
2603 | /* OK, S points to a word that does not begin with white space. | |
2604 | Now extract a word, stopping at a separator, save a pointer to | |
2605 | the first character after the word, then skip sequences of spc, | |
2606 | tab, or nl as long as they are separators. | |
ccc6cda3 | 2607 | |
726f6388 JA |
2608 | This obeys the field splitting rules in Posix.2. */ |
2609 | sindex = 0; | |
da713c23 CR |
2610 | /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim |
2611 | unless multibyte chars are possible. */ | |
2612 | slen = (MB_CUR_MAX > 1) ? strlen (s) : 1; | |
e6598ba4 | 2613 | current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags); |
726f6388 JA |
2614 | |
2615 | /* Set ENDPTR to the first character after the end of the word. */ | |
2616 | if (endptr) | |
2617 | *endptr = s + sindex; | |
2618 | ||
28ef6c31 JA |
2619 | /* Note whether or not the separator is IFS whitespace, used later. */ |
2620 | whitesep = s[sindex] && spctabnl (s[sindex]); | |
2621 | ||
726f6388 JA |
2622 | /* Move past the current separator character. */ |
2623 | if (s[sindex]) | |
633e5c6d CR |
2624 | { |
2625 | DECLARE_MBSTATE; | |
633e5c6d CR |
2626 | ADVANCE_CHAR (s, slen, sindex); |
2627 | } | |
726f6388 JA |
2628 | |
2629 | /* Now skip sequences of space, tab, or newline characters if they are | |
2630 | in the list of separators. */ | |
7117c2d2 | 2631 | while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex])) |
726f6388 JA |
2632 | sindex++; |
2633 | ||
28ef6c31 JA |
2634 | /* If the first separator was IFS whitespace and the current character is |
2635 | a non-whitespace IFS character, it should be part of the current field | |
2636 | delimiter, not a separate delimiter that would result in an empty field. | |
2637 | Look at POSIX.2, 3.6.5, (3)(b). */ | |
7117c2d2 | 2638 | if (s[sindex] && whitesep && isifs (s[sindex]) && !spctabnl (s[sindex])) |
9d2b70f0 CR |
2639 | { |
2640 | sindex++; | |
2641 | /* An IFS character that is not IFS white space, along with any adjacent | |
2642 | IFS white space, shall delimit a field. */ | |
2643 | while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex])) | |
2644 | sindex++; | |
2645 | } | |
28ef6c31 | 2646 | |
726f6388 JA |
2647 | /* Update STRING to point to the next field. */ |
2648 | *stringp = s + sindex; | |
2649 | return (current_word); | |
2650 | } | |
2651 | ||
2652 | /* Remove IFS white space at the end of STRING. Start at the end | |
2653 | of the string and walk backwards until the beginning of the string | |
2654 | or we find a character that's not IFS white space and not CTLESC. | |
2655 | Only let CTLESC escape a white space character if SAW_ESCAPE is | |
2656 | non-zero. */ | |
2657 | char * | |
2658 | strip_trailing_ifs_whitespace (string, separators, saw_escape) | |
2659 | char *string, *separators; | |
2660 | int saw_escape; | |
2661 | { | |
2662 | char *s; | |
ccc6cda3 | 2663 | |
726f6388 | 2664 | s = string + STRLEN (string) - 1; |
7117c2d2 | 2665 | while (s > string && ((spctabnl (*s) && isifs (*s)) || |
726f6388 JA |
2666 | (saw_escape && *s == CTLESC && spctabnl (s[1])))) |
2667 | s--; | |
2668 | *++s = '\0'; | |
2669 | return string; | |
2670 | } | |
2671 | ||
bb70624e JA |
2672 | #if 0 |
2673 | /* UNUSED */ | |
2674 | /* Split STRING into words at whitespace. Obeys shell-style quoting with | |
2675 | backslashes, single and double quotes. */ | |
ccc6cda3 JA |
2676 | WORD_LIST * |
2677 | list_string_with_quotes (string) | |
2678 | char *string; | |
2679 | { | |
2680 | WORD_LIST *list; | |
2681 | char *token, *s; | |
7117c2d2 | 2682 | size_t s_len; |
ccc6cda3 JA |
2683 | int c, i, tokstart, len; |
2684 | ||
2685 | for (s = string; s && *s && spctabnl (*s); s++) | |
2686 | ; | |
2687 | if (s == 0 || *s == 0) | |
2688 | return ((WORD_LIST *)NULL); | |
2689 | ||
7117c2d2 | 2690 | s_len = strlen (s); |
ccc6cda3 JA |
2691 | tokstart = i = 0; |
2692 | list = (WORD_LIST *)NULL; | |
2693 | while (1) | |
2694 | { | |
2695 | c = s[i]; | |
2696 | if (c == '\\') | |
2697 | { | |
2698 | i++; | |
2699 | if (s[i]) | |
2700 | i++; | |
2701 | } | |
2702 | else if (c == '\'') | |
7117c2d2 | 2703 | i = skip_single_quoted (s, s_len, ++i); |
ccc6cda3 | 2704 | else if (c == '"') |
7117c2d2 | 2705 | i = skip_double_quoted (s, s_len, ++i); |
ccc6cda3 JA |
2706 | else if (c == 0 || spctabnl (c)) |
2707 | { | |
2708 | /* We have found the end of a token. Make a word out of it and | |
2709 | add it to the word list. */ | |
bb70624e | 2710 | token = substring (s, tokstart, i); |
cce855bc | 2711 | list = add_string_to_list (token, list); |
ccc6cda3 JA |
2712 | free (token); |
2713 | while (spctabnl (s[i])) | |
2714 | i++; | |
2715 | if (s[i]) | |
2716 | tokstart = i; | |
2717 | else | |
2718 | break; | |
2719 | } | |
2720 | else | |
2721 | i++; /* normal character */ | |
2722 | } | |
2723 | return (REVERSE_LIST (list, WORD_LIST *)); | |
2724 | } | |
bb70624e | 2725 | #endif |
d166f048 | 2726 | |
cce855bc JA |
2727 | /********************************************************/ |
2728 | /* */ | |
2729 | /* Functions to perform assignment statements */ | |
2730 | /* */ | |
2731 | /********************************************************/ | |
d166f048 | 2732 | |
43df7bbb CR |
2733 | #if defined (ARRAY_VARS) |
2734 | static SHELL_VAR * | |
d11b8b46 | 2735 | do_compound_assignment (name, value, flags) |
43df7bbb | 2736 | char *name, *value; |
d11b8b46 | 2737 | int flags; |
43df7bbb CR |
2738 | { |
2739 | SHELL_VAR *v; | |
36eb585c | 2740 | int mklocal, mkassoc, mkglobal; |
d3ad40de | 2741 | WORD_LIST *list; |
d11b8b46 CR |
2742 | |
2743 | mklocal = flags & ASS_MKLOCAL; | |
09767ff0 | 2744 | mkassoc = flags & ASS_MKASSOC; |
36eb585c | 2745 | mkglobal = flags & ASS_MKGLOBAL; |
43df7bbb CR |
2746 | |
2747 | if (mklocal && variable_context) | |
2748 | { | |
2749 | v = find_variable (name); | |
fdf670ea | 2750 | list = expand_compound_array_assignment (v, value, flags); |
09767ff0 CR |
2751 | if (mkassoc) |
2752 | v = make_local_assoc_variable (name); | |
2753 | else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context) | |
7f947b68 | 2754 | v = make_local_array_variable (name, 0); |
73a146be CR |
2755 | if (v) |
2756 | assign_compound_array_list (v, list, flags); | |
43df7bbb | 2757 | } |
36eb585c CR |
2758 | /* In a function but forcing assignment in global context */ |
2759 | else if (mkglobal && variable_context) | |
2760 | { | |
2761 | v = find_global_variable (name); | |
2762 | list = expand_compound_array_assignment (v, value, flags); | |
2763 | if (v == 0 && mkassoc) | |
2764 | v = make_new_assoc_variable (name); | |
2765 | else if (v && mkassoc && assoc_p (v) == 0) | |
2766 | v = convert_var_to_assoc (v); | |
2767 | else if (v == 0) | |
2768 | v = make_new_array_variable (name); | |
1a81420a | 2769 | else if (v && mkassoc == 0 && array_p (v) == 0) |
36eb585c | 2770 | v = convert_var_to_array (v); |
73a146be CR |
2771 | if (v) |
2772 | assign_compound_array_list (v, list, flags); | |
36eb585c | 2773 | } |
43df7bbb | 2774 | else |
d11b8b46 | 2775 | v = assign_array_from_string (name, value, flags); |
43df7bbb CR |
2776 | |
2777 | return (v); | |
2778 | } | |
2779 | #endif | |
2780 | ||
726f6388 JA |
2781 | /* Given STRING, an assignment string, get the value of the right side |
2782 | of the `=', and bind it to the left side. If EXPAND is true, then | |
2783 | perform parameter expansion, command substitution, and arithmetic | |
2784 | expansion on the right-hand side. Perform tilde expansion in any | |
2785 | case. Do not perform word splitting on the result of expansion. */ | |
2786 | static int | |
43df7bbb CR |
2787 | do_assignment_internal (word, expand) |
2788 | const WORD_DESC *word; | |
726f6388 JA |
2789 | int expand; |
2790 | { | |
3d8cce26 CR |
2791 | int offset, appendop, assign_list, aflags, retval; |
2792 | char *name, *value, *temp; | |
ccc6cda3 JA |
2793 | SHELL_VAR *entry; |
2794 | #if defined (ARRAY_VARS) | |
2795 | char *t; | |
1d7ecd77 | 2796 | int ni; |
ccc6cda3 | 2797 | #endif |
43df7bbb | 2798 | const char *string; |
ccc6cda3 | 2799 | |
43df7bbb CR |
2800 | if (word == 0 || word->word == 0) |
2801 | return 0; | |
2802 | ||
d11b8b46 | 2803 | appendop = assign_list = aflags = 0; |
43df7bbb | 2804 | string = word->word; |
5e13499c | 2805 | offset = assignment (string, 0); |
ccc6cda3 JA |
2806 | name = savestring (string); |
2807 | value = (char *)NULL; | |
726f6388 JA |
2808 | |
2809 | if (name[offset] == '=') | |
2810 | { | |
d11b8b46 CR |
2811 | if (name[offset - 1] == '+') |
2812 | { | |
2813 | appendop = 1; | |
2814 | name[offset - 1] = '\0'; | |
2815 | } | |
2816 | ||
2817 | name[offset] = 0; /* might need this set later */ | |
726f6388 JA |
2818 | temp = name + offset + 1; |
2819 | ||
ccc6cda3 | 2820 | #if defined (ARRAY_VARS) |
43df7bbb | 2821 | if (expand && (word->flags & W_COMPASSIGN)) |
726f6388 | 2822 | { |
ccc6cda3 | 2823 | assign_list = ni = 1; |
43df7bbb | 2824 | value = extract_array_assignment_list (temp, &ni); |
ccc6cda3 JA |
2825 | } |
2826 | else | |
2827 | #endif | |
ccc6cda3 | 2828 | if (expand && temp[0]) |
22e63b05 | 2829 | value = expand_string_if_necessary (temp, 0, expand_string_assignment); |
726f6388 JA |
2830 | else |
2831 | value = savestring (temp); | |
2832 | } | |
2833 | ||
2834 | if (value == 0) | |
d166f048 | 2835 | { |
f73dda09 | 2836 | value = (char *)xmalloc (1); |
d166f048 JA |
2837 | value[0] = '\0'; |
2838 | } | |
726f6388 | 2839 | |
726f6388 | 2840 | if (echo_command_at_execute) |
d11b8b46 CR |
2841 | { |
2842 | if (appendop) | |
2843 | name[offset - 1] = '+'; | |
2844 | xtrace_print_assignment (name, value, assign_list, 1); | |
2845 | if (appendop) | |
2846 | name[offset - 1] = '\0'; | |
2847 | } | |
726f6388 | 2848 | |
d166f048 | 2849 | #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0) |
ccc6cda3 | 2850 | |
d11b8b46 CR |
2851 | if (appendop) |
2852 | aflags |= ASS_APPEND; | |
2853 | ||
ccc6cda3 | 2854 | #if defined (ARRAY_VARS) |
d0ca3503 | 2855 | if (t = mbschr (name, '[')) /*]*/ |
ccc6cda3 JA |
2856 | { |
2857 | if (assign_list) | |
2858 | { | |
5e13499c | 2859 | report_error (_("%s: cannot assign list to array member"), name); |
ccc6cda3 JA |
2860 | ASSIGN_RETURN (0); |
2861 | } | |
d11b8b46 | 2862 | entry = assign_array_element (name, value, aflags); |
ccc6cda3 | 2863 | if (entry == 0) |
28ef6c31 | 2864 | ASSIGN_RETURN (0); |
ccc6cda3 JA |
2865 | } |
2866 | else if (assign_list) | |
d11b8b46 | 2867 | { |
ec860d76 | 2868 | if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL) == 0) |
d11b8b46 | 2869 | aflags |= ASS_MKLOCAL; |
36eb585c CR |
2870 | if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL)) |
2871 | aflags |= ASS_MKGLOBAL; | |
09767ff0 CR |
2872 | if (word->flags & W_ASSIGNASSOC) |
2873 | aflags |= ASS_MKASSOC; | |
d11b8b46 CR |
2874 | entry = do_compound_assignment (name, value, aflags); |
2875 | } | |
ccc6cda3 JA |
2876 | else |
2877 | #endif /* ARRAY_VARS */ | |
d11b8b46 | 2878 | entry = bind_variable (name, value, aflags); |
ccc6cda3 | 2879 | |
726f6388 JA |
2880 | stupidly_hack_special_variables (name); |
2881 | ||
cb603128 CR |
2882 | /* Return 1 if the assignment seems to have been performed correctly. */ |
2883 | if (entry == 0 || readonly_p (entry)) | |
2884 | retval = 0; /* assignment failure */ | |
2885 | else if (noassign_p (entry)) | |
2886 | { | |
2887 | last_command_exit_value = EXECUTION_FAILURE; | |
2888 | retval = 1; /* error status, but not assignment failure */ | |
2889 | } | |
2890 | else | |
2891 | retval = 1; | |
cb603128 | 2892 | |
09767ff0 | 2893 | if (entry && retval != 0 && noassign_p (entry) == 0) |
cb603128 | 2894 | VUNSETATTR (entry, att_invisible); |
09767ff0 CR |
2895 | |
2896 | ASSIGN_RETURN (retval); | |
726f6388 JA |
2897 | } |
2898 | ||
2899 | /* Perform the assignment statement in STRING, and expand the | |
43df7bbb | 2900 | right side by doing tilde, command and parameter expansion. */ |
ccc6cda3 | 2901 | int |
726f6388 | 2902 | do_assignment (string) |
43df7bbb | 2903 | char *string; |
726f6388 | 2904 | { |
43df7bbb CR |
2905 | WORD_DESC td; |
2906 | ||
2907 | td.flags = W_ASSIGNMENT; | |
2908 | td.word = string; | |
2909 | ||
2910 | return do_assignment_internal (&td, 1); | |
2911 | } | |
2912 | ||
2913 | int | |
adc6cff5 | 2914 | do_word_assignment (word, flags) |
43df7bbb | 2915 | WORD_DESC *word; |
adc6cff5 | 2916 | int flags; |
43df7bbb CR |
2917 | { |
2918 | return do_assignment_internal (word, 1); | |
726f6388 JA |
2919 | } |
2920 | ||
2921 | /* Given STRING, an assignment string, get the value of the right side | |
43df7bbb CR |
2922 | of the `=', and bind it to the left side. Do not perform any word |
2923 | expansions on the right hand side. */ | |
ccc6cda3 | 2924 | int |
726f6388 | 2925 | do_assignment_no_expand (string) |
43df7bbb | 2926 | char *string; |
726f6388 | 2927 | { |
43df7bbb CR |
2928 | WORD_DESC td; |
2929 | ||
2930 | td.flags = W_ASSIGNMENT; | |
2931 | td.word = string; | |
2932 | ||
e225d5a9 | 2933 | return (do_assignment_internal (&td, 0)); |
726f6388 JA |
2934 | } |
2935 | ||
cce855bc JA |
2936 | /*************************************************** |
2937 | * * | |
2938 | * Functions to manage the positional parameters * | |
2939 | * * | |
2940 | ***************************************************/ | |
726f6388 JA |
2941 | |
2942 | /* Return the word list that corresponds to `$*'. */ | |
2943 | WORD_LIST * | |
2944 | list_rest_of_args () | |
2945 | { | |
e3af9370 | 2946 | register WORD_LIST *list, *args; |
726f6388 JA |
2947 | int i; |
2948 | ||
2949 | /* Break out of the loop as soon as one of the dollar variables is null. */ | |
ccc6cda3 | 2950 | for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++) |
e3af9370 | 2951 | list = make_word_list (make_bare_word (dollar_vars[i]), list); |
ccc6cda3 JA |
2952 | |
2953 | for (args = rest_of_args; args; args = args->next) | |
e3af9370 | 2954 | list = make_word_list (make_bare_word (args->word->word), list); |
726f6388 | 2955 | |
e3af9370 | 2956 | return (REVERSE_LIST (list, WORD_LIST *)); |
726f6388 JA |
2957 | } |
2958 | ||
ccc6cda3 JA |
2959 | int |
2960 | number_of_args () | |
2961 | { | |
2962 | register WORD_LIST *list; | |
2963 | int n; | |
2964 | ||
2965 | for (n = 0; n < 9 && dollar_vars[n+1]; n++) | |
2966 | ; | |
2967 | for (list = rest_of_args; list; list = list->next) | |
2968 | n++; | |
2969 | return n; | |
2970 | } | |
2971 | ||
cce855bc JA |
2972 | /* Return the value of a positional parameter. This handles values > 10. */ |
2973 | char * | |
2974 | get_dollar_var_value (ind) | |
7117c2d2 | 2975 | intmax_t ind; |
cce855bc JA |
2976 | { |
2977 | char *temp; | |
2978 | WORD_LIST *p; | |
2979 | ||
2980 | if (ind < 10) | |
2981 | temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL; | |
2982 | else /* We want something like ${11} */ | |
2983 | { | |
2984 | ind -= 10; | |
2985 | for (p = rest_of_args; p && ind--; p = p->next) | |
28ef6c31 | 2986 | ; |
cce855bc JA |
2987 | temp = p ? savestring (p->word->word) : (char *)NULL; |
2988 | } | |
2989 | return (temp); | |
2990 | } | |
2991 | ||
726f6388 JA |
2992 | /* Make a single large string out of the dollar digit variables, |
2993 | and the rest_of_args. If DOLLAR_STAR is 1, then obey the special | |
2994 | case of "$*" with respect to IFS. */ | |
2995 | char * | |
2996 | string_rest_of_args (dollar_star) | |
2997 | int dollar_star; | |
2998 | { | |
ccc6cda3 | 2999 | register WORD_LIST *list; |
726f6388 JA |
3000 | char *string; |
3001 | ||
ccc6cda3 | 3002 | list = list_rest_of_args (); |
726f6388 JA |
3003 | string = dollar_star ? string_list_dollar_star (list) : string_list (list); |
3004 | dispose_words (list); | |
3005 | return (string); | |
3006 | } | |
3007 | ||
cce855bc JA |
3008 | /* Return a string containing the positional parameters from START to |
3009 | END, inclusive. If STRING[0] == '*', we obey the rules for $*, | |
7117c2d2 JA |
3010 | which only makes a difference if QUOTED is non-zero. If QUOTED includes |
3011 | Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise | |
3012 | no quoting chars are added. */ | |
cce855bc JA |
3013 | static char * |
3014 | pos_params (string, start, end, quoted) | |
3015 | char *string; | |
3016 | int start, end, quoted; | |
726f6388 | 3017 | { |
cce855bc JA |
3018 | WORD_LIST *save, *params, *h, *t; |
3019 | char *ret; | |
3020 | int i; | |
726f6388 | 3021 | |
bb70624e JA |
3022 | /* see if we can short-circuit. if start == end, we want 0 parameters. */ |
3023 | if (start == end) | |
3024 | return ((char *)NULL); | |
3025 | ||
cce855bc | 3026 | save = params = list_rest_of_args (); |
098cbb5c | 3027 | if (save == 0 && start > 0) |
cce855bc JA |
3028 | return ((char *)NULL); |
3029 | ||
d3ad40de CR |
3030 | if (start == 0) /* handle ${@:0[:x]} specially */ |
3031 | { | |
3032 | t = make_word_list (make_word (dollar_vars[0]), params); | |
3033 | save = params = t; | |
3034 | } | |
3035 | ||
0527c903 | 3036 | for (i = start ? 1 : 0; params && i < start; i++) |
cce855bc JA |
3037 | params = params->next; |
3038 | if (params == 0) | |
b4a17eba CR |
3039 | { |
3040 | dispose_words (save); | |
3041 | return ((char *)NULL); | |
3042 | } | |
cce855bc | 3043 | for (h = t = params; params && i < end; i++) |
d166f048 | 3044 | { |
cce855bc JA |
3045 | t = params; |
3046 | params = params->next; | |
d166f048 | 3047 | } |
726f6388 | 3048 | |
cce855bc | 3049 | t->next = (WORD_LIST *)NULL; |
e33f2203 CR |
3050 | |
3051 | ret = string_list_pos_params (string[0], h, quoted); | |
3052 | ||
bb70624e JA |
3053 | if (t != params) |
3054 | t->next = params; | |
726f6388 | 3055 | |
cce855bc JA |
3056 | dispose_words (save); |
3057 | return (ret); | |
3058 | } | |
3059 | ||
3060 | /******************************************************************/ | |
3061 | /* */ | |
3062 | /* Functions to expand strings to strings or WORD_LISTs */ | |
3063 | /* */ | |
3064 | /******************************************************************/ | |
3065 | ||
3066 | #if defined (PROCESS_SUBSTITUTION) | |
22e63b05 | 3067 | #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~') |
cce855bc | 3068 | #else |
22e63b05 | 3069 | #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~') |
cce855bc JA |
3070 | #endif |
3071 | ||
3072 | /* If there are any characters in STRING that require full expansion, | |
3073 | then call FUNC to expand STRING; otherwise just perform quote | |
3074 | removal if necessary. This returns a new string. */ | |
3075 | static char * | |
f73dda09 | 3076 | expand_string_if_necessary (string, quoted, func) |
cce855bc JA |
3077 | char *string; |
3078 | int quoted; | |
f73dda09 | 3079 | EXPFUNC *func; |
cce855bc JA |
3080 | { |
3081 | WORD_LIST *list; | |
7117c2d2 | 3082 | size_t slen; |
cce855bc JA |
3083 | int i, saw_quote; |
3084 | char *ret; | |
7117c2d2 | 3085 | DECLARE_MBSTATE; |
cce855bc | 3086 | |
da713c23 CR |
3087 | /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */ |
3088 | slen = (MB_CUR_MAX > 1) ? strlen (string) : 0; | |
7117c2d2 JA |
3089 | i = saw_quote = 0; |
3090 | while (string[i]) | |
cce855bc JA |
3091 | { |
3092 | if (EXP_CHAR (string[i])) | |
3093 | break; | |
3094 | else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"') | |
3095 | saw_quote = 1; | |
7117c2d2 | 3096 | ADVANCE_CHAR (string, slen, i); |
cce855bc JA |
3097 | } |
3098 | ||
3099 | if (string[i]) | |
3100 | { | |
3101 | list = (*func) (string, quoted); | |
3102 | if (list) | |
3103 | { | |
3104 | ret = string_list (list); | |
3105 | dispose_words (list); | |
3106 | } | |
3107 | else | |
3108 | ret = (char *)NULL; | |
3109 | } | |
3110 | else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) | |
3111 | ret = string_quote_removal (string, quoted); | |
3112 | else | |
3113 | ret = savestring (string); | |
7117c2d2 | 3114 | |
cce855bc JA |
3115 | return ret; |
3116 | } | |
3117 | ||
3118 | static inline char * | |
f73dda09 | 3119 | expand_string_to_string_internal (string, quoted, func) |
cce855bc JA |
3120 | char *string; |
3121 | int quoted; | |
f73dda09 | 3122 | EXPFUNC *func; |
cce855bc JA |
3123 | { |
3124 | WORD_LIST *list; | |
3125 | char *ret; | |
3126 | ||
3127 | if (string == 0 || *string == '\0') | |
3128 | return ((char *)NULL); | |
3129 | ||
3130 | list = (*func) (string, quoted); | |
3131 | if (list) | |
3132 | { | |
3133 | ret = string_list (list); | |
3134 | dispose_words (list); | |
3135 | } | |
3136 | else | |
3137 | ret = (char *)NULL; | |
3138 | ||
3139 | return (ret); | |
3140 | } | |
3141 | ||
f73dda09 JA |
3142 | char * |
3143 | expand_string_to_string (string, quoted) | |
3144 | char *string; | |
3145 | int quoted; | |
3146 | { | |
3147 | return (expand_string_to_string_internal (string, quoted, expand_string)); | |
3148 | } | |
3149 | ||
3150 | char * | |
3151 | expand_string_unsplit_to_string (string, quoted) | |
3152 | char *string; | |
3153 | int quoted; | |
3154 | { | |
3155 | return (expand_string_to_string_internal (string, quoted, expand_string_unsplit)); | |
3156 | } | |
3157 | ||
43df7bbb CR |
3158 | char * |
3159 | expand_assignment_string_to_string (string, quoted) | |
3160 | char *string; | |
3161 | int quoted; | |
3162 | { | |
3163 | return (expand_string_to_string_internal (string, quoted, expand_string_assignment)); | |
3164 | } | |
3165 | ||
dc8fbaf9 | 3166 | char * |
d3ad40de | 3167 | expand_arith_string (string, quoted) |
dc8fbaf9 | 3168 | char *string; |
48ff5447 | 3169 | int quoted; |
dc8fbaf9 | 3170 | { |
21af69d5 CR |
3171 | WORD_DESC td; |
3172 | WORD_LIST *list, *tlist; | |
3173 | size_t slen; | |
3174 | int i, saw_quote; | |
3175 | char *ret; | |
3176 | DECLARE_MBSTATE; | |
3177 | ||
3178 | /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */ | |
3179 | slen = (MB_CUR_MAX > 1) ? strlen (string) : 0; | |
3180 | i = saw_quote = 0; | |
3181 | while (string[i]) | |
3182 | { | |
3183 | if (EXP_CHAR (string[i])) | |
3184 | break; | |
3185 | else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"') | |
3186 | saw_quote = 1; | |
3187 | ADVANCE_CHAR (string, slen, i); | |
3188 | } | |
3189 | ||
3190 | if (string[i]) | |
3191 | { | |
3192 | /* This is expanded version of expand_string_internal as it's called by | |
3193 | expand_string_leave_quoted */ | |
3194 | td.flags = W_NOPROCSUB; /* don't want process substitution */ | |
3195 | td.word = savestring (string); | |
3196 | list = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); | |
3197 | /* This takes care of the calls from expand_string_leave_quoted and | |
3198 | expand_string */ | |
3199 | if (list) | |
3200 | { | |
3201 | tlist = word_list_split (list); | |
3202 | dispose_words (list); | |
3203 | list = tlist; | |
3204 | if (list) | |
3205 | dequote_list (list); | |
3206 | } | |
3207 | /* This comes from expand_string_if_necessary */ | |
3208 | if (list) | |
3209 | { | |
3210 | ret = string_list (list); | |
3211 | dispose_words (list); | |
3212 | } | |
3213 | else | |
3214 | ret = (char *)NULL; | |
3215 | FREE (td.word); | |
3216 | } | |
3217 | else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) | |
3218 | ret = string_quote_removal (string, quoted); | |
3219 | else | |
3220 | ret = savestring (string); | |
3221 | ||
3222 | return ret; | |
dc8fbaf9 CR |
3223 | } |
3224 | ||
cce855bc JA |
3225 | #if defined (COND_COMMAND) |
3226 | /* Just remove backslashes in STRING. Returns a new string. */ | |
3227 | char * | |
3228 | remove_backslashes (string) | |
3229 | char *string; | |
3230 | { | |
3231 | char *r, *ret, *s; | |
3232 | ||
f73dda09 | 3233 | r = ret = (char *)xmalloc (strlen (string) + 1); |
cce855bc JA |
3234 | for (s = string; s && *s; ) |
3235 | { | |
3236 | if (*s == '\\') | |
28ef6c31 | 3237 | s++; |
cce855bc | 3238 | if (*s == 0) |
28ef6c31 | 3239 | break; |
cce855bc JA |
3240 | *r++ = *s++; |
3241 | } | |
3242 | *r = '\0'; | |
3243 | return ret; | |
3244 | } | |
3245 | ||
3246 | /* This needs better error handling. */ | |
3247 | /* Expand W for use as an argument to a unary or binary operator in a | |
d3ad40de | 3248 | [[...]] expression. If SPECIAL is 1, this is the rhs argument |
cce855bc | 3249 | to the != or == operator, and should be treated as a pattern. In |
d3ad40de CR |
3250 | this case, we quote the string specially for the globbing code. If |
3251 | SPECIAL is 2, this is an rhs argument for the =~ operator, and should | |
3252 | be quoted appropriately for regcomp/regexec. The caller is responsible | |
3253 | for removing the backslashes if the unquoted word is needed later. */ | |
cce855bc JA |
3254 | char * |
3255 | cond_expand_word (w, special) | |
3256 | WORD_DESC *w; | |
3257 | int special; | |
3258 | { | |
3259 | char *r, *p; | |
3260 | WORD_LIST *l; | |
d3ad40de | 3261 | int qflags; |
cce855bc JA |
3262 | |
3263 | if (w->word == 0 || w->word[0] == '\0') | |
3264 | return ((char *)NULL); | |
3265 | ||
0d9b018b | 3266 | expand_no_split_dollar_star = 1; |
e1e48bba | 3267 | w->flags |= W_NOSPLIT2; |
b72432fd | 3268 | l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0); |
0d9b018b | 3269 | expand_no_split_dollar_star = 0; |
cce855bc JA |
3270 | if (l) |
3271 | { | |
4a2c75c6 | 3272 | if (special == 0) /* LHS */ |
cce855bc JA |
3273 | { |
3274 | dequote_list (l); | |
3275 | r = string_list (l); | |
3276 | } | |
3277 | else | |
28ef6c31 | 3278 | { |
4a2c75c6 CR |
3279 | /* Need to figure out whether or not we should call dequote_escapes |
3280 | or a new dequote_ctlnul function here, and under what | |
3281 | circumstances. */ | |
d3ad40de CR |
3282 | qflags = QGLOB_CVTNULL; |
3283 | if (special == 2) | |
3284 | qflags |= QGLOB_REGEXP; | |
28ef6c31 | 3285 | p = string_list (l); |
d3ad40de | 3286 | r = quote_string_for_globbing (p, qflags); |
28ef6c31 JA |
3287 | free (p); |
3288 | } | |
cce855bc JA |
3289 | dispose_words (l); |
3290 | } | |
3291 | else | |
3292 | r = (char *)NULL; | |
3293 | ||
3294 | return r; | |
3295 | } | |
3296 | #endif | |
3297 | ||
3298 | /* Call expand_word_internal to expand W and handle error returns. | |
3299 | A convenience function for functions that don't want to handle | |
3300 | any errors or free any memory before aborting. */ | |
3301 | static WORD_LIST * | |
b72432fd | 3302 | call_expand_word_internal (w, q, i, c, e) |
cce855bc | 3303 | WORD_DESC *w; |
b72432fd | 3304 | int q, i, *c, *e; |
cce855bc JA |
3305 | { |
3306 | WORD_LIST *result; | |
3307 | ||
b72432fd | 3308 | result = expand_word_internal (w, q, i, c, e); |
bb70624e | 3309 | if (result == &expand_word_error || result == &expand_word_fatal) |
cce855bc JA |
3310 | { |
3311 | /* By convention, each time this error is returned, w->word has | |
bb70624e JA |
3312 | already been freed (it sometimes may not be in the fatal case, |
3313 | but that doesn't result in a memory leak because we're going | |
3314 | to exit in most cases). */ | |
cce855bc | 3315 | w->word = (char *)NULL; |
28ef6c31 | 3316 | last_command_exit_value = EXECUTION_FAILURE; |
d3a24ed2 | 3317 | exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF); |
cce855bc | 3318 | /* NOTREACHED */ |
c7e43312 | 3319 | return (NULL); |
cce855bc | 3320 | } |
cce855bc JA |
3321 | else |
3322 | return (result); | |
3323 | } | |
3324 | ||
3325 | /* Perform parameter expansion, command substitution, and arithmetic | |
9c2db999 CR |
3326 | expansion on STRING, as if it were a word. Leave the result quoted. |
3327 | Since this does not perform word splitting, it leaves quoted nulls | |
3328 | in the result. */ | |
cce855bc JA |
3329 | static WORD_LIST * |
3330 | expand_string_internal (string, quoted) | |
3331 | char *string; | |
3332 | int quoted; | |
3333 | { | |
3334 | WORD_DESC td; | |
3335 | WORD_LIST *tresult; | |
3336 | ||
3337 | if (string == 0 || *string == 0) | |
3338 | return ((WORD_LIST *)NULL); | |
3339 | ||
28ef6c31 JA |
3340 | td.flags = 0; |
3341 | td.word = savestring (string); | |
3342 | ||
b72432fd | 3343 | tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); |
28ef6c31 JA |
3344 | |
3345 | FREE (td.word); | |
cce855bc | 3346 | return (tresult); |
726f6388 JA |
3347 | } |
3348 | ||
3349 | /* Expand STRING by performing parameter expansion, command substitution, | |
3350 | and arithmetic expansion. Dequote the resulting WORD_LIST before | |
3351 | returning it, but do not perform word splitting. The call to | |
3352 | remove_quoted_nulls () is in here because word splitting normally | |
3353 | takes care of quote removal. */ | |
3354 | WORD_LIST * | |
3355 | expand_string_unsplit (string, quoted) | |
3356 | char *string; | |
3357 | int quoted; | |
3358 | { | |
3359 | WORD_LIST *value; | |
3360 | ||
28ef6c31 | 3361 | if (string == 0 || *string == '\0') |
726f6388 JA |
3362 | return ((WORD_LIST *)NULL); |
3363 | ||
28ef6c31 | 3364 | expand_no_split_dollar_star = 1; |
726f6388 | 3365 | value = expand_string_internal (string, quoted); |
28ef6c31 JA |
3366 | expand_no_split_dollar_star = 0; |
3367 | ||
726f6388 JA |
3368 | if (value) |
3369 | { | |
3370 | if (value->word) | |
227f982e CR |
3371 | { |
3372 | remove_quoted_nulls (value->word->word); | |
3373 | value->word->flags &= ~W_HASQUOTEDNULL; | |
3374 | } | |
726f6388 JA |
3375 | dequote_list (value); |
3376 | } | |
3377 | return (value); | |
3378 | } | |
3379 | ||
22e63b05 CR |
3380 | /* Expand the rhs of an assignment statement */ |
3381 | WORD_LIST * | |
3382 | expand_string_assignment (string, quoted) | |
3383 | char *string; | |
3384 | int quoted; | |
3385 | { | |
3386 | WORD_DESC td; | |
3387 | WORD_LIST *value; | |
3388 | ||
3389 | if (string == 0 || *string == '\0') | |
3390 | return ((WORD_LIST *)NULL); | |
3391 | ||
3392 | expand_no_split_dollar_star = 1; | |
3393 | ||
3394 | td.flags = W_ASSIGNRHS; | |
3395 | td.word = savestring (string); | |
3396 | value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); | |
3397 | FREE (td.word); | |
3398 | ||
3399 | expand_no_split_dollar_star = 0; | |
3400 | ||
3401 | if (value) | |
3402 | { | |
3403 | if (value->word) | |
227f982e CR |
3404 | { |
3405 | remove_quoted_nulls (value->word->word); | |
3406 | value->word->flags &= ~W_HASQUOTEDNULL; | |
3407 | } | |
22e63b05 CR |
3408 | dequote_list (value); |
3409 | } | |
3410 | return (value); | |
3411 | } | |
3412 | ||
bb70624e JA |
3413 | |
3414 | /* Expand one of the PS? prompt strings. This is a sort of combination of | |
3415 | expand_string_unsplit and expand_string_internal, but returns the | |
3416 | passed string when an error occurs. Might want to trap other calls | |
3417 | to jump_to_top_level here so we don't endlessly loop. */ | |
3418 | WORD_LIST * | |
4d8d005b | 3419 | expand_prompt_string (string, quoted, wflags) |
bb70624e JA |
3420 | char *string; |
3421 | int quoted; | |
4d8d005b | 3422 | int wflags; |
bb70624e JA |
3423 | { |
3424 | WORD_LIST *value; | |
3425 | WORD_DESC td; | |
3426 | ||
3427 | if (string == 0 || *string == 0) | |
3428 | return ((WORD_LIST *)NULL); | |
3429 | ||
4d8d005b | 3430 | td.flags = wflags; |
bb70624e | 3431 | td.word = savestring (string); |
28ef6c31 JA |
3432 | |
3433 | no_longjmp_on_fatal_error = 1; | |
bb70624e | 3434 | value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); |
28ef6c31 JA |
3435 | no_longjmp_on_fatal_error = 0; |
3436 | ||
bb70624e JA |
3437 | if (value == &expand_word_error || value == &expand_word_fatal) |
3438 | { | |
3439 | value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL); | |
3440 | return value; | |
3441 | } | |
3442 | FREE (td.word); | |
3443 | if (value) | |
3444 | { | |
3445 | if (value->word) | |
227f982e CR |
3446 | { |
3447 | remove_quoted_nulls (value->word->word); | |
3448 | value->word->flags &= ~W_HASQUOTEDNULL; | |
3449 | } | |
bb70624e JA |
3450 | dequote_list (value); |
3451 | } | |
3452 | return (value); | |
3453 | } | |
3454 | ||
726f6388 JA |
3455 | /* Expand STRING just as if you were expanding a word, but do not dequote |
3456 | the resultant WORD_LIST. This is called only from within this file, | |
3457 | and is used to correctly preserve quoted characters when expanding | |
3458 | things like ${1+"$@"}. This does parameter expansion, command | |
b72432fd | 3459 | substitution, arithmetic expansion, and word splitting. */ |
726f6388 JA |
3460 | static WORD_LIST * |
3461 | expand_string_leave_quoted (string, quoted) | |
3462 | char *string; | |
3463 | int quoted; | |
3464 | { | |
3465 | WORD_LIST *tlist; | |
3466 | WORD_LIST *tresult; | |
3467 | ||
ccc6cda3 | 3468 | if (string == 0 || *string == '\0') |
726f6388 JA |
3469 | return ((WORD_LIST *)NULL); |
3470 | ||
3471 | tlist = expand_string_internal (string, quoted); | |
3472 | ||
3473 | if (tlist) | |
3474 | { | |
3475 | tresult = word_list_split (tlist); | |
3476 | dispose_words (tlist); | |
3477 | return (tresult); | |
3478 | } | |
3479 | return ((WORD_LIST *)NULL); | |
3480 | } | |
3481 | ||
ccc6cda3 JA |
3482 | /* This does not perform word splitting or dequote the WORD_LIST |
3483 | it returns. */ | |
3484 | static WORD_LIST * | |
3485 | expand_string_for_rhs (string, quoted, dollar_at_p, has_dollar_at) | |
3486 | char *string; | |
3487 | int quoted, *dollar_at_p, *has_dollar_at; | |
3488 | { | |
3489 | WORD_DESC td; | |
3490 | WORD_LIST *tresult; | |
3491 | ||
3492 | if (string == 0 || *string == '\0') | |
3493 | return (WORD_LIST *)NULL; | |
3494 | ||
9c2db999 | 3495 | td.flags = W_NOSPLIT2; /* no splitting, remove "" and '' */ |
ccc6cda3 | 3496 | td.word = string; |
b72432fd | 3497 | tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, has_dollar_at); |
ccc6cda3 JA |
3498 | return (tresult); |
3499 | } | |
3500 | ||
726f6388 JA |
3501 | /* Expand STRING just as if you were expanding a word. This also returns |
3502 | a list of words. Note that filename globbing is *NOT* done for word | |
3503 | or string expansion, just when the shell is expanding a command. This | |
3504 | does parameter expansion, command substitution, arithmetic expansion, | |
3505 | and word splitting. Dequote the resultant WORD_LIST before returning. */ | |
3506 | WORD_LIST * | |
3507 | expand_string (string, quoted) | |
3508 | char *string; | |
3509 | int quoted; | |
3510 | { | |
3511 | WORD_LIST *result; | |
3512 | ||
28ef6c31 | 3513 | if (string == 0 || *string == '\0') |
726f6388 JA |
3514 | return ((WORD_LIST *)NULL); |
3515 | ||
3516 | result = expand_string_leave_quoted (string, quoted); | |
ccc6cda3 | 3517 | return (result ? dequote_list (result) : result); |
726f6388 JA |
3518 | } |
3519 | ||
3520 | /*************************************************** | |
3521 | * * | |
3522 | * Functions to handle quoting chars * | |
3523 | * * | |
3524 | ***************************************************/ | |
3525 | ||
cce855bc JA |
3526 | /* Conventions: |
3527 | ||
3528 | A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string. | |
3529 | The parser passes CTLNUL as CTLESC CTLNUL. */ | |
3530 | ||
cce855bc JA |
3531 | /* Quote escape characters in string s, but no other characters. This is |
3532 | used to protect CTLESC and CTLNUL in variable values from the rest of | |
e6598ba4 CR |
3533 | the word expansion process after the variable is expanded (word splitting |
3534 | and filename generation). If IFS is null, we quote spaces as well, just | |
3535 | in case we split on spaces later (in the case of unquoted $@, we will | |
3536 | eventually attempt to split the entire word on spaces). Corresponding | |
3537 | code exists in dequote_escapes. Even if we don't end up splitting on | |
3538 | spaces, quoting spaces is not a problem. This should never be called on | |
3539 | a string that is quoted with single or double quotes or part of a here | |
3540 | document (effectively double-quoted). */ | |
f73dda09 | 3541 | char * |
cce855bc JA |
3542 | quote_escapes (string) |
3543 | char *string; | |
3544 | { | |
3545 | register char *s, *t; | |
7117c2d2 JA |
3546 | size_t slen; |
3547 | char *result, *send; | |
e6598ba4 | 3548 | int quote_spaces, skip_ctlesc, skip_ctlnul; |
7117c2d2 | 3549 | DECLARE_MBSTATE; |
cce855bc | 3550 | |
7117c2d2 JA |
3551 | slen = strlen (string); |
3552 | send = string + slen; | |
3553 | ||
d3ad40de | 3554 | quote_spaces = (ifs_value && *ifs_value == 0); |
e6598ba4 CR |
3555 | |
3556 | for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++) | |
3557 | skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL; | |
3558 | ||
7117c2d2 JA |
3559 | t = result = (char *)xmalloc ((slen * 2) + 1); |
3560 | s = string; | |
3561 | ||
3562 | while (*s) | |
cce855bc | 3563 | { |
e6598ba4 | 3564 | if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' ')) |
cce855bc | 3565 | *t++ = CTLESC; |
7117c2d2 | 3566 | COPY_CHAR_P (t, s, send); |
cce855bc JA |
3567 | } |
3568 | *t = '\0'; | |
4a2c75c6 | 3569 | |
cce855bc JA |
3570 | return (result); |
3571 | } | |
3572 | ||
3573 | static WORD_LIST * | |
3574 | list_quote_escapes (list) | |
3575 | WORD_LIST *list; | |
3576 | { | |
3577 | register WORD_LIST *w; | |
3578 | char *t; | |
3579 | ||
3580 | for (w = list; w; w = w->next) | |
3581 | { | |
3582 | t = w->word->word; | |
3583 | w->word->word = quote_escapes (t); | |
3584 | free (t); | |
3585 | } | |
3586 | return list; | |
3587 | } | |
3588 | ||
7117c2d2 JA |
3589 | /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL. |
3590 | ||
3591 | The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL. | |
3592 | This is necessary to make unquoted CTLESC and CTLNUL characters in the | |
3593 | data stream pass through properly. | |
3594 | ||
3595 | We need to remove doubled CTLESC characters inside quoted strings before | |
3596 | quoting the entire string, so we do not double the number of CTLESC | |
3597 | characters. | |
3598 | ||
3599 | Also used by parts of the pattern substitution code. */ | |
09767ff0 | 3600 | char * |
cce855bc JA |
3601 | dequote_escapes (string) |
3602 | char *string; | |
3603 | { | |
e6598ba4 | 3604 | register char *s, *t, *s1; |
7117c2d2 JA |
3605 | size_t slen; |
3606 | char *result, *send; | |
d3ad40de | 3607 | int quote_spaces; |
7117c2d2 | 3608 | DECLARE_MBSTATE; |
cce855bc | 3609 | |
7117c2d2 JA |
3610 | if (string == 0) |
3611 | return string; | |
3612 | ||
3613 | slen = strlen (string); | |
3614 | send = string + slen; | |
3615 | ||
3616 | t = result = (char *)xmalloc (slen + 1); | |
7117c2d2 JA |
3617 | |
3618 | if (strchr (string, CTLESC) == 0) | |
e6598ba4 | 3619 | return (strcpy (result, string)); |
7117c2d2 | 3620 | |
d3ad40de | 3621 | quote_spaces = (ifs_value && *ifs_value == 0); |
e6598ba4 CR |
3622 | |
3623 | s = string; | |
7117c2d2 | 3624 | while (*s) |
cce855bc | 3625 | { |
d3ad40de | 3626 | if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' '))) |
cce855bc JA |
3627 | { |
3628 | s++; | |
3629 | if (*s == '\0') | |
3630 | break; | |
3631 | } | |
7117c2d2 | 3632 | COPY_CHAR_P (t, s, send); |
cce855bc JA |
3633 | } |
3634 | *t = '\0'; | |
4a2c75c6 | 3635 | |
cce855bc JA |
3636 | return result; |
3637 | } | |
726f6388 | 3638 | |
4a2c75c6 CR |
3639 | static WORD_LIST * |
3640 | list_dequote_escapes (list) | |
3641 | WORD_LIST *list; | |
3642 | { | |
3643 | register WORD_LIST *w; | |
3644 | char *t; | |
3645 | ||
3646 | for (w = list; w; w = w->next) | |
3647 | { | |
3648 | t = w->word->word; | |
3649 | w->word->word = dequote_escapes (t); | |
3650 | free (t); | |
3651 | } | |
3652 | return list; | |
3653 | } | |
3654 | ||
d3ad40de CR |
3655 | /* Return a new string with the quoted representation of character C. |
3656 | This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be | |
3657 | set in any resultant WORD_DESC where this value is the word. */ | |
726f6388 JA |
3658 | static char * |
3659 | make_quoted_char (c) | |
3660 | int c; | |
3661 | { | |
3662 | char *temp; | |
3663 | ||
f73dda09 | 3664 | temp = (char *)xmalloc (3); |
726f6388 JA |
3665 | if (c == 0) |
3666 | { | |
3667 | temp[0] = CTLNUL; | |
3668 | temp[1] = '\0'; | |
3669 | } | |
3670 | else | |
3671 | { | |
3672 | temp[0] = CTLESC; | |
3673 | temp[1] = c; | |
3674 | temp[2] = '\0'; | |
3675 | } | |
3676 | return (temp); | |
3677 | } | |
3678 | ||
d3ad40de CR |
3679 | /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so |
3680 | the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where | |
3681 | this value is the word. */ | |
ccc6cda3 | 3682 | char * |
726f6388 JA |
3683 | quote_string (string) |
3684 | char *string; | |
3685 | { | |
ccc6cda3 | 3686 | register char *t; |
7117c2d2 JA |
3687 | size_t slen; |
3688 | char *result, *send; | |
726f6388 | 3689 | |
ccc6cda3 | 3690 | if (*string == 0) |
726f6388 | 3691 | { |
f73dda09 | 3692 | result = (char *)xmalloc (2); |
726f6388 JA |
3693 | result[0] = CTLNUL; |
3694 | result[1] = '\0'; | |
3695 | } | |
3696 | else | |
3697 | { | |
7117c2d2 | 3698 | DECLARE_MBSTATE; |
726f6388 | 3699 | |
7117c2d2 JA |
3700 | slen = strlen (string); |
3701 | send = string + slen; | |
3702 | ||
3703 | result = (char *)xmalloc ((slen * 2) + 1); | |
3704 | ||
3705 | for (t = result; string < send; ) | |
726f6388 JA |
3706 | { |
3707 | *t++ = CTLESC; | |
7117c2d2 | 3708 | COPY_CHAR_P (t, string, send); |
726f6388 JA |
3709 | } |
3710 | *t = '\0'; | |
3711 | } | |
3712 | return (result); | |
3713 | } | |
3714 | ||
d3ad40de | 3715 | /* De-quote quoted characters in STRING. */ |
726f6388 JA |
3716 | char * |
3717 | dequote_string (string) | |
3718 | char *string; | |
3719 | { | |
7117c2d2 JA |
3720 | register char *s, *t; |
3721 | size_t slen; | |
3722 | char *result, *send; | |
3723 | DECLARE_MBSTATE; | |
726f6388 | 3724 | |
7117c2d2 JA |
3725 | slen = strlen (string); |
3726 | ||
3727 | t = result = (char *)xmalloc (slen + 1); | |
726f6388 JA |
3728 | |
3729 | if (QUOTED_NULL (string)) | |
3730 | { | |
3731 | result[0] = '\0'; | |
3732 | return (result); | |
3733 | } | |
3734 | ||
3735 | /* If no character in the string can be quoted, don't bother examining | |
3736 | each character. Just return a copy of the string passed to us. */ | |
7117c2d2 JA |
3737 | if (strchr (string, CTLESC) == NULL) |
3738 | return (strcpy (result, string)); | |
726f6388 | 3739 | |
7117c2d2 JA |
3740 | send = string + slen; |
3741 | s = string; | |
3742 | while (*s) | |
726f6388 | 3743 | { |
7117c2d2 | 3744 | if (*s == CTLESC) |
726f6388 | 3745 | { |
7117c2d2 JA |
3746 | s++; |
3747 | if (*s == '\0') | |
726f6388 JA |
3748 | break; |
3749 | } | |
7117c2d2 | 3750 | COPY_CHAR_P (t, s, send); |
726f6388 JA |
3751 | } |
3752 | ||
3753 | *t = '\0'; | |
3754 | return (result); | |
3755 | } | |
3756 | ||
3757 | /* Quote the entire WORD_LIST list. */ | |
ccc6cda3 | 3758 | static WORD_LIST * |
726f6388 JA |
3759 | quote_list (list) |
3760 | WORD_LIST *list; | |
3761 | { | |
3762 | register WORD_LIST *w; | |
ccc6cda3 | 3763 | char *t; |
726f6388 JA |
3764 | |
3765 | for (w = list; w; w = w->next) | |
3766 | { | |
ccc6cda3 | 3767 | t = w->word->word; |
726f6388 | 3768 | w->word->word = quote_string (t); |
e33f2203 CR |
3769 | if (*t == 0) |
3770 | w->word->flags |= W_HASQUOTEDNULL; /* XXX - turn on W_HASQUOTEDNULL here? */ | |
ccc6cda3 | 3771 | w->word->flags |= W_QUOTED; |
e33f2203 | 3772 | free (t); |
726f6388 | 3773 | } |
ccc6cda3 | 3774 | return list; |
726f6388 JA |
3775 | } |
3776 | ||
d3ad40de CR |
3777 | /* De-quote quoted characters in each word in LIST. */ |
3778 | WORD_LIST * | |
7117c2d2 JA |
3779 | dequote_list (list) |
3780 | WORD_LIST *list; | |
3781 | { | |
3782 | register char *s; | |
3783 | register WORD_LIST *tlist; | |
3784 | ||
3785 | for (tlist = list; tlist; tlist = tlist->next) | |
3786 | { | |
3787 | s = dequote_string (tlist->word->word); | |
e33f2203 CR |
3788 | if (QUOTED_NULL (tlist->word->word)) |
3789 | tlist->word->flags &= ~W_HASQUOTEDNULL; | |
7117c2d2 JA |
3790 | free (tlist->word->word); |
3791 | tlist->word->word = s; | |
3792 | } | |
3793 | return list; | |
3794 | } | |
3795 | ||
3796 | /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed | |
3797 | string. */ | |
b0c16657 | 3798 | char * |
7117c2d2 JA |
3799 | remove_quoted_escapes (string) |
3800 | char *string; | |
3801 | { | |
3802 | char *t; | |
3803 | ||
3804 | if (string) | |
3805 | { | |
3806 | t = dequote_escapes (string); | |
3807 | strcpy (string, t); | |
3808 | free (t); | |
3809 | } | |
3810 | ||
3811 | return (string); | |
3812 | } | |
3813 | ||
cce855bc JA |
3814 | /* Perform quoted null character removal on STRING. We don't allow any |
3815 | quoted null characters in the middle or at the ends of strings because | |
3816 | of how expand_word_internal works. remove_quoted_nulls () turns | |
3817 | STRING into an empty string iff it only consists of a quoted null, | |
3818 | and removes all unquoted CTLNUL characters. */ | |
09767ff0 | 3819 | char * |
cce855bc JA |
3820 | remove_quoted_nulls (string) |
3821 | char *string; | |
3822 | { | |
7117c2d2 JA |
3823 | register size_t slen; |
3824 | register int i, j, prev_i; | |
3825 | DECLARE_MBSTATE; | |
3826 | ||
3827 | if (strchr (string, CTLNUL) == 0) /* XXX */ | |
3828 | return string; /* XXX */ | |
3829 | ||
3830 | slen = strlen (string); | |
3831 | i = j = 0; | |
3832 | ||
3833 | while (i < slen) | |
3834 | { | |
3835 | if (string[i] == CTLESC) | |
704a1a2a | 3836 | { |
d3a24ed2 CR |
3837 | /* Old code had j++, but we cannot assume that i == j at this |
3838 | point -- what if a CTLNUL has already been removed from the | |
3839 | string? We don't want to drop the CTLESC or recopy characters | |
3840 | that we've already copied down. */ | |
3841 | i++; string[j++] = CTLESC; | |
704a1a2a CR |
3842 | if (i == slen) |
3843 | break; | |
3844 | } | |
7117c2d2 | 3845 | else if (string[i] == CTLNUL) |
f4f5e1c2 CR |
3846 | { |
3847 | i++; | |
3848 | continue; | |
3849 | } | |
7117c2d2 JA |
3850 | |
3851 | prev_i = i; | |
3852 | ADVANCE_CHAR (string, slen, i); | |
3853 | if (j < prev_i) | |
cce855bc | 3854 | { |
7117c2d2 | 3855 | do string[j++] = string[prev_i++]; while (prev_i < i); |
cce855bc | 3856 | } |
7117c2d2 JA |
3857 | else |
3858 | j = i; | |
cce855bc | 3859 | } |
7117c2d2 JA |
3860 | string[j] = '\0'; |
3861 | ||
3862 | return (string); | |
cce855bc JA |
3863 | } |
3864 | ||
3865 | /* Perform quoted null character removal on each element of LIST. | |
3866 | This modifies LIST. */ | |
3867 | void | |
3868 | word_list_remove_quoted_nulls (list) | |
3869 | WORD_LIST *list; | |
3870 | { | |
3871 | register WORD_LIST *t; | |
3872 | ||
3873 | for (t = list; t; t = t->next) | |
227f982e CR |
3874 | { |
3875 | remove_quoted_nulls (t->word->word); | |
3876 | t->word->flags &= ~W_HASQUOTEDNULL; | |
3877 | } | |
cce855bc JA |
3878 | } |
3879 | ||
3880 | /* **************************************************************** */ | |
3881 | /* */ | |
3882 | /* Functions for Matching and Removing Patterns */ | |
3883 | /* */ | |
3884 | /* **************************************************************** */ | |
3885 | ||
545f34cf | 3886 | #if defined (HANDLE_MULTIBYTE) |
704a1a2a | 3887 | #if 0 /* Currently unused */ |
545f34cf CR |
3888 | static unsigned char * |
3889 | mb_getcharlens (string, len) | |
3890 | char *string; | |
3891 | int len; | |
3892 | { | |
704a1a2a CR |
3893 | int i, offset, last; |
3894 | unsigned char *ret; | |
545f34cf CR |
3895 | char *p; |
3896 | DECLARE_MBSTATE; | |
3897 | ||
3898 | i = offset = 0; | |
3899 | last = 0; | |
3900 | ret = (unsigned char *)xmalloc (len); | |
3901 | memset (ret, 0, len); | |
3902 | while (string[last]) | |
3903 | { | |
3904 | ADVANCE_CHAR (string, len, offset); | |
3905 | ret[last] = offset - last; | |
3906 | last = offset; | |
3907 | } | |
3908 | return ret; | |
3909 | } | |
3910 | #endif | |
704a1a2a | 3911 | #endif |
545f34cf | 3912 | |
cce855bc JA |
3913 | /* Remove the portion of PARAM matched by PATTERN according to OP, where OP |
3914 | can have one of 4 values: | |
3915 | RP_LONG_LEFT remove longest matching portion at start of PARAM | |
726f6388 JA |
3916 | RP_SHORT_LEFT remove shortest matching portion at start of PARAM |
3917 | RP_LONG_RIGHT remove longest matching portion at end of PARAM | |
3918 | RP_SHORT_RIGHT remove shortest matching portion at end of PARAM | |
3919 | */ | |
3920 | ||
3921 | #define RP_LONG_LEFT 1 | |
3922 | #define RP_SHORT_LEFT 2 | |
3923 | #define RP_LONG_RIGHT 3 | |
3924 | #define RP_SHORT_RIGHT 4 | |
3925 | ||
3d35553a | 3926 | /* Returns its first argument if nothing matched; new memory otherwise */ |
726f6388 | 3927 | static char * |
704a1a2a | 3928 | remove_upattern (param, pattern, op) |
726f6388 JA |
3929 | char *param, *pattern; |
3930 | int op; | |
3931 | { | |
ccc6cda3 JA |
3932 | register int len; |
3933 | register char *end; | |
726f6388 | 3934 | register char *p, *ret, c; |
726f6388 | 3935 | |
ccc6cda3 JA |
3936 | len = STRLEN (param); |
3937 | end = param + len; | |
726f6388 JA |
3938 | |
3939 | switch (op) | |
3940 | { | |
3941 | case RP_LONG_LEFT: /* remove longest match at start */ | |
704a1a2a | 3942 | for (p = end; p >= param; p--) |
726f6388 JA |
3943 | { |
3944 | c = *p; *p = '\0'; | |
f73dda09 | 3945 | if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
726f6388 JA |
3946 | { |
3947 | *p = c; | |
3948 | return (savestring (p)); | |
3949 | } | |
3950 | *p = c; | |
545f34cf | 3951 | |
726f6388 JA |
3952 | } |
3953 | break; | |
3954 | ||
3955 | case RP_SHORT_LEFT: /* remove shortest match at start */ | |
704a1a2a | 3956 | for (p = param; p <= end; p++) |
726f6388 JA |
3957 | { |
3958 | c = *p; *p = '\0'; | |
f73dda09 | 3959 | if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
726f6388 JA |
3960 | { |
3961 | *p = c; | |
3962 | return (savestring (p)); | |
3963 | } | |
3964 | *p = c; | |
3965 | } | |
3966 | break; | |
3967 | ||
ccc6cda3 | 3968 | case RP_LONG_RIGHT: /* remove longest match at end */ |
704a1a2a | 3969 | for (p = param; p <= end; p++) |
ccc6cda3 | 3970 | { |
f73dda09 | 3971 | if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
ccc6cda3 JA |
3972 | { |
3973 | c = *p; *p = '\0'; | |
3974 | ret = savestring (param); | |
3975 | *p = c; | |
3976 | return (ret); | |
3977 | } | |
3978 | } | |
3979 | break; | |
3980 | ||
3981 | case RP_SHORT_RIGHT: /* remove shortest match at end */ | |
704a1a2a | 3982 | for (p = end; p >= param; p--) |
ccc6cda3 | 3983 | { |
f73dda09 | 3984 | if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
ccc6cda3 JA |
3985 | { |
3986 | c = *p; *p = '\0'; | |
3987 | ret = savestring (param); | |
3988 | *p = c; | |
3989 | return (ret); | |
3990 | } | |
704a1a2a CR |
3991 | } |
3992 | break; | |
3993 | } | |
3994 | ||
3d35553a | 3995 | return (param); /* no match, return original string */ |
704a1a2a CR |
3996 | } |
3997 | ||
3998 | #if defined (HANDLE_MULTIBYTE) | |
3d35553a | 3999 | /* Returns its first argument if nothing matched; new memory otherwise */ |
704a1a2a CR |
4000 | static wchar_t * |
4001 | remove_wpattern (wparam, wstrlen, wpattern, op) | |
4002 | wchar_t *wparam; | |
4003 | size_t wstrlen; | |
4004 | wchar_t *wpattern; | |
4005 | int op; | |
4006 | { | |
d3ad40de CR |
4007 | wchar_t wc, *ret; |
4008 | int n; | |
545f34cf | 4009 | |
704a1a2a CR |
4010 | switch (op) |
4011 | { | |
4012 | case RP_LONG_LEFT: /* remove longest match at start */ | |
4013 | for (n = wstrlen; n >= 0; n--) | |
4014 | { | |
4015 | wc = wparam[n]; wparam[n] = L'\0'; | |
4016 | if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
545f34cf | 4017 | { |
704a1a2a CR |
4018 | wparam[n] = wc; |
4019 | return (wcsdup (wparam + n)); | |
4020 | } | |
4021 | wparam[n] = wc; | |
4022 | } | |
4023 | break; | |
4024 | ||
4025 | case RP_SHORT_LEFT: /* remove shortest match at start */ | |
4026 | for (n = 0; n <= wstrlen; n++) | |
4027 | { | |
4028 | wc = wparam[n]; wparam[n] = L'\0'; | |
4029 | if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4030 | { | |
4031 | wparam[n] = wc; | |
4032 | return (wcsdup (wparam + n)); | |
4033 | } | |
4034 | wparam[n] = wc; | |
4035 | } | |
4036 | break; | |
4037 | ||
4038 | case RP_LONG_RIGHT: /* remove longest match at end */ | |
4039 | for (n = 0; n <= wstrlen; n++) | |
4040 | { | |
4041 | if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4042 | { | |
4043 | wc = wparam[n]; wparam[n] = L'\0'; | |
4044 | ret = wcsdup (wparam); | |
4045 | wparam[n] = wc; | |
4046 | return (ret); | |
4047 | } | |
4048 | } | |
4049 | break; | |
4050 | ||
4051 | case RP_SHORT_RIGHT: /* remove shortest match at end */ | |
4052 | for (n = wstrlen; n >= 0; n--) | |
4053 | { | |
4054 | if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4055 | { | |
4056 | wc = wparam[n]; wparam[n] = L'\0'; | |
4057 | ret = wcsdup (wparam); | |
4058 | wparam[n] = wc; | |
4059 | return (ret); | |
545f34cf | 4060 | } |
ccc6cda3 JA |
4061 | } |
4062 | break; | |
4063 | } | |
545f34cf | 4064 | |
3d35553a | 4065 | return (wparam); /* no match, return original string */ |
704a1a2a CR |
4066 | } |
4067 | #endif /* HANDLE_MULTIBYTE */ | |
4068 | ||
4069 | static char * | |
4070 | remove_pattern (param, pattern, op) | |
4071 | char *param, *pattern; | |
4072 | int op; | |
4073 | { | |
3d35553a CR |
4074 | char *xret; |
4075 | ||
704a1a2a CR |
4076 | if (param == NULL) |
4077 | return (param); | |
4078 | if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */ | |
4079 | return (savestring (param)); | |
4080 | ||
4081 | #if defined (HANDLE_MULTIBYTE) | |
4082 | if (MB_CUR_MAX > 1) | |
4083 | { | |
4084 | wchar_t *ret, *oret; | |
4085 | size_t n; | |
4086 | wchar_t *wparam, *wpattern; | |
4087 | mbstate_t ps; | |
704a1a2a CR |
4088 | |
4089 | n = xdupmbstowcs (&wpattern, NULL, pattern); | |
4090 | if (n == (size_t)-1) | |
3d35553a CR |
4091 | { |
4092 | xret = remove_upattern (param, pattern, op); | |
4093 | return ((xret == param) ? savestring (param) : xret); | |
4094 | } | |
704a1a2a | 4095 | n = xdupmbstowcs (&wparam, NULL, param); |
73a146be | 4096 | |
704a1a2a CR |
4097 | if (n == (size_t)-1) |
4098 | { | |
4099 | free (wpattern); | |
3d35553a CR |
4100 | xret = remove_upattern (param, pattern, op); |
4101 | return ((xret == param) ? savestring (param) : xret); | |
704a1a2a CR |
4102 | } |
4103 | oret = ret = remove_wpattern (wparam, n, wpattern, op); | |
3d35553a CR |
4104 | /* Don't bother to convert wparam back to multibyte string if nothing |
4105 | matched; just return copy of original string */ | |
4106 | if (ret == wparam) | |
4107 | { | |
4108 | free (wparam); | |
4109 | free (wpattern); | |
4110 | return (savestring (param)); | |
4111 | } | |
704a1a2a CR |
4112 | |
4113 | free (wparam); | |
4114 | free (wpattern); | |
4115 | ||
4116 | n = strlen (param); | |
dc8fbaf9 | 4117 | xret = (char *)xmalloc (n + 1); |
704a1a2a CR |
4118 | memset (&ps, '\0', sizeof (mbstate_t)); |
4119 | n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps); | |
4120 | xret[n] = '\0'; /* just to make sure */ | |
4121 | free (oret); | |
4122 | return xret; | |
4123 | } | |
4124 | else | |
4125 | #endif | |
3d35553a CR |
4126 | { |
4127 | xret = remove_upattern (param, pattern, op); | |
4128 | return ((xret == param) ? savestring (param) : xret); | |
4129 | } | |
ccc6cda3 JA |
4130 | } |
4131 | ||
ccc6cda3 JA |
4132 | /* Match PAT anywhere in STRING and return the match boundaries. |
4133 | This returns 1 in case of a successful match, 0 otherwise. SP | |
4134 | and EP are pointers into the string where the match begins and | |
4135 | ends, respectively. MTYPE controls what kind of match is attempted. | |
4136 | MATCH_BEG and MATCH_END anchor the match at the beginning and end | |
4137 | of the string, respectively. The longest match is returned. */ | |
4138 | static int | |
704a1a2a | 4139 | match_upattern (string, pat, mtype, sp, ep) |
ccc6cda3 JA |
4140 | char *string, *pat; |
4141 | int mtype; | |
4142 | char **sp, **ep; | |
4143 | { | |
5f8cde23 | 4144 | int c, len, mlen; |
233564d2 | 4145 | register char *p, *p1, *npat; |
ccc6cda3 | 4146 | char *end; |
06dff54a | 4147 | int n1; |
ccc6cda3 | 4148 | |
233564d2 CR |
4149 | /* If the pattern doesn't match anywhere in the string, go ahead and |
4150 | short-circuit right away. A minor optimization, saves a bunch of | |
4151 | unnecessary calls to strmatch (up to N calls for a string of N | |
4152 | characters) if the match is unsuccessful. To preserve the semantics | |
4153 | of the substring matches below, we make sure that the pattern has | |
4154 | `*' as first and last character, making a new pattern if necessary. */ | |
4155 | /* XXX - check this later if I ever implement `**' with special meaning, | |
4156 | since this will potentially result in `**' at the beginning or end */ | |
4157 | len = STRLEN (pat); | |
176b12ee | 4158 | if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*') |
233564d2 | 4159 | { |
dc8fbaf9 | 4160 | p = npat = (char *)xmalloc (len + 3); |
233564d2 | 4161 | p1 = pat; |
176b12ee | 4162 | if (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob)) |
233564d2 CR |
4163 | *p++ = '*'; |
4164 | while (*p1) | |
4165 | *p++ = *p1++; | |
4166 | if (p1[-1] != '*' || p[-2] == '\\') | |
4167 | *p++ = '*'; | |
4168 | *p = '\0'; | |
4169 | } | |
4170 | else | |
4171 | npat = pat; | |
4172 | c = strmatch (npat, string, FNMATCH_EXTFLAG); | |
4173 | if (npat != pat) | |
4174 | free (npat); | |
4175 | if (c == FNM_NOMATCH) | |
4176 | return (0); | |
4177 | ||
5e13499c CR |
4178 | len = STRLEN (string); |
4179 | end = string + len; | |
ccc6cda3 | 4180 | |
5f8cde23 CR |
4181 | mlen = umatchlen (pat, len); |
4182 | ||
ccc6cda3 JA |
4183 | switch (mtype) |
4184 | { | |
4185 | case MATCH_ANY: | |
704a1a2a | 4186 | for (p = string; p <= end; p++) |
ccc6cda3 JA |
4187 | { |
4188 | if (match_pattern_char (pat, p)) | |
4189 | { | |
5f8cde23 | 4190 | p1 = (mlen == -1) ? end : p + mlen; |
06dff54a CR |
4191 | /* p1 - p = length of portion of string to be considered |
4192 | p = current position in string | |
4193 | mlen = number of characters consumed by match (-1 for entire string) | |
4194 | end = end of string | |
4195 | we want to break immediately if the potential match len | |
4196 | is greater than the number of characters remaining in the | |
4197 | string | |
4198 | */ | |
4199 | if (p1 > end) | |
5f8cde23 CR |
4200 | break; |
4201 | for ( ; p1 >= p; p1--) | |
ccc6cda3 JA |
4202 | { |
4203 | c = *p1; *p1 = '\0'; | |
f73dda09 | 4204 | if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0) |
ccc6cda3 JA |
4205 | { |
4206 | *p1 = c; | |
4207 | *sp = p; | |
4208 | *ep = p1; | |
4209 | return 1; | |
4210 | } | |
4211 | *p1 = c; | |
5f8cde23 CR |
4212 | #if 1 |
4213 | /* If MLEN != -1, we have a fixed length pattern. */ | |
4214 | if (mlen != -1) | |
4215 | break; | |
4216 | #endif | |
ccc6cda3 JA |
4217 | } |
4218 | } | |
4219 | } | |
545f34cf | 4220 | |
ccc6cda3 JA |
4221 | return (0); |
4222 | ||
4223 | case MATCH_BEG: | |
4224 | if (match_pattern_char (pat, string) == 0) | |
28ef6c31 | 4225 | return (0); |
545f34cf | 4226 | |
5f8cde23 | 4227 | for (p = (mlen == -1) ? end : string + mlen; p >= string; p--) |
ccc6cda3 JA |
4228 | { |
4229 | c = *p; *p = '\0'; | |
f73dda09 | 4230 | if (strmatch (pat, string, FNMATCH_EXTFLAG) == 0) |
ccc6cda3 JA |
4231 | { |
4232 | *p = c; | |
4233 | *sp = string; | |
4234 | *ep = p; | |
4235 | return 1; | |
4236 | } | |
4237 | *p = c; | |
5f8cde23 CR |
4238 | /* If MLEN != -1, we have a fixed length pattern. */ |
4239 | if (mlen != -1) | |
4240 | break; | |
ccc6cda3 | 4241 | } |
545f34cf | 4242 | |
ccc6cda3 | 4243 | return (0); |
726f6388 | 4244 | |
ccc6cda3 | 4245 | case MATCH_END: |
5f8cde23 | 4246 | for (p = end - ((mlen == -1) ? len : mlen); p <= end; p++) |
545f34cf CR |
4247 | { |
4248 | if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0) | |
4249 | { | |
4250 | *sp = p; | |
4251 | *ep = end; | |
4252 | return 1; | |
4253 | } | |
5f8cde23 CR |
4254 | /* If MLEN != -1, we have a fixed length pattern. */ |
4255 | if (mlen != -1) | |
4256 | break; | |
704a1a2a CR |
4257 | } |
4258 | ||
4259 | return (0); | |
4260 | } | |
4261 | ||
4262 | return (0); | |
4263 | } | |
4264 | ||
4265 | #if defined (HANDLE_MULTIBYTE) | |
704a1a2a CR |
4266 | /* Match WPAT anywhere in WSTRING and return the match boundaries. |
4267 | This returns 1 in case of a successful match, 0 otherwise. Wide | |
4268 | character version. */ | |
4269 | static int | |
4270 | match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep) | |
4271 | wchar_t *wstring; | |
4272 | char **indices; | |
4273 | size_t wstrlen; | |
4274 | wchar_t *wpat; | |
4275 | int mtype; | |
4276 | char **sp, **ep; | |
4277 | { | |
233564d2 | 4278 | wchar_t wc, *wp, *nwpat, *wp1; |
5f8cde23 CR |
4279 | size_t len; |
4280 | int mlen; | |
adc6cff5 CR |
4281 | int n, n1, n2, simple; |
4282 | ||
4283 | simple = (wpat[0] != L'\\' && wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'['); | |
4284 | #if defined (EXTENDED_GLOB) | |
4285 | if (extended_glob) | |
e107650c | 4286 | simple &= (wpat[1] != L'(' || (wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'+' && wpat[0] != L'!' && wpat[0] != L'@')); /*)*/ |
adc6cff5 | 4287 | #endif |
704a1a2a | 4288 | |
233564d2 CR |
4289 | /* If the pattern doesn't match anywhere in the string, go ahead and |
4290 | short-circuit right away. A minor optimization, saves a bunch of | |
4291 | unnecessary calls to strmatch (up to N calls for a string of N | |
4292 | characters) if the match is unsuccessful. To preserve the semantics | |
4293 | of the substring matches below, we make sure that the pattern has | |
4294 | `*' as first and last character, making a new pattern if necessary. */ | |
233564d2 | 4295 | len = wcslen (wpat); |
176b12ee | 4296 | if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*') |
233564d2 | 4297 | { |
dc8fbaf9 | 4298 | wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t)); |
233564d2 | 4299 | wp1 = wpat; |
176b12ee | 4300 | if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob)) |
233564d2 CR |
4301 | *wp++ = L'*'; |
4302 | while (*wp1 != L'\0') | |
4303 | *wp++ = *wp1++; | |
4304 | if (wp1[-1] != L'*' || wp1[-2] == L'\\') | |
4305 | *wp++ = L'*'; | |
4306 | *wp = '\0'; | |
4307 | } | |
4308 | else | |
4309 | nwpat = wpat; | |
4310 | len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG); | |
4311 | if (nwpat != wpat) | |
4312 | free (nwpat); | |
4313 | if (len == FNM_NOMATCH) | |
4314 | return (0); | |
4315 | ||
5f8cde23 | 4316 | mlen = wmatchlen (wpat, wstrlen); |
adc6cff5 | 4317 | |
5f8cde23 | 4318 | /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */ |
704a1a2a CR |
4319 | switch (mtype) |
4320 | { | |
4321 | case MATCH_ANY: | |
4322 | for (n = 0; n <= wstrlen; n++) | |
4323 | { | |
adc6cff5 | 4324 | n2 = simple ? (*wpat == wstring[n]) : match_pattern_wchar (wpat, wstring + n); |
adc6cff5 | 4325 | if (n2) |
704a1a2a | 4326 | { |
5f8cde23 CR |
4327 | n1 = (mlen == -1) ? wstrlen : n + mlen; |
4328 | if (n1 > wstrlen) | |
4329 | break; | |
4330 | ||
4331 | for ( ; n1 >= n; n1--) | |
704a1a2a CR |
4332 | { |
4333 | wc = wstring[n1]; wstring[n1] = L'\0'; | |
4334 | if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0) | |
4335 | { | |
4336 | wstring[n1] = wc; | |
4337 | *sp = indices[n]; | |
4338 | *ep = indices[n1]; | |
4339 | return 1; | |
4340 | } | |
4341 | wstring[n1] = wc; | |
5f8cde23 CR |
4342 | /* If MLEN != -1, we have a fixed length pattern. */ |
4343 | if (mlen != -1) | |
4344 | break; | |
704a1a2a CR |
4345 | } |
4346 | } | |
4347 | } | |
4348 | ||
4349 | return (0); | |
4350 | ||
4351 | case MATCH_BEG: | |
4352 | if (match_pattern_wchar (wpat, wstring) == 0) | |
4353 | return (0); | |
4354 | ||
5f8cde23 | 4355 | for (n = (mlen == -1) ? wstrlen : mlen; n >= 0; n--) |
704a1a2a CR |
4356 | { |
4357 | wc = wstring[n]; wstring[n] = L'\0'; | |
4358 | if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG) == 0) | |
4359 | { | |
4360 | wstring[n] = wc; | |
4361 | *sp = indices[0]; | |
4362 | *ep = indices[n]; | |
4363 | return 1; | |
4364 | } | |
4365 | wstring[n] = wc; | |
5f8cde23 CR |
4366 | /* If MLEN != -1, we have a fixed length pattern. */ |
4367 | if (mlen != -1) | |
4368 | break; | |
704a1a2a CR |
4369 | } |
4370 | ||
4371 | return (0); | |
4372 | ||
4373 | case MATCH_END: | |
5f8cde23 | 4374 | for (n = wstrlen - ((mlen == -1) ? wstrlen : mlen); n <= wstrlen; n++) |
704a1a2a CR |
4375 | { |
4376 | if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0) | |
545f34cf | 4377 | { |
704a1a2a CR |
4378 | *sp = indices[n]; |
4379 | *ep = indices[wstrlen]; | |
4380 | return 1; | |
545f34cf | 4381 | } |
5f8cde23 CR |
4382 | /* If MLEN != -1, we have a fixed length pattern. */ |
4383 | if (mlen != -1) | |
4384 | break; | |
545f34cf | 4385 | } |
704a1a2a | 4386 | |
ccc6cda3 | 4387 | return (0); |
726f6388 | 4388 | } |
ccc6cda3 JA |
4389 | |
4390 | return (0); | |
726f6388 | 4391 | } |
704a1a2a CR |
4392 | #endif /* HANDLE_MULTIBYTE */ |
4393 | ||
4394 | static int | |
4395 | match_pattern (string, pat, mtype, sp, ep) | |
4396 | char *string, *pat; | |
4397 | int mtype; | |
4398 | char **sp, **ep; | |
4399 | { | |
4400 | #if defined (HANDLE_MULTIBYTE) | |
4401 | int ret; | |
4402 | size_t n; | |
4403 | wchar_t *wstring, *wpat; | |
4404 | char **indices; | |
adc6cff5 | 4405 | size_t slen, plen, mslen, mplen; |
704a1a2a CR |
4406 | #endif |
4407 | ||
4408 | if (string == 0 || *string == 0 || pat == 0 || *pat == 0) | |
4409 | return (0); | |
4410 | ||
4411 | #if defined (HANDLE_MULTIBYTE) | |
4412 | if (MB_CUR_MAX > 1) | |
4413 | { | |
adc6cff5 | 4414 | if (mbsmbchar (string) == 0 && mbsmbchar (pat) == 0) |
adc6cff5 CR |
4415 | return (match_upattern (string, pat, mtype, sp, ep)); |
4416 | ||
704a1a2a CR |
4417 | n = xdupmbstowcs (&wpat, NULL, pat); |
4418 | if (n == (size_t)-1) | |
4419 | return (match_upattern (string, pat, mtype, sp, ep)); | |
4420 | n = xdupmbstowcs (&wstring, &indices, string); | |
4421 | if (n == (size_t)-1) | |
4422 | { | |
4423 | free (wpat); | |
4424 | return (match_upattern (string, pat, mtype, sp, ep)); | |
4425 | } | |
4426 | ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep); | |
4427 | ||
4428 | free (wpat); | |
4429 | free (wstring); | |
4430 | free (indices); | |
4431 | ||
4432 | return (ret); | |
4433 | } | |
4434 | else | |
4435 | #endif | |
4436 | return (match_upattern (string, pat, mtype, sp, ep)); | |
4437 | } | |
726f6388 | 4438 | |
cce855bc JA |
4439 | static int |
4440 | getpatspec (c, value) | |
4441 | int c; | |
4442 | char *value; | |
4443 | { | |
4444 | if (c == '#') | |
4445 | return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT); | |
4446 | else /* c == '%' */ | |
4447 | return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT); | |
4448 | } | |
4449 | ||
4450 | /* Posix.2 says that the WORD should be run through tilde expansion, | |
4451 | parameter expansion, command substitution and arithmetic expansion. | |
4452 | This leaves the result quoted, so quote_string_for_globbing () has | |
f73dda09 | 4453 | to be called to fix it up for strmatch (). If QUOTED is non-zero, |
cce855bc JA |
4454 | it means that the entire expression was enclosed in double quotes. |
4455 | This means that quoting characters in the pattern do not make any | |
4456 | special pattern characters quoted. For example, the `*' in the | |
4457 | following retains its special meaning: "${foo#'*'}". */ | |
4458 | static char * | |
4459 | getpattern (value, quoted, expandpat) | |
4460 | char *value; | |
4461 | int quoted, expandpat; | |
4462 | { | |
4463 | char *pat, *tword; | |
4464 | WORD_LIST *l; | |
d3ad40de | 4465 | #if 0 |
cce855bc | 4466 | int i; |
d3ad40de | 4467 | #endif |
7117c2d2 JA |
4468 | /* There is a problem here: how to handle single or double quotes in the |
4469 | pattern string when the whole expression is between double quotes? | |
4470 | POSIX.2 says that enclosing double quotes do not cause the pattern to | |
4471 | be quoted, but does that leave us a problem with @ and array[@] and their | |
4472 | expansions inside a pattern? */ | |
4473 | #if 0 | |
cce855bc JA |
4474 | if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword) |
4475 | { | |
4476 | i = 0; | |
4477 | pat = string_extract_double_quoted (tword, &i, 1); | |
4478 | free (tword); | |
4479 | tword = pat; | |
4480 | } | |
7117c2d2 | 4481 | #endif |
cce855bc | 4482 | |
7117c2d2 JA |
4483 | /* expand_string_for_rhs () leaves WORD quoted and does not perform |
4484 | word splitting. */ | |
22e63b05 | 4485 | l = *value ? expand_string_for_rhs (value, |
7117c2d2 | 4486 | (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted, |
cce855bc | 4487 | (int *)NULL, (int *)NULL) |
cce855bc | 4488 | : (WORD_LIST *)0; |
cce855bc JA |
4489 | pat = string_list (l); |
4490 | dispose_words (l); | |
4491 | if (pat) | |
4492 | { | |
4493 | tword = quote_string_for_globbing (pat, QGLOB_CVTNULL); | |
4494 | free (pat); | |
4495 | pat = tword; | |
4496 | } | |
4497 | return (pat); | |
4498 | } | |
4499 | ||
7117c2d2 | 4500 | #if 0 |
cce855bc JA |
4501 | /* Handle removing a pattern from a string as a result of ${name%[%]value} |
4502 | or ${name#[#]value}. */ | |
4503 | static char * | |
7117c2d2 JA |
4504 | variable_remove_pattern (value, pattern, patspec, quoted) |
4505 | char *value, *pattern; | |
4506 | int patspec, quoted; | |
cce855bc | 4507 | { |
7117c2d2 | 4508 | char *tword; |
cce855bc | 4509 | |
7117c2d2 | 4510 | tword = remove_pattern (value, pattern, patspec); |
cce855bc | 4511 | |
cce855bc JA |
4512 | return (tword); |
4513 | } | |
7117c2d2 | 4514 | #endif |
cce855bc JA |
4515 | |
4516 | static char * | |
7117c2d2 | 4517 | list_remove_pattern (list, pattern, patspec, itype, quoted) |
cce855bc JA |
4518 | WORD_LIST *list; |
4519 | char *pattern; | |
7117c2d2 | 4520 | int patspec, itype, quoted; |
cce855bc JA |
4521 | { |
4522 | WORD_LIST *new, *l; | |
4523 | WORD_DESC *w; | |
4524 | char *tword; | |
4525 | ||
4526 | for (new = (WORD_LIST *)NULL, l = list; l; l = l->next) | |
4527 | { | |
4528 | tword = remove_pattern (l->word->word, pattern, patspec); | |
227f982e CR |
4529 | w = alloc_word_desc (); |
4530 | w->word = tword ? tword : savestring (""); | |
cce855bc JA |
4531 | new = make_word_list (w, new); |
4532 | } | |
4533 | ||
4534 | l = REVERSE_LIST (new, WORD_LIST *); | |
e33f2203 | 4535 | tword = string_list_pos_params (itype, l, quoted); |
cce855bc | 4536 | dispose_words (l); |
e33f2203 | 4537 | |
cce855bc JA |
4538 | return (tword); |
4539 | } | |
4540 | ||
4541 | static char * | |
7117c2d2 JA |
4542 | parameter_list_remove_pattern (itype, pattern, patspec, quoted) |
4543 | int itype; | |
4544 | char *pattern; | |
4545 | int patspec, quoted; | |
cce855bc | 4546 | { |
7117c2d2 | 4547 | char *ret; |
cce855bc JA |
4548 | WORD_LIST *list; |
4549 | ||
cce855bc | 4550 | list = list_rest_of_args (); |
7117c2d2 JA |
4551 | if (list == 0) |
4552 | return ((char *)NULL); | |
4553 | ret = list_remove_pattern (list, pattern, patspec, itype, quoted); | |
cce855bc | 4554 | dispose_words (list); |
cce855bc JA |
4555 | return (ret); |
4556 | } | |
4557 | ||
4558 | #if defined (ARRAY_VARS) | |
4559 | static char * | |
fdf670ea CR |
4560 | array_remove_pattern (var, pattern, patspec, varname, quoted) |
4561 | SHELL_VAR *var; | |
7117c2d2 JA |
4562 | char *pattern; |
4563 | int patspec; | |
4564 | char *varname; /* so we can figure out how it's indexed */ | |
4565 | int quoted; | |
cce855bc | 4566 | { |
fdf670ea CR |
4567 | ARRAY *a; |
4568 | HASH_TABLE *h; | |
7117c2d2 JA |
4569 | int itype; |
4570 | char *ret; | |
4571 | WORD_LIST *list; | |
4572 | SHELL_VAR *v; | |
cce855bc | 4573 | |
7117c2d2 JA |
4574 | /* compute itype from varname here */ |
4575 | v = array_variable_part (varname, &ret, 0); | |
a37d979e CR |
4576 | |
4577 | /* XXX */ | |
4578 | if (v && invisible_p (var)) | |
4579 | return ((char *)NULL); | |
4580 | ||
7117c2d2 JA |
4581 | itype = ret[0]; |
4582 | ||
fdf670ea CR |
4583 | a = (v && array_p (v)) ? array_cell (v) : 0; |
4584 | h = (v && assoc_p (v)) ? assoc_cell (v) : 0; | |
4585 | ||
4586 | list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0); | |
7117c2d2 JA |
4587 | if (list == 0) |
4588 | return ((char *)NULL); | |
4589 | ret = list_remove_pattern (list, pattern, patspec, itype, quoted); | |
4590 | dispose_words (list); | |
4591 | ||
4592 | return ret; | |
4593 | } | |
4594 | #endif /* ARRAY_VARS */ | |
4595 | ||
4596 | static char * | |
5f8cde23 CR |
4597 | parameter_brace_remove_pattern (varname, value, ind, patstr, rtype, quoted, flags) |
4598 | char *varname, *value; | |
4599 | int ind; | |
4600 | char *patstr; | |
4601 | int rtype, quoted, flags; | |
7117c2d2 | 4602 | { |
d3a24ed2 | 4603 | int vtype, patspec, starsub; |
7117c2d2 JA |
4604 | char *temp1, *val, *pattern; |
4605 | SHELL_VAR *v; | |
4606 | ||
4607 | if (value == 0) | |
4608 | return ((char *)NULL); | |
4609 | ||
4610 | this_command_name = varname; | |
4611 | ||
5f8cde23 | 4612 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
7117c2d2 | 4613 | if (vtype == -1) |
cce855bc JA |
4614 | return ((char *)NULL); |
4615 | ||
d3a24ed2 CR |
4616 | starsub = vtype & VT_STARSUB; |
4617 | vtype &= ~VT_STARSUB; | |
4618 | ||
7117c2d2 | 4619 | patspec = getpatspec (rtype, patstr); |
cce855bc | 4620 | if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT) |
7117c2d2 | 4621 | patstr++; |
cce855bc | 4622 | |
c40a57dd CR |
4623 | /* Need to pass getpattern newly-allocated memory in case of expansion -- |
4624 | the expansion code will free the passed string on an error. */ | |
4625 | temp1 = savestring (patstr); | |
4626 | pattern = getpattern (temp1, quoted, 1); | |
4627 | free (temp1); | |
cce855bc | 4628 | |
7117c2d2 JA |
4629 | temp1 = (char *)NULL; /* shut up gcc */ |
4630 | switch (vtype) | |
cce855bc | 4631 | { |
7117c2d2 JA |
4632 | case VT_VARIABLE: |
4633 | case VT_ARRAYMEMBER: | |
4634 | temp1 = remove_pattern (val, pattern, patspec); | |
4635 | if (vtype == VT_VARIABLE) | |
4636 | FREE (val); | |
4637 | if (temp1) | |
28ef6c31 | 4638 | { |
e6598ba4 CR |
4639 | val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
4640 | ? quote_string (temp1) | |
4641 | : quote_escapes (temp1); | |
7117c2d2 JA |
4642 | free (temp1); |
4643 | temp1 = val; | |
28ef6c31 | 4644 | } |
7117c2d2 JA |
4645 | break; |
4646 | #if defined (ARRAY_VARS) | |
4647 | case VT_ARRAYVAR: | |
fdf670ea | 4648 | temp1 = array_remove_pattern (v, pattern, patspec, varname, quoted); |
7117c2d2 | 4649 | if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) |
cce855bc | 4650 | { |
7117c2d2 JA |
4651 | val = quote_escapes (temp1); |
4652 | free (temp1); | |
4653 | temp1 = val; | |
cce855bc | 4654 | } |
7117c2d2 JA |
4655 | break; |
4656 | #endif | |
4657 | case VT_POSPARMS: | |
4658 | temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted); | |
4659 | if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) | |
4660 | { | |
4661 | val = quote_escapes (temp1); | |
4662 | free (temp1); | |
4663 | temp1 = val; | |
4664 | } | |
4665 | break; | |
cce855bc JA |
4666 | } |
4667 | ||
4668 | FREE (pattern); | |
7117c2d2 JA |
4669 | return temp1; |
4670 | } | |
cce855bc | 4671 | |
726f6388 JA |
4672 | /******************************************* |
4673 | * * | |
4674 | * Functions to expand WORD_DESCs * | |
4675 | * * | |
4676 | *******************************************/ | |
4677 | ||
4678 | /* Expand WORD, performing word splitting on the result. This does | |
4679 | parameter expansion, command substitution, arithmetic expansion, | |
4680 | word splitting, and quote removal. */ | |
4681 | ||
4682 | WORD_LIST * | |
4683 | expand_word (word, quoted) | |
4684 | WORD_DESC *word; | |
4685 | int quoted; | |
4686 | { | |
4687 | WORD_LIST *result, *tresult; | |
4688 | ||
b72432fd | 4689 | tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL); |
726f6388 JA |
4690 | result = word_list_split (tresult); |
4691 | dispose_words (tresult); | |
ccc6cda3 | 4692 | return (result ? dequote_list (result) : result); |
726f6388 JA |
4693 | } |
4694 | ||
4695 | /* Expand WORD, but do not perform word splitting on the result. This | |
4696 | does parameter expansion, command substitution, arithmetic expansion, | |
4697 | and quote removal. */ | |
4698 | WORD_LIST * | |
28ef6c31 | 4699 | expand_word_unsplit (word, quoted) |
726f6388 JA |
4700 | WORD_DESC *word; |
4701 | int quoted; | |
4702 | { | |
4703 | WORD_LIST *result; | |
4704 | ||
28ef6c31 | 4705 | expand_no_split_dollar_star = 1; |
1231ac47 CR |
4706 | #if defined (HANDLE_MULTIBYTE) |
4707 | if (ifs_firstc[0] == 0) | |
4708 | #else | |
4709 | if (ifs_firstc == 0) | |
4710 | #endif | |
4711 | word->flags |= W_NOSPLIT; | |
40647963 | 4712 | word->flags |= W_NOSPLIT2; |
b72432fd | 4713 | result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL); |
28ef6c31 | 4714 | expand_no_split_dollar_star = 0; |
7117c2d2 | 4715 | |
ccc6cda3 | 4716 | return (result ? dequote_list (result) : result); |
726f6388 JA |
4717 | } |
4718 | ||
4719 | /* Perform shell expansions on WORD, but do not perform word splitting or | |
1231ac47 CR |
4720 | quote removal on the result. Virtually identical to expand_word_unsplit; |
4721 | could be combined if implementations don't diverge. */ | |
726f6388 JA |
4722 | WORD_LIST * |
4723 | expand_word_leave_quoted (word, quoted) | |
4724 | WORD_DESC *word; | |
4725 | int quoted; | |
4726 | { | |
1231ac47 CR |
4727 | WORD_LIST *result; |
4728 | ||
4729 | expand_no_split_dollar_star = 1; | |
4730 | #if defined (HANDLE_MULTIBYTE) | |
4731 | if (ifs_firstc[0] == 0) | |
4732 | #else | |
4733 | if (ifs_firstc == 0) | |
4734 | #endif | |
4735 | word->flags |= W_NOSPLIT; | |
e1e48bba | 4736 | word->flags |= W_NOSPLIT2; |
1231ac47 CR |
4737 | result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL); |
4738 | expand_no_split_dollar_star = 0; | |
4739 | ||
4740 | return result; | |
726f6388 JA |
4741 | } |
4742 | ||
726f6388 JA |
4743 | #if defined (PROCESS_SUBSTITUTION) |
4744 | ||
cce855bc JA |
4745 | /*****************************************************************/ |
4746 | /* */ | |
4747 | /* Hacking Process Substitution */ | |
4748 | /* */ | |
4749 | /*****************************************************************/ | |
726f6388 | 4750 | |
726f6388 JA |
4751 | #if !defined (HAVE_DEV_FD) |
4752 | /* Named pipes must be removed explicitly with `unlink'. This keeps a list | |
4753 | of FIFOs the shell has open. unlink_fifo_list will walk the list and | |
4754 | unlink all of them. add_fifo_list adds the name of an open FIFO to the | |
4755 | list. NFIFO is a count of the number of FIFOs in the list. */ | |
4756 | #define FIFO_INCR 20 | |
4757 | ||
f73dda09 JA |
4758 | struct temp_fifo { |
4759 | char *file; | |
4760 | pid_t proc; | |
4761 | }; | |
4762 | ||
4763 | static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL; | |
ccc6cda3 JA |
4764 | static int nfifo; |
4765 | static int fifo_list_size; | |
726f6388 | 4766 | |
92717e1a CR |
4767 | void |
4768 | clear_fifo_list () | |
4769 | { | |
4770 | } | |
4771 | ||
112ff2a6 CR |
4772 | char * |
4773 | copy_fifo_list (sizep) | |
4774 | int *sizep; | |
4775 | { | |
4776 | if (sizep) | |
4777 | *sizep = 0; | |
4778 | return (char *)NULL; | |
4779 | } | |
4780 | ||
726f6388 JA |
4781 | static void |
4782 | add_fifo_list (pathname) | |
4783 | char *pathname; | |
4784 | { | |
4785 | if (nfifo >= fifo_list_size - 1) | |
4786 | { | |
4787 | fifo_list_size += FIFO_INCR; | |
f73dda09 JA |
4788 | fifo_list = (struct temp_fifo *)xrealloc (fifo_list, |
4789 | fifo_list_size * sizeof (struct temp_fifo)); | |
726f6388 JA |
4790 | } |
4791 | ||
f73dda09 JA |
4792 | fifo_list[nfifo].file = savestring (pathname); |
4793 | nfifo++; | |
726f6388 JA |
4794 | } |
4795 | ||
112ff2a6 CR |
4796 | void |
4797 | unlink_fifo (i) | |
4798 | int i; | |
4799 | { | |
4800 | if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1)) | |
4801 | { | |
4802 | unlink (fifo_list[i].file); | |
4803 | free (fifo_list[i].file); | |
4804 | fifo_list[i].file = (char *)NULL; | |
4805 | fifo_list[i].proc = -1; | |
4806 | } | |
4807 | } | |
4808 | ||
726f6388 JA |
4809 | void |
4810 | unlink_fifo_list () | |
4811 | { | |
f73dda09 JA |
4812 | int saved, i, j; |
4813 | ||
ccc6cda3 | 4814 | if (nfifo == 0) |
726f6388 JA |
4815 | return; |
4816 | ||
f73dda09 | 4817 | for (i = saved = 0; i < nfifo; i++) |
726f6388 | 4818 | { |
f73dda09 JA |
4819 | if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1)) |
4820 | { | |
7117c2d2 JA |
4821 | unlink (fifo_list[i].file); |
4822 | free (fifo_list[i].file); | |
4823 | fifo_list[i].file = (char *)NULL; | |
4824 | fifo_list[i].proc = -1; | |
f73dda09 JA |
4825 | } |
4826 | else | |
7117c2d2 | 4827 | saved++; |
f73dda09 JA |
4828 | } |
4829 | ||
4830 | /* If we didn't remove some of the FIFOs, compact the list. */ | |
4831 | if (saved) | |
4832 | { | |
4833 | for (i = j = 0; i < nfifo; i++) | |
4834 | if (fifo_list[i].file) | |
4835 | { | |
4836 | fifo_list[j].file = fifo_list[i].file; | |
4837 | fifo_list[j].proc = fifo_list[i].proc; | |
4838 | j++; | |
4839 | } | |
4840 | nfifo = j; | |
726f6388 | 4841 | } |
f73dda09 JA |
4842 | else |
4843 | nfifo = 0; | |
726f6388 JA |
4844 | } |
4845 | ||
112ff2a6 CR |
4846 | /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list |
4847 | from some point in the past, and close all open FIFOs in fifo_list | |
4848 | that are not marked as active in LIST. If LIST is NULL, close | |
4849 | everything in fifo_list. LSIZE is the number of elements in LIST, in | |
4850 | case it's larger than fifo_list_size (size of fifo_list). */ | |
4851 | void | |
4852 | close_new_fifos (list, lsize) | |
4853 | char *list; | |
4854 | int lsize; | |
4855 | { | |
4856 | int i; | |
4857 | ||
4858 | if (list == 0) | |
4859 | { | |
112ff2a6 CR |
4860 | unlink_fifo_list (); |
4861 | return; | |
4862 | } | |
4863 | ||
4864 | for (i = 0; i < lsize; i++) | |
4865 | if (list[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1) | |
112ff2a6 | 4866 | unlink_fifo (i); |
112ff2a6 CR |
4867 | |
4868 | for (i = lsize; i < fifo_list_size; i++) | |
112ff2a6 CR |
4869 | unlink_fifo (i); |
4870 | } | |
112ff2a6 | 4871 | |
d3ad40de CR |
4872 | int |
4873 | fifos_pending () | |
4874 | { | |
4875 | return nfifo; | |
4876 | } | |
4877 | ||
112ff2a6 CR |
4878 | int |
4879 | num_fifos () | |
4880 | { | |
4881 | return nfifo; | |
4882 | } | |
4883 | ||
726f6388 JA |
4884 | static char * |
4885 | make_named_pipe () | |
4886 | { | |
4887 | char *tname; | |
4888 | ||
d3ad40de | 4889 | tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR); |
726f6388 JA |
4890 | if (mkfifo (tname, 0600) < 0) |
4891 | { | |
4892 | free (tname); | |
4893 | return ((char *)NULL); | |
4894 | } | |
4895 | ||
4896 | add_fifo_list (tname); | |
4897 | return (tname); | |
4898 | } | |
4899 | ||
726f6388 JA |
4900 | #else /* HAVE_DEV_FD */ |
4901 | ||
4902 | /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell | |
4903 | has open to children. NFDS is a count of the number of bits currently | |
4904 | set in DEV_FD_LIST. TOTFDS is a count of the highest possible number | |
4905 | of open files. */ | |
4906 | static char *dev_fd_list = (char *)NULL; | |
ccc6cda3 | 4907 | static int nfds; |
726f6388 JA |
4908 | static int totfds; /* The highest possible number of open files. */ |
4909 | ||
92717e1a CR |
4910 | void |
4911 | clear_fifo (i) | |
4912 | int i; | |
4913 | { | |
4914 | if (dev_fd_list[i]) | |
4915 | { | |
4916 | dev_fd_list[i] = 0; | |
4917 | nfds--; | |
4918 | } | |
4919 | } | |
4920 | ||
4921 | void | |
4922 | clear_fifo_list () | |
4923 | { | |
4924 | register int i; | |
4925 | ||
4926 | if (nfds == 0) | |
4927 | return; | |
4928 | ||
4929 | for (i = 0; nfds && i < totfds; i++) | |
4930 | clear_fifo (i); | |
4931 | ||
4932 | nfds = 0; | |
4933 | } | |
4934 | ||
112ff2a6 CR |
4935 | char * |
4936 | copy_fifo_list (sizep) | |
4937 | int *sizep; | |
4938 | { | |
4939 | char *ret; | |
4940 | ||
4941 | if (nfds == 0 || totfds == 0) | |
4942 | { | |
4943 | if (sizep) | |
4944 | *sizep = 0; | |
4945 | return (char *)NULL; | |
4946 | } | |
4947 | ||
4948 | if (sizep) | |
4949 | *sizep = totfds; | |
4950 | ret = (char *)xmalloc (totfds); | |
4951 | return (memcpy (ret, dev_fd_list, totfds)); | |
4952 | } | |
4953 | ||
726f6388 JA |
4954 | static void |
4955 | add_fifo_list (fd) | |
4956 | int fd; | |
4957 | { | |
112ff2a6 | 4958 | if (dev_fd_list == 0 || fd >= totfds) |
726f6388 JA |
4959 | { |
4960 | int ofds; | |
4961 | ||
4962 | ofds = totfds; | |
4963 | totfds = getdtablesize (); | |
4964 | if (totfds < 0 || totfds > 256) | |
4965 | totfds = 256; | |
626d0694 | 4966 | if (fd >= totfds) |
726f6388 JA |
4967 | totfds = fd + 2; |
4968 | ||
f73dda09 | 4969 | dev_fd_list = (char *)xrealloc (dev_fd_list, totfds); |
7117c2d2 | 4970 | memset (dev_fd_list + ofds, '\0', totfds - ofds); |
726f6388 JA |
4971 | } |
4972 | ||
4973 | dev_fd_list[fd] = 1; | |
4974 | nfds++; | |
4975 | } | |
4976 | ||
d3ad40de CR |
4977 | int |
4978 | fifos_pending () | |
4979 | { | |
4980 | return 0; /* used for cleanup; not needed with /dev/fd */ | |
4981 | } | |
4982 | ||
112ff2a6 CR |
4983 | int |
4984 | num_fifos () | |
4985 | { | |
4986 | return nfds; | |
4987 | } | |
4988 | ||
4989 | void | |
4990 | unlink_fifo (fd) | |
4991 | int fd; | |
4992 | { | |
4993 | if (dev_fd_list[fd]) | |
4994 | { | |
4995 | close (fd); | |
4996 | dev_fd_list[fd] = 0; | |
4997 | nfds--; | |
4998 | } | |
4999 | } | |
5000 | ||
726f6388 JA |
5001 | void |
5002 | unlink_fifo_list () | |
5003 | { | |
5004 | register int i; | |
5005 | ||
ccc6cda3 | 5006 | if (nfds == 0) |
726f6388 JA |
5007 | return; |
5008 | ||
5009 | for (i = 0; nfds && i < totfds; i++) | |
112ff2a6 | 5010 | unlink_fifo (i); |
726f6388 JA |
5011 | |
5012 | nfds = 0; | |
5013 | } | |
5014 | ||
112ff2a6 CR |
5015 | /* Take LIST, which is a snapshot copy of dev_fd_list from some point in |
5016 | the past, and close all open fds in dev_fd_list that are not marked | |
5017 | as open in LIST. If LIST is NULL, close everything in dev_fd_list. | |
5018 | LSIZE is the number of elements in LIST, in case it's larger than | |
5019 | totfds (size of dev_fd_list). */ | |
5020 | void | |
5021 | close_new_fifos (list, lsize) | |
5022 | char *list; | |
5023 | int lsize; | |
5024 | { | |
5025 | int i; | |
5026 | ||
5027 | if (list == 0) | |
5028 | { | |
5029 | unlink_fifo_list (); | |
5030 | return; | |
5031 | } | |
5032 | ||
5033 | for (i = 0; i < lsize; i++) | |
5034 | if (list[i] == 0 && i < totfds && dev_fd_list[i]) | |
5035 | unlink_fifo (i); | |
5036 | ||
5037 | for (i = lsize; i < totfds; i++) | |
5038 | unlink_fifo (i); | |
5039 | } | |
5040 | ||
726f6388 JA |
5041 | #if defined (NOTDEF) |
5042 | print_dev_fd_list () | |
5043 | { | |
5044 | register int i; | |
5045 | ||
f73dda09 | 5046 | fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ()); |
726f6388 JA |
5047 | fflush (stderr); |
5048 | ||
5049 | for (i = 0; i < totfds; i++) | |
5050 | { | |
5051 | if (dev_fd_list[i]) | |
5052 | fprintf (stderr, " %d", i); | |
5053 | } | |
5054 | fprintf (stderr, "\n"); | |
5055 | } | |
5056 | #endif /* NOTDEF */ | |
5057 | ||
5058 | static char * | |
5059 | make_dev_fd_filename (fd) | |
5060 | int fd; | |
5061 | { | |
f73dda09 | 5062 | char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p; |
726f6388 | 5063 | |
e141c35a | 5064 | ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8); |
bb70624e JA |
5065 | |
5066 | strcpy (ret, DEV_FD_PREFIX); | |
5067 | p = inttostr (fd, intbuf, sizeof (intbuf)); | |
5068 | strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p); | |
5069 | ||
726f6388 JA |
5070 | add_fifo_list (fd); |
5071 | return (ret); | |
5072 | } | |
5073 | ||
5074 | #endif /* HAVE_DEV_FD */ | |
5075 | ||
5076 | /* Return a filename that will open a connection to the process defined by | |
5077 | executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return | |
5078 | a filename in /dev/fd corresponding to a descriptor that is one of the | |
5079 | ends of the pipe. If not defined, we use named pipes on systems that have | |
5080 | them. Systems without /dev/fd and named pipes are out of luck. | |
5081 | ||
5082 | OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or | |
5083 | use the read end of the pipe and dup that file descriptor to fd 0 in | |
5084 | the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for | |
5085 | writing or use the write end of the pipe in the child, and dup that | |
5086 | file descriptor to fd 1 in the child. The parent does the opposite. */ | |
5087 | ||
5088 | static char * | |
5089 | process_substitute (string, open_for_read_in_child) | |
5090 | char *string; | |
5091 | int open_for_read_in_child; | |
5092 | { | |
5093 | char *pathname; | |
5094 | int fd, result; | |
5095 | pid_t old_pid, pid; | |
5096 | #if defined (HAVE_DEV_FD) | |
5097 | int parent_pipe_fd, child_pipe_fd; | |
5098 | int fildes[2]; | |
5099 | #endif /* HAVE_DEV_FD */ | |
5100 | #if defined (JOB_CONTROL) | |
5101 | pid_t old_pipeline_pgrp; | |
ccc6cda3 | 5102 | #endif |
726f6388 | 5103 | |
cce855bc | 5104 | if (!string || !*string || wordexp_only) |
726f6388 JA |
5105 | return ((char *)NULL); |
5106 | ||
5107 | #if !defined (HAVE_DEV_FD) | |
5108 | pathname = make_named_pipe (); | |
5109 | #else /* HAVE_DEV_FD */ | |
5110 | if (pipe (fildes) < 0) | |
5111 | { | |
5e13499c | 5112 | sys_error (_("cannot make pipe for process substitution")); |
726f6388 JA |
5113 | return ((char *)NULL); |
5114 | } | |
5115 | /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of | |
5116 | the pipe in the parent, otherwise the read end. */ | |
5117 | parent_pipe_fd = fildes[open_for_read_in_child]; | |
5118 | child_pipe_fd = fildes[1 - open_for_read_in_child]; | |
d166f048 JA |
5119 | /* Move the parent end of the pipe to some high file descriptor, to |
5120 | avoid clashes with FDs used by the script. */ | |
5121 | parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64); | |
5122 | ||
726f6388 JA |
5123 | pathname = make_dev_fd_filename (parent_pipe_fd); |
5124 | #endif /* HAVE_DEV_FD */ | |
5125 | ||
36211029 | 5126 | if (pathname == 0) |
726f6388 | 5127 | { |
5e13499c | 5128 | sys_error (_("cannot make pipe for process substitution")); |
726f6388 JA |
5129 | return ((char *)NULL); |
5130 | } | |
5131 | ||
5132 | old_pid = last_made_pid; | |
5133 | ||
5134 | #if defined (JOB_CONTROL) | |
5135 | old_pipeline_pgrp = pipeline_pgrp; | |
5136 | pipeline_pgrp = shell_pgrp; | |
ccc6cda3 | 5137 | save_pipeline (1); |
ccc6cda3 JA |
5138 | #endif /* JOB_CONTROL */ |
5139 | ||
726f6388 JA |
5140 | pid = make_child ((char *)NULL, 1); |
5141 | if (pid == 0) | |
5142 | { | |
ccc6cda3 | 5143 | reset_terminating_signals (); /* XXX */ |
d3a24ed2 | 5144 | free_pushed_string_input (); |
726f6388 | 5145 | /* Cancel traps, in trap.c. */ |
d1fab3dc | 5146 | restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */ |
726f6388 | 5147 | setup_async_signals (); |
d3ad40de | 5148 | subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB; |
726f6388 | 5149 | } |
ccc6cda3 JA |
5150 | |
5151 | #if defined (JOB_CONTROL) | |
726f6388 JA |
5152 | set_sigchld_handler (); |
5153 | stop_making_children (); | |
866961ad | 5154 | /* XXX - should we only do this in the parent? (as in command subst) */ |
726f6388 | 5155 | pipeline_pgrp = old_pipeline_pgrp; |
ccc6cda3 | 5156 | #endif /* JOB_CONTROL */ |
726f6388 JA |
5157 | |
5158 | if (pid < 0) | |
5159 | { | |
5e13499c | 5160 | sys_error (_("cannot make child for process substitution")); |
726f6388 JA |
5161 | free (pathname); |
5162 | #if defined (HAVE_DEV_FD) | |
5163 | close (parent_pipe_fd); | |
5164 | close (child_pipe_fd); | |
5165 | #endif /* HAVE_DEV_FD */ | |
5166 | return ((char *)NULL); | |
5167 | } | |
5168 | ||
5169 | if (pid > 0) | |
5170 | { | |
ccc6cda3 JA |
5171 | #if defined (JOB_CONTROL) |
5172 | restore_pipeline (1); | |
5173 | #endif | |
5174 | ||
f73dda09 JA |
5175 | #if !defined (HAVE_DEV_FD) |
5176 | fifo_list[nfifo-1].proc = pid; | |
5177 | #endif | |
5178 | ||
726f6388 JA |
5179 | last_made_pid = old_pid; |
5180 | ||
5181 | #if defined (JOB_CONTROL) && defined (PGRP_PIPE) | |
5182 | close_pgrp_pipe (); | |
5183 | #endif /* JOB_CONTROL && PGRP_PIPE */ | |
5184 | ||
5185 | #if defined (HAVE_DEV_FD) | |
5186 | close (child_pipe_fd); | |
5187 | #endif /* HAVE_DEV_FD */ | |
5188 | ||
5189 | return (pathname); | |
5190 | } | |
5191 | ||
5192 | set_sigint_handler (); | |
5193 | ||
5194 | #if defined (JOB_CONTROL) | |
5195 | set_job_control (0); | |
5196 | #endif /* JOB_CONTROL */ | |
5197 | ||
5198 | #if !defined (HAVE_DEV_FD) | |
5199 | /* Open the named pipe in the child. */ | |
d76edd30 | 5200 | fd = open (pathname, open_for_read_in_child ? O_RDONLY : O_WRONLY); |
726f6388 JA |
5201 | if (fd < 0) |
5202 | { | |
5e13499c CR |
5203 | /* Two separate strings for ease of translation. */ |
5204 | if (open_for_read_in_child) | |
5205 | sys_error (_("cannot open named pipe %s for reading"), pathname); | |
5206 | else | |
5207 | sys_error (_("cannot open named pipe %s for writing"), pathname); | |
5208 | ||
726f6388 JA |
5209 | exit (127); |
5210 | } | |
bb70624e JA |
5211 | if (open_for_read_in_child) |
5212 | { | |
28ef6c31 | 5213 | if (sh_unset_nodelay_mode (fd) < 0) |
bb70624e | 5214 | { |
d3ad40de | 5215 | sys_error (_("cannot reset nodelay mode for fd %d"), fd); |
bb70624e JA |
5216 | exit (127); |
5217 | } | |
5218 | } | |
726f6388 JA |
5219 | #else /* HAVE_DEV_FD */ |
5220 | fd = child_pipe_fd; | |
5221 | #endif /* HAVE_DEV_FD */ | |
5222 | ||
5223 | if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0) | |
5224 | { | |
5e13499c | 5225 | sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname, |
ccc6cda3 | 5226 | open_for_read_in_child ? 0 : 1); |
726f6388 JA |
5227 | exit (127); |
5228 | } | |
5229 | ||
f73dda09 JA |
5230 | if (fd != (open_for_read_in_child ? 0 : 1)) |
5231 | close (fd); | |
726f6388 JA |
5232 | |
5233 | /* Need to close any files that this process has open to pipes inherited | |
5234 | from its parent. */ | |
5235 | if (current_fds_to_close) | |
5236 | { | |
5237 | close_fd_bitmap (current_fds_to_close); | |
5238 | current_fds_to_close = (struct fd_bitmap *)NULL; | |
5239 | } | |
5240 | ||
5241 | #if defined (HAVE_DEV_FD) | |
5242 | /* Make sure we close the parent's end of the pipe and clear the slot | |
5243 | in the fd list so it is not closed later, if reallocated by, for | |
5244 | instance, pipe(2). */ | |
5245 | close (parent_pipe_fd); | |
5246 | dev_fd_list[parent_pipe_fd] = 0; | |
5247 | #endif /* HAVE_DEV_FD */ | |
5248 | ||
3087e51c CR |
5249 | /* subshells shouldn't have this flag, which controls using the temporary |
5250 | environment for variable lookups. */ | |
5251 | expanding_redir = 0; | |
5252 | ||
d166f048 | 5253 | result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST)); |
726f6388 JA |
5254 | |
5255 | #if !defined (HAVE_DEV_FD) | |
5256 | /* Make sure we close the named pipe in the child before we exit. */ | |
5257 | close (open_for_read_in_child ? 0 : 1); | |
5258 | #endif /* !HAVE_DEV_FD */ | |
5259 | ||
a37d979e CR |
5260 | last_command_exit_value = result; |
5261 | result = run_exit_trap (); | |
726f6388 JA |
5262 | exit (result); |
5263 | /*NOTREACHED*/ | |
5264 | } | |
5265 | #endif /* PROCESS_SUBSTITUTION */ | |
5266 | ||
cce855bc JA |
5267 | /***********************************/ |
5268 | /* */ | |
5269 | /* Command Substitution */ | |
5270 | /* */ | |
5271 | /***********************************/ | |
5272 | ||
d166f048 | 5273 | static char * |
d3ad40de | 5274 | read_comsub (fd, quoted, rflag) |
d166f048 | 5275 | int fd, quoted; |
d3ad40de | 5276 | int *rflag; |
d166f048 | 5277 | { |
e6598ba4 CR |
5278 | char *istring, buf[128], *bufp, *s; |
5279 | int istring_index, istring_size, c, tflag, skip_ctlesc, skip_ctlnul; | |
f73dda09 | 5280 | ssize_t bufn; |
d166f048 JA |
5281 | |
5282 | istring = (char *)NULL; | |
d3ad40de | 5283 | istring_index = istring_size = bufn = tflag = 0; |
d166f048 | 5284 | |
e6598ba4 CR |
5285 | for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++) |
5286 | skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL; | |
5287 | ||
d3ad40de CR |
5288 | /* Read the output of the command through the pipe. This may need to be |
5289 | changed to understand multibyte characters in the future. */ | |
d166f048 JA |
5290 | while (1) |
5291 | { | |
5292 | if (fd < 0) | |
28ef6c31 | 5293 | break; |
d166f048 JA |
5294 | if (--bufn <= 0) |
5295 | { | |
bb70624e | 5296 | bufn = zread (fd, buf, sizeof (buf)); |
d166f048 JA |
5297 | if (bufn <= 0) |
5298 | break; | |
5299 | bufp = buf; | |
5300 | } | |
5301 | c = *bufp++; | |
5302 | ||
28ef6c31 JA |
5303 | if (c == 0) |
5304 | { | |
5305 | #if 0 | |
5306 | internal_warning ("read_comsub: ignored null byte in input"); | |
5307 | #endif | |
5308 | continue; | |
5309 | } | |
5310 | ||
d166f048 JA |
5311 | /* Add the character to ISTRING, possibly after resizing it. */ |
5312 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE); | |
5313 | ||
d3ad40de CR |
5314 | /* This is essentially quote_string inline */ |
5315 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */) | |
5316 | istring[istring_index++] = CTLESC; | |
5317 | /* Escape CTLESC and CTLNUL in the output to protect those characters | |
5318 | from the rest of the word expansions (word splitting and globbing.) | |
5319 | This is essentially quote_escapes inline. */ | |
e6598ba4 | 5320 | else if (skip_ctlesc == 0 && c == CTLESC) |
d3ad40de CR |
5321 | { |
5322 | tflag |= W_HASCTLESC; | |
5323 | istring[istring_index++] = CTLESC; | |
5324 | } | |
e6598ba4 | 5325 | else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0))) |
d166f048 JA |
5326 | istring[istring_index++] = CTLESC; |
5327 | ||
5328 | istring[istring_index++] = c; | |
28ef6c31 JA |
5329 | |
5330 | #if 0 | |
5331 | #if defined (__CYGWIN__) | |
5332 | if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r') | |
5333 | { | |
5334 | istring_index--; | |
5335 | istring[istring_index - 1] = '\n'; | |
5336 | } | |
5337 | #endif | |
5338 | #endif | |
d166f048 JA |
5339 | } |
5340 | ||
5341 | if (istring) | |
5342 | istring[istring_index] = '\0'; | |
5343 | ||
5344 | /* If we read no output, just return now and save ourselves some | |
5345 | trouble. */ | |
5346 | if (istring_index == 0) | |
5347 | { | |
5348 | FREE (istring); | |
d3ad40de CR |
5349 | if (rflag) |
5350 | *rflag = tflag; | |
d166f048 JA |
5351 | return (char *)NULL; |
5352 | } | |
5353 | ||
5354 | /* Strip trailing newlines from the output of the command. */ | |
5355 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
5356 | { | |
5357 | while (istring_index > 0) | |
5358 | { | |
5359 | if (istring[istring_index - 1] == '\n') | |
5360 | { | |
5361 | --istring_index; | |
5362 | ||
5363 | /* If the newline was quoted, remove the quoting char. */ | |
5364 | if (istring[istring_index - 1] == CTLESC) | |
5365 | --istring_index; | |
5366 | } | |
5367 | else | |
5368 | break; | |
5369 | } | |
5370 | istring[istring_index] = '\0'; | |
5371 | } | |
5372 | else | |
5373 | strip_trailing (istring, istring_index - 1, 1); | |
5374 | ||
d3ad40de CR |
5375 | if (rflag) |
5376 | *rflag = tflag; | |
d166f048 JA |
5377 | return istring; |
5378 | } | |
5379 | ||
d3ad40de CR |
5380 | /* Perform command substitution on STRING. This returns a WORD_DESC * with the |
5381 | contained string possibly quoted. */ | |
5382 | WORD_DESC * | |
726f6388 JA |
5383 | command_substitute (string, quoted) |
5384 | char *string; | |
5385 | int quoted; | |
5386 | { | |
cac4cdbf | 5387 | pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid; |
ccc6cda3 | 5388 | char *istring; |
d3ad40de CR |
5389 | int result, fildes[2], function_value, pflags, rc, tflag; |
5390 | WORD_DESC *ret; | |
726f6388 | 5391 | |
ccc6cda3 | 5392 | istring = (char *)NULL; |
726f6388 JA |
5393 | |
5394 | /* Don't fork () if there is no need to. In the case of no command to | |
5395 | run, just return NULL. */ | |
5396 | if (!string || !*string || (string[0] == '\n' && !string[1])) | |
d3ad40de | 5397 | return ((WORD_DESC *)NULL); |
726f6388 | 5398 | |
cce855bc JA |
5399 | if (wordexp_only && read_but_dont_execute) |
5400 | { | |
3eb2d94a | 5401 | last_command_exit_value = EX_WEXPCOMSUB; |
cce855bc JA |
5402 | jump_to_top_level (EXITPROG); |
5403 | } | |
5404 | ||
bb70624e JA |
5405 | /* We're making the assumption here that the command substitution will |
5406 | eventually run a command from the file system. Since we'll run | |
5407 | maybe_make_export_env in this subshell before executing that command, | |
5408 | the parent shell and any other shells it starts will have to remake | |
5409 | the environment. If we make it before we fork, other shells won't | |
5410 | have to. Don't bother if we have any temporary variable assignments, | |
5411 | though, because the export environment will be remade after this | |
5412 | command completes anyway, but do it if all the words to be expanded | |
5413 | are variable assignments. */ | |
5414 | if (subst_assign_varlist == 0 || garglist == 0) | |
5415 | maybe_make_export_env (); /* XXX */ | |
5416 | ||
d3a24ed2 | 5417 | /* Flags to pass to parse_and_execute() */ |
f486d0a1 | 5418 | pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0; |
d3a24ed2 | 5419 | |
726f6388 JA |
5420 | /* Pipe the output of executing STRING into the current shell. */ |
5421 | if (pipe (fildes) < 0) | |
5422 | { | |
5e13499c | 5423 | sys_error (_("cannot make pipe for command substitution")); |
726f6388 JA |
5424 | goto error_exit; |
5425 | } | |
5426 | ||
5427 | old_pid = last_made_pid; | |
5428 | #if defined (JOB_CONTROL) | |
ccc6cda3 | 5429 | old_pipeline_pgrp = pipeline_pgrp; |
28ef6c31 JA |
5430 | /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */ |
5431 | if ((subshell_environment & SUBSHELL_PIPE) == 0) | |
5432 | pipeline_pgrp = shell_pgrp; | |
ccc6cda3 | 5433 | cleanup_the_pipeline (); |
da719982 | 5434 | #endif /* JOB_CONTROL */ |
726f6388 | 5435 | |
cac4cdbf | 5436 | old_async_pid = last_asynchronous_pid; |
cac4cdbf | 5437 | pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC); |
cac4cdbf CR |
5438 | last_asynchronous_pid = old_async_pid; |
5439 | ||
726f6388 | 5440 | if (pid == 0) |
64419627 CR |
5441 | { |
5442 | /* Reset the signal handlers in the child, but don't free the | |
5443 | trap strings. Set a flag noting that we have to free the | |
5444 | trap strings if we run trap to change a signal disposition. */ | |
5445 | reset_signal_handlers (); | |
5446 | subshell_environment |= SUBSHELL_RESETTRAP; | |
5447 | } | |
ccc6cda3 JA |
5448 | |
5449 | #if defined (JOB_CONTROL) | |
525739ba | 5450 | /* XXX DO THIS ONLY IN PARENT ? XXX */ |
ccc6cda3 JA |
5451 | set_sigchld_handler (); |
5452 | stop_making_children (); | |
525739ba | 5453 | if (pid != 0) |
525739ba | 5454 | pipeline_pgrp = old_pipeline_pgrp; |
f73dda09 JA |
5455 | #else |
5456 | stop_making_children (); | |
ccc6cda3 | 5457 | #endif /* JOB_CONTROL */ |
726f6388 JA |
5458 | |
5459 | if (pid < 0) | |
5460 | { | |
5e13499c | 5461 | sys_error (_("cannot make child for command substitution")); |
726f6388 JA |
5462 | error_exit: |
5463 | ||
fd58d46e CR |
5464 | last_made_pid = old_pid; |
5465 | ||
726f6388 JA |
5466 | FREE (istring); |
5467 | close (fildes[0]); | |
5468 | close (fildes[1]); | |
d3ad40de | 5469 | return ((WORD_DESC *)NULL); |
726f6388 JA |
5470 | } |
5471 | ||
5472 | if (pid == 0) | |
5473 | { | |
5474 | set_sigint_handler (); /* XXX */ | |
28ef6c31 | 5475 | |
d3a24ed2 CR |
5476 | free_pushed_string_input (); |
5477 | ||
726f6388 JA |
5478 | if (dup2 (fildes[1], 1) < 0) |
5479 | { | |
5e13499c | 5480 | sys_error (_("command_substitute: cannot duplicate pipe as fd 1")); |
726f6388 JA |
5481 | exit (EXECUTION_FAILURE); |
5482 | } | |
5483 | ||
5484 | /* If standard output is closed in the parent shell | |
5485 | (such as after `exec >&-'), file descriptor 1 will be | |
5486 | the lowest available file descriptor, and end up in | |
5487 | fildes[0]. This can happen for stdin and stderr as well, | |
5488 | but stdout is more important -- it will cause no output | |
5489 | to be generated from this command. */ | |
5490 | if ((fildes[1] != fileno (stdin)) && | |
5491 | (fildes[1] != fileno (stdout)) && | |
5492 | (fildes[1] != fileno (stderr))) | |
5493 | close (fildes[1]); | |
5494 | ||
5495 | if ((fildes[0] != fileno (stdin)) && | |
5496 | (fildes[0] != fileno (stdout)) && | |
5497 | (fildes[0] != fileno (stderr))) | |
5498 | close (fildes[0]); | |
5499 | ||
54a1fa7c CR |
5500 | #ifdef __CYGWIN__ |
5501 | /* Let stdio know the fd may have changed from text to binary mode, and | |
5502 | make sure to preserve stdout line buffering. */ | |
5503 | freopen (NULL, "w", stdout); | |
5504 | sh_setlinebuf (stdout); | |
5505 | #endif /* __CYGWIN__ */ | |
5506 | ||
726f6388 JA |
5507 | /* The currently executing shell is not interactive. */ |
5508 | interactive = 0; | |
5509 | ||
ccc6cda3 | 5510 | /* This is a subshell environment. */ |
28ef6c31 | 5511 | subshell_environment |= SUBSHELL_COMSUB; |
ccc6cda3 | 5512 | |
25a0eacf CR |
5513 | /* Many shells do not appear to inherit the -v option for command |
5514 | substitutions. */ | |
5515 | change_flag ('v', FLAG_OFF); | |
5516 | ||
28ef6c31 | 5517 | /* When not in POSIX mode, command substitution does not inherit |
7117c2d2 | 5518 | the -e flag. */ |
28ef6c31 | 5519 | if (posixly_correct == 0) |
26a8e19c | 5520 | { |
77b3aacb CR |
5521 | builtin_ignoring_errexit = 0; |
5522 | change_flag ('e', FLAG_OFF); | |
26a8e19c CR |
5523 | set_shellopts (); |
5524 | } | |
726f6388 JA |
5525 | |
5526 | remove_quoted_escapes (string); | |
5527 | ||
ccc6cda3 | 5528 | startup_state = 2; /* see if we can avoid a fork */ |
726f6388 JA |
5529 | /* Give command substitution a place to jump back to on failure, |
5530 | so we don't go back up to main (). */ | |
36eb585c | 5531 | result = setjmp_nosigs (top_level); |
726f6388 | 5532 | |
bb70624e JA |
5533 | /* If we're running a command substitution inside a shell function, |
5534 | trap `return' so we don't return from the function in the subshell | |
5535 | and go off to never-never land. */ | |
5536 | if (result == 0 && return_catch_flag) | |
36eb585c | 5537 | function_value = setjmp_nosigs (return_catch); |
bb70624e JA |
5538 | else |
5539 | function_value = 0; | |
5540 | ||
d3a24ed2 | 5541 | if (result == ERREXIT) |
5e13499c | 5542 | rc = last_command_exit_value; |
d3a24ed2 | 5543 | else if (result == EXITPROG) |
5e13499c | 5544 | rc = last_command_exit_value; |
726f6388 | 5545 | else if (result) |
5e13499c | 5546 | rc = EXECUTION_FAILURE; |
bb70624e | 5547 | else if (function_value) |
5e13499c | 5548 | rc = return_catch_value; |
726f6388 | 5549 | else |
d3a24ed2 CR |
5550 | { |
5551 | subshell_level++; | |
5552 | rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST); | |
5553 | subshell_level--; | |
d3a24ed2 | 5554 | } |
5e13499c CR |
5555 | |
5556 | last_command_exit_value = rc; | |
5557 | rc = run_exit_trap (); | |
d3ad40de CR |
5558 | #if defined (PROCESS_SUBSTITUTION) |
5559 | unlink_fifo_list (); | |
5560 | #endif | |
5e13499c | 5561 | exit (rc); |
726f6388 JA |
5562 | } |
5563 | else | |
5564 | { | |
726f6388 JA |
5565 | #if defined (JOB_CONTROL) && defined (PGRP_PIPE) |
5566 | close_pgrp_pipe (); | |
5567 | #endif /* JOB_CONTROL && PGRP_PIPE */ | |
5568 | ||
5569 | close (fildes[1]); | |
5570 | ||
d3ad40de CR |
5571 | tflag = 0; |
5572 | istring = read_comsub (fildes[0], quoted, &tflag); | |
ccc6cda3 | 5573 | |
726f6388 JA |
5574 | close (fildes[0]); |
5575 | ||
b72432fd | 5576 | current_command_subst_pid = pid; |
726f6388 JA |
5577 | last_command_exit_value = wait_for (pid); |
5578 | last_command_subst_pid = pid; | |
5579 | last_made_pid = old_pid; | |
5580 | ||
5581 | #if defined (JOB_CONTROL) | |
5582 | /* If last_command_exit_value > 128, then the substituted command | |
5583 | was terminated by a signal. If that signal was SIGINT, then send | |
5584 | SIGINT to ourselves. This will break out of loops, for instance. */ | |
d3a24ed2 | 5585 | if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT) |
726f6388 JA |
5586 | kill (getpid (), SIGINT); |
5587 | ||
5588 | /* wait_for gives the terminal back to shell_pgrp. If some other | |
cce855bc JA |
5589 | process group should have it, give it away to that group here. |
5590 | pipeline_pgrp is non-zero only while we are constructing a | |
39feef01 | 5591 | pipeline, so what we are concerned about is whether or not that |
cce855bc JA |
5592 | pipeline was started in the background. A pipeline started in |
5593 | the background should never get the tty back here. */ | |
28ef6c31 | 5594 | if (interactive && pipeline_pgrp != (pid_t)0 && (subshell_environment & SUBSHELL_ASYNC) == 0) |
28ef6c31 | 5595 | give_terminal_to (pipeline_pgrp, 0); |
726f6388 JA |
5596 | #endif /* JOB_CONTROL */ |
5597 | ||
d3ad40de CR |
5598 | ret = alloc_word_desc (); |
5599 | ret->word = istring; | |
5600 | ret->flags = tflag; | |
5601 | ||
5602 | return ret; | |
726f6388 JA |
5603 | } |
5604 | } | |
5605 | ||
5606 | /******************************************************** | |
5607 | * * | |
5608 | * Utility functions for parameter expansion * | |
5609 | * * | |
5610 | ********************************************************/ | |
5611 | ||
ccc6cda3 | 5612 | #if defined (ARRAY_VARS) |
ccc6cda3 | 5613 | |
f73dda09 | 5614 | static arrayind_t |
ccc6cda3 JA |
5615 | array_length_reference (s) |
5616 | char *s; | |
5617 | { | |
f73dda09 JA |
5618 | int len; |
5619 | arrayind_t ind; | |
fdf670ea | 5620 | char *akey; |
f73dda09 | 5621 | char *t, c; |
ccc6cda3 | 5622 | ARRAY *array; |
68dfe178 | 5623 | HASH_TABLE *h; |
ccc6cda3 JA |
5624 | SHELL_VAR *var; |
5625 | ||
5626 | var = array_variable_part (s, &t, &len); | |
726f6388 | 5627 | |
ccc6cda3 JA |
5628 | /* If unbound variables should generate an error, report one and return |
5629 | failure. */ | |
a37d979e | 5630 | if ((var == 0 || invisible_p (var) || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error) |
726f6388 | 5631 | { |
f73dda09 | 5632 | c = *--t; |
ccc6cda3 | 5633 | *t = '\0'; |
0d8616ff | 5634 | last_command_exit_value = EXECUTION_FAILURE; |
7117c2d2 | 5635 | err_unboundvar (s); |
f73dda09 | 5636 | *t = c; |
ccc6cda3 | 5637 | return (-1); |
726f6388 | 5638 | } |
a37d979e | 5639 | else if (var == 0 || invisible_p (var)) |
ccc6cda3 | 5640 | return 0; |
726f6388 | 5641 | |
28ef6c31 JA |
5642 | /* We support a couple of expansions for variables that are not arrays. |
5643 | We'll return the length of the value for v[0], and 1 for v[@] or | |
5644 | v[*]. Return 0 for everything else. */ | |
5645 | ||
5646 | array = array_p (var) ? array_cell (var) : (ARRAY *)NULL; | |
68dfe178 | 5647 | h = assoc_p (var) ? assoc_cell (var) : (HASH_TABLE *)NULL; |
726f6388 | 5648 | |
ccc6cda3 | 5649 | if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']') |
ccc6cda3 | 5650 | { |
fdf670ea | 5651 | if (assoc_p (var)) |
68dfe178 | 5652 | return (h ? assoc_num_elements (h) : 0); |
fdf670ea | 5653 | else if (array_p (var)) |
68dfe178 | 5654 | return (array ? array_num_elements (array) : 0); |
fdf670ea | 5655 | else |
68dfe178 | 5656 | return (var_isset (var) ? 1 : 0); |
ccc6cda3 | 5657 | } |
ccc6cda3 | 5658 | |
fdf670ea CR |
5659 | if (assoc_p (var)) |
5660 | { | |
5661 | t[len - 1] = '\0'; | |
09767ff0 | 5662 | akey = expand_assignment_string_to_string (t, 0); /* [ */ |
fdf670ea CR |
5663 | t[len - 1] = ']'; |
5664 | if (akey == 0 || *akey == 0) | |
5665 | { | |
5666 | err_badarraysub (t); | |
631b20c6 | 5667 | FREE (akey); |
fdf670ea CR |
5668 | return (-1); |
5669 | } | |
5670 | t = assoc_reference (assoc_cell (var), akey); | |
631b20c6 | 5671 | free (akey); |
fdf670ea | 5672 | } |
28ef6c31 | 5673 | else |
fdf670ea | 5674 | { |
d9e1f41e | 5675 | ind = array_expand_index (var, t, len); |
a7ad477f CR |
5676 | /* negative subscripts to indexed arrays count back from end */ |
5677 | if (var && array_p (var) && ind < 0) | |
5678 | ind = array_max_index (array_cell (var)) + 1 + ind; | |
fdf670ea CR |
5679 | if (ind < 0) |
5680 | { | |
5681 | err_badarraysub (t); | |
5682 | return (-1); | |
5683 | } | |
5684 | if (array_p (var)) | |
5685 | t = array_reference (array, ind); | |
5686 | else | |
5687 | t = (ind == 0) ? value_cell (var) : (char *)NULL; | |
5688 | } | |
28ef6c31 | 5689 | |
30915f17 | 5690 | len = MB_STRLEN (t); |
ccc6cda3 | 5691 | return (len); |
726f6388 | 5692 | } |
ccc6cda3 | 5693 | #endif /* ARRAY_VARS */ |
726f6388 JA |
5694 | |
5695 | static int | |
5696 | valid_brace_expansion_word (name, var_is_special) | |
5697 | char *name; | |
5698 | int var_is_special; | |
5699 | { | |
f73dda09 | 5700 | if (DIGIT (*name) && all_digits (name)) |
726f6388 JA |
5701 | return 1; |
5702 | else if (var_is_special) | |
5703 | return 1; | |
ccc6cda3 JA |
5704 | #if defined (ARRAY_VARS) |
5705 | else if (valid_array_reference (name)) | |
5706 | return 1; | |
5707 | #endif /* ARRAY_VARS */ | |
726f6388 JA |
5708 | else if (legal_identifier (name)) |
5709 | return 1; | |
5710 | else | |
5711 | return 0; | |
5712 | } | |
ccc6cda3 | 5713 | |
d3a24ed2 CR |
5714 | static int |
5715 | chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at) | |
5716 | char *name; | |
5717 | int quoted; | |
5718 | int *quoted_dollar_atp, *contains_dollar_at; | |
5719 | { | |
5720 | char *temp1; | |
5721 | ||
5722 | if (name == 0) | |
5723 | { | |
5724 | if (quoted_dollar_atp) | |
5725 | *quoted_dollar_atp = 0; | |
5726 | if (contains_dollar_at) | |
5727 | *contains_dollar_at = 0; | |
5728 | return 0; | |
5729 | } | |
5730 | ||
5731 | /* check for $@ and $* */ | |
5732 | if (name[0] == '@' && name[1] == 0) | |
5733 | { | |
5734 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
5735 | *quoted_dollar_atp = 1; | |
5736 | if (contains_dollar_at) | |
5737 | *contains_dollar_at = 1; | |
5738 | return 1; | |
5739 | } | |
5740 | else if (name[0] == '*' && name[1] == '\0' && quoted == 0) | |
5741 | { | |
5742 | if (contains_dollar_at) | |
5743 | *contains_dollar_at = 1; | |
5744 | return 1; | |
5745 | } | |
5746 | ||
5747 | /* Now check for ${array[@]} and ${array[*]} */ | |
5748 | #if defined (ARRAY_VARS) | |
5749 | else if (valid_array_reference (name)) | |
5750 | { | |
d0ca3503 | 5751 | temp1 = mbschr (name, '['); |
d3a24ed2 CR |
5752 | if (temp1 && temp1[1] == '@' && temp1[2] == ']') |
5753 | { | |
5754 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
5755 | *quoted_dollar_atp = 1; | |
5756 | if (contains_dollar_at) | |
5757 | *contains_dollar_at = 1; | |
5758 | return 1; | |
5759 | } /* [ */ | |
5760 | /* ${array[*]}, when unquoted, should be treated like ${array[@]}, | |
5761 | which should result in separate words even when IFS is unset. */ | |
5762 | if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0) | |
5763 | { | |
5764 | if (contains_dollar_at) | |
5765 | *contains_dollar_at = 1; | |
5766 | return 1; | |
5767 | } | |
5768 | } | |
5769 | #endif | |
5770 | return 0; | |
5771 | } | |
5772 | ||
726f6388 JA |
5773 | /* Parameter expand NAME, and return a new string which is the expansion, |
5774 | or NULL if there was no expansion. | |
5775 | VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in | |
5776 | the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that | |
5777 | NAME was found inside of a double-quoted expression. */ | |
227f982e | 5778 | static WORD_DESC * |
5f8cde23 | 5779 | parameter_brace_expand_word (name, var_is_special, quoted, pflags, indp) |
726f6388 | 5780 | char *name; |
1231ac47 | 5781 | int var_is_special, quoted, pflags; |
5f8cde23 | 5782 | arrayind_t *indp; |
726f6388 | 5783 | { |
227f982e | 5784 | WORD_DESC *ret; |
ccc6cda3 | 5785 | char *temp, *tt; |
7117c2d2 | 5786 | intmax_t arg_index; |
ccc6cda3 | 5787 | SHELL_VAR *var; |
b1a26c01 | 5788 | int atype, rflags; |
5f8cde23 | 5789 | arrayind_t ind; |
726f6388 | 5790 | |
227f982e CR |
5791 | ret = 0; |
5792 | temp = 0; | |
b1a26c01 | 5793 | rflags = 0; |
227f982e | 5794 | |
5f8cde23 CR |
5795 | if (indp) |
5796 | *indp = INTMAX_MIN; | |
5797 | ||
227f982e | 5798 | /* Handle multiple digit arguments, as in ${11}. */ |
f73dda09 | 5799 | if (legal_number (name, &arg_index)) |
7117c2d2 JA |
5800 | { |
5801 | tt = get_dollar_var_value (arg_index); | |
762a763b CR |
5802 | if (tt) |
5803 | temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) | |
5804 | ? quote_string (tt) | |
5805 | : quote_escapes (tt); | |
5806 | else | |
5807 | temp = (char *)NULL; | |
7117c2d2 JA |
5808 | FREE (tt); |
5809 | } | |
726f6388 JA |
5810 | else if (var_is_special) /* ${@} */ |
5811 | { | |
cce855bc | 5812 | int sindex; |
f73dda09 | 5813 | tt = (char *)xmalloc (2 + strlen (name)); |
cce855bc | 5814 | tt[sindex = 0] = '$'; |
726f6388 | 5815 | strcpy (tt + 1, name); |
7117c2d2 | 5816 | |
227f982e | 5817 | ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL, |
1231ac47 | 5818 | (int *)NULL, (int *)NULL, pflags); |
cce855bc | 5819 | free (tt); |
726f6388 | 5820 | } |
ccc6cda3 JA |
5821 | #if defined (ARRAY_VARS) |
5822 | else if (valid_array_reference (name)) | |
5823 | { | |
d42cb8c1 | 5824 | expand_arrayref: |
631b20c6 | 5825 | /* XXX - does this leak if name[@] or name[*]? */ |
208fdb50 CR |
5826 | if (pflags & PF_ASSIGNRHS) |
5827 | { | |
5828 | temp = array_variable_name (name, &tt, (int *)0); | |
5829 | if (ALL_ELEMENT_SUB (tt[0]) && tt[1] == ']') | |
5830 | temp = array_value (name, quoted|Q_DOUBLE_QUOTES, 0, &atype, &ind); | |
5831 | else | |
5832 | temp = array_value (name, quoted, 0, &atype, &ind); | |
5833 | } | |
5834 | else | |
5835 | temp = array_value (name, quoted, 0, &atype, &ind); | |
7117c2d2 | 5836 | if (atype == 0 && temp) |
5f8cde23 CR |
5837 | { |
5838 | temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) | |
5839 | ? quote_string (temp) | |
5840 | : quote_escapes (temp); | |
5841 | rflags |= W_ARRAYIND; | |
5842 | if (indp) | |
5843 | *indp = ind; | |
5844 | } | |
b1a26c01 CR |
5845 | else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
5846 | rflags |= W_HASQUOTEDNULL; | |
ccc6cda3 JA |
5847 | } |
5848 | #endif | |
5849 | else if (var = find_variable (name)) | |
5850 | { | |
7117c2d2 | 5851 | if (var_isset (var) && invisible_p (var) == 0) |
28ef6c31 | 5852 | { |
ccc6cda3 | 5853 | #if defined (ARRAY_VARS) |
fdf670ea CR |
5854 | if (assoc_p (var)) |
5855 | temp = assoc_reference (assoc_cell (var), "0"); | |
5856 | else if (array_p (var)) | |
5857 | temp = array_reference (array_cell (var), 0); | |
5858 | else | |
5859 | temp = value_cell (var); | |
ccc6cda3 JA |
5860 | #else |
5861 | temp = value_cell (var); | |
5862 | #endif | |
5863 | ||
5864 | if (temp) | |
762a763b CR |
5865 | temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
5866 | ? quote_string (temp) | |
5867 | : quote_escapes (temp); | |
28ef6c31 | 5868 | } |
ccc6cda3 JA |
5869 | else |
5870 | temp = (char *)NULL; | |
5871 | } | |
d42cb8c1 CR |
5872 | else if (var = find_variable_last_nameref (name)) |
5873 | { | |
5874 | temp = nameref_cell (var); | |
15623760 CR |
5875 | #if defined (ARRAY_VARS) |
5876 | /* Handle expanding nameref whose value is x[n] */ | |
d42cb8c1 CR |
5877 | if (temp && *temp && valid_array_reference (temp)) |
5878 | { | |
5879 | name = temp; | |
5880 | goto expand_arrayref; | |
5881 | } | |
c2fa6583 | 5882 | else |
d42cb8c1 | 5883 | #endif |
15623760 | 5884 | /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */ |
c2fa6583 | 5885 | if (temp && *temp && legal_identifier (temp) == 0) |
15623760 CR |
5886 | { |
5887 | last_command_exit_value = EXECUTION_FAILURE; | |
5888 | report_error (_("%s: invalid variable name for name reference"), temp); | |
5889 | temp = &expand_param_error; | |
5890 | } | |
5891 | else | |
5892 | temp = (char *)NULL; | |
5893 | } | |
726f6388 | 5894 | else |
ccc6cda3 | 5895 | temp = (char *)NULL; |
726f6388 | 5896 | |
227f982e CR |
5897 | if (ret == 0) |
5898 | { | |
5899 | ret = alloc_word_desc (); | |
5900 | ret->word = temp; | |
b1a26c01 | 5901 | ret->flags |= rflags; |
227f982e CR |
5902 | } |
5903 | return ret; | |
726f6388 JA |
5904 | } |
5905 | ||
c111d992 CR |
5906 | static char * |
5907 | parameter_brace_find_indir (name, var_is_special, quoted, find_nameref) | |
5908 | char *name; | |
5909 | int var_is_special, quoted, find_nameref; | |
5910 | { | |
5911 | char *temp, *t; | |
5912 | WORD_DESC *w; | |
5913 | SHELL_VAR *v; | |
5914 | ||
5915 | if (find_nameref && var_is_special == 0 && (v = find_variable_last_nameref (name)) && | |
5916 | nameref_p (v) && (t = nameref_cell (v)) && *t) | |
5917 | return (savestring (t)); | |
5918 | ||
5919 | /* If var_is_special == 0, and name is not an array reference, this does | |
5920 | more expansion than necessary. It should really look up the variable's | |
5921 | value and not try to expand it. */ | |
5922 | w = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND, 0); | |
5923 | t = w->word; | |
5924 | /* Have to dequote here if necessary */ | |
5925 | if (t) | |
5926 | { | |
5927 | temp = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
5928 | ? dequote_string (t) | |
5929 | : dequote_escapes (t); | |
5930 | free (t); | |
5931 | t = temp; | |
5932 | } | |
5933 | dispose_word_desc (w); | |
5934 | ||
5935 | return t; | |
5936 | } | |
5937 | ||
ccc6cda3 JA |
5938 | /* Expand an indirect reference to a variable: ${!NAME} expands to the |
5939 | value of the variable whose name is the value of NAME. */ | |
227f982e | 5940 | static WORD_DESC * |
d3a24ed2 | 5941 | parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at) |
ccc6cda3 JA |
5942 | char *name; |
5943 | int var_is_special, quoted; | |
d3a24ed2 | 5944 | int *quoted_dollar_atp, *contains_dollar_at; |
ccc6cda3 JA |
5945 | { |
5946 | char *temp, *t; | |
227f982e | 5947 | WORD_DESC *w; |
d42cb8c1 CR |
5948 | SHELL_VAR *v; |
5949 | ||
5950 | /* See if it's a nameref first, behave in ksh93-compatible fashion. | |
5951 | There is at least one incompatibility: given ${!foo[0]} where foo=bar, | |
5952 | bash performs an indirect lookup on foo[0] and expands the result; | |
5953 | ksh93 expands bar[0]. We could do that here -- there are enough usable | |
5954 | primitives to do that -- but do not at this point. */ | |
5955 | if (var_is_special == 0 && (v = find_variable_last_nameref (name))) | |
5956 | { | |
5957 | if (nameref_p (v) && (t = nameref_cell (v)) && *t) | |
5958 | { | |
5959 | w = alloc_word_desc (); | |
5960 | w->word = savestring (t); | |
5961 | w->flags = 0; | |
5962 | return w; | |
5963 | } | |
5964 | } | |
ccc6cda3 | 5965 | |
c111d992 | 5966 | t = parameter_brace_find_indir (name, var_is_special, quoted, 0); |
227f982e | 5967 | |
d3a24ed2 | 5968 | chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at); |
ccc6cda3 | 5969 | if (t == 0) |
227f982e CR |
5970 | return (WORD_DESC *)NULL; |
5971 | ||
5f8cde23 | 5972 | w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, 0, 0); |
ccc6cda3 | 5973 | free (t); |
227f982e CR |
5974 | |
5975 | return w; | |
ccc6cda3 JA |
5976 | } |
5977 | ||
726f6388 JA |
5978 | /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE}, |
5979 | depending on the value of C, the separating character. C can be one of | |
ccc6cda3 JA |
5980 | "-", "+", or "=". QUOTED is true if the entire brace expression occurs |
5981 | between double quotes. */ | |
227f982e | 5982 | static WORD_DESC * |
ccc6cda3 | 5983 | parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat) |
726f6388 | 5984 | char *name, *value; |
ccc6cda3 | 5985 | int c, quoted, *qdollaratp, *hasdollarat; |
726f6388 | 5986 | { |
227f982e | 5987 | WORD_DESC *w; |
726f6388 | 5988 | WORD_LIST *l; |
b4a17eba | 5989 | char *t, *t1, *temp, *vname; |
ccc6cda3 | 5990 | int hasdol; |
726f6388 | 5991 | |
ccc6cda3 JA |
5992 | /* If the entire expression is between double quotes, we want to treat |
5993 | the value as a double-quoted string, with the exception that we strip | |
d3ad40de | 5994 | embedded unescaped double quotes (for sh backwards compatibility). */ |
22e63b05 | 5995 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value) |
726f6388 | 5996 | { |
ccc6cda3 | 5997 | hasdol = 0; |
22e63b05 | 5998 | temp = string_extract_double_quoted (value, &hasdol, 1); |
726f6388 | 5999 | } |
22e63b05 CR |
6000 | else |
6001 | temp = value; | |
ccc6cda3 | 6002 | |
227f982e | 6003 | w = alloc_word_desc (); |
726f6388 | 6004 | hasdol = 0; |
ccc6cda3 JA |
6005 | /* XXX was 0 not quoted */ |
6006 | l = *temp ? expand_string_for_rhs (temp, quoted, &hasdol, (int *)NULL) | |
6007 | : (WORD_LIST *)0; | |
6008 | if (hasdollarat) | |
6009 | *hasdollarat = hasdol || (l && l->next); | |
22e63b05 CR |
6010 | if (temp != value) |
6011 | free (temp); | |
726f6388 JA |
6012 | if (l) |
6013 | { | |
ccc6cda3 | 6014 | /* The expansion of TEMP returned something. We need to treat things |
12d937f9 CR |
6015 | slightly differently if HASDOL is non-zero. If we have "$@", the |
6016 | individual words have already been quoted. We need to turn them | |
6017 | into a string with the words separated by the first character of | |
6018 | $IFS without any additional quoting, so string_list_dollar_at won't | |
6019 | do the right thing. We use string_list_dollar_star instead. */ | |
6020 | temp = (hasdol || l->next) ? string_list_dollar_star (l) : string_list (l); | |
6021 | ||
ccc6cda3 JA |
6022 | /* If l->next is not null, we know that TEMP contained "$@", since that |
6023 | is the only expansion that creates more than one word. */ | |
12d937f9 | 6024 | if (qdollaratp && ((hasdol && quoted) || l->next)) |
ccc6cda3 | 6025 | *qdollaratp = 1; |
ec860d76 CR |
6026 | /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is |
6027 | a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the | |
6028 | flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the | |
6029 | expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
6030 | (which is more paranoia than anything else), we need to return the | |
6031 | quoted null string and set the flags to indicate it. */ | |
6032 | if (l->next == 0 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp) && QUOTED_NULL (l->word->word) && (l->word->flags & W_HASQUOTEDNULL)) | |
6033 | { | |
6034 | w->flags |= W_HASQUOTEDNULL; | |
6035 | } | |
726f6388 JA |
6036 | dispose_words (l); |
6037 | } | |
ccc6cda3 | 6038 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && hasdol) |
726f6388 | 6039 | { |
ccc6cda3 JA |
6040 | /* The brace expansion occurred between double quotes and there was |
6041 | a $@ in TEMP. It does not matter if the $@ is quoted, as long as | |
7117c2d2 JA |
6042 | it does not expand to anything. In this case, we want to return |
6043 | a quoted empty string. */ | |
d3ad40de | 6044 | temp = make_quoted_char ('\0'); |
227f982e | 6045 | w->flags |= W_HASQUOTEDNULL; |
726f6388 JA |
6046 | } |
6047 | else | |
6048 | temp = (char *)NULL; | |
6049 | ||
6050 | if (c == '-' || c == '+') | |
227f982e CR |
6051 | { |
6052 | w->word = temp; | |
6053 | return w; | |
6054 | } | |
726f6388 JA |
6055 | |
6056 | /* c == '=' */ | |
ccc6cda3 | 6057 | t = temp ? savestring (temp) : savestring (""); |
726f6388 JA |
6058 | t1 = dequote_string (t); |
6059 | free (t); | |
b4a17eba CR |
6060 | |
6061 | /* bash-4.4/5.0 */ | |
6062 | vname = name; | |
6063 | if (*name == '!' && | |
6064 | (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) || VALID_INDIR_PARAM (name[1]))) | |
6065 | { | |
6066 | vname = parameter_brace_find_indir (name + 1, SPECIAL_VAR (name, 1), quoted, 1); | |
6067 | if (vname == 0 || *vname == 0) | |
6068 | { | |
6069 | report_error (_("%s: invalid indirect expansion"), name); | |
6070 | free (vname); | |
6071 | dispose_word (w); | |
6072 | return &expand_wdesc_error; | |
6073 | } | |
6074 | if (legal_identifier (vname) == 0) | |
6075 | { | |
6076 | report_error (_("%s: invalid variable name"), vname); | |
6077 | free (vname); | |
6078 | dispose_word (w); | |
6079 | return &expand_wdesc_error; | |
6080 | } | |
6081 | } | |
6082 | ||
d3a24ed2 | 6083 | #if defined (ARRAY_VARS) |
b4a17eba CR |
6084 | if (valid_array_reference (vname)) |
6085 | assign_array_element (vname, t1, 0); | |
d3a24ed2 CR |
6086 | else |
6087 | #endif /* ARRAY_VARS */ | |
b4a17eba | 6088 | bind_variable (vname, t1, 0); |
f0c4de40 | 6089 | #if 0 |
b4a17eba | 6090 | if (STREQ (vname, "IFS") == 0) |
f0c4de40 | 6091 | #endif |
b4a17eba CR |
6092 | stupidly_hack_special_variables (vname); |
6093 | ||
6094 | if (vname != name) | |
6095 | free (vname); | |
227f982e | 6096 | |
9e51a74d | 6097 | /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */ |
64419627 CR |
6098 | free (temp); |
6099 | ||
6100 | w->word = t1; | |
227f982e | 6101 | return w; |
726f6388 JA |
6102 | } |
6103 | ||
6104 | /* Deal with the right hand side of a ${name:?value} expansion in the case | |
6105 | that NAME is null or not set. If VALUE is non-null it is expanded and | |
6106 | used as the error message to print, otherwise a standard message is | |
6107 | printed. */ | |
6108 | static void | |
6109 | parameter_brace_expand_error (name, value) | |
6110 | char *name, *value; | |
6111 | { | |
ccc6cda3 JA |
6112 | WORD_LIST *l; |
6113 | char *temp; | |
6114 | ||
7f947b68 | 6115 | last_command_exit_value = EXECUTION_FAILURE; /* ensure it's non-zero */ |
726f6388 JA |
6116 | if (value && *value) |
6117 | { | |
22e63b05 | 6118 | l = expand_string (value, 0); |
ccc6cda3 JA |
6119 | temp = string_list (l); |
6120 | report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */ | |
6121 | FREE (temp); | |
726f6388 JA |
6122 | dispose_words (l); |
6123 | } | |
6124 | else | |
5e13499c | 6125 | report_error (_("%s: parameter null or not set"), name); |
726f6388 JA |
6126 | |
6127 | /* Free the data we have allocated during this expansion, since we | |
6128 | are about to longjmp out. */ | |
6129 | free (name); | |
6130 | FREE (value); | |
6131 | } | |
6132 | ||
6133 | /* Return 1 if NAME is something for which parameter_brace_expand_length is | |
6134 | OK to do. */ | |
6135 | static int | |
6136 | valid_length_expression (name) | |
6137 | char *name; | |
6138 | { | |
28ef6c31 | 6139 | return (name[1] == '\0' || /* ${#} */ |
f73dda09 JA |
6140 | ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */ |
6141 | (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */ | |
ccc6cda3 JA |
6142 | #if defined (ARRAY_VARS) |
6143 | valid_array_reference (name + 1) || /* ${#a[7]} */ | |
6144 | #endif | |
726f6388 JA |
6145 | legal_identifier (name + 1)); /* ${#PS1} */ |
6146 | } | |
6147 | ||
6148 | /* Handle the parameter brace expansion that requires us to return the | |
6149 | length of a parameter. */ | |
7117c2d2 | 6150 | static intmax_t |
726f6388 JA |
6151 | parameter_brace_expand_length (name) |
6152 | char *name; | |
6153 | { | |
ccc6cda3 | 6154 | char *t, *newname; |
7117c2d2 | 6155 | intmax_t number, arg_index; |
ccc6cda3 JA |
6156 | WORD_LIST *list; |
6157 | #if defined (ARRAY_VARS) | |
6158 | SHELL_VAR *var; | |
6159 | #endif | |
6160 | ||
6161 | if (name[1] == '\0') /* ${#} */ | |
6162 | number = number_of_args (); | |
cce855bc JA |
6163 | else if ((name[1] == '@' || name[1] == '*') && name[2] == '\0') /* ${#@}, ${#*} */ |
6164 | number = number_of_args (); | |
f73dda09 | 6165 | else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') |
cce855bc JA |
6166 | { |
6167 | /* Take the lengths of some of the shell's special parameters. */ | |
6168 | switch (name[1]) | |
6169 | { | |
6170 | case '-': | |
6171 | t = which_set_flags (); | |
6172 | break; | |
6173 | case '?': | |
6174 | t = itos (last_command_exit_value); | |
6175 | break; | |
6176 | case '$': | |
6177 | t = itos (dollar_dollar_pid); | |
6178 | break; | |
6179 | case '!': | |
6180 | if (last_asynchronous_pid == NO_PID) | |
e05be32d | 6181 | t = (char *)NULL; /* XXX - error if set -u set? */ |
cce855bc | 6182 | else |
f73dda09 | 6183 | t = itos (last_asynchronous_pid); |
cce855bc JA |
6184 | break; |
6185 | case '#': | |
6186 | t = itos (number_of_args ()); | |
6187 | break; | |
6188 | } | |
6189 | number = STRLEN (t); | |
6190 | FREE (t); | |
6191 | } | |
ccc6cda3 JA |
6192 | #if defined (ARRAY_VARS) |
6193 | else if (valid_array_reference (name + 1)) | |
6194 | number = array_length_reference (name + 1); | |
6195 | #endif /* ARRAY_VARS */ | |
cce855bc | 6196 | else |
ccc6cda3 JA |
6197 | { |
6198 | number = 0; | |
6199 | ||
f73dda09 | 6200 | if (legal_number (name + 1, &arg_index)) /* ${#1} */ |
ccc6cda3 | 6201 | { |
f73dda09 | 6202 | t = get_dollar_var_value (arg_index); |
e05be32d CR |
6203 | if (t == 0 && unbound_vars_is_error) |
6204 | return INTMAX_MIN; | |
898cc92e | 6205 | number = MB_STRLEN (t); |
ccc6cda3 JA |
6206 | FREE (t); |
6207 | } | |
6208 | #if defined (ARRAY_VARS) | |
fdf670ea | 6209 | else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var))) |
ccc6cda3 | 6210 | { |
fdf670ea CR |
6211 | if (assoc_p (var)) |
6212 | t = assoc_reference (assoc_cell (var), "0"); | |
6213 | else | |
6214 | t = array_reference (array_cell (var), 0); | |
e05be32d CR |
6215 | if (t == 0 && unbound_vars_is_error) |
6216 | return INTMAX_MIN; | |
898cc92e | 6217 | number = MB_STRLEN (t); |
ccc6cda3 JA |
6218 | } |
6219 | #endif | |
6220 | else /* ${#PS1} */ | |
6221 | { | |
6222 | newname = savestring (name); | |
6223 | newname[0] = '$'; | |
6224 | list = expand_string (newname, Q_DOUBLE_QUOTES); | |
6225 | t = list ? string_list (list) : (char *)NULL; | |
6226 | free (newname); | |
6227 | if (list) | |
6228 | dispose_words (list); | |
6229 | ||
adc6cff5 | 6230 | number = t ? MB_STRLEN (t) : 0; |
ccc6cda3 JA |
6231 | FREE (t); |
6232 | } | |
6233 | } | |
ccc6cda3 JA |
6234 | |
6235 | return (number); | |
6236 | } | |
6237 | ||
28ef6c31 JA |
6238 | /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression, |
6239 | so we do some ad-hoc parsing of an arithmetic expression to find | |
6240 | the first DELIM, instead of using strchr(3). Two rules: | |
6241 | 1. If the substring contains a `(', read until closing `)'. | |
6242 | 2. If the substring contains a `?', read past one `:' for each `?'. | |
6243 | */ | |
6244 | ||
6245 | static char * | |
6246 | skiparith (substr, delim) | |
6247 | char *substr; | |
6248 | int delim; | |
6249 | { | |
7117c2d2 JA |
6250 | size_t sublen; |
6251 | int skipcol, pcount, i; | |
6252 | DECLARE_MBSTATE; | |
28ef6c31 | 6253 | |
7117c2d2 JA |
6254 | sublen = strlen (substr); |
6255 | i = skipcol = pcount = 0; | |
6256 | while (substr[i]) | |
28ef6c31 JA |
6257 | { |
6258 | /* Balance parens */ | |
7117c2d2 | 6259 | if (substr[i] == LPAREN) |
28ef6c31 JA |
6260 | { |
6261 | pcount++; | |
7117c2d2 | 6262 | i++; |
28ef6c31 JA |
6263 | continue; |
6264 | } | |
7117c2d2 | 6265 | if (substr[i] == RPAREN && pcount) |
28ef6c31 JA |
6266 | { |
6267 | pcount--; | |
7117c2d2 | 6268 | i++; |
28ef6c31 JA |
6269 | continue; |
6270 | } | |
6271 | if (pcount) | |
7117c2d2 JA |
6272 | { |
6273 | ADVANCE_CHAR (substr, sublen, i); | |
6274 | continue; | |
6275 | } | |
28ef6c31 JA |
6276 | |
6277 | /* Skip one `:' for each `?' */ | |
7117c2d2 | 6278 | if (substr[i] == ':' && skipcol) |
28ef6c31 JA |
6279 | { |
6280 | skipcol--; | |
7117c2d2 | 6281 | i++; |
28ef6c31 JA |
6282 | continue; |
6283 | } | |
7117c2d2 | 6284 | if (substr[i] == delim) |
28ef6c31 | 6285 | break; |
7117c2d2 | 6286 | if (substr[i] == '?') |
28ef6c31 JA |
6287 | { |
6288 | skipcol++; | |
7117c2d2 | 6289 | i++; |
28ef6c31 JA |
6290 | continue; |
6291 | } | |
7117c2d2 | 6292 | ADVANCE_CHAR (substr, sublen, i); |
28ef6c31 | 6293 | } |
7117c2d2 JA |
6294 | |
6295 | return (substr + i); | |
28ef6c31 JA |
6296 | } |
6297 | ||
ccc6cda3 JA |
6298 | /* Verify and limit the start and end of the desired substring. If |
6299 | VTYPE == 0, a regular shell variable is being used; if it is 1, | |
cce855bc | 6300 | then the positional parameters are being used; if it is 2, then |
e8ce775d JA |
6301 | VALUE is really a pointer to an array variable that should be used. |
6302 | Return value is 1 if both values were OK, 0 if there was a problem | |
6303 | with an invalid expression, or -1 if the values were out of range. */ | |
ccc6cda3 | 6304 | static int |
09767ff0 CR |
6305 | verify_substring_values (v, value, substr, vtype, e1p, e2p) |
6306 | SHELL_VAR *v; | |
ccc6cda3 | 6307 | char *value, *substr; |
f73dda09 | 6308 | int vtype; |
7117c2d2 | 6309 | intmax_t *e1p, *e2p; |
ccc6cda3 | 6310 | { |
bb70624e | 6311 | char *t, *temp1, *temp2; |
f73dda09 JA |
6312 | arrayind_t len; |
6313 | int expok; | |
ccc6cda3 JA |
6314 | #if defined (ARRAY_VARS) |
6315 | ARRAY *a; | |
09767ff0 | 6316 | HASH_TABLE *h; |
ccc6cda3 JA |
6317 | #endif |
6318 | ||
28ef6c31 JA |
6319 | /* duplicate behavior of strchr(3) */ |
6320 | t = skiparith (substr, ':'); | |
6321 | if (*t && *t == ':') | |
7117c2d2 | 6322 | *t = '\0'; |
28ef6c31 JA |
6323 | else |
6324 | t = (char *)0; | |
f73dda09 | 6325 | |
d3ad40de | 6326 | temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES); |
d166f048 | 6327 | *e1p = evalexp (temp1, &expok); |
ccc6cda3 | 6328 | free (temp1); |
d166f048 JA |
6329 | if (expok == 0) |
6330 | return (0); | |
ccc6cda3 | 6331 | |
f73dda09 | 6332 | len = -1; /* paranoia */ |
ccc6cda3 JA |
6333 | switch (vtype) |
6334 | { | |
6335 | case VT_VARIABLE: | |
d166f048 | 6336 | case VT_ARRAYMEMBER: |
898cc92e | 6337 | len = MB_STRLEN (value); |
ccc6cda3 JA |
6338 | break; |
6339 | case VT_POSPARMS: | |
6340 | len = number_of_args () + 1; | |
d3ad40de CR |
6341 | if (*e1p == 0) |
6342 | len++; /* add one arg if counting from $0 */ | |
ccc6cda3 JA |
6343 | break; |
6344 | #if defined (ARRAY_VARS) | |
6345 | case VT_ARRAYVAR: | |
301e2142 | 6346 | /* For arrays, the first value deals with array indices. Negative |
09767ff0 CR |
6347 | offsets count from one past the array's maximum index. Associative |
6348 | arrays treat the number of elements as the maximum index. */ | |
6349 | if (assoc_p (v)) | |
6350 | { | |
6351 | h = assoc_cell (v); | |
6352 | len = assoc_num_elements (h) + (*e1p < 0); | |
6353 | } | |
6354 | else | |
6355 | { | |
6356 | a = (ARRAY *)value; | |
6357 | len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */ | |
6358 | } | |
ccc6cda3 JA |
6359 | break; |
6360 | #endif | |
6361 | } | |
6362 | ||
f73dda09 JA |
6363 | if (len == -1) /* paranoia */ |
6364 | return -1; | |
6365 | ||
ccc6cda3 JA |
6366 | if (*e1p < 0) /* negative offsets count from end */ |
6367 | *e1p += len; | |
6368 | ||
8ed8525c | 6369 | if (*e1p > len || *e1p < 0) |
e8ce775d | 6370 | return (-1); |
d166f048 | 6371 | |
5e13499c CR |
6372 | #if defined (ARRAY_VARS) |
6373 | /* For arrays, the second offset deals with the number of elements. */ | |
6374 | if (vtype == VT_ARRAYVAR) | |
dd4f3dd8 | 6375 | len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a); |
5e13499c CR |
6376 | #endif |
6377 | ||
ccc6cda3 JA |
6378 | if (t) |
6379 | { | |
6380 | t++; | |
bb70624e | 6381 | temp2 = savestring (t); |
d3ad40de | 6382 | temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES); |
bb70624e | 6383 | free (temp2); |
ccc6cda3 | 6384 | t[-1] = ':'; |
d166f048 | 6385 | *e2p = evalexp (temp1, &expok); |
ccc6cda3 | 6386 | free (temp1); |
d166f048 | 6387 | if (expok == 0) |
28ef6c31 | 6388 | return (0); |
348a457e | 6389 | #if 1 |
67362c60 | 6390 | if ((vtype == VT_ARRAYVAR || vtype == VT_POSPARMS) && *e2p < 0) |
348a457e CR |
6391 | #else |
6392 | /* bash-4.3: allow positional parameter length < 0 to count backwards | |
6393 | from end of positional parameters */ | |
6394 | if (vtype == VT_ARRAYVAR && *e2p < 0) | |
6395 | #endif | |
28ef6c31 | 6396 | { |
5e13499c | 6397 | internal_error (_("%s: substring expression < 0"), t); |
ccc6cda3 | 6398 | return (0); |
28ef6c31 | 6399 | } |
5e13499c CR |
6400 | #if defined (ARRAY_VARS) |
6401 | /* In order to deal with sparse arrays, push the intelligence about how | |
6402 | to deal with the number of elements desired down to the array- | |
6403 | specific functions. */ | |
6404 | if (vtype != VT_ARRAYVAR) | |
6405 | #endif | |
6406 | { | |
67362c60 CR |
6407 | if (*e2p < 0) |
6408 | { | |
6409 | *e2p += len; | |
6410 | if (*e2p < 0 || *e2p < *e1p) | |
6411 | { | |
6412 | internal_error (_("%s: substring expression < 0"), t); | |
6413 | return (0); | |
6414 | } | |
6415 | } | |
6416 | else | |
6417 | *e2p += *e1p; /* want E2 chars starting at E1 */ | |
5e13499c CR |
6418 | if (*e2p > len) |
6419 | *e2p = len; | |
6420 | } | |
ccc6cda3 JA |
6421 | } |
6422 | else | |
6423 | *e2p = len; | |
6424 | ||
6425 | return (1); | |
6426 | } | |
6427 | ||
ccc6cda3 | 6428 | /* Return the type of variable specified by VARNAME (simple variable, |
cce855bc | 6429 | positional param, or array variable). Also return the value specified |
7117c2d2 | 6430 | by VARNAME (value of a variable or a reference to an array element). |
5f8cde23 CR |
6431 | QUOTED is the standard description of quoting state, using Q_* defines. |
6432 | FLAGS is currently a set of flags to pass to array_value. If IND is | |
6433 | non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is | |
6434 | passed to array_value so the array index is not computed again. | |
7117c2d2 JA |
6435 | If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL |
6436 | characters in the value are quoted with CTLESC and takes appropriate | |
6437 | steps. For convenience, *VALP is set to the dequoted VALUE. */ | |
ccc6cda3 | 6438 | static int |
5f8cde23 | 6439 | get_var_and_type (varname, value, ind, quoted, flags, varp, valp) |
ccc6cda3 | 6440 | char *varname, *value; |
5f8cde23 CR |
6441 | arrayind_t ind; |
6442 | int quoted, flags; | |
ccc6cda3 JA |
6443 | SHELL_VAR **varp; |
6444 | char **valp; | |
6445 | { | |
c111d992 CR |
6446 | int vtype, want_indir; |
6447 | char *temp, *vname; | |
6448 | WORD_DESC *wd; | |
ccc6cda3 JA |
6449 | #if defined (ARRAY_VARS) |
6450 | SHELL_VAR *v; | |
6451 | #endif | |
5f8cde23 | 6452 | arrayind_t lind; |
ccc6cda3 | 6453 | |
c111d992 CR |
6454 | want_indir = *varname == '!' && |
6455 | (legal_variable_starter ((unsigned char)varname[1]) || DIGIT (varname[1]) | |
6456 | || VALID_INDIR_PARAM (varname[1])); | |
6457 | if (want_indir) | |
6458 | vname = parameter_brace_find_indir (varname+1, SPECIAL_VAR (varname, 1), quoted, 1); | |
b4a17eba | 6459 | /* XXX - what if vname == 0 || *vname == 0 ? */ |
c111d992 CR |
6460 | else |
6461 | vname = varname; | |
6462 | ||
7117c2d2 | 6463 | /* This sets vtype to VT_VARIABLE or VT_POSPARMS */ |
c111d992 CR |
6464 | vtype = (vname[0] == '@' || vname[0] == '*') && vname[1] == '\0'; |
6465 | if (vtype == VT_POSPARMS && vname[0] == '*') | |
d3a24ed2 | 6466 | vtype |= VT_STARSUB; |
ccc6cda3 JA |
6467 | *varp = (SHELL_VAR *)NULL; |
6468 | ||
6469 | #if defined (ARRAY_VARS) | |
c111d992 | 6470 | if (valid_array_reference (vname)) |
ccc6cda3 | 6471 | { |
c111d992 | 6472 | v = array_variable_part (vname, &temp, (int *)0); |
5f8cde23 CR |
6473 | /* If we want to signal array_value to use an already-computed index, |
6474 | set LIND to that index */ | |
6475 | lind = (ind != INTMAX_MIN && (flags & AV_USEIND)) ? ind : 0; | |
a37d979e CR |
6476 | if (v && invisible_p (v)) |
6477 | { | |
6478 | vtype = VT_ARRAYMEMBER; | |
6479 | *varp = (SHELL_VAR *)NULL; | |
6480 | *valp = (char *)NULL; | |
6481 | } | |
fdf670ea | 6482 | if (v && (array_p (v) || assoc_p (v))) |
f73dda09 JA |
6483 | { /* [ */ |
6484 | if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']') | |
ccc6cda3 | 6485 | { |
39feef01 | 6486 | /* Callers have to differentiate between indexed and associative */ |
ccc6cda3 | 6487 | vtype = VT_ARRAYVAR; |
d3a24ed2 CR |
6488 | if (temp[0] == '*') |
6489 | vtype |= VT_STARSUB; | |
fdf670ea | 6490 | *valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v); |
ccc6cda3 JA |
6491 | } |
6492 | else | |
6493 | { | |
d166f048 | 6494 | vtype = VT_ARRAYMEMBER; |
c111d992 | 6495 | *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind); |
ccc6cda3 JA |
6496 | } |
6497 | *varp = v; | |
6498 | } | |
8fed3589 CR |
6499 | else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')) |
6500 | { | |
6501 | vtype = VT_VARIABLE; | |
6502 | *varp = v; | |
6503 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
6504 | *valp = dequote_string (value); | |
6505 | else | |
6506 | *valp = dequote_escapes (value); | |
6507 | } | |
ccc6cda3 | 6508 | else |
fdf670ea CR |
6509 | { |
6510 | vtype = VT_ARRAYMEMBER; | |
6511 | *varp = v; | |
c111d992 | 6512 | *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind); |
fdf670ea | 6513 | } |
ccc6cda3 | 6514 | } |
c111d992 | 6515 | else if ((v = find_variable (vname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v))) |
ccc6cda3 | 6516 | { |
7117c2d2 | 6517 | vtype = VT_ARRAYMEMBER; |
ccc6cda3 | 6518 | *varp = v; |
fdf670ea | 6519 | *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0); |
ccc6cda3 JA |
6520 | } |
6521 | else | |
6522 | #endif | |
762a763b CR |
6523 | { |
6524 | if (value && vtype == VT_VARIABLE) | |
6525 | { | |
6526 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
6527 | *valp = dequote_string (value); | |
6528 | else | |
6529 | *valp = dequote_escapes (value); | |
6530 | } | |
6531 | else | |
6532 | *valp = value; | |
6533 | } | |
ccc6cda3 | 6534 | |
c111d992 CR |
6535 | if (want_indir) |
6536 | free (vname); | |
6537 | ||
ccc6cda3 JA |
6538 | return vtype; |
6539 | } | |
6540 | ||
cce855bc JA |
6541 | /******************************************************/ |
6542 | /* */ | |
6543 | /* Functions to extract substrings of variable values */ | |
6544 | /* */ | |
6545 | /******************************************************/ | |
6546 | ||
545f34cf CR |
6547 | #if defined (HANDLE_MULTIBYTE) |
6548 | /* Character-oriented rather than strictly byte-oriented substrings. S and | |
6549 | E, rather being strict indices into STRING, indicate character (possibly | |
6550 | multibyte character) positions that require calculation. | |
6551 | Used by the ${param:offset[:length]} expansion. */ | |
6552 | static char * | |
6553 | mb_substring (string, s, e) | |
6554 | char *string; | |
6555 | int s, e; | |
6556 | { | |
6557 | char *tt; | |
6558 | int start, stop, i, slen; | |
6559 | DECLARE_MBSTATE; | |
6560 | ||
6561 | start = 0; | |
da713c23 CR |
6562 | /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */ |
6563 | slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0; | |
545f34cf CR |
6564 | |
6565 | i = s; | |
6566 | while (string[start] && i--) | |
6567 | ADVANCE_CHAR (string, slen, start); | |
6568 | stop = start; | |
6569 | i = e - s; | |
6570 | while (string[stop] && i--) | |
6571 | ADVANCE_CHAR (string, slen, stop); | |
6572 | tt = substring (string, start, stop); | |
6573 | return tt; | |
6574 | } | |
6575 | #endif | |
6576 | ||
ccc6cda3 JA |
6577 | /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME |
6578 | is `@', use the positional parameters; otherwise, use the value of | |
6579 | VARNAME. If VARNAME is an array variable, use the array elements. */ | |
6580 | ||
6581 | static char * | |
5f8cde23 CR |
6582 | parameter_brace_substring (varname, value, ind, substr, quoted, flags) |
6583 | char *varname, *value; | |
6584 | int ind; | |
6585 | char *substr; | |
6586 | int quoted, flags; | |
ccc6cda3 | 6587 | { |
7117c2d2 | 6588 | intmax_t e1, e2; |
d3a24ed2 | 6589 | int vtype, r, starsub; |
d3ad40de | 6590 | char *temp, *val, *tt, *oname; |
ccc6cda3 JA |
6591 | SHELL_VAR *v; |
6592 | ||
098cbb5c | 6593 | if (value == 0 && ((varname[0] != '@' && varname[0] != '*') || varname[1])) |
ccc6cda3 JA |
6594 | return ((char *)NULL); |
6595 | ||
d3ad40de | 6596 | oname = this_command_name; |
ccc6cda3 JA |
6597 | this_command_name = varname; |
6598 | ||
5f8cde23 | 6599 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
ccc6cda3 | 6600 | if (vtype == -1) |
d3ad40de CR |
6601 | { |
6602 | this_command_name = oname; | |
6603 | return ((char *)NULL); | |
6604 | } | |
ccc6cda3 | 6605 | |
d3a24ed2 CR |
6606 | starsub = vtype & VT_STARSUB; |
6607 | vtype &= ~VT_STARSUB; | |
6608 | ||
09767ff0 | 6609 | r = verify_substring_values (v, val, substr, vtype, &e1, &e2); |
d3ad40de | 6610 | this_command_name = oname; |
e8ce775d | 6611 | if (r <= 0) |
631b20c6 CR |
6612 | { |
6613 | if (vtype == VT_VARIABLE) | |
6614 | FREE (val); | |
6615 | return ((r == 0) ? &expand_param_error : (char *)NULL); | |
6616 | } | |
ccc6cda3 JA |
6617 | |
6618 | switch (vtype) | |
6619 | { | |
6620 | case VT_VARIABLE: | |
d166f048 | 6621 | case VT_ARRAYMEMBER: |
545f34cf CR |
6622 | #if defined (HANDLE_MULTIBYTE) |
6623 | if (MB_CUR_MAX > 1) | |
6624 | tt = mb_substring (val, e1, e2); | |
6625 | else | |
6626 | #endif | |
7117c2d2 | 6627 | tt = substring (val, e1, e2); |
545f34cf | 6628 | |
7117c2d2 JA |
6629 | if (vtype == VT_VARIABLE) |
6630 | FREE (val); | |
6631 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
6632 | temp = quote_string (tt); | |
6633 | else | |
6634 | temp = tt ? quote_escapes (tt) : (char *)NULL; | |
6635 | FREE (tt); | |
ccc6cda3 JA |
6636 | break; |
6637 | case VT_POSPARMS: | |
7117c2d2 JA |
6638 | tt = pos_params (varname, e1, e2, quoted); |
6639 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0) | |
6640 | { | |
6641 | temp = tt ? quote_escapes (tt) : (char *)NULL; | |
6642 | FREE (tt); | |
6643 | } | |
6644 | else | |
6645 | temp = tt; | |
ccc6cda3 JA |
6646 | break; |
6647 | #if defined (ARRAY_VARS) | |
6648 | case VT_ARRAYVAR: | |
fdf670ea | 6649 | if (assoc_p (v)) |
09767ff0 CR |
6650 | /* we convert to list and take first e2 elements starting at e1th |
6651 | element -- officially undefined for now */ | |
6652 | temp = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted); | |
fdf670ea | 6653 | else |
5e13499c | 6654 | /* We want E2 to be the number of elements desired (arrays can be sparse, |
704a1a2a CR |
6655 | so verify_substring_values just returns the numbers specified and we |
6656 | rely on array_subrange to understand how to deal with them). */ | |
fdf670ea | 6657 | temp = array_subrange (array_cell (v), e1, e2, starsub, quoted); |
d3ad40de CR |
6658 | /* array_subrange now calls array_quote_escapes as appropriate, so the |
6659 | caller no longer needs to. */ | |
ccc6cda3 JA |
6660 | break; |
6661 | #endif | |
f73dda09 JA |
6662 | default: |
6663 | temp = (char *)NULL; | |
ccc6cda3 JA |
6664 | } |
6665 | ||
6666 | return temp; | |
6667 | } | |
6668 | ||
cce855bc JA |
6669 | /****************************************************************/ |
6670 | /* */ | |
6671 | /* Functions to perform pattern substitution on variable values */ | |
6672 | /* */ | |
6673 | /****************************************************************/ | |
6674 | ||
ee56a865 CR |
6675 | static int |
6676 | shouldexp_replacement (s) | |
6677 | char *s; | |
6678 | { | |
6679 | register char *p; | |
6680 | ||
6681 | for (p = s; p && *p; p++) | |
6682 | { | |
6683 | if (*p == '\\') | |
6684 | p++; | |
6685 | else if (*p == '&') | |
6686 | return 1; | |
6687 | } | |
6688 | return 0; | |
6689 | } | |
6690 | ||
ccc6cda3 JA |
6691 | char * |
6692 | pat_subst (string, pat, rep, mflags) | |
6693 | char *string, *pat, *rep; | |
6694 | int mflags; | |
6695 | { | |
ee56a865 CR |
6696 | char *ret, *s, *e, *str, *rstr, *mstr; |
6697 | int rsize, rptr, l, replen, mtype, rxpand, rslen, mlen; | |
ccc6cda3 | 6698 | |
5f8cde23 CR |
6699 | if (string == 0) |
6700 | return (savestring ("")); | |
6701 | ||
b72432fd JA |
6702 | mtype = mflags & MATCH_TYPEMASK; |
6703 | ||
e054386f | 6704 | #if 0 /* bash-4.2 ? */ |
ee56a865 | 6705 | rxpand = (rep && *rep) ? shouldexp_replacement (rep) : 0; |
e054386f CR |
6706 | #else |
6707 | rxpand = 0; | |
6708 | #endif | |
ee56a865 | 6709 | |
b72432fd JA |
6710 | /* Special cases: |
6711 | * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING | |
6712 | * with REP and return the result. | |
6713 | * 2. A null pattern with mtype == MATCH_END means to append REP to | |
6714 | * STRING and return the result. | |
ee56a865 | 6715 | * These don't understand or process `&' in the replacement string. |
b72432fd JA |
6716 | */ |
6717 | if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END)) | |
6718 | { | |
6719 | replen = STRLEN (rep); | |
5f8cde23 | 6720 | l = STRLEN (string); |
f73dda09 | 6721 | ret = (char *)xmalloc (replen + l + 2); |
bb70624e JA |
6722 | if (replen == 0) |
6723 | strcpy (ret, string); | |
6724 | else if (mtype == MATCH_BEG) | |
b72432fd JA |
6725 | { |
6726 | strcpy (ret, rep); | |
6727 | strcpy (ret + replen, string); | |
6728 | } | |
6729 | else | |
6730 | { | |
6731 | strcpy (ret, string); | |
6732 | strcpy (ret + l, rep); | |
6733 | } | |
6734 | return (ret); | |
6735 | } | |
6736 | ||
f73dda09 | 6737 | ret = (char *)xmalloc (rsize = 64); |
ccc6cda3 JA |
6738 | ret[0] = '\0'; |
6739 | ||
ccc6cda3 JA |
6740 | for (replen = STRLEN (rep), rptr = 0, str = string;;) |
6741 | { | |
6742 | if (match_pattern (str, pat, mtype, &s, &e) == 0) | |
6743 | break; | |
6744 | l = s - str; | |
ee56a865 CR |
6745 | |
6746 | if (rxpand) | |
6747 | { | |
6748 | int x; | |
6749 | mlen = e - s; | |
6750 | mstr = xmalloc (mlen + 1); | |
6751 | for (x = 0; x < mlen; x++) | |
6752 | mstr[x] = s[x]; | |
6753 | mstr[mlen] = '\0'; | |
6754 | rstr = strcreplace (rep, '&', mstr, 0); | |
6755 | rslen = strlen (rstr); | |
6756 | } | |
6757 | else | |
6758 | { | |
6759 | rstr = rep; | |
6760 | rslen = replen; | |
6761 | } | |
6762 | ||
6763 | RESIZE_MALLOCED_BUFFER (ret, rptr, (l + rslen), rsize, 64); | |
ccc6cda3 JA |
6764 | |
6765 | /* OK, now copy the leading unmatched portion of the string (from | |
6766 | str to s) to ret starting at rptr (the current offset). Then copy | |
28ef6c31 JA |
6767 | the replacement string at ret + rptr + (s - str). Increment |
6768 | rptr (if necessary) and str and go on. */ | |
ccc6cda3 JA |
6769 | if (l) |
6770 | { | |
6771 | strncpy (ret + rptr, str, l); | |
6772 | rptr += l; | |
6773 | } | |
6774 | if (replen) | |
6775 | { | |
ee56a865 CR |
6776 | strncpy (ret + rptr, rstr, rslen); |
6777 | rptr += rslen; | |
ccc6cda3 JA |
6778 | } |
6779 | str = e; /* e == end of match */ | |
d3a24ed2 | 6780 | |
ee56a865 CR |
6781 | if (rstr != rep) |
6782 | free (rstr); | |
6783 | ||
ccc6cda3 | 6784 | if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY) |
28ef6c31 | 6785 | break; |
d3a24ed2 CR |
6786 | |
6787 | if (s == e) | |
5ccad779 CR |
6788 | { |
6789 | /* On a zero-length match, make sure we copy one character, since | |
6790 | we increment one character to avoid infinite recursion. */ | |
6791 | RESIZE_MALLOCED_BUFFER (ret, rptr, 1, rsize, 64); | |
6792 | ret[rptr++] = *str++; | |
6793 | e++; /* avoid infinite recursion on zero-length match */ | |
6794 | } | |
ccc6cda3 JA |
6795 | } |
6796 | ||
6797 | /* Now copy the unmatched portion of the input string */ | |
5f8cde23 | 6798 | if (str && *str) |
d166f048 JA |
6799 | { |
6800 | RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64); | |
6801 | strcpy (ret + rptr, str); | |
6802 | } | |
ccc6cda3 JA |
6803 | else |
6804 | ret[rptr] = '\0'; | |
6805 | ||
6806 | return ret; | |
6807 | } | |
6808 | ||
6809 | /* Do pattern match and replacement on the positional parameters. */ | |
6810 | static char * | |
6811 | pos_params_pat_subst (string, pat, rep, mflags) | |
6812 | char *string, *pat, *rep; | |
6813 | int mflags; | |
6814 | { | |
6815 | WORD_LIST *save, *params; | |
6816 | WORD_DESC *w; | |
d3ad40de | 6817 | char *ret; |
e33f2203 | 6818 | int pchar, qflags; |
ccc6cda3 JA |
6819 | |
6820 | save = params = list_rest_of_args (); | |
6821 | if (save == 0) | |
6822 | return ((char *)NULL); | |
6823 | ||
6824 | for ( ; params; params = params->next) | |
6825 | { | |
6826 | ret = pat_subst (params->word->word, pat, rep, mflags); | |
227f982e CR |
6827 | w = alloc_word_desc (); |
6828 | w->word = ret ? ret : savestring (""); | |
ccc6cda3 JA |
6829 | dispose_word (params->word); |
6830 | params->word = w; | |
ccc6cda3 JA |
6831 | } |
6832 | ||
e33f2203 CR |
6833 | pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@'; |
6834 | qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0; | |
6835 | ||
e33f2203 | 6836 | ret = string_list_pos_params (pchar, save, qflags); |
e33f2203 | 6837 | |
ccc6cda3 JA |
6838 | dispose_words (save); |
6839 | ||
6840 | return (ret); | |
6841 | } | |
6842 | ||
cce855bc JA |
6843 | /* Perform pattern substitution on VALUE, which is the expansion of |
6844 | VARNAME. PATSUB is an expression supplying the pattern to match | |
6845 | and the string to substitute. QUOTED is a flags word containing | |
6846 | the type of quoting currently in effect. */ | |
ccc6cda3 | 6847 | static char * |
5f8cde23 CR |
6848 | parameter_brace_patsub (varname, value, ind, patsub, quoted, flags) |
6849 | char *varname, *value; | |
6850 | int ind; | |
6851 | char *patsub; | |
6852 | int quoted, flags; | |
ccc6cda3 | 6853 | { |
c2a47ea9 | 6854 | int vtype, mflags, starsub, delim; |
7117c2d2 | 6855 | char *val, *temp, *pat, *rep, *p, *lpatsub, *tt; |
ccc6cda3 JA |
6856 | SHELL_VAR *v; |
6857 | ||
6858 | if (value == 0) | |
6859 | return ((char *)NULL); | |
6860 | ||
6861 | this_command_name = varname; | |
6862 | ||
5f8cde23 | 6863 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
ccc6cda3 JA |
6864 | if (vtype == -1) |
6865 | return ((char *)NULL); | |
6866 | ||
d3a24ed2 CR |
6867 | starsub = vtype & VT_STARSUB; |
6868 | vtype &= ~VT_STARSUB; | |
6869 | ||
ccc6cda3 | 6870 | mflags = 0; |
77638cbf CR |
6871 | /* PATSUB is never NULL when this is called. */ |
6872 | if (*patsub == '/') | |
d3ad40de CR |
6873 | { |
6874 | mflags |= MATCH_GLOBREP; | |
6875 | patsub++; | |
6876 | } | |
7117c2d2 JA |
6877 | |
6878 | /* Malloc this because expand_string_if_necessary or one of the expansion | |
6879 | functions in its call chain may free it on a substitution error. */ | |
bb70624e | 6880 | lpatsub = savestring (patsub); |
ccc6cda3 JA |
6881 | |
6882 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
6883 | mflags |= MATCH_QUOTED; | |
6884 | ||
d3a24ed2 CR |
6885 | if (starsub) |
6886 | mflags |= MATCH_STARSUB; | |
6887 | ||
dc8fbaf9 CR |
6888 | /* If the pattern starts with a `/', make sure we skip over it when looking |
6889 | for the replacement delimiter. */ | |
4d8d005b | 6890 | delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0); |
c2a47ea9 CR |
6891 | if (lpatsub[delim] == '/') |
6892 | { | |
6893 | lpatsub[delim] = 0; | |
6894 | rep = lpatsub + delim + 1; | |
6895 | } | |
6896 | else | |
6897 | rep = (char *)NULL; | |
ccc6cda3 JA |
6898 | |
6899 | if (rep && *rep == '\0') | |
6900 | rep = (char *)NULL; | |
6901 | ||
5e13499c CR |
6902 | /* Perform the same expansions on the pattern as performed by the |
6903 | pattern removal expansions. */ | |
6904 | pat = getpattern (lpatsub, quoted, 1); | |
bb70624e | 6905 | |
ccc6cda3 | 6906 | if (rep) |
7175a77f CR |
6907 | { |
6908 | /* We want to perform quote removal on the expanded replacement even if | |
6909 | the entire expansion is double-quoted because the parser and string | |
6910 | extraction functions treated quotes in the replacement string as | |
6911 | special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */ | |
6912 | if (shell_compatibility_level > 42) | |
6913 | rep = expand_string_if_necessary (rep, quoted & ~(Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT), expand_string_unsplit); | |
6914 | /* This is the bash-4.2 code. */ | |
6915 | else if ((mflags & MATCH_QUOTED) == 0) | |
6916 | rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit); | |
6917 | else | |
6918 | rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit); | |
6919 | } | |
ccc6cda3 | 6920 | |
dc8fbaf9 | 6921 | /* ksh93 doesn't allow the match specifier to be a part of the expanded |
d3ad40de CR |
6922 | pattern. This is an extension. Make sure we don't anchor the pattern |
6923 | at the beginning or end of the string if we're doing global replacement, | |
6924 | though. */ | |
ccc6cda3 | 6925 | p = pat; |
d3ad40de CR |
6926 | if (mflags & MATCH_GLOBREP) |
6927 | mflags |= MATCH_ANY; | |
dc8fbaf9 | 6928 | else if (pat && pat[0] == '#') |
ccc6cda3 JA |
6929 | { |
6930 | mflags |= MATCH_BEG; | |
6931 | p++; | |
6932 | } | |
d166f048 | 6933 | else if (pat && pat[0] == '%') |
ccc6cda3 JA |
6934 | { |
6935 | mflags |= MATCH_END; | |
6936 | p++; | |
6937 | } | |
6938 | else | |
6939 | mflags |= MATCH_ANY; | |
6940 | ||
cce855bc JA |
6941 | /* OK, we now want to substitute REP for PAT in VAL. If |
6942 | flags & MATCH_GLOBREP is non-zero, the substitution is done | |
6943 | everywhere, otherwise only the first occurrence of PAT is | |
7117c2d2 JA |
6944 | replaced. The pattern matching code doesn't understand |
6945 | CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable | |
6946 | values passed in (VT_VARIABLE) so the pattern substitution | |
6947 | code works right. We need to requote special chars after | |
6948 | we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the | |
6949 | other cases if QUOTED == 0, since the posparams and arrays | |
6950 | indexed by * or @ do special things when QUOTED != 0. */ | |
6951 | ||
ccc6cda3 JA |
6952 | switch (vtype) |
6953 | { | |
6954 | case VT_VARIABLE: | |
d166f048 | 6955 | case VT_ARRAYMEMBER: |
ccc6cda3 | 6956 | temp = pat_subst (val, p, rep, mflags); |
7117c2d2 JA |
6957 | if (vtype == VT_VARIABLE) |
6958 | FREE (val); | |
6959 | if (temp) | |
6960 | { | |
e6598ba4 | 6961 | tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp); |
7117c2d2 JA |
6962 | free (temp); |
6963 | temp = tt; | |
6964 | } | |
ccc6cda3 JA |
6965 | break; |
6966 | case VT_POSPARMS: | |
6967 | temp = pos_params_pat_subst (val, p, rep, mflags); | |
7117c2d2 JA |
6968 | if (temp && (mflags & MATCH_QUOTED) == 0) |
6969 | { | |
6970 | tt = quote_escapes (temp); | |
6971 | free (temp); | |
6972 | temp = tt; | |
6973 | } | |
ccc6cda3 JA |
6974 | break; |
6975 | #if defined (ARRAY_VARS) | |
6976 | case VT_ARRAYVAR: | |
fdf670ea CR |
6977 | temp = assoc_p (v) ? assoc_patsub (assoc_cell (v), p, rep, mflags) |
6978 | : array_patsub (array_cell (v), p, rep, mflags); | |
e6598ba4 CR |
6979 | /* Don't call quote_escapes anymore; array_patsub calls |
6980 | array_quote_escapes as appropriate before adding the | |
fdf670ea | 6981 | space separators; ditto for assoc_patsub. */ |
ccc6cda3 JA |
6982 | break; |
6983 | #endif | |
6984 | } | |
6985 | ||
6986 | FREE (pat); | |
6987 | FREE (rep); | |
bb70624e | 6988 | free (lpatsub); |
ccc6cda3 JA |
6989 | |
6990 | return temp; | |
6991 | } | |
6992 | ||
09767ff0 CR |
6993 | /****************************************************************/ |
6994 | /* */ | |
6995 | /* Functions to perform case modification on variable values */ | |
6996 | /* */ | |
6997 | /****************************************************************/ | |
6998 | ||
6999 | /* Do case modification on the positional parameters. */ | |
7000 | ||
7001 | static char * | |
7002 | pos_params_modcase (string, pat, modop, mflags) | |
7003 | char *string, *pat; | |
7004 | int modop; | |
7005 | int mflags; | |
7006 | { | |
7007 | WORD_LIST *save, *params; | |
7008 | WORD_DESC *w; | |
7009 | char *ret; | |
7010 | int pchar, qflags; | |
7011 | ||
7012 | save = params = list_rest_of_args (); | |
7013 | if (save == 0) | |
7014 | return ((char *)NULL); | |
7015 | ||
7016 | for ( ; params; params = params->next) | |
7017 | { | |
7018 | ret = sh_modcase (params->word->word, pat, modop); | |
7019 | w = alloc_word_desc (); | |
7020 | w->word = ret ? ret : savestring (""); | |
7021 | dispose_word (params->word); | |
7022 | params->word = w; | |
7023 | } | |
7024 | ||
7025 | pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@'; | |
7026 | qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0; | |
7027 | ||
7028 | ret = string_list_pos_params (pchar, save, qflags); | |
7029 | dispose_words (save); | |
7030 | ||
7031 | return (ret); | |
7032 | } | |
7033 | ||
7034 | /* Perform case modification on VALUE, which is the expansion of | |
7035 | VARNAME. MODSPEC is an expression supplying the type of modification | |
7036 | to perform. QUOTED is a flags word containing the type of quoting | |
7037 | currently in effect. */ | |
7038 | static char * | |
5f8cde23 | 7039 | parameter_brace_casemod (varname, value, ind, modspec, patspec, quoted, flags) |
09767ff0 | 7040 | char *varname, *value; |
5f8cde23 | 7041 | int ind, modspec; |
09767ff0 | 7042 | char *patspec; |
5f8cde23 | 7043 | int quoted, flags; |
09767ff0 CR |
7044 | { |
7045 | int vtype, starsub, modop, mflags, x; | |
7046 | char *val, *temp, *pat, *p, *lpat, *tt; | |
7047 | SHELL_VAR *v; | |
7048 | ||
7049 | if (value == 0) | |
7050 | return ((char *)NULL); | |
7051 | ||
7052 | this_command_name = varname; | |
7053 | ||
5f8cde23 | 7054 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
09767ff0 CR |
7055 | if (vtype == -1) |
7056 | return ((char *)NULL); | |
7057 | ||
7058 | starsub = vtype & VT_STARSUB; | |
7059 | vtype &= ~VT_STARSUB; | |
7060 | ||
7061 | modop = 0; | |
7062 | mflags = 0; | |
7063 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
7064 | mflags |= MATCH_QUOTED; | |
7065 | if (starsub) | |
7066 | mflags |= MATCH_STARSUB; | |
7067 | ||
7068 | p = patspec; | |
7069 | if (modspec == '^') | |
7070 | { | |
7071 | x = p && p[0] == modspec; | |
e141c35a | 7072 | modop = x ? CASE_UPPER : CASE_UPFIRST; |
09767ff0 CR |
7073 | p += x; |
7074 | } | |
7075 | else if (modspec == ',') | |
7076 | { | |
7077 | x = p && p[0] == modspec; | |
e141c35a | 7078 | modop = x ? CASE_LOWER : CASE_LOWFIRST; |
09767ff0 CR |
7079 | p += x; |
7080 | } | |
7081 | else if (modspec == '~') | |
7082 | { | |
7083 | x = p && p[0] == modspec; | |
7084 | modop = x ? CASE_TOGGLEALL : CASE_TOGGLE; | |
7085 | p += x; | |
7086 | } | |
7087 | ||
7088 | lpat = p ? savestring (p) : 0; | |
7089 | /* Perform the same expansions on the pattern as performed by the | |
7090 | pattern removal expansions. FOR LATER */ | |
7091 | pat = lpat ? getpattern (lpat, quoted, 1) : 0; | |
7092 | ||
7093 | /* OK, now we do the case modification. */ | |
7094 | switch (vtype) | |
7095 | { | |
7096 | case VT_VARIABLE: | |
7097 | case VT_ARRAYMEMBER: | |
7098 | temp = sh_modcase (val, pat, modop); | |
7099 | if (vtype == VT_VARIABLE) | |
7100 | FREE (val); | |
7101 | if (temp) | |
7102 | { | |
7103 | tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp); | |
7104 | free (temp); | |
7105 | temp = tt; | |
7106 | } | |
7107 | break; | |
7108 | ||
7109 | case VT_POSPARMS: | |
7110 | temp = pos_params_modcase (val, pat, modop, mflags); | |
7111 | if (temp && (mflags & MATCH_QUOTED) == 0) | |
7112 | { | |
7113 | tt = quote_escapes (temp); | |
7114 | free (temp); | |
7115 | temp = tt; | |
7116 | } | |
7117 | break; | |
7118 | ||
7119 | #if defined (ARRAY_VARS) | |
7120 | case VT_ARRAYVAR: | |
7121 | temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags) | |
7122 | : array_modcase (array_cell (v), pat, modop, mflags); | |
7123 | /* Don't call quote_escapes; array_modcase calls array_quote_escapes | |
7124 | as appropriate before adding the space separators; ditto for | |
7125 | assoc_modcase. */ | |
7126 | break; | |
7127 | #endif | |
7128 | } | |
7129 | ||
7130 | FREE (pat); | |
7131 | free (lpat); | |
7132 | ||
7133 | return temp; | |
7134 | } | |
7135 | ||
d3ad40de CR |
7136 | /* Check for unbalanced parens in S, which is the contents of $(( ... )). If |
7137 | any occur, this must be a nested command substitution, so return 0. | |
7138 | Otherwise, return 1. A valid arithmetic expression must always have a | |
7139 | ( before a matching ), so any cases where there are more right parens | |
7140 | means that this must not be an arithmetic expression, though the parser | |
7141 | will not accept it without a balanced total number of parens. */ | |
7142 | static int | |
7143 | chk_arithsub (s, len) | |
7144 | const char *s; | |
7145 | int len; | |
7146 | { | |
7147 | int i, count; | |
7148 | DECLARE_MBSTATE; | |
7149 | ||
7150 | i = count = 0; | |
7151 | while (i < len) | |
7152 | { | |
176b12ee | 7153 | if (s[i] == LPAREN) |
d3ad40de | 7154 | count++; |
176b12ee | 7155 | else if (s[i] == RPAREN) |
d3ad40de CR |
7156 | { |
7157 | count--; | |
7158 | if (count < 0) | |
7159 | return 0; | |
7160 | } | |
7161 | ||
7162 | switch (s[i]) | |
7163 | { | |
7164 | default: | |
7165 | ADVANCE_CHAR (s, len, i); | |
7166 | break; | |
7167 | ||
7168 | case '\\': | |
7169 | i++; | |
7170 | if (s[i]) | |
7171 | ADVANCE_CHAR (s, len, i); | |
7172 | break; | |
7173 | ||
7174 | case '\'': | |
7175 | i = skip_single_quoted (s, len, ++i); | |
7176 | break; | |
7177 | ||
7178 | case '"': | |
7179 | i = skip_double_quoted ((char *)s, len, ++i); | |
7180 | break; | |
7181 | } | |
7182 | } | |
7183 | ||
7184 | return (count == 0); | |
7185 | } | |
7186 | ||
cce855bc JA |
7187 | /****************************************************************/ |
7188 | /* */ | |
7189 | /* Functions to perform parameter expansion on a string */ | |
7190 | /* */ | |
7191 | /****************************************************************/ | |
7192 | ||
09767ff0 | 7193 | /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */ |
227f982e | 7194 | static WORD_DESC * |
e1e48bba | 7195 | parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at) |
ccc6cda3 | 7196 | char *string; |
e1e48bba | 7197 | int *indexp, quoted, *quoted_dollar_atp, *contains_dollar_at, pflags; |
ccc6cda3 JA |
7198 | { |
7199 | int check_nullness, var_is_set, var_is_null, var_is_special; | |
09767ff0 | 7200 | int want_substring, want_indir, want_patsub, want_casemod; |
ccc6cda3 | 7201 | char *name, *value, *temp, *temp1; |
227f982e | 7202 | WORD_DESC *tdesc, *ret; |
09767ff0 | 7203 | int t_index, sindex, c, tflag, modspec; |
7117c2d2 | 7204 | intmax_t number; |
5f8cde23 | 7205 | arrayind_t ind; |
ccc6cda3 | 7206 | |
c40a57dd | 7207 | temp = temp1 = value = (char *)NULL; |
ccc6cda3 | 7208 | var_is_set = var_is_null = var_is_special = check_nullness = 0; |
09767ff0 | 7209 | want_substring = want_indir = want_patsub = want_casemod = 0; |
ccc6cda3 | 7210 | |
cce855bc JA |
7211 | sindex = *indexp; |
7212 | t_index = ++sindex; | |
11a6f9a9 CR |
7213 | /* ${#var} doesn't have any of the other parameter expansions on it. */ |
7214 | if (string[t_index] == '#' && legal_variable_starter (string[t_index+1])) /* {{ */ | |
e6598ba4 | 7215 | name = string_extract (string, &t_index, "}", SX_VARNAME); |
11a6f9a9 | 7216 | else |
09767ff0 CR |
7217 | #if defined (CASEMOD_EXPANSIONS) |
7218 | /* To enable case-toggling expansions using the `~' operator character | |
7219 | change the 1 to 0. */ | |
7220 | # if defined (CASEMOD_CAPCASE) | |
7221 | name = string_extract (string, &t_index, "#%^,~:-=?+/}", SX_VARNAME); | |
7222 | # else | |
7223 | name = string_extract (string, &t_index, "#%^,:-=?+/}", SX_VARNAME); | |
7224 | # endif /* CASEMOD_CAPCASE */ | |
7225 | #else | |
e6598ba4 | 7226 | name = string_extract (string, &t_index, "#%:-=?+/}", SX_VARNAME); |
09767ff0 | 7227 | #endif /* CASEMOD_EXPANSIONS */ |
cce855bc | 7228 | |
227f982e CR |
7229 | ret = 0; |
7230 | tflag = 0; | |
7231 | ||
5f8cde23 CR |
7232 | ind = INTMAX_MIN; |
7233 | ||
cce855bc JA |
7234 | /* If the name really consists of a special variable, then make sure |
7235 | that we have the entire name. We don't allow indirect references | |
7236 | to special variables except `#', `?', `@' and `*'. */ | |
e05be32d CR |
7237 | if ((sindex == t_index && VALID_SPECIAL_LENGTH_PARAM (string[t_index])) || |
7238 | (sindex == t_index - 1 && string[sindex] == '!' && VALID_INDIR_PARAM (string[t_index]))) | |
ccc6cda3 JA |
7239 | { |
7240 | t_index++; | |
ccc6cda3 | 7241 | temp1 = string_extract (string, &t_index, "#%:-=?+/}", 0); |
631b20c6 | 7242 | name = (char *)xrealloc (name, 3 + (strlen (temp1))); |
ccc6cda3 JA |
7243 | *name = string[sindex]; |
7244 | if (string[sindex] == '!') | |
7245 | { | |
28ef6c31 JA |
7246 | /* indirect reference of $#, $?, $@, or $* */ |
7247 | name[1] = string[sindex + 1]; | |
7248 | strcpy (name + 2, temp1); | |
ccc6cda3 | 7249 | } |
cce855bc | 7250 | else |
ccc6cda3 JA |
7251 | strcpy (name + 1, temp1); |
7252 | free (temp1); | |
7253 | } | |
7254 | sindex = t_index; | |
7255 | ||
7256 | /* Find out what character ended the variable name. Then | |
7257 | do the appropriate thing. */ | |
7258 | if (c = string[sindex]) | |
7259 | sindex++; | |
7260 | ||
7261 | /* If c is followed by one of the valid parameter expansion | |
7262 | characters, move past it as normal. If not, assume that | |
7263 | a substring specification is being given, and do not move | |
7264 | past it. */ | |
28ef6c31 | 7265 | if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex])) |
ccc6cda3 JA |
7266 | { |
7267 | check_nullness++; | |
7268 | if (c = string[sindex]) | |
7269 | sindex++; | |
7270 | } | |
cce855bc | 7271 | else if (c == ':' && string[sindex] != RBRACE) |
ccc6cda3 | 7272 | want_substring = 1; |
77638cbf | 7273 | else if (c == '/' /* && string[sindex] != RBRACE */) /* XXX */ |
ccc6cda3 | 7274 | want_patsub = 1; |
09767ff0 CR |
7275 | #if defined (CASEMOD_EXPANSIONS) |
7276 | else if (c == '^' || c == ',' || c == '~') | |
7277 | { | |
7278 | modspec = c; | |
7279 | want_casemod = 1; | |
7280 | } | |
7281 | #endif | |
ccc6cda3 | 7282 | |
cce855bc JA |
7283 | /* Catch the valid and invalid brace expressions that made it through the |
7284 | tests above. */ | |
7285 | /* ${#-} is a valid expansion and means to take the length of $-. | |
7286 | Similarly for ${#?} and ${##}... */ | |
7287 | if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 && | |
28ef6c31 | 7288 | VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE) |
cce855bc | 7289 | { |
f73dda09 | 7290 | name = (char *)xrealloc (name, 3); |
cce855bc JA |
7291 | name[1] = c; |
7292 | name[2] = '\0'; | |
7293 | c = string[sindex++]; | |
7294 | } | |
7295 | ||
7296 | /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */ | |
7297 | if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 && | |
7298 | member (c, "%:=+/") && string[sindex] == RBRACE) | |
7299 | { | |
7300 | temp = (char *)NULL; | |
7301 | goto bad_substitution; | |
7302 | } | |
e33f2203 | 7303 | |
cce855bc JA |
7304 | /* Indirect expansion begins with a `!'. A valid indirect expansion is |
7305 | either a variable name, one of the positional parameters or a special | |
7306 | variable that expands to one of the positional parameters. */ | |
7307 | want_indir = *name == '!' && | |
f73dda09 | 7308 | (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) |
7117c2d2 | 7309 | || VALID_INDIR_PARAM (name[1])); |
ccc6cda3 JA |
7310 | |
7311 | /* Determine the value of this variable. */ | |
7312 | ||
cce855bc | 7313 | /* Check for special variables, directly referenced. */ |
bb70624e | 7314 | if (SPECIAL_VAR (name, want_indir)) |
ccc6cda3 JA |
7315 | var_is_special++; |
7316 | ||
cce855bc JA |
7317 | /* Check for special expansion things, like the length of a parameter */ |
7318 | if (*name == '#' && name[1]) | |
ccc6cda3 | 7319 | { |
cce855bc | 7320 | /* If we are not pointing at the character just after the |
28ef6c31 JA |
7321 | closing brace, then we haven't gotten all of the name. |
7322 | Since it begins with a special character, this is a bad | |
7323 | substitution. Also check NAME for validity before trying | |
7324 | to go on. */ | |
cce855bc | 7325 | if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0)) |
ccc6cda3 JA |
7326 | { |
7327 | temp = (char *)NULL; | |
7328 | goto bad_substitution; | |
7329 | } | |
7330 | ||
7331 | number = parameter_brace_expand_length (name); | |
e05be32d CR |
7332 | if (number == INTMAX_MIN && unbound_vars_is_error) |
7333 | { | |
7334 | last_command_exit_value = EXECUTION_FAILURE; | |
7335 | err_unboundvar (name+1); | |
7336 | free (name); | |
7337 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); | |
7338 | } | |
ccc6cda3 JA |
7339 | free (name); |
7340 | ||
7341 | *indexp = sindex; | |
227f982e CR |
7342 | if (number < 0) |
7343 | return (&expand_wdesc_error); | |
7344 | else | |
7345 | { | |
7346 | ret = alloc_word_desc (); | |
7347 | ret->word = itos (number); | |
7348 | return ret; | |
7349 | } | |
ccc6cda3 JA |
7350 | } |
7351 | ||
7352 | /* ${@} is identical to $@. */ | |
7353 | if (name[0] == '@' && name[1] == '\0') | |
7354 | { | |
7355 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
7356 | *quoted_dollar_atp = 1; | |
7357 | ||
7358 | if (contains_dollar_at) | |
7359 | *contains_dollar_at = 1; | |
348a457e CR |
7360 | |
7361 | tflag |= W_DOLLARAT; | |
ccc6cda3 JA |
7362 | } |
7363 | ||
d3a24ed2 | 7364 | /* Process ${!PREFIX*} expansion. */ |
bb70624e JA |
7365 | if (want_indir && string[sindex - 1] == RBRACE && |
7366 | (string[sindex - 2] == '*' || string[sindex - 2] == '@') && | |
f73dda09 | 7367 | legal_variable_starter ((unsigned char) name[1])) |
bb70624e JA |
7368 | { |
7369 | char **x; | |
7370 | WORD_LIST *xlist; | |
7371 | ||
7372 | temp1 = savestring (name + 1); | |
7373 | number = strlen (temp1); | |
7374 | temp1[number - 1] = '\0'; | |
7375 | x = all_variables_matching_prefix (temp1); | |
7117c2d2 | 7376 | xlist = strvec_to_word_list (x, 0, 0); |
28ef6c31 JA |
7377 | if (string[sindex - 2] == '*') |
7378 | temp = string_list_dollar_star (xlist); | |
7379 | else | |
7380 | { | |
7381 | temp = string_list_dollar_at (xlist, quoted); | |
7382 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
7383 | *quoted_dollar_atp = 1; | |
7384 | if (contains_dollar_at) | |
7385 | *contains_dollar_at = 1; | |
348a457e CR |
7386 | |
7387 | tflag |= W_DOLLARAT; | |
28ef6c31 | 7388 | } |
bb70624e | 7389 | free (x); |
af98a2a6 | 7390 | dispose_words (xlist); |
bb70624e JA |
7391 | free (temp1); |
7392 | *indexp = sindex; | |
227f982e | 7393 | |
631b20c6 CR |
7394 | free (name); |
7395 | ||
227f982e CR |
7396 | ret = alloc_word_desc (); |
7397 | ret->word = temp; | |
348a457e | 7398 | ret->flags = tflag; /* XXX */ |
227f982e | 7399 | return ret; |
bb70624e | 7400 | } |
d3a24ed2 CR |
7401 | |
7402 | #if defined (ARRAY_VARS) | |
5e13499c CR |
7403 | /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */ |
7404 | if (want_indir && string[sindex - 1] == RBRACE && | |
7405 | string[sindex - 2] == ']' && valid_array_reference (name+1)) | |
d3a24ed2 CR |
7406 | { |
7407 | char *x, *x1; | |
7408 | ||
7409 | temp1 = savestring (name + 1); | |
7410 | x = array_variable_name (temp1, &x1, (int *)0); /* [ */ | |
7411 | FREE (x); | |
7412 | if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == ']') | |
7413 | { | |
fdf670ea | 7414 | temp = array_keys (temp1, quoted); /* handles assoc vars too */ |
d3a24ed2 CR |
7415 | if (x1[0] == '@') |
7416 | { | |
7417 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
7418 | *quoted_dollar_atp = 1; | |
7419 | if (contains_dollar_at) | |
7420 | *contains_dollar_at = 1; | |
348a457e CR |
7421 | |
7422 | tflag |= W_DOLLARAT; | |
d3a24ed2 CR |
7423 | } |
7424 | ||
7425 | free (temp1); | |
7426 | *indexp = sindex; | |
227f982e CR |
7427 | |
7428 | ret = alloc_word_desc (); | |
7429 | ret->word = temp; | |
348a457e | 7430 | ret->flags = tflag; /* XXX */ |
227f982e | 7431 | return ret; |
d3a24ed2 CR |
7432 | } |
7433 | ||
7434 | free (temp1); | |
7435 | } | |
7436 | #endif /* ARRAY_VARS */ | |
bb70624e | 7437 | |
ccc6cda3 JA |
7438 | /* Make sure that NAME is valid before trying to go on. */ |
7439 | if (valid_brace_expansion_word (want_indir ? name + 1 : name, | |
7440 | var_is_special) == 0) | |
7441 | { | |
7442 | temp = (char *)NULL; | |
7443 | goto bad_substitution; | |
7444 | } | |
7445 | ||
7446 | if (want_indir) | |
1034e10b CR |
7447 | { |
7448 | tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at); | |
7449 | /* Turn off the W_ARRAYIND flag because there is no way for this function | |
7450 | to return the index we're supposed to be using. */ | |
7451 | if (tdesc && tdesc->flags) | |
7452 | tdesc->flags &= ~W_ARRAYIND; | |
7453 | } | |
ccc6cda3 | 7454 | else |
208fdb50 | 7455 | tdesc = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND|(pflags&(PF_NOSPLIT2|PF_ASSIGNRHS)), &ind); |
227f982e CR |
7456 | |
7457 | if (tdesc) | |
7458 | { | |
7459 | temp = tdesc->word; | |
7460 | tflag = tdesc->flags; | |
7461 | dispose_word_desc (tdesc); | |
7462 | } | |
7463 | else | |
7464 | temp = (char *)0; | |
ccc6cda3 | 7465 | |
15623760 CR |
7466 | if (temp == &expand_param_error || temp == &expand_param_fatal) |
7467 | { | |
7468 | FREE (name); | |
7469 | FREE (value); | |
7470 | return (temp == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal); | |
7471 | } | |
7472 | ||
ccc6cda3 | 7473 | #if defined (ARRAY_VARS) |
cce855bc | 7474 | if (valid_array_reference (name)) |
d3a24ed2 | 7475 | chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at); |
ccc6cda3 JA |
7476 | #endif |
7477 | ||
7478 | var_is_set = temp != (char *)0; | |
7479 | var_is_null = check_nullness && (var_is_set == 0 || *temp == 0); | |
0500de0b CR |
7480 | /* XXX - this may not need to be restricted to special variables */ |
7481 | if (check_nullness) | |
3087e51c | 7482 | var_is_null |= var_is_set && var_is_special && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp); |
ccc6cda3 JA |
7483 | |
7484 | /* Get the rest of the stuff inside the braces. */ | |
cce855bc | 7485 | if (c && c != RBRACE) |
ccc6cda3 JA |
7486 | { |
7487 | /* Extract the contents of the ${ ... } expansion | |
28ef6c31 | 7488 | according to the Posix.2 rules. */ |
e192f341 | 7489 | value = extract_dollar_brace_string (string, &sindex, quoted, (c == '%' || c == '#' || c =='/' || c == '^' || c == ',' || c ==':') ? SX_POSIXEXP|SX_WORD : SX_WORD); |
cce855bc | 7490 | if (string[sindex] == RBRACE) |
28ef6c31 | 7491 | sindex++; |
ccc6cda3 JA |
7492 | else |
7493 | goto bad_substitution; | |
7494 | } | |
7495 | else | |
7496 | value = (char *)NULL; | |
726f6388 | 7497 | |
ccc6cda3 JA |
7498 | *indexp = sindex; |
7499 | ||
e05be32d CR |
7500 | /* All the cases where an expansion can possibly generate an unbound |
7501 | variable error. */ | |
7502 | if (want_substring || want_patsub || want_casemod || c == '#' || c == '%' || c == RBRACE) | |
7503 | { | |
7504 | if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1])) | |
7505 | { | |
7506 | last_command_exit_value = EXECUTION_FAILURE; | |
7507 | err_unboundvar (name); | |
7508 | FREE (value); | |
7509 | FREE (temp); | |
7510 | free (name); | |
7511 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); | |
7512 | } | |
7513 | } | |
7514 | ||
ccc6cda3 JA |
7515 | /* If this is a substring spec, process it and add the result. */ |
7516 | if (want_substring) | |
726f6388 | 7517 | { |
5f8cde23 | 7518 | temp1 = parameter_brace_substring (name, temp, ind, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
ccc6cda3 JA |
7519 | FREE (name); |
7520 | FREE (value); | |
7521 | FREE (temp); | |
227f982e CR |
7522 | |
7523 | if (temp1 == &expand_param_error) | |
7524 | return (&expand_wdesc_error); | |
7525 | else if (temp1 == &expand_param_fatal) | |
7526 | return (&expand_wdesc_fatal); | |
7527 | ||
7528 | ret = alloc_word_desc (); | |
7529 | ret->word = temp1; | |
bb579650 CR |
7530 | /* We test quoted_dollar_atp because we want variants with double-quoted |
7531 | "$@" to take a different code path. In fact, we make sure at the end | |
7532 | of expand_word_internal that we're only looking at these flags if | |
7533 | quoted_dollar_at == 0. */ | |
7534 | if (temp1 && | |
7535 | (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) && | |
7536 | QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
d3ad40de | 7537 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; |
227f982e | 7538 | return ret; |
726f6388 | 7539 | } |
ccc6cda3 | 7540 | else if (want_patsub) |
726f6388 | 7541 | { |
5f8cde23 | 7542 | temp1 = parameter_brace_patsub (name, temp, ind, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
ccc6cda3 JA |
7543 | FREE (name); |
7544 | FREE (value); | |
7545 | FREE (temp); | |
227f982e CR |
7546 | |
7547 | if (temp1 == &expand_param_error) | |
7548 | return (&expand_wdesc_error); | |
7549 | else if (temp1 == &expand_param_fatal) | |
7550 | return (&expand_wdesc_fatal); | |
7551 | ||
40c8fbee CR |
7552 | ret = alloc_word_desc (); |
7553 | ret->word = temp1; | |
bb579650 CR |
7554 | if (temp1 && |
7555 | (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) && | |
7556 | QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
40c8fbee | 7557 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; |
227f982e | 7558 | return ret; |
ccc6cda3 | 7559 | } |
09767ff0 CR |
7560 | #if defined (CASEMOD_EXPANSIONS) |
7561 | else if (want_casemod) | |
7562 | { | |
5f8cde23 | 7563 | temp1 = parameter_brace_casemod (name, temp, ind, modspec, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
09767ff0 CR |
7564 | FREE (name); |
7565 | FREE (value); | |
7566 | FREE (temp); | |
7567 | ||
7568 | if (temp1 == &expand_param_error) | |
7569 | return (&expand_wdesc_error); | |
7570 | else if (temp1 == &expand_param_fatal) | |
7571 | return (&expand_wdesc_fatal); | |
7572 | ||
7573 | ret = alloc_word_desc (); | |
7574 | ret->word = temp1; | |
bb579650 CR |
7575 | if (temp1 && |
7576 | (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) && | |
7577 | QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
40c8fbee | 7578 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; |
09767ff0 CR |
7579 | return ret; |
7580 | } | |
7581 | #endif | |
726f6388 | 7582 | |
ccc6cda3 JA |
7583 | /* Do the right thing based on which character ended the variable name. */ |
7584 | switch (c) | |
7585 | { | |
7586 | default: | |
7587 | case '\0': | |
7588 | bad_substitution: | |
7f947b68 | 7589 | last_command_exit_value = EXECUTION_FAILURE; |
5e13499c | 7590 | report_error (_("%s: bad substitution"), string ? string : "??"); |
ccc6cda3 JA |
7591 | FREE (value); |
7592 | FREE (temp); | |
7593 | free (name); | |
227f982e | 7594 | return &expand_wdesc_error; |
ccc6cda3 | 7595 | |
cce855bc | 7596 | case RBRACE: |
ccc6cda3 | 7597 | break; |
726f6388 | 7598 | |
ccc6cda3 JA |
7599 | case '#': /* ${param#[#]pattern} */ |
7600 | case '%': /* ${param%[%]pattern} */ | |
7601 | if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0') | |
28ef6c31 JA |
7602 | { |
7603 | FREE (value); | |
ccc6cda3 | 7604 | break; |
28ef6c31 | 7605 | } |
5f8cde23 | 7606 | temp1 = parameter_brace_remove_pattern (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
ccc6cda3 JA |
7607 | free (temp); |
7608 | free (value); | |
3d35553a | 7609 | free (name); |
40c8fbee CR |
7610 | |
7611 | ret = alloc_word_desc (); | |
7612 | ret->word = temp1; | |
7613 | if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
7614 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; | |
7615 | return ret; | |
ccc6cda3 JA |
7616 | |
7617 | case '-': | |
7618 | case '=': | |
7619 | case '?': | |
7620 | case '+': | |
7621 | if (var_is_set && var_is_null == 0) | |
28ef6c31 JA |
7622 | { |
7623 | /* If the operator is `+', we don't want the value of the named | |
7624 | variable for anything, just the value of the right hand side. */ | |
ccc6cda3 JA |
7625 | if (c == '+') |
7626 | { | |
28ef6c31 JA |
7627 | /* XXX -- if we're double-quoted and the named variable is "$@", |
7628 | we want to turn off any special handling of "$@" -- | |
7629 | we're not using it, so whatever is on the rhs applies. */ | |
7630 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
7631 | *quoted_dollar_atp = 0; | |
7632 | if (contains_dollar_at) | |
7633 | *contains_dollar_at = 0; | |
7634 | ||
ccc6cda3 JA |
7635 | FREE (temp); |
7636 | if (value) | |
28ef6c31 | 7637 | { |
6faad625 CR |
7638 | /* From Posix discussion on austin-group list. Issue 221 |
7639 | requires that backslashes escaping `}' inside | |
7640 | double-quoted ${...} be removed. */ | |
64419627 CR |
7641 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
7642 | quoted |= Q_DOLBRACE; | |
227f982e CR |
7643 | ret = parameter_brace_expand_rhs (name, value, c, |
7644 | quoted, | |
7645 | quoted_dollar_atp, | |
7646 | contains_dollar_at); | |
7647 | /* XXX - fix up later, esp. noting presence of | |
7648 | W_HASQUOTEDNULL in ret->flags */ | |
ccc6cda3 JA |
7649 | free (value); |
7650 | } | |
7651 | else | |
28ef6c31 | 7652 | temp = (char *)NULL; |
ccc6cda3 JA |
7653 | } |
7654 | else | |
7655 | { | |
7656 | FREE (value); | |
7657 | } | |
7658 | /* Otherwise do nothing; just use the value in TEMP. */ | |
726f6388 | 7659 | } |
ccc6cda3 | 7660 | else /* VAR not set or VAR is NULL. */ |
28ef6c31 | 7661 | { |
ccc6cda3 JA |
7662 | FREE (temp); |
7663 | temp = (char *)NULL; | |
7664 | if (c == '=' && var_is_special) | |
7665 | { | |
7f947b68 | 7666 | last_command_exit_value = EXECUTION_FAILURE; |
5e13499c | 7667 | report_error (_("$%s: cannot assign in this way"), name); |
ccc6cda3 JA |
7668 | free (name); |
7669 | free (value); | |
227f982e | 7670 | return &expand_wdesc_error; |
ccc6cda3 JA |
7671 | } |
7672 | else if (c == '?') | |
7673 | { | |
7674 | parameter_brace_expand_error (name, value); | |
227f982e | 7675 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
ccc6cda3 JA |
7676 | } |
7677 | else if (c != '+') | |
28ef6c31 JA |
7678 | { |
7679 | /* XXX -- if we're double-quoted and the named variable is "$@", | |
7680 | we want to turn off any special handling of "$@" -- | |
7681 | we're not using it, so whatever is on the rhs applies. */ | |
7682 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
7683 | *quoted_dollar_atp = 0; | |
7684 | if (contains_dollar_at) | |
7685 | *contains_dollar_at = 0; | |
7686 | ||
6faad625 CR |
7687 | /* From Posix discussion on austin-group list. Issue 221 requires |
7688 | that backslashes escaping `}' inside double-quoted ${...} be | |
7689 | removed. */ | |
64419627 CR |
7690 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
7691 | quoted |= Q_DOLBRACE; | |
227f982e CR |
7692 | ret = parameter_brace_expand_rhs (name, value, c, quoted, |
7693 | quoted_dollar_atp, | |
7694 | contains_dollar_at); | |
7695 | /* XXX - fix up later, esp. noting presence of | |
7696 | W_HASQUOTEDNULL in tdesc->flags */ | |
28ef6c31 | 7697 | } |
ccc6cda3 | 7698 | free (value); |
726f6388 | 7699 | } |
28ef6c31 | 7700 | |
ccc6cda3 | 7701 | break; |
726f6388 | 7702 | } |
ccc6cda3 | 7703 | free (name); |
227f982e CR |
7704 | |
7705 | if (ret == 0) | |
7706 | { | |
7707 | ret = alloc_word_desc (); | |
7708 | ret->flags = tflag; | |
7709 | ret->word = temp; | |
7710 | } | |
7711 | return (ret); | |
726f6388 JA |
7712 | } |
7713 | ||
cce855bc JA |
7714 | /* Expand a single ${xxx} expansion. The braces are optional. When |
7715 | the braces are used, parameter_brace_expand() does the work, | |
7716 | possibly calling param_expand recursively. */ | |
227f982e | 7717 | static WORD_DESC * |
cce855bc JA |
7718 | param_expand (string, sindex, quoted, expanded_something, |
7719 | contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p, | |
7720 | pflags) | |
7721 | char *string; | |
7722 | int *sindex, quoted, *expanded_something, *contains_dollar_at; | |
7723 | int *quoted_dollar_at_p, *had_quoted_null_p, pflags; | |
7724 | { | |
7117c2d2 | 7725 | char *temp, *temp1, uerror[3]; |
f73dda09 JA |
7726 | int zindex, t_index, expok; |
7727 | unsigned char c; | |
7117c2d2 | 7728 | intmax_t number; |
cce855bc | 7729 | SHELL_VAR *var; |
f73dda09 | 7730 | WORD_LIST *list; |
227f982e CR |
7731 | WORD_DESC *tdesc, *ret; |
7732 | int tflag; | |
cce855bc JA |
7733 | |
7734 | zindex = *sindex; | |
7735 | c = string[++zindex]; | |
7736 | ||
7737 | temp = (char *)NULL; | |
227f982e CR |
7738 | ret = tdesc = (WORD_DESC *)NULL; |
7739 | tflag = 0; | |
cce855bc JA |
7740 | |
7741 | /* Do simple cases first. Switch on what follows '$'. */ | |
7742 | switch (c) | |
7743 | { | |
7744 | /* $0 .. $9? */ | |
7745 | case '0': | |
7746 | case '1': | |
7747 | case '2': | |
7748 | case '3': | |
7749 | case '4': | |
7750 | case '5': | |
7751 | case '6': | |
7752 | case '7': | |
7753 | case '8': | |
7754 | case '9': | |
f73dda09 | 7755 | temp1 = dollar_vars[TODIGIT (c)]; |
cce855bc JA |
7756 | if (unbound_vars_is_error && temp1 == (char *)NULL) |
7757 | { | |
7117c2d2 JA |
7758 | uerror[0] = '$'; |
7759 | uerror[1] = c; | |
7760 | uerror[2] = '\0'; | |
cce855bc | 7761 | last_command_exit_value = EXECUTION_FAILURE; |
0d8616ff | 7762 | err_unboundvar (uerror); |
227f982e | 7763 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
cce855bc | 7764 | } |
5ba8ff6e | 7765 | if (temp1) |
762a763b | 7766 | temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
5ba8ff6e CR |
7767 | ? quote_string (temp1) |
7768 | : quote_escapes (temp1); | |
7769 | else | |
7770 | temp = (char *)NULL; | |
227f982e | 7771 | |
cce855bc JA |
7772 | break; |
7773 | ||
7774 | /* $$ -- pid of the invoking shell. */ | |
7775 | case '$': | |
7776 | temp = itos (dollar_dollar_pid); | |
7777 | break; | |
7778 | ||
7779 | /* $# -- number of positional parameters. */ | |
7780 | case '#': | |
7781 | temp = itos (number_of_args ()); | |
7782 | break; | |
7783 | ||
7784 | /* $? -- return value of the last synchronous command. */ | |
7785 | case '?': | |
7786 | temp = itos (last_command_exit_value); | |
7787 | break; | |
7788 | ||
7789 | /* $- -- flags supplied to the shell on invocation or by `set'. */ | |
7790 | case '-': | |
7791 | temp = which_set_flags (); | |
7792 | break; | |
7793 | ||
7794 | /* $! -- Pid of the last asynchronous command. */ | |
7795 | case '!': | |
7796 | /* If no asynchronous pids have been created, expand to nothing. | |
7797 | If `set -u' has been executed, and no async processes have | |
7798 | been created, this is an expansion error. */ | |
7799 | if (last_asynchronous_pid == NO_PID) | |
7800 | { | |
7801 | if (expanded_something) | |
7802 | *expanded_something = 0; | |
7803 | temp = (char *)NULL; | |
7804 | if (unbound_vars_is_error) | |
7805 | { | |
7117c2d2 JA |
7806 | uerror[0] = '$'; |
7807 | uerror[1] = c; | |
7808 | uerror[2] = '\0'; | |
cce855bc | 7809 | last_command_exit_value = EXECUTION_FAILURE; |
0d8616ff | 7810 | err_unboundvar (uerror); |
227f982e | 7811 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
cce855bc JA |
7812 | } |
7813 | } | |
7814 | else | |
f73dda09 | 7815 | temp = itos (last_asynchronous_pid); |
cce855bc JA |
7816 | break; |
7817 | ||
7818 | /* The only difference between this and $@ is when the arg is quoted. */ | |
7819 | case '*': /* `$*' */ | |
7820 | list = list_rest_of_args (); | |
7821 | ||
12ae1612 CR |
7822 | #if 0 |
7823 | /* According to austin-group posix proposal by Geoff Clare in | |
7824 | <20090505091501.GA10097@squonk.masqnet> of 5 May 2009: | |
7825 | ||
7826 | "The shell shall write a message to standard error and | |
7827 | immediately exit when it tries to expand an unset parameter | |
7828 | other than the '@' and '*' special parameters." | |
7829 | */ | |
7830 | ||
1231ac47 | 7831 | if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0) |
57a3f689 CR |
7832 | { |
7833 | uerror[0] = '$'; | |
7834 | uerror[1] = '*'; | |
7835 | uerror[2] = '\0'; | |
57a3f689 | 7836 | last_command_exit_value = EXECUTION_FAILURE; |
0d8616ff | 7837 | err_unboundvar (uerror); |
57a3f689 CR |
7838 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
7839 | } | |
12ae1612 | 7840 | #endif |
57a3f689 | 7841 | |
cce855bc JA |
7842 | /* If there are no command-line arguments, this should just |
7843 | disappear if there are other characters in the expansion, | |
7844 | even if it's quoted. */ | |
7845 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0) | |
7846 | temp = (char *)NULL; | |
1231ac47 | 7847 | else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE)) |
cce855bc JA |
7848 | { |
7849 | /* If we have "$*" we want to make a string of the positional | |
7850 | parameters, separated by the first character of $IFS, and | |
7851 | quote the whole string, including the separators. If IFS | |
7852 | is unset, the parameters are separated by ' '; if $IFS is | |
7853 | null, the parameters are concatenated. */ | |
1231ac47 | 7854 | temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list) : string_list (list); |
adc6cff5 CR |
7855 | if (temp) |
7856 | { | |
7857 | temp1 = quote_string (temp); | |
7858 | if (*temp == 0) | |
7859 | tflag |= W_HASQUOTEDNULL; | |
7860 | free (temp); | |
7861 | temp = temp1; | |
7862 | } | |
cce855bc JA |
7863 | } |
7864 | else | |
28ef6c31 | 7865 | { |
227f982e CR |
7866 | /* We check whether or not we're eventually going to split $* here, |
7867 | for example when IFS is empty and we are processing the rhs of | |
7868 | an assignment statement. In that case, we don't separate the | |
7869 | arguments at all. Otherwise, if the $* is not quoted it is | |
7870 | identical to $@ */ | |
227f982e CR |
7871 | # if defined (HANDLE_MULTIBYTE) |
7872 | if (expand_no_split_dollar_star && ifs_firstc[0] == 0) | |
7873 | # else | |
7874 | if (expand_no_split_dollar_star && ifs_firstc == 0) | |
7875 | # endif | |
7876 | temp = string_list_dollar_star (list); | |
7877 | else | |
f0c4de40 CR |
7878 | { |
7879 | temp = string_list_dollar_at (list, quoted); | |
1a81420a | 7880 | if (quoted == 0 && (ifs_is_set == 0 || ifs_is_null)) |
f0c4de40 | 7881 | tflag |= W_SPLITSPACE; |
f0c4de40 CR |
7882 | } |
7883 | ||
28ef6c31 JA |
7884 | if (expand_no_split_dollar_star == 0 && contains_dollar_at) |
7885 | *contains_dollar_at = 1; | |
7886 | } | |
cce855bc JA |
7887 | |
7888 | dispose_words (list); | |
7889 | break; | |
7890 | ||
7891 | /* When we have "$@" what we want is "$1" "$2" "$3" ... This | |
7892 | means that we have to turn quoting off after we split into | |
7893 | the individually quoted arguments so that the final split | |
7894 | on the first character of $IFS is still done. */ | |
7895 | case '@': /* `$@' */ | |
7896 | list = list_rest_of_args (); | |
7897 | ||
12ae1612 CR |
7898 | #if 0 |
7899 | /* According to austin-group posix proposal by Geoff Clare in | |
7900 | <20090505091501.GA10097@squonk.masqnet> of 5 May 2009: | |
7901 | ||
7902 | "The shell shall write a message to standard error and | |
7903 | immediately exit when it tries to expand an unset parameter | |
7904 | other than the '@' and '*' special parameters." | |
7905 | */ | |
7906 | ||
1231ac47 | 7907 | if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0) |
57a3f689 CR |
7908 | { |
7909 | uerror[0] = '$'; | |
7910 | uerror[1] = '@'; | |
7911 | uerror[2] = '\0'; | |
57a3f689 | 7912 | last_command_exit_value = EXECUTION_FAILURE; |
0d8616ff | 7913 | err_unboundvar (uerror); |
57a3f689 CR |
7914 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
7915 | } | |
12ae1612 | 7916 | #endif |
57a3f689 | 7917 | |
cce855bc JA |
7918 | /* We want to flag the fact that we saw this. We can't turn |
7919 | off quoting entirely, because other characters in the | |
7920 | string might need it (consider "\"$@\""), but we need some | |
7921 | way to signal that the final split on the first character | |
7922 | of $IFS should be done, even though QUOTED is 1. */ | |
1231ac47 | 7923 | /* XXX - should this test include Q_PATQUOTE? */ |
cce855bc JA |
7924 | if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
7925 | *quoted_dollar_at_p = 1; | |
7926 | if (contains_dollar_at) | |
7927 | *contains_dollar_at = 1; | |
7928 | ||
7929 | /* We want to separate the positional parameters with the first | |
7930 | character of $IFS in case $IFS is something other than a space. | |
7931 | We also want to make sure that splitting is done no matter what -- | |
7932 | according to POSIX.2, this expands to a list of the positional | |
7933 | parameters no matter what IFS is set to. */ | |
0d9b018b CR |
7934 | /* XXX - what to do when in a context where word splitting is not |
7935 | performed? Even when IFS is not the default, posix seems to imply | |
7936 | that we behave like unquoted $* ? Maybe we should use PF_NOSPLIT2 | |
7937 | here. */ | |
40647963 | 7938 | temp = string_list_dollar_at (list, (pflags & PF_ASSIGNRHS) ? (quoted|Q_DOUBLE_QUOTES) : quoted); |
cce855bc | 7939 | |
348a457e | 7940 | tflag |= W_DOLLARAT; |
cce855bc JA |
7941 | dispose_words (list); |
7942 | break; | |
7943 | ||
7944 | case LBRACE: | |
e1e48bba | 7945 | tdesc = parameter_brace_expand (string, &zindex, quoted, pflags, |
227f982e CR |
7946 | quoted_dollar_at_p, |
7947 | contains_dollar_at); | |
7948 | ||
227f982e CR |
7949 | if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal) |
7950 | return (tdesc); | |
7951 | temp = tdesc ? tdesc->word : (char *)0; | |
cce855bc JA |
7952 | |
7953 | /* XXX */ | |
bb70624e | 7954 | /* Quoted nulls should be removed if there is anything else |
cce855bc JA |
7955 | in the string. */ |
7956 | /* Note that we saw the quoted null so we can add one back at | |
7957 | the end of this function if there are no other characters | |
28ef6c31 JA |
7958 | in the string, discard TEMP, and go on. The exception to |
7959 | this is when we have "${@}" and $1 is '', since $@ needs | |
7960 | special handling. */ | |
227f982e | 7961 | if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp)) |
cce855bc JA |
7962 | { |
7963 | if (had_quoted_null_p) | |
7964 | *had_quoted_null_p = 1; | |
28ef6c31 JA |
7965 | if (*quoted_dollar_at_p == 0) |
7966 | { | |
7967 | free (temp); | |
227f982e | 7968 | tdesc->word = temp = (char *)NULL; |
28ef6c31 JA |
7969 | } |
7970 | ||
cce855bc JA |
7971 | } |
7972 | ||
227f982e | 7973 | ret = tdesc; |
cce855bc JA |
7974 | goto return0; |
7975 | ||
7976 | /* Do command or arithmetic substitution. */ | |
7977 | case LPAREN: | |
7978 | /* We have to extract the contents of this paren substitution. */ | |
7979 | t_index = zindex + 1; | |
e33f2203 | 7980 | temp = extract_command_subst (string, &t_index, 0); |
cce855bc JA |
7981 | zindex = t_index; |
7982 | ||
7983 | /* For Posix.2-style `$(( ))' arithmetic substitution, | |
28ef6c31 | 7984 | extract the expression and pass it to the evaluator. */ |
cce855bc JA |
7985 | if (temp && *temp == LPAREN) |
7986 | { | |
7987 | char *temp2; | |
7988 | temp1 = temp + 1; | |
7989 | temp2 = savestring (temp1); | |
7990 | t_index = strlen (temp2) - 1; | |
7991 | ||
7992 | if (temp2[t_index] != RPAREN) | |
7993 | { | |
7994 | free (temp2); | |
7995 | goto comsub; | |
7996 | } | |
7997 | ||
7998 | /* Cut off ending `)' */ | |
7999 | temp2[t_index] = '\0'; | |
8000 | ||
d3ad40de CR |
8001 | if (chk_arithsub (temp2, t_index) == 0) |
8002 | { | |
8003 | free (temp2); | |
984a1947 | 8004 | #if 0 |
ecf57862 | 8005 | internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution")); |
984a1947 | 8006 | #endif |
d3ad40de CR |
8007 | goto comsub; |
8008 | } | |
8009 | ||
cce855bc | 8010 | /* Expand variables found inside the expression. */ |
d3ad40de | 8011 | temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES); |
cce855bc JA |
8012 | free (temp2); |
8013 | ||
8014 | arithsub: | |
8015 | /* No error messages. */ | |
8016 | this_command_name = (char *)NULL; | |
8017 | number = evalexp (temp1, &expok); | |
8018 | free (temp); | |
8019 | free (temp1); | |
8020 | if (expok == 0) | |
8021 | { | |
8022 | if (interactive_shell == 0 && posixly_correct) | |
8023 | { | |
8024 | last_command_exit_value = EXECUTION_FAILURE; | |
227f982e | 8025 | return (&expand_wdesc_fatal); |
cce855bc JA |
8026 | } |
8027 | else | |
227f982e | 8028 | return (&expand_wdesc_error); |
cce855bc JA |
8029 | } |
8030 | temp = itos (number); | |
8031 | break; | |
8032 | } | |
8033 | ||
8034 | comsub: | |
d3a24ed2 CR |
8035 | if (pflags & PF_NOCOMSUB) |
8036 | /* we need zindex+1 because string[zindex] == RPAREN */ | |
8037 | temp1 = substring (string, *sindex, zindex+1); | |
8038 | else | |
d3ad40de CR |
8039 | { |
8040 | tdesc = command_substitute (temp, quoted); | |
8041 | temp1 = tdesc ? tdesc->word : (char *)NULL; | |
e33f2203 CR |
8042 | if (tdesc) |
8043 | dispose_word_desc (tdesc); | |
d3ad40de | 8044 | } |
cce855bc JA |
8045 | FREE (temp); |
8046 | temp = temp1; | |
8047 | break; | |
8048 | ||
8049 | /* Do POSIX.2d9-style arithmetic substitution. This will probably go | |
8050 | away in a future bash release. */ | |
8051 | case '[': | |
bb70624e | 8052 | /* Extract the contents of this arithmetic substitution. */ |
cce855bc JA |
8053 | t_index = zindex + 1; |
8054 | temp = extract_arithmetic_subst (string, &t_index); | |
8055 | zindex = t_index; | |
35bb237e CR |
8056 | if (temp == 0) |
8057 | { | |
8058 | temp = savestring (string); | |
8059 | if (expanded_something) | |
8060 | *expanded_something = 0; | |
8061 | goto return0; | |
8062 | } | |
cce855bc JA |
8063 | |
8064 | /* Do initial variable expansion. */ | |
d3ad40de | 8065 | temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES); |
cce855bc JA |
8066 | |
8067 | goto arithsub; | |
8068 | ||
8069 | default: | |
8070 | /* Find the variable in VARIABLE_LIST. */ | |
8071 | temp = (char *)NULL; | |
8072 | ||
8073 | for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++) | |
8074 | ; | |
8075 | temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL; | |
8076 | ||
8077 | /* If this isn't a variable name, then just output the `$'. */ | |
8078 | if (temp1 == 0 || *temp1 == '\0') | |
8079 | { | |
8080 | FREE (temp1); | |
f73dda09 | 8081 | temp = (char *)xmalloc (2); |
cce855bc JA |
8082 | temp[0] = '$'; |
8083 | temp[1] = '\0'; | |
8084 | if (expanded_something) | |
8085 | *expanded_something = 0; | |
8086 | goto return0; | |
8087 | } | |
8088 | ||
8089 | /* If the variable exists, return its value cell. */ | |
8090 | var = find_variable (temp1); | |
8091 | ||
7117c2d2 | 8092 | if (var && invisible_p (var) == 0 && var_isset (var)) |
cce855bc JA |
8093 | { |
8094 | #if defined (ARRAY_VARS) | |
fdf670ea | 8095 | if (assoc_p (var) || array_p (var)) |
cce855bc | 8096 | { |
fdf670ea CR |
8097 | temp = array_p (var) ? array_reference (array_cell (var), 0) |
8098 | : assoc_reference (assoc_cell (var), "0"); | |
cce855bc | 8099 | if (temp) |
762a763b | 8100 | temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
5ba8ff6e CR |
8101 | ? quote_string (temp) |
8102 | : quote_escapes (temp); | |
5565fb1a CR |
8103 | else if (unbound_vars_is_error) |
8104 | goto unbound_variable; | |
cce855bc JA |
8105 | } |
8106 | else | |
8107 | #endif | |
762a763b CR |
8108 | { |
8109 | temp = value_cell (var); | |
8110 | ||
8111 | temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
8112 | ? quote_string (temp) | |
8113 | : quote_escapes (temp); | |
8114 | } | |
5ba8ff6e | 8115 | |
cce855bc | 8116 | free (temp1); |
7117c2d2 | 8117 | |
cce855bc JA |
8118 | goto return0; |
8119 | } | |
96f3fb66 CR |
8120 | else if (var && (invisible_p (var) || var_isset (var) == 0)) |
8121 | temp = (char *)NULL; | |
8122 | else if ((var = find_variable_last_nameref (temp1)) && var_isset (var) && invisible_p (var) == 0) | |
d42cb8c1 CR |
8123 | { |
8124 | temp = nameref_cell (var); | |
15623760 | 8125 | #if defined (ARRAY_VARS) |
d42cb8c1 CR |
8126 | if (temp && *temp && valid_array_reference (temp)) |
8127 | { | |
f14388d3 | 8128 | tdesc = parameter_brace_expand_word (temp, SPECIAL_VAR (temp, 0), quoted, pflags, (arrayind_t *)NULL); |
d42cb8c1 CR |
8129 | if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal) |
8130 | return (tdesc); | |
8131 | ret = tdesc; | |
8132 | goto return0; | |
8133 | } | |
8134 | else | |
15623760 CR |
8135 | #endif |
8136 | /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */ | |
8137 | if (temp && *temp && legal_identifier (temp) == 0) | |
8138 | { | |
8139 | last_command_exit_value = EXECUTION_FAILURE; | |
8140 | report_error (_("%s: invalid variable name for name reference"), temp); | |
8141 | return (&expand_wdesc_error); /* XXX */ | |
8142 | } | |
8143 | else | |
d42cb8c1 CR |
8144 | temp = (char *)NULL; |
8145 | } | |
cce855bc JA |
8146 | |
8147 | temp = (char *)NULL; | |
8148 | ||
5565fb1a | 8149 | unbound_variable: |
cce855bc | 8150 | if (unbound_vars_is_error) |
0d8616ff CR |
8151 | { |
8152 | last_command_exit_value = EXECUTION_FAILURE; | |
8153 | err_unboundvar (temp1); | |
8154 | } | |
cce855bc JA |
8155 | else |
8156 | { | |
8157 | free (temp1); | |
8158 | goto return0; | |
8159 | } | |
8160 | ||
8161 | free (temp1); | |
8162 | last_command_exit_value = EXECUTION_FAILURE; | |
8163 | return ((unbound_vars_is_error && interactive_shell == 0) | |
227f982e CR |
8164 | ? &expand_wdesc_fatal |
8165 | : &expand_wdesc_error); | |
cce855bc JA |
8166 | } |
8167 | ||
8168 | if (string[zindex]) | |
8169 | zindex++; | |
8170 | ||
8171 | return0: | |
8172 | *sindex = zindex; | |
227f982e CR |
8173 | |
8174 | if (ret == 0) | |
8175 | { | |
8176 | ret = alloc_word_desc (); | |
8177 | ret->flags = tflag; /* XXX */ | |
8178 | ret->word = temp; | |
8179 | } | |
8180 | return ret; | |
cce855bc JA |
8181 | } |
8182 | ||
8183 | /* Make a word list which is the result of parameter and variable | |
8184 | expansion, command substitution, arithmetic substitution, and | |
8185 | quote removal of WORD. Return a pointer to a WORD_LIST which is | |
8186 | the result of the expansion. If WORD contains a null word, the | |
8187 | word list returned is also null. | |
726f6388 | 8188 | |
ccc6cda3 JA |
8189 | QUOTED contains flag values defined in shell.h. |
8190 | ||
b72432fd JA |
8191 | ISEXP is used to tell expand_word_internal that the word should be |
8192 | treated as the result of an expansion. This has implications for | |
8193 | how IFS characters in the word are treated. | |
8194 | ||
726f6388 JA |
8195 | CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null |
8196 | they point to an integer value which receives information about expansion. | |
8197 | CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero. | |
8198 | EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions, | |
8199 | else zero. | |
8200 | ||
8201 | This only does word splitting in the case of $@ expansion. In that | |
8202 | case, we split on ' '. */ | |
8203 | ||
8204 | /* Values for the local variable quoted_state. */ | |
8205 | #define UNQUOTED 0 | |
8206 | #define PARTIALLY_QUOTED 1 | |
8207 | #define WHOLLY_QUOTED 2 | |
8208 | ||
8209 | static WORD_LIST * | |
b72432fd | 8210 | expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something) |
726f6388 | 8211 | WORD_DESC *word; |
b72432fd | 8212 | int quoted, isexp; |
726f6388 JA |
8213 | int *contains_dollar_at; |
8214 | int *expanded_something; | |
8215 | { | |
ccc6cda3 JA |
8216 | WORD_LIST *list; |
8217 | WORD_DESC *tword; | |
726f6388 JA |
8218 | |
8219 | /* The intermediate string that we build while expanding. */ | |
ccc6cda3 | 8220 | char *istring; |
726f6388 JA |
8221 | |
8222 | /* The current size of the above object. */ | |
ccc6cda3 | 8223 | int istring_size; |
726f6388 JA |
8224 | |
8225 | /* Index into ISTRING. */ | |
ccc6cda3 | 8226 | int istring_index; |
726f6388 JA |
8227 | |
8228 | /* Temporary string storage. */ | |
ccc6cda3 | 8229 | char *temp, *temp1; |
726f6388 JA |
8230 | |
8231 | /* The text of WORD. */ | |
ccc6cda3 | 8232 | register char *string; |
726f6388 | 8233 | |
7117c2d2 JA |
8234 | /* The size of STRING. */ |
8235 | size_t string_size; | |
8236 | ||
726f6388 | 8237 | /* The index into STRING. */ |
ccc6cda3 | 8238 | int sindex; |
726f6388 JA |
8239 | |
8240 | /* This gets 1 if we see a $@ while quoted. */ | |
ccc6cda3 | 8241 | int quoted_dollar_at; |
726f6388 JA |
8242 | |
8243 | /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on | |
8244 | whether WORD contains no quoting characters, a partially quoted | |
8245 | string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */ | |
ccc6cda3 JA |
8246 | int quoted_state; |
8247 | ||
22e63b05 | 8248 | /* State flags */ |
ccc6cda3 | 8249 | int had_quoted_null; |
348a457e | 8250 | int has_dollar_at, temp_has_dollar_at; |
f0c4de40 | 8251 | int split_on_spaces; |
28ef6c31 | 8252 | int tflag; |
e1e48bba | 8253 | int pflags; /* flags passed to param_expand */ |
726f6388 | 8254 | |
22e63b05 CR |
8255 | int assignoff; /* If assignment, offset of `=' */ |
8256 | ||
f73dda09 | 8257 | register unsigned char c; /* Current character. */ |
726f6388 | 8258 | int t_index; /* For calls to string_extract_xxx. */ |
726f6388 | 8259 | |
bb70624e | 8260 | char twochars[2]; |
b72432fd | 8261 | |
7117c2d2 JA |
8262 | DECLARE_MBSTATE; |
8263 | ||
f73dda09 | 8264 | istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE); |
ccc6cda3 | 8265 | istring[istring_index = 0] = '\0'; |
cce855bc | 8266 | quoted_dollar_at = had_quoted_null = has_dollar_at = 0; |
f0c4de40 | 8267 | split_on_spaces = 0; |
ccc6cda3 JA |
8268 | quoted_state = UNQUOTED; |
8269 | ||
8270 | string = word->word; | |
8271 | if (string == 0) | |
8272 | goto finished_with_string; | |
da713c23 CR |
8273 | /* Don't need the string length for the SADD... and COPY_ macros unless |
8274 | multibyte characters are possible. */ | |
8275 | string_size = (MB_CUR_MAX > 1) ? strlen (string) : 1; | |
726f6388 JA |
8276 | |
8277 | if (contains_dollar_at) | |
8278 | *contains_dollar_at = 0; | |
8279 | ||
22e63b05 CR |
8280 | assignoff = -1; |
8281 | ||
726f6388 JA |
8282 | /* Begin the expansion. */ |
8283 | ||
ccc6cda3 | 8284 | for (sindex = 0; ;) |
726f6388 JA |
8285 | { |
8286 | c = string[sindex]; | |
8287 | ||
39feef01 | 8288 | /* Case on top-level character. */ |
726f6388 JA |
8289 | switch (c) |
8290 | { | |
8291 | case '\0': | |
8292 | goto finished_with_string; | |
8293 | ||
8294 | case CTLESC: | |
7117c2d2 JA |
8295 | sindex++; |
8296 | #if HANDLE_MULTIBYTE | |
8297 | if (MB_CUR_MAX > 1 && string[sindex]) | |
8298 | { | |
545f34cf | 8299 | SADD_MBQCHAR_BODY(temp, string, sindex, string_size); |
7117c2d2 JA |
8300 | } |
8301 | else | |
8302 | #endif | |
8303 | { | |
8304 | temp = (char *)xmalloc (3); | |
8305 | temp[0] = CTLESC; | |
8306 | temp[1] = c = string[sindex]; | |
8307 | temp[2] = '\0'; | |
8308 | } | |
726f6388 | 8309 | |
cce855bc | 8310 | dollar_add_string: |
726f6388 JA |
8311 | if (string[sindex]) |
8312 | sindex++; | |
8313 | ||
cce855bc JA |
8314 | add_string: |
8315 | if (temp) | |
8316 | { | |
8317 | istring = sub_append_string (temp, istring, &istring_index, &istring_size); | |
8318 | temp = (char *)0; | |
8319 | } | |
8320 | ||
8321 | break; | |
726f6388 JA |
8322 | |
8323 | #if defined (PROCESS_SUBSTITUTION) | |
8324 | /* Process substitution. */ | |
8325 | case '<': | |
8326 | case '>': | |
8327 | { | |
098cbb5c CR |
8328 | /* bash-4.4/bash-5.0 |
8329 | XXX - technically this should only be expanded at the start | |
8330 | of a word */ | |
dc8fbaf9 | 8331 | if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)) || posixly_correct) |
726f6388 | 8332 | { |
bb70624e | 8333 | sindex--; /* add_character: label increments sindex */ |
726f6388 JA |
8334 | goto add_character; |
8335 | } | |
8336 | else | |
cce855bc | 8337 | t_index = sindex + 1; /* skip past both '<' and LPAREN */ |
726f6388 | 8338 | |
fbbc416f | 8339 | temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index, 0); /*))*/ |
ccc6cda3 | 8340 | sindex = t_index; |
726f6388 JA |
8341 | |
8342 | /* If the process substitution specification is `<()', we want to | |
8343 | open the pipe for writing in the child and produce output; if | |
8344 | it is `>()', we want to open the pipe for reading in the child | |
8345 | and consume input. */ | |
ccc6cda3 | 8346 | temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0; |
726f6388 JA |
8347 | |
8348 | FREE (temp1); | |
8349 | ||
8350 | goto dollar_add_string; | |
8351 | } | |
8352 | #endif /* PROCESS_SUBSTITUTION */ | |
8353 | ||
22e63b05 CR |
8354 | case '=': |
8355 | /* Posix.2 section 3.6.1 says that tildes following `=' in words | |
8356 | which are not assignment statements are not expanded. If the | |
8357 | shell isn't in posix mode, though, we perform tilde expansion | |
8358 | on `likely candidate' unquoted assignment statements (flags | |
8359 | include W_ASSIGNMENT but not W_QUOTED). A likely candidate | |
8360 | contains an unquoted :~ or =~. Something to think about: we | |
8361 | now have a flag that says to perform tilde expansion on arguments | |
8362 | to `assignment builtins' like declare and export that look like | |
8363 | assignment statements. We now do tilde expansion on such words | |
8364 | even in POSIX mode. */ | |
8365 | if (word->flags & (W_ASSIGNRHS|W_NOTILDE)) | |
c1d39fb8 | 8366 | { |
e1e48bba | 8367 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
c1d39fb8 CR |
8368 | goto add_ifs_character; |
8369 | else | |
8370 | goto add_character; | |
8371 | } | |
22e63b05 CR |
8372 | /* If we're not in posix mode or forcing assignment-statement tilde |
8373 | expansion, note where the `=' appears in the word and prepare to | |
8374 | do tilde expansion following the first `='. */ | |
8375 | if ((word->flags & W_ASSIGNMENT) && | |
8376 | (posixly_correct == 0 || (word->flags & W_TILDEEXP)) && | |
8377 | assignoff == -1 && sindex > 0) | |
8378 | assignoff = sindex; | |
8379 | if (sindex == assignoff && string[sindex+1] == '~') /* XXX */ | |
8380 | word->flags |= W_ITILDE; | |
d90269dd | 8381 | #if 0 |
22e63b05 CR |
8382 | else if ((word->flags & W_ASSIGNMENT) && |
8383 | (posixly_correct == 0 || (word->flags & W_TILDEEXP)) && | |
8384 | string[sindex+1] == '~') | |
8385 | word->flags |= W_ITILDE; | |
d90269dd | 8386 | #endif |
e1e48bba | 8387 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
c1d39fb8 CR |
8388 | goto add_ifs_character; |
8389 | else | |
8390 | goto add_character; | |
22e63b05 CR |
8391 | |
8392 | case ':': | |
8393 | if (word->flags & W_NOTILDE) | |
c1d39fb8 | 8394 | { |
e1e48bba | 8395 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
c1d39fb8 CR |
8396 | goto add_ifs_character; |
8397 | else | |
8398 | goto add_character; | |
8399 | } | |
cdb32d45 CR |
8400 | |
8401 | if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) && | |
22e63b05 CR |
8402 | string[sindex+1] == '~') |
8403 | word->flags |= W_ITILDE; | |
c1d39fb8 | 8404 | |
e1e48bba | 8405 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
c1d39fb8 CR |
8406 | goto add_ifs_character; |
8407 | else | |
8408 | goto add_character; | |
22e63b05 CR |
8409 | |
8410 | case '~': | |
8411 | /* If the word isn't supposed to be tilde expanded, or we're not | |
8412 | at the start of a word or after an unquoted : or = in an | |
8413 | assignment statement, we don't do tilde expansion. */ | |
da719982 | 8414 | if ((word->flags & (W_NOTILDE|W_DQUOTE)) || |
22e63b05 CR |
8415 | (sindex > 0 && ((word->flags & W_ITILDE) == 0)) || |
8416 | (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) | |
8417 | { | |
8418 | word->flags &= ~W_ITILDE; | |
e1e48bba | 8419 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0) |
c1d39fb8 CR |
8420 | goto add_ifs_character; |
8421 | else | |
8422 | goto add_character; | |
22e63b05 CR |
8423 | } |
8424 | ||
8425 | if (word->flags & W_ASSIGNRHS) | |
8426 | tflag = 2; | |
cdb32d45 | 8427 | else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP)) |
22e63b05 CR |
8428 | tflag = 1; |
8429 | else | |
8430 | tflag = 0; | |
8431 | ||
8432 | temp = bash_tilde_find_word (string + sindex, tflag, &t_index); | |
8433 | ||
8434 | word->flags &= ~W_ITILDE; | |
8435 | ||
8436 | if (temp && *temp && t_index > 0) | |
8437 | { | |
8438 | temp1 = bash_tilde_expand (temp, tflag); | |
dc8fbaf9 CR |
8439 | if (temp1 && *temp1 == '~' && STREQ (temp, temp1)) |
8440 | { | |
8441 | FREE (temp); | |
8442 | FREE (temp1); | |
8443 | goto add_character; /* tilde expansion failed */ | |
8444 | } | |
22e63b05 CR |
8445 | free (temp); |
8446 | temp = temp1; | |
8447 | sindex += t_index; | |
866961ad | 8448 | goto add_quoted_string; /* XXX was add_string */ |
22e63b05 CR |
8449 | } |
8450 | else | |
8451 | { | |
8452 | FREE (temp); | |
8453 | goto add_character; | |
8454 | } | |
8455 | ||
726f6388 | 8456 | case '$': |
726f6388 JA |
8457 | if (expanded_something) |
8458 | *expanded_something = 1; | |
8459 | ||
348a457e | 8460 | temp_has_dollar_at = 0; |
e1e48bba CR |
8461 | pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0; |
8462 | if (word->flags & W_NOSPLIT2) | |
8463 | pflags |= PF_NOSPLIT2; | |
40647963 CR |
8464 | if (word->flags & W_ASSIGNRHS) |
8465 | pflags |= PF_ASSIGNRHS; | |
227f982e | 8466 | tword = param_expand (string, &sindex, quoted, expanded_something, |
348a457e | 8467 | &temp_has_dollar_at, "ed_dollar_at, |
e1e48bba | 8468 | &had_quoted_null, pflags); |
348a457e | 8469 | has_dollar_at += temp_has_dollar_at; |
f0c4de40 | 8470 | split_on_spaces += (tword->flags & W_SPLITSPACE); |
f0c4de40 | 8471 | |
227f982e | 8472 | if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal) |
726f6388 | 8473 | { |
cce855bc JA |
8474 | free (string); |
8475 | free (istring); | |
227f982e CR |
8476 | return ((tword == &expand_wdesc_error) ? &expand_word_error |
8477 | : &expand_word_fatal); | |
cce855bc JA |
8478 | } |
8479 | if (contains_dollar_at && has_dollar_at) | |
8480 | *contains_dollar_at = 1; | |
227f982e CR |
8481 | |
8482 | if (tword && (tword->flags & W_HASQUOTEDNULL)) | |
8483 | had_quoted_null = 1; | |
8484 | ||
77638cbf | 8485 | temp = tword ? tword->word : (char *)NULL; |
227f982e CR |
8486 | dispose_word_desc (tword); |
8487 | ||
efc2f955 CR |
8488 | /* Kill quoted nulls; we will add them back at the end of |
8489 | expand_word_internal if nothing else in the string */ | |
8490 | if (had_quoted_null && temp && QUOTED_NULL (temp)) | |
8491 | { | |
8492 | FREE (temp); | |
8493 | temp = (char *)NULL; | |
8494 | } | |
8495 | ||
cce855bc JA |
8496 | goto add_string; |
8497 | break; | |
726f6388 | 8498 | |
cce855bc JA |
8499 | case '`': /* Backquoted command substitution. */ |
8500 | { | |
d3a24ed2 | 8501 | t_index = sindex++; |
726f6388 | 8502 | |
e6598ba4 | 8503 | temp = string_extract (string, &sindex, "`", SX_REQMATCH); |
bc7bed50 CR |
8504 | /* The test of sindex against t_index is to allow bare instances of |
8505 | ` to pass through, for backwards compatibility. */ | |
7027abcb CR |
8506 | if (temp == &extract_string_error || temp == &extract_string_fatal) |
8507 | { | |
bc7bed50 CR |
8508 | if (sindex - 1 == t_index) |
8509 | { | |
8510 | sindex = t_index; | |
8511 | goto add_character; | |
8512 | } | |
7f947b68 | 8513 | last_command_exit_value = EXECUTION_FAILURE; |
d3ad40de | 8514 | report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index); |
7027abcb CR |
8515 | free (string); |
8516 | free (istring); | |
8517 | return ((temp == &extract_string_error) ? &expand_word_error | |
8518 | : &expand_word_fatal); | |
8519 | } | |
8520 | ||
bc7bed50 CR |
8521 | if (expanded_something) |
8522 | *expanded_something = 1; | |
8523 | ||
d3a24ed2 CR |
8524 | if (word->flags & W_NOCOMSUB) |
8525 | /* sindex + 1 because string[sindex] == '`' */ | |
8526 | temp1 = substring (string, t_index, sindex + 1); | |
8527 | else | |
8528 | { | |
8529 | de_backslash (temp); | |
d3ad40de CR |
8530 | tword = command_substitute (temp, quoted); |
8531 | temp1 = tword ? tword->word : (char *)NULL; | |
e33f2203 CR |
8532 | if (tword) |
8533 | dispose_word_desc (tword); | |
d3a24ed2 | 8534 | } |
cce855bc JA |
8535 | FREE (temp); |
8536 | temp = temp1; | |
8537 | goto dollar_add_string; | |
8538 | } | |
ccc6cda3 | 8539 | |
cce855bc JA |
8540 | case '\\': |
8541 | if (string[sindex + 1] == '\n') | |
8542 | { | |
8543 | sindex += 2; | |
8544 | continue; | |
8545 | } | |
726f6388 | 8546 | |
cce855bc | 8547 | c = string[++sindex]; |
726f6388 | 8548 | |
cce855bc | 8549 | if (quoted & Q_HERE_DOCUMENT) |
28ef6c31 | 8550 | tflag = CBSHDOC; |
cce855bc | 8551 | else if (quoted & Q_DOUBLE_QUOTES) |
28ef6c31 | 8552 | tflag = CBSDQUOTE; |
cce855bc | 8553 | else |
28ef6c31 JA |
8554 | tflag = 0; |
8555 | ||
64419627 | 8556 | /* From Posix discussion on austin-group list: Backslash escaping |
9e51a74d CR |
8557 | a } in ${...} is removed. Issue 0000221 */ |
8558 | if ((quoted & Q_DOLBRACE) && c == RBRACE) | |
64419627 | 8559 | { |
ad4aef08 CR |
8560 | SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size); |
8561 | } | |
8562 | /* This is the fix for " $@\ " */ | |
d76edd30 | 8563 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0) && isexp == 0 && isifs (c)) |
ad4aef08 CR |
8564 | { |
8565 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, | |
8566 | DEFAULT_ARRAY_SIZE); | |
8567 | istring[istring_index++] = CTLESC; | |
8568 | istring[istring_index++] = '\\'; | |
8569 | istring[istring_index] = '\0'; | |
8570 | ||
64419627 CR |
8571 | SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size); |
8572 | } | |
8573 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0)) | |
cce855bc | 8574 | { |
7117c2d2 | 8575 | SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size); |
bb70624e JA |
8576 | } |
8577 | else if (c == 0) | |
8578 | { | |
8579 | c = CTLNUL; | |
8580 | sindex--; /* add_character: label increments sindex */ | |
8581 | goto add_character; | |
cce855bc JA |
8582 | } |
8583 | else | |
bb70624e | 8584 | { |
7117c2d2 | 8585 | SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size); |
bb70624e | 8586 | } |
726f6388 | 8587 | |
bb70624e JA |
8588 | sindex++; |
8589 | add_twochars: | |
8590 | /* BEFORE jumping here, we need to increment sindex if appropriate */ | |
8591 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, | |
8592 | DEFAULT_ARRAY_SIZE); | |
8593 | istring[istring_index++] = twochars[0]; | |
8594 | istring[istring_index++] = twochars[1]; | |
8595 | istring[istring_index] = '\0'; | |
8596 | ||
8597 | break; | |
726f6388 | 8598 | |
cce855bc | 8599 | case '"': |
da719982 | 8600 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
cce855bc | 8601 | goto add_character; |
ccc6cda3 JA |
8602 | |
8603 | t_index = ++sindex; | |
8604 | temp = string_extract_double_quoted (string, &sindex, 0); | |
8605 | ||
8606 | /* If the quotes surrounded the entire string, then the | |
8607 | whole word was quoted. */ | |
8608 | quoted_state = (t_index == 1 && string[sindex] == '\0') | |
8609 | ? WHOLLY_QUOTED | |
7117c2d2 | 8610 | : PARTIALLY_QUOTED; |
ccc6cda3 JA |
8611 | |
8612 | if (temp && *temp) | |
726f6388 | 8613 | { |
227f982e CR |
8614 | tword = alloc_word_desc (); |
8615 | tword->word = temp; | |
8616 | ||
ccc6cda3 JA |
8617 | temp = (char *)NULL; |
8618 | ||
348a457e | 8619 | temp_has_dollar_at = 0; /* XXX */ |
227f982e | 8620 | /* Need to get W_HASQUOTEDNULL flag through this function. */ |
348a457e CR |
8621 | list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &temp_has_dollar_at, (int *)NULL); |
8622 | has_dollar_at += temp_has_dollar_at; | |
726f6388 | 8623 | |
ccc6cda3 JA |
8624 | if (list == &expand_word_error || list == &expand_word_fatal) |
8625 | { | |
8626 | free (istring); | |
8627 | free (string); | |
8628 | /* expand_word_internal has already freed temp_word->word | |
8629 | for us because of the way it prints error messages. */ | |
8630 | tword->word = (char *)NULL; | |
8631 | dispose_word (tword); | |
8632 | return list; | |
8633 | } | |
726f6388 | 8634 | |
ccc6cda3 | 8635 | dispose_word (tword); |
726f6388 | 8636 | |
ccc6cda3 JA |
8637 | /* "$@" (a double-quoted dollar-at) expands into nothing, |
8638 | not even a NULL word, when there are no positional | |
8639 | parameters. */ | |
cce855bc | 8640 | if (list == 0 && has_dollar_at) |
726f6388 | 8641 | { |
ccc6cda3 JA |
8642 | quoted_dollar_at++; |
8643 | break; | |
8644 | } | |
8645 | ||
8646 | /* If we get "$@", we know we have expanded something, so we | |
8647 | need to remember it for the final split on $IFS. This is | |
8648 | a special case; it's the only case where a quoted string | |
8649 | can expand into more than one word. It's going to come back | |
8650 | from the above call to expand_word_internal as a list with | |
8651 | a single word, in which all characters are quoted and | |
8652 | separated by blanks. What we want to do is to turn it back | |
8653 | into a list for the next piece of code. */ | |
8654 | if (list) | |
8655 | dequote_list (list); | |
8656 | ||
227f982e | 8657 | if (list && list->word && (list->word->flags & W_HASQUOTEDNULL)) |
efc2f955 | 8658 | had_quoted_null = 1; /* XXX */ |
227f982e | 8659 | |
cce855bc | 8660 | if (has_dollar_at) |
ccc6cda3 JA |
8661 | { |
8662 | quoted_dollar_at++; | |
8663 | if (contains_dollar_at) | |
8664 | *contains_dollar_at = 1; | |
8665 | if (expanded_something) | |
8666 | *expanded_something = 1; | |
8667 | } | |
8668 | } | |
8669 | else | |
8670 | { | |
8671 | /* What we have is "". This is a minor optimization. */ | |
f73dda09 | 8672 | FREE (temp); |
ccc6cda3 JA |
8673 | list = (WORD_LIST *)NULL; |
8674 | } | |
8675 | ||
8676 | /* The code above *might* return a list (consider the case of "$@", | |
8677 | where it returns "$1", "$2", etc.). We can't throw away the | |
8678 | rest of the list, and we have to make sure each word gets added | |
8679 | as quoted. We test on tresult->next: if it is non-NULL, we | |
8680 | quote the whole list, save it to a string with string_list, and | |
8681 | add that string. We don't need to quote the results of this | |
8682 | (and it would be wrong, since that would quote the separators | |
8683 | as well), so we go directly to add_string. */ | |
8684 | if (list) | |
8685 | { | |
8686 | if (list->next) | |
8687 | { | |
bc4cd23c JA |
8688 | /* Testing quoted_dollar_at makes sure that "$@" is |
8689 | split correctly when $IFS does not contain a space. */ | |
8690 | temp = quoted_dollar_at | |
8691 | ? string_list_dollar_at (list, Q_DOUBLE_QUOTES) | |
8692 | : string_list (quote_list (list)); | |
ccc6cda3 | 8693 | dispose_words (list); |
726f6388 JA |
8694 | goto add_string; |
8695 | } | |
8696 | else | |
8697 | { | |
ccc6cda3 | 8698 | temp = savestring (list->word->word); |
227f982e | 8699 | tflag = list->word->flags; |
ccc6cda3 | 8700 | dispose_words (list); |
227f982e | 8701 | |
cce855bc JA |
8702 | /* If the string is not a quoted null string, we want |
8703 | to remove any embedded unquoted CTLNUL characters. | |
8704 | We do not want to turn quoted null strings back into | |
8705 | the empty string, though. We do this because we | |
8706 | want to remove any quoted nulls from expansions that | |
8707 | contain other characters. For example, if we have | |
8708 | x"$*"y or "x$*y" and there are no positional parameters, | |
7117c2d2 | 8709 | the $* should expand into nothing. */ |
227f982e CR |
8710 | /* We use the W_HASQUOTEDNULL flag to differentiate the |
8711 | cases: a quoted null character as above and when | |
8712 | CTLNUL is contained in the (non-null) expansion | |
8713 | of some variable. We use the had_quoted_null flag to | |
10590446 | 8714 | pass the value through this function to its caller. */ |
227f982e | 8715 | if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0) |
cce855bc | 8716 | remove_quoted_nulls (temp); /* XXX */ |
726f6388 JA |
8717 | } |
8718 | } | |
ccc6cda3 JA |
8719 | else |
8720 | temp = (char *)NULL; | |
726f6388 | 8721 | |
ccc6cda3 | 8722 | /* We do not want to add quoted nulls to strings that are only |
efc2f955 | 8723 | partially quoted; we can throw them away. The exception to |
f4f5e1c2 CR |
8724 | this is when we are going to be performing word splitting, |
8725 | since we have to preserve a null argument if the next character | |
8726 | will cause word splitting. */ | |
adc6cff5 | 8727 | if (temp == 0 && quoted_state == PARTIALLY_QUOTED && (word->flags & (W_NOSPLIT|W_NOSPLIT2))) |
cce855bc | 8728 | continue; |
726f6388 | 8729 | |
ccc6cda3 | 8730 | add_quoted_string: |
726f6388 | 8731 | |
ccc6cda3 JA |
8732 | if (temp) |
8733 | { | |
8734 | temp1 = temp; | |
8735 | temp = quote_string (temp); | |
8736 | free (temp1); | |
bb70624e | 8737 | goto add_string; |
ccc6cda3 JA |
8738 | } |
8739 | else | |
8740 | { | |
8741 | /* Add NULL arg. */ | |
bb70624e JA |
8742 | c = CTLNUL; |
8743 | sindex--; /* add_character: label increments sindex */ | |
8744 | goto add_character; | |
ccc6cda3 | 8745 | } |
bb70624e | 8746 | |
ccc6cda3 | 8747 | /* break; */ |
726f6388 | 8748 | |
ccc6cda3 | 8749 | case '\'': |
da719982 | 8750 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
ccc6cda3 | 8751 | goto add_character; |
726f6388 | 8752 | |
ccc6cda3 JA |
8753 | t_index = ++sindex; |
8754 | temp = string_extract_single_quoted (string, &sindex); | |
726f6388 | 8755 | |
ccc6cda3 JA |
8756 | /* If the entire STRING was surrounded by single quotes, |
8757 | then the string is wholly quoted. */ | |
8758 | quoted_state = (t_index == 1 && string[sindex] == '\0') | |
8759 | ? WHOLLY_QUOTED | |
7117c2d2 | 8760 | : PARTIALLY_QUOTED; |
726f6388 | 8761 | |
ccc6cda3 JA |
8762 | /* If all we had was '', it is a null expansion. */ |
8763 | if (*temp == '\0') | |
8764 | { | |
8765 | free (temp); | |
8766 | temp = (char *)NULL; | |
8767 | } | |
8768 | else | |
7117c2d2 | 8769 | remove_quoted_escapes (temp); /* ??? */ |
726f6388 | 8770 | |
ccc6cda3 JA |
8771 | /* We do not want to add quoted nulls to strings that are only |
8772 | partially quoted; such nulls are discarded. */ | |
8773 | if (temp == 0 && (quoted_state == PARTIALLY_QUOTED)) | |
8774 | continue; | |
726f6388 | 8775 | |
bb70624e JA |
8776 | /* If we have a quoted null expansion, add a quoted NULL to istring. */ |
8777 | if (temp == 0) | |
8778 | { | |
8779 | c = CTLNUL; | |
8780 | sindex--; /* add_character: label increments sindex */ | |
8781 | goto add_character; | |
8782 | } | |
8783 | else | |
8784 | goto add_quoted_string; | |
8785 | ||
ccc6cda3 | 8786 | /* break; */ |
726f6388 JA |
8787 | |
8788 | default: | |
726f6388 | 8789 | /* This is the fix for " $@ " */ |
c1d39fb8 | 8790 | add_ifs_character: |
7117c2d2 | 8791 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c))) |
726f6388 | 8792 | { |
bb70624e JA |
8793 | if (string[sindex]) /* from old goto dollar_add_string */ |
8794 | sindex++; | |
8795 | if (c == 0) | |
8796 | { | |
8797 | c = CTLNUL; | |
8798 | goto add_character; | |
8799 | } | |
8800 | else | |
8801 | { | |
7117c2d2 | 8802 | #if HANDLE_MULTIBYTE |
545f34cf CR |
8803 | if (MB_CUR_MAX > 1) |
8804 | sindex--; | |
8805 | ||
7117c2d2 JA |
8806 | if (MB_CUR_MAX > 1) |
8807 | { | |
545f34cf | 8808 | SADD_MBQCHAR_BODY(temp, string, sindex, string_size); |
7117c2d2 JA |
8809 | } |
8810 | else | |
8811 | #endif | |
8812 | { | |
8813 | twochars[0] = CTLESC; | |
8814 | twochars[1] = c; | |
8815 | goto add_twochars; | |
8816 | } | |
bb70624e | 8817 | } |
726f6388 JA |
8818 | } |
8819 | ||
7117c2d2 JA |
8820 | SADD_MBCHAR (temp, string, sindex, string_size); |
8821 | ||
726f6388 | 8822 | add_character: |
ccc6cda3 JA |
8823 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size, |
8824 | DEFAULT_ARRAY_SIZE); | |
726f6388 JA |
8825 | istring[istring_index++] = c; |
8826 | istring[istring_index] = '\0'; | |
8827 | ||
8828 | /* Next character. */ | |
8829 | sindex++; | |
8830 | } | |
8831 | } | |
8832 | ||
8833 | finished_with_string: | |
726f6388 JA |
8834 | /* OK, we're ready to return. If we have a quoted string, and |
8835 | quoted_dollar_at is not set, we do no splitting at all; otherwise | |
8836 | we split on ' '. The routines that call this will handle what to | |
8837 | do if nothing has been expanded. */ | |
ccc6cda3 JA |
8838 | |
8839 | /* Partially and wholly quoted strings which expand to the empty | |
8840 | string are retained as an empty arguments. Unquoted strings | |
8841 | which expand to the empty string are discarded. The single | |
8842 | exception is the case of expanding "$@" when there are no | |
8843 | positional parameters. In that case, we discard the expansion. */ | |
8844 | ||
8845 | /* Because of how the code that handles "" and '' in partially | |
8846 | quoted strings works, we need to make ISTRING into a QUOTED_NULL | |
8847 | if we saw quoting characters, but the expansion was empty. | |
8848 | "" and '' are tossed away before we get to this point when | |
8849 | processing partially quoted strings. This makes "" and $xxx"" | |
8850 | equivalent when xxx is unset. We also look to see whether we | |
8851 | saw a quoted null from a ${} expansion and add one back if we | |
8852 | need to. */ | |
8853 | ||
8854 | /* If we expand to nothing and there were no single or double quotes | |
8855 | in the word, we throw it away. Otherwise, we return a NULL word. | |
8856 | The single exception is for $@ surrounded by double quotes when | |
8857 | there are no positional parameters. In that case, we also throw | |
8858 | the word away. */ | |
8859 | ||
8860 | if (*istring == '\0') | |
8861 | { | |
8862 | if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED)) | |
726f6388 | 8863 | { |
726f6388 JA |
8864 | istring[0] = CTLNUL; |
8865 | istring[1] = '\0'; | |
ccc6cda3 | 8866 | tword = make_bare_word (istring); |
227f982e | 8867 | tword->flags |= W_HASQUOTEDNULL; /* XXX */ |
ccc6cda3 JA |
8868 | list = make_word_list (tword, (WORD_LIST *)NULL); |
8869 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
8870 | tword->flags |= W_QUOTED; | |
726f6388 | 8871 | } |
ccc6cda3 JA |
8872 | /* According to sh, ksh, and Posix.2, if a word expands into nothing |
8873 | and a double-quoted "$@" appears anywhere in it, then the entire | |
8874 | word is removed. */ | |
8875 | else if (quoted_state == UNQUOTED || quoted_dollar_at) | |
8876 | list = (WORD_LIST *)NULL; | |
8877 | #if 0 | |
8878 | else | |
726f6388 | 8879 | { |
ccc6cda3 | 8880 | tword = make_bare_word (istring); |
ccc6cda3 JA |
8881 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
8882 | tword->flags |= W_QUOTED; | |
227f982e | 8883 | list = make_word_list (tword, (WORD_LIST *)NULL); |
726f6388 | 8884 | } |
f73dda09 JA |
8885 | #else |
8886 | else | |
8887 | list = (WORD_LIST *)NULL; | |
ccc6cda3 JA |
8888 | #endif |
8889 | } | |
8890 | else if (word->flags & W_NOSPLIT) | |
8891 | { | |
8892 | tword = make_bare_word (istring); | |
ccc6cda3 JA |
8893 | if (word->flags & W_ASSIGNMENT) |
8894 | tword->flags |= W_ASSIGNMENT; /* XXX */ | |
43df7bbb CR |
8895 | if (word->flags & W_COMPASSIGN) |
8896 | tword->flags |= W_COMPASSIGN; /* XXX */ | |
b72432fd JA |
8897 | if (word->flags & W_NOGLOB) |
8898 | tword->flags |= W_NOGLOB; /* XXX */ | |
df0e4bfe CR |
8899 | if (word->flags & W_NOBRACE) |
8900 | tword->flags |= W_NOBRACE; /* XXX */ | |
43df7bbb CR |
8901 | if (word->flags & W_NOEXPAND) |
8902 | tword->flags |= W_NOEXPAND; /* XXX */ | |
ccc6cda3 | 8903 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
28ef6c31 | 8904 | tword->flags |= W_QUOTED; |
efc2f955 | 8905 | if (had_quoted_null && QUOTED_NULL (istring)) |
227f982e CR |
8906 | tword->flags |= W_HASQUOTEDNULL; |
8907 | list = make_word_list (tword, (WORD_LIST *)NULL); | |
ccc6cda3 JA |
8908 | } |
8909 | else | |
8910 | { | |
8911 | char *ifs_chars; | |
0d9b018b | 8912 | char *tstring; |
ccc6cda3 | 8913 | |
7117c2d2 | 8914 | ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL; |
726f6388 | 8915 | |
cce855bc JA |
8916 | /* If we have $@, we need to split the results no matter what. If |
8917 | IFS is unset or NULL, string_list_dollar_at has separated the | |
8918 | positional parameters with a space, so we split on space (we have | |
8919 | set ifs_chars to " \t\n" above if ifs is unset). If IFS is set, | |
8920 | string_list_dollar_at has separated the positional parameters | |
1a81420a CR |
8921 | with the first character of $IFS, so we split on $IFS. If |
8922 | SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either | |
8923 | unset or null, and we want to make sure that we split on spaces | |
8924 | regardless of what else has happened to IFS since the expansion. */ | |
8925 | if (split_on_spaces) | |
8926 | list = list_string (istring, " ", 1); /* XXX quoted == 1? */ | |
0d9b018b CR |
8927 | /* If we have $@ (has_dollar_at != 0) and we are in a context where we |
8928 | don't want to split the result (W_NOSPLIT2), and we are not quoted, | |
8929 | we have already separated the arguments with the first character of | |
8930 | $IFS. In this case, we want to return a list with a single word | |
8931 | with the separator possibly replaced with a space (it's what other | |
8932 | shells seem to do). | |
8933 | quoted_dollar_at is internal to this function and is set if we are | |
8934 | passed an argument that is unquoted (quoted == 0) but we encounter a | |
8935 | double-quoted $@ while expanding it. */ | |
8936 | else if (has_dollar_at && quoted_dollar_at == 0 && ifs_chars && quoted == 0 && (word->flags & W_NOSPLIT2)) | |
8937 | { | |
8938 | /* Only split and rejoin if we have to */ | |
8939 | if (*ifs_chars && *ifs_chars != ' ') | |
8940 | { | |
8941 | list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1); | |
8942 | tstring = string_list (list); | |
8943 | } | |
8944 | else | |
8945 | tstring = istring; | |
8946 | tword = make_bare_word (tstring); | |
8947 | if (tstring != istring) | |
8948 | free (tstring); | |
8949 | goto set_word_flags; | |
8950 | } | |
1a81420a | 8951 | else if (has_dollar_at && ifs_chars) |
cce855bc | 8952 | list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1); |
ccc6cda3 JA |
8953 | else |
8954 | { | |
8955 | tword = make_bare_word (istring); | |
0d9b018b | 8956 | set_word_flags: |
ccc6cda3 JA |
8957 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED)) |
8958 | tword->flags |= W_QUOTED; | |
8959 | if (word->flags & W_ASSIGNMENT) | |
8960 | tword->flags |= W_ASSIGNMENT; | |
43df7bbb CR |
8961 | if (word->flags & W_COMPASSIGN) |
8962 | tword->flags |= W_COMPASSIGN; | |
b72432fd JA |
8963 | if (word->flags & W_NOGLOB) |
8964 | tword->flags |= W_NOGLOB; | |
df0e4bfe CR |
8965 | if (word->flags & W_NOBRACE) |
8966 | tword->flags |= W_NOBRACE; | |
43df7bbb CR |
8967 | if (word->flags & W_NOEXPAND) |
8968 | tword->flags |= W_NOEXPAND; | |
efc2f955 | 8969 | if (had_quoted_null && QUOTED_NULL (istring)) |
227f982e CR |
8970 | tword->flags |= W_HASQUOTEDNULL; /* XXX */ |
8971 | list = make_word_list (tword, (WORD_LIST *)NULL); | |
726f6388 | 8972 | } |
726f6388 | 8973 | } |
726f6388 | 8974 | |
ccc6cda3 JA |
8975 | free (istring); |
8976 | return (list); | |
726f6388 JA |
8977 | } |
8978 | ||
8979 | /* **************************************************************** */ | |
8980 | /* */ | |
8981 | /* Functions for Quote Removal */ | |
8982 | /* */ | |
8983 | /* **************************************************************** */ | |
8984 | ||
8985 | /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the | |
7117c2d2 | 8986 | backslash quoting rules for within double quotes or a here document. */ |
726f6388 JA |
8987 | char * |
8988 | string_quote_removal (string, quoted) | |
8989 | char *string; | |
8990 | int quoted; | |
8991 | { | |
7117c2d2 JA |
8992 | size_t slen; |
8993 | char *r, *result_string, *temp, *send; | |
f73dda09 JA |
8994 | int sindex, tindex, dquote; |
8995 | unsigned char c; | |
7117c2d2 | 8996 | DECLARE_MBSTATE; |
726f6388 JA |
8997 | |
8998 | /* The result can be no longer than the original string. */ | |
7117c2d2 JA |
8999 | slen = strlen (string); |
9000 | send = string + slen; | |
9001 | ||
9002 | r = result_string = (char *)xmalloc (slen + 1); | |
726f6388 | 9003 | |
ccc6cda3 | 9004 | for (dquote = sindex = 0; c = string[sindex];) |
726f6388 JA |
9005 | { |
9006 | switch (c) | |
9007 | { | |
9008 | case '\\': | |
9009 | c = string[++sindex]; | |
33fe8777 CR |
9010 | if (c == 0) |
9011 | { | |
9012 | *r++ = '\\'; | |
9013 | break; | |
9014 | } | |
28ef6c31 | 9015 | if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0) |
726f6388 | 9016 | *r++ = '\\'; |
ccc6cda3 | 9017 | /* FALLTHROUGH */ |
726f6388 JA |
9018 | |
9019 | default: | |
7117c2d2 | 9020 | SCOPY_CHAR_M (r, string, send, sindex); |
726f6388 JA |
9021 | break; |
9022 | ||
9023 | case '\'': | |
ccc6cda3 | 9024 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) |
726f6388 JA |
9025 | { |
9026 | *r++ = c; | |
9027 | sindex++; | |
ccc6cda3 | 9028 | break; |
726f6388 | 9029 | } |
ccc6cda3 JA |
9030 | tindex = sindex + 1; |
9031 | temp = string_extract_single_quoted (string, &tindex); | |
9032 | if (temp) | |
726f6388 | 9033 | { |
ccc6cda3 JA |
9034 | strcpy (r, temp); |
9035 | r += strlen (r); | |
9036 | free (temp); | |
726f6388 | 9037 | } |
ccc6cda3 | 9038 | sindex = tindex; |
726f6388 JA |
9039 | break; |
9040 | ||
9041 | case '"': | |
9042 | dquote = 1 - dquote; | |
9043 | sindex++; | |
9044 | break; | |
9045 | } | |
9046 | } | |
9047 | *r = '\0'; | |
9048 | return (result_string); | |
9049 | } | |
9050 | ||
ccc6cda3 JA |
9051 | #if 0 |
9052 | /* UNUSED */ | |
726f6388 JA |
9053 | /* Perform quote removal on word WORD. This allocates and returns a new |
9054 | WORD_DESC *. */ | |
9055 | WORD_DESC * | |
9056 | word_quote_removal (word, quoted) | |
9057 | WORD_DESC *word; | |
9058 | int quoted; | |
9059 | { | |
9060 | WORD_DESC *w; | |
9061 | char *t; | |
9062 | ||
9063 | t = string_quote_removal (word->word, quoted); | |
227f982e CR |
9064 | w = alloc_word_desc (); |
9065 | w->word = t ? t : savestring (""); | |
726f6388 JA |
9066 | return (w); |
9067 | } | |
9068 | ||
9069 | /* Perform quote removal on all words in LIST. If QUOTED is non-zero, | |
9070 | the members of the list are treated as if they are surrounded by | |
9071 | double quotes. Return a new list, or NULL if LIST is NULL. */ | |
9072 | WORD_LIST * | |
9073 | word_list_quote_removal (list, quoted) | |
9074 | WORD_LIST *list; | |
9075 | int quoted; | |
9076 | { | |
227f982e | 9077 | WORD_LIST *result, *t, *tresult, *e; |
726f6388 | 9078 | |
ccc6cda3 | 9079 | for (t = list, result = (WORD_LIST *)NULL; t; t = t->next) |
726f6388 | 9080 | { |
7117c2d2 | 9081 | tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL); |
227f982e | 9082 | #if 0 |
726f6388 | 9083 | result = (WORD_LIST *) list_append (result, tresult); |
227f982e CR |
9084 | #else |
9085 | if (result == 0) | |
9086 | result = e = tresult; | |
9087 | else | |
9088 | { | |
9089 | e->next = tresult; | |
9090 | while (e->next) | |
9091 | e = e->next; | |
9092 | } | |
9093 | #endif | |
726f6388 JA |
9094 | } |
9095 | return (result); | |
9096 | } | |
ccc6cda3 | 9097 | #endif |
726f6388 | 9098 | |
726f6388 JA |
9099 | /******************************************* |
9100 | * * | |
9101 | * Functions to perform word splitting * | |
9102 | * * | |
9103 | *******************************************/ | |
9104 | ||
7117c2d2 JA |
9105 | void |
9106 | setifs (v) | |
9107 | SHELL_VAR *v; | |
b72432fd | 9108 | { |
7117c2d2 JA |
9109 | char *t; |
9110 | unsigned char uc; | |
9111 | ||
9112 | ifs_var = v; | |
cdb32d45 | 9113 | ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n"; |
b72432fd | 9114 | |
f0c4de40 CR |
9115 | ifs_is_set = ifs_var != 0; |
9116 | ifs_is_null = ifs_is_set && (*ifs_value == 0); | |
9117 | ||
633e5c6d CR |
9118 | /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet |
9119 | handle multibyte chars in IFS */ | |
7117c2d2 JA |
9120 | memset (ifs_cmap, '\0', sizeof (ifs_cmap)); |
9121 | for (t = ifs_value ; t && *t; t++) | |
9122 | { | |
9123 | uc = *t; | |
9124 | ifs_cmap[uc] = 1; | |
9125 | } | |
9126 | ||
633e5c6d CR |
9127 | #if defined (HANDLE_MULTIBYTE) |
9128 | if (ifs_value == 0) | |
9129 | { | |
9130 | ifs_firstc[0] = '\0'; | |
9131 | ifs_firstc_len = 1; | |
9132 | } | |
9133 | else | |
9134 | { | |
9135 | size_t ifs_len; | |
9136 | ifs_len = strnlen (ifs_value, MB_CUR_MAX); | |
9137 | ifs_firstc_len = MBLEN (ifs_value, ifs_len); | |
22e63b05 | 9138 | if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len)) |
633e5c6d CR |
9139 | { |
9140 | ifs_firstc[0] = ifs_value[0]; | |
9141 | ifs_firstc[1] = '\0'; | |
9142 | ifs_firstc_len = 1; | |
9143 | } | |
9144 | else | |
9145 | memcpy (ifs_firstc, ifs_value, ifs_firstc_len); | |
9146 | } | |
9147 | #else | |
7117c2d2 | 9148 | ifs_firstc = ifs_value ? *ifs_value : 0; |
633e5c6d | 9149 | #endif |
7117c2d2 JA |
9150 | } |
9151 | ||
9152 | char * | |
9153 | getifs () | |
9154 | { | |
9155 | return ifs_value; | |
b72432fd JA |
9156 | } |
9157 | ||
726f6388 JA |
9158 | /* This splits a single word into a WORD LIST on $IFS, but only if the word |
9159 | is not quoted. list_string () performs quote removal for us, even if we | |
9160 | don't do any splitting. */ | |
9161 | WORD_LIST * | |
7117c2d2 | 9162 | word_split (w, ifs_chars) |
726f6388 | 9163 | WORD_DESC *w; |
7117c2d2 | 9164 | char *ifs_chars; |
726f6388 JA |
9165 | { |
9166 | WORD_LIST *result; | |
9167 | ||
9168 | if (w) | |
9169 | { | |
7117c2d2 | 9170 | char *xifs; |
726f6388 | 9171 | |
7117c2d2 JA |
9172 | xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars; |
9173 | result = list_string (w->word, xifs, w->flags & W_QUOTED); | |
726f6388 JA |
9174 | } |
9175 | else | |
9176 | result = (WORD_LIST *)NULL; | |
ccc6cda3 | 9177 | |
726f6388 JA |
9178 | return (result); |
9179 | } | |
9180 | ||
9181 | /* Perform word splitting on LIST and return the RESULT. It is possible | |
9182 | to return (WORD_LIST *)NULL. */ | |
9183 | static WORD_LIST * | |
9184 | word_list_split (list) | |
9185 | WORD_LIST *list; | |
9186 | { | |
37c41ab1 | 9187 | WORD_LIST *result, *t, *tresult, *e; |
726f6388 | 9188 | |
ccc6cda3 | 9189 | for (t = list, result = (WORD_LIST *)NULL; t; t = t->next) |
726f6388 | 9190 | { |
7117c2d2 | 9191 | tresult = word_split (t->word, ifs_value); |
37c41ab1 CR |
9192 | if (result == 0) |
9193 | result = e = tresult; | |
9194 | else | |
9195 | { | |
9196 | e->next = tresult; | |
9197 | while (e->next) | |
9198 | e = e->next; | |
9199 | } | |
726f6388 JA |
9200 | } |
9201 | return (result); | |
9202 | } | |
9203 | ||
9204 | /************************************************** | |
9205 | * * | |
cce855bc | 9206 | * Functions to expand an entire WORD_LIST * |
726f6388 JA |
9207 | * * |
9208 | **************************************************/ | |
9209 | ||
d3a24ed2 CR |
9210 | /* Do any word-expansion-specific cleanup and jump to top_level */ |
9211 | static void | |
9212 | exp_jump_to_top_level (v) | |
9213 | int v; | |
9214 | { | |
f11997f8 CR |
9215 | set_pipestatus_from_exit (last_command_exit_value); |
9216 | ||
d3a24ed2 CR |
9217 | /* Cleanup code goes here. */ |
9218 | expand_no_split_dollar_star = 0; /* XXX */ | |
9219 | expanding_redir = 0; | |
d7f49990 | 9220 | assigning_in_environment = 0; |
d3a24ed2 | 9221 | |
2e4498b3 CR |
9222 | if (parse_and_execute_level == 0) |
9223 | top_level_cleanup (); /* from sig.c */ | |
9224 | ||
d3a24ed2 CR |
9225 | jump_to_top_level (v); |
9226 | } | |
9227 | ||
cce855bc JA |
9228 | /* Put NLIST (which is a WORD_LIST * of only one element) at the front of |
9229 | ELIST, and set ELIST to the new list. */ | |
9230 | #define PREPEND_LIST(nlist, elist) \ | |
9231 | do { nlist->next = elist; elist = nlist; } while (0) | |
9232 | ||
726f6388 JA |
9233 | /* Separate out any initial variable assignments from TLIST. If set -k has |
9234 | been executed, remove all assignment statements from TLIST. Initial | |
9235 | variable assignments and other environment assignments are placed | |
bb70624e | 9236 | on SUBST_ASSIGN_VARLIST. */ |
726f6388 JA |
9237 | static WORD_LIST * |
9238 | separate_out_assignments (tlist) | |
9239 | WORD_LIST *tlist; | |
9240 | { | |
9241 | register WORD_LIST *vp, *lp; | |
9242 | ||
0527c903 | 9243 | if (tlist == 0) |
726f6388 JA |
9244 | return ((WORD_LIST *)NULL); |
9245 | ||
bb70624e JA |
9246 | if (subst_assign_varlist) |
9247 | dispose_words (subst_assign_varlist); /* Clean up after previous error */ | |
b72432fd | 9248 | |
bb70624e | 9249 | subst_assign_varlist = (WORD_LIST *)NULL; |
726f6388 JA |
9250 | vp = lp = tlist; |
9251 | ||
9252 | /* Separate out variable assignments at the start of the command. | |
9253 | Loop invariant: vp->next == lp | |
9254 | Loop postcondition: | |
7117c2d2 JA |
9255 | lp = list of words left after assignment statements skipped |
9256 | tlist = original list of words | |
726f6388 | 9257 | */ |
ccc6cda3 | 9258 | while (lp && (lp->word->flags & W_ASSIGNMENT)) |
726f6388 JA |
9259 | { |
9260 | vp = lp; | |
9261 | lp = lp->next; | |
9262 | } | |
9263 | ||
bb70624e JA |
9264 | /* If lp != tlist, we have some initial assignment statements. |
9265 | We make SUBST_ASSIGN_VARLIST point to the list of assignment | |
9266 | words and TLIST point to the remaining words. */ | |
726f6388 JA |
9267 | if (lp != tlist) |
9268 | { | |
bb70624e | 9269 | subst_assign_varlist = tlist; |
726f6388 JA |
9270 | /* ASSERT(vp->next == lp); */ |
9271 | vp->next = (WORD_LIST *)NULL; /* terminate variable list */ | |
9272 | tlist = lp; /* remainder of word list */ | |
9273 | } | |
9274 | ||
9275 | /* vp == end of variable list */ | |
9276 | /* tlist == remainder of original word list without variable assignments */ | |
9277 | if (!tlist) | |
9278 | /* All the words in tlist were assignment statements */ | |
9279 | return ((WORD_LIST *)NULL); | |
9280 | ||
9281 | /* ASSERT(tlist != NULL); */ | |
ccc6cda3 | 9282 | /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */ |
726f6388 JA |
9283 | |
9284 | /* If the -k option is in effect, we need to go through the remaining | |
bb70624e JA |
9285 | words, separate out the assignment words, and place them on |
9286 | SUBST_ASSIGN_VARLIST. */ | |
726f6388 JA |
9287 | if (place_keywords_in_env) |
9288 | { | |
9289 | WORD_LIST *tp; /* tp == running pointer into tlist */ | |
9290 | ||
9291 | tp = tlist; | |
9292 | lp = tlist->next; | |
9293 | ||
9294 | /* Loop Invariant: tp->next == lp */ | |
9295 | /* Loop postcondition: tlist == word list without assignment statements */ | |
9296 | while (lp) | |
9297 | { | |
ccc6cda3 | 9298 | if (lp->word->flags & W_ASSIGNMENT) |
726f6388 JA |
9299 | { |
9300 | /* Found an assignment statement, add this word to end of | |
bb70624e JA |
9301 | subst_assign_varlist (vp). */ |
9302 | if (!subst_assign_varlist) | |
9303 | subst_assign_varlist = vp = lp; | |
726f6388 JA |
9304 | else |
9305 | { | |
9306 | vp->next = lp; | |
9307 | vp = lp; | |
9308 | } | |
9309 | ||
9310 | /* Remove the word pointed to by LP from TLIST. */ | |
9311 | tp->next = lp->next; | |
9312 | /* ASSERT(vp == lp); */ | |
9313 | lp->next = (WORD_LIST *)NULL; | |
9314 | lp = tp->next; | |
9315 | } | |
9316 | else | |
9317 | { | |
9318 | tp = lp; | |
9319 | lp = lp->next; | |
9320 | } | |
9321 | } | |
9322 | } | |
9323 | return (tlist); | |
9324 | } | |
9325 | ||
cce855bc JA |
9326 | #define WEXP_VARASSIGN 0x001 |
9327 | #define WEXP_BRACEEXP 0x002 | |
9328 | #define WEXP_TILDEEXP 0x004 | |
9329 | #define WEXP_PARAMEXP 0x008 | |
9330 | #define WEXP_PATHEXP 0x010 | |
9331 | ||
9332 | /* All of the expansions, including variable assignments at the start of | |
9333 | the list. */ | |
9334 | #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP) | |
9335 | ||
9336 | /* All of the expansions except variable assignments at the start of | |
9337 | the list. */ | |
9338 | #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP) | |
9339 | ||
9340 | /* All of the `shell expansions': brace expansion, tilde expansion, parameter | |
9341 | expansion, command substitution, arithmetic expansion, word splitting, and | |
9342 | quote removal. */ | |
9343 | #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP) | |
9344 | ||
726f6388 JA |
9345 | /* Take the list of words in LIST and do the various substitutions. Return |
9346 | a new list of words which is the expanded list, and without things like | |
9347 | variable assignments. */ | |
9348 | ||
9349 | WORD_LIST * | |
9350 | expand_words (list) | |
9351 | WORD_LIST *list; | |
9352 | { | |
cce855bc | 9353 | return (expand_word_list_internal (list, WEXP_ALL)); |
726f6388 JA |
9354 | } |
9355 | ||
9356 | /* Same as expand_words (), but doesn't hack variable or environment | |
9357 | variables. */ | |
9358 | WORD_LIST * | |
9359 | expand_words_no_vars (list) | |
9360 | WORD_LIST *list; | |
9361 | { | |
cce855bc | 9362 | return (expand_word_list_internal (list, WEXP_NOVARS)); |
726f6388 JA |
9363 | } |
9364 | ||
cce855bc JA |
9365 | WORD_LIST * |
9366 | expand_words_shellexp (list) | |
726f6388 | 9367 | WORD_LIST *list; |
726f6388 | 9368 | { |
cce855bc JA |
9369 | return (expand_word_list_internal (list, WEXP_SHELLEXP)); |
9370 | } | |
726f6388 | 9371 | |
cce855bc JA |
9372 | static WORD_LIST * |
9373 | glob_expand_word_list (tlist, eflags) | |
9374 | WORD_LIST *tlist; | |
9375 | int eflags; | |
9376 | { | |
9377 | char **glob_array, *temp_string; | |
9378 | register int glob_index; | |
9379 | WORD_LIST *glob_list, *output_list, *disposables, *next; | |
9380 | WORD_DESC *tword; | |
726f6388 | 9381 | |
cce855bc JA |
9382 | output_list = disposables = (WORD_LIST *)NULL; |
9383 | glob_array = (char **)NULL; | |
9384 | while (tlist) | |
9385 | { | |
9386 | /* For each word, either globbing is attempted or the word is | |
9387 | added to orig_list. If globbing succeeds, the results are | |
9388 | added to orig_list and the word (tlist) is added to the list | |
9389 | of disposable words. If globbing fails and failed glob | |
9390 | expansions are left unchanged (the shell default), the | |
9391 | original word is added to orig_list. If globbing fails and | |
9392 | failed glob expansions are removed, the original word is | |
9393 | added to the list of disposable words. orig_list ends up | |
7117c2d2 | 9394 | in reverse order and requires a call to REVERSE_LIST to |
cce855bc JA |
9395 | be set right. After all words are examined, the disposable |
9396 | words are freed. */ | |
9397 | next = tlist->next; | |
726f6388 | 9398 | |
cce855bc | 9399 | /* If the word isn't an assignment and contains an unquoted |
28ef6c31 | 9400 | pattern matching character, then glob it. */ |
b72432fd | 9401 | if ((tlist->word->flags & W_NOGLOB) == 0 && |
cce855bc | 9402 | unquoted_glob_pattern_p (tlist->word->word)) |
726f6388 | 9403 | { |
cce855bc JA |
9404 | glob_array = shell_glob_filename (tlist->word->word); |
9405 | ||
9406 | /* Handle error cases. | |
9407 | I don't think we should report errors like "No such file | |
9408 | or directory". However, I would like to report errors | |
9409 | like "Read failed". */ | |
9410 | ||
d3a24ed2 | 9411 | if (glob_array == 0 || GLOB_FAILED (glob_array)) |
726f6388 | 9412 | { |
bb70624e | 9413 | glob_array = (char **)xmalloc (sizeof (char *)); |
cce855bc JA |
9414 | glob_array[0] = (char *)NULL; |
9415 | } | |
9416 | ||
9417 | /* Dequote the current word in case we have to use it. */ | |
9418 | if (glob_array[0] == NULL) | |
9419 | { | |
9420 | temp_string = dequote_string (tlist->word->word); | |
9421 | free (tlist->word->word); | |
9422 | tlist->word->word = temp_string; | |
9423 | } | |
9424 | ||
9425 | /* Make the array into a word list. */ | |
9426 | glob_list = (WORD_LIST *)NULL; | |
9427 | for (glob_index = 0; glob_array[glob_index]; glob_index++) | |
9428 | { | |
9429 | tword = make_bare_word (glob_array[glob_index]); | |
cce855bc JA |
9430 | glob_list = make_word_list (tword, glob_list); |
9431 | } | |
9432 | ||
9433 | if (glob_list) | |
9434 | { | |
9435 | output_list = (WORD_LIST *)list_append (glob_list, output_list); | |
9436 | PREPEND_LIST (tlist, disposables); | |
9437 | } | |
d3a24ed2 CR |
9438 | else if (fail_glob_expansion != 0) |
9439 | { | |
7f947b68 | 9440 | last_command_exit_value = EXECUTION_FAILURE; |
5e13499c | 9441 | report_error (_("no match: %s"), tlist->word->word); |
c184f645 | 9442 | exp_jump_to_top_level (DISCARD); |
d3a24ed2 | 9443 | } |
cce855bc JA |
9444 | else if (allow_null_glob_expansion == 0) |
9445 | { | |
9446 | /* Failed glob expressions are left unchanged. */ | |
9447 | PREPEND_LIST (tlist, output_list); | |
9448 | } | |
9449 | else | |
9450 | { | |
9451 | /* Failed glob expressions are removed. */ | |
9452 | PREPEND_LIST (tlist, disposables); | |
726f6388 | 9453 | } |
726f6388 | 9454 | } |
cce855bc JA |
9455 | else |
9456 | { | |
9457 | /* Dequote the string. */ | |
9458 | temp_string = dequote_string (tlist->word->word); | |
9459 | free (tlist->word->word); | |
9460 | tlist->word->word = temp_string; | |
9461 | PREPEND_LIST (tlist, output_list); | |
9462 | } | |
9463 | ||
7117c2d2 | 9464 | strvec_dispose (glob_array); |
cce855bc JA |
9465 | glob_array = (char **)NULL; |
9466 | ||
9467 | tlist = next; | |
726f6388 JA |
9468 | } |
9469 | ||
cce855bc JA |
9470 | if (disposables) |
9471 | dispose_words (disposables); | |
9472 | ||
9473 | if (output_list) | |
9474 | output_list = REVERSE_LIST (output_list, WORD_LIST *); | |
9475 | ||
9476 | return (output_list); | |
9477 | } | |
726f6388 JA |
9478 | |
9479 | #if defined (BRACE_EXPANSION) | |
cce855bc JA |
9480 | static WORD_LIST * |
9481 | brace_expand_word_list (tlist, eflags) | |
9482 | WORD_LIST *tlist; | |
9483 | int eflags; | |
9484 | { | |
9485 | register char **expansions; | |
9486 | char *temp_string; | |
9487 | WORD_LIST *disposables, *output_list, *next; | |
9488 | WORD_DESC *w; | |
9489 | int eindex; | |
9490 | ||
9491 | for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next) | |
726f6388 | 9492 | { |
cce855bc | 9493 | next = tlist->next; |
726f6388 | 9494 | |
df0e4bfe CR |
9495 | if (tlist->word->flags & W_NOBRACE) |
9496 | { | |
3087e51c | 9497 | /*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/ |
df0e4bfe CR |
9498 | PREPEND_LIST (tlist, output_list); |
9499 | continue; | |
9500 | } | |
9501 | ||
6669f0e5 CR |
9502 | if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG)) |
9503 | { | |
9504 | /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/ | |
9505 | PREPEND_LIST (tlist, output_list); | |
9506 | continue; | |
9507 | } | |
df0e4bfe | 9508 | |
cce855bc JA |
9509 | /* Only do brace expansion if the word has a brace character. If |
9510 | not, just add the word list element to BRACES and continue. In | |
9511 | the common case, at least when running shell scripts, this will | |
d0ca3503 | 9512 | degenerate to a bunch of calls to `mbschr', and then what is |
cce855bc | 9513 | basically a reversal of TLIST into BRACES, which is corrected |
7117c2d2 | 9514 | by a call to REVERSE_LIST () on BRACES when the end of TLIST |
cce855bc | 9515 | is reached. */ |
d0ca3503 | 9516 | if (mbschr (tlist->word->word, LBRACE)) |
726f6388 | 9517 | { |
cce855bc | 9518 | expansions = brace_expand (tlist->word->word); |
726f6388 | 9519 | |
cce855bc | 9520 | for (eindex = 0; temp_string = expansions[eindex]; eindex++) |
726f6388 | 9521 | { |
51f7ea36 CR |
9522 | w = alloc_word_desc (); |
9523 | w->word = temp_string; | |
9524 | ||
cce855bc JA |
9525 | /* If brace expansion didn't change the word, preserve |
9526 | the flags. We may want to preserve the flags | |
9527 | unconditionally someday -- XXX */ | |
9528 | if (STREQ (temp_string, tlist->word->word)) | |
9529 | w->flags = tlist->word->flags; | |
51f7ea36 CR |
9530 | else |
9531 | w = make_word_flags (w, temp_string); | |
9532 | ||
cce855bc | 9533 | output_list = make_word_list (w, output_list); |
726f6388 | 9534 | } |
cce855bc | 9535 | free (expansions); |
726f6388 | 9536 | |
cce855bc JA |
9537 | /* Add TLIST to the list of words to be freed after brace |
9538 | expansion has been performed. */ | |
9539 | PREPEND_LIST (tlist, disposables); | |
9540 | } | |
9541 | else | |
9542 | PREPEND_LIST (tlist, output_list); | |
726f6388 | 9543 | } |
cce855bc JA |
9544 | |
9545 | if (disposables) | |
9546 | dispose_words (disposables); | |
9547 | ||
9548 | if (output_list) | |
9549 | output_list = REVERSE_LIST (output_list, WORD_LIST *); | |
9550 | ||
9551 | return (output_list); | |
9552 | } | |
9553 | #endif | |
9554 | ||
fdf670ea CR |
9555 | #if defined (ARRAY_VARS) |
9556 | /* Take WORD, a compound associative array assignment, and internally run | |
9557 | 'declare -A w', where W is the variable name portion of WORD. */ | |
9558 | static int | |
9559 | make_internal_declare (word, option) | |
9560 | char *word; | |
9561 | char *option; | |
9562 | { | |
9563 | int t; | |
9564 | WORD_LIST *wl; | |
9565 | WORD_DESC *w; | |
9566 | ||
9567 | w = make_word (word); | |
9568 | ||
9569 | t = assignment (w->word, 0); | |
9570 | w->word[t] = '\0'; | |
9571 | ||
9572 | wl = make_word_list (w, (WORD_LIST *)NULL); | |
9573 | wl = make_word_list (make_word (option), wl); | |
9574 | ||
9575 | return (declare_builtin (wl)); | |
9576 | } | |
9577 | #endif | |
9578 | ||
cce855bc JA |
9579 | static WORD_LIST * |
9580 | shell_expand_word_list (tlist, eflags) | |
9581 | WORD_LIST *tlist; | |
9582 | int eflags; | |
9583 | { | |
9584 | WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list; | |
9585 | int expanded_something, has_dollar_at; | |
9586 | char *temp_string; | |
726f6388 | 9587 | |
726f6388 | 9588 | /* We do tilde expansion all the time. This is what 1003.2 says. */ |
cce855bc JA |
9589 | new_list = (WORD_LIST *)NULL; |
9590 | for (orig_list = tlist; tlist; tlist = next) | |
726f6388 | 9591 | { |
ccc6cda3 | 9592 | temp_string = tlist->word->word; |
726f6388 JA |
9593 | |
9594 | next = tlist->next; | |
9595 | ||
43df7bbb CR |
9596 | #if defined (ARRAY_VARS) |
9597 | /* If this is a compound array assignment to a builtin that accepts | |
9598 | such assignments (e.g., `declare'), take the assignment and perform | |
9599 | it separately, handling the semantics of declarations inside shell | |
9600 | functions. This avoids the double-evaluation of such arguments, | |
9601 | because `declare' does some evaluation of compound assignments on | |
9602 | its own. */ | |
9603 | if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG)) | |
9604 | { | |
9605 | int t; | |
7571d3f4 CR |
9606 | char opts[8], opti; |
9607 | ||
9608 | opti = 0; | |
9609 | if (tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL|W_ASSIGNARRAY)) | |
9610 | opts[opti++] = '-'; | |
43df7bbb | 9611 | |
3087e51c | 9612 | if ((tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL)) == (W_ASSIGNASSOC|W_ASSNGLOBAL)) |
7571d3f4 CR |
9613 | { |
9614 | opts[opti++] = 'g'; | |
9615 | opts[opti++] = 'A'; | |
9616 | } | |
3087e51c | 9617 | else if (tlist->word->flags & W_ASSIGNASSOC) |
7571d3f4 | 9618 | opts[opti++] = 'A'; |
1a81420a | 9619 | else if ((tlist->word->flags & (W_ASSIGNARRAY|W_ASSNGLOBAL)) == (W_ASSIGNARRAY|W_ASSNGLOBAL)) |
7571d3f4 CR |
9620 | { |
9621 | opts[opti++] = 'g'; | |
9622 | opts[opti++] = 'a'; | |
9623 | } | |
36eb585c | 9624 | else if (tlist->word->flags & W_ASSIGNARRAY) |
7571d3f4 | 9625 | opts[opti++] = 'a'; |
3087e51c | 9626 | else if (tlist->word->flags & W_ASSNGLOBAL) |
7571d3f4 CR |
9627 | opts[opti++] = 'g'; |
9628 | ||
9629 | #if 0 | |
9630 | /* If we have special handling note the integer attribute */ | |
9631 | if (opti > 0 && (tlist->word->flags & W_ASSIGNINT)) | |
9632 | opts[opti++] = 'i'; | |
9633 | #endif | |
9634 | ||
9635 | opts[opti] = '\0'; | |
9636 | if (opti > 0) | |
9637 | make_internal_declare (tlist->word->word, opts); | |
fdf670ea | 9638 | |
adc6cff5 | 9639 | t = do_word_assignment (tlist->word, 0); |
43df7bbb CR |
9640 | if (t == 0) |
9641 | { | |
9642 | last_command_exit_value = EXECUTION_FAILURE; | |
9643 | exp_jump_to_top_level (DISCARD); | |
9644 | } | |
9645 | ||
9646 | /* Now transform the word as ksh93 appears to do and go on */ | |
9647 | t = assignment (tlist->word->word, 0); | |
9648 | tlist->word->word[t] = '\0'; | |
36eb585c | 9649 | tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC|W_ASSIGNARRAY); |
43df7bbb CR |
9650 | } |
9651 | #endif | |
9652 | ||
ccc6cda3 | 9653 | expanded_something = 0; |
726f6388 | 9654 | expanded = expand_word_internal |
b72432fd | 9655 | (tlist->word, 0, 0, &has_dollar_at, &expanded_something); |
726f6388 JA |
9656 | |
9657 | if (expanded == &expand_word_error || expanded == &expand_word_fatal) | |
9658 | { | |
9659 | /* By convention, each time this error is returned, | |
9660 | tlist->word->word has already been freed. */ | |
9661 | tlist->word->word = (char *)NULL; | |
ccc6cda3 | 9662 | |
726f6388 JA |
9663 | /* Dispose our copy of the original list. */ |
9664 | dispose_words (orig_list); | |
d166f048 | 9665 | /* Dispose the new list we're building. */ |
726f6388 JA |
9666 | dispose_words (new_list); |
9667 | ||
28ef6c31 | 9668 | last_command_exit_value = EXECUTION_FAILURE; |
726f6388 | 9669 | if (expanded == &expand_word_error) |
d3a24ed2 | 9670 | exp_jump_to_top_level (DISCARD); |
726f6388 | 9671 | else |
d3a24ed2 | 9672 | exp_jump_to_top_level (FORCE_EOF); |
726f6388 JA |
9673 | } |
9674 | ||
ccc6cda3 JA |
9675 | /* Don't split words marked W_NOSPLIT. */ |
9676 | if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0) | |
726f6388 | 9677 | { |
ccc6cda3 | 9678 | temp_list = word_list_split (expanded); |
726f6388 JA |
9679 | dispose_words (expanded); |
9680 | } | |
9681 | else | |
9682 | { | |
9683 | /* If no parameter expansion, command substitution, process | |
9684 | substitution, or arithmetic substitution took place, then | |
9685 | do not do word splitting. We still have to remove quoted | |
9686 | null characters from the result. */ | |
9687 | word_list_remove_quoted_nulls (expanded); | |
ccc6cda3 | 9688 | temp_list = expanded; |
726f6388 JA |
9689 | } |
9690 | ||
ccc6cda3 JA |
9691 | expanded = REVERSE_LIST (temp_list, WORD_LIST *); |
9692 | new_list = (WORD_LIST *)list_append (expanded, new_list); | |
726f6388 JA |
9693 | } |
9694 | ||
cce855bc JA |
9695 | if (orig_list) |
9696 | dispose_words (orig_list); | |
726f6388 | 9697 | |
726f6388 | 9698 | if (new_list) |
cce855bc | 9699 | new_list = REVERSE_LIST (new_list, WORD_LIST *); |
726f6388 | 9700 | |
cce855bc JA |
9701 | return (new_list); |
9702 | } | |
726f6388 | 9703 | |
cce855bc JA |
9704 | /* The workhorse for expand_words () and expand_words_no_vars (). |
9705 | First arg is LIST, a WORD_LIST of words. | |
b72432fd JA |
9706 | Second arg EFLAGS is a flags word controlling which expansions are |
9707 | performed. | |
726f6388 | 9708 | |
cce855bc JA |
9709 | This does all of the substitutions: brace expansion, tilde expansion, |
9710 | parameter expansion, command substitution, arithmetic expansion, | |
9711 | process substitution, word splitting, and pathname expansion, according | |
9712 | to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits | |
9713 | set, or for which no expansion is done, do not undergo word splitting. | |
df0e4bfe CR |
9714 | Words with the W_NOGLOB bit set do not undergo pathname expansion; words |
9715 | with W_NOBRACE set do not undergo brace expansion (see | |
9716 | brace_expand_word_list above). */ | |
cce855bc JA |
9717 | static WORD_LIST * |
9718 | expand_word_list_internal (list, eflags) | |
9719 | WORD_LIST *list; | |
9720 | int eflags; | |
9721 | { | |
9722 | WORD_LIST *new_list, *temp_list; | |
9723 | int tint; | |
726f6388 | 9724 | |
f0c4de40 | 9725 | tempenv_assign_error = 0; |
cce855bc JA |
9726 | if (list == 0) |
9727 | return ((WORD_LIST *)NULL); | |
726f6388 | 9728 | |
bb70624e | 9729 | garglist = new_list = copy_word_list (list); |
cce855bc JA |
9730 | if (eflags & WEXP_VARASSIGN) |
9731 | { | |
bb70624e | 9732 | garglist = new_list = separate_out_assignments (new_list); |
cce855bc JA |
9733 | if (new_list == 0) |
9734 | { | |
bb70624e | 9735 | if (subst_assign_varlist) |
cce855bc JA |
9736 | { |
9737 | /* All the words were variable assignments, so they are placed | |
9738 | into the shell's environment. */ | |
bb70624e | 9739 | for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next) |
cce855bc JA |
9740 | { |
9741 | this_command_name = (char *)NULL; /* no arithmetic errors */ | |
adc6cff5 | 9742 | tint = do_word_assignment (temp_list->word, 0); |
cce855bc JA |
9743 | /* Variable assignment errors in non-interactive shells |
9744 | running in Posix.2 mode cause the shell to exit. */ | |
28ef6c31 | 9745 | if (tint == 0) |
ccc6cda3 | 9746 | { |
cce855bc | 9747 | last_command_exit_value = EXECUTION_FAILURE; |
28ef6c31 | 9748 | if (interactive_shell == 0 && posixly_correct) |
d3a24ed2 | 9749 | exp_jump_to_top_level (FORCE_EOF); |
28ef6c31 | 9750 | else |
d3a24ed2 | 9751 | exp_jump_to_top_level (DISCARD); |
ccc6cda3 | 9752 | } |
726f6388 | 9753 | } |
bb70624e JA |
9754 | dispose_words (subst_assign_varlist); |
9755 | subst_assign_varlist = (WORD_LIST *)NULL; | |
cce855bc JA |
9756 | } |
9757 | return ((WORD_LIST *)NULL); | |
9758 | } | |
9759 | } | |
726f6388 | 9760 | |
cce855bc JA |
9761 | /* Begin expanding the words that remain. The expansions take place on |
9762 | things that aren't really variable assignments. */ | |
726f6388 | 9763 | |
cce855bc JA |
9764 | #if defined (BRACE_EXPANSION) |
9765 | /* Do brace expansion on this word if there are any brace characters | |
9766 | in the string. */ | |
9767 | if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list) | |
9768 | new_list = brace_expand_word_list (new_list, eflags); | |
9769 | #endif /* BRACE_EXPANSION */ | |
726f6388 | 9770 | |
cce855bc JA |
9771 | /* Perform the `normal' shell expansions: tilde expansion, parameter and |
9772 | variable substitution, command substitution, arithmetic expansion, | |
9773 | and word splitting. */ | |
9774 | new_list = shell_expand_word_list (new_list, eflags); | |
726f6388 | 9775 | |
cce855bc JA |
9776 | /* Okay, we're almost done. Now let's just do some filename |
9777 | globbing. */ | |
9778 | if (new_list) | |
9779 | { | |
9780 | if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0) | |
9781 | /* Glob expand the word list unless globbing has been disabled. */ | |
9782 | new_list = glob_expand_word_list (new_list, eflags); | |
726f6388 | 9783 | else |
cce855bc JA |
9784 | /* Dequote the words, because we're not performing globbing. */ |
9785 | new_list = dequote_list (new_list); | |
726f6388 JA |
9786 | } |
9787 | ||
bb70624e | 9788 | if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist) |
726f6388 | 9789 | { |
43df7bbb | 9790 | sh_wassign_func_t *assign_func; |
adc6cff5 | 9791 | int is_special_builtin, is_builtin_or_func; |
726f6388 JA |
9792 | |
9793 | /* If the remainder of the words expand to nothing, Posix.2 requires | |
9794 | that the variable and environment assignments affect the shell's | |
9795 | environment. */ | |
43df7bbb | 9796 | assign_func = new_list ? assign_in_env : do_word_assignment; |
56299fa5 | 9797 | tempenv_assign_error = 0; |
726f6388 | 9798 | |
adc6cff5 | 9799 | is_builtin_or_func = (new_list && new_list->word && (find_shell_builtin (new_list->word->word) || find_function (new_list->word->word))); |
e05be32d CR |
9800 | /* Posix says that special builtins exit if a variable assignment error |
9801 | occurs in an assignment preceding it. */ | |
adc6cff5 | 9802 | is_special_builtin = (posixly_correct && new_list && new_list->word && find_special_builtin (new_list->word->word)); |
e05be32d | 9803 | |
bb70624e | 9804 | for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next) |
726f6388 | 9805 | { |
ccc6cda3 | 9806 | this_command_name = (char *)NULL; |
d7f49990 | 9807 | assigning_in_environment = (assign_func == assign_in_env); |
adc6cff5 | 9808 | tint = (*assign_func) (temp_list->word, is_builtin_or_func); |
d7f49990 | 9809 | assigning_in_environment = 0; |
ccc6cda3 JA |
9810 | /* Variable assignment errors in non-interactive shells running |
9811 | in Posix.2 mode cause the shell to exit. */ | |
56299fa5 | 9812 | if (tint == 0) |
ccc6cda3 | 9813 | { |
43df7bbb | 9814 | if (assign_func == do_word_assignment) |
56299fa5 CR |
9815 | { |
9816 | last_command_exit_value = EXECUTION_FAILURE; | |
e05be32d | 9817 | if (interactive_shell == 0 && posixly_correct && is_special_builtin) |
56299fa5 CR |
9818 | exp_jump_to_top_level (FORCE_EOF); |
9819 | else | |
9820 | exp_jump_to_top_level (DISCARD); | |
9821 | } | |
28ef6c31 | 9822 | else |
56299fa5 | 9823 | tempenv_assign_error++; |
ccc6cda3 | 9824 | } |
726f6388 | 9825 | } |
726f6388 | 9826 | |
bb70624e JA |
9827 | dispose_words (subst_assign_varlist); |
9828 | subst_assign_varlist = (WORD_LIST *)NULL; | |
726f6388 JA |
9829 | } |
9830 | ||
cce855bc | 9831 | return (new_list); |
ccc6cda3 | 9832 | } |