]>
Commit | Line | Data |
---|---|---|
95732b49 JA |
1 | /* subst.c -- The part of the shell that does parameter, command, arithmetic, |
2 | and globbing substitutions. */ | |
726f6388 | 3 | |
bb70624e JA |
4 | /* ``Have a little faith, there's magic in the night. You ain't a |
5 | beauty, but, hey, you're alright.'' */ | |
6 | ||
712f80b0 | 7 | /* Copyright (C) 1987-2020 Free Software Foundation, Inc. |
726f6388 JA |
8 | |
9 | This file is part of GNU Bash, the Bourne Again SHell. | |
10 | ||
3185942a JA |
11 | Bash is free software: you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License as published by | |
13 | the Free Software Foundation, either version 3 of the License, or | |
14 | (at your option) any later version. | |
726f6388 | 15 | |
3185942a JA |
16 | Bash is distributed in the hope that it will be useful, |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
726f6388 | 20 | |
3185942a JA |
21 | You should have received a copy of the GNU General Public License |
22 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | |
23 | */ | |
726f6388 | 24 | |
ccc6cda3 JA |
25 | #include "config.h" |
26 | ||
726f6388 JA |
27 | #include "bashtypes.h" |
28 | #include <stdio.h> | |
f73dda09 | 29 | #include "chartypes.h" |
3185942a JA |
30 | #if defined (HAVE_PWD_H) |
31 | # include <pwd.h> | |
32 | #endif | |
726f6388 JA |
33 | #include <signal.h> |
34 | #include <errno.h> | |
ccc6cda3 JA |
35 | |
36 | #if defined (HAVE_UNISTD_H) | |
37 | # include <unistd.h> | |
38 | #endif | |
726f6388 | 39 | |
a0c0a00f CR |
40 | #define NEED_FPURGE_DECL |
41 | ||
726f6388 JA |
42 | #include "bashansi.h" |
43 | #include "posixstat.h" | |
b80f6443 | 44 | #include "bashintl.h" |
726f6388 JA |
45 | |
46 | #include "shell.h" | |
495aee44 | 47 | #include "parser.h" |
726f6388 JA |
48 | #include "flags.h" |
49 | #include "jobs.h" | |
50 | #include "execute_cmd.h" | |
51 | #include "filecntl.h" | |
ccc6cda3 JA |
52 | #include "trap.h" |
53 | #include "pathexp.h" | |
54 | #include "mailcheck.h" | |
55 | ||
7117c2d2 | 56 | #include "shmbutil.h" |
a0c0a00f CR |
57 | #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR) |
58 | # include <mbstr.h> /* mbschr */ | |
59 | #endif | |
495aee44 | 60 | #include "typemax.h" |
7117c2d2 | 61 | |
ccc6cda3 JA |
62 | #include "builtins/getopt.h" |
63 | #include "builtins/common.h" | |
726f6388 | 64 | |
3185942a JA |
65 | #include "builtins/builtext.h" |
66 | ||
cce855bc | 67 | #include <tilde/tilde.h> |
f73dda09 | 68 | #include <glob/strmatch.h> |
ccc6cda3 JA |
69 | |
70 | #if !defined (errno) | |
71 | extern int errno; | |
72 | #endif /* !errno */ | |
726f6388 JA |
73 | |
74 | /* The size that strings change by. */ | |
d166f048 | 75 | #define DEFAULT_INITIAL_ARRAY_SIZE 112 |
ccc6cda3 JA |
76 | #define DEFAULT_ARRAY_SIZE 128 |
77 | ||
78 | /* Variable types. */ | |
79 | #define VT_VARIABLE 0 | |
80 | #define VT_POSPARMS 1 | |
81 | #define VT_ARRAYVAR 2 | |
d166f048 | 82 | #define VT_ARRAYMEMBER 3 |
3185942a | 83 | #define VT_ASSOCVAR 4 |
726f6388 | 84 | |
b80f6443 JA |
85 | #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */ |
86 | ||
ccc6cda3 JA |
87 | /* Flags for quoted_strchr */ |
88 | #define ST_BACKSL 0x01 | |
89 | #define ST_CTLESC 0x02 | |
7117c2d2 JA |
90 | #define ST_SQUOTE 0x04 /* unused yet */ |
91 | #define ST_DQUOTE 0x08 /* unused yet */ | |
92 | ||
cce855bc JA |
93 | /* These defs make it easier to use the editor. */ |
94 | #define LBRACE '{' | |
95 | #define RBRACE '}' | |
96 | #define LPAREN '(' | |
97 | #define RPAREN ')' | |
ac50fbac CR |
98 | #define LBRACK '[' |
99 | #define RBRACK ']' | |
726f6388 | 100 | |
0001803f CR |
101 | #if defined (HANDLE_MULTIBYTE) |
102 | #define WLPAREN L'(' | |
103 | #define WRPAREN L')' | |
104 | #endif | |
105 | ||
a0c0a00f CR |
106 | #define DOLLAR_AT_STAR(c) ((c) == '@' || (c) == '*') |
107 | #define STR_DOLLAR_AT_STAR(s) (DOLLAR_AT_STAR ((s)[0]) && (s)[1] == '\0') | |
108 | ||
28ef6c31 JA |
109 | /* Evaluates to 1 if C is one of the shell's special parameters whose length |
110 | can be taken, but is also one of the special expansion characters. */ | |
111 | #define VALID_SPECIAL_LENGTH_PARAM(c) \ | |
a0c0a00f | 112 | ((c) == '-' || (c) == '?' || (c) == '#' || (c) == '@') |
28ef6c31 JA |
113 | |
114 | /* Evaluates to 1 if C is one of the shell's special parameters for which an | |
115 | indirect variable reference may be made. */ | |
116 | #define VALID_INDIR_PARAM(c) \ | |
495aee44 | 117 | ((posixly_correct == 0 && (c) == '#') || (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*') |
28ef6c31 JA |
118 | |
119 | /* Evaluates to 1 if C is one of the OP characters that follows the parameter | |
120 | in ${parameter[:]OPword}. */ | |
7117c2d2 | 121 | #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP) |
28ef6c31 | 122 | |
bb70624e JA |
123 | /* Evaluates to 1 if this is one of the shell's special variables. */ |
124 | #define SPECIAL_VAR(name, wi) \ | |
a0c0a00f | 125 | (*name && ((DIGIT (*name) && all_digits (name)) || \ |
f73dda09 | 126 | (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \ |
a0c0a00f CR |
127 | (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))) |
128 | ||
129 | /* This can be used by all of the *_extract_* functions that have a similar | |
130 | structure. It can't just be wrapped in a do...while(0) loop because of | |
131 | the embedded `break'. The dangling else accommodates a trailing semicolon; | |
132 | we could also put in a do ; while (0) */ | |
133 | ||
a0c0a00f CR |
134 | #define CHECK_STRING_OVERRUN(oind, ind, len, ch) \ |
135 | if (ind >= len) \ | |
136 | { \ | |
137 | oind = len; \ | |
138 | ch = 0; \ | |
139 | break; \ | |
140 | } \ | |
141 | else \ | |
bb70624e | 142 | |
f73dda09 JA |
143 | /* An expansion function that takes a string and a quoted flag and returns |
144 | a WORD_LIST *. Used as the type of the third argument to | |
145 | expand_string_if_necessary(). */ | |
712f80b0 | 146 | typedef WORD_LIST *EXPFUNC PARAMS((char *, int)); |
f73dda09 | 147 | |
726f6388 JA |
148 | /* Process ID of the last command executed within command substitution. */ |
149 | pid_t last_command_subst_pid = NO_PID; | |
b72432fd | 150 | pid_t current_command_subst_pid = NO_PID; |
726f6388 | 151 | |
7117c2d2 JA |
152 | /* Variables used to keep track of the characters in IFS. */ |
153 | SHELL_VAR *ifs_var; | |
154 | char *ifs_value; | |
155 | unsigned char ifs_cmap[UCHAR_MAX + 1]; | |
ac50fbac | 156 | int ifs_is_set, ifs_is_null; |
95732b49 JA |
157 | |
158 | #if defined (HANDLE_MULTIBYTE) | |
159 | unsigned char ifs_firstc[MB_LEN_MAX]; | |
160 | size_t ifs_firstc_len; | |
161 | #else | |
7117c2d2 | 162 | unsigned char ifs_firstc; |
95732b49 | 163 | #endif |
7117c2d2 | 164 | |
a0c0a00f CR |
165 | /* If non-zero, command substitution inherits the value of errexit option */ |
166 | int inherit_errexit = 0; | |
167 | ||
0001803f CR |
168 | /* Sentinel to tell when we are performing variable assignments preceding a |
169 | command name and putting them into the environment. Used to make sure | |
170 | we use the temporary environment when looking up variable values. */ | |
3185942a JA |
171 | int assigning_in_environment; |
172 | ||
0001803f CR |
173 | /* Used to hold a list of variable assignments preceding a command. Global |
174 | so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a | |
175 | SIGCHLD trap and so it can be saved and restored by the trap handlers. */ | |
176 | WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL; | |
177 | ||
a0c0a00f CR |
178 | /* Tell the expansion functions to not longjmp back to top_level on fatal |
179 | errors. Enabled when doing completion and prompt string expansion. */ | |
180 | int no_longjmp_on_fatal_error = 0; | |
181 | ||
d233b485 CR |
182 | /* Non-zero means to allow unmatched globbed filenames to expand to |
183 | a null file. */ | |
184 | int allow_null_glob_expansion; | |
185 | ||
186 | /* Non-zero means to throw an error when globbing fails to match anything. */ | |
187 | int fail_glob_expansion; | |
188 | ||
726f6388 | 189 | /* Extern functions and variables from different files. */ |
ccc6cda3 | 190 | extern struct fd_bitmap *current_fds_to_close; |
cce855bc | 191 | extern int wordexp_only; |
726f6388 | 192 | |
a0c0a00f CR |
193 | #if defined (JOB_CONTROL) && defined (PROCESS_SUBSTITUTION) |
194 | extern PROCESS *last_procsub_child; | |
195 | #endif | |
196 | ||
0628567a | 197 | #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE) |
712f80b0 | 198 | extern wchar_t *wcsdup PARAMS((const wchar_t *)); |
0628567a JA |
199 | #endif |
200 | ||
f73dda09 | 201 | #if 0 |
ccc6cda3 JA |
202 | /* Variables to keep track of which words in an expanded word list (the |
203 | output of expand_word_list_internal) are the result of globbing | |
f73dda09 JA |
204 | expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c. |
205 | (CURRENTLY UNUSED). */ | |
ccc6cda3 JA |
206 | char *glob_argv_flags; |
207 | static int glob_argv_flags_size; | |
f73dda09 | 208 | #endif |
726f6388 | 209 | |
a0c0a00f CR |
210 | static WORD_LIST *cached_quoted_dollar_at = 0; |
211 | ||
d233b485 | 212 | /* Distinguished error values to return from expansion functions */ |
726f6388 | 213 | static WORD_LIST expand_word_error, expand_word_fatal; |
95732b49 | 214 | static WORD_DESC expand_wdesc_error, expand_wdesc_fatal; |
d233b485 | 215 | static char expand_param_error, expand_param_fatal, expand_param_unset; |
95732b49 | 216 | static char extract_string_error, extract_string_fatal; |
726f6388 | 217 | |
d233b485 CR |
218 | /* Set by expand_word_unsplit and several of the expand_string_XXX functions; |
219 | used to inhibit splitting and re-joining $* on $IFS, primarily when doing | |
220 | assignment statements. The idea is that if we're in a context where this | |
221 | is set, we're not going to be performing word splitting, so we use the same | |
222 | rules to expand $* as we would if it appeared within double quotes. */ | |
28ef6c31 | 223 | static int expand_no_split_dollar_star = 0; |
bb70624e | 224 | |
bb70624e JA |
225 | /* A WORD_LIST of words to be expanded by expand_word_list_internal, |
226 | without any leading variable assignments. */ | |
227 | static WORD_LIST *garglist = (WORD_LIST *)NULL; | |
b72432fd | 228 | |
712f80b0 CR |
229 | static char *quoted_substring PARAMS((char *, int, int)); |
230 | static int quoted_strlen PARAMS((char *)); | |
231 | static char *quoted_strchr PARAMS((char *, int, int)); | |
f73dda09 | 232 | |
712f80b0 CR |
233 | static char *expand_string_if_necessary PARAMS((char *, int, EXPFUNC *)); |
234 | static inline char *expand_string_to_string_internal PARAMS((char *, int, EXPFUNC *)); | |
235 | static WORD_LIST *call_expand_word_internal PARAMS((WORD_DESC *, int, int, int *, int *)); | |
236 | static WORD_LIST *expand_string_internal PARAMS((char *, int)); | |
237 | static WORD_LIST *expand_string_leave_quoted PARAMS((char *, int)); | |
238 | static WORD_LIST *expand_string_for_rhs PARAMS((char *, int, int, int, int *, int *)); | |
239 | static WORD_LIST *expand_string_for_pat PARAMS((char *, int, int *, int *)); | |
d233b485 | 240 | |
712f80b0 | 241 | static char *quote_escapes_internal PARAMS((const char *, int)); |
f73dda09 | 242 | |
712f80b0 CR |
243 | static WORD_LIST *list_quote_escapes PARAMS((WORD_LIST *)); |
244 | static WORD_LIST *list_dequote_escapes PARAMS((WORD_LIST *)); | |
ac50fbac | 245 | |
712f80b0 CR |
246 | static char *make_quoted_char PARAMS((int)); |
247 | static WORD_LIST *quote_list PARAMS((WORD_LIST *)); | |
f73dda09 | 248 | |
712f80b0 CR |
249 | static int unquoted_substring PARAMS((char *, char *)); |
250 | static int unquoted_member PARAMS((int, char *)); | |
f73dda09 | 251 | |
95732b49 | 252 | #if defined (ARRAY_VARS) |
712f80b0 | 253 | static SHELL_VAR *do_compound_assignment PARAMS((char *, char *, int)); |
95732b49 | 254 | #endif |
712f80b0 | 255 | static int do_assignment_internal PARAMS((const WORD_DESC *, int)); |
f73dda09 | 256 | |
712f80b0 CR |
257 | static char *string_extract_verbatim PARAMS((char *, size_t, int *, char *, int)); |
258 | static char *string_extract PARAMS((char *, int *, char *, int)); | |
259 | static char *string_extract_double_quoted PARAMS((char *, int *, int)); | |
260 | static inline char *string_extract_single_quoted PARAMS((char *, int *)); | |
261 | static inline int skip_single_quoted PARAMS((const char *, size_t, int, int)); | |
262 | static int skip_double_quoted PARAMS((char *, size_t, int, int)); | |
263 | static char *extract_delimited_string PARAMS((char *, int *, char *, char *, char *, int)); | |
264 | static char *extract_dollar_brace_string PARAMS((char *, int *, int, int)); | |
265 | static int skip_matched_pair PARAMS((const char *, int, int, int, int)); | |
f73dda09 | 266 | |
712f80b0 | 267 | static char *pos_params PARAMS((char *, int, int, int, int)); |
f73dda09 | 268 | |
712f80b0 | 269 | static unsigned char *mb_getcharlens PARAMS((char *, int)); |
b80f6443 | 270 | |
712f80b0 | 271 | static char *remove_upattern PARAMS((char *, char *, int)); |
0628567a | 272 | #if defined (HANDLE_MULTIBYTE) |
712f80b0 | 273 | static wchar_t *remove_wpattern PARAMS((wchar_t *, size_t, wchar_t *, int)); |
b80f6443 | 274 | #endif |
712f80b0 | 275 | static char *remove_pattern PARAMS((char *, char *, int)); |
b80f6443 | 276 | |
712f80b0 | 277 | static int match_upattern PARAMS((char *, char *, int, char **, char **)); |
b80f6443 | 278 | #if defined (HANDLE_MULTIBYTE) |
712f80b0 | 279 | static int match_wpattern PARAMS((wchar_t *, char **, size_t, wchar_t *, int, char **, char **)); |
b80f6443 | 280 | #endif |
712f80b0 CR |
281 | static int match_pattern PARAMS((char *, char *, int, char **, char **)); |
282 | static int getpatspec PARAMS((int, char *)); | |
283 | static char *getpattern PARAMS((char *, int, int)); | |
284 | static char *variable_remove_pattern PARAMS((char *, char *, int, int)); | |
285 | static char *list_remove_pattern PARAMS((WORD_LIST *, char *, int, int, int)); | |
286 | static char *parameter_list_remove_pattern PARAMS((int, char *, int, int)); | |
f73dda09 | 287 | #ifdef ARRAY_VARS |
712f80b0 | 288 | static char *array_remove_pattern PARAMS((SHELL_VAR *, char *, int, int, int)); |
f73dda09 | 289 | #endif |
712f80b0 | 290 | static char *parameter_brace_remove_pattern PARAMS((char *, char *, int, char *, int, int, int)); |
f73dda09 | 291 | |
712f80b0 | 292 | static char *string_var_assignment PARAMS((SHELL_VAR *, char *)); |
a0c0a00f | 293 | #if defined (ARRAY_VARS) |
712f80b0 | 294 | static char *array_var_assignment PARAMS((SHELL_VAR *, int, int, int)); |
a0c0a00f | 295 | #endif |
712f80b0 CR |
296 | static char *pos_params_assignment PARAMS((WORD_LIST *, int, int)); |
297 | static char *string_transform PARAMS((int, SHELL_VAR *, char *)); | |
298 | static char *list_transform PARAMS((int, SHELL_VAR *, WORD_LIST *, int, int)); | |
299 | static char *parameter_list_transform PARAMS((int, int, int)); | |
a0c0a00f | 300 | #if defined ARRAY_VARS |
712f80b0 | 301 | static char *array_transform PARAMS((int, SHELL_VAR *, int, int)); |
a0c0a00f | 302 | #endif |
712f80b0 | 303 | static char *parameter_brace_transform PARAMS((char *, char *, int, char *, int, int, int, int)); |
3eb0018e | 304 | static int valid_parameter_transform PARAMS((char *)); |
a0c0a00f | 305 | |
712f80b0 | 306 | static char *process_substitute PARAMS((char *, int)); |
f73dda09 | 307 | |
712f80b0 | 308 | static char *read_comsub PARAMS((int, int, int, int *)); |
f73dda09 JA |
309 | |
310 | #ifdef ARRAY_VARS | |
712f80b0 | 311 | static arrayind_t array_length_reference PARAMS((char *)); |
f73dda09 JA |
312 | #endif |
313 | ||
712f80b0 CR |
314 | static int valid_brace_expansion_word PARAMS((char *, int)); |
315 | static int chk_atstar PARAMS((char *, int, int, int *, int *)); | |
316 | static int chk_arithsub PARAMS((const char *, int)); | |
b80f6443 | 317 | |
712f80b0 CR |
318 | static WORD_DESC *parameter_brace_expand_word PARAMS((char *, int, int, int, arrayind_t *)); |
319 | static char *parameter_brace_find_indir PARAMS((char *, int, int, int)); | |
320 | static WORD_DESC *parameter_brace_expand_indir PARAMS((char *, int, int, int, int *, int *)); | |
321 | static WORD_DESC *parameter_brace_expand_rhs PARAMS((char *, char *, int, int, int, int *, int *)); | |
322 | static void parameter_brace_expand_error PARAMS((char *, char *, int)); | |
f73dda09 | 323 | |
712f80b0 CR |
324 | static int valid_length_expression PARAMS((char *)); |
325 | static intmax_t parameter_brace_expand_length PARAMS((char *)); | |
f73dda09 | 326 | |
712f80b0 CR |
327 | static char *skiparith PARAMS((char *, int)); |
328 | static int verify_substring_values PARAMS((SHELL_VAR *, char *, char *, int, intmax_t *, intmax_t *)); | |
329 | static int get_var_and_type PARAMS((char *, char *, arrayind_t, int, int, SHELL_VAR **, char **)); | |
330 | static char *mb_substring PARAMS((char *, int, int)); | |
331 | static char *parameter_brace_substring PARAMS((char *, char *, int, char *, int, int, int)); | |
495aee44 | 332 | |
712f80b0 | 333 | static int shouldexp_replacement PARAMS((char *)); |
f73dda09 | 334 | |
712f80b0 | 335 | static char *pos_params_pat_subst PARAMS((char *, char *, char *, int)); |
f73dda09 | 336 | |
712f80b0 | 337 | static char *parameter_brace_patsub PARAMS((char *, char *, int, char *, int, int, int)); |
f73dda09 | 338 | |
712f80b0 CR |
339 | static char *pos_params_casemod PARAMS((char *, char *, int, int)); |
340 | static char *parameter_brace_casemod PARAMS((char *, char *, int, int, char *, int, int, int)); | |
3185942a | 341 | |
712f80b0 CR |
342 | static WORD_DESC *parameter_brace_expand PARAMS((char *, int *, int, int, int *, int *)); |
343 | static WORD_DESC *param_expand PARAMS((char *, int *, int, int *, int *, int *, int *, int)); | |
f73dda09 | 344 | |
712f80b0 | 345 | static WORD_LIST *expand_word_internal PARAMS((WORD_DESC *, int, int, int *, int *)); |
f73dda09 | 346 | |
712f80b0 | 347 | static WORD_LIST *word_list_split PARAMS((WORD_LIST *)); |
f73dda09 | 348 | |
712f80b0 | 349 | static void exp_jump_to_top_level PARAMS((int)); |
b80f6443 | 350 | |
712f80b0 CR |
351 | static WORD_LIST *separate_out_assignments PARAMS((WORD_LIST *)); |
352 | static WORD_LIST *glob_expand_word_list PARAMS((WORD_LIST *, int)); | |
f73dda09 | 353 | #ifdef BRACE_EXPANSION |
712f80b0 | 354 | static WORD_LIST *brace_expand_word_list PARAMS((WORD_LIST *, int)); |
f73dda09 | 355 | #endif |
3185942a | 356 | #if defined (ARRAY_VARS) |
712f80b0 CR |
357 | static int make_internal_declare PARAMS((char *, char *, char *)); |
358 | static void expand_compound_assignment_word PARAMS((WORD_LIST *, int)); | |
359 | static WORD_LIST *expand_declaration_argument PARAMS((WORD_LIST *, WORD_LIST *)); | |
3185942a | 360 | #endif |
712f80b0 CR |
361 | static WORD_LIST *shell_expand_word_list PARAMS((WORD_LIST *, int)); |
362 | static WORD_LIST *expand_word_list_internal PARAMS((WORD_LIST *, int)); | |
726f6388 JA |
363 | |
364 | /* **************************************************************** */ | |
365 | /* */ | |
366 | /* Utility Functions */ | |
367 | /* */ | |
368 | /* **************************************************************** */ | |
369 | ||
0001803f CR |
370 | #if defined (DEBUG) |
371 | void | |
372 | dump_word_flags (flags) | |
373 | int flags; | |
374 | { | |
375 | int f; | |
376 | ||
377 | f = flags; | |
378 | fprintf (stderr, "%d -> ", f); | |
a0c0a00f CR |
379 | if (f & W_ARRAYIND) |
380 | { | |
381 | f &= ~W_ARRAYIND; | |
382 | fprintf (stderr, "W_ARRAYIND%s", f ? "|" : ""); | |
383 | } | |
0001803f CR |
384 | if (f & W_ASSIGNASSOC) |
385 | { | |
386 | f &= ~W_ASSIGNASSOC; | |
387 | fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : ""); | |
388 | } | |
ac50fbac CR |
389 | if (f & W_ASSIGNARRAY) |
390 | { | |
391 | f &= ~W_ASSIGNARRAY; | |
392 | fprintf (stderr, "W_ASSIGNARRAY%s", f ? "|" : ""); | |
393 | } | |
d233b485 | 394 | if (f & W_SAWQUOTEDNULL) |
0001803f | 395 | { |
d233b485 CR |
396 | f &= ~W_SAWQUOTEDNULL; |
397 | fprintf (stderr, "W_SAWQUOTEDNULL%s", f ? "|" : ""); | |
0001803f CR |
398 | } |
399 | if (f & W_NOPROCSUB) | |
400 | { | |
401 | f &= ~W_NOPROCSUB; | |
402 | fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : ""); | |
403 | } | |
404 | if (f & W_DQUOTE) | |
405 | { | |
406 | f &= ~W_DQUOTE; | |
407 | fprintf (stderr, "W_DQUOTE%s", f ? "|" : ""); | |
408 | } | |
409 | if (f & W_HASQUOTEDNULL) | |
410 | { | |
411 | f &= ~W_HASQUOTEDNULL; | |
412 | fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : ""); | |
413 | } | |
414 | if (f & W_ASSIGNARG) | |
415 | { | |
416 | f &= ~W_ASSIGNARG; | |
417 | fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : ""); | |
418 | } | |
419 | if (f & W_ASSNBLTIN) | |
420 | { | |
421 | f &= ~W_ASSNBLTIN; | |
422 | fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : ""); | |
423 | } | |
6d41b715 CR |
424 | if (f & W_ASSNGLOBAL) |
425 | { | |
426 | f &= ~W_ASSNGLOBAL; | |
427 | fprintf (stderr, "W_ASSNGLOBAL%s", f ? "|" : ""); | |
428 | } | |
0001803f CR |
429 | if (f & W_COMPASSIGN) |
430 | { | |
431 | f &= ~W_COMPASSIGN; | |
432 | fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : ""); | |
433 | } | |
712f80b0 | 434 | if (f & W_EXPANDRHS) |
0001803f | 435 | { |
712f80b0 CR |
436 | f &= ~W_EXPANDRHS; |
437 | fprintf (stderr, "W_EXPANDRHS%s", f ? "|" : ""); | |
0001803f CR |
438 | } |
439 | if (f & W_ITILDE) | |
440 | { | |
441 | f &= ~W_ITILDE; | |
442 | fprintf (stderr, "W_ITILDE%s", f ? "|" : ""); | |
443 | } | |
444 | if (f & W_NOTILDE) | |
445 | { | |
446 | f &= ~W_NOTILDE; | |
447 | fprintf (stderr, "W_NOTILDE%s", f ? "|" : ""); | |
448 | } | |
449 | if (f & W_ASSIGNRHS) | |
450 | { | |
451 | f &= ~W_ASSIGNRHS; | |
452 | fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : ""); | |
453 | } | |
d233b485 CR |
454 | if (f & W_NOASSNTILDE) |
455 | { | |
456 | f &= ~W_NOASSNTILDE; | |
457 | fprintf (stderr, "W_NOASSNTILDE%s", f ? "|" : ""); | |
458 | } | |
0001803f CR |
459 | if (f & W_NOCOMSUB) |
460 | { | |
461 | f &= ~W_NOCOMSUB; | |
462 | fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : ""); | |
463 | } | |
464 | if (f & W_DOLLARSTAR) | |
465 | { | |
466 | f &= ~W_DOLLARSTAR; | |
467 | fprintf (stderr, "W_DOLLARSTAR%s", f ? "|" : ""); | |
468 | } | |
469 | if (f & W_DOLLARAT) | |
470 | { | |
471 | f &= ~W_DOLLARAT; | |
472 | fprintf (stderr, "W_DOLLARAT%s", f ? "|" : ""); | |
473 | } | |
474 | if (f & W_TILDEEXP) | |
475 | { | |
476 | f &= ~W_TILDEEXP; | |
477 | fprintf (stderr, "W_TILDEEXP%s", f ? "|" : ""); | |
478 | } | |
479 | if (f & W_NOSPLIT2) | |
480 | { | |
481 | f &= ~W_NOSPLIT2; | |
482 | fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : ""); | |
483 | } | |
0001803f CR |
484 | if (f & W_NOSPLIT) |
485 | { | |
486 | f &= ~W_NOSPLIT; | |
487 | fprintf (stderr, "W_NOSPLIT%s", f ? "|" : ""); | |
488 | } | |
ac50fbac | 489 | if (f & W_NOBRACE) |
0001803f | 490 | { |
ac50fbac CR |
491 | f &= ~W_NOBRACE; |
492 | fprintf (stderr, "W_NOBRACE%s", f ? "|" : ""); | |
493 | } | |
494 | if (f & W_NOGLOB) | |
495 | { | |
496 | f &= ~W_NOGLOB; | |
497 | fprintf (stderr, "W_NOGLOB%s", f ? "|" : ""); | |
498 | } | |
499 | if (f & W_SPLITSPACE) | |
500 | { | |
501 | f &= ~W_SPLITSPACE; | |
502 | fprintf (stderr, "W_SPLITSPACE%s", f ? "|" : ""); | |
0001803f CR |
503 | } |
504 | if (f & W_ASSIGNMENT) | |
505 | { | |
506 | f &= ~W_ASSIGNMENT; | |
507 | fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : ""); | |
508 | } | |
509 | if (f & W_QUOTED) | |
510 | { | |
511 | f &= ~W_QUOTED; | |
512 | fprintf (stderr, "W_QUOTED%s", f ? "|" : ""); | |
513 | } | |
514 | if (f & W_HASDOLLAR) | |
515 | { | |
516 | f &= ~W_HASDOLLAR; | |
517 | fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : ""); | |
518 | } | |
a0c0a00f CR |
519 | if (f & W_COMPLETE) |
520 | { | |
521 | f &= ~W_COMPLETE; | |
522 | fprintf (stderr, "W_COMPLETE%s", f ? "|" : ""); | |
523 | } | |
d233b485 CR |
524 | if (f & W_CHKLOCAL) |
525 | { | |
526 | f &= ~W_CHKLOCAL; | |
527 | fprintf (stderr, "W_CHKLOCAL%s", f ? "|" : ""); | |
528 | } | |
712f80b0 CR |
529 | if (f & W_FORCELOCAL) |
530 | { | |
531 | f &= ~W_FORCELOCAL; | |
532 | fprintf (stderr, "W_FORCELOCAL%s", f ? "|" : ""); | |
533 | } | |
534 | ||
0001803f CR |
535 | fprintf (stderr, "\n"); |
536 | fflush (stderr); | |
537 | } | |
538 | #endif | |
539 | ||
7117c2d2 | 540 | #ifdef INCLUDE_UNUSED |
ccc6cda3 JA |
541 | static char * |
542 | quoted_substring (string, start, end) | |
543 | char *string; | |
544 | int start, end; | |
545 | { | |
546 | register int len, l; | |
547 | register char *result, *s, *r; | |
548 | ||
549 | len = end - start; | |
550 | ||
551 | /* Move to string[start], skipping quoted characters. */ | |
552 | for (s = string, l = 0; *s && l < start; ) | |
553 | { | |
554 | if (*s == CTLESC) | |
555 | { | |
28ef6c31 JA |
556 | s++; |
557 | continue; | |
ccc6cda3 JA |
558 | } |
559 | l++; | |
560 | if (*s == 0) | |
28ef6c31 | 561 | break; |
ccc6cda3 JA |
562 | } |
563 | ||
f73dda09 | 564 | r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */ |
ccc6cda3 JA |
565 | |
566 | /* Copy LEN characters, including quote characters. */ | |
567 | s = string + l; | |
568 | for (l = 0; l < len; s++) | |
569 | { | |
570 | if (*s == CTLESC) | |
28ef6c31 | 571 | *r++ = *s++; |
ccc6cda3 JA |
572 | *r++ = *s; |
573 | l++; | |
574 | if (*s == 0) | |
28ef6c31 | 575 | break; |
ccc6cda3 JA |
576 | } |
577 | *r = '\0'; | |
578 | return result; | |
579 | } | |
7117c2d2 JA |
580 | #endif |
581 | ||
582 | #ifdef INCLUDE_UNUSED | |
583 | /* Return the length of S, skipping over quoted characters */ | |
584 | static int | |
585 | quoted_strlen (s) | |
586 | char *s; | |
587 | { | |
588 | register char *p; | |
589 | int i; | |
590 | ||
591 | i = 0; | |
592 | for (p = s; *p; p++) | |
593 | { | |
594 | if (*p == CTLESC) | |
595 | { | |
596 | p++; | |
597 | if (*p == 0) | |
598 | return (i + 1); | |
599 | } | |
600 | i++; | |
601 | } | |
602 | ||
603 | return i; | |
604 | } | |
605 | #endif | |
ccc6cda3 | 606 | |
d233b485 | 607 | #ifdef INCLUDE_UNUSED |
ccc6cda3 JA |
608 | /* Find the first occurrence of character C in string S, obeying shell |
609 | quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped | |
610 | characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters | |
611 | escaped with CTLESC are skipped. */ | |
7117c2d2 | 612 | static char * |
ccc6cda3 JA |
613 | quoted_strchr (s, c, flags) |
614 | char *s; | |
615 | int c, flags; | |
616 | { | |
617 | register char *p; | |
618 | ||
619 | for (p = s; *p; p++) | |
620 | { | |
621 | if (((flags & ST_BACKSL) && *p == '\\') | |
622 | || ((flags & ST_CTLESC) && *p == CTLESC)) | |
623 | { | |
624 | p++; | |
625 | if (*p == '\0') | |
626 | return ((char *)NULL); | |
627 | continue; | |
628 | } | |
629 | else if (*p == c) | |
630 | return p; | |
631 | } | |
632 | return ((char *)NULL); | |
633 | } | |
634 | ||
cce855bc | 635 | /* Return 1 if CHARACTER appears in an unquoted portion of |
7117c2d2 | 636 | STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */ |
cce855bc JA |
637 | static int |
638 | unquoted_member (character, string) | |
639 | int character; | |
726f6388 JA |
640 | char *string; |
641 | { | |
7117c2d2 | 642 | size_t slen; |
cce855bc | 643 | int sindex, c; |
7117c2d2 | 644 | DECLARE_MBSTATE; |
726f6388 | 645 | |
7117c2d2 JA |
646 | slen = strlen (string); |
647 | sindex = 0; | |
648 | while (c = string[sindex]) | |
726f6388 | 649 | { |
cce855bc JA |
650 | if (c == character) |
651 | return (1); | |
652 | ||
653 | switch (c) | |
ccc6cda3 | 654 | { |
cce855bc | 655 | default: |
7117c2d2 | 656 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
657 | break; |
658 | ||
659 | case '\\': | |
660 | sindex++; | |
661 | if (string[sindex]) | |
7117c2d2 | 662 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
663 | break; |
664 | ||
665 | case '\'': | |
a0c0a00f | 666 | sindex = skip_single_quoted (string, slen, ++sindex, 0); |
cce855bc JA |
667 | break; |
668 | ||
669 | case '"': | |
a0c0a00f | 670 | sindex = skip_double_quoted (string, slen, ++sindex, 0); |
cce855bc | 671 | break; |
ccc6cda3 | 672 | } |
726f6388 | 673 | } |
cce855bc | 674 | return (0); |
726f6388 JA |
675 | } |
676 | ||
cce855bc JA |
677 | /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */ |
678 | static int | |
679 | unquoted_substring (substr, string) | |
680 | char *substr, *string; | |
726f6388 | 681 | { |
7117c2d2 | 682 | size_t slen; |
cce855bc | 683 | int sindex, c, sublen; |
7117c2d2 | 684 | DECLARE_MBSTATE; |
726f6388 | 685 | |
cce855bc JA |
686 | if (substr == 0 || *substr == '\0') |
687 | return (0); | |
688 | ||
7117c2d2 | 689 | slen = strlen (string); |
cce855bc JA |
690 | sublen = strlen (substr); |
691 | for (sindex = 0; c = string[sindex]; ) | |
726f6388 | 692 | { |
cce855bc JA |
693 | if (STREQN (string + sindex, substr, sublen)) |
694 | return (1); | |
695 | ||
696 | switch (c) | |
697 | { | |
698 | case '\\': | |
699 | sindex++; | |
cce855bc | 700 | if (string[sindex]) |
7117c2d2 | 701 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
702 | break; |
703 | ||
704 | case '\'': | |
a0c0a00f | 705 | sindex = skip_single_quoted (string, slen, ++sindex, 0); |
cce855bc JA |
706 | break; |
707 | ||
708 | case '"': | |
a0c0a00f | 709 | sindex = skip_double_quoted (string, slen, ++sindex, 0); |
cce855bc JA |
710 | break; |
711 | ||
712 | default: | |
7117c2d2 | 713 | ADVANCE_CHAR (string, slen, sindex); |
cce855bc JA |
714 | break; |
715 | } | |
726f6388 | 716 | } |
cce855bc | 717 | return (0); |
ccc6cda3 | 718 | } |
a0c0a00f | 719 | #endif |
726f6388 | 720 | |
cce855bc JA |
721 | /* Most of the substitutions must be done in parallel. In order |
722 | to avoid using tons of unclear goto's, I have some functions | |
723 | for manipulating malloc'ed strings. They all take INDX, a | |
724 | pointer to an integer which is the offset into the string | |
725 | where manipulation is taking place. They also take SIZE, a | |
726 | pointer to an integer which is the current length of the | |
727 | character array for this string. */ | |
726f6388 | 728 | |
cce855bc JA |
729 | /* Append SOURCE to TARGET at INDEX. SIZE is the current amount |
730 | of space allocated to TARGET. SOURCE can be NULL, in which | |
731 | case nothing happens. Gets rid of SOURCE by freeing it. | |
732 | Returns TARGET in case the location has changed. */ | |
7117c2d2 | 733 | INLINE char * |
cce855bc JA |
734 | sub_append_string (source, target, indx, size) |
735 | char *source, *target; | |
a0c0a00f CR |
736 | int *indx; |
737 | size_t *size; | |
cce855bc JA |
738 | { |
739 | if (source) | |
726f6388 | 740 | { |
a0c0a00f CR |
741 | int n; |
742 | size_t srclen; | |
cce855bc JA |
743 | |
744 | srclen = STRLEN (source); | |
745 | if (srclen >= (int)(*size - *indx)) | |
726f6388 | 746 | { |
cce855bc JA |
747 | n = srclen + *indx; |
748 | n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE); | |
f73dda09 | 749 | target = (char *)xrealloc (target, (*size = n)); |
726f6388 | 750 | } |
cce855bc JA |
751 | |
752 | FASTCOPY (source, target + *indx, srclen); | |
753 | *indx += srclen; | |
754 | target[*indx] = '\0'; | |
755 | ||
756 | free (source); | |
726f6388 | 757 | } |
cce855bc JA |
758 | return (target); |
759 | } | |
760 | ||
761 | #if 0 | |
762 | /* UNUSED */ | |
763 | /* Append the textual representation of NUMBER to TARGET. | |
764 | INDX and SIZE are as in SUB_APPEND_STRING. */ | |
765 | char * | |
766 | sub_append_number (number, target, indx, size) | |
7117c2d2 | 767 | intmax_t number; |
cce855bc | 768 | char *target; |
a0c0a00f CR |
769 | int *indx; |
770 | size_t *size; | |
cce855bc JA |
771 | { |
772 | char *temp; | |
773 | ||
774 | temp = itos (number); | |
775 | return (sub_append_string (temp, target, indx, size)); | |
726f6388 | 776 | } |
d166f048 | 777 | #endif |
726f6388 JA |
778 | |
779 | /* Extract a substring from STRING, starting at SINDEX and ending with | |
780 | one of the characters in CHARLIST. Don't make the ending character | |
781 | part of the string. Leave SINDEX pointing at the ending character. | |
3185942a | 782 | Understand about backslashes in the string. If (flags & SX_VARNAME) |
7117c2d2 JA |
783 | is non-zero, and array variables have been compiled into the shell, |
784 | everything between a `[' and a corresponding `]' is skipped over. | |
3185942a JA |
785 | If (flags & SX_NOALLOC) is non-zero, don't return the substring, just |
786 | update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must | |
95732b49 | 787 | contain a closing character from CHARLIST. */ |
726f6388 | 788 | static char * |
7117c2d2 | 789 | string_extract (string, sindex, charlist, flags) |
f73dda09 JA |
790 | char *string; |
791 | int *sindex; | |
792 | char *charlist; | |
7117c2d2 | 793 | int flags; |
726f6388 | 794 | { |
ccc6cda3 | 795 | register int c, i; |
95732b49 | 796 | int found; |
7117c2d2 | 797 | size_t slen; |
726f6388 | 798 | char *temp; |
7117c2d2 | 799 | DECLARE_MBSTATE; |
726f6388 | 800 | |
95732b49 | 801 | slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0; |
7117c2d2 | 802 | i = *sindex; |
95732b49 | 803 | found = 0; |
7117c2d2 | 804 | while (c = string[i]) |
726f6388 JA |
805 | { |
806 | if (c == '\\') | |
7117c2d2 JA |
807 | { |
808 | if (string[i + 1]) | |
809 | i++; | |
810 | else | |
811 | break; | |
812 | } | |
ccc6cda3 | 813 | #if defined (ARRAY_VARS) |
d233b485 | 814 | else if ((flags & SX_VARNAME) && c == LBRACK) |
ccc6cda3 JA |
815 | { |
816 | int ni; | |
817 | /* If this is an array subscript, skip over it and continue. */ | |
0001803f | 818 | ni = skipsubscript (string, i, 0); |
d233b485 | 819 | if (string[ni] == RBRACK) |
ccc6cda3 JA |
820 | i = ni; |
821 | } | |
822 | #endif | |
823 | else if (MEMBER (c, charlist)) | |
95732b49 JA |
824 | { |
825 | found = 1; | |
726f6388 | 826 | break; |
95732b49 | 827 | } |
7117c2d2 JA |
828 | |
829 | ADVANCE_CHAR (string, slen, i); | |
726f6388 | 830 | } |
bb70624e | 831 | |
95732b49 JA |
832 | /* If we had to have a matching delimiter and didn't find one, return an |
833 | error and let the caller deal with it. */ | |
3185942a | 834 | if ((flags & SX_REQMATCH) && found == 0) |
95732b49 JA |
835 | { |
836 | *sindex = i; | |
837 | return (&extract_string_error); | |
838 | } | |
839 | ||
3185942a | 840 | temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i); |
726f6388 | 841 | *sindex = i; |
95732b49 | 842 | |
726f6388 JA |
843 | return (temp); |
844 | } | |
845 | ||
ccc6cda3 JA |
846 | /* Extract the contents of STRING as if it is enclosed in double quotes. |
847 | SINDEX, when passed in, is the offset of the character immediately | |
848 | following the opening double quote; on exit, SINDEX is left pointing after | |
849 | the closing double quote. If STRIPDQ is non-zero, unquoted double | |
850 | quotes are stripped and the string is terminated by a null byte. | |
851 | Backslashes between the embedded double quotes are processed. If STRIPDQ | |
852 | is zero, an unquoted `"' terminates the string. */ | |
7117c2d2 | 853 | static char * |
a0c0a00f | 854 | string_extract_double_quoted (string, sindex, flags) |
726f6388 | 855 | char *string; |
a0c0a00f | 856 | int *sindex, flags; |
726f6388 | 857 | { |
7117c2d2 JA |
858 | size_t slen; |
859 | char *send; | |
f73dda09 JA |
860 | int j, i, t; |
861 | unsigned char c; | |
ccc6cda3 JA |
862 | char *temp, *ret; /* The new string we return. */ |
863 | int pass_next, backquote, si; /* State variables for the machine. */ | |
864 | int dquote; | |
a0c0a00f | 865 | int stripdq; |
7117c2d2 JA |
866 | DECLARE_MBSTATE; |
867 | ||
868 | slen = strlen (string + *sindex) + *sindex; | |
869 | send = string + slen; | |
726f6388 | 870 | |
a0c0a00f CR |
871 | stripdq = (flags & SX_STRIPDQ); |
872 | ||
ccc6cda3 | 873 | pass_next = backquote = dquote = 0; |
7117c2d2 | 874 | temp = (char *)xmalloc (1 + slen - *sindex); |
726f6388 | 875 | |
7117c2d2 JA |
876 | j = 0; |
877 | i = *sindex; | |
878 | while (c = string[i]) | |
726f6388 | 879 | { |
ccc6cda3 JA |
880 | /* Process a character that was quoted by a backslash. */ |
881 | if (pass_next) | |
726f6388 | 882 | { |
495aee44 | 883 | /* XXX - take another look at this in light of Interp 221 */ |
ccc6cda3 | 884 | /* Posix.2 sez: |
726f6388 | 885 | |
ccc6cda3 JA |
886 | ``The backslash shall retain its special meaning as an escape |
887 | character only when followed by one of the characters: | |
7117c2d2 | 888 | $ ` " \ <newline>''. |
726f6388 | 889 | |
ccc6cda3 JA |
890 | If STRIPDQ is zero, we handle the double quotes here and let |
891 | expand_word_internal handle the rest. If STRIPDQ is non-zero, | |
892 | we have already been through one round of backslash stripping, | |
893 | and want to strip these backslashes only if DQUOTE is non-zero, | |
894 | indicating that we are inside an embedded double-quoted string. */ | |
895 | ||
d233b485 CR |
896 | /* If we are in an embedded quoted string, then don't strip |
897 | backslashes before characters for which the backslash | |
898 | retains its special meaning, but remove backslashes in | |
899 | front of other characters. If we are not in an | |
900 | embedded quoted string, don't strip backslashes at all. | |
901 | This mess is necessary because the string was already | |
902 | surrounded by double quotes (and sh has some really weird | |
903 | quoting rules). | |
904 | The returned string will be run through expansion as if | |
905 | it were double-quoted. */ | |
ccc6cda3 | 906 | if ((stripdq == 0 && c != '"') || |
28ef6c31 | 907 | (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0))) |
ccc6cda3 | 908 | temp[j++] = '\\'; |
ccc6cda3 | 909 | pass_next = 0; |
7117c2d2 JA |
910 | |
911 | add_one_character: | |
912 | COPY_CHAR_I (temp, j, string, send, i); | |
ccc6cda3 JA |
913 | continue; |
914 | } | |
726f6388 | 915 | |
ccc6cda3 JA |
916 | /* A backslash protects the next character. The code just above |
917 | handles preserving the backslash in front of any character but | |
918 | a double quote. */ | |
919 | if (c == '\\') | |
726f6388 | 920 | { |
ccc6cda3 | 921 | pass_next++; |
7117c2d2 | 922 | i++; |
726f6388 JA |
923 | continue; |
924 | } | |
925 | ||
ccc6cda3 JA |
926 | /* Inside backquotes, ``the portion of the quoted string from the |
927 | initial backquote and the characters up to the next backquote | |
928 | that is not preceded by a backslash, having escape characters | |
929 | removed, defines that command''. */ | |
930 | if (backquote) | |
726f6388 | 931 | { |
ccc6cda3 JA |
932 | if (c == '`') |
933 | backquote = 0; | |
d233b485 | 934 | temp[j++] = c; /* COPY_CHAR_I? */ |
7117c2d2 | 935 | i++; |
726f6388 JA |
936 | continue; |
937 | } | |
938 | ||
ccc6cda3 | 939 | if (c == '`') |
726f6388 | 940 | { |
ccc6cda3 JA |
941 | temp[j++] = c; |
942 | backquote++; | |
7117c2d2 | 943 | i++; |
ccc6cda3 | 944 | continue; |
726f6388 JA |
945 | } |
946 | ||
ccc6cda3 JA |
947 | /* Pass everything between `$(' and the matching `)' or a quoted |
948 | ${ ... } pair through according to the Posix.2 specification. */ | |
cce855bc | 949 | if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE))) |
726f6388 | 950 | { |
b80f6443 JA |
951 | int free_ret = 1; |
952 | ||
ccc6cda3 | 953 | si = i + 2; |
cce855bc | 954 | if (string[i + 1] == LPAREN) |
a0c0a00f | 955 | ret = extract_command_subst (string, &si, (flags & SX_COMPLETE)); |
ccc6cda3 | 956 | else |
495aee44 | 957 | ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, 0); |
726f6388 | 958 | |
ccc6cda3 JA |
959 | temp[j++] = '$'; |
960 | temp[j++] = string[i + 1]; | |
726f6388 | 961 | |
b80f6443 JA |
962 | /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error |
963 | is set. */ | |
964 | if (ret == 0 && no_longjmp_on_fatal_error) | |
965 | { | |
966 | free_ret = 0; | |
967 | ret = string + i + 2; | |
968 | } | |
969 | ||
d233b485 | 970 | /* XXX - CHECK_STRING_OVERRUN here? */ |
ccc6cda3 JA |
971 | for (t = 0; ret[t]; t++, j++) |
972 | temp[j] = ret[t]; | |
b80f6443 | 973 | temp[j] = string[si]; |
726f6388 | 974 | |
712f80b0 CR |
975 | if (si < i + 2) /* we went back? */ |
976 | i += 2; | |
977 | else if (string[si]) | |
b80f6443 JA |
978 | { |
979 | j++; | |
980 | i = si + 1; | |
981 | } | |
982 | else | |
983 | i = si; | |
984 | ||
985 | if (free_ret) | |
986 | free (ret); | |
ccc6cda3 | 987 | continue; |
726f6388 JA |
988 | } |
989 | ||
ccc6cda3 | 990 | /* Add any character but a double quote to the quoted string we're |
28ef6c31 | 991 | accumulating. */ |
ccc6cda3 | 992 | if (c != '"') |
7117c2d2 | 993 | goto add_one_character; |
ccc6cda3 JA |
994 | |
995 | /* c == '"' */ | |
996 | if (stripdq) | |
726f6388 | 997 | { |
ccc6cda3 | 998 | dquote ^= 1; |
7117c2d2 | 999 | i++; |
ccc6cda3 | 1000 | continue; |
726f6388 | 1001 | } |
ccc6cda3 JA |
1002 | |
1003 | break; | |
726f6388 | 1004 | } |
ccc6cda3 | 1005 | temp[j] = '\0'; |
726f6388 | 1006 | |
ccc6cda3 JA |
1007 | /* Point to after the closing quote. */ |
1008 | if (c) | |
1009 | i++; | |
726f6388 JA |
1010 | *sindex = i; |
1011 | ||
ccc6cda3 JA |
1012 | return (temp); |
1013 | } | |
1014 | ||
1015 | /* This should really be another option to string_extract_double_quoted. */ | |
f73dda09 | 1016 | static int |
a0c0a00f | 1017 | skip_double_quoted (string, slen, sind, flags) |
ccc6cda3 | 1018 | char *string; |
7117c2d2 | 1019 | size_t slen; |
ccc6cda3 | 1020 | int sind; |
a0c0a00f | 1021 | int flags; |
ccc6cda3 | 1022 | { |
f73dda09 | 1023 | int c, i; |
ccc6cda3 JA |
1024 | char *ret; |
1025 | int pass_next, backquote, si; | |
7117c2d2 | 1026 | DECLARE_MBSTATE; |
ccc6cda3 JA |
1027 | |
1028 | pass_next = backquote = 0; | |
7117c2d2 JA |
1029 | i = sind; |
1030 | while (c = string[i]) | |
726f6388 | 1031 | { |
ccc6cda3 JA |
1032 | if (pass_next) |
1033 | { | |
1034 | pass_next = 0; | |
7117c2d2 | 1035 | ADVANCE_CHAR (string, slen, i); |
ccc6cda3 JA |
1036 | continue; |
1037 | } | |
1038 | else if (c == '\\') | |
1039 | { | |
1040 | pass_next++; | |
7117c2d2 | 1041 | i++; |
ccc6cda3 JA |
1042 | continue; |
1043 | } | |
1044 | else if (backquote) | |
1045 | { | |
1046 | if (c == '`') | |
1047 | backquote = 0; | |
7117c2d2 | 1048 | ADVANCE_CHAR (string, slen, i); |
ccc6cda3 JA |
1049 | continue; |
1050 | } | |
1051 | else if (c == '`') | |
1052 | { | |
1053 | backquote++; | |
7117c2d2 | 1054 | i++; |
ccc6cda3 JA |
1055 | continue; |
1056 | } | |
cce855bc | 1057 | else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE))) |
ccc6cda3 JA |
1058 | { |
1059 | si = i + 2; | |
cce855bc | 1060 | if (string[i + 1] == LPAREN) |
a0c0a00f | 1061 | ret = extract_command_subst (string, &si, SX_NOALLOC|(flags&SX_COMPLETE)); |
ccc6cda3 | 1062 | else |
495aee44 | 1063 | ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, SX_NOALLOC); |
ccc6cda3 | 1064 | |
a0c0a00f CR |
1065 | /* These can consume the entire string if they are unterminated */ |
1066 | CHECK_STRING_OVERRUN (i, si, slen, c); | |
1067 | ||
7117c2d2 | 1068 | i = si + 1; |
ccc6cda3 JA |
1069 | continue; |
1070 | } | |
1071 | else if (c != '"') | |
7117c2d2 JA |
1072 | { |
1073 | ADVANCE_CHAR (string, slen, i); | |
1074 | continue; | |
1075 | } | |
ccc6cda3 JA |
1076 | else |
1077 | break; | |
726f6388 | 1078 | } |
ccc6cda3 JA |
1079 | |
1080 | if (c) | |
1081 | i++; | |
1082 | ||
1083 | return (i); | |
726f6388 JA |
1084 | } |
1085 | ||
ccc6cda3 JA |
1086 | /* Extract the contents of STRING as if it is enclosed in single quotes. |
1087 | SINDEX, when passed in, is the offset of the character immediately | |
1088 | following the opening single quote; on exit, SINDEX is left pointing after | |
1089 | the closing single quote. */ | |
1090 | static inline char * | |
1091 | string_extract_single_quoted (string, sindex) | |
1092 | char *string; | |
1093 | int *sindex; | |
1094 | { | |
f73dda09 | 1095 | register int i; |
7117c2d2 | 1096 | size_t slen; |
ccc6cda3 | 1097 | char *t; |
7117c2d2 | 1098 | DECLARE_MBSTATE; |
ccc6cda3 | 1099 | |
95732b49 JA |
1100 | /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */ |
1101 | slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0; | |
7117c2d2 JA |
1102 | i = *sindex; |
1103 | while (string[i] && string[i] != '\'') | |
1104 | ADVANCE_CHAR (string, slen, i); | |
ccc6cda3 | 1105 | |
bb70624e | 1106 | t = substring (string, *sindex, i); |
ccc6cda3 JA |
1107 | |
1108 | if (string[i]) | |
1109 | i++; | |
1110 | *sindex = i; | |
1111 | ||
1112 | return (t); | |
1113 | } | |
1114 | ||
a0c0a00f CR |
1115 | /* Skip over a single-quoted string. We overload the SX_COMPLETE flag to mean |
1116 | that we are splitting out words for completion and have encountered a $'...' | |
1117 | string, which allows backslash-escaped single quotes. */ | |
ccc6cda3 | 1118 | static inline int |
a0c0a00f | 1119 | skip_single_quoted (string, slen, sind, flags) |
0628567a | 1120 | const char *string; |
7117c2d2 | 1121 | size_t slen; |
ccc6cda3 | 1122 | int sind; |
a0c0a00f | 1123 | int flags; |
ccc6cda3 | 1124 | { |
28ef6c31 | 1125 | register int c; |
7117c2d2 JA |
1126 | DECLARE_MBSTATE; |
1127 | ||
1128 | c = sind; | |
1129 | while (string[c] && string[c] != '\'') | |
a0c0a00f CR |
1130 | { |
1131 | if ((flags & SX_COMPLETE) && string[c] == '\\' && string[c+1] == '\'' && string[c+2]) | |
1132 | ADVANCE_CHAR (string, slen, c); | |
1133 | ADVANCE_CHAR (string, slen, c); | |
1134 | } | |
ccc6cda3 | 1135 | |
28ef6c31 JA |
1136 | if (string[c]) |
1137 | c++; | |
1138 | return c; | |
ccc6cda3 JA |
1139 | } |
1140 | ||
1141 | /* Just like string_extract, but doesn't hack backslashes or any of | |
bb70624e | 1142 | that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */ |
726f6388 | 1143 | static char * |
3185942a | 1144 | string_extract_verbatim (string, slen, sindex, charlist, flags) |
f73dda09 | 1145 | char *string; |
95732b49 | 1146 | size_t slen; |
ccc6cda3 | 1147 | int *sindex; |
f73dda09 | 1148 | char *charlist; |
3185942a | 1149 | int flags; |
ccc6cda3 | 1150 | { |
0001803f | 1151 | register int i; |
95732b49 | 1152 | #if defined (HANDLE_MULTIBYTE) |
95732b49 JA |
1153 | wchar_t *wcharlist; |
1154 | #endif | |
ccc6cda3 JA |
1155 | int c; |
1156 | char *temp; | |
95732b49 | 1157 | DECLARE_MBSTATE; |
ccc6cda3 | 1158 | |
a0c0a00f | 1159 | if ((flags & SX_NOCTLESC) && charlist[0] == '\'' && charlist[1] == '\0') |
ccc6cda3 JA |
1160 | { |
1161 | temp = string_extract_single_quoted (string, sindex); | |
1162 | --*sindex; /* leave *sindex at separator character */ | |
1163 | return temp; | |
1164 | } | |
1165 | ||
712f80b0 CR |
1166 | /* This can never be called with charlist == NULL. If *charlist == NULL, |
1167 | we can skip the loop and just return a copy of the string, updating | |
1168 | *sindex */ | |
1169 | if (*charlist == 0) | |
1170 | { | |
1171 | temp = string + *sindex; | |
1172 | c = (*sindex == 0) ? slen : STRLEN (temp); | |
1173 | temp = savestring (temp); | |
1174 | *sindex += c; | |
1175 | return temp; | |
1176 | } | |
1177 | ||
95732b49 | 1178 | i = *sindex; |
95732b49 | 1179 | #if defined (HANDLE_MULTIBYTE) |
95732b49 JA |
1180 | wcharlist = 0; |
1181 | #endif | |
1182 | while (c = string[i]) | |
ccc6cda3 | 1183 | { |
95732b49 JA |
1184 | #if defined (HANDLE_MULTIBYTE) |
1185 | size_t mblength; | |
1186 | #endif | |
3185942a JA |
1187 | if ((flags & SX_NOCTLESC) == 0 && c == CTLESC) |
1188 | { | |
1189 | i += 2; | |
d233b485 | 1190 | CHECK_STRING_OVERRUN (i, i, slen, c); |
3185942a JA |
1191 | continue; |
1192 | } | |
1193 | /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL | |
1194 | through, to protect the CTLNULs from later calls to | |
1195 | remove_quoted_nulls. */ | |
1196 | else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC && string[i+1] == CTLNUL) | |
ccc6cda3 | 1197 | { |
95732b49 | 1198 | i += 2; |
d233b485 | 1199 | CHECK_STRING_OVERRUN (i, i, slen, c); |
ccc6cda3 JA |
1200 | continue; |
1201 | } | |
1202 | ||
95732b49 | 1203 | #if defined (HANDLE_MULTIBYTE) |
d233b485 CR |
1204 | if (locale_utf8locale && slen > i && UTF8_SINGLEBYTE (string[i])) |
1205 | mblength = (string[i] != 0) ? 1 : 0; | |
1206 | else | |
1207 | mblength = MBLEN (string + i, slen - i); | |
95732b49 JA |
1208 | if (mblength > 1) |
1209 | { | |
1210 | wchar_t wc; | |
1211 | mblength = mbtowc (&wc, string + i, slen - i); | |
1212 | if (MB_INVALIDCH (mblength)) | |
1213 | { | |
1214 | if (MEMBER (c, charlist)) | |
1215 | break; | |
1216 | } | |
1217 | else | |
1218 | { | |
1219 | if (wcharlist == 0) | |
1220 | { | |
1221 | size_t len; | |
1222 | len = mbstowcs (wcharlist, charlist, 0); | |
1223 | if (len == -1) | |
1224 | len = 0; | |
0628567a JA |
1225 | wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1)); |
1226 | mbstowcs (wcharlist, charlist, len + 1); | |
95732b49 JA |
1227 | } |
1228 | ||
1229 | if (wcschr (wcharlist, wc)) | |
1230 | break; | |
1231 | } | |
1232 | } | |
1233 | else | |
1234 | #endif | |
ccc6cda3 JA |
1235 | if (MEMBER (c, charlist)) |
1236 | break; | |
95732b49 JA |
1237 | |
1238 | ADVANCE_CHAR (string, slen, i); | |
ccc6cda3 JA |
1239 | } |
1240 | ||
95732b49 JA |
1241 | #if defined (HANDLE_MULTIBYTE) |
1242 | FREE (wcharlist); | |
1243 | #endif | |
1244 | ||
bb70624e | 1245 | temp = substring (string, *sindex, i); |
ccc6cda3 JA |
1246 | *sindex = i; |
1247 | ||
1248 | return (temp); | |
1249 | } | |
1250 | ||
1251 | /* Extract the $( construct in STRING, and return a new string. | |
1252 | Start extracting at (SINDEX) as if we had just seen "$(". | |
3185942a | 1253 | Make (SINDEX) get the position of the matching ")". ) |
0001803f | 1254 | XFLAGS is additional flags to pass to other extraction functions. */ |
ccc6cda3 | 1255 | char * |
3185942a | 1256 | extract_command_subst (string, sindex, xflags) |
726f6388 JA |
1257 | char *string; |
1258 | int *sindex; | |
3185942a | 1259 | int xflags; |
726f6388 | 1260 | { |
a0c0a00f CR |
1261 | char *ret; |
1262 | ||
1263 | if (string[*sindex] == LPAREN || (xflags & SX_COMPLETE)) | |
3185942a JA |
1264 | return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/ |
1265 | else | |
1266 | { | |
1267 | xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0); | |
a0c0a00f CR |
1268 | ret = xparse_dolparen (string, string+*sindex, sindex, xflags); |
1269 | return ret; | |
3185942a | 1270 | } |
ccc6cda3 JA |
1271 | } |
1272 | ||
28ef6c31 | 1273 | /* Extract the $[ construct in STRING, and return a new string. (]) |
ccc6cda3 JA |
1274 | Start extracting at (SINDEX) as if we had just seen "$[". |
1275 | Make (SINDEX) get the position of the matching "]". */ | |
1276 | char * | |
1277 | extract_arithmetic_subst (string, sindex) | |
1278 | char *string; | |
1279 | int *sindex; | |
1280 | { | |
7117c2d2 | 1281 | return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/ |
ccc6cda3 JA |
1282 | } |
1283 | ||
1284 | #if defined (PROCESS_SUBSTITUTION) | |
1285 | /* Extract the <( or >( construct in STRING, and return a new string. | |
1286 | Start extracting at (SINDEX) as if we had just seen "<(". | |
cce855bc | 1287 | Make (SINDEX) get the position of the matching ")". */ /*))*/ |
ccc6cda3 | 1288 | char * |
85b94814 | 1289 | extract_process_subst (string, starter, sindex, xflags) |
ccc6cda3 JA |
1290 | char *string; |
1291 | char *starter; | |
1292 | int *sindex; | |
85b94814 | 1293 | int xflags; |
ccc6cda3 | 1294 | { |
85b94814 | 1295 | #if 0 |
d233b485 | 1296 | /* XXX - check xflags&SX_COMPLETE here? */ |
ac50fbac | 1297 | return (extract_delimited_string (string, sindex, starter, "(", ")", SX_COMMAND)); |
85b94814 CR |
1298 | #else |
1299 | xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0); | |
1300 | return (xparse_dolparen (string, string+*sindex, sindex, xflags)); | |
1301 | #endif | |
ccc6cda3 JA |
1302 | } |
1303 | #endif /* PROCESS_SUBSTITUTION */ | |
1304 | ||
1305 | #if defined (ARRAY_VARS) | |
95732b49 JA |
1306 | /* This can be fooled by unquoted right parens in the passed string. If |
1307 | each caller verifies that the last character in STRING is a right paren, | |
1308 | we don't even need to call extract_delimited_string. */ | |
ccc6cda3 JA |
1309 | char * |
1310 | extract_array_assignment_list (string, sindex) | |
1311 | char *string; | |
1312 | int *sindex; | |
1313 | { | |
95732b49 JA |
1314 | int slen; |
1315 | char *ret; | |
1316 | ||
d233b485 CR |
1317 | slen = strlen (string); |
1318 | if (string[slen - 1] == RPAREN) | |
95732b49 JA |
1319 | { |
1320 | ret = substring (string, *sindex, slen - 1); | |
1321 | *sindex = slen - 1; | |
1322 | return ret; | |
1323 | } | |
1324 | return 0; | |
ccc6cda3 JA |
1325 | } |
1326 | #endif | |
1327 | ||
1328 | /* Extract and create a new string from the contents of STRING, a | |
1329 | character string delimited with OPENER and CLOSER. SINDEX is | |
1330 | the address of an int describing the current offset in STRING; | |
1331 | it should point to just after the first OPENER found. On exit, | |
1332 | SINDEX gets the position of the last character of the matching CLOSER. | |
1333 | If OPENER is more than a single character, ALT_OPENER, if non-null, | |
1334 | contains a character string that can also match CLOSER and thus | |
1335 | needs to be skipped. */ | |
1336 | static char * | |
7117c2d2 | 1337 | extract_delimited_string (string, sindex, opener, alt_opener, closer, flags) |
ccc6cda3 JA |
1338 | char *string; |
1339 | int *sindex; | |
1340 | char *opener, *alt_opener, *closer; | |
7117c2d2 | 1341 | int flags; |
ccc6cda3 JA |
1342 | { |
1343 | int i, c, si; | |
7117c2d2 | 1344 | size_t slen; |
ccc6cda3 | 1345 | char *t, *result; |
0628567a | 1346 | int pass_character, nesting_level, in_comment; |
ccc6cda3 | 1347 | int len_closer, len_opener, len_alt_opener; |
7117c2d2 | 1348 | DECLARE_MBSTATE; |
ccc6cda3 | 1349 | |
7117c2d2 | 1350 | slen = strlen (string + *sindex) + *sindex; |
ccc6cda3 JA |
1351 | len_opener = STRLEN (opener); |
1352 | len_alt_opener = STRLEN (alt_opener); | |
1353 | len_closer = STRLEN (closer); | |
726f6388 | 1354 | |
0628567a | 1355 | pass_character = in_comment = 0; |
726f6388 JA |
1356 | |
1357 | nesting_level = 1; | |
ccc6cda3 | 1358 | i = *sindex; |
726f6388 | 1359 | |
ccc6cda3 | 1360 | while (nesting_level) |
726f6388 | 1361 | { |
ccc6cda3 JA |
1362 | c = string[i]; |
1363 | ||
a0c0a00f CR |
1364 | /* If a recursive call or a call to ADVANCE_CHAR leaves the index beyond |
1365 | the end of the string, catch it and cut the loop. */ | |
1366 | if (i > slen) | |
1367 | { | |
1368 | i = slen; | |
1369 | c = string[i = slen]; | |
1370 | break; | |
1371 | } | |
1372 | ||
ccc6cda3 | 1373 | if (c == 0) |
28ef6c31 | 1374 | break; |
ccc6cda3 | 1375 | |
0628567a JA |
1376 | if (in_comment) |
1377 | { | |
1378 | if (c == '\n') | |
1379 | in_comment = 0; | |
1380 | ADVANCE_CHAR (string, slen, i); | |
1381 | continue; | |
1382 | } | |
1383 | ||
ccc6cda3 | 1384 | if (pass_character) /* previous char was backslash */ |
726f6388 JA |
1385 | { |
1386 | pass_character = 0; | |
7117c2d2 | 1387 | ADVANCE_CHAR (string, slen, i); |
726f6388 JA |
1388 | continue; |
1389 | } | |
1390 | ||
0628567a | 1391 | /* Not exactly right yet; should handle shell metacharacters and |
0001803f | 1392 | multibyte characters, too. See COMMENT_BEGIN define in parse.y */ |
3185942a | 1393 | if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1]))) |
0628567a JA |
1394 | { |
1395 | in_comment = 1; | |
1396 | ADVANCE_CHAR (string, slen, i); | |
1397 | continue; | |
1398 | } | |
1399 | ||
7117c2d2 | 1400 | if (c == CTLESC || c == '\\') |
726f6388 | 1401 | { |
ccc6cda3 JA |
1402 | pass_character++; |
1403 | i++; | |
1404 | continue; | |
726f6388 JA |
1405 | } |
1406 | ||
495aee44 CR |
1407 | /* Process a nested command substitution, but only if we're parsing an |
1408 | arithmetic substitution. */ | |
0001803f CR |
1409 | if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN) |
1410 | { | |
1411 | si = i + 2; | |
495aee44 | 1412 | t = extract_command_subst (string, &si, flags|SX_NOALLOC); |
d233b485 | 1413 | CHECK_STRING_OVERRUN (i, si, slen, c); |
0001803f CR |
1414 | i = si + 1; |
1415 | continue; | |
1416 | } | |
0001803f | 1417 | |
ccc6cda3 JA |
1418 | /* Process a nested OPENER. */ |
1419 | if (STREQN (string + i, opener, len_opener)) | |
726f6388 | 1420 | { |
ccc6cda3 | 1421 | si = i + len_opener; |
3185942a | 1422 | t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC); |
d233b485 | 1423 | CHECK_STRING_OVERRUN (i, si, slen, c); |
ccc6cda3 | 1424 | i = si + 1; |
ccc6cda3 | 1425 | continue; |
726f6388 JA |
1426 | } |
1427 | ||
ccc6cda3 JA |
1428 | /* Process a nested ALT_OPENER */ |
1429 | if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener)) | |
726f6388 | 1430 | { |
ccc6cda3 | 1431 | si = i + len_alt_opener; |
3185942a | 1432 | t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC); |
d233b485 | 1433 | CHECK_STRING_OVERRUN (i, si, slen, c); |
ccc6cda3 | 1434 | i = si + 1; |
726f6388 JA |
1435 | continue; |
1436 | } | |
ccc6cda3 JA |
1437 | |
1438 | /* If the current substring terminates the delimited string, decrement | |
1439 | the nesting level. */ | |
1440 | if (STREQN (string + i, closer, len_closer)) | |
726f6388 | 1441 | { |
7117c2d2 | 1442 | i += len_closer - 1; /* move to last byte of the closer */ |
ccc6cda3 JA |
1443 | nesting_level--; |
1444 | if (nesting_level == 0) | |
1445 | break; | |
726f6388 | 1446 | } |
ccc6cda3 JA |
1447 | |
1448 | /* Pass old-style command substitution through verbatim. */ | |
1449 | if (c == '`') | |
28ef6c31 JA |
1450 | { |
1451 | si = i + 1; | |
3185942a | 1452 | t = string_extract (string, &si, "`", flags|SX_NOALLOC); |
d233b485 | 1453 | CHECK_STRING_OVERRUN (i, si, slen, c); |
28ef6c31 | 1454 | i = si + 1; |
28ef6c31 JA |
1455 | continue; |
1456 | } | |
ccc6cda3 | 1457 | |
7117c2d2 JA |
1458 | /* Pass single-quoted and double-quoted strings through verbatim. */ |
1459 | if (c == '\'' || c == '"') | |
28ef6c31 JA |
1460 | { |
1461 | si = i + 1; | |
a0c0a00f CR |
1462 | i = (c == '\'') ? skip_single_quoted (string, slen, si, 0) |
1463 | : skip_double_quoted (string, slen, si, 0); | |
28ef6c31 JA |
1464 | continue; |
1465 | } | |
ccc6cda3 | 1466 | |
7117c2d2 JA |
1467 | /* move past this character, which was not special. */ |
1468 | ADVANCE_CHAR (string, slen, i); | |
726f6388 JA |
1469 | } |
1470 | ||
b80f6443 | 1471 | if (c == 0 && nesting_level) |
726f6388 | 1472 | { |
b80f6443 JA |
1473 | if (no_longjmp_on_fatal_error == 0) |
1474 | { | |
b80f6443 | 1475 | last_command_exit_value = EXECUTION_FAILURE; |
ac50fbac | 1476 | report_error (_("bad substitution: no closing `%s' in %s"), closer, string); |
b80f6443 JA |
1477 | exp_jump_to_top_level (DISCARD); |
1478 | } | |
1479 | else | |
1480 | { | |
1481 | *sindex = i; | |
1482 | return (char *)NULL; | |
1483 | } | |
726f6388 | 1484 | } |
ccc6cda3 | 1485 | |
cce855bc | 1486 | si = i - *sindex - len_closer + 1; |
3185942a | 1487 | if (flags & SX_NOALLOC) |
7117c2d2 JA |
1488 | result = (char *)NULL; |
1489 | else | |
1490 | { | |
1491 | result = (char *)xmalloc (1 + si); | |
1492 | strncpy (result, string + *sindex, si); | |
1493 | result[si] = '\0'; | |
1494 | } | |
cce855bc JA |
1495 | *sindex = i; |
1496 | ||
726f6388 JA |
1497 | return (result); |
1498 | } | |
1499 | ||
ccc6cda3 JA |
1500 | /* Extract a parameter expansion expression within ${ and } from STRING. |
1501 | Obey the Posix.2 rules for finding the ending `}': count braces while | |
1502 | skipping over enclosed quoted strings and command substitutions. | |
1503 | SINDEX is the address of an int describing the current offset in STRING; | |
1504 | it should point to just after the first `{' found. On exit, SINDEX | |
1505 | gets the position of the matching `}'. QUOTED is non-zero if this | |
1506 | occurs inside double quotes. */ | |
1507 | /* XXX -- this is very similar to extract_delimited_string -- XXX */ | |
726f6388 | 1508 | static char * |
7117c2d2 | 1509 | extract_dollar_brace_string (string, sindex, quoted, flags) |
726f6388 | 1510 | char *string; |
7117c2d2 | 1511 | int *sindex, quoted, flags; |
726f6388 | 1512 | { |
f73dda09 | 1513 | register int i, c; |
7117c2d2 | 1514 | size_t slen; |
495aee44 | 1515 | int pass_character, nesting_level, si, dolbrace_state; |
ccc6cda3 | 1516 | char *result, *t; |
7117c2d2 | 1517 | DECLARE_MBSTATE; |
726f6388 | 1518 | |
ccc6cda3 | 1519 | pass_character = 0; |
ccc6cda3 | 1520 | nesting_level = 1; |
7117c2d2 | 1521 | slen = strlen (string + *sindex) + *sindex; |
ccc6cda3 | 1522 | |
495aee44 | 1523 | /* The handling of dolbrace_state needs to agree with the code in parse.y: |
49ed961b CR |
1524 | parse_matched_pair(). The different initial value is to handle the |
1525 | case where this function is called to parse the word in | |
1526 | ${param op word} (SX_WORD). */ | |
1527 | dolbrace_state = (flags & SX_WORD) ? DOLBRACE_WORD : DOLBRACE_PARAM; | |
1528 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && (flags & SX_POSIXEXP)) | |
1529 | dolbrace_state = DOLBRACE_QUOTE; | |
495aee44 | 1530 | |
7117c2d2 JA |
1531 | i = *sindex; |
1532 | while (c = string[i]) | |
726f6388 | 1533 | { |
ccc6cda3 | 1534 | if (pass_character) |
726f6388 | 1535 | { |
ccc6cda3 | 1536 | pass_character = 0; |
7117c2d2 | 1537 | ADVANCE_CHAR (string, slen, i); |
ccc6cda3 JA |
1538 | continue; |
1539 | } | |
726f6388 | 1540 | |
cce855bc JA |
1541 | /* CTLESCs and backslashes quote the next character. */ |
1542 | if (c == CTLESC || c == '\\') | |
726f6388 | 1543 | { |
ccc6cda3 | 1544 | pass_character++; |
7117c2d2 | 1545 | i++; |
726f6388 JA |
1546 | continue; |
1547 | } | |
1548 | ||
cce855bc | 1549 | if (string[i] == '$' && string[i+1] == LBRACE) |
726f6388 | 1550 | { |
ccc6cda3 | 1551 | nesting_level++; |
7117c2d2 | 1552 | i += 2; |
726f6388 JA |
1553 | continue; |
1554 | } | |
1555 | ||
cce855bc | 1556 | if (c == RBRACE) |
726f6388 | 1557 | { |
ccc6cda3 JA |
1558 | nesting_level--; |
1559 | if (nesting_level == 0) | |
1560 | break; | |
7117c2d2 | 1561 | i++; |
726f6388 JA |
1562 | continue; |
1563 | } | |
1564 | ||
ccc6cda3 JA |
1565 | /* Pass the contents of old-style command substitutions through |
1566 | verbatim. */ | |
1567 | if (c == '`') | |
726f6388 | 1568 | { |
ccc6cda3 | 1569 | si = i + 1; |
3185942a | 1570 | t = string_extract (string, &si, "`", flags|SX_NOALLOC); |
a0c0a00f CR |
1571 | |
1572 | CHECK_STRING_OVERRUN (i, si, slen, c); | |
d233b485 | 1573 | |
7117c2d2 | 1574 | i = si + 1; |
ccc6cda3 JA |
1575 | continue; |
1576 | } | |
726f6388 | 1577 | |
cce855bc JA |
1578 | /* Pass the contents of new-style command substitutions and |
1579 | arithmetic substitutions through verbatim. */ | |
1580 | if (string[i] == '$' && string[i+1] == LPAREN) | |
ccc6cda3 | 1581 | { |
726f6388 | 1582 | si = i + 2; |
3185942a | 1583 | t = extract_command_subst (string, &si, flags|SX_NOALLOC); |
d233b485 CR |
1584 | |
1585 | CHECK_STRING_OVERRUN (i, si, slen, c); | |
1586 | ||
1587 | i = si + 1; | |
1588 | continue; | |
1589 | } | |
1590 | ||
1591 | #if defined (PROCESS_SUBSTITUTION) | |
1592 | /* Technically this should only work at the start of a word */ | |
1593 | if ((string[i] == '<' || string[i] == '>') && string[i+1] == LPAREN) | |
1594 | { | |
1595 | si = i + 2; | |
1596 | t = extract_process_subst (string, (string[i] == '<' ? "<(" : ">)"), &si, flags|SX_NOALLOC); | |
1597 | ||
1598 | CHECK_STRING_OVERRUN (i, si, slen, c); | |
1599 | ||
7117c2d2 | 1600 | i = si + 1; |
726f6388 JA |
1601 | continue; |
1602 | } | |
d233b485 | 1603 | #endif |
726f6388 | 1604 | |
495aee44 CR |
1605 | /* Pass the contents of double-quoted strings through verbatim. */ |
1606 | if (c == '"') | |
1607 | { | |
1608 | si = i + 1; | |
a0c0a00f | 1609 | i = skip_double_quoted (string, slen, si, 0); |
495aee44 CR |
1610 | /* skip_XXX_quoted leaves index one past close quote */ |
1611 | continue; | |
1612 | } | |
1613 | ||
1614 | if (c == '\'') | |
1615 | { | |
1616 | /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/ | |
ac50fbac | 1617 | if (posixly_correct && shell_compatibility_level > 42 && dolbrace_state != DOLBRACE_QUOTE && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
495aee44 CR |
1618 | ADVANCE_CHAR (string, slen, i); |
1619 | else | |
1620 | { | |
1621 | si = i + 1; | |
a0c0a00f | 1622 | i = skip_single_quoted (string, slen, si, 0); |
495aee44 CR |
1623 | } |
1624 | ||
1625 | continue; | |
1626 | } | |
7117c2d2 | 1627 | |
d233b485 CR |
1628 | #if defined (ARRAY_VARS) |
1629 | if (c == LBRACK && dolbrace_state == DOLBRACE_PARAM) | |
1630 | { | |
1631 | si = skipsubscript (string, i, 0); | |
1632 | CHECK_STRING_OVERRUN (i, si, slen, c); | |
1633 | if (string[si] == RBRACK) | |
1634 | c = string[i = si]; | |
1635 | } | |
1636 | #endif | |
1637 | ||
7117c2d2 JA |
1638 | /* move past this character, which was not special. */ |
1639 | ADVANCE_CHAR (string, slen, i); | |
495aee44 CR |
1640 | |
1641 | /* This logic must agree with parse.y:parse_matched_pair, since they | |
1642 | share the same defines. */ | |
1643 | if (dolbrace_state == DOLBRACE_PARAM && c == '%' && (i - *sindex) > 1) | |
1644 | dolbrace_state = DOLBRACE_QUOTE; | |
1645 | else if (dolbrace_state == DOLBRACE_PARAM && c == '#' && (i - *sindex) > 1) | |
1646 | dolbrace_state = DOLBRACE_QUOTE; | |
1647 | else if (dolbrace_state == DOLBRACE_PARAM && c == '/' && (i - *sindex) > 1) | |
ac50fbac | 1648 | dolbrace_state = DOLBRACE_QUOTE2; /* XXX */ |
495aee44 CR |
1649 | else if (dolbrace_state == DOLBRACE_PARAM && c == '^' && (i - *sindex) > 1) |
1650 | dolbrace_state = DOLBRACE_QUOTE; | |
1651 | else if (dolbrace_state == DOLBRACE_PARAM && c == ',' && (i - *sindex) > 1) | |
1652 | dolbrace_state = DOLBRACE_QUOTE; | |
a0c0a00f CR |
1653 | /* This is intended to handle all of the [:]op expansions and the substring/ |
1654 | length/pattern removal/pattern substitution expansions. */ | |
495aee44 CR |
1655 | else if (dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", c) != 0) |
1656 | dolbrace_state = DOLBRACE_OP; | |
1657 | else if (dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", c) == 0) | |
1658 | dolbrace_state = DOLBRACE_WORD; | |
cce855bc | 1659 | } |
726f6388 | 1660 | |
b80f6443 | 1661 | if (c == 0 && nesting_level) |
cce855bc | 1662 | { |
b80f6443 JA |
1663 | if (no_longjmp_on_fatal_error == 0) |
1664 | { /* { */ | |
b80f6443 | 1665 | last_command_exit_value = EXECUTION_FAILURE; |
ac50fbac | 1666 | report_error (_("bad substitution: no closing `%s' in %s"), "}", string); |
b80f6443 JA |
1667 | exp_jump_to_top_level (DISCARD); |
1668 | } | |
1669 | else | |
1670 | { | |
1671 | *sindex = i; | |
1672 | return ((char *)NULL); | |
1673 | } | |
726f6388 | 1674 | } |
726f6388 | 1675 | |
3185942a | 1676 | result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i); |
726f6388 JA |
1677 | *sindex = i; |
1678 | ||
ccc6cda3 | 1679 | return (result); |
726f6388 JA |
1680 | } |
1681 | ||
ccc6cda3 JA |
1682 | /* Remove backslashes which are quoting backquotes from STRING. Modifies |
1683 | STRING, and returns a pointer to it. */ | |
1684 | char * | |
1685 | de_backslash (string) | |
726f6388 | 1686 | char *string; |
ccc6cda3 | 1687 | { |
7117c2d2 JA |
1688 | register size_t slen; |
1689 | register int i, j, prev_i; | |
1690 | DECLARE_MBSTATE; | |
726f6388 | 1691 | |
7117c2d2 JA |
1692 | slen = strlen (string); |
1693 | i = j = 0; | |
1694 | ||
1695 | /* Loop copying string[i] to string[j], i >= j. */ | |
1696 | while (i < slen) | |
1697 | { | |
1698 | if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' || | |
ccc6cda3 | 1699 | string[i + 1] == '$')) |
7117c2d2 JA |
1700 | i++; |
1701 | prev_i = i; | |
1702 | ADVANCE_CHAR (string, slen, i); | |
1703 | if (j < prev_i) | |
b80f6443 | 1704 | do string[j++] = string[prev_i++]; while (prev_i < i); |
7117c2d2 | 1705 | else |
b80f6443 | 1706 | j = i; |
7117c2d2 JA |
1707 | } |
1708 | string[j] = '\0'; | |
1709 | ||
ccc6cda3 JA |
1710 | return (string); |
1711 | } | |
726f6388 | 1712 | |
ccc6cda3 | 1713 | #if 0 |
cce855bc | 1714 | /*UNUSED*/ |
ccc6cda3 JA |
1715 | /* Replace instances of \! in a string with !. */ |
1716 | void | |
1717 | unquote_bang (string) | |
1718 | char *string; | |
1719 | { | |
1720 | register int i, j; | |
1721 | register char *temp; | |
726f6388 | 1722 | |
f73dda09 | 1723 | temp = (char *)xmalloc (1 + strlen (string)); |
726f6388 | 1724 | |
ccc6cda3 JA |
1725 | for (i = 0, j = 0; (temp[j] = string[i]); i++, j++) |
1726 | { | |
1727 | if (string[i] == '\\' && string[i + 1] == '!') | |
1728 | { | |
1729 | temp[j] = '!'; | |
1730 | i++; | |
1731 | } | |
1732 | } | |
1733 | strcpy (string, temp); | |
1734 | free (temp); | |
726f6388 | 1735 | } |
ccc6cda3 | 1736 | #endif |
726f6388 | 1737 | |
a0c0a00f | 1738 | #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = oldjmp; return (x); } while (0) |
3185942a | 1739 | |
89a92869 | 1740 | /* This function assumes s[i] == open; returns with s[ret] == close; used to |
0001803f CR |
1741 | parse array subscripts. FLAGS & 1 means to not attempt to skip over |
1742 | matched pairs of quotes or backquotes, or skip word expansions; it is | |
1743 | intended to be used after expansion has been performed and during final | |
d233b485 CR |
1744 | assignment parsing (see arrayfunc.c:assign_compound_array_list()) or |
1745 | during execution by a builtin which has already undergone word expansion. */ | |
89a92869 CR |
1746 | static int |
1747 | skip_matched_pair (string, start, open, close, flags) | |
1748 | const char *string; | |
1749 | int start, open, close, flags; | |
1750 | { | |
a0c0a00f | 1751 | int i, pass_next, backq, si, c, count, oldjmp; |
89a92869 CR |
1752 | size_t slen; |
1753 | char *temp, *ss; | |
1754 | DECLARE_MBSTATE; | |
1755 | ||
1756 | slen = strlen (string + start) + start; | |
a0c0a00f | 1757 | oldjmp = no_longjmp_on_fatal_error; |
89a92869 CR |
1758 | no_longjmp_on_fatal_error = 1; |
1759 | ||
1760 | i = start + 1; /* skip over leading bracket */ | |
1761 | count = 1; | |
1762 | pass_next = backq = 0; | |
1763 | ss = (char *)string; | |
1764 | while (c = string[i]) | |
1765 | { | |
1766 | if (pass_next) | |
1767 | { | |
1768 | pass_next = 0; | |
1769 | if (c == 0) | |
1770 | CQ_RETURN(i); | |
1771 | ADVANCE_CHAR (string, slen, i); | |
1772 | continue; | |
1773 | } | |
d233b485 | 1774 | else if ((flags & 1) == 0 && c == '\\') |
89a92869 CR |
1775 | { |
1776 | pass_next = 1; | |
1777 | i++; | |
1778 | continue; | |
1779 | } | |
1780 | else if (backq) | |
1781 | { | |
1782 | if (c == '`') | |
1783 | backq = 0; | |
1784 | ADVANCE_CHAR (string, slen, i); | |
1785 | continue; | |
1786 | } | |
0001803f | 1787 | else if ((flags & 1) == 0 && c == '`') |
89a92869 CR |
1788 | { |
1789 | backq = 1; | |
1790 | i++; | |
1791 | continue; | |
1792 | } | |
0001803f | 1793 | else if ((flags & 1) == 0 && c == open) |
89a92869 CR |
1794 | { |
1795 | count++; | |
1796 | i++; | |
1797 | continue; | |
1798 | } | |
1799 | else if (c == close) | |
1800 | { | |
1801 | count--; | |
1802 | if (count == 0) | |
1803 | break; | |
1804 | i++; | |
1805 | continue; | |
1806 | } | |
0001803f | 1807 | else if ((flags & 1) == 0 && (c == '\'' || c == '"')) |
89a92869 | 1808 | { |
a0c0a00f CR |
1809 | i = (c == '\'') ? skip_single_quoted (ss, slen, ++i, 0) |
1810 | : skip_double_quoted (ss, slen, ++i, 0); | |
89a92869 CR |
1811 | /* no increment, the skip functions increment past the closing quote. */ |
1812 | } | |
0001803f | 1813 | else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE)) |
89a92869 CR |
1814 | { |
1815 | si = i + 2; | |
1816 | if (string[si] == '\0') | |
1817 | CQ_RETURN(si); | |
1818 | ||
d233b485 | 1819 | /* XXX - extract_command_subst here? */ |
89a92869 CR |
1820 | if (string[i+1] == LPAREN) |
1821 | temp = extract_delimited_string (ss, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */ | |
1822 | else | |
1823 | temp = extract_dollar_brace_string (ss, &si, 0, SX_NOALLOC); | |
a0c0a00f CR |
1824 | |
1825 | CHECK_STRING_OVERRUN (i, si, slen, c); | |
1826 | ||
89a92869 CR |
1827 | i = si; |
1828 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1829 | break; | |
1830 | i++; | |
1831 | continue; | |
1832 | } | |
1833 | else | |
1834 | ADVANCE_CHAR (string, slen, i); | |
1835 | } | |
1836 | ||
1837 | CQ_RETURN(i); | |
1838 | } | |
1839 | ||
1840 | #if defined (ARRAY_VARS) | |
d233b485 CR |
1841 | /* Flags has 1 as a reserved value, since skip_matched_pair uses it for |
1842 | skipping over quoted strings and taking the first instance of the | |
1843 | closing character. */ | |
89a92869 | 1844 | int |
0001803f | 1845 | skipsubscript (string, start, flags) |
89a92869 | 1846 | const char *string; |
0001803f | 1847 | int start, flags; |
89a92869 | 1848 | { |
0001803f | 1849 | return (skip_matched_pair (string, start, '[', ']', flags)); |
89a92869 CR |
1850 | } |
1851 | #endif | |
1852 | ||
3185942a JA |
1853 | /* Skip characters in STRING until we find a character in DELIMS, and return |
1854 | the index of that character. START is the index into string at which we | |
1855 | begin. This is similar in spirit to strpbrk, but it returns an index into | |
1856 | STRING and takes a starting index. This little piece of code knows quite | |
1857 | a lot of shell syntax. It's very similar to skip_double_quoted and other | |
1858 | functions of that ilk. */ | |
1859 | int | |
1860 | skip_to_delim (string, start, delims, flags) | |
1861 | char *string; | |
1862 | int start; | |
1863 | char *delims; | |
1864 | int flags; | |
1865 | { | |
a0c0a00f CR |
1866 | int i, pass_next, backq, dquote, si, c, oldjmp; |
1867 | int invert, skipquote, skipcmd, noprocsub, completeflag; | |
1868 | int arithexp, skipcol; | |
3185942a | 1869 | size_t slen; |
495aee44 | 1870 | char *temp, open[3]; |
3185942a JA |
1871 | DECLARE_MBSTATE; |
1872 | ||
1873 | slen = strlen (string + start) + start; | |
a0c0a00f | 1874 | oldjmp = no_longjmp_on_fatal_error; |
3185942a JA |
1875 | if (flags & SD_NOJMP) |
1876 | no_longjmp_on_fatal_error = 1; | |
1877 | invert = (flags & SD_INVERT); | |
0001803f | 1878 | skipcmd = (flags & SD_NOSKIPCMD) == 0; |
a0c0a00f CR |
1879 | noprocsub = (flags & SD_NOPROCSUB); |
1880 | completeflag = (flags & SD_COMPLETE) ? SX_COMPLETE : 0; | |
1881 | ||
1882 | arithexp = (flags & SD_ARITHEXP); | |
1883 | skipcol = 0; | |
3185942a JA |
1884 | |
1885 | i = start; | |
a0c0a00f | 1886 | pass_next = backq = dquote = 0; |
3185942a JA |
1887 | while (c = string[i]) |
1888 | { | |
0001803f CR |
1889 | /* If this is non-zero, we should not let quote characters be delimiters |
1890 | and the current character is a single or double quote. We should not | |
1891 | test whether or not it's a delimiter until after we skip single- or | |
1892 | double-quoted strings. */ | |
1893 | skipquote = ((flags & SD_NOQUOTEDELIM) && (c == '\'' || c =='"')); | |
3185942a JA |
1894 | if (pass_next) |
1895 | { | |
1896 | pass_next = 0; | |
1897 | if (c == 0) | |
1898 | CQ_RETURN(i); | |
1899 | ADVANCE_CHAR (string, slen, i); | |
1900 | continue; | |
1901 | } | |
1902 | else if (c == '\\') | |
1903 | { | |
1904 | pass_next = 1; | |
1905 | i++; | |
1906 | continue; | |
1907 | } | |
1908 | else if (backq) | |
1909 | { | |
1910 | if (c == '`') | |
1911 | backq = 0; | |
1912 | ADVANCE_CHAR (string, slen, i); | |
1913 | continue; | |
1914 | } | |
1915 | else if (c == '`') | |
1916 | { | |
1917 | backq = 1; | |
1918 | i++; | |
1919 | continue; | |
1920 | } | |
a0c0a00f | 1921 | else if (arithexp && skipcol && c == ':') |
3185942a | 1922 | { |
a0c0a00f CR |
1923 | skipcol--; |
1924 | i++; | |
1925 | continue; | |
3185942a | 1926 | } |
a0c0a00f CR |
1927 | else if (arithexp && c == '?') |
1928 | { | |
1929 | skipcol++; | |
1930 | i++; | |
1931 | continue; | |
1932 | } | |
1933 | else if (skipquote == 0 && invert == 0 && member (c, delims)) | |
1934 | break; | |
1935 | /* the usual case is to use skip_xxx_quoted, but we don't skip over double | |
1936 | quoted strings when looking for the history expansion character as a | |
1937 | delimiter. */ | |
1938 | /* special case for programmable completion which takes place before | |
1939 | parser converts backslash-escaped single quotes between $'...' to | |
1940 | `regular' single-quoted strings. */ | |
1941 | else if (completeflag && i > 0 && string[i-1] == '$' && c == '\'') | |
1942 | i = skip_single_quoted (string, slen, ++i, SX_COMPLETE); | |
1943 | else if (c == '\'') | |
1944 | i = skip_single_quoted (string, slen, ++i, 0); | |
1945 | else if (c == '"') | |
1946 | i = skip_double_quoted (string, slen, ++i, completeflag); | |
1947 | else if (c == LPAREN && arithexp) | |
1948 | { | |
1949 | si = i + 1; | |
1950 | if (string[si] == '\0') | |
1951 | CQ_RETURN(si); | |
1952 | ||
1953 | temp = extract_delimited_string (string, &si, "(", "(", ")", SX_NOALLOC); /* ) */ | |
1954 | i = si; | |
1955 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1956 | break; | |
1957 | i++; | |
1958 | continue; | |
1959 | } | |
0001803f | 1960 | else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE)) |
3185942a JA |
1961 | { |
1962 | si = i + 2; | |
1963 | if (string[si] == '\0') | |
1964 | CQ_RETURN(si); | |
1965 | ||
1966 | if (string[i+1] == LPAREN) | |
1967 | temp = extract_delimited_string (string, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */ | |
1968 | else | |
1969 | temp = extract_dollar_brace_string (string, &si, 0, SX_NOALLOC); | |
d233b485 | 1970 | CHECK_STRING_OVERRUN (i, si, slen, c); |
3185942a JA |
1971 | i = si; |
1972 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
1973 | break; | |
1974 | i++; | |
1975 | continue; | |
1976 | } | |
0001803f | 1977 | #if defined (PROCESS_SUBSTITUTION) |
a0c0a00f | 1978 | else if (skipcmd && noprocsub == 0 && (c == '<' || c == '>') && string[i+1] == LPAREN) |
0001803f CR |
1979 | { |
1980 | si = i + 2; | |
1981 | if (string[si] == '\0') | |
1982 | CQ_RETURN(si); | |
d233b485 | 1983 | |
a0c0a00f | 1984 | temp = extract_delimited_string (string, &si, (c == '<') ? "<(" : ">(", "(", ")", SX_COMMAND|SX_NOALLOC); /* )) */ |
d233b485 | 1985 | CHECK_STRING_OVERRUN (i, si, slen, c); |
0001803f CR |
1986 | i = si; |
1987 | if (string[i] == '\0') | |
1988 | break; | |
1989 | i++; | |
1990 | continue; | |
1991 | } | |
1992 | #endif /* PROCESS_SUBSTITUTION */ | |
495aee44 CR |
1993 | #if defined (EXTENDED_GLOB) |
1994 | else if ((flags & SD_EXTGLOB) && extended_glob && string[i+1] == LPAREN && member (c, "?*+!@")) | |
1995 | { | |
1996 | si = i + 2; | |
1997 | if (string[si] == '\0') | |
1998 | CQ_RETURN(si); | |
1999 | ||
2000 | open[0] = c; | |
2001 | open[1] = LPAREN; | |
2002 | open[2] = '\0'; | |
2003 | temp = extract_delimited_string (string, &si, open, "(", ")", SX_NOALLOC); /* ) */ | |
2004 | ||
d233b485 | 2005 | CHECK_STRING_OVERRUN (i, si, slen, c); |
495aee44 CR |
2006 | i = si; |
2007 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
2008 | break; | |
2009 | i++; | |
2010 | continue; | |
2011 | } | |
2012 | #endif | |
ac50fbac CR |
2013 | else if ((flags & SD_GLOB) && c == LBRACK) |
2014 | { | |
2015 | si = i + 1; | |
2016 | if (string[si] == '\0') | |
2017 | CQ_RETURN(si); | |
2018 | ||
2019 | temp = extract_delimited_string (string, &si, "[", "[", "]", SX_NOALLOC); /* ] */ | |
2020 | ||
2021 | i = si; | |
2022 | if (string[i] == '\0') /* don't increment i past EOS in loop */ | |
2023 | break; | |
2024 | i++; | |
2025 | continue; | |
2026 | } | |
0001803f | 2027 | else if ((skipquote || invert) && (member (c, delims) == 0)) |
3185942a JA |
2028 | break; |
2029 | else | |
2030 | ADVANCE_CHAR (string, slen, i); | |
2031 | } | |
2032 | ||
2033 | CQ_RETURN(i); | |
2034 | } | |
2035 | ||
a0c0a00f CR |
2036 | #if defined (BANG_HISTORY) |
2037 | /* Skip to the history expansion character (delims[0]), paying attention to | |
2038 | quoted strings and command and process substitution. This is a stripped- | |
2039 | down version of skip_to_delims. The essential difference is that this | |
2040 | resets the quoting state when starting a command substitution */ | |
2041 | int | |
2042 | skip_to_histexp (string, start, delims, flags) | |
2043 | char *string; | |
2044 | int start; | |
2045 | char *delims; | |
2046 | int flags; | |
2047 | { | |
d233b485 | 2048 | int i, pass_next, backq, dquote, c, oldjmp; |
a0c0a00f CR |
2049 | int histexp_comsub, histexp_backq, old_dquote; |
2050 | size_t slen; | |
a0c0a00f CR |
2051 | DECLARE_MBSTATE; |
2052 | ||
2053 | slen = strlen (string + start) + start; | |
2054 | oldjmp = no_longjmp_on_fatal_error; | |
2055 | if (flags & SD_NOJMP) | |
2056 | no_longjmp_on_fatal_error = 1; | |
2057 | ||
2058 | histexp_comsub = histexp_backq = old_dquote = 0; | |
2059 | ||
2060 | i = start; | |
2061 | pass_next = backq = dquote = 0; | |
2062 | while (c = string[i]) | |
2063 | { | |
2064 | if (pass_next) | |
2065 | { | |
2066 | pass_next = 0; | |
2067 | if (c == 0) | |
2068 | CQ_RETURN(i); | |
2069 | ADVANCE_CHAR (string, slen, i); | |
2070 | continue; | |
2071 | } | |
2072 | else if (c == '\\') | |
2073 | { | |
2074 | pass_next = 1; | |
2075 | i++; | |
2076 | continue; | |
2077 | } | |
2078 | else if (backq && c == '`') | |
2079 | { | |
2080 | backq = 0; | |
2081 | histexp_backq--; | |
2082 | dquote = old_dquote; | |
2083 | i++; | |
2084 | continue; | |
2085 | } | |
2086 | else if (c == '`') | |
2087 | { | |
2088 | backq = 1; | |
2089 | histexp_backq++; | |
2090 | old_dquote = dquote; /* simple - one level for now */ | |
2091 | dquote = 0; | |
2092 | i++; | |
2093 | continue; | |
2094 | } | |
2095 | /* When in double quotes, act as if the double quote is a member of | |
2096 | history_no_expand_chars, like the history library does */ | |
2097 | else if (dquote && c == delims[0] && string[i+1] == '"') | |
2098 | { | |
2099 | i++; | |
2100 | continue; | |
2101 | } | |
2102 | else if (c == delims[0]) | |
2103 | break; | |
2104 | /* the usual case is to use skip_xxx_quoted, but we don't skip over double | |
2105 | quoted strings when looking for the history expansion character as a | |
2106 | delimiter. */ | |
2107 | else if (dquote && c == '\'') | |
2108 | { | |
2109 | i++; | |
2110 | continue; | |
2111 | } | |
2112 | else if (c == '\'') | |
2113 | i = skip_single_quoted (string, slen, ++i, 0); | |
2114 | /* The posixly_correct test makes posix-mode shells allow double quotes | |
2115 | to quote the history expansion character */ | |
2116 | else if (posixly_correct == 0 && c == '"') | |
2117 | { | |
2118 | dquote = 1 - dquote; | |
2119 | i++; | |
2120 | continue; | |
2121 | } | |
2122 | else if (c == '"') | |
2123 | i = skip_double_quoted (string, slen, ++i, 0); | |
2124 | #if defined (PROCESS_SUBSTITUTION) | |
2125 | else if ((c == '$' || c == '<' || c == '>') && string[i+1] == LPAREN && string[i+2] != LPAREN) | |
2126 | #else | |
2127 | else if (c == '$' && string[i+1] == LPAREN && string[i+2] != LPAREN) | |
2128 | #endif | |
2129 | { | |
2130 | if (string[i+2] == '\0') | |
2131 | CQ_RETURN(i+2); | |
2132 | i += 2; | |
2133 | histexp_comsub++; | |
2134 | old_dquote = dquote; | |
2135 | dquote = 0; | |
2136 | } | |
2137 | else if (histexp_comsub && c == RPAREN) | |
2138 | { | |
2139 | histexp_comsub--; | |
2140 | dquote = old_dquote; | |
2141 | i++; | |
2142 | continue; | |
2143 | } | |
2144 | else if (backq) /* placeholder */ | |
2145 | { | |
2146 | ADVANCE_CHAR (string, slen, i); | |
2147 | continue; | |
2148 | } | |
2149 | else | |
2150 | ADVANCE_CHAR (string, slen, i); | |
2151 | } | |
2152 | ||
2153 | CQ_RETURN(i); | |
2154 | } | |
2155 | #endif /* BANG_HISTORY */ | |
2156 | ||
ccc6cda3 | 2157 | #if defined (READLINE) |
726f6388 JA |
2158 | /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is |
2159 | an unclosed quoted string), or if the character at EINDEX is quoted | |
28ef6c31 | 2160 | by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various |
b72432fd | 2161 | single and double-quoted string parsing functions should not return an |
7117c2d2 JA |
2162 | error if there are unclosed quotes or braces. The characters that this |
2163 | recognizes need to be the same as the contents of | |
2164 | rl_completer_quote_characters. */ | |
b72432fd | 2165 | |
726f6388 JA |
2166 | int |
2167 | char_is_quoted (string, eindex) | |
2168 | char *string; | |
2169 | int eindex; | |
2170 | { | |
a0c0a00f | 2171 | int i, pass_next, c, oldjmp; |
7117c2d2 JA |
2172 | size_t slen; |
2173 | DECLARE_MBSTATE; | |
726f6388 | 2174 | |
7117c2d2 | 2175 | slen = strlen (string); |
a0c0a00f | 2176 | oldjmp = no_longjmp_on_fatal_error; |
28ef6c31 | 2177 | no_longjmp_on_fatal_error = 1; |
7117c2d2 JA |
2178 | i = pass_next = 0; |
2179 | while (i <= eindex) | |
726f6388 | 2180 | { |
7117c2d2 JA |
2181 | c = string[i]; |
2182 | ||
726f6388 JA |
2183 | if (pass_next) |
2184 | { | |
2185 | pass_next = 0; | |
2186 | if (i >= eindex) /* XXX was if (i >= eindex - 1) */ | |
b72432fd | 2187 | CQ_RETURN(1); |
7117c2d2 | 2188 | ADVANCE_CHAR (string, slen, i); |
726f6388 JA |
2189 | continue; |
2190 | } | |
7117c2d2 | 2191 | else if (c == '\\') |
ccc6cda3 JA |
2192 | { |
2193 | pass_next = 1; | |
7117c2d2 | 2194 | i++; |
ccc6cda3 JA |
2195 | continue; |
2196 | } | |
a0c0a00f CR |
2197 | else if (c == '$' && string[i+1] == '\'' && string[i+2]) |
2198 | { | |
2199 | i += 2; | |
2200 | i = skip_single_quoted (string, slen, i, SX_COMPLETE); | |
2201 | if (i > eindex) | |
2202 | CQ_RETURN (i); | |
2203 | } | |
7117c2d2 JA |
2204 | else if (c == '\'' || c == '"') |
2205 | { | |
a0c0a00f CR |
2206 | i = (c == '\'') ? skip_single_quoted (string, slen, ++i, 0) |
2207 | : skip_double_quoted (string, slen, ++i, SX_COMPLETE); | |
7117c2d2 JA |
2208 | if (i > eindex) |
2209 | CQ_RETURN(1); | |
2210 | /* no increment, the skip_xxx functions go one past end */ | |
2211 | } | |
2212 | else | |
2213 | ADVANCE_CHAR (string, slen, i); | |
726f6388 | 2214 | } |
7117c2d2 | 2215 | |
b72432fd | 2216 | CQ_RETURN(0); |
726f6388 JA |
2217 | } |
2218 | ||
726f6388 JA |
2219 | int |
2220 | unclosed_pair (string, eindex, openstr) | |
2221 | char *string; | |
2222 | int eindex; | |
2223 | char *openstr; | |
2224 | { | |
ccc6cda3 | 2225 | int i, pass_next, openc, olen; |
7117c2d2 JA |
2226 | size_t slen; |
2227 | DECLARE_MBSTATE; | |
726f6388 | 2228 | |
7117c2d2 | 2229 | slen = strlen (string); |
726f6388 | 2230 | olen = strlen (openstr); |
7117c2d2 JA |
2231 | i = pass_next = openc = 0; |
2232 | while (i <= eindex) | |
726f6388 JA |
2233 | { |
2234 | if (pass_next) | |
2235 | { | |
2236 | pass_next = 0; | |
2237 | if (i >= eindex) /* XXX was if (i >= eindex - 1) */ | |
2238 | return 0; | |
7117c2d2 JA |
2239 | ADVANCE_CHAR (string, slen, i); |
2240 | continue; | |
2241 | } | |
2242 | else if (string[i] == '\\') | |
2243 | { | |
2244 | pass_next = 1; | |
2245 | i++; | |
726f6388 JA |
2246 | continue; |
2247 | } | |
2248 | else if (STREQN (string + i, openstr, olen)) | |
2249 | { | |
2250 | openc = 1 - openc; | |
7117c2d2 | 2251 | i += olen; |
726f6388 | 2252 | } |
a0c0a00f | 2253 | /* XXX - may want to handle $'...' specially here */ |
ccc6cda3 | 2254 | else if (string[i] == '\'' || string[i] == '"') |
726f6388 | 2255 | { |
a0c0a00f CR |
2256 | i = (string[i] == '\'') ? skip_single_quoted (string, slen, i, 0) |
2257 | : skip_double_quoted (string, slen, i, SX_COMPLETE); | |
726f6388 JA |
2258 | if (i > eindex) |
2259 | return 0; | |
2260 | } | |
7117c2d2 JA |
2261 | else |
2262 | ADVANCE_CHAR (string, slen, i); | |
726f6388 JA |
2263 | } |
2264 | return (openc); | |
2265 | } | |
bb70624e | 2266 | |
bb70624e JA |
2267 | /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the |
2268 | individual words. If DELIMS is NULL, the current value of $IFS is used | |
b80f6443 JA |
2269 | to split the string, and the function follows the shell field splitting |
2270 | rules. SENTINEL is an index to look for. NWP, if non-NULL, | |
bb70624e JA |
2271 | gets the number of words in the returned list. CWP, if non-NULL, gets |
2272 | the index of the word containing SENTINEL. Non-whitespace chars in | |
d233b485 | 2273 | DELIMS delimit separate fields. This is used by programmable completion. */ |
bb70624e | 2274 | WORD_LIST * |
0001803f | 2275 | split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp) |
bb70624e JA |
2276 | char *string; |
2277 | int slen; | |
2278 | char *delims; | |
0001803f | 2279 | int sentinel, flags; |
bb70624e JA |
2280 | int *nwp, *cwp; |
2281 | { | |
0001803f | 2282 | int ts, te, i, nw, cw, ifs_split, dflags; |
f73dda09 | 2283 | char *token, *d, *d2; |
bb70624e JA |
2284 | WORD_LIST *ret, *tl; |
2285 | ||
2286 | if (string == 0 || *string == '\0') | |
2287 | { | |
2288 | if (nwp) | |
2289 | *nwp = 0; | |
2290 | if (cwp) | |
2291 | *cwp = 0; | |
2292 | return ((WORD_LIST *)NULL); | |
2293 | } | |
2294 | ||
7117c2d2 | 2295 | d = (delims == 0) ? ifs_value : delims; |
b80f6443 | 2296 | ifs_split = delims == 0; |
bb70624e JA |
2297 | |
2298 | /* Make d2 the non-whitespace characters in delims */ | |
2299 | d2 = 0; | |
2300 | if (delims) | |
2301 | { | |
95732b49 JA |
2302 | size_t slength; |
2303 | #if defined (HANDLE_MULTIBYTE) | |
2304 | size_t mblength = 1; | |
2305 | #endif | |
2306 | DECLARE_MBSTATE; | |
2307 | ||
2308 | slength = strlen (delims); | |
2309 | d2 = (char *)xmalloc (slength + 1); | |
2310 | i = ts = 0; | |
2311 | while (delims[i]) | |
bb70624e | 2312 | { |
95732b49 | 2313 | #if defined (HANDLE_MULTIBYTE) |
0628567a JA |
2314 | mbstate_t state_bak; |
2315 | state_bak = state; | |
95732b49 JA |
2316 | mblength = MBRLEN (delims + i, slength, &state); |
2317 | if (MB_INVALIDCH (mblength)) | |
2318 | state = state_bak; | |
2319 | else if (mblength > 1) | |
2320 | { | |
2321 | memcpy (d2 + ts, delims + i, mblength); | |
2322 | ts += mblength; | |
2323 | i += mblength; | |
2324 | slength -= mblength; | |
2325 | continue; | |
2326 | } | |
2327 | #endif | |
2328 | if (whitespace (delims[i]) == 0) | |
bb70624e | 2329 | d2[ts++] = delims[i]; |
95732b49 JA |
2330 | |
2331 | i++; | |
2332 | slength--; | |
bb70624e JA |
2333 | } |
2334 | d2[ts] = '\0'; | |
2335 | } | |
2336 | ||
2337 | ret = (WORD_LIST *)NULL; | |
2338 | ||
0001803f | 2339 | /* Remove sequences of whitespace characters at the start of the string, as |
b80f6443 JA |
2340 | long as those characters are delimiters. */ |
2341 | for (i = 0; member (string[i], d) && spctabnl (string[i]); i++) | |
bb70624e JA |
2342 | ; |
2343 | if (string[i] == '\0') | |
a0c0a00f CR |
2344 | { |
2345 | FREE (d2); | |
2346 | return (ret); | |
2347 | } | |
bb70624e JA |
2348 | |
2349 | ts = i; | |
2350 | nw = 0; | |
2351 | cw = -1; | |
0001803f | 2352 | dflags = flags|SD_NOJMP; |
bb70624e JA |
2353 | while (1) |
2354 | { | |
0001803f | 2355 | te = skip_to_delim (string, ts, d, dflags); |
bb70624e JA |
2356 | |
2357 | /* If we have a non-whitespace delimiter character, use it to make a | |
2358 | separate field. This is just about what $IFS splitting does and | |
2359 | is closer to the behavior of the shell parser. */ | |
28ef6c31 | 2360 | if (ts == te && d2 && member (string[ts], d2)) |
bb70624e JA |
2361 | { |
2362 | te = ts + 1; | |
b80f6443 JA |
2363 | /* If we're using IFS splitting, the non-whitespace delimiter char |
2364 | and any additional IFS whitespace delimits a field. */ | |
2365 | if (ifs_split) | |
d233b485 | 2366 | while (member (string[te], d) && spctabnl (string[te]) && ((flags&SD_NOQUOTEDELIM) == 0 || (string[te] != '\'' && string[te] != '"'))) |
b80f6443 JA |
2367 | te++; |
2368 | else | |
d233b485 | 2369 | while (member (string[te], d2) && ((flags&SD_NOQUOTEDELIM) == 0 || (string[te] != '\'' && string[te] != '"'))) |
b80f6443 | 2370 | te++; |
bb70624e JA |
2371 | } |
2372 | ||
2373 | token = substring (string, ts, te); | |
2374 | ||
712f80b0 | 2375 | ret = add_string_to_list (token, ret); /* XXX */ |
bb70624e JA |
2376 | free (token); |
2377 | nw++; | |
2378 | ||
2379 | if (sentinel >= ts && sentinel <= te) | |
2380 | cw = nw; | |
2381 | ||
2382 | /* If the cursor is at whitespace just before word start, set the | |
28ef6c31 | 2383 | sentinel word to the current word. */ |
bb70624e JA |
2384 | if (cwp && cw == -1 && sentinel == ts-1) |
2385 | cw = nw; | |
2386 | ||
2387 | /* If the cursor is at whitespace between two words, make a new, empty | |
28ef6c31 JA |
2388 | word, add it before (well, after, since the list is in reverse order) |
2389 | the word we just added, and set the current word to that one. */ | |
bb70624e | 2390 | if (cwp && cw == -1 && sentinel < ts) |
28ef6c31 | 2391 | { |
7117c2d2 | 2392 | tl = make_word_list (make_word (""), ret->next); |
28ef6c31 JA |
2393 | ret->next = tl; |
2394 | cw = nw; | |
2395 | nw++; | |
2396 | } | |
bb70624e JA |
2397 | |
2398 | if (string[te] == 0) | |
2399 | break; | |
2400 | ||
b80f6443 | 2401 | i = te; |
d233b485 CR |
2402 | /* XXX - honor SD_NOQUOTEDELIM here */ |
2403 | while (member (string[i], d) && (ifs_split || spctabnl(string[i])) && ((flags&SD_NOQUOTEDELIM) == 0 || (string[te] != '\'' && string[te] != '"'))) | |
bb70624e JA |
2404 | i++; |
2405 | ||
2406 | if (string[i]) | |
2407 | ts = i; | |
2408 | else | |
2409 | break; | |
2410 | } | |
2411 | ||
2412 | /* Special case for SENTINEL at the end of STRING. If we haven't found | |
2413 | the word containing SENTINEL yet, and the index we're looking for is at | |
0001803f CR |
2414 | the end of STRING (or past the end of the previously-found token, |
2415 | possible if the end of the line is composed solely of IFS whitespace) | |
2416 | add an additional null argument and set the current word pointer to that. */ | |
2417 | if (cwp && cw == -1 && (sentinel >= slen || sentinel >= te)) | |
bb70624e JA |
2418 | { |
2419 | if (whitespace (string[sentinel - 1])) | |
28ef6c31 JA |
2420 | { |
2421 | token = ""; | |
2422 | ret = add_string_to_list (token, ret); | |
2423 | nw++; | |
2424 | } | |
bb70624e JA |
2425 | cw = nw; |
2426 | } | |
2427 | ||
2428 | if (nwp) | |
2429 | *nwp = nw; | |
2430 | if (cwp) | |
2431 | *cwp = cw; | |
2432 | ||
ac50fbac CR |
2433 | FREE (d2); |
2434 | ||
bb70624e JA |
2435 | return (REVERSE_LIST (ret, WORD_LIST *)); |
2436 | } | |
726f6388 JA |
2437 | #endif /* READLINE */ |
2438 | ||
ccc6cda3 JA |
2439 | #if 0 |
2440 | /* UNUSED */ | |
726f6388 JA |
2441 | /* Extract the name of the variable to bind to from the assignment string. */ |
2442 | char * | |
2443 | assignment_name (string) | |
2444 | char *string; | |
2445 | { | |
ccc6cda3 | 2446 | int offset; |
726f6388 JA |
2447 | char *temp; |
2448 | ||
b80f6443 | 2449 | offset = assignment (string, 0); |
ccc6cda3 | 2450 | if (offset == 0) |
726f6388 | 2451 | return (char *)NULL; |
bb70624e | 2452 | temp = substring (string, 0, offset); |
726f6388 JA |
2453 | return (temp); |
2454 | } | |
ccc6cda3 | 2455 | #endif |
726f6388 | 2456 | |
cce855bc JA |
2457 | /* **************************************************************** */ |
2458 | /* */ | |
2459 | /* Functions to convert strings to WORD_LISTs and vice versa */ | |
2460 | /* */ | |
2461 | /* **************************************************************** */ | |
2462 | ||
726f6388 JA |
2463 | /* Return a single string of all the words in LIST. SEP is the separator |
2464 | to put between individual elements of LIST in the output string. */ | |
7117c2d2 | 2465 | char * |
726f6388 JA |
2466 | string_list_internal (list, sep) |
2467 | WORD_LIST *list; | |
2468 | char *sep; | |
2469 | { | |
2470 | register WORD_LIST *t; | |
2471 | char *result, *r; | |
a0c0a00f | 2472 | size_t word_len, sep_len, result_size; |
726f6388 | 2473 | |
ccc6cda3 | 2474 | if (list == 0) |
726f6388 JA |
2475 | return ((char *)NULL); |
2476 | ||
b80f6443 JA |
2477 | /* Short-circuit quickly if we don't need to separate anything. */ |
2478 | if (list->next == 0) | |
2479 | return (savestring (list->word->word)); | |
2480 | ||
726f6388 JA |
2481 | /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */ |
2482 | sep_len = STRLEN (sep); | |
2483 | result_size = 0; | |
2484 | ||
2485 | for (t = list; t; t = t->next) | |
2486 | { | |
2487 | if (t != list) | |
2488 | result_size += sep_len; | |
2489 | result_size += strlen (t->word->word); | |
2490 | } | |
2491 | ||
f73dda09 | 2492 | r = result = (char *)xmalloc (result_size + 1); |
726f6388 JA |
2493 | |
2494 | for (t = list; t; t = t->next) | |
2495 | { | |
2496 | if (t != list && sep_len) | |
2497 | { | |
ccc6cda3 JA |
2498 | if (sep_len > 1) |
2499 | { | |
2500 | FASTCOPY (sep, r, sep_len); | |
2501 | r += sep_len; | |
2502 | } | |
2503 | else | |
2504 | *r++ = sep[0]; | |
726f6388 JA |
2505 | } |
2506 | ||
2507 | word_len = strlen (t->word->word); | |
2508 | FASTCOPY (t->word->word, r, word_len); | |
2509 | r += word_len; | |
2510 | } | |
2511 | ||
ccc6cda3 | 2512 | *r = '\0'; |
726f6388 JA |
2513 | return (result); |
2514 | } | |
2515 | ||
2516 | /* Return a single string of all the words present in LIST, separating | |
2517 | each word with a space. */ | |
2518 | char * | |
2519 | string_list (list) | |
2520 | WORD_LIST *list; | |
2521 | { | |
2522 | return (string_list_internal (list, " ")); | |
2523 | } | |
2524 | ||
3185942a JA |
2525 | /* An external interface that can be used by the rest of the shell to |
2526 | obtain a string containing the first character in $IFS. Handles all | |
2527 | the multibyte complications. If LENP is non-null, it is set to the | |
2528 | length of the returned string. */ | |
2529 | char * | |
2530 | ifs_firstchar (lenp) | |
2531 | int *lenp; | |
2532 | { | |
2533 | char *ret; | |
2534 | int len; | |
2535 | ||
2536 | ret = xmalloc (MB_LEN_MAX + 1); | |
2537 | #if defined (HANDLE_MULTIBYTE) | |
2538 | if (ifs_firstc_len == 1) | |
2539 | { | |
2540 | ret[0] = ifs_firstc[0]; | |
2541 | ret[1] = '\0'; | |
2542 | len = ret[0] ? 1 : 0; | |
2543 | } | |
2544 | else | |
2545 | { | |
2546 | memcpy (ret, ifs_firstc, ifs_firstc_len); | |
2547 | ret[len = ifs_firstc_len] = '\0'; | |
2548 | } | |
2549 | #else | |
2550 | ret[0] = ifs_firstc; | |
2551 | ret[1] = '\0'; | |
2552 | len = ret[0] ? 0 : 1; | |
2553 | #endif | |
2554 | ||
2555 | if (lenp) | |
2556 | *lenp = len; | |
2557 | ||
2558 | return ret; | |
2559 | } | |
2560 | ||
726f6388 JA |
2561 | /* Return a single string of all the words present in LIST, obeying the |
2562 | quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the | |
2563 | expansion [of $*] appears within a double quoted string, it expands | |
2564 | to a single field with the value of each parameter separated by the | |
2565 | first character of the IFS variable, or by a <space> if IFS is unset." */ | |
d233b485 CR |
2566 | /* Posix interpretation 888 changes this when IFS is null by specifying |
2567 | that when unquoted, this expands to separate arguments */ | |
f73dda09 | 2568 | char * |
d233b485 | 2569 | string_list_dollar_star (list, quoted, flags) |
726f6388 | 2570 | WORD_LIST *list; |
d233b485 | 2571 | int quoted, flags; |
726f6388 | 2572 | { |
0628567a | 2573 | char *ret; |
95732b49 | 2574 | #if defined (HANDLE_MULTIBYTE) |
0628567a | 2575 | # if defined (__GNUC__) |
95732b49 | 2576 | char sep[MB_CUR_MAX + 1]; |
0628567a JA |
2577 | # else |
2578 | char *sep = 0; | |
2579 | # endif | |
95732b49 | 2580 | #else |
7117c2d2 | 2581 | char sep[2]; |
95732b49 | 2582 | #endif |
726f6388 | 2583 | |
95732b49 | 2584 | #if defined (HANDLE_MULTIBYTE) |
0628567a JA |
2585 | # if !defined (__GNUC__) |
2586 | sep = (char *)xmalloc (MB_CUR_MAX + 1); | |
2587 | # endif /* !__GNUC__ */ | |
95732b49 JA |
2588 | if (ifs_firstc_len == 1) |
2589 | { | |
2590 | sep[0] = ifs_firstc[0]; | |
2591 | sep[1] = '\0'; | |
2592 | } | |
2593 | else | |
2594 | { | |
2595 | memcpy (sep, ifs_firstc, ifs_firstc_len); | |
2596 | sep[ifs_firstc_len] = '\0'; | |
2597 | } | |
2598 | #else | |
7117c2d2 | 2599 | sep[0] = ifs_firstc; |
726f6388 | 2600 | sep[1] = '\0'; |
95732b49 | 2601 | #endif |
726f6388 | 2602 | |
0628567a JA |
2603 | ret = string_list_internal (list, sep); |
2604 | #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__) | |
2605 | free (sep); | |
2606 | #endif | |
2607 | return ret; | |
726f6388 JA |
2608 | } |
2609 | ||
cce855bc JA |
2610 | /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
2611 | is non-zero, the $@ appears within double quotes, and we should quote | |
2612 | the list before converting it into a string. If IFS is unset, and the | |
2613 | word is not quoted, we just need to quote CTLESC and CTLNUL characters | |
2614 | in the words in the list, because the default value of $IFS is | |
2615 | <space><tab><newline>, IFS characters in the words in the list should | |
2616 | also be split. If IFS is null, and the word is not quoted, we need | |
2617 | to quote the words in the list to preserve the positional parameters | |
a0c0a00f CR |
2618 | exactly. |
2619 | Valid values for the FLAGS argument are the PF_ flags in command.h, | |
2620 | the only one we care about is PF_ASSIGNRHS. $@ is supposed to expand | |
2621 | to the positional parameters separated by spaces no matter what IFS is | |
2622 | set to if in a context where word splitting is not performed. The only | |
2623 | one that we didn't handle before is assignment statement arguments to | |
2624 | declaration builtins like `declare'. */ | |
f73dda09 | 2625 | char * |
a0c0a00f | 2626 | string_list_dollar_at (list, quoted, flags) |
cce855bc JA |
2627 | WORD_LIST *list; |
2628 | int quoted; | |
a0c0a00f | 2629 | int flags; |
cce855bc | 2630 | { |
95732b49 JA |
2631 | char *ifs, *ret; |
2632 | #if defined (HANDLE_MULTIBYTE) | |
0628567a | 2633 | # if defined (__GNUC__) |
95732b49 | 2634 | char sep[MB_CUR_MAX + 1]; |
0628567a JA |
2635 | # else |
2636 | char *sep = 0; | |
2637 | # endif /* !__GNUC__ */ | |
95732b49 JA |
2638 | #else |
2639 | char sep[2]; | |
2640 | #endif | |
cce855bc JA |
2641 | WORD_LIST *tlist; |
2642 | ||
7117c2d2 JA |
2643 | /* XXX this could just be ifs = ifs_value; */ |
2644 | ifs = ifs_var ? value_cell (ifs_var) : (char *)0; | |
cce855bc | 2645 | |
95732b49 | 2646 | #if defined (HANDLE_MULTIBYTE) |
0628567a JA |
2647 | # if !defined (__GNUC__) |
2648 | sep = (char *)xmalloc (MB_CUR_MAX + 1); | |
2649 | # endif /* !__GNUC__ */ | |
d233b485 CR |
2650 | /* XXX - testing PF_ASSIGNRHS to make sure positional parameters are |
2651 | separated with a space even when word splitting will not occur. */ | |
a0c0a00f CR |
2652 | if (flags & PF_ASSIGNRHS) |
2653 | { | |
2654 | sep[0] = ' '; | |
2655 | sep[1] = '\0'; | |
2656 | } | |
2657 | else if (ifs && *ifs) | |
95732b49 JA |
2658 | { |
2659 | if (ifs_firstc_len == 1) | |
2660 | { | |
2661 | sep[0] = ifs_firstc[0]; | |
2662 | sep[1] = '\0'; | |
2663 | } | |
2664 | else | |
2665 | { | |
2666 | memcpy (sep, ifs_firstc, ifs_firstc_len); | |
2667 | sep[ifs_firstc_len] = '\0'; | |
2668 | } | |
2669 | } | |
2670 | else | |
2671 | { | |
2672 | sep[0] = ' '; | |
2673 | sep[1] = '\0'; | |
2674 | } | |
d233b485 CR |
2675 | #else /* !HANDLE_MULTIBYTE */ |
2676 | /* XXX - PF_ASSIGNRHS means no word splitting, so we want positional | |
2677 | parameters separated by a space. */ | |
a0c0a00f | 2678 | sep[0] = ((flags & PF_ASSIGNRHS) || ifs == 0 || *ifs == 0) ? ' ' : *ifs; |
cce855bc | 2679 | sep[1] = '\0'; |
d233b485 | 2680 | #endif /* !HANDLE_MULTIBYTE */ |
cce855bc | 2681 | |
f1be666c JA |
2682 | /* XXX -- why call quote_list if ifs == 0? we can get away without doing |
2683 | it now that quote_escapes quotes spaces */ | |
0001803f | 2684 | tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE)) |
cce855bc JA |
2685 | ? quote_list (list) |
2686 | : list_quote_escapes (list); | |
0628567a JA |
2687 | |
2688 | ret = string_list_internal (tlist, sep); | |
2689 | #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__) | |
2690 | free (sep); | |
2691 | #endif | |
2692 | return ret; | |
cce855bc JA |
2693 | } |
2694 | ||
ac50fbac | 2695 | /* Turn the positional parameters into a string, understanding quoting and |
3185942a JA |
2696 | the various subtleties of using the first character of $IFS as the |
2697 | separator. Calls string_list_dollar_at, string_list_dollar_star, and | |
2698 | string_list as appropriate. */ | |
712f80b0 CR |
2699 | /* This needs to fully understand the additional contexts where word |
2700 | splitting does not occur (W_ASSIGNRHS, etc.) */ | |
3185942a | 2701 | char * |
712f80b0 | 2702 | string_list_pos_params (pchar, list, quoted, pflags) |
3185942a JA |
2703 | int pchar; |
2704 | WORD_LIST *list; | |
712f80b0 | 2705 | int quoted, pflags; |
3185942a JA |
2706 | { |
2707 | char *ret; | |
2708 | WORD_LIST *tlist; | |
2709 | ||
2710 | if (pchar == '*' && (quoted & Q_DOUBLE_QUOTES)) | |
2711 | { | |
2712 | tlist = quote_list (list); | |
2713 | word_list_remove_quoted_nulls (tlist); | |
d233b485 | 2714 | ret = string_list_dollar_star (tlist, 0, 0); |
3185942a JA |
2715 | } |
2716 | else if (pchar == '*' && (quoted & Q_HERE_DOCUMENT)) | |
2717 | { | |
2718 | tlist = quote_list (list); | |
2719 | word_list_remove_quoted_nulls (tlist); | |
2720 | ret = string_list (tlist); | |
2721 | } | |
d233b485 CR |
2722 | else if (pchar == '*' && quoted == 0 && ifs_is_null) /* XXX */ |
2723 | ret = expand_no_split_dollar_star ? string_list_dollar_star (list, quoted, 0) : string_list_dollar_at (list, quoted, 0); /* Posix interp 888 */ | |
712f80b0 CR |
2724 | else if (pchar == '*' && quoted == 0 && (pflags & PF_ASSIGNRHS)) /* XXX */ |
2725 | ret = expand_no_split_dollar_star ? string_list_dollar_star (list, quoted, 0) : string_list_dollar_at (list, quoted, 0); /* Posix interp 888 */ | |
3185942a JA |
2726 | else if (pchar == '*') |
2727 | { | |
2728 | /* Even when unquoted, string_list_dollar_star does the right thing | |
2729 | making sure that the first character of $IFS is used as the | |
2730 | separator. */ | |
d233b485 | 2731 | ret = string_list_dollar_star (list, quoted, 0); |
3185942a JA |
2732 | } |
2733 | else if (pchar == '@' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
2734 | /* We use string_list_dollar_at, but only if the string is quoted, since | |
2735 | that quotes the escapes if it's not, which we don't want. We could | |
2736 | use string_list (the old code did), but that doesn't do the right | |
2737 | thing if the first character of $IFS is not a space. We use | |
2738 | string_list_dollar_star if the string is unquoted so we make sure that | |
2739 | the elements of $@ are separated by the first character of $IFS for | |
2740 | later splitting. */ | |
a0c0a00f | 2741 | ret = string_list_dollar_at (list, quoted, 0); |
d233b485 CR |
2742 | else if (pchar == '@' && quoted == 0 && ifs_is_null) /* XXX */ |
2743 | ret = string_list_dollar_at (list, quoted, 0); /* Posix interp 888 */ | |
712f80b0 CR |
2744 | else if (pchar == '@' && quoted == 0 && (pflags & PF_ASSIGNRHS)) |
2745 | ret = string_list_dollar_at (list, quoted, pflags); /* Posix interp 888 */ | |
3185942a | 2746 | else if (pchar == '@') |
d233b485 | 2747 | ret = string_list_dollar_star (list, quoted, 0); |
3185942a JA |
2748 | else |
2749 | ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list); | |
2750 | ||
2751 | return ret; | |
2752 | } | |
2753 | ||
726f6388 JA |
2754 | /* Return the list of words present in STRING. Separate the string into |
2755 | words at any of the characters found in SEPARATORS. If QUOTED is | |
2756 | non-zero then word in the list will have its quoted flag set, otherwise | |
2757 | the quoted flag is left as make_word () deemed fit. | |
2758 | ||
2759 | This obeys the P1003.2 word splitting semantics. If `separators' is | |
2760 | exactly <space><tab><newline>, then the splitting algorithm is that of | |
2761 | the Bourne shell, which treats any sequence of characters from `separators' | |
2762 | as a delimiter. If IFS is unset, which results in `separators' being set | |
2763 | to "", no splitting occurs. If separators has some other value, the | |
2764 | following rules are applied (`IFS white space' means zero or more | |
2765 | occurrences of <space>, <tab>, or <newline>, as long as those characters | |
2766 | are in `separators'): | |
2767 | ||
2768 | 1) IFS white space is ignored at the start and the end of the | |
2769 | string. | |
2770 | 2) Each occurrence of a character in `separators' that is not | |
2771 | IFS white space, along with any adjacent occurrences of | |
2772 | IFS white space delimits a field. | |
2773 | 3) Any nonzero-length sequence of IFS white space delimits a field. | |
2774 | */ | |
2775 | ||
2776 | /* BEWARE! list_string strips null arguments. Don't call it twice and | |
2777 | expect to have "" preserved! */ | |
2778 | ||
726f6388 JA |
2779 | /* This performs word splitting and quoted null character removal on |
2780 | STRING. */ | |
b80f6443 JA |
2781 | #define issep(c) \ |
2782 | (((separators)[0]) ? ((separators)[1] ? isifs(c) \ | |
2783 | : (c) == (separators)[0]) \ | |
2784 | : 0) | |
726f6388 | 2785 | |
d233b485 CR |
2786 | /* member of the space character class in the current locale */ |
2787 | #define ifs_whitespace(c) ISSPACE(c) | |
2788 | ||
2789 | /* "adjacent IFS white space" */ | |
2790 | #define ifs_whitesep(c) ((sh_style_split || separators == 0) ? spctabnl (c) \ | |
2791 | : ifs_whitespace (c)) | |
2792 | ||
726f6388 JA |
2793 | WORD_LIST * |
2794 | list_string (string, separators, quoted) | |
2795 | register char *string, *separators; | |
2796 | int quoted; | |
2797 | { | |
ccc6cda3 JA |
2798 | WORD_LIST *result; |
2799 | WORD_DESC *t; | |
2800 | char *current_word, *s; | |
712f80b0 | 2801 | int sindex, sh_style_split, whitesep, xflags, free_word; |
95732b49 | 2802 | size_t slen; |
726f6388 JA |
2803 | |
2804 | if (!string || !*string) | |
2805 | return ((WORD_LIST *)NULL); | |
2806 | ||
7117c2d2 JA |
2807 | sh_style_split = separators && separators[0] == ' ' && |
2808 | separators[1] == '\t' && | |
2809 | separators[2] == '\n' && | |
2810 | separators[3] == '\0'; | |
3185942a JA |
2811 | for (xflags = 0, s = ifs_value; s && *s; s++) |
2812 | { | |
2813 | if (*s == CTLESC) xflags |= SX_NOCTLESC; | |
2814 | else if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL; | |
2815 | } | |
726f6388 | 2816 | |
95732b49 | 2817 | slen = 0; |
726f6388 JA |
2818 | /* Remove sequences of whitespace at the beginning of STRING, as |
2819 | long as those characters appear in IFS. Do not do this if | |
d233b485 CR |
2820 | STRING is quoted or if there are no separator characters. We use the |
2821 | Posix definition of whitespace as a member of the space character | |
2822 | class in the current locale. */ | |
712f80b0 | 2823 | #if 0 |
726f6388 | 2824 | if (!quoted || !separators || !*separators) |
712f80b0 CR |
2825 | #else |
2826 | /* issep() requires that separators be non-null, and always returns 0 if | |
2827 | separator is the empty string, so don't bother if we get an empty string | |
2828 | for separators. We already returned NULL above if STRING is empty. */ | |
2829 | if (!quoted && separators && *separators) | |
2830 | #endif | |
726f6388 | 2831 | { |
d233b485 | 2832 | for (s = string; *s && issep (*s) && ifs_whitespace (*s); s++); |
726f6388 JA |
2833 | |
2834 | if (!*s) | |
2835 | return ((WORD_LIST *)NULL); | |
2836 | ||
2837 | string = s; | |
2838 | } | |
2839 | ||
2840 | /* OK, now STRING points to a word that does not begin with white space. | |
2841 | The splitting algorithm is: | |
7117c2d2 | 2842 | extract a word, stopping at a separator |
d233b485 | 2843 | skip sequences of whitespace characters as long as they are separators |
726f6388 | 2844 | This obeys the field splitting rules in Posix.2. */ |
d233b485 | 2845 | slen = STRLEN (string); |
ccc6cda3 | 2846 | for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; ) |
726f6388 | 2847 | { |
d233b485 CR |
2848 | /* Don't need string length in ADVANCE_CHAR unless multibyte chars are |
2849 | possible, but need it in string_extract_verbatim for bounds checking */ | |
3185942a | 2850 | current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags); |
ccc6cda3 | 2851 | if (current_word == 0) |
726f6388 JA |
2852 | break; |
2853 | ||
712f80b0 CR |
2854 | free_word = 1; /* If non-zero, we free current_word */ |
2855 | ||
726f6388 JA |
2856 | /* If we have a quoted empty string, add a quoted null argument. We |
2857 | want to preserve the quoted null character iff this is a quoted | |
2858 | empty string; otherwise the quoted null characters are removed | |
2859 | below. */ | |
2860 | if (QUOTED_NULL (current_word)) | |
2861 | { | |
95732b49 | 2862 | t = alloc_word_desc (); |
726f6388 | 2863 | t->word = make_quoted_char ('\0'); |
95732b49 | 2864 | t->flags |= W_QUOTED|W_HASQUOTEDNULL; |
726f6388 JA |
2865 | result = make_word_list (t, result); |
2866 | } | |
ccc6cda3 | 2867 | else if (current_word[0] != '\0') |
726f6388 JA |
2868 | { |
2869 | /* If we have something, then add it regardless. However, | |
2870 | perform quoted null character removal on the current word. */ | |
2871 | remove_quoted_nulls (current_word); | |
712f80b0 CR |
2872 | |
2873 | /* We don't want to set the word flags based on the string contents | |
2874 | here -- that's mostly for the parser -- so we just allocate a | |
2875 | WORD_DESC *, assign current_word (noting that we don't want to | |
2876 | free it), and skip all of make_word. */ | |
2877 | t = alloc_word_desc (); | |
2878 | t->word = current_word; | |
2879 | result = make_word_list (t, result); | |
2880 | free_word = 0; | |
95732b49 | 2881 | result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */ |
ccc6cda3 JA |
2882 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) |
2883 | result->word->flags |= W_QUOTED; | |
712f80b0 CR |
2884 | /* If removing quoted null characters leaves an empty word, note |
2885 | that we saw this for the caller to act on. */ | |
2886 | if (current_word == 0 || current_word[0] == '\0') | |
2887 | result->word->flags |= W_SAWQUOTEDNULL; | |
726f6388 JA |
2888 | } |
2889 | ||
2890 | /* If we're not doing sequences of separators in the traditional | |
2891 | Bourne shell style, then add a quoted null argument. */ | |
d233b485 | 2892 | else if (!sh_style_split && !ifs_whitespace (string[sindex])) |
726f6388 | 2893 | { |
95732b49 | 2894 | t = alloc_word_desc (); |
ccc6cda3 | 2895 | t->word = make_quoted_char ('\0'); |
95732b49 | 2896 | t->flags |= W_QUOTED|W_HASQUOTEDNULL; |
ccc6cda3 | 2897 | result = make_word_list (t, result); |
726f6388 JA |
2898 | } |
2899 | ||
712f80b0 CR |
2900 | if (free_word) |
2901 | free (current_word); | |
726f6388 | 2902 | |
28ef6c31 | 2903 | /* Note whether or not the separator is IFS whitespace, used later. */ |
d233b485 | 2904 | whitesep = string[sindex] && ifs_whitesep (string[sindex]); |
28ef6c31 | 2905 | |
726f6388 JA |
2906 | /* Move past the current separator character. */ |
2907 | if (string[sindex]) | |
95732b49 JA |
2908 | { |
2909 | DECLARE_MBSTATE; | |
2910 | ADVANCE_CHAR (string, slen, sindex); | |
2911 | } | |
726f6388 | 2912 | |
d233b485 | 2913 | /* Now skip sequences of whitespace characters if they are |
726f6388 | 2914 | in the list of separators. */ |
d233b485 | 2915 | while (string[sindex] && ifs_whitesep (string[sindex]) && issep (string[sindex])) |
726f6388 | 2916 | sindex++; |
28ef6c31 | 2917 | |
7117c2d2 JA |
2918 | /* If the first separator was IFS whitespace and the current character |
2919 | is a non-whitespace IFS character, it should be part of the current | |
2920 | field delimiter, not a separate delimiter that would result in an | |
2921 | empty field. Look at POSIX.2, 3.6.5, (3)(b). */ | |
d233b485 | 2922 | if (string[sindex] && whitesep && issep (string[sindex]) && !ifs_whitesep (string[sindex])) |
95732b49 JA |
2923 | { |
2924 | sindex++; | |
2925 | /* An IFS character that is not IFS white space, along with any | |
2926 | adjacent IFS white space, shall delimit a field. (SUSv3) */ | |
d233b485 | 2927 | while (string[sindex] && ifs_whitesep (string[sindex]) && isifs (string[sindex])) |
95732b49 JA |
2928 | sindex++; |
2929 | } | |
726f6388 JA |
2930 | } |
2931 | return (REVERSE_LIST (result, WORD_LIST *)); | |
2932 | } | |
2933 | ||
2934 | /* Parse a single word from STRING, using SEPARATORS to separate fields. | |
2935 | ENDPTR is set to the first character after the word. This is used by | |
bc007799 CR |
2936 | the `read' builtin. |
2937 | ||
2938 | This is never called with SEPARATORS != $IFS, and takes advantage of that. | |
7117c2d2 | 2939 | |
726f6388 JA |
2940 | XXX - this function is very similar to list_string; they should be |
2941 | combined - XXX */ | |
bc007799 | 2942 | |
d233b485 | 2943 | /* character is in $IFS */ |
bc007799 CR |
2944 | #define islocalsep(c) (local_cmap[(unsigned char)(c)] != 0) |
2945 | ||
726f6388 JA |
2946 | char * |
2947 | get_word_from_string (stringp, separators, endptr) | |
2948 | char **stringp, *separators, **endptr; | |
2949 | { | |
2950 | register char *s; | |
2951 | char *current_word; | |
3185942a | 2952 | int sindex, sh_style_split, whitesep, xflags; |
bc007799 | 2953 | unsigned char local_cmap[UCHAR_MAX+1]; /* really only need single-byte chars here */ |
95732b49 | 2954 | size_t slen; |
726f6388 JA |
2955 | |
2956 | if (!stringp || !*stringp || !**stringp) | |
2957 | return ((char *)NULL); | |
ccc6cda3 | 2958 | |
7117c2d2 JA |
2959 | sh_style_split = separators && separators[0] == ' ' && |
2960 | separators[1] == '\t' && | |
2961 | separators[2] == '\n' && | |
2962 | separators[3] == '\0'; | |
bc007799 CR |
2963 | memset (local_cmap, '\0', sizeof (local_cmap)); |
2964 | for (xflags = 0, s = separators; s && *s; s++) | |
3185942a JA |
2965 | { |
2966 | if (*s == CTLESC) xflags |= SX_NOCTLESC; | |
2967 | if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL; | |
bc007799 | 2968 | local_cmap[(unsigned char)*s] = 1; /* local charmap of separators */ |
3185942a | 2969 | } |
726f6388 | 2970 | |
3185942a | 2971 | s = *stringp; |
95732b49 JA |
2972 | slen = 0; |
2973 | ||
726f6388 | 2974 | /* Remove sequences of whitespace at the beginning of STRING, as |
bc007799 CR |
2975 | long as those characters appear in SEPARATORS. This happens if |
2976 | SEPARATORS == $' \t\n' or if IFS is unset. */ | |
2977 | if (sh_style_split || separators == 0) | |
d233b485 CR |
2978 | for (; *s && spctabnl (*s) && islocalsep (*s); s++); |
2979 | else | |
2980 | for (; *s && ifs_whitespace (*s) && islocalsep (*s); s++); | |
726f6388 | 2981 | |
d233b485 CR |
2982 | /* If the string is nothing but whitespace, update it and return. */ |
2983 | if (!*s) | |
2984 | { | |
2985 | *stringp = s; | |
2986 | if (endptr) | |
2987 | *endptr = s; | |
2988 | return ((char *)NULL); | |
726f6388 JA |
2989 | } |
2990 | ||
2991 | /* OK, S points to a word that does not begin with white space. | |
2992 | Now extract a word, stopping at a separator, save a pointer to | |
2993 | the first character after the word, then skip sequences of spc, | |
2994 | tab, or nl as long as they are separators. | |
ccc6cda3 | 2995 | |
726f6388 JA |
2996 | This obeys the field splitting rules in Posix.2. */ |
2997 | sindex = 0; | |
bc007799 CR |
2998 | /* Don't need string length in ADVANCE_CHAR unless multibyte chars are |
2999 | possible, but need it in string_extract_verbatim for bounds checking */ | |
3000 | slen = STRLEN (s); | |
3185942a | 3001 | current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags); |
726f6388 JA |
3002 | |
3003 | /* Set ENDPTR to the first character after the end of the word. */ | |
3004 | if (endptr) | |
3005 | *endptr = s + sindex; | |
3006 | ||
28ef6c31 | 3007 | /* Note whether or not the separator is IFS whitespace, used later. */ |
d233b485 | 3008 | whitesep = s[sindex] && ifs_whitesep (s[sindex]); |
28ef6c31 | 3009 | |
726f6388 JA |
3010 | /* Move past the current separator character. */ |
3011 | if (s[sindex]) | |
95732b49 JA |
3012 | { |
3013 | DECLARE_MBSTATE; | |
3014 | ADVANCE_CHAR (s, slen, sindex); | |
3015 | } | |
726f6388 JA |
3016 | |
3017 | /* Now skip sequences of space, tab, or newline characters if they are | |
3018 | in the list of separators. */ | |
bc007799 | 3019 | while (s[sindex] && spctabnl (s[sindex]) && islocalsep (s[sindex])) |
726f6388 JA |
3020 | sindex++; |
3021 | ||
28ef6c31 JA |
3022 | /* If the first separator was IFS whitespace and the current character is |
3023 | a non-whitespace IFS character, it should be part of the current field | |
3024 | delimiter, not a separate delimiter that would result in an empty field. | |
3025 | Look at POSIX.2, 3.6.5, (3)(b). */ | |
d233b485 | 3026 | if (s[sindex] && whitesep && islocalsep (s[sindex]) && !ifs_whitesep (s[sindex])) |
95732b49 JA |
3027 | { |
3028 | sindex++; | |
3029 | /* An IFS character that is not IFS white space, along with any adjacent | |
3030 | IFS white space, shall delimit a field. */ | |
d233b485 | 3031 | while (s[sindex] && ifs_whitesep (s[sindex]) && islocalsep(s[sindex])) |
95732b49 JA |
3032 | sindex++; |
3033 | } | |
28ef6c31 | 3034 | |
726f6388 JA |
3035 | /* Update STRING to point to the next field. */ |
3036 | *stringp = s + sindex; | |
3037 | return (current_word); | |
3038 | } | |
3039 | ||
3040 | /* Remove IFS white space at the end of STRING. Start at the end | |
3041 | of the string and walk backwards until the beginning of the string | |
3042 | or we find a character that's not IFS white space and not CTLESC. | |
3043 | Only let CTLESC escape a white space character if SAW_ESCAPE is | |
3044 | non-zero. */ | |
3045 | char * | |
3046 | strip_trailing_ifs_whitespace (string, separators, saw_escape) | |
3047 | char *string, *separators; | |
3048 | int saw_escape; | |
3049 | { | |
3050 | char *s; | |
ccc6cda3 | 3051 | |
726f6388 | 3052 | s = string + STRLEN (string) - 1; |
7117c2d2 | 3053 | while (s > string && ((spctabnl (*s) && isifs (*s)) || |
726f6388 JA |
3054 | (saw_escape && *s == CTLESC && spctabnl (s[1])))) |
3055 | s--; | |
3056 | *++s = '\0'; | |
3057 | return string; | |
3058 | } | |
3059 | ||
bb70624e JA |
3060 | #if 0 |
3061 | /* UNUSED */ | |
3062 | /* Split STRING into words at whitespace. Obeys shell-style quoting with | |
3063 | backslashes, single and double quotes. */ | |
ccc6cda3 JA |
3064 | WORD_LIST * |
3065 | list_string_with_quotes (string) | |
3066 | char *string; | |
3067 | { | |
3068 | WORD_LIST *list; | |
3069 | char *token, *s; | |
7117c2d2 | 3070 | size_t s_len; |
ccc6cda3 JA |
3071 | int c, i, tokstart, len; |
3072 | ||
3073 | for (s = string; s && *s && spctabnl (*s); s++) | |
3074 | ; | |
3075 | if (s == 0 || *s == 0) | |
3076 | return ((WORD_LIST *)NULL); | |
3077 | ||
7117c2d2 | 3078 | s_len = strlen (s); |
ccc6cda3 JA |
3079 | tokstart = i = 0; |
3080 | list = (WORD_LIST *)NULL; | |
3081 | while (1) | |
3082 | { | |
3083 | c = s[i]; | |
3084 | if (c == '\\') | |
3085 | { | |
3086 | i++; | |
3087 | if (s[i]) | |
3088 | i++; | |
3089 | } | |
3090 | else if (c == '\'') | |
a0c0a00f | 3091 | i = skip_single_quoted (s, s_len, ++i, 0); |
ccc6cda3 | 3092 | else if (c == '"') |
a0c0a00f | 3093 | i = skip_double_quoted (s, s_len, ++i, 0); |
ccc6cda3 JA |
3094 | else if (c == 0 || spctabnl (c)) |
3095 | { | |
3096 | /* We have found the end of a token. Make a word out of it and | |
3097 | add it to the word list. */ | |
bb70624e | 3098 | token = substring (s, tokstart, i); |
cce855bc | 3099 | list = add_string_to_list (token, list); |
ccc6cda3 JA |
3100 | free (token); |
3101 | while (spctabnl (s[i])) | |
3102 | i++; | |
3103 | if (s[i]) | |
3104 | tokstart = i; | |
3105 | else | |
3106 | break; | |
3107 | } | |
3108 | else | |
3109 | i++; /* normal character */ | |
3110 | } | |
3111 | return (REVERSE_LIST (list, WORD_LIST *)); | |
3112 | } | |
bb70624e | 3113 | #endif |
d166f048 | 3114 | |
cce855bc JA |
3115 | /********************************************************/ |
3116 | /* */ | |
3117 | /* Functions to perform assignment statements */ | |
3118 | /* */ | |
3119 | /********************************************************/ | |
d166f048 | 3120 | |
95732b49 JA |
3121 | #if defined (ARRAY_VARS) |
3122 | static SHELL_VAR * | |
3123 | do_compound_assignment (name, value, flags) | |
3124 | char *name, *value; | |
3125 | int flags; | |
3126 | { | |
3127 | SHELL_VAR *v; | |
d233b485 | 3128 | int mklocal, mkassoc, mkglobal, chklocal; |
0628567a | 3129 | WORD_LIST *list; |
d233b485 | 3130 | char *newname; /* used for local nameref references */ |
95732b49 JA |
3131 | |
3132 | mklocal = flags & ASS_MKLOCAL; | |
3185942a | 3133 | mkassoc = flags & ASS_MKASSOC; |
ac50fbac | 3134 | mkglobal = flags & ASS_MKGLOBAL; |
d233b485 | 3135 | chklocal = flags & ASS_CHKLOCAL; |
95732b49 JA |
3136 | |
3137 | if (mklocal && variable_context) | |
3138 | { | |
d233b485 CR |
3139 | v = find_variable (name); /* follows namerefs */ |
3140 | newname = (v == 0) ? nameref_transform_name (name, flags) : v->name; | |
a0c0a00f CR |
3141 | if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v))) |
3142 | { | |
d233b485 | 3143 | if (readonly_p (v)) |
a0c0a00f CR |
3144 | err_readonly (name); |
3145 | return (v); /* XXX */ | |
3146 | } | |
3185942a JA |
3147 | list = expand_compound_array_assignment (v, value, flags); |
3148 | if (mkassoc) | |
d233b485 | 3149 | v = make_local_assoc_variable (newname, 0); |
3185942a | 3150 | else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context) |
d233b485 | 3151 | v = make_local_array_variable (newname, 0); |
ac50fbac CR |
3152 | if (v) |
3153 | assign_compound_array_list (v, list, flags); | |
a0c0a00f CR |
3154 | if (list) |
3155 | dispose_words (list); | |
ac50fbac | 3156 | } |
d233b485 CR |
3157 | /* In a function but forcing assignment in global context. CHKLOCAL means to |
3158 | check for an existing local variable first. */ | |
ac50fbac CR |
3159 | else if (mkglobal && variable_context) |
3160 | { | |
d233b485 CR |
3161 | v = chklocal ? find_variable (name) : 0; |
3162 | if (v && (local_p (v) == 0 || v->context != variable_context)) | |
3163 | v = 0; | |
3164 | if (v == 0) | |
3165 | v = find_global_variable (name); | |
a0c0a00f CR |
3166 | if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v))) |
3167 | { | |
d233b485 | 3168 | if (readonly_p (v)) |
a0c0a00f CR |
3169 | err_readonly (name); |
3170 | return (v); /* XXX */ | |
3171 | } | |
d233b485 CR |
3172 | /* sanity check */ |
3173 | newname = (v == 0) ? nameref_transform_name (name, flags) : name; | |
ac50fbac CR |
3174 | list = expand_compound_array_assignment (v, value, flags); |
3175 | if (v == 0 && mkassoc) | |
d233b485 | 3176 | v = make_new_assoc_variable (newname); |
ac50fbac CR |
3177 | else if (v && mkassoc && assoc_p (v) == 0) |
3178 | v = convert_var_to_assoc (v); | |
3179 | else if (v == 0) | |
d233b485 | 3180 | v = make_new_array_variable (newname); |
ac50fbac CR |
3181 | else if (v && mkassoc == 0 && array_p (v) == 0) |
3182 | v = convert_var_to_array (v); | |
3183 | if (v) | |
3184 | assign_compound_array_list (v, list, flags); | |
a0c0a00f CR |
3185 | if (list) |
3186 | dispose_words (list); | |
95732b49 JA |
3187 | } |
3188 | else | |
a0c0a00f CR |
3189 | { |
3190 | v = assign_array_from_string (name, value, flags); | |
3191 | if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v))) | |
3192 | { | |
d233b485 | 3193 | if (readonly_p (v)) |
a0c0a00f CR |
3194 | err_readonly (name); |
3195 | return (v); /* XXX */ | |
3196 | } | |
3197 | } | |
95732b49 JA |
3198 | |
3199 | return (v); | |
3200 | } | |
3201 | #endif | |
3202 | ||
726f6388 JA |
3203 | /* Given STRING, an assignment string, get the value of the right side |
3204 | of the `=', and bind it to the left side. If EXPAND is true, then | |
3205 | perform parameter expansion, command substitution, and arithmetic | |
3206 | expansion on the right-hand side. Perform tilde expansion in any | |
3207 | case. Do not perform word splitting on the result of expansion. */ | |
3208 | static int | |
95732b49 JA |
3209 | do_assignment_internal (word, expand) |
3210 | const WORD_DESC *word; | |
726f6388 JA |
3211 | int expand; |
3212 | { | |
495aee44 CR |
3213 | int offset, appendop, assign_list, aflags, retval; |
3214 | char *name, *value, *temp; | |
ccc6cda3 JA |
3215 | SHELL_VAR *entry; |
3216 | #if defined (ARRAY_VARS) | |
3217 | char *t; | |
b80f6443 | 3218 | int ni; |
ccc6cda3 | 3219 | #endif |
95732b49 | 3220 | const char *string; |
ccc6cda3 | 3221 | |
95732b49 JA |
3222 | if (word == 0 || word->word == 0) |
3223 | return 0; | |
3224 | ||
3225 | appendop = assign_list = aflags = 0; | |
3226 | string = word->word; | |
b80f6443 | 3227 | offset = assignment (string, 0); |
ccc6cda3 JA |
3228 | name = savestring (string); |
3229 | value = (char *)NULL; | |
726f6388 JA |
3230 | |
3231 | if (name[offset] == '=') | |
3232 | { | |
95732b49 JA |
3233 | if (name[offset - 1] == '+') |
3234 | { | |
3235 | appendop = 1; | |
3236 | name[offset - 1] = '\0'; | |
3237 | } | |
3238 | ||
3239 | name[offset] = 0; /* might need this set later */ | |
726f6388 JA |
3240 | temp = name + offset + 1; |
3241 | ||
ccc6cda3 | 3242 | #if defined (ARRAY_VARS) |
95732b49 | 3243 | if (expand && (word->flags & W_COMPASSIGN)) |
726f6388 | 3244 | { |
ccc6cda3 | 3245 | assign_list = ni = 1; |
95732b49 | 3246 | value = extract_array_assignment_list (temp, &ni); |
ccc6cda3 JA |
3247 | } |
3248 | else | |
3249 | #endif | |
ccc6cda3 | 3250 | if (expand && temp[0]) |
95732b49 | 3251 | value = expand_string_if_necessary (temp, 0, expand_string_assignment); |
726f6388 JA |
3252 | else |
3253 | value = savestring (temp); | |
3254 | } | |
3255 | ||
3256 | if (value == 0) | |
d166f048 | 3257 | { |
f73dda09 | 3258 | value = (char *)xmalloc (1); |
d166f048 JA |
3259 | value[0] = '\0'; |
3260 | } | |
726f6388 | 3261 | |
726f6388 | 3262 | if (echo_command_at_execute) |
95732b49 JA |
3263 | { |
3264 | if (appendop) | |
3265 | name[offset - 1] = '+'; | |
3266 | xtrace_print_assignment (name, value, assign_list, 1); | |
3267 | if (appendop) | |
3268 | name[offset - 1] = '\0'; | |
3269 | } | |
726f6388 | 3270 | |
d166f048 | 3271 | #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0) |
ccc6cda3 | 3272 | |
95732b49 JA |
3273 | if (appendop) |
3274 | aflags |= ASS_APPEND; | |
3275 | ||
ccc6cda3 | 3276 | #if defined (ARRAY_VARS) |
d233b485 | 3277 | if (t = mbschr (name, LBRACK)) |
ccc6cda3 JA |
3278 | { |
3279 | if (assign_list) | |
3280 | { | |
b80f6443 | 3281 | report_error (_("%s: cannot assign list to array member"), name); |
ccc6cda3 JA |
3282 | ASSIGN_RETURN (0); |
3283 | } | |
95732b49 | 3284 | entry = assign_array_element (name, value, aflags); |
ccc6cda3 | 3285 | if (entry == 0) |
28ef6c31 | 3286 | ASSIGN_RETURN (0); |
ccc6cda3 JA |
3287 | } |
3288 | else if (assign_list) | |
95732b49 | 3289 | { |
d233b485 CR |
3290 | if ((word->flags & W_ASSIGNARG) && (word->flags & W_CHKLOCAL)) |
3291 | aflags |= ASS_CHKLOCAL; | |
6d41b715 | 3292 | if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL) == 0) |
95732b49 | 3293 | aflags |= ASS_MKLOCAL; |
ac50fbac CR |
3294 | if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL)) |
3295 | aflags |= ASS_MKGLOBAL; | |
3185942a JA |
3296 | if (word->flags & W_ASSIGNASSOC) |
3297 | aflags |= ASS_MKASSOC; | |
95732b49 JA |
3298 | entry = do_compound_assignment (name, value, aflags); |
3299 | } | |
ccc6cda3 JA |
3300 | else |
3301 | #endif /* ARRAY_VARS */ | |
95732b49 | 3302 | entry = bind_variable (name, value, aflags); |
ccc6cda3 | 3303 | |
3eb0018e CR |
3304 | if (entry) |
3305 | stupidly_hack_special_variables (entry->name); /* might be a nameref */ | |
3306 | else | |
3307 | stupidly_hack_special_variables (name); | |
726f6388 | 3308 | |
3185942a JA |
3309 | /* Return 1 if the assignment seems to have been performed correctly. */ |
3310 | if (entry == 0 || readonly_p (entry)) | |
3311 | retval = 0; /* assignment failure */ | |
3312 | else if (noassign_p (entry)) | |
3313 | { | |
712f80b0 | 3314 | set_exit_status (EXECUTION_FAILURE); |
3185942a JA |
3315 | retval = 1; /* error status, but not assignment failure */ |
3316 | } | |
3317 | else | |
3318 | retval = 1; | |
3319 | ||
3320 | if (entry && retval != 0 && noassign_p (entry) == 0) | |
3321 | VUNSETATTR (entry, att_invisible); | |
3322 | ||
3323 | ASSIGN_RETURN (retval); | |
726f6388 JA |
3324 | } |
3325 | ||
3326 | /* Perform the assignment statement in STRING, and expand the | |
95732b49 | 3327 | right side by doing tilde, command and parameter expansion. */ |
ccc6cda3 | 3328 | int |
726f6388 | 3329 | do_assignment (string) |
95732b49 | 3330 | char *string; |
726f6388 | 3331 | { |
95732b49 JA |
3332 | WORD_DESC td; |
3333 | ||
3334 | td.flags = W_ASSIGNMENT; | |
3335 | td.word = string; | |
3336 | ||
3337 | return do_assignment_internal (&td, 1); | |
3338 | } | |
3339 | ||
3340 | int | |
495aee44 | 3341 | do_word_assignment (word, flags) |
95732b49 | 3342 | WORD_DESC *word; |
495aee44 | 3343 | int flags; |
95732b49 JA |
3344 | { |
3345 | return do_assignment_internal (word, 1); | |
726f6388 JA |
3346 | } |
3347 | ||
3348 | /* Given STRING, an assignment string, get the value of the right side | |
95732b49 JA |
3349 | of the `=', and bind it to the left side. Do not perform any word |
3350 | expansions on the right hand side. */ | |
ccc6cda3 | 3351 | int |
726f6388 | 3352 | do_assignment_no_expand (string) |
95732b49 | 3353 | char *string; |
726f6388 | 3354 | { |
95732b49 JA |
3355 | WORD_DESC td; |
3356 | ||
3357 | td.flags = W_ASSIGNMENT; | |
3358 | td.word = string; | |
3359 | ||
3360 | return (do_assignment_internal (&td, 0)); | |
726f6388 JA |
3361 | } |
3362 | ||
cce855bc JA |
3363 | /*************************************************** |
3364 | * * | |
3365 | * Functions to manage the positional parameters * | |
3366 | * * | |
3367 | ***************************************************/ | |
726f6388 JA |
3368 | |
3369 | /* Return the word list that corresponds to `$*'. */ | |
3370 | WORD_LIST * | |
3371 | list_rest_of_args () | |
3372 | { | |
ccc6cda3 | 3373 | register WORD_LIST *list, *args; |
726f6388 JA |
3374 | int i; |
3375 | ||
3376 | /* Break out of the loop as soon as one of the dollar variables is null. */ | |
ccc6cda3 JA |
3377 | for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++) |
3378 | list = make_word_list (make_bare_word (dollar_vars[i]), list); | |
3379 | ||
3380 | for (args = rest_of_args; args; args = args->next) | |
3381 | list = make_word_list (make_bare_word (args->word->word), list); | |
726f6388 | 3382 | |
726f6388 JA |
3383 | return (REVERSE_LIST (list, WORD_LIST *)); |
3384 | } | |
3385 | ||
cce855bc JA |
3386 | /* Return the value of a positional parameter. This handles values > 10. */ |
3387 | char * | |
3388 | get_dollar_var_value (ind) | |
7117c2d2 | 3389 | intmax_t ind; |
cce855bc JA |
3390 | { |
3391 | char *temp; | |
3392 | WORD_LIST *p; | |
3393 | ||
3394 | if (ind < 10) | |
3395 | temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL; | |
3396 | else /* We want something like ${11} */ | |
3397 | { | |
3398 | ind -= 10; | |
3399 | for (p = rest_of_args; p && ind--; p = p->next) | |
28ef6c31 | 3400 | ; |
cce855bc JA |
3401 | temp = p ? savestring (p->word->word) : (char *)NULL; |
3402 | } | |
3403 | return (temp); | |
3404 | } | |
3405 | ||
726f6388 JA |
3406 | /* Make a single large string out of the dollar digit variables, |
3407 | and the rest_of_args. If DOLLAR_STAR is 1, then obey the special | |
3408 | case of "$*" with respect to IFS. */ | |
3409 | char * | |
3410 | string_rest_of_args (dollar_star) | |
3411 | int dollar_star; | |
3412 | { | |
ccc6cda3 | 3413 | register WORD_LIST *list; |
726f6388 JA |
3414 | char *string; |
3415 | ||
ccc6cda3 | 3416 | list = list_rest_of_args (); |
d233b485 | 3417 | string = dollar_star ? string_list_dollar_star (list, 0, 0) : string_list (list); |
726f6388 JA |
3418 | dispose_words (list); |
3419 | return (string); | |
3420 | } | |
3421 | ||
cce855bc JA |
3422 | /* Return a string containing the positional parameters from START to |
3423 | END, inclusive. If STRING[0] == '*', we obey the rules for $*, | |
7117c2d2 JA |
3424 | which only makes a difference if QUOTED is non-zero. If QUOTED includes |
3425 | Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise | |
3426 | no quoting chars are added. */ | |
cce855bc | 3427 | static char * |
712f80b0 | 3428 | pos_params (string, start, end, quoted, pflags) |
cce855bc | 3429 | char *string; |
712f80b0 | 3430 | int start, end, quoted, pflags; |
726f6388 | 3431 | { |
cce855bc JA |
3432 | WORD_LIST *save, *params, *h, *t; |
3433 | char *ret; | |
3434 | int i; | |
726f6388 | 3435 | |
bb70624e JA |
3436 | /* see if we can short-circuit. if start == end, we want 0 parameters. */ |
3437 | if (start == end) | |
3438 | return ((char *)NULL); | |
3439 | ||
cce855bc | 3440 | save = params = list_rest_of_args (); |
a0c0a00f | 3441 | if (save == 0 && start > 0) |
cce855bc JA |
3442 | return ((char *)NULL); |
3443 | ||
3185942a JA |
3444 | if (start == 0) /* handle ${@:0[:x]} specially */ |
3445 | { | |
3446 | t = make_word_list (make_word (dollar_vars[0]), params); | |
3447 | save = params = t; | |
3448 | } | |
3449 | ||
0001803f | 3450 | for (i = start ? 1 : 0; params && i < start; i++) |
cce855bc JA |
3451 | params = params->next; |
3452 | if (params == 0) | |
a0c0a00f CR |
3453 | { |
3454 | dispose_words (save); | |
3455 | return ((char *)NULL); | |
3456 | } | |
cce855bc | 3457 | for (h = t = params; params && i < end; i++) |
d166f048 | 3458 | { |
cce855bc JA |
3459 | t = params; |
3460 | params = params->next; | |
d166f048 | 3461 | } |
cce855bc | 3462 | t->next = (WORD_LIST *)NULL; |
3185942a | 3463 | |
712f80b0 | 3464 | ret = string_list_pos_params (string[0], h, quoted, pflags); |
3185942a | 3465 | |
bb70624e JA |
3466 | if (t != params) |
3467 | t->next = params; | |
726f6388 | 3468 | |
cce855bc JA |
3469 | dispose_words (save); |
3470 | return (ret); | |
3471 | } | |
3472 | ||
3473 | /******************************************************************/ | |
3474 | /* */ | |
3475 | /* Functions to expand strings to strings or WORD_LISTs */ | |
3476 | /* */ | |
3477 | /******************************************************************/ | |
3478 | ||
3479 | #if defined (PROCESS_SUBSTITUTION) | |
95732b49 | 3480 | #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~') |
cce855bc | 3481 | #else |
95732b49 | 3482 | #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~') |
cce855bc JA |
3483 | #endif |
3484 | ||
3485 | /* If there are any characters in STRING that require full expansion, | |
3486 | then call FUNC to expand STRING; otherwise just perform quote | |
3487 | removal if necessary. This returns a new string. */ | |
3488 | static char * | |
f73dda09 | 3489 | expand_string_if_necessary (string, quoted, func) |
cce855bc JA |
3490 | char *string; |
3491 | int quoted; | |
f73dda09 | 3492 | EXPFUNC *func; |
cce855bc JA |
3493 | { |
3494 | WORD_LIST *list; | |
7117c2d2 | 3495 | size_t slen; |
cce855bc JA |
3496 | int i, saw_quote; |
3497 | char *ret; | |
7117c2d2 | 3498 | DECLARE_MBSTATE; |
cce855bc | 3499 | |
95732b49 JA |
3500 | /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */ |
3501 | slen = (MB_CUR_MAX > 1) ? strlen (string) : 0; | |
7117c2d2 JA |
3502 | i = saw_quote = 0; |
3503 | while (string[i]) | |
cce855bc JA |
3504 | { |
3505 | if (EXP_CHAR (string[i])) | |
3506 | break; | |
3507 | else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"') | |
3508 | saw_quote = 1; | |
7117c2d2 | 3509 | ADVANCE_CHAR (string, slen, i); |
cce855bc JA |
3510 | } |
3511 | ||
3512 | if (string[i]) | |
3513 | { | |
3514 | list = (*func) (string, quoted); | |
3515 | if (list) | |
3516 | { | |
3517 | ret = string_list (list); | |
3518 | dispose_words (list); | |
3519 | } | |
3520 | else | |
3521 | ret = (char *)NULL; | |
3522 | } | |
3523 | else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) | |
3524 | ret = string_quote_removal (string, quoted); | |
3525 | else | |
3526 | ret = savestring (string); | |
7117c2d2 | 3527 | |
cce855bc JA |
3528 | return ret; |
3529 | } | |
3530 | ||
3531 | static inline char * | |
f73dda09 | 3532 | expand_string_to_string_internal (string, quoted, func) |
cce855bc JA |
3533 | char *string; |
3534 | int quoted; | |
f73dda09 | 3535 | EXPFUNC *func; |
cce855bc JA |
3536 | { |
3537 | WORD_LIST *list; | |
3538 | char *ret; | |
3539 | ||
3540 | if (string == 0 || *string == '\0') | |
3541 | return ((char *)NULL); | |
3542 | ||
3543 | list = (*func) (string, quoted); | |
3544 | if (list) | |
3545 | { | |
3546 | ret = string_list (list); | |
3547 | dispose_words (list); | |
3548 | } | |
3549 | else | |
3550 | ret = (char *)NULL; | |
3551 | ||
3552 | return (ret); | |
3553 | } | |
3554 | ||
f73dda09 JA |
3555 | char * |
3556 | expand_string_to_string (string, quoted) | |
3557 | char *string; | |
3558 | int quoted; | |
3559 | { | |
3560 | return (expand_string_to_string_internal (string, quoted, expand_string)); | |
3561 | } | |
3562 | ||
3563 | char * | |
3564 | expand_string_unsplit_to_string (string, quoted) | |
3565 | char *string; | |
3566 | int quoted; | |
3567 | { | |
3568 | return (expand_string_to_string_internal (string, quoted, expand_string_unsplit)); | |
3569 | } | |
3570 | ||
95732b49 JA |
3571 | char * |
3572 | expand_assignment_string_to_string (string, quoted) | |
3573 | char *string; | |
3574 | int quoted; | |
3575 | { | |
3576 | return (expand_string_to_string_internal (string, quoted, expand_string_assignment)); | |
3577 | } | |
3578 | ||
0628567a JA |
3579 | char * |
3580 | expand_arith_string (string, quoted) | |
3581 | char *string; | |
3185942a | 3582 | int quoted; |
0628567a | 3583 | { |
ac50fbac CR |
3584 | WORD_DESC td; |
3585 | WORD_LIST *list, *tlist; | |
3586 | size_t slen; | |
3587 | int i, saw_quote; | |
3588 | char *ret; | |
3589 | DECLARE_MBSTATE; | |
3590 | ||
3591 | /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */ | |
3592 | slen = (MB_CUR_MAX > 1) ? strlen (string) : 0; | |
3593 | i = saw_quote = 0; | |
3594 | while (string[i]) | |
3595 | { | |
3596 | if (EXP_CHAR (string[i])) | |
3597 | break; | |
3598 | else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"') | |
3599 | saw_quote = 1; | |
3600 | ADVANCE_CHAR (string, slen, i); | |
3601 | } | |
3602 | ||
3603 | if (string[i]) | |
3604 | { | |
3605 | /* This is expanded version of expand_string_internal as it's called by | |
3606 | expand_string_leave_quoted */ | |
d233b485 | 3607 | td.flags = W_NOPROCSUB|W_NOTILDE; /* don't want process substitution or tilde expansion */ |
712f80b0 CR |
3608 | #if 0 /* TAG:bash-5.1 */ |
3609 | if (quoted & Q_ARRAYSUB) | |
3610 | td.flags |= W_NOCOMSUB; | |
3611 | #endif | |
ac50fbac CR |
3612 | td.word = savestring (string); |
3613 | list = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); | |
3614 | /* This takes care of the calls from expand_string_leave_quoted and | |
3615 | expand_string */ | |
3616 | if (list) | |
3617 | { | |
3618 | tlist = word_list_split (list); | |
3619 | dispose_words (list); | |
3620 | list = tlist; | |
3621 | if (list) | |
3622 | dequote_list (list); | |
3623 | } | |
3624 | /* This comes from expand_string_if_necessary */ | |
3625 | if (list) | |
3626 | { | |
3627 | ret = string_list (list); | |
3628 | dispose_words (list); | |
3629 | } | |
3630 | else | |
3631 | ret = (char *)NULL; | |
3632 | FREE (td.word); | |
3633 | } | |
a0c0a00f CR |
3634 | else if (saw_quote && (quoted & Q_ARITH)) |
3635 | ret = string_quote_removal (string, quoted); | |
ac50fbac CR |
3636 | else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) |
3637 | ret = string_quote_removal (string, quoted); | |
3638 | else | |
3639 | ret = savestring (string); | |
3640 | ||
3641 | return ret; | |
0628567a JA |
3642 | } |
3643 | ||
cce855bc JA |
3644 | #if defined (COND_COMMAND) |
3645 | /* Just remove backslashes in STRING. Returns a new string. */ | |
3646 | char * | |
3647 | remove_backslashes (string) | |
3648 | char *string; | |
3649 | { | |
3650 | char *r, *ret, *s; | |
3651 | ||
f73dda09 | 3652 | r = ret = (char *)xmalloc (strlen (string) + 1); |
cce855bc JA |
3653 | for (s = string; s && *s; ) |
3654 | { | |
3655 | if (*s == '\\') | |
28ef6c31 | 3656 | s++; |
cce855bc | 3657 | if (*s == 0) |
28ef6c31 | 3658 | break; |
cce855bc JA |
3659 | *r++ = *s++; |
3660 | } | |
3661 | *r = '\0'; | |
3662 | return ret; | |
3663 | } | |
3664 | ||
3665 | /* This needs better error handling. */ | |
3666 | /* Expand W for use as an argument to a unary or binary operator in a | |
f1be666c | 3667 | [[...]] expression. If SPECIAL is 1, this is the rhs argument |
cce855bc | 3668 | to the != or == operator, and should be treated as a pattern. In |
f1be666c JA |
3669 | this case, we quote the string specially for the globbing code. If |
3670 | SPECIAL is 2, this is an rhs argument for the =~ operator, and should | |
3671 | be quoted appropriately for regcomp/regexec. The caller is responsible | |
d894cfd1 CR |
3672 | for removing the backslashes if the unquoted word is needed later. In |
3673 | any case, since we don't perform word splitting, we need to do quoted | |
3674 | null character removal. */ | |
cce855bc JA |
3675 | char * |
3676 | cond_expand_word (w, special) | |
3677 | WORD_DESC *w; | |
3678 | int special; | |
3679 | { | |
3680 | char *r, *p; | |
3681 | WORD_LIST *l; | |
f1be666c | 3682 | int qflags; |
cce855bc JA |
3683 | |
3684 | if (w->word == 0 || w->word[0] == '\0') | |
3685 | return ((char *)NULL); | |
3686 | ||
3b34f6e6 | 3687 | expand_no_split_dollar_star = 1; |
0001803f | 3688 | w->flags |= W_NOSPLIT2; |
b72432fd | 3689 | l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0); |
3b34f6e6 | 3690 | expand_no_split_dollar_star = 0; |
cce855bc JA |
3691 | if (l) |
3692 | { | |
ac50fbac | 3693 | if (special == 0) /* LHS */ |
cce855bc | 3694 | { |
d894cfd1 CR |
3695 | if (l->word) |
3696 | word_list_remove_quoted_nulls (l); | |
cce855bc JA |
3697 | dequote_list (l); |
3698 | r = string_list (l); | |
3699 | } | |
3700 | else | |
28ef6c31 | 3701 | { |
ac50fbac CR |
3702 | /* Need to figure out whether or not we should call dequote_escapes |
3703 | or a new dequote_ctlnul function here, and under what | |
3704 | circumstances. */ | |
d233b485 | 3705 | qflags = QGLOB_CVTNULL|QGLOB_CTLESC; |
f1be666c JA |
3706 | if (special == 2) |
3707 | qflags |= QGLOB_REGEXP; | |
a0c0a00f | 3708 | word_list_remove_quoted_nulls (l); |
28ef6c31 | 3709 | p = string_list (l); |
f1be666c | 3710 | r = quote_string_for_globbing (p, qflags); |
28ef6c31 JA |
3711 | free (p); |
3712 | } | |
cce855bc JA |
3713 | dispose_words (l); |
3714 | } | |
3715 | else | |
3716 | r = (char *)NULL; | |
3717 | ||
3718 | return r; | |
3719 | } | |
3720 | #endif | |
3721 | ||
3722 | /* Call expand_word_internal to expand W and handle error returns. | |
3723 | A convenience function for functions that don't want to handle | |
3724 | any errors or free any memory before aborting. */ | |
3725 | static WORD_LIST * | |
b72432fd | 3726 | call_expand_word_internal (w, q, i, c, e) |
cce855bc | 3727 | WORD_DESC *w; |
b72432fd | 3728 | int q, i, *c, *e; |
cce855bc JA |
3729 | { |
3730 | WORD_LIST *result; | |
3731 | ||
b72432fd | 3732 | result = expand_word_internal (w, q, i, c, e); |
bb70624e | 3733 | if (result == &expand_word_error || result == &expand_word_fatal) |
cce855bc JA |
3734 | { |
3735 | /* By convention, each time this error is returned, w->word has | |
bb70624e JA |
3736 | already been freed (it sometimes may not be in the fatal case, |
3737 | but that doesn't result in a memory leak because we're going | |
3738 | to exit in most cases). */ | |
cce855bc | 3739 | w->word = (char *)NULL; |
28ef6c31 | 3740 | last_command_exit_value = EXECUTION_FAILURE; |
b80f6443 | 3741 | exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF); |
cce855bc | 3742 | /* NOTREACHED */ |
ac50fbac | 3743 | return (NULL); |
cce855bc | 3744 | } |
cce855bc JA |
3745 | else |
3746 | return (result); | |
3747 | } | |
3748 | ||
3749 | /* Perform parameter expansion, command substitution, and arithmetic | |
ac50fbac CR |
3750 | expansion on STRING, as if it were a word. Leave the result quoted. |
3751 | Since this does not perform word splitting, it leaves quoted nulls | |
3752 | in the result. */ | |
cce855bc JA |
3753 | static WORD_LIST * |
3754 | expand_string_internal (string, quoted) | |
3755 | char *string; | |
3756 | int quoted; | |
3757 | { | |
3758 | WORD_DESC td; | |
3759 | WORD_LIST *tresult; | |
3760 | ||
3761 | if (string == 0 || *string == 0) | |
3762 | return ((WORD_LIST *)NULL); | |
3763 | ||
28ef6c31 JA |
3764 | td.flags = 0; |
3765 | td.word = savestring (string); | |
3766 | ||
b72432fd | 3767 | tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); |
28ef6c31 JA |
3768 | |
3769 | FREE (td.word); | |
cce855bc | 3770 | return (tresult); |
726f6388 JA |
3771 | } |
3772 | ||
3773 | /* Expand STRING by performing parameter expansion, command substitution, | |
3774 | and arithmetic expansion. Dequote the resulting WORD_LIST before | |
3775 | returning it, but do not perform word splitting. The call to | |
3776 | remove_quoted_nulls () is in here because word splitting normally | |
3777 | takes care of quote removal. */ | |
3778 | WORD_LIST * | |
3779 | expand_string_unsplit (string, quoted) | |
3780 | char *string; | |
3781 | int quoted; | |
3782 | { | |
3783 | WORD_LIST *value; | |
3784 | ||
28ef6c31 | 3785 | if (string == 0 || *string == '\0') |
726f6388 JA |
3786 | return ((WORD_LIST *)NULL); |
3787 | ||
28ef6c31 | 3788 | expand_no_split_dollar_star = 1; |
726f6388 | 3789 | value = expand_string_internal (string, quoted); |
28ef6c31 JA |
3790 | expand_no_split_dollar_star = 0; |
3791 | ||
726f6388 JA |
3792 | if (value) |
3793 | { | |
3794 | if (value->word) | |
95732b49 | 3795 | { |
712f80b0 | 3796 | remove_quoted_nulls (value->word->word); /* XXX */ |
95732b49 JA |
3797 | value->word->flags &= ~W_HASQUOTEDNULL; |
3798 | } | |
3799 | dequote_list (value); | |
3800 | } | |
3801 | return (value); | |
3802 | } | |
3803 | ||
3804 | /* Expand the rhs of an assignment statement */ | |
3805 | WORD_LIST * | |
3806 | expand_string_assignment (string, quoted) | |
3807 | char *string; | |
3808 | int quoted; | |
3809 | { | |
3810 | WORD_DESC td; | |
3811 | WORD_LIST *value; | |
3812 | ||
3813 | if (string == 0 || *string == '\0') | |
3814 | return ((WORD_LIST *)NULL); | |
3815 | ||
3816 | expand_no_split_dollar_star = 1; | |
3817 | ||
d233b485 CR |
3818 | #if 0 |
3819 | /* Other shells (ksh93) do it this way, which affects how $@ is expanded | |
3820 | in constructs like bar=${@#0} (preserves the spaces resulting from the | |
3821 | expansion of $@ in a context where you don't do word splitting); Posix | |
3822 | interp 888 makes the expansion of $@ in contexts where word splitting | |
3823 | is not performed unspecified. */ | |
3824 | td.flags = W_ASSIGNRHS|W_NOSPLIT2; /* Posix interp 888 */ | |
3825 | #else | |
95732b49 | 3826 | td.flags = W_ASSIGNRHS; |
d233b485 | 3827 | #endif |
95732b49 JA |
3828 | td.word = savestring (string); |
3829 | value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); | |
3830 | FREE (td.word); | |
3831 | ||
3832 | expand_no_split_dollar_star = 0; | |
3833 | ||
3834 | if (value) | |
3835 | { | |
3836 | if (value->word) | |
3837 | { | |
712f80b0 | 3838 | remove_quoted_nulls (value->word->word); /* XXX */ |
95732b49 JA |
3839 | value->word->flags &= ~W_HASQUOTEDNULL; |
3840 | } | |
726f6388 JA |
3841 | dequote_list (value); |
3842 | } | |
3843 | return (value); | |
3844 | } | |
3845 | ||
bb70624e JA |
3846 | |
3847 | /* Expand one of the PS? prompt strings. This is a sort of combination of | |
3848 | expand_string_unsplit and expand_string_internal, but returns the | |
3849 | passed string when an error occurs. Might want to trap other calls | |
3850 | to jump_to_top_level here so we don't endlessly loop. */ | |
3851 | WORD_LIST * | |
f1be666c | 3852 | expand_prompt_string (string, quoted, wflags) |
bb70624e JA |
3853 | char *string; |
3854 | int quoted; | |
f1be666c | 3855 | int wflags; |
bb70624e JA |
3856 | { |
3857 | WORD_LIST *value; | |
3858 | WORD_DESC td; | |
3859 | ||
3860 | if (string == 0 || *string == 0) | |
3861 | return ((WORD_LIST *)NULL); | |
3862 | ||
f1be666c | 3863 | td.flags = wflags; |
bb70624e | 3864 | td.word = savestring (string); |
28ef6c31 JA |
3865 | |
3866 | no_longjmp_on_fatal_error = 1; | |
bb70624e | 3867 | value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL); |
28ef6c31 JA |
3868 | no_longjmp_on_fatal_error = 0; |
3869 | ||
bb70624e JA |
3870 | if (value == &expand_word_error || value == &expand_word_fatal) |
3871 | { | |
3872 | value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL); | |
3873 | return value; | |
3874 | } | |
3875 | FREE (td.word); | |
3876 | if (value) | |
3877 | { | |
3878 | if (value->word) | |
95732b49 | 3879 | { |
712f80b0 | 3880 | remove_quoted_nulls (value->word->word); /* XXX */ |
95732b49 JA |
3881 | value->word->flags &= ~W_HASQUOTEDNULL; |
3882 | } | |
bb70624e JA |
3883 | dequote_list (value); |
3884 | } | |
3885 | return (value); | |
3886 | } | |
3887 | ||
726f6388 JA |
3888 | /* Expand STRING just as if you were expanding a word, but do not dequote |
3889 | the resultant WORD_LIST. This is called only from within this file, | |
3890 | and is used to correctly preserve quoted characters when expanding | |
3891 | things like ${1+"$@"}. This does parameter expansion, command | |
b72432fd | 3892 | substitution, arithmetic expansion, and word splitting. */ |
726f6388 JA |
3893 | static WORD_LIST * |
3894 | expand_string_leave_quoted (string, quoted) | |
3895 | char *string; | |
3896 | int quoted; | |
3897 | { | |
3898 | WORD_LIST *tlist; | |
3899 | WORD_LIST *tresult; | |
3900 | ||
ccc6cda3 | 3901 | if (string == 0 || *string == '\0') |
726f6388 JA |
3902 | return ((WORD_LIST *)NULL); |
3903 | ||
3904 | tlist = expand_string_internal (string, quoted); | |
3905 | ||
3906 | if (tlist) | |
3907 | { | |
3908 | tresult = word_list_split (tlist); | |
3909 | dispose_words (tlist); | |
3910 | return (tresult); | |
3911 | } | |
3912 | return ((WORD_LIST *)NULL); | |
3913 | } | |
3914 | ||
ccc6cda3 JA |
3915 | /* This does not perform word splitting or dequote the WORD_LIST |
3916 | it returns. */ | |
3917 | static WORD_LIST * | |
d233b485 CR |
3918 | expand_string_for_rhs (string, quoted, op, pflags, dollar_at_p, expanded_p) |
3919 | char *string; | |
3920 | int quoted, op, pflags; | |
3921 | int *dollar_at_p, *expanded_p; | |
3922 | { | |
3923 | WORD_DESC td; | |
3924 | WORD_LIST *tresult; | |
3925 | int old_nosplit; | |
3926 | ||
3927 | if (string == 0 || *string == '\0') | |
3928 | return (WORD_LIST *)NULL; | |
3929 | ||
3930 | /* We want field splitting to be determined by what is going to be done with | |
3931 | the entire ${parameterOPword} expansion, so we don't want to split the RHS | |
3932 | we expand here. However, the expansion of $* is determined by whether we | |
3933 | are going to eventually perform word splitting, so we want to set this | |
3934 | depending on whether or not are are going to be splitting: if the expansion | |
3935 | is quoted, if the OP is `=', or if IFS is set to the empty string, we | |
3936 | are not going to be splitting, so we set expand_no_split_dollar_star to | |
712f80b0 | 3937 | note this to callees. |
d233b485 CR |
3938 | We pass through PF_ASSIGNRHS as W_ASSIGNRHS if this is on the RHS of an |
3939 | assignment statement. */ | |
3940 | /* The updated treatment of $* is the result of Posix interp 888 */ | |
3941 | /* This was further clarified on the austin-group list in March, 2017 and | |
3942 | in Posix bug 1129 */ | |
3943 | old_nosplit = expand_no_split_dollar_star; | |
3944 | expand_no_split_dollar_star = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || op == '=' || ifs_is_null == 0; /* XXX - was 1 */ | |
712f80b0 CR |
3945 | td.flags = W_EXPANDRHS; /* expanding RHS of ${paramOPword} */ |
3946 | td.flags |= W_NOSPLIT2; /* no splitting, remove "" and '' */ | |
d233b485 CR |
3947 | if (pflags & PF_ASSIGNRHS) /* pass through */ |
3948 | td.flags |= W_ASSIGNRHS; | |
3949 | if (op == '=') | |
3950 | #if 0 | |
3951 | td.flags |= W_ASSIGNRHS; /* expand b in ${a=b} like assignment */ | |
3952 | #else | |
3953 | td.flags |= W_ASSIGNRHS|W_NOASSNTILDE; /* expand b in ${a=b} like assignment */ | |
3954 | #endif | |
3955 | td.word = string; | |
3956 | tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, expanded_p); | |
3957 | expand_no_split_dollar_star = old_nosplit; | |
3958 | ||
3959 | return (tresult); | |
3960 | } | |
3961 | ||
3962 | /* This does not perform word splitting or dequote the WORD_LIST | |
3963 | it returns and it treats $* as if it were quoted. */ | |
3964 | static WORD_LIST * | |
3965 | expand_string_for_pat (string, quoted, dollar_at_p, expanded_p) | |
ccc6cda3 | 3966 | char *string; |
a0c0a00f | 3967 | int quoted, *dollar_at_p, *expanded_p; |
ccc6cda3 JA |
3968 | { |
3969 | WORD_DESC td; | |
3970 | WORD_LIST *tresult; | |
d233b485 | 3971 | int oexp; |
ccc6cda3 JA |
3972 | |
3973 | if (string == 0 || *string == '\0') | |
3974 | return (WORD_LIST *)NULL; | |
3975 | ||
d233b485 | 3976 | oexp = expand_no_split_dollar_star; |
a0c0a00f | 3977 | expand_no_split_dollar_star = 1; |
aeb26a67 | 3978 | td.flags = W_NOSPLIT2; /* no splitting, remove "" and '' */ |
ccc6cda3 | 3979 | td.word = string; |
a0c0a00f | 3980 | tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, expanded_p); |
d233b485 | 3981 | expand_no_split_dollar_star = oexp; |
a0c0a00f | 3982 | |
ccc6cda3 JA |
3983 | return (tresult); |
3984 | } | |
3985 | ||
726f6388 JA |
3986 | /* Expand STRING just as if you were expanding a word. This also returns |
3987 | a list of words. Note that filename globbing is *NOT* done for word | |
3988 | or string expansion, just when the shell is expanding a command. This | |
3989 | does parameter expansion, command substitution, arithmetic expansion, | |
3990 | and word splitting. Dequote the resultant WORD_LIST before returning. */ | |
3991 | WORD_LIST * | |
3992 | expand_string (string, quoted) | |
3993 | char *string; | |
3994 | int quoted; | |
3995 | { | |
3996 | WORD_LIST *result; | |
3997 | ||
28ef6c31 | 3998 | if (string == 0 || *string == '\0') |
726f6388 JA |
3999 | return ((WORD_LIST *)NULL); |
4000 | ||
4001 | result = expand_string_leave_quoted (string, quoted); | |
ccc6cda3 | 4002 | return (result ? dequote_list (result) : result); |
726f6388 JA |
4003 | } |
4004 | ||
d233b485 CR |
4005 | /******************************************* |
4006 | * * | |
4007 | * Functions to expand WORD_DESCs * | |
4008 | * * | |
4009 | *******************************************/ | |
4010 | ||
4011 | /* Expand WORD, performing word splitting on the result. This does | |
4012 | parameter expansion, command substitution, arithmetic expansion, | |
4013 | word splitting, and quote removal. */ | |
4014 | ||
4015 | WORD_LIST * | |
4016 | expand_word (word, quoted) | |
4017 | WORD_DESC *word; | |
4018 | int quoted; | |
4019 | { | |
4020 | WORD_LIST *result, *tresult; | |
4021 | ||
4022 | tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL); | |
4023 | result = word_list_split (tresult); | |
4024 | dispose_words (tresult); | |
4025 | return (result ? dequote_list (result) : result); | |
4026 | } | |
4027 | ||
4028 | /* Expand WORD, but do not perform word splitting on the result. This | |
4029 | does parameter expansion, command substitution, arithmetic expansion, | |
4030 | and quote removal. */ | |
4031 | WORD_LIST * | |
4032 | expand_word_unsplit (word, quoted) | |
4033 | WORD_DESC *word; | |
4034 | int quoted; | |
4035 | { | |
4036 | WORD_LIST *result; | |
4037 | ||
4038 | result = expand_word_leave_quoted (word, quoted); | |
4039 | return (result ? dequote_list (result) : result); | |
4040 | } | |
4041 | ||
4042 | /* Perform shell expansions on WORD, but do not perform word splitting or | |
4043 | quote removal on the result. Virtually identical to expand_word_unsplit; | |
4044 | could be combined if implementations don't diverge. */ | |
4045 | WORD_LIST * | |
4046 | expand_word_leave_quoted (word, quoted) | |
4047 | WORD_DESC *word; | |
4048 | int quoted; | |
4049 | { | |
4050 | WORD_LIST *result; | |
4051 | ||
4052 | expand_no_split_dollar_star = 1; | |
4053 | if (ifs_is_null) | |
4054 | word->flags |= W_NOSPLIT; | |
4055 | word->flags |= W_NOSPLIT2; | |
4056 | result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL); | |
4057 | expand_no_split_dollar_star = 0; | |
4058 | ||
4059 | return result; | |
4060 | } | |
4061 | ||
726f6388 JA |
4062 | /*************************************************** |
4063 | * * | |
4064 | * Functions to handle quoting chars * | |
4065 | * * | |
4066 | ***************************************************/ | |
4067 | ||
cce855bc JA |
4068 | /* Conventions: |
4069 | ||
4070 | A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string. | |
4071 | The parser passes CTLNUL as CTLESC CTLNUL. */ | |
4072 | ||
cce855bc JA |
4073 | /* Quote escape characters in string s, but no other characters. This is |
4074 | used to protect CTLESC and CTLNUL in variable values from the rest of | |
3185942a JA |
4075 | the word expansion process after the variable is expanded (word splitting |
4076 | and filename generation). If IFS is null, we quote spaces as well, just | |
4077 | in case we split on spaces later (in the case of unquoted $@, we will | |
4078 | eventually attempt to split the entire word on spaces). Corresponding | |
4079 | code exists in dequote_escapes. Even if we don't end up splitting on | |
4080 | spaces, quoting spaces is not a problem. This should never be called on | |
4081 | a string that is quoted with single or double quotes or part of a here | |
d233b485 CR |
4082 | document (effectively double-quoted). |
4083 | FLAGS says whether or not we are going to split the result. If we are not, | |
4084 | and there is a CTLESC or CTLNUL in IFS, we need to quote CTLESC and CTLNUL, | |
4085 | respectively, to prevent them from being removed as part of dequoting. */ | |
4086 | static char * | |
4087 | quote_escapes_internal (string, flags) | |
4088 | const char *string; | |
4089 | int flags; | |
cce855bc | 4090 | { |
d233b485 CR |
4091 | const char *s, *send; |
4092 | char *t, *result; | |
7117c2d2 | 4093 | size_t slen; |
d233b485 | 4094 | int quote_spaces, skip_ctlesc, skip_ctlnul, nosplit; |
7117c2d2 | 4095 | DECLARE_MBSTATE; |
cce855bc | 4096 | |
7117c2d2 JA |
4097 | slen = strlen (string); |
4098 | send = string + slen; | |
4099 | ||
f1be666c | 4100 | quote_spaces = (ifs_value && *ifs_value == 0); |
d233b485 | 4101 | nosplit = (flags & PF_NOSPLIT2); |
3185942a JA |
4102 | |
4103 | for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++) | |
d233b485 CR |
4104 | { |
4105 | skip_ctlesc |= (nosplit == 0 && *s == CTLESC); | |
4106 | skip_ctlnul |= (nosplit == 0 && *s == CTLNUL); | |
4107 | } | |
3185942a | 4108 | |
7117c2d2 JA |
4109 | t = result = (char *)xmalloc ((slen * 2) + 1); |
4110 | s = string; | |
4111 | ||
4112 | while (*s) | |
cce855bc | 4113 | { |
3185942a | 4114 | if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' ')) |
cce855bc | 4115 | *t++ = CTLESC; |
7117c2d2 | 4116 | COPY_CHAR_P (t, s, send); |
cce855bc JA |
4117 | } |
4118 | *t = '\0'; | |
ac50fbac | 4119 | |
cce855bc JA |
4120 | return (result); |
4121 | } | |
4122 | ||
d233b485 CR |
4123 | char * |
4124 | quote_escapes (string) | |
4125 | const char *string; | |
4126 | { | |
4127 | return (quote_escapes_internal (string, 0)); | |
4128 | } | |
4129 | ||
4130 | char * | |
4131 | quote_rhs (string) | |
4132 | const char *string; | |
4133 | { | |
4134 | return (quote_escapes_internal (string, PF_NOSPLIT2)); | |
4135 | } | |
4136 | ||
cce855bc JA |
4137 | static WORD_LIST * |
4138 | list_quote_escapes (list) | |
4139 | WORD_LIST *list; | |
4140 | { | |
4141 | register WORD_LIST *w; | |
4142 | char *t; | |
4143 | ||
4144 | for (w = list; w; w = w->next) | |
4145 | { | |
4146 | t = w->word->word; | |
4147 | w->word->word = quote_escapes (t); | |
4148 | free (t); | |
4149 | } | |
4150 | return list; | |
4151 | } | |
4152 | ||
7117c2d2 JA |
4153 | /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL. |
4154 | ||
4155 | The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL. | |
4156 | This is necessary to make unquoted CTLESC and CTLNUL characters in the | |
4157 | data stream pass through properly. | |
4158 | ||
4159 | We need to remove doubled CTLESC characters inside quoted strings before | |
4160 | quoting the entire string, so we do not double the number of CTLESC | |
4161 | characters. | |
4162 | ||
4163 | Also used by parts of the pattern substitution code. */ | |
3185942a | 4164 | char * |
cce855bc | 4165 | dequote_escapes (string) |
d233b485 | 4166 | const char *string; |
cce855bc | 4167 | { |
d233b485 CR |
4168 | const char *s, *send; |
4169 | char *t, *result; | |
7117c2d2 | 4170 | size_t slen; |
f1be666c | 4171 | int quote_spaces; |
7117c2d2 | 4172 | DECLARE_MBSTATE; |
cce855bc | 4173 | |
7117c2d2 | 4174 | if (string == 0) |
d233b485 | 4175 | return (char *)0; |
7117c2d2 JA |
4176 | |
4177 | slen = strlen (string); | |
4178 | send = string + slen; | |
4179 | ||
4180 | t = result = (char *)xmalloc (slen + 1); | |
7117c2d2 JA |
4181 | |
4182 | if (strchr (string, CTLESC) == 0) | |
3185942a | 4183 | return (strcpy (result, string)); |
7117c2d2 | 4184 | |
f1be666c | 4185 | quote_spaces = (ifs_value && *ifs_value == 0); |
3185942a JA |
4186 | |
4187 | s = string; | |
7117c2d2 | 4188 | while (*s) |
cce855bc | 4189 | { |
f1be666c | 4190 | if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' '))) |
cce855bc JA |
4191 | { |
4192 | s++; | |
4193 | if (*s == '\0') | |
4194 | break; | |
4195 | } | |
7117c2d2 | 4196 | COPY_CHAR_P (t, s, send); |
cce855bc JA |
4197 | } |
4198 | *t = '\0'; | |
ac50fbac | 4199 | |
cce855bc JA |
4200 | return result; |
4201 | } | |
726f6388 | 4202 | |
d233b485 | 4203 | #if defined (INCLUDE_UNUSED) |
ac50fbac CR |
4204 | static WORD_LIST * |
4205 | list_dequote_escapes (list) | |
4206 | WORD_LIST *list; | |
4207 | { | |
4208 | register WORD_LIST *w; | |
4209 | char *t; | |
4210 | ||
4211 | for (w = list; w; w = w->next) | |
4212 | { | |
4213 | t = w->word->word; | |
4214 | w->word->word = dequote_escapes (t); | |
4215 | free (t); | |
4216 | } | |
4217 | return list; | |
4218 | } | |
d233b485 | 4219 | #endif |
ac50fbac | 4220 | |
0628567a JA |
4221 | /* Return a new string with the quoted representation of character C. |
4222 | This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be | |
4223 | set in any resultant WORD_DESC where this value is the word. */ | |
726f6388 JA |
4224 | static char * |
4225 | make_quoted_char (c) | |
4226 | int c; | |
4227 | { | |
4228 | char *temp; | |
4229 | ||
f73dda09 | 4230 | temp = (char *)xmalloc (3); |
726f6388 JA |
4231 | if (c == 0) |
4232 | { | |
4233 | temp[0] = CTLNUL; | |
4234 | temp[1] = '\0'; | |
4235 | } | |
4236 | else | |
4237 | { | |
4238 | temp[0] = CTLESC; | |
4239 | temp[1] = c; | |
4240 | temp[2] = '\0'; | |
4241 | } | |
4242 | return (temp); | |
4243 | } | |
4244 | ||
0628567a JA |
4245 | /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so |
4246 | the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where | |
4247 | this value is the word. */ | |
ccc6cda3 | 4248 | char * |
726f6388 JA |
4249 | quote_string (string) |
4250 | char *string; | |
4251 | { | |
ccc6cda3 | 4252 | register char *t; |
7117c2d2 JA |
4253 | size_t slen; |
4254 | char *result, *send; | |
726f6388 | 4255 | |
ccc6cda3 | 4256 | if (*string == 0) |
726f6388 | 4257 | { |
f73dda09 | 4258 | result = (char *)xmalloc (2); |
726f6388 JA |
4259 | result[0] = CTLNUL; |
4260 | result[1] = '\0'; | |
4261 | } | |
4262 | else | |
4263 | { | |
7117c2d2 | 4264 | DECLARE_MBSTATE; |
726f6388 | 4265 | |
7117c2d2 JA |
4266 | slen = strlen (string); |
4267 | send = string + slen; | |
4268 | ||
4269 | result = (char *)xmalloc ((slen * 2) + 1); | |
4270 | ||
4271 | for (t = result; string < send; ) | |
726f6388 JA |
4272 | { |
4273 | *t++ = CTLESC; | |
7117c2d2 | 4274 | COPY_CHAR_P (t, string, send); |
726f6388 JA |
4275 | } |
4276 | *t = '\0'; | |
4277 | } | |
4278 | return (result); | |
4279 | } | |
4280 | ||
0628567a | 4281 | /* De-quote quoted characters in STRING. */ |
726f6388 JA |
4282 | char * |
4283 | dequote_string (string) | |
4284 | char *string; | |
4285 | { | |
7117c2d2 JA |
4286 | register char *s, *t; |
4287 | size_t slen; | |
4288 | char *result, *send; | |
4289 | DECLARE_MBSTATE; | |
726f6388 | 4290 | |
a0c0a00f CR |
4291 | #if defined (DEBUG) |
4292 | if (string[0] == CTLESC && string[1] == 0) | |
4293 | internal_inform ("dequote_string: string with bare CTLESC"); | |
4294 | #endif | |
4295 | ||
d233b485 | 4296 | slen = STRLEN (string); |
7117c2d2 JA |
4297 | |
4298 | t = result = (char *)xmalloc (slen + 1); | |
726f6388 JA |
4299 | |
4300 | if (QUOTED_NULL (string)) | |
4301 | { | |
4302 | result[0] = '\0'; | |
4303 | return (result); | |
4304 | } | |
4305 | ||
a0c0a00f CR |
4306 | /* A string consisting of only a single CTLESC should pass through unchanged */ |
4307 | if (string[0] == CTLESC && string[1] == 0) | |
4308 | { | |
4309 | result[0] = CTLESC; | |
4310 | result[1] = '\0'; | |
4311 | return (result); | |
4312 | } | |
4313 | ||
726f6388 JA |
4314 | /* If no character in the string can be quoted, don't bother examining |
4315 | each character. Just return a copy of the string passed to us. */ | |
7117c2d2 JA |
4316 | if (strchr (string, CTLESC) == NULL) |
4317 | return (strcpy (result, string)); | |
726f6388 | 4318 | |
7117c2d2 JA |
4319 | send = string + slen; |
4320 | s = string; | |
4321 | while (*s) | |
726f6388 | 4322 | { |
7117c2d2 | 4323 | if (*s == CTLESC) |
726f6388 | 4324 | { |
7117c2d2 JA |
4325 | s++; |
4326 | if (*s == '\0') | |
726f6388 JA |
4327 | break; |
4328 | } | |
7117c2d2 | 4329 | COPY_CHAR_P (t, s, send); |
726f6388 JA |
4330 | } |
4331 | ||
4332 | *t = '\0'; | |
4333 | return (result); | |
4334 | } | |
4335 | ||
4336 | /* Quote the entire WORD_LIST list. */ | |
ccc6cda3 | 4337 | static WORD_LIST * |
726f6388 JA |
4338 | quote_list (list) |
4339 | WORD_LIST *list; | |
4340 | { | |
4341 | register WORD_LIST *w; | |
ccc6cda3 | 4342 | char *t; |
726f6388 JA |
4343 | |
4344 | for (w = list; w; w = w->next) | |
4345 | { | |
ccc6cda3 | 4346 | t = w->word->word; |
726f6388 | 4347 | w->word->word = quote_string (t); |
3185942a JA |
4348 | if (*t == 0) |
4349 | w->word->flags |= W_HASQUOTEDNULL; /* XXX - turn on W_HASQUOTEDNULL here? */ | |
ccc6cda3 | 4350 | w->word->flags |= W_QUOTED; |
3185942a | 4351 | free (t); |
726f6388 | 4352 | } |
ccc6cda3 | 4353 | return list; |
726f6388 JA |
4354 | } |
4355 | ||
d233b485 CR |
4356 | WORD_DESC * |
4357 | dequote_word (word) | |
4358 | WORD_DESC *word; | |
4359 | { | |
4360 | register char *s; | |
4361 | ||
4362 | s = dequote_string (word->word); | |
4363 | if (QUOTED_NULL (word->word)) | |
4364 | word->flags &= ~W_HASQUOTEDNULL; | |
4365 | free (word->word); | |
4366 | word->word = s; | |
4367 | ||
4368 | return word; | |
4369 | } | |
4370 | ||
0628567a JA |
4371 | /* De-quote quoted characters in each word in LIST. */ |
4372 | WORD_LIST * | |
7117c2d2 JA |
4373 | dequote_list (list) |
4374 | WORD_LIST *list; | |
4375 | { | |
4376 | register char *s; | |
4377 | register WORD_LIST *tlist; | |
4378 | ||
4379 | for (tlist = list; tlist; tlist = tlist->next) | |
4380 | { | |
4381 | s = dequote_string (tlist->word->word); | |
3185942a JA |
4382 | if (QUOTED_NULL (tlist->word->word)) |
4383 | tlist->word->flags &= ~W_HASQUOTEDNULL; | |
7117c2d2 JA |
4384 | free (tlist->word->word); |
4385 | tlist->word->word = s; | |
4386 | } | |
4387 | return list; | |
4388 | } | |
4389 | ||
4390 | /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed | |
4391 | string. */ | |
3185942a | 4392 | char * |
7117c2d2 JA |
4393 | remove_quoted_escapes (string) |
4394 | char *string; | |
4395 | { | |
4396 | char *t; | |
4397 | ||
4398 | if (string) | |
4399 | { | |
4400 | t = dequote_escapes (string); | |
4401 | strcpy (string, t); | |
4402 | free (t); | |
4403 | } | |
4404 | ||
4405 | return (string); | |
4406 | } | |
4407 | ||
d233b485 CR |
4408 | /* Remove quoted $IFS characters from STRING. Quoted IFS characters are |
4409 | added to protect them from word splitting, but we need to remove them | |
4410 | if no word splitting takes place. This returns newly-allocated memory, | |
4411 | so callers can use it to replace savestring(). */ | |
4412 | char * | |
4413 | remove_quoted_ifs (string) | |
4414 | char *string; | |
4415 | { | |
4416 | register size_t slen; | |
4417 | register int i, j; | |
4418 | char *ret, *send; | |
4419 | DECLARE_MBSTATE; | |
4420 | ||
4421 | slen = strlen (string); | |
4422 | send = string + slen; | |
4423 | ||
4424 | i = j = 0; | |
4425 | ret = (char *)xmalloc (slen + 1); | |
4426 | ||
4427 | while (i < slen) | |
4428 | { | |
4429 | if (string[i] == CTLESC) | |
4430 | { | |
4431 | i++; | |
4432 | if (string[i] == 0 || isifs (string[i]) == 0) | |
4433 | ret[j++] = CTLESC; | |
4434 | if (i == slen) | |
4435 | break; | |
4436 | } | |
4437 | ||
4438 | COPY_CHAR_I (ret, j, string, send, i); | |
4439 | } | |
4440 | ret[j] = '\0'; | |
4441 | ||
4442 | return (ret); | |
4443 | } | |
4444 | ||
3185942a | 4445 | char * |
cce855bc JA |
4446 | remove_quoted_nulls (string) |
4447 | char *string; | |
4448 | { | |
7117c2d2 JA |
4449 | register size_t slen; |
4450 | register int i, j, prev_i; | |
4451 | DECLARE_MBSTATE; | |
4452 | ||
4453 | if (strchr (string, CTLNUL) == 0) /* XXX */ | |
4454 | return string; /* XXX */ | |
4455 | ||
4456 | slen = strlen (string); | |
4457 | i = j = 0; | |
4458 | ||
4459 | while (i < slen) | |
4460 | { | |
4461 | if (string[i] == CTLESC) | |
b80f6443 JA |
4462 | { |
4463 | /* Old code had j++, but we cannot assume that i == j at this | |
4464 | point -- what if a CTLNUL has already been removed from the | |
4465 | string? We don't want to drop the CTLESC or recopy characters | |
4466 | that we've already copied down. */ | |
d233b485 CR |
4467 | i++; |
4468 | string[j++] = CTLESC; | |
b80f6443 JA |
4469 | if (i == slen) |
4470 | break; | |
4471 | } | |
7117c2d2 | 4472 | else if (string[i] == CTLNUL) |
a48a8ac3 CR |
4473 | { |
4474 | i++; | |
4475 | continue; | |
4476 | } | |
7117c2d2 JA |
4477 | |
4478 | prev_i = i; | |
d233b485 | 4479 | ADVANCE_CHAR (string, slen, i); /* COPY_CHAR_I? */ |
7117c2d2 | 4480 | if (j < prev_i) |
cce855bc | 4481 | { |
7117c2d2 | 4482 | do string[j++] = string[prev_i++]; while (prev_i < i); |
cce855bc | 4483 | } |
7117c2d2 JA |
4484 | else |
4485 | j = i; | |
cce855bc | 4486 | } |
7117c2d2 JA |
4487 | string[j] = '\0'; |
4488 | ||
4489 | return (string); | |
cce855bc JA |
4490 | } |
4491 | ||
4492 | /* Perform quoted null character removal on each element of LIST. | |
4493 | This modifies LIST. */ | |
4494 | void | |
4495 | word_list_remove_quoted_nulls (list) | |
4496 | WORD_LIST *list; | |
4497 | { | |
4498 | register WORD_LIST *t; | |
4499 | ||
4500 | for (t = list; t; t = t->next) | |
95732b49 JA |
4501 | { |
4502 | remove_quoted_nulls (t->word->word); | |
4503 | t->word->flags &= ~W_HASQUOTEDNULL; | |
4504 | } | |
cce855bc JA |
4505 | } |
4506 | ||
4507 | /* **************************************************************** */ | |
4508 | /* */ | |
4509 | /* Functions for Matching and Removing Patterns */ | |
4510 | /* */ | |
4511 | /* **************************************************************** */ | |
4512 | ||
b80f6443 | 4513 | #if defined (HANDLE_MULTIBYTE) |
d233b485 | 4514 | # ifdef INCLUDE_UNUSED |
b80f6443 JA |
4515 | static unsigned char * |
4516 | mb_getcharlens (string, len) | |
4517 | char *string; | |
4518 | int len; | |
4519 | { | |
4520 | int i, offset, last; | |
4521 | unsigned char *ret; | |
4522 | char *p; | |
4523 | DECLARE_MBSTATE; | |
4524 | ||
4525 | i = offset = 0; | |
4526 | last = 0; | |
4527 | ret = (unsigned char *)xmalloc (len); | |
4528 | memset (ret, 0, len); | |
4529 | while (string[last]) | |
4530 | { | |
4531 | ADVANCE_CHAR (string, len, offset); | |
4532 | ret[last] = offset - last; | |
4533 | last = offset; | |
4534 | } | |
4535 | return ret; | |
4536 | } | |
d233b485 | 4537 | # endif |
b80f6443 JA |
4538 | #endif |
4539 | ||
cce855bc JA |
4540 | /* Remove the portion of PARAM matched by PATTERN according to OP, where OP |
4541 | can have one of 4 values: | |
4542 | RP_LONG_LEFT remove longest matching portion at start of PARAM | |
726f6388 JA |
4543 | RP_SHORT_LEFT remove shortest matching portion at start of PARAM |
4544 | RP_LONG_RIGHT remove longest matching portion at end of PARAM | |
4545 | RP_SHORT_RIGHT remove shortest matching portion at end of PARAM | |
4546 | */ | |
4547 | ||
4548 | #define RP_LONG_LEFT 1 | |
4549 | #define RP_SHORT_LEFT 2 | |
4550 | #define RP_LONG_RIGHT 3 | |
4551 | #define RP_SHORT_RIGHT 4 | |
4552 | ||
495aee44 | 4553 | /* Returns its first argument if nothing matched; new memory otherwise */ |
726f6388 | 4554 | static char * |
b80f6443 | 4555 | remove_upattern (param, pattern, op) |
726f6388 JA |
4556 | char *param, *pattern; |
4557 | int op; | |
4558 | { | |
a0c0a00f | 4559 | register size_t len; |
ccc6cda3 | 4560 | register char *end; |
726f6388 JA |
4561 | register char *p, *ret, c; |
4562 | ||
ccc6cda3 JA |
4563 | len = STRLEN (param); |
4564 | end = param + len; | |
726f6388 JA |
4565 | |
4566 | switch (op) | |
4567 | { | |
4568 | case RP_LONG_LEFT: /* remove longest match at start */ | |
4569 | for (p = end; p >= param; p--) | |
4570 | { | |
4571 | c = *p; *p = '\0'; | |
f73dda09 | 4572 | if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
726f6388 JA |
4573 | { |
4574 | *p = c; | |
4575 | return (savestring (p)); | |
4576 | } | |
4577 | *p = c; | |
b80f6443 | 4578 | |
726f6388 JA |
4579 | } |
4580 | break; | |
4581 | ||
4582 | case RP_SHORT_LEFT: /* remove shortest match at start */ | |
4583 | for (p = param; p <= end; p++) | |
4584 | { | |
4585 | c = *p; *p = '\0'; | |
f73dda09 | 4586 | if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
726f6388 JA |
4587 | { |
4588 | *p = c; | |
4589 | return (savestring (p)); | |
4590 | } | |
4591 | *p = c; | |
4592 | } | |
4593 | break; | |
4594 | ||
ccc6cda3 JA |
4595 | case RP_LONG_RIGHT: /* remove longest match at end */ |
4596 | for (p = param; p <= end; p++) | |
4597 | { | |
f73dda09 | 4598 | if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
ccc6cda3 JA |
4599 | { |
4600 | c = *p; *p = '\0'; | |
4601 | ret = savestring (param); | |
4602 | *p = c; | |
4603 | return (ret); | |
4604 | } | |
4605 | } | |
4606 | break; | |
4607 | ||
4608 | case RP_SHORT_RIGHT: /* remove shortest match at end */ | |
4609 | for (p = end; p >= param; p--) | |
4610 | { | |
f73dda09 | 4611 | if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH) |
ccc6cda3 JA |
4612 | { |
4613 | c = *p; *p = '\0'; | |
4614 | ret = savestring (param); | |
4615 | *p = c; | |
4616 | return (ret); | |
4617 | } | |
4618 | } | |
4619 | break; | |
4620 | } | |
b80f6443 | 4621 | |
495aee44 | 4622 | return (param); /* no match, return original string */ |
ccc6cda3 JA |
4623 | } |
4624 | ||
b80f6443 | 4625 | #if defined (HANDLE_MULTIBYTE) |
495aee44 | 4626 | /* Returns its first argument if nothing matched; new memory otherwise */ |
b80f6443 JA |
4627 | static wchar_t * |
4628 | remove_wpattern (wparam, wstrlen, wpattern, op) | |
4629 | wchar_t *wparam; | |
4630 | size_t wstrlen; | |
4631 | wchar_t *wpattern; | |
4632 | int op; | |
4633 | { | |
0628567a JA |
4634 | wchar_t wc, *ret; |
4635 | int n; | |
b80f6443 JA |
4636 | |
4637 | switch (op) | |
4638 | { | |
4639 | case RP_LONG_LEFT: /* remove longest match at start */ | |
4640 | for (n = wstrlen; n >= 0; n--) | |
4641 | { | |
4642 | wc = wparam[n]; wparam[n] = L'\0'; | |
4643 | if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4644 | { | |
4645 | wparam[n] = wc; | |
4646 | return (wcsdup (wparam + n)); | |
4647 | } | |
4648 | wparam[n] = wc; | |
4649 | } | |
4650 | break; | |
4651 | ||
4652 | case RP_SHORT_LEFT: /* remove shortest match at start */ | |
4653 | for (n = 0; n <= wstrlen; n++) | |
4654 | { | |
4655 | wc = wparam[n]; wparam[n] = L'\0'; | |
4656 | if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4657 | { | |
4658 | wparam[n] = wc; | |
4659 | return (wcsdup (wparam + n)); | |
4660 | } | |
4661 | wparam[n] = wc; | |
4662 | } | |
4663 | break; | |
4664 | ||
4665 | case RP_LONG_RIGHT: /* remove longest match at end */ | |
4666 | for (n = 0; n <= wstrlen; n++) | |
4667 | { | |
4668 | if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4669 | { | |
4670 | wc = wparam[n]; wparam[n] = L'\0'; | |
4671 | ret = wcsdup (wparam); | |
4672 | wparam[n] = wc; | |
4673 | return (ret); | |
4674 | } | |
4675 | } | |
4676 | break; | |
4677 | ||
4678 | case RP_SHORT_RIGHT: /* remove shortest match at end */ | |
4679 | for (n = wstrlen; n >= 0; n--) | |
4680 | { | |
4681 | if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH) | |
4682 | { | |
4683 | wc = wparam[n]; wparam[n] = L'\0'; | |
4684 | ret = wcsdup (wparam); | |
4685 | wparam[n] = wc; | |
4686 | return (ret); | |
4687 | } | |
4688 | } | |
4689 | break; | |
4690 | } | |
4691 | ||
495aee44 | 4692 | return (wparam); /* no match, return original string */ |
b80f6443 JA |
4693 | } |
4694 | #endif /* HANDLE_MULTIBYTE */ | |
4695 | ||
4696 | static char * | |
4697 | remove_pattern (param, pattern, op) | |
4698 | char *param, *pattern; | |
4699 | int op; | |
4700 | { | |
495aee44 CR |
4701 | char *xret; |
4702 | ||
b80f6443 JA |
4703 | if (param == NULL) |
4704 | return (param); | |
4705 | if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */ | |
4706 | return (savestring (param)); | |
4707 | ||
4708 | #if defined (HANDLE_MULTIBYTE) | |
4709 | if (MB_CUR_MAX > 1) | |
4710 | { | |
4711 | wchar_t *ret, *oret; | |
4712 | size_t n; | |
4713 | wchar_t *wparam, *wpattern; | |
4714 | mbstate_t ps; | |
b80f6443 | 4715 | |
d233b485 CR |
4716 | /* XXX - could optimize here by checking param and pattern for multibyte |
4717 | chars with mbsmbchar and calling remove_upattern. */ | |
4718 | ||
b80f6443 JA |
4719 | n = xdupmbstowcs (&wpattern, NULL, pattern); |
4720 | if (n == (size_t)-1) | |
495aee44 CR |
4721 | { |
4722 | xret = remove_upattern (param, pattern, op); | |
4723 | return ((xret == param) ? savestring (param) : xret); | |
4724 | } | |
b80f6443 | 4725 | n = xdupmbstowcs (&wparam, NULL, param); |
ac50fbac | 4726 | |
b80f6443 JA |
4727 | if (n == (size_t)-1) |
4728 | { | |
4729 | free (wpattern); | |
495aee44 CR |
4730 | xret = remove_upattern (param, pattern, op); |
4731 | return ((xret == param) ? savestring (param) : xret); | |
b80f6443 JA |
4732 | } |
4733 | oret = ret = remove_wpattern (wparam, n, wpattern, op); | |
495aee44 CR |
4734 | /* Don't bother to convert wparam back to multibyte string if nothing |
4735 | matched; just return copy of original string */ | |
4736 | if (ret == wparam) | |
4737 | { | |
4738 | free (wparam); | |
4739 | free (wpattern); | |
4740 | return (savestring (param)); | |
4741 | } | |
b80f6443 JA |
4742 | |
4743 | free (wparam); | |
4744 | free (wpattern); | |
4745 | ||
4746 | n = strlen (param); | |
0628567a | 4747 | xret = (char *)xmalloc (n + 1); |
b80f6443 JA |
4748 | memset (&ps, '\0', sizeof (mbstate_t)); |
4749 | n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps); | |
4750 | xret[n] = '\0'; /* just to make sure */ | |
4751 | free (oret); | |
4752 | return xret; | |
4753 | } | |
4754 | else | |
4755 | #endif | |
ccc6cda3 | 4756 | { |
495aee44 CR |
4757 | xret = remove_upattern (param, pattern, op); |
4758 | return ((xret == param) ? savestring (param) : xret); | |
ccc6cda3 JA |
4759 | } |
4760 | } | |
4761 | ||
4762 | /* Match PAT anywhere in STRING and return the match boundaries. | |
4763 | This returns 1 in case of a successful match, 0 otherwise. SP | |
4764 | and EP are pointers into the string where the match begins and | |
4765 | ends, respectively. MTYPE controls what kind of match is attempted. | |
4766 | MATCH_BEG and MATCH_END anchor the match at the beginning and end | |
4767 | of the string, respectively. The longest match is returned. */ | |
4768 | static int | |
b80f6443 | 4769 | match_upattern (string, pat, mtype, sp, ep) |
ccc6cda3 JA |
4770 | char *string, *pat; |
4771 | int mtype; | |
4772 | char **sp, **ep; | |
4773 | { | |
a0c0a00f CR |
4774 | int c, mlen; |
4775 | size_t len; | |
95732b49 | 4776 | register char *p, *p1, *npat; |
ccc6cda3 JA |
4777 | char *end; |
4778 | ||
95732b49 JA |
4779 | /* If the pattern doesn't match anywhere in the string, go ahead and |
4780 | short-circuit right away. A minor optimization, saves a bunch of | |
4781 | unnecessary calls to strmatch (up to N calls for a string of N | |
4782 | characters) if the match is unsuccessful. To preserve the semantics | |
4783 | of the substring matches below, we make sure that the pattern has | |
4784 | `*' as first and last character, making a new pattern if necessary. */ | |
4785 | /* XXX - check this later if I ever implement `**' with special meaning, | |
4786 | since this will potentially result in `**' at the beginning or end */ | |
4787 | len = STRLEN (pat); | |
0001803f | 4788 | if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*') |
95732b49 | 4789 | { |
a0c0a00f CR |
4790 | int unescaped_backslash; |
4791 | char *pp; | |
4792 | ||
0628567a | 4793 | p = npat = (char *)xmalloc (len + 3); |
95732b49 | 4794 | p1 = pat; |
d233b485 | 4795 | if ((mtype != MATCH_BEG) && (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob))) |
95732b49 JA |
4796 | *p++ = '*'; |
4797 | while (*p1) | |
4798 | *p++ = *p1++; | |
a0c0a00f CR |
4799 | #if 1 |
4800 | /* Need to also handle a pattern that ends with an unescaped backslash. | |
4801 | For right now, we ignore it because the pattern matching code will | |
4802 | fail the match anyway */ | |
4803 | /* If the pattern ends with a `*' we leave it alone if it's preceded by | |
4804 | an even number of backslashes, but if it's escaped by a backslash | |
4805 | we need to add another `*'. */ | |
d233b485 | 4806 | if ((mtype != MATCH_END) && (p1[-1] == '*' && (unescaped_backslash = p1[-2] == '\\'))) |
a0c0a00f CR |
4807 | { |
4808 | pp = p1 - 3; | |
4809 | while (pp >= pat && *pp-- == '\\') | |
4810 | unescaped_backslash = 1 - unescaped_backslash; | |
4811 | if (unescaped_backslash) | |
4812 | *p++ = '*'; | |
4813 | } | |
d233b485 | 4814 | else if (mtype != MATCH_END && p1[-1] != '*') |
95732b49 | 4815 | *p++ = '*'; |
a0c0a00f CR |
4816 | #else |
4817 | if (p1[-1] != '*' || p1[-2] == '\\') | |
4818 | *p++ = '*'; | |
4819 | #endif | |
95732b49 JA |
4820 | *p = '\0'; |
4821 | } | |
4822 | else | |
4823 | npat = pat; | |
a0c0a00f | 4824 | c = strmatch (npat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE); |
95732b49 JA |
4825 | if (npat != pat) |
4826 | free (npat); | |
4827 | if (c == FNM_NOMATCH) | |
4828 | return (0); | |
4829 | ||
b80f6443 JA |
4830 | len = STRLEN (string); |
4831 | end = string + len; | |
ccc6cda3 | 4832 | |
495aee44 | 4833 | mlen = umatchlen (pat, len); |
712f80b0 CR |
4834 | if (mlen > (int)len) |
4835 | return (0); | |
495aee44 | 4836 | |
ccc6cda3 JA |
4837 | switch (mtype) |
4838 | { | |
4839 | case MATCH_ANY: | |
4840 | for (p = string; p <= end; p++) | |
4841 | { | |
a0c0a00f | 4842 | if (match_pattern_char (pat, p, FNMATCH_IGNCASE)) |
ccc6cda3 | 4843 | { |
495aee44 CR |
4844 | p1 = (mlen == -1) ? end : p + mlen; |
4845 | /* p1 - p = length of portion of string to be considered | |
4846 | p = current position in string | |
4847 | mlen = number of characters consumed by match (-1 for entire string) | |
4848 | end = end of string | |
4849 | we want to break immediately if the potential match len | |
4850 | is greater than the number of characters remaining in the | |
4851 | string | |
4852 | */ | |
4853 | if (p1 > end) | |
4854 | break; | |
4855 | for ( ; p1 >= p; p1--) | |
ccc6cda3 JA |
4856 | { |
4857 | c = *p1; *p1 = '\0'; | |
a0c0a00f | 4858 | if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
ccc6cda3 JA |
4859 | { |
4860 | *p1 = c; | |
4861 | *sp = p; | |
4862 | *ep = p1; | |
4863 | return 1; | |
4864 | } | |
4865 | *p1 = c; | |
495aee44 CR |
4866 | #if 1 |
4867 | /* If MLEN != -1, we have a fixed length pattern. */ | |
4868 | if (mlen != -1) | |
4869 | break; | |
4870 | #endif | |
ccc6cda3 JA |
4871 | } |
4872 | } | |
4873 | } | |
b80f6443 | 4874 | |
ccc6cda3 JA |
4875 | return (0); |
4876 | ||
4877 | case MATCH_BEG: | |
a0c0a00f | 4878 | if (match_pattern_char (pat, string, FNMATCH_IGNCASE) == 0) |
28ef6c31 | 4879 | return (0); |
b80f6443 | 4880 | |
495aee44 | 4881 | for (p = (mlen == -1) ? end : string + mlen; p >= string; p--) |
ccc6cda3 JA |
4882 | { |
4883 | c = *p; *p = '\0'; | |
a0c0a00f | 4884 | if (strmatch (pat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
ccc6cda3 JA |
4885 | { |
4886 | *p = c; | |
4887 | *sp = string; | |
4888 | *ep = p; | |
4889 | return 1; | |
4890 | } | |
4891 | *p = c; | |
495aee44 CR |
4892 | /* If MLEN != -1, we have a fixed length pattern. */ |
4893 | if (mlen != -1) | |
4894 | break; | |
ccc6cda3 | 4895 | } |
b80f6443 | 4896 | |
ccc6cda3 | 4897 | return (0); |
726f6388 | 4898 | |
ccc6cda3 | 4899 | case MATCH_END: |
495aee44 | 4900 | for (p = end - ((mlen == -1) ? len : mlen); p <= end; p++) |
b80f6443 | 4901 | { |
a0c0a00f | 4902 | if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
4903 | { |
4904 | *sp = p; | |
4905 | *ep = end; | |
4906 | return 1; | |
4907 | } | |
495aee44 CR |
4908 | /* If MLEN != -1, we have a fixed length pattern. */ |
4909 | if (mlen != -1) | |
4910 | break; | |
b80f6443 JA |
4911 | } |
4912 | ||
4913 | return (0); | |
4914 | } | |
4915 | ||
4916 | return (0); | |
4917 | } | |
4918 | ||
4919 | #if defined (HANDLE_MULTIBYTE) | |
a0c0a00f CR |
4920 | |
4921 | #define WFOLD(c) (match_ignore_case && iswupper (c) ? towlower (c) : (c)) | |
4922 | ||
b80f6443 JA |
4923 | /* Match WPAT anywhere in WSTRING and return the match boundaries. |
4924 | This returns 1 in case of a successful match, 0 otherwise. Wide | |
4925 | character version. */ | |
4926 | static int | |
4927 | match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep) | |
4928 | wchar_t *wstring; | |
4929 | char **indices; | |
4930 | size_t wstrlen; | |
4931 | wchar_t *wpat; | |
4932 | int mtype; | |
4933 | char **sp, **ep; | |
4934 | { | |
95732b49 | 4935 | wchar_t wc, *wp, *nwpat, *wp1; |
495aee44 CR |
4936 | size_t len; |
4937 | int mlen; | |
4938 | int n, n1, n2, simple; | |
4939 | ||
4940 | simple = (wpat[0] != L'\\' && wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'['); | |
4941 | #if defined (EXTENDED_GLOB) | |
4942 | if (extended_glob) | |
91717ba3 | 4943 | simple &= (wpat[1] != L'(' || (wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'+' && wpat[0] != L'!' && wpat[0] != L'@')); /*)*/ |
b80f6443 JA |
4944 | #endif |
4945 | ||
95732b49 JA |
4946 | /* If the pattern doesn't match anywhere in the string, go ahead and |
4947 | short-circuit right away. A minor optimization, saves a bunch of | |
4948 | unnecessary calls to strmatch (up to N calls for a string of N | |
4949 | characters) if the match is unsuccessful. To preserve the semantics | |
4950 | of the substring matches below, we make sure that the pattern has | |
4951 | `*' as first and last character, making a new pattern if necessary. */ | |
95732b49 | 4952 | len = wcslen (wpat); |
0001803f | 4953 | if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*') |
95732b49 | 4954 | { |
a0c0a00f CR |
4955 | int unescaped_backslash; |
4956 | wchar_t *wpp; | |
4957 | ||
0628567a | 4958 | wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t)); |
95732b49 | 4959 | wp1 = wpat; |
0001803f | 4960 | if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob)) |
95732b49 JA |
4961 | *wp++ = L'*'; |
4962 | while (*wp1 != L'\0') | |
4963 | *wp++ = *wp1++; | |
a0c0a00f CR |
4964 | #if 1 |
4965 | /* See comments above in match_upattern. */ | |
4966 | if (wp1[-1] == L'*' && (unescaped_backslash = wp1[-2] == L'\\')) | |
4967 | { | |
4968 | wpp = wp1 - 3; | |
4969 | while (wpp >= wpat && *wpp-- == L'\\') | |
4970 | unescaped_backslash = 1 - unescaped_backslash; | |
4971 | if (unescaped_backslash) | |
4972 | *wp++ = L'*'; | |
4973 | } | |
4974 | else if (wp1[-1] != L'*') | |
4975 | *wp++ = L'*'; | |
4976 | #else | |
95732b49 JA |
4977 | if (wp1[-1] != L'*' || wp1[-2] == L'\\') |
4978 | *wp++ = L'*'; | |
a0c0a00f | 4979 | #endif |
95732b49 JA |
4980 | *wp = '\0'; |
4981 | } | |
4982 | else | |
4983 | nwpat = wpat; | |
a0c0a00f | 4984 | len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE); |
95732b49 JA |
4985 | if (nwpat != wpat) |
4986 | free (nwpat); | |
4987 | if (len == FNM_NOMATCH) | |
4988 | return (0); | |
4989 | ||
495aee44 | 4990 | mlen = wmatchlen (wpat, wstrlen); |
712f80b0 CR |
4991 | if (mlen > (int)wstrlen) |
4992 | return (0); | |
495aee44 CR |
4993 | |
4994 | /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */ | |
b80f6443 JA |
4995 | switch (mtype) |
4996 | { | |
4997 | case MATCH_ANY: | |
4998 | for (n = 0; n <= wstrlen; n++) | |
4999 | { | |
a0c0a00f | 5000 | n2 = simple ? (WFOLD(*wpat) == WFOLD(wstring[n])) : match_pattern_wchar (wpat, wstring + n, FNMATCH_IGNCASE); |
495aee44 | 5001 | if (n2) |
b80f6443 | 5002 | { |
495aee44 CR |
5003 | n1 = (mlen == -1) ? wstrlen : n + mlen; |
5004 | if (n1 > wstrlen) | |
5005 | break; | |
5006 | ||
5007 | for ( ; n1 >= n; n1--) | |
b80f6443 JA |
5008 | { |
5009 | wc = wstring[n1]; wstring[n1] = L'\0'; | |
a0c0a00f | 5010 | if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
5011 | { |
5012 | wstring[n1] = wc; | |
5013 | *sp = indices[n]; | |
5014 | *ep = indices[n1]; | |
5015 | return 1; | |
5016 | } | |
5017 | wstring[n1] = wc; | |
495aee44 CR |
5018 | /* If MLEN != -1, we have a fixed length pattern. */ |
5019 | if (mlen != -1) | |
5020 | break; | |
b80f6443 JA |
5021 | } |
5022 | } | |
5023 | } | |
5024 | ||
5025 | return (0); | |
5026 | ||
5027 | case MATCH_BEG: | |
a0c0a00f | 5028 | if (match_pattern_wchar (wpat, wstring, FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
5029 | return (0); |
5030 | ||
495aee44 | 5031 | for (n = (mlen == -1) ? wstrlen : mlen; n >= 0; n--) |
b80f6443 JA |
5032 | { |
5033 | wc = wstring[n]; wstring[n] = L'\0'; | |
a0c0a00f | 5034 | if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
5035 | { |
5036 | wstring[n] = wc; | |
5037 | *sp = indices[0]; | |
5038 | *ep = indices[n]; | |
5039 | return 1; | |
5040 | } | |
5041 | wstring[n] = wc; | |
495aee44 CR |
5042 | /* If MLEN != -1, we have a fixed length pattern. */ |
5043 | if (mlen != -1) | |
5044 | break; | |
b80f6443 JA |
5045 | } |
5046 | ||
5047 | return (0); | |
5048 | ||
5049 | case MATCH_END: | |
495aee44 | 5050 | for (n = wstrlen - ((mlen == -1) ? wstrlen : mlen); n <= wstrlen; n++) |
b80f6443 | 5051 | { |
a0c0a00f | 5052 | if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0) |
b80f6443 JA |
5053 | { |
5054 | *sp = indices[n]; | |
5055 | *ep = indices[wstrlen]; | |
5056 | return 1; | |
5057 | } | |
495aee44 CR |
5058 | /* If MLEN != -1, we have a fixed length pattern. */ |
5059 | if (mlen != -1) | |
5060 | break; | |
b80f6443 JA |
5061 | } |
5062 | ||
ccc6cda3 | 5063 | return (0); |
726f6388 | 5064 | } |
ccc6cda3 JA |
5065 | |
5066 | return (0); | |
726f6388 | 5067 | } |
a0c0a00f | 5068 | #undef WFOLD |
b80f6443 JA |
5069 | #endif /* HANDLE_MULTIBYTE */ |
5070 | ||
5071 | static int | |
5072 | match_pattern (string, pat, mtype, sp, ep) | |
5073 | char *string, *pat; | |
5074 | int mtype; | |
5075 | char **sp, **ep; | |
5076 | { | |
5077 | #if defined (HANDLE_MULTIBYTE) | |
5078 | int ret; | |
5079 | size_t n; | |
5080 | wchar_t *wstring, *wpat; | |
5081 | char **indices; | |
5082 | #endif | |
5083 | ||
a0c0a00f | 5084 | if (string == 0 || pat == 0 || *pat == 0) |
b80f6443 JA |
5085 | return (0); |
5086 | ||
5087 | #if defined (HANDLE_MULTIBYTE) | |
5088 | if (MB_CUR_MAX > 1) | |
5089 | { | |
495aee44 | 5090 | if (mbsmbchar (string) == 0 && mbsmbchar (pat) == 0) |
495aee44 CR |
5091 | return (match_upattern (string, pat, mtype, sp, ep)); |
5092 | ||
b80f6443 JA |
5093 | n = xdupmbstowcs (&wpat, NULL, pat); |
5094 | if (n == (size_t)-1) | |
5095 | return (match_upattern (string, pat, mtype, sp, ep)); | |
5096 | n = xdupmbstowcs (&wstring, &indices, string); | |
5097 | if (n == (size_t)-1) | |
5098 | { | |
5099 | free (wpat); | |
5100 | return (match_upattern (string, pat, mtype, sp, ep)); | |
5101 | } | |
5102 | ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep); | |
5103 | ||
5104 | free (wpat); | |
5105 | free (wstring); | |
5106 | free (indices); | |
5107 | ||
5108 | return (ret); | |
5109 | } | |
5110 | else | |
5111 | #endif | |
5112 | return (match_upattern (string, pat, mtype, sp, ep)); | |
5113 | } | |
726f6388 | 5114 | |
cce855bc JA |
5115 | static int |
5116 | getpatspec (c, value) | |
5117 | int c; | |
5118 | char *value; | |
5119 | { | |
5120 | if (c == '#') | |
5121 | return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT); | |
5122 | else /* c == '%' */ | |
5123 | return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT); | |
5124 | } | |
5125 | ||
5126 | /* Posix.2 says that the WORD should be run through tilde expansion, | |
5127 | parameter expansion, command substitution and arithmetic expansion. | |
5128 | This leaves the result quoted, so quote_string_for_globbing () has | |
f73dda09 | 5129 | to be called to fix it up for strmatch (). If QUOTED is non-zero, |
cce855bc JA |
5130 | it means that the entire expression was enclosed in double quotes. |
5131 | This means that quoting characters in the pattern do not make any | |
5132 | special pattern characters quoted. For example, the `*' in the | |
5133 | following retains its special meaning: "${foo#'*'}". */ | |
5134 | static char * | |
5135 | getpattern (value, quoted, expandpat) | |
5136 | char *value; | |
5137 | int quoted, expandpat; | |
5138 | { | |
5139 | char *pat, *tword; | |
5140 | WORD_LIST *l; | |
0628567a | 5141 | #if 0 |
cce855bc | 5142 | int i; |
0628567a | 5143 | #endif |
7117c2d2 JA |
5144 | /* There is a problem here: how to handle single or double quotes in the |
5145 | pattern string when the whole expression is between double quotes? | |
5146 | POSIX.2 says that enclosing double quotes do not cause the pattern to | |
5147 | be quoted, but does that leave us a problem with @ and array[@] and their | |
5148 | expansions inside a pattern? */ | |
5149 | #if 0 | |
cce855bc JA |
5150 | if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword) |
5151 | { | |
5152 | i = 0; | |
a0c0a00f | 5153 | pat = string_extract_double_quoted (tword, &i, SX_STRIPDQ); |
cce855bc JA |
5154 | free (tword); |
5155 | tword = pat; | |
5156 | } | |
7117c2d2 | 5157 | #endif |
cce855bc | 5158 | |
d233b485 | 5159 | /* expand_string_for_pat () leaves WORD quoted and does not perform |
7117c2d2 | 5160 | word splitting. */ |
d233b485 | 5161 | l = *value ? expand_string_for_pat (value, |
7117c2d2 | 5162 | (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted, |
cce855bc | 5163 | (int *)NULL, (int *)NULL) |
cce855bc | 5164 | : (WORD_LIST *)0; |
712f80b0 CR |
5165 | if (l) |
5166 | word_list_remove_quoted_nulls (l); | |
cce855bc JA |
5167 | pat = string_list (l); |
5168 | dispose_words (l); | |
5169 | if (pat) | |
5170 | { | |
5171 | tword = quote_string_for_globbing (pat, QGLOB_CVTNULL); | |
5172 | free (pat); | |
5173 | pat = tword; | |
5174 | } | |
5175 | return (pat); | |
5176 | } | |
5177 | ||
7117c2d2 | 5178 | #if 0 |
cce855bc JA |
5179 | /* Handle removing a pattern from a string as a result of ${name%[%]value} |
5180 | or ${name#[#]value}. */ | |
5181 | static char * | |
7117c2d2 JA |
5182 | variable_remove_pattern (value, pattern, patspec, quoted) |
5183 | char *value, *pattern; | |
5184 | int patspec, quoted; | |
cce855bc | 5185 | { |
7117c2d2 | 5186 | char *tword; |
cce855bc | 5187 | |
7117c2d2 | 5188 | tword = remove_pattern (value, pattern, patspec); |
cce855bc | 5189 | |
cce855bc JA |
5190 | return (tword); |
5191 | } | |
a0c0a00f CR |
5192 | #endif |
5193 | ||
5194 | static char * | |
5195 | list_remove_pattern (list, pattern, patspec, itype, quoted) | |
5196 | WORD_LIST *list; | |
5197 | char *pattern; | |
5198 | int patspec, itype, quoted; | |
5199 | { | |
5200 | WORD_LIST *new, *l; | |
5201 | WORD_DESC *w; | |
5202 | char *tword; | |
5203 | ||
5204 | for (new = (WORD_LIST *)NULL, l = list; l; l = l->next) | |
5205 | { | |
5206 | tword = remove_pattern (l->word->word, pattern, patspec); | |
5207 | w = alloc_word_desc (); | |
5208 | w->word = tword ? tword : savestring (""); | |
5209 | new = make_word_list (w, new); | |
5210 | } | |
5211 | ||
5212 | l = REVERSE_LIST (new, WORD_LIST *); | |
712f80b0 | 5213 | tword = string_list_pos_params (itype, l, quoted, 0); |
a0c0a00f CR |
5214 | dispose_words (l); |
5215 | ||
5216 | return (tword); | |
5217 | } | |
5218 | ||
5219 | static char * | |
5220 | parameter_list_remove_pattern (itype, pattern, patspec, quoted) | |
5221 | int itype; | |
5222 | char *pattern; | |
5223 | int patspec, quoted; | |
5224 | { | |
5225 | char *ret; | |
5226 | WORD_LIST *list; | |
5227 | ||
5228 | list = list_rest_of_args (); | |
5229 | if (list == 0) | |
5230 | return ((char *)NULL); | |
5231 | ret = list_remove_pattern (list, pattern, patspec, itype, quoted); | |
5232 | dispose_words (list); | |
5233 | return (ret); | |
5234 | } | |
5235 | ||
5236 | #if defined (ARRAY_VARS) | |
5237 | static char * | |
712f80b0 | 5238 | array_remove_pattern (var, pattern, patspec, starsub, quoted) |
a0c0a00f CR |
5239 | SHELL_VAR *var; |
5240 | char *pattern; | |
5241 | int patspec; | |
712f80b0 | 5242 | int starsub; /* so we can figure out how it's indexed */ |
a0c0a00f CR |
5243 | int quoted; |
5244 | { | |
5245 | ARRAY *a; | |
5246 | HASH_TABLE *h; | |
5247 | int itype; | |
5248 | char *ret; | |
5249 | WORD_LIST *list; | |
5250 | SHELL_VAR *v; | |
5251 | ||
712f80b0 | 5252 | v = var; /* XXX - for now */ |
a0c0a00f | 5253 | |
712f80b0 | 5254 | itype = starsub ? '*' : '@'; |
a0c0a00f CR |
5255 | |
5256 | a = (v && array_p (v)) ? array_cell (v) : 0; | |
5257 | h = (v && assoc_p (v)) ? assoc_cell (v) : 0; | |
5258 | ||
5259 | list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0); | |
5260 | if (list == 0) | |
5261 | return ((char *)NULL); | |
5262 | ret = list_remove_pattern (list, pattern, patspec, itype, quoted); | |
5263 | dispose_words (list); | |
5264 | ||
5265 | return ret; | |
5266 | } | |
5267 | #endif /* ARRAY_VARS */ | |
5268 | ||
5269 | static char * | |
5270 | parameter_brace_remove_pattern (varname, value, ind, patstr, rtype, quoted, flags) | |
5271 | char *varname, *value; | |
5272 | int ind; | |
5273 | char *patstr; | |
5274 | int rtype, quoted, flags; | |
5275 | { | |
5276 | int vtype, patspec, starsub; | |
d233b485 | 5277 | char *temp1, *val, *pattern, *oname; |
a0c0a00f CR |
5278 | SHELL_VAR *v; |
5279 | ||
5280 | if (value == 0) | |
5281 | return ((char *)NULL); | |
5282 | ||
d233b485 | 5283 | oname = this_command_name; |
a0c0a00f CR |
5284 | this_command_name = varname; |
5285 | ||
5286 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); | |
5287 | if (vtype == -1) | |
d233b485 CR |
5288 | { |
5289 | this_command_name = oname; | |
5290 | return ((char *)NULL); | |
5291 | } | |
a0c0a00f CR |
5292 | |
5293 | starsub = vtype & VT_STARSUB; | |
5294 | vtype &= ~VT_STARSUB; | |
5295 | ||
5296 | patspec = getpatspec (rtype, patstr); | |
5297 | if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT) | |
5298 | patstr++; | |
5299 | ||
5300 | /* Need to pass getpattern newly-allocated memory in case of expansion -- | |
5301 | the expansion code will free the passed string on an error. */ | |
5302 | temp1 = savestring (patstr); | |
5303 | pattern = getpattern (temp1, quoted, 1); | |
5304 | free (temp1); | |
5305 | ||
5306 | temp1 = (char *)NULL; /* shut up gcc */ | |
5307 | switch (vtype) | |
5308 | { | |
5309 | case VT_VARIABLE: | |
5310 | case VT_ARRAYMEMBER: | |
5311 | temp1 = remove_pattern (val, pattern, patspec); | |
5312 | if (vtype == VT_VARIABLE) | |
5313 | FREE (val); | |
5314 | if (temp1) | |
5315 | { | |
5316 | val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
5317 | ? quote_string (temp1) | |
5318 | : quote_escapes (temp1); | |
5319 | free (temp1); | |
5320 | temp1 = val; | |
5321 | } | |
5322 | break; | |
5323 | #if defined (ARRAY_VARS) | |
5324 | case VT_ARRAYVAR: | |
712f80b0 | 5325 | temp1 = array_remove_pattern (v, pattern, patspec, starsub, quoted); |
a0c0a00f CR |
5326 | if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) |
5327 | { | |
5328 | val = quote_escapes (temp1); | |
5329 | free (temp1); | |
5330 | temp1 = val; | |
5331 | } | |
5332 | break; | |
5333 | #endif | |
5334 | case VT_POSPARMS: | |
5335 | temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted); | |
d233b485 CR |
5336 | if (temp1 && quoted == 0 && ifs_is_null) |
5337 | { | |
5338 | /* Posix interp 888 */ | |
5339 | } | |
5340 | else if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) | |
a0c0a00f CR |
5341 | { |
5342 | val = quote_escapes (temp1); | |
5343 | free (temp1); | |
5344 | temp1 = val; | |
5345 | } | |
5346 | break; | |
5347 | } | |
5348 | ||
d233b485 CR |
5349 | this_command_name = oname; |
5350 | ||
a0c0a00f CR |
5351 | FREE (pattern); |
5352 | return temp1; | |
5353 | } | |
5354 | ||
d233b485 | 5355 | #if defined (PROCESS_SUBSTITUTION) |
a0c0a00f | 5356 | |
712f80b0 CR |
5357 | static void reap_some_procsubs PARAMS((int)); |
5358 | ||
d233b485 CR |
5359 | /*****************************************************************/ |
5360 | /* */ | |
5361 | /* Hacking Process Substitution */ | |
5362 | /* */ | |
5363 | /*****************************************************************/ | |
5364 | ||
5365 | #if !defined (HAVE_DEV_FD) | |
5366 | /* Named pipes must be removed explicitly with `unlink'. This keeps a list | |
5367 | of FIFOs the shell has open. unlink_fifo_list will walk the list and | |
3eb0018e CR |
5368 | unlink the ones that don't have a living process on the other end. |
5369 | unlink_all_fifos will walk the list and unconditionally unlink them, trying | |
5370 | to open and close the FIFO first to release any child processes sleeping on | |
5371 | the FIFO. add_fifo_list adds the name of an open FIFO to the list. | |
5372 | NFIFO is a count of the number of FIFOs in the list. */ | |
d233b485 CR |
5373 | #define FIFO_INCR 20 |
5374 | ||
5375 | /* PROC value of -1 means the process has been reaped and the FIFO needs to | |
5376 | be removed. PROC value of 0 means the slot is unused. */ | |
5377 | struct temp_fifo { | |
5378 | char *file; | |
5379 | pid_t proc; | |
5380 | }; | |
5381 | ||
5382 | static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL; | |
5383 | static int nfifo; | |
5384 | static int fifo_list_size; | |
5385 | ||
5386 | void | |
5387 | clear_fifo_list () | |
a0c0a00f | 5388 | { |
712f80b0 CR |
5389 | int i; |
5390 | ||
5391 | for (i = 0; i < fifo_list_size; i++) | |
5392 | { | |
5393 | if (fifo_list[i].file) | |
5394 | free (fifo_list[i].file); | |
5395 | fifo_list[i].file = NULL; | |
5396 | fifo_list[i].proc = 0; | |
5397 | } | |
5398 | nfifo = 0; | |
d233b485 | 5399 | } |
a0c0a00f | 5400 | |
9e49d343 | 5401 | void * |
d233b485 CR |
5402 | copy_fifo_list (sizep) |
5403 | int *sizep; | |
5404 | { | |
5405 | if (sizep) | |
5406 | *sizep = 0; | |
9e49d343 | 5407 | return (void *)NULL; |
d233b485 CR |
5408 | } |
5409 | ||
5410 | static void | |
5411 | add_fifo_list (pathname) | |
5412 | char *pathname; | |
5413 | { | |
5414 | int osize, i; | |
5415 | ||
5416 | if (nfifo >= fifo_list_size - 1) | |
a0c0a00f | 5417 | { |
d233b485 CR |
5418 | osize = fifo_list_size; |
5419 | fifo_list_size += FIFO_INCR; | |
5420 | fifo_list = (struct temp_fifo *)xrealloc (fifo_list, | |
5421 | fifo_list_size * sizeof (struct temp_fifo)); | |
5422 | for (i = osize; i < fifo_list_size; i++) | |
5423 | { | |
5424 | fifo_list[i].file = (char *)NULL; | |
5425 | fifo_list[i].proc = 0; /* unused */ | |
5426 | } | |
a0c0a00f | 5427 | } |
d233b485 CR |
5428 | |
5429 | fifo_list[nfifo].file = savestring (pathname); | |
5430 | nfifo++; | |
a0c0a00f | 5431 | } |
a0c0a00f | 5432 | |
d233b485 CR |
5433 | void |
5434 | unlink_fifo (i) | |
5435 | int i; | |
a0c0a00f | 5436 | { |
d233b485 CR |
5437 | if ((fifo_list[i].proc == (pid_t)-1) || (fifo_list[i].proc > 0 && (kill(fifo_list[i].proc, 0) == -1))) |
5438 | { | |
5439 | unlink (fifo_list[i].file); | |
5440 | free (fifo_list[i].file); | |
5441 | fifo_list[i].file = (char *)NULL; | |
5442 | fifo_list[i].proc = 0; | |
5443 | } | |
a0c0a00f CR |
5444 | } |
5445 | ||
d233b485 CR |
5446 | void |
5447 | unlink_fifo_list () | |
a0c0a00f | 5448 | { |
d233b485 | 5449 | int saved, i, j; |
a0c0a00f | 5450 | |
d233b485 CR |
5451 | if (nfifo == 0) |
5452 | return; | |
a0c0a00f | 5453 | |
d233b485 | 5454 | for (i = saved = 0; i < nfifo; i++) |
a0c0a00f | 5455 | { |
d233b485 CR |
5456 | if ((fifo_list[i].proc == (pid_t)-1) || (fifo_list[i].proc > 0 && (kill(fifo_list[i].proc, 0) == -1))) |
5457 | { | |
5458 | unlink (fifo_list[i].file); | |
5459 | free (fifo_list[i].file); | |
5460 | fifo_list[i].file = (char *)NULL; | |
5461 | fifo_list[i].proc = 0; | |
5462 | } | |
5463 | else | |
5464 | saved++; | |
a0c0a00f | 5465 | } |
d233b485 CR |
5466 | |
5467 | /* If we didn't remove some of the FIFOs, compact the list. */ | |
5468 | if (saved) | |
5469 | { | |
5470 | for (i = j = 0; i < nfifo; i++) | |
5471 | if (fifo_list[i].file) | |
5472 | { | |
9e49d343 CR |
5473 | if (i != j) |
5474 | { | |
5475 | fifo_list[j].file = fifo_list[i].file; | |
5476 | fifo_list[j].proc = fifo_list[i].proc; | |
5477 | fifo_list[i].file = (char *)NULL; | |
5478 | fifo_list[i].proc = 0; | |
5479 | } | |
d233b485 CR |
5480 | j++; |
5481 | } | |
5482 | nfifo = j; | |
5483 | } | |
5484 | else | |
5485 | nfifo = 0; | |
a0c0a00f | 5486 | } |
cce855bc | 5487 | |
3eb0018e CR |
5488 | void |
5489 | unlink_all_fifos () | |
5490 | { | |
5491 | int i, fd; | |
5492 | ||
5493 | if (nfifo == 0) | |
5494 | return; | |
5495 | ||
5496 | for (i = 0; i < nfifo; i++) | |
5497 | { | |
5498 | fifo_list[i].proc = (pid_t)-1; | |
5499 | fd = open (fifo_list[i].file, O_RDWR|O_NONBLOCK); | |
5500 | unlink_fifo (i); | |
5501 | if (fd >= 0) | |
5502 | close (fd); | |
5503 | } | |
5504 | ||
5505 | nfifo = 0; | |
5506 | } | |
5507 | ||
d233b485 CR |
5508 | /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list |
5509 | from some point in the past, and close all open FIFOs in fifo_list | |
5510 | that are not marked as active in LIST. If LIST is NULL, close | |
5511 | everything in fifo_list. LSIZE is the number of elements in LIST, in | |
5512 | case it's larger than fifo_list_size (size of fifo_list). */ | |
5513 | void | |
5514 | close_new_fifos (list, lsize) | |
9e49d343 | 5515 | void *list; |
d233b485 | 5516 | int lsize; |
cce855bc | 5517 | { |
d233b485 | 5518 | int i; |
9e49d343 | 5519 | char *plist; |
cce855bc | 5520 | |
d233b485 | 5521 | if (list == 0) |
cce855bc | 5522 | { |
d233b485 CR |
5523 | unlink_fifo_list (); |
5524 | return; | |
cce855bc JA |
5525 | } |
5526 | ||
9e49d343 CR |
5527 | for (plist = (char *)list, i = 0; i < lsize; i++) |
5528 | if (plist[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1) | |
d233b485 | 5529 | unlink_fifo (i); |
3185942a | 5530 | |
d233b485 CR |
5531 | for (i = lsize; i < fifo_list_size; i++) |
5532 | unlink_fifo (i); | |
cce855bc JA |
5533 | } |
5534 | ||
d233b485 CR |
5535 | int |
5536 | find_procsub_child (pid) | |
5537 | pid_t pid; | |
cce855bc | 5538 | { |
d233b485 | 5539 | int i; |
cce855bc | 5540 | |
d233b485 CR |
5541 | for (i = 0; i < nfifo; i++) |
5542 | if (fifo_list[i].proc == pid) | |
5543 | return i; | |
5544 | return -1; | |
cce855bc JA |
5545 | } |
5546 | ||
d233b485 CR |
5547 | void |
5548 | set_procsub_status (ind, pid, status) | |
5549 | int ind; | |
5550 | pid_t pid; | |
5551 | int status; | |
cce855bc | 5552 | { |
d233b485 CR |
5553 | if (ind >= 0 && ind < nfifo) |
5554 | fifo_list[ind].proc = (pid_t)-1; /* sentinel */ | |
5555 | } | |
ac50fbac | 5556 | |
d233b485 CR |
5557 | /* If we've marked the process for this procsub as dead, close the |
5558 | associated file descriptor and delete the FIFO. */ | |
712f80b0 CR |
5559 | static void |
5560 | reap_some_procsubs (max) | |
5561 | int max; | |
d233b485 CR |
5562 | { |
5563 | int i; | |
7117c2d2 | 5564 | |
712f80b0 | 5565 | for (i = 0; i < max; i++) |
d233b485 CR |
5566 | if (fifo_list[i].proc == (pid_t)-1) /* reaped */ |
5567 | unlink_fifo (i); | |
5568 | } | |
a0c0a00f | 5569 | |
712f80b0 CR |
5570 | void |
5571 | reap_procsubs () | |
5572 | { | |
5573 | reap_some_procsubs (nfifo); | |
5574 | } | |
5575 | ||
5576 | #if 0 | |
5577 | /* UNUSED */ | |
d233b485 CR |
5578 | void |
5579 | wait_procsubs () | |
5580 | { | |
5581 | int i, r; | |
7117c2d2 | 5582 | |
d233b485 CR |
5583 | for (i = 0; i < nfifo; i++) |
5584 | { | |
5585 | if (fifo_list[i].proc != (pid_t)-1 && fifo_list[i].proc > 0) | |
5586 | { | |
712f80b0 CR |
5587 | r = wait_for (fifo_list[i].proc, 0); |
5588 | save_proc_status (fifo_list[i].proc, r); | |
d233b485 CR |
5589 | fifo_list[i].proc = (pid_t)-1; |
5590 | } | |
5591 | } | |
7117c2d2 | 5592 | } |
712f80b0 | 5593 | #endif |
7117c2d2 | 5594 | |
d233b485 CR |
5595 | int |
5596 | fifos_pending () | |
7117c2d2 | 5597 | { |
d233b485 CR |
5598 | return nfifo; |
5599 | } | |
7117c2d2 | 5600 | |
d233b485 CR |
5601 | int |
5602 | num_fifos () | |
5603 | { | |
5604 | return nfifo; | |
5605 | } | |
cce855bc | 5606 | |
d233b485 CR |
5607 | static char * |
5608 | make_named_pipe () | |
5609 | { | |
5610 | char *tname; | |
cce855bc | 5611 | |
d233b485 CR |
5612 | tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR); |
5613 | if (mkfifo (tname, 0600) < 0) | |
cce855bc | 5614 | { |
d233b485 CR |
5615 | free (tname); |
5616 | return ((char *)NULL); | |
cce855bc JA |
5617 | } |
5618 | ||
d233b485 CR |
5619 | add_fifo_list (tname); |
5620 | return (tname); | |
a0c0a00f | 5621 | } |
cce855bc | 5622 | |
d233b485 | 5623 | #else /* HAVE_DEV_FD */ |
726f6388 | 5624 | |
d233b485 CR |
5625 | /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell |
5626 | has open to children. NFDS is a count of the number of bits currently | |
5627 | set in DEV_FD_LIST. TOTFDS is a count of the highest possible number | |
5628 | of open files. */ | |
5629 | /* dev_fd_list[I] value of -1 means the process has been reaped and file | |
5630 | descriptor I needs to be closed. Value of 0 means the slot is unused. */ | |
726f6388 | 5631 | |
d233b485 CR |
5632 | static pid_t *dev_fd_list = (pid_t *)NULL; |
5633 | static int nfds; | |
5634 | static int totfds; /* The highest possible number of open files. */ | |
726f6388 | 5635 | |
d233b485 CR |
5636 | void |
5637 | clear_fifo (i) | |
5638 | int i; | |
726f6388 | 5639 | { |
d233b485 CR |
5640 | if (dev_fd_list[i]) |
5641 | { | |
5642 | dev_fd_list[i] = 0; | |
5643 | nfds--; | |
5644 | } | |
726f6388 JA |
5645 | } |
5646 | ||
d233b485 CR |
5647 | void |
5648 | clear_fifo_list () | |
726f6388 | 5649 | { |
d233b485 | 5650 | register int i; |
726f6388 | 5651 | |
d233b485 CR |
5652 | if (nfds == 0) |
5653 | return; | |
f73dda09 | 5654 | |
d233b485 CR |
5655 | for (i = 0; nfds && i < totfds; i++) |
5656 | clear_fifo (i); | |
726f6388 | 5657 | |
d233b485 | 5658 | nfds = 0; |
a0c0a00f CR |
5659 | } |
5660 | ||
9e49d343 | 5661 | void * |
495aee44 CR |
5662 | copy_fifo_list (sizep) |
5663 | int *sizep; | |
5664 | { | |
9e49d343 | 5665 | void *ret; |
d233b485 CR |
5666 | |
5667 | if (nfds == 0 || totfds == 0) | |
5668 | { | |
5669 | if (sizep) | |
5670 | *sizep = 0; | |
9e49d343 | 5671 | return (void *)NULL; |
d233b485 CR |
5672 | } |
5673 | ||
495aee44 | 5674 | if (sizep) |
d233b485 | 5675 | *sizep = totfds; |
9e49d343 | 5676 | ret = xmalloc (totfds * sizeof (pid_t)); |
d233b485 | 5677 | return (memcpy (ret, dev_fd_list, totfds * sizeof (pid_t))); |
495aee44 CR |
5678 | } |
5679 | ||
726f6388 | 5680 | static void |
d233b485 CR |
5681 | add_fifo_list (fd) |
5682 | int fd; | |
726f6388 | 5683 | { |
d233b485 | 5684 | if (dev_fd_list == 0 || fd >= totfds) |
726f6388 | 5685 | { |
d233b485 CR |
5686 | int ofds; |
5687 | ||
5688 | ofds = totfds; | |
5689 | totfds = getdtablesize (); | |
5690 | if (totfds < 0 || totfds > 256) | |
5691 | totfds = 256; | |
5692 | if (fd >= totfds) | |
5693 | totfds = fd + 2; | |
5694 | ||
5695 | dev_fd_list = (pid_t *)xrealloc (dev_fd_list, totfds * sizeof (dev_fd_list[0])); | |
5696 | /* XXX - might need a loop for this */ | |
5697 | memset (dev_fd_list + ofds, '\0', (totfds - ofds) * sizeof (pid_t)); | |
726f6388 JA |
5698 | } |
5699 | ||
d233b485 CR |
5700 | dev_fd_list[fd] = 1; /* marker; updated later */ |
5701 | nfds++; | |
5702 | } | |
5703 | ||
5704 | int | |
5705 | fifos_pending () | |
5706 | { | |
5707 | return 0; /* used for cleanup; not needed with /dev/fd */ | |
5708 | } | |
5709 | ||
5710 | int | |
5711 | num_fifos () | |
5712 | { | |
5713 | return nfds; | |
726f6388 JA |
5714 | } |
5715 | ||
495aee44 | 5716 | void |
d233b485 CR |
5717 | unlink_fifo (fd) |
5718 | int fd; | |
495aee44 | 5719 | { |
d233b485 | 5720 | if (dev_fd_list[fd]) |
495aee44 | 5721 | { |
d233b485 CR |
5722 | close (fd); |
5723 | dev_fd_list[fd] = 0; | |
5724 | nfds--; | |
495aee44 CR |
5725 | } |
5726 | } | |
5727 | ||
726f6388 JA |
5728 | void |
5729 | unlink_fifo_list () | |
5730 | { | |
d233b485 | 5731 | register int i; |
f73dda09 | 5732 | |
d233b485 | 5733 | if (nfds == 0) |
726f6388 JA |
5734 | return; |
5735 | ||
d233b485 CR |
5736 | for (i = totfds-1; nfds && i >= 0; i--) |
5737 | unlink_fifo (i); | |
f73dda09 | 5738 | |
d233b485 | 5739 | nfds = 0; |
726f6388 JA |
5740 | } |
5741 | ||
3eb0018e CR |
5742 | void |
5743 | unlink_all_fifos () | |
5744 | { | |
5745 | unlink_fifo_list (); | |
5746 | } | |
5747 | ||
d233b485 CR |
5748 | /* Take LIST, which is a snapshot copy of dev_fd_list from some point in |
5749 | the past, and close all open fds in dev_fd_list that are not marked | |
5750 | as open in LIST. If LIST is NULL, close everything in dev_fd_list. | |
5751 | LSIZE is the number of elements in LIST, in case it's larger than | |
5752 | totfds (size of dev_fd_list). */ | |
495aee44 CR |
5753 | void |
5754 | close_new_fifos (list, lsize) | |
9e49d343 | 5755 | void *list; |
495aee44 CR |
5756 | int lsize; |
5757 | { | |
5758 | int i; | |
9e49d343 | 5759 | pid_t *plist; |
495aee44 CR |
5760 | |
5761 | if (list == 0) | |
5762 | { | |
5763 | unlink_fifo_list (); | |
5764 | return; | |
5765 | } | |
5766 | ||
9e49d343 CR |
5767 | for (plist = (pid_t *)list, i = 0; i < lsize; i++) |
5768 | if (plist[i] == 0 && i < totfds && dev_fd_list[i]) | |
495aee44 CR |
5769 | unlink_fifo (i); |
5770 | ||
d233b485 | 5771 | for (i = lsize; i < totfds; i++) |
495aee44 CR |
5772 | unlink_fifo (i); |
5773 | } | |
5774 | ||
f1be666c | 5775 | int |
d233b485 CR |
5776 | find_procsub_child (pid) |
5777 | pid_t pid; | |
a0c0a00f | 5778 | { |
d233b485 | 5779 | int i; |
a0c0a00f CR |
5780 | |
5781 | if (nfds == 0) | |
d233b485 | 5782 | return -1; |
726f6388 | 5783 | |
d233b485 CR |
5784 | for (i = 0; i < totfds; i++) |
5785 | if (dev_fd_list[i] == pid) | |
5786 | return i; | |
f1be666c | 5787 | |
d233b485 | 5788 | return -1; |
495aee44 CR |
5789 | } |
5790 | ||
5791 | void | |
d233b485 CR |
5792 | set_procsub_status (ind, pid, status) |
5793 | int ind; | |
5794 | pid_t pid; | |
5795 | int status; | |
495aee44 | 5796 | { |
d233b485 CR |
5797 | if (ind >= 0 && ind < totfds) |
5798 | dev_fd_list[ind] = (pid_t)-1; /* sentinel */ | |
495aee44 CR |
5799 | } |
5800 | ||
d233b485 CR |
5801 | /* If we've marked the process for this procsub as dead, close the |
5802 | associated file descriptor. */ | |
712f80b0 CR |
5803 | static void |
5804 | reap_some_procsubs (max) | |
5805 | int max; | |
726f6388 | 5806 | { |
d233b485 | 5807 | int i; |
726f6388 | 5808 | |
712f80b0 | 5809 | for (i = 0; nfds > 0 && i < max; i++) |
d233b485 CR |
5810 | if (dev_fd_list[i] == (pid_t)-1) |
5811 | unlink_fifo (i); | |
726f6388 JA |
5812 | } |
5813 | ||
712f80b0 CR |
5814 | void |
5815 | reap_procsubs () | |
5816 | { | |
5817 | reap_some_procsubs (totfds); | |
5818 | } | |
5819 | ||
5820 | #if 0 | |
5821 | /* UNUSED */ | |
495aee44 | 5822 | void |
d233b485 | 5823 | wait_procsubs () |
495aee44 | 5824 | { |
d233b485 | 5825 | int i, r; |
495aee44 | 5826 | |
d233b485 | 5827 | for (i = 0; nfds > 0 && i < totfds; i++) |
495aee44 | 5828 | { |
d233b485 CR |
5829 | if (dev_fd_list[i] != (pid_t)-1 && dev_fd_list[i] > 0) |
5830 | { | |
712f80b0 CR |
5831 | r = wait_for (dev_fd_list[i], 0); |
5832 | save_proc_status (dev_fd_list[i], r); | |
d233b485 CR |
5833 | dev_fd_list[i] = (pid_t)-1; |
5834 | } | |
495aee44 | 5835 | } |
495aee44 | 5836 | } |
712f80b0 | 5837 | #endif |
495aee44 | 5838 | |
726f6388 JA |
5839 | #if defined (NOTDEF) |
5840 | print_dev_fd_list () | |
5841 | { | |
5842 | register int i; | |
5843 | ||
f73dda09 | 5844 | fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ()); |
726f6388 JA |
5845 | fflush (stderr); |
5846 | ||
5847 | for (i = 0; i < totfds; i++) | |
5848 | { | |
5849 | if (dev_fd_list[i]) | |
5850 | fprintf (stderr, " %d", i); | |
5851 | } | |
5852 | fprintf (stderr, "\n"); | |
5853 | } | |
5854 | #endif /* NOTDEF */ | |
5855 | ||
5856 | static char * | |
5857 | make_dev_fd_filename (fd) | |
5858 | int fd; | |
5859 | { | |
f73dda09 | 5860 | char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p; |
726f6388 | 5861 | |
17345e5a | 5862 | ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8); |
bb70624e JA |
5863 | |
5864 | strcpy (ret, DEV_FD_PREFIX); | |
5865 | p = inttostr (fd, intbuf, sizeof (intbuf)); | |
5866 | strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p); | |
5867 | ||
726f6388 JA |
5868 | add_fifo_list (fd); |
5869 | return (ret); | |
5870 | } | |
5871 | ||
5872 | #endif /* HAVE_DEV_FD */ | |
5873 | ||
5874 | /* Return a filename that will open a connection to the process defined by | |
5875 | executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return | |
5876 | a filename in /dev/fd corresponding to a descriptor that is one of the | |
5877 | ends of the pipe. If not defined, we use named pipes on systems that have | |
5878 | them. Systems without /dev/fd and named pipes are out of luck. | |
5879 | ||
5880 | OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or | |
5881 | use the read end of the pipe and dup that file descriptor to fd 0 in | |
5882 | the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for | |
5883 | writing or use the write end of the pipe in the child, and dup that | |
5884 | file descriptor to fd 1 in the child. The parent does the opposite. */ | |
5885 | ||
5886 | static char * | |
5887 | process_substitute (string, open_for_read_in_child) | |
5888 | char *string; | |
5889 | int open_for_read_in_child; | |
5890 | { | |
5891 | char *pathname; | |
d233b485 | 5892 | int fd, result, rc, function_value; |
726f6388 JA |
5893 | pid_t old_pid, pid; |
5894 | #if defined (HAVE_DEV_FD) | |
5895 | int parent_pipe_fd, child_pipe_fd; | |
5896 | int fildes[2]; | |
5897 | #endif /* HAVE_DEV_FD */ | |
5898 | #if defined (JOB_CONTROL) | |
5899 | pid_t old_pipeline_pgrp; | |
ccc6cda3 | 5900 | #endif |
726f6388 | 5901 | |
cce855bc | 5902 | if (!string || !*string || wordexp_only) |
726f6388 JA |
5903 | return ((char *)NULL); |
5904 | ||
5905 | #if !defined (HAVE_DEV_FD) | |
5906 | pathname = make_named_pipe (); | |
5907 | #else /* HAVE_DEV_FD */ | |
5908 | if (pipe (fildes) < 0) | |
5909 | { | |
a0c0a00f | 5910 | sys_error ("%s", _("cannot make pipe for process substitution")); |
726f6388 JA |
5911 | return ((char *)NULL); |
5912 | } | |
5913 | /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of | |
5914 | the pipe in the parent, otherwise the read end. */ | |
5915 | parent_pipe_fd = fildes[open_for_read_in_child]; | |
5916 | child_pipe_fd = fildes[1 - open_for_read_in_child]; | |
d166f048 JA |
5917 | /* Move the parent end of the pipe to some high file descriptor, to |
5918 | avoid clashes with FDs used by the script. */ | |
5919 | parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64); | |
5920 | ||
726f6388 JA |
5921 | pathname = make_dev_fd_filename (parent_pipe_fd); |
5922 | #endif /* HAVE_DEV_FD */ | |
5923 | ||
3185942a | 5924 | if (pathname == 0) |
726f6388 | 5925 | { |
a0c0a00f | 5926 | sys_error ("%s", _("cannot make pipe for process substitution")); |
726f6388 JA |
5927 | return ((char *)NULL); |
5928 | } | |
5929 | ||
5930 | old_pid = last_made_pid; | |
5931 | ||
5932 | #if defined (JOB_CONTROL) | |
5933 | old_pipeline_pgrp = pipeline_pgrp; | |
a0c0a00f CR |
5934 | if (pipeline_pgrp == 0 || (subshell_environment & (SUBSHELL_PIPE|SUBSHELL_FORK|SUBSHELL_ASYNC)) == 0) |
5935 | pipeline_pgrp = shell_pgrp; | |
ccc6cda3 | 5936 | save_pipeline (1); |
ccc6cda3 JA |
5937 | #endif /* JOB_CONTROL */ |
5938 | ||
712f80b0 | 5939 | pid = make_child ((char *)NULL, FORK_ASYNC); |
726f6388 JA |
5940 | if (pid == 0) |
5941 | { | |
712f80b0 CR |
5942 | /* The currently-executing shell is not interactive */ |
5943 | interactive = 0; | |
5944 | ||
ccc6cda3 | 5945 | reset_terminating_signals (); /* XXX */ |
b80f6443 | 5946 | free_pushed_string_input (); |
726f6388 | 5947 | /* Cancel traps, in trap.c. */ |
495aee44 | 5948 | restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */ |
a0c0a00f | 5949 | QUIT; /* catch any interrupts we got post-fork */ |
726f6388 | 5950 | setup_async_signals (); |
712f80b0 CR |
5951 | if (open_for_read_in_child == 0) |
5952 | async_redirect_stdin (); | |
3eb0018e | 5953 | subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB|SUBSHELL_ASYNC; |
a0c0a00f | 5954 | |
d233b485 CR |
5955 | /* We don't inherit the verbose option for command substitutions now, so |
5956 | let's try it for process substitutions. */ | |
5957 | change_flag ('v', FLAG_OFF); | |
5958 | ||
a0c0a00f CR |
5959 | /* if we're expanding a redirection, we shouldn't have access to the |
5960 | temporary environment, but commands in the subshell should have | |
5961 | access to their own temporary environment. */ | |
5962 | if (expanding_redir) | |
5963 | flush_temporary_env (); | |
726f6388 | 5964 | } |
ccc6cda3 JA |
5965 | |
5966 | #if defined (JOB_CONTROL) | |
726f6388 JA |
5967 | set_sigchld_handler (); |
5968 | stop_making_children (); | |
3185942a | 5969 | /* XXX - should we only do this in the parent? (as in command subst) */ |
726f6388 | 5970 | pipeline_pgrp = old_pipeline_pgrp; |
a0c0a00f CR |
5971 | #else |
5972 | stop_making_children (); | |
ccc6cda3 | 5973 | #endif /* JOB_CONTROL */ |
726f6388 JA |
5974 | |
5975 | if (pid < 0) | |
5976 | { | |
a0c0a00f | 5977 | sys_error ("%s", _("cannot make child for process substitution")); |
726f6388 JA |
5978 | free (pathname); |
5979 | #if defined (HAVE_DEV_FD) | |
5980 | close (parent_pipe_fd); | |
5981 | close (child_pipe_fd); | |
5982 | #endif /* HAVE_DEV_FD */ | |
d233b485 CR |
5983 | #if defined (JOB_CONTROL) |
5984 | restore_pipeline (1); | |
5985 | #endif | |
726f6388 JA |
5986 | return ((char *)NULL); |
5987 | } | |
5988 | ||
5989 | if (pid > 0) | |
5990 | { | |
ccc6cda3 | 5991 | #if defined (JOB_CONTROL) |
a0c0a00f | 5992 | last_procsub_child = restore_pipeline (0); |
712f80b0 CR |
5993 | /* We assume that last_procsub_child->next == last_procsub_child because |
5994 | of how jobs.c:add_process() works. */ | |
5995 | last_procsub_child->next = 0; | |
5996 | procsub_add (last_procsub_child); | |
ccc6cda3 JA |
5997 | #endif |
5998 | ||
d233b485 CR |
5999 | #if defined (HAVE_DEV_FD) |
6000 | dev_fd_list[parent_pipe_fd] = pid; | |
6001 | #else | |
f73dda09 JA |
6002 | fifo_list[nfifo-1].proc = pid; |
6003 | #endif | |
6004 | ||
726f6388 JA |
6005 | last_made_pid = old_pid; |
6006 | ||
6007 | #if defined (JOB_CONTROL) && defined (PGRP_PIPE) | |
6008 | close_pgrp_pipe (); | |
6009 | #endif /* JOB_CONTROL && PGRP_PIPE */ | |
6010 | ||
6011 | #if defined (HAVE_DEV_FD) | |
6012 | close (child_pipe_fd); | |
6013 | #endif /* HAVE_DEV_FD */ | |
6014 | ||
6015 | return (pathname); | |
6016 | } | |
6017 | ||
6018 | set_sigint_handler (); | |
6019 | ||
6020 | #if defined (JOB_CONTROL) | |
d233b485 | 6021 | /* make sure we don't have any job control */ |
726f6388 | 6022 | set_job_control (0); |
d233b485 | 6023 | |
712f80b0 CR |
6024 | /* Clear out any existing list of process substitutions */ |
6025 | procsub_clear (); | |
6026 | ||
d233b485 CR |
6027 | /* The idea is that we want all the jobs we start from an async process |
6028 | substitution to be in the same process group, but not the same pgrp | |
6029 | as our parent shell, since we don't want to affect our parent shell's | |
6030 | jobs if we get a SIGHUP and end up calling hangup_all_jobs, for example. | |
6031 | If pipeline_pgrp != shell_pgrp, we assume that there is a job control | |
6032 | shell somewhere in our parent process chain (since make_child initializes | |
6033 | pipeline_pgrp to shell_pgrp if job_control == 0). What we do in this | |
6034 | case is to set pipeline_pgrp to our PID, so all jobs started by this | |
6035 | process have that same pgrp and we are basically the process group leader. | |
6036 | This should not have negative effects on child processes surviving | |
6037 | after we exit, since we wait for the children we create, but that is | |
6038 | something to watch for. */ | |
6039 | ||
6040 | if (pipeline_pgrp != shell_pgrp) | |
6041 | pipeline_pgrp = getpid (); | |
726f6388 JA |
6042 | #endif /* JOB_CONTROL */ |
6043 | ||
6044 | #if !defined (HAVE_DEV_FD) | |
6045 | /* Open the named pipe in the child. */ | |
ac50fbac | 6046 | fd = open (pathname, open_for_read_in_child ? O_RDONLY : O_WRONLY); |
726f6388 JA |
6047 | if (fd < 0) |
6048 | { | |
b80f6443 JA |
6049 | /* Two separate strings for ease of translation. */ |
6050 | if (open_for_read_in_child) | |
6051 | sys_error (_("cannot open named pipe %s for reading"), pathname); | |
6052 | else | |
6053 | sys_error (_("cannot open named pipe %s for writing"), pathname); | |
6054 | ||
726f6388 JA |
6055 | exit (127); |
6056 | } | |
bb70624e JA |
6057 | if (open_for_read_in_child) |
6058 | { | |
28ef6c31 | 6059 | if (sh_unset_nodelay_mode (fd) < 0) |
bb70624e | 6060 | { |
3185942a | 6061 | sys_error (_("cannot reset nodelay mode for fd %d"), fd); |
bb70624e JA |
6062 | exit (127); |
6063 | } | |
6064 | } | |
726f6388 JA |
6065 | #else /* HAVE_DEV_FD */ |
6066 | fd = child_pipe_fd; | |
6067 | #endif /* HAVE_DEV_FD */ | |
6068 | ||
a0c0a00f CR |
6069 | /* Discard buffered stdio output before replacing the underlying file |
6070 | descriptor. */ | |
6071 | if (open_for_read_in_child == 0) | |
6072 | fpurge (stdout); | |
6073 | ||
726f6388 JA |
6074 | if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0) |
6075 | { | |
b80f6443 | 6076 | sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname, |
ccc6cda3 | 6077 | open_for_read_in_child ? 0 : 1); |
726f6388 JA |
6078 | exit (127); |
6079 | } | |
6080 | ||
f73dda09 JA |
6081 | if (fd != (open_for_read_in_child ? 0 : 1)) |
6082 | close (fd); | |
726f6388 JA |
6083 | |
6084 | /* Need to close any files that this process has open to pipes inherited | |
6085 | from its parent. */ | |
6086 | if (current_fds_to_close) | |
6087 | { | |
6088 | close_fd_bitmap (current_fds_to_close); | |
6089 | current_fds_to_close = (struct fd_bitmap *)NULL; | |
6090 | } | |
6091 | ||
6092 | #if defined (HAVE_DEV_FD) | |
6093 | /* Make sure we close the parent's end of the pipe and clear the slot | |
6094 | in the fd list so it is not closed later, if reallocated by, for | |
6095 | instance, pipe(2). */ | |
6096 | close (parent_pipe_fd); | |
6097 | dev_fd_list[parent_pipe_fd] = 0; | |
6098 | #endif /* HAVE_DEV_FD */ | |
6099 | ||
8dea6e87 | 6100 | /* subshells shouldn't have this flag, which controls using the temporary |
a0c0a00f CR |
6101 | environment for variable lookups. We have already flushed the temporary |
6102 | environment above in the case we're expanding a redirection, so processes | |
6103 | executed by this command need to be able to set it independently of their | |
6104 | parent. */ | |
8dea6e87 CR |
6105 | expanding_redir = 0; |
6106 | ||
9cce630e CR |
6107 | remove_quoted_escapes (string); |
6108 | ||
d233b485 CR |
6109 | /* Give process substitution a place to jump back to on failure, |
6110 | so we don't go back up to main (). */ | |
6111 | result = setjmp_nosigs (top_level); | |
6112 | ||
6113 | /* If we're running a process substitution inside a shell function, | |
6114 | trap `return' so we don't return from the function in the subshell | |
6115 | and go off to never-never land. */ | |
6116 | if (result == 0 && return_catch_flag) | |
6117 | function_value = setjmp_nosigs (return_catch); | |
6118 | else | |
6119 | function_value = 0; | |
6120 | ||
6121 | if (result == ERREXIT) | |
6122 | rc = last_command_exit_value; | |
6123 | else if (result == EXITPROG) | |
6124 | rc = last_command_exit_value; | |
6125 | else if (result) | |
6126 | rc = EXECUTION_FAILURE; | |
6127 | else if (function_value) | |
6128 | rc = return_catch_value; | |
6129 | else | |
6130 | { | |
6131 | subshell_level++; | |
6132 | rc = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST)); | |
6133 | /* leave subshell level intact for any exit trap */ | |
6134 | } | |
726f6388 JA |
6135 | |
6136 | #if !defined (HAVE_DEV_FD) | |
6137 | /* Make sure we close the named pipe in the child before we exit. */ | |
6138 | close (open_for_read_in_child ? 0 : 1); | |
6139 | #endif /* !HAVE_DEV_FD */ | |
6140 | ||
d233b485 CR |
6141 | last_command_exit_value = rc; |
6142 | rc = run_exit_trap (); | |
6143 | exit (rc); | |
726f6388 JA |
6144 | /*NOTREACHED*/ |
6145 | } | |
6146 | #endif /* PROCESS_SUBSTITUTION */ | |
6147 | ||
cce855bc JA |
6148 | /***********************************/ |
6149 | /* */ | |
6150 | /* Command Substitution */ | |
6151 | /* */ | |
6152 | /***********************************/ | |
6153 | ||
d166f048 | 6154 | static char * |
d233b485 CR |
6155 | read_comsub (fd, quoted, flags, rflag) |
6156 | int fd, quoted, flags; | |
3185942a | 6157 | int *rflag; |
d166f048 | 6158 | { |
712f80b0 | 6159 | char *istring, buf[512], *bufp; |
d233b485 | 6160 | int istring_index, c, tflag, skip_ctlesc, skip_ctlnul; |
712f80b0 | 6161 | int mb_cur_max; |
d233b485 | 6162 | size_t istring_size; |
f73dda09 | 6163 | ssize_t bufn; |
280bd77d | 6164 | int nullbyte; |
712f80b0 CR |
6165 | #if defined (HANDLE_MULTIBYTE) |
6166 | mbstate_t ps; | |
6167 | wchar_t wc; | |
6168 | size_t mblen; | |
6169 | int i; | |
6170 | #endif | |
d166f048 JA |
6171 | |
6172 | istring = (char *)NULL; | |
3185942a JA |
6173 | istring_index = istring_size = bufn = tflag = 0; |
6174 | ||
d233b485 CR |
6175 | skip_ctlesc = ifs_cmap[CTLESC]; |
6176 | skip_ctlnul = ifs_cmap[CTLNUL]; | |
d166f048 | 6177 | |
712f80b0 | 6178 | mb_cur_max = MB_CUR_MAX; |
280bd77d CR |
6179 | nullbyte = 0; |
6180 | ||
712f80b0 | 6181 | /* Read the output of the command through the pipe. */ |
d166f048 JA |
6182 | while (1) |
6183 | { | |
6184 | if (fd < 0) | |
28ef6c31 | 6185 | break; |
d166f048 JA |
6186 | if (--bufn <= 0) |
6187 | { | |
bb70624e | 6188 | bufn = zread (fd, buf, sizeof (buf)); |
d166f048 JA |
6189 | if (bufn <= 0) |
6190 | break; | |
6191 | bufp = buf; | |
6192 | } | |
6193 | c = *bufp++; | |
6194 | ||
28ef6c31 JA |
6195 | if (c == 0) |
6196 | { | |
a0c0a00f | 6197 | #if 1 |
280bd77d CR |
6198 | if (nullbyte == 0) |
6199 | { | |
6200 | internal_warning ("%s", _("command substitution: ignored null byte in input")); | |
6201 | nullbyte = 1; | |
6202 | } | |
28ef6c31 JA |
6203 | #endif |
6204 | continue; | |
6205 | } | |
6206 | ||
d166f048 | 6207 | /* Add the character to ISTRING, possibly after resizing it. */ |
712f80b0 | 6208 | RESIZE_MALLOCED_BUFFER (istring, istring_index, mb_cur_max+1, istring_size, 512); |
d166f048 | 6209 | |
f1be666c JA |
6210 | /* This is essentially quote_string inline */ |
6211 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */) | |
6212 | istring[istring_index++] = CTLESC; | |
d233b485 CR |
6213 | else if ((flags & PF_ASSIGNRHS) && skip_ctlesc && c == CTLESC) |
6214 | istring[istring_index++] = CTLESC; | |
f1be666c JA |
6215 | /* Escape CTLESC and CTLNUL in the output to protect those characters |
6216 | from the rest of the word expansions (word splitting and globbing.) | |
6217 | This is essentially quote_escapes inline. */ | |
3185942a | 6218 | else if (skip_ctlesc == 0 && c == CTLESC) |
d233b485 | 6219 | istring[istring_index++] = CTLESC; |
3185942a | 6220 | else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0))) |
d166f048 JA |
6221 | istring[istring_index++] = CTLESC; |
6222 | ||
712f80b0 CR |
6223 | #if defined (HANDLE_MULTIBYTE) |
6224 | if ((locale_utf8locale && (c & 0x80)) || | |
6225 | (locale_utf8locale == 0 && mb_cur_max > 1 && (unsigned char)c > 127)) | |
6226 | { | |
6227 | /* read a multibyte character from buf */ | |
6228 | /* punt on the hard case for now */ | |
6229 | memset (&ps, '\0', sizeof (mbstate_t)); | |
6230 | mblen = mbrtowc (&wc, bufp-1, bufn+1, &ps); | |
6231 | if (MB_INVALIDCH (mblen) || mblen == 0 || mblen == 1) | |
6232 | istring[istring_index++] = c; | |
6233 | else | |
6234 | { | |
6235 | istring[istring_index++] = c; | |
6236 | for (i = 0; i < mblen-1; i++) | |
6237 | istring[istring_index++] = *bufp++; | |
6238 | bufn -= mblen - 1; | |
6239 | } | |
6240 | continue; | |
28ef6c31 JA |
6241 | } |
6242 | #endif | |
712f80b0 CR |
6243 | |
6244 | istring[istring_index++] = c; | |
d166f048 JA |
6245 | } |
6246 | ||
6247 | if (istring) | |
6248 | istring[istring_index] = '\0'; | |
6249 | ||
6250 | /* If we read no output, just return now and save ourselves some | |
6251 | trouble. */ | |
6252 | if (istring_index == 0) | |
6253 | { | |
6254 | FREE (istring); | |
3185942a JA |
6255 | if (rflag) |
6256 | *rflag = tflag; | |
d166f048 JA |
6257 | return (char *)NULL; |
6258 | } | |
6259 | ||
6260 | /* Strip trailing newlines from the output of the command. */ | |
6261 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
6262 | { | |
6263 | while (istring_index > 0) | |
6264 | { | |
6265 | if (istring[istring_index - 1] == '\n') | |
6266 | { | |
6267 | --istring_index; | |
6268 | ||
6269 | /* If the newline was quoted, remove the quoting char. */ | |
6270 | if (istring[istring_index - 1] == CTLESC) | |
6271 | --istring_index; | |
6272 | } | |
6273 | else | |
6274 | break; | |
6275 | } | |
6276 | istring[istring_index] = '\0'; | |
6277 | } | |
6278 | else | |
6279 | strip_trailing (istring, istring_index - 1, 1); | |
6280 | ||
3185942a JA |
6281 | if (rflag) |
6282 | *rflag = tflag; | |
d166f048 JA |
6283 | return istring; |
6284 | } | |
6285 | ||
3185942a JA |
6286 | /* Perform command substitution on STRING. This returns a WORD_DESC * with the |
6287 | contained string possibly quoted. */ | |
6288 | WORD_DESC * | |
d233b485 | 6289 | command_substitute (string, quoted, flags) |
726f6388 JA |
6290 | char *string; |
6291 | int quoted; | |
d233b485 | 6292 | int flags; |
726f6388 | 6293 | { |
95732b49 | 6294 | pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid; |
a0c0a00f | 6295 | char *istring, *s; |
712f80b0 | 6296 | int result, fildes[2], function_value, pflags, rc, tflag, fork_flags; |
3185942a | 6297 | WORD_DESC *ret; |
712f80b0 | 6298 | sigset_t set, oset; |
726f6388 | 6299 | |
ccc6cda3 | 6300 | istring = (char *)NULL; |
726f6388 JA |
6301 | |
6302 | /* Don't fork () if there is no need to. In the case of no command to | |
6303 | run, just return NULL. */ | |
a0c0a00f CR |
6304 | #if 1 |
6305 | for (s = string; s && *s && (shellblank (*s) || *s == '\n'); s++) | |
6306 | ; | |
6307 | if (s == 0 || *s == 0) | |
6308 | return ((WORD_DESC *)NULL); | |
6309 | #else | |
726f6388 | 6310 | if (!string || !*string || (string[0] == '\n' && !string[1])) |
3185942a | 6311 | return ((WORD_DESC *)NULL); |
a0c0a00f | 6312 | #endif |
726f6388 | 6313 | |
cce855bc JA |
6314 | if (wordexp_only && read_but_dont_execute) |
6315 | { | |
0001803f | 6316 | last_command_exit_value = EX_WEXPCOMSUB; |
cce855bc JA |
6317 | jump_to_top_level (EXITPROG); |
6318 | } | |
6319 | ||
bb70624e JA |
6320 | /* We're making the assumption here that the command substitution will |
6321 | eventually run a command from the file system. Since we'll run | |
6322 | maybe_make_export_env in this subshell before executing that command, | |
6323 | the parent shell and any other shells it starts will have to remake | |
6324 | the environment. If we make it before we fork, other shells won't | |
6325 | have to. Don't bother if we have any temporary variable assignments, | |
6326 | though, because the export environment will be remade after this | |
6327 | command completes anyway, but do it if all the words to be expanded | |
6328 | are variable assignments. */ | |
6329 | if (subst_assign_varlist == 0 || garglist == 0) | |
6330 | maybe_make_export_env (); /* XXX */ | |
6331 | ||
b80f6443 | 6332 | /* Flags to pass to parse_and_execute() */ |
0001803f | 6333 | pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0; |
b80f6443 | 6334 | |
d233b485 CR |
6335 | old_pid = last_made_pid; |
6336 | ||
726f6388 JA |
6337 | /* Pipe the output of executing STRING into the current shell. */ |
6338 | if (pipe (fildes) < 0) | |
6339 | { | |
a0c0a00f | 6340 | sys_error ("%s", _("cannot make pipe for command substitution")); |
726f6388 JA |
6341 | goto error_exit; |
6342 | } | |
6343 | ||
726f6388 | 6344 | #if defined (JOB_CONTROL) |
ccc6cda3 | 6345 | old_pipeline_pgrp = pipeline_pgrp; |
28ef6c31 JA |
6346 | /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */ |
6347 | if ((subshell_environment & SUBSHELL_PIPE) == 0) | |
6348 | pipeline_pgrp = shell_pgrp; | |
ccc6cda3 | 6349 | cleanup_the_pipeline (); |
95732b49 | 6350 | #endif /* JOB_CONTROL */ |
726f6388 | 6351 | |
95732b49 | 6352 | old_async_pid = last_asynchronous_pid; |
712f80b0 CR |
6353 | fork_flags = (subshell_environment&SUBSHELL_ASYNC) ? FORK_ASYNC : 0; |
6354 | pid = make_child ((char *)NULL, fork_flags|FORK_NOTERM); | |
95732b49 JA |
6355 | last_asynchronous_pid = old_async_pid; |
6356 | ||
726f6388 | 6357 | if (pid == 0) |
495aee44 CR |
6358 | { |
6359 | /* Reset the signal handlers in the child, but don't free the | |
6360 | trap strings. Set a flag noting that we have to free the | |
6361 | trap strings if we run trap to change a signal disposition. */ | |
6362 | reset_signal_handlers (); | |
a0c0a00f CR |
6363 | if (ISINTERRUPT) |
6364 | { | |
6365 | kill (getpid (), SIGINT); | |
6366 | CLRINTERRUPT; /* if we're ignoring SIGINT somehow */ | |
6367 | } | |
6368 | QUIT; /* catch any interrupts we got post-fork */ | |
495aee44 CR |
6369 | subshell_environment |= SUBSHELL_RESETTRAP; |
6370 | } | |
ccc6cda3 JA |
6371 | |
6372 | #if defined (JOB_CONTROL) | |
3185942a | 6373 | /* XXX DO THIS ONLY IN PARENT ? XXX */ |
ccc6cda3 JA |
6374 | set_sigchld_handler (); |
6375 | stop_making_children (); | |
f1be666c JA |
6376 | if (pid != 0) |
6377 | pipeline_pgrp = old_pipeline_pgrp; | |
f73dda09 JA |
6378 | #else |
6379 | stop_making_children (); | |
ccc6cda3 | 6380 | #endif /* JOB_CONTROL */ |
726f6388 JA |
6381 | |
6382 | if (pid < 0) | |
6383 | { | |
b80f6443 | 6384 | sys_error (_("cannot make child for command substitution")); |
726f6388 JA |
6385 | error_exit: |
6386 | ||
ac50fbac CR |
6387 | last_made_pid = old_pid; |
6388 | ||
726f6388 JA |
6389 | FREE (istring); |
6390 | close (fildes[0]); | |
6391 | close (fildes[1]); | |
3185942a | 6392 | return ((WORD_DESC *)NULL); |
726f6388 JA |
6393 | } |
6394 | ||
6395 | if (pid == 0) | |
6396 | { | |
a0c0a00f CR |
6397 | /* The currently executing shell is not interactive. */ |
6398 | interactive = 0; | |
6399 | ||
726f6388 | 6400 | set_sigint_handler (); /* XXX */ |
28ef6c31 | 6401 | |
b80f6443 JA |
6402 | free_pushed_string_input (); |
6403 | ||
a0c0a00f CR |
6404 | /* Discard buffered stdio output before replacing the underlying file |
6405 | descriptor. */ | |
6406 | fpurge (stdout); | |
6407 | ||
726f6388 JA |
6408 | if (dup2 (fildes[1], 1) < 0) |
6409 | { | |
a0c0a00f | 6410 | sys_error ("%s", _("command_substitute: cannot duplicate pipe as fd 1")); |
726f6388 JA |
6411 | exit (EXECUTION_FAILURE); |
6412 | } | |
6413 | ||
6414 | /* If standard output is closed in the parent shell | |
6415 | (such as after `exec >&-'), file descriptor 1 will be | |
6416 | the lowest available file descriptor, and end up in | |
6417 | fildes[0]. This can happen for stdin and stderr as well, | |
6418 | but stdout is more important -- it will cause no output | |
6419 | to be generated from this command. */ | |
6420 | if ((fildes[1] != fileno (stdin)) && | |
6421 | (fildes[1] != fileno (stdout)) && | |
6422 | (fildes[1] != fileno (stderr))) | |
6423 | close (fildes[1]); | |
6424 | ||
6425 | if ((fildes[0] != fileno (stdin)) && | |
6426 | (fildes[0] != fileno (stdout)) && | |
6427 | (fildes[0] != fileno (stderr))) | |
6428 | close (fildes[0]); | |
6429 | ||
495aee44 CR |
6430 | #ifdef __CYGWIN__ |
6431 | /* Let stdio know the fd may have changed from text to binary mode, and | |
6432 | make sure to preserve stdout line buffering. */ | |
6433 | freopen (NULL, "w", stdout); | |
6434 | sh_setlinebuf (stdout); | |
6435 | #endif /* __CYGWIN__ */ | |
6436 | ||
ccc6cda3 | 6437 | /* This is a subshell environment. */ |
28ef6c31 | 6438 | subshell_environment |= SUBSHELL_COMSUB; |
ccc6cda3 | 6439 | |
a0c0a00f CR |
6440 | /* Many shells do not appear to inherit the -v option for command |
6441 | substitutions. */ | |
6442 | change_flag ('v', FLAG_OFF); | |
6443 | ||
6444 | /* When inherit_errexit option is not enabled, command substitution does | |
6445 | not inherit the -e flag. It is enabled when Posix mode is enabled */ | |
6446 | if (inherit_errexit == 0) | |
ac50fbac CR |
6447 | { |
6448 | builtin_ignoring_errexit = 0; | |
6449 | change_flag ('e', FLAG_OFF); | |
ac50fbac | 6450 | } |
a0c0a00f CR |
6451 | set_shellopts (); |
6452 | ||
6453 | /* If we are expanding a redirection, we can dispose of any temporary | |
6454 | environment we received, since redirections are not supposed to have | |
6455 | access to the temporary environment. We will have to see whether this | |
6456 | affects temporary environments supplied to `eval', but the temporary | |
6457 | environment gets copied to builtin_env at some point. */ | |
6458 | if (expanding_redir) | |
6459 | { | |
6460 | flush_temporary_env (); | |
6461 | expanding_redir = 0; | |
6462 | } | |
726f6388 JA |
6463 | |
6464 | remove_quoted_escapes (string); | |
6465 | ||
ccc6cda3 | 6466 | startup_state = 2; /* see if we can avoid a fork */ |
d233b485 CR |
6467 | parse_and_execute_level = 0; |
6468 | ||
726f6388 JA |
6469 | /* Give command substitution a place to jump back to on failure, |
6470 | so we don't go back up to main (). */ | |
ac50fbac | 6471 | result = setjmp_nosigs (top_level); |
726f6388 | 6472 | |
bb70624e JA |
6473 | /* If we're running a command substitution inside a shell function, |
6474 | trap `return' so we don't return from the function in the subshell | |
6475 | and go off to never-never land. */ | |
6476 | if (result == 0 && return_catch_flag) | |
ac50fbac | 6477 | function_value = setjmp_nosigs (return_catch); |
bb70624e JA |
6478 | else |
6479 | function_value = 0; | |
6480 | ||
b80f6443 JA |
6481 | if (result == ERREXIT) |
6482 | rc = last_command_exit_value; | |
6483 | else if (result == EXITPROG) | |
6484 | rc = last_command_exit_value; | |
726f6388 | 6485 | else if (result) |
b80f6443 | 6486 | rc = EXECUTION_FAILURE; |
bb70624e | 6487 | else if (function_value) |
b80f6443 | 6488 | rc = return_catch_value; |
726f6388 | 6489 | else |
b80f6443 JA |
6490 | { |
6491 | subshell_level++; | |
6492 | rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST); | |
d233b485 | 6493 | /* leave subshell level intact for any exit trap */ |
b80f6443 JA |
6494 | } |
6495 | ||
6496 | last_command_exit_value = rc; | |
6497 | rc = run_exit_trap (); | |
f1be666c JA |
6498 | #if defined (PROCESS_SUBSTITUTION) |
6499 | unlink_fifo_list (); | |
6500 | #endif | |
b80f6443 | 6501 | exit (rc); |
726f6388 JA |
6502 | } |
6503 | else | |
6504 | { | |
712f80b0 CR |
6505 | int dummyfd; |
6506 | ||
726f6388 JA |
6507 | #if defined (JOB_CONTROL) && defined (PGRP_PIPE) |
6508 | close_pgrp_pipe (); | |
6509 | #endif /* JOB_CONTROL && PGRP_PIPE */ | |
6510 | ||
6511 | close (fildes[1]); | |
6512 | ||
712f80b0 CR |
6513 | begin_unwind_frame ("read-comsub"); |
6514 | dummyfd = fildes[0]; | |
6515 | add_unwind_protect (close, dummyfd); | |
6516 | ||
6517 | /* Block SIGINT while we're reading from the pipe. If the child | |
6518 | process gets a SIGINT, it will either handle it or die, and the | |
6519 | read will return. */ | |
6520 | BLOCK_SIGNAL (SIGINT, set, oset); | |
3185942a | 6521 | tflag = 0; |
d233b485 | 6522 | istring = read_comsub (fildes[0], quoted, flags, &tflag); |
ccc6cda3 | 6523 | |
726f6388 | 6524 | close (fildes[0]); |
712f80b0 CR |
6525 | discard_unwind_frame ("read-comsub"); |
6526 | UNBLOCK_SIGNAL (oset); | |
726f6388 | 6527 | |
b72432fd | 6528 | current_command_subst_pid = pid; |
712f80b0 | 6529 | last_command_exit_value = wait_for (pid, JWAIT_NOTERM); |
726f6388 JA |
6530 | last_command_subst_pid = pid; |
6531 | last_made_pid = old_pid; | |
6532 | ||
6533 | #if defined (JOB_CONTROL) | |
6534 | /* If last_command_exit_value > 128, then the substituted command | |
6535 | was terminated by a signal. If that signal was SIGINT, then send | |
6536 | SIGINT to ourselves. This will break out of loops, for instance. */ | |
b80f6443 | 6537 | if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT) |
726f6388 | 6538 | kill (getpid (), SIGINT); |
726f6388 JA |
6539 | #endif /* JOB_CONTROL */ |
6540 | ||
3185942a JA |
6541 | ret = alloc_word_desc (); |
6542 | ret->word = istring; | |
6543 | ret->flags = tflag; | |
6544 | ||
6545 | return ret; | |
726f6388 JA |
6546 | } |
6547 | } | |
6548 | ||
6549 | /******************************************************** | |
6550 | * * | |
6551 | * Utility functions for parameter expansion * | |
6552 | * * | |
6553 | ********************************************************/ | |
6554 | ||
ccc6cda3 | 6555 | #if defined (ARRAY_VARS) |
ccc6cda3 | 6556 | |
f73dda09 | 6557 | static arrayind_t |
ccc6cda3 JA |
6558 | array_length_reference (s) |
6559 | char *s; | |
6560 | { | |
f73dda09 JA |
6561 | int len; |
6562 | arrayind_t ind; | |
3185942a | 6563 | char *akey; |
f73dda09 | 6564 | char *t, c; |
ccc6cda3 | 6565 | ARRAY *array; |
495aee44 | 6566 | HASH_TABLE *h; |
ccc6cda3 JA |
6567 | SHELL_VAR *var; |
6568 | ||
d233b485 | 6569 | var = array_variable_part (s, 0, &t, &len); |
726f6388 | 6570 | |
ccc6cda3 JA |
6571 | /* If unbound variables should generate an error, report one and return |
6572 | failure. */ | |
ac50fbac | 6573 | if ((var == 0 || invisible_p (var) || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error) |
726f6388 | 6574 | { |
f73dda09 | 6575 | c = *--t; |
ccc6cda3 | 6576 | *t = '\0'; |
712f80b0 | 6577 | set_exit_status (EXECUTION_FAILURE); |
7117c2d2 | 6578 | err_unboundvar (s); |
f73dda09 | 6579 | *t = c; |
ccc6cda3 | 6580 | return (-1); |
726f6388 | 6581 | } |
ac50fbac | 6582 | else if (var == 0 || invisible_p (var)) |
ccc6cda3 | 6583 | return 0; |
726f6388 | 6584 | |
28ef6c31 JA |
6585 | /* We support a couple of expansions for variables that are not arrays. |
6586 | We'll return the length of the value for v[0], and 1 for v[@] or | |
6587 | v[*]. Return 0 for everything else. */ | |
6588 | ||
6589 | array = array_p (var) ? array_cell (var) : (ARRAY *)NULL; | |
495aee44 | 6590 | h = assoc_p (var) ? assoc_cell (var) : (HASH_TABLE *)NULL; |
726f6388 | 6591 | |
d233b485 | 6592 | if (ALL_ELEMENT_SUB (t[0]) && t[1] == RBRACK) |
ccc6cda3 | 6593 | { |
3185942a | 6594 | if (assoc_p (var)) |
495aee44 | 6595 | return (h ? assoc_num_elements (h) : 0); |
3185942a | 6596 | else if (array_p (var)) |
495aee44 | 6597 | return (array ? array_num_elements (array) : 0); |
3185942a | 6598 | else |
495aee44 | 6599 | return (var_isset (var) ? 1 : 0); |
ccc6cda3 | 6600 | } |
ccc6cda3 | 6601 | |
3185942a JA |
6602 | if (assoc_p (var)) |
6603 | { | |
6604 | t[len - 1] = '\0'; | |
6605 | akey = expand_assignment_string_to_string (t, 0); /* [ */ | |
d233b485 | 6606 | t[len - 1] = RBRACK; |
3185942a JA |
6607 | if (akey == 0 || *akey == 0) |
6608 | { | |
6609 | err_badarraysub (t); | |
ac50fbac | 6610 | FREE (akey); |
3185942a JA |
6611 | return (-1); |
6612 | } | |
6613 | t = assoc_reference (assoc_cell (var), akey); | |
ac50fbac | 6614 | free (akey); |
3185942a | 6615 | } |
28ef6c31 | 6616 | else |
3185942a | 6617 | { |
d233b485 | 6618 | ind = array_expand_index (var, t, len, 0); |
ac50fbac CR |
6619 | /* negative subscripts to indexed arrays count back from end */ |
6620 | if (var && array_p (var) && ind < 0) | |
6621 | ind = array_max_index (array_cell (var)) + 1 + ind; | |
3185942a JA |
6622 | if (ind < 0) |
6623 | { | |
6624 | err_badarraysub (t); | |
6625 | return (-1); | |
6626 | } | |
6627 | if (array_p (var)) | |
6628 | t = array_reference (array, ind); | |
6629 | else | |
6630 | t = (ind == 0) ? value_cell (var) : (char *)NULL; | |
6631 | } | |
28ef6c31 | 6632 | |
f1be666c | 6633 | len = MB_STRLEN (t); |
ccc6cda3 | 6634 | return (len); |
726f6388 | 6635 | } |
ccc6cda3 | 6636 | #endif /* ARRAY_VARS */ |
726f6388 JA |
6637 | |
6638 | static int | |
6639 | valid_brace_expansion_word (name, var_is_special) | |
6640 | char *name; | |
6641 | int var_is_special; | |
6642 | { | |
f73dda09 | 6643 | if (DIGIT (*name) && all_digits (name)) |
726f6388 JA |
6644 | return 1; |
6645 | else if (var_is_special) | |
6646 | return 1; | |
ccc6cda3 | 6647 | #if defined (ARRAY_VARS) |
a0c0a00f | 6648 | else if (valid_array_reference (name, 0)) |
ccc6cda3 JA |
6649 | return 1; |
6650 | #endif /* ARRAY_VARS */ | |
726f6388 JA |
6651 | else if (legal_identifier (name)) |
6652 | return 1; | |
6653 | else | |
6654 | return 0; | |
6655 | } | |
ccc6cda3 | 6656 | |
b80f6443 | 6657 | static int |
712f80b0 | 6658 | chk_atstar (name, quoted, pflags, quoted_dollar_atp, contains_dollar_at) |
b80f6443 | 6659 | char *name; |
712f80b0 | 6660 | int quoted, pflags; |
b80f6443 JA |
6661 | int *quoted_dollar_atp, *contains_dollar_at; |
6662 | { | |
6663 | char *temp1; | |
6664 | ||
6665 | if (name == 0) | |
6666 | { | |
6667 | if (quoted_dollar_atp) | |
6668 | *quoted_dollar_atp = 0; | |
6669 | if (contains_dollar_at) | |
6670 | *contains_dollar_at = 0; | |
6671 | return 0; | |
6672 | } | |
6673 | ||
6674 | /* check for $@ and $* */ | |
6675 | if (name[0] == '@' && name[1] == 0) | |
6676 | { | |
6677 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
6678 | *quoted_dollar_atp = 1; | |
6679 | if (contains_dollar_at) | |
6680 | *contains_dollar_at = 1; | |
6681 | return 1; | |
6682 | } | |
6683 | else if (name[0] == '*' && name[1] == '\0' && quoted == 0) | |
6684 | { | |
712f80b0 CR |
6685 | /* Need more checks here that parallel what string_list_pos_params and |
6686 | param_expand do. Check expand_no_split_dollar_star and ??? */ | |
6687 | if (contains_dollar_at && expand_no_split_dollar_star == 0) | |
b80f6443 JA |
6688 | *contains_dollar_at = 1; |
6689 | return 1; | |
6690 | } | |
6691 | ||
6692 | /* Now check for ${array[@]} and ${array[*]} */ | |
6693 | #if defined (ARRAY_VARS) | |
a0c0a00f | 6694 | else if (valid_array_reference (name, 0)) |
b80f6443 | 6695 | { |
d233b485 CR |
6696 | temp1 = mbschr (name, LBRACK); |
6697 | if (temp1 && temp1[1] == '@' && temp1[2] == RBRACK) | |
b80f6443 JA |
6698 | { |
6699 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
6700 | *quoted_dollar_atp = 1; | |
6701 | if (contains_dollar_at) | |
6702 | *contains_dollar_at = 1; | |
6703 | return 1; | |
d233b485 | 6704 | } |
b80f6443 JA |
6705 | /* ${array[*]}, when unquoted, should be treated like ${array[@]}, |
6706 | which should result in separate words even when IFS is unset. */ | |
d233b485 | 6707 | if (temp1 && temp1[1] == '*' && temp1[2] == RBRACK && quoted == 0) |
b80f6443 JA |
6708 | { |
6709 | if (contains_dollar_at) | |
6710 | *contains_dollar_at = 1; | |
6711 | return 1; | |
6712 | } | |
6713 | } | |
6714 | #endif | |
6715 | return 0; | |
6716 | } | |
6717 | ||
726f6388 | 6718 | /* Parameter expand NAME, and return a new string which is the expansion, |
d233b485 | 6719 | or NULL if there was no expansion. NAME is as given in ${NAMEcWORD}. |
726f6388 JA |
6720 | VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in |
6721 | the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that | |
6722 | NAME was found inside of a double-quoted expression. */ | |
95732b49 | 6723 | static WORD_DESC * |
495aee44 | 6724 | parameter_brace_expand_word (name, var_is_special, quoted, pflags, indp) |
726f6388 | 6725 | char *name; |
89a92869 | 6726 | int var_is_special, quoted, pflags; |
495aee44 | 6727 | arrayind_t *indp; |
726f6388 | 6728 | { |
95732b49 | 6729 | WORD_DESC *ret; |
ccc6cda3 | 6730 | char *temp, *tt; |
7117c2d2 | 6731 | intmax_t arg_index; |
ccc6cda3 | 6732 | SHELL_VAR *var; |
f1be666c | 6733 | int atype, rflags; |
495aee44 | 6734 | arrayind_t ind; |
726f6388 | 6735 | |
95732b49 JA |
6736 | ret = 0; |
6737 | temp = 0; | |
f1be666c | 6738 | rflags = 0; |
95732b49 | 6739 | |
495aee44 CR |
6740 | if (indp) |
6741 | *indp = INTMAX_MIN; | |
6742 | ||
95732b49 | 6743 | /* Handle multiple digit arguments, as in ${11}. */ |
f73dda09 | 6744 | if (legal_number (name, &arg_index)) |
7117c2d2 JA |
6745 | { |
6746 | tt = get_dollar_var_value (arg_index); | |
b80f6443 JA |
6747 | if (tt) |
6748 | temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) | |
6749 | ? quote_string (tt) | |
6750 | : quote_escapes (tt); | |
6751 | else | |
6752 | temp = (char *)NULL; | |
7117c2d2 JA |
6753 | FREE (tt); |
6754 | } | |
726f6388 JA |
6755 | else if (var_is_special) /* ${@} */ |
6756 | { | |
cce855bc | 6757 | int sindex; |
f73dda09 | 6758 | tt = (char *)xmalloc (2 + strlen (name)); |
cce855bc | 6759 | tt[sindex = 0] = '$'; |
726f6388 | 6760 | strcpy (tt + 1, name); |
7117c2d2 | 6761 | |
95732b49 | 6762 | ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL, |
89a92869 | 6763 | (int *)NULL, (int *)NULL, pflags); |
cce855bc | 6764 | free (tt); |
726f6388 | 6765 | } |
ccc6cda3 | 6766 | #if defined (ARRAY_VARS) |
a0c0a00f | 6767 | else if (valid_array_reference (name, 0)) |
ccc6cda3 | 6768 | { |
ac50fbac | 6769 | expand_arrayref: |
d233b485 CR |
6770 | var = array_variable_part (name, 0, &tt, (int *)0); |
6771 | /* These are the cases where word splitting will not be performed */ | |
ac50fbac | 6772 | if (pflags & PF_ASSIGNRHS) |
a0c0a00f | 6773 | { |
d233b485 | 6774 | if (ALL_ELEMENT_SUB (tt[0]) && tt[1] == RBRACK) |
a0c0a00f CR |
6775 | { |
6776 | /* Only treat as double quoted if array variable */ | |
6777 | if (var && (array_p (var) || assoc_p (var))) | |
a0c0a00f CR |
6778 | temp = array_value (name, quoted|Q_DOUBLE_QUOTES, AV_ASSIGNRHS, &atype, &ind); |
6779 | else | |
6780 | temp = array_value (name, quoted, 0, &atype, &ind); | |
6781 | } | |
ac50fbac CR |
6782 | else |
6783 | temp = array_value (name, quoted, 0, &atype, &ind); | |
a0c0a00f | 6784 | } |
d233b485 CR |
6785 | /* Posix interp 888 */ |
6786 | else if (pflags & PF_NOSPLIT2) | |
6787 | { | |
6788 | /* Special cases, then general case, for each of A[@], A[*], A[n] */ | |
6789 | #if defined (HANDLE_MULTIBYTE) | |
6790 | if (tt[0] == '@' && tt[1] == RBRACK && var && quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc[0] != ' ') | |
6791 | #else | |
6792 | if (tt[0] == '@' && tt[1] == RBRACK && var && quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc != ' ') | |
6793 | #endif | |
6794 | temp = array_value (name, Q_DOUBLE_QUOTES, AV_ASSIGNRHS, &atype, &ind); | |
6795 | else if (tt[0] == '@' && tt[1] == RBRACK) | |
6796 | temp = array_value (name, quoted, 0, &atype, &ind); | |
6797 | else if (tt[0] == '*' && tt[1] == RBRACK && expand_no_split_dollar_star && ifs_is_null) | |
6798 | temp = array_value (name, Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT, 0, &atype, &ind); | |
6799 | else if (tt[0] == '*' && tt[1] == RBRACK) | |
6800 | temp = array_value (name, quoted, 0, &atype, &ind); | |
6801 | else | |
6802 | temp = array_value (name, quoted, 0, &atype, &ind); | |
6803 | } | |
6804 | else if (tt[0] == '*' && tt[1] == RBRACK && expand_no_split_dollar_star && ifs_is_null) | |
6805 | temp = array_value (name, Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT, 0, &atype, &ind); | |
ac50fbac CR |
6806 | else |
6807 | temp = array_value (name, quoted, 0, &atype, &ind); | |
7117c2d2 | 6808 | if (atype == 0 && temp) |
495aee44 CR |
6809 | { |
6810 | temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) | |
6811 | ? quote_string (temp) | |
6812 | : quote_escapes (temp); | |
6813 | rflags |= W_ARRAYIND; | |
6814 | if (indp) | |
6815 | *indp = ind; | |
6816 | } | |
f1be666c JA |
6817 | else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
6818 | rflags |= W_HASQUOTEDNULL; | |
ccc6cda3 JA |
6819 | } |
6820 | #endif | |
6821 | else if (var = find_variable (name)) | |
6822 | { | |
7117c2d2 | 6823 | if (var_isset (var) && invisible_p (var) == 0) |
28ef6c31 | 6824 | { |
ccc6cda3 | 6825 | #if defined (ARRAY_VARS) |
3eb0018e CR |
6826 | /* We avoid a memory leak by saving TT as the memory allocated by |
6827 | assoc_to_string or array_to_string and leaving it 0 otherwise, | |
6828 | then freeing TT after quoting temp. */ | |
6829 | tt = (char *)NULL; | |
6830 | if ((pflags & PF_ALLINDS) && assoc_p (var)) | |
6831 | tt = temp = assoc_empty (assoc_cell (var)) ? (char *)NULL : assoc_to_string (assoc_cell (var), " ", quoted); | |
6832 | else if ((pflags & PF_ALLINDS) && array_p (var)) | |
6833 | tt = temp = array_empty (array_cell (var)) ? (char *)NULL : array_to_string (array_cell (var), " ", quoted); | |
6834 | else if (assoc_p (var)) | |
3185942a JA |
6835 | temp = assoc_reference (assoc_cell (var), "0"); |
6836 | else if (array_p (var)) | |
6837 | temp = array_reference (array_cell (var), 0); | |
6838 | else | |
6839 | temp = value_cell (var); | |
ccc6cda3 JA |
6840 | #else |
6841 | temp = value_cell (var); | |
6842 | #endif | |
6843 | ||
6844 | if (temp) | |
b80f6443 JA |
6845 | temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
6846 | ? quote_string (temp) | |
d233b485 CR |
6847 | : ((pflags & PF_ASSIGNRHS) ? quote_rhs (temp) |
6848 | : quote_escapes (temp)); | |
3eb0018e | 6849 | FREE (tt); |
28ef6c31 | 6850 | } |
ccc6cda3 JA |
6851 | else |
6852 | temp = (char *)NULL; | |
6853 | } | |
a0c0a00f | 6854 | else if (var = find_variable_last_nameref (name, 0)) |
ac50fbac CR |
6855 | { |
6856 | temp = nameref_cell (var); | |
6857 | #if defined (ARRAY_VARS) | |
6858 | /* Handle expanding nameref whose value is x[n] */ | |
a0c0a00f | 6859 | if (temp && *temp && valid_array_reference (temp, 0)) |
ac50fbac CR |
6860 | { |
6861 | name = temp; | |
6862 | goto expand_arrayref; | |
6863 | } | |
6864 | else | |
6865 | #endif | |
6866 | /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */ | |
6867 | if (temp && *temp && legal_identifier (temp) == 0) | |
6868 | { | |
712f80b0 | 6869 | set_exit_status (EXECUTION_FAILURE); |
ac50fbac CR |
6870 | report_error (_("%s: invalid variable name for name reference"), temp); |
6871 | temp = &expand_param_error; | |
6872 | } | |
6873 | else | |
6874 | temp = (char *)NULL; | |
6875 | } | |
726f6388 | 6876 | else |
ccc6cda3 | 6877 | temp = (char *)NULL; |
726f6388 | 6878 | |
95732b49 JA |
6879 | if (ret == 0) |
6880 | { | |
6881 | ret = alloc_word_desc (); | |
6882 | ret->word = temp; | |
f1be666c | 6883 | ret->flags |= rflags; |
95732b49 JA |
6884 | } |
6885 | return ret; | |
726f6388 JA |
6886 | } |
6887 | ||
ac50fbac CR |
6888 | static char * |
6889 | parameter_brace_find_indir (name, var_is_special, quoted, find_nameref) | |
ccc6cda3 | 6890 | char *name; |
ac50fbac | 6891 | int var_is_special, quoted, find_nameref; |
ccc6cda3 JA |
6892 | { |
6893 | char *temp, *t; | |
95732b49 | 6894 | WORD_DESC *w; |
ac50fbac | 6895 | SHELL_VAR *v; |
d233b485 | 6896 | int pflags, oldex; |
ac50fbac | 6897 | |
a0c0a00f | 6898 | if (find_nameref && var_is_special == 0 && (v = find_variable_last_nameref (name, 0)) && |
ac50fbac CR |
6899 | nameref_p (v) && (t = nameref_cell (v)) && *t) |
6900 | return (savestring (t)); | |
ccc6cda3 | 6901 | |
ac50fbac CR |
6902 | /* If var_is_special == 0, and name is not an array reference, this does |
6903 | more expansion than necessary. It should really look up the variable's | |
6904 | value and not try to expand it. */ | |
d233b485 CR |
6905 | pflags = PF_IGNUNBOUND; |
6906 | /* Note that we're not going to be doing word splitting here */ | |
6907 | if (var_is_special) | |
6908 | { | |
6909 | pflags |= PF_ASSIGNRHS; /* suppresses word splitting */ | |
6910 | oldex = expand_no_split_dollar_star; | |
6911 | expand_no_split_dollar_star = 1; | |
6912 | } | |
6913 | w = parameter_brace_expand_word (name, var_is_special, quoted, pflags, 0); | |
6914 | if (var_is_special) | |
6915 | expand_no_split_dollar_star = oldex; | |
6916 | ||
95732b49 | 6917 | t = w->word; |
b80f6443 JA |
6918 | /* Have to dequote here if necessary */ |
6919 | if (t) | |
6920 | { | |
d233b485 | 6921 | temp = ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || var_is_special) |
b80f6443 JA |
6922 | ? dequote_string (t) |
6923 | : dequote_escapes (t); | |
6924 | free (t); | |
6925 | t = temp; | |
6926 | } | |
95732b49 JA |
6927 | dispose_word_desc (w); |
6928 | ||
ac50fbac CR |
6929 | return t; |
6930 | } | |
6931 | ||
6932 | /* Expand an indirect reference to a variable: ${!NAME} expands to the | |
6933 | value of the variable whose name is the value of NAME. */ | |
6934 | static WORD_DESC * | |
712f80b0 | 6935 | parameter_brace_expand_indir (name, var_is_special, quoted, pflags, quoted_dollar_atp, contains_dollar_at) |
ac50fbac | 6936 | char *name; |
712f80b0 | 6937 | int var_is_special, quoted, pflags; |
ac50fbac CR |
6938 | int *quoted_dollar_atp, *contains_dollar_at; |
6939 | { | |
d233b485 | 6940 | char *t; |
ac50fbac CR |
6941 | WORD_DESC *w; |
6942 | SHELL_VAR *v; | |
6943 | ||
6944 | /* See if it's a nameref first, behave in ksh93-compatible fashion. | |
6945 | There is at least one incompatibility: given ${!foo[0]} where foo=bar, | |
6946 | bash performs an indirect lookup on foo[0] and expands the result; | |
6947 | ksh93 expands bar[0]. We could do that here -- there are enough usable | |
6948 | primitives to do that -- but do not at this point. */ | |
a0c0a00f | 6949 | if (var_is_special == 0 && (v = find_variable_last_nameref (name, 0))) |
ac50fbac CR |
6950 | { |
6951 | if (nameref_p (v) && (t = nameref_cell (v)) && *t) | |
6952 | { | |
6953 | w = alloc_word_desc (); | |
6954 | w->word = savestring (t); | |
6955 | w->flags = 0; | |
6956 | return w; | |
6957 | } | |
6958 | } | |
6959 | ||
d233b485 CR |
6960 | /* An indirect reference to a positional parameter or a special parameter |
6961 | is ok. Indirect references to array references, as explained above, are | |
6962 | ok (currently). Only references to unset variables are errors at this | |
6963 | point. */ | |
6964 | if (legal_identifier (name) && v == 0) | |
6965 | { | |
6966 | report_error (_("%s: invalid indirect expansion"), name); | |
6967 | w = alloc_word_desc (); | |
6968 | w->word = &expand_param_error; | |
6969 | w->flags = 0; | |
6970 | return (w); | |
6971 | } | |
6972 | ||
ac50fbac CR |
6973 | t = parameter_brace_find_indir (name, var_is_special, quoted, 0); |
6974 | ||
712f80b0 | 6975 | chk_atstar (t, quoted, pflags, quoted_dollar_atp, contains_dollar_at); |
d233b485 CR |
6976 | |
6977 | #if defined (ARRAY_VARS) | |
6978 | /* Array references to unset variables are also an error */ | |
6979 | if (t == 0 && valid_array_reference (name, 0)) | |
6980 | { | |
6981 | v = array_variable_part (name, 0, (char **)0, (int *)0); | |
6982 | if (v == 0) | |
6983 | { | |
6984 | report_error (_("%s: invalid indirect expansion"), name); | |
6985 | w = alloc_word_desc (); | |
6986 | w->word = &expand_param_error; | |
6987 | w->flags = 0; | |
6988 | return (w); | |
6989 | } | |
6990 | else | |
6991 | return (WORD_DESC *)NULL; | |
6992 | } | |
6993 | #endif | |
6994 | ||
ccc6cda3 | 6995 | if (t == 0) |
95732b49 JA |
6996 | return (WORD_DESC *)NULL; |
6997 | ||
a0c0a00f CR |
6998 | if (valid_brace_expansion_word (t, SPECIAL_VAR (t, 0)) == 0) |
6999 | { | |
d233b485 | 7000 | report_error (_("%s: invalid variable name"), t); |
a0c0a00f CR |
7001 | free (t); |
7002 | w = alloc_word_desc (); | |
7003 | w->word = &expand_param_error; | |
7004 | w->flags = 0; | |
7005 | return (w); | |
7006 | } | |
7007 | ||
712f80b0 | 7008 | w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, pflags, 0); |
ccc6cda3 | 7009 | free (t); |
95732b49 JA |
7010 | |
7011 | return w; | |
ccc6cda3 JA |
7012 | } |
7013 | ||
726f6388 JA |
7014 | /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE}, |
7015 | depending on the value of C, the separating character. C can be one of | |
ccc6cda3 JA |
7016 | "-", "+", or "=". QUOTED is true if the entire brace expression occurs |
7017 | between double quotes. */ | |
95732b49 | 7018 | static WORD_DESC * |
d233b485 | 7019 | parameter_brace_expand_rhs (name, value, op, quoted, pflags, qdollaratp, hasdollarat) |
726f6388 | 7020 | char *name, *value; |
d233b485 | 7021 | int op, quoted, pflags, *qdollaratp, *hasdollarat; |
726f6388 | 7022 | { |
95732b49 | 7023 | WORD_DESC *w; |
712f80b0 | 7024 | WORD_LIST *l, *tl; |
a0c0a00f CR |
7025 | char *t, *t1, *temp, *vname; |
7026 | int l_hasdollat, sindex; | |
d233b485 | 7027 | SHELL_VAR *v; |
726f6388 | 7028 | |
a0c0a00f | 7029 | /*itrace("parameter_brace_expand_rhs: %s:%s pflags = %d", name, value, pflags);*/ |
ccc6cda3 JA |
7030 | /* If the entire expression is between double quotes, we want to treat |
7031 | the value as a double-quoted string, with the exception that we strip | |
3185942a | 7032 | embedded unescaped double quotes (for sh backwards compatibility). */ |
95732b49 | 7033 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value) |
726f6388 | 7034 | { |
a0c0a00f CR |
7035 | sindex = 0; |
7036 | temp = string_extract_double_quoted (value, &sindex, SX_STRIPDQ); | |
726f6388 | 7037 | } |
95732b49 JA |
7038 | else |
7039 | temp = value; | |
ccc6cda3 | 7040 | |
95732b49 | 7041 | w = alloc_word_desc (); |
a0c0a00f | 7042 | l_hasdollat = 0; |
d233b485 | 7043 | l = *temp ? expand_string_for_rhs (temp, quoted, op, pflags, &l_hasdollat, (int *)NULL) |
ccc6cda3 JA |
7044 | : (WORD_LIST *)0; |
7045 | if (hasdollarat) | |
a0c0a00f | 7046 | *hasdollarat = l_hasdollat || (l && l->next); |
95732b49 JA |
7047 | if (temp != value) |
7048 | free (temp); | |
712f80b0 CR |
7049 | |
7050 | /* list_string takes multiple CTLNULs and turns them into an empty word | |
7051 | with W_SAWQUOTEDNULL set. Turn it back into a single CTLNUL for the | |
7052 | rest of this function and the caller. */ | |
7053 | for (tl = l; tl; tl = tl->next) | |
7054 | { | |
7055 | if (tl->word && (tl->word->word == 0 || tl->word->word[0] == 0) && | |
7056 | (tl->word->flags | W_SAWQUOTEDNULL)) | |
7057 | { | |
7058 | t = make_quoted_char ('\0'); | |
7059 | FREE (tl->word->word); | |
7060 | tl->word->word = t; | |
7061 | tl->word->flags |= W_QUOTED|W_HASQUOTEDNULL; | |
7062 | tl->word->flags &= ~W_SAWQUOTEDNULL; | |
7063 | } | |
7064 | } | |
7065 | ||
726f6388 JA |
7066 | if (l) |
7067 | { | |
a0c0a00f CR |
7068 | /* If l->next is not null, we know that TEMP contained "$@", since that |
7069 | is the only expansion that creates more than one word. */ | |
7070 | if (qdollaratp && ((l_hasdollat && quoted) || l->next)) | |
7071 | { | |
7072 | /*itrace("parameter_brace_expand_rhs: %s:%s: l != NULL, set *qdollaratp", name, value);*/ | |
7073 | *qdollaratp = 1; | |
7074 | } | |
7075 | ||
ccc6cda3 | 7076 | /* The expansion of TEMP returned something. We need to treat things |
a0c0a00f CR |
7077 | slightly differently if L_HASDOLLAT is non-zero. If we have "$@", |
7078 | the individual words have already been quoted. We need to turn them | |
b80f6443 JA |
7079 | into a string with the words separated by the first character of |
7080 | $IFS without any additional quoting, so string_list_dollar_at won't | |
a0c0a00f CR |
7081 | do the right thing. If IFS is null, we want "$@" to split into |
7082 | separate arguments, not be concatenated, so we use string_list_internal | |
7083 | and mark the word to be split on spaces later. We use | |
7084 | string_list_dollar_star for "$@" otherwise. */ | |
7085 | if (l->next && ifs_is_null) | |
7086 | { | |
7087 | temp = string_list_internal (l, " "); | |
7088 | w->flags |= W_SPLITSPACE; | |
7089 | } | |
d233b485 CR |
7090 | else if (l_hasdollat || l->next) |
7091 | temp = string_list_dollar_star (l, quoted, 0); | |
a0c0a00f | 7092 | else |
d233b485 CR |
7093 | { |
7094 | temp = string_list (l); | |
7095 | if (temp && (QUOTED_NULL (temp) == 0) && (l->word->flags & W_SAWQUOTEDNULL)) | |
7096 | w->flags |= W_SAWQUOTEDNULL; /* XXX */ | |
7097 | } | |
b80f6443 | 7098 | |
cd110fdf CR |
7099 | /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is |
7100 | a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the | |
7101 | flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the | |
7102 | expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
7103 | (which is more paranoia than anything else), we need to return the | |
7104 | quoted null string and set the flags to indicate it. */ | |
ac50fbac | 7105 | if (l->next == 0 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp) && QUOTED_NULL (l->word->word) && (l->word->flags & W_HASQUOTEDNULL)) |
cd110fdf CR |
7106 | { |
7107 | w->flags |= W_HASQUOTEDNULL; | |
a0c0a00f CR |
7108 | /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null, turning off qdollaratp", name, value);*/ |
7109 | /* If we return a quoted null with L_HASDOLLARAT, we either have a | |
7110 | construct like "${@-$@}" or "${@-${@-$@}}" with no positional | |
7111 | parameters or a quoted expansion of "$@" with $1 == ''. In either | |
7112 | case, we don't want to enable special handling of $@. */ | |
7113 | if (qdollaratp && l_hasdollat) | |
7114 | *qdollaratp = 0; | |
cd110fdf | 7115 | } |
726f6388 JA |
7116 | dispose_words (l); |
7117 | } | |
a0c0a00f | 7118 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && l_hasdollat) |
726f6388 | 7119 | { |
a0c0a00f CR |
7120 | /* Posix interp 221 changed the rules on this. The idea is that |
7121 | something like "$xxx$@" should expand the same as "${foo-$xxx$@}" | |
7122 | when foo and xxx are unset. The problem is that it's not in any | |
7123 | way backwards compatible and few other shells do it. We're eventually | |
7124 | going to try and split the difference (heh) a little bit here. */ | |
7125 | /* l_hasdollat == 1 means we saw a quoted dollar at. */ | |
7126 | ||
ccc6cda3 JA |
7127 | /* The brace expansion occurred between double quotes and there was |
7128 | a $@ in TEMP. It does not matter if the $@ is quoted, as long as | |
7117c2d2 | 7129 | it does not expand to anything. In this case, we want to return |
a0c0a00f | 7130 | a quoted empty string. Posix interp 888 */ |
0628567a | 7131 | temp = make_quoted_char ('\0'); |
95732b49 | 7132 | w->flags |= W_HASQUOTEDNULL; |
a0c0a00f | 7133 | /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null", name, value);*/ |
726f6388 JA |
7134 | } |
7135 | else | |
7136 | temp = (char *)NULL; | |
7137 | ||
d233b485 | 7138 | if (op == '-' || op == '+') |
95732b49 JA |
7139 | { |
7140 | w->word = temp; | |
7141 | return w; | |
7142 | } | |
726f6388 | 7143 | |
d233b485 CR |
7144 | /* op == '=' */ |
7145 | t1 = temp ? dequote_string (temp) : savestring (""); | |
7146 | free (temp); | |
a0c0a00f CR |
7147 | |
7148 | /* bash-4.4/5.0 */ | |
7149 | vname = name; | |
7150 | if (*name == '!' && | |
7151 | (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) || VALID_INDIR_PARAM (name[1]))) | |
7152 | { | |
7153 | vname = parameter_brace_find_indir (name + 1, SPECIAL_VAR (name, 1), quoted, 1); | |
7154 | if (vname == 0 || *vname == 0) | |
7155 | { | |
7156 | report_error (_("%s: invalid indirect expansion"), name); | |
7157 | free (vname); | |
d233b485 | 7158 | free (t1); |
a0c0a00f CR |
7159 | dispose_word (w); |
7160 | return &expand_wdesc_error; | |
7161 | } | |
7162 | if (legal_identifier (vname) == 0) | |
7163 | { | |
7164 | report_error (_("%s: invalid variable name"), vname); | |
7165 | free (vname); | |
d233b485 | 7166 | free (t1); |
a0c0a00f CR |
7167 | dispose_word (w); |
7168 | return &expand_wdesc_error; | |
7169 | } | |
7170 | } | |
7171 | ||
b80f6443 | 7172 | #if defined (ARRAY_VARS) |
a0c0a00f | 7173 | if (valid_array_reference (vname, 0)) |
d233b485 | 7174 | v = assign_array_element (vname, t1, 0); |
b80f6443 JA |
7175 | else |
7176 | #endif /* ARRAY_VARS */ | |
d233b485 CR |
7177 | v = bind_variable (vname, t1, 0); |
7178 | ||
7179 | if (v == 0 || readonly_p (v) || noassign_p (v)) /* expansion error */ | |
7180 | { | |
7181 | if ((v == 0 || readonly_p (v)) && interactive_shell == 0 && posixly_correct) | |
7182 | { | |
7183 | last_command_exit_value = EXECUTION_FAILURE; | |
7184 | exp_jump_to_top_level (FORCE_EOF); | |
7185 | } | |
7186 | else | |
7187 | { | |
7188 | if (vname != name) | |
7189 | free (vname); | |
7190 | last_command_exit_value = EX_BADUSAGE; | |
7191 | exp_jump_to_top_level (DISCARD); | |
7192 | } | |
7193 | } | |
a0c0a00f CR |
7194 | |
7195 | stupidly_hack_special_variables (vname); | |
7196 | ||
7197 | if (vname != name) | |
7198 | free (vname); | |
95732b49 | 7199 | |
495aee44 | 7200 | /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */ |
495aee44 | 7201 | |
d233b485 CR |
7202 | /* If we are double-quoted or if we are not going to be performing word |
7203 | splitting, we want to quote the value we return appropriately, like | |
7204 | the other expansions this function handles. */ | |
7205 | w->word = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) ? quote_string (t1) : quote_escapes (t1); | |
712f80b0 CR |
7206 | /* If we have something that's non-null, that's not a quoted null string, |
7207 | and we're not going to be performing word splitting (we know we're not | |
7208 | because the operator is `='), we can forget we saw a quoted null. */ | |
7209 | if (w->word && w->word[0] && QUOTED_NULL (w->word) == 0) | |
7210 | w->flags &= ~W_SAWQUOTEDNULL; | |
d233b485 CR |
7211 | free (t1); |
7212 | ||
712f80b0 CR |
7213 | /* If we convert a null string into a quoted null, make sure the caller |
7214 | knows it. */ | |
7215 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && QUOTED_NULL (w->word)) | |
7216 | w->flags |= W_HASQUOTEDNULL; | |
7217 | ||
95732b49 | 7218 | return w; |
726f6388 JA |
7219 | } |
7220 | ||
7221 | /* Deal with the right hand side of a ${name:?value} expansion in the case | |
7222 | that NAME is null or not set. If VALUE is non-null it is expanded and | |
7223 | used as the error message to print, otherwise a standard message is | |
7224 | printed. */ | |
7225 | static void | |
d233b485 | 7226 | parameter_brace_expand_error (name, value, check_null) |
726f6388 | 7227 | char *name, *value; |
d233b485 | 7228 | int check_null; |
726f6388 | 7229 | { |
ccc6cda3 JA |
7230 | WORD_LIST *l; |
7231 | char *temp; | |
7232 | ||
712f80b0 | 7233 | set_exit_status (EXECUTION_FAILURE); /* ensure it's non-zero */ |
726f6388 JA |
7234 | if (value && *value) |
7235 | { | |
95732b49 | 7236 | l = expand_string (value, 0); |
ccc6cda3 JA |
7237 | temp = string_list (l); |
7238 | report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */ | |
7239 | FREE (temp); | |
726f6388 JA |
7240 | dispose_words (l); |
7241 | } | |
d233b485 CR |
7242 | else if (check_null == 0) |
7243 | report_error (_("%s: parameter not set"), name); | |
726f6388 | 7244 | else |
b80f6443 | 7245 | report_error (_("%s: parameter null or not set"), name); |
726f6388 JA |
7246 | |
7247 | /* Free the data we have allocated during this expansion, since we | |
7248 | are about to longjmp out. */ | |
7249 | free (name); | |
7250 | FREE (value); | |
7251 | } | |
7252 | ||
7253 | /* Return 1 if NAME is something for which parameter_brace_expand_length is | |
7254 | OK to do. */ | |
7255 | static int | |
7256 | valid_length_expression (name) | |
7257 | char *name; | |
7258 | { | |
28ef6c31 | 7259 | return (name[1] == '\0' || /* ${#} */ |
f73dda09 JA |
7260 | ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */ |
7261 | (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */ | |
ccc6cda3 | 7262 | #if defined (ARRAY_VARS) |
a0c0a00f | 7263 | valid_array_reference (name + 1, 0) || /* ${#a[7]} */ |
ccc6cda3 | 7264 | #endif |
726f6388 JA |
7265 | legal_identifier (name + 1)); /* ${#PS1} */ |
7266 | } | |
7267 | ||
7268 | /* Handle the parameter brace expansion that requires us to return the | |
7269 | length of a parameter. */ | |
7117c2d2 | 7270 | static intmax_t |
726f6388 JA |
7271 | parameter_brace_expand_length (name) |
7272 | char *name; | |
7273 | { | |
ccc6cda3 | 7274 | char *t, *newname; |
7117c2d2 | 7275 | intmax_t number, arg_index; |
ccc6cda3 | 7276 | WORD_LIST *list; |
ccc6cda3 | 7277 | SHELL_VAR *var; |
712f80b0 CR |
7278 | |
7279 | var = (SHELL_VAR *)NULL; | |
ccc6cda3 JA |
7280 | |
7281 | if (name[1] == '\0') /* ${#} */ | |
7282 | number = number_of_args (); | |
a0c0a00f | 7283 | else if (DOLLAR_AT_STAR (name[1]) && name[2] == '\0') /* ${#@}, ${#*} */ |
cce855bc | 7284 | number = number_of_args (); |
f73dda09 | 7285 | else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') |
cce855bc JA |
7286 | { |
7287 | /* Take the lengths of some of the shell's special parameters. */ | |
7288 | switch (name[1]) | |
7289 | { | |
7290 | case '-': | |
7291 | t = which_set_flags (); | |
7292 | break; | |
7293 | case '?': | |
7294 | t = itos (last_command_exit_value); | |
7295 | break; | |
7296 | case '$': | |
7297 | t = itos (dollar_dollar_pid); | |
7298 | break; | |
7299 | case '!': | |
7300 | if (last_asynchronous_pid == NO_PID) | |
495aee44 | 7301 | t = (char *)NULL; /* XXX - error if set -u set? */ |
cce855bc | 7302 | else |
f73dda09 | 7303 | t = itos (last_asynchronous_pid); |
cce855bc JA |
7304 | break; |
7305 | case '#': | |
7306 | t = itos (number_of_args ()); | |
7307 | break; | |
7308 | } | |
7309 | number = STRLEN (t); | |
7310 | FREE (t); | |
7311 | } | |
ccc6cda3 | 7312 | #if defined (ARRAY_VARS) |
a0c0a00f | 7313 | else if (valid_array_reference (name + 1, 0)) |
ccc6cda3 JA |
7314 | number = array_length_reference (name + 1); |
7315 | #endif /* ARRAY_VARS */ | |
cce855bc | 7316 | else |
ccc6cda3 JA |
7317 | { |
7318 | number = 0; | |
7319 | ||
f73dda09 | 7320 | if (legal_number (name + 1, &arg_index)) /* ${#1} */ |
ccc6cda3 | 7321 | { |
f73dda09 | 7322 | t = get_dollar_var_value (arg_index); |
495aee44 CR |
7323 | if (t == 0 && unbound_vars_is_error) |
7324 | return INTMAX_MIN; | |
eb873671 | 7325 | number = MB_STRLEN (t); |
ccc6cda3 JA |
7326 | FREE (t); |
7327 | } | |
7328 | #if defined (ARRAY_VARS) | |
3185942a | 7329 | else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var))) |
ccc6cda3 | 7330 | { |
3185942a JA |
7331 | if (assoc_p (var)) |
7332 | t = assoc_reference (assoc_cell (var), "0"); | |
7333 | else | |
7334 | t = array_reference (array_cell (var), 0); | |
495aee44 CR |
7335 | if (t == 0 && unbound_vars_is_error) |
7336 | return INTMAX_MIN; | |
eb873671 | 7337 | number = MB_STRLEN (t); |
ccc6cda3 JA |
7338 | } |
7339 | #endif | |
712f80b0 CR |
7340 | /* Fast path for the common case of taking the length of a non-dynamic |
7341 | scalar variable value. */ | |
7342 | else if ((var || (var = find_variable (name + 1))) && | |
7343 | invisible_p (var) == 0 && | |
7344 | array_p (var) == 0 && assoc_p (var) == 0 && | |
7345 | var->dynamic_value == 0) | |
7346 | number = value_cell (var) ? MB_STRLEN (value_cell (var)) : 0; | |
7347 | else if (var == 0 && unbound_vars_is_error == 0) | |
7348 | number = 0; | |
ccc6cda3 JA |
7349 | else /* ${#PS1} */ |
7350 | { | |
7351 | newname = savestring (name); | |
7352 | newname[0] = '$'; | |
7353 | list = expand_string (newname, Q_DOUBLE_QUOTES); | |
7354 | t = list ? string_list (list) : (char *)NULL; | |
7355 | free (newname); | |
7356 | if (list) | |
7357 | dispose_words (list); | |
7358 | ||
495aee44 | 7359 | number = t ? MB_STRLEN (t) : 0; |
ccc6cda3 JA |
7360 | FREE (t); |
7361 | } | |
7362 | } | |
ccc6cda3 JA |
7363 | |
7364 | return (number); | |
7365 | } | |
7366 | ||
28ef6c31 JA |
7367 | /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression, |
7368 | so we do some ad-hoc parsing of an arithmetic expression to find | |
7369 | the first DELIM, instead of using strchr(3). Two rules: | |
7370 | 1. If the substring contains a `(', read until closing `)'. | |
7371 | 2. If the substring contains a `?', read past one `:' for each `?'. | |
a0c0a00f | 7372 | The SD_ARITHEXP flag to skip_to_delim takes care of doing this. |
28ef6c31 JA |
7373 | */ |
7374 | ||
7375 | static char * | |
7376 | skiparith (substr, delim) | |
7377 | char *substr; | |
7378 | int delim; | |
7379 | { | |
a0c0a00f CR |
7380 | int i; |
7381 | char delims[2]; | |
28ef6c31 | 7382 | |
a0c0a00f CR |
7383 | delims[0] = delim; |
7384 | delims[1] = '\0'; | |
7117c2d2 | 7385 | |
a0c0a00f | 7386 | i = skip_to_delim (substr, 0, delims, SD_ARITHEXP); |
7117c2d2 | 7387 | return (substr + i); |
28ef6c31 JA |
7388 | } |
7389 | ||
ccc6cda3 JA |
7390 | /* Verify and limit the start and end of the desired substring. If |
7391 | VTYPE == 0, a regular shell variable is being used; if it is 1, | |
cce855bc | 7392 | then the positional parameters are being used; if it is 2, then |
e8ce775d JA |
7393 | VALUE is really a pointer to an array variable that should be used. |
7394 | Return value is 1 if both values were OK, 0 if there was a problem | |
7395 | with an invalid expression, or -1 if the values were out of range. */ | |
ccc6cda3 | 7396 | static int |
3185942a JA |
7397 | verify_substring_values (v, value, substr, vtype, e1p, e2p) |
7398 | SHELL_VAR *v; | |
ccc6cda3 | 7399 | char *value, *substr; |
f73dda09 | 7400 | int vtype; |
7117c2d2 | 7401 | intmax_t *e1p, *e2p; |
ccc6cda3 | 7402 | { |
bb70624e | 7403 | char *t, *temp1, *temp2; |
f73dda09 JA |
7404 | arrayind_t len; |
7405 | int expok; | |
ccc6cda3 JA |
7406 | #if defined (ARRAY_VARS) |
7407 | ARRAY *a; | |
3185942a | 7408 | HASH_TABLE *h; |
ccc6cda3 JA |
7409 | #endif |
7410 | ||
28ef6c31 JA |
7411 | /* duplicate behavior of strchr(3) */ |
7412 | t = skiparith (substr, ':'); | |
7413 | if (*t && *t == ':') | |
7117c2d2 | 7414 | *t = '\0'; |
28ef6c31 JA |
7415 | else |
7416 | t = (char *)0; | |
f73dda09 | 7417 | |
0628567a | 7418 | temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES); |
d233b485 | 7419 | *e1p = evalexp (temp1, 0, &expok); /* XXX - EXP_EXPANDED? */ |
ccc6cda3 | 7420 | free (temp1); |
d166f048 JA |
7421 | if (expok == 0) |
7422 | return (0); | |
ccc6cda3 | 7423 | |
f73dda09 | 7424 | len = -1; /* paranoia */ |
ccc6cda3 JA |
7425 | switch (vtype) |
7426 | { | |
7427 | case VT_VARIABLE: | |
d166f048 | 7428 | case VT_ARRAYMEMBER: |
eb873671 | 7429 | len = MB_STRLEN (value); |
ccc6cda3 JA |
7430 | break; |
7431 | case VT_POSPARMS: | |
7432 | len = number_of_args () + 1; | |
3185942a JA |
7433 | if (*e1p == 0) |
7434 | len++; /* add one arg if counting from $0 */ | |
ccc6cda3 JA |
7435 | break; |
7436 | #if defined (ARRAY_VARS) | |
7437 | case VT_ARRAYVAR: | |
eb873671 | 7438 | /* For arrays, the first value deals with array indices. Negative |
3185942a JA |
7439 | offsets count from one past the array's maximum index. Associative |
7440 | arrays treat the number of elements as the maximum index. */ | |
7441 | if (assoc_p (v)) | |
7442 | { | |
7443 | h = assoc_cell (v); | |
7444 | len = assoc_num_elements (h) + (*e1p < 0); | |
7445 | } | |
7446 | else | |
7447 | { | |
7448 | a = (ARRAY *)value; | |
7449 | len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */ | |
7450 | } | |
ccc6cda3 JA |
7451 | break; |
7452 | #endif | |
7453 | } | |
7454 | ||
f73dda09 JA |
7455 | if (len == -1) /* paranoia */ |
7456 | return -1; | |
7457 | ||
ccc6cda3 JA |
7458 | if (*e1p < 0) /* negative offsets count from end */ |
7459 | *e1p += len; | |
7460 | ||
eb873671 | 7461 | if (*e1p > len || *e1p < 0) |
e8ce775d | 7462 | return (-1); |
d166f048 | 7463 | |
b80f6443 JA |
7464 | #if defined (ARRAY_VARS) |
7465 | /* For arrays, the second offset deals with the number of elements. */ | |
7466 | if (vtype == VT_ARRAYVAR) | |
3185942a | 7467 | len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a); |
b80f6443 JA |
7468 | #endif |
7469 | ||
ccc6cda3 JA |
7470 | if (t) |
7471 | { | |
7472 | t++; | |
bb70624e | 7473 | temp2 = savestring (t); |
0628567a | 7474 | temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES); |
bb70624e | 7475 | free (temp2); |
ccc6cda3 | 7476 | t[-1] = ':'; |
d233b485 | 7477 | *e2p = evalexp (temp1, 0, &expok); /* XXX - EXP_EXPANDED? */ |
ccc6cda3 | 7478 | free (temp1); |
d166f048 | 7479 | if (expok == 0) |
28ef6c31 | 7480 | return (0); |
d233b485 CR |
7481 | |
7482 | /* Should we allow positional parameter length < 0 to count backwards | |
7483 | from end of positional parameters? */ | |
ac50fbac | 7484 | #if 1 |
495aee44 | 7485 | if ((vtype == VT_ARRAYVAR || vtype == VT_POSPARMS) && *e2p < 0) |
712f80b0 | 7486 | #else /* XXX - TAG: bash-5.1 */ |
ac50fbac CR |
7487 | if (vtype == VT_ARRAYVAR && *e2p < 0) |
7488 | #endif | |
28ef6c31 | 7489 | { |
b80f6443 | 7490 | internal_error (_("%s: substring expression < 0"), t); |
ccc6cda3 | 7491 | return (0); |
28ef6c31 | 7492 | } |
b80f6443 JA |
7493 | #if defined (ARRAY_VARS) |
7494 | /* In order to deal with sparse arrays, push the intelligence about how | |
7495 | to deal with the number of elements desired down to the array- | |
7496 | specific functions. */ | |
7497 | if (vtype != VT_ARRAYVAR) | |
7498 | #endif | |
7499 | { | |
495aee44 CR |
7500 | if (*e2p < 0) |
7501 | { | |
7502 | *e2p += len; | |
7503 | if (*e2p < 0 || *e2p < *e1p) | |
7504 | { | |
7505 | internal_error (_("%s: substring expression < 0"), t); | |
7506 | return (0); | |
7507 | } | |
7508 | } | |
7509 | else | |
7510 | *e2p += *e1p; /* want E2 chars starting at E1 */ | |
b80f6443 JA |
7511 | if (*e2p > len) |
7512 | *e2p = len; | |
7513 | } | |
ccc6cda3 JA |
7514 | } |
7515 | else | |
7516 | *e2p = len; | |
7517 | ||
7518 | return (1); | |
7519 | } | |
7520 | ||
ccc6cda3 | 7521 | /* Return the type of variable specified by VARNAME (simple variable, |
cce855bc | 7522 | positional param, or array variable). Also return the value specified |
7117c2d2 | 7523 | by VARNAME (value of a variable or a reference to an array element). |
495aee44 CR |
7524 | QUOTED is the standard description of quoting state, using Q_* defines. |
7525 | FLAGS is currently a set of flags to pass to array_value. If IND is | |
7526 | non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is | |
7527 | passed to array_value so the array index is not computed again. | |
7117c2d2 JA |
7528 | If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL |
7529 | characters in the value are quoted with CTLESC and takes appropriate | |
7530 | steps. For convenience, *VALP is set to the dequoted VALUE. */ | |
ccc6cda3 | 7531 | static int |
495aee44 | 7532 | get_var_and_type (varname, value, ind, quoted, flags, varp, valp) |
ccc6cda3 | 7533 | char *varname, *value; |
495aee44 CR |
7534 | arrayind_t ind; |
7535 | int quoted, flags; | |
ccc6cda3 JA |
7536 | SHELL_VAR **varp; |
7537 | char **valp; | |
7538 | { | |
ac50fbac CR |
7539 | int vtype, want_indir; |
7540 | char *temp, *vname; | |
ccc6cda3 | 7541 | SHELL_VAR *v; |
495aee44 | 7542 | arrayind_t lind; |
ccc6cda3 | 7543 | |
ac50fbac CR |
7544 | want_indir = *varname == '!' && |
7545 | (legal_variable_starter ((unsigned char)varname[1]) || DIGIT (varname[1]) | |
7546 | || VALID_INDIR_PARAM (varname[1])); | |
7547 | if (want_indir) | |
7548 | vname = parameter_brace_find_indir (varname+1, SPECIAL_VAR (varname, 1), quoted, 1); | |
a0c0a00f | 7549 | /* XXX - what if vname == 0 || *vname == 0 ? */ |
ac50fbac CR |
7550 | else |
7551 | vname = varname; | |
a0c0a00f CR |
7552 | |
7553 | if (vname == 0) | |
7554 | { | |
7555 | vtype = VT_VARIABLE; | |
7556 | *varp = (SHELL_VAR *)NULL; | |
7557 | *valp = (char *)NULL; | |
7558 | return (vtype); | |
7559 | } | |
7560 | ||
7117c2d2 | 7561 | /* This sets vtype to VT_VARIABLE or VT_POSPARMS */ |
a0c0a00f | 7562 | vtype = STR_DOLLAR_AT_STAR (vname); |
ac50fbac | 7563 | if (vtype == VT_POSPARMS && vname[0] == '*') |
b80f6443 | 7564 | vtype |= VT_STARSUB; |
ccc6cda3 JA |
7565 | *varp = (SHELL_VAR *)NULL; |
7566 | ||
7567 | #if defined (ARRAY_VARS) | |
a0c0a00f | 7568 | if (valid_array_reference (vname, 0)) |
ccc6cda3 | 7569 | { |
d233b485 | 7570 | v = array_variable_part (vname, 0, &temp, (int *)0); |
495aee44 CR |
7571 | /* If we want to signal array_value to use an already-computed index, |
7572 | set LIND to that index */ | |
7573 | lind = (ind != INTMAX_MIN && (flags & AV_USEIND)) ? ind : 0; | |
ac50fbac CR |
7574 | if (v && invisible_p (v)) |
7575 | { | |
7576 | vtype = VT_ARRAYMEMBER; | |
7577 | *varp = (SHELL_VAR *)NULL; | |
7578 | *valp = (char *)NULL; | |
7579 | } | |
3185942a | 7580 | if (v && (array_p (v) || assoc_p (v))) |
d233b485 CR |
7581 | { |
7582 | if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == RBRACK) | |
ccc6cda3 | 7583 | { |
ac50fbac | 7584 | /* Callers have to differentiate between indexed and associative */ |
ccc6cda3 | 7585 | vtype = VT_ARRAYVAR; |
b80f6443 JA |
7586 | if (temp[0] == '*') |
7587 | vtype |= VT_STARSUB; | |
3185942a | 7588 | *valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v); |
ccc6cda3 JA |
7589 | } |
7590 | else | |
7591 | { | |
d166f048 | 7592 | vtype = VT_ARRAYMEMBER; |
ac50fbac | 7593 | *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind); |
ccc6cda3 JA |
7594 | } |
7595 | *varp = v; | |
7596 | } | |
d233b485 CR |
7597 | else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == RBRACK)) |
7598 | { | |
7599 | vtype = VT_VARIABLE; | |
7600 | *varp = v; | |
7601 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
712f80b0 | 7602 | *valp = value ? dequote_string (value) : (char *)NULL; |
d233b485 CR |
7603 | else |
7604 | *valp = value ? dequote_escapes (value) : (char *)NULL; | |
7605 | } | |
7606 | else | |
7607 | { | |
7608 | vtype = VT_ARRAYMEMBER; | |
7609 | *varp = v; | |
7610 | *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind); | |
7611 | } | |
7612 | } | |
7613 | else if ((v = find_variable (vname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v))) | |
7614 | { | |
7615 | vtype = VT_ARRAYMEMBER; | |
7616 | *varp = v; | |
7617 | *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0); | |
7618 | } | |
7619 | else | |
7620 | #endif | |
7621 | { | |
7622 | if (value && vtype == VT_VARIABLE) | |
7623 | { | |
7624 | *varp = find_variable (vname); | |
7625 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
7626 | *valp = dequote_string (value); | |
7627 | else | |
7628 | *valp = dequote_escapes (value); | |
7629 | } | |
7630 | else | |
7631 | *valp = value; | |
7632 | } | |
7633 | ||
7634 | if (want_indir) | |
7635 | free (vname); | |
7636 | ||
7637 | return vtype; | |
7638 | } | |
7639 | ||
7640 | /***********************************************************/ | |
7641 | /* */ | |
7642 | /* Functions to perform transformations on variable values */ | |
7643 | /* */ | |
7644 | /***********************************************************/ | |
7645 | ||
7646 | static char * | |
7647 | string_var_assignment (v, s) | |
7648 | SHELL_VAR *v; | |
7649 | char *s; | |
7650 | { | |
7651 | char flags[MAX_ATTRIBUTES], *ret, *val; | |
7652 | int i; | |
7653 | ||
712f80b0 | 7654 | val = (v && (invisible_p (v) || var_isset (v) == 0)) ? (char *)NULL : sh_quote_reusable (s, 0); |
d233b485 | 7655 | i = var_attribute_string (v, 0, flags); |
712f80b0 CR |
7656 | if (i == 0 && val == 0) |
7657 | return (char *)NULL; | |
7658 | ||
7659 | ret = (char *)xmalloc (i + STRLEN (val) + strlen (v->name) + 16 + MAX_ATTRIBUTES); | |
7660 | if (i > 0 && val == 0) | |
7661 | sprintf (ret, "declare -%s %s", flags, v->name); | |
7662 | else if (i > 0) | |
d233b485 CR |
7663 | sprintf (ret, "declare -%s %s=%s", flags, v->name, val); |
7664 | else | |
7665 | sprintf (ret, "%s=%s", v->name, val); | |
7666 | free (val); | |
7667 | return ret; | |
7668 | } | |
7669 | ||
7670 | #if defined (ARRAY_VARS) | |
7671 | static char * | |
712f80b0 | 7672 | array_var_assignment (v, itype, quoted, atype) |
d233b485 | 7673 | SHELL_VAR *v; |
712f80b0 | 7674 | int itype, quoted, atype; |
d233b485 CR |
7675 | { |
7676 | char *ret, *val, flags[MAX_ATTRIBUTES]; | |
7677 | int i; | |
7678 | ||
7679 | if (v == 0) | |
7680 | return (char *)NULL; | |
712f80b0 CR |
7681 | if (atype == 2) |
7682 | val = array_p (v) ? array_to_kvpair (array_cell (v), 0) | |
7683 | : assoc_to_kvpair (assoc_cell (v), 0); | |
7684 | else | |
7685 | val = array_p (v) ? array_to_assign (array_cell (v), 0) | |
7686 | : assoc_to_assign (assoc_cell (v), 0); | |
7687 | ||
7688 | if (val == 0 && (invisible_p (v) || var_isset (v) == 0)) | |
7689 | ; /* placeholder */ | |
7690 | else if (val == 0) | |
d233b485 CR |
7691 | { |
7692 | val = (char *)xmalloc (3); | |
7693 | val[0] = LPAREN; | |
7694 | val[1] = RPAREN; | |
7695 | val[2] = 0; | |
7696 | } | |
7697 | else | |
7698 | { | |
7699 | ret = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) ? quote_string (val) : quote_escapes (val); | |
7700 | free (val); | |
7701 | val = ret; | |
7702 | } | |
712f80b0 CR |
7703 | |
7704 | if (atype == 2) | |
7705 | return val; | |
7706 | ||
d233b485 | 7707 | i = var_attribute_string (v, 0, flags); |
712f80b0 CR |
7708 | ret = (char *)xmalloc (i + STRLEN (val) + strlen (v->name) + 16); |
7709 | if (val) | |
7710 | sprintf (ret, "declare -%s %s=%s", flags, v->name, val); | |
7711 | else | |
7712 | sprintf (ret, "declare -%s %s", flags, v->name); | |
d233b485 CR |
7713 | free (val); |
7714 | return ret; | |
7715 | } | |
7716 | #endif | |
7717 | ||
7718 | static char * | |
7719 | pos_params_assignment (list, itype, quoted) | |
7720 | WORD_LIST *list; | |
7721 | int itype; | |
7722 | int quoted; | |
7723 | { | |
7724 | char *temp, *ret; | |
7725 | ||
7726 | /* first, we transform the list to quote each word. */ | |
7727 | temp = list_transform ('Q', (SHELL_VAR *)0, list, itype, quoted); | |
7728 | ret = (char *)xmalloc (strlen (temp) + 8); | |
7729 | strcpy (ret, "set -- "); | |
7730 | strcpy (ret + 7, temp); | |
7731 | free (temp); | |
7732 | return ret; | |
7733 | } | |
7734 | ||
7735 | static char * | |
7736 | string_transform (xc, v, s) | |
7737 | int xc; | |
7738 | SHELL_VAR *v; | |
7739 | char *s; | |
7740 | { | |
7741 | char *ret, flags[MAX_ATTRIBUTES], *t; | |
7742 | int i; | |
7743 | ||
712f80b0 CR |
7744 | if (((xc == 'A' || xc == 'a') && v == 0)) |
7745 | return (char *)NULL; | |
7746 | else if (xc != 'a' && xc != 'A' && s == 0) | |
d233b485 CR |
7747 | return (char *)NULL; |
7748 | ||
7749 | switch (xc) | |
7750 | { | |
7751 | /* Transformations that interrogate the variable */ | |
7752 | case 'a': | |
7753 | i = var_attribute_string (v, 0, flags); | |
7754 | ret = (i > 0) ? savestring (flags) : (char *)NULL; | |
7755 | break; | |
7756 | case 'A': | |
7757 | ret = string_var_assignment (v, s); | |
7758 | break; | |
712f80b0 CR |
7759 | case 'K': |
7760 | ret = sh_quote_reusable (s, 0); | |
7761 | break; | |
d233b485 CR |
7762 | /* Transformations that modify the variable's value */ |
7763 | case 'E': | |
7764 | t = ansiexpand (s, 0, strlen (s), (int *)0); | |
7765 | ret = dequote_escapes (t); | |
7766 | free (t); | |
7767 | break; | |
7768 | case 'P': | |
7769 | ret = decode_prompt_string (s); | |
7770 | break; | |
7771 | case 'Q': | |
7772 | ret = sh_quote_reusable (s, 0); | |
7773 | break; | |
712f80b0 CR |
7774 | case 'U': |
7775 | ret = sh_modcase (s, 0, CASE_UPPER); | |
7776 | break; | |
7777 | case 'u': | |
7778 | ret = sh_modcase (s, 0, CASE_UPFIRST); /* capitalize */ | |
7779 | break; | |
7780 | case 'L': | |
7781 | ret = sh_modcase (s, 0, CASE_LOWER); | |
7782 | break; | |
d233b485 CR |
7783 | default: |
7784 | ret = (char *)NULL; | |
7785 | break; | |
7786 | } | |
7787 | return ret; | |
7788 | } | |
7789 | ||
7790 | static char * | |
7791 | list_transform (xc, v, list, itype, quoted) | |
7792 | int xc; | |
7793 | SHELL_VAR *v; | |
7794 | WORD_LIST *list; | |
7795 | int itype, quoted; | |
7796 | { | |
7797 | WORD_LIST *new, *l; | |
7798 | WORD_DESC *w; | |
7799 | char *tword; | |
7800 | int qflags; | |
7801 | ||
7802 | for (new = (WORD_LIST *)NULL, l = list; l; l = l->next) | |
7803 | { | |
7804 | tword = string_transform (xc, v, l->word->word); | |
7805 | w = alloc_word_desc (); | |
7806 | w->word = tword ? tword : savestring (""); /* XXX */ | |
7807 | new = make_word_list (w, new); | |
7808 | } | |
7809 | l = REVERSE_LIST (new, WORD_LIST *); | |
7810 | ||
7811 | qflags = quoted; | |
7812 | /* If we are expanding in a context where word splitting will not be | |
7813 | performed, treat as quoted. This changes how $* will be expanded. */ | |
7814 | if (itype == '*' && expand_no_split_dollar_star && ifs_is_null) | |
7815 | qflags |= Q_DOUBLE_QUOTES; /* Posix interp 888 */ | |
7816 | ||
712f80b0 | 7817 | tword = string_list_pos_params (itype, l, qflags, 0); |
d233b485 CR |
7818 | dispose_words (l); |
7819 | ||
7820 | return (tword); | |
7821 | } | |
7822 | ||
7823 | static char * | |
7824 | parameter_list_transform (xc, itype, quoted) | |
7825 | int xc; | |
7826 | int itype; | |
7827 | int quoted; | |
7828 | { | |
7829 | char *ret; | |
7830 | WORD_LIST *list; | |
7831 | ||
7832 | list = list_rest_of_args (); | |
7833 | if (list == 0) | |
7834 | return ((char *)NULL); | |
7835 | if (xc == 'A') | |
7836 | ret = pos_params_assignment (list, itype, quoted); | |
7837 | else | |
7838 | ret = list_transform (xc, (SHELL_VAR *)0, list, itype, quoted); | |
7839 | dispose_words (list); | |
7840 | return (ret); | |
7841 | } | |
7842 | ||
7843 | #if defined (ARRAY_VARS) | |
7844 | static char * | |
712f80b0 | 7845 | array_transform (xc, var, starsub, quoted) |
d233b485 CR |
7846 | int xc; |
7847 | SHELL_VAR *var; | |
712f80b0 | 7848 | int starsub; /* so we can figure out how it's indexed */ |
d233b485 CR |
7849 | int quoted; |
7850 | { | |
7851 | ARRAY *a; | |
7852 | HASH_TABLE *h; | |
7853 | int itype; | |
7854 | char *ret; | |
7855 | WORD_LIST *list; | |
7856 | SHELL_VAR *v; | |
7857 | ||
712f80b0 | 7858 | v = var; /* XXX - for now */ |
d233b485 | 7859 | |
712f80b0 | 7860 | itype = starsub ? '*' : '@'; |
d233b485 CR |
7861 | |
7862 | if (xc == 'A') | |
712f80b0 CR |
7863 | return (array_var_assignment (v, itype, quoted, 1)); |
7864 | else if (xc == 'K') | |
7865 | return (array_var_assignment (v, itype, quoted, 2)); | |
7866 | ||
7867 | /* special case for unset arrays and attributes */ | |
7868 | if (xc == 'a' && (invisible_p (v) || var_isset (v) == 0)) | |
7869 | { | |
7870 | char flags[MAX_ATTRIBUTES]; | |
7871 | int i; | |
7872 | ||
7873 | i = var_attribute_string (v, 0, flags); | |
7874 | return ((i > 0) ? savestring (flags) : (char *)NULL); | |
7875 | } | |
d233b485 CR |
7876 | |
7877 | a = (v && array_p (v)) ? array_cell (v) : 0; | |
7878 | h = (v && assoc_p (v)) ? assoc_cell (v) : 0; | |
712f80b0 | 7879 | |
d233b485 CR |
7880 | list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0); |
7881 | if (list == 0) | |
7882 | return ((char *)NULL); | |
7883 | ret = list_transform (xc, v, list, itype, quoted); | |
7884 | dispose_words (list); | |
7885 | ||
7886 | return ret; | |
7887 | } | |
7888 | #endif /* ARRAY_VARS */ | |
7889 | ||
3eb0018e CR |
7890 | static int |
7891 | valid_parameter_transform (xform) | |
7892 | char *xform; | |
7893 | { | |
7894 | if (xform[1]) | |
7895 | return 0; | |
7896 | ||
7897 | /* check for valid values of xform[0] */ | |
7898 | switch (xform[0]) | |
7899 | { | |
7900 | case 'a': /* expand to a string with just attributes */ | |
7901 | case 'A': /* expand as an assignment statement with attributes */ | |
7902 | case 'K': /* expand assoc array to list of key/value pairs */ | |
7903 | case 'E': /* expand like $'...' */ | |
7904 | case 'P': /* expand like prompt string */ | |
7905 | case 'Q': /* quote reusably */ | |
7906 | case 'U': /* transform to uppercase */ | |
7907 | case 'u': /* tranform by capitalizing */ | |
7908 | case 'L': /* transform to lowercase */ | |
7909 | return 1; | |
7910 | default: | |
7911 | return 0; | |
7912 | } | |
7913 | } | |
7914 | ||
d233b485 CR |
7915 | static char * |
7916 | parameter_brace_transform (varname, value, ind, xform, rtype, quoted, pflags, flags) | |
7917 | char *varname, *value; | |
7918 | int ind; | |
7919 | char *xform; | |
7920 | int rtype, quoted, pflags, flags; | |
7921 | { | |
712f80b0 | 7922 | int vtype, xc, starsub; |
d233b485 CR |
7923 | char *temp1, *val, *oname; |
7924 | SHELL_VAR *v; | |
7925 | ||
7926 | xc = xform[0]; | |
7927 | if (value == 0 && xc != 'A' && xc != 'a') | |
7928 | return ((char *)NULL); | |
7929 | ||
7930 | oname = this_command_name; | |
7931 | this_command_name = varname; | |
7932 | ||
7933 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); | |
7934 | if (vtype == -1) | |
7935 | { | |
7936 | this_command_name = oname; | |
7937 | return ((char *)NULL); | |
7938 | } | |
7939 | ||
3eb0018e | 7940 | if (valid_parameter_transform (xform) == 0) |
d233b485 | 7941 | { |
d233b485 CR |
7942 | this_command_name = oname; |
7943 | return &expand_param_error; | |
7944 | } | |
7945 | ||
712f80b0 CR |
7946 | starsub = vtype & VT_STARSUB; |
7947 | vtype &= ~VT_STARSUB; | |
7948 | ||
d233b485 CR |
7949 | /* If we are asked to display the attributes of an unset variable, V will |
7950 | be NULL after the call to get_var_and_type. Double-check here. */ | |
712f80b0 | 7951 | if ((xc == 'a' || xc == 'A') && vtype == VT_VARIABLE && varname && v == 0) |
d233b485 CR |
7952 | v = find_variable (varname); |
7953 | ||
7954 | temp1 = (char *)NULL; /* shut up gcc */ | |
712f80b0 | 7955 | switch (vtype) |
d233b485 CR |
7956 | { |
7957 | case VT_VARIABLE: | |
7958 | case VT_ARRAYMEMBER: | |
7959 | temp1 = string_transform (xc, v, val); | |
7960 | if (vtype == VT_VARIABLE) | |
7961 | FREE (val); | |
7962 | if (temp1) | |
7963 | { | |
7964 | val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
7965 | ? quote_string (temp1) | |
7966 | : quote_escapes (temp1); | |
7967 | free (temp1); | |
7968 | temp1 = val; | |
7969 | } | |
7970 | break; | |
7971 | #if defined (ARRAY_VARS) | |
7972 | case VT_ARRAYVAR: | |
712f80b0 | 7973 | temp1 = array_transform (xc, v, starsub, quoted); |
d233b485 | 7974 | if (temp1 && quoted == 0 && ifs_is_null) |
95732b49 | 7975 | { |
d233b485 | 7976 | /* Posix interp 888 */ |
95732b49 | 7977 | } |
d233b485 | 7978 | else if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) |
3185942a | 7979 | { |
d233b485 CR |
7980 | val = quote_escapes (temp1); |
7981 | free (temp1); | |
7982 | temp1 = val; | |
3185942a | 7983 | } |
d233b485 | 7984 | break; |
ccc6cda3 | 7985 | #endif |
d233b485 CR |
7986 | case VT_POSPARMS: |
7987 | temp1 = parameter_list_transform (xc, varname[0], quoted); | |
7988 | if (temp1 && quoted == 0 && ifs_is_null) | |
b80f6443 | 7989 | { |
d233b485 | 7990 | /* Posix interp 888 */ |
b80f6443 | 7991 | } |
d233b485 CR |
7992 | else if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)) |
7993 | { | |
7994 | val = quote_escapes (temp1); | |
7995 | free (temp1); | |
7996 | temp1 = val; | |
7997 | } | |
7998 | break; | |
b80f6443 | 7999 | } |
ccc6cda3 | 8000 | |
d233b485 CR |
8001 | this_command_name = oname; |
8002 | return temp1; | |
ccc6cda3 JA |
8003 | } |
8004 | ||
cce855bc JA |
8005 | /******************************************************/ |
8006 | /* */ | |
8007 | /* Functions to extract substrings of variable values */ | |
8008 | /* */ | |
8009 | /******************************************************/ | |
8010 | ||
b80f6443 JA |
8011 | #if defined (HANDLE_MULTIBYTE) |
8012 | /* Character-oriented rather than strictly byte-oriented substrings. S and | |
8013 | E, rather being strict indices into STRING, indicate character (possibly | |
8014 | multibyte character) positions that require calculation. | |
8015 | Used by the ${param:offset[:length]} expansion. */ | |
8016 | static char * | |
8017 | mb_substring (string, s, e) | |
8018 | char *string; | |
8019 | int s, e; | |
8020 | { | |
8021 | char *tt; | |
a0c0a00f CR |
8022 | int start, stop, i; |
8023 | size_t slen; | |
b80f6443 JA |
8024 | DECLARE_MBSTATE; |
8025 | ||
8026 | start = 0; | |
95732b49 JA |
8027 | /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */ |
8028 | slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0; | |
b80f6443 JA |
8029 | |
8030 | i = s; | |
8031 | while (string[start] && i--) | |
8032 | ADVANCE_CHAR (string, slen, start); | |
8033 | stop = start; | |
8034 | i = e - s; | |
8035 | while (string[stop] && i--) | |
8036 | ADVANCE_CHAR (string, slen, stop); | |
8037 | tt = substring (string, start, stop); | |
8038 | return tt; | |
8039 | } | |
8040 | #endif | |
8041 | ||
ccc6cda3 JA |
8042 | /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME |
8043 | is `@', use the positional parameters; otherwise, use the value of | |
8044 | VARNAME. If VARNAME is an array variable, use the array elements. */ | |
8045 | ||
8046 | static char * | |
d233b485 | 8047 | parameter_brace_substring (varname, value, ind, substr, quoted, pflags, flags) |
495aee44 CR |
8048 | char *varname, *value; |
8049 | int ind; | |
8050 | char *substr; | |
d233b485 | 8051 | int quoted, pflags, flags; |
ccc6cda3 | 8052 | { |
7117c2d2 | 8053 | intmax_t e1, e2; |
b80f6443 | 8054 | int vtype, r, starsub; |
0628567a | 8055 | char *temp, *val, *tt, *oname; |
ccc6cda3 JA |
8056 | SHELL_VAR *v; |
8057 | ||
a0c0a00f | 8058 | if (value == 0 && ((varname[0] != '@' && varname[0] != '*') || varname[1])) |
ccc6cda3 JA |
8059 | return ((char *)NULL); |
8060 | ||
0628567a | 8061 | oname = this_command_name; |
ccc6cda3 JA |
8062 | this_command_name = varname; |
8063 | ||
495aee44 | 8064 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
ccc6cda3 | 8065 | if (vtype == -1) |
0628567a JA |
8066 | { |
8067 | this_command_name = oname; | |
8068 | return ((char *)NULL); | |
8069 | } | |
ccc6cda3 | 8070 | |
b80f6443 JA |
8071 | starsub = vtype & VT_STARSUB; |
8072 | vtype &= ~VT_STARSUB; | |
8073 | ||
3185942a | 8074 | r = verify_substring_values (v, val, substr, vtype, &e1, &e2); |
0628567a | 8075 | this_command_name = oname; |
e8ce775d | 8076 | if (r <= 0) |
ac50fbac CR |
8077 | { |
8078 | if (vtype == VT_VARIABLE) | |
8079 | FREE (val); | |
8080 | return ((r == 0) ? &expand_param_error : (char *)NULL); | |
8081 | } | |
ccc6cda3 JA |
8082 | |
8083 | switch (vtype) | |
8084 | { | |
8085 | case VT_VARIABLE: | |
d166f048 | 8086 | case VT_ARRAYMEMBER: |
b80f6443 JA |
8087 | #if defined (HANDLE_MULTIBYTE) |
8088 | if (MB_CUR_MAX > 1) | |
8089 | tt = mb_substring (val, e1, e2); | |
8090 | else | |
8091 | #endif | |
7117c2d2 | 8092 | tt = substring (val, e1, e2); |
b80f6443 | 8093 | |
7117c2d2 JA |
8094 | if (vtype == VT_VARIABLE) |
8095 | FREE (val); | |
8096 | if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | |
8097 | temp = quote_string (tt); | |
8098 | else | |
8099 | temp = tt ? quote_escapes (tt) : (char *)NULL; | |
8100 | FREE (tt); | |
ccc6cda3 JA |
8101 | break; |
8102 | case VT_POSPARMS: | |
d233b485 CR |
8103 | case VT_ARRAYVAR: |
8104 | if (vtype == VT_POSPARMS) | |
712f80b0 | 8105 | tt = pos_params (varname, e1, e2, quoted, pflags); |
d233b485 CR |
8106 | #if defined (ARRAY_VARS) |
8107 | /* assoc_subrange and array_subrange both call string_list_pos_params, | |
8108 | so we can treat this case just like VT_POSPARAMS. */ | |
8109 | else if (assoc_p (v)) | |
8110 | /* we convert to list and take first e2 elements starting at e1th | |
8111 | element -- officially undefined for now */ | |
712f80b0 | 8112 | tt = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted, pflags); |
d233b485 CR |
8113 | else |
8114 | /* We want E2 to be the number of elements desired (arrays can be | |
8115 | sparse, so verify_substring_values just returns the numbers | |
8116 | specified and we rely on array_subrange to understand how to | |
8117 | deal with them). */ | |
712f80b0 | 8118 | tt = array_subrange (array_cell (v), e1, e2, starsub, quoted, pflags); |
d233b485 CR |
8119 | #endif |
8120 | /* We want to leave this alone in every case where pos_params/ | |
8121 | string_list_pos_params quotes the list members */ | |
8122 | if (tt && quoted == 0 && ifs_is_null) | |
8123 | { | |
8124 | temp = tt; /* Posix interp 888 */ | |
8125 | } | |
712f80b0 CR |
8126 | else if (tt && quoted == 0 && (pflags & PF_ASSIGNRHS)) |
8127 | { | |
8128 | temp = tt; /* Posix interp 888 */ | |
8129 | } | |
d233b485 | 8130 | else if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0) |
7117c2d2 JA |
8131 | { |
8132 | temp = tt ? quote_escapes (tt) : (char *)NULL; | |
8133 | FREE (tt); | |
8134 | } | |
8135 | else | |
8136 | temp = tt; | |
ccc6cda3 | 8137 | break; |
d233b485 | 8138 | |
f73dda09 JA |
8139 | default: |
8140 | temp = (char *)NULL; | |
ccc6cda3 JA |
8141 | } |
8142 | ||
8143 | return temp; | |
8144 | } | |
8145 | ||
cce855bc JA |
8146 | /****************************************************************/ |
8147 | /* */ | |
8148 | /* Functions to perform pattern substitution on variable values */ | |
8149 | /* */ | |
8150 | /****************************************************************/ | |
8151 | ||
d233b485 | 8152 | #ifdef INCLUDE_UNUSED |
495aee44 CR |
8153 | static int |
8154 | shouldexp_replacement (s) | |
8155 | char *s; | |
8156 | { | |
8157 | register char *p; | |
8158 | ||
8159 | for (p = s; p && *p; p++) | |
8160 | { | |
8161 | if (*p == '\\') | |
8162 | p++; | |
8163 | else if (*p == '&') | |
8164 | return 1; | |
8165 | } | |
8166 | return 0; | |
8167 | } | |
a0c0a00f | 8168 | #endif |
495aee44 | 8169 | |
ccc6cda3 JA |
8170 | char * |
8171 | pat_subst (string, pat, rep, mflags) | |
8172 | char *string, *pat, *rep; | |
8173 | int mflags; | |
8174 | { | |
712f80b0 | 8175 | char *ret, *s, *e, *str, *rstr, *mstr, *send; |
a0c0a00f CR |
8176 | int rptr, mtype, rxpand, mlen; |
8177 | size_t rsize, l, replen, rslen; | |
712f80b0 | 8178 | DECLARE_MBSTATE; |
495aee44 | 8179 | |
d233b485 | 8180 | if (string == 0) |
495aee44 | 8181 | return (savestring ("")); |
ccc6cda3 | 8182 | |
b72432fd JA |
8183 | mtype = mflags & MATCH_TYPEMASK; |
8184 | ||
712f80b0 | 8185 | #if 0 /* TAG: bash-5.2? */ |
495aee44 CR |
8186 | rxpand = (rep && *rep) ? shouldexp_replacement (rep) : 0; |
8187 | #else | |
8188 | rxpand = 0; | |
8189 | #endif | |
8190 | ||
b72432fd JA |
8191 | /* Special cases: |
8192 | * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING | |
8193 | * with REP and return the result. | |
8194 | * 2. A null pattern with mtype == MATCH_END means to append REP to | |
8195 | * STRING and return the result. | |
a0c0a00f CR |
8196 | * 3. A null STRING with a matching pattern means to append REP to |
8197 | * STRING and return the result. | |
495aee44 | 8198 | * These don't understand or process `&' in the replacement string. |
b72432fd JA |
8199 | */ |
8200 | if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END)) | |
8201 | { | |
8202 | replen = STRLEN (rep); | |
495aee44 | 8203 | l = STRLEN (string); |
f73dda09 | 8204 | ret = (char *)xmalloc (replen + l + 2); |
bb70624e JA |
8205 | if (replen == 0) |
8206 | strcpy (ret, string); | |
8207 | else if (mtype == MATCH_BEG) | |
b72432fd JA |
8208 | { |
8209 | strcpy (ret, rep); | |
8210 | strcpy (ret + replen, string); | |
8211 | } | |
8212 | else | |
8213 | { | |
8214 | strcpy (ret, string); | |
8215 | strcpy (ret + l, rep); | |
8216 | } | |
8217 | return (ret); | |
8218 | } | |
a0c0a00f CR |
8219 | else if (*string == 0 && (match_pattern (string, pat, mtype, &s, &e) != 0)) |
8220 | { | |
8221 | replen = STRLEN (rep); | |
8222 | ret = (char *)xmalloc (replen + 1); | |
8223 | if (replen == 0) | |
8224 | ret[0] = '\0'; | |
8225 | else | |
8226 | strcpy (ret, rep); | |
8227 | return (ret); | |
8228 | } | |
b72432fd | 8229 | |
f73dda09 | 8230 | ret = (char *)xmalloc (rsize = 64); |
ccc6cda3 | 8231 | ret[0] = '\0'; |
712f80b0 | 8232 | send = string + strlen (string); |
ccc6cda3 | 8233 | |
a0c0a00f | 8234 | for (replen = STRLEN (rep), rptr = 0, str = string; *str;) |
ccc6cda3 JA |
8235 | { |
8236 | if (match_pattern (str, pat, mtype, &s, &e) == 0) | |
8237 | break; | |
8238 | l = s - str; | |
495aee44 | 8239 | |
a0c0a00f | 8240 | if (rep && rxpand) |
495aee44 | 8241 | { |
a0c0a00f CR |
8242 | int x; |
8243 | mlen = e - s; | |
8244 | mstr = xmalloc (mlen + 1); | |
495aee44 CR |
8245 | for (x = 0; x < mlen; x++) |
8246 | mstr[x] = s[x]; | |
a0c0a00f CR |
8247 | mstr[mlen] = '\0'; |
8248 | rstr = strcreplace (rep, '&', mstr, 0); | |
8249 | free (mstr); | |
8250 | rslen = strlen (rstr); | |
495aee44 CR |
8251 | } |
8252 | else | |
a0c0a00f CR |
8253 | { |
8254 | rstr = rep; | |
8255 | rslen = replen; | |
8256 | } | |
495aee44 CR |
8257 | |
8258 | RESIZE_MALLOCED_BUFFER (ret, rptr, (l + rslen), rsize, 64); | |
ccc6cda3 JA |
8259 | |
8260 | /* OK, now copy the leading unmatched portion of the string (from | |
8261 | str to s) to ret starting at rptr (the current offset). Then copy | |
28ef6c31 JA |
8262 | the replacement string at ret + rptr + (s - str). Increment |
8263 | rptr (if necessary) and str and go on. */ | |
ccc6cda3 JA |
8264 | if (l) |
8265 | { | |
8266 | strncpy (ret + rptr, str, l); | |
8267 | rptr += l; | |
8268 | } | |
8269 | if (replen) | |
8270 | { | |
495aee44 CR |
8271 | strncpy (ret + rptr, rstr, rslen); |
8272 | rptr += rslen; | |
ccc6cda3 JA |
8273 | } |
8274 | str = e; /* e == end of match */ | |
b80f6443 | 8275 | |
495aee44 CR |
8276 | if (rstr != rep) |
8277 | free (rstr); | |
8278 | ||
ccc6cda3 | 8279 | if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY) |
28ef6c31 | 8280 | break; |
b80f6443 JA |
8281 | |
8282 | if (s == e) | |
0001803f CR |
8283 | { |
8284 | /* On a zero-length match, make sure we copy one character, since | |
8285 | we increment one character to avoid infinite recursion. */ | |
712f80b0 CR |
8286 | char *p, *origp, *origs; |
8287 | size_t clen; | |
8288 | ||
8289 | RESIZE_MALLOCED_BUFFER (ret, rptr, locale_mb_cur_max, rsize, 64); | |
8290 | #if defined (HANDLE_MULTIBYTE) | |
8291 | p = origp = ret + rptr; | |
8292 | origs = str; | |
8293 | COPY_CHAR_P (p, str, send); | |
8294 | rptr += p - origp; | |
8295 | e += str - origs; | |
8296 | #else | |
0001803f CR |
8297 | ret[rptr++] = *str++; |
8298 | e++; /* avoid infinite recursion on zero-length match */ | |
712f80b0 | 8299 | #endif |
0001803f | 8300 | } |
ccc6cda3 JA |
8301 | } |
8302 | ||
8303 | /* Now copy the unmatched portion of the input string */ | |
495aee44 | 8304 | if (str && *str) |
d166f048 JA |
8305 | { |
8306 | RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64); | |
8307 | strcpy (ret + rptr, str); | |
8308 | } | |
ccc6cda3 JA |
8309 | else |
8310 | ret[rptr] = '\0'; | |
8311 | ||
8312 | return ret; | |
8313 | } | |
8314 | ||
8315 | /* Do pattern match and replacement on the positional parameters. */ | |
8316 | static char * | |
8317 | pos_params_pat_subst (string, pat, rep, mflags) | |
8318 | char *string, *pat, *rep; | |
8319 | int mflags; | |
8320 | { | |
8321 | WORD_LIST *save, *params; | |
8322 | WORD_DESC *w; | |
0628567a | 8323 | char *ret; |
712f80b0 | 8324 | int pchar, qflags, pflags; |
ccc6cda3 JA |
8325 | |
8326 | save = params = list_rest_of_args (); | |
8327 | if (save == 0) | |
8328 | return ((char *)NULL); | |
8329 | ||
8330 | for ( ; params; params = params->next) | |
8331 | { | |
8332 | ret = pat_subst (params->word->word, pat, rep, mflags); | |
95732b49 JA |
8333 | w = alloc_word_desc (); |
8334 | w->word = ret ? ret : savestring (""); | |
ccc6cda3 JA |
8335 | dispose_word (params->word); |
8336 | params->word = w; | |
ccc6cda3 JA |
8337 | } |
8338 | ||
3185942a JA |
8339 | pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@'; |
8340 | qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0; | |
712f80b0 | 8341 | pflags = (mflags & MATCH_ASSIGNRHS) == MATCH_ASSIGNRHS ? PF_ASSIGNRHS : 0; |
3185942a | 8342 | |
d233b485 CR |
8343 | /* If we are expanding in a context where word splitting will not be |
8344 | performed, treat as quoted. This changes how $* will be expanded. */ | |
8345 | if (pchar == '*' && (mflags & MATCH_ASSIGNRHS) && expand_no_split_dollar_star && ifs_is_null) | |
8346 | qflags |= Q_DOUBLE_QUOTES; /* Posix interp 888 */ | |
3185942a | 8347 | |
712f80b0 | 8348 | ret = string_list_pos_params (pchar, save, qflags, pflags); |
ccc6cda3 JA |
8349 | dispose_words (save); |
8350 | ||
8351 | return (ret); | |
8352 | } | |
8353 | ||
cce855bc JA |
8354 | /* Perform pattern substitution on VALUE, which is the expansion of |
8355 | VARNAME. PATSUB is an expression supplying the pattern to match | |
8356 | and the string to substitute. QUOTED is a flags word containing | |
8357 | the type of quoting currently in effect. */ | |
ccc6cda3 | 8358 | static char * |
a0c0a00f | 8359 | parameter_brace_patsub (varname, value, ind, patsub, quoted, pflags, flags) |
495aee44 CR |
8360 | char *varname, *value; |
8361 | int ind; | |
8362 | char *patsub; | |
a0c0a00f | 8363 | int quoted, pflags, flags; |
ccc6cda3 | 8364 | { |
3185942a | 8365 | int vtype, mflags, starsub, delim; |
d233b485 | 8366 | char *val, *temp, *pat, *rep, *p, *lpatsub, *tt, *oname; |
ccc6cda3 JA |
8367 | SHELL_VAR *v; |
8368 | ||
8369 | if (value == 0) | |
8370 | return ((char *)NULL); | |
8371 | ||
d233b485 CR |
8372 | oname = this_command_name; |
8373 | this_command_name = varname; /* error messages */ | |
ccc6cda3 | 8374 | |
495aee44 | 8375 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
ccc6cda3 | 8376 | if (vtype == -1) |
d233b485 CR |
8377 | { |
8378 | this_command_name = oname; | |
8379 | return ((char *)NULL); | |
8380 | } | |
ccc6cda3 | 8381 | |
b80f6443 JA |
8382 | starsub = vtype & VT_STARSUB; |
8383 | vtype &= ~VT_STARSUB; | |
8384 | ||
ccc6cda3 | 8385 | mflags = 0; |
ac50fbac CR |
8386 | /* PATSUB is never NULL when this is called. */ |
8387 | if (*patsub == '/') | |
f1be666c JA |
8388 | { |
8389 | mflags |= MATCH_GLOBREP; | |
8390 | patsub++; | |
8391 | } | |
7117c2d2 JA |
8392 | |
8393 | /* Malloc this because expand_string_if_necessary or one of the expansion | |
8394 | functions in its call chain may free it on a substitution error. */ | |
bb70624e | 8395 | lpatsub = savestring (patsub); |
ccc6cda3 JA |
8396 | |
8397 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
8398 | mflags |= MATCH_QUOTED; | |
8399 | ||
b80f6443 JA |
8400 | if (starsub) |
8401 | mflags |= MATCH_STARSUB; | |
8402 | ||
a0c0a00f CR |
8403 | if (pflags & PF_ASSIGNRHS) |
8404 | mflags |= MATCH_ASSIGNRHS; | |
8405 | ||
0628567a JA |
8406 | /* If the pattern starts with a `/', make sure we skip over it when looking |
8407 | for the replacement delimiter. */ | |
3185942a JA |
8408 | delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0); |
8409 | if (lpatsub[delim] == '/') | |
8410 | { | |
8411 | lpatsub[delim] = 0; | |
8412 | rep = lpatsub + delim + 1; | |
8413 | } | |
8414 | else | |
8415 | rep = (char *)NULL; | |
ccc6cda3 JA |
8416 | |
8417 | if (rep && *rep == '\0') | |
8418 | rep = (char *)NULL; | |
8419 | ||
b80f6443 JA |
8420 | /* Perform the same expansions on the pattern as performed by the |
8421 | pattern removal expansions. */ | |
8422 | pat = getpattern (lpatsub, quoted, 1); | |
bb70624e | 8423 | |
ccc6cda3 | 8424 | if (rep) |
d166f048 | 8425 | { |
ac50fbac CR |
8426 | /* We want to perform quote removal on the expanded replacement even if |
8427 | the entire expansion is double-quoted because the parser and string | |
8428 | extraction functions treated quotes in the replacement string as | |
8429 | special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */ | |
8430 | if (shell_compatibility_level > 42) | |
8431 | rep = expand_string_if_necessary (rep, quoted & ~(Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT), expand_string_unsplit); | |
8432 | /* This is the bash-4.2 code. */ | |
8433 | else if ((mflags & MATCH_QUOTED) == 0) | |
f73dda09 | 8434 | rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit); |
d166f048 | 8435 | else |
f73dda09 | 8436 | rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit); |
d166f048 | 8437 | } |
ccc6cda3 | 8438 | |
0628567a | 8439 | /* ksh93 doesn't allow the match specifier to be a part of the expanded |
f1be666c JA |
8440 | pattern. This is an extension. Make sure we don't anchor the pattern |
8441 | at the beginning or end of the string if we're doing global replacement, | |
8442 | though. */ | |
ccc6cda3 | 8443 | p = pat; |
f1be666c JA |
8444 | if (mflags & MATCH_GLOBREP) |
8445 | mflags |= MATCH_ANY; | |
0628567a | 8446 | else if (pat && pat[0] == '#') |
ccc6cda3 JA |
8447 | { |
8448 | mflags |= MATCH_BEG; | |
8449 | p++; | |
8450 | } | |
d166f048 | 8451 | else if (pat && pat[0] == '%') |
ccc6cda3 JA |
8452 | { |
8453 | mflags |= MATCH_END; | |
8454 | p++; | |
8455 | } | |
8456 | else | |
8457 | mflags |= MATCH_ANY; | |
8458 | ||
cce855bc JA |
8459 | /* OK, we now want to substitute REP for PAT in VAL. If |
8460 | flags & MATCH_GLOBREP is non-zero, the substitution is done | |
8461 | everywhere, otherwise only the first occurrence of PAT is | |
7117c2d2 JA |
8462 | replaced. The pattern matching code doesn't understand |
8463 | CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable | |
8464 | values passed in (VT_VARIABLE) so the pattern substitution | |
8465 | code works right. We need to requote special chars after | |
8466 | we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the | |
8467 | other cases if QUOTED == 0, since the posparams and arrays | |
8468 | indexed by * or @ do special things when QUOTED != 0. */ | |
8469 | ||
ccc6cda3 JA |
8470 | switch (vtype) |
8471 | { | |
8472 | case VT_VARIABLE: | |
d166f048 | 8473 | case VT_ARRAYMEMBER: |
ccc6cda3 | 8474 | temp = pat_subst (val, p, rep, mflags); |
7117c2d2 JA |
8475 | if (vtype == VT_VARIABLE) |
8476 | FREE (val); | |
8477 | if (temp) | |
8478 | { | |
3185942a | 8479 | tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp); |
7117c2d2 JA |
8480 | free (temp); |
8481 | temp = tt; | |
8482 | } | |
ccc6cda3 JA |
8483 | break; |
8484 | case VT_POSPARMS: | |
d233b485 CR |
8485 | /* This does the right thing for the case where we are not performing |
8486 | word splitting. MATCH_STARSUB restricts it to ${* /foo/bar}, and | |
8487 | pos_params_pat_subst/string_list_pos_params will do the right thing | |
8488 | in turn for the case where ifs_is_null. Posix interp 888 */ | |
8489 | if ((pflags & PF_NOSPLIT2) && (mflags & MATCH_STARSUB)) | |
8490 | mflags |= MATCH_ASSIGNRHS; | |
ccc6cda3 | 8491 | temp = pos_params_pat_subst (val, p, rep, mflags); |
d233b485 CR |
8492 | if (temp && quoted == 0 && ifs_is_null) |
8493 | { | |
8494 | /* Posix interp 888 */ | |
8495 | } | |
712f80b0 CR |
8496 | else if (temp && quoted == 0 && (pflags & PF_ASSIGNRHS)) |
8497 | { | |
8498 | /* Posix interp 888 */ | |
8499 | } | |
d233b485 | 8500 | else if (temp && (mflags & MATCH_QUOTED) == 0) |
7117c2d2 JA |
8501 | { |
8502 | tt = quote_escapes (temp); | |
8503 | free (temp); | |
8504 | temp = tt; | |
8505 | } | |
ccc6cda3 JA |
8506 | break; |
8507 | #if defined (ARRAY_VARS) | |
8508 | case VT_ARRAYVAR: | |
d233b485 CR |
8509 | /* If we are expanding in a context where word splitting will not be |
8510 | performed, treat as quoted. This changes how ${A[*]} will be | |
8511 | expanded to make it identical to $*. */ | |
8512 | if ((mflags & MATCH_STARSUB) && (mflags & MATCH_ASSIGNRHS) && ifs_is_null) | |
8513 | mflags |= MATCH_QUOTED; /* Posix interp 888 */ | |
8514 | ||
8515 | /* these eventually call string_list_pos_params */ | |
8516 | if (assoc_p (v)) | |
8517 | temp = assoc_patsub (assoc_cell (v), p, rep, mflags); | |
8518 | else | |
8519 | temp = array_patsub (array_cell (v), p, rep, mflags); | |
8520 | ||
8521 | if (temp && quoted == 0 && ifs_is_null) | |
8522 | { | |
8523 | /* Posix interp 888 */ | |
8524 | } | |
8525 | else if (temp && (mflags & MATCH_QUOTED) == 0) | |
8526 | { | |
8527 | tt = quote_escapes (temp); | |
8528 | free (temp); | |
8529 | temp = tt; | |
8530 | } | |
3185942a JA |
8531 | break; |
8532 | #endif | |
8533 | } | |
8534 | ||
8535 | FREE (pat); | |
8536 | FREE (rep); | |
8537 | free (lpatsub); | |
8538 | ||
d233b485 CR |
8539 | this_command_name = oname; |
8540 | ||
3185942a JA |
8541 | return temp; |
8542 | } | |
8543 | ||
8544 | /****************************************************************/ | |
8545 | /* */ | |
8546 | /* Functions to perform case modification on variable values */ | |
8547 | /* */ | |
8548 | /****************************************************************/ | |
8549 | ||
8550 | /* Do case modification on the positional parameters. */ | |
8551 | ||
8552 | static char * | |
8553 | pos_params_modcase (string, pat, modop, mflags) | |
8554 | char *string, *pat; | |
8555 | int modop; | |
8556 | int mflags; | |
8557 | { | |
8558 | WORD_LIST *save, *params; | |
8559 | WORD_DESC *w; | |
8560 | char *ret; | |
712f80b0 | 8561 | int pchar, qflags, pflags; |
3185942a JA |
8562 | |
8563 | save = params = list_rest_of_args (); | |
8564 | if (save == 0) | |
8565 | return ((char *)NULL); | |
8566 | ||
8567 | for ( ; params; params = params->next) | |
8568 | { | |
8569 | ret = sh_modcase (params->word->word, pat, modop); | |
8570 | w = alloc_word_desc (); | |
8571 | w->word = ret ? ret : savestring (""); | |
8572 | dispose_word (params->word); | |
8573 | params->word = w; | |
8574 | } | |
8575 | ||
8576 | pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@'; | |
8577 | qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0; | |
712f80b0 | 8578 | pflags = (mflags & MATCH_ASSIGNRHS) == MATCH_ASSIGNRHS ? PF_ASSIGNRHS : 0; |
3185942a | 8579 | |
d233b485 CR |
8580 | /* If we are expanding in a context where word splitting will not be |
8581 | performed, treat as quoted. This changes how $* will be expanded. */ | |
8582 | if (pchar == '*' && (mflags & MATCH_ASSIGNRHS) && ifs_is_null) | |
8583 | qflags |= Q_DOUBLE_QUOTES; /* Posix interp 888 */ | |
8584 | ||
712f80b0 | 8585 | ret = string_list_pos_params (pchar, save, qflags, pflags); |
3185942a JA |
8586 | dispose_words (save); |
8587 | ||
8588 | return (ret); | |
8589 | } | |
8590 | ||
8591 | /* Perform case modification on VALUE, which is the expansion of | |
8592 | VARNAME. MODSPEC is an expression supplying the type of modification | |
8593 | to perform. QUOTED is a flags word containing the type of quoting | |
8594 | currently in effect. */ | |
8595 | static char * | |
d233b485 | 8596 | parameter_brace_casemod (varname, value, ind, modspec, patspec, quoted, pflags, flags) |
3185942a | 8597 | char *varname, *value; |
495aee44 | 8598 | int ind, modspec; |
3185942a | 8599 | char *patspec; |
d233b485 | 8600 | int quoted, pflags, flags; |
3185942a JA |
8601 | { |
8602 | int vtype, starsub, modop, mflags, x; | |
d233b485 | 8603 | char *val, *temp, *pat, *p, *lpat, *tt, *oname; |
3185942a JA |
8604 | SHELL_VAR *v; |
8605 | ||
8606 | if (value == 0) | |
8607 | return ((char *)NULL); | |
8608 | ||
d233b485 | 8609 | oname = this_command_name; |
3185942a JA |
8610 | this_command_name = varname; |
8611 | ||
495aee44 | 8612 | vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val); |
3185942a | 8613 | if (vtype == -1) |
d233b485 CR |
8614 | { |
8615 | this_command_name = oname; | |
8616 | return ((char *)NULL); | |
8617 | } | |
3185942a JA |
8618 | |
8619 | starsub = vtype & VT_STARSUB; | |
8620 | vtype &= ~VT_STARSUB; | |
8621 | ||
8622 | modop = 0; | |
8623 | mflags = 0; | |
8624 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
8625 | mflags |= MATCH_QUOTED; | |
8626 | if (starsub) | |
8627 | mflags |= MATCH_STARSUB; | |
d233b485 CR |
8628 | if (pflags & PF_ASSIGNRHS) |
8629 | mflags |= MATCH_ASSIGNRHS; | |
3185942a JA |
8630 | |
8631 | p = patspec; | |
8632 | if (modspec == '^') | |
8633 | { | |
8634 | x = p && p[0] == modspec; | |
17345e5a | 8635 | modop = x ? CASE_UPPER : CASE_UPFIRST; |
3185942a JA |
8636 | p += x; |
8637 | } | |
8638 | else if (modspec == ',') | |
8639 | { | |
8640 | x = p && p[0] == modspec; | |
17345e5a | 8641 | modop = x ? CASE_LOWER : CASE_LOWFIRST; |
3185942a JA |
8642 | p += x; |
8643 | } | |
8644 | else if (modspec == '~') | |
8645 | { | |
8646 | x = p && p[0] == modspec; | |
8647 | modop = x ? CASE_TOGGLEALL : CASE_TOGGLE; | |
8648 | p += x; | |
8649 | } | |
8650 | ||
8651 | lpat = p ? savestring (p) : 0; | |
8652 | /* Perform the same expansions on the pattern as performed by the | |
d233b485 | 8653 | pattern removal expansions. */ |
3185942a JA |
8654 | pat = lpat ? getpattern (lpat, quoted, 1) : 0; |
8655 | ||
8656 | /* OK, now we do the case modification. */ | |
8657 | switch (vtype) | |
8658 | { | |
8659 | case VT_VARIABLE: | |
8660 | case VT_ARRAYMEMBER: | |
8661 | temp = sh_modcase (val, pat, modop); | |
8662 | if (vtype == VT_VARIABLE) | |
8663 | FREE (val); | |
8664 | if (temp) | |
8665 | { | |
8666 | tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp); | |
8667 | free (temp); | |
8668 | temp = tt; | |
8669 | } | |
8670 | break; | |
8671 | ||
8672 | case VT_POSPARMS: | |
8673 | temp = pos_params_modcase (val, pat, modop, mflags); | |
d233b485 CR |
8674 | if (temp && quoted == 0 && ifs_is_null) |
8675 | { | |
8676 | /* Posix interp 888 */ | |
8677 | } | |
8678 | else if (temp && (mflags & MATCH_QUOTED) == 0) | |
7117c2d2 JA |
8679 | { |
8680 | tt = quote_escapes (temp); | |
8681 | free (temp); | |
8682 | temp = tt; | |
8683 | } | |
3185942a JA |
8684 | break; |
8685 | ||
8686 | #if defined (ARRAY_VARS) | |
8687 | case VT_ARRAYVAR: | |
d233b485 CR |
8688 | /* If we are expanding in a context where word splitting will not be |
8689 | performed, treat as quoted. This changes how ${A[*]} will be | |
8690 | expanded to make it identical to $*. */ | |
8691 | if ((mflags & MATCH_STARSUB) && (mflags & MATCH_ASSIGNRHS) && ifs_is_null) | |
8692 | mflags |= MATCH_QUOTED; /* Posix interp 888 */ | |
8693 | ||
3185942a JA |
8694 | temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags) |
8695 | : array_modcase (array_cell (v), pat, modop, mflags); | |
d233b485 CR |
8696 | |
8697 | if (temp && quoted == 0 && ifs_is_null) | |
8698 | { | |
8699 | /* Posix interp 888 */ | |
8700 | } | |
8701 | else if (temp && (mflags & MATCH_QUOTED) == 0) | |
8702 | { | |
8703 | tt = quote_escapes (temp); | |
8704 | free (temp); | |
8705 | temp = tt; | |
8706 | } | |
8707 | ||
ccc6cda3 JA |
8708 | break; |
8709 | #endif | |
8710 | } | |
8711 | ||
8712 | FREE (pat); | |
3185942a | 8713 | free (lpat); |
ccc6cda3 | 8714 | |
d233b485 CR |
8715 | this_command_name = oname; |
8716 | ||
ccc6cda3 JA |
8717 | return temp; |
8718 | } | |
8719 | ||
0628567a JA |
8720 | /* Check for unbalanced parens in S, which is the contents of $(( ... )). If |
8721 | any occur, this must be a nested command substitution, so return 0. | |
8722 | Otherwise, return 1. A valid arithmetic expression must always have a | |
8723 | ( before a matching ), so any cases where there are more right parens | |
8724 | means that this must not be an arithmetic expression, though the parser | |
8725 | will not accept it without a balanced total number of parens. */ | |
8726 | static int | |
8727 | chk_arithsub (s, len) | |
8728 | const char *s; | |
8729 | int len; | |
8730 | { | |
8731 | int i, count; | |
8732 | DECLARE_MBSTATE; | |
8733 | ||
8734 | i = count = 0; | |
8735 | while (i < len) | |
8736 | { | |
0001803f | 8737 | if (s[i] == LPAREN) |
0628567a | 8738 | count++; |
0001803f | 8739 | else if (s[i] == RPAREN) |
0628567a JA |
8740 | { |
8741 | count--; | |
8742 | if (count < 0) | |
8743 | return 0; | |
8744 | } | |
8745 | ||
8746 | switch (s[i]) | |
8747 | { | |
8748 | default: | |
8749 | ADVANCE_CHAR (s, len, i); | |
8750 | break; | |
8751 | ||
8752 | case '\\': | |
8753 | i++; | |
8754 | if (s[i]) | |
8755 | ADVANCE_CHAR (s, len, i); | |
8756 | break; | |
8757 | ||
8758 | case '\'': | |
a0c0a00f | 8759 | i = skip_single_quoted (s, len, ++i, 0); |
0628567a JA |
8760 | break; |
8761 | ||
8762 | case '"': | |
a0c0a00f | 8763 | i = skip_double_quoted ((char *)s, len, ++i, 0); |
0628567a JA |
8764 | break; |
8765 | } | |
8766 | } | |
8767 | ||
8768 | return (count == 0); | |
8769 | } | |
8770 | ||
cce855bc JA |
8771 | /****************************************************************/ |
8772 | /* */ | |
8773 | /* Functions to perform parameter expansion on a string */ | |
8774 | /* */ | |
8775 | /****************************************************************/ | |
8776 | ||
3185942a | 8777 | /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */ |
95732b49 | 8778 | static WORD_DESC * |
0001803f | 8779 | parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at) |
ccc6cda3 | 8780 | char *string; |
a0c0a00f | 8781 | int *indexp, quoted, pflags, *quoted_dollar_atp, *contains_dollar_at; |
ccc6cda3 JA |
8782 | { |
8783 | int check_nullness, var_is_set, var_is_null, var_is_special; | |
3eb0018e | 8784 | int want_substring, want_indir, want_patsub, want_casemod, want_attributes; |
ccc6cda3 | 8785 | char *name, *value, *temp, *temp1; |
95732b49 | 8786 | WORD_DESC *tdesc, *ret; |
3eb0018e | 8787 | int t_index, sindex, c, tflag, modspec, local_pflags, all_element_arrayref; |
7117c2d2 | 8788 | intmax_t number; |
495aee44 | 8789 | arrayind_t ind; |
ccc6cda3 | 8790 | |
3185942a | 8791 | temp = temp1 = value = (char *)NULL; |
ccc6cda3 | 8792 | var_is_set = var_is_null = var_is_special = check_nullness = 0; |
3eb0018e | 8793 | want_substring = want_indir = want_patsub = want_casemod = want_attributes = 0; |
ccc6cda3 | 8794 | |
3eb0018e | 8795 | local_pflags = 0; |
a0c0a00f CR |
8796 | all_element_arrayref = 0; |
8797 | ||
cce855bc JA |
8798 | sindex = *indexp; |
8799 | t_index = ++sindex; | |
0628567a JA |
8800 | /* ${#var} doesn't have any of the other parameter expansions on it. */ |
8801 | if (string[t_index] == '#' && legal_variable_starter (string[t_index+1])) /* {{ */ | |
3185942a | 8802 | name = string_extract (string, &t_index, "}", SX_VARNAME); |
0628567a | 8803 | else |
3185942a JA |
8804 | #if defined (CASEMOD_EXPANSIONS) |
8805 | /* To enable case-toggling expansions using the `~' operator character | |
3eb0018e | 8806 | define CASEMOD_CAPCASE in config-top.h */ |
3185942a | 8807 | # if defined (CASEMOD_CAPCASE) |
a0c0a00f | 8808 | name = string_extract (string, &t_index, "#%^,~:-=?+/@}", SX_VARNAME); |
3185942a | 8809 | # else |
a0c0a00f | 8810 | name = string_extract (string, &t_index, "#%^,:-=?+/@}", SX_VARNAME); |
3185942a JA |
8811 | # endif /* CASEMOD_CAPCASE */ |
8812 | #else | |
a0c0a00f | 8813 | name = string_extract (string, &t_index, "#%:-=?+/@}", SX_VARNAME); |
3185942a | 8814 | #endif /* CASEMOD_EXPANSIONS */ |
cce855bc | 8815 | |
a0c0a00f CR |
8816 | /* Handle ${@[stuff]} now that @ is a word expansion operator. Not exactly |
8817 | the cleanest code ever. */ | |
8818 | if (*name == 0 && sindex == t_index && string[sindex] == '@') | |
8819 | { | |
8820 | name = (char *)xrealloc (name, 2); | |
8821 | name[0] = '@'; | |
8822 | name[1] = '\0'; | |
8823 | t_index++; | |
8824 | } | |
d233b485 | 8825 | else if (*name == '!' && t_index > sindex && string[t_index] == '@' && string[t_index+1] == RBRACE) |
a0c0a00f CR |
8826 | { |
8827 | name = (char *)xrealloc (name, t_index - sindex + 2); | |
8828 | name[t_index - sindex] = '@'; | |
8829 | name[t_index - sindex + 1] = '\0'; | |
8830 | t_index++; | |
8831 | } | |
8832 | ||
95732b49 JA |
8833 | ret = 0; |
8834 | tflag = 0; | |
8835 | ||
495aee44 CR |
8836 | ind = INTMAX_MIN; |
8837 | ||
cce855bc JA |
8838 | /* If the name really consists of a special variable, then make sure |
8839 | that we have the entire name. We don't allow indirect references | |
a0c0a00f CR |
8840 | to special variables except `#', `?', `@' and `*'. This clause is |
8841 | designed to handle ${#SPECIAL} and ${!SPECIAL}, not anything more | |
8842 | general. */ | |
495aee44 | 8843 | if ((sindex == t_index && VALID_SPECIAL_LENGTH_PARAM (string[t_index])) || |
a0c0a00f | 8844 | (sindex == t_index && string[sindex] == '#' && VALID_SPECIAL_LENGTH_PARAM (string[sindex + 1])) || |
495aee44 | 8845 | (sindex == t_index - 1 && string[sindex] == '!' && VALID_INDIR_PARAM (string[t_index]))) |
ccc6cda3 JA |
8846 | { |
8847 | t_index++; | |
a0c0a00f | 8848 | temp1 = string_extract (string, &t_index, "#%:-=?+/@}", 0); |
ac50fbac | 8849 | name = (char *)xrealloc (name, 3 + (strlen (temp1))); |
ccc6cda3 JA |
8850 | *name = string[sindex]; |
8851 | if (string[sindex] == '!') | |
8852 | { | |
28ef6c31 JA |
8853 | /* indirect reference of $#, $?, $@, or $* */ |
8854 | name[1] = string[sindex + 1]; | |
8855 | strcpy (name + 2, temp1); | |
ccc6cda3 | 8856 | } |
cce855bc | 8857 | else |
ccc6cda3 JA |
8858 | strcpy (name + 1, temp1); |
8859 | free (temp1); | |
8860 | } | |
8861 | sindex = t_index; | |
8862 | ||
8863 | /* Find out what character ended the variable name. Then | |
8864 | do the appropriate thing. */ | |
8865 | if (c = string[sindex]) | |
8866 | sindex++; | |
8867 | ||
8868 | /* If c is followed by one of the valid parameter expansion | |
8869 | characters, move past it as normal. If not, assume that | |
8870 | a substring specification is being given, and do not move | |
8871 | past it. */ | |
28ef6c31 | 8872 | if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex])) |
ccc6cda3 JA |
8873 | { |
8874 | check_nullness++; | |
8875 | if (c = string[sindex]) | |
8876 | sindex++; | |
8877 | } | |
cce855bc | 8878 | else if (c == ':' && string[sindex] != RBRACE) |
ccc6cda3 | 8879 | want_substring = 1; |
ac50fbac | 8880 | else if (c == '/' /* && string[sindex] != RBRACE */) /* XXX */ |
ccc6cda3 | 8881 | want_patsub = 1; |
3185942a JA |
8882 | #if defined (CASEMOD_EXPANSIONS) |
8883 | else if (c == '^' || c == ',' || c == '~') | |
8884 | { | |
8885 | modspec = c; | |
8886 | want_casemod = 1; | |
8887 | } | |
8888 | #endif | |
3eb0018e CR |
8889 | else if (c == '@' && (string[sindex] == 'a' || string[sindex] == 'A') && string[sindex+1] == RBRACE) |
8890 | { | |
8891 | /* special case because we do not want to shortcut foo as foo[0] here */ | |
8892 | want_attributes = 1; | |
8893 | local_pflags |= PF_ALLINDS; | |
8894 | } | |
ccc6cda3 | 8895 | |
cce855bc JA |
8896 | /* Catch the valid and invalid brace expressions that made it through the |
8897 | tests above. */ | |
8898 | /* ${#-} is a valid expansion and means to take the length of $-. | |
8899 | Similarly for ${#?} and ${##}... */ | |
8900 | if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 && | |
28ef6c31 | 8901 | VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE) |
cce855bc | 8902 | { |
f73dda09 | 8903 | name = (char *)xrealloc (name, 3); |
cce855bc JA |
8904 | name[1] = c; |
8905 | name[2] = '\0'; | |
8906 | c = string[sindex++]; | |
8907 | } | |
8908 | ||
8909 | /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */ | |
8910 | if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 && | |
8911 | member (c, "%:=+/") && string[sindex] == RBRACE) | |
8912 | { | |
8913 | temp = (char *)NULL; | |
a0c0a00f | 8914 | goto bad_substitution; /* XXX - substitution error */ |
cce855bc JA |
8915 | } |
8916 | ||
8917 | /* Indirect expansion begins with a `!'. A valid indirect expansion is | |
8918 | either a variable name, one of the positional parameters or a special | |
8919 | variable that expands to one of the positional parameters. */ | |
8920 | want_indir = *name == '!' && | |
f73dda09 | 8921 | (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) |
7117c2d2 | 8922 | || VALID_INDIR_PARAM (name[1])); |
ccc6cda3 | 8923 | |
d233b485 | 8924 | /* Determine the value of this variable whose name is NAME. */ |
ccc6cda3 | 8925 | |
cce855bc | 8926 | /* Check for special variables, directly referenced. */ |
bb70624e | 8927 | if (SPECIAL_VAR (name, want_indir)) |
ccc6cda3 JA |
8928 | var_is_special++; |
8929 | ||
cce855bc JA |
8930 | /* Check for special expansion things, like the length of a parameter */ |
8931 | if (*name == '#' && name[1]) | |
ccc6cda3 | 8932 | { |
cce855bc | 8933 | /* If we are not pointing at the character just after the |
28ef6c31 JA |
8934 | closing brace, then we haven't gotten all of the name. |
8935 | Since it begins with a special character, this is a bad | |
8936 | substitution. Also check NAME for validity before trying | |
8937 | to go on. */ | |
cce855bc | 8938 | if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0)) |
ccc6cda3 JA |
8939 | { |
8940 | temp = (char *)NULL; | |
a0c0a00f | 8941 | goto bad_substitution; /* substitution error */ |
ccc6cda3 JA |
8942 | } |
8943 | ||
8944 | number = parameter_brace_expand_length (name); | |
495aee44 CR |
8945 | if (number == INTMAX_MIN && unbound_vars_is_error) |
8946 | { | |
712f80b0 | 8947 | set_exit_status (EXECUTION_FAILURE); |
495aee44 CR |
8948 | err_unboundvar (name+1); |
8949 | free (name); | |
8950 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); | |
8951 | } | |
ccc6cda3 JA |
8952 | free (name); |
8953 | ||
8954 | *indexp = sindex; | |
95732b49 JA |
8955 | if (number < 0) |
8956 | return (&expand_wdesc_error); | |
8957 | else | |
8958 | { | |
8959 | ret = alloc_word_desc (); | |
8960 | ret->word = itos (number); | |
8961 | return ret; | |
8962 | } | |
ccc6cda3 JA |
8963 | } |
8964 | ||
8965 | /* ${@} is identical to $@. */ | |
8966 | if (name[0] == '@' && name[1] == '\0') | |
8967 | { | |
8968 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
8969 | *quoted_dollar_atp = 1; | |
8970 | ||
8971 | if (contains_dollar_at) | |
8972 | *contains_dollar_at = 1; | |
ac50fbac CR |
8973 | |
8974 | tflag |= W_DOLLARAT; | |
ccc6cda3 JA |
8975 | } |
8976 | ||
b80f6443 | 8977 | /* Process ${!PREFIX*} expansion. */ |
bb70624e JA |
8978 | if (want_indir && string[sindex - 1] == RBRACE && |
8979 | (string[sindex - 2] == '*' || string[sindex - 2] == '@') && | |
f73dda09 | 8980 | legal_variable_starter ((unsigned char) name[1])) |
bb70624e JA |
8981 | { |
8982 | char **x; | |
8983 | WORD_LIST *xlist; | |
8984 | ||
8985 | temp1 = savestring (name + 1); | |
8986 | number = strlen (temp1); | |
8987 | temp1[number - 1] = '\0'; | |
8988 | x = all_variables_matching_prefix (temp1); | |
7117c2d2 | 8989 | xlist = strvec_to_word_list (x, 0, 0); |
28ef6c31 | 8990 | if (string[sindex - 2] == '*') |
d233b485 | 8991 | temp = string_list_dollar_star (xlist, quoted, 0); |
28ef6c31 JA |
8992 | else |
8993 | { | |
a0c0a00f | 8994 | temp = string_list_dollar_at (xlist, quoted, 0); |
28ef6c31 JA |
8995 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) |
8996 | *quoted_dollar_atp = 1; | |
8997 | if (contains_dollar_at) | |
8998 | *contains_dollar_at = 1; | |
ac50fbac CR |
8999 | |
9000 | tflag |= W_DOLLARAT; | |
28ef6c31 | 9001 | } |
bb70624e | 9002 | free (x); |
89a92869 | 9003 | dispose_words (xlist); |
bb70624e JA |
9004 | free (temp1); |
9005 | *indexp = sindex; | |
95732b49 | 9006 | |
ac50fbac CR |
9007 | free (name); |
9008 | ||
95732b49 JA |
9009 | ret = alloc_word_desc (); |
9010 | ret->word = temp; | |
ac50fbac | 9011 | ret->flags = tflag; /* XXX */ |
95732b49 | 9012 | return ret; |
bb70624e | 9013 | } |
b80f6443 JA |
9014 | |
9015 | #if defined (ARRAY_VARS) | |
d233b485 | 9016 | /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ |
b80f6443 | 9017 | if (want_indir && string[sindex - 1] == RBRACE && |
d233b485 | 9018 | string[sindex - 2] == RBRACK && valid_array_reference (name+1, 0)) |
b80f6443 JA |
9019 | { |
9020 | char *x, *x1; | |
9021 | ||
9022 | temp1 = savestring (name + 1); | |
d233b485 | 9023 | x = array_variable_name (temp1, 0, &x1, (int *)0); |
b80f6443 | 9024 | FREE (x); |
d233b485 | 9025 | if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == RBRACK) |
b80f6443 | 9026 | { |
712f80b0 | 9027 | temp = array_keys (temp1, quoted, pflags); /* handles assoc vars too */ |
b80f6443 JA |
9028 | if (x1[0] == '@') |
9029 | { | |
9030 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
9031 | *quoted_dollar_atp = 1; | |
9032 | if (contains_dollar_at) | |
9033 | *contains_dollar_at = 1; | |
ac50fbac CR |
9034 | |
9035 | tflag |= W_DOLLARAT; | |
b80f6443 JA |
9036 | } |
9037 | ||
a0c0a00f | 9038 | free (name); |
b80f6443 JA |
9039 | free (temp1); |
9040 | *indexp = sindex; | |
95732b49 JA |
9041 | |
9042 | ret = alloc_word_desc (); | |
9043 | ret->word = temp; | |
ac50fbac | 9044 | ret->flags = tflag; /* XXX */ |
95732b49 | 9045 | return ret; |
b80f6443 JA |
9046 | } |
9047 | ||
9048 | free (temp1); | |
9049 | } | |
9050 | #endif /* ARRAY_VARS */ | |
bb70624e | 9051 | |
ccc6cda3 JA |
9052 | /* Make sure that NAME is valid before trying to go on. */ |
9053 | if (valid_brace_expansion_word (want_indir ? name + 1 : name, | |
9054 | var_is_special) == 0) | |
9055 | { | |
9056 | temp = (char *)NULL; | |
a0c0a00f | 9057 | goto bad_substitution; /* substitution error */ |
ccc6cda3 JA |
9058 | } |
9059 | ||
9060 | if (want_indir) | |
1a1f8b54 | 9061 | { |
3eb0018e | 9062 | tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, pflags|local_pflags, quoted_dollar_atp, contains_dollar_at); |
a0c0a00f CR |
9063 | if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal) |
9064 | { | |
9065 | temp = (char *)NULL; | |
9066 | goto bad_substitution; | |
9067 | } | |
d233b485 | 9068 | |
1a1f8b54 CR |
9069 | /* Turn off the W_ARRAYIND flag because there is no way for this function |
9070 | to return the index we're supposed to be using. */ | |
9071 | if (tdesc && tdesc->flags) | |
9072 | tdesc->flags &= ~W_ARRAYIND; | |
9073 | } | |
95732b49 | 9074 | else |
3eb0018e CR |
9075 | { |
9076 | local_pflags |= PF_IGNUNBOUND|(pflags&(PF_NOSPLIT2|PF_ASSIGNRHS)); | |
9077 | tdesc = parameter_brace_expand_word (name, var_is_special, quoted, local_pflags, &ind); | |
9078 | } | |
95732b49 | 9079 | |
d233b485 CR |
9080 | if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal) |
9081 | { | |
9082 | tflag = 0; | |
9083 | tdesc = 0; | |
9084 | } | |
9085 | ||
95732b49 JA |
9086 | if (tdesc) |
9087 | { | |
9088 | temp = tdesc->word; | |
9089 | tflag = tdesc->flags; | |
9090 | dispose_word_desc (tdesc); | |
9091 | } | |
ccc6cda3 | 9092 | else |
95732b49 | 9093 | temp = (char *)0; |
ccc6cda3 | 9094 | |
ac50fbac CR |
9095 | if (temp == &expand_param_error || temp == &expand_param_fatal) |
9096 | { | |
9097 | FREE (name); | |
9098 | FREE (value); | |
9099 | return (temp == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal); | |
9100 | } | |
9101 | ||
ccc6cda3 | 9102 | #if defined (ARRAY_VARS) |
a0c0a00f CR |
9103 | if (valid_array_reference (name, 0)) |
9104 | { | |
9105 | int qflags; | |
9106 | char *t; | |
9107 | ||
9108 | qflags = quoted; | |
9109 | /* If in a context where word splitting will not take place, treat as | |
9110 | if double-quoted. Has effects with $* and ${array[*]} */ | |
d233b485 | 9111 | |
a0c0a00f CR |
9112 | if (pflags & PF_ASSIGNRHS) |
9113 | qflags |= Q_DOUBLE_QUOTES; | |
a0c0a00f | 9114 | /* We duplicate a little code here */ |
d233b485 CR |
9115 | t = mbschr (name, LBRACK); |
9116 | if (t && ALL_ELEMENT_SUB (t[1]) && t[2] == RBRACK) | |
9117 | { | |
9118 | all_element_arrayref = 1; | |
9119 | if (expand_no_split_dollar_star && t[1] == '*') /* XXX */ | |
9120 | qflags |= Q_DOUBLE_QUOTES; | |
9121 | } | |
712f80b0 | 9122 | chk_atstar (name, qflags, pflags, quoted_dollar_atp, contains_dollar_at); |
a0c0a00f | 9123 | } |
ccc6cda3 JA |
9124 | #endif |
9125 | ||
9126 | var_is_set = temp != (char *)0; | |
9127 | var_is_null = check_nullness && (var_is_set == 0 || *temp == 0); | |
ac50fbac CR |
9128 | /* XXX - this may not need to be restricted to special variables */ |
9129 | if (check_nullness) | |
9130 | var_is_null |= var_is_set && var_is_special && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp); | |
d233b485 CR |
9131 | #if defined (ARRAY_VARS) |
9132 | if (check_nullness) | |
9133 | var_is_null |= var_is_set && | |
9134 | (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && | |
9135 | QUOTED_NULL (temp) && | |
9136 | valid_array_reference (name, 0) && | |
712f80b0 | 9137 | chk_atstar (name, 0, 0, (int *)0, (int *)0); |
d233b485 | 9138 | #endif |
ccc6cda3 JA |
9139 | |
9140 | /* Get the rest of the stuff inside the braces. */ | |
cce855bc | 9141 | if (c && c != RBRACE) |
ccc6cda3 JA |
9142 | { |
9143 | /* Extract the contents of the ${ ... } expansion | |
28ef6c31 | 9144 | according to the Posix.2 rules. */ |
49ed961b | 9145 | value = extract_dollar_brace_string (string, &sindex, quoted, (c == '%' || c == '#' || c =='/' || c == '^' || c == ',' || c ==':') ? SX_POSIXEXP|SX_WORD : SX_WORD); |
cce855bc | 9146 | if (string[sindex] == RBRACE) |
28ef6c31 | 9147 | sindex++; |
ccc6cda3 | 9148 | else |
a0c0a00f | 9149 | goto bad_substitution; /* substitution error */ |
ccc6cda3 JA |
9150 | } |
9151 | else | |
9152 | value = (char *)NULL; | |
726f6388 | 9153 | |
ccc6cda3 JA |
9154 | *indexp = sindex; |
9155 | ||
495aee44 CR |
9156 | /* All the cases where an expansion can possibly generate an unbound |
9157 | variable error. */ | |
712f80b0 | 9158 | if (want_substring || want_patsub || want_casemod || c == '@' || c == '#' || c == '%' || c == RBRACE) |
495aee44 | 9159 | { |
a0c0a00f | 9160 | if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1]) && all_element_arrayref == 0) |
495aee44 | 9161 | { |
712f80b0 | 9162 | set_exit_status (EXECUTION_FAILURE); |
495aee44 CR |
9163 | err_unboundvar (name); |
9164 | FREE (value); | |
9165 | FREE (temp); | |
9166 | free (name); | |
9167 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); | |
9168 | } | |
9169 | } | |
9170 | ||
ccc6cda3 JA |
9171 | /* If this is a substring spec, process it and add the result. */ |
9172 | if (want_substring) | |
726f6388 | 9173 | { |
d233b485 | 9174 | temp1 = parameter_brace_substring (name, temp, ind, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
ccc6cda3 JA |
9175 | FREE (value); |
9176 | FREE (temp); | |
95732b49 | 9177 | |
d233b485 CR |
9178 | if (temp1 == &expand_param_error || temp1 == &expand_param_fatal) |
9179 | { | |
9180 | FREE (name); | |
9181 | return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal); | |
9182 | } | |
95732b49 JA |
9183 | |
9184 | ret = alloc_word_desc (); | |
9185 | ret->word = temp1; | |
ac50fbac CR |
9186 | /* We test quoted_dollar_atp because we want variants with double-quoted |
9187 | "$@" to take a different code path. In fact, we make sure at the end | |
9188 | of expand_word_internal that we're only looking at these flags if | |
9189 | quoted_dollar_at == 0. */ | |
712f80b0 | 9190 | if (temp1 && |
ac50fbac CR |
9191 | (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) && |
9192 | QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
0628567a | 9193 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; |
712f80b0 CR |
9194 | else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && |
9195 | (pflags & PF_ASSIGNRHS)) | |
9196 | ret->flags |= W_SPLITSPACE; /* Posix interp 888 */ | |
d233b485 CR |
9197 | /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */ |
9198 | else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null) | |
9199 | ret->flags |= W_SPLITSPACE; /* Posix interp 888 */ | |
9200 | ||
9201 | FREE (name); | |
95732b49 | 9202 | return ret; |
726f6388 | 9203 | } |
ccc6cda3 | 9204 | else if (want_patsub) |
726f6388 | 9205 | { |
a0c0a00f | 9206 | temp1 = parameter_brace_patsub (name, temp, ind, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
ccc6cda3 JA |
9207 | FREE (value); |
9208 | FREE (temp); | |
95732b49 | 9209 | |
d233b485 CR |
9210 | if (temp1 == &expand_param_error || temp1 == &expand_param_fatal) |
9211 | { | |
9212 | FREE (name); | |
9213 | return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal); | |
9214 | } | |
95732b49 JA |
9215 | |
9216 | ret = alloc_word_desc (); | |
9217 | ret->word = temp1; | |
ac50fbac CR |
9218 | if (temp1 && |
9219 | (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) && | |
9220 | QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
3185942a | 9221 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; |
d233b485 CR |
9222 | /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */ |
9223 | else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null) | |
9224 | ret->flags |= W_SPLITSPACE; /* Posix interp 888 */ | |
9225 | ||
9226 | FREE (name); | |
95732b49 | 9227 | return ret; |
ccc6cda3 | 9228 | } |
3185942a JA |
9229 | #if defined (CASEMOD_EXPANSIONS) |
9230 | else if (want_casemod) | |
9231 | { | |
d233b485 | 9232 | temp1 = parameter_brace_casemod (name, temp, ind, modspec, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
3185942a JA |
9233 | FREE (value); |
9234 | FREE (temp); | |
9235 | ||
d233b485 CR |
9236 | if (temp1 == &expand_param_error || temp1 == &expand_param_fatal) |
9237 | { | |
9238 | FREE (name); | |
9239 | return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal); | |
9240 | } | |
3185942a JA |
9241 | |
9242 | ret = alloc_word_desc (); | |
9243 | ret->word = temp1; | |
ac50fbac CR |
9244 | if (temp1 && |
9245 | (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) && | |
9246 | QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
3185942a | 9247 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; |
d233b485 CR |
9248 | /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */ |
9249 | else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null) | |
9250 | ret->flags |= W_SPLITSPACE; /* Posix interp 888 */ | |
9251 | ||
9252 | FREE (name); | |
3185942a JA |
9253 | return ret; |
9254 | } | |
9255 | #endif | |
726f6388 | 9256 | |
ccc6cda3 JA |
9257 | /* Do the right thing based on which character ended the variable name. */ |
9258 | switch (c) | |
9259 | { | |
9260 | default: | |
9261 | case '\0': | |
a0c0a00f | 9262 | bad_substitution: |
712f80b0 | 9263 | set_exit_status (EXECUTION_FAILURE); |
b80f6443 | 9264 | report_error (_("%s: bad substitution"), string ? string : "??"); |
ccc6cda3 JA |
9265 | FREE (value); |
9266 | FREE (temp); | |
9267 | free (name); | |
a0c0a00f CR |
9268 | if (shell_compatibility_level <= 43) |
9269 | return &expand_wdesc_error; | |
9270 | else | |
9271 | return ((posixly_correct && interactive_shell == 0) ? &expand_wdesc_fatal : &expand_wdesc_error); | |
ccc6cda3 | 9272 | |
cce855bc | 9273 | case RBRACE: |
ccc6cda3 | 9274 | break; |
726f6388 | 9275 | |
a0c0a00f | 9276 | case '@': |
d233b485 | 9277 | temp1 = parameter_brace_transform (name, temp, ind, value, c, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
a0c0a00f CR |
9278 | free (temp); |
9279 | free (value); | |
d233b485 | 9280 | |
a0c0a00f CR |
9281 | if (temp1 == &expand_param_error || temp1 == &expand_param_fatal) |
9282 | { | |
d233b485 | 9283 | free (name); |
712f80b0 | 9284 | set_exit_status (EXECUTION_FAILURE); |
a0c0a00f CR |
9285 | report_error (_("%s: bad substitution"), string ? string : "??"); |
9286 | return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal); | |
9287 | } | |
9288 | ||
9289 | ret = alloc_word_desc (); | |
9290 | ret->word = temp1; | |
9291 | if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
9292 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; | |
d233b485 CR |
9293 | /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */ |
9294 | else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null) | |
9295 | ret->flags |= W_SPLITSPACE; /* Posix interp 888 */ | |
9296 | ||
9297 | free (name); | |
a0c0a00f CR |
9298 | return ret; |
9299 | ||
ccc6cda3 JA |
9300 | case '#': /* ${param#[#]pattern} */ |
9301 | case '%': /* ${param%[%]pattern} */ | |
9302 | if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0') | |
28ef6c31 JA |
9303 | { |
9304 | FREE (value); | |
ccc6cda3 | 9305 | break; |
28ef6c31 | 9306 | } |
495aee44 | 9307 | temp1 = parameter_brace_remove_pattern (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0); |
ccc6cda3 JA |
9308 | free (temp); |
9309 | free (value); | |
3185942a JA |
9310 | |
9311 | ret = alloc_word_desc (); | |
9312 | ret->word = temp1; | |
9313 | if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
9314 | ret->flags |= W_QUOTED|W_HASQUOTEDNULL; | |
d233b485 CR |
9315 | /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */ |
9316 | else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null) | |
9317 | ret->flags |= W_SPLITSPACE; /* Posix interp 888 */ | |
9318 | ||
9319 | free (name); | |
3185942a | 9320 | return ret; |
ccc6cda3 JA |
9321 | |
9322 | case '-': | |
9323 | case '=': | |
9324 | case '?': | |
9325 | case '+': | |
9326 | if (var_is_set && var_is_null == 0) | |
28ef6c31 JA |
9327 | { |
9328 | /* If the operator is `+', we don't want the value of the named | |
9329 | variable for anything, just the value of the right hand side. */ | |
ccc6cda3 JA |
9330 | if (c == '+') |
9331 | { | |
28ef6c31 JA |
9332 | /* XXX -- if we're double-quoted and the named variable is "$@", |
9333 | we want to turn off any special handling of "$@" -- | |
9334 | we're not using it, so whatever is on the rhs applies. */ | |
9335 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
9336 | *quoted_dollar_atp = 0; | |
9337 | if (contains_dollar_at) | |
9338 | *contains_dollar_at = 0; | |
9339 | ||
ccc6cda3 JA |
9340 | FREE (temp); |
9341 | if (value) | |
28ef6c31 | 9342 | { |
495aee44 CR |
9343 | /* From Posix discussion on austin-group list. Issue 221 |
9344 | requires that backslashes escaping `}' inside | |
9345 | double-quoted ${...} be removed. */ | |
9346 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
9347 | quoted |= Q_DOLBRACE; | |
95732b49 JA |
9348 | ret = parameter_brace_expand_rhs (name, value, c, |
9349 | quoted, | |
a0c0a00f | 9350 | pflags, |
95732b49 JA |
9351 | quoted_dollar_atp, |
9352 | contains_dollar_at); | |
9353 | /* XXX - fix up later, esp. noting presence of | |
9354 | W_HASQUOTEDNULL in ret->flags */ | |
ccc6cda3 JA |
9355 | free (value); |
9356 | } | |
9357 | else | |
28ef6c31 | 9358 | temp = (char *)NULL; |
ccc6cda3 JA |
9359 | } |
9360 | else | |
9361 | { | |
9362 | FREE (value); | |
9363 | } | |
9364 | /* Otherwise do nothing; just use the value in TEMP. */ | |
726f6388 | 9365 | } |
ccc6cda3 | 9366 | else /* VAR not set or VAR is NULL. */ |
28ef6c31 | 9367 | { |
ccc6cda3 JA |
9368 | FREE (temp); |
9369 | temp = (char *)NULL; | |
9370 | if (c == '=' && var_is_special) | |
9371 | { | |
712f80b0 | 9372 | set_exit_status (EXECUTION_FAILURE); |
b80f6443 | 9373 | report_error (_("$%s: cannot assign in this way"), name); |
ccc6cda3 JA |
9374 | free (name); |
9375 | free (value); | |
95732b49 | 9376 | return &expand_wdesc_error; |
ccc6cda3 JA |
9377 | } |
9378 | else if (c == '?') | |
9379 | { | |
d233b485 | 9380 | parameter_brace_expand_error (name, value, check_nullness); |
95732b49 | 9381 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
ccc6cda3 JA |
9382 | } |
9383 | else if (c != '+') | |
28ef6c31 JA |
9384 | { |
9385 | /* XXX -- if we're double-quoted and the named variable is "$@", | |
9386 | we want to turn off any special handling of "$@" -- | |
9387 | we're not using it, so whatever is on the rhs applies. */ | |
9388 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp) | |
9389 | *quoted_dollar_atp = 0; | |
9390 | if (contains_dollar_at) | |
9391 | *contains_dollar_at = 0; | |
9392 | ||
495aee44 CR |
9393 | /* From Posix discussion on austin-group list. Issue 221 requires |
9394 | that backslashes escaping `}' inside double-quoted ${...} be | |
9395 | removed. */ | |
9396 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
9397 | quoted |= Q_DOLBRACE; | |
a0c0a00f | 9398 | ret = parameter_brace_expand_rhs (name, value, c, quoted, pflags, |
95732b49 JA |
9399 | quoted_dollar_atp, |
9400 | contains_dollar_at); | |
9401 | /* XXX - fix up later, esp. noting presence of | |
9402 | W_HASQUOTEDNULL in tdesc->flags */ | |
28ef6c31 | 9403 | } |
ccc6cda3 | 9404 | free (value); |
726f6388 | 9405 | } |
28ef6c31 | 9406 | |
ccc6cda3 | 9407 | break; |
726f6388 | 9408 | } |
ccc6cda3 | 9409 | free (name); |
95732b49 JA |
9410 | |
9411 | if (ret == 0) | |
9412 | { | |
9413 | ret = alloc_word_desc (); | |
9414 | ret->flags = tflag; | |
9415 | ret->word = temp; | |
9416 | } | |
9417 | return (ret); | |
726f6388 JA |
9418 | } |
9419 | ||
cce855bc JA |
9420 | /* Expand a single ${xxx} expansion. The braces are optional. When |
9421 | the braces are used, parameter_brace_expand() does the work, | |
9422 | possibly calling param_expand recursively. */ | |
95732b49 | 9423 | static WORD_DESC * |
cce855bc JA |
9424 | param_expand (string, sindex, quoted, expanded_something, |
9425 | contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p, | |
9426 | pflags) | |
9427 | char *string; | |
9428 | int *sindex, quoted, *expanded_something, *contains_dollar_at; | |
9429 | int *quoted_dollar_at_p, *had_quoted_null_p, pflags; | |
9430 | { | |
a0c0a00f | 9431 | char *temp, *temp1, uerror[3], *savecmd; |
f73dda09 JA |
9432 | int zindex, t_index, expok; |
9433 | unsigned char c; | |
7117c2d2 | 9434 | intmax_t number; |
cce855bc | 9435 | SHELL_VAR *var; |
712f80b0 | 9436 | WORD_LIST *list, *l; |
95732b49 | 9437 | WORD_DESC *tdesc, *ret; |
712f80b0 | 9438 | int tflag, nullarg; |
cce855bc | 9439 | |
a0c0a00f | 9440 | /*itrace("param_expand: `%s' pflags = %d", string+*sindex, pflags);*/ |
cce855bc JA |
9441 | zindex = *sindex; |
9442 | c = string[++zindex]; | |
9443 | ||
9444 | temp = (char *)NULL; | |
95732b49 JA |
9445 | ret = tdesc = (WORD_DESC *)NULL; |
9446 | tflag = 0; | |
cce855bc JA |
9447 | |
9448 | /* Do simple cases first. Switch on what follows '$'. */ | |
9449 | switch (c) | |
9450 | { | |
9451 | /* $0 .. $9? */ | |
9452 | case '0': | |
9453 | case '1': | |
9454 | case '2': | |
9455 | case '3': | |
9456 | case '4': | |
9457 | case '5': | |
9458 | case '6': | |
9459 | case '7': | |
9460 | case '8': | |
9461 | case '9': | |
f73dda09 | 9462 | temp1 = dollar_vars[TODIGIT (c)]; |
d233b485 | 9463 | /* This doesn't get called when (pflags&PF_IGNUNBOUND) != 0 */ |
cce855bc JA |
9464 | if (unbound_vars_is_error && temp1 == (char *)NULL) |
9465 | { | |
7117c2d2 JA |
9466 | uerror[0] = '$'; |
9467 | uerror[1] = c; | |
9468 | uerror[2] = '\0'; | |
712f80b0 | 9469 | set_exit_status (EXECUTION_FAILURE); |
0001803f | 9470 | err_unboundvar (uerror); |
95732b49 | 9471 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
cce855bc | 9472 | } |
b80f6443 JA |
9473 | if (temp1) |
9474 | temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
9475 | ? quote_string (temp1) | |
9476 | : quote_escapes (temp1); | |
9477 | else | |
9478 | temp = (char *)NULL; | |
95732b49 | 9479 | |
cce855bc JA |
9480 | break; |
9481 | ||
9482 | /* $$ -- pid of the invoking shell. */ | |
9483 | case '$': | |
9484 | temp = itos (dollar_dollar_pid); | |
9485 | break; | |
9486 | ||
9487 | /* $# -- number of positional parameters. */ | |
9488 | case '#': | |
9489 | temp = itos (number_of_args ()); | |
9490 | break; | |
9491 | ||
9492 | /* $? -- return value of the last synchronous command. */ | |
9493 | case '?': | |
9494 | temp = itos (last_command_exit_value); | |
9495 | break; | |
9496 | ||
9497 | /* $- -- flags supplied to the shell on invocation or by `set'. */ | |
9498 | case '-': | |
9499 | temp = which_set_flags (); | |
9500 | break; | |
9501 | ||
9502 | /* $! -- Pid of the last asynchronous command. */ | |
9503 | case '!': | |
9504 | /* If no asynchronous pids have been created, expand to nothing. | |
9505 | If `set -u' has been executed, and no async processes have | |
9506 | been created, this is an expansion error. */ | |
9507 | if (last_asynchronous_pid == NO_PID) | |
9508 | { | |
9509 | if (expanded_something) | |
9510 | *expanded_something = 0; | |
9511 | temp = (char *)NULL; | |
d233b485 | 9512 | if (unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0) |
cce855bc | 9513 | { |
7117c2d2 JA |
9514 | uerror[0] = '$'; |
9515 | uerror[1] = c; | |
9516 | uerror[2] = '\0'; | |
712f80b0 | 9517 | set_exit_status (EXECUTION_FAILURE); |
0001803f | 9518 | err_unboundvar (uerror); |
95732b49 | 9519 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
cce855bc JA |
9520 | } |
9521 | } | |
9522 | else | |
f73dda09 | 9523 | temp = itos (last_asynchronous_pid); |
cce855bc JA |
9524 | break; |
9525 | ||
9526 | /* The only difference between this and $@ is when the arg is quoted. */ | |
9527 | case '*': /* `$*' */ | |
9528 | list = list_rest_of_args (); | |
9529 | ||
89a92869 CR |
9530 | #if 0 |
9531 | /* According to austin-group posix proposal by Geoff Clare in | |
9532 | <20090505091501.GA10097@squonk.masqnet> of 5 May 2009: | |
9533 | ||
9534 | "The shell shall write a message to standard error and | |
9535 | immediately exit when it tries to expand an unset parameter | |
9536 | other than the '@' and '*' special parameters." | |
9537 | */ | |
9538 | ||
9539 | if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0) | |
3185942a JA |
9540 | { |
9541 | uerror[0] = '$'; | |
9542 | uerror[1] = '*'; | |
9543 | uerror[2] = '\0'; | |
712f80b0 | 9544 | set_exit_status (EXECUTION_FAILURE); |
89a92869 | 9545 | err_unboundvar (uerror); |
3185942a JA |
9546 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
9547 | } | |
89a92869 | 9548 | #endif |
3185942a | 9549 | |
cce855bc JA |
9550 | /* If there are no command-line arguments, this should just |
9551 | disappear if there are other characters in the expansion, | |
9552 | even if it's quoted. */ | |
9553 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0) | |
9554 | temp = (char *)NULL; | |
0001803f | 9555 | else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE)) |
cce855bc JA |
9556 | { |
9557 | /* If we have "$*" we want to make a string of the positional | |
9558 | parameters, separated by the first character of $IFS, and | |
9559 | quote the whole string, including the separators. If IFS | |
9560 | is unset, the parameters are separated by ' '; if $IFS is | |
9561 | null, the parameters are concatenated. */ | |
d233b485 | 9562 | temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list, quoted, 0) : string_list (list); |
495aee44 CR |
9563 | if (temp) |
9564 | { | |
a0c0a00f | 9565 | temp1 = (quoted & Q_DOUBLE_QUOTES) ? quote_string (temp) : temp; |
495aee44 CR |
9566 | if (*temp == 0) |
9567 | tflag |= W_HASQUOTEDNULL; | |
a0c0a00f CR |
9568 | if (temp != temp1) |
9569 | free (temp); | |
495aee44 CR |
9570 | temp = temp1; |
9571 | } | |
cce855bc JA |
9572 | } |
9573 | else | |
28ef6c31 | 9574 | { |
95732b49 JA |
9575 | /* We check whether or not we're eventually going to split $* here, |
9576 | for example when IFS is empty and we are processing the rhs of | |
9577 | an assignment statement. In that case, we don't separate the | |
9578 | arguments at all. Otherwise, if the $* is not quoted it is | |
9579 | identical to $@ */ | |
d233b485 CR |
9580 | if (expand_no_split_dollar_star && quoted == 0 && ifs_is_set == 0 && (pflags & PF_ASSIGNRHS)) |
9581 | { | |
712f80b0 CR |
9582 | /* Posix interp 888: RHS of assignment, IFS unset: no splitting, |
9583 | separate with space */ | |
9584 | temp1 = string_list_dollar_star (list, quoted, pflags); | |
9585 | temp = temp1 ? quote_string (temp1) : temp1; | |
9586 | /* XXX - tentative - note that we saw a quoted null here */ | |
9587 | if (temp1 && *temp1 == 0 && QUOTED_NULL (temp)) | |
9588 | tflag |= W_SAWQUOTEDNULL; | |
9589 | FREE (temp1); | |
d233b485 CR |
9590 | } |
9591 | else if (expand_no_split_dollar_star && quoted == 0 && ifs_is_null && (pflags & PF_ASSIGNRHS)) | |
9592 | { | |
9593 | /* Posix interp 888: RHS of assignment, IFS set to '' */ | |
9594 | temp1 = string_list_dollar_star (list, quoted, pflags); | |
9595 | temp = temp1 ? quote_escapes (temp1) : temp1; | |
9596 | FREE (temp1); | |
9597 | } | |
9598 | else if (expand_no_split_dollar_star && quoted == 0 && ifs_is_set && ifs_is_null == 0 && (pflags & PF_ASSIGNRHS)) | |
9599 | { | |
9600 | /* Posix interp 888: RHS of assignment, IFS set to non-null value */ | |
9601 | temp1 = string_list_dollar_star (list, quoted, pflags); | |
9602 | temp = temp1 ? quote_string (temp1) : temp1; | |
712f80b0 CR |
9603 | |
9604 | /* XXX - tentative - note that we saw a quoted null here */ | |
9605 | if (temp1 && *temp1 == 0 && QUOTED_NULL (temp)) | |
9606 | tflag |= W_SAWQUOTEDNULL; | |
d233b485 CR |
9607 | FREE (temp1); |
9608 | } | |
9609 | /* XXX - should we check ifs_is_set here as well? */ | |
95732b49 | 9610 | # if defined (HANDLE_MULTIBYTE) |
d233b485 | 9611 | else if (expand_no_split_dollar_star && ifs_firstc[0] == 0) |
95732b49 | 9612 | # else |
d233b485 | 9613 | else if (expand_no_split_dollar_star && ifs_firstc == 0) |
95732b49 | 9614 | # endif |
d233b485 CR |
9615 | /* Posix interp 888: not RHS, no splitting, IFS set to '' */ |
9616 | temp = string_list_dollar_star (list, quoted, 0); | |
95732b49 | 9617 | else |
ac50fbac | 9618 | { |
a0c0a00f | 9619 | temp = string_list_dollar_at (list, quoted, 0); |
d233b485 CR |
9620 | /* Set W_SPLITSPACE to make sure the individual positional |
9621 | parameters are split into separate arguments */ | |
9622 | #if 0 | |
ac50fbac | 9623 | if (quoted == 0 && (ifs_is_set == 0 || ifs_is_null)) |
d233b485 CR |
9624 | #else /* change with bash-5.0 */ |
9625 | if (quoted == 0 && ifs_is_null) | |
9626 | #endif | |
ac50fbac | 9627 | tflag |= W_SPLITSPACE; |
a0c0a00f CR |
9628 | /* If we're not quoted but we still don't want word splitting, make |
9629 | we quote the IFS characters to protect them from splitting (e.g., | |
9630 | when $@ is in the string as well). */ | |
9631 | else if (temp && quoted == 0 && ifs_is_set && (pflags & PF_ASSIGNRHS)) | |
9632 | { | |
9633 | temp1 = quote_string (temp); | |
9634 | free (temp); | |
9635 | temp = temp1; | |
9636 | } | |
ac50fbac CR |
9637 | } |
9638 | ||
28ef6c31 JA |
9639 | if (expand_no_split_dollar_star == 0 && contains_dollar_at) |
9640 | *contains_dollar_at = 1; | |
9641 | } | |
cce855bc JA |
9642 | |
9643 | dispose_words (list); | |
9644 | break; | |
9645 | ||
9646 | /* When we have "$@" what we want is "$1" "$2" "$3" ... This | |
9647 | means that we have to turn quoting off after we split into | |
9648 | the individually quoted arguments so that the final split | |
9649 | on the first character of $IFS is still done. */ | |
9650 | case '@': /* `$@' */ | |
9651 | list = list_rest_of_args (); | |
9652 | ||
89a92869 CR |
9653 | #if 0 |
9654 | /* According to austin-group posix proposal by Geoff Clare in | |
9655 | <20090505091501.GA10097@squonk.masqnet> of 5 May 2009: | |
9656 | ||
9657 | "The shell shall write a message to standard error and | |
9658 | immediately exit when it tries to expand an unset parameter | |
9659 | other than the '@' and '*' special parameters." | |
9660 | */ | |
9661 | ||
9662 | if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0) | |
3185942a JA |
9663 | { |
9664 | uerror[0] = '$'; | |
9665 | uerror[1] = '@'; | |
9666 | uerror[2] = '\0'; | |
712f80b0 | 9667 | set_exit_status (EXECUTION_FAILURE); |
89a92869 | 9668 | err_unboundvar (uerror); |
3185942a JA |
9669 | return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal); |
9670 | } | |
89a92869 | 9671 | #endif |
3185942a | 9672 | |
712f80b0 CR |
9673 | for (nullarg = 0, l = list; l; l = l->next) |
9674 | { | |
9675 | if (l->word && (l->word->word == 0 || l->word->word[0] == 0)) | |
9676 | nullarg = 1; | |
9677 | } | |
9678 | ||
cce855bc JA |
9679 | /* We want to flag the fact that we saw this. We can't turn |
9680 | off quoting entirely, because other characters in the | |
9681 | string might need it (consider "\"$@\""), but we need some | |
9682 | way to signal that the final split on the first character | |
9683 | of $IFS should be done, even though QUOTED is 1. */ | |
0001803f | 9684 | /* XXX - should this test include Q_PATQUOTE? */ |
cce855bc JA |
9685 | if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
9686 | *quoted_dollar_at_p = 1; | |
9687 | if (contains_dollar_at) | |
9688 | *contains_dollar_at = 1; | |
9689 | ||
9690 | /* We want to separate the positional parameters with the first | |
9691 | character of $IFS in case $IFS is something other than a space. | |
9692 | We also want to make sure that splitting is done no matter what -- | |
9693 | according to POSIX.2, this expands to a list of the positional | |
9694 | parameters no matter what IFS is set to. */ | |
3b34f6e6 CR |
9695 | /* XXX - what to do when in a context where word splitting is not |
9696 | performed? Even when IFS is not the default, posix seems to imply | |
712f80b0 CR |
9697 | that we have to expand $@ to all the positional parameters and |
9698 | separate them with spaces, which are preserved because word splitting | |
9699 | doesn't take place. See below for how we use PF_NOSPLIT2 here. */ | |
d233b485 CR |
9700 | |
9701 | /* These are the cases where word splitting will not be performed. */ | |
9702 | if (pflags & PF_ASSIGNRHS) | |
712f80b0 CR |
9703 | { |
9704 | temp = string_list_dollar_at (list, (quoted|Q_DOUBLE_QUOTES), pflags); | |
9705 | if (nullarg) | |
9706 | tflag |= W_HASQUOTEDNULL; /* we know quoting produces quoted nulls */ | |
9707 | } | |
9708 | ||
d233b485 CR |
9709 | /* This needs to match what expand_word_internal does with non-quoted $@ |
9710 | does with separating with spaces. Passing Q_DOUBLE_QUOTES means that | |
9711 | the characters in LIST will be quoted, and PF_ASSIGNRHS ensures that | |
9712 | they will separated by spaces. After doing this, we need the special | |
9713 | handling for PF_NOSPLIT2 in expand_word_internal to remove the CTLESC | |
9714 | quotes. */ | |
9715 | else if (pflags & PF_NOSPLIT2) | |
9716 | { | |
9717 | #if defined (HANDLE_MULTIBYTE) | |
9718 | if (quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc[0] != ' ') | |
9719 | #else | |
9720 | if (quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc != ' ') | |
9721 | #endif | |
9722 | /* Posix interp 888 */ | |
9723 | temp = string_list_dollar_at (list, Q_DOUBLE_QUOTES, pflags); | |
9724 | else | |
9725 | temp = string_list_dollar_at (list, quoted, pflags); | |
9726 | } | |
9727 | else | |
9728 | temp = string_list_dollar_at (list, quoted, pflags); | |
cce855bc | 9729 | |
ac50fbac | 9730 | tflag |= W_DOLLARAT; |
cce855bc JA |
9731 | dispose_words (list); |
9732 | break; | |
9733 | ||
9734 | case LBRACE: | |
0001803f | 9735 | tdesc = parameter_brace_expand (string, &zindex, quoted, pflags, |
95732b49 JA |
9736 | quoted_dollar_at_p, |
9737 | contains_dollar_at); | |
9738 | ||
95732b49 JA |
9739 | if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal) |
9740 | return (tdesc); | |
9741 | temp = tdesc ? tdesc->word : (char *)0; | |
cce855bc JA |
9742 | |
9743 | /* XXX */ | |
bb70624e | 9744 | /* Quoted nulls should be removed if there is anything else |
cce855bc JA |
9745 | in the string. */ |
9746 | /* Note that we saw the quoted null so we can add one back at | |
9747 | the end of this function if there are no other characters | |
28ef6c31 JA |
9748 | in the string, discard TEMP, and go on. The exception to |
9749 | this is when we have "${@}" and $1 is '', since $@ needs | |
9750 | special handling. */ | |
95732b49 | 9751 | if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp)) |
cce855bc JA |
9752 | { |
9753 | if (had_quoted_null_p) | |
9754 | *had_quoted_null_p = 1; | |
28ef6c31 JA |
9755 | if (*quoted_dollar_at_p == 0) |
9756 | { | |
9757 | free (temp); | |
95732b49 | 9758 | tdesc->word = temp = (char *)NULL; |
28ef6c31 JA |
9759 | } |
9760 | ||
cce855bc JA |
9761 | } |
9762 | ||
95732b49 | 9763 | ret = tdesc; |
cce855bc JA |
9764 | goto return0; |
9765 | ||
9766 | /* Do command or arithmetic substitution. */ | |
9767 | case LPAREN: | |
9768 | /* We have to extract the contents of this paren substitution. */ | |
9769 | t_index = zindex + 1; | |
a0c0a00f CR |
9770 | /* XXX - might want to check for string[t_index+2] == LPAREN and parse |
9771 | as arithmetic substitution immediately. */ | |
9772 | temp = extract_command_subst (string, &t_index, (pflags&PF_COMPLETE) ? SX_COMPLETE : 0); | |
cce855bc JA |
9773 | zindex = t_index; |
9774 | ||
9775 | /* For Posix.2-style `$(( ))' arithmetic substitution, | |
28ef6c31 | 9776 | extract the expression and pass it to the evaluator. */ |
cce855bc JA |
9777 | if (temp && *temp == LPAREN) |
9778 | { | |
9779 | char *temp2; | |
9780 | temp1 = temp + 1; | |
9781 | temp2 = savestring (temp1); | |
9782 | t_index = strlen (temp2) - 1; | |
9783 | ||
9784 | if (temp2[t_index] != RPAREN) | |
9785 | { | |
9786 | free (temp2); | |
9787 | goto comsub; | |
9788 | } | |
9789 | ||
9790 | /* Cut off ending `)' */ | |
9791 | temp2[t_index] = '\0'; | |
9792 | ||
0628567a JA |
9793 | if (chk_arithsub (temp2, t_index) == 0) |
9794 | { | |
9795 | free (temp2); | |
0001803f CR |
9796 | #if 0 |
9797 | internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution")); | |
9798 | #endif | |
0628567a JA |
9799 | goto comsub; |
9800 | } | |
9801 | ||
cce855bc | 9802 | /* Expand variables found inside the expression. */ |
a0c0a00f | 9803 | temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES|Q_ARITH); |
cce855bc JA |
9804 | free (temp2); |
9805 | ||
9806 | arithsub: | |
9807 | /* No error messages. */ | |
a0c0a00f | 9808 | savecmd = this_command_name; |
cce855bc | 9809 | this_command_name = (char *)NULL; |
d233b485 | 9810 | number = evalexp (temp1, EXP_EXPANDED, &expok); |
a0c0a00f | 9811 | this_command_name = savecmd; |
cce855bc JA |
9812 | free (temp); |
9813 | free (temp1); | |
9814 | if (expok == 0) | |
9815 | { | |
9816 | if (interactive_shell == 0 && posixly_correct) | |
9817 | { | |
712f80b0 | 9818 | set_exit_status (EXECUTION_FAILURE); |
95732b49 | 9819 | return (&expand_wdesc_fatal); |
cce855bc JA |
9820 | } |
9821 | else | |
95732b49 | 9822 | return (&expand_wdesc_error); |
cce855bc JA |
9823 | } |
9824 | temp = itos (number); | |
9825 | break; | |
9826 | } | |
9827 | ||
9828 | comsub: | |
b80f6443 JA |
9829 | if (pflags & PF_NOCOMSUB) |
9830 | /* we need zindex+1 because string[zindex] == RPAREN */ | |
9831 | temp1 = substring (string, *sindex, zindex+1); | |
9832 | else | |
3185942a | 9833 | { |
d233b485 | 9834 | tdesc = command_substitute (temp, quoted, pflags&PF_ASSIGNRHS); |
3185942a JA |
9835 | temp1 = tdesc ? tdesc->word : (char *)NULL; |
9836 | if (tdesc) | |
9837 | dispose_word_desc (tdesc); | |
9838 | } | |
cce855bc JA |
9839 | FREE (temp); |
9840 | temp = temp1; | |
9841 | break; | |
9842 | ||
9843 | /* Do POSIX.2d9-style arithmetic substitution. This will probably go | |
9844 | away in a future bash release. */ | |
d233b485 | 9845 | case '[': /*]*/ |
bb70624e | 9846 | /* Extract the contents of this arithmetic substitution. */ |
cce855bc JA |
9847 | t_index = zindex + 1; |
9848 | temp = extract_arithmetic_subst (string, &t_index); | |
9849 | zindex = t_index; | |
3185942a JA |
9850 | if (temp == 0) |
9851 | { | |
9852 | temp = savestring (string); | |
9853 | if (expanded_something) | |
9854 | *expanded_something = 0; | |
9855 | goto return0; | |
9856 | } | |
cce855bc JA |
9857 | |
9858 | /* Do initial variable expansion. */ | |
a0c0a00f | 9859 | temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES|Q_ARITH); |
cce855bc JA |
9860 | |
9861 | goto arithsub; | |
9862 | ||
9863 | default: | |
9864 | /* Find the variable in VARIABLE_LIST. */ | |
9865 | temp = (char *)NULL; | |
9866 | ||
9867 | for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++) | |
9868 | ; | |
9869 | temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL; | |
9870 | ||
9871 | /* If this isn't a variable name, then just output the `$'. */ | |
9872 | if (temp1 == 0 || *temp1 == '\0') | |
9873 | { | |
9874 | FREE (temp1); | |
f73dda09 | 9875 | temp = (char *)xmalloc (2); |
cce855bc JA |
9876 | temp[0] = '$'; |
9877 | temp[1] = '\0'; | |
9878 | if (expanded_something) | |
9879 | *expanded_something = 0; | |
9880 | goto return0; | |
9881 | } | |
9882 | ||
9883 | /* If the variable exists, return its value cell. */ | |
9884 | var = find_variable (temp1); | |
9885 | ||
7117c2d2 | 9886 | if (var && invisible_p (var) == 0 && var_isset (var)) |
cce855bc JA |
9887 | { |
9888 | #if defined (ARRAY_VARS) | |
3185942a | 9889 | if (assoc_p (var) || array_p (var)) |
cce855bc | 9890 | { |
3185942a JA |
9891 | temp = array_p (var) ? array_reference (array_cell (var), 0) |
9892 | : assoc_reference (assoc_cell (var), "0"); | |
cce855bc | 9893 | if (temp) |
b80f6443 JA |
9894 | temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) |
9895 | ? quote_string (temp) | |
9896 | : quote_escapes (temp); | |
9897 | else if (unbound_vars_is_error) | |
9898 | goto unbound_variable; | |
cce855bc JA |
9899 | } |
9900 | else | |
9901 | #endif | |
b80f6443 JA |
9902 | { |
9903 | temp = value_cell (var); | |
9904 | ||
9905 | temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | |
9906 | ? quote_string (temp) | |
d233b485 CR |
9907 | : ((pflags & PF_ASSIGNRHS) ? quote_rhs (temp) |
9908 | : quote_escapes (temp)); | |
b80f6443 JA |
9909 | } |
9910 | ||
cce855bc | 9911 | free (temp1); |
7117c2d2 | 9912 | |
cce855bc JA |
9913 | goto return0; |
9914 | } | |
b64a0e1d CR |
9915 | else if (var && (invisible_p (var) || var_isset (var) == 0)) |
9916 | temp = (char *)NULL; | |
a0c0a00f | 9917 | else if ((var = find_variable_last_nameref (temp1, 0)) && var_isset (var) && invisible_p (var) == 0) |
ac50fbac CR |
9918 | { |
9919 | temp = nameref_cell (var); | |
9920 | #if defined (ARRAY_VARS) | |
a0c0a00f | 9921 | if (temp && *temp && valid_array_reference (temp, 0)) |
ac50fbac CR |
9922 | { |
9923 | tdesc = parameter_brace_expand_word (temp, SPECIAL_VAR (temp, 0), quoted, pflags, (arrayind_t *)NULL); | |
9924 | if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal) | |
9925 | return (tdesc); | |
9926 | ret = tdesc; | |
9927 | goto return0; | |
9928 | } | |
9929 | else | |
9930 | #endif | |
9931 | /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */ | |
9932 | if (temp && *temp && legal_identifier (temp) == 0) | |
9933 | { | |
712f80b0 | 9934 | set_exit_status (EXECUTION_FAILURE); |
ac50fbac CR |
9935 | report_error (_("%s: invalid variable name for name reference"), temp); |
9936 | return (&expand_wdesc_error); /* XXX */ | |
9937 | } | |
9938 | else | |
9939 | temp = (char *)NULL; | |
9940 | } | |
cce855bc JA |
9941 | |
9942 | temp = (char *)NULL; | |
9943 | ||
b80f6443 | 9944 | unbound_variable: |
cce855bc | 9945 | if (unbound_vars_is_error) |
0001803f | 9946 | { |
712f80b0 | 9947 | set_exit_status (EXECUTION_FAILURE); |
0001803f CR |
9948 | err_unboundvar (temp1); |
9949 | } | |
cce855bc JA |
9950 | else |
9951 | { | |
9952 | free (temp1); | |
9953 | goto return0; | |
9954 | } | |
9955 | ||
9956 | free (temp1); | |
712f80b0 | 9957 | set_exit_status (EXECUTION_FAILURE); |
cce855bc | 9958 | return ((unbound_vars_is_error && interactive_shell == 0) |
95732b49 JA |
9959 | ? &expand_wdesc_fatal |
9960 | : &expand_wdesc_error); | |
cce855bc JA |
9961 | } |
9962 | ||
9963 | if (string[zindex]) | |
9964 | zindex++; | |
9965 | ||
9966 | return0: | |
9967 | *sindex = zindex; | |
95732b49 JA |
9968 | |
9969 | if (ret == 0) | |
9970 | { | |
9971 | ret = alloc_word_desc (); | |
9972 | ret->flags = tflag; /* XXX */ | |
9973 | ret->word = temp; | |
9974 | } | |
9975 | return ret; | |
cce855bc JA |
9976 | } |
9977 | ||
a0c0a00f CR |
9978 | void |
9979 | invalidate_cached_quoted_dollar_at () | |
9980 | { | |
9981 | dispose_words (cached_quoted_dollar_at); | |
9982 | cached_quoted_dollar_at = 0; | |
9983 | } | |
9984 | ||
cce855bc JA |
9985 | /* Make a word list which is the result of parameter and variable |
9986 | expansion, command substitution, arithmetic substitution, and | |
9987 | quote removal of WORD. Return a pointer to a WORD_LIST which is | |
9988 | the result of the expansion. If WORD contains a null word, the | |
9989 | word list returned is also null. | |
726f6388 | 9990 | |
ccc6cda3 JA |
9991 | QUOTED contains flag values defined in shell.h. |
9992 | ||
b72432fd JA |
9993 | ISEXP is used to tell expand_word_internal that the word should be |
9994 | treated as the result of an expansion. This has implications for | |
9995 | how IFS characters in the word are treated. | |
9996 | ||
726f6388 JA |
9997 | CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null |
9998 | they point to an integer value which receives information about expansion. | |
9999 | CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero. | |
10000 | EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions, | |
10001 | else zero. | |
10002 | ||
10003 | This only does word splitting in the case of $@ expansion. In that | |
10004 | case, we split on ' '. */ | |
10005 | ||
10006 | /* Values for the local variable quoted_state. */ | |
10007 | #define UNQUOTED 0 | |
10008 | #define PARTIALLY_QUOTED 1 | |
10009 | #define WHOLLY_QUOTED 2 | |
10010 | ||
10011 | static WORD_LIST * | |
b72432fd | 10012 | expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something) |
726f6388 | 10013 | WORD_DESC *word; |
b72432fd | 10014 | int quoted, isexp; |
726f6388 JA |
10015 | int *contains_dollar_at; |
10016 | int *expanded_something; | |
10017 | { | |
ccc6cda3 JA |
10018 | WORD_LIST *list; |
10019 | WORD_DESC *tword; | |
726f6388 JA |
10020 | |
10021 | /* The intermediate string that we build while expanding. */ | |
ccc6cda3 | 10022 | char *istring; |
726f6388 JA |
10023 | |
10024 | /* The current size of the above object. */ | |
a0c0a00f | 10025 | size_t istring_size; |
726f6388 JA |
10026 | |
10027 | /* Index into ISTRING. */ | |
ccc6cda3 | 10028 | int istring_index; |
726f6388 JA |
10029 | |
10030 | /* Temporary string storage. */ | |
ccc6cda3 | 10031 | char *temp, *temp1; |
726f6388 JA |
10032 | |
10033 | /* The text of WORD. */ | |
ccc6cda3 | 10034 | register char *string; |
726f6388 | 10035 | |
7117c2d2 JA |
10036 | /* The size of STRING. */ |
10037 | size_t string_size; | |
10038 | ||
726f6388 | 10039 | /* The index into STRING. */ |
ccc6cda3 | 10040 | int sindex; |
726f6388 JA |
10041 | |
10042 | /* This gets 1 if we see a $@ while quoted. */ | |
ccc6cda3 | 10043 | int quoted_dollar_at; |
726f6388 JA |
10044 | |
10045 | /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on | |
10046 | whether WORD contains no quoting characters, a partially quoted | |
10047 | string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */ | |
ccc6cda3 JA |
10048 | int quoted_state; |
10049 | ||
95732b49 | 10050 | /* State flags */ |
ccc6cda3 | 10051 | int had_quoted_null; |
d233b485 | 10052 | int has_quoted_ifs; /* did we add a quoted $IFS character here? */ |
0b913689 | 10053 | int has_dollar_at, temp_has_dollar_at; |
ac50fbac | 10054 | int split_on_spaces; |
d233b485 | 10055 | int local_expanded; |
28ef6c31 | 10056 | int tflag; |
0001803f | 10057 | int pflags; /* flags passed to param_expand */ |
a0c0a00f | 10058 | int mb_cur_max; |
726f6388 | 10059 | |
95732b49 JA |
10060 | int assignoff; /* If assignment, offset of `=' */ |
10061 | ||
f73dda09 | 10062 | register unsigned char c; /* Current character. */ |
726f6388 | 10063 | int t_index; /* For calls to string_extract_xxx. */ |
726f6388 | 10064 | |
bb70624e | 10065 | char twochars[2]; |
b72432fd | 10066 | |
7117c2d2 JA |
10067 | DECLARE_MBSTATE; |
10068 | ||
a0c0a00f CR |
10069 | /* OK, let's see if we can optimize a common idiom: "$@" */ |
10070 | if (STREQ (word->word, "\"$@\"") && | |
10071 | (word->flags == (W_HASDOLLAR|W_QUOTED)) && | |
10072 | dollar_vars[1]) /* XXX - check IFS here as well? */ | |
10073 | { | |
10074 | if (contains_dollar_at) | |
10075 | *contains_dollar_at = 1; | |
10076 | if (expanded_something) | |
10077 | *expanded_something = 1; | |
10078 | if (cached_quoted_dollar_at) | |
10079 | return (copy_word_list (cached_quoted_dollar_at)); | |
10080 | list = list_rest_of_args (); | |
10081 | list = quote_list (list); | |
10082 | cached_quoted_dollar_at = copy_word_list (list); | |
10083 | return (list); | |
10084 | } | |
10085 | ||
f73dda09 | 10086 | istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE); |
ccc6cda3 | 10087 | istring[istring_index = 0] = '\0'; |
cce855bc | 10088 | quoted_dollar_at = had_quoted_null = has_dollar_at = 0; |
d233b485 | 10089 | has_quoted_ifs = 0; |
ac50fbac | 10090 | split_on_spaces = 0; |
ccc6cda3 JA |
10091 | quoted_state = UNQUOTED; |
10092 | ||
10093 | string = word->word; | |
10094 | if (string == 0) | |
10095 | goto finished_with_string; | |
a0c0a00f CR |
10096 | mb_cur_max = MB_CUR_MAX; |
10097 | ||
95732b49 | 10098 | /* Don't need the string length for the SADD... and COPY_ macros unless |
d233b485 | 10099 | multibyte characters are possible, but do need it for bounds checking. */ |
a0c0a00f | 10100 | string_size = (mb_cur_max > 1) ? strlen (string) : 1; |
726f6388 JA |
10101 | |
10102 | if (contains_dollar_at) | |
10103 | *contains_dollar_at = 0; | |
10104 | ||
95732b49 JA |
10105 | assignoff = -1; |
10106 | ||
726f6388 JA |
10107 | /* Begin the expansion. */ |
10108 | ||
ccc6cda3 | 10109 | for (sindex = 0; ;) |
726f6388 JA |
10110 | { |
10111 | c = string[sindex]; | |
10112 | ||
ac50fbac | 10113 | /* Case on top-level character. */ |
726f6388 JA |
10114 | switch (c) |
10115 | { | |
10116 | case '\0': | |
10117 | goto finished_with_string; | |
10118 | ||
10119 | case CTLESC: | |
7117c2d2 JA |
10120 | sindex++; |
10121 | #if HANDLE_MULTIBYTE | |
a0c0a00f | 10122 | if (mb_cur_max > 1 && string[sindex]) |
7117c2d2 | 10123 | { |
b80f6443 | 10124 | SADD_MBQCHAR_BODY(temp, string, sindex, string_size); |
7117c2d2 JA |
10125 | } |
10126 | else | |
10127 | #endif | |
10128 | { | |
10129 | temp = (char *)xmalloc (3); | |
10130 | temp[0] = CTLESC; | |
10131 | temp[1] = c = string[sindex]; | |
10132 | temp[2] = '\0'; | |
10133 | } | |
726f6388 | 10134 | |
cce855bc | 10135 | dollar_add_string: |
726f6388 JA |
10136 | if (string[sindex]) |
10137 | sindex++; | |
10138 | ||
cce855bc JA |
10139 | add_string: |
10140 | if (temp) | |
10141 | { | |
10142 | istring = sub_append_string (temp, istring, &istring_index, &istring_size); | |
10143 | temp = (char *)0; | |
10144 | } | |
10145 | ||
10146 | break; | |
726f6388 JA |
10147 | |
10148 | #if defined (PROCESS_SUBSTITUTION) | |
10149 | /* Process substitution. */ | |
10150 | case '<': | |
10151 | case '>': | |
10152 | { | |
d233b485 | 10153 | /* XXX - technically this should only be expanded at the start |
a0c0a00f | 10154 | of a word */ |
712f80b0 | 10155 | if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB))) |
726f6388 | 10156 | { |
bb70624e | 10157 | sindex--; /* add_character: label increments sindex */ |
726f6388 JA |
10158 | goto add_character; |
10159 | } | |
10160 | else | |
cce855bc | 10161 | t_index = sindex + 1; /* skip past both '<' and LPAREN */ |
726f6388 | 10162 | |
85b94814 | 10163 | temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index, 0); /*))*/ |
ccc6cda3 | 10164 | sindex = t_index; |
726f6388 JA |
10165 | |
10166 | /* If the process substitution specification is `<()', we want to | |
10167 | open the pipe for writing in the child and produce output; if | |
10168 | it is `>()', we want to open the pipe for reading in the child | |
10169 | and consume input. */ | |
ccc6cda3 | 10170 | temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0; |
726f6388 JA |
10171 | |
10172 | FREE (temp1); | |
10173 | ||
10174 | goto dollar_add_string; | |
10175 | } | |
10176 | #endif /* PROCESS_SUBSTITUTION */ | |
10177 | ||
95732b49 JA |
10178 | case '=': |
10179 | /* Posix.2 section 3.6.1 says that tildes following `=' in words | |
10180 | which are not assignment statements are not expanded. If the | |
10181 | shell isn't in posix mode, though, we perform tilde expansion | |
10182 | on `likely candidate' unquoted assignment statements (flags | |
10183 | include W_ASSIGNMENT but not W_QUOTED). A likely candidate | |
10184 | contains an unquoted :~ or =~. Something to think about: we | |
10185 | now have a flag that says to perform tilde expansion on arguments | |
10186 | to `assignment builtins' like declare and export that look like | |
10187 | assignment statements. We now do tilde expansion on such words | |
10188 | even in POSIX mode. */ | |
10189 | if (word->flags & (W_ASSIGNRHS|W_NOTILDE)) | |
17345e5a | 10190 | { |
0001803f | 10191 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
17345e5a JA |
10192 | goto add_ifs_character; |
10193 | else | |
10194 | goto add_character; | |
10195 | } | |
95732b49 | 10196 | /* If we're not in posix mode or forcing assignment-statement tilde |
3eb0018e CR |
10197 | expansion, note where the first `=' appears in the word and prepare |
10198 | to do tilde expansion following the first `='. We have to keep | |
10199 | track of the first `=' (using assignoff) to avoid being confused | |
10200 | by an `=' in the rhs of the assignment statement. */ | |
95732b49 JA |
10201 | if ((word->flags & W_ASSIGNMENT) && |
10202 | (posixly_correct == 0 || (word->flags & W_TILDEEXP)) && | |
10203 | assignoff == -1 && sindex > 0) | |
10204 | assignoff = sindex; | |
10205 | if (sindex == assignoff && string[sindex+1] == '~') /* XXX */ | |
10206 | word->flags |= W_ITILDE; | |
a0c0a00f | 10207 | |
a0c0a00f CR |
10208 | if (word->flags & W_ASSIGNARG) |
10209 | word->flags |= W_ASSIGNRHS; /* affects $@ */ | |
10210 | ||
0001803f | 10211 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
d233b485 CR |
10212 | { |
10213 | has_quoted_ifs++; | |
10214 | goto add_ifs_character; | |
10215 | } | |
17345e5a JA |
10216 | else |
10217 | goto add_character; | |
95732b49 JA |
10218 | |
10219 | case ':': | |
d233b485 | 10220 | if (word->flags & (W_NOTILDE|W_NOASSNTILDE)) |
17345e5a | 10221 | { |
0001803f | 10222 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
17345e5a JA |
10223 | goto add_ifs_character; |
10224 | else | |
10225 | goto add_character; | |
10226 | } | |
95732b49 | 10227 | |
3eb0018e CR |
10228 | if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS)) && |
10229 | (posixly_correct == 0 || (word->flags & W_TILDEEXP)) && | |
95732b49 JA |
10230 | string[sindex+1] == '~') |
10231 | word->flags |= W_ITILDE; | |
17345e5a | 10232 | |
0001803f | 10233 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c)) |
17345e5a JA |
10234 | goto add_ifs_character; |
10235 | else | |
10236 | goto add_character; | |
95732b49 JA |
10237 | |
10238 | case '~': | |
10239 | /* If the word isn't supposed to be tilde expanded, or we're not | |
10240 | at the start of a word or after an unquoted : or = in an | |
d233b485 CR |
10241 | assignment statement, we don't do tilde expansion. We don't |
10242 | do tilde expansion if quoted or in an arithmetic context. */ | |
10243 | ||
95732b49 JA |
10244 | if ((word->flags & (W_NOTILDE|W_DQUOTE)) || |
10245 | (sindex > 0 && ((word->flags & W_ITILDE) == 0)) || | |
d233b485 | 10246 | (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
95732b49 JA |
10247 | { |
10248 | word->flags &= ~W_ITILDE; | |
0001803f | 10249 | if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0) |
17345e5a JA |
10250 | goto add_ifs_character; |
10251 | else | |
10252 | goto add_character; | |
95732b49 JA |
10253 | } |
10254 | ||
10255 | if (word->flags & W_ASSIGNRHS) | |
10256 | tflag = 2; | |
10257 | else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP)) | |
10258 | tflag = 1; | |
10259 | else | |
10260 | tflag = 0; | |
10261 | ||
10262 | temp = bash_tilde_find_word (string + sindex, tflag, &t_index); | |
10263 | ||
10264 | word->flags &= ~W_ITILDE; | |
10265 | ||
10266 | if (temp && *temp && t_index > 0) | |
10267 | { | |
10268 | temp1 = bash_tilde_expand (temp, tflag); | |
0628567a JA |
10269 | if (temp1 && *temp1 == '~' && STREQ (temp, temp1)) |
10270 | { | |
10271 | FREE (temp); | |
10272 | FREE (temp1); | |
10273 | goto add_character; /* tilde expansion failed */ | |
10274 | } | |
95732b49 JA |
10275 | free (temp); |
10276 | temp = temp1; | |
10277 | sindex += t_index; | |
3185942a | 10278 | goto add_quoted_string; /* XXX was add_string */ |
95732b49 JA |
10279 | } |
10280 | else | |
10281 | { | |
10282 | FREE (temp); | |
10283 | goto add_character; | |
10284 | } | |
10285 | ||
726f6388 | 10286 | case '$': |
726f6388 JA |
10287 | if (expanded_something) |
10288 | *expanded_something = 1; | |
d233b485 | 10289 | local_expanded = 1; |
726f6388 | 10290 | |
0b913689 | 10291 | temp_has_dollar_at = 0; |
0001803f CR |
10292 | pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0; |
10293 | if (word->flags & W_NOSPLIT2) | |
10294 | pflags |= PF_NOSPLIT2; | |
ac50fbac CR |
10295 | if (word->flags & W_ASSIGNRHS) |
10296 | pflags |= PF_ASSIGNRHS; | |
a0c0a00f CR |
10297 | if (word->flags & W_COMPLETE) |
10298 | pflags |= PF_COMPLETE; | |
712f80b0 | 10299 | |
95732b49 | 10300 | tword = param_expand (string, &sindex, quoted, expanded_something, |
0b913689 | 10301 | &temp_has_dollar_at, "ed_dollar_at, |
0001803f | 10302 | &had_quoted_null, pflags); |
0b913689 | 10303 | has_dollar_at += temp_has_dollar_at; |
ac50fbac | 10304 | split_on_spaces += (tword->flags & W_SPLITSPACE); |
726f6388 | 10305 | |
95732b49 | 10306 | if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal) |
726f6388 | 10307 | { |
cce855bc JA |
10308 | free (string); |
10309 | free (istring); | |
95732b49 JA |
10310 | return ((tword == &expand_wdesc_error) ? &expand_word_error |
10311 | : &expand_word_fatal); | |
cce855bc JA |
10312 | } |
10313 | if (contains_dollar_at && has_dollar_at) | |
10314 | *contains_dollar_at = 1; | |
95732b49 JA |
10315 | |
10316 | if (tword && (tword->flags & W_HASQUOTEDNULL)) | |
d233b485 CR |
10317 | had_quoted_null = 1; /* note for later */ |
10318 | if (tword && (tword->flags & W_SAWQUOTEDNULL)) | |
10319 | had_quoted_null = 1; /* XXX */ | |
95732b49 | 10320 | |
ac50fbac | 10321 | temp = tword ? tword->word : (char *)NULL; |
95732b49 JA |
10322 | dispose_word_desc (tword); |
10323 | ||
a601c749 CR |
10324 | /* Kill quoted nulls; we will add them back at the end of |
10325 | expand_word_internal if nothing else in the string */ | |
10326 | if (had_quoted_null && temp && QUOTED_NULL (temp)) | |
10327 | { | |
10328 | FREE (temp); | |
10329 | temp = (char *)NULL; | |
10330 | } | |
10331 | ||
cce855bc JA |
10332 | goto add_string; |
10333 | break; | |
726f6388 | 10334 | |
cce855bc JA |
10335 | case '`': /* Backquoted command substitution. */ |
10336 | { | |
b80f6443 | 10337 | t_index = sindex++; |
726f6388 | 10338 | |
3185942a | 10339 | temp = string_extract (string, &sindex, "`", SX_REQMATCH); |
95732b49 JA |
10340 | /* The test of sindex against t_index is to allow bare instances of |
10341 | ` to pass through, for backwards compatibility. */ | |
10342 | if (temp == &extract_string_error || temp == &extract_string_fatal) | |
10343 | { | |
10344 | if (sindex - 1 == t_index) | |
10345 | { | |
10346 | sindex = t_index; | |
10347 | goto add_character; | |
10348 | } | |
712f80b0 | 10349 | set_exit_status (EXECUTION_FAILURE); |
3185942a | 10350 | report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index); |
95732b49 JA |
10351 | free (string); |
10352 | free (istring); | |
10353 | return ((temp == &extract_string_error) ? &expand_word_error | |
10354 | : &expand_word_fatal); | |
10355 | } | |
10356 | ||
cce855bc JA |
10357 | if (expanded_something) |
10358 | *expanded_something = 1; | |
d233b485 | 10359 | local_expanded = 1; |
726f6388 | 10360 | |
b80f6443 JA |
10361 | if (word->flags & W_NOCOMSUB) |
10362 | /* sindex + 1 because string[sindex] == '`' */ | |
10363 | temp1 = substring (string, t_index, sindex + 1); | |
10364 | else | |
10365 | { | |
10366 | de_backslash (temp); | |
d233b485 | 10367 | tword = command_substitute (temp, quoted, 0); |
3185942a JA |
10368 | temp1 = tword ? tword->word : (char *)NULL; |
10369 | if (tword) | |
10370 | dispose_word_desc (tword); | |
b80f6443 | 10371 | } |
cce855bc JA |
10372 | FREE (temp); |
10373 | temp = temp1; | |
10374 | goto dollar_add_string; | |
10375 | } | |
ccc6cda3 | 10376 | |
cce855bc JA |
10377 | case '\\': |
10378 | if (string[sindex + 1] == '\n') | |
10379 | { | |
10380 | sindex += 2; | |
10381 | continue; | |
10382 | } | |
726f6388 | 10383 | |
cce855bc | 10384 | c = string[++sindex]; |
726f6388 | 10385 | |
cce855bc | 10386 | if (quoted & Q_HERE_DOCUMENT) |
28ef6c31 | 10387 | tflag = CBSHDOC; |
cce855bc | 10388 | else if (quoted & Q_DOUBLE_QUOTES) |
28ef6c31 | 10389 | tflag = CBSDQUOTE; |
cce855bc | 10390 | else |
28ef6c31 JA |
10391 | tflag = 0; |
10392 | ||
495aee44 CR |
10393 | /* From Posix discussion on austin-group list: Backslash escaping |
10394 | a } in ${...} is removed. Issue 0000221 */ | |
10395 | if ((quoted & Q_DOLBRACE) && c == RBRACE) | |
10396 | { | |
ac50fbac CR |
10397 | SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size); |
10398 | } | |
10399 | /* This is the fix for " $@\ " */ | |
10400 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0) && isexp == 0 && isifs (c)) | |
10401 | { | |
10402 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, | |
10403 | DEFAULT_ARRAY_SIZE); | |
10404 | istring[istring_index++] = CTLESC; | |
10405 | istring[istring_index++] = '\\'; | |
10406 | istring[istring_index] = '\0'; | |
10407 | ||
495aee44 CR |
10408 | SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size); |
10409 | } | |
d233b485 CR |
10410 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && c == 0) |
10411 | { | |
10412 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, | |
10413 | DEFAULT_ARRAY_SIZE); | |
10414 | istring[istring_index++] = CTLESC; | |
10415 | istring[istring_index++] = '\\'; | |
10416 | istring[istring_index] = '\0'; | |
10417 | break; | |
10418 | } | |
495aee44 | 10419 | else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0)) |
cce855bc | 10420 | { |
7117c2d2 | 10421 | SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size); |
bb70624e JA |
10422 | } |
10423 | else if (c == 0) | |
10424 | { | |
10425 | c = CTLNUL; | |
10426 | sindex--; /* add_character: label increments sindex */ | |
10427 | goto add_character; | |
cce855bc JA |
10428 | } |
10429 | else | |
bb70624e | 10430 | { |
7117c2d2 | 10431 | SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size); |
bb70624e | 10432 | } |
726f6388 | 10433 | |
bb70624e JA |
10434 | sindex++; |
10435 | add_twochars: | |
10436 | /* BEFORE jumping here, we need to increment sindex if appropriate */ | |
10437 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, | |
10438 | DEFAULT_ARRAY_SIZE); | |
10439 | istring[istring_index++] = twochars[0]; | |
10440 | istring[istring_index++] = twochars[1]; | |
10441 | istring[istring_index] = '\0'; | |
10442 | ||
10443 | break; | |
726f6388 | 10444 | |
cce855bc | 10445 | case '"': |
a0c0a00f | 10446 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && ((quoted & Q_ARITH) == 0)) |
cce855bc | 10447 | goto add_character; |
ccc6cda3 JA |
10448 | |
10449 | t_index = ++sindex; | |
a0c0a00f | 10450 | temp = string_extract_double_quoted (string, &sindex, (word->flags & W_COMPLETE) ? SX_COMPLETE : 0); |
ccc6cda3 JA |
10451 | |
10452 | /* If the quotes surrounded the entire string, then the | |
10453 | whole word was quoted. */ | |
10454 | quoted_state = (t_index == 1 && string[sindex] == '\0') | |
10455 | ? WHOLLY_QUOTED | |
7117c2d2 | 10456 | : PARTIALLY_QUOTED; |
ccc6cda3 JA |
10457 | |
10458 | if (temp && *temp) | |
726f6388 | 10459 | { |
95732b49 JA |
10460 | tword = alloc_word_desc (); |
10461 | tword->word = temp; | |
10462 | ||
a0c0a00f | 10463 | if (word->flags & W_ASSIGNARG) |
d233b485 | 10464 | tword->flags |= word->flags & (W_ASSIGNARG|W_ASSIGNRHS); /* affects $@ */ |
a0c0a00f CR |
10465 | if (word->flags & W_COMPLETE) |
10466 | tword->flags |= W_COMPLETE; /* for command substitutions */ | |
4f747edc CR |
10467 | if (word->flags & W_NOCOMSUB) |
10468 | tword->flags |= W_NOCOMSUB; | |
10469 | if (word->flags & W_NOPROCSUB) | |
10470 | tword->flags |= W_NOPROCSUB; | |
a0c0a00f | 10471 | |
712f80b0 CR |
10472 | if (word->flags & W_ASSIGNRHS) |
10473 | tword->flags |= W_ASSIGNRHS; | |
10474 | ||
ccc6cda3 JA |
10475 | temp = (char *)NULL; |
10476 | ||
d233b485 | 10477 | temp_has_dollar_at = 0; /* does this quoted (sub)string include $@? */ |
95732b49 | 10478 | /* Need to get W_HASQUOTEDNULL flag through this function. */ |
0b913689 CR |
10479 | list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &temp_has_dollar_at, (int *)NULL); |
10480 | has_dollar_at += temp_has_dollar_at; | |
726f6388 | 10481 | |
ccc6cda3 JA |
10482 | if (list == &expand_word_error || list == &expand_word_fatal) |
10483 | { | |
10484 | free (istring); | |
10485 | free (string); | |
10486 | /* expand_word_internal has already freed temp_word->word | |
10487 | for us because of the way it prints error messages. */ | |
10488 | tword->word = (char *)NULL; | |
10489 | dispose_word (tword); | |
10490 | return list; | |
10491 | } | |
726f6388 | 10492 | |
ccc6cda3 | 10493 | dispose_word (tword); |
726f6388 | 10494 | |
ccc6cda3 JA |
10495 | /* "$@" (a double-quoted dollar-at) expands into nothing, |
10496 | not even a NULL word, when there are no positional | |
d233b485 CR |
10497 | parameters. Posix interp 888 says that other parts of the |
10498 | word that expand to quoted nulls result in quoted nulls, so | |
10499 | we can't just throw the entire word away if we have "$@" | |
10500 | anywhere in it. We use had_quoted_null to keep track */ | |
a0c0a00f | 10501 | if (list == 0 && temp_has_dollar_at) /* XXX - was has_dollar_at */ |
726f6388 | 10502 | { |
ccc6cda3 JA |
10503 | quoted_dollar_at++; |
10504 | break; | |
10505 | } | |
10506 | ||
d233b485 CR |
10507 | /* If this list comes back with a quoted null from expansion, |
10508 | we have either "$x" or "$@" with $1 == ''. In either case, | |
10509 | we need to make sure we add a quoted null argument and | |
10510 | disable the special handling that "$@" gets. */ | |
10511 | if (list && list->word && list->next == 0 && (list->word->flags & W_HASQUOTEDNULL)) | |
10512 | { | |
d233b485 | 10513 | if (had_quoted_null && temp_has_dollar_at) |
712f80b0 | 10514 | quoted_dollar_at++; |
d233b485 CR |
10515 | had_quoted_null = 1; /* XXX */ |
10516 | } | |
10517 | ||
ccc6cda3 JA |
10518 | /* If we get "$@", we know we have expanded something, so we |
10519 | need to remember it for the final split on $IFS. This is | |
10520 | a special case; it's the only case where a quoted string | |
10521 | can expand into more than one word. It's going to come back | |
10522 | from the above call to expand_word_internal as a list with | |
712f80b0 | 10523 | multiple words. */ |
ccc6cda3 JA |
10524 | if (list) |
10525 | dequote_list (list); | |
10526 | ||
a0c0a00f | 10527 | if (temp_has_dollar_at) /* XXX - was has_dollar_at */ |
ccc6cda3 JA |
10528 | { |
10529 | quoted_dollar_at++; | |
10530 | if (contains_dollar_at) | |
10531 | *contains_dollar_at = 1; | |
10532 | if (expanded_something) | |
10533 | *expanded_something = 1; | |
d233b485 | 10534 | local_expanded = 1; |
ccc6cda3 JA |
10535 | } |
10536 | } | |
10537 | else | |
10538 | { | |
10539 | /* What we have is "". This is a minor optimization. */ | |
f73dda09 | 10540 | FREE (temp); |
ccc6cda3 | 10541 | list = (WORD_LIST *)NULL; |
d233b485 | 10542 | had_quoted_null = 1; /* note for later */ |
ccc6cda3 JA |
10543 | } |
10544 | ||
10545 | /* The code above *might* return a list (consider the case of "$@", | |
10546 | where it returns "$1", "$2", etc.). We can't throw away the | |
10547 | rest of the list, and we have to make sure each word gets added | |
10548 | as quoted. We test on tresult->next: if it is non-NULL, we | |
10549 | quote the whole list, save it to a string with string_list, and | |
10550 | add that string. We don't need to quote the results of this | |
10551 | (and it would be wrong, since that would quote the separators | |
10552 | as well), so we go directly to add_string. */ | |
10553 | if (list) | |
10554 | { | |
10555 | if (list->next) | |
10556 | { | |
bc4cd23c JA |
10557 | /* Testing quoted_dollar_at makes sure that "$@" is |
10558 | split correctly when $IFS does not contain a space. */ | |
10559 | temp = quoted_dollar_at | |
a0c0a00f | 10560 | ? string_list_dollar_at (list, Q_DOUBLE_QUOTES, 0) |
bc4cd23c | 10561 | : string_list (quote_list (list)); |
ccc6cda3 | 10562 | dispose_words (list); |
726f6388 JA |
10563 | goto add_string; |
10564 | } | |
10565 | else | |
10566 | { | |
ccc6cda3 | 10567 | temp = savestring (list->word->word); |
95732b49 | 10568 | tflag = list->word->flags; |
ccc6cda3 | 10569 | dispose_words (list); |
95732b49 | 10570 | |
cce855bc JA |
10571 | /* If the string is not a quoted null string, we want |
10572 | to remove any embedded unquoted CTLNUL characters. | |
10573 | We do not want to turn quoted null strings back into | |
10574 | the empty string, though. We do this because we | |
10575 | want to remove any quoted nulls from expansions that | |
10576 | contain other characters. For example, if we have | |
10577 | x"$*"y or "x$*y" and there are no positional parameters, | |
7117c2d2 | 10578 | the $* should expand into nothing. */ |
95732b49 JA |
10579 | /* We use the W_HASQUOTEDNULL flag to differentiate the |
10580 | cases: a quoted null character as above and when | |
10581 | CTLNUL is contained in the (non-null) expansion | |
10582 | of some variable. We use the had_quoted_null flag to | |
10583 | pass the value through this function to its caller. */ | |
10584 | if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0) | |
cce855bc | 10585 | remove_quoted_nulls (temp); /* XXX */ |
726f6388 JA |
10586 | } |
10587 | } | |
ccc6cda3 JA |
10588 | else |
10589 | temp = (char *)NULL; | |
726f6388 | 10590 | |
d233b485 CR |
10591 | if (temp == 0 && quoted_state == PARTIALLY_QUOTED) |
10592 | had_quoted_null = 1; /* note for later */ | |
10593 | ||
ccc6cda3 | 10594 | /* We do not want to add quoted nulls to strings that are only |
ac50fbac CR |
10595 | partially quoted; we can throw them away. The exception to |
10596 | this is when we are going to be performing word splitting, | |
10597 | since we have to preserve a null argument if the next character | |
10598 | will cause word splitting. */ | |
712f80b0 CR |
10599 | if (temp == 0 && quoted_state == PARTIALLY_QUOTED && quoted == 0 && (word->flags & (W_NOSPLIT|W_EXPANDRHS|W_ASSIGNRHS)) == W_EXPANDRHS) |
10600 | { | |
10601 | c = CTLNUL; | |
10602 | sindex--; | |
10603 | had_quoted_null = 1; | |
10604 | goto add_character; | |
10605 | } | |
495aee44 | 10606 | if (temp == 0 && quoted_state == PARTIALLY_QUOTED && (word->flags & (W_NOSPLIT|W_NOSPLIT2))) |
cce855bc | 10607 | continue; |
726f6388 | 10608 | |
ccc6cda3 | 10609 | add_quoted_string: |
726f6388 | 10610 | |
ccc6cda3 JA |
10611 | if (temp) |
10612 | { | |
10613 | temp1 = temp; | |
10614 | temp = quote_string (temp); | |
10615 | free (temp1); | |
bb70624e | 10616 | goto add_string; |
ccc6cda3 JA |
10617 | } |
10618 | else | |
10619 | { | |
10620 | /* Add NULL arg. */ | |
bb70624e JA |
10621 | c = CTLNUL; |
10622 | sindex--; /* add_character: label increments sindex */ | |
d233b485 | 10623 | had_quoted_null = 1; /* note for later */ |
bb70624e | 10624 | goto add_character; |
ccc6cda3 | 10625 | } |
bb70624e | 10626 | |
ccc6cda3 | 10627 | /* break; */ |
726f6388 | 10628 | |
ccc6cda3 | 10629 | case '\'': |
95732b49 | 10630 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) |
ccc6cda3 | 10631 | goto add_character; |
726f6388 | 10632 | |
ccc6cda3 JA |
10633 | t_index = ++sindex; |
10634 | temp = string_extract_single_quoted (string, &sindex); | |
726f6388 | 10635 | |
ccc6cda3 JA |
10636 | /* If the entire STRING was surrounded by single quotes, |
10637 | then the string is wholly quoted. */ | |
10638 | quoted_state = (t_index == 1 && string[sindex] == '\0') | |
10639 | ? WHOLLY_QUOTED | |
7117c2d2 | 10640 | : PARTIALLY_QUOTED; |
726f6388 | 10641 | |
ccc6cda3 JA |
10642 | /* If all we had was '', it is a null expansion. */ |
10643 | if (*temp == '\0') | |
10644 | { | |
10645 | free (temp); | |
10646 | temp = (char *)NULL; | |
10647 | } | |
10648 | else | |
7117c2d2 | 10649 | remove_quoted_escapes (temp); /* ??? */ |
726f6388 | 10650 | |
d233b485 CR |
10651 | if (temp == 0 && quoted_state == PARTIALLY_QUOTED) |
10652 | had_quoted_null = 1; /* note for later */ | |
10653 | ||
ccc6cda3 | 10654 | /* We do not want to add quoted nulls to strings that are only |
a0c0a00f | 10655 | partially quoted; such nulls are discarded. See above for the |
d233b485 | 10656 | exception, which is when the string is going to be split. |
712f80b0 CR |
10657 | Posix interp 888/1129 */ |
10658 | if (temp == 0 && quoted_state == PARTIALLY_QUOTED && quoted == 0 && (word->flags & (W_NOSPLIT|W_EXPANDRHS|W_ASSIGNRHS)) == W_EXPANDRHS) | |
10659 | { | |
10660 | c = CTLNUL; | |
10661 | sindex--; | |
10662 | goto add_character; | |
10663 | } | |
10664 | ||
a0c0a00f | 10665 | if (temp == 0 && (quoted_state == PARTIALLY_QUOTED) && (word->flags & (W_NOSPLIT|W_NOSPLIT2))) |
ccc6cda3 | 10666 | continue; |
726f6388 | 10667 | |
bb70624e JA |
10668 | /* If we have a quoted null expansion, add a quoted NULL to istring. */ |
10669 | if (temp == 0) | |
10670 | { | |
10671 | c = CTLNUL; | |
10672 | sindex--; /* add_character: label increments sindex */ | |
10673 | goto add_character; | |
10674 | } | |
10675 | else | |
10676 | goto add_quoted_string; | |
10677 | ||
ccc6cda3 | 10678 | /* break; */ |
726f6388 | 10679 | |
712f80b0 CR |
10680 | case ' ': |
10681 | /* If we are in a context where the word is not going to be split, but | |
10682 | we need to account for $@ and $* producing one word for each | |
10683 | positional parameter, add quoted spaces so the spaces in the | |
10684 | expansion of "$@", if any, behave correctly. We still may need to | |
10685 | split if we are expanding the rhs of a word expansion. */ | |
10686 | if (ifs_is_null || split_on_spaces || ((word->flags & (W_NOSPLIT|W_NOSPLIT2|W_ASSIGNRHS)) && (word->flags & W_EXPANDRHS) == 0)) | |
10687 | { | |
10688 | if (string[sindex]) | |
10689 | sindex++; | |
10690 | twochars[0] = CTLESC; | |
10691 | twochars[1] = c; | |
10692 | goto add_twochars; | |
10693 | } | |
10694 | /* FALLTHROUGH */ | |
10695 | ||
726f6388 | 10696 | default: |
726f6388 | 10697 | /* This is the fix for " $@ " */ |
712f80b0 | 10698 | add_ifs_character: |
a0c0a00f | 10699 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0)) |
726f6388 | 10700 | { |
d233b485 CR |
10701 | if ((quoted&(Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0) |
10702 | has_quoted_ifs++; | |
712f80b0 | 10703 | add_quoted_character: |
bb70624e JA |
10704 | if (string[sindex]) /* from old goto dollar_add_string */ |
10705 | sindex++; | |
10706 | if (c == 0) | |
10707 | { | |
10708 | c = CTLNUL; | |
10709 | goto add_character; | |
10710 | } | |
10711 | else | |
10712 | { | |
7117c2d2 | 10713 | #if HANDLE_MULTIBYTE |
d233b485 CR |
10714 | /* XXX - should make sure that c is actually multibyte, |
10715 | otherwise we can use the twochars branch */ | |
a0c0a00f | 10716 | if (mb_cur_max > 1) |
b80f6443 JA |
10717 | sindex--; |
10718 | ||
a0c0a00f | 10719 | if (mb_cur_max > 1) |
7117c2d2 | 10720 | { |
b80f6443 | 10721 | SADD_MBQCHAR_BODY(temp, string, sindex, string_size); |
7117c2d2 JA |
10722 | } |
10723 | else | |
10724 | #endif | |
10725 | { | |
10726 | twochars[0] = CTLESC; | |
10727 | twochars[1] = c; | |
10728 | goto add_twochars; | |
10729 | } | |
bb70624e | 10730 | } |
726f6388 JA |
10731 | } |
10732 | ||
7117c2d2 JA |
10733 | SADD_MBCHAR (temp, string, sindex, string_size); |
10734 | ||
712f80b0 | 10735 | add_character: |
ccc6cda3 JA |
10736 | RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size, |
10737 | DEFAULT_ARRAY_SIZE); | |
726f6388 JA |
10738 | istring[istring_index++] = c; |
10739 | istring[istring_index] = '\0'; | |
10740 | ||
10741 | /* Next character. */ | |
10742 | sindex++; | |
10743 | } | |
10744 | } | |
10745 | ||
10746 | finished_with_string: | |
726f6388 JA |
10747 | /* OK, we're ready to return. If we have a quoted string, and |
10748 | quoted_dollar_at is not set, we do no splitting at all; otherwise | |
10749 | we split on ' '. The routines that call this will handle what to | |
10750 | do if nothing has been expanded. */ | |
ccc6cda3 JA |
10751 | |
10752 | /* Partially and wholly quoted strings which expand to the empty | |
10753 | string are retained as an empty arguments. Unquoted strings | |
10754 | which expand to the empty string are discarded. The single | |
10755 | exception is the case of expanding "$@" when there are no | |
10756 | positional parameters. In that case, we discard the expansion. */ | |
10757 | ||
10758 | /* Because of how the code that handles "" and '' in partially | |
10759 | quoted strings works, we need to make ISTRING into a QUOTED_NULL | |
10760 | if we saw quoting characters, but the expansion was empty. | |
10761 | "" and '' are tossed away before we get to this point when | |
10762 | processing partially quoted strings. This makes "" and $xxx"" | |
10763 | equivalent when xxx is unset. We also look to see whether we | |
10764 | saw a quoted null from a ${} expansion and add one back if we | |
10765 | need to. */ | |
10766 | ||
10767 | /* If we expand to nothing and there were no single or double quotes | |
10768 | in the word, we throw it away. Otherwise, we return a NULL word. | |
10769 | The single exception is for $@ surrounded by double quotes when | |
10770 | there are no positional parameters. In that case, we also throw | |
10771 | the word away. */ | |
10772 | ||
10773 | if (*istring == '\0') | |
10774 | { | |
10775 | if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED)) | |
726f6388 | 10776 | { |
726f6388 JA |
10777 | istring[0] = CTLNUL; |
10778 | istring[1] = '\0'; | |
d233b485 CR |
10779 | tword = alloc_word_desc (); |
10780 | tword->word = istring; | |
10781 | istring = 0; /* avoid later free() */ | |
95732b49 | 10782 | tword->flags |= W_HASQUOTEDNULL; /* XXX */ |
ccc6cda3 JA |
10783 | list = make_word_list (tword, (WORD_LIST *)NULL); |
10784 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) | |
10785 | tword->flags |= W_QUOTED; | |
726f6388 | 10786 | } |
ccc6cda3 JA |
10787 | /* According to sh, ksh, and Posix.2, if a word expands into nothing |
10788 | and a double-quoted "$@" appears anywhere in it, then the entire | |
10789 | word is removed. */ | |
a0c0a00f CR |
10790 | /* XXX - exception appears to be that quoted null strings result in |
10791 | null arguments */ | |
ccc6cda3 JA |
10792 | else if (quoted_state == UNQUOTED || quoted_dollar_at) |
10793 | list = (WORD_LIST *)NULL; | |
f73dda09 JA |
10794 | else |
10795 | list = (WORD_LIST *)NULL; | |
ccc6cda3 JA |
10796 | } |
10797 | else if (word->flags & W_NOSPLIT) | |
10798 | { | |
d233b485 CR |
10799 | tword = alloc_word_desc (); |
10800 | tword->word = istring; | |
10801 | if (had_quoted_null && QUOTED_NULL (istring)) | |
10802 | tword->flags |= W_HASQUOTEDNULL; | |
10803 | istring = 0; /* avoid later free() */ | |
ccc6cda3 JA |
10804 | if (word->flags & W_ASSIGNMENT) |
10805 | tword->flags |= W_ASSIGNMENT; /* XXX */ | |
95732b49 JA |
10806 | if (word->flags & W_COMPASSIGN) |
10807 | tword->flags |= W_COMPASSIGN; /* XXX */ | |
b72432fd JA |
10808 | if (word->flags & W_NOGLOB) |
10809 | tword->flags |= W_NOGLOB; /* XXX */ | |
ac50fbac CR |
10810 | if (word->flags & W_NOBRACE) |
10811 | tword->flags |= W_NOBRACE; /* XXX */ | |
ccc6cda3 | 10812 | if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) |
28ef6c31 | 10813 | tword->flags |= W_QUOTED; |
95732b49 | 10814 | list = make_word_list (tword, (WORD_LIST *)NULL); |
ccc6cda3 | 10815 | } |
712f80b0 CR |
10816 | else if (word->flags & W_ASSIGNRHS) |
10817 | { | |
10818 | list = list_string (istring, "", quoted); | |
10819 | tword = list->word; | |
10820 | if (had_quoted_null && QUOTED_NULL (istring)) | |
10821 | tword->flags |= W_HASQUOTEDNULL; | |
10822 | free (list); | |
10823 | free (istring); | |
10824 | istring = 0; /* avoid later free() */ | |
10825 | goto set_word_flags; | |
10826 | } | |
ccc6cda3 JA |
10827 | else |
10828 | { | |
10829 | char *ifs_chars; | |
10830 | ||
7117c2d2 | 10831 | ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL; |
726f6388 | 10832 | |
cce855bc JA |
10833 | /* If we have $@, we need to split the results no matter what. If |
10834 | IFS is unset or NULL, string_list_dollar_at has separated the | |
10835 | positional parameters with a space, so we split on space (we have | |
10836 | set ifs_chars to " \t\n" above if ifs is unset). If IFS is set, | |
10837 | string_list_dollar_at has separated the positional parameters | |
ac50fbac CR |
10838 | with the first character of $IFS, so we split on $IFS. If |
10839 | SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either | |
10840 | unset or null, and we want to make sure that we split on spaces | |
a0c0a00f CR |
10841 | regardless of what else has happened to IFS since the expansion, |
10842 | or we expanded "$@" with IFS null and we need to split the positional | |
10843 | parameters into separate words. */ | |
ac50fbac | 10844 | if (split_on_spaces) |
d233b485 CR |
10845 | { |
10846 | /* If IFS is not set, and the word is not quoted, we want to split | |
10847 | the individual words on $' \t\n'. We rely on previous steps to | |
10848 | quote the portions of the word that should not be split */ | |
10849 | if (ifs_is_set == 0) | |
10850 | list = list_string (istring, " \t\n", 1); /* XXX quoted == 1? */ | |
10851 | else | |
10852 | list = list_string (istring, " ", 1); /* XXX quoted == 1? */ | |
10853 | } | |
10854 | ||
3b34f6e6 CR |
10855 | /* If we have $@ (has_dollar_at != 0) and we are in a context where we |
10856 | don't want to split the result (W_NOSPLIT2), and we are not quoted, | |
10857 | we have already separated the arguments with the first character of | |
10858 | $IFS. In this case, we want to return a list with a single word | |
10859 | with the separator possibly replaced with a space (it's what other | |
10860 | shells seem to do). | |
10861 | quoted_dollar_at is internal to this function and is set if we are | |
10862 | passed an argument that is unquoted (quoted == 0) but we encounter a | |
10863 | double-quoted $@ while expanding it. */ | |
10864 | else if (has_dollar_at && quoted_dollar_at == 0 && ifs_chars && quoted == 0 && (word->flags & W_NOSPLIT2)) | |
10865 | { | |
d233b485 | 10866 | tword = alloc_word_desc (); |
3b34f6e6 CR |
10867 | /* Only split and rejoin if we have to */ |
10868 | if (*ifs_chars && *ifs_chars != ' ') | |
10869 | { | |
d233b485 CR |
10870 | /* list_string dequotes CTLESCs in the string it's passed, so we |
10871 | need it to get the space separation right if space isn't the | |
10872 | first character in IFS (but is present) and to remove the | |
10873 | quoting we added back in param_expand(). */ | |
3b34f6e6 | 10874 | list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1); |
d233b485 CR |
10875 | /* This isn't exactly right in the case where we're expanding |
10876 | the RHS of an expansion like ${var-$@} where IFS=: (for | |
10877 | example). The W_NOSPLIT2 means we do the separation with :; | |
10878 | the list_string removes the quotes and breaks the string into | |
10879 | a list, and the string_list rejoins it on spaces. When we | |
10880 | return, we expect to be able to split the results, but the | |
10881 | space separation means the right split doesn't happen. */ | |
10882 | tword->word = string_list (list); | |
3b34f6e6 CR |
10883 | } |
10884 | else | |
d233b485 CR |
10885 | tword->word = istring; |
10886 | if (had_quoted_null && QUOTED_NULL (istring)) | |
10887 | tword->flags |= W_HASQUOTEDNULL; /* XXX */ | |
10888 | if (tword->word != istring) | |
10889 | free (istring); | |
10890 | istring = 0; /* avoid later free() */ | |
a0c0a00f CR |
10891 | goto set_word_flags; |
10892 | } | |
ac50fbac | 10893 | else if (has_dollar_at && ifs_chars) |
cce855bc | 10894 | list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1); |
ccc6cda3 JA |
10895 | else |
10896 | { | |
d233b485 CR |
10897 | tword = alloc_word_desc (); |
10898 | if (expanded_something && *expanded_something == 0 && has_quoted_ifs) | |
10899 | tword->word = remove_quoted_ifs (istring); | |
10900 | else | |
10901 | tword->word = istring; | |
712f80b0 | 10902 | if (had_quoted_null && QUOTED_NULL (istring)) /* should check for more than one */ |
d233b485 CR |
10903 | tword->flags |= W_HASQUOTEDNULL; /* XXX */ |
10904 | else if (had_quoted_null) | |
10905 | tword->flags |= W_SAWQUOTEDNULL; /* XXX */ | |
10906 | if (tword->word != istring) | |
10907 | free (istring); | |
10908 | istring = 0; /* avoid later free() */ | |
3b34f6e6 | 10909 | set_word_flags: |
ccc6cda3 JA |
10910 | if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED)) |
10911 | tword->flags |= W_QUOTED; | |
10912 | if (word->flags & W_ASSIGNMENT) | |
10913 | tword->flags |= W_ASSIGNMENT; | |
95732b49 JA |
10914 | if (word->flags & W_COMPASSIGN) |
10915 | tword->flags |= W_COMPASSIGN; | |
b72432fd JA |
10916 | if (word->flags & W_NOGLOB) |
10917 | tword->flags |= W_NOGLOB; | |
ac50fbac CR |
10918 | if (word->flags & W_NOBRACE) |
10919 | tword->flags |= W_NOBRACE; | |
95732b49 | 10920 | list = make_word_list (tword, (WORD_LIST *)NULL); |
726f6388 | 10921 | } |
726f6388 | 10922 | } |
726f6388 | 10923 | |
ccc6cda3 JA |
10924 | free (istring); |
10925 | return (list); | |
726f6388 JA |
10926 | } |
10927 | ||
10928 | /* **************************************************************** */ | |
10929 | /* */ | |
10930 | /* Functions for Quote Removal */ | |
10931 | /* */ | |
10932 | /* **************************************************************** */ | |
10933 | ||
10934 | /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the | |
7117c2d2 | 10935 | backslash quoting rules for within double quotes or a here document. */ |
726f6388 JA |
10936 | char * |
10937 | string_quote_removal (string, quoted) | |
10938 | char *string; | |
10939 | int quoted; | |
10940 | { | |
7117c2d2 JA |
10941 | size_t slen; |
10942 | char *r, *result_string, *temp, *send; | |
f73dda09 JA |
10943 | int sindex, tindex, dquote; |
10944 | unsigned char c; | |
7117c2d2 | 10945 | DECLARE_MBSTATE; |
726f6388 JA |
10946 | |
10947 | /* The result can be no longer than the original string. */ | |
7117c2d2 JA |
10948 | slen = strlen (string); |
10949 | send = string + slen; | |
10950 | ||
10951 | r = result_string = (char *)xmalloc (slen + 1); | |
726f6388 | 10952 | |
ccc6cda3 | 10953 | for (dquote = sindex = 0; c = string[sindex];) |
726f6388 JA |
10954 | { |
10955 | switch (c) | |
10956 | { | |
10957 | case '\\': | |
10958 | c = string[++sindex]; | |
3185942a JA |
10959 | if (c == 0) |
10960 | { | |
10961 | *r++ = '\\'; | |
10962 | break; | |
10963 | } | |
28ef6c31 | 10964 | if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0) |
726f6388 | 10965 | *r++ = '\\'; |
ccc6cda3 | 10966 | /* FALLTHROUGH */ |
726f6388 JA |
10967 | |
10968 | default: | |
7117c2d2 | 10969 | SCOPY_CHAR_M (r, string, send, sindex); |
726f6388 JA |
10970 | break; |
10971 | ||
10972 | case '\'': | |
ccc6cda3 | 10973 | if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) |
726f6388 JA |
10974 | { |
10975 | *r++ = c; | |
10976 | sindex++; | |
ccc6cda3 | 10977 | break; |
726f6388 | 10978 | } |
ccc6cda3 JA |
10979 | tindex = sindex + 1; |
10980 | temp = string_extract_single_quoted (string, &tindex); | |
10981 | if (temp) | |
726f6388 | 10982 | { |
ccc6cda3 JA |
10983 | strcpy (r, temp); |
10984 | r += strlen (r); | |
10985 | free (temp); | |
726f6388 | 10986 | } |
ccc6cda3 | 10987 | sindex = tindex; |
726f6388 JA |
10988 | break; |
10989 | ||
10990 | case '"': | |
10991 | dquote = 1 - dquote; | |
10992 | sindex++; | |
10993 | break; | |
10994 | } | |
10995 | } | |
10996 | *r = '\0'; | |
10997 | return (result_string); | |
10998 | } | |
10999 | ||
ccc6cda3 JA |
11000 | #if 0 |
11001 | /* UNUSED */ | |
726f6388 JA |
11002 | /* Perform quote removal on word WORD. This allocates and returns a new |
11003 | WORD_DESC *. */ | |
11004 | WORD_DESC * | |
11005 | word_quote_removal (word, quoted) | |
11006 | WORD_DESC *word; | |
11007 | int quoted; | |
11008 | { | |
11009 | WORD_DESC *w; | |
11010 | char *t; | |
11011 | ||
11012 | t = string_quote_removal (word->word, quoted); | |
95732b49 JA |
11013 | w = alloc_word_desc (); |
11014 | w->word = t ? t : savestring (""); | |
726f6388 JA |
11015 | return (w); |
11016 | } | |
11017 | ||
11018 | /* Perform quote removal on all words in LIST. If QUOTED is non-zero, | |
11019 | the members of the list are treated as if they are surrounded by | |
11020 | double quotes. Return a new list, or NULL if LIST is NULL. */ | |
11021 | WORD_LIST * | |
11022 | word_list_quote_removal (list, quoted) | |
11023 | WORD_LIST *list; | |
11024 | int quoted; | |
11025 | { | |
95732b49 | 11026 | WORD_LIST *result, *t, *tresult, *e; |
726f6388 | 11027 | |
ccc6cda3 | 11028 | for (t = list, result = (WORD_LIST *)NULL; t; t = t->next) |
726f6388 | 11029 | { |
7117c2d2 | 11030 | tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL); |
95732b49 | 11031 | #if 0 |
726f6388 | 11032 | result = (WORD_LIST *) list_append (result, tresult); |
95732b49 JA |
11033 | #else |
11034 | if (result == 0) | |
11035 | result = e = tresult; | |
11036 | else | |
11037 | { | |
11038 | e->next = tresult; | |
11039 | while (e->next) | |
11040 | e = e->next; | |
11041 | } | |
11042 | #endif | |
726f6388 JA |
11043 | } |
11044 | return (result); | |
11045 | } | |
ccc6cda3 | 11046 | #endif |
726f6388 | 11047 | |
726f6388 JA |
11048 | /******************************************* |
11049 | * * | |
11050 | * Functions to perform word splitting * | |
11051 | * * | |
11052 | *******************************************/ | |
11053 | ||
7117c2d2 JA |
11054 | void |
11055 | setifs (v) | |
11056 | SHELL_VAR *v; | |
b72432fd | 11057 | { |
7117c2d2 JA |
11058 | char *t; |
11059 | unsigned char uc; | |
11060 | ||
11061 | ifs_var = v; | |
95732b49 | 11062 | ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n"; |
b72432fd | 11063 | |
ac50fbac CR |
11064 | ifs_is_set = ifs_var != 0; |
11065 | ifs_is_null = ifs_is_set && (*ifs_value == 0); | |
11066 | ||
95732b49 JA |
11067 | /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet |
11068 | handle multibyte chars in IFS */ | |
7117c2d2 JA |
11069 | memset (ifs_cmap, '\0', sizeof (ifs_cmap)); |
11070 | for (t = ifs_value ; t && *t; t++) | |
11071 | { | |
11072 | uc = *t; | |
11073 | ifs_cmap[uc] = 1; | |
11074 | } | |
11075 | ||
95732b49 JA |
11076 | #if defined (HANDLE_MULTIBYTE) |
11077 | if (ifs_value == 0) | |
11078 | { | |
d233b485 | 11079 | ifs_firstc[0] = '\0'; /* XXX - ? */ |
95732b49 JA |
11080 | ifs_firstc_len = 1; |
11081 | } | |
11082 | else | |
11083 | { | |
d233b485 CR |
11084 | if (locale_utf8locale && UTF8_SINGLEBYTE (*ifs_value)) |
11085 | ifs_firstc_len = (*ifs_value != 0) ? 1 : 0; | |
11086 | else | |
11087 | { | |
11088 | size_t ifs_len; | |
11089 | ifs_len = strnlen (ifs_value, MB_CUR_MAX); | |
11090 | ifs_firstc_len = MBLEN (ifs_value, ifs_len); | |
11091 | } | |
95732b49 JA |
11092 | if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len)) |
11093 | { | |
11094 | ifs_firstc[0] = ifs_value[0]; | |
11095 | ifs_firstc[1] = '\0'; | |
11096 | ifs_firstc_len = 1; | |
11097 | } | |
11098 | else | |
11099 | memcpy (ifs_firstc, ifs_value, ifs_firstc_len); | |
11100 | } | |
11101 | #else | |
7117c2d2 | 11102 | ifs_firstc = ifs_value ? *ifs_value : 0; |
95732b49 | 11103 | #endif |
7117c2d2 JA |
11104 | } |
11105 | ||
11106 | char * | |
11107 | getifs () | |
11108 | { | |
11109 | return ifs_value; | |
b72432fd JA |
11110 | } |
11111 | ||
726f6388 JA |
11112 | /* This splits a single word into a WORD LIST on $IFS, but only if the word |
11113 | is not quoted. list_string () performs quote removal for us, even if we | |
11114 | don't do any splitting. */ | |
11115 | WORD_LIST * | |
7117c2d2 | 11116 | word_split (w, ifs_chars) |
726f6388 | 11117 | WORD_DESC *w; |
7117c2d2 | 11118 | char *ifs_chars; |
726f6388 JA |
11119 | { |
11120 | WORD_LIST *result; | |
11121 | ||
11122 | if (w) | |
11123 | { | |
7117c2d2 | 11124 | char *xifs; |
726f6388 | 11125 | |
7117c2d2 JA |
11126 | xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars; |
11127 | result = list_string (w->word, xifs, w->flags & W_QUOTED); | |
726f6388 JA |
11128 | } |
11129 | else | |
11130 | result = (WORD_LIST *)NULL; | |
ccc6cda3 | 11131 | |
726f6388 JA |
11132 | return (result); |
11133 | } | |
11134 | ||
11135 | /* Perform word splitting on LIST and return the RESULT. It is possible | |
11136 | to return (WORD_LIST *)NULL. */ | |
11137 | static WORD_LIST * | |
11138 | word_list_split (list) | |
11139 | WORD_LIST *list; | |
11140 | { | |
95732b49 | 11141 | WORD_LIST *result, *t, *tresult, *e; |
d233b485 | 11142 | WORD_DESC *w; |
726f6388 | 11143 | |
ccc6cda3 | 11144 | for (t = list, result = (WORD_LIST *)NULL; t; t = t->next) |
726f6388 | 11145 | { |
7117c2d2 | 11146 | tresult = word_split (t->word, ifs_value); |
d233b485 CR |
11147 | /* POSIX 2.6: "If the complete expansion appropriate for a word results |
11148 | in an empty field, that empty field shall be deleted from the list | |
11149 | of fields that form the completely expanded command, unless the | |
11150 | original word contained single-quote or double-quote characters." | |
11151 | This is where we handle these words that contain quoted null strings | |
11152 | and other characters that expand to nothing after word splitting. */ | |
11153 | if (tresult == 0 && t->word && (t->word->flags & W_SAWQUOTEDNULL)) /* XXX */ | |
11154 | { | |
11155 | w = alloc_word_desc (); | |
11156 | w->word = (char *)xmalloc (1); | |
11157 | w->word[0] = '\0'; | |
11158 | tresult = make_word_list (w, (WORD_LIST *)NULL); | |
11159 | } | |
95732b49 JA |
11160 | if (result == 0) |
11161 | result = e = tresult; | |
11162 | else | |
11163 | { | |
11164 | e->next = tresult; | |
11165 | while (e->next) | |
11166 | e = e->next; | |
11167 | } | |
726f6388 JA |
11168 | } |
11169 | return (result); | |
11170 | } | |
11171 | ||
11172 | /************************************************** | |
11173 | * * | |
cce855bc | 11174 | * Functions to expand an entire WORD_LIST * |
726f6388 JA |
11175 | * * |
11176 | **************************************************/ | |
11177 | ||
b80f6443 JA |
11178 | /* Do any word-expansion-specific cleanup and jump to top_level */ |
11179 | static void | |
11180 | exp_jump_to_top_level (v) | |
11181 | int v; | |
11182 | { | |
3185942a JA |
11183 | set_pipestatus_from_exit (last_command_exit_value); |
11184 | ||
b80f6443 JA |
11185 | /* Cleanup code goes here. */ |
11186 | expand_no_split_dollar_star = 0; /* XXX */ | |
d233b485 CR |
11187 | if (expanding_redir) |
11188 | undo_partial_redirects (); | |
b80f6443 | 11189 | expanding_redir = 0; |
3185942a | 11190 | assigning_in_environment = 0; |
b80f6443 | 11191 | |
f1be666c JA |
11192 | if (parse_and_execute_level == 0) |
11193 | top_level_cleanup (); /* from sig.c */ | |
11194 | ||
b80f6443 JA |
11195 | jump_to_top_level (v); |
11196 | } | |
11197 | ||
cce855bc JA |
11198 | /* Put NLIST (which is a WORD_LIST * of only one element) at the front of |
11199 | ELIST, and set ELIST to the new list. */ | |
11200 | #define PREPEND_LIST(nlist, elist) \ | |
11201 | do { nlist->next = elist; elist = nlist; } while (0) | |
11202 | ||
726f6388 JA |
11203 | /* Separate out any initial variable assignments from TLIST. If set -k has |
11204 | been executed, remove all assignment statements from TLIST. Initial | |
11205 | variable assignments and other environment assignments are placed | |
bb70624e | 11206 | on SUBST_ASSIGN_VARLIST. */ |
726f6388 JA |
11207 | static WORD_LIST * |
11208 | separate_out_assignments (tlist) | |
11209 | WORD_LIST *tlist; | |
11210 | { | |
11211 | register WORD_LIST *vp, *lp; | |
11212 | ||
0001803f | 11213 | if (tlist == 0) |
726f6388 JA |
11214 | return ((WORD_LIST *)NULL); |
11215 | ||
bb70624e JA |
11216 | if (subst_assign_varlist) |
11217 | dispose_words (subst_assign_varlist); /* Clean up after previous error */ | |
b72432fd | 11218 | |
bb70624e | 11219 | subst_assign_varlist = (WORD_LIST *)NULL; |
726f6388 JA |
11220 | vp = lp = tlist; |
11221 | ||
11222 | /* Separate out variable assignments at the start of the command. | |
11223 | Loop invariant: vp->next == lp | |
11224 | Loop postcondition: | |
7117c2d2 JA |
11225 | lp = list of words left after assignment statements skipped |
11226 | tlist = original list of words | |
726f6388 | 11227 | */ |
ccc6cda3 | 11228 | while (lp && (lp->word->flags & W_ASSIGNMENT)) |
726f6388 JA |
11229 | { |
11230 | vp = lp; | |
11231 | lp = lp->next; | |
11232 | } | |
11233 | ||
bb70624e JA |
11234 | /* If lp != tlist, we have some initial assignment statements. |
11235 | We make SUBST_ASSIGN_VARLIST point to the list of assignment | |
11236 | words and TLIST point to the remaining words. */ | |
726f6388 JA |
11237 | if (lp != tlist) |
11238 | { | |
bb70624e | 11239 | subst_assign_varlist = tlist; |
726f6388 JA |
11240 | /* ASSERT(vp->next == lp); */ |
11241 | vp->next = (WORD_LIST *)NULL; /* terminate variable list */ | |
11242 | tlist = lp; /* remainder of word list */ | |
11243 | } | |
11244 | ||
11245 | /* vp == end of variable list */ | |
11246 | /* tlist == remainder of original word list without variable assignments */ | |
11247 | if (!tlist) | |
11248 | /* All the words in tlist were assignment statements */ | |
11249 | return ((WORD_LIST *)NULL); | |
11250 | ||
11251 | /* ASSERT(tlist != NULL); */ | |
ccc6cda3 | 11252 | /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */ |
726f6388 JA |
11253 | |
11254 | /* If the -k option is in effect, we need to go through the remaining | |
bb70624e JA |
11255 | words, separate out the assignment words, and place them on |
11256 | SUBST_ASSIGN_VARLIST. */ | |
726f6388 JA |
11257 | if (place_keywords_in_env) |
11258 | { | |
11259 | WORD_LIST *tp; /* tp == running pointer into tlist */ | |
11260 | ||
11261 | tp = tlist; | |
11262 | lp = tlist->next; | |
11263 | ||
11264 | /* Loop Invariant: tp->next == lp */ | |
11265 | /* Loop postcondition: tlist == word list without assignment statements */ | |
11266 | while (lp) | |
11267 | { | |
ccc6cda3 | 11268 | if (lp->word->flags & W_ASSIGNMENT) |
726f6388 JA |
11269 | { |
11270 | /* Found an assignment statement, add this word to end of | |
bb70624e JA |
11271 | subst_assign_varlist (vp). */ |
11272 | if (!subst_assign_varlist) | |
11273 | subst_assign_varlist = vp = lp; | |
726f6388 JA |
11274 | else |
11275 | { | |
11276 | vp->next = lp; | |
11277 | vp = lp; | |
11278 | } | |
11279 | ||
11280 | /* Remove the word pointed to by LP from TLIST. */ | |
11281 | tp->next = lp->next; | |
11282 | /* ASSERT(vp == lp); */ | |
11283 | lp->next = (WORD_LIST *)NULL; | |
11284 | lp = tp->next; | |
11285 | } | |
11286 | else | |
11287 | { | |
11288 | tp = lp; | |
11289 | lp = lp->next; | |
11290 | } | |
11291 | } | |
11292 | } | |
11293 | return (tlist); | |
11294 | } | |
11295 | ||
cce855bc JA |
11296 | #define WEXP_VARASSIGN 0x001 |
11297 | #define WEXP_BRACEEXP 0x002 | |
11298 | #define WEXP_TILDEEXP 0x004 | |
11299 | #define WEXP_PARAMEXP 0x008 | |
11300 | #define WEXP_PATHEXP 0x010 | |
11301 | ||
11302 | /* All of the expansions, including variable assignments at the start of | |
11303 | the list. */ | |
11304 | #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP) | |
11305 | ||
11306 | /* All of the expansions except variable assignments at the start of | |
11307 | the list. */ | |
11308 | #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP) | |
11309 | ||
11310 | /* All of the `shell expansions': brace expansion, tilde expansion, parameter | |
11311 | expansion, command substitution, arithmetic expansion, word splitting, and | |
11312 | quote removal. */ | |
11313 | #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP) | |
11314 | ||
726f6388 JA |
11315 | /* Take the list of words in LIST and do the various substitutions. Return |
11316 | a new list of words which is the expanded list, and without things like | |
11317 | variable assignments. */ | |
11318 | ||
11319 | WORD_LIST * | |
11320 | expand_words (list) | |
11321 | WORD_LIST *list; | |
11322 | { | |
cce855bc | 11323 | return (expand_word_list_internal (list, WEXP_ALL)); |
726f6388 JA |
11324 | } |
11325 | ||
11326 | /* Same as expand_words (), but doesn't hack variable or environment | |
11327 | variables. */ | |
11328 | WORD_LIST * | |
11329 | expand_words_no_vars (list) | |
11330 | WORD_LIST *list; | |
11331 | { | |
cce855bc | 11332 | return (expand_word_list_internal (list, WEXP_NOVARS)); |
726f6388 JA |
11333 | } |
11334 | ||
cce855bc JA |
11335 | WORD_LIST * |
11336 | expand_words_shellexp (list) | |
726f6388 | 11337 | WORD_LIST *list; |
726f6388 | 11338 | { |
cce855bc JA |
11339 | return (expand_word_list_internal (list, WEXP_SHELLEXP)); |
11340 | } | |
726f6388 | 11341 | |
cce855bc JA |
11342 | static WORD_LIST * |
11343 | glob_expand_word_list (tlist, eflags) | |
11344 | WORD_LIST *tlist; | |
11345 | int eflags; | |
11346 | { | |
11347 | char **glob_array, *temp_string; | |
11348 | register int glob_index; | |
11349 | WORD_LIST *glob_list, *output_list, *disposables, *next; | |
11350 | WORD_DESC *tword; | |
712f80b0 | 11351 | int x; |
726f6388 | 11352 | |
cce855bc JA |
11353 | output_list = disposables = (WORD_LIST *)NULL; |
11354 | glob_array = (char **)NULL; | |
11355 | while (tlist) | |
11356 | { | |
11357 | /* For each word, either globbing is attempted or the word is | |
11358 | added to orig_list. If globbing succeeds, the results are | |
11359 | added to orig_list and the word (tlist) is added to the list | |
11360 | of disposable words. If globbing fails and failed glob | |
11361 | expansions are left unchanged (the shell default), the | |
11362 | original word is added to orig_list. If globbing fails and | |
11363 | failed glob expansions are removed, the original word is | |
11364 | added to the list of disposable words. orig_list ends up | |
7117c2d2 | 11365 | in reverse order and requires a call to REVERSE_LIST to |
cce855bc JA |
11366 | be set right. After all words are examined, the disposable |
11367 | words are freed. */ | |
11368 | next = tlist->next; | |
726f6388 | 11369 | |
cce855bc | 11370 | /* If the word isn't an assignment and contains an unquoted |
28ef6c31 | 11371 | pattern matching character, then glob it. */ |
b72432fd | 11372 | if ((tlist->word->flags & W_NOGLOB) == 0 && |
cce855bc | 11373 | unquoted_glob_pattern_p (tlist->word->word)) |
726f6388 | 11374 | { |
712f80b0 | 11375 | glob_array = shell_glob_filename (tlist->word->word, QGLOB_CTLESC); /* XXX */ |
cce855bc JA |
11376 | |
11377 | /* Handle error cases. | |
11378 | I don't think we should report errors like "No such file | |
11379 | or directory". However, I would like to report errors | |
11380 | like "Read failed". */ | |
11381 | ||
b80f6443 | 11382 | if (glob_array == 0 || GLOB_FAILED (glob_array)) |
726f6388 | 11383 | { |
bb70624e | 11384 | glob_array = (char **)xmalloc (sizeof (char *)); |
cce855bc JA |
11385 | glob_array[0] = (char *)NULL; |
11386 | } | |
11387 | ||
11388 | /* Dequote the current word in case we have to use it. */ | |
11389 | if (glob_array[0] == NULL) | |
11390 | { | |
11391 | temp_string = dequote_string (tlist->word->word); | |
11392 | free (tlist->word->word); | |
11393 | tlist->word->word = temp_string; | |
11394 | } | |
11395 | ||
11396 | /* Make the array into a word list. */ | |
11397 | glob_list = (WORD_LIST *)NULL; | |
11398 | for (glob_index = 0; glob_array[glob_index]; glob_index++) | |
11399 | { | |
11400 | tword = make_bare_word (glob_array[glob_index]); | |
cce855bc JA |
11401 | glob_list = make_word_list (tword, glob_list); |
11402 | } | |
11403 | ||
11404 | if (glob_list) | |
11405 | { | |
11406 | output_list = (WORD_LIST *)list_append (glob_list, output_list); | |
11407 | PREPEND_LIST (tlist, disposables); | |
11408 | } | |
b80f6443 JA |
11409 | else if (fail_glob_expansion != 0) |
11410 | { | |
ac50fbac | 11411 | last_command_exit_value = EXECUTION_FAILURE; |
b80f6443 | 11412 | report_error (_("no match: %s"), tlist->word->word); |
f1be666c | 11413 | exp_jump_to_top_level (DISCARD); |
b80f6443 | 11414 | } |
cce855bc JA |
11415 | else if (allow_null_glob_expansion == 0) |
11416 | { | |
11417 | /* Failed glob expressions are left unchanged. */ | |
11418 | PREPEND_LIST (tlist, output_list); | |
11419 | } | |
11420 | else | |
11421 | { | |
11422 | /* Failed glob expressions are removed. */ | |
11423 | PREPEND_LIST (tlist, disposables); | |
726f6388 | 11424 | } |
726f6388 | 11425 | } |
cce855bc JA |
11426 | else |
11427 | { | |
11428 | /* Dequote the string. */ | |
11429 | temp_string = dequote_string (tlist->word->word); | |
11430 | free (tlist->word->word); | |
11431 | tlist->word->word = temp_string; | |
11432 | PREPEND_LIST (tlist, output_list); | |
11433 | } | |
11434 | ||
7117c2d2 | 11435 | strvec_dispose (glob_array); |
cce855bc JA |
11436 | glob_array = (char **)NULL; |
11437 | ||
11438 | tlist = next; | |
726f6388 JA |
11439 | } |
11440 | ||
cce855bc JA |
11441 | if (disposables) |
11442 | dispose_words (disposables); | |
11443 | ||
11444 | if (output_list) | |
11445 | output_list = REVERSE_LIST (output_list, WORD_LIST *); | |
11446 | ||
11447 | return (output_list); | |
11448 | } | |
726f6388 JA |
11449 | |
11450 | #if defined (BRACE_EXPANSION) | |
cce855bc JA |
11451 | static WORD_LIST * |
11452 | brace_expand_word_list (tlist, eflags) | |
11453 | WORD_LIST *tlist; | |
11454 | int eflags; | |
11455 | { | |
11456 | register char **expansions; | |
11457 | char *temp_string; | |
11458 | WORD_LIST *disposables, *output_list, *next; | |
11459 | WORD_DESC *w; | |
11460 | int eindex; | |
11461 | ||
11462 | for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next) | |
726f6388 | 11463 | { |
cce855bc | 11464 | next = tlist->next; |
726f6388 | 11465 | |
ac50fbac CR |
11466 | if (tlist->word->flags & W_NOBRACE) |
11467 | { | |
11468 | /*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/ | |
11469 | PREPEND_LIST (tlist, output_list); | |
11470 | continue; | |
11471 | } | |
11472 | ||
0001803f CR |
11473 | if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG)) |
11474 | { | |
11475 | /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/ | |
11476 | PREPEND_LIST (tlist, output_list); | |
11477 | continue; | |
11478 | } | |
ac50fbac | 11479 | |
cce855bc JA |
11480 | /* Only do brace expansion if the word has a brace character. If |
11481 | not, just add the word list element to BRACES and continue. In | |
11482 | the common case, at least when running shell scripts, this will | |
0001803f | 11483 | degenerate to a bunch of calls to `mbschr', and then what is |
cce855bc | 11484 | basically a reversal of TLIST into BRACES, which is corrected |
7117c2d2 | 11485 | by a call to REVERSE_LIST () on BRACES when the end of TLIST |
cce855bc | 11486 | is reached. */ |
0001803f | 11487 | if (mbschr (tlist->word->word, LBRACE)) |
726f6388 | 11488 | { |
cce855bc | 11489 | expansions = brace_expand (tlist->word->word); |
726f6388 | 11490 | |
cce855bc | 11491 | for (eindex = 0; temp_string = expansions[eindex]; eindex++) |
726f6388 | 11492 | { |
ac50fbac CR |
11493 | w = alloc_word_desc (); |
11494 | w->word = temp_string; | |
11495 | ||
cce855bc JA |
11496 | /* If brace expansion didn't change the word, preserve |
11497 | the flags. We may want to preserve the flags | |
11498 | unconditionally someday -- XXX */ | |
11499 | if (STREQ (temp_string, tlist->word->word)) | |
11500 | w->flags = tlist->word->flags; | |
ac50fbac CR |
11501 | else |
11502 | w = make_word_flags (w, temp_string); | |
11503 | ||
cce855bc | 11504 | output_list = make_word_list (w, output_list); |
726f6388 | 11505 | } |
cce855bc | 11506 | free (expansions); |
726f6388 | 11507 | |
cce855bc JA |
11508 | /* Add TLIST to the list of words to be freed after brace |
11509 | expansion has been performed. */ | |
11510 | PREPEND_LIST (tlist, disposables); | |
11511 | } | |
11512 | else | |
11513 | PREPEND_LIST (tlist, output_list); | |
726f6388 | 11514 | } |
cce855bc JA |
11515 | |
11516 | if (disposables) | |
11517 | dispose_words (disposables); | |
11518 | ||
11519 | if (output_list) | |
11520 | output_list = REVERSE_LIST (output_list, WORD_LIST *); | |
11521 | ||
11522 | return (output_list); | |
11523 | } | |
11524 | #endif | |
11525 | ||
3185942a | 11526 | #if defined (ARRAY_VARS) |
712f80b0 CR |
11527 | /* Take WORD, a compound array assignment, and internally run (for example), |
11528 | 'declare -A w', where W is the variable name portion of WORD. OPTION is | |
11529 | the list of options to supply to `declare'. CMD is the declaration command | |
11530 | we are expanding right now; it's unused currently. */ | |
3185942a | 11531 | static int |
a0c0a00f | 11532 | make_internal_declare (word, option, cmd) |
3185942a JA |
11533 | char *word; |
11534 | char *option; | |
a0c0a00f | 11535 | char *cmd; |
3185942a | 11536 | { |
a0c0a00f | 11537 | int t, r; |
3185942a JA |
11538 | WORD_LIST *wl; |
11539 | WORD_DESC *w; | |
11540 | ||
11541 | w = make_word (word); | |
11542 | ||
11543 | t = assignment (w->word, 0); | |
a0c0a00f CR |
11544 | if (w->word[t] == '=') |
11545 | { | |
11546 | w->word[t] = '\0'; | |
11547 | if (w->word[t - 1] == '+') /* cut off any append op */ | |
11548 | w->word[t - 1] = '\0'; | |
11549 | } | |
3185942a JA |
11550 | |
11551 | wl = make_word_list (w, (WORD_LIST *)NULL); | |
11552 | wl = make_word_list (make_word (option), wl); | |
11553 | ||
a0c0a00f CR |
11554 | r = declare_builtin (wl); |
11555 | ||
11556 | dispose_words (wl); | |
11557 | return r; | |
3185942a | 11558 | } |
712f80b0 CR |
11559 | |
11560 | /* Expand VALUE in NAME[+]=( VALUE ) to a list of words. FLAGS is 1 if NAME | |
11561 | is an associative array. | |
11562 | ||
11563 | If we are processing an indexed array, expand_compound_array_assignment | |
11564 | will expand all the individual words and quote_compound_array_list will | |
11565 | single-quote them. If we are processing an associative array, we use | |
11566 | parse_string_to_word_list to split VALUE into a list of words instead of | |
11567 | faking up a shell variable and calling expand_compound_array_assignment. | |
11568 | expand_and_quote_assoc_word expands and single-quotes each word in VALUE | |
11569 | together so we don't have problems finding the end of the subscript when | |
11570 | quoting it. | |
11571 | ||
11572 | Words in VALUE can be individual words, which are expanded and single-quoted, | |
11573 | or words of the form [IND]=VALUE, which end up as explained below, as | |
11574 | ['expanded-ind']='expanded-value'. */ | |
11575 | ||
11576 | static WORD_LIST * | |
11577 | expand_oneword (value, flags) | |
11578 | char *value; | |
11579 | int flags; | |
11580 | { | |
11581 | WORD_LIST *l, *nl; | |
11582 | char *t; | |
11583 | ||
11584 | if (flags == 0) | |
11585 | { | |
11586 | /* Indexed array */ | |
11587 | l = expand_compound_array_assignment ((SHELL_VAR *)NULL, value, flags); | |
11588 | /* Now we quote the results of the expansion above to prevent double | |
11589 | expansion. */ | |
11590 | quote_compound_array_list (l, flags); | |
11591 | return l; | |
11592 | } | |
11593 | else | |
11594 | { | |
11595 | /* Associative array */ | |
11596 | l = parse_string_to_word_list (value, 1, "array assign"); | |
11597 | /* For associative arrays, with their arbitrary subscripts, we have to | |
11598 | expand and quote in one step so we don't have to search for the | |
11599 | closing right bracket more than once. */ | |
11600 | for (nl = l; nl; nl = nl->next) | |
11601 | { | |
11602 | if ((nl->word->flags & W_ASSIGNMENT) == 0) | |
11603 | t = sh_single_quote (nl->word->word ? nl->word->word : ""); | |
11604 | else | |
11605 | t = expand_and_quote_assoc_word (nl->word->word, flags); | |
11606 | free (nl->word->word); | |
11607 | nl->word->word = t; | |
11608 | } | |
11609 | return l; | |
11610 | } | |
11611 | } | |
11612 | ||
11613 | /* Expand a single compound assignment argument to a declaration builtin. | |
11614 | This word takes the form NAME[+]=( VALUE ). The NAME[+]= is passed through | |
11615 | unchanged. The VALUE is expanded and each word in the result is single- | |
11616 | quoted. Words of the form [key]=value end up as | |
11617 | ['expanded-key']='expanded-value'. Associative arrays have special | |
11618 | handling, see expand_oneword() above. The return value is | |
11619 | NAME[+]=( expanded-and-quoted-VALUE ). */ | |
11620 | static void | |
11621 | expand_compound_assignment_word (tlist, flags) | |
11622 | WORD_LIST *tlist; | |
11623 | int flags; | |
11624 | { | |
11625 | WORD_LIST *l; | |
11626 | int wlen, oind, t; | |
11627 | char *value, *temp; | |
11628 | ||
11629 | /*itrace("expand_compound_assignment_word: original word = -%s-", tlist->word->word);*/ | |
11630 | t = assignment (tlist->word->word, 0); | |
11631 | ||
11632 | /* value doesn't have the open and close parens */ | |
11633 | oind = 1; | |
11634 | value = extract_array_assignment_list (tlist->word->word + t + 1, &oind); | |
11635 | /* This performs one round of expansion on the index/key and value and | |
11636 | single-quotes each word in the result. */ | |
11637 | l = expand_oneword (value, flags); | |
11638 | free (value); | |
11639 | ||
11640 | value = string_list (l); | |
11641 | wlen = STRLEN (value); | |
11642 | ||
11643 | /* Now, let's rebuild the string */ | |
11644 | temp = xmalloc (t + 3 + wlen + 1); /* name[+]=(value) */ | |
11645 | memcpy (temp, tlist->word->word, ++t); | |
11646 | temp[t++] = '('; | |
11647 | if (value) | |
11648 | memcpy (temp + t, value, wlen); | |
11649 | t += wlen; | |
11650 | temp[t++] = ')'; | |
11651 | temp[t] = '\0'; | |
11652 | /*itrace("expand_compound_assignment_word: reconstructed word = -%s-", temp);*/ | |
11653 | ||
11654 | free (tlist->word->word); | |
11655 | tlist->word->word = temp; | |
11656 | ||
11657 | free (value); | |
11658 | } | |
11659 | ||
11660 | /* Expand and process an argument to a declaration command. We have already | |
11661 | set flags in TLIST->word->flags depending on the declaration command | |
11662 | (declare, local, etc.) and the options supplied to it (-a, -A, etc.). | |
11663 | TLIST->word->word is of the form NAME[+]=( VALUE ). | |
11664 | ||
11665 | This does several things, all using pieces of other functions to get the | |
11666 | evaluation sequence right. It's called for compound array assignments with | |
11667 | the W_ASSIGNMENT flag set (basically, valid identifier names on the lhs). | |
11668 | It parses out which flags need to be set for declare to create the variable | |
11669 | correctly, then calls declare internally (make_internal_declare) to make | |
11670 | sure the variable exists with the correct attributes. Before the variable | |
11671 | is created, it calls expand_compound_assignment_word to expand VALUE to a | |
11672 | list of words, appropriately quoted for further evaluation. This preserves | |
11673 | the semantics of word-expansion-before-calling-builtins. Finally, it calls | |
11674 | do_word_assignment to perform the expansion and assignment with the same | |
11675 | expansion semantics as a standalone assignment statement (no word splitting, | |
11676 | etc.) even though the word is single-quoted so all that needs to happen is | |
11677 | quote removal. */ | |
11678 | static WORD_LIST * | |
11679 | expand_declaration_argument (tlist, wcmd) | |
11680 | WORD_LIST *tlist, *wcmd; | |
11681 | { | |
11682 | char opts[16], omap[128]; | |
11683 | int t, opti, oind, skip, inheriting; | |
11684 | WORD_LIST *l; | |
11685 | ||
11686 | inheriting = localvar_inherit; | |
11687 | opti = 0; | |
11688 | if (tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL|W_CHKLOCAL|W_ASSIGNARRAY)) | |
11689 | opts[opti++] = '-'; | |
11690 | ||
11691 | if ((tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL)) == (W_ASSIGNASSOC|W_ASSNGLOBAL)) | |
11692 | { | |
11693 | opts[opti++] = 'g'; | |
11694 | opts[opti++] = 'A'; | |
11695 | } | |
11696 | else if (tlist->word->flags & W_ASSIGNASSOC) | |
11697 | { | |
11698 | opts[opti++] = 'A'; | |
11699 | } | |
11700 | else if ((tlist->word->flags & (W_ASSIGNARRAY|W_ASSNGLOBAL)) == (W_ASSIGNARRAY|W_ASSNGLOBAL)) | |
11701 | { | |
11702 | opts[opti++] = 'g'; | |
11703 | opts[opti++] = 'a'; | |
11704 | } | |
11705 | else if (tlist->word->flags & W_ASSIGNARRAY) | |
11706 | { | |
11707 | opts[opti++] = 'a'; | |
11708 | } | |
11709 | else if (tlist->word->flags & W_ASSNGLOBAL) | |
11710 | opts[opti++] = 'g'; | |
11711 | ||
11712 | if (tlist->word->flags & W_CHKLOCAL) | |
11713 | opts[opti++] = 'G'; | |
11714 | ||
11715 | /* If we have special handling note the integer attribute and others | |
11716 | that transform the value upon assignment. What we do is take all | |
11717 | of the option arguments and scan through them looking for options | |
11718 | that cause such transformations, and add them to the `opts' array. */ | |
11719 | ||
11720 | memset (omap, '\0', sizeof (omap)); | |
11721 | for (l = wcmd->next; l != tlist; l = l->next) | |
11722 | { | |
11723 | if (l->word->word[0] != '-') | |
11724 | break; /* non-option argument */ | |
11725 | if (l->word->word[0] == '-' && l->word->word[1] == '-' && l->word->word[2] == 0) | |
11726 | break; /* -- signals end of options */ | |
11727 | for (oind = 1; l->word->word[oind]; oind++) | |
11728 | switch (l->word->word[oind]) | |
11729 | { | |
11730 | case 'I': | |
11731 | inheriting = 1; | |
11732 | case 'i': | |
11733 | case 'l': | |
11734 | case 'u': | |
11735 | case 'c': | |
11736 | omap[l->word->word[oind]] = 1; | |
11737 | if (opti == 0) | |
11738 | opts[opti++] = '-'; | |
11739 | break; | |
11740 | default: | |
11741 | break; | |
11742 | } | |
11743 | } | |
11744 | ||
11745 | for (oind = 0; oind < sizeof (omap); oind++) | |
11746 | if (omap[oind]) | |
11747 | opts[opti++] = oind; | |
11748 | ||
11749 | /* If there are no -a/-A options, but we have a compound assignment, | |
11750 | we have a choice: we can set opts[0]='-', opts[1]='a', since the | |
11751 | default is to create an indexed array, and call | |
11752 | make_internal_declare with that, or we can just skip the -a and let | |
11753 | declare_builtin deal with it. Once we're here, we're better set | |
11754 | up for the latter, since we don't want to deal with looking up | |
11755 | any existing variable here -- better to let declare_builtin do it. | |
11756 | We need the variable created, though, especially if it's local, so | |
11757 | we get the scoping right before we call do_word_assignment. | |
11758 | To ensure that make_local_declare gets called, we add `--' if there | |
11759 | aren't any options. */ | |
11760 | if ((tlist->word->flags & (W_ASSIGNASSOC|W_ASSIGNARRAY)) == 0) | |
11761 | { | |
11762 | if (opti == 0) | |
11763 | { | |
11764 | opts[opti++] = '-'; | |
11765 | opts[opti++] = '-'; | |
11766 | } | |
11767 | } | |
11768 | opts[opti] = '\0'; | |
11769 | ||
11770 | /* This isn't perfect, but it's a start. Improvements later. We expand | |
11771 | tlist->word->word and single-quote the results to avoid multiple | |
11772 | expansions by, say, do_assignment_internal(). We have to weigh the | |
11773 | cost of reconstructing the compound assignment string with its single | |
11774 | quoting and letting the declare builtin handle it. The single quotes | |
11775 | will prevent any unwanted additional expansion or word splitting. */ | |
11776 | expand_compound_assignment_word (tlist, (tlist->word->flags & W_ASSIGNASSOC) ? 1 : 0); | |
11777 | ||
11778 | skip = 0; | |
11779 | if (opti > 0) | |
11780 | { | |
11781 | t = make_internal_declare (tlist->word->word, opts, wcmd ? wcmd->word->word : (char *)0); | |
11782 | if (t != EXECUTION_SUCCESS) | |
11783 | { | |
11784 | last_command_exit_value = t; | |
11785 | if (tlist->word->flags & W_FORCELOCAL) /* non-fatal error */ | |
11786 | skip = 1; | |
11787 | else | |
11788 | exp_jump_to_top_level (DISCARD); | |
11789 | } | |
11790 | } | |
11791 | ||
11792 | if (skip == 0) | |
11793 | { | |
11794 | t = do_word_assignment (tlist->word, 0); | |
11795 | if (t == 0) | |
11796 | { | |
11797 | last_command_exit_value = EXECUTION_FAILURE; | |
11798 | exp_jump_to_top_level (DISCARD); | |
11799 | } | |
11800 | } | |
11801 | ||
11802 | /* Now transform the word as ksh93 appears to do and go on */ | |
11803 | t = assignment (tlist->word->word, 0); | |
11804 | tlist->word->word[t] = '\0'; | |
11805 | if (tlist->word->word[t - 1] == '+') | |
11806 | tlist->word->word[t - 1] = '\0'; /* cut off append op */ | |
11807 | tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC|W_ASSIGNARRAY); | |
11808 | ||
11809 | return (tlist); | |
11810 | } | |
11811 | #endif /* ARRAY_VARS */ | |
3185942a | 11812 | |
cce855bc JA |
11813 | static WORD_LIST * |
11814 | shell_expand_word_list (tlist, eflags) | |
11815 | WORD_LIST *tlist; | |
11816 | int eflags; | |
11817 | { | |
a0c0a00f | 11818 | WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list, *wcmd; |
cce855bc | 11819 | int expanded_something, has_dollar_at; |
726f6388 | 11820 | |
726f6388 | 11821 | /* We do tilde expansion all the time. This is what 1003.2 says. */ |
712f80b0 | 11822 | wcmd = new_list = (WORD_LIST *)NULL; |
a0c0a00f | 11823 | |
cce855bc | 11824 | for (orig_list = tlist; tlist; tlist = next) |
726f6388 | 11825 | { |
712f80b0 CR |
11826 | if (wcmd == 0 && (tlist->word->flags & W_ASSNBLTIN)) |
11827 | wcmd = tlist; | |
11828 | ||
726f6388 JA |
11829 | next = tlist->next; |
11830 | ||
95732b49 JA |
11831 | #if defined (ARRAY_VARS) |
11832 | /* If this is a compound array assignment to a builtin that accepts | |
11833 | such assignments (e.g., `declare'), take the assignment and perform | |
11834 | it separately, handling the semantics of declarations inside shell | |
11835 | functions. This avoids the double-evaluation of such arguments, | |
11836 | because `declare' does some evaluation of compound assignments on | |
11837 | its own. */ | |
11838 | if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG)) | |
712f80b0 | 11839 | expand_declaration_argument (tlist, wcmd); |
95732b49 | 11840 | #endif |
726f6388 | 11841 | |
ccc6cda3 | 11842 | expanded_something = 0; |
726f6388 | 11843 | expanded = expand_word_internal |
b72432fd | 11844 | (tlist->word, 0, 0, &has_dollar_at, &expanded_something); |
726f6388 JA |
11845 | |
11846 | if (expanded == &expand_word_error || expanded == &expand_word_fatal) | |
11847 | { | |
11848 | /* By convention, each time this error is returned, | |
11849 | tlist->word->word has already been freed. */ | |
11850 | tlist->word->word = (char *)NULL; | |
ccc6cda3 | 11851 | |
726f6388 JA |
11852 | /* Dispose our copy of the original list. */ |
11853 | dispose_words (orig_list); | |
d166f048 | 11854 | /* Dispose the new list we're building. */ |
726f6388 JA |
11855 | dispose_words (new_list); |
11856 | ||
28ef6c31 | 11857 | last_command_exit_value = EXECUTION_FAILURE; |
726f6388 | 11858 | if (expanded == &expand_word_error) |
b80f6443 | 11859 | exp_jump_to_top_level (DISCARD); |
726f6388 | 11860 | else |
b80f6443 | 11861 | exp_jump_to_top_level (FORCE_EOF); |
726f6388 JA |
11862 | } |
11863 | ||
ccc6cda3 JA |
11864 | /* Don't split words marked W_NOSPLIT. */ |
11865 | if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0) | |
726f6388 | 11866 | { |
ccc6cda3 | 11867 | temp_list = word_list_split (expanded); |
726f6388 JA |
11868 | dispose_words (expanded); |
11869 | } | |
11870 | else | |
11871 | { | |
11872 | /* If no parameter expansion, command substitution, process | |
11873 | substitution, or arithmetic substitution took place, then | |
11874 | do not do word splitting. We still have to remove quoted | |
11875 | null characters from the result. */ | |
11876 | word_list_remove_quoted_nulls (expanded); | |
ccc6cda3 | 11877 | temp_list = expanded; |
726f6388 JA |
11878 | } |
11879 | ||
ccc6cda3 JA |
11880 | expanded = REVERSE_LIST (temp_list, WORD_LIST *); |
11881 | new_list = (WORD_LIST *)list_append (expanded, new_list); | |
726f6388 JA |
11882 | } |
11883 | ||
cce855bc JA |
11884 | if (orig_list) |
11885 | dispose_words (orig_list); | |
726f6388 | 11886 | |
726f6388 | 11887 | if (new_list) |
cce855bc | 11888 | new_list = REVERSE_LIST (new_list, WORD_LIST *); |
726f6388 | 11889 | |
cce855bc JA |
11890 | return (new_list); |
11891 | } | |
726f6388 | 11892 | |
cce855bc JA |
11893 | /* The workhorse for expand_words () and expand_words_no_vars (). |
11894 | First arg is LIST, a WORD_LIST of words. | |
b72432fd JA |
11895 | Second arg EFLAGS is a flags word controlling which expansions are |
11896 | performed. | |
726f6388 | 11897 | |
cce855bc JA |
11898 | This does all of the substitutions: brace expansion, tilde expansion, |
11899 | parameter expansion, command substitution, arithmetic expansion, | |
11900 | process substitution, word splitting, and pathname expansion, according | |
11901 | to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits | |
11902 | set, or for which no expansion is done, do not undergo word splitting. | |
ac50fbac CR |
11903 | Words with the W_NOGLOB bit set do not undergo pathname expansion; words |
11904 | with W_NOBRACE set do not undergo brace expansion (see | |
11905 | brace_expand_word_list above). */ | |
cce855bc JA |
11906 | static WORD_LIST * |
11907 | expand_word_list_internal (list, eflags) | |
11908 | WORD_LIST *list; | |
11909 | int eflags; | |
11910 | { | |
11911 | WORD_LIST *new_list, *temp_list; | |
11912 | int tint; | |
a0c0a00f | 11913 | char *savecmd; |
726f6388 | 11914 | |
ac50fbac | 11915 | tempenv_assign_error = 0; |
cce855bc JA |
11916 | if (list == 0) |
11917 | return ((WORD_LIST *)NULL); | |
726f6388 | 11918 | |
bb70624e | 11919 | garglist = new_list = copy_word_list (list); |
cce855bc JA |
11920 | if (eflags & WEXP_VARASSIGN) |
11921 | { | |
bb70624e | 11922 | garglist = new_list = separate_out_assignments (new_list); |
cce855bc JA |
11923 | if (new_list == 0) |
11924 | { | |
bb70624e | 11925 | if (subst_assign_varlist) |
cce855bc JA |
11926 | { |
11927 | /* All the words were variable assignments, so they are placed | |
11928 | into the shell's environment. */ | |
bb70624e | 11929 | for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next) |
cce855bc | 11930 | { |
a0c0a00f | 11931 | savecmd = this_command_name; |
cce855bc | 11932 | this_command_name = (char *)NULL; /* no arithmetic errors */ |
495aee44 | 11933 | tint = do_word_assignment (temp_list->word, 0); |
a0c0a00f | 11934 | this_command_name = savecmd; |
cce855bc | 11935 | /* Variable assignment errors in non-interactive shells |
d233b485 CR |
11936 | running in Posix.2 mode cause the shell to exit, unless |
11937 | they are being run by the `command' builtin. */ | |
28ef6c31 | 11938 | if (tint == 0) |
ccc6cda3 | 11939 | { |
cce855bc | 11940 | last_command_exit_value = EXECUTION_FAILURE; |
d233b485 | 11941 | if (interactive_shell == 0 && posixly_correct && executing_command_builtin == 0) |
b80f6443 | 11942 | exp_jump_to_top_level (FORCE_EOF); |
28ef6c31 | 11943 | else |
b80f6443 | 11944 | exp_jump_to_top_level (DISCARD); |
ccc6cda3 | 11945 | } |
726f6388 | 11946 | } |
bb70624e JA |
11947 | dispose_words (subst_assign_varlist); |
11948 | subst_assign_varlist = (WORD_LIST *)NULL; | |
cce855bc JA |
11949 | } |
11950 | return ((WORD_LIST *)NULL); | |
11951 | } | |
11952 | } | |
726f6388 | 11953 | |
cce855bc JA |
11954 | /* Begin expanding the words that remain. The expansions take place on |
11955 | things that aren't really variable assignments. */ | |
726f6388 | 11956 | |
cce855bc JA |
11957 | #if defined (BRACE_EXPANSION) |
11958 | /* Do brace expansion on this word if there are any brace characters | |
11959 | in the string. */ | |
11960 | if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list) | |
11961 | new_list = brace_expand_word_list (new_list, eflags); | |
11962 | #endif /* BRACE_EXPANSION */ | |
726f6388 | 11963 | |
cce855bc JA |
11964 | /* Perform the `normal' shell expansions: tilde expansion, parameter and |
11965 | variable substitution, command substitution, arithmetic expansion, | |
11966 | and word splitting. */ | |
11967 | new_list = shell_expand_word_list (new_list, eflags); | |
726f6388 | 11968 | |
cce855bc JA |
11969 | /* Okay, we're almost done. Now let's just do some filename |
11970 | globbing. */ | |
11971 | if (new_list) | |
11972 | { | |
11973 | if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0) | |
11974 | /* Glob expand the word list unless globbing has been disabled. */ | |
11975 | new_list = glob_expand_word_list (new_list, eflags); | |
726f6388 | 11976 | else |
cce855bc JA |
11977 | /* Dequote the words, because we're not performing globbing. */ |
11978 | new_list = dequote_list (new_list); | |
726f6388 JA |
11979 | } |
11980 | ||
bb70624e | 11981 | if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist) |
726f6388 | 11982 | { |
95732b49 | 11983 | sh_wassign_func_t *assign_func; |
495aee44 | 11984 | int is_special_builtin, is_builtin_or_func; |
726f6388 JA |
11985 | |
11986 | /* If the remainder of the words expand to nothing, Posix.2 requires | |
11987 | that the variable and environment assignments affect the shell's | |
11988 | environment. */ | |
95732b49 | 11989 | assign_func = new_list ? assign_in_env : do_word_assignment; |
b80f6443 | 11990 | tempenv_assign_error = 0; |
726f6388 | 11991 | |
495aee44 CR |
11992 | is_builtin_or_func = (new_list && new_list->word && (find_shell_builtin (new_list->word->word) || find_function (new_list->word->word))); |
11993 | /* Posix says that special builtins exit if a variable assignment error | |
11994 | occurs in an assignment preceding it. */ | |
11995 | is_special_builtin = (posixly_correct && new_list && new_list->word && find_special_builtin (new_list->word->word)); | |
11996 | ||
bb70624e | 11997 | for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next) |
726f6388 | 11998 | { |
a0c0a00f | 11999 | savecmd = this_command_name; |
ccc6cda3 | 12000 | this_command_name = (char *)NULL; |
3185942a | 12001 | assigning_in_environment = (assign_func == assign_in_env); |
495aee44 | 12002 | tint = (*assign_func) (temp_list->word, is_builtin_or_func); |
3185942a | 12003 | assigning_in_environment = 0; |
a0c0a00f | 12004 | this_command_name = savecmd; |
ccc6cda3 JA |
12005 | /* Variable assignment errors in non-interactive shells running |
12006 | in Posix.2 mode cause the shell to exit. */ | |
b80f6443 | 12007 | if (tint == 0) |
ccc6cda3 | 12008 | { |
95732b49 | 12009 | if (assign_func == do_word_assignment) |
b80f6443 JA |
12010 | { |
12011 | last_command_exit_value = EXECUTION_FAILURE; | |
d233b485 | 12012 | if (interactive_shell == 0 && posixly_correct) |
b80f6443 JA |
12013 | exp_jump_to_top_level (FORCE_EOF); |
12014 | else | |
12015 | exp_jump_to_top_level (DISCARD); | |
12016 | } | |
d233b485 CR |
12017 | else if (interactive_shell == 0 && is_special_builtin) |
12018 | { | |
12019 | last_command_exit_value = EXECUTION_FAILURE; | |
12020 | exp_jump_to_top_level (FORCE_EOF); | |
12021 | } | |
28ef6c31 | 12022 | else |
b80f6443 | 12023 | tempenv_assign_error++; |
ccc6cda3 | 12024 | } |
726f6388 | 12025 | } |
726f6388 | 12026 | |
bb70624e JA |
12027 | dispose_words (subst_assign_varlist); |
12028 | subst_assign_varlist = (WORD_LIST *)NULL; | |
726f6388 JA |
12029 | } |
12030 | ||
cce855bc | 12031 | return (new_list); |
ccc6cda3 | 12032 | } |