]> git.ipfire.org Git - thirdparty/bash.git/blob - subst.c
Bash-5.1 patch 14: fix off-by-one error when reading multibyte characters from comman...
[thirdparty/bash.git] / subst.c
1 /* subst.c -- The part of the shell that does parameter, command, arithmetic,
2 and globbing substitutions. */
3
4 /* ``Have a little faith, there's magic in the night. You ain't a
5 beauty, but, hey, you're alright.'' */
6
7 /* Copyright (C) 1987-2020 Free Software Foundation, Inc.
8
9 This file is part of GNU Bash, the Bourne Again SHell.
10
11 Bash is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 Bash is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "config.h"
26
27 #include "bashtypes.h"
28 #include <stdio.h>
29 #include "chartypes.h"
30 #if defined (HAVE_PWD_H)
31 # include <pwd.h>
32 #endif
33 #include <signal.h>
34 #include <errno.h>
35
36 #if defined (HAVE_UNISTD_H)
37 # include <unistd.h>
38 #endif
39
40 #define NEED_FPURGE_DECL
41
42 #include "bashansi.h"
43 #include "posixstat.h"
44 #include "bashintl.h"
45
46 #include "shell.h"
47 #include "parser.h"
48 #include "flags.h"
49 #include "jobs.h"
50 #include "execute_cmd.h"
51 #include "filecntl.h"
52 #include "trap.h"
53 #include "pathexp.h"
54 #include "mailcheck.h"
55
56 #include "shmbutil.h"
57 #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
58 # include <mbstr.h> /* mbschr */
59 #endif
60 #include "typemax.h"
61
62 #include "builtins/getopt.h"
63 #include "builtins/common.h"
64
65 #include "builtins/builtext.h"
66
67 #include <tilde/tilde.h>
68 #include <glob/strmatch.h>
69
70 #if !defined (errno)
71 extern int errno;
72 #endif /* !errno */
73
74 /* The size that strings change by. */
75 #define DEFAULT_INITIAL_ARRAY_SIZE 112
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
82 #define VT_ARRAYMEMBER 3
83 #define VT_ASSOCVAR 4
84
85 #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */
86
87 /* Flags for quoted_strchr */
88 #define ST_BACKSL 0x01
89 #define ST_CTLESC 0x02
90 #define ST_SQUOTE 0x04 /* unused yet */
91 #define ST_DQUOTE 0x08 /* unused yet */
92
93 /* These defs make it easier to use the editor. */
94 #define LBRACE '{'
95 #define RBRACE '}'
96 #define LPAREN '('
97 #define RPAREN ')'
98 #define LBRACK '['
99 #define RBRACK ']'
100
101 #if defined (HANDLE_MULTIBYTE)
102 #define WLPAREN L'('
103 #define WRPAREN L')'
104 #endif
105
106 #define DOLLAR_AT_STAR(c) ((c) == '@' || (c) == '*')
107 #define STR_DOLLAR_AT_STAR(s) (DOLLAR_AT_STAR ((s)[0]) && (s)[1] == '\0')
108
109 /* Evaluates to 1 if C is one of the shell's special parameters whose length
110 can be taken, but is also one of the special expansion characters. */
111 #define VALID_SPECIAL_LENGTH_PARAM(c) \
112 ((c) == '-' || (c) == '?' || (c) == '#' || (c) == '@')
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) \
117 ((posixly_correct == 0 && (c) == '#') || (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*')
118
119 /* Evaluates to 1 if C is one of the OP characters that follows the parameter
120 in ${parameter[:]OPword}. */
121 #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
122
123 /* Evaluates to 1 if this is one of the shell's special variables. */
124 #define SPECIAL_VAR(name, wi) \
125 (*name && ((DIGIT (*name) && all_digits (name)) || \
126 (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
127 (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1]))))
128
129 /* This can be used by all of the *_extract_* functions that have a similar
130 structure. It can't just be wrapped in a do...while(0) loop because of
131 the embedded `break'. The dangling else accommodates a trailing semicolon;
132 we could also put in a do ; while (0) */
133
134 #define CHECK_STRING_OVERRUN(oind, ind, len, ch) \
135 if (ind >= len) \
136 { \
137 oind = len; \
138 ch = 0; \
139 break; \
140 } \
141 else \
142
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(). */
146 typedef WORD_LIST *EXPFUNC PARAMS((char *, int));
147
148 /* Process ID of the last command executed within command substitution. */
149 pid_t last_command_subst_pid = NO_PID;
150 pid_t current_command_subst_pid = NO_PID;
151
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];
156 int ifs_is_set, ifs_is_null;
157
158 #if defined (HANDLE_MULTIBYTE)
159 unsigned char ifs_firstc[MB_LEN_MAX];
160 size_t ifs_firstc_len;
161 #else
162 unsigned char ifs_firstc;
163 #endif
164
165 /* If non-zero, command substitution inherits the value of errexit option */
166 int inherit_errexit = 0;
167
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. */
171 int assigning_in_environment;
172
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
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
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
189 /* Extern functions and variables from different files. */
190 extern struct fd_bitmap *current_fds_to_close;
191 extern int wordexp_only;
192
193 #if defined (JOB_CONTROL) && defined (PROCESS_SUBSTITUTION)
194 extern PROCESS *last_procsub_child;
195 #endif
196
197 #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
198 extern wchar_t *wcsdup PARAMS((const wchar_t *));
199 #endif
200
201 #if 0
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
204 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
205 (CURRENTLY UNUSED). */
206 char *glob_argv_flags;
207 static int glob_argv_flags_size;
208 #endif
209
210 static WORD_LIST *cached_quoted_dollar_at = 0;
211
212 /* Distinguished error values to return from expansion functions */
213 static WORD_LIST expand_word_error, expand_word_fatal;
214 static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
215 static char expand_param_error, expand_param_fatal, expand_param_unset;
216 static char extract_string_error, extract_string_fatal;
217
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. */
223 static int expand_no_split_dollar_star = 0;
224
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;
228
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));
232
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 *));
240
241 static char *quote_escapes_internal PARAMS((const char *, int));
242
243 static WORD_LIST *list_quote_escapes PARAMS((WORD_LIST *));
244 static WORD_LIST *list_dequote_escapes PARAMS((WORD_LIST *));
245
246 static char *make_quoted_char PARAMS((int));
247 static WORD_LIST *quote_list PARAMS((WORD_LIST *));
248
249 static int unquoted_substring PARAMS((char *, char *));
250 static int unquoted_member PARAMS((int, char *));
251
252 #if defined (ARRAY_VARS)
253 static SHELL_VAR *do_compound_assignment PARAMS((char *, char *, int));
254 #endif
255 static int do_assignment_internal PARAMS((const WORD_DESC *, int));
256
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));
266
267 static char *pos_params PARAMS((char *, int, int, int, int));
268
269 static unsigned char *mb_getcharlens PARAMS((char *, int));
270
271 static char *remove_upattern PARAMS((char *, char *, int));
272 #if defined (HANDLE_MULTIBYTE)
273 static wchar_t *remove_wpattern PARAMS((wchar_t *, size_t, wchar_t *, int));
274 #endif
275 static char *remove_pattern PARAMS((char *, char *, int));
276
277 static int match_upattern PARAMS((char *, char *, int, char **, char **));
278 #if defined (HANDLE_MULTIBYTE)
279 static int match_wpattern PARAMS((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
280 #endif
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));
287 #ifdef ARRAY_VARS
288 static char *array_remove_pattern PARAMS((SHELL_VAR *, char *, int, int, int));
289 #endif
290 static char *parameter_brace_remove_pattern PARAMS((char *, char *, int, char *, int, int, int));
291
292 static char *string_var_assignment PARAMS((SHELL_VAR *, char *));
293 #if defined (ARRAY_VARS)
294 static char *array_var_assignment PARAMS((SHELL_VAR *, int, int, int));
295 #endif
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));
300 #if defined ARRAY_VARS
301 static char *array_transform PARAMS((int, SHELL_VAR *, int, int));
302 #endif
303 static char *parameter_brace_transform PARAMS((char *, char *, int, char *, int, int, int, int));
304 static int valid_parameter_transform PARAMS((char *));
305
306 static char *process_substitute PARAMS((char *, int));
307
308 static char *read_comsub PARAMS((int, int, int, int *));
309
310 #ifdef ARRAY_VARS
311 static arrayind_t array_length_reference PARAMS((char *));
312 #endif
313
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));
317
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));
323
324 static int valid_length_expression PARAMS((char *));
325 static intmax_t parameter_brace_expand_length PARAMS((char *));
326
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));
332
333 static int shouldexp_replacement PARAMS((char *));
334
335 static char *pos_params_pat_subst PARAMS((char *, char *, char *, int));
336
337 static char *parameter_brace_patsub PARAMS((char *, char *, int, char *, int, int, int));
338
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));
341
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));
344
345 static WORD_LIST *expand_word_internal PARAMS((WORD_DESC *, int, int, int *, int *));
346
347 static WORD_LIST *word_list_split PARAMS((WORD_LIST *));
348
349 static void exp_jump_to_top_level PARAMS((int));
350
351 static WORD_LIST *separate_out_assignments PARAMS((WORD_LIST *));
352 static WORD_LIST *glob_expand_word_list PARAMS((WORD_LIST *, int));
353 #ifdef BRACE_EXPANSION
354 static WORD_LIST *brace_expand_word_list PARAMS((WORD_LIST *, int));
355 #endif
356 #if defined (ARRAY_VARS)
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 *));
360 #endif
361 static WORD_LIST *shell_expand_word_list PARAMS((WORD_LIST *, int));
362 static WORD_LIST *expand_word_list_internal PARAMS((WORD_LIST *, int));
363
364 /* **************************************************************** */
365 /* */
366 /* Utility Functions */
367 /* */
368 /* **************************************************************** */
369
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);
379 if (f & W_ARRAYIND)
380 {
381 f &= ~W_ARRAYIND;
382 fprintf (stderr, "W_ARRAYIND%s", f ? "|" : "");
383 }
384 if (f & W_ASSIGNASSOC)
385 {
386 f &= ~W_ASSIGNASSOC;
387 fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : "");
388 }
389 if (f & W_ASSIGNARRAY)
390 {
391 f &= ~W_ASSIGNARRAY;
392 fprintf (stderr, "W_ASSIGNARRAY%s", f ? "|" : "");
393 }
394 if (f & W_SAWQUOTEDNULL)
395 {
396 f &= ~W_SAWQUOTEDNULL;
397 fprintf (stderr, "W_SAWQUOTEDNULL%s", f ? "|" : "");
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 }
424 if (f & W_ASSNGLOBAL)
425 {
426 f &= ~W_ASSNGLOBAL;
427 fprintf (stderr, "W_ASSNGLOBAL%s", f ? "|" : "");
428 }
429 if (f & W_COMPASSIGN)
430 {
431 f &= ~W_COMPASSIGN;
432 fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : "");
433 }
434 if (f & W_EXPANDRHS)
435 {
436 f &= ~W_EXPANDRHS;
437 fprintf (stderr, "W_EXPANDRHS%s", f ? "|" : "");
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 }
454 if (f & W_NOASSNTILDE)
455 {
456 f &= ~W_NOASSNTILDE;
457 fprintf (stderr, "W_NOASSNTILDE%s", f ? "|" : "");
458 }
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 }
484 if (f & W_NOSPLIT)
485 {
486 f &= ~W_NOSPLIT;
487 fprintf (stderr, "W_NOSPLIT%s", f ? "|" : "");
488 }
489 if (f & W_NOBRACE)
490 {
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 ? "|" : "");
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 }
519 if (f & W_COMPLETE)
520 {
521 f &= ~W_COMPLETE;
522 fprintf (stderr, "W_COMPLETE%s", f ? "|" : "");
523 }
524 if (f & W_CHKLOCAL)
525 {
526 f &= ~W_CHKLOCAL;
527 fprintf (stderr, "W_CHKLOCAL%s", f ? "|" : "");
528 }
529 if (f & W_FORCELOCAL)
530 {
531 f &= ~W_FORCELOCAL;
532 fprintf (stderr, "W_FORCELOCAL%s", f ? "|" : "");
533 }
534
535 fprintf (stderr, "\n");
536 fflush (stderr);
537 }
538 #endif
539
540 #ifdef INCLUDE_UNUSED
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 {
556 s++;
557 continue;
558 }
559 l++;
560 if (*s == 0)
561 break;
562 }
563
564 r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */
565
566 /* Copy LEN characters, including quote characters. */
567 s = string + l;
568 for (l = 0; l < len; s++)
569 {
570 if (*s == CTLESC)
571 *r++ = *s++;
572 *r++ = *s;
573 l++;
574 if (*s == 0)
575 break;
576 }
577 *r = '\0';
578 return result;
579 }
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
606
607 #ifdef INCLUDE_UNUSED
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. */
612 static char *
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
635 /* Return 1 if CHARACTER appears in an unquoted portion of
636 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
637 static int
638 unquoted_member (character, string)
639 int character;
640 char *string;
641 {
642 size_t slen;
643 int sindex, c;
644 DECLARE_MBSTATE;
645
646 slen = strlen (string);
647 sindex = 0;
648 while (c = string[sindex])
649 {
650 if (c == character)
651 return (1);
652
653 switch (c)
654 {
655 default:
656 ADVANCE_CHAR (string, slen, sindex);
657 break;
658
659 case '\\':
660 sindex++;
661 if (string[sindex])
662 ADVANCE_CHAR (string, slen, sindex);
663 break;
664
665 case '\'':
666 sindex = skip_single_quoted (string, slen, ++sindex, 0);
667 break;
668
669 case '"':
670 sindex = skip_double_quoted (string, slen, ++sindex, 0);
671 break;
672 }
673 }
674 return (0);
675 }
676
677 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
678 static int
679 unquoted_substring (substr, string)
680 char *substr, *string;
681 {
682 size_t slen;
683 int sindex, c, sublen;
684 DECLARE_MBSTATE;
685
686 if (substr == 0 || *substr == '\0')
687 return (0);
688
689 slen = strlen (string);
690 sublen = strlen (substr);
691 for (sindex = 0; c = string[sindex]; )
692 {
693 if (STREQN (string + sindex, substr, sublen))
694 return (1);
695
696 switch (c)
697 {
698 case '\\':
699 sindex++;
700 if (string[sindex])
701 ADVANCE_CHAR (string, slen, sindex);
702 break;
703
704 case '\'':
705 sindex = skip_single_quoted (string, slen, ++sindex, 0);
706 break;
707
708 case '"':
709 sindex = skip_double_quoted (string, slen, ++sindex, 0);
710 break;
711
712 default:
713 ADVANCE_CHAR (string, slen, sindex);
714 break;
715 }
716 }
717 return (0);
718 }
719 #endif
720
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. */
728
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. */
733 INLINE char *
734 sub_append_string (source, target, indx, size)
735 char *source, *target;
736 int *indx;
737 size_t *size;
738 {
739 if (source)
740 {
741 int n;
742 size_t srclen;
743
744 srclen = STRLEN (source);
745 if (srclen >= (int)(*size - *indx))
746 {
747 n = srclen + *indx;
748 n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
749 target = (char *)xrealloc (target, (*size = n));
750 }
751
752 FASTCOPY (source, target + *indx, srclen);
753 *indx += srclen;
754 target[*indx] = '\0';
755
756 free (source);
757 }
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)
767 intmax_t number;
768 char *target;
769 int *indx;
770 size_t *size;
771 {
772 char *temp;
773
774 temp = itos (number);
775 return (sub_append_string (temp, target, indx, size));
776 }
777 #endif
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.
782 Understand about backslashes in the string. If (flags & SX_VARNAME)
783 is non-zero, and array variables have been compiled into the shell,
784 everything between a `[' and a corresponding `]' is skipped over.
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
787 contain a closing character from CHARLIST. */
788 static char *
789 string_extract (string, sindex, charlist, flags)
790 char *string;
791 int *sindex;
792 char *charlist;
793 int flags;
794 {
795 register int c, i;
796 int found;
797 size_t slen;
798 char *temp;
799 DECLARE_MBSTATE;
800
801 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
802 i = *sindex;
803 found = 0;
804 while (c = string[i])
805 {
806 if (c == '\\')
807 {
808 if (string[i + 1])
809 i++;
810 else
811 break;
812 }
813 #if defined (ARRAY_VARS)
814 else if ((flags & SX_VARNAME) && c == LBRACK)
815 {
816 int ni;
817 /* If this is an array subscript, skip over it and continue. */
818 ni = skipsubscript (string, i, 0);
819 if (string[ni] == RBRACK)
820 i = ni;
821 }
822 #endif
823 else if (MEMBER (c, charlist))
824 {
825 found = 1;
826 break;
827 }
828
829 ADVANCE_CHAR (string, slen, i);
830 }
831
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. */
834 if ((flags & SX_REQMATCH) && found == 0)
835 {
836 *sindex = i;
837 return (&extract_string_error);
838 }
839
840 temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
841 *sindex = i;
842
843 return (temp);
844 }
845
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. */
853 static char *
854 string_extract_double_quoted (string, sindex, flags)
855 char *string;
856 int *sindex, flags;
857 {
858 size_t slen;
859 char *send;
860 int j, i, t;
861 unsigned char c;
862 char *temp, *ret; /* The new string we return. */
863 int pass_next, backquote, si; /* State variables for the machine. */
864 int dquote;
865 int stripdq;
866 DECLARE_MBSTATE;
867
868 slen = strlen (string + *sindex) + *sindex;
869 send = string + slen;
870
871 stripdq = (flags & SX_STRIPDQ);
872
873 pass_next = backquote = dquote = 0;
874 temp = (char *)xmalloc (1 + slen - *sindex);
875
876 j = 0;
877 i = *sindex;
878 while (c = string[i])
879 {
880 /* Process a character that was quoted by a backslash. */
881 if (pass_next)
882 {
883 /* XXX - take another look at this in light of Interp 221 */
884 /* Posix.2 sez:
885
886 ``The backslash shall retain its special meaning as an escape
887 character only when followed by one of the characters:
888 $ ` " \ <newline>''.
889
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
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. */
906 if ((stripdq == 0 && c != '"') ||
907 (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
908 temp[j++] = '\\';
909 pass_next = 0;
910
911 add_one_character:
912 COPY_CHAR_I (temp, j, string, send, i);
913 continue;
914 }
915
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 == '\\')
920 {
921 pass_next++;
922 i++;
923 continue;
924 }
925
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)
931 {
932 if (c == '`')
933 backquote = 0;
934 temp[j++] = c; /* COPY_CHAR_I? */
935 i++;
936 continue;
937 }
938
939 if (c == '`')
940 {
941 temp[j++] = c;
942 backquote++;
943 i++;
944 continue;
945 }
946
947 /* Pass everything between `$(' and the matching `)' or a quoted
948 ${ ... } pair through according to the Posix.2 specification. */
949 if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
950 {
951 int free_ret = 1;
952
953 si = i + 2;
954 if (string[i + 1] == LPAREN)
955 ret = extract_command_subst (string, &si, (flags & SX_COMPLETE));
956 else
957 ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, 0);
958
959 temp[j++] = '$';
960 temp[j++] = string[i + 1];
961
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
970 /* XXX - CHECK_STRING_OVERRUN here? */
971 for (t = 0; ret[t]; t++, j++)
972 temp[j] = ret[t];
973 temp[j] = string[si];
974
975 if (si < i + 2) /* we went back? */
976 i += 2;
977 else if (string[si])
978 {
979 j++;
980 i = si + 1;
981 }
982 else
983 i = si;
984
985 if (free_ret)
986 free (ret);
987 continue;
988 }
989
990 /* Add any character but a double quote to the quoted string we're
991 accumulating. */
992 if (c != '"')
993 goto add_one_character;
994
995 /* c == '"' */
996 if (stripdq)
997 {
998 dquote ^= 1;
999 i++;
1000 continue;
1001 }
1002
1003 break;
1004 }
1005 temp[j] = '\0';
1006
1007 /* Point to after the closing quote. */
1008 if (c)
1009 i++;
1010 *sindex = i;
1011
1012 return (temp);
1013 }
1014
1015 /* This should really be another option to string_extract_double_quoted. */
1016 static int
1017 skip_double_quoted (string, slen, sind, flags)
1018 char *string;
1019 size_t slen;
1020 int sind;
1021 int flags;
1022 {
1023 int c, i;
1024 char *ret;
1025 int pass_next, backquote, si;
1026 DECLARE_MBSTATE;
1027
1028 pass_next = backquote = 0;
1029 i = sind;
1030 while (c = string[i])
1031 {
1032 if (pass_next)
1033 {
1034 pass_next = 0;
1035 ADVANCE_CHAR (string, slen, i);
1036 continue;
1037 }
1038 else if (c == '\\')
1039 {
1040 pass_next++;
1041 i++;
1042 continue;
1043 }
1044 else if (backquote)
1045 {
1046 if (c == '`')
1047 backquote = 0;
1048 ADVANCE_CHAR (string, slen, i);
1049 continue;
1050 }
1051 else if (c == '`')
1052 {
1053 backquote++;
1054 i++;
1055 continue;
1056 }
1057 else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
1058 {
1059 si = i + 2;
1060 if (string[i + 1] == LPAREN)
1061 ret = extract_command_subst (string, &si, SX_NOALLOC|(flags&SX_COMPLETE));
1062 else
1063 ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, SX_NOALLOC);
1064
1065 /* These can consume the entire string if they are unterminated */
1066 CHECK_STRING_OVERRUN (i, si, slen, c);
1067
1068 i = si + 1;
1069 continue;
1070 }
1071 else if (c != '"')
1072 {
1073 ADVANCE_CHAR (string, slen, i);
1074 continue;
1075 }
1076 else
1077 break;
1078 }
1079
1080 if (c)
1081 i++;
1082
1083 return (i);
1084 }
1085
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 {
1095 register int i;
1096 size_t slen;
1097 char *t;
1098 DECLARE_MBSTATE;
1099
1100 /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
1101 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
1102 i = *sindex;
1103 while (string[i] && string[i] != '\'')
1104 ADVANCE_CHAR (string, slen, i);
1105
1106 t = substring (string, *sindex, i);
1107
1108 if (string[i])
1109 i++;
1110 *sindex = i;
1111
1112 return (t);
1113 }
1114
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. */
1118 static inline int
1119 skip_single_quoted (string, slen, sind, flags)
1120 const char *string;
1121 size_t slen;
1122 int sind;
1123 int flags;
1124 {
1125 register int c;
1126 DECLARE_MBSTATE;
1127
1128 c = sind;
1129 while (string[c] && string[c] != '\'')
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 }
1135
1136 if (string[c])
1137 c++;
1138 return c;
1139 }
1140
1141 /* Just like string_extract, but doesn't hack backslashes or any of
1142 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
1143 static char *
1144 string_extract_verbatim (string, slen, sindex, charlist, flags)
1145 char *string;
1146 size_t slen;
1147 int *sindex;
1148 char *charlist;
1149 int flags;
1150 {
1151 register int i;
1152 #if defined (HANDLE_MULTIBYTE)
1153 wchar_t *wcharlist;
1154 #endif
1155 int c;
1156 char *temp;
1157 DECLARE_MBSTATE;
1158
1159 if ((flags & SX_NOCTLESC) && charlist[0] == '\'' && charlist[1] == '\0')
1160 {
1161 temp = string_extract_single_quoted (string, sindex);
1162 --*sindex; /* leave *sindex at separator character */
1163 return temp;
1164 }
1165
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
1178 i = *sindex;
1179 #if defined (HANDLE_MULTIBYTE)
1180 wcharlist = 0;
1181 #endif
1182 while (c = string[i])
1183 {
1184 #if defined (HANDLE_MULTIBYTE)
1185 size_t mblength;
1186 #endif
1187 if ((flags & SX_NOCTLESC) == 0 && c == CTLESC)
1188 {
1189 i += 2;
1190 CHECK_STRING_OVERRUN (i, i, slen, c);
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)
1197 {
1198 i += 2;
1199 CHECK_STRING_OVERRUN (i, i, slen, c);
1200 continue;
1201 }
1202
1203 #if defined (HANDLE_MULTIBYTE)
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);
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;
1225 wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1));
1226 mbstowcs (wcharlist, charlist, len + 1);
1227 }
1228
1229 if (wcschr (wcharlist, wc))
1230 break;
1231 }
1232 }
1233 else
1234 #endif
1235 if (MEMBER (c, charlist))
1236 break;
1237
1238 ADVANCE_CHAR (string, slen, i);
1239 }
1240
1241 #if defined (HANDLE_MULTIBYTE)
1242 FREE (wcharlist);
1243 #endif
1244
1245 temp = substring (string, *sindex, i);
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 "$(".
1253 Make (SINDEX) get the position of the matching ")". )
1254 XFLAGS is additional flags to pass to other extraction functions. */
1255 char *
1256 extract_command_subst (string, sindex, xflags)
1257 char *string;
1258 int *sindex;
1259 int xflags;
1260 {
1261 char *ret;
1262
1263 if (string[*sindex] == LPAREN || (xflags & SX_COMPLETE))
1264 return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/
1265 else
1266 {
1267 xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
1268 ret = xparse_dolparen (string, string+*sindex, sindex, xflags);
1269 return ret;
1270 }
1271 }
1272
1273 /* Extract the $[ construct in STRING, and return a new string. (])
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 {
1281 return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/
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 "<(".
1287 Make (SINDEX) get the position of the matching ")". */ /*))*/
1288 char *
1289 extract_process_subst (string, starter, sindex, xflags)
1290 char *string;
1291 char *starter;
1292 int *sindex;
1293 int xflags;
1294 {
1295 #if 0
1296 /* XXX - check xflags&SX_COMPLETE here? */
1297 return (extract_delimited_string (string, sindex, starter, "(", ")", SX_COMMAND));
1298 #else
1299 xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
1300 return (xparse_dolparen (string, string+*sindex, sindex, xflags));
1301 #endif
1302 }
1303 #endif /* PROCESS_SUBSTITUTION */
1304
1305 #if defined (ARRAY_VARS)
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. */
1309 char *
1310 extract_array_assignment_list (string, sindex)
1311 char *string;
1312 int *sindex;
1313 {
1314 int slen;
1315 char *ret;
1316
1317 slen = strlen (string);
1318 if (string[slen - 1] == RPAREN)
1319 {
1320 ret = substring (string, *sindex, slen - 1);
1321 *sindex = slen - 1;
1322 return ret;
1323 }
1324 return 0;
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 *
1337 extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
1338 char *string;
1339 int *sindex;
1340 char *opener, *alt_opener, *closer;
1341 int flags;
1342 {
1343 int i, c, si;
1344 size_t slen;
1345 char *t, *result;
1346 int pass_character, nesting_level, in_comment;
1347 int len_closer, len_opener, len_alt_opener;
1348 DECLARE_MBSTATE;
1349
1350 slen = strlen (string + *sindex) + *sindex;
1351 len_opener = STRLEN (opener);
1352 len_alt_opener = STRLEN (alt_opener);
1353 len_closer = STRLEN (closer);
1354
1355 pass_character = in_comment = 0;
1356
1357 nesting_level = 1;
1358 i = *sindex;
1359
1360 while (nesting_level)
1361 {
1362 c = string[i];
1363
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
1373 if (c == 0)
1374 break;
1375
1376 if (in_comment)
1377 {
1378 if (c == '\n')
1379 in_comment = 0;
1380 ADVANCE_CHAR (string, slen, i);
1381 continue;
1382 }
1383
1384 if (pass_character) /* previous char was backslash */
1385 {
1386 pass_character = 0;
1387 ADVANCE_CHAR (string, slen, i);
1388 continue;
1389 }
1390
1391 /* Not exactly right yet; should handle shell metacharacters and
1392 multibyte characters, too. See COMMENT_BEGIN define in parse.y */
1393 if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1])))
1394 {
1395 in_comment = 1;
1396 ADVANCE_CHAR (string, slen, i);
1397 continue;
1398 }
1399
1400 if (c == CTLESC || c == '\\')
1401 {
1402 pass_character++;
1403 i++;
1404 continue;
1405 }
1406
1407 /* Process a nested command substitution, but only if we're parsing an
1408 arithmetic substitution. */
1409 if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN)
1410 {
1411 si = i + 2;
1412 t = extract_command_subst (string, &si, flags|SX_NOALLOC);
1413 CHECK_STRING_OVERRUN (i, si, slen, c);
1414 i = si + 1;
1415 continue;
1416 }
1417
1418 /* Process a nested OPENER. */
1419 if (STREQN (string + i, opener, len_opener))
1420 {
1421 si = i + len_opener;
1422 t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC);
1423 CHECK_STRING_OVERRUN (i, si, slen, c);
1424 i = si + 1;
1425 continue;
1426 }
1427
1428 /* Process a nested ALT_OPENER */
1429 if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
1430 {
1431 si = i + len_alt_opener;
1432 t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC);
1433 CHECK_STRING_OVERRUN (i, si, slen, c);
1434 i = si + 1;
1435 continue;
1436 }
1437
1438 /* If the current substring terminates the delimited string, decrement
1439 the nesting level. */
1440 if (STREQN (string + i, closer, len_closer))
1441 {
1442 i += len_closer - 1; /* move to last byte of the closer */
1443 nesting_level--;
1444 if (nesting_level == 0)
1445 break;
1446 }
1447
1448 /* Pass old-style command substitution through verbatim. */
1449 if (c == '`')
1450 {
1451 si = i + 1;
1452 t = string_extract (string, &si, "`", flags|SX_NOALLOC);
1453 CHECK_STRING_OVERRUN (i, si, slen, c);
1454 i = si + 1;
1455 continue;
1456 }
1457
1458 /* Pass single-quoted and double-quoted strings through verbatim. */
1459 if (c == '\'' || c == '"')
1460 {
1461 si = i + 1;
1462 i = (c == '\'') ? skip_single_quoted (string, slen, si, 0)
1463 : skip_double_quoted (string, slen, si, 0);
1464 continue;
1465 }
1466
1467 /* move past this character, which was not special. */
1468 ADVANCE_CHAR (string, slen, i);
1469 }
1470
1471 if (c == 0 && nesting_level)
1472 {
1473 if (no_longjmp_on_fatal_error == 0)
1474 {
1475 last_command_exit_value = EXECUTION_FAILURE;
1476 report_error (_("bad substitution: no closing `%s' in %s"), closer, string);
1477 exp_jump_to_top_level (DISCARD);
1478 }
1479 else
1480 {
1481 *sindex = i;
1482 return (char *)NULL;
1483 }
1484 }
1485
1486 si = i - *sindex - len_closer + 1;
1487 if (flags & SX_NOALLOC)
1488 result = (char *)NULL;
1489 else
1490 {
1491 result = (char *)xmalloc (1 + si);
1492 strncpy (result, string + *sindex, si);
1493 result[si] = '\0';
1494 }
1495 *sindex = i;
1496
1497 return (result);
1498 }
1499
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 */
1508 static char *
1509 extract_dollar_brace_string (string, sindex, quoted, flags)
1510 char *string;
1511 int *sindex, quoted, flags;
1512 {
1513 register int i, c;
1514 size_t slen;
1515 int pass_character, nesting_level, si, dolbrace_state;
1516 char *result, *t;
1517 DECLARE_MBSTATE;
1518
1519 pass_character = 0;
1520 nesting_level = 1;
1521 slen = strlen (string + *sindex) + *sindex;
1522
1523 /* The handling of dolbrace_state needs to agree with the code in parse.y:
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;
1530
1531 i = *sindex;
1532 while (c = string[i])
1533 {
1534 if (pass_character)
1535 {
1536 pass_character = 0;
1537 ADVANCE_CHAR (string, slen, i);
1538 continue;
1539 }
1540
1541 /* CTLESCs and backslashes quote the next character. */
1542 if (c == CTLESC || c == '\\')
1543 {
1544 pass_character++;
1545 i++;
1546 continue;
1547 }
1548
1549 if (string[i] == '$' && string[i+1] == LBRACE)
1550 {
1551 nesting_level++;
1552 i += 2;
1553 continue;
1554 }
1555
1556 if (c == RBRACE)
1557 {
1558 nesting_level--;
1559 if (nesting_level == 0)
1560 break;
1561 i++;
1562 continue;
1563 }
1564
1565 /* Pass the contents of old-style command substitutions through
1566 verbatim. */
1567 if (c == '`')
1568 {
1569 si = i + 1;
1570 t = string_extract (string, &si, "`", flags|SX_NOALLOC);
1571
1572 CHECK_STRING_OVERRUN (i, si, slen, c);
1573
1574 i = si + 1;
1575 continue;
1576 }
1577
1578 /* Pass the contents of new-style command substitutions and
1579 arithmetic substitutions through verbatim. */
1580 if (string[i] == '$' && string[i+1] == LPAREN)
1581 {
1582 si = i + 2;
1583 t = extract_command_subst (string, &si, flags|SX_NOALLOC);
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
1600 i = si + 1;
1601 continue;
1602 }
1603 #endif
1604
1605 /* Pass the contents of double-quoted strings through verbatim. */
1606 if (c == '"')
1607 {
1608 si = i + 1;
1609 i = skip_double_quoted (string, slen, si, 0);
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);*/
1617 if (posixly_correct && shell_compatibility_level > 42 && dolbrace_state != DOLBRACE_QUOTE && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
1618 ADVANCE_CHAR (string, slen, i);
1619 else
1620 {
1621 si = i + 1;
1622 i = skip_single_quoted (string, slen, si, 0);
1623 }
1624
1625 continue;
1626 }
1627
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
1638 /* move past this character, which was not special. */
1639 ADVANCE_CHAR (string, slen, i);
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)
1648 dolbrace_state = DOLBRACE_QUOTE2; /* XXX */
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;
1653 /* This is intended to handle all of the [:]op expansions and the substring/
1654 length/pattern removal/pattern substitution expansions. */
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;
1659 }
1660
1661 if (c == 0 && nesting_level)
1662 {
1663 if (no_longjmp_on_fatal_error == 0)
1664 { /* { */
1665 last_command_exit_value = EXECUTION_FAILURE;
1666 report_error (_("bad substitution: no closing `%s' in %s"), "}", string);
1667 exp_jump_to_top_level (DISCARD);
1668 }
1669 else
1670 {
1671 *sindex = i;
1672 return ((char *)NULL);
1673 }
1674 }
1675
1676 result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
1677 *sindex = i;
1678
1679 return (result);
1680 }
1681
1682 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1683 STRING, and returns a pointer to it. */
1684 char *
1685 de_backslash (string)
1686 char *string;
1687 {
1688 register size_t slen;
1689 register int i, j, prev_i;
1690 DECLARE_MBSTATE;
1691
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] == '\\' ||
1699 string[i + 1] == '$'))
1700 i++;
1701 prev_i = i;
1702 ADVANCE_CHAR (string, slen, i);
1703 if (j < prev_i)
1704 do string[j++] = string[prev_i++]; while (prev_i < i);
1705 else
1706 j = i;
1707 }
1708 string[j] = '\0';
1709
1710 return (string);
1711 }
1712
1713 #if 0
1714 /*UNUSED*/
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;
1722
1723 temp = (char *)xmalloc (1 + strlen (string));
1724
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);
1735 }
1736 #endif
1737
1738 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = oldjmp; return (x); } while (0)
1739
1740 /* This function assumes s[i] == open; returns with s[ret] == close; used to
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
1744 assignment parsing (see arrayfunc.c:assign_compound_array_list()) or
1745 during execution by a builtin which has already undergone word expansion. */
1746 static int
1747 skip_matched_pair (string, start, open, close, flags)
1748 const char *string;
1749 int start, open, close, flags;
1750 {
1751 int i, pass_next, backq, si, c, count, oldjmp;
1752 size_t slen;
1753 char *temp, *ss;
1754 DECLARE_MBSTATE;
1755
1756 slen = strlen (string + start) + start;
1757 oldjmp = no_longjmp_on_fatal_error;
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 }
1774 else if ((flags & 1) == 0 && c == '\\')
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 }
1787 else if ((flags & 1) == 0 && c == '`')
1788 {
1789 backq = 1;
1790 i++;
1791 continue;
1792 }
1793 else if ((flags & 1) == 0 && c == open)
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 }
1807 else if ((flags & 1) == 0 && (c == '\'' || c == '"'))
1808 {
1809 i = (c == '\'') ? skip_single_quoted (ss, slen, ++i, 0)
1810 : skip_double_quoted (ss, slen, ++i, 0);
1811 /* no increment, the skip functions increment past the closing quote. */
1812 }
1813 else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
1814 {
1815 si = i + 2;
1816 if (string[si] == '\0')
1817 CQ_RETURN(si);
1818
1819 /* XXX - extract_command_subst here? */
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);
1824
1825 CHECK_STRING_OVERRUN (i, si, slen, c);
1826
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)
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. */
1844 int
1845 skipsubscript (string, start, flags)
1846 const char *string;
1847 int start, flags;
1848 {
1849 return (skip_matched_pair (string, start, '[', ']', flags));
1850 }
1851 #endif
1852
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 {
1866 int i, pass_next, backq, dquote, si, c, oldjmp;
1867 int invert, skipquote, skipcmd, noprocsub, completeflag;
1868 int arithexp, skipcol;
1869 size_t slen;
1870 char *temp, open[3];
1871 DECLARE_MBSTATE;
1872
1873 slen = strlen (string + start) + start;
1874 oldjmp = no_longjmp_on_fatal_error;
1875 if (flags & SD_NOJMP)
1876 no_longjmp_on_fatal_error = 1;
1877 invert = (flags & SD_INVERT);
1878 skipcmd = (flags & SD_NOSKIPCMD) == 0;
1879 noprocsub = (flags & SD_NOPROCSUB);
1880 completeflag = (flags & SD_COMPLETE) ? SX_COMPLETE : 0;
1881
1882 arithexp = (flags & SD_ARITHEXP);
1883 skipcol = 0;
1884
1885 i = start;
1886 pass_next = backq = dquote = 0;
1887 while (c = string[i])
1888 {
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 =='"'));
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 }
1921 else if (arithexp && skipcol && c == ':')
1922 {
1923 skipcol--;
1924 i++;
1925 continue;
1926 }
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 }
1960 else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE))
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);
1970 CHECK_STRING_OVERRUN (i, si, slen, c);
1971 i = si;
1972 if (string[i] == '\0') /* don't increment i past EOS in loop */
1973 break;
1974 i++;
1975 continue;
1976 }
1977 #if defined (PROCESS_SUBSTITUTION)
1978 else if (skipcmd && noprocsub == 0 && (c == '<' || c == '>') && string[i+1] == LPAREN)
1979 {
1980 si = i + 2;
1981 if (string[si] == '\0')
1982 CQ_RETURN(si);
1983
1984 temp = extract_delimited_string (string, &si, (c == '<') ? "<(" : ">(", "(", ")", SX_COMMAND|SX_NOALLOC); /* )) */
1985 CHECK_STRING_OVERRUN (i, si, slen, c);
1986 i = si;
1987 if (string[i] == '\0')
1988 break;
1989 i++;
1990 continue;
1991 }
1992 #endif /* PROCESS_SUBSTITUTION */
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
2005 CHECK_STRING_OVERRUN (i, si, slen, c);
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
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 }
2027 else if ((skipquote || invert) && (member (c, delims) == 0))
2028 break;
2029 else
2030 ADVANCE_CHAR (string, slen, i);
2031 }
2032
2033 CQ_RETURN(i);
2034 }
2035
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 {
2048 int i, pass_next, backq, dquote, c, oldjmp;
2049 int histexp_comsub, histexp_backq, old_dquote;
2050 size_t slen;
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
2157 #if defined (READLINE)
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
2160 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
2161 single and double-quoted string parsing functions should not return an
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. */
2165
2166 int
2167 char_is_quoted (string, eindex)
2168 char *string;
2169 int eindex;
2170 {
2171 int i, pass_next, c, oldjmp;
2172 size_t slen;
2173 DECLARE_MBSTATE;
2174
2175 slen = strlen (string);
2176 oldjmp = no_longjmp_on_fatal_error;
2177 no_longjmp_on_fatal_error = 1;
2178 i = pass_next = 0;
2179 while (i <= eindex)
2180 {
2181 c = string[i];
2182
2183 if (pass_next)
2184 {
2185 pass_next = 0;
2186 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
2187 CQ_RETURN(1);
2188 ADVANCE_CHAR (string, slen, i);
2189 continue;
2190 }
2191 else if (c == '\\')
2192 {
2193 pass_next = 1;
2194 i++;
2195 continue;
2196 }
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 }
2204 else if (c == '\'' || c == '"')
2205 {
2206 i = (c == '\'') ? skip_single_quoted (string, slen, ++i, 0)
2207 : skip_double_quoted (string, slen, ++i, SX_COMPLETE);
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);
2214 }
2215
2216 CQ_RETURN(0);
2217 }
2218
2219 int
2220 unclosed_pair (string, eindex, openstr)
2221 char *string;
2222 int eindex;
2223 char *openstr;
2224 {
2225 int i, pass_next, openc, olen;
2226 size_t slen;
2227 DECLARE_MBSTATE;
2228
2229 slen = strlen (string);
2230 olen = strlen (openstr);
2231 i = pass_next = openc = 0;
2232 while (i <= eindex)
2233 {
2234 if (pass_next)
2235 {
2236 pass_next = 0;
2237 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
2238 return 0;
2239 ADVANCE_CHAR (string, slen, i);
2240 continue;
2241 }
2242 else if (string[i] == '\\')
2243 {
2244 pass_next = 1;
2245 i++;
2246 continue;
2247 }
2248 else if (STREQN (string + i, openstr, olen))
2249 {
2250 openc = 1 - openc;
2251 i += olen;
2252 }
2253 /* XXX - may want to handle $'...' specially here */
2254 else if (string[i] == '\'' || string[i] == '"')
2255 {
2256 i = (string[i] == '\'') ? skip_single_quoted (string, slen, i, 0)
2257 : skip_double_quoted (string, slen, i, SX_COMPLETE);
2258 if (i > eindex)
2259 return 0;
2260 }
2261 else
2262 ADVANCE_CHAR (string, slen, i);
2263 }
2264 return (openc);
2265 }
2266
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
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,
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
2273 DELIMS delimit separate fields. This is used by programmable completion. */
2274 WORD_LIST *
2275 split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp)
2276 char *string;
2277 int slen;
2278 char *delims;
2279 int sentinel, flags;
2280 int *nwp, *cwp;
2281 {
2282 int ts, te, i, nw, cw, ifs_split, dflags;
2283 char *token, *d, *d2;
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
2295 d = (delims == 0) ? ifs_value : delims;
2296 ifs_split = delims == 0;
2297
2298 /* Make d2 the non-whitespace characters in delims */
2299 d2 = 0;
2300 if (delims)
2301 {
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])
2312 {
2313 #if defined (HANDLE_MULTIBYTE)
2314 mbstate_t state_bak;
2315 state_bak = state;
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)
2329 d2[ts++] = delims[i];
2330
2331 i++;
2332 slength--;
2333 }
2334 d2[ts] = '\0';
2335 }
2336
2337 ret = (WORD_LIST *)NULL;
2338
2339 /* Remove sequences of whitespace characters at the start of the string, as
2340 long as those characters are delimiters. */
2341 for (i = 0; member (string[i], d) && spctabnl (string[i]); i++)
2342 ;
2343 if (string[i] == '\0')
2344 {
2345 FREE (d2);
2346 return (ret);
2347 }
2348
2349 ts = i;
2350 nw = 0;
2351 cw = -1;
2352 dflags = flags|SD_NOJMP;
2353 while (1)
2354 {
2355 te = skip_to_delim (string, ts, d, dflags);
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. */
2360 if (ts == te && d2 && member (string[ts], d2))
2361 {
2362 te = ts + 1;
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)
2366 while (member (string[te], d) && spctabnl (string[te]) && ((flags&SD_NOQUOTEDELIM) == 0 || (string[te] != '\'' && string[te] != '"')))
2367 te++;
2368 else
2369 while (member (string[te], d2) && ((flags&SD_NOQUOTEDELIM) == 0 || (string[te] != '\'' && string[te] != '"')))
2370 te++;
2371 }
2372
2373 token = substring (string, ts, te);
2374
2375 ret = add_string_to_list (token, ret); /* XXX */
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
2383 sentinel word to the current word. */
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
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. */
2390 if (cwp && cw == -1 && sentinel < ts)
2391 {
2392 tl = make_word_list (make_word (""), ret->next);
2393 ret->next = tl;
2394 cw = nw;
2395 nw++;
2396 }
2397
2398 if (string[te] == 0)
2399 break;
2400
2401 i = te;
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] != '"')))
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
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))
2418 {
2419 if (whitespace (string[sentinel - 1]))
2420 {
2421 token = "";
2422 ret = add_string_to_list (token, ret);
2423 nw++;
2424 }
2425 cw = nw;
2426 }
2427
2428 if (nwp)
2429 *nwp = nw;
2430 if (cwp)
2431 *cwp = cw;
2432
2433 FREE (d2);
2434
2435 return (REVERSE_LIST (ret, WORD_LIST *));
2436 }
2437 #endif /* READLINE */
2438
2439 #if 0
2440 /* UNUSED */
2441 /* Extract the name of the variable to bind to from the assignment string. */
2442 char *
2443 assignment_name (string)
2444 char *string;
2445 {
2446 int offset;
2447 char *temp;
2448
2449 offset = assignment (string, 0);
2450 if (offset == 0)
2451 return (char *)NULL;
2452 temp = substring (string, 0, offset);
2453 return (temp);
2454 }
2455 #endif
2456
2457 /* **************************************************************** */
2458 /* */
2459 /* Functions to convert strings to WORD_LISTs and vice versa */
2460 /* */
2461 /* **************************************************************** */
2462
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. */
2465 char *
2466 string_list_internal (list, sep)
2467 WORD_LIST *list;
2468 char *sep;
2469 {
2470 register WORD_LIST *t;
2471 char *result, *r;
2472 size_t word_len, sep_len, result_size;
2473
2474 if (list == 0)
2475 return ((char *)NULL);
2476
2477 /* Short-circuit quickly if we don't need to separate anything. */
2478 if (list->next == 0)
2479 return (savestring (list->word->word));
2480
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
2492 r = result = (char *)xmalloc (result_size + 1);
2493
2494 for (t = list; t; t = t->next)
2495 {
2496 if (t != list && sep_len)
2497 {
2498 if (sep_len > 1)
2499 {
2500 FASTCOPY (sep, r, sep_len);
2501 r += sep_len;
2502 }
2503 else
2504 *r++ = sep[0];
2505 }
2506
2507 word_len = strlen (t->word->word);
2508 FASTCOPY (t->word->word, r, word_len);
2509 r += word_len;
2510 }
2511
2512 *r = '\0';
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
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
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." */
2566 /* Posix interpretation 888 changes this when IFS is null by specifying
2567 that when unquoted, this expands to separate arguments */
2568 char *
2569 string_list_dollar_star (list, quoted, flags)
2570 WORD_LIST *list;
2571 int quoted, flags;
2572 {
2573 char *ret;
2574 #if defined (HANDLE_MULTIBYTE)
2575 # if defined (__GNUC__)
2576 char sep[MB_CUR_MAX + 1];
2577 # else
2578 char *sep = 0;
2579 # endif
2580 #else
2581 char sep[2];
2582 #endif
2583
2584 #if defined (HANDLE_MULTIBYTE)
2585 # if !defined (__GNUC__)
2586 sep = (char *)xmalloc (MB_CUR_MAX + 1);
2587 # endif /* !__GNUC__ */
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
2599 sep[0] = ifs_firstc;
2600 sep[1] = '\0';
2601 #endif
2602
2603 ret = string_list_internal (list, sep);
2604 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2605 free (sep);
2606 #endif
2607 return ret;
2608 }
2609
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
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'. */
2625 char *
2626 string_list_dollar_at (list, quoted, flags)
2627 WORD_LIST *list;
2628 int quoted;
2629 int flags;
2630 {
2631 char *ifs, *ret;
2632 #if defined (HANDLE_MULTIBYTE)
2633 # if defined (__GNUC__)
2634 char sep[MB_CUR_MAX + 1];
2635 # else
2636 char *sep = 0;
2637 # endif /* !__GNUC__ */
2638 #else
2639 char sep[2];
2640 #endif
2641 WORD_LIST *tlist;
2642
2643 /* XXX this could just be ifs = ifs_value; */
2644 ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
2645
2646 #if defined (HANDLE_MULTIBYTE)
2647 # if !defined (__GNUC__)
2648 sep = (char *)xmalloc (MB_CUR_MAX + 1);
2649 # endif /* !__GNUC__ */
2650 /* XXX - testing PF_ASSIGNRHS to make sure positional parameters are
2651 separated with a space even when word splitting will not occur. */
2652 if (flags & PF_ASSIGNRHS)
2653 {
2654 sep[0] = ' ';
2655 sep[1] = '\0';
2656 }
2657 else if (ifs && *ifs)
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 }
2675 #else /* !HANDLE_MULTIBYTE */
2676 /* XXX - PF_ASSIGNRHS means no word splitting, so we want positional
2677 parameters separated by a space. */
2678 sep[0] = ((flags & PF_ASSIGNRHS) || ifs == 0 || *ifs == 0) ? ' ' : *ifs;
2679 sep[1] = '\0';
2680 #endif /* !HANDLE_MULTIBYTE */
2681
2682 /* XXX -- why call quote_list if ifs == 0? we can get away without doing
2683 it now that quote_escapes quotes spaces */
2684 tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
2685 ? quote_list (list)
2686 : list_quote_escapes (list);
2687
2688 ret = string_list_internal (tlist, sep);
2689 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2690 free (sep);
2691 #endif
2692 return ret;
2693 }
2694
2695 /* Turn the positional parameters into a string, understanding quoting and
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. */
2699 /* This needs to fully understand the additional contexts where word
2700 splitting does not occur (W_ASSIGNRHS, etc.) */
2701 char *
2702 string_list_pos_params (pchar, list, quoted, pflags)
2703 int pchar;
2704 WORD_LIST *list;
2705 int quoted, pflags;
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);
2714 ret = string_list_dollar_star (tlist, 0, 0);
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 }
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 */
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 */
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. */
2731 ret = string_list_dollar_star (list, quoted, 0);
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. */
2741 ret = string_list_dollar_at (list, quoted, 0);
2742 else if (pchar == '@' && quoted == 0 && ifs_is_null) /* XXX */
2743 ret = string_list_dollar_at (list, quoted, 0); /* Posix interp 888 */
2744 else if (pchar == '@' && quoted == 0 && (pflags & PF_ASSIGNRHS))
2745 ret = string_list_dollar_at (list, quoted, pflags); /* Posix interp 888 */
2746 else if (pchar == '@')
2747 ret = string_list_dollar_star (list, quoted, 0);
2748 else
2749 ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list);
2750
2751 return ret;
2752 }
2753
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
2779 /* This performs word splitting and quoted null character removal on
2780 STRING. */
2781 #define issep(c) \
2782 (((separators)[0]) ? ((separators)[1] ? isifs(c) \
2783 : (c) == (separators)[0]) \
2784 : 0)
2785
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
2793 WORD_LIST *
2794 list_string (string, separators, quoted)
2795 register char *string, *separators;
2796 int quoted;
2797 {
2798 WORD_LIST *result;
2799 WORD_DESC *t;
2800 char *current_word, *s;
2801 int sindex, sh_style_split, whitesep, xflags, free_word;
2802 size_t slen;
2803
2804 if (!string || !*string)
2805 return ((WORD_LIST *)NULL);
2806
2807 sh_style_split = separators && separators[0] == ' ' &&
2808 separators[1] == '\t' &&
2809 separators[2] == '\n' &&
2810 separators[3] == '\0';
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 }
2816
2817 slen = 0;
2818 /* Remove sequences of whitespace at the beginning of STRING, as
2819 long as those characters appear in IFS. Do not do this if
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. */
2823 #if 0
2824 if (!quoted || !separators || !*separators)
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
2831 {
2832 for (s = string; *s && issep (*s) && ifs_whitespace (*s); s++);
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:
2842 extract a word, stopping at a separator
2843 skip sequences of whitespace characters as long as they are separators
2844 This obeys the field splitting rules in Posix.2. */
2845 slen = STRLEN (string);
2846 for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
2847 {
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 */
2850 current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags);
2851 if (current_word == 0)
2852 break;
2853
2854 free_word = 1; /* If non-zero, we free current_word */
2855
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 {
2862 t = alloc_word_desc ();
2863 t->word = make_quoted_char ('\0');
2864 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
2865 result = make_word_list (t, result);
2866 }
2867 else if (current_word[0] != '\0')
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);
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;
2881 result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */
2882 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
2883 result->word->flags |= W_QUOTED;
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;
2888 }
2889
2890 /* If we're not doing sequences of separators in the traditional
2891 Bourne shell style, then add a quoted null argument. */
2892 else if (!sh_style_split && !ifs_whitespace (string[sindex]))
2893 {
2894 t = alloc_word_desc ();
2895 t->word = make_quoted_char ('\0');
2896 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
2897 result = make_word_list (t, result);
2898 }
2899
2900 if (free_word)
2901 free (current_word);
2902
2903 /* Note whether or not the separator is IFS whitespace, used later. */
2904 whitesep = string[sindex] && ifs_whitesep (string[sindex]);
2905
2906 /* Move past the current separator character. */
2907 if (string[sindex])
2908 {
2909 DECLARE_MBSTATE;
2910 ADVANCE_CHAR (string, slen, sindex);
2911 }
2912
2913 /* Now skip sequences of whitespace characters if they are
2914 in the list of separators. */
2915 while (string[sindex] && ifs_whitesep (string[sindex]) && issep (string[sindex]))
2916 sindex++;
2917
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). */
2922 if (string[sindex] && whitesep && issep (string[sindex]) && !ifs_whitesep (string[sindex]))
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) */
2927 while (string[sindex] && ifs_whitesep (string[sindex]) && isifs (string[sindex]))
2928 sindex++;
2929 }
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
2936 the `read' builtin.
2937
2938 This is never called with SEPARATORS != $IFS, and takes advantage of that.
2939
2940 XXX - this function is very similar to list_string; they should be
2941 combined - XXX */
2942
2943 /* character is in $IFS */
2944 #define islocalsep(c) (local_cmap[(unsigned char)(c)] != 0)
2945
2946 char *
2947 get_word_from_string (stringp, separators, endptr)
2948 char **stringp, *separators, **endptr;
2949 {
2950 register char *s;
2951 char *current_word;
2952 int sindex, sh_style_split, whitesep, xflags;
2953 unsigned char local_cmap[UCHAR_MAX+1]; /* really only need single-byte chars here */
2954 size_t slen;
2955
2956 if (!stringp || !*stringp || !**stringp)
2957 return ((char *)NULL);
2958
2959 sh_style_split = separators && separators[0] == ' ' &&
2960 separators[1] == '\t' &&
2961 separators[2] == '\n' &&
2962 separators[3] == '\0';
2963 memset (local_cmap, '\0', sizeof (local_cmap));
2964 for (xflags = 0, s = separators; s && *s; s++)
2965 {
2966 if (*s == CTLESC) xflags |= SX_NOCTLESC;
2967 if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
2968 local_cmap[(unsigned char)*s] = 1; /* local charmap of separators */
2969 }
2970
2971 s = *stringp;
2972 slen = 0;
2973
2974 /* Remove sequences of whitespace at the beginning of STRING, as
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)
2978 for (; *s && spctabnl (*s) && islocalsep (*s); s++);
2979 else
2980 for (; *s && ifs_whitespace (*s) && islocalsep (*s); s++);
2981
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);
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.
2995
2996 This obeys the field splitting rules in Posix.2. */
2997 sindex = 0;
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);
3001 current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags);
3002
3003 /* Set ENDPTR to the first character after the end of the word. */
3004 if (endptr)
3005 *endptr = s + sindex;
3006
3007 /* Note whether or not the separator is IFS whitespace, used later. */
3008 whitesep = s[sindex] && ifs_whitesep (s[sindex]);
3009
3010 /* Move past the current separator character. */
3011 if (s[sindex])
3012 {
3013 DECLARE_MBSTATE;
3014 ADVANCE_CHAR (s, slen, sindex);
3015 }
3016
3017 /* Now skip sequences of space, tab, or newline characters if they are
3018 in the list of separators. */
3019 while (s[sindex] && spctabnl (s[sindex]) && islocalsep (s[sindex]))
3020 sindex++;
3021
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). */
3026 if (s[sindex] && whitesep && islocalsep (s[sindex]) && !ifs_whitesep (s[sindex]))
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. */
3031 while (s[sindex] && ifs_whitesep (s[sindex]) && islocalsep(s[sindex]))
3032 sindex++;
3033 }
3034
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;
3051
3052 s = string + STRLEN (string) - 1;
3053 while (s > string && ((spctabnl (*s) && isifs (*s)) ||
3054 (saw_escape && *s == CTLESC && spctabnl (s[1]))))
3055 s--;
3056 *++s = '\0';
3057 return string;
3058 }
3059
3060 #if 0
3061 /* UNUSED */
3062 /* Split STRING into words at whitespace. Obeys shell-style quoting with
3063 backslashes, single and double quotes. */
3064 WORD_LIST *
3065 list_string_with_quotes (string)
3066 char *string;
3067 {
3068 WORD_LIST *list;
3069 char *token, *s;
3070 size_t s_len;
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
3078 s_len = strlen (s);
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 == '\'')
3091 i = skip_single_quoted (s, s_len, ++i, 0);
3092 else if (c == '"')
3093 i = skip_double_quoted (s, s_len, ++i, 0);
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. */
3098 token = substring (s, tokstart, i);
3099 list = add_string_to_list (token, list);
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 }
3113 #endif
3114
3115 /********************************************************/
3116 /* */
3117 /* Functions to perform assignment statements */
3118 /* */
3119 /********************************************************/
3120
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;
3128 int mklocal, mkassoc, mkglobal, chklocal;
3129 WORD_LIST *list;
3130 char *newname; /* used for local nameref references */
3131
3132 mklocal = flags & ASS_MKLOCAL;
3133 mkassoc = flags & ASS_MKASSOC;
3134 mkglobal = flags & ASS_MKGLOBAL;
3135 chklocal = flags & ASS_CHKLOCAL;
3136
3137 if (mklocal && variable_context)
3138 {
3139 v = find_variable (name); /* follows namerefs */
3140 newname = (v == 0) ? nameref_transform_name (name, flags) : v->name;
3141 if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
3142 {
3143 if (readonly_p (v))
3144 err_readonly (name);
3145 return (v); /* XXX */
3146 }
3147 list = expand_compound_array_assignment (v, value, flags);
3148 if (mkassoc)
3149 v = make_local_assoc_variable (newname, 0);
3150 else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context)
3151 v = make_local_array_variable (newname, 0);
3152 if (v)
3153 assign_compound_array_list (v, list, flags);
3154 if (list)
3155 dispose_words (list);
3156 }
3157 /* In a function but forcing assignment in global context. CHKLOCAL means to
3158 check for an existing local variable first. */
3159 else if (mkglobal && variable_context)
3160 {
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);
3166 if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
3167 {
3168 if (readonly_p (v))
3169 err_readonly (name);
3170 return (v); /* XXX */
3171 }
3172 /* sanity check */
3173 newname = (v == 0) ? nameref_transform_name (name, flags) : name;
3174 list = expand_compound_array_assignment (v, value, flags);
3175 if (v == 0 && mkassoc)
3176 v = make_new_assoc_variable (newname);
3177 else if (v && mkassoc && assoc_p (v) == 0)
3178 v = convert_var_to_assoc (v);
3179 else if (v == 0)
3180 v = make_new_array_variable (newname);
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);
3185 if (list)
3186 dispose_words (list);
3187 }
3188 else
3189 {
3190 v = assign_array_from_string (name, value, flags);
3191 if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
3192 {
3193 if (readonly_p (v))
3194 err_readonly (name);
3195 return (v); /* XXX */
3196 }
3197 }
3198
3199 return (v);
3200 }
3201 #endif
3202
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
3209 do_assignment_internal (word, expand)
3210 const WORD_DESC *word;
3211 int expand;
3212 {
3213 int offset, appendop, assign_list, aflags, retval;
3214 char *name, *value, *temp;
3215 SHELL_VAR *entry;
3216 #if defined (ARRAY_VARS)
3217 char *t;
3218 int ni;
3219 #endif
3220 const char *string;
3221
3222 if (word == 0 || word->word == 0)
3223 return 0;
3224
3225 appendop = assign_list = aflags = 0;
3226 string = word->word;
3227 offset = assignment (string, 0);
3228 name = savestring (string);
3229 value = (char *)NULL;
3230
3231 if (name[offset] == '=')
3232 {
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 */
3240 temp = name + offset + 1;
3241
3242 #if defined (ARRAY_VARS)
3243 if (expand && (word->flags & W_COMPASSIGN))
3244 {
3245 assign_list = ni = 1;
3246 value = extract_array_assignment_list (temp, &ni);
3247 }
3248 else
3249 #endif
3250 if (expand && temp[0])
3251 value = expand_string_if_necessary (temp, 0, expand_string_assignment);
3252 else
3253 value = savestring (temp);
3254 }
3255
3256 if (value == 0)
3257 {
3258 value = (char *)xmalloc (1);
3259 value[0] = '\0';
3260 }
3261
3262 if (echo_command_at_execute)
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 }
3270
3271 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
3272
3273 if (appendop)
3274 aflags |= ASS_APPEND;
3275
3276 #if defined (ARRAY_VARS)
3277 if (t = mbschr (name, LBRACK))
3278 {
3279 if (assign_list)
3280 {
3281 report_error (_("%s: cannot assign list to array member"), name);
3282 ASSIGN_RETURN (0);
3283 }
3284 entry = assign_array_element (name, value, aflags);
3285 if (entry == 0)
3286 ASSIGN_RETURN (0);
3287 }
3288 else if (assign_list)
3289 {
3290 if ((word->flags & W_ASSIGNARG) && (word->flags & W_CHKLOCAL))
3291 aflags |= ASS_CHKLOCAL;
3292 if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL) == 0)
3293 aflags |= ASS_MKLOCAL;
3294 if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL))
3295 aflags |= ASS_MKGLOBAL;
3296 if (word->flags & W_ASSIGNASSOC)
3297 aflags |= ASS_MKASSOC;
3298 entry = do_compound_assignment (name, value, aflags);
3299 }
3300 else
3301 #endif /* ARRAY_VARS */
3302 entry = bind_variable (name, value, aflags);
3303
3304 if (entry)
3305 stupidly_hack_special_variables (entry->name); /* might be a nameref */
3306 else
3307 stupidly_hack_special_variables (name);
3308
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 {
3314 set_exit_status (EXECUTION_FAILURE);
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);
3324 }
3325
3326 /* Perform the assignment statement in STRING, and expand the
3327 right side by doing tilde, command and parameter expansion. */
3328 int
3329 do_assignment (string)
3330 char *string;
3331 {
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
3341 do_word_assignment (word, flags)
3342 WORD_DESC *word;
3343 int flags;
3344 {
3345 return do_assignment_internal (word, 1);
3346 }
3347
3348 /* Given STRING, an assignment string, get the value of the right side
3349 of the `=', and bind it to the left side. Do not perform any word
3350 expansions on the right hand side. */
3351 int
3352 do_assignment_no_expand (string)
3353 char *string;
3354 {
3355 WORD_DESC td;
3356
3357 td.flags = W_ASSIGNMENT;
3358 td.word = string;
3359
3360 return (do_assignment_internal (&td, 0));
3361 }
3362
3363 /***************************************************
3364 * *
3365 * Functions to manage the positional parameters *
3366 * *
3367 ***************************************************/
3368
3369 /* Return the word list that corresponds to `$*'. */
3370 WORD_LIST *
3371 list_rest_of_args ()
3372 {
3373 register WORD_LIST *list, *args;
3374 int i;
3375
3376 /* Break out of the loop as soon as one of the dollar variables is null. */
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);
3382
3383 return (REVERSE_LIST (list, WORD_LIST *));
3384 }
3385
3386 /* Return the value of a positional parameter. This handles values > 10. */
3387 char *
3388 get_dollar_var_value (ind)
3389 intmax_t ind;
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)
3400 ;
3401 temp = p ? savestring (p->word->word) : (char *)NULL;
3402 }
3403 return (temp);
3404 }
3405
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 {
3413 register WORD_LIST *list;
3414 char *string;
3415
3416 list = list_rest_of_args ();
3417 string = dollar_star ? string_list_dollar_star (list, 0, 0) : string_list (list);
3418 dispose_words (list);
3419 return (string);
3420 }
3421
3422 /* Return a string containing the positional parameters from START to
3423 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
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. */
3427 static char *
3428 pos_params (string, start, end, quoted, pflags)
3429 char *string;
3430 int start, end, quoted, pflags;
3431 {
3432 WORD_LIST *save, *params, *h, *t;
3433 char *ret;
3434 int i;
3435
3436 /* see if we can short-circuit. if start == end, we want 0 parameters. */
3437 if (start == end)
3438 return ((char *)NULL);
3439
3440 save = params = list_rest_of_args ();
3441 if (save == 0 && start > 0)
3442 return ((char *)NULL);
3443
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
3450 for (i = start ? 1 : 0; params && i < start; i++)
3451 params = params->next;
3452 if (params == 0)
3453 {
3454 dispose_words (save);
3455 return ((char *)NULL);
3456 }
3457 for (h = t = params; params && i < end; i++)
3458 {
3459 t = params;
3460 params = params->next;
3461 }
3462 t->next = (WORD_LIST *)NULL;
3463
3464 ret = string_list_pos_params (string[0], h, quoted, pflags);
3465
3466 if (t != params)
3467 t->next = params;
3468
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)
3480 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
3481 #else
3482 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
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 *
3489 expand_string_if_necessary (string, quoted, func)
3490 char *string;
3491 int quoted;
3492 EXPFUNC *func;
3493 {
3494 WORD_LIST *list;
3495 size_t slen;
3496 int i, saw_quote;
3497 char *ret;
3498 DECLARE_MBSTATE;
3499
3500 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3501 slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
3502 i = saw_quote = 0;
3503 while (string[i])
3504 {
3505 if (EXP_CHAR (string[i]))
3506 break;
3507 else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
3508 saw_quote = 1;
3509 ADVANCE_CHAR (string, slen, i);
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);
3527
3528 return ret;
3529 }
3530
3531 static inline char *
3532 expand_string_to_string_internal (string, quoted, func)
3533 char *string;
3534 int quoted;
3535 EXPFUNC *func;
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
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
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
3579 char *
3580 expand_arith_string (string, quoted)
3581 char *string;
3582 int quoted;
3583 {
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 */
3607 td.flags = W_NOPROCSUB|W_NOTILDE; /* don't want process substitution or tilde expansion */
3608 #if 0 /* TAG: bash-5.2 */
3609 if (quoted & Q_ARRAYSUB)
3610 td.flags |= W_NOCOMSUB;
3611 #endif
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 }
3634 else if (saw_quote && (quoted & Q_ARITH))
3635 ret = string_quote_removal (string, quoted);
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;
3642 }
3643
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
3652 r = ret = (char *)xmalloc (strlen (string) + 1);
3653 for (s = string; s && *s; )
3654 {
3655 if (*s == '\\')
3656 s++;
3657 if (*s == 0)
3658 break;
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
3667 [[...]] expression. If SPECIAL is 1, this is the rhs argument
3668 to the != or == operator, and should be treated as a pattern. In
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
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. */
3675 char *
3676 cond_expand_word (w, special)
3677 WORD_DESC *w;
3678 int special;
3679 {
3680 char *r, *p;
3681 WORD_LIST *l;
3682 int qflags;
3683
3684 if (w->word == 0 || w->word[0] == '\0')
3685 return ((char *)NULL);
3686
3687 expand_no_split_dollar_star = 1;
3688 w->flags |= W_NOSPLIT2;
3689 l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
3690 expand_no_split_dollar_star = 0;
3691 if (l)
3692 {
3693 if (special == 0) /* LHS */
3694 {
3695 if (l->word)
3696 word_list_remove_quoted_nulls (l);
3697 dequote_list (l);
3698 r = string_list (l);
3699 }
3700 else
3701 {
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. */
3705 qflags = QGLOB_CVTNULL|QGLOB_CTLESC;
3706 if (special == 2)
3707 qflags |= QGLOB_REGEXP;
3708 word_list_remove_quoted_nulls (l);
3709 p = string_list (l);
3710 r = quote_string_for_globbing (p, qflags);
3711 free (p);
3712 }
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 *
3726 call_expand_word_internal (w, q, i, c, e)
3727 WORD_DESC *w;
3728 int q, i, *c, *e;
3729 {
3730 WORD_LIST *result;
3731
3732 result = expand_word_internal (w, q, i, c, e);
3733 if (result == &expand_word_error || result == &expand_word_fatal)
3734 {
3735 /* By convention, each time this error is returned, w->word has
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). */
3739 w->word = (char *)NULL;
3740 last_command_exit_value = EXECUTION_FAILURE;
3741 exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
3742 /* NOTREACHED */
3743 return (NULL);
3744 }
3745 else
3746 return (result);
3747 }
3748
3749 /* Perform parameter expansion, command substitution, and arithmetic
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. */
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
3764 td.flags = 0;
3765 td.word = savestring (string);
3766
3767 tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3768
3769 FREE (td.word);
3770 return (tresult);
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
3785 if (string == 0 || *string == '\0')
3786 return ((WORD_LIST *)NULL);
3787
3788 expand_no_split_dollar_star = 1;
3789 value = expand_string_internal (string, quoted);
3790 expand_no_split_dollar_star = 0;
3791
3792 if (value)
3793 {
3794 if (value->word)
3795 {
3796 remove_quoted_nulls (value->word->word); /* XXX */
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
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
3826 td.flags = W_ASSIGNRHS;
3827 #endif
3828 td.flags |= (W_NOGLOB|W_TILDEEXP);
3829 td.word = savestring (string);
3830 value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3831 FREE (td.word);
3832
3833 expand_no_split_dollar_star = 0;
3834
3835 if (value)
3836 {
3837 if (value->word)
3838 {
3839 remove_quoted_nulls (value->word->word); /* XXX */
3840 value->word->flags &= ~W_HASQUOTEDNULL;
3841 }
3842 dequote_list (value);
3843 }
3844 return (value);
3845 }
3846
3847
3848 /* Expand one of the PS? prompt strings. This is a sort of combination of
3849 expand_string_unsplit and expand_string_internal, but returns the
3850 passed string when an error occurs. Might want to trap other calls
3851 to jump_to_top_level here so we don't endlessly loop. */
3852 WORD_LIST *
3853 expand_prompt_string (string, quoted, wflags)
3854 char *string;
3855 int quoted;
3856 int wflags;
3857 {
3858 WORD_LIST *value;
3859 WORD_DESC td;
3860
3861 if (string == 0 || *string == 0)
3862 return ((WORD_LIST *)NULL);
3863
3864 td.flags = wflags;
3865 td.word = savestring (string);
3866
3867 no_longjmp_on_fatal_error = 1;
3868 value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3869 no_longjmp_on_fatal_error = 0;
3870
3871 if (value == &expand_word_error || value == &expand_word_fatal)
3872 {
3873 value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
3874 return value;
3875 }
3876 FREE (td.word);
3877 if (value)
3878 {
3879 if (value->word)
3880 {
3881 remove_quoted_nulls (value->word->word); /* XXX */
3882 value->word->flags &= ~W_HASQUOTEDNULL;
3883 }
3884 dequote_list (value);
3885 }
3886 return (value);
3887 }
3888
3889 /* Expand STRING just as if you were expanding a word, but do not dequote
3890 the resultant WORD_LIST. This is called only from within this file,
3891 and is used to correctly preserve quoted characters when expanding
3892 things like ${1+"$@"}. This does parameter expansion, command
3893 substitution, arithmetic expansion, and word splitting. */
3894 static WORD_LIST *
3895 expand_string_leave_quoted (string, quoted)
3896 char *string;
3897 int quoted;
3898 {
3899 WORD_LIST *tlist;
3900 WORD_LIST *tresult;
3901
3902 if (string == 0 || *string == '\0')
3903 return ((WORD_LIST *)NULL);
3904
3905 tlist = expand_string_internal (string, quoted);
3906
3907 if (tlist)
3908 {
3909 tresult = word_list_split (tlist);
3910 dispose_words (tlist);
3911 return (tresult);
3912 }
3913 return ((WORD_LIST *)NULL);
3914 }
3915
3916 /* This does not perform word splitting or dequote the WORD_LIST
3917 it returns. */
3918 static WORD_LIST *
3919 expand_string_for_rhs (string, quoted, op, pflags, dollar_at_p, expanded_p)
3920 char *string;
3921 int quoted, op, pflags;
3922 int *dollar_at_p, *expanded_p;
3923 {
3924 WORD_DESC td;
3925 WORD_LIST *tresult;
3926 int old_nosplit;
3927
3928 if (string == 0 || *string == '\0')
3929 return (WORD_LIST *)NULL;
3930
3931 /* We want field splitting to be determined by what is going to be done with
3932 the entire ${parameterOPword} expansion, so we don't want to split the RHS
3933 we expand here. However, the expansion of $* is determined by whether we
3934 are going to eventually perform word splitting, so we want to set this
3935 depending on whether or not are are going to be splitting: if the expansion
3936 is quoted, if the OP is `=', or if IFS is set to the empty string, we
3937 are not going to be splitting, so we set expand_no_split_dollar_star to
3938 note this to callees.
3939 We pass through PF_ASSIGNRHS as W_ASSIGNRHS if this is on the RHS of an
3940 assignment statement. */
3941 /* The updated treatment of $* is the result of Posix interp 888 */
3942 /* This was further clarified on the austin-group list in March, 2017 and
3943 in Posix bug 1129 */
3944 old_nosplit = expand_no_split_dollar_star;
3945 expand_no_split_dollar_star = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || op == '=' || ifs_is_null == 0; /* XXX - was 1 */
3946 td.flags = W_EXPANDRHS; /* expanding RHS of ${paramOPword} */
3947 td.flags |= W_NOSPLIT2; /* no splitting, remove "" and '' */
3948 if (pflags & PF_ASSIGNRHS) /* pass through */
3949 td.flags |= W_ASSIGNRHS;
3950 if (op == '=')
3951 #if 0
3952 td.flags |= W_ASSIGNRHS; /* expand b in ${a=b} like assignment */
3953 #else
3954 td.flags |= W_ASSIGNRHS|W_NOASSNTILDE; /* expand b in ${a=b} like assignment */
3955 #endif
3956 td.word = string;
3957 tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, expanded_p);
3958 expand_no_split_dollar_star = old_nosplit;
3959
3960 return (tresult);
3961 }
3962
3963 /* This does not perform word splitting or dequote the WORD_LIST
3964 it returns and it treats $* as if it were quoted. */
3965 static WORD_LIST *
3966 expand_string_for_pat (string, quoted, dollar_at_p, expanded_p)
3967 char *string;
3968 int quoted, *dollar_at_p, *expanded_p;
3969 {
3970 WORD_DESC td;
3971 WORD_LIST *tresult;
3972 int oexp;
3973
3974 if (string == 0 || *string == '\0')
3975 return (WORD_LIST *)NULL;
3976
3977 oexp = expand_no_split_dollar_star;
3978 expand_no_split_dollar_star = 1;
3979 td.flags = W_NOSPLIT2; /* no splitting, remove "" and '' */
3980 td.word = string;
3981 tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, expanded_p);
3982 expand_no_split_dollar_star = oexp;
3983
3984 return (tresult);
3985 }
3986
3987 /* Expand STRING just as if you were expanding a word. This also returns
3988 a list of words. Note that filename globbing is *NOT* done for word
3989 or string expansion, just when the shell is expanding a command. This
3990 does parameter expansion, command substitution, arithmetic expansion,
3991 and word splitting. Dequote the resultant WORD_LIST before returning. */
3992 WORD_LIST *
3993 expand_string (string, quoted)
3994 char *string;
3995 int quoted;
3996 {
3997 WORD_LIST *result;
3998
3999 if (string == 0 || *string == '\0')
4000 return ((WORD_LIST *)NULL);
4001
4002 result = expand_string_leave_quoted (string, quoted);
4003 return (result ? dequote_list (result) : result);
4004 }
4005
4006 /*******************************************
4007 * *
4008 * Functions to expand WORD_DESCs *
4009 * *
4010 *******************************************/
4011
4012 /* Expand WORD, performing word splitting on the result. This does
4013 parameter expansion, command substitution, arithmetic expansion,
4014 word splitting, and quote removal. */
4015
4016 WORD_LIST *
4017 expand_word (word, quoted)
4018 WORD_DESC *word;
4019 int quoted;
4020 {
4021 WORD_LIST *result, *tresult;
4022
4023 tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
4024 result = word_list_split (tresult);
4025 dispose_words (tresult);
4026 return (result ? dequote_list (result) : result);
4027 }
4028
4029 /* Expand WORD, but do not perform word splitting on the result. This
4030 does parameter expansion, command substitution, arithmetic expansion,
4031 and quote removal. */
4032 WORD_LIST *
4033 expand_word_unsplit (word, quoted)
4034 WORD_DESC *word;
4035 int quoted;
4036 {
4037 WORD_LIST *result;
4038
4039 result = expand_word_leave_quoted (word, quoted);
4040 return (result ? dequote_list (result) : result);
4041 }
4042
4043 /* Perform shell expansions on WORD, but do not perform word splitting or
4044 quote removal on the result. Virtually identical to expand_word_unsplit;
4045 could be combined if implementations don't diverge. */
4046 WORD_LIST *
4047 expand_word_leave_quoted (word, quoted)
4048 WORD_DESC *word;
4049 int quoted;
4050 {
4051 WORD_LIST *result;
4052
4053 expand_no_split_dollar_star = 1;
4054 if (ifs_is_null)
4055 word->flags |= W_NOSPLIT;
4056 word->flags |= W_NOSPLIT2;
4057 result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
4058 expand_no_split_dollar_star = 0;
4059
4060 return result;
4061 }
4062
4063 /***************************************************
4064 * *
4065 * Functions to handle quoting chars *
4066 * *
4067 ***************************************************/
4068
4069 /* Conventions:
4070
4071 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
4072 The parser passes CTLNUL as CTLESC CTLNUL. */
4073
4074 /* Quote escape characters in string s, but no other characters. This is
4075 used to protect CTLESC and CTLNUL in variable values from the rest of
4076 the word expansion process after the variable is expanded (word splitting
4077 and filename generation). If IFS is null, we quote spaces as well, just
4078 in case we split on spaces later (in the case of unquoted $@, we will
4079 eventually attempt to split the entire word on spaces). Corresponding
4080 code exists in dequote_escapes. Even if we don't end up splitting on
4081 spaces, quoting spaces is not a problem. This should never be called on
4082 a string that is quoted with single or double quotes or part of a here
4083 document (effectively double-quoted).
4084 FLAGS says whether or not we are going to split the result. If we are not,
4085 and there is a CTLESC or CTLNUL in IFS, we need to quote CTLESC and CTLNUL,
4086 respectively, to prevent them from being removed as part of dequoting. */
4087 static char *
4088 quote_escapes_internal (string, flags)
4089 const char *string;
4090 int flags;
4091 {
4092 const char *s, *send;
4093 char *t, *result;
4094 size_t slen;
4095 int quote_spaces, skip_ctlesc, skip_ctlnul, nosplit;
4096 DECLARE_MBSTATE;
4097
4098 slen = strlen (string);
4099 send = string + slen;
4100
4101 quote_spaces = (ifs_value && *ifs_value == 0);
4102 nosplit = (flags & PF_NOSPLIT2);
4103
4104 for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
4105 {
4106 skip_ctlesc |= (nosplit == 0 && *s == CTLESC);
4107 skip_ctlnul |= (nosplit == 0 && *s == CTLNUL);
4108 }
4109
4110 t = result = (char *)xmalloc ((slen * 2) + 1);
4111 s = string;
4112
4113 while (*s)
4114 {
4115 if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' '))
4116 *t++ = CTLESC;
4117 COPY_CHAR_P (t, s, send);
4118 }
4119 *t = '\0';
4120
4121 return (result);
4122 }
4123
4124 char *
4125 quote_escapes (string)
4126 const char *string;
4127 {
4128 return (quote_escapes_internal (string, 0));
4129 }
4130
4131 char *
4132 quote_rhs (string)
4133 const char *string;
4134 {
4135 return (quote_escapes_internal (string, PF_NOSPLIT2));
4136 }
4137
4138 static WORD_LIST *
4139 list_quote_escapes (list)
4140 WORD_LIST *list;
4141 {
4142 register WORD_LIST *w;
4143 char *t;
4144
4145 for (w = list; w; w = w->next)
4146 {
4147 t = w->word->word;
4148 w->word->word = quote_escapes (t);
4149 free (t);
4150 }
4151 return list;
4152 }
4153
4154 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
4155
4156 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
4157 This is necessary to make unquoted CTLESC and CTLNUL characters in the
4158 data stream pass through properly.
4159
4160 We need to remove doubled CTLESC characters inside quoted strings before
4161 quoting the entire string, so we do not double the number of CTLESC
4162 characters.
4163
4164 Also used by parts of the pattern substitution code. */
4165 char *
4166 dequote_escapes (string)
4167 const char *string;
4168 {
4169 const char *s, *send;
4170 char *t, *result;
4171 size_t slen;
4172 int quote_spaces;
4173 DECLARE_MBSTATE;
4174
4175 if (string == 0)
4176 return (char *)0;
4177
4178 slen = strlen (string);
4179 send = string + slen;
4180
4181 t = result = (char *)xmalloc (slen + 1);
4182
4183 if (strchr (string, CTLESC) == 0)
4184 return (strcpy (result, string));
4185
4186 quote_spaces = (ifs_value && *ifs_value == 0);
4187
4188 s = string;
4189 while (*s)
4190 {
4191 if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' ')))
4192 {
4193 s++;
4194 if (*s == '\0')
4195 break;
4196 }
4197 COPY_CHAR_P (t, s, send);
4198 }
4199 *t = '\0';
4200
4201 return result;
4202 }
4203
4204 #if defined (INCLUDE_UNUSED)
4205 static WORD_LIST *
4206 list_dequote_escapes (list)
4207 WORD_LIST *list;
4208 {
4209 register WORD_LIST *w;
4210 char *t;
4211
4212 for (w = list; w; w = w->next)
4213 {
4214 t = w->word->word;
4215 w->word->word = dequote_escapes (t);
4216 free (t);
4217 }
4218 return list;
4219 }
4220 #endif
4221
4222 /* Return a new string with the quoted representation of character C.
4223 This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
4224 set in any resultant WORD_DESC where this value is the word. */
4225 static char *
4226 make_quoted_char (c)
4227 int c;
4228 {
4229 char *temp;
4230
4231 temp = (char *)xmalloc (3);
4232 if (c == 0)
4233 {
4234 temp[0] = CTLNUL;
4235 temp[1] = '\0';
4236 }
4237 else
4238 {
4239 temp[0] = CTLESC;
4240 temp[1] = c;
4241 temp[2] = '\0';
4242 }
4243 return (temp);
4244 }
4245
4246 /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
4247 the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
4248 this value is the word. */
4249 char *
4250 quote_string (string)
4251 char *string;
4252 {
4253 register char *t;
4254 size_t slen;
4255 char *result, *send;
4256
4257 if (*string == 0)
4258 {
4259 result = (char *)xmalloc (2);
4260 result[0] = CTLNUL;
4261 result[1] = '\0';
4262 }
4263 else
4264 {
4265 DECLARE_MBSTATE;
4266
4267 slen = strlen (string);
4268 send = string + slen;
4269
4270 result = (char *)xmalloc ((slen * 2) + 1);
4271
4272 for (t = result; string < send; )
4273 {
4274 *t++ = CTLESC;
4275 COPY_CHAR_P (t, string, send);
4276 }
4277 *t = '\0';
4278 }
4279 return (result);
4280 }
4281
4282 /* De-quote quoted characters in STRING. */
4283 char *
4284 dequote_string (string)
4285 char *string;
4286 {
4287 register char *s, *t;
4288 size_t slen;
4289 char *result, *send;
4290 DECLARE_MBSTATE;
4291
4292 #if defined (DEBUG)
4293 if (string[0] == CTLESC && string[1] == 0)
4294 internal_inform ("dequote_string: string with bare CTLESC");
4295 #endif
4296
4297 slen = STRLEN (string);
4298
4299 t = result = (char *)xmalloc (slen + 1);
4300
4301 if (QUOTED_NULL (string))
4302 {
4303 result[0] = '\0';
4304 return (result);
4305 }
4306
4307 /* A string consisting of only a single CTLESC should pass through unchanged */
4308 if (string[0] == CTLESC && string[1] == 0)
4309 {
4310 result[0] = CTLESC;
4311 result[1] = '\0';
4312 return (result);
4313 }
4314
4315 /* If no character in the string can be quoted, don't bother examining
4316 each character. Just return a copy of the string passed to us. */
4317 if (strchr (string, CTLESC) == NULL)
4318 return (strcpy (result, string));
4319
4320 send = string + slen;
4321 s = string;
4322 while (*s)
4323 {
4324 if (*s == CTLESC)
4325 {
4326 s++;
4327 if (*s == '\0')
4328 break;
4329 }
4330 COPY_CHAR_P (t, s, send);
4331 }
4332
4333 *t = '\0';
4334 return (result);
4335 }
4336
4337 /* Quote the entire WORD_LIST list. */
4338 static WORD_LIST *
4339 quote_list (list)
4340 WORD_LIST *list;
4341 {
4342 register WORD_LIST *w;
4343 char *t;
4344
4345 for (w = list; w; w = w->next)
4346 {
4347 t = w->word->word;
4348 w->word->word = quote_string (t);
4349 if (*t == 0)
4350 w->word->flags |= W_HASQUOTEDNULL; /* XXX - turn on W_HASQUOTEDNULL here? */
4351 w->word->flags |= W_QUOTED;
4352 free (t);
4353 }
4354 return list;
4355 }
4356
4357 WORD_DESC *
4358 dequote_word (word)
4359 WORD_DESC *word;
4360 {
4361 register char *s;
4362
4363 s = dequote_string (word->word);
4364 if (QUOTED_NULL (word->word))
4365 word->flags &= ~W_HASQUOTEDNULL;
4366 free (word->word);
4367 word->word = s;
4368
4369 return word;
4370 }
4371
4372 /* De-quote quoted characters in each word in LIST. */
4373 WORD_LIST *
4374 dequote_list (list)
4375 WORD_LIST *list;
4376 {
4377 register char *s;
4378 register WORD_LIST *tlist;
4379
4380 for (tlist = list; tlist; tlist = tlist->next)
4381 {
4382 s = dequote_string (tlist->word->word);
4383 if (QUOTED_NULL (tlist->word->word))
4384 tlist->word->flags &= ~W_HASQUOTEDNULL;
4385 free (tlist->word->word);
4386 tlist->word->word = s;
4387 }
4388 return list;
4389 }
4390
4391 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
4392 string. */
4393 char *
4394 remove_quoted_escapes (string)
4395 char *string;
4396 {
4397 char *t;
4398
4399 if (string)
4400 {
4401 t = dequote_escapes (string);
4402 strcpy (string, t);
4403 free (t);
4404 }
4405
4406 return (string);
4407 }
4408
4409 /* Remove quoted $IFS characters from STRING. Quoted IFS characters are
4410 added to protect them from word splitting, but we need to remove them
4411 if no word splitting takes place. This returns newly-allocated memory,
4412 so callers can use it to replace savestring(). */
4413 char *
4414 remove_quoted_ifs (string)
4415 char *string;
4416 {
4417 register size_t slen;
4418 register int i, j;
4419 char *ret, *send;
4420 DECLARE_MBSTATE;
4421
4422 slen = strlen (string);
4423 send = string + slen;
4424
4425 i = j = 0;
4426 ret = (char *)xmalloc (slen + 1);
4427
4428 while (i < slen)
4429 {
4430 if (string[i] == CTLESC)
4431 {
4432 i++;
4433 if (string[i] == 0 || isifs (string[i]) == 0)
4434 ret[j++] = CTLESC;
4435 if (i == slen)
4436 break;
4437 }
4438
4439 COPY_CHAR_I (ret, j, string, send, i);
4440 }
4441 ret[j] = '\0';
4442
4443 return (ret);
4444 }
4445
4446 char *
4447 remove_quoted_nulls (string)
4448 char *string;
4449 {
4450 register size_t slen;
4451 register int i, j, prev_i;
4452 DECLARE_MBSTATE;
4453
4454 if (strchr (string, CTLNUL) == 0) /* XXX */
4455 return string; /* XXX */
4456
4457 slen = strlen (string);
4458 i = j = 0;
4459
4460 while (i < slen)
4461 {
4462 if (string[i] == CTLESC)
4463 {
4464 /* Old code had j++, but we cannot assume that i == j at this
4465 point -- what if a CTLNUL has already been removed from the
4466 string? We don't want to drop the CTLESC or recopy characters
4467 that we've already copied down. */
4468 i++;
4469 string[j++] = CTLESC;
4470 if (i == slen)
4471 break;
4472 }
4473 else if (string[i] == CTLNUL)
4474 {
4475 i++;
4476 continue;
4477 }
4478
4479 prev_i = i;
4480 ADVANCE_CHAR (string, slen, i); /* COPY_CHAR_I? */
4481 if (j < prev_i)
4482 {
4483 do string[j++] = string[prev_i++]; while (prev_i < i);
4484 }
4485 else
4486 j = i;
4487 }
4488 string[j] = '\0';
4489
4490 return (string);
4491 }
4492
4493 /* Perform quoted null character removal on each element of LIST.
4494 This modifies LIST. */
4495 void
4496 word_list_remove_quoted_nulls (list)
4497 WORD_LIST *list;
4498 {
4499 register WORD_LIST *t;
4500
4501 for (t = list; t; t = t->next)
4502 {
4503 remove_quoted_nulls (t->word->word);
4504 t->word->flags &= ~W_HASQUOTEDNULL;
4505 }
4506 }
4507
4508 /* **************************************************************** */
4509 /* */
4510 /* Functions for Matching and Removing Patterns */
4511 /* */
4512 /* **************************************************************** */
4513
4514 #if defined (HANDLE_MULTIBYTE)
4515 # ifdef INCLUDE_UNUSED
4516 static unsigned char *
4517 mb_getcharlens (string, len)
4518 char *string;
4519 int len;
4520 {
4521 int i, offset, last;
4522 unsigned char *ret;
4523 char *p;
4524 DECLARE_MBSTATE;
4525
4526 i = offset = 0;
4527 last = 0;
4528 ret = (unsigned char *)xmalloc (len);
4529 memset (ret, 0, len);
4530 while (string[last])
4531 {
4532 ADVANCE_CHAR (string, len, offset);
4533 ret[last] = offset - last;
4534 last = offset;
4535 }
4536 return ret;
4537 }
4538 # endif
4539 #endif
4540
4541 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
4542 can have one of 4 values:
4543 RP_LONG_LEFT remove longest matching portion at start of PARAM
4544 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
4545 RP_LONG_RIGHT remove longest matching portion at end of PARAM
4546 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
4547 */
4548
4549 #define RP_LONG_LEFT 1
4550 #define RP_SHORT_LEFT 2
4551 #define RP_LONG_RIGHT 3
4552 #define RP_SHORT_RIGHT 4
4553
4554 /* Returns its first argument if nothing matched; new memory otherwise */
4555 static char *
4556 remove_upattern (param, pattern, op)
4557 char *param, *pattern;
4558 int op;
4559 {
4560 register size_t len;
4561 register char *end;
4562 register char *p, *ret, c;
4563
4564 len = STRLEN (param);
4565 end = param + len;
4566
4567 switch (op)
4568 {
4569 case RP_LONG_LEFT: /* remove longest match at start */
4570 for (p = end; p >= param; p--)
4571 {
4572 c = *p; *p = '\0';
4573 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4574 {
4575 *p = c;
4576 return (savestring (p));
4577 }
4578 *p = c;
4579
4580 }
4581 break;
4582
4583 case RP_SHORT_LEFT: /* remove shortest match at start */
4584 for (p = param; p <= end; p++)
4585 {
4586 c = *p; *p = '\0';
4587 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4588 {
4589 *p = c;
4590 return (savestring (p));
4591 }
4592 *p = c;
4593 }
4594 break;
4595
4596 case RP_LONG_RIGHT: /* remove longest match at end */
4597 for (p = param; p <= end; p++)
4598 {
4599 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4600 {
4601 c = *p; *p = '\0';
4602 ret = savestring (param);
4603 *p = c;
4604 return (ret);
4605 }
4606 }
4607 break;
4608
4609 case RP_SHORT_RIGHT: /* remove shortest match at end */
4610 for (p = end; p >= param; p--)
4611 {
4612 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4613 {
4614 c = *p; *p = '\0';
4615 ret = savestring (param);
4616 *p = c;
4617 return (ret);
4618 }
4619 }
4620 break;
4621 }
4622
4623 return (param); /* no match, return original string */
4624 }
4625
4626 #if defined (HANDLE_MULTIBYTE)
4627 /* Returns its first argument if nothing matched; new memory otherwise */
4628 static wchar_t *
4629 remove_wpattern (wparam, wstrlen, wpattern, op)
4630 wchar_t *wparam;
4631 size_t wstrlen;
4632 wchar_t *wpattern;
4633 int op;
4634 {
4635 wchar_t wc, *ret;
4636 int n;
4637
4638 switch (op)
4639 {
4640 case RP_LONG_LEFT: /* remove longest match at start */
4641 for (n = wstrlen; n >= 0; n--)
4642 {
4643 wc = wparam[n]; wparam[n] = L'\0';
4644 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4645 {
4646 wparam[n] = wc;
4647 return (wcsdup (wparam + n));
4648 }
4649 wparam[n] = wc;
4650 }
4651 break;
4652
4653 case RP_SHORT_LEFT: /* remove shortest match at start */
4654 for (n = 0; n <= wstrlen; n++)
4655 {
4656 wc = wparam[n]; wparam[n] = L'\0';
4657 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4658 {
4659 wparam[n] = wc;
4660 return (wcsdup (wparam + n));
4661 }
4662 wparam[n] = wc;
4663 }
4664 break;
4665
4666 case RP_LONG_RIGHT: /* remove longest match at end */
4667 for (n = 0; n <= wstrlen; n++)
4668 {
4669 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4670 {
4671 wc = wparam[n]; wparam[n] = L'\0';
4672 ret = wcsdup (wparam);
4673 wparam[n] = wc;
4674 return (ret);
4675 }
4676 }
4677 break;
4678
4679 case RP_SHORT_RIGHT: /* remove shortest match at end */
4680 for (n = wstrlen; n >= 0; n--)
4681 {
4682 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4683 {
4684 wc = wparam[n]; wparam[n] = L'\0';
4685 ret = wcsdup (wparam);
4686 wparam[n] = wc;
4687 return (ret);
4688 }
4689 }
4690 break;
4691 }
4692
4693 return (wparam); /* no match, return original string */
4694 }
4695 #endif /* HANDLE_MULTIBYTE */
4696
4697 static char *
4698 remove_pattern (param, pattern, op)
4699 char *param, *pattern;
4700 int op;
4701 {
4702 char *xret;
4703
4704 if (param == NULL)
4705 return (param);
4706 if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */
4707 return (savestring (param));
4708
4709 #if defined (HANDLE_MULTIBYTE)
4710 if (MB_CUR_MAX > 1)
4711 {
4712 wchar_t *ret, *oret;
4713 size_t n;
4714 wchar_t *wparam, *wpattern;
4715 mbstate_t ps;
4716
4717 /* XXX - could optimize here by checking param and pattern for multibyte
4718 chars with mbsmbchar and calling remove_upattern. */
4719
4720 n = xdupmbstowcs (&wpattern, NULL, pattern);
4721 if (n == (size_t)-1)
4722 {
4723 xret = remove_upattern (param, pattern, op);
4724 return ((xret == param) ? savestring (param) : xret);
4725 }
4726 n = xdupmbstowcs (&wparam, NULL, param);
4727
4728 if (n == (size_t)-1)
4729 {
4730 free (wpattern);
4731 xret = remove_upattern (param, pattern, op);
4732 return ((xret == param) ? savestring (param) : xret);
4733 }
4734 oret = ret = remove_wpattern (wparam, n, wpattern, op);
4735 /* Don't bother to convert wparam back to multibyte string if nothing
4736 matched; just return copy of original string */
4737 if (ret == wparam)
4738 {
4739 free (wparam);
4740 free (wpattern);
4741 return (savestring (param));
4742 }
4743
4744 free (wparam);
4745 free (wpattern);
4746
4747 n = strlen (param);
4748 xret = (char *)xmalloc (n + 1);
4749 memset (&ps, '\0', sizeof (mbstate_t));
4750 n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps);
4751 xret[n] = '\0'; /* just to make sure */
4752 free (oret);
4753 return xret;
4754 }
4755 else
4756 #endif
4757 {
4758 xret = remove_upattern (param, pattern, op);
4759 return ((xret == param) ? savestring (param) : xret);
4760 }
4761 }
4762
4763 /* Match PAT anywhere in STRING and return the match boundaries.
4764 This returns 1 in case of a successful match, 0 otherwise. SP
4765 and EP are pointers into the string where the match begins and
4766 ends, respectively. MTYPE controls what kind of match is attempted.
4767 MATCH_BEG and MATCH_END anchor the match at the beginning and end
4768 of the string, respectively. The longest match is returned. */
4769 static int
4770 match_upattern (string, pat, mtype, sp, ep)
4771 char *string, *pat;
4772 int mtype;
4773 char **sp, **ep;
4774 {
4775 int c, mlen;
4776 size_t len;
4777 register char *p, *p1, *npat;
4778 char *end;
4779
4780 /* If the pattern doesn't match anywhere in the string, go ahead and
4781 short-circuit right away. A minor optimization, saves a bunch of
4782 unnecessary calls to strmatch (up to N calls for a string of N
4783 characters) if the match is unsuccessful. To preserve the semantics
4784 of the substring matches below, we make sure that the pattern has
4785 `*' as first and last character, making a new pattern if necessary. */
4786 /* XXX - check this later if I ever implement `**' with special meaning,
4787 since this will potentially result in `**' at the beginning or end */
4788 len = STRLEN (pat);
4789 if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*')
4790 {
4791 int unescaped_backslash;
4792 char *pp;
4793
4794 p = npat = (char *)xmalloc (len + 3);
4795 p1 = pat;
4796 if ((mtype != MATCH_BEG) && (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob)))
4797 *p++ = '*';
4798 while (*p1)
4799 *p++ = *p1++;
4800 #if 1
4801 /* Need to also handle a pattern that ends with an unescaped backslash.
4802 For right now, we ignore it because the pattern matching code will
4803 fail the match anyway */
4804 /* If the pattern ends with a `*' we leave it alone if it's preceded by
4805 an even number of backslashes, but if it's escaped by a backslash
4806 we need to add another `*'. */
4807 if ((mtype != MATCH_END) && (p1[-1] == '*' && (unescaped_backslash = p1[-2] == '\\')))
4808 {
4809 pp = p1 - 3;
4810 while (pp >= pat && *pp-- == '\\')
4811 unescaped_backslash = 1 - unescaped_backslash;
4812 if (unescaped_backslash)
4813 *p++ = '*';
4814 }
4815 else if (mtype != MATCH_END && p1[-1] != '*')
4816 *p++ = '*';
4817 #else
4818 if (p1[-1] != '*' || p1[-2] == '\\')
4819 *p++ = '*';
4820 #endif
4821 *p = '\0';
4822 }
4823 else
4824 npat = pat;
4825 c = strmatch (npat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE);
4826 if (npat != pat)
4827 free (npat);
4828 if (c == FNM_NOMATCH)
4829 return (0);
4830
4831 len = STRLEN (string);
4832 end = string + len;
4833
4834 mlen = umatchlen (pat, len);
4835 if (mlen > (int)len)
4836 return (0);
4837
4838 switch (mtype)
4839 {
4840 case MATCH_ANY:
4841 for (p = string; p <= end; p++)
4842 {
4843 if (match_pattern_char (pat, p, FNMATCH_IGNCASE))
4844 {
4845 p1 = (mlen == -1) ? end : p + mlen;
4846 /* p1 - p = length of portion of string to be considered
4847 p = current position in string
4848 mlen = number of characters consumed by match (-1 for entire string)
4849 end = end of string
4850 we want to break immediately if the potential match len
4851 is greater than the number of characters remaining in the
4852 string
4853 */
4854 if (p1 > end)
4855 break;
4856 for ( ; p1 >= p; p1--)
4857 {
4858 c = *p1; *p1 = '\0';
4859 if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
4860 {
4861 *p1 = c;
4862 *sp = p;
4863 *ep = p1;
4864 return 1;
4865 }
4866 *p1 = c;
4867 #if 1
4868 /* If MLEN != -1, we have a fixed length pattern. */
4869 if (mlen != -1)
4870 break;
4871 #endif
4872 }
4873 }
4874 }
4875
4876 return (0);
4877
4878 case MATCH_BEG:
4879 if (match_pattern_char (pat, string, FNMATCH_IGNCASE) == 0)
4880 return (0);
4881
4882 for (p = (mlen == -1) ? end : string + mlen; p >= string; p--)
4883 {
4884 c = *p; *p = '\0';
4885 if (strmatch (pat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
4886 {
4887 *p = c;
4888 *sp = string;
4889 *ep = p;
4890 return 1;
4891 }
4892 *p = c;
4893 /* If MLEN != -1, we have a fixed length pattern. */
4894 if (mlen != -1)
4895 break;
4896 }
4897
4898 return (0);
4899
4900 case MATCH_END:
4901 for (p = end - ((mlen == -1) ? len : mlen); p <= end; p++)
4902 {
4903 if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
4904 {
4905 *sp = p;
4906 *ep = end;
4907 return 1;
4908 }
4909 /* If MLEN != -1, we have a fixed length pattern. */
4910 if (mlen != -1)
4911 break;
4912 }
4913
4914 return (0);
4915 }
4916
4917 return (0);
4918 }
4919
4920 #if defined (HANDLE_MULTIBYTE)
4921
4922 #define WFOLD(c) (match_ignore_case && iswupper (c) ? towlower (c) : (c))
4923
4924 /* Match WPAT anywhere in WSTRING and return the match boundaries.
4925 This returns 1 in case of a successful match, 0 otherwise. Wide
4926 character version. */
4927 static int
4928 match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
4929 wchar_t *wstring;
4930 char **indices;
4931 size_t wstrlen;
4932 wchar_t *wpat;
4933 int mtype;
4934 char **sp, **ep;
4935 {
4936 wchar_t wc, *wp, *nwpat, *wp1;
4937 size_t len;
4938 int mlen;
4939 int n, n1, n2, simple;
4940
4941 simple = (wpat[0] != L'\\' && wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'[');
4942 #if defined (EXTENDED_GLOB)
4943 if (extended_glob)
4944 simple &= (wpat[1] != L'(' || (wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'+' && wpat[0] != L'!' && wpat[0] != L'@')); /*)*/
4945 #endif
4946
4947 /* If the pattern doesn't match anywhere in the string, go ahead and
4948 short-circuit right away. A minor optimization, saves a bunch of
4949 unnecessary calls to strmatch (up to N calls for a string of N
4950 characters) if the match is unsuccessful. To preserve the semantics
4951 of the substring matches below, we make sure that the pattern has
4952 `*' as first and last character, making a new pattern if necessary. */
4953 len = wcslen (wpat);
4954 if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*')
4955 {
4956 int unescaped_backslash;
4957 wchar_t *wpp;
4958
4959 wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t));
4960 wp1 = wpat;
4961 if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob))
4962 *wp++ = L'*';
4963 while (*wp1 != L'\0')
4964 *wp++ = *wp1++;
4965 #if 1
4966 /* See comments above in match_upattern. */
4967 if (wp1[-1] == L'*' && (unescaped_backslash = wp1[-2] == L'\\'))
4968 {
4969 wpp = wp1 - 3;
4970 while (wpp >= wpat && *wpp-- == L'\\')
4971 unescaped_backslash = 1 - unescaped_backslash;
4972 if (unescaped_backslash)
4973 *wp++ = L'*';
4974 }
4975 else if (wp1[-1] != L'*')
4976 *wp++ = L'*';
4977 #else
4978 if (wp1[-1] != L'*' || wp1[-2] == L'\\')
4979 *wp++ = L'*';
4980 #endif
4981 *wp = '\0';
4982 }
4983 else
4984 nwpat = wpat;
4985 len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE);
4986 if (nwpat != wpat)
4987 free (nwpat);
4988 if (len == FNM_NOMATCH)
4989 return (0);
4990
4991 mlen = wmatchlen (wpat, wstrlen);
4992 if (mlen > (int)wstrlen)
4993 return (0);
4994
4995 /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */
4996 switch (mtype)
4997 {
4998 case MATCH_ANY:
4999 for (n = 0; n <= wstrlen; n++)
5000 {
5001 n2 = simple ? (WFOLD(*wpat) == WFOLD(wstring[n])) : match_pattern_wchar (wpat, wstring + n, FNMATCH_IGNCASE);
5002 if (n2)
5003 {
5004 n1 = (mlen == -1) ? wstrlen : n + mlen;
5005 if (n1 > wstrlen)
5006 break;
5007
5008 for ( ; n1 >= n; n1--)
5009 {
5010 wc = wstring[n1]; wstring[n1] = L'\0';
5011 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
5012 {
5013 wstring[n1] = wc;
5014 *sp = indices[n];
5015 *ep = indices[n1];
5016 return 1;
5017 }
5018 wstring[n1] = wc;
5019 /* If MLEN != -1, we have a fixed length pattern. */
5020 if (mlen != -1)
5021 break;
5022 }
5023 }
5024 }
5025
5026 return (0);
5027
5028 case MATCH_BEG:
5029 if (match_pattern_wchar (wpat, wstring, FNMATCH_IGNCASE) == 0)
5030 return (0);
5031
5032 for (n = (mlen == -1) ? wstrlen : mlen; n >= 0; n--)
5033 {
5034 wc = wstring[n]; wstring[n] = L'\0';
5035 if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
5036 {
5037 wstring[n] = wc;
5038 *sp = indices[0];
5039 *ep = indices[n];
5040 return 1;
5041 }
5042 wstring[n] = wc;
5043 /* If MLEN != -1, we have a fixed length pattern. */
5044 if (mlen != -1)
5045 break;
5046 }
5047
5048 return (0);
5049
5050 case MATCH_END:
5051 for (n = wstrlen - ((mlen == -1) ? wstrlen : mlen); n <= wstrlen; n++)
5052 {
5053 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
5054 {
5055 *sp = indices[n];
5056 *ep = indices[wstrlen];
5057 return 1;
5058 }
5059 /* If MLEN != -1, we have a fixed length pattern. */
5060 if (mlen != -1)
5061 break;
5062 }
5063
5064 return (0);
5065 }
5066
5067 return (0);
5068 }
5069 #undef WFOLD
5070 #endif /* HANDLE_MULTIBYTE */
5071
5072 static int
5073 match_pattern (string, pat, mtype, sp, ep)
5074 char *string, *pat;
5075 int mtype;
5076 char **sp, **ep;
5077 {
5078 #if defined (HANDLE_MULTIBYTE)
5079 int ret;
5080 size_t n;
5081 wchar_t *wstring, *wpat;
5082 char **indices;
5083 #endif
5084
5085 if (string == 0 || pat == 0 || *pat == 0)
5086 return (0);
5087
5088 #if defined (HANDLE_MULTIBYTE)
5089 if (MB_CUR_MAX > 1)
5090 {
5091 if (mbsmbchar (string) == 0 && mbsmbchar (pat) == 0)
5092 return (match_upattern (string, pat, mtype, sp, ep));
5093
5094 n = xdupmbstowcs (&wpat, NULL, pat);
5095 if (n == (size_t)-1)
5096 return (match_upattern (string, pat, mtype, sp, ep));
5097 n = xdupmbstowcs (&wstring, &indices, string);
5098 if (n == (size_t)-1)
5099 {
5100 free (wpat);
5101 return (match_upattern (string, pat, mtype, sp, ep));
5102 }
5103 ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep);
5104
5105 free (wpat);
5106 free (wstring);
5107 free (indices);
5108
5109 return (ret);
5110 }
5111 else
5112 #endif
5113 return (match_upattern (string, pat, mtype, sp, ep));
5114 }
5115
5116 static int
5117 getpatspec (c, value)
5118 int c;
5119 char *value;
5120 {
5121 if (c == '#')
5122 return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
5123 else /* c == '%' */
5124 return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
5125 }
5126
5127 /* Posix.2 says that the WORD should be run through tilde expansion,
5128 parameter expansion, command substitution and arithmetic expansion.
5129 This leaves the result quoted, so quote_string_for_globbing () has
5130 to be called to fix it up for strmatch (). If QUOTED is non-zero,
5131 it means that the entire expression was enclosed in double quotes.
5132 This means that quoting characters in the pattern do not make any
5133 special pattern characters quoted. For example, the `*' in the
5134 following retains its special meaning: "${foo#'*'}". */
5135 static char *
5136 getpattern (value, quoted, expandpat)
5137 char *value;
5138 int quoted, expandpat;
5139 {
5140 char *pat, *tword;
5141 WORD_LIST *l;
5142 #if 0
5143 int i;
5144 #endif
5145 /* There is a problem here: how to handle single or double quotes in the
5146 pattern string when the whole expression is between double quotes?
5147 POSIX.2 says that enclosing double quotes do not cause the pattern to
5148 be quoted, but does that leave us a problem with @ and array[@] and their
5149 expansions inside a pattern? */
5150 #if 0
5151 if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
5152 {
5153 i = 0;
5154 pat = string_extract_double_quoted (tword, &i, SX_STRIPDQ);
5155 free (tword);
5156 tword = pat;
5157 }
5158 #endif
5159
5160 /* expand_string_for_pat () leaves WORD quoted and does not perform
5161 word splitting. */
5162 l = *value ? expand_string_for_pat (value,
5163 (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
5164 (int *)NULL, (int *)NULL)
5165 : (WORD_LIST *)0;
5166 if (l)
5167 word_list_remove_quoted_nulls (l);
5168 pat = string_list (l);
5169 dispose_words (l);
5170 if (pat)
5171 {
5172 tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
5173 free (pat);
5174 pat = tword;
5175 }
5176 return (pat);
5177 }
5178
5179 #if 0
5180 /* Handle removing a pattern from a string as a result of ${name%[%]value}
5181 or ${name#[#]value}. */
5182 static char *
5183 variable_remove_pattern (value, pattern, patspec, quoted)
5184 char *value, *pattern;
5185 int patspec, quoted;
5186 {
5187 char *tword;
5188
5189 tword = remove_pattern (value, pattern, patspec);
5190
5191 return (tword);
5192 }
5193 #endif
5194
5195 static char *
5196 list_remove_pattern (list, pattern, patspec, itype, quoted)
5197 WORD_LIST *list;
5198 char *pattern;
5199 int patspec, itype, quoted;
5200 {
5201 WORD_LIST *new, *l;
5202 WORD_DESC *w;
5203 char *tword;
5204
5205 for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
5206 {
5207 tword = remove_pattern (l->word->word, pattern, patspec);
5208 w = alloc_word_desc ();
5209 w->word = tword ? tword : savestring ("");
5210 new = make_word_list (w, new);
5211 }
5212
5213 l = REVERSE_LIST (new, WORD_LIST *);
5214 tword = string_list_pos_params (itype, l, quoted, 0);
5215 dispose_words (l);
5216
5217 return (tword);
5218 }
5219
5220 static char *
5221 parameter_list_remove_pattern (itype, pattern, patspec, quoted)
5222 int itype;
5223 char *pattern;
5224 int patspec, quoted;
5225 {
5226 char *ret;
5227 WORD_LIST *list;
5228
5229 list = list_rest_of_args ();
5230 if (list == 0)
5231 return ((char *)NULL);
5232 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
5233 dispose_words (list);
5234 return (ret);
5235 }
5236
5237 #if defined (ARRAY_VARS)
5238 static char *
5239 array_remove_pattern (var, pattern, patspec, starsub, quoted)
5240 SHELL_VAR *var;
5241 char *pattern;
5242 int patspec;
5243 int starsub; /* so we can figure out how it's indexed */
5244 int quoted;
5245 {
5246 ARRAY *a;
5247 HASH_TABLE *h;
5248 int itype;
5249 char *ret;
5250 WORD_LIST *list;
5251 SHELL_VAR *v;
5252
5253 v = var; /* XXX - for now */
5254
5255 itype = starsub ? '*' : '@';
5256
5257 a = (v && array_p (v)) ? array_cell (v) : 0;
5258 h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
5259
5260 list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
5261 if (list == 0)
5262 return ((char *)NULL);
5263 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
5264 dispose_words (list);
5265
5266 return ret;
5267 }
5268 #endif /* ARRAY_VARS */
5269
5270 static char *
5271 parameter_brace_remove_pattern (varname, value, ind, patstr, rtype, quoted, flags)
5272 char *varname, *value;
5273 int ind;
5274 char *patstr;
5275 int rtype, quoted, flags;
5276 {
5277 int vtype, patspec, starsub;
5278 char *temp1, *val, *pattern, *oname;
5279 SHELL_VAR *v;
5280
5281 if (value == 0)
5282 return ((char *)NULL);
5283
5284 oname = this_command_name;
5285 this_command_name = varname;
5286
5287 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
5288 if (vtype == -1)
5289 {
5290 this_command_name = oname;
5291 return ((char *)NULL);
5292 }
5293
5294 starsub = vtype & VT_STARSUB;
5295 vtype &= ~VT_STARSUB;
5296
5297 patspec = getpatspec (rtype, patstr);
5298 if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
5299 patstr++;
5300
5301 /* Need to pass getpattern newly-allocated memory in case of expansion --
5302 the expansion code will free the passed string on an error. */
5303 temp1 = savestring (patstr);
5304 pattern = getpattern (temp1, quoted, 1);
5305 free (temp1);
5306
5307 temp1 = (char *)NULL; /* shut up gcc */
5308 switch (vtype)
5309 {
5310 case VT_VARIABLE:
5311 case VT_ARRAYMEMBER:
5312 temp1 = remove_pattern (val, pattern, patspec);
5313 if (vtype == VT_VARIABLE)
5314 FREE (val);
5315 if (temp1)
5316 {
5317 val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
5318 ? quote_string (temp1)
5319 : quote_escapes (temp1);
5320 free (temp1);
5321 temp1 = val;
5322 }
5323 break;
5324 #if defined (ARRAY_VARS)
5325 case VT_ARRAYVAR:
5326 temp1 = array_remove_pattern (v, pattern, patspec, starsub, quoted);
5327 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
5328 {
5329 val = quote_escapes (temp1);
5330 free (temp1);
5331 temp1 = val;
5332 }
5333 break;
5334 #endif
5335 case VT_POSPARMS:
5336 temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
5337 if (temp1 && quoted == 0 && ifs_is_null)
5338 {
5339 /* Posix interp 888 */
5340 }
5341 else if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
5342 {
5343 val = quote_escapes (temp1);
5344 free (temp1);
5345 temp1 = val;
5346 }
5347 break;
5348 }
5349
5350 this_command_name = oname;
5351
5352 FREE (pattern);
5353 return temp1;
5354 }
5355
5356 #if defined (PROCESS_SUBSTITUTION)
5357
5358 static void reap_some_procsubs PARAMS((int));
5359
5360 /*****************************************************************/
5361 /* */
5362 /* Hacking Process Substitution */
5363 /* */
5364 /*****************************************************************/
5365
5366 #if !defined (HAVE_DEV_FD)
5367 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
5368 of FIFOs the shell has open. unlink_fifo_list will walk the list and
5369 unlink the ones that don't have a living process on the other end.
5370 unlink_all_fifos will walk the list and unconditionally unlink them, trying
5371 to open and close the FIFO first to release any child processes sleeping on
5372 the FIFO. add_fifo_list adds the name of an open FIFO to the list.
5373 NFIFO is a count of the number of FIFOs in the list. */
5374 #define FIFO_INCR 20
5375
5376 /* PROC value of -1 means the process has been reaped and the FIFO needs to
5377 be removed. PROC value of 0 means the slot is unused. */
5378 struct temp_fifo {
5379 char *file;
5380 pid_t proc;
5381 };
5382
5383 static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
5384 static int nfifo;
5385 static int fifo_list_size;
5386
5387 void
5388 clear_fifo_list ()
5389 {
5390 int i;
5391
5392 for (i = 0; i < fifo_list_size; i++)
5393 {
5394 if (fifo_list[i].file)
5395 free (fifo_list[i].file);
5396 fifo_list[i].file = NULL;
5397 fifo_list[i].proc = 0;
5398 }
5399 nfifo = 0;
5400 }
5401
5402 void *
5403 copy_fifo_list (sizep)
5404 int *sizep;
5405 {
5406 if (sizep)
5407 *sizep = 0;
5408 return (void *)NULL;
5409 }
5410
5411 static void
5412 add_fifo_list (pathname)
5413 char *pathname;
5414 {
5415 int osize, i;
5416
5417 if (nfifo >= fifo_list_size - 1)
5418 {
5419 osize = fifo_list_size;
5420 fifo_list_size += FIFO_INCR;
5421 fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
5422 fifo_list_size * sizeof (struct temp_fifo));
5423 for (i = osize; i < fifo_list_size; i++)
5424 {
5425 fifo_list[i].file = (char *)NULL;
5426 fifo_list[i].proc = 0; /* unused */
5427 }
5428 }
5429
5430 fifo_list[nfifo].file = savestring (pathname);
5431 nfifo++;
5432 }
5433
5434 void
5435 unlink_fifo (i)
5436 int i;
5437 {
5438 if ((fifo_list[i].proc == (pid_t)-1) || (fifo_list[i].proc > 0 && (kill(fifo_list[i].proc, 0) == -1)))
5439 {
5440 unlink (fifo_list[i].file);
5441 free (fifo_list[i].file);
5442 fifo_list[i].file = (char *)NULL;
5443 fifo_list[i].proc = 0;
5444 }
5445 }
5446
5447 void
5448 unlink_fifo_list ()
5449 {
5450 int saved, i, j;
5451
5452 if (nfifo == 0)
5453 return;
5454
5455 for (i = saved = 0; i < nfifo; i++)
5456 {
5457 if ((fifo_list[i].proc == (pid_t)-1) || (fifo_list[i].proc > 0 && (kill(fifo_list[i].proc, 0) == -1)))
5458 {
5459 unlink (fifo_list[i].file);
5460 free (fifo_list[i].file);
5461 fifo_list[i].file = (char *)NULL;
5462 fifo_list[i].proc = 0;
5463 }
5464 else
5465 saved++;
5466 }
5467
5468 /* If we didn't remove some of the FIFOs, compact the list. */
5469 if (saved)
5470 {
5471 for (i = j = 0; i < nfifo; i++)
5472 if (fifo_list[i].file)
5473 {
5474 if (i != j)
5475 {
5476 fifo_list[j].file = fifo_list[i].file;
5477 fifo_list[j].proc = fifo_list[i].proc;
5478 fifo_list[i].file = (char *)NULL;
5479 fifo_list[i].proc = 0;
5480 }
5481 j++;
5482 }
5483 nfifo = j;
5484 }
5485 else
5486 nfifo = 0;
5487 }
5488
5489 void
5490 unlink_all_fifos ()
5491 {
5492 int i, fd;
5493
5494 if (nfifo == 0)
5495 return;
5496
5497 for (i = 0; i < nfifo; i++)
5498 {
5499 fifo_list[i].proc = (pid_t)-1;
5500 fd = open (fifo_list[i].file, O_RDWR|O_NONBLOCK);
5501 unlink_fifo (i);
5502 if (fd >= 0)
5503 close (fd);
5504 }
5505
5506 nfifo = 0;
5507 }
5508
5509 /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list
5510 from some point in the past, and close all open FIFOs in fifo_list
5511 that are not marked as active in LIST. If LIST is NULL, close
5512 everything in fifo_list. LSIZE is the number of elements in LIST, in
5513 case it's larger than fifo_list_size (size of fifo_list). */
5514 void
5515 close_new_fifos (list, lsize)
5516 void *list;
5517 int lsize;
5518 {
5519 int i;
5520 char *plist;
5521
5522 if (list == 0)
5523 {
5524 unlink_fifo_list ();
5525 return;
5526 }
5527
5528 for (plist = (char *)list, i = 0; i < lsize; i++)
5529 if (plist[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1)
5530 unlink_fifo (i);
5531
5532 for (i = lsize; i < fifo_list_size; i++)
5533 unlink_fifo (i);
5534 }
5535
5536 int
5537 find_procsub_child (pid)
5538 pid_t pid;
5539 {
5540 int i;
5541
5542 for (i = 0; i < nfifo; i++)
5543 if (fifo_list[i].proc == pid)
5544 return i;
5545 return -1;
5546 }
5547
5548 void
5549 set_procsub_status (ind, pid, status)
5550 int ind;
5551 pid_t pid;
5552 int status;
5553 {
5554 if (ind >= 0 && ind < nfifo)
5555 fifo_list[ind].proc = (pid_t)-1; /* sentinel */
5556 }
5557
5558 /* If we've marked the process for this procsub as dead, close the
5559 associated file descriptor and delete the FIFO. */
5560 static void
5561 reap_some_procsubs (max)
5562 int max;
5563 {
5564 int i;
5565
5566 for (i = 0; i < max; i++)
5567 if (fifo_list[i].proc == (pid_t)-1) /* reaped */
5568 unlink_fifo (i);
5569 }
5570
5571 void
5572 reap_procsubs ()
5573 {
5574 reap_some_procsubs (nfifo);
5575 }
5576
5577 #if 0
5578 /* UNUSED */
5579 void
5580 wait_procsubs ()
5581 {
5582 int i, r;
5583
5584 for (i = 0; i < nfifo; i++)
5585 {
5586 if (fifo_list[i].proc != (pid_t)-1 && fifo_list[i].proc > 0)
5587 {
5588 r = wait_for (fifo_list[i].proc, 0);
5589 save_proc_status (fifo_list[i].proc, r);
5590 fifo_list[i].proc = (pid_t)-1;
5591 }
5592 }
5593 }
5594 #endif
5595
5596 int
5597 fifos_pending ()
5598 {
5599 return nfifo;
5600 }
5601
5602 int
5603 num_fifos ()
5604 {
5605 return nfifo;
5606 }
5607
5608 static char *
5609 make_named_pipe ()
5610 {
5611 char *tname;
5612
5613 tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR);
5614 if (mkfifo (tname, 0600) < 0)
5615 {
5616 free (tname);
5617 return ((char *)NULL);
5618 }
5619
5620 add_fifo_list (tname);
5621 return (tname);
5622 }
5623
5624 #else /* HAVE_DEV_FD */
5625
5626 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
5627 has open to children. NFDS is a count of the number of bits currently
5628 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
5629 of open files. */
5630 /* dev_fd_list[I] value of -1 means the process has been reaped and file
5631 descriptor I needs to be closed. Value of 0 means the slot is unused. */
5632
5633 static pid_t *dev_fd_list = (pid_t *)NULL;
5634 static int nfds;
5635 static int totfds; /* The highest possible number of open files. */
5636
5637 void
5638 clear_fifo (i)
5639 int i;
5640 {
5641 if (dev_fd_list[i])
5642 {
5643 dev_fd_list[i] = 0;
5644 nfds--;
5645 }
5646 }
5647
5648 void
5649 clear_fifo_list ()
5650 {
5651 register int i;
5652
5653 if (nfds == 0)
5654 return;
5655
5656 for (i = 0; nfds && i < totfds; i++)
5657 clear_fifo (i);
5658
5659 nfds = 0;
5660 }
5661
5662 void *
5663 copy_fifo_list (sizep)
5664 int *sizep;
5665 {
5666 void *ret;
5667
5668 if (nfds == 0 || totfds == 0)
5669 {
5670 if (sizep)
5671 *sizep = 0;
5672 return (void *)NULL;
5673 }
5674
5675 if (sizep)
5676 *sizep = totfds;
5677 ret = xmalloc (totfds * sizeof (pid_t));
5678 return (memcpy (ret, dev_fd_list, totfds * sizeof (pid_t)));
5679 }
5680
5681 static void
5682 add_fifo_list (fd)
5683 int fd;
5684 {
5685 if (dev_fd_list == 0 || fd >= totfds)
5686 {
5687 int ofds;
5688
5689 ofds = totfds;
5690 totfds = getdtablesize ();
5691 if (totfds < 0 || totfds > 256)
5692 totfds = 256;
5693 if (fd >= totfds)
5694 totfds = fd + 2;
5695
5696 dev_fd_list = (pid_t *)xrealloc (dev_fd_list, totfds * sizeof (dev_fd_list[0]));
5697 /* XXX - might need a loop for this */
5698 memset (dev_fd_list + ofds, '\0', (totfds - ofds) * sizeof (pid_t));
5699 }
5700
5701 dev_fd_list[fd] = 1; /* marker; updated later */
5702 nfds++;
5703 }
5704
5705 int
5706 fifos_pending ()
5707 {
5708 return 0; /* used for cleanup; not needed with /dev/fd */
5709 }
5710
5711 int
5712 num_fifos ()
5713 {
5714 return nfds;
5715 }
5716
5717 void
5718 unlink_fifo (fd)
5719 int fd;
5720 {
5721 if (dev_fd_list[fd])
5722 {
5723 close (fd);
5724 dev_fd_list[fd] = 0;
5725 nfds--;
5726 }
5727 }
5728
5729 void
5730 unlink_fifo_list ()
5731 {
5732 register int i;
5733
5734 if (nfds == 0)
5735 return;
5736
5737 for (i = totfds-1; nfds && i >= 0; i--)
5738 unlink_fifo (i);
5739
5740 nfds = 0;
5741 }
5742
5743 void
5744 unlink_all_fifos ()
5745 {
5746 unlink_fifo_list ();
5747 }
5748
5749 /* Take LIST, which is a snapshot copy of dev_fd_list from some point in
5750 the past, and close all open fds in dev_fd_list that are not marked
5751 as open in LIST. If LIST is NULL, close everything in dev_fd_list.
5752 LSIZE is the number of elements in LIST, in case it's larger than
5753 totfds (size of dev_fd_list). */
5754 void
5755 close_new_fifos (list, lsize)
5756 void *list;
5757 int lsize;
5758 {
5759 int i;
5760 pid_t *plist;
5761
5762 if (list == 0)
5763 {
5764 unlink_fifo_list ();
5765 return;
5766 }
5767
5768 for (plist = (pid_t *)list, i = 0; i < lsize; i++)
5769 if (plist[i] == 0 && i < totfds && dev_fd_list[i])
5770 unlink_fifo (i);
5771
5772 for (i = lsize; i < totfds; i++)
5773 unlink_fifo (i);
5774 }
5775
5776 int
5777 find_procsub_child (pid)
5778 pid_t pid;
5779 {
5780 int i;
5781
5782 if (nfds == 0)
5783 return -1;
5784
5785 for (i = 0; i < totfds; i++)
5786 if (dev_fd_list[i] == pid)
5787 return i;
5788
5789 return -1;
5790 }
5791
5792 void
5793 set_procsub_status (ind, pid, status)
5794 int ind;
5795 pid_t pid;
5796 int status;
5797 {
5798 if (ind >= 0 && ind < totfds)
5799 dev_fd_list[ind] = (pid_t)-1; /* sentinel */
5800 }
5801
5802 /* If we've marked the process for this procsub as dead, close the
5803 associated file descriptor. */
5804 static void
5805 reap_some_procsubs (max)
5806 int max;
5807 {
5808 int i;
5809
5810 for (i = 0; nfds > 0 && i < max; i++)
5811 if (dev_fd_list[i] == (pid_t)-1)
5812 unlink_fifo (i);
5813 }
5814
5815 void
5816 reap_procsubs ()
5817 {
5818 reap_some_procsubs (totfds);
5819 }
5820
5821 #if 0
5822 /* UNUSED */
5823 void
5824 wait_procsubs ()
5825 {
5826 int i, r;
5827
5828 for (i = 0; nfds > 0 && i < totfds; i++)
5829 {
5830 if (dev_fd_list[i] != (pid_t)-1 && dev_fd_list[i] > 0)
5831 {
5832 r = wait_for (dev_fd_list[i], 0);
5833 save_proc_status (dev_fd_list[i], r);
5834 dev_fd_list[i] = (pid_t)-1;
5835 }
5836 }
5837 }
5838 #endif
5839
5840 #if defined (NOTDEF)
5841 print_dev_fd_list ()
5842 {
5843 register int i;
5844
5845 fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
5846 fflush (stderr);
5847
5848 for (i = 0; i < totfds; i++)
5849 {
5850 if (dev_fd_list[i])
5851 fprintf (stderr, " %d", i);
5852 }
5853 fprintf (stderr, "\n");
5854 }
5855 #endif /* NOTDEF */
5856
5857 static char *
5858 make_dev_fd_filename (fd)
5859 int fd;
5860 {
5861 char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
5862
5863 ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8);
5864
5865 strcpy (ret, DEV_FD_PREFIX);
5866 p = inttostr (fd, intbuf, sizeof (intbuf));
5867 strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
5868
5869 add_fifo_list (fd);
5870 return (ret);
5871 }
5872
5873 #endif /* HAVE_DEV_FD */
5874
5875 /* Return a filename that will open a connection to the process defined by
5876 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
5877 a filename in /dev/fd corresponding to a descriptor that is one of the
5878 ends of the pipe. If not defined, we use named pipes on systems that have
5879 them. Systems without /dev/fd and named pipes are out of luck.
5880
5881 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
5882 use the read end of the pipe and dup that file descriptor to fd 0 in
5883 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
5884 writing or use the write end of the pipe in the child, and dup that
5885 file descriptor to fd 1 in the child. The parent does the opposite. */
5886
5887 static char *
5888 process_substitute (string, open_for_read_in_child)
5889 char *string;
5890 int open_for_read_in_child;
5891 {
5892 char *pathname;
5893 int fd, result, rc, function_value;
5894 pid_t old_pid, pid;
5895 #if defined (HAVE_DEV_FD)
5896 int parent_pipe_fd, child_pipe_fd;
5897 int fildes[2];
5898 #endif /* HAVE_DEV_FD */
5899 #if defined (JOB_CONTROL)
5900 pid_t old_pipeline_pgrp;
5901 #endif
5902
5903 if (!string || !*string || wordexp_only)
5904 return ((char *)NULL);
5905
5906 #if !defined (HAVE_DEV_FD)
5907 pathname = make_named_pipe ();
5908 #else /* HAVE_DEV_FD */
5909 if (pipe (fildes) < 0)
5910 {
5911 sys_error ("%s", _("cannot make pipe for process substitution"));
5912 return ((char *)NULL);
5913 }
5914 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
5915 the pipe in the parent, otherwise the read end. */
5916 parent_pipe_fd = fildes[open_for_read_in_child];
5917 child_pipe_fd = fildes[1 - open_for_read_in_child];
5918 /* Move the parent end of the pipe to some high file descriptor, to
5919 avoid clashes with FDs used by the script. */
5920 parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
5921
5922 pathname = make_dev_fd_filename (parent_pipe_fd);
5923 #endif /* HAVE_DEV_FD */
5924
5925 if (pathname == 0)
5926 {
5927 sys_error ("%s", _("cannot make pipe for process substitution"));
5928 return ((char *)NULL);
5929 }
5930
5931 old_pid = last_made_pid;
5932
5933 #if defined (JOB_CONTROL)
5934 old_pipeline_pgrp = pipeline_pgrp;
5935 if (pipeline_pgrp == 0 || (subshell_environment & (SUBSHELL_PIPE|SUBSHELL_FORK|SUBSHELL_ASYNC)) == 0)
5936 pipeline_pgrp = shell_pgrp;
5937 save_pipeline (1);
5938 #endif /* JOB_CONTROL */
5939
5940 pid = make_child ((char *)NULL, FORK_ASYNC);
5941 if (pid == 0)
5942 {
5943 #if 0
5944 int old_interactive;
5945
5946 old_interactive = interactive;
5947 #endif
5948 /* The currently-executing shell is not interactive */
5949 interactive = 0;
5950
5951 reset_terminating_signals (); /* XXX */
5952 free_pushed_string_input ();
5953 /* Cancel traps, in trap.c. */
5954 restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */
5955 subshell_environment &= ~SUBSHELL_IGNTRAP;
5956 QUIT; /* catch any interrupts we got post-fork */
5957 setup_async_signals ();
5958 #if 0
5959 if (open_for_read_in_child == 0 && old_interactive && (bash_input.type == st_stdin || bash_input.type == st_stream))
5960 async_redirect_stdin ();
5961 #endif
5962
5963 subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB|SUBSHELL_ASYNC;
5964
5965 /* We don't inherit the verbose option for command substitutions now, so
5966 let's try it for process substitutions. */
5967 change_flag ('v', FLAG_OFF);
5968
5969 /* if we're expanding a redirection, we shouldn't have access to the
5970 temporary environment, but commands in the subshell should have
5971 access to their own temporary environment. */
5972 if (expanding_redir)
5973 flush_temporary_env ();
5974 }
5975
5976 #if defined (JOB_CONTROL)
5977 set_sigchld_handler ();
5978 stop_making_children ();
5979 /* XXX - should we only do this in the parent? (as in command subst) */
5980 pipeline_pgrp = old_pipeline_pgrp;
5981 #else
5982 stop_making_children ();
5983 #endif /* JOB_CONTROL */
5984
5985 if (pid < 0)
5986 {
5987 sys_error ("%s", _("cannot make child for process substitution"));
5988 free (pathname);
5989 #if defined (HAVE_DEV_FD)
5990 close (parent_pipe_fd);
5991 close (child_pipe_fd);
5992 #endif /* HAVE_DEV_FD */
5993 #if defined (JOB_CONTROL)
5994 restore_pipeline (1);
5995 #endif
5996 return ((char *)NULL);
5997 }
5998
5999 if (pid > 0)
6000 {
6001 #if defined (JOB_CONTROL)
6002 last_procsub_child = restore_pipeline (0);
6003 /* We assume that last_procsub_child->next == last_procsub_child because
6004 of how jobs.c:add_process() works. */
6005 last_procsub_child->next = 0;
6006 procsub_add (last_procsub_child);
6007 #endif
6008
6009 #if defined (HAVE_DEV_FD)
6010 dev_fd_list[parent_pipe_fd] = pid;
6011 #else
6012 fifo_list[nfifo-1].proc = pid;
6013 #endif
6014
6015 last_made_pid = old_pid;
6016
6017 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
6018 close_pgrp_pipe ();
6019 #endif /* JOB_CONTROL && PGRP_PIPE */
6020
6021 #if defined (HAVE_DEV_FD)
6022 close (child_pipe_fd);
6023 #endif /* HAVE_DEV_FD */
6024
6025 return (pathname);
6026 }
6027
6028 set_sigint_handler ();
6029
6030 #if defined (JOB_CONTROL)
6031 /* make sure we don't have any job control */
6032 set_job_control (0);
6033
6034 /* Clear out any existing list of process substitutions */
6035 procsub_clear ();
6036
6037 /* The idea is that we want all the jobs we start from an async process
6038 substitution to be in the same process group, but not the same pgrp
6039 as our parent shell, since we don't want to affect our parent shell's
6040 jobs if we get a SIGHUP and end up calling hangup_all_jobs, for example.
6041 If pipeline_pgrp != shell_pgrp, we assume that there is a job control
6042 shell somewhere in our parent process chain (since make_child initializes
6043 pipeline_pgrp to shell_pgrp if job_control == 0). What we do in this
6044 case is to set pipeline_pgrp to our PID, so all jobs started by this
6045 process have that same pgrp and we are basically the process group leader.
6046 This should not have negative effects on child processes surviving
6047 after we exit, since we wait for the children we create, but that is
6048 something to watch for. */
6049
6050 if (pipeline_pgrp != shell_pgrp)
6051 pipeline_pgrp = getpid ();
6052 #endif /* JOB_CONTROL */
6053
6054 #if !defined (HAVE_DEV_FD)
6055 /* Open the named pipe in the child. */
6056 fd = open (pathname, open_for_read_in_child ? O_RDONLY : O_WRONLY);
6057 if (fd < 0)
6058 {
6059 /* Two separate strings for ease of translation. */
6060 if (open_for_read_in_child)
6061 sys_error (_("cannot open named pipe %s for reading"), pathname);
6062 else
6063 sys_error (_("cannot open named pipe %s for writing"), pathname);
6064
6065 exit (127);
6066 }
6067 if (open_for_read_in_child)
6068 {
6069 if (sh_unset_nodelay_mode (fd) < 0)
6070 {
6071 sys_error (_("cannot reset nodelay mode for fd %d"), fd);
6072 exit (127);
6073 }
6074 }
6075 #else /* HAVE_DEV_FD */
6076 fd = child_pipe_fd;
6077 #endif /* HAVE_DEV_FD */
6078
6079 /* Discard buffered stdio output before replacing the underlying file
6080 descriptor. */
6081 if (open_for_read_in_child == 0)
6082 fpurge (stdout);
6083
6084 if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
6085 {
6086 sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname,
6087 open_for_read_in_child ? 0 : 1);
6088 exit (127);
6089 }
6090
6091 if (fd != (open_for_read_in_child ? 0 : 1))
6092 close (fd);
6093
6094 /* Need to close any files that this process has open to pipes inherited
6095 from its parent. */
6096 if (current_fds_to_close)
6097 {
6098 close_fd_bitmap (current_fds_to_close);
6099 current_fds_to_close = (struct fd_bitmap *)NULL;
6100 }
6101
6102 #if defined (HAVE_DEV_FD)
6103 /* Make sure we close the parent's end of the pipe and clear the slot
6104 in the fd list so it is not closed later, if reallocated by, for
6105 instance, pipe(2). */
6106 close (parent_pipe_fd);
6107 dev_fd_list[parent_pipe_fd] = 0;
6108 #endif /* HAVE_DEV_FD */
6109
6110 /* subshells shouldn't have this flag, which controls using the temporary
6111 environment for variable lookups. We have already flushed the temporary
6112 environment above in the case we're expanding a redirection, so processes
6113 executed by this command need to be able to set it independently of their
6114 parent. */
6115 expanding_redir = 0;
6116
6117 remove_quoted_escapes (string);
6118
6119 #if 0 /* TAG: bash-5.2 */
6120 startup_state = 2; /* see if we can avoid a fork */
6121 parse_and_execute_level = 0;
6122 #endif
6123
6124 /* Give process substitution a place to jump back to on failure,
6125 so we don't go back up to main (). */
6126 result = setjmp_nosigs (top_level);
6127
6128 /* If we're running a process substitution inside a shell function,
6129 trap `return' so we don't return from the function in the subshell
6130 and go off to never-never land. */
6131 if (result == 0 && return_catch_flag)
6132 function_value = setjmp_nosigs (return_catch);
6133 else
6134 function_value = 0;
6135
6136 if (result == ERREXIT)
6137 rc = last_command_exit_value;
6138 else if (result == EXITPROG)
6139 rc = last_command_exit_value;
6140 else if (result)
6141 rc = EXECUTION_FAILURE;
6142 else if (function_value)
6143 rc = return_catch_value;
6144 else
6145 {
6146 subshell_level++;
6147 rc = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
6148 /* leave subshell level intact for any exit trap */
6149 }
6150
6151 #if !defined (HAVE_DEV_FD)
6152 /* Make sure we close the named pipe in the child before we exit. */
6153 close (open_for_read_in_child ? 0 : 1);
6154 #endif /* !HAVE_DEV_FD */
6155
6156 last_command_exit_value = rc;
6157 rc = run_exit_trap ();
6158 exit (rc);
6159 /*NOTREACHED*/
6160 }
6161 #endif /* PROCESS_SUBSTITUTION */
6162
6163 /***********************************/
6164 /* */
6165 /* Command Substitution */
6166 /* */
6167 /***********************************/
6168
6169 static char *
6170 read_comsub (fd, quoted, flags, rflag)
6171 int fd, quoted, flags;
6172 int *rflag;
6173 {
6174 char *istring, buf[512], *bufp;
6175 int istring_index, c, tflag, skip_ctlesc, skip_ctlnul;
6176 int mb_cur_max;
6177 size_t istring_size;
6178 ssize_t bufn;
6179 int nullbyte;
6180 #if defined (HANDLE_MULTIBYTE)
6181 mbstate_t ps;
6182 wchar_t wc;
6183 size_t mblen;
6184 int i;
6185 #endif
6186
6187 istring = (char *)NULL;
6188 istring_index = istring_size = bufn = tflag = 0;
6189
6190 skip_ctlesc = ifs_cmap[CTLESC];
6191 skip_ctlnul = ifs_cmap[CTLNUL];
6192
6193 mb_cur_max = MB_CUR_MAX;
6194 nullbyte = 0;
6195
6196 /* Read the output of the command through the pipe. */
6197 while (1)
6198 {
6199 if (fd < 0)
6200 break;
6201 if (--bufn <= 0)
6202 {
6203 bufn = zread (fd, buf, sizeof (buf));
6204 if (bufn <= 0)
6205 break;
6206 bufp = buf;
6207 }
6208 c = *bufp++;
6209
6210 if (c == 0)
6211 {
6212 #if 1
6213 if (nullbyte == 0)
6214 {
6215 internal_warning ("%s", _("command substitution: ignored null byte in input"));
6216 nullbyte = 1;
6217 }
6218 #endif
6219 continue;
6220 }
6221
6222 /* Add the character to ISTRING, possibly after resizing it. */
6223 RESIZE_MALLOCED_BUFFER (istring, istring_index, mb_cur_max+1, istring_size, 512);
6224
6225 /* This is essentially quote_string inline */
6226 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */)
6227 istring[istring_index++] = CTLESC;
6228 else if ((flags & PF_ASSIGNRHS) && skip_ctlesc && c == CTLESC)
6229 istring[istring_index++] = CTLESC;
6230 /* Escape CTLESC and CTLNUL in the output to protect those characters
6231 from the rest of the word expansions (word splitting and globbing.)
6232 This is essentially quote_escapes inline. */
6233 else if (skip_ctlesc == 0 && c == CTLESC)
6234 istring[istring_index++] = CTLESC;
6235 else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0)))
6236 istring[istring_index++] = CTLESC;
6237
6238 #if defined (HANDLE_MULTIBYTE)
6239 if ((locale_utf8locale && (c & 0x80)) ||
6240 (locale_utf8locale == 0 && mb_cur_max > 1 && (unsigned char)c > 127))
6241 {
6242 /* read a multibyte character from buf */
6243 /* punt on the hard case for now */
6244 memset (&ps, '\0', sizeof (mbstate_t));
6245 mblen = mbrtowc (&wc, bufp-1, bufn, &ps);
6246 if (MB_INVALIDCH (mblen) || mblen == 0 || mblen == 1)
6247 istring[istring_index++] = c;
6248 else
6249 {
6250 istring[istring_index++] = c;
6251 for (i = 0; i < mblen-1; i++)
6252 istring[istring_index++] = *bufp++;
6253 bufn -= mblen - 1;
6254 }
6255 continue;
6256 }
6257 #endif
6258
6259 istring[istring_index++] = c;
6260 }
6261
6262 if (istring)
6263 istring[istring_index] = '\0';
6264
6265 /* If we read no output, just return now and save ourselves some
6266 trouble. */
6267 if (istring_index == 0)
6268 {
6269 FREE (istring);
6270 if (rflag)
6271 *rflag = tflag;
6272 return (char *)NULL;
6273 }
6274
6275 /* Strip trailing newlines from the output of the command. */
6276 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6277 {
6278 while (istring_index > 0)
6279 {
6280 if (istring[istring_index - 1] == '\n')
6281 {
6282 --istring_index;
6283
6284 /* If the newline was quoted, remove the quoting char. */
6285 if (istring[istring_index - 1] == CTLESC)
6286 --istring_index;
6287 }
6288 else
6289 break;
6290 }
6291 istring[istring_index] = '\0';
6292 }
6293 else
6294 strip_trailing (istring, istring_index - 1, 1);
6295
6296 if (rflag)
6297 *rflag = tflag;
6298 return istring;
6299 }
6300
6301 /* Perform command substitution on STRING. This returns a WORD_DESC * with the
6302 contained string possibly quoted. */
6303 WORD_DESC *
6304 command_substitute (string, quoted, flags)
6305 char *string;
6306 int quoted;
6307 int flags;
6308 {
6309 pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
6310 char *istring, *s;
6311 int result, fildes[2], function_value, pflags, rc, tflag, fork_flags;
6312 WORD_DESC *ret;
6313 sigset_t set, oset;
6314
6315 istring = (char *)NULL;
6316
6317 /* Don't fork () if there is no need to. In the case of no command to
6318 run, just return NULL. */
6319 #if 1
6320 for (s = string; s && *s && (shellblank (*s) || *s == '\n'); s++)
6321 ;
6322 if (s == 0 || *s == 0)
6323 return ((WORD_DESC *)NULL);
6324 #else
6325 if (!string || !*string || (string[0] == '\n' && !string[1]))
6326 return ((WORD_DESC *)NULL);
6327 #endif
6328
6329 if (wordexp_only && read_but_dont_execute)
6330 {
6331 last_command_exit_value = EX_WEXPCOMSUB;
6332 jump_to_top_level (EXITPROG);
6333 }
6334
6335 /* We're making the assumption here that the command substitution will
6336 eventually run a command from the file system. Since we'll run
6337 maybe_make_export_env in this subshell before executing that command,
6338 the parent shell and any other shells it starts will have to remake
6339 the environment. If we make it before we fork, other shells won't
6340 have to. Don't bother if we have any temporary variable assignments,
6341 though, because the export environment will be remade after this
6342 command completes anyway, but do it if all the words to be expanded
6343 are variable assignments. */
6344 if (subst_assign_varlist == 0 || garglist == 0)
6345 maybe_make_export_env (); /* XXX */
6346
6347 /* Flags to pass to parse_and_execute() */
6348 pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0;
6349
6350 old_pid = last_made_pid;
6351
6352 /* Pipe the output of executing STRING into the current shell. */
6353 if (pipe (fildes) < 0)
6354 {
6355 sys_error ("%s", _("cannot make pipe for command substitution"));
6356 goto error_exit;
6357 }
6358
6359 #if defined (JOB_CONTROL)
6360 old_pipeline_pgrp = pipeline_pgrp;
6361 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline or
6362 we've already forked to run a disk command (and are expanding redirections,
6363 for example). */
6364 if ((subshell_environment & (SUBSHELL_FORK|SUBSHELL_PIPE)) == 0)
6365 pipeline_pgrp = shell_pgrp;
6366 cleanup_the_pipeline ();
6367 #endif /* JOB_CONTROL */
6368
6369 old_async_pid = last_asynchronous_pid;
6370 fork_flags = (subshell_environment&SUBSHELL_ASYNC) ? FORK_ASYNC : 0;
6371 pid = make_child ((char *)NULL, fork_flags|FORK_NOTERM);
6372 last_asynchronous_pid = old_async_pid;
6373
6374 if (pid == 0)
6375 {
6376 /* Reset the signal handlers in the child, but don't free the
6377 trap strings. Set a flag noting that we have to free the
6378 trap strings if we run trap to change a signal disposition. */
6379 reset_signal_handlers ();
6380 if (ISINTERRUPT)
6381 {
6382 kill (getpid (), SIGINT);
6383 CLRINTERRUPT; /* if we're ignoring SIGINT somehow */
6384 }
6385 QUIT; /* catch any interrupts we got post-fork */
6386 subshell_environment |= SUBSHELL_RESETTRAP;
6387 subshell_environment &= ~SUBSHELL_IGNTRAP;
6388 }
6389
6390 #if defined (JOB_CONTROL)
6391 /* XXX DO THIS ONLY IN PARENT ? XXX */
6392 set_sigchld_handler ();
6393 stop_making_children ();
6394 if (pid != 0)
6395 pipeline_pgrp = old_pipeline_pgrp;
6396 #else
6397 stop_making_children ();
6398 #endif /* JOB_CONTROL */
6399
6400 if (pid < 0)
6401 {
6402 sys_error (_("cannot make child for command substitution"));
6403 error_exit:
6404
6405 last_made_pid = old_pid;
6406
6407 FREE (istring);
6408 close (fildes[0]);
6409 close (fildes[1]);
6410 return ((WORD_DESC *)NULL);
6411 }
6412
6413 if (pid == 0)
6414 {
6415 /* The currently executing shell is not interactive. */
6416 interactive = 0;
6417
6418 #if defined (JOB_CONTROL)
6419 /* Invariant: in child processes started to run command substitutions,
6420 pipeline_pgrp == shell_pgrp. Other parts of the shell assume this. */
6421 if (pipeline_pgrp > 0 && pipeline_pgrp != shell_pgrp)
6422 shell_pgrp = pipeline_pgrp;
6423 #endif
6424
6425 set_sigint_handler (); /* XXX */
6426
6427 free_pushed_string_input ();
6428
6429 /* Discard buffered stdio output before replacing the underlying file
6430 descriptor. */
6431 fpurge (stdout);
6432
6433 if (dup2 (fildes[1], 1) < 0)
6434 {
6435 sys_error ("%s", _("command_substitute: cannot duplicate pipe as fd 1"));
6436 exit (EXECUTION_FAILURE);
6437 }
6438
6439 /* If standard output is closed in the parent shell
6440 (such as after `exec >&-'), file descriptor 1 will be
6441 the lowest available file descriptor, and end up in
6442 fildes[0]. This can happen for stdin and stderr as well,
6443 but stdout is more important -- it will cause no output
6444 to be generated from this command. */
6445 if ((fildes[1] != fileno (stdin)) &&
6446 (fildes[1] != fileno (stdout)) &&
6447 (fildes[1] != fileno (stderr)))
6448 close (fildes[1]);
6449
6450 if ((fildes[0] != fileno (stdin)) &&
6451 (fildes[0] != fileno (stdout)) &&
6452 (fildes[0] != fileno (stderr)))
6453 close (fildes[0]);
6454
6455 #ifdef __CYGWIN__
6456 /* Let stdio know the fd may have changed from text to binary mode, and
6457 make sure to preserve stdout line buffering. */
6458 freopen (NULL, "w", stdout);
6459 sh_setlinebuf (stdout);
6460 #endif /* __CYGWIN__ */
6461
6462 /* This is a subshell environment. */
6463 subshell_environment |= SUBSHELL_COMSUB;
6464
6465 /* Many shells do not appear to inherit the -v option for command
6466 substitutions. */
6467 change_flag ('v', FLAG_OFF);
6468
6469 /* When inherit_errexit option is not enabled, command substitution does
6470 not inherit the -e flag. It is enabled when Posix mode is enabled */
6471 if (inherit_errexit == 0)
6472 {
6473 builtin_ignoring_errexit = 0;
6474 change_flag ('e', FLAG_OFF);
6475 }
6476 set_shellopts ();
6477
6478 /* If we are expanding a redirection, we can dispose of any temporary
6479 environment we received, since redirections are not supposed to have
6480 access to the temporary environment. We will have to see whether this
6481 affects temporary environments supplied to `eval', but the temporary
6482 environment gets copied to builtin_env at some point. */
6483 if (expanding_redir)
6484 {
6485 flush_temporary_env ();
6486 expanding_redir = 0;
6487 }
6488
6489 remove_quoted_escapes (string);
6490
6491 startup_state = 2; /* see if we can avoid a fork */
6492 parse_and_execute_level = 0;
6493
6494 /* Give command substitution a place to jump back to on failure,
6495 so we don't go back up to main (). */
6496 result = setjmp_nosigs (top_level);
6497
6498 /* If we're running a command substitution inside a shell function,
6499 trap `return' so we don't return from the function in the subshell
6500 and go off to never-never land. */
6501 if (result == 0 && return_catch_flag)
6502 function_value = setjmp_nosigs (return_catch);
6503 else
6504 function_value = 0;
6505
6506 if (result == ERREXIT)
6507 rc = last_command_exit_value;
6508 else if (result == EXITPROG)
6509 rc = last_command_exit_value;
6510 else if (result)
6511 rc = EXECUTION_FAILURE;
6512 else if (function_value)
6513 rc = return_catch_value;
6514 else
6515 {
6516 subshell_level++;
6517 rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST);
6518 /* leave subshell level intact for any exit trap */
6519 }
6520
6521 last_command_exit_value = rc;
6522 rc = run_exit_trap ();
6523 #if defined (PROCESS_SUBSTITUTION)
6524 unlink_fifo_list ();
6525 #endif
6526 exit (rc);
6527 }
6528 else
6529 {
6530 int dummyfd;
6531
6532 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
6533 close_pgrp_pipe ();
6534 #endif /* JOB_CONTROL && PGRP_PIPE */
6535
6536 close (fildes[1]);
6537
6538 begin_unwind_frame ("read-comsub");
6539 dummyfd = fildes[0];
6540 add_unwind_protect (close, dummyfd);
6541
6542 /* Block SIGINT while we're reading from the pipe. If the child
6543 process gets a SIGINT, it will either handle it or die, and the
6544 read will return. */
6545 BLOCK_SIGNAL (SIGINT, set, oset);
6546 tflag = 0;
6547 istring = read_comsub (fildes[0], quoted, flags, &tflag);
6548
6549 close (fildes[0]);
6550 discard_unwind_frame ("read-comsub");
6551 UNBLOCK_SIGNAL (oset);
6552
6553 current_command_subst_pid = pid;
6554 last_command_exit_value = wait_for (pid, JWAIT_NOTERM);
6555 last_command_subst_pid = pid;
6556 last_made_pid = old_pid;
6557
6558 #if defined (JOB_CONTROL)
6559 /* If last_command_exit_value > 128, then the substituted command
6560 was terminated by a signal. If that signal was SIGINT, then send
6561 SIGINT to ourselves. This will break out of loops, for instance. */
6562 if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT)
6563 kill (getpid (), SIGINT);
6564 #endif /* JOB_CONTROL */
6565
6566 ret = alloc_word_desc ();
6567 ret->word = istring;
6568 ret->flags = tflag;
6569
6570 return ret;
6571 }
6572 }
6573
6574 /********************************************************
6575 * *
6576 * Utility functions for parameter expansion *
6577 * *
6578 ********************************************************/
6579
6580 #if defined (ARRAY_VARS)
6581
6582 static arrayind_t
6583 array_length_reference (s)
6584 char *s;
6585 {
6586 int len;
6587 arrayind_t ind;
6588 char *akey;
6589 char *t, c;
6590 ARRAY *array;
6591 HASH_TABLE *h;
6592 SHELL_VAR *var;
6593
6594 var = array_variable_part (s, 0, &t, &len);
6595
6596 /* If unbound variables should generate an error, report one and return
6597 failure. */
6598 if ((var == 0 || invisible_p (var) || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error)
6599 {
6600 c = *--t;
6601 *t = '\0';
6602 set_exit_status (EXECUTION_FAILURE);
6603 err_unboundvar (s);
6604 *t = c;
6605 return (-1);
6606 }
6607 else if (var == 0 || invisible_p (var))
6608 return 0;
6609
6610 /* We support a couple of expansions for variables that are not arrays.
6611 We'll return the length of the value for v[0], and 1 for v[@] or
6612 v[*]. Return 0 for everything else. */
6613
6614 array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
6615 h = assoc_p (var) ? assoc_cell (var) : (HASH_TABLE *)NULL;
6616
6617 if (ALL_ELEMENT_SUB (t[0]) && t[1] == RBRACK)
6618 {
6619 if (assoc_p (var))
6620 return (h ? assoc_num_elements (h) : 0);
6621 else if (array_p (var))
6622 return (array ? array_num_elements (array) : 0);
6623 else
6624 return (var_isset (var) ? 1 : 0);
6625 }
6626
6627 if (assoc_p (var))
6628 {
6629 t[len - 1] = '\0';
6630 akey = expand_assignment_string_to_string (t, 0); /* [ */
6631 t[len - 1] = RBRACK;
6632 if (akey == 0 || *akey == 0)
6633 {
6634 err_badarraysub (t);
6635 FREE (akey);
6636 return (-1);
6637 }
6638 t = assoc_reference (assoc_cell (var), akey);
6639 free (akey);
6640 }
6641 else
6642 {
6643 ind = array_expand_index (var, t, len, 0);
6644 /* negative subscripts to indexed arrays count back from end */
6645 if (var && array_p (var) && ind < 0)
6646 ind = array_max_index (array_cell (var)) + 1 + ind;
6647 if (ind < 0)
6648 {
6649 err_badarraysub (t);
6650 return (-1);
6651 }
6652 if (array_p (var))
6653 t = array_reference (array, ind);
6654 else
6655 t = (ind == 0) ? value_cell (var) : (char *)NULL;
6656 }
6657
6658 len = MB_STRLEN (t);
6659 return (len);
6660 }
6661 #endif /* ARRAY_VARS */
6662
6663 static int
6664 valid_brace_expansion_word (name, var_is_special)
6665 char *name;
6666 int var_is_special;
6667 {
6668 if (DIGIT (*name) && all_digits (name))
6669 return 1;
6670 else if (var_is_special)
6671 return 1;
6672 #if defined (ARRAY_VARS)
6673 else if (valid_array_reference (name, 0))
6674 return 1;
6675 #endif /* ARRAY_VARS */
6676 else if (legal_identifier (name))
6677 return 1;
6678 else
6679 return 0;
6680 }
6681
6682 static int
6683 chk_atstar (name, quoted, pflags, quoted_dollar_atp, contains_dollar_at)
6684 char *name;
6685 int quoted, pflags;
6686 int *quoted_dollar_atp, *contains_dollar_at;
6687 {
6688 char *temp1;
6689
6690 if (name == 0)
6691 {
6692 if (quoted_dollar_atp)
6693 *quoted_dollar_atp = 0;
6694 if (contains_dollar_at)
6695 *contains_dollar_at = 0;
6696 return 0;
6697 }
6698
6699 /* check for $@ and $* */
6700 if (name[0] == '@' && name[1] == 0)
6701 {
6702 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
6703 *quoted_dollar_atp = 1;
6704 if (contains_dollar_at)
6705 *contains_dollar_at = 1;
6706 return 1;
6707 }
6708 else if (name[0] == '*' && name[1] == '\0' && quoted == 0)
6709 {
6710 /* Need more checks here that parallel what string_list_pos_params and
6711 param_expand do. Check expand_no_split_dollar_star and ??? */
6712 if (contains_dollar_at && expand_no_split_dollar_star == 0)
6713 *contains_dollar_at = 1;
6714 return 1;
6715 }
6716
6717 /* Now check for ${array[@]} and ${array[*]} */
6718 #if defined (ARRAY_VARS)
6719 else if (valid_array_reference (name, 0))
6720 {
6721 temp1 = mbschr (name, LBRACK);
6722 if (temp1 && temp1[1] == '@' && temp1[2] == RBRACK)
6723 {
6724 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
6725 *quoted_dollar_atp = 1;
6726 if (contains_dollar_at)
6727 *contains_dollar_at = 1;
6728 return 1;
6729 }
6730 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
6731 which should result in separate words even when IFS is unset. */
6732 if (temp1 && temp1[1] == '*' && temp1[2] == RBRACK && quoted == 0)
6733 {
6734 if (contains_dollar_at)
6735 *contains_dollar_at = 1;
6736 return 1;
6737 }
6738 }
6739 #endif
6740 return 0;
6741 }
6742
6743 /* Parameter expand NAME, and return a new string which is the expansion,
6744 or NULL if there was no expansion. NAME is as given in ${NAMEcWORD}.
6745 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
6746 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
6747 NAME was found inside of a double-quoted expression. */
6748 static WORD_DESC *
6749 parameter_brace_expand_word (name, var_is_special, quoted, pflags, indp)
6750 char *name;
6751 int var_is_special, quoted, pflags;
6752 arrayind_t *indp;
6753 {
6754 WORD_DESC *ret;
6755 char *temp, *tt;
6756 intmax_t arg_index;
6757 SHELL_VAR *var;
6758 int atype, rflags;
6759 arrayind_t ind;
6760
6761 ret = 0;
6762 temp = 0;
6763 rflags = 0;
6764
6765 if (indp)
6766 *indp = INTMAX_MIN;
6767
6768 /* Handle multiple digit arguments, as in ${11}. */
6769 if (legal_number (name, &arg_index))
6770 {
6771 tt = get_dollar_var_value (arg_index);
6772 if (tt)
6773 temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6774 ? quote_string (tt)
6775 : quote_escapes (tt);
6776 else
6777 temp = (char *)NULL;
6778 FREE (tt);
6779 }
6780 else if (var_is_special) /* ${@} */
6781 {
6782 int sindex;
6783 tt = (char *)xmalloc (2 + strlen (name));
6784 tt[sindex = 0] = '$';
6785 strcpy (tt + 1, name);
6786
6787 ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
6788 (int *)NULL, (int *)NULL, pflags);
6789 free (tt);
6790 }
6791 #if defined (ARRAY_VARS)
6792 else if (valid_array_reference (name, 0))
6793 {
6794 expand_arrayref:
6795 var = array_variable_part (name, 0, &tt, (int *)0);
6796 /* These are the cases where word splitting will not be performed */
6797 if (pflags & PF_ASSIGNRHS)
6798 {
6799 if (ALL_ELEMENT_SUB (tt[0]) && tt[1] == RBRACK)
6800 {
6801 /* Only treat as double quoted if array variable */
6802 if (var && (array_p (var) || assoc_p (var)))
6803 temp = array_value (name, quoted|Q_DOUBLE_QUOTES, AV_ASSIGNRHS, &atype, &ind);
6804 else
6805 temp = array_value (name, quoted, 0, &atype, &ind);
6806 }
6807 else
6808 temp = array_value (name, quoted, 0, &atype, &ind);
6809 }
6810 /* Posix interp 888 */
6811 else if (pflags & PF_NOSPLIT2)
6812 {
6813 /* Special cases, then general case, for each of A[@], A[*], A[n] */
6814 #if defined (HANDLE_MULTIBYTE)
6815 if (tt[0] == '@' && tt[1] == RBRACK && var && quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc[0] != ' ')
6816 #else
6817 if (tt[0] == '@' && tt[1] == RBRACK && var && quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc != ' ')
6818 #endif
6819 temp = array_value (name, Q_DOUBLE_QUOTES, AV_ASSIGNRHS, &atype, &ind);
6820 else if (tt[0] == '@' && tt[1] == RBRACK)
6821 temp = array_value (name, quoted, 0, &atype, &ind);
6822 else if (tt[0] == '*' && tt[1] == RBRACK && expand_no_split_dollar_star && ifs_is_null)
6823 temp = array_value (name, Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT, 0, &atype, &ind);
6824 else if (tt[0] == '*' && tt[1] == RBRACK)
6825 temp = array_value (name, quoted, 0, &atype, &ind);
6826 else
6827 temp = array_value (name, quoted, 0, &atype, &ind);
6828 }
6829 else if (tt[0] == '*' && tt[1] == RBRACK && expand_no_split_dollar_star && ifs_is_null)
6830 temp = array_value (name, Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT, 0, &atype, &ind);
6831 else
6832 temp = array_value (name, quoted, 0, &atype, &ind);
6833 if (atype == 0 && temp)
6834 {
6835 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6836 ? quote_string (temp)
6837 : quote_escapes (temp);
6838 rflags |= W_ARRAYIND;
6839 if (indp)
6840 *indp = ind;
6841 }
6842 else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6843 rflags |= W_HASQUOTEDNULL;
6844 }
6845 #endif
6846 else if (var = find_variable (name))
6847 {
6848 if (var_isset (var) && invisible_p (var) == 0)
6849 {
6850 #if defined (ARRAY_VARS)
6851 /* We avoid a memory leak by saving TT as the memory allocated by
6852 assoc_to_string or array_to_string and leaving it 0 otherwise,
6853 then freeing TT after quoting temp. */
6854 tt = (char *)NULL;
6855 if ((pflags & PF_ALLINDS) && assoc_p (var))
6856 tt = temp = assoc_empty (assoc_cell (var)) ? (char *)NULL : assoc_to_string (assoc_cell (var), " ", quoted);
6857 else if ((pflags & PF_ALLINDS) && array_p (var))
6858 tt = temp = array_empty (array_cell (var)) ? (char *)NULL : array_to_string (array_cell (var), " ", quoted);
6859 else if (assoc_p (var))
6860 temp = assoc_reference (assoc_cell (var), "0");
6861 else if (array_p (var))
6862 temp = array_reference (array_cell (var), 0);
6863 else
6864 temp = value_cell (var);
6865 #else
6866 temp = value_cell (var);
6867 #endif
6868
6869 if (temp)
6870 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6871 ? quote_string (temp)
6872 : ((pflags & PF_ASSIGNRHS) ? quote_rhs (temp)
6873 : quote_escapes (temp));
6874 FREE (tt);
6875 }
6876 else
6877 temp = (char *)NULL;
6878 }
6879 else if (var = find_variable_last_nameref (name, 0))
6880 {
6881 temp = nameref_cell (var);
6882 #if defined (ARRAY_VARS)
6883 /* Handle expanding nameref whose value is x[n] */
6884 if (temp && *temp && valid_array_reference (temp, 0))
6885 {
6886 name = temp;
6887 goto expand_arrayref;
6888 }
6889 else
6890 #endif
6891 /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */
6892 if (temp && *temp && legal_identifier (temp) == 0)
6893 {
6894 set_exit_status (EXECUTION_FAILURE);
6895 report_error (_("%s: invalid variable name for name reference"), temp);
6896 temp = &expand_param_error;
6897 }
6898 else
6899 temp = (char *)NULL;
6900 }
6901 else
6902 temp = (char *)NULL;
6903
6904 if (ret == 0)
6905 {
6906 ret = alloc_word_desc ();
6907 ret->word = temp;
6908 ret->flags |= rflags;
6909 }
6910 return ret;
6911 }
6912
6913 static char *
6914 parameter_brace_find_indir (name, var_is_special, quoted, find_nameref)
6915 char *name;
6916 int var_is_special, quoted, find_nameref;
6917 {
6918 char *temp, *t;
6919 WORD_DESC *w;
6920 SHELL_VAR *v;
6921 int pflags, oldex;
6922
6923 if (find_nameref && var_is_special == 0 && (v = find_variable_last_nameref (name, 0)) &&
6924 nameref_p (v) && (t = nameref_cell (v)) && *t)
6925 return (savestring (t));
6926
6927 /* If var_is_special == 0, and name is not an array reference, this does
6928 more expansion than necessary. It should really look up the variable's
6929 value and not try to expand it. */
6930 pflags = PF_IGNUNBOUND;
6931 /* Note that we're not going to be doing word splitting here */
6932 if (var_is_special)
6933 {
6934 pflags |= PF_ASSIGNRHS; /* suppresses word splitting */
6935 oldex = expand_no_split_dollar_star;
6936 expand_no_split_dollar_star = 1;
6937 }
6938 w = parameter_brace_expand_word (name, var_is_special, quoted, pflags, 0);
6939 if (var_is_special)
6940 expand_no_split_dollar_star = oldex;
6941
6942 t = w->word;
6943 /* Have to dequote here if necessary */
6944 if (t)
6945 {
6946 temp = ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || var_is_special)
6947 ? dequote_string (t)
6948 : dequote_escapes (t);
6949 free (t);
6950 t = temp;
6951 }
6952 dispose_word_desc (w);
6953
6954 return t;
6955 }
6956
6957 /* Expand an indirect reference to a variable: ${!NAME} expands to the
6958 value of the variable whose name is the value of NAME. */
6959 static WORD_DESC *
6960 parameter_brace_expand_indir (name, var_is_special, quoted, pflags, quoted_dollar_atp, contains_dollar_at)
6961 char *name;
6962 int var_is_special, quoted, pflags;
6963 int *quoted_dollar_atp, *contains_dollar_at;
6964 {
6965 char *t;
6966 WORD_DESC *w;
6967 SHELL_VAR *v;
6968
6969 /* See if it's a nameref first, behave in ksh93-compatible fashion.
6970 There is at least one incompatibility: given ${!foo[0]} where foo=bar,
6971 bash performs an indirect lookup on foo[0] and expands the result;
6972 ksh93 expands bar[0]. We could do that here -- there are enough usable
6973 primitives to do that -- but do not at this point. */
6974 if (var_is_special == 0 && (v = find_variable_last_nameref (name, 0)))
6975 {
6976 if (nameref_p (v) && (t = nameref_cell (v)) && *t)
6977 {
6978 w = alloc_word_desc ();
6979 w->word = savestring (t);
6980 w->flags = 0;
6981 return w;
6982 }
6983 }
6984
6985 /* An indirect reference to a positional parameter or a special parameter
6986 is ok. Indirect references to array references, as explained above, are
6987 ok (currently). Only references to unset variables are errors at this
6988 point. */
6989 if (legal_identifier (name) && v == 0)
6990 {
6991 report_error (_("%s: invalid indirect expansion"), name);
6992 w = alloc_word_desc ();
6993 w->word = &expand_param_error;
6994 w->flags = 0;
6995 return (w);
6996 }
6997
6998 t = parameter_brace_find_indir (name, var_is_special, quoted, 0);
6999
7000 chk_atstar (t, quoted, pflags, quoted_dollar_atp, contains_dollar_at);
7001
7002 #if defined (ARRAY_VARS)
7003 /* Array references to unset variables are also an error */
7004 if (t == 0 && valid_array_reference (name, 0))
7005 {
7006 v = array_variable_part (name, 0, (char **)0, (int *)0);
7007 if (v == 0)
7008 {
7009 report_error (_("%s: invalid indirect expansion"), name);
7010 w = alloc_word_desc ();
7011 w->word = &expand_param_error;
7012 w->flags = 0;
7013 return (w);
7014 }
7015 else
7016 return (WORD_DESC *)NULL;
7017 }
7018 #endif
7019
7020 if (t == 0)
7021 return (WORD_DESC *)NULL;
7022
7023 if (valid_brace_expansion_word (t, SPECIAL_VAR (t, 0)) == 0)
7024 {
7025 report_error (_("%s: invalid variable name"), t);
7026 free (t);
7027 w = alloc_word_desc ();
7028 w->word = &expand_param_error;
7029 w->flags = 0;
7030 return (w);
7031 }
7032
7033 w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, pflags, 0);
7034 free (t);
7035
7036 return w;
7037 }
7038
7039 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
7040 depending on the value of C, the separating character. C can be one of
7041 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
7042 between double quotes. */
7043 static WORD_DESC *
7044 parameter_brace_expand_rhs (name, value, op, quoted, pflags, qdollaratp, hasdollarat)
7045 char *name, *value;
7046 int op, quoted, pflags, *qdollaratp, *hasdollarat;
7047 {
7048 WORD_DESC *w;
7049 WORD_LIST *l, *tl;
7050 char *t, *t1, *temp, *vname;
7051 int l_hasdollat, sindex;
7052 SHELL_VAR *v;
7053
7054 /*itrace("parameter_brace_expand_rhs: %s:%s pflags = %d", name, value, pflags);*/
7055 /* If the entire expression is between double quotes, we want to treat
7056 the value as a double-quoted string, with the exception that we strip
7057 embedded unescaped double quotes (for sh backwards compatibility). */
7058 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
7059 {
7060 sindex = 0;
7061 temp = string_extract_double_quoted (value, &sindex, SX_STRIPDQ);
7062 }
7063 else
7064 temp = value;
7065
7066 w = alloc_word_desc ();
7067 l_hasdollat = 0;
7068 l = *temp ? expand_string_for_rhs (temp, quoted, op, pflags, &l_hasdollat, (int *)NULL)
7069 : (WORD_LIST *)0;
7070 if (hasdollarat)
7071 *hasdollarat = l_hasdollat || (l && l->next);
7072 if (temp != value)
7073 free (temp);
7074
7075 /* list_string takes multiple CTLNULs and turns them into an empty word
7076 with W_SAWQUOTEDNULL set. Turn it back into a single CTLNUL for the
7077 rest of this function and the caller. */
7078 for (tl = l; tl; tl = tl->next)
7079 {
7080 if (tl->word && (tl->word->word == 0 || tl->word->word[0] == 0) &&
7081 (tl->word->flags | W_SAWQUOTEDNULL))
7082 {
7083 t = make_quoted_char ('\0');
7084 FREE (tl->word->word);
7085 tl->word->word = t;
7086 tl->word->flags |= W_QUOTED|W_HASQUOTEDNULL;
7087 tl->word->flags &= ~W_SAWQUOTEDNULL;
7088 }
7089 }
7090
7091 if (l)
7092 {
7093 /* If l->next is not null, we know that TEMP contained "$@", since that
7094 is the only expansion that creates more than one word. */
7095 if (qdollaratp && ((l_hasdollat && quoted) || l->next))
7096 {
7097 /*itrace("parameter_brace_expand_rhs: %s:%s: l != NULL, set *qdollaratp", name, value);*/
7098 *qdollaratp = 1;
7099 }
7100
7101 /* The expansion of TEMP returned something. We need to treat things
7102 slightly differently if L_HASDOLLAT is non-zero. If we have "$@",
7103 the individual words have already been quoted. We need to turn them
7104 into a string with the words separated by the first character of
7105 $IFS without any additional quoting, so string_list_dollar_at won't
7106 do the right thing. If IFS is null, we want "$@" to split into
7107 separate arguments, not be concatenated, so we use string_list_internal
7108 and mark the word to be split on spaces later. We use
7109 string_list_dollar_star for "$@" otherwise. */
7110 if (l->next && ifs_is_null)
7111 {
7112 temp = string_list_internal (l, " ");
7113 w->flags |= W_SPLITSPACE;
7114 }
7115 else if (l_hasdollat || l->next)
7116 temp = string_list_dollar_star (l, quoted, 0);
7117 else
7118 {
7119 temp = string_list (l);
7120 if (temp && (QUOTED_NULL (temp) == 0) && (l->word->flags & W_SAWQUOTEDNULL))
7121 w->flags |= W_SAWQUOTEDNULL; /* XXX */
7122 }
7123
7124 /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is
7125 a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the
7126 flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the
7127 expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7128 (which is more paranoia than anything else), we need to return the
7129 quoted null string and set the flags to indicate it. */
7130 if (l->next == 0 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp) && QUOTED_NULL (l->word->word) && (l->word->flags & W_HASQUOTEDNULL))
7131 {
7132 w->flags |= W_HASQUOTEDNULL;
7133 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null, turning off qdollaratp", name, value);*/
7134 /* If we return a quoted null with L_HASDOLLARAT, we either have a
7135 construct like "${@-$@}" or "${@-${@-$@}}" with no positional
7136 parameters or a quoted expansion of "$@" with $1 == ''. In either
7137 case, we don't want to enable special handling of $@. */
7138 if (qdollaratp && l_hasdollat)
7139 *qdollaratp = 0;
7140 }
7141 dispose_words (l);
7142 }
7143 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && l_hasdollat)
7144 {
7145 /* Posix interp 221 changed the rules on this. The idea is that
7146 something like "$xxx$@" should expand the same as "${foo-$xxx$@}"
7147 when foo and xxx are unset. The problem is that it's not in any
7148 way backwards compatible and few other shells do it. We're eventually
7149 going to try and split the difference (heh) a little bit here. */
7150 /* l_hasdollat == 1 means we saw a quoted dollar at. */
7151
7152 /* The brace expansion occurred between double quotes and there was
7153 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
7154 it does not expand to anything. In this case, we want to return
7155 a quoted empty string. Posix interp 888 */
7156 temp = make_quoted_char ('\0');
7157 w->flags |= W_HASQUOTEDNULL;
7158 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null", name, value);*/
7159 }
7160 else
7161 temp = (char *)NULL;
7162
7163 if (op == '-' || op == '+')
7164 {
7165 w->word = temp;
7166 return w;
7167 }
7168
7169 /* op == '=' */
7170 t1 = temp ? dequote_string (temp) : savestring ("");
7171 free (temp);
7172
7173 /* bash-4.4/5.0 */
7174 vname = name;
7175 if (*name == '!' &&
7176 (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) || VALID_INDIR_PARAM (name[1])))
7177 {
7178 vname = parameter_brace_find_indir (name + 1, SPECIAL_VAR (name, 1), quoted, 1);
7179 if (vname == 0 || *vname == 0)
7180 {
7181 report_error (_("%s: invalid indirect expansion"), name);
7182 free (vname);
7183 free (t1);
7184 dispose_word (w);
7185 return &expand_wdesc_error;
7186 }
7187 if (legal_identifier (vname) == 0)
7188 {
7189 report_error (_("%s: invalid variable name"), vname);
7190 free (vname);
7191 free (t1);
7192 dispose_word (w);
7193 return &expand_wdesc_error;
7194 }
7195 }
7196
7197 #if defined (ARRAY_VARS)
7198 if (valid_array_reference (vname, 0))
7199 v = assign_array_element (vname, t1, 0);
7200 else
7201 #endif /* ARRAY_VARS */
7202 v = bind_variable (vname, t1, 0);
7203
7204 if (v == 0 || readonly_p (v) || noassign_p (v)) /* expansion error */
7205 {
7206 if ((v == 0 || readonly_p (v)) && interactive_shell == 0 && posixly_correct)
7207 {
7208 last_command_exit_value = EXECUTION_FAILURE;
7209 exp_jump_to_top_level (FORCE_EOF);
7210 }
7211 else
7212 {
7213 if (vname != name)
7214 free (vname);
7215 last_command_exit_value = EX_BADUSAGE;
7216 exp_jump_to_top_level (DISCARD);
7217 }
7218 }
7219
7220 stupidly_hack_special_variables (vname);
7221
7222 if (vname != name)
7223 free (vname);
7224
7225 /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */
7226
7227 /* If we are double-quoted or if we are not going to be performing word
7228 splitting, we want to quote the value we return appropriately, like
7229 the other expansions this function handles. */
7230 w->word = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) ? quote_string (t1) : quote_escapes (t1);
7231 /* If we have something that's non-null, that's not a quoted null string,
7232 and we're not going to be performing word splitting (we know we're not
7233 because the operator is `='), we can forget we saw a quoted null. */
7234 if (w->word && w->word[0] && QUOTED_NULL (w->word) == 0)
7235 w->flags &= ~W_SAWQUOTEDNULL;
7236 free (t1);
7237
7238 /* If we convert a null string into a quoted null, make sure the caller
7239 knows it. */
7240 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && QUOTED_NULL (w->word))
7241 w->flags |= W_HASQUOTEDNULL;
7242
7243 return w;
7244 }
7245
7246 /* Deal with the right hand side of a ${name:?value} expansion in the case
7247 that NAME is null or not set. If VALUE is non-null it is expanded and
7248 used as the error message to print, otherwise a standard message is
7249 printed. */
7250 static void
7251 parameter_brace_expand_error (name, value, check_null)
7252 char *name, *value;
7253 int check_null;
7254 {
7255 WORD_LIST *l;
7256 char *temp;
7257
7258 set_exit_status (EXECUTION_FAILURE); /* ensure it's non-zero */
7259 if (value && *value)
7260 {
7261 l = expand_string (value, 0);
7262 temp = string_list (l);
7263 report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */
7264 FREE (temp);
7265 dispose_words (l);
7266 }
7267 else if (check_null == 0)
7268 report_error (_("%s: parameter not set"), name);
7269 else
7270 report_error (_("%s: parameter null or not set"), name);
7271
7272 /* Free the data we have allocated during this expansion, since we
7273 are about to longjmp out. */
7274 free (name);
7275 FREE (value);
7276 }
7277
7278 /* Return 1 if NAME is something for which parameter_brace_expand_length is
7279 OK to do. */
7280 static int
7281 valid_length_expression (name)
7282 char *name;
7283 {
7284 return (name[1] == '\0' || /* ${#} */
7285 ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */
7286 (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */
7287 #if defined (ARRAY_VARS)
7288 valid_array_reference (name + 1, 0) || /* ${#a[7]} */
7289 #endif
7290 legal_identifier (name + 1)); /* ${#PS1} */
7291 }
7292
7293 /* Handle the parameter brace expansion that requires us to return the
7294 length of a parameter. */
7295 static intmax_t
7296 parameter_brace_expand_length (name)
7297 char *name;
7298 {
7299 char *t, *newname;
7300 intmax_t number, arg_index;
7301 WORD_LIST *list;
7302 SHELL_VAR *var;
7303
7304 var = (SHELL_VAR *)NULL;
7305
7306 if (name[1] == '\0') /* ${#} */
7307 number = number_of_args ();
7308 else if (DOLLAR_AT_STAR (name[1]) && name[2] == '\0') /* ${#@}, ${#*} */
7309 number = number_of_args ();
7310 else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
7311 {
7312 /* Take the lengths of some of the shell's special parameters. */
7313 switch (name[1])
7314 {
7315 case '-':
7316 t = which_set_flags ();
7317 break;
7318 case '?':
7319 t = itos (last_command_exit_value);
7320 break;
7321 case '$':
7322 t = itos (dollar_dollar_pid);
7323 break;
7324 case '!':
7325 if (last_asynchronous_pid == NO_PID)
7326 t = (char *)NULL; /* XXX - error if set -u set? */
7327 else
7328 t = itos (last_asynchronous_pid);
7329 break;
7330 case '#':
7331 t = itos (number_of_args ());
7332 break;
7333 }
7334 number = STRLEN (t);
7335 FREE (t);
7336 }
7337 #if defined (ARRAY_VARS)
7338 else if (valid_array_reference (name + 1, 0))
7339 number = array_length_reference (name + 1);
7340 #endif /* ARRAY_VARS */
7341 else
7342 {
7343 number = 0;
7344
7345 if (legal_number (name + 1, &arg_index)) /* ${#1} */
7346 {
7347 t = get_dollar_var_value (arg_index);
7348 if (t == 0 && unbound_vars_is_error)
7349 return INTMAX_MIN;
7350 number = MB_STRLEN (t);
7351 FREE (t);
7352 }
7353 #if defined (ARRAY_VARS)
7354 else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var)))
7355 {
7356 if (assoc_p (var))
7357 t = assoc_reference (assoc_cell (var), "0");
7358 else
7359 t = array_reference (array_cell (var), 0);
7360 if (t == 0 && unbound_vars_is_error)
7361 return INTMAX_MIN;
7362 number = MB_STRLEN (t);
7363 }
7364 #endif
7365 /* Fast path for the common case of taking the length of a non-dynamic
7366 scalar variable value. */
7367 else if ((var || (var = find_variable (name + 1))) &&
7368 invisible_p (var) == 0 &&
7369 array_p (var) == 0 && assoc_p (var) == 0 &&
7370 var->dynamic_value == 0)
7371 number = value_cell (var) ? MB_STRLEN (value_cell (var)) : 0;
7372 else if (var == 0 && unbound_vars_is_error == 0)
7373 number = 0;
7374 else /* ${#PS1} */
7375 {
7376 newname = savestring (name);
7377 newname[0] = '$';
7378 list = expand_string (newname, Q_DOUBLE_QUOTES);
7379 t = list ? string_list (list) : (char *)NULL;
7380 free (newname);
7381 if (list)
7382 dispose_words (list);
7383
7384 number = t ? MB_STRLEN (t) : 0;
7385 FREE (t);
7386 }
7387 }
7388
7389 return (number);
7390 }
7391
7392 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
7393 so we do some ad-hoc parsing of an arithmetic expression to find
7394 the first DELIM, instead of using strchr(3). Two rules:
7395 1. If the substring contains a `(', read until closing `)'.
7396 2. If the substring contains a `?', read past one `:' for each `?'.
7397 The SD_ARITHEXP flag to skip_to_delim takes care of doing this.
7398 */
7399
7400 static char *
7401 skiparith (substr, delim)
7402 char *substr;
7403 int delim;
7404 {
7405 int i;
7406 char delims[2];
7407
7408 delims[0] = delim;
7409 delims[1] = '\0';
7410
7411 i = skip_to_delim (substr, 0, delims, SD_ARITHEXP);
7412 return (substr + i);
7413 }
7414
7415 /* Verify and limit the start and end of the desired substring. If
7416 VTYPE == 0, a regular shell variable is being used; if it is 1,
7417 then the positional parameters are being used; if it is 2, then
7418 VALUE is really a pointer to an array variable that should be used.
7419 Return value is 1 if both values were OK, 0 if there was a problem
7420 with an invalid expression, or -1 if the values were out of range. */
7421 static int
7422 verify_substring_values (v, value, substr, vtype, e1p, e2p)
7423 SHELL_VAR *v;
7424 char *value, *substr;
7425 int vtype;
7426 intmax_t *e1p, *e2p;
7427 {
7428 char *t, *temp1, *temp2;
7429 arrayind_t len;
7430 int expok;
7431 #if defined (ARRAY_VARS)
7432 ARRAY *a;
7433 HASH_TABLE *h;
7434 #endif
7435
7436 /* duplicate behavior of strchr(3) */
7437 t = skiparith (substr, ':');
7438 if (*t && *t == ':')
7439 *t = '\0';
7440 else
7441 t = (char *)0;
7442
7443 temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES);
7444 *e1p = evalexp (temp1, 0, &expok); /* XXX - EXP_EXPANDED? */
7445 free (temp1);
7446 if (expok == 0)
7447 return (0);
7448
7449 len = -1; /* paranoia */
7450 switch (vtype)
7451 {
7452 case VT_VARIABLE:
7453 case VT_ARRAYMEMBER:
7454 len = MB_STRLEN (value);
7455 break;
7456 case VT_POSPARMS:
7457 len = number_of_args () + 1;
7458 if (*e1p == 0)
7459 len++; /* add one arg if counting from $0 */
7460 break;
7461 #if defined (ARRAY_VARS)
7462 case VT_ARRAYVAR:
7463 /* For arrays, the first value deals with array indices. Negative
7464 offsets count from one past the array's maximum index. Associative
7465 arrays treat the number of elements as the maximum index. */
7466 if (assoc_p (v))
7467 {
7468 h = assoc_cell (v);
7469 len = assoc_num_elements (h) + (*e1p < 0);
7470 }
7471 else
7472 {
7473 a = (ARRAY *)value;
7474 len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */
7475 }
7476 break;
7477 #endif
7478 }
7479
7480 if (len == -1) /* paranoia */
7481 return -1;
7482
7483 if (*e1p < 0) /* negative offsets count from end */
7484 *e1p += len;
7485
7486 if (*e1p > len || *e1p < 0)
7487 return (-1);
7488
7489 #if defined (ARRAY_VARS)
7490 /* For arrays, the second offset deals with the number of elements. */
7491 if (vtype == VT_ARRAYVAR)
7492 len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a);
7493 #endif
7494
7495 if (t)
7496 {
7497 t++;
7498 temp2 = savestring (t);
7499 temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
7500 free (temp2);
7501 t[-1] = ':';
7502 *e2p = evalexp (temp1, 0, &expok); /* XXX - EXP_EXPANDED? */
7503 free (temp1);
7504 if (expok == 0)
7505 return (0);
7506
7507 /* Should we allow positional parameter length < 0 to count backwards
7508 from end of positional parameters? */
7509 #if 1
7510 if ((vtype == VT_ARRAYVAR || vtype == VT_POSPARMS) && *e2p < 0)
7511 #else /* TAG: bash-5.2 */
7512 if (vtype == VT_ARRAYVAR && *e2p < 0)
7513 #endif
7514 {
7515 internal_error (_("%s: substring expression < 0"), t);
7516 return (0);
7517 }
7518 #if defined (ARRAY_VARS)
7519 /* In order to deal with sparse arrays, push the intelligence about how
7520 to deal with the number of elements desired down to the array-
7521 specific functions. */
7522 if (vtype != VT_ARRAYVAR)
7523 #endif
7524 {
7525 if (*e2p < 0)
7526 {
7527 *e2p += len;
7528 if (*e2p < 0 || *e2p < *e1p)
7529 {
7530 internal_error (_("%s: substring expression < 0"), t);
7531 return (0);
7532 }
7533 }
7534 else
7535 *e2p += *e1p; /* want E2 chars starting at E1 */
7536 if (*e2p > len)
7537 *e2p = len;
7538 }
7539 }
7540 else
7541 *e2p = len;
7542
7543 return (1);
7544 }
7545
7546 /* Return the type of variable specified by VARNAME (simple variable,
7547 positional param, or array variable). Also return the value specified
7548 by VARNAME (value of a variable or a reference to an array element).
7549 QUOTED is the standard description of quoting state, using Q_* defines.
7550 FLAGS is currently a set of flags to pass to array_value. If IND is
7551 non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is
7552 passed to array_value so the array index is not computed again.
7553 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
7554 characters in the value are quoted with CTLESC and takes appropriate
7555 steps. For convenience, *VALP is set to the dequoted VALUE. */
7556 static int
7557 get_var_and_type (varname, value, ind, quoted, flags, varp, valp)
7558 char *varname, *value;
7559 arrayind_t ind;
7560 int quoted, flags;
7561 SHELL_VAR **varp;
7562 char **valp;
7563 {
7564 int vtype, want_indir;
7565 char *temp, *vname;
7566 SHELL_VAR *v;
7567 arrayind_t lind;
7568
7569 want_indir = *varname == '!' &&
7570 (legal_variable_starter ((unsigned char)varname[1]) || DIGIT (varname[1])
7571 || VALID_INDIR_PARAM (varname[1]));
7572 if (want_indir)
7573 vname = parameter_brace_find_indir (varname+1, SPECIAL_VAR (varname, 1), quoted, 1);
7574 /* XXX - what if vname == 0 || *vname == 0 ? */
7575 else
7576 vname = varname;
7577
7578 if (vname == 0)
7579 {
7580 vtype = VT_VARIABLE;
7581 *varp = (SHELL_VAR *)NULL;
7582 *valp = (char *)NULL;
7583 return (vtype);
7584 }
7585
7586 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
7587 vtype = STR_DOLLAR_AT_STAR (vname);
7588 if (vtype == VT_POSPARMS && vname[0] == '*')
7589 vtype |= VT_STARSUB;
7590 *varp = (SHELL_VAR *)NULL;
7591
7592 #if defined (ARRAY_VARS)
7593 if (valid_array_reference (vname, 0))
7594 {
7595 v = array_variable_part (vname, 0, &temp, (int *)0);
7596 /* If we want to signal array_value to use an already-computed index,
7597 set LIND to that index */
7598 lind = (ind != INTMAX_MIN && (flags & AV_USEIND)) ? ind : 0;
7599 if (v && invisible_p (v))
7600 {
7601 vtype = VT_ARRAYMEMBER;
7602 *varp = (SHELL_VAR *)NULL;
7603 *valp = (char *)NULL;
7604 }
7605 if (v && (array_p (v) || assoc_p (v)))
7606 {
7607 if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == RBRACK)
7608 {
7609 /* Callers have to differentiate between indexed and associative */
7610 vtype = VT_ARRAYVAR;
7611 if (temp[0] == '*')
7612 vtype |= VT_STARSUB;
7613 *valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v);
7614 }
7615 else
7616 {
7617 vtype = VT_ARRAYMEMBER;
7618 *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
7619 }
7620 *varp = v;
7621 }
7622 else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == RBRACK))
7623 {
7624 vtype = VT_VARIABLE;
7625 *varp = v;
7626 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
7627 *valp = value ? dequote_string (value) : (char *)NULL;
7628 else
7629 *valp = value ? dequote_escapes (value) : (char *)NULL;
7630 }
7631 else
7632 {
7633 vtype = VT_ARRAYMEMBER;
7634 *varp = v;
7635 *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
7636 }
7637 }
7638 else if ((v = find_variable (vname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v)))
7639 {
7640 vtype = VT_ARRAYMEMBER;
7641 *varp = v;
7642 *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0);
7643 }
7644 else
7645 #endif
7646 {
7647 if (value && vtype == VT_VARIABLE)
7648 {
7649 *varp = find_variable (vname);
7650 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
7651 *valp = dequote_string (value);
7652 else
7653 *valp = dequote_escapes (value);
7654 }
7655 else
7656 *valp = value;
7657 }
7658
7659 if (want_indir)
7660 free (vname);
7661
7662 return vtype;
7663 }
7664
7665 /***********************************************************/
7666 /* */
7667 /* Functions to perform transformations on variable values */
7668 /* */
7669 /***********************************************************/
7670
7671 static char *
7672 string_var_assignment (v, s)
7673 SHELL_VAR *v;
7674 char *s;
7675 {
7676 char flags[MAX_ATTRIBUTES], *ret, *val;
7677 int i;
7678
7679 val = (v && (invisible_p (v) || var_isset (v) == 0)) ? (char *)NULL : sh_quote_reusable (s, 0);
7680 i = var_attribute_string (v, 0, flags);
7681 if (i == 0 && val == 0)
7682 return (char *)NULL;
7683
7684 ret = (char *)xmalloc (i + STRLEN (val) + strlen (v->name) + 16 + MAX_ATTRIBUTES);
7685 if (i > 0 && val == 0)
7686 sprintf (ret, "declare -%s %s", flags, v->name);
7687 else if (i > 0)
7688 sprintf (ret, "declare -%s %s=%s", flags, v->name, val);
7689 else
7690 sprintf (ret, "%s=%s", v->name, val);
7691 free (val);
7692 return ret;
7693 }
7694
7695 #if defined (ARRAY_VARS)
7696 static char *
7697 array_var_assignment (v, itype, quoted, atype)
7698 SHELL_VAR *v;
7699 int itype, quoted, atype;
7700 {
7701 char *ret, *val, flags[MAX_ATTRIBUTES];
7702 int i;
7703
7704 if (v == 0)
7705 return (char *)NULL;
7706 if (atype == 2)
7707 val = array_p (v) ? array_to_kvpair (array_cell (v), 0)
7708 : assoc_to_kvpair (assoc_cell (v), 0);
7709 else
7710 val = array_p (v) ? array_to_assign (array_cell (v), 0)
7711 : assoc_to_assign (assoc_cell (v), 0);
7712
7713 if (val == 0 && (invisible_p (v) || var_isset (v) == 0))
7714 ; /* placeholder */
7715 else if (val == 0)
7716 {
7717 val = (char *)xmalloc (3);
7718 val[0] = LPAREN;
7719 val[1] = RPAREN;
7720 val[2] = 0;
7721 }
7722 else
7723 {
7724 ret = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) ? quote_string (val) : quote_escapes (val);
7725 free (val);
7726 val = ret;
7727 }
7728
7729 if (atype == 2)
7730 return val;
7731
7732 i = var_attribute_string (v, 0, flags);
7733 ret = (char *)xmalloc (i + STRLEN (val) + strlen (v->name) + 16);
7734 if (val)
7735 sprintf (ret, "declare -%s %s=%s", flags, v->name, val);
7736 else
7737 sprintf (ret, "declare -%s %s", flags, v->name);
7738 free (val);
7739 return ret;
7740 }
7741 #endif
7742
7743 static char *
7744 pos_params_assignment (list, itype, quoted)
7745 WORD_LIST *list;
7746 int itype;
7747 int quoted;
7748 {
7749 char *temp, *ret;
7750
7751 /* first, we transform the list to quote each word. */
7752 temp = list_transform ('Q', (SHELL_VAR *)0, list, itype, quoted);
7753 ret = (char *)xmalloc (strlen (temp) + 8);
7754 strcpy (ret, "set -- ");
7755 strcpy (ret + 7, temp);
7756 free (temp);
7757 return ret;
7758 }
7759
7760 static char *
7761 string_transform (xc, v, s)
7762 int xc;
7763 SHELL_VAR *v;
7764 char *s;
7765 {
7766 char *ret, flags[MAX_ATTRIBUTES], *t;
7767 int i;
7768
7769 if (((xc == 'A' || xc == 'a') && v == 0))
7770 return (char *)NULL;
7771 else if (xc != 'a' && xc != 'A' && s == 0)
7772 return (char *)NULL;
7773
7774 switch (xc)
7775 {
7776 /* Transformations that interrogate the variable */
7777 case 'a':
7778 i = var_attribute_string (v, 0, flags);
7779 ret = (i > 0) ? savestring (flags) : (char *)NULL;
7780 break;
7781 case 'A':
7782 ret = string_var_assignment (v, s);
7783 break;
7784 case 'K':
7785 ret = sh_quote_reusable (s, 0);
7786 break;
7787 /* Transformations that modify the variable's value */
7788 case 'E':
7789 t = ansiexpand (s, 0, strlen (s), (int *)0);
7790 ret = dequote_escapes (t);
7791 free (t);
7792 break;
7793 case 'P':
7794 ret = decode_prompt_string (s);
7795 break;
7796 case 'Q':
7797 ret = sh_quote_reusable (s, 0);
7798 break;
7799 case 'U':
7800 ret = sh_modcase (s, 0, CASE_UPPER);
7801 break;
7802 case 'u':
7803 ret = sh_modcase (s, 0, CASE_UPFIRST); /* capitalize */
7804 break;
7805 case 'L':
7806 ret = sh_modcase (s, 0, CASE_LOWER);
7807 break;
7808 default:
7809 ret = (char *)NULL;
7810 break;
7811 }
7812 return ret;
7813 }
7814
7815 static char *
7816 list_transform (xc, v, list, itype, quoted)
7817 int xc;
7818 SHELL_VAR *v;
7819 WORD_LIST *list;
7820 int itype, quoted;
7821 {
7822 WORD_LIST *new, *l;
7823 WORD_DESC *w;
7824 char *tword;
7825 int qflags;
7826
7827 for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
7828 {
7829 tword = string_transform (xc, v, l->word->word);
7830 w = alloc_word_desc ();
7831 w->word = tword ? tword : savestring (""); /* XXX */
7832 new = make_word_list (w, new);
7833 }
7834 l = REVERSE_LIST (new, WORD_LIST *);
7835
7836 qflags = quoted;
7837 /* If we are expanding in a context where word splitting will not be
7838 performed, treat as quoted. This changes how $* will be expanded. */
7839 if (itype == '*' && expand_no_split_dollar_star && ifs_is_null)
7840 qflags |= Q_DOUBLE_QUOTES; /* Posix interp 888 */
7841
7842 tword = string_list_pos_params (itype, l, qflags, 0);
7843 dispose_words (l);
7844
7845 return (tword);
7846 }
7847
7848 static char *
7849 parameter_list_transform (xc, itype, quoted)
7850 int xc;
7851 int itype;
7852 int quoted;
7853 {
7854 char *ret;
7855 WORD_LIST *list;
7856
7857 list = list_rest_of_args ();
7858 if (list == 0)
7859 return ((char *)NULL);
7860 if (xc == 'A')
7861 ret = pos_params_assignment (list, itype, quoted);
7862 else
7863 ret = list_transform (xc, (SHELL_VAR *)0, list, itype, quoted);
7864 dispose_words (list);
7865 return (ret);
7866 }
7867
7868 #if defined (ARRAY_VARS)
7869 static char *
7870 array_transform (xc, var, starsub, quoted)
7871 int xc;
7872 SHELL_VAR *var;
7873 int starsub; /* so we can figure out how it's indexed */
7874 int quoted;
7875 {
7876 ARRAY *a;
7877 HASH_TABLE *h;
7878 int itype;
7879 char *ret;
7880 WORD_LIST *list;
7881 SHELL_VAR *v;
7882
7883 v = var; /* XXX - for now */
7884
7885 itype = starsub ? '*' : '@';
7886
7887 if (xc == 'A')
7888 return (array_var_assignment (v, itype, quoted, 1));
7889 else if (xc == 'K')
7890 return (array_var_assignment (v, itype, quoted, 2));
7891
7892 /* special case for unset arrays and attributes */
7893 if (xc == 'a' && (invisible_p (v) || var_isset (v) == 0))
7894 {
7895 char flags[MAX_ATTRIBUTES];
7896 int i;
7897
7898 i = var_attribute_string (v, 0, flags);
7899 return ((i > 0) ? savestring (flags) : (char *)NULL);
7900 }
7901
7902 a = (v && array_p (v)) ? array_cell (v) : 0;
7903 h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
7904
7905 list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
7906 if (list == 0)
7907 return ((char *)NULL);
7908 ret = list_transform (xc, v, list, itype, quoted);
7909 dispose_words (list);
7910
7911 return ret;
7912 }
7913 #endif /* ARRAY_VARS */
7914
7915 static int
7916 valid_parameter_transform (xform)
7917 char *xform;
7918 {
7919 if (xform[1])
7920 return 0;
7921
7922 /* check for valid values of xform[0] */
7923 switch (xform[0])
7924 {
7925 case 'a': /* expand to a string with just attributes */
7926 case 'A': /* expand as an assignment statement with attributes */
7927 case 'K': /* expand assoc array to list of key/value pairs */
7928 case 'E': /* expand like $'...' */
7929 case 'P': /* expand like prompt string */
7930 case 'Q': /* quote reusably */
7931 case 'U': /* transform to uppercase */
7932 case 'u': /* tranform by capitalizing */
7933 case 'L': /* transform to lowercase */
7934 return 1;
7935 default:
7936 return 0;
7937 }
7938 }
7939
7940 static char *
7941 parameter_brace_transform (varname, value, ind, xform, rtype, quoted, pflags, flags)
7942 char *varname, *value;
7943 int ind;
7944 char *xform;
7945 int rtype, quoted, pflags, flags;
7946 {
7947 int vtype, xc, starsub;
7948 char *temp1, *val, *oname;
7949 SHELL_VAR *v;
7950
7951 xc = xform[0];
7952 if (value == 0 && xc != 'A' && xc != 'a')
7953 return ((char *)NULL);
7954
7955 oname = this_command_name;
7956 this_command_name = varname;
7957
7958 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
7959 if (vtype == -1)
7960 {
7961 this_command_name = oname;
7962 return ((char *)NULL);
7963 }
7964
7965 if (valid_parameter_transform (xform) == 0)
7966 {
7967 this_command_name = oname;
7968 #if 0 /* TAG: bash-5.2 Martin Schulte <gnu@schrader-schulte.de> 10/2020 */
7969 return (interactive_shell ? &expand_param_error : &expand_param_fatal);
7970 #else
7971 return &expand_param_error;
7972 #endif
7973 }
7974
7975 starsub = vtype & VT_STARSUB;
7976 vtype &= ~VT_STARSUB;
7977
7978 /* If we are asked to display the attributes of an unset variable, V will
7979 be NULL after the call to get_var_and_type. Double-check here. */
7980 if ((xc == 'a' || xc == 'A') && vtype == VT_VARIABLE && varname && v == 0)
7981 v = find_variable (varname);
7982
7983 temp1 = (char *)NULL; /* shut up gcc */
7984 switch (vtype)
7985 {
7986 case VT_VARIABLE:
7987 case VT_ARRAYMEMBER:
7988 temp1 = string_transform (xc, v, val);
7989 if (vtype == VT_VARIABLE)
7990 FREE (val);
7991 if (temp1)
7992 {
7993 val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7994 ? quote_string (temp1)
7995 : quote_escapes (temp1);
7996 free (temp1);
7997 temp1 = val;
7998 }
7999 break;
8000 #if defined (ARRAY_VARS)
8001 case VT_ARRAYVAR:
8002 temp1 = array_transform (xc, v, starsub, quoted);
8003 if (temp1 && quoted == 0 && ifs_is_null)
8004 {
8005 /* Posix interp 888 */
8006 }
8007 else if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
8008 {
8009 val = quote_escapes (temp1);
8010 free (temp1);
8011 temp1 = val;
8012 }
8013 break;
8014 #endif
8015 case VT_POSPARMS:
8016 temp1 = parameter_list_transform (xc, varname[0], quoted);
8017 if (temp1 && quoted == 0 && ifs_is_null)
8018 {
8019 /* Posix interp 888 */
8020 }
8021 else if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
8022 {
8023 val = quote_escapes (temp1);
8024 free (temp1);
8025 temp1 = val;
8026 }
8027 break;
8028 }
8029
8030 this_command_name = oname;
8031 return temp1;
8032 }
8033
8034 /******************************************************/
8035 /* */
8036 /* Functions to extract substrings of variable values */
8037 /* */
8038 /******************************************************/
8039
8040 #if defined (HANDLE_MULTIBYTE)
8041 /* Character-oriented rather than strictly byte-oriented substrings. S and
8042 E, rather being strict indices into STRING, indicate character (possibly
8043 multibyte character) positions that require calculation.
8044 Used by the ${param:offset[:length]} expansion. */
8045 static char *
8046 mb_substring (string, s, e)
8047 char *string;
8048 int s, e;
8049 {
8050 char *tt;
8051 int start, stop, i;
8052 size_t slen;
8053 DECLARE_MBSTATE;
8054
8055 start = 0;
8056 /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
8057 slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0;
8058
8059 i = s;
8060 while (string[start] && i--)
8061 ADVANCE_CHAR (string, slen, start);
8062 stop = start;
8063 i = e - s;
8064 while (string[stop] && i--)
8065 ADVANCE_CHAR (string, slen, stop);
8066 tt = substring (string, start, stop);
8067 return tt;
8068 }
8069 #endif
8070
8071 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
8072 is `@', use the positional parameters; otherwise, use the value of
8073 VARNAME. If VARNAME is an array variable, use the array elements. */
8074
8075 static char *
8076 parameter_brace_substring (varname, value, ind, substr, quoted, pflags, flags)
8077 char *varname, *value;
8078 int ind;
8079 char *substr;
8080 int quoted, pflags, flags;
8081 {
8082 intmax_t e1, e2;
8083 int vtype, r, starsub;
8084 char *temp, *val, *tt, *oname;
8085 SHELL_VAR *v;
8086
8087 if (value == 0 && ((varname[0] != '@' && varname[0] != '*') || varname[1]))
8088 return ((char *)NULL);
8089
8090 oname = this_command_name;
8091 this_command_name = varname;
8092
8093 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
8094 if (vtype == -1)
8095 {
8096 this_command_name = oname;
8097 return ((char *)NULL);
8098 }
8099
8100 starsub = vtype & VT_STARSUB;
8101 vtype &= ~VT_STARSUB;
8102
8103 r = verify_substring_values (v, val, substr, vtype, &e1, &e2);
8104 this_command_name = oname;
8105 if (r <= 0)
8106 {
8107 if (vtype == VT_VARIABLE)
8108 FREE (val);
8109 return ((r == 0) ? &expand_param_error : (char *)NULL);
8110 }
8111
8112 switch (vtype)
8113 {
8114 case VT_VARIABLE:
8115 case VT_ARRAYMEMBER:
8116 #if defined (HANDLE_MULTIBYTE)
8117 if (MB_CUR_MAX > 1)
8118 tt = mb_substring (val, e1, e2);
8119 else
8120 #endif
8121 tt = substring (val, e1, e2);
8122
8123 if (vtype == VT_VARIABLE)
8124 FREE (val);
8125 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
8126 temp = quote_string (tt);
8127 else
8128 temp = tt ? quote_escapes (tt) : (char *)NULL;
8129 FREE (tt);
8130 break;
8131 case VT_POSPARMS:
8132 case VT_ARRAYVAR:
8133 if (vtype == VT_POSPARMS)
8134 tt = pos_params (varname, e1, e2, quoted, pflags);
8135 #if defined (ARRAY_VARS)
8136 /* assoc_subrange and array_subrange both call string_list_pos_params,
8137 so we can treat this case just like VT_POSPARAMS. */
8138 else if (assoc_p (v))
8139 /* we convert to list and take first e2 elements starting at e1th
8140 element -- officially undefined for now */
8141 tt = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted, pflags);
8142 else
8143 /* We want E2 to be the number of elements desired (arrays can be
8144 sparse, so verify_substring_values just returns the numbers
8145 specified and we rely on array_subrange to understand how to
8146 deal with them). */
8147 tt = array_subrange (array_cell (v), e1, e2, starsub, quoted, pflags);
8148 #endif
8149 /* We want to leave this alone in every case where pos_params/
8150 string_list_pos_params quotes the list members */
8151 if (tt && quoted == 0 && ifs_is_null)
8152 {
8153 temp = tt; /* Posix interp 888 */
8154 }
8155 else if (tt && quoted == 0 && (pflags & PF_ASSIGNRHS))
8156 {
8157 temp = tt; /* Posix interp 888 */
8158 }
8159 else if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
8160 {
8161 temp = tt ? quote_escapes (tt) : (char *)NULL;
8162 FREE (tt);
8163 }
8164 else
8165 temp = tt;
8166 break;
8167
8168 default:
8169 temp = (char *)NULL;
8170 }
8171
8172 return temp;
8173 }
8174
8175 /****************************************************************/
8176 /* */
8177 /* Functions to perform pattern substitution on variable values */
8178 /* */
8179 /****************************************************************/
8180
8181 #ifdef INCLUDE_UNUSED
8182 static int
8183 shouldexp_replacement (s)
8184 char *s;
8185 {
8186 register char *p;
8187
8188 for (p = s; p && *p; p++)
8189 {
8190 if (*p == '\\')
8191 p++;
8192 else if (*p == '&')
8193 return 1;
8194 }
8195 return 0;
8196 }
8197 #endif
8198
8199 char *
8200 pat_subst (string, pat, rep, mflags)
8201 char *string, *pat, *rep;
8202 int mflags;
8203 {
8204 char *ret, *s, *e, *str, *rstr, *mstr, *send;
8205 int rptr, mtype, rxpand, mlen;
8206 size_t rsize, l, replen, rslen;
8207 DECLARE_MBSTATE;
8208
8209 if (string == 0)
8210 return (savestring (""));
8211
8212 mtype = mflags & MATCH_TYPEMASK;
8213
8214 #if 0 /* TAG: bash-5.2? */
8215 rxpand = (rep && *rep) ? shouldexp_replacement (rep) : 0;
8216 #else
8217 rxpand = 0;
8218 #endif
8219
8220 /* Special cases:
8221 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
8222 * with REP and return the result.
8223 * 2. A null pattern with mtype == MATCH_END means to append REP to
8224 * STRING and return the result.
8225 * 3. A null STRING with a matching pattern means to append REP to
8226 * STRING and return the result.
8227 * These don't understand or process `&' in the replacement string.
8228 */
8229 if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
8230 {
8231 replen = STRLEN (rep);
8232 l = STRLEN (string);
8233 ret = (char *)xmalloc (replen + l + 2);
8234 if (replen == 0)
8235 strcpy (ret, string);
8236 else if (mtype == MATCH_BEG)
8237 {
8238 strcpy (ret, rep);
8239 strcpy (ret + replen, string);
8240 }
8241 else
8242 {
8243 strcpy (ret, string);
8244 strcpy (ret + l, rep);
8245 }
8246 return (ret);
8247 }
8248 else if (*string == 0 && (match_pattern (string, pat, mtype, &s, &e) != 0))
8249 {
8250 replen = STRLEN (rep);
8251 ret = (char *)xmalloc (replen + 1);
8252 if (replen == 0)
8253 ret[0] = '\0';
8254 else
8255 strcpy (ret, rep);
8256 return (ret);
8257 }
8258
8259 ret = (char *)xmalloc (rsize = 64);
8260 ret[0] = '\0';
8261 send = string + strlen (string);
8262
8263 for (replen = STRLEN (rep), rptr = 0, str = string; *str;)
8264 {
8265 if (match_pattern (str, pat, mtype, &s, &e) == 0)
8266 break;
8267 l = s - str;
8268
8269 if (rep && rxpand)
8270 {
8271 int x;
8272 mlen = e - s;
8273 mstr = xmalloc (mlen + 1);
8274 for (x = 0; x < mlen; x++)
8275 mstr[x] = s[x];
8276 mstr[mlen] = '\0';
8277 rstr = strcreplace (rep, '&', mstr, 0);
8278 free (mstr);
8279 rslen = strlen (rstr);
8280 }
8281 else
8282 {
8283 rstr = rep;
8284 rslen = replen;
8285 }
8286
8287 RESIZE_MALLOCED_BUFFER (ret, rptr, (l + rslen), rsize, 64);
8288
8289 /* OK, now copy the leading unmatched portion of the string (from
8290 str to s) to ret starting at rptr (the current offset). Then copy
8291 the replacement string at ret + rptr + (s - str). Increment
8292 rptr (if necessary) and str and go on. */
8293 if (l)
8294 {
8295 strncpy (ret + rptr, str, l);
8296 rptr += l;
8297 }
8298 if (replen)
8299 {
8300 strncpy (ret + rptr, rstr, rslen);
8301 rptr += rslen;
8302 }
8303 str = e; /* e == end of match */
8304
8305 if (rstr != rep)
8306 free (rstr);
8307
8308 if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
8309 break;
8310
8311 if (s == e)
8312 {
8313 /* On a zero-length match, make sure we copy one character, since
8314 we increment one character to avoid infinite recursion. */
8315 char *p, *origp, *origs;
8316 size_t clen;
8317
8318 RESIZE_MALLOCED_BUFFER (ret, rptr, locale_mb_cur_max, rsize, 64);
8319 #if defined (HANDLE_MULTIBYTE)
8320 p = origp = ret + rptr;
8321 origs = str;
8322 COPY_CHAR_P (p, str, send);
8323 rptr += p - origp;
8324 e += str - origs;
8325 #else
8326 ret[rptr++] = *str++;
8327 e++; /* avoid infinite recursion on zero-length match */
8328 #endif
8329 }
8330 }
8331
8332 /* Now copy the unmatched portion of the input string */
8333 if (str && *str)
8334 {
8335 RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
8336 strcpy (ret + rptr, str);
8337 }
8338 else
8339 ret[rptr] = '\0';
8340
8341 return ret;
8342 }
8343
8344 /* Do pattern match and replacement on the positional parameters. */
8345 static char *
8346 pos_params_pat_subst (string, pat, rep, mflags)
8347 char *string, *pat, *rep;
8348 int mflags;
8349 {
8350 WORD_LIST *save, *params;
8351 WORD_DESC *w;
8352 char *ret;
8353 int pchar, qflags, pflags;
8354
8355 save = params = list_rest_of_args ();
8356 if (save == 0)
8357 return ((char *)NULL);
8358
8359 for ( ; params; params = params->next)
8360 {
8361 ret = pat_subst (params->word->word, pat, rep, mflags);
8362 w = alloc_word_desc ();
8363 w->word = ret ? ret : savestring ("");
8364 dispose_word (params->word);
8365 params->word = w;
8366 }
8367
8368 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
8369 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
8370 pflags = (mflags & MATCH_ASSIGNRHS) == MATCH_ASSIGNRHS ? PF_ASSIGNRHS : 0;
8371
8372 /* If we are expanding in a context where word splitting will not be
8373 performed, treat as quoted. This changes how $* will be expanded. */
8374 if (pchar == '*' && (mflags & MATCH_ASSIGNRHS) && expand_no_split_dollar_star && ifs_is_null)
8375 qflags |= Q_DOUBLE_QUOTES; /* Posix interp 888 */
8376
8377 ret = string_list_pos_params (pchar, save, qflags, pflags);
8378 dispose_words (save);
8379
8380 return (ret);
8381 }
8382
8383 /* Perform pattern substitution on VALUE, which is the expansion of
8384 VARNAME. PATSUB is an expression supplying the pattern to match
8385 and the string to substitute. QUOTED is a flags word containing
8386 the type of quoting currently in effect. */
8387 static char *
8388 parameter_brace_patsub (varname, value, ind, patsub, quoted, pflags, flags)
8389 char *varname, *value;
8390 int ind;
8391 char *patsub;
8392 int quoted, pflags, flags;
8393 {
8394 int vtype, mflags, starsub, delim;
8395 char *val, *temp, *pat, *rep, *p, *lpatsub, *tt, *oname;
8396 SHELL_VAR *v;
8397
8398 if (value == 0)
8399 return ((char *)NULL);
8400
8401 oname = this_command_name;
8402 this_command_name = varname; /* error messages */
8403
8404 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
8405 if (vtype == -1)
8406 {
8407 this_command_name = oname;
8408 return ((char *)NULL);
8409 }
8410
8411 starsub = vtype & VT_STARSUB;
8412 vtype &= ~VT_STARSUB;
8413
8414 mflags = 0;
8415 /* PATSUB is never NULL when this is called. */
8416 if (*patsub == '/')
8417 {
8418 mflags |= MATCH_GLOBREP;
8419 patsub++;
8420 }
8421
8422 /* Malloc this because expand_string_if_necessary or one of the expansion
8423 functions in its call chain may free it on a substitution error. */
8424 lpatsub = savestring (patsub);
8425
8426 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8427 mflags |= MATCH_QUOTED;
8428
8429 if (starsub)
8430 mflags |= MATCH_STARSUB;
8431
8432 if (pflags & PF_ASSIGNRHS)
8433 mflags |= MATCH_ASSIGNRHS;
8434
8435 /* If the pattern starts with a `/', make sure we skip over it when looking
8436 for the replacement delimiter. */
8437 delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0);
8438 if (lpatsub[delim] == '/')
8439 {
8440 lpatsub[delim] = 0;
8441 rep = lpatsub + delim + 1;
8442 }
8443 else
8444 rep = (char *)NULL;
8445
8446 if (rep && *rep == '\0')
8447 rep = (char *)NULL;
8448
8449 /* Perform the same expansions on the pattern as performed by the
8450 pattern removal expansions. */
8451 pat = getpattern (lpatsub, quoted, 1);
8452
8453 if (rep)
8454 {
8455 /* We want to perform quote removal on the expanded replacement even if
8456 the entire expansion is double-quoted because the parser and string
8457 extraction functions treated quotes in the replacement string as
8458 special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */
8459 if (shell_compatibility_level > 42)
8460 rep = expand_string_if_necessary (rep, quoted & ~(Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT), expand_string_unsplit);
8461 /* This is the bash-4.2 code. */
8462 else if ((mflags & MATCH_QUOTED) == 0)
8463 rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
8464 else
8465 rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
8466 }
8467
8468 /* ksh93 doesn't allow the match specifier to be a part of the expanded
8469 pattern. This is an extension. Make sure we don't anchor the pattern
8470 at the beginning or end of the string if we're doing global replacement,
8471 though. */
8472 p = pat;
8473 if (mflags & MATCH_GLOBREP)
8474 mflags |= MATCH_ANY;
8475 else if (pat && pat[0] == '#')
8476 {
8477 mflags |= MATCH_BEG;
8478 p++;
8479 }
8480 else if (pat && pat[0] == '%')
8481 {
8482 mflags |= MATCH_END;
8483 p++;
8484 }
8485 else
8486 mflags |= MATCH_ANY;
8487
8488 /* OK, we now want to substitute REP for PAT in VAL. If
8489 flags & MATCH_GLOBREP is non-zero, the substitution is done
8490 everywhere, otherwise only the first occurrence of PAT is
8491 replaced. The pattern matching code doesn't understand
8492 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
8493 values passed in (VT_VARIABLE) so the pattern substitution
8494 code works right. We need to requote special chars after
8495 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
8496 other cases if QUOTED == 0, since the posparams and arrays
8497 indexed by * or @ do special things when QUOTED != 0. */
8498
8499 switch (vtype)
8500 {
8501 case VT_VARIABLE:
8502 case VT_ARRAYMEMBER:
8503 temp = pat_subst (val, p, rep, mflags);
8504 if (vtype == VT_VARIABLE)
8505 FREE (val);
8506 if (temp)
8507 {
8508 tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
8509 free (temp);
8510 temp = tt;
8511 }
8512 break;
8513 case VT_POSPARMS:
8514 /* This does the right thing for the case where we are not performing
8515 word splitting. MATCH_STARSUB restricts it to ${* /foo/bar}, and
8516 pos_params_pat_subst/string_list_pos_params will do the right thing
8517 in turn for the case where ifs_is_null. Posix interp 888 */
8518 if ((pflags & PF_NOSPLIT2) && (mflags & MATCH_STARSUB))
8519 mflags |= MATCH_ASSIGNRHS;
8520 temp = pos_params_pat_subst (val, p, rep, mflags);
8521 if (temp && quoted == 0 && ifs_is_null)
8522 {
8523 /* Posix interp 888 */
8524 }
8525 else if (temp && quoted == 0 && (pflags & PF_ASSIGNRHS))
8526 {
8527 /* Posix interp 888 */
8528 }
8529 else if (temp && (mflags & MATCH_QUOTED) == 0)
8530 {
8531 tt = quote_escapes (temp);
8532 free (temp);
8533 temp = tt;
8534 }
8535 break;
8536 #if defined (ARRAY_VARS)
8537 case VT_ARRAYVAR:
8538 /* If we are expanding in a context where word splitting will not be
8539 performed, treat as quoted. This changes how ${A[*]} will be
8540 expanded to make it identical to $*. */
8541 if ((mflags & MATCH_STARSUB) && (mflags & MATCH_ASSIGNRHS) && ifs_is_null)
8542 mflags |= MATCH_QUOTED; /* Posix interp 888 */
8543
8544 /* these eventually call string_list_pos_params */
8545 if (assoc_p (v))
8546 temp = assoc_patsub (assoc_cell (v), p, rep, mflags);
8547 else
8548 temp = array_patsub (array_cell (v), p, rep, mflags);
8549
8550 if (temp && quoted == 0 && ifs_is_null)
8551 {
8552 /* Posix interp 888 */
8553 }
8554 else if (temp && (mflags & MATCH_QUOTED) == 0)
8555 {
8556 tt = quote_escapes (temp);
8557 free (temp);
8558 temp = tt;
8559 }
8560 break;
8561 #endif
8562 }
8563
8564 FREE (pat);
8565 FREE (rep);
8566 free (lpatsub);
8567
8568 this_command_name = oname;
8569
8570 return temp;
8571 }
8572
8573 /****************************************************************/
8574 /* */
8575 /* Functions to perform case modification on variable values */
8576 /* */
8577 /****************************************************************/
8578
8579 /* Do case modification on the positional parameters. */
8580
8581 static char *
8582 pos_params_modcase (string, pat, modop, mflags)
8583 char *string, *pat;
8584 int modop;
8585 int mflags;
8586 {
8587 WORD_LIST *save, *params;
8588 WORD_DESC *w;
8589 char *ret;
8590 int pchar, qflags, pflags;
8591
8592 save = params = list_rest_of_args ();
8593 if (save == 0)
8594 return ((char *)NULL);
8595
8596 for ( ; params; params = params->next)
8597 {
8598 ret = sh_modcase (params->word->word, pat, modop);
8599 w = alloc_word_desc ();
8600 w->word = ret ? ret : savestring ("");
8601 dispose_word (params->word);
8602 params->word = w;
8603 }
8604
8605 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
8606 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
8607 pflags = (mflags & MATCH_ASSIGNRHS) == MATCH_ASSIGNRHS ? PF_ASSIGNRHS : 0;
8608
8609 /* If we are expanding in a context where word splitting will not be
8610 performed, treat as quoted. This changes how $* will be expanded. */
8611 if (pchar == '*' && (mflags & MATCH_ASSIGNRHS) && ifs_is_null)
8612 qflags |= Q_DOUBLE_QUOTES; /* Posix interp 888 */
8613
8614 ret = string_list_pos_params (pchar, save, qflags, pflags);
8615 dispose_words (save);
8616
8617 return (ret);
8618 }
8619
8620 /* Perform case modification on VALUE, which is the expansion of
8621 VARNAME. MODSPEC is an expression supplying the type of modification
8622 to perform. QUOTED is a flags word containing the type of quoting
8623 currently in effect. */
8624 static char *
8625 parameter_brace_casemod (varname, value, ind, modspec, patspec, quoted, pflags, flags)
8626 char *varname, *value;
8627 int ind, modspec;
8628 char *patspec;
8629 int quoted, pflags, flags;
8630 {
8631 int vtype, starsub, modop, mflags, x;
8632 char *val, *temp, *pat, *p, *lpat, *tt, *oname;
8633 SHELL_VAR *v;
8634
8635 if (value == 0)
8636 return ((char *)NULL);
8637
8638 oname = this_command_name;
8639 this_command_name = varname;
8640
8641 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
8642 if (vtype == -1)
8643 {
8644 this_command_name = oname;
8645 return ((char *)NULL);
8646 }
8647
8648 starsub = vtype & VT_STARSUB;
8649 vtype &= ~VT_STARSUB;
8650
8651 modop = 0;
8652 mflags = 0;
8653 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8654 mflags |= MATCH_QUOTED;
8655 if (starsub)
8656 mflags |= MATCH_STARSUB;
8657 if (pflags & PF_ASSIGNRHS)
8658 mflags |= MATCH_ASSIGNRHS;
8659
8660 p = patspec;
8661 if (modspec == '^')
8662 {
8663 x = p && p[0] == modspec;
8664 modop = x ? CASE_UPPER : CASE_UPFIRST;
8665 p += x;
8666 }
8667 else if (modspec == ',')
8668 {
8669 x = p && p[0] == modspec;
8670 modop = x ? CASE_LOWER : CASE_LOWFIRST;
8671 p += x;
8672 }
8673 else if (modspec == '~')
8674 {
8675 x = p && p[0] == modspec;
8676 modop = x ? CASE_TOGGLEALL : CASE_TOGGLE;
8677 p += x;
8678 }
8679
8680 lpat = p ? savestring (p) : 0;
8681 /* Perform the same expansions on the pattern as performed by the
8682 pattern removal expansions. */
8683 pat = lpat ? getpattern (lpat, quoted, 1) : 0;
8684
8685 /* OK, now we do the case modification. */
8686 switch (vtype)
8687 {
8688 case VT_VARIABLE:
8689 case VT_ARRAYMEMBER:
8690 temp = sh_modcase (val, pat, modop);
8691 if (vtype == VT_VARIABLE)
8692 FREE (val);
8693 if (temp)
8694 {
8695 tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
8696 free (temp);
8697 temp = tt;
8698 }
8699 break;
8700
8701 case VT_POSPARMS:
8702 temp = pos_params_modcase (val, pat, modop, mflags);
8703 if (temp && quoted == 0 && ifs_is_null)
8704 {
8705 /* Posix interp 888 */
8706 }
8707 else if (temp && (mflags & MATCH_QUOTED) == 0)
8708 {
8709 tt = quote_escapes (temp);
8710 free (temp);
8711 temp = tt;
8712 }
8713 break;
8714
8715 #if defined (ARRAY_VARS)
8716 case VT_ARRAYVAR:
8717 /* If we are expanding in a context where word splitting will not be
8718 performed, treat as quoted. This changes how ${A[*]} will be
8719 expanded to make it identical to $*. */
8720 if ((mflags & MATCH_STARSUB) && (mflags & MATCH_ASSIGNRHS) && ifs_is_null)
8721 mflags |= MATCH_QUOTED; /* Posix interp 888 */
8722
8723 temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags)
8724 : array_modcase (array_cell (v), pat, modop, mflags);
8725
8726 if (temp && quoted == 0 && ifs_is_null)
8727 {
8728 /* Posix interp 888 */
8729 }
8730 else if (temp && (mflags & MATCH_QUOTED) == 0)
8731 {
8732 tt = quote_escapes (temp);
8733 free (temp);
8734 temp = tt;
8735 }
8736
8737 break;
8738 #endif
8739 }
8740
8741 FREE (pat);
8742 free (lpat);
8743
8744 this_command_name = oname;
8745
8746 return temp;
8747 }
8748
8749 /* Check for unbalanced parens in S, which is the contents of $(( ... )). If
8750 any occur, this must be a nested command substitution, so return 0.
8751 Otherwise, return 1. A valid arithmetic expression must always have a
8752 ( before a matching ), so any cases where there are more right parens
8753 means that this must not be an arithmetic expression, though the parser
8754 will not accept it without a balanced total number of parens. */
8755 static int
8756 chk_arithsub (s, len)
8757 const char *s;
8758 int len;
8759 {
8760 int i, count;
8761 DECLARE_MBSTATE;
8762
8763 i = count = 0;
8764 while (i < len)
8765 {
8766 if (s[i] == LPAREN)
8767 count++;
8768 else if (s[i] == RPAREN)
8769 {
8770 count--;
8771 if (count < 0)
8772 return 0;
8773 }
8774
8775 switch (s[i])
8776 {
8777 default:
8778 ADVANCE_CHAR (s, len, i);
8779 break;
8780
8781 case '\\':
8782 i++;
8783 if (s[i])
8784 ADVANCE_CHAR (s, len, i);
8785 break;
8786
8787 case '\'':
8788 i = skip_single_quoted (s, len, ++i, 0);
8789 break;
8790
8791 case '"':
8792 i = skip_double_quoted ((char *)s, len, ++i, 0);
8793 break;
8794 }
8795 }
8796
8797 return (count == 0);
8798 }
8799
8800 /****************************************************************/
8801 /* */
8802 /* Functions to perform parameter expansion on a string */
8803 /* */
8804 /****************************************************************/
8805
8806 /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
8807 static WORD_DESC *
8808 parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at)
8809 char *string;
8810 int *indexp, quoted, pflags, *quoted_dollar_atp, *contains_dollar_at;
8811 {
8812 int check_nullness, var_is_set, var_is_null, var_is_special;
8813 int want_substring, want_indir, want_patsub, want_casemod, want_attributes;
8814 char *name, *value, *temp, *temp1;
8815 WORD_DESC *tdesc, *ret;
8816 int t_index, sindex, c, tflag, modspec, local_pflags, all_element_arrayref;
8817 intmax_t number;
8818 arrayind_t ind;
8819
8820 temp = temp1 = value = (char *)NULL;
8821 var_is_set = var_is_null = var_is_special = check_nullness = 0;
8822 want_substring = want_indir = want_patsub = want_casemod = want_attributes = 0;
8823
8824 local_pflags = 0;
8825 all_element_arrayref = 0;
8826
8827 sindex = *indexp;
8828 t_index = ++sindex;
8829 /* ${#var} doesn't have any of the other parameter expansions on it. */
8830 if (string[t_index] == '#' && legal_variable_starter (string[t_index+1])) /* {{ */
8831 name = string_extract (string, &t_index, "}", SX_VARNAME);
8832 else
8833 #if defined (CASEMOD_EXPANSIONS)
8834 /* To enable case-toggling expansions using the `~' operator character
8835 define CASEMOD_TOGGLECASE in config-top.h */
8836 # if defined (CASEMOD_TOGGLECASE)
8837 name = string_extract (string, &t_index, "#%^,~:-=?+/@}", SX_VARNAME);
8838 # else
8839 name = string_extract (string, &t_index, "#%^,:-=?+/@}", SX_VARNAME);
8840 # endif /* CASEMOD_TOGGLECASE */
8841 #else
8842 name = string_extract (string, &t_index, "#%:-=?+/@}", SX_VARNAME);
8843 #endif /* CASEMOD_EXPANSIONS */
8844
8845 /* Handle ${@[stuff]} now that @ is a word expansion operator. Not exactly
8846 the cleanest code ever. */
8847 if (*name == 0 && sindex == t_index && string[sindex] == '@')
8848 {
8849 name = (char *)xrealloc (name, 2);
8850 name[0] = '@';
8851 name[1] = '\0';
8852 t_index++;
8853 }
8854 else if (*name == '!' && t_index > sindex && string[t_index] == '@' && string[t_index+1] == RBRACE)
8855 {
8856 name = (char *)xrealloc (name, t_index - sindex + 2);
8857 name[t_index - sindex] = '@';
8858 name[t_index - sindex + 1] = '\0';
8859 t_index++;
8860 }
8861
8862 ret = 0;
8863 tflag = 0;
8864
8865 ind = INTMAX_MIN;
8866
8867 /* If the name really consists of a special variable, then make sure
8868 that we have the entire name. We don't allow indirect references
8869 to special variables except `#', `?', `@' and `*'. This clause is
8870 designed to handle ${#SPECIAL} and ${!SPECIAL}, not anything more
8871 general. */
8872 if ((sindex == t_index && VALID_SPECIAL_LENGTH_PARAM (string[t_index])) ||
8873 (sindex == t_index && string[sindex] == '#' && VALID_SPECIAL_LENGTH_PARAM (string[sindex + 1])) ||
8874 (sindex == t_index - 1 && string[sindex] == '!' && VALID_INDIR_PARAM (string[t_index])))
8875 {
8876 t_index++;
8877 temp1 = string_extract (string, &t_index, "#%:-=?+/@}", 0);
8878 name = (char *)xrealloc (name, 3 + (strlen (temp1)));
8879 *name = string[sindex];
8880 if (string[sindex] == '!')
8881 {
8882 /* indirect reference of $#, $?, $@, or $* */
8883 name[1] = string[sindex + 1];
8884 strcpy (name + 2, temp1);
8885 }
8886 else
8887 strcpy (name + 1, temp1);
8888 free (temp1);
8889 }
8890 sindex = t_index;
8891
8892 /* Find out what character ended the variable name. Then
8893 do the appropriate thing. */
8894 if (c = string[sindex])
8895 sindex++;
8896
8897 /* If c is followed by one of the valid parameter expansion
8898 characters, move past it as normal. If not, assume that
8899 a substring specification is being given, and do not move
8900 past it. */
8901 if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
8902 {
8903 check_nullness++;
8904 if (c = string[sindex])
8905 sindex++;
8906 }
8907 else if (c == ':' && string[sindex] != RBRACE)
8908 want_substring = 1;
8909 else if (c == '/' /* && string[sindex] != RBRACE */) /* XXX */
8910 want_patsub = 1;
8911 #if defined (CASEMOD_EXPANSIONS)
8912 else if (c == '^' || c == ',' || c == '~')
8913 {
8914 modspec = c;
8915 want_casemod = 1;
8916 }
8917 #endif
8918 else if (c == '@' && (string[sindex] == 'a' || string[sindex] == 'A') && string[sindex+1] == RBRACE)
8919 {
8920 /* special case because we do not want to shortcut foo as foo[0] here */
8921 want_attributes = 1;
8922 local_pflags |= PF_ALLINDS;
8923 }
8924
8925 /* Catch the valid and invalid brace expressions that made it through the
8926 tests above. */
8927 /* ${#-} is a valid expansion and means to take the length of $-.
8928 Similarly for ${#?} and ${##}... */
8929 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
8930 VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
8931 {
8932 name = (char *)xrealloc (name, 3);
8933 name[1] = c;
8934 name[2] = '\0';
8935 c = string[sindex++];
8936 }
8937
8938 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
8939 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
8940 member (c, "%:=+/") && string[sindex] == RBRACE)
8941 {
8942 temp = (char *)NULL;
8943 goto bad_substitution; /* XXX - substitution error */
8944 }
8945
8946 /* Indirect expansion begins with a `!'. A valid indirect expansion is
8947 either a variable name, one of the positional parameters or a special
8948 variable that expands to one of the positional parameters. */
8949 want_indir = *name == '!' &&
8950 (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
8951 || VALID_INDIR_PARAM (name[1]));
8952
8953 /* Determine the value of this variable whose name is NAME. */
8954
8955 /* Check for special variables, directly referenced. */
8956 if (SPECIAL_VAR (name, want_indir))
8957 var_is_special++;
8958
8959 /* Check for special expansion things, like the length of a parameter */
8960 if (*name == '#' && name[1])
8961 {
8962 /* If we are not pointing at the character just after the
8963 closing brace, then we haven't gotten all of the name.
8964 Since it begins with a special character, this is a bad
8965 substitution. Also check NAME for validity before trying
8966 to go on. */
8967 if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
8968 {
8969 temp = (char *)NULL;
8970 goto bad_substitution; /* substitution error */
8971 }
8972
8973 number = parameter_brace_expand_length (name);
8974 if (number == INTMAX_MIN && unbound_vars_is_error)
8975 {
8976 set_exit_status (EXECUTION_FAILURE);
8977 err_unboundvar (name+1);
8978 free (name);
8979 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
8980 }
8981 free (name);
8982
8983 *indexp = sindex;
8984 if (number < 0)
8985 return (&expand_wdesc_error);
8986 else
8987 {
8988 ret = alloc_word_desc ();
8989 ret->word = itos (number);
8990 return ret;
8991 }
8992 }
8993
8994 /* ${@} is identical to $@. */
8995 if (name[0] == '@' && name[1] == '\0')
8996 {
8997 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
8998 *quoted_dollar_atp = 1;
8999
9000 if (contains_dollar_at)
9001 *contains_dollar_at = 1;
9002
9003 tflag |= W_DOLLARAT;
9004 }
9005
9006 /* Process ${!PREFIX*} expansion. */
9007 if (want_indir && string[sindex - 1] == RBRACE &&
9008 (string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
9009 legal_variable_starter ((unsigned char) name[1]))
9010 {
9011 char **x;
9012 WORD_LIST *xlist;
9013
9014 temp1 = savestring (name + 1);
9015 number = strlen (temp1);
9016 temp1[number - 1] = '\0';
9017 x = all_variables_matching_prefix (temp1);
9018 xlist = strvec_to_word_list (x, 0, 0);
9019 if (string[sindex - 2] == '*')
9020 temp = string_list_dollar_star (xlist, quoted, 0);
9021 else
9022 {
9023 temp = string_list_dollar_at (xlist, quoted, 0);
9024 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
9025 *quoted_dollar_atp = 1;
9026 if (contains_dollar_at)
9027 *contains_dollar_at = 1;
9028
9029 tflag |= W_DOLLARAT;
9030 }
9031 free (x);
9032 dispose_words (xlist);
9033 free (temp1);
9034 *indexp = sindex;
9035
9036 free (name);
9037
9038 ret = alloc_word_desc ();
9039 ret->word = temp;
9040 ret->flags = tflag; /* XXX */
9041 return ret;
9042 }
9043
9044 #if defined (ARRAY_VARS)
9045 /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */
9046 if (want_indir && string[sindex - 1] == RBRACE &&
9047 string[sindex - 2] == RBRACK && valid_array_reference (name+1, 0))
9048 {
9049 char *x, *x1;
9050
9051 temp1 = savestring (name + 1);
9052 x = array_variable_name (temp1, 0, &x1, (int *)0);
9053 FREE (x);
9054 if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == RBRACK)
9055 {
9056 temp = array_keys (temp1, quoted, pflags); /* handles assoc vars too */
9057 if (x1[0] == '@')
9058 {
9059 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
9060 *quoted_dollar_atp = 1;
9061 if (contains_dollar_at)
9062 *contains_dollar_at = 1;
9063
9064 tflag |= W_DOLLARAT;
9065 }
9066
9067 free (name);
9068 free (temp1);
9069 *indexp = sindex;
9070
9071 ret = alloc_word_desc ();
9072 ret->word = temp;
9073 ret->flags = tflag; /* XXX */
9074 return ret;
9075 }
9076
9077 free (temp1);
9078 }
9079 #endif /* ARRAY_VARS */
9080
9081 /* Make sure that NAME is valid before trying to go on. */
9082 if (valid_brace_expansion_word (want_indir ? name + 1 : name,
9083 var_is_special) == 0)
9084 {
9085 temp = (char *)NULL;
9086 goto bad_substitution; /* substitution error */
9087 }
9088
9089 if (want_indir)
9090 {
9091 tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, pflags|local_pflags, quoted_dollar_atp, contains_dollar_at);
9092 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
9093 {
9094 temp = (char *)NULL;
9095 goto bad_substitution;
9096 }
9097
9098 /* Turn off the W_ARRAYIND flag because there is no way for this function
9099 to return the index we're supposed to be using. */
9100 if (tdesc && tdesc->flags)
9101 tdesc->flags &= ~W_ARRAYIND;
9102 }
9103 else
9104 {
9105 local_pflags |= PF_IGNUNBOUND|(pflags&(PF_NOSPLIT2|PF_ASSIGNRHS));
9106 tdesc = parameter_brace_expand_word (name, var_is_special, quoted, local_pflags, &ind);
9107 }
9108
9109 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
9110 {
9111 tflag = 0;
9112 tdesc = 0;
9113 }
9114
9115 if (tdesc)
9116 {
9117 temp = tdesc->word;
9118 tflag = tdesc->flags;
9119 dispose_word_desc (tdesc);
9120 }
9121 else
9122 temp = (char *)0;
9123
9124 if (temp == &expand_param_error || temp == &expand_param_fatal)
9125 {
9126 FREE (name);
9127 FREE (value);
9128 return (temp == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
9129 }
9130
9131 #if defined (ARRAY_VARS)
9132 if (valid_array_reference (name, 0))
9133 {
9134 int qflags;
9135 char *t;
9136
9137 qflags = quoted;
9138 /* If in a context where word splitting will not take place, treat as
9139 if double-quoted. Has effects with $* and ${array[*]} */
9140
9141 if (pflags & PF_ASSIGNRHS)
9142 qflags |= Q_DOUBLE_QUOTES;
9143 /* We duplicate a little code here */
9144 t = mbschr (name, LBRACK);
9145 if (t && ALL_ELEMENT_SUB (t[1]) && t[2] == RBRACK)
9146 {
9147 all_element_arrayref = 1;
9148 if (expand_no_split_dollar_star && t[1] == '*') /* XXX */
9149 qflags |= Q_DOUBLE_QUOTES;
9150 }
9151 chk_atstar (name, qflags, pflags, quoted_dollar_atp, contains_dollar_at);
9152 }
9153 #endif
9154
9155 var_is_set = temp != (char *)0;
9156 var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
9157 /* XXX - this may not need to be restricted to special variables */
9158 if (check_nullness)
9159 var_is_null |= var_is_set && var_is_special && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp);
9160 #if defined (ARRAY_VARS)
9161 if (check_nullness)
9162 var_is_null |= var_is_set &&
9163 (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) &&
9164 QUOTED_NULL (temp) &&
9165 valid_array_reference (name, 0) &&
9166 chk_atstar (name, 0, 0, (int *)0, (int *)0);
9167 #endif
9168
9169 /* Get the rest of the stuff inside the braces. */
9170 if (c && c != RBRACE)
9171 {
9172 /* Extract the contents of the ${ ... } expansion
9173 according to the Posix.2 rules. */
9174 value = extract_dollar_brace_string (string, &sindex, quoted, (c == '%' || c == '#' || c =='/' || c == '^' || c == ',' || c ==':') ? SX_POSIXEXP|SX_WORD : SX_WORD);
9175 if (string[sindex] == RBRACE)
9176 sindex++;
9177 else
9178 goto bad_substitution; /* substitution error */
9179 }
9180 else
9181 value = (char *)NULL;
9182
9183 *indexp = sindex;
9184
9185 /* All the cases where an expansion can possibly generate an unbound
9186 variable error. */
9187 if (want_substring || want_patsub || want_casemod || c == '@' || c == '#' || c == '%' || c == RBRACE)
9188 {
9189 if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1]) && all_element_arrayref == 0)
9190 {
9191 set_exit_status (EXECUTION_FAILURE);
9192 err_unboundvar (name);
9193 FREE (value);
9194 FREE (temp);
9195 free (name);
9196 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
9197 }
9198 }
9199
9200 /* If this is a substring spec, process it and add the result. */
9201 if (want_substring)
9202 {
9203 temp1 = parameter_brace_substring (name, temp, ind, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
9204 FREE (value);
9205 FREE (temp);
9206
9207 if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
9208 {
9209 FREE (name);
9210 return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
9211 }
9212
9213 ret = alloc_word_desc ();
9214 ret->word = temp1;
9215 /* We test quoted_dollar_atp because we want variants with double-quoted
9216 "$@" to take a different code path. In fact, we make sure at the end
9217 of expand_word_internal that we're only looking at these flags if
9218 quoted_dollar_at == 0. */
9219 if (temp1 &&
9220 (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
9221 QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
9222 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
9223 else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 &&
9224 (pflags & PF_ASSIGNRHS))
9225 ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
9226 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9227 else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
9228 ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
9229
9230 FREE (name);
9231 return ret;
9232 }
9233 else if (want_patsub)
9234 {
9235 temp1 = parameter_brace_patsub (name, temp, ind, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
9236 FREE (value);
9237 FREE (temp);
9238
9239 if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
9240 {
9241 FREE (name);
9242 return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
9243 }
9244
9245 ret = alloc_word_desc ();
9246 ret->word = temp1;
9247 if (temp1 &&
9248 (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
9249 QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
9250 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
9251 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9252 else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
9253 ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
9254
9255 FREE (name);
9256 return ret;
9257 }
9258 #if defined (CASEMOD_EXPANSIONS)
9259 else if (want_casemod)
9260 {
9261 temp1 = parameter_brace_casemod (name, temp, ind, modspec, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
9262 FREE (value);
9263 FREE (temp);
9264
9265 if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
9266 {
9267 FREE (name);
9268 return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
9269 }
9270
9271 ret = alloc_word_desc ();
9272 ret->word = temp1;
9273 if (temp1 &&
9274 (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
9275 QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
9276 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
9277 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9278 else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
9279 ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
9280
9281 FREE (name);
9282 return ret;
9283 }
9284 #endif
9285
9286 /* Do the right thing based on which character ended the variable name. */
9287 switch (c)
9288 {
9289 default:
9290 case '\0':
9291 bad_substitution:
9292 set_exit_status (EXECUTION_FAILURE);
9293 report_error (_("%s: bad substitution"), string ? string : "??");
9294 FREE (value);
9295 FREE (temp);
9296 free (name);
9297 if (shell_compatibility_level <= 43)
9298 return &expand_wdesc_error;
9299 else
9300 return ((posixly_correct && interactive_shell == 0) ? &expand_wdesc_fatal : &expand_wdesc_error);
9301
9302 case RBRACE:
9303 break;
9304
9305 case '@':
9306 temp1 = parameter_brace_transform (name, temp, ind, value, c, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
9307 free (temp);
9308 free (value);
9309
9310 if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
9311 {
9312 free (name);
9313 set_exit_status (EXECUTION_FAILURE);
9314 report_error (_("%s: bad substitution"), string ? string : "??");
9315 return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
9316 }
9317
9318 ret = alloc_word_desc ();
9319 ret->word = temp1;
9320 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
9321 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
9322 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9323 else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
9324 ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
9325
9326 free (name);
9327 return ret;
9328
9329 case '#': /* ${param#[#]pattern} */
9330 case '%': /* ${param%[%]pattern} */
9331 if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
9332 {
9333 FREE (value);
9334 break;
9335 }
9336 temp1 = parameter_brace_remove_pattern (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
9337 free (temp);
9338 free (value);
9339
9340 ret = alloc_word_desc ();
9341 ret->word = temp1;
9342 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
9343 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
9344 /* Special handling for $* when unquoted and $IFS is null. Posix interp 888 */
9345 else if (temp1 && (name[0] == '*' && name[1] == 0) && quoted == 0 && ifs_is_null)
9346 ret->flags |= W_SPLITSPACE; /* Posix interp 888 */
9347
9348 free (name);
9349 return ret;
9350
9351 case '-':
9352 case '=':
9353 case '?':
9354 case '+':
9355 if (var_is_set && var_is_null == 0)
9356 {
9357 /* If the operator is `+', we don't want the value of the named
9358 variable for anything, just the value of the right hand side. */
9359 if (c == '+')
9360 {
9361 /* XXX -- if we're double-quoted and the named variable is "$@",
9362 we want to turn off any special handling of "$@" --
9363 we're not using it, so whatever is on the rhs applies. */
9364 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
9365 *quoted_dollar_atp = 0;
9366 if (contains_dollar_at)
9367 *contains_dollar_at = 0;
9368
9369 FREE (temp);
9370 if (value)
9371 {
9372 /* From Posix discussion on austin-group list. Issue 221
9373 requires that backslashes escaping `}' inside
9374 double-quoted ${...} be removed. */
9375 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
9376 quoted |= Q_DOLBRACE;
9377 ret = parameter_brace_expand_rhs (name, value, c,
9378 quoted,
9379 pflags,
9380 quoted_dollar_atp,
9381 contains_dollar_at);
9382 /* XXX - fix up later, esp. noting presence of
9383 W_HASQUOTEDNULL in ret->flags */
9384 free (value);
9385 }
9386 else
9387 temp = (char *)NULL;
9388 }
9389 else
9390 {
9391 FREE (value);
9392 }
9393 /* Otherwise do nothing; just use the value in TEMP. */
9394 }
9395 else /* VAR not set or VAR is NULL. */
9396 {
9397 FREE (temp);
9398 temp = (char *)NULL;
9399 if (c == '=' && var_is_special)
9400 {
9401 set_exit_status (EXECUTION_FAILURE);
9402 report_error (_("$%s: cannot assign in this way"), name);
9403 free (name);
9404 free (value);
9405 return &expand_wdesc_error;
9406 }
9407 else if (c == '?')
9408 {
9409 parameter_brace_expand_error (name, value, check_nullness);
9410 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
9411 }
9412 else if (c != '+')
9413 {
9414 /* XXX -- if we're double-quoted and the named variable is "$@",
9415 we want to turn off any special handling of "$@" --
9416 we're not using it, so whatever is on the rhs applies. */
9417 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
9418 *quoted_dollar_atp = 0;
9419 if (contains_dollar_at)
9420 *contains_dollar_at = 0;
9421
9422 /* From Posix discussion on austin-group list. Issue 221 requires
9423 that backslashes escaping `}' inside double-quoted ${...} be
9424 removed. */
9425 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
9426 quoted |= Q_DOLBRACE;
9427 ret = parameter_brace_expand_rhs (name, value, c, quoted, pflags,
9428 quoted_dollar_atp,
9429 contains_dollar_at);
9430 /* XXX - fix up later, esp. noting presence of
9431 W_HASQUOTEDNULL in tdesc->flags */
9432 }
9433 free (value);
9434 }
9435
9436 break;
9437 }
9438 free (name);
9439
9440 if (ret == 0)
9441 {
9442 ret = alloc_word_desc ();
9443 ret->flags = tflag;
9444 ret->word = temp;
9445 }
9446 return (ret);
9447 }
9448
9449 /* Expand a single ${xxx} expansion. The braces are optional. When
9450 the braces are used, parameter_brace_expand() does the work,
9451 possibly calling param_expand recursively. */
9452 static WORD_DESC *
9453 param_expand (string, sindex, quoted, expanded_something,
9454 contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
9455 pflags)
9456 char *string;
9457 int *sindex, quoted, *expanded_something, *contains_dollar_at;
9458 int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
9459 {
9460 char *temp, *temp1, uerror[3], *savecmd;
9461 int zindex, t_index, expok;
9462 unsigned char c;
9463 intmax_t number;
9464 SHELL_VAR *var;
9465 WORD_LIST *list, *l;
9466 WORD_DESC *tdesc, *ret;
9467 int tflag, nullarg;
9468
9469 /*itrace("param_expand: `%s' pflags = %d", string+*sindex, pflags);*/
9470 zindex = *sindex;
9471 c = string[++zindex];
9472
9473 temp = (char *)NULL;
9474 ret = tdesc = (WORD_DESC *)NULL;
9475 tflag = 0;
9476
9477 /* Do simple cases first. Switch on what follows '$'. */
9478 switch (c)
9479 {
9480 /* $0 .. $9? */
9481 case '0':
9482 case '1':
9483 case '2':
9484 case '3':
9485 case '4':
9486 case '5':
9487 case '6':
9488 case '7':
9489 case '8':
9490 case '9':
9491 temp1 = dollar_vars[TODIGIT (c)];
9492 /* This doesn't get called when (pflags&PF_IGNUNBOUND) != 0 */
9493 if (unbound_vars_is_error && temp1 == (char *)NULL)
9494 {
9495 uerror[0] = '$';
9496 uerror[1] = c;
9497 uerror[2] = '\0';
9498 set_exit_status (EXECUTION_FAILURE);
9499 err_unboundvar (uerror);
9500 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
9501 }
9502 if (temp1)
9503 temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
9504 ? quote_string (temp1)
9505 : quote_escapes (temp1);
9506 else
9507 temp = (char *)NULL;
9508
9509 break;
9510
9511 /* $$ -- pid of the invoking shell. */
9512 case '$':
9513 temp = itos (dollar_dollar_pid);
9514 break;
9515
9516 /* $# -- number of positional parameters. */
9517 case '#':
9518 temp = itos (number_of_args ());
9519 break;
9520
9521 /* $? -- return value of the last synchronous command. */
9522 case '?':
9523 temp = itos (last_command_exit_value);
9524 break;
9525
9526 /* $- -- flags supplied to the shell on invocation or by `set'. */
9527 case '-':
9528 temp = which_set_flags ();
9529 break;
9530
9531 /* $! -- Pid of the last asynchronous command. */
9532 case '!':
9533 /* If no asynchronous pids have been created, expand to nothing.
9534 If `set -u' has been executed, and no async processes have
9535 been created, this is an expansion error. */
9536 if (last_asynchronous_pid == NO_PID)
9537 {
9538 if (expanded_something)
9539 *expanded_something = 0;
9540 temp = (char *)NULL;
9541 if (unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
9542 {
9543 uerror[0] = '$';
9544 uerror[1] = c;
9545 uerror[2] = '\0';
9546 set_exit_status (EXECUTION_FAILURE);
9547 err_unboundvar (uerror);
9548 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
9549 }
9550 }
9551 else
9552 temp = itos (last_asynchronous_pid);
9553 break;
9554
9555 /* The only difference between this and $@ is when the arg is quoted. */
9556 case '*': /* `$*' */
9557 list = list_rest_of_args ();
9558
9559 #if 0
9560 /* According to austin-group posix proposal by Geoff Clare in
9561 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
9562
9563 "The shell shall write a message to standard error and
9564 immediately exit when it tries to expand an unset parameter
9565 other than the '@' and '*' special parameters."
9566 */
9567
9568 if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
9569 {
9570 uerror[0] = '$';
9571 uerror[1] = '*';
9572 uerror[2] = '\0';
9573 set_exit_status (EXECUTION_FAILURE);
9574 err_unboundvar (uerror);
9575 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
9576 }
9577 #endif
9578
9579 /* If there are no command-line arguments, this should just
9580 disappear if there are other characters in the expansion,
9581 even if it's quoted. */
9582 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
9583 temp = (char *)NULL;
9584 else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
9585 {
9586 /* If we have "$*" we want to make a string of the positional
9587 parameters, separated by the first character of $IFS, and
9588 quote the whole string, including the separators. If IFS
9589 is unset, the parameters are separated by ' '; if $IFS is
9590 null, the parameters are concatenated. */
9591 temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list, quoted, 0) : string_list (list);
9592 if (temp)
9593 {
9594 temp1 = (quoted & Q_DOUBLE_QUOTES) ? quote_string (temp) : temp;
9595 if (*temp == 0)
9596 tflag |= W_HASQUOTEDNULL;
9597 if (temp != temp1)
9598 free (temp);
9599 temp = temp1;
9600 }
9601 }
9602 else
9603 {
9604 /* We check whether or not we're eventually going to split $* here,
9605 for example when IFS is empty and we are processing the rhs of
9606 an assignment statement. In that case, we don't separate the
9607 arguments at all. Otherwise, if the $* is not quoted it is
9608 identical to $@ */
9609 if (expand_no_split_dollar_star && quoted == 0 && ifs_is_set == 0 && (pflags & PF_ASSIGNRHS))
9610 {
9611 /* Posix interp 888: RHS of assignment, IFS unset: no splitting,
9612 separate with space */
9613 temp1 = string_list_dollar_star (list, quoted, pflags);
9614 temp = temp1 ? quote_string (temp1) : temp1;
9615 /* XXX - tentative - note that we saw a quoted null here */
9616 if (temp1 && *temp1 == 0 && QUOTED_NULL (temp))
9617 tflag |= W_SAWQUOTEDNULL;
9618 FREE (temp1);
9619 }
9620 else if (expand_no_split_dollar_star && quoted == 0 && ifs_is_null && (pflags & PF_ASSIGNRHS))
9621 {
9622 /* Posix interp 888: RHS of assignment, IFS set to '' */
9623 temp1 = string_list_dollar_star (list, quoted, pflags);
9624 temp = temp1 ? quote_escapes (temp1) : temp1;
9625 FREE (temp1);
9626 }
9627 else if (expand_no_split_dollar_star && quoted == 0 && ifs_is_set && ifs_is_null == 0 && (pflags & PF_ASSIGNRHS))
9628 {
9629 /* Posix interp 888: RHS of assignment, IFS set to non-null value */
9630 temp1 = string_list_dollar_star (list, quoted, pflags);
9631 temp = temp1 ? quote_string (temp1) : temp1;
9632
9633 /* XXX - tentative - note that we saw a quoted null here */
9634 if (temp1 && *temp1 == 0 && QUOTED_NULL (temp))
9635 tflag |= W_SAWQUOTEDNULL;
9636 FREE (temp1);
9637 }
9638 /* XXX - should we check ifs_is_set here as well? */
9639 # if defined (HANDLE_MULTIBYTE)
9640 else if (expand_no_split_dollar_star && ifs_firstc[0] == 0)
9641 # else
9642 else if (expand_no_split_dollar_star && ifs_firstc == 0)
9643 # endif
9644 /* Posix interp 888: not RHS, no splitting, IFS set to '' */
9645 temp = string_list_dollar_star (list, quoted, 0);
9646 else
9647 {
9648 temp = string_list_dollar_at (list, quoted, 0);
9649 /* Set W_SPLITSPACE to make sure the individual positional
9650 parameters are split into separate arguments */
9651 #if 0
9652 if (quoted == 0 && (ifs_is_set == 0 || ifs_is_null))
9653 #else /* change with bash-5.0 */
9654 if (quoted == 0 && ifs_is_null)
9655 #endif
9656 tflag |= W_SPLITSPACE;
9657 /* If we're not quoted but we still don't want word splitting, make
9658 we quote the IFS characters to protect them from splitting (e.g.,
9659 when $@ is in the string as well). */
9660 else if (temp && quoted == 0 && ifs_is_set && (pflags & PF_ASSIGNRHS))
9661 {
9662 temp1 = quote_string (temp);
9663 free (temp);
9664 temp = temp1;
9665 }
9666 }
9667
9668 if (expand_no_split_dollar_star == 0 && contains_dollar_at)
9669 *contains_dollar_at = 1;
9670 }
9671
9672 dispose_words (list);
9673 break;
9674
9675 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
9676 means that we have to turn quoting off after we split into
9677 the individually quoted arguments so that the final split
9678 on the first character of $IFS is still done. */
9679 case '@': /* `$@' */
9680 list = list_rest_of_args ();
9681
9682 #if 0
9683 /* According to austin-group posix proposal by Geoff Clare in
9684 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
9685
9686 "The shell shall write a message to standard error and
9687 immediately exit when it tries to expand an unset parameter
9688 other than the '@' and '*' special parameters."
9689 */
9690
9691 if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
9692 {
9693 uerror[0] = '$';
9694 uerror[1] = '@';
9695 uerror[2] = '\0';
9696 set_exit_status (EXECUTION_FAILURE);
9697 err_unboundvar (uerror);
9698 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
9699 }
9700 #endif
9701
9702 for (nullarg = 0, l = list; l; l = l->next)
9703 {
9704 if (l->word && (l->word->word == 0 || l->word->word[0] == 0))
9705 nullarg = 1;
9706 }
9707
9708 /* We want to flag the fact that we saw this. We can't turn
9709 off quoting entirely, because other characters in the
9710 string might need it (consider "\"$@\""), but we need some
9711 way to signal that the final split on the first character
9712 of $IFS should be done, even though QUOTED is 1. */
9713 /* XXX - should this test include Q_PATQUOTE? */
9714 if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
9715 *quoted_dollar_at_p = 1;
9716 if (contains_dollar_at)
9717 *contains_dollar_at = 1;
9718
9719 /* We want to separate the positional parameters with the first
9720 character of $IFS in case $IFS is something other than a space.
9721 We also want to make sure that splitting is done no matter what --
9722 according to POSIX.2, this expands to a list of the positional
9723 parameters no matter what IFS is set to. */
9724 /* XXX - what to do when in a context where word splitting is not
9725 performed? Even when IFS is not the default, posix seems to imply
9726 that we have to expand $@ to all the positional parameters and
9727 separate them with spaces, which are preserved because word splitting
9728 doesn't take place. See below for how we use PF_NOSPLIT2 here. */
9729
9730 /* These are the cases where word splitting will not be performed. */
9731 if (pflags & PF_ASSIGNRHS)
9732 {
9733 temp = string_list_dollar_at (list, (quoted|Q_DOUBLE_QUOTES), pflags);
9734 if (nullarg)
9735 tflag |= W_HASQUOTEDNULL; /* we know quoting produces quoted nulls */
9736 }
9737
9738 /* This needs to match what expand_word_internal does with non-quoted $@
9739 does with separating with spaces. Passing Q_DOUBLE_QUOTES means that
9740 the characters in LIST will be quoted, and PF_ASSIGNRHS ensures that
9741 they will separated by spaces. After doing this, we need the special
9742 handling for PF_NOSPLIT2 in expand_word_internal to remove the CTLESC
9743 quotes. */
9744 else if (pflags & PF_NOSPLIT2)
9745 {
9746 #if defined (HANDLE_MULTIBYTE)
9747 if (quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc[0] != ' ')
9748 #else
9749 if (quoted == 0 && ifs_is_set && ifs_is_null == 0 && ifs_firstc != ' ')
9750 #endif
9751 /* Posix interp 888 */
9752 temp = string_list_dollar_at (list, Q_DOUBLE_QUOTES, pflags);
9753 else
9754 temp = string_list_dollar_at (list, quoted, pflags);
9755 }
9756 else
9757 temp = string_list_dollar_at (list, quoted, pflags);
9758
9759 tflag |= W_DOLLARAT;
9760 dispose_words (list);
9761 break;
9762
9763 case LBRACE:
9764 tdesc = parameter_brace_expand (string, &zindex, quoted, pflags,
9765 quoted_dollar_at_p,
9766 contains_dollar_at);
9767
9768 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
9769 return (tdesc);
9770 temp = tdesc ? tdesc->word : (char *)0;
9771
9772 /* XXX */
9773 /* Quoted nulls should be removed if there is anything else
9774 in the string. */
9775 /* Note that we saw the quoted null so we can add one back at
9776 the end of this function if there are no other characters
9777 in the string, discard TEMP, and go on. The exception to
9778 this is when we have "${@}" and $1 is '', since $@ needs
9779 special handling. */
9780 if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp))
9781 {
9782 if (had_quoted_null_p)
9783 *had_quoted_null_p = 1;
9784 if (*quoted_dollar_at_p == 0)
9785 {
9786 free (temp);
9787 tdesc->word = temp = (char *)NULL;
9788 }
9789
9790 }
9791
9792 ret = tdesc;
9793 goto return0;
9794
9795 /* Do command or arithmetic substitution. */
9796 case LPAREN:
9797 /* We have to extract the contents of this paren substitution. */
9798 t_index = zindex + 1;
9799 /* XXX - might want to check for string[t_index+2] == LPAREN and parse
9800 as arithmetic substitution immediately. */
9801 temp = extract_command_subst (string, &t_index, (pflags&PF_COMPLETE) ? SX_COMPLETE : 0);
9802 zindex = t_index;
9803
9804 /* For Posix.2-style `$(( ))' arithmetic substitution,
9805 extract the expression and pass it to the evaluator. */
9806 if (temp && *temp == LPAREN)
9807 {
9808 char *temp2;
9809 temp1 = temp + 1;
9810 temp2 = savestring (temp1);
9811 t_index = strlen (temp2) - 1;
9812
9813 if (temp2[t_index] != RPAREN)
9814 {
9815 free (temp2);
9816 goto comsub;
9817 }
9818
9819 /* Cut off ending `)' */
9820 temp2[t_index] = '\0';
9821
9822 if (chk_arithsub (temp2, t_index) == 0)
9823 {
9824 free (temp2);
9825 #if 0
9826 internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
9827 #endif
9828 goto comsub;
9829 }
9830
9831 /* Expand variables found inside the expression. */
9832 temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES|Q_ARITH);
9833 free (temp2);
9834
9835 arithsub:
9836 /* No error messages. */
9837 savecmd = this_command_name;
9838 this_command_name = (char *)NULL;
9839 number = evalexp (temp1, EXP_EXPANDED, &expok);
9840 this_command_name = savecmd;
9841 free (temp);
9842 free (temp1);
9843 if (expok == 0)
9844 {
9845 if (interactive_shell == 0 && posixly_correct)
9846 {
9847 set_exit_status (EXECUTION_FAILURE);
9848 return (&expand_wdesc_fatal);
9849 }
9850 else
9851 return (&expand_wdesc_error);
9852 }
9853 temp = itos (number);
9854 break;
9855 }
9856
9857 comsub:
9858 if (pflags & PF_NOCOMSUB)
9859 /* we need zindex+1 because string[zindex] == RPAREN */
9860 temp1 = substring (string, *sindex, zindex+1);
9861 else
9862 {
9863 tdesc = command_substitute (temp, quoted, pflags&PF_ASSIGNRHS);
9864 temp1 = tdesc ? tdesc->word : (char *)NULL;
9865 if (tdesc)
9866 dispose_word_desc (tdesc);
9867 }
9868 FREE (temp);
9869 temp = temp1;
9870 break;
9871
9872 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
9873 away in a future bash release. */
9874 case '[': /*]*/
9875 /* Extract the contents of this arithmetic substitution. */
9876 t_index = zindex + 1;
9877 temp = extract_arithmetic_subst (string, &t_index);
9878 zindex = t_index;
9879 if (temp == 0)
9880 {
9881 temp = savestring (string);
9882 if (expanded_something)
9883 *expanded_something = 0;
9884 goto return0;
9885 }
9886
9887 /* Do initial variable expansion. */
9888 temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES|Q_ARITH);
9889
9890 goto arithsub;
9891
9892 default:
9893 /* Find the variable in VARIABLE_LIST. */
9894 temp = (char *)NULL;
9895
9896 for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
9897 ;
9898 temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
9899
9900 /* If this isn't a variable name, then just output the `$'. */
9901 if (temp1 == 0 || *temp1 == '\0')
9902 {
9903 FREE (temp1);
9904 temp = (char *)xmalloc (2);
9905 temp[0] = '$';
9906 temp[1] = '\0';
9907 if (expanded_something)
9908 *expanded_something = 0;
9909 goto return0;
9910 }
9911
9912 /* If the variable exists, return its value cell. */
9913 var = find_variable (temp1);
9914
9915 if (var && invisible_p (var) == 0 && var_isset (var))
9916 {
9917 #if defined (ARRAY_VARS)
9918 if (assoc_p (var) || array_p (var))
9919 {
9920 temp = array_p (var) ? array_reference (array_cell (var), 0)
9921 : assoc_reference (assoc_cell (var), "0");
9922 if (temp)
9923 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
9924 ? quote_string (temp)
9925 : quote_escapes (temp);
9926 else if (unbound_vars_is_error)
9927 goto unbound_variable;
9928 }
9929 else
9930 #endif
9931 {
9932 temp = value_cell (var);
9933
9934 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
9935 ? quote_string (temp)
9936 : ((pflags & PF_ASSIGNRHS) ? quote_rhs (temp)
9937 : quote_escapes (temp));
9938 }
9939
9940 free (temp1);
9941
9942 goto return0;
9943 }
9944 else if (var && (invisible_p (var) || var_isset (var) == 0))
9945 temp = (char *)NULL;
9946 else if ((var = find_variable_last_nameref (temp1, 0)) && var_isset (var) && invisible_p (var) == 0)
9947 {
9948 temp = nameref_cell (var);
9949 #if defined (ARRAY_VARS)
9950 if (temp && *temp && valid_array_reference (temp, 0))
9951 {
9952 tdesc = parameter_brace_expand_word (temp, SPECIAL_VAR (temp, 0), quoted, pflags, (arrayind_t *)NULL);
9953 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
9954 return (tdesc);
9955 ret = tdesc;
9956 goto return0;
9957 }
9958 else
9959 #endif
9960 /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */
9961 if (temp && *temp && legal_identifier (temp) == 0)
9962 {
9963 set_exit_status (EXECUTION_FAILURE);
9964 report_error (_("%s: invalid variable name for name reference"), temp);
9965 return (&expand_wdesc_error); /* XXX */
9966 }
9967 else
9968 temp = (char *)NULL;
9969 }
9970
9971 temp = (char *)NULL;
9972
9973 unbound_variable:
9974 if (unbound_vars_is_error)
9975 {
9976 set_exit_status (EXECUTION_FAILURE);
9977 err_unboundvar (temp1);
9978 }
9979 else
9980 {
9981 free (temp1);
9982 goto return0;
9983 }
9984
9985 free (temp1);
9986 set_exit_status (EXECUTION_FAILURE);
9987 return ((unbound_vars_is_error && interactive_shell == 0)
9988 ? &expand_wdesc_fatal
9989 : &expand_wdesc_error);
9990 }
9991
9992 if (string[zindex])
9993 zindex++;
9994
9995 return0:
9996 *sindex = zindex;
9997
9998 if (ret == 0)
9999 {
10000 ret = alloc_word_desc ();
10001 ret->flags = tflag; /* XXX */
10002 ret->word = temp;
10003 }
10004 return ret;
10005 }
10006
10007 void
10008 invalidate_cached_quoted_dollar_at ()
10009 {
10010 dispose_words (cached_quoted_dollar_at);
10011 cached_quoted_dollar_at = 0;
10012 }
10013
10014 /* Make a word list which is the result of parameter and variable
10015 expansion, command substitution, arithmetic substitution, and
10016 quote removal of WORD. Return a pointer to a WORD_LIST which is
10017 the result of the expansion. If WORD contains a null word, the
10018 word list returned is also null.
10019
10020 QUOTED contains flag values defined in shell.h.
10021
10022 ISEXP is used to tell expand_word_internal that the word should be
10023 treated as the result of an expansion. This has implications for
10024 how IFS characters in the word are treated.
10025
10026 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
10027 they point to an integer value which receives information about expansion.
10028 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
10029 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
10030 else zero.
10031
10032 This only does word splitting in the case of $@ expansion. In that
10033 case, we split on ' '. */
10034
10035 /* Values for the local variable quoted_state. */
10036 #define UNQUOTED 0
10037 #define PARTIALLY_QUOTED 1
10038 #define WHOLLY_QUOTED 2
10039
10040 static WORD_LIST *
10041 expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
10042 WORD_DESC *word;
10043 int quoted, isexp;
10044 int *contains_dollar_at;
10045 int *expanded_something;
10046 {
10047 WORD_LIST *list;
10048 WORD_DESC *tword;
10049
10050 /* The intermediate string that we build while expanding. */
10051 char *istring;
10052
10053 /* The current size of the above object. */
10054 size_t istring_size;
10055
10056 /* Index into ISTRING. */
10057 int istring_index;
10058
10059 /* Temporary string storage. */
10060 char *temp, *temp1;
10061
10062 /* The text of WORD. */
10063 register char *string;
10064
10065 /* The size of STRING. */
10066 size_t string_size;
10067
10068 /* The index into STRING. */
10069 int sindex;
10070
10071 /* This gets 1 if we see a $@ while quoted. */
10072 int quoted_dollar_at;
10073
10074 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
10075 whether WORD contains no quoting characters, a partially quoted
10076 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
10077 int quoted_state;
10078
10079 /* State flags */
10080 int had_quoted_null;
10081 int has_quoted_ifs; /* did we add a quoted $IFS character here? */
10082 int has_dollar_at, temp_has_dollar_at;
10083 int split_on_spaces;
10084 int local_expanded;
10085 int tflag;
10086 int pflags; /* flags passed to param_expand */
10087 int mb_cur_max;
10088
10089 int assignoff; /* If assignment, offset of `=' */
10090
10091 register unsigned char c; /* Current character. */
10092 int t_index; /* For calls to string_extract_xxx. */
10093
10094 char twochars[2];
10095
10096 DECLARE_MBSTATE;
10097
10098 /* OK, let's see if we can optimize a common idiom: "$@" */
10099 if (STREQ (word->word, "\"$@\"") &&
10100 (word->flags == (W_HASDOLLAR|W_QUOTED)) &&
10101 dollar_vars[1]) /* XXX - check IFS here as well? */
10102 {
10103 if (contains_dollar_at)
10104 *contains_dollar_at = 1;
10105 if (expanded_something)
10106 *expanded_something = 1;
10107 if (cached_quoted_dollar_at)
10108 return (copy_word_list (cached_quoted_dollar_at));
10109 list = list_rest_of_args ();
10110 list = quote_list (list);
10111 cached_quoted_dollar_at = copy_word_list (list);
10112 return (list);
10113 }
10114
10115 istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
10116 istring[istring_index = 0] = '\0';
10117 quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
10118 has_quoted_ifs = 0;
10119 split_on_spaces = 0;
10120 quoted_state = UNQUOTED;
10121
10122 string = word->word;
10123 if (string == 0)
10124 goto finished_with_string;
10125 mb_cur_max = MB_CUR_MAX;
10126
10127 /* Don't need the string length for the SADD... and COPY_ macros unless
10128 multibyte characters are possible, but do need it for bounds checking. */
10129 string_size = (mb_cur_max > 1) ? strlen (string) : 1;
10130
10131 if (contains_dollar_at)
10132 *contains_dollar_at = 0;
10133
10134 assignoff = -1;
10135
10136 /* Begin the expansion. */
10137
10138 for (sindex = 0; ;)
10139 {
10140 c = string[sindex];
10141
10142 /* Case on top-level character. */
10143 switch (c)
10144 {
10145 case '\0':
10146 goto finished_with_string;
10147
10148 case CTLESC:
10149 sindex++;
10150 #if HANDLE_MULTIBYTE
10151 if (mb_cur_max > 1 && string[sindex])
10152 {
10153 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
10154 }
10155 else
10156 #endif
10157 {
10158 temp = (char *)xmalloc (3);
10159 temp[0] = CTLESC;
10160 temp[1] = c = string[sindex];
10161 temp[2] = '\0';
10162 }
10163
10164 dollar_add_string:
10165 if (string[sindex])
10166 sindex++;
10167
10168 add_string:
10169 if (temp)
10170 {
10171 istring = sub_append_string (temp, istring, &istring_index, &istring_size);
10172 temp = (char *)0;
10173 }
10174
10175 break;
10176
10177 #if defined (PROCESS_SUBSTITUTION)
10178 /* Process substitution. */
10179 case '<':
10180 case '>':
10181 {
10182 /* XXX - technically this should only be expanded at the start
10183 of a word */
10184 if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)))
10185 {
10186 sindex--; /* add_character: label increments sindex */
10187 goto add_character;
10188 }
10189 else
10190 t_index = sindex + 1; /* skip past both '<' and LPAREN */
10191
10192 temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index, 0); /*))*/
10193 sindex = t_index;
10194
10195 /* If the process substitution specification is `<()', we want to
10196 open the pipe for writing in the child and produce output; if
10197 it is `>()', we want to open the pipe for reading in the child
10198 and consume input. */
10199 temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
10200
10201 FREE (temp1);
10202
10203 goto dollar_add_string;
10204 }
10205 #endif /* PROCESS_SUBSTITUTION */
10206
10207 case '=':
10208 /* Posix.2 section 3.6.1 says that tildes following `=' in words
10209 which are not assignment statements are not expanded. If the
10210 shell isn't in posix mode, though, we perform tilde expansion
10211 on `likely candidate' unquoted assignment statements (flags
10212 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
10213 contains an unquoted :~ or =~. Something to think about: we
10214 now have a flag that says to perform tilde expansion on arguments
10215 to `assignment builtins' like declare and export that look like
10216 assignment statements. We now do tilde expansion on such words
10217 even in POSIX mode. */
10218 if (word->flags & (W_ASSIGNRHS|W_NOTILDE))
10219 {
10220 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
10221 goto add_ifs_character;
10222 else
10223 goto add_character;
10224 }
10225 /* If we're not in posix mode or forcing assignment-statement tilde
10226 expansion, note where the first `=' appears in the word and prepare
10227 to do tilde expansion following the first `='. We have to keep
10228 track of the first `=' (using assignoff) to avoid being confused
10229 by an `=' in the rhs of the assignment statement. */
10230 if ((word->flags & W_ASSIGNMENT) &&
10231 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
10232 assignoff == -1 && sindex > 0)
10233 assignoff = sindex;
10234 if (sindex == assignoff && string[sindex+1] == '~') /* XXX */
10235 word->flags |= W_ITILDE;
10236
10237 if (word->flags & W_ASSIGNARG)
10238 word->flags |= W_ASSIGNRHS; /* affects $@ */
10239
10240 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
10241 {
10242 has_quoted_ifs++;
10243 goto add_ifs_character;
10244 }
10245 else
10246 goto add_character;
10247
10248 case ':':
10249 if (word->flags & (W_NOTILDE|W_NOASSNTILDE))
10250 {
10251 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
10252 goto add_ifs_character;
10253 else
10254 goto add_character;
10255 }
10256
10257 if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS)) &&
10258 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
10259 string[sindex+1] == '~')
10260 word->flags |= W_ITILDE;
10261
10262 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
10263 goto add_ifs_character;
10264 else
10265 goto add_character;
10266
10267 case '~':
10268 /* If the word isn't supposed to be tilde expanded, or we're not
10269 at the start of a word or after an unquoted : or = in an
10270 assignment statement, we don't do tilde expansion. We don't
10271 do tilde expansion if quoted or in an arithmetic context. */
10272
10273 if ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
10274 (sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
10275 (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
10276 {
10277 word->flags &= ~W_ITILDE;
10278 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
10279 goto add_ifs_character;
10280 else
10281 goto add_character;
10282 }
10283
10284 if (word->flags & W_ASSIGNRHS)
10285 tflag = 2;
10286 else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
10287 tflag = 1;
10288 else
10289 tflag = 0;
10290
10291 temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
10292
10293 word->flags &= ~W_ITILDE;
10294
10295 if (temp && *temp && t_index > 0)
10296 {
10297 temp1 = bash_tilde_expand (temp, tflag);
10298 if (temp1 && *temp1 == '~' && STREQ (temp, temp1))
10299 {
10300 FREE (temp);
10301 FREE (temp1);
10302 goto add_character; /* tilde expansion failed */
10303 }
10304 free (temp);
10305 temp = temp1;
10306 sindex += t_index;
10307 goto add_quoted_string; /* XXX was add_string */
10308 }
10309 else
10310 {
10311 FREE (temp);
10312 goto add_character;
10313 }
10314
10315 case '$':
10316 if (expanded_something)
10317 *expanded_something = 1;
10318 local_expanded = 1;
10319
10320 temp_has_dollar_at = 0;
10321 pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0;
10322 if (word->flags & W_NOSPLIT2)
10323 pflags |= PF_NOSPLIT2;
10324 if (word->flags & W_ASSIGNRHS)
10325 pflags |= PF_ASSIGNRHS;
10326 if (word->flags & W_COMPLETE)
10327 pflags |= PF_COMPLETE;
10328
10329 tword = param_expand (string, &sindex, quoted, expanded_something,
10330 &temp_has_dollar_at, &quoted_dollar_at,
10331 &had_quoted_null, pflags);
10332 has_dollar_at += temp_has_dollar_at;
10333 split_on_spaces += (tword->flags & W_SPLITSPACE);
10334
10335 if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
10336 {
10337 free (string);
10338 free (istring);
10339 return ((tword == &expand_wdesc_error) ? &expand_word_error
10340 : &expand_word_fatal);
10341 }
10342 if (contains_dollar_at && has_dollar_at)
10343 *contains_dollar_at = 1;
10344
10345 if (tword && (tword->flags & W_HASQUOTEDNULL))
10346 had_quoted_null = 1; /* note for later */
10347 if (tword && (tword->flags & W_SAWQUOTEDNULL))
10348 had_quoted_null = 1; /* XXX */
10349
10350 temp = tword ? tword->word : (char *)NULL;
10351 dispose_word_desc (tword);
10352
10353 /* Kill quoted nulls; we will add them back at the end of
10354 expand_word_internal if nothing else in the string */
10355 if (had_quoted_null && temp && QUOTED_NULL (temp))
10356 {
10357 FREE (temp);
10358 temp = (char *)NULL;
10359 }
10360
10361 goto add_string;
10362 break;
10363
10364 case '`': /* Backquoted command substitution. */
10365 {
10366 t_index = sindex++;
10367
10368 temp = string_extract (string, &sindex, "`", SX_REQMATCH);
10369 /* The test of sindex against t_index is to allow bare instances of
10370 ` to pass through, for backwards compatibility. */
10371 if (temp == &extract_string_error || temp == &extract_string_fatal)
10372 {
10373 if (sindex - 1 == t_index)
10374 {
10375 sindex = t_index;
10376 goto add_character;
10377 }
10378 set_exit_status (EXECUTION_FAILURE);
10379 report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index);
10380 free (string);
10381 free (istring);
10382 return ((temp == &extract_string_error) ? &expand_word_error
10383 : &expand_word_fatal);
10384 }
10385
10386 if (expanded_something)
10387 *expanded_something = 1;
10388 local_expanded = 1;
10389
10390 if (word->flags & W_NOCOMSUB)
10391 /* sindex + 1 because string[sindex] == '`' */
10392 temp1 = substring (string, t_index, sindex + 1);
10393 else
10394 {
10395 de_backslash (temp);
10396 tword = command_substitute (temp, quoted, 0);
10397 temp1 = tword ? tword->word : (char *)NULL;
10398 if (tword)
10399 dispose_word_desc (tword);
10400 }
10401 FREE (temp);
10402 temp = temp1;
10403 goto dollar_add_string;
10404 }
10405
10406 case '\\':
10407 if (string[sindex + 1] == '\n')
10408 {
10409 sindex += 2;
10410 continue;
10411 }
10412
10413 c = string[++sindex];
10414
10415 /* "However, the double-quote character ( '"' ) shall not be treated
10416 specially within a here-document, except when the double-quote
10417 appears within "$()", "``", or "${}"." */
10418 if ((quoted & Q_HERE_DOCUMENT) && (quoted & Q_DOLBRACE) && c == '"')
10419 tflag = CBSDQUOTE; /* special case */
10420 else if (quoted & Q_HERE_DOCUMENT)
10421 tflag = CBSHDOC;
10422 else if (quoted & Q_DOUBLE_QUOTES)
10423 tflag = CBSDQUOTE;
10424 else
10425 tflag = 0;
10426
10427 /* From Posix discussion on austin-group list: Backslash escaping
10428 a } in ${...} is removed. Issue 0000221 */
10429 if ((quoted & Q_DOLBRACE) && c == RBRACE)
10430 {
10431 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
10432 }
10433 /* This is the fix for " $@\ " */
10434 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0) && isexp == 0 && isifs (c))
10435 {
10436 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
10437 DEFAULT_ARRAY_SIZE);
10438 istring[istring_index++] = CTLESC;
10439 istring[istring_index++] = '\\';
10440 istring[istring_index] = '\0';
10441
10442 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
10443 }
10444 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && c == 0)
10445 {
10446 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
10447 DEFAULT_ARRAY_SIZE);
10448 istring[istring_index++] = CTLESC;
10449 istring[istring_index++] = '\\';
10450 istring[istring_index] = '\0';
10451 break;
10452 }
10453 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
10454 {
10455 SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
10456 }
10457 else if (c == 0)
10458 {
10459 c = CTLNUL;
10460 sindex--; /* add_character: label increments sindex */
10461 goto add_character;
10462 }
10463 else
10464 {
10465 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
10466 }
10467
10468 sindex++;
10469 add_twochars:
10470 /* BEFORE jumping here, we need to increment sindex if appropriate */
10471 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
10472 DEFAULT_ARRAY_SIZE);
10473 istring[istring_index++] = twochars[0];
10474 istring[istring_index++] = twochars[1];
10475 istring[istring_index] = '\0';
10476
10477 break;
10478
10479 case '"':
10480 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && ((quoted & Q_ARITH) == 0))
10481 goto add_character;
10482
10483 t_index = ++sindex;
10484 temp = string_extract_double_quoted (string, &sindex, (word->flags & W_COMPLETE) ? SX_COMPLETE : 0);
10485
10486 /* If the quotes surrounded the entire string, then the
10487 whole word was quoted. */
10488 quoted_state = (t_index == 1 && string[sindex] == '\0')
10489 ? WHOLLY_QUOTED
10490 : PARTIALLY_QUOTED;
10491
10492 if (temp && *temp)
10493 {
10494 tword = alloc_word_desc ();
10495 tword->word = temp;
10496
10497 if (word->flags & W_ASSIGNARG)
10498 tword->flags |= word->flags & (W_ASSIGNARG|W_ASSIGNRHS); /* affects $@ */
10499 if (word->flags & W_COMPLETE)
10500 tword->flags |= W_COMPLETE; /* for command substitutions */
10501 if (word->flags & W_NOCOMSUB)
10502 tword->flags |= W_NOCOMSUB;
10503 if (word->flags & W_NOPROCSUB)
10504 tword->flags |= W_NOPROCSUB;
10505
10506 if (word->flags & W_ASSIGNRHS)
10507 tword->flags |= W_ASSIGNRHS;
10508
10509 temp = (char *)NULL;
10510
10511 temp_has_dollar_at = 0; /* does this quoted (sub)string include $@? */
10512 /* Need to get W_HASQUOTEDNULL flag through this function. */
10513 list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &temp_has_dollar_at, (int *)NULL);
10514 has_dollar_at += temp_has_dollar_at;
10515
10516 if (list == &expand_word_error || list == &expand_word_fatal)
10517 {
10518 free (istring);
10519 free (string);
10520 /* expand_word_internal has already freed temp_word->word
10521 for us because of the way it prints error messages. */
10522 tword->word = (char *)NULL;
10523 dispose_word (tword);
10524 return list;
10525 }
10526
10527 dispose_word (tword);
10528
10529 /* "$@" (a double-quoted dollar-at) expands into nothing,
10530 not even a NULL word, when there are no positional
10531 parameters. Posix interp 888 says that other parts of the
10532 word that expand to quoted nulls result in quoted nulls, so
10533 we can't just throw the entire word away if we have "$@"
10534 anywhere in it. We use had_quoted_null to keep track */
10535 if (list == 0 && temp_has_dollar_at) /* XXX - was has_dollar_at */
10536 {
10537 quoted_dollar_at++;
10538 break;
10539 }
10540
10541 /* If this list comes back with a quoted null from expansion,
10542 we have either "$x" or "$@" with $1 == ''. In either case,
10543 we need to make sure we add a quoted null argument and
10544 disable the special handling that "$@" gets. */
10545 if (list && list->word && list->next == 0 && (list->word->flags & W_HASQUOTEDNULL))
10546 {
10547 if (had_quoted_null && temp_has_dollar_at)
10548 quoted_dollar_at++;
10549 had_quoted_null = 1; /* XXX */
10550 }
10551
10552 /* If we get "$@", we know we have expanded something, so we
10553 need to remember it for the final split on $IFS. This is
10554 a special case; it's the only case where a quoted string
10555 can expand into more than one word. It's going to come back
10556 from the above call to expand_word_internal as a list with
10557 multiple words. */
10558 if (list)
10559 dequote_list (list);
10560
10561 if (temp_has_dollar_at) /* XXX - was has_dollar_at */
10562 {
10563 quoted_dollar_at++;
10564 if (contains_dollar_at)
10565 *contains_dollar_at = 1;
10566 if (expanded_something)
10567 *expanded_something = 1;
10568 local_expanded = 1;
10569 }
10570 }
10571 else
10572 {
10573 /* What we have is "". This is a minor optimization. */
10574 FREE (temp);
10575 list = (WORD_LIST *)NULL;
10576 had_quoted_null = 1; /* note for later */
10577 }
10578
10579 /* The code above *might* return a list (consider the case of "$@",
10580 where it returns "$1", "$2", etc.). We can't throw away the
10581 rest of the list, and we have to make sure each word gets added
10582 as quoted. We test on tresult->next: if it is non-NULL, we
10583 quote the whole list, save it to a string with string_list, and
10584 add that string. We don't need to quote the results of this
10585 (and it would be wrong, since that would quote the separators
10586 as well), so we go directly to add_string. */
10587 if (list)
10588 {
10589 if (list->next)
10590 {
10591 /* Testing quoted_dollar_at makes sure that "$@" is
10592 split correctly when $IFS does not contain a space. */
10593 temp = quoted_dollar_at
10594 ? string_list_dollar_at (list, Q_DOUBLE_QUOTES, 0)
10595 : string_list (quote_list (list));
10596 dispose_words (list);
10597 goto add_string;
10598 }
10599 else
10600 {
10601 temp = savestring (list->word->word);
10602 tflag = list->word->flags;
10603 dispose_words (list);
10604
10605 /* If the string is not a quoted null string, we want
10606 to remove any embedded unquoted CTLNUL characters.
10607 We do not want to turn quoted null strings back into
10608 the empty string, though. We do this because we
10609 want to remove any quoted nulls from expansions that
10610 contain other characters. For example, if we have
10611 x"$*"y or "x$*y" and there are no positional parameters,
10612 the $* should expand into nothing. */
10613 /* We use the W_HASQUOTEDNULL flag to differentiate the
10614 cases: a quoted null character as above and when
10615 CTLNUL is contained in the (non-null) expansion
10616 of some variable. We use the had_quoted_null flag to
10617 pass the value through this function to its caller. */
10618 if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0)
10619 remove_quoted_nulls (temp); /* XXX */
10620 }
10621 }
10622 else
10623 temp = (char *)NULL;
10624
10625 if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
10626 had_quoted_null = 1; /* note for later */
10627
10628 /* We do not want to add quoted nulls to strings that are only
10629 partially quoted; we can throw them away. The exception to
10630 this is when we are going to be performing word splitting,
10631 since we have to preserve a null argument if the next character
10632 will cause word splitting. */
10633 if (temp == 0 && quoted_state == PARTIALLY_QUOTED && quoted == 0 && (word->flags & (W_NOSPLIT|W_EXPANDRHS|W_ASSIGNRHS)) == W_EXPANDRHS)
10634 {
10635 c = CTLNUL;
10636 sindex--;
10637 had_quoted_null = 1;
10638 goto add_character;
10639 }
10640 if (temp == 0 && quoted_state == PARTIALLY_QUOTED && (word->flags & (W_NOSPLIT|W_NOSPLIT2)))
10641 continue;
10642
10643 add_quoted_string:
10644
10645 if (temp)
10646 {
10647 temp1 = temp;
10648 temp = quote_string (temp);
10649 free (temp1);
10650 goto add_string;
10651 }
10652 else
10653 {
10654 /* Add NULL arg. */
10655 c = CTLNUL;
10656 sindex--; /* add_character: label increments sindex */
10657 had_quoted_null = 1; /* note for later */
10658 goto add_character;
10659 }
10660
10661 /* break; */
10662
10663 case '\'':
10664 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
10665 goto add_character;
10666
10667 t_index = ++sindex;
10668 temp = string_extract_single_quoted (string, &sindex);
10669
10670 /* If the entire STRING was surrounded by single quotes,
10671 then the string is wholly quoted. */
10672 quoted_state = (t_index == 1 && string[sindex] == '\0')
10673 ? WHOLLY_QUOTED
10674 : PARTIALLY_QUOTED;
10675
10676 /* If all we had was '', it is a null expansion. */
10677 if (*temp == '\0')
10678 {
10679 free (temp);
10680 temp = (char *)NULL;
10681 }
10682 else
10683 remove_quoted_escapes (temp); /* ??? */
10684
10685 if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
10686 had_quoted_null = 1; /* note for later */
10687
10688 /* We do not want to add quoted nulls to strings that are only
10689 partially quoted; such nulls are discarded. See above for the
10690 exception, which is when the string is going to be split.
10691 Posix interp 888/1129 */
10692 if (temp == 0 && quoted_state == PARTIALLY_QUOTED && quoted == 0 && (word->flags & (W_NOSPLIT|W_EXPANDRHS|W_ASSIGNRHS)) == W_EXPANDRHS)
10693 {
10694 c = CTLNUL;
10695 sindex--;
10696 goto add_character;
10697 }
10698
10699 if (temp == 0 && (quoted_state == PARTIALLY_QUOTED) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)))
10700 continue;
10701
10702 /* If we have a quoted null expansion, add a quoted NULL to istring. */
10703 if (temp == 0)
10704 {
10705 c = CTLNUL;
10706 sindex--; /* add_character: label increments sindex */
10707 goto add_character;
10708 }
10709 else
10710 goto add_quoted_string;
10711
10712 /* break; */
10713
10714 case ' ':
10715 /* If we are in a context where the word is not going to be split, but
10716 we need to account for $@ and $* producing one word for each
10717 positional parameter, add quoted spaces so the spaces in the
10718 expansion of "$@", if any, behave correctly. We still may need to
10719 split if we are expanding the rhs of a word expansion. */
10720 if (ifs_is_null || split_on_spaces || ((word->flags & (W_NOSPLIT|W_NOSPLIT2|W_ASSIGNRHS)) && (word->flags & W_EXPANDRHS) == 0))
10721 {
10722 if (string[sindex])
10723 sindex++;
10724 twochars[0] = CTLESC;
10725 twochars[1] = c;
10726 goto add_twochars;
10727 }
10728 /* FALLTHROUGH */
10729
10730 default:
10731 /* This is the fix for " $@ " */
10732 add_ifs_character:
10733 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0))
10734 {
10735 if ((quoted&(Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0)
10736 has_quoted_ifs++;
10737 add_quoted_character:
10738 if (string[sindex]) /* from old goto dollar_add_string */
10739 sindex++;
10740 if (c == 0)
10741 {
10742 c = CTLNUL;
10743 goto add_character;
10744 }
10745 else
10746 {
10747 #if HANDLE_MULTIBYTE
10748 /* XXX - should make sure that c is actually multibyte,
10749 otherwise we can use the twochars branch */
10750 if (mb_cur_max > 1)
10751 sindex--;
10752
10753 if (mb_cur_max > 1)
10754 {
10755 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
10756 }
10757 else
10758 #endif
10759 {
10760 twochars[0] = CTLESC;
10761 twochars[1] = c;
10762 goto add_twochars;
10763 }
10764 }
10765 }
10766
10767 SADD_MBCHAR (temp, string, sindex, string_size);
10768
10769 add_character:
10770 RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
10771 DEFAULT_ARRAY_SIZE);
10772 istring[istring_index++] = c;
10773 istring[istring_index] = '\0';
10774
10775 /* Next character. */
10776 sindex++;
10777 }
10778 }
10779
10780 finished_with_string:
10781 /* OK, we're ready to return. If we have a quoted string, and
10782 quoted_dollar_at is not set, we do no splitting at all; otherwise
10783 we split on ' '. The routines that call this will handle what to
10784 do if nothing has been expanded. */
10785
10786 /* Partially and wholly quoted strings which expand to the empty
10787 string are retained as an empty arguments. Unquoted strings
10788 which expand to the empty string are discarded. The single
10789 exception is the case of expanding "$@" when there are no
10790 positional parameters. In that case, we discard the expansion. */
10791
10792 /* Because of how the code that handles "" and '' in partially
10793 quoted strings works, we need to make ISTRING into a QUOTED_NULL
10794 if we saw quoting characters, but the expansion was empty.
10795 "" and '' are tossed away before we get to this point when
10796 processing partially quoted strings. This makes "" and $xxx""
10797 equivalent when xxx is unset. We also look to see whether we
10798 saw a quoted null from a ${} expansion and add one back if we
10799 need to. */
10800
10801 /* If we expand to nothing and there were no single or double quotes
10802 in the word, we throw it away. Otherwise, we return a NULL word.
10803 The single exception is for $@ surrounded by double quotes when
10804 there are no positional parameters. In that case, we also throw
10805 the word away. */
10806
10807 if (*istring == '\0')
10808 {
10809 if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
10810 {
10811 istring[0] = CTLNUL;
10812 istring[1] = '\0';
10813 tword = alloc_word_desc ();
10814 tword->word = istring;
10815 istring = 0; /* avoid later free() */
10816 tword->flags |= W_HASQUOTEDNULL; /* XXX */
10817 list = make_word_list (tword, (WORD_LIST *)NULL);
10818 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
10819 tword->flags |= W_QUOTED;
10820 }
10821 /* According to sh, ksh, and Posix.2, if a word expands into nothing
10822 and a double-quoted "$@" appears anywhere in it, then the entire
10823 word is removed. */
10824 /* XXX - exception appears to be that quoted null strings result in
10825 null arguments */
10826 else if (quoted_state == UNQUOTED || quoted_dollar_at)
10827 list = (WORD_LIST *)NULL;
10828 else
10829 list = (WORD_LIST *)NULL;
10830 }
10831 else if (word->flags & W_NOSPLIT)
10832 {
10833 tword = alloc_word_desc ();
10834 tword->word = istring;
10835 if (had_quoted_null && QUOTED_NULL (istring))
10836 tword->flags |= W_HASQUOTEDNULL;
10837 istring = 0; /* avoid later free() */
10838 if (word->flags & W_ASSIGNMENT)
10839 tword->flags |= W_ASSIGNMENT; /* XXX */
10840 if (word->flags & W_COMPASSIGN)
10841 tword->flags |= W_COMPASSIGN; /* XXX */
10842 if (word->flags & W_NOGLOB)
10843 tword->flags |= W_NOGLOB; /* XXX */
10844 if (word->flags & W_NOBRACE)
10845 tword->flags |= W_NOBRACE; /* XXX */
10846 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
10847 tword->flags |= W_QUOTED;
10848 list = make_word_list (tword, (WORD_LIST *)NULL);
10849 }
10850 else if (word->flags & W_ASSIGNRHS)
10851 {
10852 list = list_string (istring, "", quoted);
10853 tword = list->word;
10854 if (had_quoted_null && QUOTED_NULL (istring))
10855 tword->flags |= W_HASQUOTEDNULL;
10856 free (list);
10857 free (istring);
10858 istring = 0; /* avoid later free() */
10859 goto set_word_flags;
10860 }
10861 else
10862 {
10863 char *ifs_chars;
10864
10865 ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
10866
10867 /* If we have $@, we need to split the results no matter what. If
10868 IFS is unset or NULL, string_list_dollar_at has separated the
10869 positional parameters with a space, so we split on space (we have
10870 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
10871 string_list_dollar_at has separated the positional parameters
10872 with the first character of $IFS, so we split on $IFS. If
10873 SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either
10874 unset or null, and we want to make sure that we split on spaces
10875 regardless of what else has happened to IFS since the expansion,
10876 or we expanded "$@" with IFS null and we need to split the positional
10877 parameters into separate words. */
10878 if (split_on_spaces)
10879 {
10880 /* If IFS is not set, and the word is not quoted, we want to split
10881 the individual words on $' \t\n'. We rely on previous steps to
10882 quote the portions of the word that should not be split */
10883 if (ifs_is_set == 0)
10884 list = list_string (istring, " \t\n", 1); /* XXX quoted == 1? */
10885 else
10886 list = list_string (istring, " ", 1); /* XXX quoted == 1? */
10887 }
10888
10889 /* If we have $@ (has_dollar_at != 0) and we are in a context where we
10890 don't want to split the result (W_NOSPLIT2), and we are not quoted,
10891 we have already separated the arguments with the first character of
10892 $IFS. In this case, we want to return a list with a single word
10893 with the separator possibly replaced with a space (it's what other
10894 shells seem to do).
10895 quoted_dollar_at is internal to this function and is set if we are
10896 passed an argument that is unquoted (quoted == 0) but we encounter a
10897 double-quoted $@ while expanding it. */
10898 else if (has_dollar_at && quoted_dollar_at == 0 && ifs_chars && quoted == 0 && (word->flags & W_NOSPLIT2))
10899 {
10900 tword = alloc_word_desc ();
10901 /* Only split and rejoin if we have to */
10902 if (*ifs_chars && *ifs_chars != ' ')
10903 {
10904 /* list_string dequotes CTLESCs in the string it's passed, so we
10905 need it to get the space separation right if space isn't the
10906 first character in IFS (but is present) and to remove the
10907 quoting we added back in param_expand(). */
10908 list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
10909 /* This isn't exactly right in the case where we're expanding
10910 the RHS of an expansion like ${var-$@} where IFS=: (for
10911 example). The W_NOSPLIT2 means we do the separation with :;
10912 the list_string removes the quotes and breaks the string into
10913 a list, and the string_list rejoins it on spaces. When we
10914 return, we expect to be able to split the results, but the
10915 space separation means the right split doesn't happen. */
10916 tword->word = string_list (list);
10917 }
10918 else
10919 tword->word = istring;
10920 if (had_quoted_null && QUOTED_NULL (istring))
10921 tword->flags |= W_HASQUOTEDNULL; /* XXX */
10922 if (tword->word != istring)
10923 free (istring);
10924 istring = 0; /* avoid later free() */
10925 goto set_word_flags;
10926 }
10927 else if (has_dollar_at && ifs_chars)
10928 list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
10929 else
10930 {
10931 tword = alloc_word_desc ();
10932 if (expanded_something && *expanded_something == 0 && has_quoted_ifs)
10933 tword->word = remove_quoted_ifs (istring);
10934 else
10935 tword->word = istring;
10936 if (had_quoted_null && QUOTED_NULL (istring)) /* should check for more than one */
10937 tword->flags |= W_HASQUOTEDNULL; /* XXX */
10938 else if (had_quoted_null)
10939 tword->flags |= W_SAWQUOTEDNULL; /* XXX */
10940 if (tword->word != istring)
10941 free (istring);
10942 istring = 0; /* avoid later free() */
10943 set_word_flags:
10944 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
10945 tword->flags |= W_QUOTED;
10946 if (word->flags & W_ASSIGNMENT)
10947 tword->flags |= W_ASSIGNMENT;
10948 if (word->flags & W_COMPASSIGN)
10949 tword->flags |= W_COMPASSIGN;
10950 if (word->flags & W_NOGLOB)
10951 tword->flags |= W_NOGLOB;
10952 if (word->flags & W_NOBRACE)
10953 tword->flags |= W_NOBRACE;
10954 list = make_word_list (tword, (WORD_LIST *)NULL);
10955 }
10956 }
10957
10958 free (istring);
10959 return (list);
10960 }
10961
10962 /* **************************************************************** */
10963 /* */
10964 /* Functions for Quote Removal */
10965 /* */
10966 /* **************************************************************** */
10967
10968 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
10969 backslash quoting rules for within double quotes or a here document. */
10970 char *
10971 string_quote_removal (string, quoted)
10972 char *string;
10973 int quoted;
10974 {
10975 size_t slen;
10976 char *r, *result_string, *temp, *send;
10977 int sindex, tindex, dquote;
10978 unsigned char c;
10979 DECLARE_MBSTATE;
10980
10981 /* The result can be no longer than the original string. */
10982 slen = strlen (string);
10983 send = string + slen;
10984
10985 r = result_string = (char *)xmalloc (slen + 1);
10986
10987 for (dquote = sindex = 0; c = string[sindex];)
10988 {
10989 switch (c)
10990 {
10991 case '\\':
10992 c = string[++sindex];
10993 if (c == 0)
10994 {
10995 *r++ = '\\';
10996 break;
10997 }
10998 if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
10999 *r++ = '\\';
11000 /* FALLTHROUGH */
11001
11002 default:
11003 SCOPY_CHAR_M (r, string, send, sindex);
11004 break;
11005
11006 case '\'':
11007 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
11008 {
11009 *r++ = c;
11010 sindex++;
11011 break;
11012 }
11013 tindex = sindex + 1;
11014 temp = string_extract_single_quoted (string, &tindex);
11015 if (temp)
11016 {
11017 strcpy (r, temp);
11018 r += strlen (r);
11019 free (temp);
11020 }
11021 sindex = tindex;
11022 break;
11023
11024 case '"':
11025 dquote = 1 - dquote;
11026 sindex++;
11027 break;
11028 }
11029 }
11030 *r = '\0';
11031 return (result_string);
11032 }
11033
11034 #if 0
11035 /* UNUSED */
11036 /* Perform quote removal on word WORD. This allocates and returns a new
11037 WORD_DESC *. */
11038 WORD_DESC *
11039 word_quote_removal (word, quoted)
11040 WORD_DESC *word;
11041 int quoted;
11042 {
11043 WORD_DESC *w;
11044 char *t;
11045
11046 t = string_quote_removal (word->word, quoted);
11047 w = alloc_word_desc ();
11048 w->word = t ? t : savestring ("");
11049 return (w);
11050 }
11051
11052 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
11053 the members of the list are treated as if they are surrounded by
11054 double quotes. Return a new list, or NULL if LIST is NULL. */
11055 WORD_LIST *
11056 word_list_quote_removal (list, quoted)
11057 WORD_LIST *list;
11058 int quoted;
11059 {
11060 WORD_LIST *result, *t, *tresult, *e;
11061
11062 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
11063 {
11064 tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
11065 #if 0
11066 result = (WORD_LIST *) list_append (result, tresult);
11067 #else
11068 if (result == 0)
11069 result = e = tresult;
11070 else
11071 {
11072 e->next = tresult;
11073 while (e->next)
11074 e = e->next;
11075 }
11076 #endif
11077 }
11078 return (result);
11079 }
11080 #endif
11081
11082 /*******************************************
11083 * *
11084 * Functions to perform word splitting *
11085 * *
11086 *******************************************/
11087
11088 void
11089 setifs (v)
11090 SHELL_VAR *v;
11091 {
11092 char *t;
11093 unsigned char uc;
11094
11095 ifs_var = v;
11096 ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n";
11097
11098 ifs_is_set = ifs_var != 0;
11099 ifs_is_null = ifs_is_set && (*ifs_value == 0);
11100
11101 /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
11102 handle multibyte chars in IFS */
11103 memset (ifs_cmap, '\0', sizeof (ifs_cmap));
11104 for (t = ifs_value ; t && *t; t++)
11105 {
11106 uc = *t;
11107 ifs_cmap[uc] = 1;
11108 }
11109
11110 #if defined (HANDLE_MULTIBYTE)
11111 if (ifs_value == 0)
11112 {
11113 ifs_firstc[0] = '\0'; /* XXX - ? */
11114 ifs_firstc_len = 1;
11115 }
11116 else
11117 {
11118 if (locale_utf8locale && UTF8_SINGLEBYTE (*ifs_value))
11119 ifs_firstc_len = (*ifs_value != 0) ? 1 : 0;
11120 else
11121 {
11122 size_t ifs_len;
11123 ifs_len = strnlen (ifs_value, MB_CUR_MAX);
11124 ifs_firstc_len = MBLEN (ifs_value, ifs_len);
11125 }
11126 if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len))
11127 {
11128 ifs_firstc[0] = ifs_value[0];
11129 ifs_firstc[1] = '\0';
11130 ifs_firstc_len = 1;
11131 }
11132 else
11133 memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
11134 }
11135 #else
11136 ifs_firstc = ifs_value ? *ifs_value : 0;
11137 #endif
11138 }
11139
11140 char *
11141 getifs ()
11142 {
11143 return ifs_value;
11144 }
11145
11146 /* This splits a single word into a WORD LIST on $IFS, but only if the word
11147 is not quoted. list_string () performs quote removal for us, even if we
11148 don't do any splitting. */
11149 WORD_LIST *
11150 word_split (w, ifs_chars)
11151 WORD_DESC *w;
11152 char *ifs_chars;
11153 {
11154 WORD_LIST *result;
11155
11156 if (w)
11157 {
11158 char *xifs;
11159
11160 xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
11161 result = list_string (w->word, xifs, w->flags & W_QUOTED);
11162 }
11163 else
11164 result = (WORD_LIST *)NULL;
11165
11166 return (result);
11167 }
11168
11169 /* Perform word splitting on LIST and return the RESULT. It is possible
11170 to return (WORD_LIST *)NULL. */
11171 static WORD_LIST *
11172 word_list_split (list)
11173 WORD_LIST *list;
11174 {
11175 WORD_LIST *result, *t, *tresult, *e;
11176 WORD_DESC *w;
11177
11178 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
11179 {
11180 tresult = word_split (t->word, ifs_value);
11181 /* POSIX 2.6: "If the complete expansion appropriate for a word results
11182 in an empty field, that empty field shall be deleted from the list
11183 of fields that form the completely expanded command, unless the
11184 original word contained single-quote or double-quote characters."
11185 This is where we handle these words that contain quoted null strings
11186 and other characters that expand to nothing after word splitting. */
11187 if (tresult == 0 && t->word && (t->word->flags & W_SAWQUOTEDNULL)) /* XXX */
11188 {
11189 w = alloc_word_desc ();
11190 w->word = (char *)xmalloc (1);
11191 w->word[0] = '\0';
11192 tresult = make_word_list (w, (WORD_LIST *)NULL);
11193 }
11194 if (result == 0)
11195 result = e = tresult;
11196 else
11197 {
11198 e->next = tresult;
11199 while (e->next)
11200 e = e->next;
11201 }
11202 }
11203 return (result);
11204 }
11205
11206 /**************************************************
11207 * *
11208 * Functions to expand an entire WORD_LIST *
11209 * *
11210 **************************************************/
11211
11212 /* Do any word-expansion-specific cleanup and jump to top_level */
11213 static void
11214 exp_jump_to_top_level (v)
11215 int v;
11216 {
11217 set_pipestatus_from_exit (last_command_exit_value);
11218
11219 /* Cleanup code goes here. */
11220 expand_no_split_dollar_star = 0; /* XXX */
11221 if (expanding_redir)
11222 undo_partial_redirects ();
11223 expanding_redir = 0;
11224 assigning_in_environment = 0;
11225
11226 if (parse_and_execute_level == 0)
11227 top_level_cleanup (); /* from sig.c */
11228
11229 jump_to_top_level (v);
11230 }
11231
11232 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
11233 ELIST, and set ELIST to the new list. */
11234 #define PREPEND_LIST(nlist, elist) \
11235 do { nlist->next = elist; elist = nlist; } while (0)
11236
11237 /* Separate out any initial variable assignments from TLIST. If set -k has
11238 been executed, remove all assignment statements from TLIST. Initial
11239 variable assignments and other environment assignments are placed
11240 on SUBST_ASSIGN_VARLIST. */
11241 static WORD_LIST *
11242 separate_out_assignments (tlist)
11243 WORD_LIST *tlist;
11244 {
11245 register WORD_LIST *vp, *lp;
11246
11247 if (tlist == 0)
11248 return ((WORD_LIST *)NULL);
11249
11250 if (subst_assign_varlist)
11251 dispose_words (subst_assign_varlist); /* Clean up after previous error */
11252
11253 subst_assign_varlist = (WORD_LIST *)NULL;
11254 vp = lp = tlist;
11255
11256 /* Separate out variable assignments at the start of the command.
11257 Loop invariant: vp->next == lp
11258 Loop postcondition:
11259 lp = list of words left after assignment statements skipped
11260 tlist = original list of words
11261 */
11262 while (lp && (lp->word->flags & W_ASSIGNMENT))
11263 {
11264 vp = lp;
11265 lp = lp->next;
11266 }
11267
11268 /* If lp != tlist, we have some initial assignment statements.
11269 We make SUBST_ASSIGN_VARLIST point to the list of assignment
11270 words and TLIST point to the remaining words. */
11271 if (lp != tlist)
11272 {
11273 subst_assign_varlist = tlist;
11274 /* ASSERT(vp->next == lp); */
11275 vp->next = (WORD_LIST *)NULL; /* terminate variable list */
11276 tlist = lp; /* remainder of word list */
11277 }
11278
11279 /* vp == end of variable list */
11280 /* tlist == remainder of original word list without variable assignments */
11281 if (!tlist)
11282 /* All the words in tlist were assignment statements */
11283 return ((WORD_LIST *)NULL);
11284
11285 /* ASSERT(tlist != NULL); */
11286 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
11287
11288 /* If the -k option is in effect, we need to go through the remaining
11289 words, separate out the assignment words, and place them on
11290 SUBST_ASSIGN_VARLIST. */
11291 if (place_keywords_in_env)
11292 {
11293 WORD_LIST *tp; /* tp == running pointer into tlist */
11294
11295 tp = tlist;
11296 lp = tlist->next;
11297
11298 /* Loop Invariant: tp->next == lp */
11299 /* Loop postcondition: tlist == word list without assignment statements */
11300 while (lp)
11301 {
11302 if (lp->word->flags & W_ASSIGNMENT)
11303 {
11304 /* Found an assignment statement, add this word to end of
11305 subst_assign_varlist (vp). */
11306 if (!subst_assign_varlist)
11307 subst_assign_varlist = vp = lp;
11308 else
11309 {
11310 vp->next = lp;
11311 vp = lp;
11312 }
11313
11314 /* Remove the word pointed to by LP from TLIST. */
11315 tp->next = lp->next;
11316 /* ASSERT(vp == lp); */
11317 lp->next = (WORD_LIST *)NULL;
11318 lp = tp->next;
11319 }
11320 else
11321 {
11322 tp = lp;
11323 lp = lp->next;
11324 }
11325 }
11326 }
11327 return (tlist);
11328 }
11329
11330 #define WEXP_VARASSIGN 0x001
11331 #define WEXP_BRACEEXP 0x002
11332 #define WEXP_TILDEEXP 0x004
11333 #define WEXP_PARAMEXP 0x008
11334 #define WEXP_PATHEXP 0x010
11335
11336 /* All of the expansions, including variable assignments at the start of
11337 the list. */
11338 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
11339
11340 /* All of the expansions except variable assignments at the start of
11341 the list. */
11342 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
11343
11344 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
11345 expansion, command substitution, arithmetic expansion, word splitting, and
11346 quote removal. */
11347 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
11348
11349 /* Take the list of words in LIST and do the various substitutions. Return
11350 a new list of words which is the expanded list, and without things like
11351 variable assignments. */
11352
11353 WORD_LIST *
11354 expand_words (list)
11355 WORD_LIST *list;
11356 {
11357 return (expand_word_list_internal (list, WEXP_ALL));
11358 }
11359
11360 /* Same as expand_words (), but doesn't hack variable or environment
11361 variables. */
11362 WORD_LIST *
11363 expand_words_no_vars (list)
11364 WORD_LIST *list;
11365 {
11366 return (expand_word_list_internal (list, WEXP_NOVARS));
11367 }
11368
11369 WORD_LIST *
11370 expand_words_shellexp (list)
11371 WORD_LIST *list;
11372 {
11373 return (expand_word_list_internal (list, WEXP_SHELLEXP));
11374 }
11375
11376 static WORD_LIST *
11377 glob_expand_word_list (tlist, eflags)
11378 WORD_LIST *tlist;
11379 int eflags;
11380 {
11381 char **glob_array, *temp_string;
11382 register int glob_index;
11383 WORD_LIST *glob_list, *output_list, *disposables, *next;
11384 WORD_DESC *tword;
11385 int x;
11386
11387 output_list = disposables = (WORD_LIST *)NULL;
11388 glob_array = (char **)NULL;
11389 while (tlist)
11390 {
11391 /* For each word, either globbing is attempted or the word is
11392 added to orig_list. If globbing succeeds, the results are
11393 added to orig_list and the word (tlist) is added to the list
11394 of disposable words. If globbing fails and failed glob
11395 expansions are left unchanged (the shell default), the
11396 original word is added to orig_list. If globbing fails and
11397 failed glob expansions are removed, the original word is
11398 added to the list of disposable words. orig_list ends up
11399 in reverse order and requires a call to REVERSE_LIST to
11400 be set right. After all words are examined, the disposable
11401 words are freed. */
11402 next = tlist->next;
11403
11404 /* If the word isn't an assignment and contains an unquoted
11405 pattern matching character, then glob it. */
11406 if ((tlist->word->flags & W_NOGLOB) == 0 &&
11407 unquoted_glob_pattern_p (tlist->word->word))
11408 {
11409 glob_array = shell_glob_filename (tlist->word->word, QGLOB_CTLESC); /* XXX */
11410
11411 /* Handle error cases.
11412 I don't think we should report errors like "No such file
11413 or directory". However, I would like to report errors
11414 like "Read failed". */
11415
11416 if (glob_array == 0 || GLOB_FAILED (glob_array))
11417 {
11418 glob_array = (char **)xmalloc (sizeof (char *));
11419 glob_array[0] = (char *)NULL;
11420 }
11421
11422 /* Dequote the current word in case we have to use it. */
11423 if (glob_array[0] == NULL)
11424 {
11425 temp_string = dequote_string (tlist->word->word);
11426 free (tlist->word->word);
11427 tlist->word->word = temp_string;
11428 }
11429
11430 /* Make the array into a word list. */
11431 glob_list = (WORD_LIST *)NULL;
11432 for (glob_index = 0; glob_array[glob_index]; glob_index++)
11433 {
11434 tword = make_bare_word (glob_array[glob_index]);
11435 glob_list = make_word_list (tword, glob_list);
11436 }
11437
11438 if (glob_list)
11439 {
11440 output_list = (WORD_LIST *)list_append (glob_list, output_list);
11441 PREPEND_LIST (tlist, disposables);
11442 }
11443 else if (fail_glob_expansion != 0)
11444 {
11445 last_command_exit_value = EXECUTION_FAILURE;
11446 report_error (_("no match: %s"), tlist->word->word);
11447 exp_jump_to_top_level (DISCARD);
11448 }
11449 else if (allow_null_glob_expansion == 0)
11450 {
11451 /* Failed glob expressions are left unchanged. */
11452 PREPEND_LIST (tlist, output_list);
11453 }
11454 else
11455 {
11456 /* Failed glob expressions are removed. */
11457 PREPEND_LIST (tlist, disposables);
11458 }
11459 }
11460 else
11461 {
11462 /* Dequote the string. */
11463 temp_string = dequote_string (tlist->word->word);
11464 free (tlist->word->word);
11465 tlist->word->word = temp_string;
11466 PREPEND_LIST (tlist, output_list);
11467 }
11468
11469 strvec_dispose (glob_array);
11470 glob_array = (char **)NULL;
11471
11472 tlist = next;
11473 }
11474
11475 if (disposables)
11476 dispose_words (disposables);
11477
11478 if (output_list)
11479 output_list = REVERSE_LIST (output_list, WORD_LIST *);
11480
11481 return (output_list);
11482 }
11483
11484 #if defined (BRACE_EXPANSION)
11485 static WORD_LIST *
11486 brace_expand_word_list (tlist, eflags)
11487 WORD_LIST *tlist;
11488 int eflags;
11489 {
11490 register char **expansions;
11491 char *temp_string;
11492 WORD_LIST *disposables, *output_list, *next;
11493 WORD_DESC *w;
11494 int eindex;
11495
11496 for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
11497 {
11498 next = tlist->next;
11499
11500 if (tlist->word->flags & W_NOBRACE)
11501 {
11502 /*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/
11503 PREPEND_LIST (tlist, output_list);
11504 continue;
11505 }
11506
11507 if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
11508 {
11509 /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
11510 PREPEND_LIST (tlist, output_list);
11511 continue;
11512 }
11513
11514 /* Only do brace expansion if the word has a brace character. If
11515 not, just add the word list element to BRACES and continue. In
11516 the common case, at least when running shell scripts, this will
11517 degenerate to a bunch of calls to `mbschr', and then what is
11518 basically a reversal of TLIST into BRACES, which is corrected
11519 by a call to REVERSE_LIST () on BRACES when the end of TLIST
11520 is reached. */
11521 if (mbschr (tlist->word->word, LBRACE))
11522 {
11523 expansions = brace_expand (tlist->word->word);
11524
11525 for (eindex = 0; temp_string = expansions[eindex]; eindex++)
11526 {
11527 w = alloc_word_desc ();
11528 w->word = temp_string;
11529
11530 /* If brace expansion didn't change the word, preserve
11531 the flags. We may want to preserve the flags
11532 unconditionally someday -- XXX */
11533 if (STREQ (temp_string, tlist->word->word))
11534 w->flags = tlist->word->flags;
11535 else
11536 w = make_word_flags (w, temp_string);
11537
11538 output_list = make_word_list (w, output_list);
11539 }
11540 free (expansions);
11541
11542 /* Add TLIST to the list of words to be freed after brace
11543 expansion has been performed. */
11544 PREPEND_LIST (tlist, disposables);
11545 }
11546 else
11547 PREPEND_LIST (tlist, output_list);
11548 }
11549
11550 if (disposables)
11551 dispose_words (disposables);
11552
11553 if (output_list)
11554 output_list = REVERSE_LIST (output_list, WORD_LIST *);
11555
11556 return (output_list);
11557 }
11558 #endif
11559
11560 #if defined (ARRAY_VARS)
11561 /* Take WORD, a compound array assignment, and internally run (for example),
11562 'declare -A w', where W is the variable name portion of WORD. OPTION is
11563 the list of options to supply to `declare'. CMD is the declaration command
11564 we are expanding right now; it's unused currently. */
11565 static int
11566 make_internal_declare (word, option, cmd)
11567 char *word;
11568 char *option;
11569 char *cmd;
11570 {
11571 int t, r;
11572 WORD_LIST *wl;
11573 WORD_DESC *w;
11574
11575 w = make_word (word);
11576
11577 t = assignment (w->word, 0);
11578 if (w->word[t] == '=')
11579 {
11580 w->word[t] = '\0';
11581 if (w->word[t - 1] == '+') /* cut off any append op */
11582 w->word[t - 1] = '\0';
11583 }
11584
11585 wl = make_word_list (w, (WORD_LIST *)NULL);
11586 wl = make_word_list (make_word (option), wl);
11587
11588 r = declare_builtin (wl);
11589
11590 dispose_words (wl);
11591 return r;
11592 }
11593
11594 /* Expand VALUE in NAME[+]=( VALUE ) to a list of words. FLAGS is 1 if NAME
11595 is an associative array.
11596
11597 If we are processing an indexed array, expand_compound_array_assignment
11598 will expand all the individual words and quote_compound_array_list will
11599 single-quote them. If we are processing an associative array, we use
11600 parse_string_to_word_list to split VALUE into a list of words instead of
11601 faking up a shell variable and calling expand_compound_array_assignment.
11602 expand_and_quote_assoc_word expands and single-quotes each word in VALUE
11603 together so we don't have problems finding the end of the subscript when
11604 quoting it.
11605
11606 Words in VALUE can be individual words, which are expanded and single-quoted,
11607 or words of the form [IND]=VALUE, which end up as explained below, as
11608 ['expanded-ind']='expanded-value'. */
11609
11610 static WORD_LIST *
11611 expand_oneword (value, flags)
11612 char *value;
11613 int flags;
11614 {
11615 WORD_LIST *l, *nl;
11616 char *t;
11617 int kvpair;
11618
11619 if (flags == 0)
11620 {
11621 /* Indexed array */
11622 l = expand_compound_array_assignment ((SHELL_VAR *)NULL, value, flags);
11623 /* Now we quote the results of the expansion above to prevent double
11624 expansion. */
11625 quote_compound_array_list (l, flags);
11626 return l;
11627 }
11628 else
11629 {
11630 /* Associative array */
11631 l = parse_string_to_word_list (value, 1, "array assign");
11632 #if ASSOC_KVPAIR_ASSIGNMENT
11633 kvpair = kvpair_assignment_p (l);
11634 #endif
11635
11636 /* For associative arrays, with their arbitrary subscripts, we have to
11637 expand and quote in one step so we don't have to search for the
11638 closing right bracket more than once. */
11639 for (nl = l; nl; nl = nl->next)
11640 {
11641 #if ASSOC_KVPAIR_ASSIGNMENT
11642 if (kvpair)
11643 /* keys and values undergo the same set of expansions */
11644 t = expand_and_quote_kvpair_word (nl->word->word);
11645 else
11646 #endif
11647 if ((nl->word->flags & W_ASSIGNMENT) == 0)
11648 t = sh_single_quote (nl->word->word ? nl->word->word : "");
11649 else
11650 t = expand_and_quote_assoc_word (nl->word->word, flags);
11651 free (nl->word->word);
11652 nl->word->word = t;
11653 }
11654 return l;
11655 }
11656 }
11657
11658 /* Expand a single compound assignment argument to a declaration builtin.
11659 This word takes the form NAME[+]=( VALUE ). The NAME[+]= is passed through
11660 unchanged. The VALUE is expanded and each word in the result is single-
11661 quoted. Words of the form [key]=value end up as
11662 ['expanded-key']='expanded-value'. Associative arrays have special
11663 handling, see expand_oneword() above. The return value is
11664 NAME[+]=( expanded-and-quoted-VALUE ). */
11665 static void
11666 expand_compound_assignment_word (tlist, flags)
11667 WORD_LIST *tlist;
11668 int flags;
11669 {
11670 WORD_LIST *l;
11671 int wlen, oind, t;
11672 char *value, *temp;
11673
11674 /*itrace("expand_compound_assignment_word: original word = -%s-", tlist->word->word);*/
11675 t = assignment (tlist->word->word, 0);
11676
11677 /* value doesn't have the open and close parens */
11678 oind = 1;
11679 value = extract_array_assignment_list (tlist->word->word + t + 1, &oind);
11680 /* This performs one round of expansion on the index/key and value and
11681 single-quotes each word in the result. */
11682 l = expand_oneword (value, flags);
11683 free (value);
11684
11685 value = string_list (l);
11686 dispose_words (l);
11687
11688 wlen = STRLEN (value);
11689
11690 /* Now, let's rebuild the string */
11691 temp = xmalloc (t + 3 + wlen + 1); /* name[+]=(value) */
11692 memcpy (temp, tlist->word->word, ++t);
11693 temp[t++] = '(';
11694 if (value)
11695 memcpy (temp + t, value, wlen);
11696 t += wlen;
11697 temp[t++] = ')';
11698 temp[t] = '\0';
11699 /*itrace("expand_compound_assignment_word: reconstructed word = -%s-", temp);*/
11700
11701 free (tlist->word->word);
11702 tlist->word->word = temp;
11703
11704 free (value);
11705 }
11706
11707 /* Expand and process an argument to a declaration command. We have already
11708 set flags in TLIST->word->flags depending on the declaration command
11709 (declare, local, etc.) and the options supplied to it (-a, -A, etc.).
11710 TLIST->word->word is of the form NAME[+]=( VALUE ).
11711
11712 This does several things, all using pieces of other functions to get the
11713 evaluation sequence right. It's called for compound array assignments with
11714 the W_ASSIGNMENT flag set (basically, valid identifier names on the lhs).
11715 It parses out which flags need to be set for declare to create the variable
11716 correctly, then calls declare internally (make_internal_declare) to make
11717 sure the variable exists with the correct attributes. Before the variable
11718 is created, it calls expand_compound_assignment_word to expand VALUE to a
11719 list of words, appropriately quoted for further evaluation. This preserves
11720 the semantics of word-expansion-before-calling-builtins. Finally, it calls
11721 do_word_assignment to perform the expansion and assignment with the same
11722 expansion semantics as a standalone assignment statement (no word splitting,
11723 etc.) even though the word is single-quoted so all that needs to happen is
11724 quote removal. */
11725 static WORD_LIST *
11726 expand_declaration_argument (tlist, wcmd)
11727 WORD_LIST *tlist, *wcmd;
11728 {
11729 char opts[16], omap[128];
11730 int t, opti, oind, skip, inheriting;
11731 WORD_LIST *l;
11732
11733 inheriting = localvar_inherit;
11734 opti = 0;
11735 if (tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL|W_CHKLOCAL|W_ASSIGNARRAY))
11736 opts[opti++] = '-';
11737
11738 if ((tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL)) == (W_ASSIGNASSOC|W_ASSNGLOBAL))
11739 {
11740 opts[opti++] = 'g';
11741 opts[opti++] = 'A';
11742 }
11743 else if (tlist->word->flags & W_ASSIGNASSOC)
11744 {
11745 opts[opti++] = 'A';
11746 }
11747 else if ((tlist->word->flags & (W_ASSIGNARRAY|W_ASSNGLOBAL)) == (W_ASSIGNARRAY|W_ASSNGLOBAL))
11748 {
11749 opts[opti++] = 'g';
11750 opts[opti++] = 'a';
11751 }
11752 else if (tlist->word->flags & W_ASSIGNARRAY)
11753 {
11754 opts[opti++] = 'a';
11755 }
11756 else if (tlist->word->flags & W_ASSNGLOBAL)
11757 opts[opti++] = 'g';
11758
11759 if (tlist->word->flags & W_CHKLOCAL)
11760 opts[opti++] = 'G';
11761
11762 /* If we have special handling note the integer attribute and others
11763 that transform the value upon assignment. What we do is take all
11764 of the option arguments and scan through them looking for options
11765 that cause such transformations, and add them to the `opts' array. */
11766
11767 memset (omap, '\0', sizeof (omap));
11768 for (l = wcmd->next; l != tlist; l = l->next)
11769 {
11770 if (l->word->word[0] != '-')
11771 break; /* non-option argument */
11772 if (l->word->word[0] == '-' && l->word->word[1] == '-' && l->word->word[2] == 0)
11773 break; /* -- signals end of options */
11774 for (oind = 1; l->word->word[oind]; oind++)
11775 switch (l->word->word[oind])
11776 {
11777 case 'I':
11778 inheriting = 1;
11779 case 'i':
11780 case 'l':
11781 case 'u':
11782 case 'c':
11783 omap[l->word->word[oind]] = 1;
11784 if (opti == 0)
11785 opts[opti++] = '-';
11786 break;
11787 default:
11788 break;
11789 }
11790 }
11791
11792 for (oind = 0; oind < sizeof (omap); oind++)
11793 if (omap[oind])
11794 opts[opti++] = oind;
11795
11796 /* If there are no -a/-A options, but we have a compound assignment,
11797 we have a choice: we can set opts[0]='-', opts[1]='a', since the
11798 default is to create an indexed array, and call
11799 make_internal_declare with that, or we can just skip the -a and let
11800 declare_builtin deal with it. Once we're here, we're better set
11801 up for the latter, since we don't want to deal with looking up
11802 any existing variable here -- better to let declare_builtin do it.
11803 We need the variable created, though, especially if it's local, so
11804 we get the scoping right before we call do_word_assignment.
11805 To ensure that make_local_declare gets called, we add `--' if there
11806 aren't any options. */
11807 if ((tlist->word->flags & (W_ASSIGNASSOC|W_ASSIGNARRAY)) == 0)
11808 {
11809 if (opti == 0)
11810 {
11811 opts[opti++] = '-';
11812 opts[opti++] = '-';
11813 }
11814 }
11815 opts[opti] = '\0';
11816
11817 /* This isn't perfect, but it's a start. Improvements later. We expand
11818 tlist->word->word and single-quote the results to avoid multiple
11819 expansions by, say, do_assignment_internal(). We have to weigh the
11820 cost of reconstructing the compound assignment string with its single
11821 quoting and letting the declare builtin handle it. The single quotes
11822 will prevent any unwanted additional expansion or word splitting. */
11823 expand_compound_assignment_word (tlist, (tlist->word->flags & W_ASSIGNASSOC) ? 1 : 0);
11824
11825 skip = 0;
11826 if (opti > 0)
11827 {
11828 t = make_internal_declare (tlist->word->word, opts, wcmd ? wcmd->word->word : (char *)0);
11829 if (t != EXECUTION_SUCCESS)
11830 {
11831 last_command_exit_value = t;
11832 if (tlist->word->flags & W_FORCELOCAL) /* non-fatal error */
11833 skip = 1;
11834 else
11835 exp_jump_to_top_level (DISCARD);
11836 }
11837 }
11838
11839 if (skip == 0)
11840 {
11841 t = do_word_assignment (tlist->word, 0);
11842 if (t == 0)
11843 {
11844 last_command_exit_value = EXECUTION_FAILURE;
11845 exp_jump_to_top_level (DISCARD);
11846 }
11847 }
11848
11849 /* Now transform the word as ksh93 appears to do and go on */
11850 t = assignment (tlist->word->word, 0);
11851 tlist->word->word[t] = '\0';
11852 if (tlist->word->word[t - 1] == '+')
11853 tlist->word->word[t - 1] = '\0'; /* cut off append op */
11854 tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC|W_ASSIGNARRAY);
11855
11856 return (tlist);
11857 }
11858 #endif /* ARRAY_VARS */
11859
11860 static WORD_LIST *
11861 shell_expand_word_list (tlist, eflags)
11862 WORD_LIST *tlist;
11863 int eflags;
11864 {
11865 WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list, *wcmd;
11866 int expanded_something, has_dollar_at;
11867
11868 /* We do tilde expansion all the time. This is what 1003.2 says. */
11869 wcmd = new_list = (WORD_LIST *)NULL;
11870
11871 for (orig_list = tlist; tlist; tlist = next)
11872 {
11873 if (wcmd == 0 && (tlist->word->flags & W_ASSNBLTIN))
11874 wcmd = tlist;
11875
11876 next = tlist->next;
11877
11878 #if defined (ARRAY_VARS)
11879 /* If this is a compound array assignment to a builtin that accepts
11880 such assignments (e.g., `declare'), take the assignment and perform
11881 it separately, handling the semantics of declarations inside shell
11882 functions. This avoids the double-evaluation of such arguments,
11883 because `declare' does some evaluation of compound assignments on
11884 its own. */
11885 if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
11886 expand_declaration_argument (tlist, wcmd);
11887 #endif
11888
11889 expanded_something = 0;
11890 expanded = expand_word_internal
11891 (tlist->word, 0, 0, &has_dollar_at, &expanded_something);
11892
11893 if (expanded == &expand_word_error || expanded == &expand_word_fatal)
11894 {
11895 /* By convention, each time this error is returned,
11896 tlist->word->word has already been freed. */
11897 tlist->word->word = (char *)NULL;
11898
11899 /* Dispose our copy of the original list. */
11900 dispose_words (orig_list);
11901 /* Dispose the new list we're building. */
11902 dispose_words (new_list);
11903
11904 last_command_exit_value = EXECUTION_FAILURE;
11905 if (expanded == &expand_word_error)
11906 exp_jump_to_top_level (DISCARD);
11907 else
11908 exp_jump_to_top_level (FORCE_EOF);
11909 }
11910
11911 /* Don't split words marked W_NOSPLIT. */
11912 if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
11913 {
11914 temp_list = word_list_split (expanded);
11915 dispose_words (expanded);
11916 }
11917 else
11918 {
11919 /* If no parameter expansion, command substitution, process
11920 substitution, or arithmetic substitution took place, then
11921 do not do word splitting. We still have to remove quoted
11922 null characters from the result. */
11923 word_list_remove_quoted_nulls (expanded);
11924 temp_list = expanded;
11925 }
11926
11927 expanded = REVERSE_LIST (temp_list, WORD_LIST *);
11928 new_list = (WORD_LIST *)list_append (expanded, new_list);
11929 }
11930
11931 if (orig_list)
11932 dispose_words (orig_list);
11933
11934 if (new_list)
11935 new_list = REVERSE_LIST (new_list, WORD_LIST *);
11936
11937 return (new_list);
11938 }
11939
11940 /* The workhorse for expand_words () and expand_words_no_vars ().
11941 First arg is LIST, a WORD_LIST of words.
11942 Second arg EFLAGS is a flags word controlling which expansions are
11943 performed.
11944
11945 This does all of the substitutions: brace expansion, tilde expansion,
11946 parameter expansion, command substitution, arithmetic expansion,
11947 process substitution, word splitting, and pathname expansion, according
11948 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
11949 set, or for which no expansion is done, do not undergo word splitting.
11950 Words with the W_NOGLOB bit set do not undergo pathname expansion; words
11951 with W_NOBRACE set do not undergo brace expansion (see
11952 brace_expand_word_list above). */
11953 static WORD_LIST *
11954 expand_word_list_internal (list, eflags)
11955 WORD_LIST *list;
11956 int eflags;
11957 {
11958 WORD_LIST *new_list, *temp_list;
11959 int tint;
11960 char *savecmd;
11961
11962 tempenv_assign_error = 0;
11963 if (list == 0)
11964 return ((WORD_LIST *)NULL);
11965
11966 garglist = new_list = copy_word_list (list);
11967 if (eflags & WEXP_VARASSIGN)
11968 {
11969 garglist = new_list = separate_out_assignments (new_list);
11970 if (new_list == 0)
11971 {
11972 if (subst_assign_varlist)
11973 {
11974 /* All the words were variable assignments, so they are placed
11975 into the shell's environment. */
11976 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
11977 {
11978 savecmd = this_command_name;
11979 this_command_name = (char *)NULL; /* no arithmetic errors */
11980 tint = do_word_assignment (temp_list->word, 0);
11981 this_command_name = savecmd;
11982 /* Variable assignment errors in non-interactive shells
11983 running in Posix.2 mode cause the shell to exit, unless
11984 they are being run by the `command' builtin. */
11985 if (tint == 0)
11986 {
11987 last_command_exit_value = EXECUTION_FAILURE;
11988 if (interactive_shell == 0 && posixly_correct && executing_command_builtin == 0)
11989 exp_jump_to_top_level (FORCE_EOF);
11990 else
11991 exp_jump_to_top_level (DISCARD);
11992 }
11993 }
11994 dispose_words (subst_assign_varlist);
11995 subst_assign_varlist = (WORD_LIST *)NULL;
11996 }
11997 return ((WORD_LIST *)NULL);
11998 }
11999 }
12000
12001 /* Begin expanding the words that remain. The expansions take place on
12002 things that aren't really variable assignments. */
12003
12004 #if defined (BRACE_EXPANSION)
12005 /* Do brace expansion on this word if there are any brace characters
12006 in the string. */
12007 if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
12008 new_list = brace_expand_word_list (new_list, eflags);
12009 #endif /* BRACE_EXPANSION */
12010
12011 /* Perform the `normal' shell expansions: tilde expansion, parameter and
12012 variable substitution, command substitution, arithmetic expansion,
12013 and word splitting. */
12014 new_list = shell_expand_word_list (new_list, eflags);
12015
12016 /* Okay, we're almost done. Now let's just do some filename
12017 globbing. */
12018 if (new_list)
12019 {
12020 if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
12021 /* Glob expand the word list unless globbing has been disabled. */
12022 new_list = glob_expand_word_list (new_list, eflags);
12023 else
12024 /* Dequote the words, because we're not performing globbing. */
12025 new_list = dequote_list (new_list);
12026 }
12027
12028 if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
12029 {
12030 sh_wassign_func_t *assign_func;
12031 int is_special_builtin, is_builtin_or_func;
12032
12033 /* If the remainder of the words expand to nothing, Posix.2 requires
12034 that the variable and environment assignments affect the shell's
12035 environment. */
12036 assign_func = new_list ? assign_in_env : do_word_assignment;
12037 tempenv_assign_error = 0;
12038
12039 is_builtin_or_func = (new_list && new_list->word && (find_shell_builtin (new_list->word->word) || find_function (new_list->word->word)));
12040 /* Posix says that special builtins exit if a variable assignment error
12041 occurs in an assignment preceding it. */
12042 is_special_builtin = (posixly_correct && new_list && new_list->word && find_special_builtin (new_list->word->word));
12043
12044 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
12045 {
12046 savecmd = this_command_name;
12047 this_command_name = (char *)NULL;
12048 assigning_in_environment = (assign_func == assign_in_env);
12049 tint = (*assign_func) (temp_list->word, is_builtin_or_func);
12050 assigning_in_environment = 0;
12051 this_command_name = savecmd;
12052 /* Variable assignment errors in non-interactive shells running
12053 in Posix.2 mode cause the shell to exit. */
12054 if (tint == 0)
12055 {
12056 if (assign_func == do_word_assignment)
12057 {
12058 last_command_exit_value = EXECUTION_FAILURE;
12059 if (interactive_shell == 0 && posixly_correct)
12060 exp_jump_to_top_level (FORCE_EOF);
12061 else
12062 exp_jump_to_top_level (DISCARD);
12063 }
12064 else if (interactive_shell == 0 && is_special_builtin)
12065 {
12066 last_command_exit_value = EXECUTION_FAILURE;
12067 exp_jump_to_top_level (FORCE_EOF);
12068 }
12069 else
12070 tempenv_assign_error++;
12071 }
12072 }
12073
12074 dispose_words (subst_assign_varlist);
12075 subst_assign_varlist = (WORD_LIST *)NULL;
12076 }
12077
12078 return (new_list);
12079 }