]> git.ipfire.org Git - thirdparty/bash.git/blob - subst.c
Bash-4.4 patch 15
[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-2016 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
135 #define CHECK_STRING_OVERRUN(oind, ind, len, ch) \
136 if (ind >= len) \
137 { \
138 oind = len; \
139 ch = 0; \
140 break; \
141 } \
142 else \
143
144 /* An expansion function that takes a string and a quoted flag and returns
145 a WORD_LIST *. Used as the type of the third argument to
146 expand_string_if_necessary(). */
147 typedef WORD_LIST *EXPFUNC __P((char *, int));
148
149 /* Process ID of the last command executed within command substitution. */
150 pid_t last_command_subst_pid = NO_PID;
151 pid_t current_command_subst_pid = NO_PID;
152
153 /* Variables used to keep track of the characters in IFS. */
154 SHELL_VAR *ifs_var;
155 char *ifs_value;
156 unsigned char ifs_cmap[UCHAR_MAX + 1];
157 int ifs_is_set, ifs_is_null;
158
159 #if defined (HANDLE_MULTIBYTE)
160 unsigned char ifs_firstc[MB_LEN_MAX];
161 size_t ifs_firstc_len;
162 #else
163 unsigned char ifs_firstc;
164 #endif
165
166 /* If non-zero, command substitution inherits the value of errexit option */
167 int inherit_errexit = 0;
168
169 /* Sentinel to tell when we are performing variable assignments preceding a
170 command name and putting them into the environment. Used to make sure
171 we use the temporary environment when looking up variable values. */
172 int assigning_in_environment;
173
174 /* Used to hold a list of variable assignments preceding a command. Global
175 so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
176 SIGCHLD trap and so it can be saved and restored by the trap handlers. */
177 WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
178
179 /* Tell the expansion functions to not longjmp back to top_level on fatal
180 errors. Enabled when doing completion and prompt string expansion. */
181 int no_longjmp_on_fatal_error = 0;
182
183 /* Extern functions and variables from different files. */
184 extern int last_command_exit_value, last_command_exit_signal;
185 extern int subshell_environment, running_in_background;
186 extern int subshell_level, parse_and_execute_level, sourcelevel;
187 extern int eof_encountered, line_number;
188 extern int return_catch_flag, return_catch_value;
189 extern pid_t dollar_dollar_pid;
190 extern int posixly_correct;
191 extern char *this_command_name;
192 extern struct fd_bitmap *current_fds_to_close;
193 extern int wordexp_only;
194 extern int expanding_redir;
195 extern int tempenv_assign_error;
196 extern int builtin_ignoring_errexit;
197
198 #if defined (JOB_CONTROL) && defined (PROCESS_SUBSTITUTION)
199 extern PROCESS *last_procsub_child;
200 #endif
201
202 #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
203 extern wchar_t *wcsdup __P((const wchar_t *));
204 #endif
205
206 /* Non-zero means to allow unmatched globbed filenames to expand to
207 a null file. */
208 int allow_null_glob_expansion;
209
210 /* Non-zero means to throw an error when globbing fails to match anything. */
211 int fail_glob_expansion;
212
213 #if 0
214 /* Variables to keep track of which words in an expanded word list (the
215 output of expand_word_list_internal) are the result of globbing
216 expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
217 (CURRENTLY UNUSED). */
218 char *glob_argv_flags;
219 static int glob_argv_flags_size;
220 #endif
221
222 static WORD_LIST *cached_quoted_dollar_at = 0;
223
224 static WORD_LIST expand_word_error, expand_word_fatal;
225 static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
226 static char expand_param_error, expand_param_fatal;
227 static char extract_string_error, extract_string_fatal;
228
229 /* Set by expand_word_unsplit; used to inhibit splitting and re-joining
230 $* on $IFS, primarily when doing assignment statements. */
231 static int expand_no_split_dollar_star = 0;
232
233 /* A WORD_LIST of words to be expanded by expand_word_list_internal,
234 without any leading variable assignments. */
235 static WORD_LIST *garglist = (WORD_LIST *)NULL;
236
237 static char *quoted_substring __P((char *, int, int));
238 static int quoted_strlen __P((char *));
239 static char *quoted_strchr __P((char *, int, int));
240
241 static char *expand_string_if_necessary __P((char *, int, EXPFUNC *));
242 static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *));
243 static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
244 static WORD_LIST *expand_string_internal __P((char *, int));
245 static WORD_LIST *expand_string_leave_quoted __P((char *, int));
246 static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *));
247
248 static WORD_LIST *list_quote_escapes __P((WORD_LIST *));
249 static WORD_LIST *list_dequote_escapes __P((WORD_LIST *));
250
251 static char *make_quoted_char __P((int));
252 static WORD_LIST *quote_list __P((WORD_LIST *));
253
254 static int unquoted_substring __P((char *, char *));
255 static int unquoted_member __P((int, char *));
256
257 #if defined (ARRAY_VARS)
258 static SHELL_VAR *do_compound_assignment __P((char *, char *, int));
259 #endif
260 static int do_assignment_internal __P((const WORD_DESC *, int));
261
262 static char *string_extract_verbatim __P((char *, size_t, int *, char *, int));
263 static char *string_extract __P((char *, int *, char *, int));
264 static char *string_extract_double_quoted __P((char *, int *, int));
265 static inline char *string_extract_single_quoted __P((char *, int *));
266 static inline int skip_single_quoted __P((const char *, size_t, int, int));
267 static int skip_double_quoted __P((char *, size_t, int, int));
268 static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int));
269 static char *extract_dollar_brace_string __P((char *, int *, int, int));
270 static int skip_matched_pair __P((const char *, int, int, int, int));
271
272 static char *pos_params __P((char *, int, int, int));
273
274 static unsigned char *mb_getcharlens __P((char *, int));
275
276 static char *remove_upattern __P((char *, char *, int));
277 #if defined (HANDLE_MULTIBYTE)
278 static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int));
279 #endif
280 static char *remove_pattern __P((char *, char *, int));
281
282 static int match_upattern __P((char *, char *, int, char **, char **));
283 #if defined (HANDLE_MULTIBYTE)
284 static int match_wpattern __P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
285 #endif
286 static int match_pattern __P((char *, char *, int, char **, char **));
287 static int getpatspec __P((int, char *));
288 static char *getpattern __P((char *, int, int));
289 static char *variable_remove_pattern __P((char *, char *, int, int));
290 static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int));
291 static char *parameter_list_remove_pattern __P((int, char *, int, int));
292 #ifdef ARRAY_VARS
293 static char *array_remove_pattern __P((SHELL_VAR *, char *, int, char *, int));
294 #endif
295 static char *parameter_brace_remove_pattern __P((char *, char *, int, char *, int, int, int));
296
297 static char *string_var_assignment __P((SHELL_VAR *, char *));
298 #if defined (ARRAY_VARS)
299 static char *array_var_assignment __P((SHELL_VAR *, int, int));
300 #endif
301 static char *pos_params_assignment __P((WORD_LIST *, int, int));
302 static char *string_transform __P((int, SHELL_VAR *, char *));
303 static char *list_transform __P((int, SHELL_VAR *, WORD_LIST *, int, int));
304 static char *parameter_list_transform __P((int, int, int));
305 #if defined ARRAY_VARS
306 static char *array_transform __P((int, SHELL_VAR *, char *, int));
307 #endif
308 static char *parameter_brace_transform __P((char *, char *, int, char *, int, int, int));
309
310 static char *process_substitute __P((char *, int));
311
312 static char *read_comsub __P((int, int, int *));
313
314 #ifdef ARRAY_VARS
315 static arrayind_t array_length_reference __P((char *));
316 #endif
317
318 static int valid_brace_expansion_word __P((char *, int));
319 static int chk_atstar __P((char *, int, int *, int *));
320 static int chk_arithsub __P((const char *, int));
321
322 static WORD_DESC *parameter_brace_expand_word __P((char *, int, int, int, arrayind_t *));
323 static char *parameter_brace_find_indir __P((char *, int, int, int));
324 static WORD_DESC *parameter_brace_expand_indir __P((char *, int, int, int *, int *));
325 static WORD_DESC *parameter_brace_expand_rhs __P((char *, char *, int, int, int, int *, int *));
326 static void parameter_brace_expand_error __P((char *, char *));
327
328 static int valid_length_expression __P((char *));
329 static intmax_t parameter_brace_expand_length __P((char *));
330
331 static char *skiparith __P((char *, int));
332 static int verify_substring_values __P((SHELL_VAR *, char *, char *, int, intmax_t *, intmax_t *));
333 static int get_var_and_type __P((char *, char *, arrayind_t, int, int, SHELL_VAR **, char **));
334 static char *mb_substring __P((char *, int, int));
335 static char *parameter_brace_substring __P((char *, char *, int, char *, int, int));
336
337 static int shouldexp_replacement __P((char *));
338
339 static char *pos_params_pat_subst __P((char *, char *, char *, int));
340
341 static char *parameter_brace_patsub __P((char *, char *, int, char *, int, int, int));
342
343 static char *pos_params_casemod __P((char *, char *, int, int));
344 static char *parameter_brace_casemod __P((char *, char *, int, int, char *, int, int));
345
346 static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int, int *, int *));
347 static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
348
349 static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
350
351 static WORD_LIST *word_list_split __P((WORD_LIST *));
352
353 static void exp_jump_to_top_level __P((int));
354
355 static WORD_LIST *separate_out_assignments __P((WORD_LIST *));
356 static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int));
357 #ifdef BRACE_EXPANSION
358 static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int));
359 #endif
360 #if defined (ARRAY_VARS)
361 static int make_internal_declare __P((char *, char *, char *));
362 #endif
363 static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
364 static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
365
366 /* **************************************************************** */
367 /* */
368 /* Utility Functions */
369 /* */
370 /* **************************************************************** */
371
372 #if defined (DEBUG)
373 void
374 dump_word_flags (flags)
375 int flags;
376 {
377 int f;
378
379 f = flags;
380 fprintf (stderr, "%d -> ", f);
381 if (f & W_ARRAYIND)
382 {
383 f &= ~W_ARRAYIND;
384 fprintf (stderr, "W_ARRAYIND%s", f ? "|" : "");
385 }
386 if (f & W_ASSIGNASSOC)
387 {
388 f &= ~W_ASSIGNASSOC;
389 fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : "");
390 }
391 if (f & W_ASSIGNARRAY)
392 {
393 f &= ~W_ASSIGNARRAY;
394 fprintf (stderr, "W_ASSIGNARRAY%s", f ? "|" : "");
395 }
396 if (f & W_HASCTLESC)
397 {
398 f &= ~W_HASCTLESC;
399 fprintf (stderr, "W_HASCTLESC%s", f ? "|" : "");
400 }
401 if (f & W_NOPROCSUB)
402 {
403 f &= ~W_NOPROCSUB;
404 fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : "");
405 }
406 if (f & W_DQUOTE)
407 {
408 f &= ~W_DQUOTE;
409 fprintf (stderr, "W_DQUOTE%s", f ? "|" : "");
410 }
411 if (f & W_HASQUOTEDNULL)
412 {
413 f &= ~W_HASQUOTEDNULL;
414 fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : "");
415 }
416 if (f & W_ASSIGNARG)
417 {
418 f &= ~W_ASSIGNARG;
419 fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : "");
420 }
421 if (f & W_ASSNBLTIN)
422 {
423 f &= ~W_ASSNBLTIN;
424 fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : "");
425 }
426 if (f & W_ASSNGLOBAL)
427 {
428 f &= ~W_ASSNGLOBAL;
429 fprintf (stderr, "W_ASSNGLOBAL%s", f ? "|" : "");
430 }
431 if (f & W_COMPASSIGN)
432 {
433 f &= ~W_COMPASSIGN;
434 fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : "");
435 }
436 if (f & W_NOEXPAND)
437 {
438 f &= ~W_NOEXPAND;
439 fprintf (stderr, "W_NOEXPAND%s", f ? "|" : "");
440 }
441 if (f & W_ITILDE)
442 {
443 f &= ~W_ITILDE;
444 fprintf (stderr, "W_ITILDE%s", f ? "|" : "");
445 }
446 if (f & W_NOTILDE)
447 {
448 f &= ~W_NOTILDE;
449 fprintf (stderr, "W_NOTILDE%s", f ? "|" : "");
450 }
451 if (f & W_ASSIGNRHS)
452 {
453 f &= ~W_ASSIGNRHS;
454 fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : "");
455 }
456 if (f & W_NOCOMSUB)
457 {
458 f &= ~W_NOCOMSUB;
459 fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : "");
460 }
461 if (f & W_DOLLARSTAR)
462 {
463 f &= ~W_DOLLARSTAR;
464 fprintf (stderr, "W_DOLLARSTAR%s", f ? "|" : "");
465 }
466 if (f & W_DOLLARAT)
467 {
468 f &= ~W_DOLLARAT;
469 fprintf (stderr, "W_DOLLARAT%s", f ? "|" : "");
470 }
471 if (f & W_TILDEEXP)
472 {
473 f &= ~W_TILDEEXP;
474 fprintf (stderr, "W_TILDEEXP%s", f ? "|" : "");
475 }
476 if (f & W_NOSPLIT2)
477 {
478 f &= ~W_NOSPLIT2;
479 fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : "");
480 }
481 if (f & W_NOSPLIT)
482 {
483 f &= ~W_NOSPLIT;
484 fprintf (stderr, "W_NOSPLIT%s", f ? "|" : "");
485 }
486 if (f & W_NOBRACE)
487 {
488 f &= ~W_NOBRACE;
489 fprintf (stderr, "W_NOBRACE%s", f ? "|" : "");
490 }
491 if (f & W_NOGLOB)
492 {
493 f &= ~W_NOGLOB;
494 fprintf (stderr, "W_NOGLOB%s", f ? "|" : "");
495 }
496 if (f & W_SPLITSPACE)
497 {
498 f &= ~W_SPLITSPACE;
499 fprintf (stderr, "W_SPLITSPACE%s", f ? "|" : "");
500 }
501 if (f & W_ASSIGNMENT)
502 {
503 f &= ~W_ASSIGNMENT;
504 fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : "");
505 }
506 if (f & W_QUOTED)
507 {
508 f &= ~W_QUOTED;
509 fprintf (stderr, "W_QUOTED%s", f ? "|" : "");
510 }
511 if (f & W_HASDOLLAR)
512 {
513 f &= ~W_HASDOLLAR;
514 fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : "");
515 }
516 if (f & W_COMPLETE)
517 {
518 f &= ~W_COMPLETE;
519 fprintf (stderr, "W_COMPLETE%s", f ? "|" : "");
520 }
521
522 fprintf (stderr, "\n");
523 fflush (stderr);
524 }
525 #endif
526
527 #ifdef INCLUDE_UNUSED
528 static char *
529 quoted_substring (string, start, end)
530 char *string;
531 int start, end;
532 {
533 register int len, l;
534 register char *result, *s, *r;
535
536 len = end - start;
537
538 /* Move to string[start], skipping quoted characters. */
539 for (s = string, l = 0; *s && l < start; )
540 {
541 if (*s == CTLESC)
542 {
543 s++;
544 continue;
545 }
546 l++;
547 if (*s == 0)
548 break;
549 }
550
551 r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */
552
553 /* Copy LEN characters, including quote characters. */
554 s = string + l;
555 for (l = 0; l < len; s++)
556 {
557 if (*s == CTLESC)
558 *r++ = *s++;
559 *r++ = *s;
560 l++;
561 if (*s == 0)
562 break;
563 }
564 *r = '\0';
565 return result;
566 }
567 #endif
568
569 #ifdef INCLUDE_UNUSED
570 /* Return the length of S, skipping over quoted characters */
571 static int
572 quoted_strlen (s)
573 char *s;
574 {
575 register char *p;
576 int i;
577
578 i = 0;
579 for (p = s; *p; p++)
580 {
581 if (*p == CTLESC)
582 {
583 p++;
584 if (*p == 0)
585 return (i + 1);
586 }
587 i++;
588 }
589
590 return i;
591 }
592 #endif
593
594 /* Find the first occurrence of character C in string S, obeying shell
595 quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
596 characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
597 escaped with CTLESC are skipped. */
598 static char *
599 quoted_strchr (s, c, flags)
600 char *s;
601 int c, flags;
602 {
603 register char *p;
604
605 for (p = s; *p; p++)
606 {
607 if (((flags & ST_BACKSL) && *p == '\\')
608 || ((flags & ST_CTLESC) && *p == CTLESC))
609 {
610 p++;
611 if (*p == '\0')
612 return ((char *)NULL);
613 continue;
614 }
615 else if (*p == c)
616 return p;
617 }
618 return ((char *)NULL);
619 }
620
621 #if defined (INCLUDE_UNUSED)
622 /* Return 1 if CHARACTER appears in an unquoted portion of
623 STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
624 static int
625 unquoted_member (character, string)
626 int character;
627 char *string;
628 {
629 size_t slen;
630 int sindex, c;
631 DECLARE_MBSTATE;
632
633 slen = strlen (string);
634 sindex = 0;
635 while (c = string[sindex])
636 {
637 if (c == character)
638 return (1);
639
640 switch (c)
641 {
642 default:
643 ADVANCE_CHAR (string, slen, sindex);
644 break;
645
646 case '\\':
647 sindex++;
648 if (string[sindex])
649 ADVANCE_CHAR (string, slen, sindex);
650 break;
651
652 case '\'':
653 sindex = skip_single_quoted (string, slen, ++sindex, 0);
654 break;
655
656 case '"':
657 sindex = skip_double_quoted (string, slen, ++sindex, 0);
658 break;
659 }
660 }
661 return (0);
662 }
663
664 /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
665 static int
666 unquoted_substring (substr, string)
667 char *substr, *string;
668 {
669 size_t slen;
670 int sindex, c, sublen;
671 DECLARE_MBSTATE;
672
673 if (substr == 0 || *substr == '\0')
674 return (0);
675
676 slen = strlen (string);
677 sublen = strlen (substr);
678 for (sindex = 0; c = string[sindex]; )
679 {
680 if (STREQN (string + sindex, substr, sublen))
681 return (1);
682
683 switch (c)
684 {
685 case '\\':
686 sindex++;
687 if (string[sindex])
688 ADVANCE_CHAR (string, slen, sindex);
689 break;
690
691 case '\'':
692 sindex = skip_single_quoted (string, slen, ++sindex, 0);
693 break;
694
695 case '"':
696 sindex = skip_double_quoted (string, slen, ++sindex, 0);
697 break;
698
699 default:
700 ADVANCE_CHAR (string, slen, sindex);
701 break;
702 }
703 }
704 return (0);
705 }
706 #endif
707
708 /* Most of the substitutions must be done in parallel. In order
709 to avoid using tons of unclear goto's, I have some functions
710 for manipulating malloc'ed strings. They all take INDX, a
711 pointer to an integer which is the offset into the string
712 where manipulation is taking place. They also take SIZE, a
713 pointer to an integer which is the current length of the
714 character array for this string. */
715
716 /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
717 of space allocated to TARGET. SOURCE can be NULL, in which
718 case nothing happens. Gets rid of SOURCE by freeing it.
719 Returns TARGET in case the location has changed. */
720 INLINE char *
721 sub_append_string (source, target, indx, size)
722 char *source, *target;
723 int *indx;
724 size_t *size;
725 {
726 if (source)
727 {
728 int n;
729 size_t srclen;
730
731 srclen = STRLEN (source);
732 if (srclen >= (int)(*size - *indx))
733 {
734 n = srclen + *indx;
735 n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
736 target = (char *)xrealloc (target, (*size = n));
737 }
738
739 FASTCOPY (source, target + *indx, srclen);
740 *indx += srclen;
741 target[*indx] = '\0';
742
743 free (source);
744 }
745 return (target);
746 }
747
748 #if 0
749 /* UNUSED */
750 /* Append the textual representation of NUMBER to TARGET.
751 INDX and SIZE are as in SUB_APPEND_STRING. */
752 char *
753 sub_append_number (number, target, indx, size)
754 intmax_t number;
755 char *target;
756 int *indx;
757 size_t *size;
758 {
759 char *temp;
760
761 temp = itos (number);
762 return (sub_append_string (temp, target, indx, size));
763 }
764 #endif
765
766 /* Extract a substring from STRING, starting at SINDEX and ending with
767 one of the characters in CHARLIST. Don't make the ending character
768 part of the string. Leave SINDEX pointing at the ending character.
769 Understand about backslashes in the string. If (flags & SX_VARNAME)
770 is non-zero, and array variables have been compiled into the shell,
771 everything between a `[' and a corresponding `]' is skipped over.
772 If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
773 update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
774 contain a closing character from CHARLIST. */
775 static char *
776 string_extract (string, sindex, charlist, flags)
777 char *string;
778 int *sindex;
779 char *charlist;
780 int flags;
781 {
782 register int c, i;
783 int found;
784 size_t slen;
785 char *temp;
786 DECLARE_MBSTATE;
787
788 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
789 i = *sindex;
790 found = 0;
791 while (c = string[i])
792 {
793 if (c == '\\')
794 {
795 if (string[i + 1])
796 i++;
797 else
798 break;
799 }
800 #if defined (ARRAY_VARS)
801 else if ((flags & SX_VARNAME) && c == '[')
802 {
803 int ni;
804 /* If this is an array subscript, skip over it and continue. */
805 ni = skipsubscript (string, i, 0);
806 if (string[ni] == ']')
807 i = ni;
808 }
809 #endif
810 else if (MEMBER (c, charlist))
811 {
812 found = 1;
813 break;
814 }
815
816 ADVANCE_CHAR (string, slen, i);
817 }
818
819 /* If we had to have a matching delimiter and didn't find one, return an
820 error and let the caller deal with it. */
821 if ((flags & SX_REQMATCH) && found == 0)
822 {
823 *sindex = i;
824 return (&extract_string_error);
825 }
826
827 temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
828 *sindex = i;
829
830 return (temp);
831 }
832
833 /* Extract the contents of STRING as if it is enclosed in double quotes.
834 SINDEX, when passed in, is the offset of the character immediately
835 following the opening double quote; on exit, SINDEX is left pointing after
836 the closing double quote. If STRIPDQ is non-zero, unquoted double
837 quotes are stripped and the string is terminated by a null byte.
838 Backslashes between the embedded double quotes are processed. If STRIPDQ
839 is zero, an unquoted `"' terminates the string. */
840 static char *
841 string_extract_double_quoted (string, sindex, flags)
842 char *string;
843 int *sindex, flags;
844 {
845 size_t slen;
846 char *send;
847 int j, i, t;
848 unsigned char c;
849 char *temp, *ret; /* The new string we return. */
850 int pass_next, backquote, si; /* State variables for the machine. */
851 int dquote;
852 int stripdq;
853 DECLARE_MBSTATE;
854
855 slen = strlen (string + *sindex) + *sindex;
856 send = string + slen;
857
858 stripdq = (flags & SX_STRIPDQ);
859
860 pass_next = backquote = dquote = 0;
861 temp = (char *)xmalloc (1 + slen - *sindex);
862
863 j = 0;
864 i = *sindex;
865 while (c = string[i])
866 {
867 /* Process a character that was quoted by a backslash. */
868 if (pass_next)
869 {
870 /* XXX - take another look at this in light of Interp 221 */
871 /* Posix.2 sez:
872
873 ``The backslash shall retain its special meaning as an escape
874 character only when followed by one of the characters:
875 $ ` " \ <newline>''.
876
877 If STRIPDQ is zero, we handle the double quotes here and let
878 expand_word_internal handle the rest. If STRIPDQ is non-zero,
879 we have already been through one round of backslash stripping,
880 and want to strip these backslashes only if DQUOTE is non-zero,
881 indicating that we are inside an embedded double-quoted string. */
882
883 /* If we are in an embedded quoted string, then don't strip
884 backslashes before characters for which the backslash
885 retains its special meaning, but remove backslashes in
886 front of other characters. If we are not in an
887 embedded quoted string, don't strip backslashes at all.
888 This mess is necessary because the string was already
889 surrounded by double quotes (and sh has some really weird
890 quoting rules).
891 The returned string will be run through expansion as if
892 it were double-quoted. */
893 if ((stripdq == 0 && c != '"') ||
894 (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
895 temp[j++] = '\\';
896 pass_next = 0;
897
898 add_one_character:
899 COPY_CHAR_I (temp, j, string, send, i);
900 continue;
901 }
902
903 /* A backslash protects the next character. The code just above
904 handles preserving the backslash in front of any character but
905 a double quote. */
906 if (c == '\\')
907 {
908 pass_next++;
909 i++;
910 continue;
911 }
912
913 /* Inside backquotes, ``the portion of the quoted string from the
914 initial backquote and the characters up to the next backquote
915 that is not preceded by a backslash, having escape characters
916 removed, defines that command''. */
917 if (backquote)
918 {
919 if (c == '`')
920 backquote = 0;
921 temp[j++] = c;
922 i++;
923 continue;
924 }
925
926 if (c == '`')
927 {
928 temp[j++] = c;
929 backquote++;
930 i++;
931 continue;
932 }
933
934 /* Pass everything between `$(' and the matching `)' or a quoted
935 ${ ... } pair through according to the Posix.2 specification. */
936 if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
937 {
938 int free_ret = 1;
939
940 si = i + 2;
941 if (string[i + 1] == LPAREN)
942 ret = extract_command_subst (string, &si, (flags & SX_COMPLETE));
943 else
944 ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, 0);
945
946 temp[j++] = '$';
947 temp[j++] = string[i + 1];
948
949 /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
950 is set. */
951 if (ret == 0 && no_longjmp_on_fatal_error)
952 {
953 free_ret = 0;
954 ret = string + i + 2;
955 }
956
957 for (t = 0; ret[t]; t++, j++)
958 temp[j] = ret[t];
959 temp[j] = string[si];
960
961 if (string[si])
962 {
963 j++;
964 i = si + 1;
965 }
966 else
967 i = si;
968
969 if (free_ret)
970 free (ret);
971 continue;
972 }
973
974 /* Add any character but a double quote to the quoted string we're
975 accumulating. */
976 if (c != '"')
977 goto add_one_character;
978
979 /* c == '"' */
980 if (stripdq)
981 {
982 dquote ^= 1;
983 i++;
984 continue;
985 }
986
987 break;
988 }
989 temp[j] = '\0';
990
991 /* Point to after the closing quote. */
992 if (c)
993 i++;
994 *sindex = i;
995
996 return (temp);
997 }
998
999 /* This should really be another option to string_extract_double_quoted. */
1000 static int
1001 skip_double_quoted (string, slen, sind, flags)
1002 char *string;
1003 size_t slen;
1004 int sind;
1005 int flags;
1006 {
1007 int c, i;
1008 char *ret;
1009 int pass_next, backquote, si;
1010 DECLARE_MBSTATE;
1011
1012 pass_next = backquote = 0;
1013 i = sind;
1014 while (c = string[i])
1015 {
1016 if (pass_next)
1017 {
1018 pass_next = 0;
1019 ADVANCE_CHAR (string, slen, i);
1020 continue;
1021 }
1022 else if (c == '\\')
1023 {
1024 pass_next++;
1025 i++;
1026 continue;
1027 }
1028 else if (backquote)
1029 {
1030 if (c == '`')
1031 backquote = 0;
1032 ADVANCE_CHAR (string, slen, i);
1033 continue;
1034 }
1035 else if (c == '`')
1036 {
1037 backquote++;
1038 i++;
1039 continue;
1040 }
1041 else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
1042 {
1043 si = i + 2;
1044 if (string[i + 1] == LPAREN)
1045 ret = extract_command_subst (string, &si, SX_NOALLOC|(flags&SX_COMPLETE));
1046 else
1047 ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, SX_NOALLOC);
1048
1049 /* These can consume the entire string if they are unterminated */
1050 CHECK_STRING_OVERRUN (i, si, slen, c);
1051
1052 i = si + 1;
1053 continue;
1054 }
1055 else if (c != '"')
1056 {
1057 ADVANCE_CHAR (string, slen, i);
1058 continue;
1059 }
1060 else
1061 break;
1062 }
1063
1064 if (c)
1065 i++;
1066
1067 return (i);
1068 }
1069
1070 /* Extract the contents of STRING as if it is enclosed in single quotes.
1071 SINDEX, when passed in, is the offset of the character immediately
1072 following the opening single quote; on exit, SINDEX is left pointing after
1073 the closing single quote. */
1074 static inline char *
1075 string_extract_single_quoted (string, sindex)
1076 char *string;
1077 int *sindex;
1078 {
1079 register int i;
1080 size_t slen;
1081 char *t;
1082 DECLARE_MBSTATE;
1083
1084 /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
1085 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
1086 i = *sindex;
1087 while (string[i] && string[i] != '\'')
1088 ADVANCE_CHAR (string, slen, i);
1089
1090 t = substring (string, *sindex, i);
1091
1092 if (string[i])
1093 i++;
1094 *sindex = i;
1095
1096 return (t);
1097 }
1098
1099 /* Skip over a single-quoted string. We overload the SX_COMPLETE flag to mean
1100 that we are splitting out words for completion and have encountered a $'...'
1101 string, which allows backslash-escaped single quotes. */
1102 static inline int
1103 skip_single_quoted (string, slen, sind, flags)
1104 const char *string;
1105 size_t slen;
1106 int sind;
1107 int flags;
1108 {
1109 register int c;
1110 DECLARE_MBSTATE;
1111
1112 c = sind;
1113 while (string[c] && string[c] != '\'')
1114 {
1115 if ((flags & SX_COMPLETE) && string[c] == '\\' && string[c+1] == '\'' && string[c+2])
1116 ADVANCE_CHAR (string, slen, c);
1117 ADVANCE_CHAR (string, slen, c);
1118 }
1119
1120 if (string[c])
1121 c++;
1122 return c;
1123 }
1124
1125 /* Just like string_extract, but doesn't hack backslashes or any of
1126 that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
1127 static char *
1128 string_extract_verbatim (string, slen, sindex, charlist, flags)
1129 char *string;
1130 size_t slen;
1131 int *sindex;
1132 char *charlist;
1133 int flags;
1134 {
1135 register int i;
1136 #if defined (HANDLE_MULTIBYTE)
1137 size_t clen;
1138 wchar_t *wcharlist;
1139 #endif
1140 int c;
1141 char *temp;
1142 DECLARE_MBSTATE;
1143
1144 if ((flags & SX_NOCTLESC) && charlist[0] == '\'' && charlist[1] == '\0')
1145 {
1146 temp = string_extract_single_quoted (string, sindex);
1147 --*sindex; /* leave *sindex at separator character */
1148 return temp;
1149 }
1150
1151 i = *sindex;
1152 #if 0
1153 /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need
1154 this only if MB_CUR_MAX > 1. */
1155 slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 1;
1156 #endif
1157 #if defined (HANDLE_MULTIBYTE)
1158 clen = strlen (charlist);
1159 wcharlist = 0;
1160 #endif
1161 while (c = string[i])
1162 {
1163 #if defined (HANDLE_MULTIBYTE)
1164 size_t mblength;
1165 #endif
1166 if ((flags & SX_NOCTLESC) == 0 && c == CTLESC)
1167 {
1168 i += 2;
1169 continue;
1170 }
1171 /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
1172 through, to protect the CTLNULs from later calls to
1173 remove_quoted_nulls. */
1174 else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC && string[i+1] == CTLNUL)
1175 {
1176 i += 2;
1177 continue;
1178 }
1179
1180 #if defined (HANDLE_MULTIBYTE)
1181 mblength = MBLEN (string + i, slen - i);
1182 if (mblength > 1)
1183 {
1184 wchar_t wc;
1185 mblength = mbtowc (&wc, string + i, slen - i);
1186 if (MB_INVALIDCH (mblength))
1187 {
1188 if (MEMBER (c, charlist))
1189 break;
1190 }
1191 else
1192 {
1193 if (wcharlist == 0)
1194 {
1195 size_t len;
1196 len = mbstowcs (wcharlist, charlist, 0);
1197 if (len == -1)
1198 len = 0;
1199 wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1));
1200 mbstowcs (wcharlist, charlist, len + 1);
1201 }
1202
1203 if (wcschr (wcharlist, wc))
1204 break;
1205 }
1206 }
1207 else
1208 #endif
1209 if (MEMBER (c, charlist))
1210 break;
1211
1212 ADVANCE_CHAR (string, slen, i);
1213 }
1214
1215 #if defined (HANDLE_MULTIBYTE)
1216 FREE (wcharlist);
1217 #endif
1218
1219 temp = substring (string, *sindex, i);
1220 *sindex = i;
1221
1222 return (temp);
1223 }
1224
1225 /* Extract the $( construct in STRING, and return a new string.
1226 Start extracting at (SINDEX) as if we had just seen "$(".
1227 Make (SINDEX) get the position of the matching ")". )
1228 XFLAGS is additional flags to pass to other extraction functions. */
1229 char *
1230 extract_command_subst (string, sindex, xflags)
1231 char *string;
1232 int *sindex;
1233 int xflags;
1234 {
1235 char *ret;
1236
1237 if (string[*sindex] == LPAREN || (xflags & SX_COMPLETE))
1238 return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/
1239 else
1240 {
1241 xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
1242 ret = xparse_dolparen (string, string+*sindex, sindex, xflags);
1243 return ret;
1244 }
1245 }
1246
1247 /* Extract the $[ construct in STRING, and return a new string. (])
1248 Start extracting at (SINDEX) as if we had just seen "$[".
1249 Make (SINDEX) get the position of the matching "]". */
1250 char *
1251 extract_arithmetic_subst (string, sindex)
1252 char *string;
1253 int *sindex;
1254 {
1255 return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/
1256 }
1257
1258 #if defined (PROCESS_SUBSTITUTION)
1259 /* Extract the <( or >( construct in STRING, and return a new string.
1260 Start extracting at (SINDEX) as if we had just seen "<(".
1261 Make (SINDEX) get the position of the matching ")". */ /*))*/
1262 char *
1263 extract_process_subst (string, starter, sindex, xflags)
1264 char *string;
1265 char *starter;
1266 int *sindex;
1267 int xflags;
1268 {
1269 #if 0
1270 return (extract_delimited_string (string, sindex, starter, "(", ")", SX_COMMAND));
1271 #else
1272 xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
1273 return (xparse_dolparen (string, string+*sindex, sindex, xflags));
1274 #endif
1275 }
1276 #endif /* PROCESS_SUBSTITUTION */
1277
1278 #if defined (ARRAY_VARS)
1279 /* This can be fooled by unquoted right parens in the passed string. If
1280 each caller verifies that the last character in STRING is a right paren,
1281 we don't even need to call extract_delimited_string. */
1282 char *
1283 extract_array_assignment_list (string, sindex)
1284 char *string;
1285 int *sindex;
1286 {
1287 int slen;
1288 char *ret;
1289
1290 slen = strlen (string); /* ( */
1291 if (string[slen - 1] == ')')
1292 {
1293 ret = substring (string, *sindex, slen - 1);
1294 *sindex = slen - 1;
1295 return ret;
1296 }
1297 return 0;
1298 }
1299 #endif
1300
1301 /* Extract and create a new string from the contents of STRING, a
1302 character string delimited with OPENER and CLOSER. SINDEX is
1303 the address of an int describing the current offset in STRING;
1304 it should point to just after the first OPENER found. On exit,
1305 SINDEX gets the position of the last character of the matching CLOSER.
1306 If OPENER is more than a single character, ALT_OPENER, if non-null,
1307 contains a character string that can also match CLOSER and thus
1308 needs to be skipped. */
1309 static char *
1310 extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
1311 char *string;
1312 int *sindex;
1313 char *opener, *alt_opener, *closer;
1314 int flags;
1315 {
1316 int i, c, si;
1317 size_t slen;
1318 char *t, *result;
1319 int pass_character, nesting_level, in_comment;
1320 int len_closer, len_opener, len_alt_opener;
1321 DECLARE_MBSTATE;
1322
1323 slen = strlen (string + *sindex) + *sindex;
1324 len_opener = STRLEN (opener);
1325 len_alt_opener = STRLEN (alt_opener);
1326 len_closer = STRLEN (closer);
1327
1328 pass_character = in_comment = 0;
1329
1330 nesting_level = 1;
1331 i = *sindex;
1332
1333 while (nesting_level)
1334 {
1335 c = string[i];
1336
1337 /* If a recursive call or a call to ADVANCE_CHAR leaves the index beyond
1338 the end of the string, catch it and cut the loop. */
1339 if (i > slen)
1340 {
1341 i = slen;
1342 c = string[i = slen];
1343 break;
1344 }
1345
1346 if (c == 0)
1347 break;
1348
1349 if (in_comment)
1350 {
1351 if (c == '\n')
1352 in_comment = 0;
1353 ADVANCE_CHAR (string, slen, i);
1354 continue;
1355 }
1356
1357 if (pass_character) /* previous char was backslash */
1358 {
1359 pass_character = 0;
1360 ADVANCE_CHAR (string, slen, i);
1361 continue;
1362 }
1363
1364 /* Not exactly right yet; should handle shell metacharacters and
1365 multibyte characters, too. See COMMENT_BEGIN define in parse.y */
1366 if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1])))
1367 {
1368 in_comment = 1;
1369 ADVANCE_CHAR (string, slen, i);
1370 continue;
1371 }
1372
1373 if (c == CTLESC || c == '\\')
1374 {
1375 pass_character++;
1376 i++;
1377 continue;
1378 }
1379
1380 /* Process a nested command substitution, but only if we're parsing an
1381 arithmetic substitution. */
1382 if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN)
1383 {
1384 si = i + 2;
1385 t = extract_command_subst (string, &si, flags|SX_NOALLOC);
1386 i = si + 1;
1387 continue;
1388 }
1389
1390 /* Process a nested OPENER. */
1391 if (STREQN (string + i, opener, len_opener))
1392 {
1393 si = i + len_opener;
1394 t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC);
1395 i = si + 1;
1396 continue;
1397 }
1398
1399 /* Process a nested ALT_OPENER */
1400 if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
1401 {
1402 si = i + len_alt_opener;
1403 t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC);
1404 i = si + 1;
1405 continue;
1406 }
1407
1408 /* If the current substring terminates the delimited string, decrement
1409 the nesting level. */
1410 if (STREQN (string + i, closer, len_closer))
1411 {
1412 i += len_closer - 1; /* move to last byte of the closer */
1413 nesting_level--;
1414 if (nesting_level == 0)
1415 break;
1416 }
1417
1418 /* Pass old-style command substitution through verbatim. */
1419 if (c == '`')
1420 {
1421 si = i + 1;
1422 t = string_extract (string, &si, "`", flags|SX_NOALLOC);
1423 i = si + 1;
1424 continue;
1425 }
1426
1427 /* Pass single-quoted and double-quoted strings through verbatim. */
1428 if (c == '\'' || c == '"')
1429 {
1430 si = i + 1;
1431 i = (c == '\'') ? skip_single_quoted (string, slen, si, 0)
1432 : skip_double_quoted (string, slen, si, 0);
1433 continue;
1434 }
1435
1436 /* move past this character, which was not special. */
1437 ADVANCE_CHAR (string, slen, i);
1438 }
1439
1440 if (c == 0 && nesting_level)
1441 {
1442 if (no_longjmp_on_fatal_error == 0)
1443 {
1444 last_command_exit_value = EXECUTION_FAILURE;
1445 report_error (_("bad substitution: no closing `%s' in %s"), closer, string);
1446 exp_jump_to_top_level (DISCARD);
1447 }
1448 else
1449 {
1450 *sindex = i;
1451 return (char *)NULL;
1452 }
1453 }
1454
1455 si = i - *sindex - len_closer + 1;
1456 if (flags & SX_NOALLOC)
1457 result = (char *)NULL;
1458 else
1459 {
1460 result = (char *)xmalloc (1 + si);
1461 strncpy (result, string + *sindex, si);
1462 result[si] = '\0';
1463 }
1464 *sindex = i;
1465
1466 return (result);
1467 }
1468
1469 /* Extract a parameter expansion expression within ${ and } from STRING.
1470 Obey the Posix.2 rules for finding the ending `}': count braces while
1471 skipping over enclosed quoted strings and command substitutions.
1472 SINDEX is the address of an int describing the current offset in STRING;
1473 it should point to just after the first `{' found. On exit, SINDEX
1474 gets the position of the matching `}'. QUOTED is non-zero if this
1475 occurs inside double quotes. */
1476 /* XXX -- this is very similar to extract_delimited_string -- XXX */
1477 static char *
1478 extract_dollar_brace_string (string, sindex, quoted, flags)
1479 char *string;
1480 int *sindex, quoted, flags;
1481 {
1482 register int i, c;
1483 size_t slen;
1484 int pass_character, nesting_level, si, dolbrace_state;
1485 char *result, *t;
1486 DECLARE_MBSTATE;
1487
1488 pass_character = 0;
1489 nesting_level = 1;
1490 slen = strlen (string + *sindex) + *sindex;
1491
1492 /* The handling of dolbrace_state needs to agree with the code in parse.y:
1493 parse_matched_pair(). The different initial value is to handle the
1494 case where this function is called to parse the word in
1495 ${param op word} (SX_WORD). */
1496 dolbrace_state = (flags & SX_WORD) ? DOLBRACE_WORD : DOLBRACE_PARAM;
1497 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && (flags & SX_POSIXEXP))
1498 dolbrace_state = DOLBRACE_QUOTE;
1499
1500 i = *sindex;
1501 while (c = string[i])
1502 {
1503 if (pass_character)
1504 {
1505 pass_character = 0;
1506 ADVANCE_CHAR (string, slen, i);
1507 continue;
1508 }
1509
1510 /* CTLESCs and backslashes quote the next character. */
1511 if (c == CTLESC || c == '\\')
1512 {
1513 pass_character++;
1514 i++;
1515 continue;
1516 }
1517
1518 if (string[i] == '$' && string[i+1] == LBRACE)
1519 {
1520 nesting_level++;
1521 i += 2;
1522 continue;
1523 }
1524
1525 if (c == RBRACE)
1526 {
1527 nesting_level--;
1528 if (nesting_level == 0)
1529 break;
1530 i++;
1531 continue;
1532 }
1533
1534 /* Pass the contents of old-style command substitutions through
1535 verbatim. */
1536 if (c == '`')
1537 {
1538 si = i + 1;
1539 t = string_extract (string, &si, "`", flags|SX_NOALLOC);
1540
1541 CHECK_STRING_OVERRUN (i, si, slen, c);
1542
1543 i = si + 1;
1544 continue;
1545 }
1546
1547 /* Pass the contents of new-style command substitutions and
1548 arithmetic substitutions through verbatim. */
1549 if (string[i] == '$' && string[i+1] == LPAREN)
1550 {
1551 si = i + 2;
1552 t = extract_command_subst (string, &si, flags|SX_NOALLOC);
1553 i = si + 1;
1554 continue;
1555 }
1556
1557 /* Pass the contents of double-quoted strings through verbatim. */
1558 if (c == '"')
1559 {
1560 si = i + 1;
1561 i = skip_double_quoted (string, slen, si, 0);
1562 /* skip_XXX_quoted leaves index one past close quote */
1563 continue;
1564 }
1565
1566 if (c == '\'')
1567 {
1568 /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/
1569 if (posixly_correct && shell_compatibility_level > 42 && dolbrace_state != DOLBRACE_QUOTE && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
1570 ADVANCE_CHAR (string, slen, i);
1571 else
1572 {
1573 si = i + 1;
1574 i = skip_single_quoted (string, slen, si, 0);
1575 }
1576
1577 continue;
1578 }
1579
1580 /* move past this character, which was not special. */
1581 ADVANCE_CHAR (string, slen, i);
1582
1583 /* This logic must agree with parse.y:parse_matched_pair, since they
1584 share the same defines. */
1585 if (dolbrace_state == DOLBRACE_PARAM && c == '%' && (i - *sindex) > 1)
1586 dolbrace_state = DOLBRACE_QUOTE;
1587 else if (dolbrace_state == DOLBRACE_PARAM && c == '#' && (i - *sindex) > 1)
1588 dolbrace_state = DOLBRACE_QUOTE;
1589 else if (dolbrace_state == DOLBRACE_PARAM && c == '/' && (i - *sindex) > 1)
1590 dolbrace_state = DOLBRACE_QUOTE2; /* XXX */
1591 else if (dolbrace_state == DOLBRACE_PARAM && c == '^' && (i - *sindex) > 1)
1592 dolbrace_state = DOLBRACE_QUOTE;
1593 else if (dolbrace_state == DOLBRACE_PARAM && c == ',' && (i - *sindex) > 1)
1594 dolbrace_state = DOLBRACE_QUOTE;
1595 /* This is intended to handle all of the [:]op expansions and the substring/
1596 length/pattern removal/pattern substitution expansions. */
1597 else if (dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", c) != 0)
1598 dolbrace_state = DOLBRACE_OP;
1599 else if (dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", c) == 0)
1600 dolbrace_state = DOLBRACE_WORD;
1601 }
1602
1603 if (c == 0 && nesting_level)
1604 {
1605 if (no_longjmp_on_fatal_error == 0)
1606 { /* { */
1607 last_command_exit_value = EXECUTION_FAILURE;
1608 report_error (_("bad substitution: no closing `%s' in %s"), "}", string);
1609 exp_jump_to_top_level (DISCARD);
1610 }
1611 else
1612 {
1613 *sindex = i;
1614 return ((char *)NULL);
1615 }
1616 }
1617
1618 result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
1619 *sindex = i;
1620
1621 return (result);
1622 }
1623
1624 /* Remove backslashes which are quoting backquotes from STRING. Modifies
1625 STRING, and returns a pointer to it. */
1626 char *
1627 de_backslash (string)
1628 char *string;
1629 {
1630 register size_t slen;
1631 register int i, j, prev_i;
1632 DECLARE_MBSTATE;
1633
1634 slen = strlen (string);
1635 i = j = 0;
1636
1637 /* Loop copying string[i] to string[j], i >= j. */
1638 while (i < slen)
1639 {
1640 if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
1641 string[i + 1] == '$'))
1642 i++;
1643 prev_i = i;
1644 ADVANCE_CHAR (string, slen, i);
1645 if (j < prev_i)
1646 do string[j++] = string[prev_i++]; while (prev_i < i);
1647 else
1648 j = i;
1649 }
1650 string[j] = '\0';
1651
1652 return (string);
1653 }
1654
1655 #if 0
1656 /*UNUSED*/
1657 /* Replace instances of \! in a string with !. */
1658 void
1659 unquote_bang (string)
1660 char *string;
1661 {
1662 register int i, j;
1663 register char *temp;
1664
1665 temp = (char *)xmalloc (1 + strlen (string));
1666
1667 for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
1668 {
1669 if (string[i] == '\\' && string[i + 1] == '!')
1670 {
1671 temp[j] = '!';
1672 i++;
1673 }
1674 }
1675 strcpy (string, temp);
1676 free (temp);
1677 }
1678 #endif
1679
1680 #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = oldjmp; return (x); } while (0)
1681
1682 /* This function assumes s[i] == open; returns with s[ret] == close; used to
1683 parse array subscripts. FLAGS & 1 means to not attempt to skip over
1684 matched pairs of quotes or backquotes, or skip word expansions; it is
1685 intended to be used after expansion has been performed and during final
1686 assignment parsing (see arrayfunc.c:assign_compound_array_list()). */
1687 static int
1688 skip_matched_pair (string, start, open, close, flags)
1689 const char *string;
1690 int start, open, close, flags;
1691 {
1692 int i, pass_next, backq, si, c, count, oldjmp;
1693 size_t slen;
1694 char *temp, *ss;
1695 DECLARE_MBSTATE;
1696
1697 slen = strlen (string + start) + start;
1698 oldjmp = no_longjmp_on_fatal_error;
1699 no_longjmp_on_fatal_error = 1;
1700
1701 i = start + 1; /* skip over leading bracket */
1702 count = 1;
1703 pass_next = backq = 0;
1704 ss = (char *)string;
1705 while (c = string[i])
1706 {
1707 if (pass_next)
1708 {
1709 pass_next = 0;
1710 if (c == 0)
1711 CQ_RETURN(i);
1712 ADVANCE_CHAR (string, slen, i);
1713 continue;
1714 }
1715 else if (c == '\\')
1716 {
1717 pass_next = 1;
1718 i++;
1719 continue;
1720 }
1721 else if (backq)
1722 {
1723 if (c == '`')
1724 backq = 0;
1725 ADVANCE_CHAR (string, slen, i);
1726 continue;
1727 }
1728 else if ((flags & 1) == 0 && c == '`')
1729 {
1730 backq = 1;
1731 i++;
1732 continue;
1733 }
1734 else if ((flags & 1) == 0 && c == open)
1735 {
1736 count++;
1737 i++;
1738 continue;
1739 }
1740 else if (c == close)
1741 {
1742 count--;
1743 if (count == 0)
1744 break;
1745 i++;
1746 continue;
1747 }
1748 else if ((flags & 1) == 0 && (c == '\'' || c == '"'))
1749 {
1750 i = (c == '\'') ? skip_single_quoted (ss, slen, ++i, 0)
1751 : skip_double_quoted (ss, slen, ++i, 0);
1752 /* no increment, the skip functions increment past the closing quote. */
1753 }
1754 else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
1755 {
1756 si = i + 2;
1757 if (string[si] == '\0')
1758 CQ_RETURN(si);
1759
1760 if (string[i+1] == LPAREN)
1761 temp = extract_delimited_string (ss, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
1762 else
1763 temp = extract_dollar_brace_string (ss, &si, 0, SX_NOALLOC);
1764
1765 CHECK_STRING_OVERRUN (i, si, slen, c);
1766
1767 i = si;
1768 if (string[i] == '\0') /* don't increment i past EOS in loop */
1769 break;
1770 i++;
1771 continue;
1772 }
1773 else
1774 ADVANCE_CHAR (string, slen, i);
1775 }
1776
1777 CQ_RETURN(i);
1778 }
1779
1780 #if defined (ARRAY_VARS)
1781 int
1782 skipsubscript (string, start, flags)
1783 const char *string;
1784 int start, flags;
1785 {
1786 return (skip_matched_pair (string, start, '[', ']', flags));
1787 }
1788 #endif
1789
1790 /* Skip characters in STRING until we find a character in DELIMS, and return
1791 the index of that character. START is the index into string at which we
1792 begin. This is similar in spirit to strpbrk, but it returns an index into
1793 STRING and takes a starting index. This little piece of code knows quite
1794 a lot of shell syntax. It's very similar to skip_double_quoted and other
1795 functions of that ilk. */
1796 int
1797 skip_to_delim (string, start, delims, flags)
1798 char *string;
1799 int start;
1800 char *delims;
1801 int flags;
1802 {
1803 int i, pass_next, backq, dquote, si, c, oldjmp;
1804 int invert, skipquote, skipcmd, noprocsub, completeflag;
1805 int arithexp, skipcol;
1806 size_t slen;
1807 char *temp, open[3];
1808 DECLARE_MBSTATE;
1809
1810 slen = strlen (string + start) + start;
1811 oldjmp = no_longjmp_on_fatal_error;
1812 if (flags & SD_NOJMP)
1813 no_longjmp_on_fatal_error = 1;
1814 invert = (flags & SD_INVERT);
1815 skipcmd = (flags & SD_NOSKIPCMD) == 0;
1816 noprocsub = (flags & SD_NOPROCSUB);
1817 completeflag = (flags & SD_COMPLETE) ? SX_COMPLETE : 0;
1818
1819 arithexp = (flags & SD_ARITHEXP);
1820 skipcol = 0;
1821
1822 i = start;
1823 pass_next = backq = dquote = 0;
1824 while (c = string[i])
1825 {
1826 /* If this is non-zero, we should not let quote characters be delimiters
1827 and the current character is a single or double quote. We should not
1828 test whether or not it's a delimiter until after we skip single- or
1829 double-quoted strings. */
1830 skipquote = ((flags & SD_NOQUOTEDELIM) && (c == '\'' || c =='"'));
1831 if (pass_next)
1832 {
1833 pass_next = 0;
1834 if (c == 0)
1835 CQ_RETURN(i);
1836 ADVANCE_CHAR (string, slen, i);
1837 continue;
1838 }
1839 else if (c == '\\')
1840 {
1841 pass_next = 1;
1842 i++;
1843 continue;
1844 }
1845 else if (backq)
1846 {
1847 if (c == '`')
1848 backq = 0;
1849 ADVANCE_CHAR (string, slen, i);
1850 continue;
1851 }
1852 else if (c == '`')
1853 {
1854 backq = 1;
1855 i++;
1856 continue;
1857 }
1858 else if (arithexp && skipcol && c == ':')
1859 {
1860 skipcol--;
1861 i++;
1862 continue;
1863 }
1864 else if (arithexp && c == '?')
1865 {
1866 skipcol++;
1867 i++;
1868 continue;
1869 }
1870 else if (skipquote == 0 && invert == 0 && member (c, delims))
1871 break;
1872 /* the usual case is to use skip_xxx_quoted, but we don't skip over double
1873 quoted strings when looking for the history expansion character as a
1874 delimiter. */
1875 /* special case for programmable completion which takes place before
1876 parser converts backslash-escaped single quotes between $'...' to
1877 `regular' single-quoted strings. */
1878 else if (completeflag && i > 0 && string[i-1] == '$' && c == '\'')
1879 i = skip_single_quoted (string, slen, ++i, SX_COMPLETE);
1880 else if (c == '\'')
1881 i = skip_single_quoted (string, slen, ++i, 0);
1882 else if (c == '"')
1883 i = skip_double_quoted (string, slen, ++i, completeflag);
1884 else if (c == LPAREN && arithexp)
1885 {
1886 si = i + 1;
1887 if (string[si] == '\0')
1888 CQ_RETURN(si);
1889
1890 temp = extract_delimited_string (string, &si, "(", "(", ")", SX_NOALLOC); /* ) */
1891 i = si;
1892 if (string[i] == '\0') /* don't increment i past EOS in loop */
1893 break;
1894 i++;
1895 continue;
1896 }
1897 else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE))
1898 {
1899 si = i + 2;
1900 if (string[si] == '\0')
1901 CQ_RETURN(si);
1902
1903 if (string[i+1] == LPAREN)
1904 temp = extract_delimited_string (string, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
1905 else
1906 temp = extract_dollar_brace_string (string, &si, 0, SX_NOALLOC);
1907 i = si;
1908 if (string[i] == '\0') /* don't increment i past EOS in loop */
1909 break;
1910 i++;
1911 continue;
1912 }
1913 #if defined (PROCESS_SUBSTITUTION)
1914 else if (skipcmd && noprocsub == 0 && (c == '<' || c == '>') && string[i+1] == LPAREN)
1915 {
1916 si = i + 2;
1917 if (string[si] == '\0')
1918 CQ_RETURN(si);
1919 #if 1
1920 temp = extract_delimited_string (string, &si, (c == '<') ? "<(" : ">(", "(", ")", SX_COMMAND|SX_NOALLOC); /* )) */
1921 #else
1922 temp = extract_process_subst (string, (c == '<') ? "<(" : ">(", &si, 0);
1923 free (temp); /* XXX - not using SX_NOALLOC here yet */
1924 #endif
1925 i = si;
1926 if (string[i] == '\0')
1927 break;
1928 i++;
1929 continue;
1930 }
1931 #endif /* PROCESS_SUBSTITUTION */
1932 #if defined (EXTENDED_GLOB)
1933 else if ((flags & SD_EXTGLOB) && extended_glob && string[i+1] == LPAREN && member (c, "?*+!@"))
1934 {
1935 si = i + 2;
1936 if (string[si] == '\0')
1937 CQ_RETURN(si);
1938
1939 open[0] = c;
1940 open[1] = LPAREN;
1941 open[2] = '\0';
1942 temp = extract_delimited_string (string, &si, open, "(", ")", SX_NOALLOC); /* ) */
1943
1944 i = si;
1945 if (string[i] == '\0') /* don't increment i past EOS in loop */
1946 break;
1947 i++;
1948 continue;
1949 }
1950 #endif
1951 else if ((flags & SD_GLOB) && c == LBRACK)
1952 {
1953 si = i + 1;
1954 if (string[si] == '\0')
1955 CQ_RETURN(si);
1956
1957 temp = extract_delimited_string (string, &si, "[", "[", "]", SX_NOALLOC); /* ] */
1958
1959 i = si;
1960 if (string[i] == '\0') /* don't increment i past EOS in loop */
1961 break;
1962 i++;
1963 continue;
1964 }
1965 else if ((skipquote || invert) && (member (c, delims) == 0))
1966 break;
1967 else
1968 ADVANCE_CHAR (string, slen, i);
1969 }
1970
1971 CQ_RETURN(i);
1972 }
1973
1974 #if defined (BANG_HISTORY)
1975 /* Skip to the history expansion character (delims[0]), paying attention to
1976 quoted strings and command and process substitution. This is a stripped-
1977 down version of skip_to_delims. The essential difference is that this
1978 resets the quoting state when starting a command substitution */
1979 int
1980 skip_to_histexp (string, start, delims, flags)
1981 char *string;
1982 int start;
1983 char *delims;
1984 int flags;
1985 {
1986 int i, pass_next, backq, dquote, si, c, oldjmp;
1987 int histexp_comsub, histexp_backq, old_dquote;
1988 size_t slen;
1989 char *temp, open[3];
1990 DECLARE_MBSTATE;
1991
1992 slen = strlen (string + start) + start;
1993 oldjmp = no_longjmp_on_fatal_error;
1994 if (flags & SD_NOJMP)
1995 no_longjmp_on_fatal_error = 1;
1996
1997 histexp_comsub = histexp_backq = old_dquote = 0;
1998
1999 i = start;
2000 pass_next = backq = dquote = 0;
2001 while (c = string[i])
2002 {
2003 if (pass_next)
2004 {
2005 pass_next = 0;
2006 if (c == 0)
2007 CQ_RETURN(i);
2008 ADVANCE_CHAR (string, slen, i);
2009 continue;
2010 }
2011 else if (c == '\\')
2012 {
2013 pass_next = 1;
2014 i++;
2015 continue;
2016 }
2017 else if (backq && c == '`')
2018 {
2019 backq = 0;
2020 histexp_backq--;
2021 dquote = old_dquote;
2022 i++;
2023 continue;
2024 }
2025 else if (c == '`')
2026 {
2027 backq = 1;
2028 histexp_backq++;
2029 old_dquote = dquote; /* simple - one level for now */
2030 dquote = 0;
2031 i++;
2032 continue;
2033 }
2034 /* When in double quotes, act as if the double quote is a member of
2035 history_no_expand_chars, like the history library does */
2036 else if (dquote && c == delims[0] && string[i+1] == '"')
2037 {
2038 i++;
2039 continue;
2040 }
2041 else if (c == delims[0])
2042 break;
2043 /* the usual case is to use skip_xxx_quoted, but we don't skip over double
2044 quoted strings when looking for the history expansion character as a
2045 delimiter. */
2046 else if (dquote && c == '\'')
2047 {
2048 i++;
2049 continue;
2050 }
2051 else if (c == '\'')
2052 i = skip_single_quoted (string, slen, ++i, 0);
2053 /* The posixly_correct test makes posix-mode shells allow double quotes
2054 to quote the history expansion character */
2055 else if (posixly_correct == 0 && c == '"')
2056 {
2057 dquote = 1 - dquote;
2058 i++;
2059 continue;
2060 }
2061 else if (c == '"')
2062 i = skip_double_quoted (string, slen, ++i, 0);
2063 #if defined (PROCESS_SUBSTITUTION)
2064 else if ((c == '$' || c == '<' || c == '>') && string[i+1] == LPAREN && string[i+2] != LPAREN)
2065 #else
2066 else if (c == '$' && string[i+1] == LPAREN && string[i+2] != LPAREN)
2067 #endif
2068 {
2069 if (string[i+2] == '\0')
2070 CQ_RETURN(i+2);
2071 i += 2;
2072 histexp_comsub++;
2073 old_dquote = dquote;
2074 dquote = 0;
2075 }
2076 else if (histexp_comsub && c == RPAREN)
2077 {
2078 histexp_comsub--;
2079 dquote = old_dquote;
2080 i++;
2081 continue;
2082 }
2083 else if (backq) /* placeholder */
2084 {
2085 ADVANCE_CHAR (string, slen, i);
2086 continue;
2087 }
2088 else
2089 ADVANCE_CHAR (string, slen, i);
2090 }
2091
2092 CQ_RETURN(i);
2093 }
2094 #endif /* BANG_HISTORY */
2095
2096 #if defined (READLINE)
2097 /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
2098 an unclosed quoted string), or if the character at EINDEX is quoted
2099 by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
2100 single and double-quoted string parsing functions should not return an
2101 error if there are unclosed quotes or braces. The characters that this
2102 recognizes need to be the same as the contents of
2103 rl_completer_quote_characters. */
2104
2105 int
2106 char_is_quoted (string, eindex)
2107 char *string;
2108 int eindex;
2109 {
2110 int i, pass_next, c, oldjmp;
2111 size_t slen;
2112 DECLARE_MBSTATE;
2113
2114 slen = strlen (string);
2115 oldjmp = no_longjmp_on_fatal_error;
2116 no_longjmp_on_fatal_error = 1;
2117 i = pass_next = 0;
2118 while (i <= eindex)
2119 {
2120 c = string[i];
2121
2122 if (pass_next)
2123 {
2124 pass_next = 0;
2125 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
2126 CQ_RETURN(1);
2127 ADVANCE_CHAR (string, slen, i);
2128 continue;
2129 }
2130 else if (c == '\\')
2131 {
2132 pass_next = 1;
2133 i++;
2134 continue;
2135 }
2136 else if (c == '$' && string[i+1] == '\'' && string[i+2])
2137 {
2138 i += 2;
2139 i = skip_single_quoted (string, slen, i, SX_COMPLETE);
2140 if (i > eindex)
2141 CQ_RETURN (i);
2142 }
2143 else if (c == '\'' || c == '"')
2144 {
2145 i = (c == '\'') ? skip_single_quoted (string, slen, ++i, 0)
2146 : skip_double_quoted (string, slen, ++i, SX_COMPLETE);
2147 if (i > eindex)
2148 CQ_RETURN(1);
2149 /* no increment, the skip_xxx functions go one past end */
2150 }
2151 else
2152 ADVANCE_CHAR (string, slen, i);
2153 }
2154
2155 CQ_RETURN(0);
2156 }
2157
2158 int
2159 unclosed_pair (string, eindex, openstr)
2160 char *string;
2161 int eindex;
2162 char *openstr;
2163 {
2164 int i, pass_next, openc, olen;
2165 size_t slen;
2166 DECLARE_MBSTATE;
2167
2168 slen = strlen (string);
2169 olen = strlen (openstr);
2170 i = pass_next = openc = 0;
2171 while (i <= eindex)
2172 {
2173 if (pass_next)
2174 {
2175 pass_next = 0;
2176 if (i >= eindex) /* XXX was if (i >= eindex - 1) */
2177 return 0;
2178 ADVANCE_CHAR (string, slen, i);
2179 continue;
2180 }
2181 else if (string[i] == '\\')
2182 {
2183 pass_next = 1;
2184 i++;
2185 continue;
2186 }
2187 else if (STREQN (string + i, openstr, olen))
2188 {
2189 openc = 1 - openc;
2190 i += olen;
2191 }
2192 /* XXX - may want to handle $'...' specially here */
2193 else if (string[i] == '\'' || string[i] == '"')
2194 {
2195 i = (string[i] == '\'') ? skip_single_quoted (string, slen, i, 0)
2196 : skip_double_quoted (string, slen, i, SX_COMPLETE);
2197 if (i > eindex)
2198 return 0;
2199 }
2200 else
2201 ADVANCE_CHAR (string, slen, i);
2202 }
2203 return (openc);
2204 }
2205
2206 /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
2207 individual words. If DELIMS is NULL, the current value of $IFS is used
2208 to split the string, and the function follows the shell field splitting
2209 rules. SENTINEL is an index to look for. NWP, if non-NULL,
2210 gets the number of words in the returned list. CWP, if non-NULL, gets
2211 the index of the word containing SENTINEL. Non-whitespace chars in
2212 DELIMS delimit separate fields. */
2213 WORD_LIST *
2214 split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp)
2215 char *string;
2216 int slen;
2217 char *delims;
2218 int sentinel, flags;
2219 int *nwp, *cwp;
2220 {
2221 int ts, te, i, nw, cw, ifs_split, dflags;
2222 char *token, *d, *d2;
2223 WORD_LIST *ret, *tl;
2224
2225 if (string == 0 || *string == '\0')
2226 {
2227 if (nwp)
2228 *nwp = 0;
2229 if (cwp)
2230 *cwp = 0;
2231 return ((WORD_LIST *)NULL);
2232 }
2233
2234 d = (delims == 0) ? ifs_value : delims;
2235 ifs_split = delims == 0;
2236
2237 /* Make d2 the non-whitespace characters in delims */
2238 d2 = 0;
2239 if (delims)
2240 {
2241 size_t slength;
2242 #if defined (HANDLE_MULTIBYTE)
2243 size_t mblength = 1;
2244 #endif
2245 DECLARE_MBSTATE;
2246
2247 slength = strlen (delims);
2248 d2 = (char *)xmalloc (slength + 1);
2249 i = ts = 0;
2250 while (delims[i])
2251 {
2252 #if defined (HANDLE_MULTIBYTE)
2253 mbstate_t state_bak;
2254 state_bak = state;
2255 mblength = MBRLEN (delims + i, slength, &state);
2256 if (MB_INVALIDCH (mblength))
2257 state = state_bak;
2258 else if (mblength > 1)
2259 {
2260 memcpy (d2 + ts, delims + i, mblength);
2261 ts += mblength;
2262 i += mblength;
2263 slength -= mblength;
2264 continue;
2265 }
2266 #endif
2267 if (whitespace (delims[i]) == 0)
2268 d2[ts++] = delims[i];
2269
2270 i++;
2271 slength--;
2272 }
2273 d2[ts] = '\0';
2274 }
2275
2276 ret = (WORD_LIST *)NULL;
2277
2278 /* Remove sequences of whitespace characters at the start of the string, as
2279 long as those characters are delimiters. */
2280 for (i = 0; member (string[i], d) && spctabnl (string[i]); i++)
2281 ;
2282 if (string[i] == '\0')
2283 {
2284 FREE (d2);
2285 return (ret);
2286 }
2287
2288 ts = i;
2289 nw = 0;
2290 cw = -1;
2291 dflags = flags|SD_NOJMP;
2292 while (1)
2293 {
2294 te = skip_to_delim (string, ts, d, dflags);
2295
2296 /* If we have a non-whitespace delimiter character, use it to make a
2297 separate field. This is just about what $IFS splitting does and
2298 is closer to the behavior of the shell parser. */
2299 if (ts == te && d2 && member (string[ts], d2))
2300 {
2301 te = ts + 1;
2302 /* If we're using IFS splitting, the non-whitespace delimiter char
2303 and any additional IFS whitespace delimits a field. */
2304 if (ifs_split)
2305 while (member (string[te], d) && spctabnl (string[te]))
2306 te++;
2307 else
2308 while (member (string[te], d2))
2309 te++;
2310 }
2311
2312 token = substring (string, ts, te);
2313
2314 ret = add_string_to_list (token, ret);
2315 free (token);
2316 nw++;
2317
2318 if (sentinel >= ts && sentinel <= te)
2319 cw = nw;
2320
2321 /* If the cursor is at whitespace just before word start, set the
2322 sentinel word to the current word. */
2323 if (cwp && cw == -1 && sentinel == ts-1)
2324 cw = nw;
2325
2326 /* If the cursor is at whitespace between two words, make a new, empty
2327 word, add it before (well, after, since the list is in reverse order)
2328 the word we just added, and set the current word to that one. */
2329 if (cwp && cw == -1 && sentinel < ts)
2330 {
2331 tl = make_word_list (make_word (""), ret->next);
2332 ret->next = tl;
2333 cw = nw;
2334 nw++;
2335 }
2336
2337 if (string[te] == 0)
2338 break;
2339
2340 i = te;
2341 while (member (string[i], d) && (ifs_split || spctabnl(string[i])))
2342 i++;
2343
2344 if (string[i])
2345 ts = i;
2346 else
2347 break;
2348 }
2349
2350 /* Special case for SENTINEL at the end of STRING. If we haven't found
2351 the word containing SENTINEL yet, and the index we're looking for is at
2352 the end of STRING (or past the end of the previously-found token,
2353 possible if the end of the line is composed solely of IFS whitespace)
2354 add an additional null argument and set the current word pointer to that. */
2355 if (cwp && cw == -1 && (sentinel >= slen || sentinel >= te))
2356 {
2357 if (whitespace (string[sentinel - 1]))
2358 {
2359 token = "";
2360 ret = add_string_to_list (token, ret);
2361 nw++;
2362 }
2363 cw = nw;
2364 }
2365
2366 if (nwp)
2367 *nwp = nw;
2368 if (cwp)
2369 *cwp = cw;
2370
2371 FREE (d2);
2372
2373 return (REVERSE_LIST (ret, WORD_LIST *));
2374 }
2375 #endif /* READLINE */
2376
2377 #if 0
2378 /* UNUSED */
2379 /* Extract the name of the variable to bind to from the assignment string. */
2380 char *
2381 assignment_name (string)
2382 char *string;
2383 {
2384 int offset;
2385 char *temp;
2386
2387 offset = assignment (string, 0);
2388 if (offset == 0)
2389 return (char *)NULL;
2390 temp = substring (string, 0, offset);
2391 return (temp);
2392 }
2393 #endif
2394
2395 /* **************************************************************** */
2396 /* */
2397 /* Functions to convert strings to WORD_LISTs and vice versa */
2398 /* */
2399 /* **************************************************************** */
2400
2401 /* Return a single string of all the words in LIST. SEP is the separator
2402 to put between individual elements of LIST in the output string. */
2403 char *
2404 string_list_internal (list, sep)
2405 WORD_LIST *list;
2406 char *sep;
2407 {
2408 register WORD_LIST *t;
2409 char *result, *r;
2410 size_t word_len, sep_len, result_size;
2411
2412 if (list == 0)
2413 return ((char *)NULL);
2414
2415 /* Short-circuit quickly if we don't need to separate anything. */
2416 if (list->next == 0)
2417 return (savestring (list->word->word));
2418
2419 /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
2420 sep_len = STRLEN (sep);
2421 result_size = 0;
2422
2423 for (t = list; t; t = t->next)
2424 {
2425 if (t != list)
2426 result_size += sep_len;
2427 result_size += strlen (t->word->word);
2428 }
2429
2430 r = result = (char *)xmalloc (result_size + 1);
2431
2432 for (t = list; t; t = t->next)
2433 {
2434 if (t != list && sep_len)
2435 {
2436 if (sep_len > 1)
2437 {
2438 FASTCOPY (sep, r, sep_len);
2439 r += sep_len;
2440 }
2441 else
2442 *r++ = sep[0];
2443 }
2444
2445 word_len = strlen (t->word->word);
2446 FASTCOPY (t->word->word, r, word_len);
2447 r += word_len;
2448 }
2449
2450 *r = '\0';
2451 return (result);
2452 }
2453
2454 /* Return a single string of all the words present in LIST, separating
2455 each word with a space. */
2456 char *
2457 string_list (list)
2458 WORD_LIST *list;
2459 {
2460 return (string_list_internal (list, " "));
2461 }
2462
2463 /* An external interface that can be used by the rest of the shell to
2464 obtain a string containing the first character in $IFS. Handles all
2465 the multibyte complications. If LENP is non-null, it is set to the
2466 length of the returned string. */
2467 char *
2468 ifs_firstchar (lenp)
2469 int *lenp;
2470 {
2471 char *ret;
2472 int len;
2473
2474 ret = xmalloc (MB_LEN_MAX + 1);
2475 #if defined (HANDLE_MULTIBYTE)
2476 if (ifs_firstc_len == 1)
2477 {
2478 ret[0] = ifs_firstc[0];
2479 ret[1] = '\0';
2480 len = ret[0] ? 1 : 0;
2481 }
2482 else
2483 {
2484 memcpy (ret, ifs_firstc, ifs_firstc_len);
2485 ret[len = ifs_firstc_len] = '\0';
2486 }
2487 #else
2488 ret[0] = ifs_firstc;
2489 ret[1] = '\0';
2490 len = ret[0] ? 0 : 1;
2491 #endif
2492
2493 if (lenp)
2494 *lenp = len;
2495
2496 return ret;
2497 }
2498
2499 /* Return a single string of all the words present in LIST, obeying the
2500 quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
2501 expansion [of $*] appears within a double quoted string, it expands
2502 to a single field with the value of each parameter separated by the
2503 first character of the IFS variable, or by a <space> if IFS is unset." */
2504 char *
2505 string_list_dollar_star (list)
2506 WORD_LIST *list;
2507 {
2508 char *ret;
2509 #if defined (HANDLE_MULTIBYTE)
2510 # if defined (__GNUC__)
2511 char sep[MB_CUR_MAX + 1];
2512 # else
2513 char *sep = 0;
2514 # endif
2515 #else
2516 char sep[2];
2517 #endif
2518
2519 #if defined (HANDLE_MULTIBYTE)
2520 # if !defined (__GNUC__)
2521 sep = (char *)xmalloc (MB_CUR_MAX + 1);
2522 # endif /* !__GNUC__ */
2523 if (ifs_firstc_len == 1)
2524 {
2525 sep[0] = ifs_firstc[0];
2526 sep[1] = '\0';
2527 }
2528 else
2529 {
2530 memcpy (sep, ifs_firstc, ifs_firstc_len);
2531 sep[ifs_firstc_len] = '\0';
2532 }
2533 #else
2534 sep[0] = ifs_firstc;
2535 sep[1] = '\0';
2536 #endif
2537
2538 ret = string_list_internal (list, sep);
2539 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2540 free (sep);
2541 #endif
2542 return ret;
2543 }
2544
2545 /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
2546 is non-zero, the $@ appears within double quotes, and we should quote
2547 the list before converting it into a string. If IFS is unset, and the
2548 word is not quoted, we just need to quote CTLESC and CTLNUL characters
2549 in the words in the list, because the default value of $IFS is
2550 <space><tab><newline>, IFS characters in the words in the list should
2551 also be split. If IFS is null, and the word is not quoted, we need
2552 to quote the words in the list to preserve the positional parameters
2553 exactly.
2554 Valid values for the FLAGS argument are the PF_ flags in command.h,
2555 the only one we care about is PF_ASSIGNRHS. $@ is supposed to expand
2556 to the positional parameters separated by spaces no matter what IFS is
2557 set to if in a context where word splitting is not performed. The only
2558 one that we didn't handle before is assignment statement arguments to
2559 declaration builtins like `declare'. */
2560 char *
2561 string_list_dollar_at (list, quoted, flags)
2562 WORD_LIST *list;
2563 int quoted;
2564 int flags;
2565 {
2566 char *ifs, *ret;
2567 #if defined (HANDLE_MULTIBYTE)
2568 # if defined (__GNUC__)
2569 char sep[MB_CUR_MAX + 1];
2570 # else
2571 char *sep = 0;
2572 # endif /* !__GNUC__ */
2573 #else
2574 char sep[2];
2575 #endif
2576 WORD_LIST *tlist;
2577
2578 /* XXX this could just be ifs = ifs_value; */
2579 ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
2580
2581 #if defined (HANDLE_MULTIBYTE)
2582 # if !defined (__GNUC__)
2583 sep = (char *)xmalloc (MB_CUR_MAX + 1);
2584 # endif /* !__GNUC__ */
2585 /* XXX - bash-4.4/bash-5.0 testing PF_ASSIGNRHS */
2586 if (flags & PF_ASSIGNRHS)
2587 {
2588 sep[0] = ' ';
2589 sep[1] = '\0';
2590 }
2591 else if (ifs && *ifs)
2592 {
2593 if (ifs_firstc_len == 1)
2594 {
2595 sep[0] = ifs_firstc[0];
2596 sep[1] = '\0';
2597 }
2598 else
2599 {
2600 memcpy (sep, ifs_firstc, ifs_firstc_len);
2601 sep[ifs_firstc_len] = '\0';
2602 }
2603 }
2604 else
2605 {
2606 sep[0] = ' ';
2607 sep[1] = '\0';
2608 }
2609 #else
2610 /* XXX - bash-4.4/bash-5.0 test PF_ASSIGNRHS */
2611 sep[0] = ((flags & PF_ASSIGNRHS) || ifs == 0 || *ifs == 0) ? ' ' : *ifs;
2612 sep[1] = '\0';
2613 #endif
2614
2615 /* XXX -- why call quote_list if ifs == 0? we can get away without doing
2616 it now that quote_escapes quotes spaces */
2617 tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
2618 ? quote_list (list)
2619 : list_quote_escapes (list);
2620
2621 ret = string_list_internal (tlist, sep);
2622 #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
2623 free (sep);
2624 #endif
2625 return ret;
2626 }
2627
2628 /* Turn the positional parameters into a string, understanding quoting and
2629 the various subtleties of using the first character of $IFS as the
2630 separator. Calls string_list_dollar_at, string_list_dollar_star, and
2631 string_list as appropriate. */
2632 char *
2633 string_list_pos_params (pchar, list, quoted)
2634 int pchar;
2635 WORD_LIST *list;
2636 int quoted;
2637 {
2638 char *ret;
2639 WORD_LIST *tlist;
2640
2641 if (pchar == '*' && (quoted & Q_DOUBLE_QUOTES))
2642 {
2643 tlist = quote_list (list);
2644 word_list_remove_quoted_nulls (tlist);
2645 ret = string_list_dollar_star (tlist);
2646 }
2647 else if (pchar == '*' && (quoted & Q_HERE_DOCUMENT))
2648 {
2649 tlist = quote_list (list);
2650 word_list_remove_quoted_nulls (tlist);
2651 ret = string_list (tlist);
2652 }
2653 else if (pchar == '*')
2654 {
2655 /* Even when unquoted, string_list_dollar_star does the right thing
2656 making sure that the first character of $IFS is used as the
2657 separator. */
2658 ret = string_list_dollar_star (list);
2659 }
2660 else if (pchar == '@' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
2661 /* We use string_list_dollar_at, but only if the string is quoted, since
2662 that quotes the escapes if it's not, which we don't want. We could
2663 use string_list (the old code did), but that doesn't do the right
2664 thing if the first character of $IFS is not a space. We use
2665 string_list_dollar_star if the string is unquoted so we make sure that
2666 the elements of $@ are separated by the first character of $IFS for
2667 later splitting. */
2668 ret = string_list_dollar_at (list, quoted, 0);
2669 else if (pchar == '@')
2670 ret = string_list_dollar_star (list);
2671 else
2672 ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list);
2673
2674 return ret;
2675 }
2676
2677 /* Return the list of words present in STRING. Separate the string into
2678 words at any of the characters found in SEPARATORS. If QUOTED is
2679 non-zero then word in the list will have its quoted flag set, otherwise
2680 the quoted flag is left as make_word () deemed fit.
2681
2682 This obeys the P1003.2 word splitting semantics. If `separators' is
2683 exactly <space><tab><newline>, then the splitting algorithm is that of
2684 the Bourne shell, which treats any sequence of characters from `separators'
2685 as a delimiter. If IFS is unset, which results in `separators' being set
2686 to "", no splitting occurs. If separators has some other value, the
2687 following rules are applied (`IFS white space' means zero or more
2688 occurrences of <space>, <tab>, or <newline>, as long as those characters
2689 are in `separators'):
2690
2691 1) IFS white space is ignored at the start and the end of the
2692 string.
2693 2) Each occurrence of a character in `separators' that is not
2694 IFS white space, along with any adjacent occurrences of
2695 IFS white space delimits a field.
2696 3) Any nonzero-length sequence of IFS white space delimits a field.
2697 */
2698
2699 /* BEWARE! list_string strips null arguments. Don't call it twice and
2700 expect to have "" preserved! */
2701
2702 /* This performs word splitting and quoted null character removal on
2703 STRING. */
2704 #define issep(c) \
2705 (((separators)[0]) ? ((separators)[1] ? isifs(c) \
2706 : (c) == (separators)[0]) \
2707 : 0)
2708
2709 WORD_LIST *
2710 list_string (string, separators, quoted)
2711 register char *string, *separators;
2712 int quoted;
2713 {
2714 WORD_LIST *result;
2715 WORD_DESC *t;
2716 char *current_word, *s;
2717 int sindex, sh_style_split, whitesep, xflags;
2718 size_t slen;
2719
2720 if (!string || !*string)
2721 return ((WORD_LIST *)NULL);
2722
2723 sh_style_split = separators && separators[0] == ' ' &&
2724 separators[1] == '\t' &&
2725 separators[2] == '\n' &&
2726 separators[3] == '\0';
2727 for (xflags = 0, s = ifs_value; s && *s; s++)
2728 {
2729 if (*s == CTLESC) xflags |= SX_NOCTLESC;
2730 else if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
2731 }
2732
2733 slen = 0;
2734 /* Remove sequences of whitespace at the beginning of STRING, as
2735 long as those characters appear in IFS. Do not do this if
2736 STRING is quoted or if there are no separator characters. */
2737 if (!quoted || !separators || !*separators)
2738 {
2739 for (s = string; *s && spctabnl (*s) && issep (*s); s++);
2740
2741 if (!*s)
2742 return ((WORD_LIST *)NULL);
2743
2744 string = s;
2745 }
2746
2747 /* OK, now STRING points to a word that does not begin with white space.
2748 The splitting algorithm is:
2749 extract a word, stopping at a separator
2750 skip sequences of spc, tab, or nl as long as they are separators
2751 This obeys the field splitting rules in Posix.2. */
2752 slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 1;
2753 for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
2754 {
2755 /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
2756 unless multibyte chars are possible. */
2757 current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags);
2758 if (current_word == 0)
2759 break;
2760
2761 /* If we have a quoted empty string, add a quoted null argument. We
2762 want to preserve the quoted null character iff this is a quoted
2763 empty string; otherwise the quoted null characters are removed
2764 below. */
2765 if (QUOTED_NULL (current_word))
2766 {
2767 t = alloc_word_desc ();
2768 t->word = make_quoted_char ('\0');
2769 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
2770 result = make_word_list (t, result);
2771 }
2772 else if (current_word[0] != '\0')
2773 {
2774 /* If we have something, then add it regardless. However,
2775 perform quoted null character removal on the current word. */
2776 remove_quoted_nulls (current_word);
2777 result = add_string_to_list (current_word, result);
2778 result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */
2779 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
2780 result->word->flags |= W_QUOTED;
2781 }
2782
2783 /* If we're not doing sequences of separators in the traditional
2784 Bourne shell style, then add a quoted null argument. */
2785 else if (!sh_style_split && !spctabnl (string[sindex]))
2786 {
2787 t = alloc_word_desc ();
2788 t->word = make_quoted_char ('\0');
2789 t->flags |= W_QUOTED|W_HASQUOTEDNULL;
2790 result = make_word_list (t, result);
2791 }
2792
2793 free (current_word);
2794
2795 /* Note whether or not the separator is IFS whitespace, used later. */
2796 whitesep = string[sindex] && spctabnl (string[sindex]);
2797
2798 /* Move past the current separator character. */
2799 if (string[sindex])
2800 {
2801 DECLARE_MBSTATE;
2802 ADVANCE_CHAR (string, slen, sindex);
2803 }
2804
2805 /* Now skip sequences of space, tab, or newline characters if they are
2806 in the list of separators. */
2807 while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex]))
2808 sindex++;
2809
2810 /* If the first separator was IFS whitespace and the current character
2811 is a non-whitespace IFS character, it should be part of the current
2812 field delimiter, not a separate delimiter that would result in an
2813 empty field. Look at POSIX.2, 3.6.5, (3)(b). */
2814 if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex]))
2815 {
2816 sindex++;
2817 /* An IFS character that is not IFS white space, along with any
2818 adjacent IFS white space, shall delimit a field. (SUSv3) */
2819 while (string[sindex] && spctabnl (string[sindex]) && isifs (string[sindex]))
2820 sindex++;
2821 }
2822 }
2823 return (REVERSE_LIST (result, WORD_LIST *));
2824 }
2825
2826 /* Parse a single word from STRING, using SEPARATORS to separate fields.
2827 ENDPTR is set to the first character after the word. This is used by
2828 the `read' builtin.
2829
2830 This is never called with SEPARATORS != $IFS, and takes advantage of that.
2831
2832 XXX - this function is very similar to list_string; they should be
2833 combined - XXX */
2834
2835 #define islocalsep(c) (local_cmap[(unsigned char)(c)] != 0)
2836
2837 char *
2838 get_word_from_string (stringp, separators, endptr)
2839 char **stringp, *separators, **endptr;
2840 {
2841 register char *s;
2842 char *current_word;
2843 int sindex, sh_style_split, whitesep, xflags;
2844 unsigned char local_cmap[UCHAR_MAX+1]; /* really only need single-byte chars here */
2845 size_t slen;
2846
2847 if (!stringp || !*stringp || !**stringp)
2848 return ((char *)NULL);
2849
2850 sh_style_split = separators && separators[0] == ' ' &&
2851 separators[1] == '\t' &&
2852 separators[2] == '\n' &&
2853 separators[3] == '\0';
2854 memset (local_cmap, '\0', sizeof (local_cmap));
2855 for (xflags = 0, s = separators; s && *s; s++)
2856 {
2857 if (*s == CTLESC) xflags |= SX_NOCTLESC;
2858 if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
2859 local_cmap[(unsigned char)*s] = 1; /* local charmap of separators */
2860 }
2861
2862 s = *stringp;
2863 slen = 0;
2864
2865 /* Remove sequences of whitespace at the beginning of STRING, as
2866 long as those characters appear in SEPARATORS. This happens if
2867 SEPARATORS == $' \t\n' or if IFS is unset. */
2868 if (sh_style_split || separators == 0)
2869 {
2870 for (; *s && spctabnl (*s) && islocalsep (*s); s++);
2871
2872 /* If the string is nothing but whitespace, update it and return. */
2873 if (!*s)
2874 {
2875 *stringp = s;
2876 if (endptr)
2877 *endptr = s;
2878 return ((char *)NULL);
2879 }
2880 }
2881
2882 /* OK, S points to a word that does not begin with white space.
2883 Now extract a word, stopping at a separator, save a pointer to
2884 the first character after the word, then skip sequences of spc,
2885 tab, or nl as long as they are separators.
2886
2887 This obeys the field splitting rules in Posix.2. */
2888 sindex = 0;
2889 /* Don't need string length in ADVANCE_CHAR unless multibyte chars are
2890 possible, but need it in string_extract_verbatim for bounds checking */
2891 slen = STRLEN (s);
2892 current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags);
2893
2894 /* Set ENDPTR to the first character after the end of the word. */
2895 if (endptr)
2896 *endptr = s + sindex;
2897
2898 /* Note whether or not the separator is IFS whitespace, used later. */
2899 whitesep = s[sindex] && spctabnl (s[sindex]);
2900
2901 /* Move past the current separator character. */
2902 if (s[sindex])
2903 {
2904 DECLARE_MBSTATE;
2905 ADVANCE_CHAR (s, slen, sindex);
2906 }
2907
2908 /* Now skip sequences of space, tab, or newline characters if they are
2909 in the list of separators. */
2910 while (s[sindex] && spctabnl (s[sindex]) && islocalsep (s[sindex]))
2911 sindex++;
2912
2913 /* If the first separator was IFS whitespace and the current character is
2914 a non-whitespace IFS character, it should be part of the current field
2915 delimiter, not a separate delimiter that would result in an empty field.
2916 Look at POSIX.2, 3.6.5, (3)(b). */
2917 if (s[sindex] && whitesep && islocalsep (s[sindex]) && !spctabnl (s[sindex]))
2918 {
2919 sindex++;
2920 /* An IFS character that is not IFS white space, along with any adjacent
2921 IFS white space, shall delimit a field. */
2922 while (s[sindex] && spctabnl (s[sindex]) && islocalsep(s[sindex]))
2923 sindex++;
2924 }
2925
2926 /* Update STRING to point to the next field. */
2927 *stringp = s + sindex;
2928 return (current_word);
2929 }
2930
2931 /* Remove IFS white space at the end of STRING. Start at the end
2932 of the string and walk backwards until the beginning of the string
2933 or we find a character that's not IFS white space and not CTLESC.
2934 Only let CTLESC escape a white space character if SAW_ESCAPE is
2935 non-zero. */
2936 char *
2937 strip_trailing_ifs_whitespace (string, separators, saw_escape)
2938 char *string, *separators;
2939 int saw_escape;
2940 {
2941 char *s;
2942
2943 s = string + STRLEN (string) - 1;
2944 while (s > string && ((spctabnl (*s) && isifs (*s)) ||
2945 (saw_escape && *s == CTLESC && spctabnl (s[1]))))
2946 s--;
2947 *++s = '\0';
2948 return string;
2949 }
2950
2951 #if 0
2952 /* UNUSED */
2953 /* Split STRING into words at whitespace. Obeys shell-style quoting with
2954 backslashes, single and double quotes. */
2955 WORD_LIST *
2956 list_string_with_quotes (string)
2957 char *string;
2958 {
2959 WORD_LIST *list;
2960 char *token, *s;
2961 size_t s_len;
2962 int c, i, tokstart, len;
2963
2964 for (s = string; s && *s && spctabnl (*s); s++)
2965 ;
2966 if (s == 0 || *s == 0)
2967 return ((WORD_LIST *)NULL);
2968
2969 s_len = strlen (s);
2970 tokstart = i = 0;
2971 list = (WORD_LIST *)NULL;
2972 while (1)
2973 {
2974 c = s[i];
2975 if (c == '\\')
2976 {
2977 i++;
2978 if (s[i])
2979 i++;
2980 }
2981 else if (c == '\'')
2982 i = skip_single_quoted (s, s_len, ++i, 0);
2983 else if (c == '"')
2984 i = skip_double_quoted (s, s_len, ++i, 0);
2985 else if (c == 0 || spctabnl (c))
2986 {
2987 /* We have found the end of a token. Make a word out of it and
2988 add it to the word list. */
2989 token = substring (s, tokstart, i);
2990 list = add_string_to_list (token, list);
2991 free (token);
2992 while (spctabnl (s[i]))
2993 i++;
2994 if (s[i])
2995 tokstart = i;
2996 else
2997 break;
2998 }
2999 else
3000 i++; /* normal character */
3001 }
3002 return (REVERSE_LIST (list, WORD_LIST *));
3003 }
3004 #endif
3005
3006 /********************************************************/
3007 /* */
3008 /* Functions to perform assignment statements */
3009 /* */
3010 /********************************************************/
3011
3012 #if defined (ARRAY_VARS)
3013 static SHELL_VAR *
3014 do_compound_assignment (name, value, flags)
3015 char *name, *value;
3016 int flags;
3017 {
3018 SHELL_VAR *v;
3019 int mklocal, mkassoc, mkglobal;
3020 WORD_LIST *list;
3021
3022 mklocal = flags & ASS_MKLOCAL;
3023 mkassoc = flags & ASS_MKASSOC;
3024 mkglobal = flags & ASS_MKGLOBAL;
3025
3026 if (mklocal && variable_context)
3027 {
3028 v = find_variable (name);
3029 if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
3030 {
3031 if (v && readonly_p (v))
3032 err_readonly (name);
3033 return (v); /* XXX */
3034 }
3035 list = expand_compound_array_assignment (v, value, flags);
3036 if (mkassoc)
3037 v = make_local_assoc_variable (name);
3038 else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context)
3039 v = make_local_array_variable (name, 0);
3040 if (v)
3041 assign_compound_array_list (v, list, flags);
3042 if (list)
3043 dispose_words (list);
3044 }
3045 /* In a function but forcing assignment in global context */
3046 else if (mkglobal && variable_context)
3047 {
3048 v = find_global_variable (name);
3049 if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
3050 {
3051 if (v && readonly_p (v))
3052 err_readonly (name);
3053 return (v); /* XXX */
3054 }
3055 list = expand_compound_array_assignment (v, value, flags);
3056 if (v == 0 && mkassoc)
3057 v = make_new_assoc_variable (name);
3058 else if (v && mkassoc && assoc_p (v) == 0)
3059 v = convert_var_to_assoc (v);
3060 else if (v == 0)
3061 v = make_new_array_variable (name);
3062 else if (v && mkassoc == 0 && array_p (v) == 0)
3063 v = convert_var_to_array (v);
3064 if (v)
3065 assign_compound_array_list (v, list, flags);
3066 if (list)
3067 dispose_words (list);
3068 }
3069 else
3070 {
3071 v = assign_array_from_string (name, value, flags);
3072 if (v && ((readonly_p (v) && (flags & ASS_FORCE) == 0) || noassign_p (v)))
3073 {
3074 if (v && readonly_p (v))
3075 err_readonly (name);
3076 return (v); /* XXX */
3077 }
3078 }
3079
3080 return (v);
3081 }
3082 #endif
3083
3084 /* Given STRING, an assignment string, get the value of the right side
3085 of the `=', and bind it to the left side. If EXPAND is true, then
3086 perform parameter expansion, command substitution, and arithmetic
3087 expansion on the right-hand side. Perform tilde expansion in any
3088 case. Do not perform word splitting on the result of expansion. */
3089 static int
3090 do_assignment_internal (word, expand)
3091 const WORD_DESC *word;
3092 int expand;
3093 {
3094 int offset, appendop, assign_list, aflags, retval;
3095 char *name, *value, *temp;
3096 SHELL_VAR *entry;
3097 #if defined (ARRAY_VARS)
3098 char *t;
3099 int ni;
3100 #endif
3101 const char *string;
3102
3103 if (word == 0 || word->word == 0)
3104 return 0;
3105
3106 appendop = assign_list = aflags = 0;
3107 string = word->word;
3108 offset = assignment (string, 0);
3109 name = savestring (string);
3110 value = (char *)NULL;
3111
3112 if (name[offset] == '=')
3113 {
3114 if (name[offset - 1] == '+')
3115 {
3116 appendop = 1;
3117 name[offset - 1] = '\0';
3118 }
3119
3120 name[offset] = 0; /* might need this set later */
3121 temp = name + offset + 1;
3122
3123 #if defined (ARRAY_VARS)
3124 if (expand && (word->flags & W_COMPASSIGN))
3125 {
3126 assign_list = ni = 1;
3127 value = extract_array_assignment_list (temp, &ni);
3128 }
3129 else
3130 #endif
3131 if (expand && temp[0])
3132 value = expand_string_if_necessary (temp, 0, expand_string_assignment);
3133 else
3134 value = savestring (temp);
3135 }
3136
3137 if (value == 0)
3138 {
3139 value = (char *)xmalloc (1);
3140 value[0] = '\0';
3141 }
3142
3143 if (echo_command_at_execute)
3144 {
3145 if (appendop)
3146 name[offset - 1] = '+';
3147 xtrace_print_assignment (name, value, assign_list, 1);
3148 if (appendop)
3149 name[offset - 1] = '\0';
3150 }
3151
3152 #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
3153
3154 if (appendop)
3155 aflags |= ASS_APPEND;
3156
3157 #if defined (ARRAY_VARS)
3158 if (t = mbschr (name, '[')) /*]*/
3159 {
3160 if (assign_list)
3161 {
3162 report_error (_("%s: cannot assign list to array member"), name);
3163 ASSIGN_RETURN (0);
3164 }
3165 entry = assign_array_element (name, value, aflags);
3166 if (entry == 0)
3167 ASSIGN_RETURN (0);
3168 }
3169 else if (assign_list)
3170 {
3171 if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL) == 0)
3172 aflags |= ASS_MKLOCAL;
3173 if ((word->flags & W_ASSIGNARG) && (word->flags & W_ASSNGLOBAL))
3174 aflags |= ASS_MKGLOBAL;
3175 if (word->flags & W_ASSIGNASSOC)
3176 aflags |= ASS_MKASSOC;
3177 entry = do_compound_assignment (name, value, aflags);
3178 }
3179 else
3180 #endif /* ARRAY_VARS */
3181 entry = bind_variable (name, value, aflags);
3182
3183 stupidly_hack_special_variables (name);
3184
3185 /* Return 1 if the assignment seems to have been performed correctly. */
3186 if (entry == 0 || readonly_p (entry))
3187 retval = 0; /* assignment failure */
3188 else if (noassign_p (entry))
3189 {
3190 last_command_exit_value = EXECUTION_FAILURE;
3191 retval = 1; /* error status, but not assignment failure */
3192 }
3193 else
3194 retval = 1;
3195
3196 if (entry && retval != 0 && noassign_p (entry) == 0)
3197 VUNSETATTR (entry, att_invisible);
3198
3199 ASSIGN_RETURN (retval);
3200 }
3201
3202 /* Perform the assignment statement in STRING, and expand the
3203 right side by doing tilde, command and parameter expansion. */
3204 int
3205 do_assignment (string)
3206 char *string;
3207 {
3208 WORD_DESC td;
3209
3210 td.flags = W_ASSIGNMENT;
3211 td.word = string;
3212
3213 return do_assignment_internal (&td, 1);
3214 }
3215
3216 int
3217 do_word_assignment (word, flags)
3218 WORD_DESC *word;
3219 int flags;
3220 {
3221 return do_assignment_internal (word, 1);
3222 }
3223
3224 /* Given STRING, an assignment string, get the value of the right side
3225 of the `=', and bind it to the left side. Do not perform any word
3226 expansions on the right hand side. */
3227 int
3228 do_assignment_no_expand (string)
3229 char *string;
3230 {
3231 WORD_DESC td;
3232
3233 td.flags = W_ASSIGNMENT;
3234 td.word = string;
3235
3236 return (do_assignment_internal (&td, 0));
3237 }
3238
3239 /***************************************************
3240 * *
3241 * Functions to manage the positional parameters *
3242 * *
3243 ***************************************************/
3244
3245 /* Return the word list that corresponds to `$*'. */
3246 WORD_LIST *
3247 list_rest_of_args ()
3248 {
3249 register WORD_LIST *list, *args;
3250 int i;
3251
3252 /* Break out of the loop as soon as one of the dollar variables is null. */
3253 for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++)
3254 list = make_word_list (make_bare_word (dollar_vars[i]), list);
3255
3256 for (args = rest_of_args; args; args = args->next)
3257 list = make_word_list (make_bare_word (args->word->word), list);
3258
3259 return (REVERSE_LIST (list, WORD_LIST *));
3260 }
3261
3262 int
3263 number_of_args ()
3264 {
3265 register WORD_LIST *list;
3266 int n;
3267
3268 for (n = 0; n < 9 && dollar_vars[n+1]; n++)
3269 ;
3270 for (list = rest_of_args; list; list = list->next)
3271 n++;
3272 return n;
3273 }
3274
3275 /* Return the value of a positional parameter. This handles values > 10. */
3276 char *
3277 get_dollar_var_value (ind)
3278 intmax_t ind;
3279 {
3280 char *temp;
3281 WORD_LIST *p;
3282
3283 if (ind < 10)
3284 temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL;
3285 else /* We want something like ${11} */
3286 {
3287 ind -= 10;
3288 for (p = rest_of_args; p && ind--; p = p->next)
3289 ;
3290 temp = p ? savestring (p->word->word) : (char *)NULL;
3291 }
3292 return (temp);
3293 }
3294
3295 /* Make a single large string out of the dollar digit variables,
3296 and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
3297 case of "$*" with respect to IFS. */
3298 char *
3299 string_rest_of_args (dollar_star)
3300 int dollar_star;
3301 {
3302 register WORD_LIST *list;
3303 char *string;
3304
3305 list = list_rest_of_args ();
3306 string = dollar_star ? string_list_dollar_star (list) : string_list (list);
3307 dispose_words (list);
3308 return (string);
3309 }
3310
3311 /* Return a string containing the positional parameters from START to
3312 END, inclusive. If STRING[0] == '*', we obey the rules for $*,
3313 which only makes a difference if QUOTED is non-zero. If QUOTED includes
3314 Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
3315 no quoting chars are added. */
3316 static char *
3317 pos_params (string, start, end, quoted)
3318 char *string;
3319 int start, end, quoted;
3320 {
3321 WORD_LIST *save, *params, *h, *t;
3322 char *ret;
3323 int i;
3324
3325 /* see if we can short-circuit. if start == end, we want 0 parameters. */
3326 if (start == end)
3327 return ((char *)NULL);
3328
3329 save = params = list_rest_of_args ();
3330 if (save == 0 && start > 0)
3331 return ((char *)NULL);
3332
3333 if (start == 0) /* handle ${@:0[:x]} specially */
3334 {
3335 t = make_word_list (make_word (dollar_vars[0]), params);
3336 save = params = t;
3337 }
3338
3339 for (i = start ? 1 : 0; params && i < start; i++)
3340 params = params->next;
3341 if (params == 0)
3342 {
3343 dispose_words (save);
3344 return ((char *)NULL);
3345 }
3346 for (h = t = params; params && i < end; i++)
3347 {
3348 t = params;
3349 params = params->next;
3350 }
3351
3352 t->next = (WORD_LIST *)NULL;
3353
3354 ret = string_list_pos_params (string[0], h, quoted);
3355
3356 if (t != params)
3357 t->next = params;
3358
3359 dispose_words (save);
3360 return (ret);
3361 }
3362
3363 /******************************************************************/
3364 /* */
3365 /* Functions to expand strings to strings or WORD_LISTs */
3366 /* */
3367 /******************************************************************/
3368
3369 #if defined (PROCESS_SUBSTITUTION)
3370 #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
3371 #else
3372 #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
3373 #endif
3374
3375 /* If there are any characters in STRING that require full expansion,
3376 then call FUNC to expand STRING; otherwise just perform quote
3377 removal if necessary. This returns a new string. */
3378 static char *
3379 expand_string_if_necessary (string, quoted, func)
3380 char *string;
3381 int quoted;
3382 EXPFUNC *func;
3383 {
3384 WORD_LIST *list;
3385 size_t slen;
3386 int i, saw_quote;
3387 char *ret;
3388 DECLARE_MBSTATE;
3389
3390 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3391 slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
3392 i = saw_quote = 0;
3393 while (string[i])
3394 {
3395 if (EXP_CHAR (string[i]))
3396 break;
3397 else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
3398 saw_quote = 1;
3399 ADVANCE_CHAR (string, slen, i);
3400 }
3401
3402 if (string[i])
3403 {
3404 list = (*func) (string, quoted);
3405 if (list)
3406 {
3407 ret = string_list (list);
3408 dispose_words (list);
3409 }
3410 else
3411 ret = (char *)NULL;
3412 }
3413 else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
3414 ret = string_quote_removal (string, quoted);
3415 else
3416 ret = savestring (string);
3417
3418 return ret;
3419 }
3420
3421 static inline char *
3422 expand_string_to_string_internal (string, quoted, func)
3423 char *string;
3424 int quoted;
3425 EXPFUNC *func;
3426 {
3427 WORD_LIST *list;
3428 char *ret;
3429
3430 if (string == 0 || *string == '\0')
3431 return ((char *)NULL);
3432
3433 list = (*func) (string, quoted);
3434 if (list)
3435 {
3436 ret = string_list (list);
3437 dispose_words (list);
3438 }
3439 else
3440 ret = (char *)NULL;
3441
3442 return (ret);
3443 }
3444
3445 char *
3446 expand_string_to_string (string, quoted)
3447 char *string;
3448 int quoted;
3449 {
3450 return (expand_string_to_string_internal (string, quoted, expand_string));
3451 }
3452
3453 char *
3454 expand_string_unsplit_to_string (string, quoted)
3455 char *string;
3456 int quoted;
3457 {
3458 return (expand_string_to_string_internal (string, quoted, expand_string_unsplit));
3459 }
3460
3461 char *
3462 expand_assignment_string_to_string (string, quoted)
3463 char *string;
3464 int quoted;
3465 {
3466 return (expand_string_to_string_internal (string, quoted, expand_string_assignment));
3467 }
3468
3469 char *
3470 expand_arith_string (string, quoted)
3471 char *string;
3472 int quoted;
3473 {
3474 WORD_DESC td;
3475 WORD_LIST *list, *tlist;
3476 size_t slen;
3477 int i, saw_quote;
3478 char *ret;
3479 DECLARE_MBSTATE;
3480
3481 /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
3482 slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
3483 i = saw_quote = 0;
3484 while (string[i])
3485 {
3486 if (EXP_CHAR (string[i]))
3487 break;
3488 else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
3489 saw_quote = 1;
3490 ADVANCE_CHAR (string, slen, i);
3491 }
3492
3493 if (string[i])
3494 {
3495 /* This is expanded version of expand_string_internal as it's called by
3496 expand_string_leave_quoted */
3497 td.flags = W_NOPROCSUB; /* don't want process substitution */
3498 td.word = savestring (string);
3499 list = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3500 /* This takes care of the calls from expand_string_leave_quoted and
3501 expand_string */
3502 if (list)
3503 {
3504 tlist = word_list_split (list);
3505 dispose_words (list);
3506 list = tlist;
3507 if (list)
3508 dequote_list (list);
3509 }
3510 /* This comes from expand_string_if_necessary */
3511 if (list)
3512 {
3513 ret = string_list (list);
3514 dispose_words (list);
3515 }
3516 else
3517 ret = (char *)NULL;
3518 FREE (td.word);
3519 }
3520 else if (saw_quote && (quoted & Q_ARITH))
3521 ret = string_quote_removal (string, quoted);
3522 else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
3523 ret = string_quote_removal (string, quoted);
3524 else
3525 ret = savestring (string);
3526
3527 return ret;
3528 }
3529
3530 #if defined (COND_COMMAND)
3531 /* Just remove backslashes in STRING. Returns a new string. */
3532 char *
3533 remove_backslashes (string)
3534 char *string;
3535 {
3536 char *r, *ret, *s;
3537
3538 r = ret = (char *)xmalloc (strlen (string) + 1);
3539 for (s = string; s && *s; )
3540 {
3541 if (*s == '\\')
3542 s++;
3543 if (*s == 0)
3544 break;
3545 *r++ = *s++;
3546 }
3547 *r = '\0';
3548 return ret;
3549 }
3550
3551 /* This needs better error handling. */
3552 /* Expand W for use as an argument to a unary or binary operator in a
3553 [[...]] expression. If SPECIAL is 1, this is the rhs argument
3554 to the != or == operator, and should be treated as a pattern. In
3555 this case, we quote the string specially for the globbing code. If
3556 SPECIAL is 2, this is an rhs argument for the =~ operator, and should
3557 be quoted appropriately for regcomp/regexec. The caller is responsible
3558 for removing the backslashes if the unquoted word is needed later. */
3559 char *
3560 cond_expand_word (w, special)
3561 WORD_DESC *w;
3562 int special;
3563 {
3564 char *r, *p;
3565 WORD_LIST *l;
3566 int qflags;
3567
3568 if (w->word == 0 || w->word[0] == '\0')
3569 return ((char *)NULL);
3570
3571 expand_no_split_dollar_star = 1;
3572 w->flags |= W_NOSPLIT2;
3573 l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
3574 expand_no_split_dollar_star = 0;
3575 if (l)
3576 {
3577 if (special == 0) /* LHS */
3578 {
3579 dequote_list (l);
3580 r = string_list (l);
3581 }
3582 else
3583 {
3584 /* Need to figure out whether or not we should call dequote_escapes
3585 or a new dequote_ctlnul function here, and under what
3586 circumstances. */
3587 qflags = QGLOB_CVTNULL;
3588 if (special == 2)
3589 qflags |= QGLOB_REGEXP;
3590 word_list_remove_quoted_nulls (l);
3591 p = string_list (l);
3592 r = quote_string_for_globbing (p, qflags);
3593 free (p);
3594 }
3595 dispose_words (l);
3596 }
3597 else
3598 r = (char *)NULL;
3599
3600 return r;
3601 }
3602 #endif
3603
3604 /* Call expand_word_internal to expand W and handle error returns.
3605 A convenience function for functions that don't want to handle
3606 any errors or free any memory before aborting. */
3607 static WORD_LIST *
3608 call_expand_word_internal (w, q, i, c, e)
3609 WORD_DESC *w;
3610 int q, i, *c, *e;
3611 {
3612 WORD_LIST *result;
3613
3614 result = expand_word_internal (w, q, i, c, e);
3615 if (result == &expand_word_error || result == &expand_word_fatal)
3616 {
3617 /* By convention, each time this error is returned, w->word has
3618 already been freed (it sometimes may not be in the fatal case,
3619 but that doesn't result in a memory leak because we're going
3620 to exit in most cases). */
3621 w->word = (char *)NULL;
3622 last_command_exit_value = EXECUTION_FAILURE;
3623 exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
3624 /* NOTREACHED */
3625 return (NULL);
3626 }
3627 else
3628 return (result);
3629 }
3630
3631 /* Perform parameter expansion, command substitution, and arithmetic
3632 expansion on STRING, as if it were a word. Leave the result quoted.
3633 Since this does not perform word splitting, it leaves quoted nulls
3634 in the result. */
3635 static WORD_LIST *
3636 expand_string_internal (string, quoted)
3637 char *string;
3638 int quoted;
3639 {
3640 WORD_DESC td;
3641 WORD_LIST *tresult;
3642
3643 if (string == 0 || *string == 0)
3644 return ((WORD_LIST *)NULL);
3645
3646 td.flags = 0;
3647 td.word = savestring (string);
3648
3649 tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3650
3651 FREE (td.word);
3652 return (tresult);
3653 }
3654
3655 /* Expand STRING by performing parameter expansion, command substitution,
3656 and arithmetic expansion. Dequote the resulting WORD_LIST before
3657 returning it, but do not perform word splitting. The call to
3658 remove_quoted_nulls () is in here because word splitting normally
3659 takes care of quote removal. */
3660 WORD_LIST *
3661 expand_string_unsplit (string, quoted)
3662 char *string;
3663 int quoted;
3664 {
3665 WORD_LIST *value;
3666
3667 if (string == 0 || *string == '\0')
3668 return ((WORD_LIST *)NULL);
3669
3670 expand_no_split_dollar_star = 1;
3671 value = expand_string_internal (string, quoted);
3672 expand_no_split_dollar_star = 0;
3673
3674 if (value)
3675 {
3676 if (value->word)
3677 {
3678 remove_quoted_nulls (value->word->word);
3679 value->word->flags &= ~W_HASQUOTEDNULL;
3680 }
3681 dequote_list (value);
3682 }
3683 return (value);
3684 }
3685
3686 /* Expand the rhs of an assignment statement */
3687 WORD_LIST *
3688 expand_string_assignment (string, quoted)
3689 char *string;
3690 int quoted;
3691 {
3692 WORD_DESC td;
3693 WORD_LIST *value;
3694
3695 if (string == 0 || *string == '\0')
3696 return ((WORD_LIST *)NULL);
3697
3698 expand_no_split_dollar_star = 1;
3699
3700 td.flags = W_ASSIGNRHS;
3701 td.word = savestring (string);
3702 value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3703 FREE (td.word);
3704
3705 expand_no_split_dollar_star = 0;
3706
3707 if (value)
3708 {
3709 if (value->word)
3710 {
3711 remove_quoted_nulls (value->word->word);
3712 value->word->flags &= ~W_HASQUOTEDNULL;
3713 }
3714 dequote_list (value);
3715 }
3716 return (value);
3717 }
3718
3719
3720 /* Expand one of the PS? prompt strings. This is a sort of combination of
3721 expand_string_unsplit and expand_string_internal, but returns the
3722 passed string when an error occurs. Might want to trap other calls
3723 to jump_to_top_level here so we don't endlessly loop. */
3724 WORD_LIST *
3725 expand_prompt_string (string, quoted, wflags)
3726 char *string;
3727 int quoted;
3728 int wflags;
3729 {
3730 WORD_LIST *value;
3731 WORD_DESC td;
3732
3733 if (string == 0 || *string == 0)
3734 return ((WORD_LIST *)NULL);
3735
3736 td.flags = wflags;
3737 td.word = savestring (string);
3738
3739 no_longjmp_on_fatal_error = 1;
3740 value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
3741 no_longjmp_on_fatal_error = 0;
3742
3743 if (value == &expand_word_error || value == &expand_word_fatal)
3744 {
3745 value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
3746 return value;
3747 }
3748 FREE (td.word);
3749 if (value)
3750 {
3751 if (value->word)
3752 {
3753 remove_quoted_nulls (value->word->word);
3754 value->word->flags &= ~W_HASQUOTEDNULL;
3755 }
3756 dequote_list (value);
3757 }
3758 return (value);
3759 }
3760
3761 /* Expand STRING just as if you were expanding a word, but do not dequote
3762 the resultant WORD_LIST. This is called only from within this file,
3763 and is used to correctly preserve quoted characters when expanding
3764 things like ${1+"$@"}. This does parameter expansion, command
3765 substitution, arithmetic expansion, and word splitting. */
3766 static WORD_LIST *
3767 expand_string_leave_quoted (string, quoted)
3768 char *string;
3769 int quoted;
3770 {
3771 WORD_LIST *tlist;
3772 WORD_LIST *tresult;
3773
3774 if (string == 0 || *string == '\0')
3775 return ((WORD_LIST *)NULL);
3776
3777 tlist = expand_string_internal (string, quoted);
3778
3779 if (tlist)
3780 {
3781 tresult = word_list_split (tlist);
3782 dispose_words (tlist);
3783 return (tresult);
3784 }
3785 return ((WORD_LIST *)NULL);
3786 }
3787
3788 /* This does not perform word splitting or dequote the WORD_LIST
3789 it returns. */
3790 static WORD_LIST *
3791 expand_string_for_rhs (string, quoted, dollar_at_p, expanded_p)
3792 char *string;
3793 int quoted, *dollar_at_p, *expanded_p;
3794 {
3795 WORD_DESC td;
3796 WORD_LIST *tresult;
3797
3798 if (string == 0 || *string == '\0')
3799 return (WORD_LIST *)NULL;
3800
3801 expand_no_split_dollar_star = 1;
3802 td.flags = W_NOSPLIT2; /* no splitting, remove "" and '' */
3803 td.word = string;
3804 tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, expanded_p);
3805 expand_no_split_dollar_star = 0;
3806
3807 return (tresult);
3808 }
3809
3810 /* Expand STRING just as if you were expanding a word. This also returns
3811 a list of words. Note that filename globbing is *NOT* done for word
3812 or string expansion, just when the shell is expanding a command. This
3813 does parameter expansion, command substitution, arithmetic expansion,
3814 and word splitting. Dequote the resultant WORD_LIST before returning. */
3815 WORD_LIST *
3816 expand_string (string, quoted)
3817 char *string;
3818 int quoted;
3819 {
3820 WORD_LIST *result;
3821
3822 if (string == 0 || *string == '\0')
3823 return ((WORD_LIST *)NULL);
3824
3825 result = expand_string_leave_quoted (string, quoted);
3826 return (result ? dequote_list (result) : result);
3827 }
3828
3829 /***************************************************
3830 * *
3831 * Functions to handle quoting chars *
3832 * *
3833 ***************************************************/
3834
3835 /* Conventions:
3836
3837 A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
3838 The parser passes CTLNUL as CTLESC CTLNUL. */
3839
3840 /* Quote escape characters in string s, but no other characters. This is
3841 used to protect CTLESC and CTLNUL in variable values from the rest of
3842 the word expansion process after the variable is expanded (word splitting
3843 and filename generation). If IFS is null, we quote spaces as well, just
3844 in case we split on spaces later (in the case of unquoted $@, we will
3845 eventually attempt to split the entire word on spaces). Corresponding
3846 code exists in dequote_escapes. Even if we don't end up splitting on
3847 spaces, quoting spaces is not a problem. This should never be called on
3848 a string that is quoted with single or double quotes or part of a here
3849 document (effectively double-quoted). */
3850 char *
3851 quote_escapes (string)
3852 char *string;
3853 {
3854 register char *s, *t;
3855 size_t slen;
3856 char *result, *send;
3857 int quote_spaces, skip_ctlesc, skip_ctlnul;
3858 DECLARE_MBSTATE;
3859
3860 slen = strlen (string);
3861 send = string + slen;
3862
3863 quote_spaces = (ifs_value && *ifs_value == 0);
3864
3865 for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
3866 skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
3867
3868 t = result = (char *)xmalloc ((slen * 2) + 1);
3869 s = string;
3870
3871 while (*s)
3872 {
3873 if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' '))
3874 *t++ = CTLESC;
3875 COPY_CHAR_P (t, s, send);
3876 }
3877 *t = '\0';
3878
3879 return (result);
3880 }
3881
3882 static WORD_LIST *
3883 list_quote_escapes (list)
3884 WORD_LIST *list;
3885 {
3886 register WORD_LIST *w;
3887 char *t;
3888
3889 for (w = list; w; w = w->next)
3890 {
3891 t = w->word->word;
3892 w->word->word = quote_escapes (t);
3893 free (t);
3894 }
3895 return list;
3896 }
3897
3898 /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
3899
3900 The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
3901 This is necessary to make unquoted CTLESC and CTLNUL characters in the
3902 data stream pass through properly.
3903
3904 We need to remove doubled CTLESC characters inside quoted strings before
3905 quoting the entire string, so we do not double the number of CTLESC
3906 characters.
3907
3908 Also used by parts of the pattern substitution code. */
3909 char *
3910 dequote_escapes (string)
3911 char *string;
3912 {
3913 register char *s, *t, *s1;
3914 size_t slen;
3915 char *result, *send;
3916 int quote_spaces;
3917 DECLARE_MBSTATE;
3918
3919 if (string == 0)
3920 return string;
3921
3922 slen = strlen (string);
3923 send = string + slen;
3924
3925 t = result = (char *)xmalloc (slen + 1);
3926
3927 if (strchr (string, CTLESC) == 0)
3928 return (strcpy (result, string));
3929
3930 quote_spaces = (ifs_value && *ifs_value == 0);
3931
3932 s = string;
3933 while (*s)
3934 {
3935 if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' ')))
3936 {
3937 s++;
3938 if (*s == '\0')
3939 break;
3940 }
3941 COPY_CHAR_P (t, s, send);
3942 }
3943 *t = '\0';
3944
3945 return result;
3946 }
3947
3948 static WORD_LIST *
3949 list_dequote_escapes (list)
3950 WORD_LIST *list;
3951 {
3952 register WORD_LIST *w;
3953 char *t;
3954
3955 for (w = list; w; w = w->next)
3956 {
3957 t = w->word->word;
3958 w->word->word = dequote_escapes (t);
3959 free (t);
3960 }
3961 return list;
3962 }
3963
3964 /* Return a new string with the quoted representation of character C.
3965 This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
3966 set in any resultant WORD_DESC where this value is the word. */
3967 static char *
3968 make_quoted_char (c)
3969 int c;
3970 {
3971 char *temp;
3972
3973 temp = (char *)xmalloc (3);
3974 if (c == 0)
3975 {
3976 temp[0] = CTLNUL;
3977 temp[1] = '\0';
3978 }
3979 else
3980 {
3981 temp[0] = CTLESC;
3982 temp[1] = c;
3983 temp[2] = '\0';
3984 }
3985 return (temp);
3986 }
3987
3988 /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
3989 the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
3990 this value is the word. */
3991 char *
3992 quote_string (string)
3993 char *string;
3994 {
3995 register char *t;
3996 size_t slen;
3997 char *result, *send;
3998
3999 if (*string == 0)
4000 {
4001 result = (char *)xmalloc (2);
4002 result[0] = CTLNUL;
4003 result[1] = '\0';
4004 }
4005 else
4006 {
4007 DECLARE_MBSTATE;
4008
4009 slen = strlen (string);
4010 send = string + slen;
4011
4012 result = (char *)xmalloc ((slen * 2) + 1);
4013
4014 for (t = result; string < send; )
4015 {
4016 *t++ = CTLESC;
4017 COPY_CHAR_P (t, string, send);
4018 }
4019 *t = '\0';
4020 }
4021 return (result);
4022 }
4023
4024 /* De-quote quoted characters in STRING. */
4025 char *
4026 dequote_string (string)
4027 char *string;
4028 {
4029 register char *s, *t;
4030 size_t slen;
4031 char *result, *send;
4032 DECLARE_MBSTATE;
4033
4034 #if defined (DEBUG)
4035 if (string[0] == CTLESC && string[1] == 0)
4036 internal_inform ("dequote_string: string with bare CTLESC");
4037 #endif
4038
4039 slen = strlen (string);
4040
4041 t = result = (char *)xmalloc (slen + 1);
4042
4043 if (QUOTED_NULL (string))
4044 {
4045 result[0] = '\0';
4046 return (result);
4047 }
4048
4049 /* A string consisting of only a single CTLESC should pass through unchanged */
4050 if (string[0] == CTLESC && string[1] == 0)
4051 {
4052 result[0] = CTLESC;
4053 result[1] = '\0';
4054 return (result);
4055 }
4056
4057 /* If no character in the string can be quoted, don't bother examining
4058 each character. Just return a copy of the string passed to us. */
4059 if (strchr (string, CTLESC) == NULL)
4060 return (strcpy (result, string));
4061
4062 send = string + slen;
4063 s = string;
4064 while (*s)
4065 {
4066 if (*s == CTLESC)
4067 {
4068 s++;
4069 if (*s == '\0')
4070 break;
4071 }
4072 COPY_CHAR_P (t, s, send);
4073 }
4074
4075 *t = '\0';
4076 return (result);
4077 }
4078
4079 /* Quote the entire WORD_LIST list. */
4080 static WORD_LIST *
4081 quote_list (list)
4082 WORD_LIST *list;
4083 {
4084 register WORD_LIST *w;
4085 char *t;
4086
4087 for (w = list; w; w = w->next)
4088 {
4089 t = w->word->word;
4090 w->word->word = quote_string (t);
4091 if (*t == 0)
4092 w->word->flags |= W_HASQUOTEDNULL; /* XXX - turn on W_HASQUOTEDNULL here? */
4093 w->word->flags |= W_QUOTED;
4094 free (t);
4095 }
4096 return list;
4097 }
4098
4099 /* De-quote quoted characters in each word in LIST. */
4100 WORD_LIST *
4101 dequote_list (list)
4102 WORD_LIST *list;
4103 {
4104 register char *s;
4105 register WORD_LIST *tlist;
4106
4107 for (tlist = list; tlist; tlist = tlist->next)
4108 {
4109 s = dequote_string (tlist->word->word);
4110 if (QUOTED_NULL (tlist->word->word))
4111 tlist->word->flags &= ~W_HASQUOTEDNULL;
4112 free (tlist->word->word);
4113 tlist->word->word = s;
4114 }
4115 return list;
4116 }
4117
4118 /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
4119 string. */
4120 char *
4121 remove_quoted_escapes (string)
4122 char *string;
4123 {
4124 char *t;
4125
4126 if (string)
4127 {
4128 t = dequote_escapes (string);
4129 strcpy (string, t);
4130 free (t);
4131 }
4132
4133 return (string);
4134 }
4135
4136 /* Perform quoted null character removal on STRING. We don't allow any
4137 quoted null characters in the middle or at the ends of strings because
4138 of how expand_word_internal works. remove_quoted_nulls () turns
4139 STRING into an empty string iff it only consists of a quoted null,
4140 and removes all unquoted CTLNUL characters. */
4141 char *
4142 remove_quoted_nulls (string)
4143 char *string;
4144 {
4145 register size_t slen;
4146 register int i, j, prev_i;
4147 DECLARE_MBSTATE;
4148
4149 if (strchr (string, CTLNUL) == 0) /* XXX */
4150 return string; /* XXX */
4151
4152 slen = strlen (string);
4153 i = j = 0;
4154
4155 while (i < slen)
4156 {
4157 if (string[i] == CTLESC)
4158 {
4159 /* Old code had j++, but we cannot assume that i == j at this
4160 point -- what if a CTLNUL has already been removed from the
4161 string? We don't want to drop the CTLESC or recopy characters
4162 that we've already copied down. */
4163 i++; string[j++] = CTLESC;
4164 if (i == slen)
4165 break;
4166 }
4167 else if (string[i] == CTLNUL)
4168 {
4169 i++;
4170 continue;
4171 }
4172
4173 prev_i = i;
4174 ADVANCE_CHAR (string, slen, i);
4175 if (j < prev_i)
4176 {
4177 do string[j++] = string[prev_i++]; while (prev_i < i);
4178 }
4179 else
4180 j = i;
4181 }
4182 string[j] = '\0';
4183
4184 return (string);
4185 }
4186
4187 /* Perform quoted null character removal on each element of LIST.
4188 This modifies LIST. */
4189 void
4190 word_list_remove_quoted_nulls (list)
4191 WORD_LIST *list;
4192 {
4193 register WORD_LIST *t;
4194
4195 for (t = list; t; t = t->next)
4196 {
4197 remove_quoted_nulls (t->word->word);
4198 t->word->flags &= ~W_HASQUOTEDNULL;
4199 }
4200 }
4201
4202 /* **************************************************************** */
4203 /* */
4204 /* Functions for Matching and Removing Patterns */
4205 /* */
4206 /* **************************************************************** */
4207
4208 #if defined (HANDLE_MULTIBYTE)
4209 #if 0 /* Currently unused */
4210 static unsigned char *
4211 mb_getcharlens (string, len)
4212 char *string;
4213 int len;
4214 {
4215 int i, offset, last;
4216 unsigned char *ret;
4217 char *p;
4218 DECLARE_MBSTATE;
4219
4220 i = offset = 0;
4221 last = 0;
4222 ret = (unsigned char *)xmalloc (len);
4223 memset (ret, 0, len);
4224 while (string[last])
4225 {
4226 ADVANCE_CHAR (string, len, offset);
4227 ret[last] = offset - last;
4228 last = offset;
4229 }
4230 return ret;
4231 }
4232 #endif
4233 #endif
4234
4235 /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
4236 can have one of 4 values:
4237 RP_LONG_LEFT remove longest matching portion at start of PARAM
4238 RP_SHORT_LEFT remove shortest matching portion at start of PARAM
4239 RP_LONG_RIGHT remove longest matching portion at end of PARAM
4240 RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
4241 */
4242
4243 #define RP_LONG_LEFT 1
4244 #define RP_SHORT_LEFT 2
4245 #define RP_LONG_RIGHT 3
4246 #define RP_SHORT_RIGHT 4
4247
4248 /* Returns its first argument if nothing matched; new memory otherwise */
4249 static char *
4250 remove_upattern (param, pattern, op)
4251 char *param, *pattern;
4252 int op;
4253 {
4254 register size_t len;
4255 register char *end;
4256 register char *p, *ret, c;
4257
4258 len = STRLEN (param);
4259 end = param + len;
4260
4261 switch (op)
4262 {
4263 case RP_LONG_LEFT: /* remove longest match at start */
4264 for (p = end; p >= param; p--)
4265 {
4266 c = *p; *p = '\0';
4267 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4268 {
4269 *p = c;
4270 return (savestring (p));
4271 }
4272 *p = c;
4273
4274 }
4275 break;
4276
4277 case RP_SHORT_LEFT: /* remove shortest match at start */
4278 for (p = param; p <= end; p++)
4279 {
4280 c = *p; *p = '\0';
4281 if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4282 {
4283 *p = c;
4284 return (savestring (p));
4285 }
4286 *p = c;
4287 }
4288 break;
4289
4290 case RP_LONG_RIGHT: /* remove longest match at end */
4291 for (p = param; p <= end; p++)
4292 {
4293 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4294 {
4295 c = *p; *p = '\0';
4296 ret = savestring (param);
4297 *p = c;
4298 return (ret);
4299 }
4300 }
4301 break;
4302
4303 case RP_SHORT_RIGHT: /* remove shortest match at end */
4304 for (p = end; p >= param; p--)
4305 {
4306 if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4307 {
4308 c = *p; *p = '\0';
4309 ret = savestring (param);
4310 *p = c;
4311 return (ret);
4312 }
4313 }
4314 break;
4315 }
4316
4317 return (param); /* no match, return original string */
4318 }
4319
4320 #if defined (HANDLE_MULTIBYTE)
4321 /* Returns its first argument if nothing matched; new memory otherwise */
4322 static wchar_t *
4323 remove_wpattern (wparam, wstrlen, wpattern, op)
4324 wchar_t *wparam;
4325 size_t wstrlen;
4326 wchar_t *wpattern;
4327 int op;
4328 {
4329 wchar_t wc, *ret;
4330 int n;
4331
4332 switch (op)
4333 {
4334 case RP_LONG_LEFT: /* remove longest match at start */
4335 for (n = wstrlen; n >= 0; n--)
4336 {
4337 wc = wparam[n]; wparam[n] = L'\0';
4338 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4339 {
4340 wparam[n] = wc;
4341 return (wcsdup (wparam + n));
4342 }
4343 wparam[n] = wc;
4344 }
4345 break;
4346
4347 case RP_SHORT_LEFT: /* remove shortest match at start */
4348 for (n = 0; n <= wstrlen; n++)
4349 {
4350 wc = wparam[n]; wparam[n] = L'\0';
4351 if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4352 {
4353 wparam[n] = wc;
4354 return (wcsdup (wparam + n));
4355 }
4356 wparam[n] = wc;
4357 }
4358 break;
4359
4360 case RP_LONG_RIGHT: /* remove longest match at end */
4361 for (n = 0; n <= wstrlen; n++)
4362 {
4363 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4364 {
4365 wc = wparam[n]; wparam[n] = L'\0';
4366 ret = wcsdup (wparam);
4367 wparam[n] = wc;
4368 return (ret);
4369 }
4370 }
4371 break;
4372
4373 case RP_SHORT_RIGHT: /* remove shortest match at end */
4374 for (n = wstrlen; n >= 0; n--)
4375 {
4376 if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
4377 {
4378 wc = wparam[n]; wparam[n] = L'\0';
4379 ret = wcsdup (wparam);
4380 wparam[n] = wc;
4381 return (ret);
4382 }
4383 }
4384 break;
4385 }
4386
4387 return (wparam); /* no match, return original string */
4388 }
4389 #endif /* HANDLE_MULTIBYTE */
4390
4391 static char *
4392 remove_pattern (param, pattern, op)
4393 char *param, *pattern;
4394 int op;
4395 {
4396 char *xret;
4397
4398 if (param == NULL)
4399 return (param);
4400 if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */
4401 return (savestring (param));
4402
4403 #if defined (HANDLE_MULTIBYTE)
4404 if (MB_CUR_MAX > 1)
4405 {
4406 wchar_t *ret, *oret;
4407 size_t n;
4408 wchar_t *wparam, *wpattern;
4409 mbstate_t ps;
4410
4411 n = xdupmbstowcs (&wpattern, NULL, pattern);
4412 if (n == (size_t)-1)
4413 {
4414 xret = remove_upattern (param, pattern, op);
4415 return ((xret == param) ? savestring (param) : xret);
4416 }
4417 n = xdupmbstowcs (&wparam, NULL, param);
4418
4419 if (n == (size_t)-1)
4420 {
4421 free (wpattern);
4422 xret = remove_upattern (param, pattern, op);
4423 return ((xret == param) ? savestring (param) : xret);
4424 }
4425 oret = ret = remove_wpattern (wparam, n, wpattern, op);
4426 /* Don't bother to convert wparam back to multibyte string if nothing
4427 matched; just return copy of original string */
4428 if (ret == wparam)
4429 {
4430 free (wparam);
4431 free (wpattern);
4432 return (savestring (param));
4433 }
4434
4435 free (wparam);
4436 free (wpattern);
4437
4438 n = strlen (param);
4439 xret = (char *)xmalloc (n + 1);
4440 memset (&ps, '\0', sizeof (mbstate_t));
4441 n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps);
4442 xret[n] = '\0'; /* just to make sure */
4443 free (oret);
4444 return xret;
4445 }
4446 else
4447 #endif
4448 {
4449 xret = remove_upattern (param, pattern, op);
4450 return ((xret == param) ? savestring (param) : xret);
4451 }
4452 }
4453
4454 /* Match PAT anywhere in STRING and return the match boundaries.
4455 This returns 1 in case of a successful match, 0 otherwise. SP
4456 and EP are pointers into the string where the match begins and
4457 ends, respectively. MTYPE controls what kind of match is attempted.
4458 MATCH_BEG and MATCH_END anchor the match at the beginning and end
4459 of the string, respectively. The longest match is returned. */
4460 static int
4461 match_upattern (string, pat, mtype, sp, ep)
4462 char *string, *pat;
4463 int mtype;
4464 char **sp, **ep;
4465 {
4466 int c, mlen;
4467 size_t len;
4468 register char *p, *p1, *npat;
4469 char *end;
4470 int n1;
4471
4472 /* If the pattern doesn't match anywhere in the string, go ahead and
4473 short-circuit right away. A minor optimization, saves a bunch of
4474 unnecessary calls to strmatch (up to N calls for a string of N
4475 characters) if the match is unsuccessful. To preserve the semantics
4476 of the substring matches below, we make sure that the pattern has
4477 `*' as first and last character, making a new pattern if necessary. */
4478 /* XXX - check this later if I ever implement `**' with special meaning,
4479 since this will potentially result in `**' at the beginning or end */
4480 len = STRLEN (pat);
4481 if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*')
4482 {
4483 int unescaped_backslash;
4484 char *pp;
4485
4486 p = npat = (char *)xmalloc (len + 3);
4487 p1 = pat;
4488 if (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob))
4489 *p++ = '*';
4490 while (*p1)
4491 *p++ = *p1++;
4492 #if 1
4493 /* Need to also handle a pattern that ends with an unescaped backslash.
4494 For right now, we ignore it because the pattern matching code will
4495 fail the match anyway */
4496 /* If the pattern ends with a `*' we leave it alone if it's preceded by
4497 an even number of backslashes, but if it's escaped by a backslash
4498 we need to add another `*'. */
4499 if (p1[-1] == '*' && (unescaped_backslash = p1[-2] == '\\'))
4500 {
4501 pp = p1 - 3;
4502 while (pp >= pat && *pp-- == '\\')
4503 unescaped_backslash = 1 - unescaped_backslash;
4504 if (unescaped_backslash)
4505 *p++ = '*';
4506 }
4507 else if (p1[-1] != '*')
4508 *p++ = '*';
4509 #else
4510 if (p1[-1] != '*' || p1[-2] == '\\')
4511 *p++ = '*';
4512 #endif
4513 *p = '\0';
4514 }
4515 else
4516 npat = pat;
4517 c = strmatch (npat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE);
4518 if (npat != pat)
4519 free (npat);
4520 if (c == FNM_NOMATCH)
4521 return (0);
4522
4523 len = STRLEN (string);
4524 end = string + len;
4525
4526 mlen = umatchlen (pat, len);
4527
4528 switch (mtype)
4529 {
4530 case MATCH_ANY:
4531 for (p = string; p <= end; p++)
4532 {
4533 if (match_pattern_char (pat, p, FNMATCH_IGNCASE))
4534 {
4535 p1 = (mlen == -1) ? end : p + mlen;
4536 /* p1 - p = length of portion of string to be considered
4537 p = current position in string
4538 mlen = number of characters consumed by match (-1 for entire string)
4539 end = end of string
4540 we want to break immediately if the potential match len
4541 is greater than the number of characters remaining in the
4542 string
4543 */
4544 if (p1 > end)
4545 break;
4546 for ( ; p1 >= p; p1--)
4547 {
4548 c = *p1; *p1 = '\0';
4549 if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
4550 {
4551 *p1 = c;
4552 *sp = p;
4553 *ep = p1;
4554 return 1;
4555 }
4556 *p1 = c;
4557 #if 1
4558 /* If MLEN != -1, we have a fixed length pattern. */
4559 if (mlen != -1)
4560 break;
4561 #endif
4562 }
4563 }
4564 }
4565
4566 return (0);
4567
4568 case MATCH_BEG:
4569 if (match_pattern_char (pat, string, FNMATCH_IGNCASE) == 0)
4570 return (0);
4571
4572 for (p = (mlen == -1) ? end : string + mlen; p >= string; p--)
4573 {
4574 c = *p; *p = '\0';
4575 if (strmatch (pat, string, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
4576 {
4577 *p = c;
4578 *sp = string;
4579 *ep = p;
4580 return 1;
4581 }
4582 *p = c;
4583 /* If MLEN != -1, we have a fixed length pattern. */
4584 if (mlen != -1)
4585 break;
4586 }
4587
4588 return (0);
4589
4590 case MATCH_END:
4591 for (p = end - ((mlen == -1) ? len : mlen); p <= end; p++)
4592 {
4593 if (strmatch (pat, p, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
4594 {
4595 *sp = p;
4596 *ep = end;
4597 return 1;
4598 }
4599 /* If MLEN != -1, we have a fixed length pattern. */
4600 if (mlen != -1)
4601 break;
4602 }
4603
4604 return (0);
4605 }
4606
4607 return (0);
4608 }
4609
4610 #if defined (HANDLE_MULTIBYTE)
4611
4612 #define WFOLD(c) (match_ignore_case && iswupper (c) ? towlower (c) : (c))
4613
4614 /* Match WPAT anywhere in WSTRING and return the match boundaries.
4615 This returns 1 in case of a successful match, 0 otherwise. Wide
4616 character version. */
4617 static int
4618 match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
4619 wchar_t *wstring;
4620 char **indices;
4621 size_t wstrlen;
4622 wchar_t *wpat;
4623 int mtype;
4624 char **sp, **ep;
4625 {
4626 wchar_t wc, *wp, *nwpat, *wp1;
4627 size_t len;
4628 int mlen;
4629 int n, n1, n2, simple;
4630
4631 simple = (wpat[0] != L'\\' && wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'[');
4632 #if defined (EXTENDED_GLOB)
4633 if (extended_glob)
4634 simple &= (wpat[1] != L'(' || (wpat[0] != L'*' && wpat[0] != L'?' && wpat[0] != L'+' && wpat[0] != L'!' && wpat[0] != L'@')); /*)*/
4635 #endif
4636
4637 /* If the pattern doesn't match anywhere in the string, go ahead and
4638 short-circuit right away. A minor optimization, saves a bunch of
4639 unnecessary calls to strmatch (up to N calls for a string of N
4640 characters) if the match is unsuccessful. To preserve the semantics
4641 of the substring matches below, we make sure that the pattern has
4642 `*' as first and last character, making a new pattern if necessary. */
4643 len = wcslen (wpat);
4644 if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*')
4645 {
4646 int unescaped_backslash;
4647 wchar_t *wpp;
4648
4649 wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t));
4650 wp1 = wpat;
4651 if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob))
4652 *wp++ = L'*';
4653 while (*wp1 != L'\0')
4654 *wp++ = *wp1++;
4655 #if 1
4656 /* See comments above in match_upattern. */
4657 if (wp1[-1] == L'*' && (unescaped_backslash = wp1[-2] == L'\\'))
4658 {
4659 wpp = wp1 - 3;
4660 while (wpp >= wpat && *wpp-- == L'\\')
4661 unescaped_backslash = 1 - unescaped_backslash;
4662 if (unescaped_backslash)
4663 *wp++ = L'*';
4664 }
4665 else if (wp1[-1] != L'*')
4666 *wp++ = L'*';
4667 #else
4668 if (wp1[-1] != L'*' || wp1[-2] == L'\\')
4669 *wp++ = L'*';
4670 #endif
4671 *wp = '\0';
4672 }
4673 else
4674 nwpat = wpat;
4675 len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE);
4676 if (nwpat != wpat)
4677 free (nwpat);
4678 if (len == FNM_NOMATCH)
4679 return (0);
4680
4681 mlen = wmatchlen (wpat, wstrlen);
4682
4683 /* itrace("wmatchlen (%ls) -> %d", wpat, mlen); */
4684 switch (mtype)
4685 {
4686 case MATCH_ANY:
4687 for (n = 0; n <= wstrlen; n++)
4688 {
4689 n2 = simple ? (WFOLD(*wpat) == WFOLD(wstring[n])) : match_pattern_wchar (wpat, wstring + n, FNMATCH_IGNCASE);
4690 if (n2)
4691 {
4692 n1 = (mlen == -1) ? wstrlen : n + mlen;
4693 if (n1 > wstrlen)
4694 break;
4695
4696 for ( ; n1 >= n; n1--)
4697 {
4698 wc = wstring[n1]; wstring[n1] = L'\0';
4699 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
4700 {
4701 wstring[n1] = wc;
4702 *sp = indices[n];
4703 *ep = indices[n1];
4704 return 1;
4705 }
4706 wstring[n1] = wc;
4707 /* If MLEN != -1, we have a fixed length pattern. */
4708 if (mlen != -1)
4709 break;
4710 }
4711 }
4712 }
4713
4714 return (0);
4715
4716 case MATCH_BEG:
4717 if (match_pattern_wchar (wpat, wstring, FNMATCH_IGNCASE) == 0)
4718 return (0);
4719
4720 for (n = (mlen == -1) ? wstrlen : mlen; n >= 0; n--)
4721 {
4722 wc = wstring[n]; wstring[n] = L'\0';
4723 if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
4724 {
4725 wstring[n] = wc;
4726 *sp = indices[0];
4727 *ep = indices[n];
4728 return 1;
4729 }
4730 wstring[n] = wc;
4731 /* If MLEN != -1, we have a fixed length pattern. */
4732 if (mlen != -1)
4733 break;
4734 }
4735
4736 return (0);
4737
4738 case MATCH_END:
4739 for (n = wstrlen - ((mlen == -1) ? wstrlen : mlen); n <= wstrlen; n++)
4740 {
4741 if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG | FNMATCH_IGNCASE) == 0)
4742 {
4743 *sp = indices[n];
4744 *ep = indices[wstrlen];
4745 return 1;
4746 }
4747 /* If MLEN != -1, we have a fixed length pattern. */
4748 if (mlen != -1)
4749 break;
4750 }
4751
4752 return (0);
4753 }
4754
4755 return (0);
4756 }
4757 #undef WFOLD
4758 #endif /* HANDLE_MULTIBYTE */
4759
4760 static int
4761 match_pattern (string, pat, mtype, sp, ep)
4762 char *string, *pat;
4763 int mtype;
4764 char **sp, **ep;
4765 {
4766 #if defined (HANDLE_MULTIBYTE)
4767 int ret;
4768 size_t n;
4769 wchar_t *wstring, *wpat;
4770 char **indices;
4771 size_t slen, plen, mslen, mplen;
4772 #endif
4773
4774 if (string == 0 || pat == 0 || *pat == 0)
4775 return (0);
4776
4777 #if defined (HANDLE_MULTIBYTE)
4778 if (MB_CUR_MAX > 1)
4779 {
4780 if (mbsmbchar (string) == 0 && mbsmbchar (pat) == 0)
4781 return (match_upattern (string, pat, mtype, sp, ep));
4782
4783 n = xdupmbstowcs (&wpat, NULL, pat);
4784 if (n == (size_t)-1)
4785 return (match_upattern (string, pat, mtype, sp, ep));
4786 n = xdupmbstowcs (&wstring, &indices, string);
4787 if (n == (size_t)-1)
4788 {
4789 free (wpat);
4790 return (match_upattern (string, pat, mtype, sp, ep));
4791 }
4792 ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep);
4793
4794 free (wpat);
4795 free (wstring);
4796 free (indices);
4797
4798 return (ret);
4799 }
4800 else
4801 #endif
4802 return (match_upattern (string, pat, mtype, sp, ep));
4803 }
4804
4805 static int
4806 getpatspec (c, value)
4807 int c;
4808 char *value;
4809 {
4810 if (c == '#')
4811 return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
4812 else /* c == '%' */
4813 return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
4814 }
4815
4816 /* Posix.2 says that the WORD should be run through tilde expansion,
4817 parameter expansion, command substitution and arithmetic expansion.
4818 This leaves the result quoted, so quote_string_for_globbing () has
4819 to be called to fix it up for strmatch (). If QUOTED is non-zero,
4820 it means that the entire expression was enclosed in double quotes.
4821 This means that quoting characters in the pattern do not make any
4822 special pattern characters quoted. For example, the `*' in the
4823 following retains its special meaning: "${foo#'*'}". */
4824 static char *
4825 getpattern (value, quoted, expandpat)
4826 char *value;
4827 int quoted, expandpat;
4828 {
4829 char *pat, *tword;
4830 WORD_LIST *l;
4831 #if 0
4832 int i;
4833 #endif
4834 /* There is a problem here: how to handle single or double quotes in the
4835 pattern string when the whole expression is between double quotes?
4836 POSIX.2 says that enclosing double quotes do not cause the pattern to
4837 be quoted, but does that leave us a problem with @ and array[@] and their
4838 expansions inside a pattern? */
4839 #if 0
4840 if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
4841 {
4842 i = 0;
4843 pat = string_extract_double_quoted (tword, &i, SX_STRIPDQ);
4844 free (tword);
4845 tword = pat;
4846 }
4847 #endif
4848
4849 /* expand_string_for_rhs () leaves WORD quoted and does not perform
4850 word splitting. */
4851 l = *value ? expand_string_for_rhs (value,
4852 (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
4853 (int *)NULL, (int *)NULL)
4854 : (WORD_LIST *)0;
4855 pat = string_list (l);
4856 dispose_words (l);
4857 if (pat)
4858 {
4859 tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
4860 free (pat);
4861 pat = tword;
4862 }
4863 return (pat);
4864 }
4865
4866 #if 0
4867 /* Handle removing a pattern from a string as a result of ${name%[%]value}
4868 or ${name#[#]value}. */
4869 static char *
4870 variable_remove_pattern (value, pattern, patspec, quoted)
4871 char *value, *pattern;
4872 int patspec, quoted;
4873 {
4874 char *tword;
4875
4876 tword = remove_pattern (value, pattern, patspec);
4877
4878 return (tword);
4879 }
4880 #endif
4881
4882 static char *
4883 list_remove_pattern (list, pattern, patspec, itype, quoted)
4884 WORD_LIST *list;
4885 char *pattern;
4886 int patspec, itype, quoted;
4887 {
4888 WORD_LIST *new, *l;
4889 WORD_DESC *w;
4890 char *tword;
4891
4892 for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
4893 {
4894 tword = remove_pattern (l->word->word, pattern, patspec);
4895 w = alloc_word_desc ();
4896 w->word = tword ? tword : savestring ("");
4897 new = make_word_list (w, new);
4898 }
4899
4900 l = REVERSE_LIST (new, WORD_LIST *);
4901 tword = string_list_pos_params (itype, l, quoted);
4902 dispose_words (l);
4903
4904 return (tword);
4905 }
4906
4907 static char *
4908 parameter_list_remove_pattern (itype, pattern, patspec, quoted)
4909 int itype;
4910 char *pattern;
4911 int patspec, quoted;
4912 {
4913 char *ret;
4914 WORD_LIST *list;
4915
4916 list = list_rest_of_args ();
4917 if (list == 0)
4918 return ((char *)NULL);
4919 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
4920 dispose_words (list);
4921 return (ret);
4922 }
4923
4924 #if defined (ARRAY_VARS)
4925 static char *
4926 array_remove_pattern (var, pattern, patspec, varname, quoted)
4927 SHELL_VAR *var;
4928 char *pattern;
4929 int patspec;
4930 char *varname; /* so we can figure out how it's indexed */
4931 int quoted;
4932 {
4933 ARRAY *a;
4934 HASH_TABLE *h;
4935 int itype;
4936 char *ret;
4937 WORD_LIST *list;
4938 SHELL_VAR *v;
4939
4940 /* compute itype from varname here */
4941 v = array_variable_part (varname, &ret, 0);
4942
4943 /* XXX */
4944 if (v && invisible_p (v))
4945 return ((char *)NULL);
4946
4947 itype = ret[0];
4948
4949 a = (v && array_p (v)) ? array_cell (v) : 0;
4950 h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
4951
4952 list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
4953 if (list == 0)
4954 return ((char *)NULL);
4955 ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
4956 dispose_words (list);
4957
4958 return ret;
4959 }
4960 #endif /* ARRAY_VARS */
4961
4962 static char *
4963 parameter_brace_remove_pattern (varname, value, ind, patstr, rtype, quoted, flags)
4964 char *varname, *value;
4965 int ind;
4966 char *patstr;
4967 int rtype, quoted, flags;
4968 {
4969 int vtype, patspec, starsub;
4970 char *temp1, *val, *pattern;
4971 SHELL_VAR *v;
4972
4973 if (value == 0)
4974 return ((char *)NULL);
4975
4976 this_command_name = varname;
4977
4978 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
4979 if (vtype == -1)
4980 return ((char *)NULL);
4981
4982 starsub = vtype & VT_STARSUB;
4983 vtype &= ~VT_STARSUB;
4984
4985 patspec = getpatspec (rtype, patstr);
4986 if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
4987 patstr++;
4988
4989 /* Need to pass getpattern newly-allocated memory in case of expansion --
4990 the expansion code will free the passed string on an error. */
4991 temp1 = savestring (patstr);
4992 pattern = getpattern (temp1, quoted, 1);
4993 free (temp1);
4994
4995 temp1 = (char *)NULL; /* shut up gcc */
4996 switch (vtype)
4997 {
4998 case VT_VARIABLE:
4999 case VT_ARRAYMEMBER:
5000 temp1 = remove_pattern (val, pattern, patspec);
5001 if (vtype == VT_VARIABLE)
5002 FREE (val);
5003 if (temp1)
5004 {
5005 val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
5006 ? quote_string (temp1)
5007 : quote_escapes (temp1);
5008 free (temp1);
5009 temp1 = val;
5010 }
5011 break;
5012 #if defined (ARRAY_VARS)
5013 case VT_ARRAYVAR:
5014 temp1 = array_remove_pattern (v, pattern, patspec, varname, quoted);
5015 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
5016 {
5017 val = quote_escapes (temp1);
5018 free (temp1);
5019 temp1 = val;
5020 }
5021 break;
5022 #endif
5023 case VT_POSPARMS:
5024 temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
5025 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
5026 {
5027 val = quote_escapes (temp1);
5028 free (temp1);
5029 temp1 = val;
5030 }
5031 break;
5032 }
5033
5034 FREE (pattern);
5035 return temp1;
5036 }
5037
5038 static char *
5039 string_var_assignment (v, s)
5040 SHELL_VAR *v;
5041 char *s;
5042 {
5043 char flags[MAX_ATTRIBUTES], *ret, *val;
5044 int i;
5045
5046 val = sh_quote_reusable (s, 0);
5047 i = var_attribute_string (v, 0, flags);
5048 ret = (char *)xmalloc (i + strlen (val) + strlen (v->name) + 16 + MAX_ATTRIBUTES);
5049 if (i > 0)
5050 sprintf (ret, "declare -%s %s=%s", flags, v->name, val);
5051 else
5052 sprintf (ret, "%s=%s", v->name, val);
5053 free (val);
5054 return ret;
5055 }
5056
5057 #if defined (ARRAY_VARS)
5058 static char *
5059 array_var_assignment (v, itype, quoted)
5060 SHELL_VAR *v;
5061 int itype, quoted;
5062 {
5063 char *ret, *val, flags[MAX_ATTRIBUTES];
5064 int i;
5065
5066 if (v == 0)
5067 return (char *)NULL;
5068 val = array_p (v) ? array_to_assign (array_cell (v), 0)
5069 : assoc_to_assign (assoc_cell (v), 0);
5070 if (val == 0)
5071 {
5072 val = (char *)xmalloc (3);
5073 val[0] = '(';
5074 val[1] = ')';
5075 val[2] = 0;
5076 }
5077 else
5078 {
5079 ret = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) ? quote_string (val) : quote_escapes (val);
5080 free (val);
5081 val = ret;
5082 }
5083 i = var_attribute_string (v, 0, flags);
5084 ret = (char *)xmalloc (i + strlen (val) + strlen (v->name) + 16);
5085 sprintf (ret, "declare -%s %s=%s", flags, v->name, val);
5086 free (val);
5087 return ret;
5088 }
5089 #endif
5090
5091 static char *
5092 pos_params_assignment (list, itype, quoted)
5093 WORD_LIST *list;
5094 int itype;
5095 int quoted;
5096 {
5097 char *temp, *ret;
5098
5099 /* first, we transform the list to quote each word. */
5100 temp = list_transform ('Q', (SHELL_VAR *)0, list, itype, quoted);
5101 ret = (char *)xmalloc (strlen (temp) + 8);
5102 strcpy (ret, "set -- ");
5103 strcpy (ret + 7, temp);
5104 free (temp);
5105 return ret;
5106 }
5107
5108 static char *
5109 string_transform (xc, v, s)
5110 int xc;
5111 SHELL_VAR *v;
5112 char *s;
5113 {
5114 char *ret, flags[MAX_ATTRIBUTES];
5115 int i;
5116
5117 if (((xc == 'A' || xc == 'a') && v == 0) || (xc != 'a' && s == 0))
5118 return (char *)NULL;
5119
5120 switch (xc)
5121 {
5122 /* Transformations that interrogate the variable */
5123 case 'a':
5124 i = var_attribute_string (v, 0, flags);
5125 ret = (i > 0) ? savestring (flags) : (char *)NULL;
5126 break;
5127 case 'A':
5128 ret = string_var_assignment (v, s);
5129 break;
5130 /* Transformations that modify the variable's value */
5131 case 'E':
5132 ret = ansiexpand (s, 0, strlen (s), (int *)0);
5133 break;
5134 case 'P':
5135 ret = decode_prompt_string (s);
5136 break;
5137 case 'Q':
5138 ret = sh_quote_reusable (s, 0);
5139 break;
5140 default:
5141 ret = (char *)NULL;
5142 break;
5143 }
5144 return ret;
5145 }
5146
5147 static char *
5148 list_transform (xc, v, list, itype, quoted)
5149 int xc;
5150 SHELL_VAR *v;
5151 WORD_LIST *list;
5152 int itype, quoted;
5153 {
5154 WORD_LIST *new, *l;
5155 WORD_DESC *w;
5156 char *tword;
5157
5158 for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
5159 {
5160 tword = string_transform (xc, v, l->word->word);
5161 w = alloc_word_desc ();
5162 w->word = tword ? tword : savestring (""); /* XXX */
5163 new = make_word_list (w, new);
5164 }
5165
5166 l = REVERSE_LIST (new, WORD_LIST *);
5167 tword = string_list_pos_params (itype, l, quoted);
5168 dispose_words (l);
5169
5170 return (tword);
5171 }
5172
5173 static char *
5174 parameter_list_transform (xc, itype, quoted)
5175 int xc;
5176 int itype;
5177 int quoted;
5178 {
5179 char *ret;
5180 WORD_LIST *list;
5181
5182 list = list_rest_of_args ();
5183 if (list == 0)
5184 return ((char *)NULL);
5185 if (xc == 'A')
5186 return (pos_params_assignment (list, itype, quoted));
5187 ret = list_transform (xc, (SHELL_VAR *)0, list, itype, quoted);
5188 dispose_words (list);
5189 return (ret);
5190 }
5191
5192 #if defined (ARRAY_VARS)
5193 static char *
5194 array_transform (xc, var, varname, quoted)
5195 int xc;
5196 SHELL_VAR *var;
5197 char *varname; /* so we can figure out how it's indexed */
5198 int quoted;
5199 {
5200 ARRAY *a;
5201 HASH_TABLE *h;
5202 int itype;
5203 char *ret;
5204 WORD_LIST *list;
5205 SHELL_VAR *v;
5206
5207 /* compute itype from varname here */
5208 v = array_variable_part (varname, &ret, 0);
5209
5210 /* XXX */
5211 if (v && invisible_p (v))
5212 return ((char *)NULL);
5213
5214 itype = ret[0];
5215
5216 if (xc == 'A')
5217 return (array_var_assignment (v, itype, quoted));
5218
5219 a = (v && array_p (v)) ? array_cell (v) : 0;
5220 h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
5221
5222 list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
5223 if (list == 0)
5224 return ((char *)NULL);
5225 ret = list_transform (xc, v, list, itype, quoted);
5226 dispose_words (list);
5227
5228 return ret;
5229 }
5230 #endif /* ARRAY_VARS */
5231
5232 static char *
5233 parameter_brace_transform (varname, value, ind, xform, rtype, quoted, flags)
5234 char *varname, *value;
5235 int ind;
5236 char *xform;
5237 int rtype, quoted, flags;
5238 {
5239 int vtype, xc;
5240 char *temp1, *val;
5241 SHELL_VAR *v;
5242
5243 xc = xform[0];
5244 if (value == 0 && xc != 'A' && xc != 'a')
5245 return ((char *)NULL);
5246
5247 this_command_name = varname;
5248
5249 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
5250 if (vtype == -1)
5251 return ((char *)NULL);
5252
5253 /* check for valid values of xc */
5254 switch (xc)
5255 {
5256 case 'a': /* expand to a string with just attributes */
5257 case 'A': /* expand as an assignment statement with attributes */
5258 case 'E': /* expand like $'...' */
5259 case 'P': /* expand like prompt string */
5260 case 'Q': /* quote reusably */
5261 break;
5262 default:
5263 return &expand_param_error;
5264 }
5265
5266 temp1 = (char *)NULL; /* shut up gcc */
5267 switch (vtype & ~VT_STARSUB)
5268 {
5269 case VT_VARIABLE:
5270 case VT_ARRAYMEMBER:
5271 temp1 = string_transform (xc, v, val);
5272 if (vtype == VT_VARIABLE)
5273 FREE (val);
5274 if (temp1)
5275 {
5276 val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
5277 ? quote_string (temp1)
5278 : quote_escapes (temp1);
5279 free (temp1);
5280 temp1 = val;
5281 }
5282 break;
5283 #if defined (ARRAY_VARS)
5284 case VT_ARRAYVAR:
5285 temp1 = array_transform (xc, v, varname, quoted);
5286 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
5287 {
5288 val = quote_escapes (temp1);
5289 free (temp1);
5290 temp1 = val;
5291 }
5292 break;
5293 #endif
5294 case VT_POSPARMS:
5295 temp1 = parameter_list_transform (xc, varname[0], quoted);
5296 if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
5297 {
5298 val = quote_escapes (temp1);
5299 free (temp1);
5300 temp1 = val;
5301 }
5302 break;
5303 }
5304
5305 return temp1;
5306 }
5307
5308 /*******************************************
5309 * *
5310 * Functions to expand WORD_DESCs *
5311 * *
5312 *******************************************/
5313
5314 /* Expand WORD, performing word splitting on the result. This does
5315 parameter expansion, command substitution, arithmetic expansion,
5316 word splitting, and quote removal. */
5317
5318 WORD_LIST *
5319 expand_word (word, quoted)
5320 WORD_DESC *word;
5321 int quoted;
5322 {
5323 WORD_LIST *result, *tresult;
5324
5325 tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
5326 result = word_list_split (tresult);
5327 dispose_words (tresult);
5328 return (result ? dequote_list (result) : result);
5329 }
5330
5331 /* Expand WORD, but do not perform word splitting on the result. This
5332 does parameter expansion, command substitution, arithmetic expansion,
5333 and quote removal. */
5334 WORD_LIST *
5335 expand_word_unsplit (word, quoted)
5336 WORD_DESC *word;
5337 int quoted;
5338 {
5339 WORD_LIST *result;
5340
5341 expand_no_split_dollar_star = 1;
5342 #if defined (HANDLE_MULTIBYTE)
5343 if (ifs_firstc[0] == 0)
5344 #else
5345 if (ifs_firstc == 0)
5346 #endif
5347 word->flags |= W_NOSPLIT;
5348 word->flags |= W_NOSPLIT2;
5349 result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
5350 expand_no_split_dollar_star = 0;
5351
5352 return (result ? dequote_list (result) : result);
5353 }
5354
5355 /* Perform shell expansions on WORD, but do not perform word splitting or
5356 quote removal on the result. Virtually identical to expand_word_unsplit;
5357 could be combined if implementations don't diverge. */
5358 WORD_LIST *
5359 expand_word_leave_quoted (word, quoted)
5360 WORD_DESC *word;
5361 int quoted;
5362 {
5363 WORD_LIST *result;
5364
5365 expand_no_split_dollar_star = 1;
5366 #if defined (HANDLE_MULTIBYTE)
5367 if (ifs_firstc[0] == 0)
5368 #else
5369 if (ifs_firstc == 0)
5370 #endif
5371 word->flags |= W_NOSPLIT;
5372 word->flags |= W_NOSPLIT2;
5373 result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
5374 expand_no_split_dollar_star = 0;
5375
5376 return result;
5377 }
5378
5379 #if defined (PROCESS_SUBSTITUTION)
5380
5381 /*****************************************************************/
5382 /* */
5383 /* Hacking Process Substitution */
5384 /* */
5385 /*****************************************************************/
5386
5387 #if !defined (HAVE_DEV_FD)
5388 /* Named pipes must be removed explicitly with `unlink'. This keeps a list
5389 of FIFOs the shell has open. unlink_fifo_list will walk the list and
5390 unlink all of them. add_fifo_list adds the name of an open FIFO to the
5391 list. NFIFO is a count of the number of FIFOs in the list. */
5392 #define FIFO_INCR 20
5393
5394 struct temp_fifo {
5395 char *file;
5396 pid_t proc;
5397 };
5398
5399 static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
5400 static int nfifo;
5401 static int fifo_list_size;
5402
5403 void
5404 clear_fifo_list ()
5405 {
5406 }
5407
5408 char *
5409 copy_fifo_list (sizep)
5410 int *sizep;
5411 {
5412 if (sizep)
5413 *sizep = 0;
5414 return (char *)NULL;
5415 }
5416
5417 static void
5418 add_fifo_list (pathname)
5419 char *pathname;
5420 {
5421 if (nfifo >= fifo_list_size - 1)
5422 {
5423 fifo_list_size += FIFO_INCR;
5424 fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
5425 fifo_list_size * sizeof (struct temp_fifo));
5426 }
5427
5428 fifo_list[nfifo].file = savestring (pathname);
5429 nfifo++;
5430 }
5431
5432 void
5433 unlink_fifo (i)
5434 int i;
5435 {
5436 if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
5437 {
5438 unlink (fifo_list[i].file);
5439 free (fifo_list[i].file);
5440 fifo_list[i].file = (char *)NULL;
5441 fifo_list[i].proc = -1;
5442 }
5443 }
5444
5445 void
5446 unlink_fifo_list ()
5447 {
5448 int saved, i, j;
5449
5450 if (nfifo == 0)
5451 return;
5452
5453 for (i = saved = 0; i < nfifo; i++)
5454 {
5455 if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
5456 {
5457 unlink (fifo_list[i].file);
5458 free (fifo_list[i].file);
5459 fifo_list[i].file = (char *)NULL;
5460 fifo_list[i].proc = -1;
5461 }
5462 else
5463 saved++;
5464 }
5465
5466 /* If we didn't remove some of the FIFOs, compact the list. */
5467 if (saved)
5468 {
5469 for (i = j = 0; i < nfifo; i++)
5470 if (fifo_list[i].file)
5471 {
5472 fifo_list[j].file = fifo_list[i].file;
5473 fifo_list[j].proc = fifo_list[i].proc;
5474 j++;
5475 }
5476 nfifo = j;
5477 }
5478 else
5479 nfifo = 0;
5480 }
5481
5482 /* Take LIST, which is a bitmap denoting active FIFOs in fifo_list
5483 from some point in the past, and close all open FIFOs in fifo_list
5484 that are not marked as active in LIST. If LIST is NULL, close
5485 everything in fifo_list. LSIZE is the number of elements in LIST, in
5486 case it's larger than fifo_list_size (size of fifo_list). */
5487 void
5488 close_new_fifos (list, lsize)
5489 char *list;
5490 int lsize;
5491 {
5492 int i;
5493
5494 if (list == 0)
5495 {
5496 unlink_fifo_list ();
5497 return;
5498 }
5499
5500 for (i = 0; i < lsize; i++)
5501 if (list[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1)
5502 unlink_fifo (i);
5503
5504 for (i = lsize; i < fifo_list_size; i++)
5505 unlink_fifo (i);
5506 }
5507
5508 int
5509 fifos_pending ()
5510 {
5511 return nfifo;
5512 }
5513
5514 int
5515 num_fifos ()
5516 {
5517 return nfifo;
5518 }
5519
5520 static char *
5521 make_named_pipe ()
5522 {
5523 char *tname;
5524
5525 tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR);
5526 if (mkfifo (tname, 0600) < 0)
5527 {
5528 free (tname);
5529 return ((char *)NULL);
5530 }
5531
5532 add_fifo_list (tname);
5533 return (tname);
5534 }
5535
5536 #else /* HAVE_DEV_FD */
5537
5538 /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
5539 has open to children. NFDS is a count of the number of bits currently
5540 set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
5541 of open files. */
5542 static char *dev_fd_list = (char *)NULL;
5543 static int nfds;
5544 static int totfds; /* The highest possible number of open files. */
5545
5546 void
5547 clear_fifo (i)
5548 int i;
5549 {
5550 if (dev_fd_list[i])
5551 {
5552 dev_fd_list[i] = 0;
5553 nfds--;
5554 }
5555 }
5556
5557 void
5558 clear_fifo_list ()
5559 {
5560 register int i;
5561
5562 if (nfds == 0)
5563 return;
5564
5565 for (i = 0; nfds && i < totfds; i++)
5566 clear_fifo (i);
5567
5568 nfds = 0;
5569 }
5570
5571 char *
5572 copy_fifo_list (sizep)
5573 int *sizep;
5574 {
5575 char *ret;
5576
5577 if (nfds == 0 || totfds == 0)
5578 {
5579 if (sizep)
5580 *sizep = 0;
5581 return (char *)NULL;
5582 }
5583
5584 if (sizep)
5585 *sizep = totfds;
5586 ret = (char *)xmalloc (totfds);
5587 return (memcpy (ret, dev_fd_list, totfds));
5588 }
5589
5590 static void
5591 add_fifo_list (fd)
5592 int fd;
5593 {
5594 if (dev_fd_list == 0 || fd >= totfds)
5595 {
5596 int ofds;
5597
5598 ofds = totfds;
5599 totfds = getdtablesize ();
5600 if (totfds < 0 || totfds > 256)
5601 totfds = 256;
5602 if (fd >= totfds)
5603 totfds = fd + 2;
5604
5605 dev_fd_list = (char *)xrealloc (dev_fd_list, totfds);
5606 memset (dev_fd_list + ofds, '\0', totfds - ofds);
5607 }
5608
5609 dev_fd_list[fd] = 1;
5610 nfds++;
5611 }
5612
5613 int
5614 fifos_pending ()
5615 {
5616 return 0; /* used for cleanup; not needed with /dev/fd */
5617 }
5618
5619 int
5620 num_fifos ()
5621 {
5622 return nfds;
5623 }
5624
5625 void
5626 unlink_fifo (fd)
5627 int fd;
5628 {
5629 if (dev_fd_list[fd])
5630 {
5631 close (fd);
5632 dev_fd_list[fd] = 0;
5633 nfds--;
5634 }
5635 }
5636
5637 void
5638 unlink_fifo_list ()
5639 {
5640 register int i;
5641
5642 if (nfds == 0)
5643 return;
5644
5645 for (i = 0; nfds && i < totfds; i++)
5646 unlink_fifo (i);
5647
5648 nfds = 0;
5649 }
5650
5651 /* Take LIST, which is a snapshot copy of dev_fd_list from some point in
5652 the past, and close all open fds in dev_fd_list that are not marked
5653 as open in LIST. If LIST is NULL, close everything in dev_fd_list.
5654 LSIZE is the number of elements in LIST, in case it's larger than
5655 totfds (size of dev_fd_list). */
5656 void
5657 close_new_fifos (list, lsize)
5658 char *list;
5659 int lsize;
5660 {
5661 int i;
5662
5663 if (list == 0)
5664 {
5665 unlink_fifo_list ();
5666 return;
5667 }
5668
5669 for (i = 0; i < lsize; i++)
5670 if (list[i] == 0 && i < totfds && dev_fd_list[i])
5671 unlink_fifo (i);
5672
5673 for (i = lsize; i < totfds; i++)
5674 unlink_fifo (i);
5675 }
5676
5677 #if defined (NOTDEF)
5678 print_dev_fd_list ()
5679 {
5680 register int i;
5681
5682 fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
5683 fflush (stderr);
5684
5685 for (i = 0; i < totfds; i++)
5686 {
5687 if (dev_fd_list[i])
5688 fprintf (stderr, " %d", i);
5689 }
5690 fprintf (stderr, "\n");
5691 }
5692 #endif /* NOTDEF */
5693
5694 static char *
5695 make_dev_fd_filename (fd)
5696 int fd;
5697 {
5698 char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
5699
5700 ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8);
5701
5702 strcpy (ret, DEV_FD_PREFIX);
5703 p = inttostr (fd, intbuf, sizeof (intbuf));
5704 strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
5705
5706 add_fifo_list (fd);
5707 return (ret);
5708 }
5709
5710 #endif /* HAVE_DEV_FD */
5711
5712 /* Return a filename that will open a connection to the process defined by
5713 executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
5714 a filename in /dev/fd corresponding to a descriptor that is one of the
5715 ends of the pipe. If not defined, we use named pipes on systems that have
5716 them. Systems without /dev/fd and named pipes are out of luck.
5717
5718 OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
5719 use the read end of the pipe and dup that file descriptor to fd 0 in
5720 the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
5721 writing or use the write end of the pipe in the child, and dup that
5722 file descriptor to fd 1 in the child. The parent does the opposite. */
5723
5724 static char *
5725 process_substitute (string, open_for_read_in_child)
5726 char *string;
5727 int open_for_read_in_child;
5728 {
5729 char *pathname;
5730 int fd, result;
5731 pid_t old_pid, pid;
5732 #if defined (HAVE_DEV_FD)
5733 int parent_pipe_fd, child_pipe_fd;
5734 int fildes[2];
5735 #endif /* HAVE_DEV_FD */
5736 #if defined (JOB_CONTROL)
5737 pid_t old_pipeline_pgrp;
5738 #endif
5739
5740 if (!string || !*string || wordexp_only)
5741 return ((char *)NULL);
5742
5743 #if !defined (HAVE_DEV_FD)
5744 pathname = make_named_pipe ();
5745 #else /* HAVE_DEV_FD */
5746 if (pipe (fildes) < 0)
5747 {
5748 sys_error ("%s", _("cannot make pipe for process substitution"));
5749 return ((char *)NULL);
5750 }
5751 /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
5752 the pipe in the parent, otherwise the read end. */
5753 parent_pipe_fd = fildes[open_for_read_in_child];
5754 child_pipe_fd = fildes[1 - open_for_read_in_child];
5755 /* Move the parent end of the pipe to some high file descriptor, to
5756 avoid clashes with FDs used by the script. */
5757 parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
5758
5759 pathname = make_dev_fd_filename (parent_pipe_fd);
5760 #endif /* HAVE_DEV_FD */
5761
5762 if (pathname == 0)
5763 {
5764 sys_error ("%s", _("cannot make pipe for process substitution"));
5765 return ((char *)NULL);
5766 }
5767
5768 old_pid = last_made_pid;
5769
5770 #if defined (JOB_CONTROL)
5771 old_pipeline_pgrp = pipeline_pgrp;
5772 if (pipeline_pgrp == 0 || (subshell_environment & (SUBSHELL_PIPE|SUBSHELL_FORK|SUBSHELL_ASYNC)) == 0)
5773 pipeline_pgrp = shell_pgrp;
5774 save_pipeline (1);
5775 #endif /* JOB_CONTROL */
5776
5777 pid = make_child ((char *)NULL, 1);
5778 if (pid == 0)
5779 {
5780 reset_terminating_signals (); /* XXX */
5781 free_pushed_string_input ();
5782 /* Cancel traps, in trap.c. */
5783 restore_original_signals (); /* XXX - what about special builtins? bash-4.2 */
5784 QUIT; /* catch any interrupts we got post-fork */
5785 setup_async_signals ();
5786 subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB;
5787
5788 /* if we're expanding a redirection, we shouldn't have access to the
5789 temporary environment, but commands in the subshell should have
5790 access to their own temporary environment. */
5791 if (expanding_redir)
5792 flush_temporary_env ();
5793 }
5794
5795 #if defined (JOB_CONTROL)
5796 set_sigchld_handler ();
5797 stop_making_children ();
5798 /* XXX - should we only do this in the parent? (as in command subst) */
5799 pipeline_pgrp = old_pipeline_pgrp;
5800 #else
5801 stop_making_children ();
5802 #endif /* JOB_CONTROL */
5803
5804 if (pid < 0)
5805 {
5806 sys_error ("%s", _("cannot make child for process substitution"));
5807 free (pathname);
5808 #if defined (HAVE_DEV_FD)
5809 close (parent_pipe_fd);
5810 close (child_pipe_fd);
5811 #endif /* HAVE_DEV_FD */
5812 return ((char *)NULL);
5813 }
5814
5815 if (pid > 0)
5816 {
5817 #if defined (JOB_CONTROL)
5818 if (last_procsub_child)
5819 discard_last_procsub_child ();
5820 last_procsub_child = restore_pipeline (0);
5821 #endif
5822
5823 #if !defined (HAVE_DEV_FD)
5824 fifo_list[nfifo-1].proc = pid;
5825 #endif
5826
5827 last_made_pid = old_pid;
5828
5829 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
5830 close_pgrp_pipe ();
5831 #endif /* JOB_CONTROL && PGRP_PIPE */
5832
5833 #if defined (HAVE_DEV_FD)
5834 close (child_pipe_fd);
5835 #endif /* HAVE_DEV_FD */
5836
5837 return (pathname);
5838 }
5839
5840 set_sigint_handler ();
5841
5842 #if defined (JOB_CONTROL)
5843 set_job_control (0);
5844 #endif /* JOB_CONTROL */
5845
5846 #if !defined (HAVE_DEV_FD)
5847 /* Open the named pipe in the child. */
5848 fd = open (pathname, open_for_read_in_child ? O_RDONLY : O_WRONLY);
5849 if (fd < 0)
5850 {
5851 /* Two separate strings for ease of translation. */
5852 if (open_for_read_in_child)
5853 sys_error (_("cannot open named pipe %s for reading"), pathname);
5854 else
5855 sys_error (_("cannot open named pipe %s for writing"), pathname);
5856
5857 exit (127);
5858 }
5859 if (open_for_read_in_child)
5860 {
5861 if (sh_unset_nodelay_mode (fd) < 0)
5862 {
5863 sys_error (_("cannot reset nodelay mode for fd %d"), fd);
5864 exit (127);
5865 }
5866 }
5867 #else /* HAVE_DEV_FD */
5868 fd = child_pipe_fd;
5869 #endif /* HAVE_DEV_FD */
5870
5871 /* Discard buffered stdio output before replacing the underlying file
5872 descriptor. */
5873 if (open_for_read_in_child == 0)
5874 fpurge (stdout);
5875
5876 if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
5877 {
5878 sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname,
5879 open_for_read_in_child ? 0 : 1);
5880 exit (127);
5881 }
5882
5883 if (fd != (open_for_read_in_child ? 0 : 1))
5884 close (fd);
5885
5886 /* Need to close any files that this process has open to pipes inherited
5887 from its parent. */
5888 if (current_fds_to_close)
5889 {
5890 close_fd_bitmap (current_fds_to_close);
5891 current_fds_to_close = (struct fd_bitmap *)NULL;
5892 }
5893
5894 #if defined (HAVE_DEV_FD)
5895 /* Make sure we close the parent's end of the pipe and clear the slot
5896 in the fd list so it is not closed later, if reallocated by, for
5897 instance, pipe(2). */
5898 close (parent_pipe_fd);
5899 dev_fd_list[parent_pipe_fd] = 0;
5900 #endif /* HAVE_DEV_FD */
5901
5902 /* subshells shouldn't have this flag, which controls using the temporary
5903 environment for variable lookups. We have already flushed the temporary
5904 environment above in the case we're expanding a redirection, so processes
5905 executed by this command need to be able to set it independently of their
5906 parent. */
5907 expanding_redir = 0;
5908
5909 remove_quoted_escapes (string);
5910
5911 subshell_level++;
5912 result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
5913 subshell_level--;
5914
5915 #if !defined (HAVE_DEV_FD)
5916 /* Make sure we close the named pipe in the child before we exit. */
5917 close (open_for_read_in_child ? 0 : 1);
5918 #endif /* !HAVE_DEV_FD */
5919
5920 last_command_exit_value = result;
5921 result = run_exit_trap ();
5922 exit (result);
5923 /*NOTREACHED*/
5924 }
5925 #endif /* PROCESS_SUBSTITUTION */
5926
5927 /***********************************/
5928 /* */
5929 /* Command Substitution */
5930 /* */
5931 /***********************************/
5932
5933 static char *
5934 read_comsub (fd, quoted, rflag)
5935 int fd, quoted;
5936 int *rflag;
5937 {
5938 char *istring, buf[128], *bufp, *s;
5939 int istring_index, istring_size, c, tflag, skip_ctlesc, skip_ctlnul;
5940 ssize_t bufn;
5941 int nullbyte;
5942
5943 istring = (char *)NULL;
5944 istring_index = istring_size = bufn = tflag = 0;
5945
5946 for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
5947 skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
5948
5949 nullbyte = 0;
5950
5951 /* Read the output of the command through the pipe. This may need to be
5952 changed to understand multibyte characters in the future. */
5953 while (1)
5954 {
5955 if (fd < 0)
5956 break;
5957 if (--bufn <= 0)
5958 {
5959 bufn = zread (fd, buf, sizeof (buf));
5960 if (bufn <= 0)
5961 break;
5962 bufp = buf;
5963 }
5964 c = *bufp++;
5965
5966 if (c == 0)
5967 {
5968 #if 1
5969 if (nullbyte == 0)
5970 {
5971 internal_warning ("%s", _("command substitution: ignored null byte in input"));
5972 nullbyte = 1;
5973 }
5974 #endif
5975 continue;
5976 }
5977
5978 /* Add the character to ISTRING, possibly after resizing it. */
5979 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
5980
5981 /* This is essentially quote_string inline */
5982 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */)
5983 istring[istring_index++] = CTLESC;
5984 /* Escape CTLESC and CTLNUL in the output to protect those characters
5985 from the rest of the word expansions (word splitting and globbing.)
5986 This is essentially quote_escapes inline. */
5987 else if (skip_ctlesc == 0 && c == CTLESC)
5988 {
5989 tflag |= W_HASCTLESC;
5990 istring[istring_index++] = CTLESC;
5991 }
5992 else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0)))
5993 istring[istring_index++] = CTLESC;
5994
5995 istring[istring_index++] = c;
5996
5997 #if 0
5998 #if defined (__CYGWIN__)
5999 if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r')
6000 {
6001 istring_index--;
6002 istring[istring_index - 1] = '\n';
6003 }
6004 #endif
6005 #endif
6006 }
6007
6008 if (istring)
6009 istring[istring_index] = '\0';
6010
6011 /* If we read no output, just return now and save ourselves some
6012 trouble. */
6013 if (istring_index == 0)
6014 {
6015 FREE (istring);
6016 if (rflag)
6017 *rflag = tflag;
6018 return (char *)NULL;
6019 }
6020
6021 /* Strip trailing newlines from the output of the command. */
6022 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6023 {
6024 while (istring_index > 0)
6025 {
6026 if (istring[istring_index - 1] == '\n')
6027 {
6028 --istring_index;
6029
6030 /* If the newline was quoted, remove the quoting char. */
6031 if (istring[istring_index - 1] == CTLESC)
6032 --istring_index;
6033 }
6034 else
6035 break;
6036 }
6037 istring[istring_index] = '\0';
6038 }
6039 else
6040 strip_trailing (istring, istring_index - 1, 1);
6041
6042 if (rflag)
6043 *rflag = tflag;
6044 return istring;
6045 }
6046
6047 /* Perform command substitution on STRING. This returns a WORD_DESC * with the
6048 contained string possibly quoted. */
6049 WORD_DESC *
6050 command_substitute (string, quoted)
6051 char *string;
6052 int quoted;
6053 {
6054 pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
6055 char *istring, *s;
6056 int result, fildes[2], function_value, pflags, rc, tflag;
6057 WORD_DESC *ret;
6058
6059 istring = (char *)NULL;
6060
6061 /* Don't fork () if there is no need to. In the case of no command to
6062 run, just return NULL. */
6063 #if 1
6064 for (s = string; s && *s && (shellblank (*s) || *s == '\n'); s++)
6065 ;
6066 if (s == 0 || *s == 0)
6067 return ((WORD_DESC *)NULL);
6068 #else
6069 if (!string || !*string || (string[0] == '\n' && !string[1]))
6070 return ((WORD_DESC *)NULL);
6071 #endif
6072
6073 if (wordexp_only && read_but_dont_execute)
6074 {
6075 last_command_exit_value = EX_WEXPCOMSUB;
6076 jump_to_top_level (EXITPROG);
6077 }
6078
6079 /* We're making the assumption here that the command substitution will
6080 eventually run a command from the file system. Since we'll run
6081 maybe_make_export_env in this subshell before executing that command,
6082 the parent shell and any other shells it starts will have to remake
6083 the environment. If we make it before we fork, other shells won't
6084 have to. Don't bother if we have any temporary variable assignments,
6085 though, because the export environment will be remade after this
6086 command completes anyway, but do it if all the words to be expanded
6087 are variable assignments. */
6088 if (subst_assign_varlist == 0 || garglist == 0)
6089 maybe_make_export_env (); /* XXX */
6090
6091 /* Flags to pass to parse_and_execute() */
6092 pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0;
6093
6094 /* Pipe the output of executing STRING into the current shell. */
6095 if (pipe (fildes) < 0)
6096 {
6097 sys_error ("%s", _("cannot make pipe for command substitution"));
6098 goto error_exit;
6099 }
6100
6101 old_pid = last_made_pid;
6102 #if defined (JOB_CONTROL)
6103 old_pipeline_pgrp = pipeline_pgrp;
6104 /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
6105 if ((subshell_environment & SUBSHELL_PIPE) == 0)
6106 pipeline_pgrp = shell_pgrp;
6107 cleanup_the_pipeline ();
6108 #endif /* JOB_CONTROL */
6109
6110 old_async_pid = last_asynchronous_pid;
6111 pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
6112 last_asynchronous_pid = old_async_pid;
6113
6114 if (pid == 0)
6115 {
6116 /* Reset the signal handlers in the child, but don't free the
6117 trap strings. Set a flag noting that we have to free the
6118 trap strings if we run trap to change a signal disposition. */
6119 reset_signal_handlers ();
6120 if (ISINTERRUPT)
6121 {
6122 kill (getpid (), SIGINT);
6123 CLRINTERRUPT; /* if we're ignoring SIGINT somehow */
6124 }
6125 QUIT; /* catch any interrupts we got post-fork */
6126 subshell_environment |= SUBSHELL_RESETTRAP;
6127 }
6128
6129 #if defined (JOB_CONTROL)
6130 /* XXX DO THIS ONLY IN PARENT ? XXX */
6131 set_sigchld_handler ();
6132 stop_making_children ();
6133 if (pid != 0)
6134 pipeline_pgrp = old_pipeline_pgrp;
6135 #else
6136 stop_making_children ();
6137 #endif /* JOB_CONTROL */
6138
6139 if (pid < 0)
6140 {
6141 sys_error (_("cannot make child for command substitution"));
6142 error_exit:
6143
6144 last_made_pid = old_pid;
6145
6146 FREE (istring);
6147 close (fildes[0]);
6148 close (fildes[1]);
6149 return ((WORD_DESC *)NULL);
6150 }
6151
6152 if (pid == 0)
6153 {
6154 /* The currently executing shell is not interactive. */
6155 interactive = 0;
6156
6157 set_sigint_handler (); /* XXX */
6158
6159 free_pushed_string_input ();
6160
6161 /* Discard buffered stdio output before replacing the underlying file
6162 descriptor. */
6163 fpurge (stdout);
6164
6165 if (dup2 (fildes[1], 1) < 0)
6166 {
6167 sys_error ("%s", _("command_substitute: cannot duplicate pipe as fd 1"));
6168 exit (EXECUTION_FAILURE);
6169 }
6170
6171 /* If standard output is closed in the parent shell
6172 (such as after `exec >&-'), file descriptor 1 will be
6173 the lowest available file descriptor, and end up in
6174 fildes[0]. This can happen for stdin and stderr as well,
6175 but stdout is more important -- it will cause no output
6176 to be generated from this command. */
6177 if ((fildes[1] != fileno (stdin)) &&
6178 (fildes[1] != fileno (stdout)) &&
6179 (fildes[1] != fileno (stderr)))
6180 close (fildes[1]);
6181
6182 if ((fildes[0] != fileno (stdin)) &&
6183 (fildes[0] != fileno (stdout)) &&
6184 (fildes[0] != fileno (stderr)))
6185 close (fildes[0]);
6186
6187 #ifdef __CYGWIN__
6188 /* Let stdio know the fd may have changed from text to binary mode, and
6189 make sure to preserve stdout line buffering. */
6190 freopen (NULL, "w", stdout);
6191 sh_setlinebuf (stdout);
6192 #endif /* __CYGWIN__ */
6193
6194 /* This is a subshell environment. */
6195 subshell_environment |= SUBSHELL_COMSUB;
6196
6197 /* Many shells do not appear to inherit the -v option for command
6198 substitutions. */
6199 change_flag ('v', FLAG_OFF);
6200
6201 /* When inherit_errexit option is not enabled, command substitution does
6202 not inherit the -e flag. It is enabled when Posix mode is enabled */
6203 if (inherit_errexit == 0)
6204 {
6205 builtin_ignoring_errexit = 0;
6206 change_flag ('e', FLAG_OFF);
6207 }
6208 set_shellopts ();
6209
6210 /* If we are expanding a redirection, we can dispose of any temporary
6211 environment we received, since redirections are not supposed to have
6212 access to the temporary environment. We will have to see whether this
6213 affects temporary environments supplied to `eval', but the temporary
6214 environment gets copied to builtin_env at some point. */
6215 if (expanding_redir)
6216 {
6217 flush_temporary_env ();
6218 expanding_redir = 0;
6219 }
6220
6221 remove_quoted_escapes (string);
6222
6223 startup_state = 2; /* see if we can avoid a fork */
6224 /* Give command substitution a place to jump back to on failure,
6225 so we don't go back up to main (). */
6226 result = setjmp_nosigs (top_level);
6227
6228 /* If we're running a command substitution inside a shell function,
6229 trap `return' so we don't return from the function in the subshell
6230 and go off to never-never land. */
6231 if (result == 0 && return_catch_flag)
6232 function_value = setjmp_nosigs (return_catch);
6233 else
6234 function_value = 0;
6235
6236 if (result == ERREXIT)
6237 rc = last_command_exit_value;
6238 else if (result == EXITPROG)
6239 rc = last_command_exit_value;
6240 else if (result)
6241 rc = EXECUTION_FAILURE;
6242 else if (function_value)
6243 rc = return_catch_value;
6244 else
6245 {
6246 subshell_level++;
6247 rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST);
6248 subshell_level--;
6249 }
6250
6251 last_command_exit_value = rc;
6252 rc = run_exit_trap ();
6253 #if defined (PROCESS_SUBSTITUTION)
6254 unlink_fifo_list ();
6255 #endif
6256 exit (rc);
6257 }
6258 else
6259 {
6260 #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
6261 close_pgrp_pipe ();
6262 #endif /* JOB_CONTROL && PGRP_PIPE */
6263
6264 close (fildes[1]);
6265
6266 tflag = 0;
6267 istring = read_comsub (fildes[0], quoted, &tflag);
6268
6269 close (fildes[0]);
6270
6271 current_command_subst_pid = pid;
6272 last_command_exit_value = wait_for (pid);
6273 last_command_subst_pid = pid;
6274 last_made_pid = old_pid;
6275
6276 #if defined (JOB_CONTROL)
6277 /* If last_command_exit_value > 128, then the substituted command
6278 was terminated by a signal. If that signal was SIGINT, then send
6279 SIGINT to ourselves. This will break out of loops, for instance. */
6280 if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT)
6281 kill (getpid (), SIGINT);
6282
6283 /* wait_for gives the terminal back to shell_pgrp. If some other
6284 process group should have it, give it away to that group here.
6285 pipeline_pgrp is non-zero only while we are constructing a
6286 pipeline, so what we are concerned about is whether or not that
6287 pipeline was started in the background. A pipeline started in
6288 the background should never get the tty back here. We duplicate
6289 the conditions that wait_for tests to make sure we only give
6290 the terminal back to pipeline_pgrp under the conditions that wait_for
6291 gave it to shell_pgrp. If wait_for doesn't mess with the terminal
6292 pgrp, we should not either. */
6293 if (interactive && pipeline_pgrp != (pid_t)0 && running_in_background == 0 &&
6294 (subshell_environment & (SUBSHELL_ASYNC|SUBSHELL_PIPE)) == 0)
6295 give_terminal_to (pipeline_pgrp, 0);
6296 #endif /* JOB_CONTROL */
6297
6298 ret = alloc_word_desc ();
6299 ret->word = istring;
6300 ret->flags = tflag;
6301
6302 return ret;
6303 }
6304 }
6305
6306 /********************************************************
6307 * *
6308 * Utility functions for parameter expansion *
6309 * *
6310 ********************************************************/
6311
6312 #if defined (ARRAY_VARS)
6313
6314 static arrayind_t
6315 array_length_reference (s)
6316 char *s;
6317 {
6318 int len;
6319 arrayind_t ind;
6320 char *akey;
6321 char *t, c;
6322 ARRAY *array;
6323 HASH_TABLE *h;
6324 SHELL_VAR *var;
6325
6326 var = array_variable_part (s, &t, &len);
6327
6328 /* If unbound variables should generate an error, report one and return
6329 failure. */
6330 if ((var == 0 || invisible_p (var) || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error)
6331 {
6332 c = *--t;
6333 *t = '\0';
6334 last_command_exit_value = EXECUTION_FAILURE;
6335 err_unboundvar (s);
6336 *t = c;
6337 return (-1);
6338 }
6339 else if (var == 0 || invisible_p (var))
6340 return 0;
6341
6342 /* We support a couple of expansions for variables that are not arrays.
6343 We'll return the length of the value for v[0], and 1 for v[@] or
6344 v[*]. Return 0 for everything else. */
6345
6346 array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
6347 h = assoc_p (var) ? assoc_cell (var) : (HASH_TABLE *)NULL;
6348
6349 if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
6350 {
6351 if (assoc_p (var))
6352 return (h ? assoc_num_elements (h) : 0);
6353 else if (array_p (var))
6354 return (array ? array_num_elements (array) : 0);
6355 else
6356 return (var_isset (var) ? 1 : 0);
6357 }
6358
6359 if (assoc_p (var))
6360 {
6361 t[len - 1] = '\0';
6362 akey = expand_assignment_string_to_string (t, 0); /* [ */
6363 t[len - 1] = ']';
6364 if (akey == 0 || *akey == 0)
6365 {
6366 err_badarraysub (t);
6367 FREE (akey);
6368 return (-1);
6369 }
6370 t = assoc_reference (assoc_cell (var), akey);
6371 free (akey);
6372 }
6373 else
6374 {
6375 ind = array_expand_index (var, t, len);
6376 /* negative subscripts to indexed arrays count back from end */
6377 if (var && array_p (var) && ind < 0)
6378 ind = array_max_index (array_cell (var)) + 1 + ind;
6379 if (ind < 0)
6380 {
6381 err_badarraysub (t);
6382 return (-1);
6383 }
6384 if (array_p (var))
6385 t = array_reference (array, ind);
6386 else
6387 t = (ind == 0) ? value_cell (var) : (char *)NULL;
6388 }
6389
6390 len = MB_STRLEN (t);
6391 return (len);
6392 }
6393 #endif /* ARRAY_VARS */
6394
6395 static int
6396 valid_brace_expansion_word (name, var_is_special)
6397 char *name;
6398 int var_is_special;
6399 {
6400 if (DIGIT (*name) && all_digits (name))
6401 return 1;
6402 else if (var_is_special)
6403 return 1;
6404 #if defined (ARRAY_VARS)
6405 else if (valid_array_reference (name, 0))
6406 return 1;
6407 #endif /* ARRAY_VARS */
6408 else if (legal_identifier (name))
6409 return 1;
6410 else
6411 return 0;
6412 }
6413
6414 static int
6415 chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at)
6416 char *name;
6417 int quoted;
6418 int *quoted_dollar_atp, *contains_dollar_at;
6419 {
6420 char *temp1;
6421
6422 if (name == 0)
6423 {
6424 if (quoted_dollar_atp)
6425 *quoted_dollar_atp = 0;
6426 if (contains_dollar_at)
6427 *contains_dollar_at = 0;
6428 return 0;
6429 }
6430
6431 /* check for $@ and $* */
6432 if (name[0] == '@' && name[1] == 0)
6433 {
6434 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
6435 *quoted_dollar_atp = 1;
6436 if (contains_dollar_at)
6437 *contains_dollar_at = 1;
6438 return 1;
6439 }
6440 else if (name[0] == '*' && name[1] == '\0' && quoted == 0)
6441 {
6442 if (contains_dollar_at)
6443 *contains_dollar_at = 1;
6444 return 1;
6445 }
6446
6447 /* Now check for ${array[@]} and ${array[*]} */
6448 #if defined (ARRAY_VARS)
6449 else if (valid_array_reference (name, 0))
6450 {
6451 temp1 = mbschr (name, '[');
6452 if (temp1 && temp1[1] == '@' && temp1[2] == ']')
6453 {
6454 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
6455 *quoted_dollar_atp = 1;
6456 if (contains_dollar_at)
6457 *contains_dollar_at = 1;
6458 return 1;
6459 } /* [ */
6460 /* ${array[*]}, when unquoted, should be treated like ${array[@]},
6461 which should result in separate words even when IFS is unset. */
6462 if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0)
6463 {
6464 if (contains_dollar_at)
6465 *contains_dollar_at = 1;
6466 return 1;
6467 }
6468 }
6469 #endif
6470 return 0;
6471 }
6472
6473 /* Parameter expand NAME, and return a new string which is the expansion,
6474 or NULL if there was no expansion.
6475 VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
6476 the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
6477 NAME was found inside of a double-quoted expression. */
6478 static WORD_DESC *
6479 parameter_brace_expand_word (name, var_is_special, quoted, pflags, indp)
6480 char *name;
6481 int var_is_special, quoted, pflags;
6482 arrayind_t *indp;
6483 {
6484 WORD_DESC *ret;
6485 char *temp, *tt;
6486 intmax_t arg_index;
6487 SHELL_VAR *var;
6488 int atype, rflags;
6489 arrayind_t ind;
6490
6491 ret = 0;
6492 temp = 0;
6493 rflags = 0;
6494
6495 if (indp)
6496 *indp = INTMAX_MIN;
6497
6498 /* Handle multiple digit arguments, as in ${11}. */
6499 if (legal_number (name, &arg_index))
6500 {
6501 tt = get_dollar_var_value (arg_index);
6502 if (tt)
6503 temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6504 ? quote_string (tt)
6505 : quote_escapes (tt);
6506 else
6507 temp = (char *)NULL;
6508 FREE (tt);
6509 }
6510 else if (var_is_special) /* ${@} */
6511 {
6512 int sindex;
6513 tt = (char *)xmalloc (2 + strlen (name));
6514 tt[sindex = 0] = '$';
6515 strcpy (tt + 1, name);
6516
6517 ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
6518 (int *)NULL, (int *)NULL, pflags);
6519 free (tt);
6520 }
6521 #if defined (ARRAY_VARS)
6522 else if (valid_array_reference (name, 0))
6523 {
6524 expand_arrayref:
6525 if (pflags & PF_ASSIGNRHS)
6526 {
6527 var = array_variable_part (name, &tt, (int *)0);
6528 if (ALL_ELEMENT_SUB (tt[0]) && tt[1] == ']')
6529 {
6530 /* Only treat as double quoted if array variable */
6531 if (var && (array_p (var) || assoc_p (var)))
6532 /* XXX - bash-4.4/bash-5.0 pass AV_ASSIGNRHS */
6533 temp = array_value (name, quoted|Q_DOUBLE_QUOTES, AV_ASSIGNRHS, &atype, &ind);
6534 else
6535 temp = array_value (name, quoted, 0, &atype, &ind);
6536 }
6537 else
6538 temp = array_value (name, quoted, 0, &atype, &ind);
6539 }
6540 else
6541 temp = array_value (name, quoted, 0, &atype, &ind);
6542 if (atype == 0 && temp)
6543 {
6544 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6545 ? quote_string (temp)
6546 : quote_escapes (temp);
6547 rflags |= W_ARRAYIND;
6548 if (indp)
6549 *indp = ind;
6550 }
6551 else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6552 rflags |= W_HASQUOTEDNULL;
6553 }
6554 #endif
6555 else if (var = find_variable (name))
6556 {
6557 if (var_isset (var) && invisible_p (var) == 0)
6558 {
6559 #if defined (ARRAY_VARS)
6560 if (assoc_p (var))
6561 temp = assoc_reference (assoc_cell (var), "0");
6562 else if (array_p (var))
6563 temp = array_reference (array_cell (var), 0);
6564 else
6565 temp = value_cell (var);
6566 #else
6567 temp = value_cell (var);
6568 #endif
6569
6570 if (temp)
6571 temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
6572 ? quote_string (temp)
6573 : quote_escapes (temp);
6574 }
6575 else
6576 temp = (char *)NULL;
6577 }
6578 else if (var = find_variable_last_nameref (name, 0))
6579 {
6580 temp = nameref_cell (var);
6581 #if defined (ARRAY_VARS)
6582 /* Handle expanding nameref whose value is x[n] */
6583 if (temp && *temp && valid_array_reference (temp, 0))
6584 {
6585 name = temp;
6586 goto expand_arrayref;
6587 }
6588 else
6589 #endif
6590 /* y=2 ; typeset -n x=y; echo ${x} is not the same as echo ${2} in ksh */
6591 if (temp && *temp && legal_identifier (temp) == 0)
6592 {
6593 last_command_exit_value = EXECUTION_FAILURE;
6594 report_error (_("%s: invalid variable name for name reference"), temp);
6595 temp = &expand_param_error;
6596 }
6597 else
6598 temp = (char *)NULL;
6599 }
6600 else
6601 temp = (char *)NULL;
6602
6603 if (ret == 0)
6604 {
6605 ret = alloc_word_desc ();
6606 ret->word = temp;
6607 ret->flags |= rflags;
6608 }
6609 return ret;
6610 }
6611
6612 static char *
6613 parameter_brace_find_indir (name, var_is_special, quoted, find_nameref)
6614 char *name;
6615 int var_is_special, quoted, find_nameref;
6616 {
6617 char *temp, *t;
6618 WORD_DESC *w;
6619 SHELL_VAR *v;
6620
6621 if (find_nameref && var_is_special == 0 && (v = find_variable_last_nameref (name, 0)) &&
6622 nameref_p (v) && (t = nameref_cell (v)) && *t)
6623 return (savestring (t));
6624
6625 /* If var_is_special == 0, and name is not an array reference, this does
6626 more expansion than necessary. It should really look up the variable's
6627 value and not try to expand it. */
6628 w = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND, 0);
6629 t = w->word;
6630 /* Have to dequote here if necessary */
6631 if (t)
6632 {
6633 temp = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
6634 ? dequote_string (t)
6635 : dequote_escapes (t);
6636 free (t);
6637 t = temp;
6638 }
6639 dispose_word_desc (w);
6640
6641 return t;
6642 }
6643
6644 /* Expand an indirect reference to a variable: ${!NAME} expands to the
6645 value of the variable whose name is the value of NAME. */
6646 static WORD_DESC *
6647 parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at)
6648 char *name;
6649 int var_is_special, quoted;
6650 int *quoted_dollar_atp, *contains_dollar_at;
6651 {
6652 char *temp, *t;
6653 WORD_DESC *w;
6654 SHELL_VAR *v;
6655
6656 /* See if it's a nameref first, behave in ksh93-compatible fashion.
6657 There is at least one incompatibility: given ${!foo[0]} where foo=bar,
6658 bash performs an indirect lookup on foo[0] and expands the result;
6659 ksh93 expands bar[0]. We could do that here -- there are enough usable
6660 primitives to do that -- but do not at this point. */
6661 if (var_is_special == 0 && (v = find_variable_last_nameref (name, 0)))
6662 {
6663 if (nameref_p (v) && (t = nameref_cell (v)) && *t)
6664 {
6665 w = alloc_word_desc ();
6666 w->word = savestring (t);
6667 w->flags = 0;
6668 return w;
6669 }
6670 }
6671
6672 t = parameter_brace_find_indir (name, var_is_special, quoted, 0);
6673
6674 chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at);
6675 if (t == 0)
6676 return (WORD_DESC *)NULL;
6677
6678 if (valid_brace_expansion_word (t, SPECIAL_VAR (t, 0)) == 0)
6679 {
6680 report_error (_("%s: bad substitution"), t);
6681 free (t);
6682 w = alloc_word_desc ();
6683 w->word = &expand_param_error;
6684 w->flags = 0;
6685 return (w);
6686 }
6687
6688 w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, 0, 0);
6689 free (t);
6690
6691 return w;
6692 }
6693
6694 /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
6695 depending on the value of C, the separating character. C can be one of
6696 "-", "+", or "=". QUOTED is true if the entire brace expression occurs
6697 between double quotes. */
6698 static WORD_DESC *
6699 parameter_brace_expand_rhs (name, value, c, quoted, pflags, qdollaratp, hasdollarat)
6700 char *name, *value;
6701 int c, quoted, pflags, *qdollaratp, *hasdollarat;
6702 {
6703 WORD_DESC *w;
6704 WORD_LIST *l;
6705 char *t, *t1, *temp, *vname;
6706 int l_hasdollat, sindex;
6707
6708 /*itrace("parameter_brace_expand_rhs: %s:%s pflags = %d", name, value, pflags);*/
6709 /* If the entire expression is between double quotes, we want to treat
6710 the value as a double-quoted string, with the exception that we strip
6711 embedded unescaped double quotes (for sh backwards compatibility). */
6712 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
6713 {
6714 sindex = 0;
6715 temp = string_extract_double_quoted (value, &sindex, SX_STRIPDQ);
6716 }
6717 else
6718 temp = value;
6719
6720 w = alloc_word_desc ();
6721 l_hasdollat = 0;
6722 /* XXX was 0 not quoted */
6723 l = *temp ? expand_string_for_rhs (temp, quoted, &l_hasdollat, (int *)NULL)
6724 : (WORD_LIST *)0;
6725 if (hasdollarat)
6726 *hasdollarat = l_hasdollat || (l && l->next);
6727 if (temp != value)
6728 free (temp);
6729 if (l)
6730 {
6731 /* If l->next is not null, we know that TEMP contained "$@", since that
6732 is the only expansion that creates more than one word. */
6733 if (qdollaratp && ((l_hasdollat && quoted) || l->next))
6734 {
6735 /*itrace("parameter_brace_expand_rhs: %s:%s: l != NULL, set *qdollaratp", name, value);*/
6736 *qdollaratp = 1;
6737 }
6738
6739 /* The expansion of TEMP returned something. We need to treat things
6740 slightly differently if L_HASDOLLAT is non-zero. If we have "$@",
6741 the individual words have already been quoted. We need to turn them
6742 into a string with the words separated by the first character of
6743 $IFS without any additional quoting, so string_list_dollar_at won't
6744 do the right thing. If IFS is null, we want "$@" to split into
6745 separate arguments, not be concatenated, so we use string_list_internal
6746 and mark the word to be split on spaces later. We use
6747 string_list_dollar_star for "$@" otherwise. */
6748 if (l->next && ifs_is_null)
6749 {
6750 temp = string_list_internal (l, " ");
6751 w->flags |= W_SPLITSPACE;
6752 }
6753 else
6754 temp = (l_hasdollat || l->next) ? string_list_dollar_star (l) : string_list (l);
6755
6756 /* If we have a quoted null result (QUOTED_NULL(temp)) and the word is
6757 a quoted null (l->next == 0 && QUOTED_NULL(l->word->word)), the
6758 flags indicate it (l->word->flags & W_HASQUOTEDNULL), and the
6759 expansion is quoted (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
6760 (which is more paranoia than anything else), we need to return the
6761 quoted null string and set the flags to indicate it. */
6762 if (l->next == 0 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp) && QUOTED_NULL (l->word->word) && (l->word->flags & W_HASQUOTEDNULL))
6763 {
6764 w->flags |= W_HASQUOTEDNULL;
6765 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null, turning off qdollaratp", name, value);*/
6766 /* If we return a quoted null with L_HASDOLLARAT, we either have a
6767 construct like "${@-$@}" or "${@-${@-$@}}" with no positional
6768 parameters or a quoted expansion of "$@" with $1 == ''. In either
6769 case, we don't want to enable special handling of $@. */
6770 if (qdollaratp && l_hasdollat)
6771 *qdollaratp = 0;
6772 }
6773 dispose_words (l);
6774 }
6775 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && l_hasdollat)
6776 {
6777 /* Posix interp 221 changed the rules on this. The idea is that
6778 something like "$xxx$@" should expand the same as "${foo-$xxx$@}"
6779 when foo and xxx are unset. The problem is that it's not in any
6780 way backwards compatible and few other shells do it. We're eventually
6781 going to try and split the difference (heh) a little bit here. */
6782 /* l_hasdollat == 1 means we saw a quoted dollar at. */
6783
6784 /* The brace expansion occurred between double quotes and there was
6785 a $@ in TEMP. It does not matter if the $@ is quoted, as long as
6786 it does not expand to anything. In this case, we want to return
6787 a quoted empty string. Posix interp 888 */
6788 temp = make_quoted_char ('\0');
6789 w->flags |= W_HASQUOTEDNULL;
6790 /*itrace("parameter_brace_expand_rhs (%s:%s): returning quoted null", name, value);*/
6791 }
6792 else
6793 temp = (char *)NULL;
6794
6795 if (c == '-' || c == '+')
6796 {
6797 w->word = temp;
6798 return w;
6799 }
6800
6801 /* c == '=' */
6802 t = temp ? savestring (temp) : savestring ("");
6803 t1 = dequote_string (t);
6804 free (t);
6805
6806 /* bash-4.4/5.0 */
6807 vname = name;
6808 if (*name == '!' &&
6809 (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1]) || VALID_INDIR_PARAM (name[1])))
6810 {
6811 vname = parameter_brace_find_indir (name + 1, SPECIAL_VAR (name, 1), quoted, 1);
6812 if (vname == 0 || *vname == 0)
6813 {
6814 report_error (_("%s: invalid indirect expansion"), name);
6815 free (vname);
6816 dispose_word (w);
6817 return &expand_wdesc_error;
6818 }
6819 if (legal_identifier (vname) == 0)
6820 {
6821 report_error (_("%s: invalid variable name"), vname);
6822 free (vname);
6823 dispose_word (w);
6824 return &expand_wdesc_error;
6825 }
6826 }
6827
6828 #if defined (ARRAY_VARS)
6829 if (valid_array_reference (vname, 0))
6830 assign_array_element (vname, t1, 0);
6831 else
6832 #endif /* ARRAY_VARS */
6833 bind_variable (vname, t1, 0);
6834
6835 stupidly_hack_special_variables (vname);
6836
6837 if (vname != name)
6838 free (vname);
6839
6840 /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */
6841 free (temp);
6842
6843 w->word = t1;
6844 return w;
6845 }
6846
6847 /* Deal with the right hand side of a ${name:?value} expansion in the case
6848 that NAME is null or not set. If VALUE is non-null it is expanded and
6849 used as the error message to print, otherwise a standard message is
6850 printed. */
6851 static void
6852 parameter_brace_expand_error (name, value)
6853 char *name, *value;
6854 {
6855 WORD_LIST *l;
6856 char *temp;
6857
6858 last_command_exit_value = EXECUTION_FAILURE; /* ensure it's non-zero */
6859 if (value && *value)
6860 {
6861 l = expand_string (value, 0);
6862 temp = string_list (l);
6863 report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */
6864 FREE (temp);
6865 dispose_words (l);
6866 }
6867 else
6868 report_error (_("%s: parameter null or not set"), name);
6869
6870 /* Free the data we have allocated during this expansion, since we
6871 are about to longjmp out. */
6872 free (name);
6873 FREE (value);
6874 }
6875
6876 /* Return 1 if NAME is something for which parameter_brace_expand_length is
6877 OK to do. */
6878 static int
6879 valid_length_expression (name)
6880 char *name;
6881 {
6882 return (name[1] == '\0' || /* ${#} */
6883 ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */
6884 (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */
6885 #if defined (ARRAY_VARS)
6886 valid_array_reference (name + 1, 0) || /* ${#a[7]} */
6887 #endif
6888 legal_identifier (name + 1)); /* ${#PS1} */
6889 }
6890
6891 /* Handle the parameter brace expansion that requires us to return the
6892 length of a parameter. */
6893 static intmax_t
6894 parameter_brace_expand_length (name)
6895 char *name;
6896 {
6897 char *t, *newname;
6898 intmax_t number, arg_index;
6899 WORD_LIST *list;
6900 #if defined (ARRAY_VARS)
6901 SHELL_VAR *var;
6902 #endif
6903
6904 if (name[1] == '\0') /* ${#} */
6905 number = number_of_args ();
6906 else if (DOLLAR_AT_STAR (name[1]) && name[2] == '\0') /* ${#@}, ${#*} */
6907 number = number_of_args ();
6908 else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
6909 {
6910 /* Take the lengths of some of the shell's special parameters. */
6911 switch (name[1])
6912 {
6913 case '-':
6914 t = which_set_flags ();
6915 break;
6916 case '?':
6917 t = itos (last_command_exit_value);
6918 break;
6919 case '$':
6920 t = itos (dollar_dollar_pid);
6921 break;
6922 case '!':
6923 if (last_asynchronous_pid == NO_PID)
6924 t = (char *)NULL; /* XXX - error if set -u set? */
6925 else
6926 t = itos (last_asynchronous_pid);
6927 break;
6928 case '#':
6929 t = itos (number_of_args ());
6930 break;
6931 }
6932 number = STRLEN (t);
6933 FREE (t);
6934 }
6935 #if defined (ARRAY_VARS)
6936 else if (valid_array_reference (name + 1, 0))
6937 number = array_length_reference (name + 1);
6938 #endif /* ARRAY_VARS */
6939 else
6940 {
6941 number = 0;
6942
6943 if (legal_number (name + 1, &arg_index)) /* ${#1} */
6944 {
6945 t = get_dollar_var_value (arg_index);
6946 if (t == 0 && unbound_vars_is_error)
6947 return INTMAX_MIN;
6948 number = MB_STRLEN (t);
6949 FREE (t);
6950 }
6951 #if defined (ARRAY_VARS)
6952 else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var)))
6953 {
6954 if (assoc_p (var))
6955 t = assoc_reference (assoc_cell (var), "0");
6956 else
6957 t = array_reference (array_cell (var), 0);
6958 if (t == 0 && unbound_vars_is_error)
6959 return INTMAX_MIN;
6960 number = MB_STRLEN (t);
6961 }
6962 #endif
6963 else /* ${#PS1} */
6964 {
6965 newname = savestring (name);
6966 newname[0] = '$';
6967 list = expand_string (newname, Q_DOUBLE_QUOTES);
6968 t = list ? string_list (list) : (char *)NULL;
6969 free (newname);
6970 if (list)
6971 dispose_words (list);
6972
6973 number = t ? MB_STRLEN (t) : 0;
6974 FREE (t);
6975 }
6976 }
6977
6978 return (number);
6979 }
6980
6981 /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
6982 so we do some ad-hoc parsing of an arithmetic expression to find
6983 the first DELIM, instead of using strchr(3). Two rules:
6984 1. If the substring contains a `(', read until closing `)'.
6985 2. If the substring contains a `?', read past one `:' for each `?'.
6986 The SD_ARITHEXP flag to skip_to_delim takes care of doing this.
6987 */
6988
6989 static char *
6990 skiparith (substr, delim)
6991 char *substr;
6992 int delim;
6993 {
6994 int i;
6995 char delims[2];
6996
6997 delims[0] = delim;
6998 delims[1] = '\0';
6999
7000 i = skip_to_delim (substr, 0, delims, SD_ARITHEXP);
7001 return (substr + i);
7002 }
7003
7004 /* Verify and limit the start and end of the desired substring. If
7005 VTYPE == 0, a regular shell variable is being used; if it is 1,
7006 then the positional parameters are being used; if it is 2, then
7007 VALUE is really a pointer to an array variable that should be used.
7008 Return value is 1 if both values were OK, 0 if there was a problem
7009 with an invalid expression, or -1 if the values were out of range. */
7010 static int
7011 verify_substring_values (v, value, substr, vtype, e1p, e2p)
7012 SHELL_VAR *v;
7013 char *value, *substr;
7014 int vtype;
7015 intmax_t *e1p, *e2p;
7016 {
7017 char *t, *temp1, *temp2;
7018 arrayind_t len;
7019 int expok;
7020 #if defined (ARRAY_VARS)
7021 ARRAY *a;
7022 HASH_TABLE *h;
7023 #endif
7024
7025 /* duplicate behavior of strchr(3) */
7026 t = skiparith (substr, ':');
7027 if (*t && *t == ':')
7028 *t = '\0';
7029 else
7030 t = (char *)0;
7031
7032 temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES);
7033 *e1p = evalexp (temp1, &expok);
7034 free (temp1);
7035 if (expok == 0)
7036 return (0);
7037
7038 len = -1; /* paranoia */
7039 switch (vtype)
7040 {
7041 case VT_VARIABLE:
7042 case VT_ARRAYMEMBER:
7043 len = MB_STRLEN (value);
7044 break;
7045 case VT_POSPARMS:
7046 len = number_of_args () + 1;
7047 if (*e1p == 0)
7048 len++; /* add one arg if counting from $0 */
7049 break;
7050 #if defined (ARRAY_VARS)
7051 case VT_ARRAYVAR:
7052 /* For arrays, the first value deals with array indices. Negative
7053 offsets count from one past the array's maximum index. Associative
7054 arrays treat the number of elements as the maximum index. */
7055 if (assoc_p (v))
7056 {
7057 h = assoc_cell (v);
7058 len = assoc_num_elements (h) + (*e1p < 0);
7059 }
7060 else
7061 {
7062 a = (ARRAY *)value;
7063 len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */
7064 }
7065 break;
7066 #endif
7067 }
7068
7069 if (len == -1) /* paranoia */
7070 return -1;
7071
7072 if (*e1p < 0) /* negative offsets count from end */
7073 *e1p += len;
7074
7075 if (*e1p > len || *e1p < 0)
7076 return (-1);
7077
7078 #if defined (ARRAY_VARS)
7079 /* For arrays, the second offset deals with the number of elements. */
7080 if (vtype == VT_ARRAYVAR)
7081 len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a);
7082 #endif
7083
7084 if (t)
7085 {
7086 t++;
7087 temp2 = savestring (t);
7088 temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
7089 free (temp2);
7090 t[-1] = ':';
7091 *e2p = evalexp (temp1, &expok);
7092 free (temp1);
7093 if (expok == 0)
7094 return (0);
7095 #if 1
7096 if ((vtype == VT_ARRAYVAR || vtype == VT_POSPARMS) && *e2p < 0)
7097 #else
7098 /* bash-4.3: allow positional parameter length < 0 to count backwards
7099 from end of positional parameters */
7100 if (vtype == VT_ARRAYVAR && *e2p < 0)
7101 #endif
7102 {
7103 internal_error (_("%s: substring expression < 0"), t);
7104 return (0);
7105 }
7106 #if defined (ARRAY_VARS)
7107 /* In order to deal with sparse arrays, push the intelligence about how
7108 to deal with the number of elements desired down to the array-
7109 specific functions. */
7110 if (vtype != VT_ARRAYVAR)
7111 #endif
7112 {
7113 if (*e2p < 0)
7114 {
7115 *e2p += len;
7116 if (*e2p < 0 || *e2p < *e1p)
7117 {
7118 internal_error (_("%s: substring expression < 0"), t);
7119 return (0);
7120 }
7121 }
7122 else
7123 *e2p += *e1p; /* want E2 chars starting at E1 */
7124 if (*e2p > len)
7125 *e2p = len;
7126 }
7127 }
7128 else
7129 *e2p = len;
7130
7131 return (1);
7132 }
7133
7134 /* Return the type of variable specified by VARNAME (simple variable,
7135 positional param, or array variable). Also return the value specified
7136 by VARNAME (value of a variable or a reference to an array element).
7137 QUOTED is the standard description of quoting state, using Q_* defines.
7138 FLAGS is currently a set of flags to pass to array_value. If IND is
7139 non-null and not INTMAX_MIN, and FLAGS includes AV_USEIND, IND is
7140 passed to array_value so the array index is not computed again.
7141 If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
7142 characters in the value are quoted with CTLESC and takes appropriate
7143 steps. For convenience, *VALP is set to the dequoted VALUE. */
7144 static int
7145 get_var_and_type (varname, value, ind, quoted, flags, varp, valp)
7146 char *varname, *value;
7147 arrayind_t ind;
7148 int quoted, flags;
7149 SHELL_VAR **varp;
7150 char **valp;
7151 {
7152 int vtype, want_indir;
7153 char *temp, *vname;
7154 WORD_DESC *wd;
7155 SHELL_VAR *v;
7156 arrayind_t lind;
7157
7158 want_indir = *varname == '!' &&
7159 (legal_variable_starter ((unsigned char)varname[1]) || DIGIT (varname[1])
7160 || VALID_INDIR_PARAM (varname[1]));
7161 if (want_indir)
7162 vname = parameter_brace_find_indir (varname+1, SPECIAL_VAR (varname, 1), quoted, 1);
7163 /* XXX - what if vname == 0 || *vname == 0 ? */
7164 else
7165 vname = varname;
7166
7167 if (vname == 0)
7168 {
7169 vtype = VT_VARIABLE;
7170 *varp = (SHELL_VAR *)NULL;
7171 *valp = (char *)NULL;
7172 return (vtype);
7173 }
7174
7175 /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
7176 vtype = STR_DOLLAR_AT_STAR (vname);
7177 if (vtype == VT_POSPARMS && vname[0] == '*')
7178 vtype |= VT_STARSUB;
7179 *varp = (SHELL_VAR *)NULL;
7180
7181 #if defined (ARRAY_VARS)
7182 if (valid_array_reference (vname, 0))
7183 {
7184 v = array_variable_part (vname, &temp, (int *)0);
7185 /* If we want to signal array_value to use an already-computed index,
7186 set LIND to that index */
7187 lind = (ind != INTMAX_MIN && (flags & AV_USEIND)) ? ind : 0;
7188 if (v && invisible_p (v))
7189 {
7190 vtype = VT_ARRAYMEMBER;
7191 *varp = (SHELL_VAR *)NULL;
7192 *valp = (char *)NULL;
7193 }
7194 if (v && (array_p (v) || assoc_p (v)))
7195 { /* [ */
7196 if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')
7197 {
7198 /* Callers have to differentiate between indexed and associative */
7199 vtype = VT_ARRAYVAR;
7200 if (temp[0] == '*')
7201 vtype |= VT_STARSUB;
7202 *valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v);
7203 }
7204 else
7205 {
7206 vtype = VT_ARRAYMEMBER;
7207 *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
7208 }
7209 *varp = v;
7210 }
7211 else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']'))
7212 {
7213 vtype = VT_VARIABLE;
7214 *varp = v;
7215 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
7216 *valp = dequote_string (value);
7217 else
7218 *valp = dequote_escapes (value);
7219 }
7220 else
7221 {
7222 vtype = VT_ARRAYMEMBER;
7223 *varp = v;
7224 *valp = array_value (vname, Q_DOUBLE_QUOTES, flags, (int *)NULL, &lind);
7225 }
7226 }
7227 else if ((v = find_variable (vname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v)))
7228 {
7229 vtype = VT_ARRAYMEMBER;
7230 *varp = v;
7231 *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0);
7232 }
7233 else
7234 #endif
7235 {
7236 if (value && vtype == VT_VARIABLE)
7237 {
7238 *varp = find_variable (vname);
7239 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
7240 *valp = dequote_string (value);
7241 else
7242 *valp = dequote_escapes (value);
7243 }
7244 else
7245 *valp = value;
7246 }
7247
7248 if (want_indir)
7249 free (vname);
7250
7251 return vtype;
7252 }
7253
7254 /******************************************************/
7255 /* */
7256 /* Functions to extract substrings of variable values */
7257 /* */
7258 /******************************************************/
7259
7260 #if defined (HANDLE_MULTIBYTE)
7261 /* Character-oriented rather than strictly byte-oriented substrings. S and
7262 E, rather being strict indices into STRING, indicate character (possibly
7263 multibyte character) positions that require calculation.
7264 Used by the ${param:offset[:length]} expansion. */
7265 static char *
7266 mb_substring (string, s, e)
7267 char *string;
7268 int s, e;
7269 {
7270 char *tt;
7271 int start, stop, i;
7272 size_t slen;
7273 DECLARE_MBSTATE;
7274
7275 start = 0;
7276 /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
7277 slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0;
7278
7279 i = s;
7280 while (string[start] && i--)
7281 ADVANCE_CHAR (string, slen, start);
7282 stop = start;
7283 i = e - s;
7284 while (string[stop] && i--)
7285 ADVANCE_CHAR (string, slen, stop);
7286 tt = substring (string, start, stop);
7287 return tt;
7288 }
7289 #endif
7290
7291 /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
7292 is `@', use the positional parameters; otherwise, use the value of
7293 VARNAME. If VARNAME is an array variable, use the array elements. */
7294
7295 static char *
7296 parameter_brace_substring (varname, value, ind, substr, quoted, flags)
7297 char *varname, *value;
7298 int ind;
7299 char *substr;
7300 int quoted, flags;
7301 {
7302 intmax_t e1, e2;
7303 int vtype, r, starsub;
7304 char *temp, *val, *tt, *oname;
7305 SHELL_VAR *v;
7306
7307 if (value == 0 && ((varname[0] != '@' && varname[0] != '*') || varname[1]))
7308 return ((char *)NULL);
7309
7310 oname = this_command_name;
7311 this_command_name = varname;
7312
7313 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
7314 if (vtype == -1)
7315 {
7316 this_command_name = oname;
7317 return ((char *)NULL);
7318 }
7319
7320 starsub = vtype & VT_STARSUB;
7321 vtype &= ~VT_STARSUB;
7322
7323 r = verify_substring_values (v, val, substr, vtype, &e1, &e2);
7324 this_command_name = oname;
7325 if (r <= 0)
7326 {
7327 if (vtype == VT_VARIABLE)
7328 FREE (val);
7329 return ((r == 0) ? &expand_param_error : (char *)NULL);
7330 }
7331
7332 switch (vtype)
7333 {
7334 case VT_VARIABLE:
7335 case VT_ARRAYMEMBER:
7336 #if defined (HANDLE_MULTIBYTE)
7337 if (MB_CUR_MAX > 1)
7338 tt = mb_substring (val, e1, e2);
7339 else
7340 #endif
7341 tt = substring (val, e1, e2);
7342
7343 if (vtype == VT_VARIABLE)
7344 FREE (val);
7345 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
7346 temp = quote_string (tt);
7347 else
7348 temp = tt ? quote_escapes (tt) : (char *)NULL;
7349 FREE (tt);
7350 break;
7351 case VT_POSPARMS:
7352 tt = pos_params (varname, e1, e2, quoted);
7353 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
7354 {
7355 temp = tt ? quote_escapes (tt) : (char *)NULL;
7356 FREE (tt);
7357 }
7358 else
7359 temp = tt;
7360 break;
7361 #if defined (ARRAY_VARS)
7362 case VT_ARRAYVAR:
7363 if (assoc_p (v))
7364 /* we convert to list and take first e2 elements starting at e1th
7365 element -- officially undefined for now */
7366 temp = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted);
7367 else
7368 /* We want E2 to be the number of elements desired (arrays can be sparse,
7369 so verify_substring_values just returns the numbers specified and we
7370 rely on array_subrange to understand how to deal with them). */
7371 temp = array_subrange (array_cell (v), e1, e2, starsub, quoted);
7372 /* array_subrange now calls array_quote_escapes as appropriate, so the
7373 caller no longer needs to. */
7374 break;
7375 #endif
7376 default:
7377 temp = (char *)NULL;
7378 }
7379
7380 return temp;
7381 }
7382
7383 /****************************************************************/
7384 /* */
7385 /* Functions to perform pattern substitution on variable values */
7386 /* */
7387 /****************************************************************/
7388
7389 #if 0 /* Unused */
7390 static int
7391 shouldexp_replacement (s)
7392 char *s;
7393 {
7394 register char *p;
7395
7396 for (p = s; p && *p; p++)
7397 {
7398 if (*p == '\\')
7399 p++;
7400 else if (*p == '&')
7401 return 1;
7402 }
7403 return 0;
7404 }
7405 #endif
7406
7407 char *
7408 pat_subst (string, pat, rep, mflags)
7409 char *string, *pat, *rep;
7410 int mflags;
7411 {
7412 char *ret, *s, *e, *str, *rstr, *mstr;
7413 int rptr, mtype, rxpand, mlen;
7414 size_t rsize, l, replen, rslen;
7415
7416 if (string == 0)
7417 return (savestring (""));
7418
7419 mtype = mflags & MATCH_TYPEMASK;
7420
7421 #if 0 /* bash-4.2 ? */
7422 rxpand = (rep && *rep) ? shouldexp_replacement (rep) : 0;
7423 #else
7424 rxpand = 0;
7425 #endif
7426
7427 /* Special cases:
7428 * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
7429 * with REP and return the result.
7430 * 2. A null pattern with mtype == MATCH_END means to append REP to
7431 * STRING and return the result.
7432 * 3. A null STRING with a matching pattern means to append REP to
7433 * STRING and return the result.
7434 * These don't understand or process `&' in the replacement string.
7435 */
7436 if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
7437 {
7438 replen = STRLEN (rep);
7439 l = STRLEN (string);
7440 ret = (char *)xmalloc (replen + l + 2);
7441 if (replen == 0)
7442 strcpy (ret, string);
7443 else if (mtype == MATCH_BEG)
7444 {
7445 strcpy (ret, rep);
7446 strcpy (ret + replen, string);
7447 }
7448 else
7449 {
7450 strcpy (ret, string);
7451 strcpy (ret + l, rep);
7452 }
7453 return (ret);
7454 }
7455 else if (*string == 0 && (match_pattern (string, pat, mtype, &s, &e) != 0))
7456 {
7457 replen = STRLEN (rep);
7458 ret = (char *)xmalloc (replen + 1);
7459 if (replen == 0)
7460 ret[0] = '\0';
7461 else
7462 strcpy (ret, rep);
7463 return (ret);
7464 }
7465
7466 ret = (char *)xmalloc (rsize = 64);
7467 ret[0] = '\0';
7468
7469 for (replen = STRLEN (rep), rptr = 0, str = string; *str;)
7470 {
7471 if (match_pattern (str, pat, mtype, &s, &e) == 0)
7472 break;
7473 l = s - str;
7474
7475 if (rep && rxpand)
7476 {
7477 int x;
7478 mlen = e - s;
7479 mstr = xmalloc (mlen + 1);
7480 for (x = 0; x < mlen; x++)
7481 mstr[x] = s[x];
7482 mstr[mlen] = '\0';
7483 rstr = strcreplace (rep, '&', mstr, 0);
7484 free (mstr);
7485 rslen = strlen (rstr);
7486 }
7487 else
7488 {
7489 rstr = rep;
7490 rslen = replen;
7491 }
7492
7493 RESIZE_MALLOCED_BUFFER (ret, rptr, (l + rslen), rsize, 64);
7494
7495 /* OK, now copy the leading unmatched portion of the string (from
7496 str to s) to ret starting at rptr (the current offset). Then copy
7497 the replacement string at ret + rptr + (s - str). Increment
7498 rptr (if necessary) and str and go on. */
7499 if (l)
7500 {
7501 strncpy (ret + rptr, str, l);
7502 rptr += l;
7503 }
7504 if (replen)
7505 {
7506 strncpy (ret + rptr, rstr, rslen);
7507 rptr += rslen;
7508 }
7509 str = e; /* e == end of match */
7510
7511 if (rstr != rep)
7512 free (rstr);
7513
7514 if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
7515 break;
7516
7517 if (s == e)
7518 {
7519 /* On a zero-length match, make sure we copy one character, since
7520 we increment one character to avoid infinite recursion. */
7521 RESIZE_MALLOCED_BUFFER (ret, rptr, 1, rsize, 64);
7522 ret[rptr++] = *str++;
7523 e++; /* avoid infinite recursion on zero-length match */
7524 }
7525 }
7526
7527 /* Now copy the unmatched portion of the input string */
7528 if (str && *str)
7529 {
7530 RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
7531 strcpy (ret + rptr, str);
7532 }
7533 else
7534 ret[rptr] = '\0';
7535
7536 return ret;
7537 }
7538
7539 /* Do pattern match and replacement on the positional parameters. */
7540 static char *
7541 pos_params_pat_subst (string, pat, rep, mflags)
7542 char *string, *pat, *rep;
7543 int mflags;
7544 {
7545 WORD_LIST *save, *params;
7546 WORD_DESC *w;
7547 char *ret;
7548 int pchar, qflags;
7549
7550 save = params = list_rest_of_args ();
7551 if (save == 0)
7552 return ((char *)NULL);
7553
7554 for ( ; params; params = params->next)
7555 {
7556 ret = pat_subst (params->word->word, pat, rep, mflags);
7557 w = alloc_word_desc ();
7558 w->word = ret ? ret : savestring ("");
7559 dispose_word (params->word);
7560 params->word = w;
7561 }
7562
7563 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
7564 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
7565
7566 ret = string_list_pos_params (pchar, save, qflags);
7567
7568 dispose_words (save);
7569
7570 return (ret);
7571 }
7572
7573 /* Perform pattern substitution on VALUE, which is the expansion of
7574 VARNAME. PATSUB is an expression supplying the pattern to match
7575 and the string to substitute. QUOTED is a flags word containing
7576 the type of quoting currently in effect. */
7577 static char *
7578 parameter_brace_patsub (varname, value, ind, patsub, quoted, pflags, flags)
7579 char *varname, *value;
7580 int ind;
7581 char *patsub;
7582 int quoted, pflags, flags;
7583 {
7584 int vtype, mflags, starsub, delim;
7585 char *val, *temp, *pat, *rep, *p, *lpatsub, *tt;
7586 SHELL_VAR *v;
7587
7588 if (value == 0)
7589 return ((char *)NULL);
7590
7591 this_command_name = varname;
7592
7593 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
7594 if (vtype == -1)
7595 return ((char *)NULL);
7596
7597 starsub = vtype & VT_STARSUB;
7598 vtype &= ~VT_STARSUB;
7599
7600 mflags = 0;
7601 /* PATSUB is never NULL when this is called. */
7602 if (*patsub == '/')
7603 {
7604 mflags |= MATCH_GLOBREP;
7605 patsub++;
7606 }
7607
7608 /* Malloc this because expand_string_if_necessary or one of the expansion
7609 functions in its call chain may free it on a substitution error. */
7610 lpatsub = savestring (patsub);
7611
7612 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7613 mflags |= MATCH_QUOTED;
7614
7615 if (starsub)
7616 mflags |= MATCH_STARSUB;
7617
7618 if (pflags & PF_ASSIGNRHS)
7619 mflags |= MATCH_ASSIGNRHS;
7620
7621 /* If the pattern starts with a `/', make sure we skip over it when looking
7622 for the replacement delimiter. */
7623 delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0);
7624 if (lpatsub[delim] == '/')
7625 {
7626 lpatsub[delim] = 0;
7627 rep = lpatsub + delim + 1;
7628 }
7629 else
7630 rep = (char *)NULL;
7631
7632 if (rep && *rep == '\0')
7633 rep = (char *)NULL;
7634
7635 /* Perform the same expansions on the pattern as performed by the
7636 pattern removal expansions. */
7637 pat = getpattern (lpatsub, quoted, 1);
7638
7639 if (rep)
7640 {
7641 /* We want to perform quote removal on the expanded replacement even if
7642 the entire expansion is double-quoted because the parser and string
7643 extraction functions treated quotes in the replacement string as
7644 special. THIS IS NOT BACKWARDS COMPATIBLE WITH BASH-4.2. */
7645 if (shell_compatibility_level > 42)
7646 rep = expand_string_if_necessary (rep, quoted & ~(Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT), expand_string_unsplit);
7647 /* This is the bash-4.2 code. */
7648 else if ((mflags & MATCH_QUOTED) == 0)
7649 rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
7650 else
7651 rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
7652 }
7653
7654 /* ksh93 doesn't allow the match specifier to be a part of the expanded
7655 pattern. This is an extension. Make sure we don't anchor the pattern
7656 at the beginning or end of the string if we're doing global replacement,
7657 though. */
7658 p = pat;
7659 if (mflags & MATCH_GLOBREP)
7660 mflags |= MATCH_ANY;
7661 else if (pat && pat[0] == '#')
7662 {
7663 mflags |= MATCH_BEG;
7664 p++;
7665 }
7666 else if (pat && pat[0] == '%')
7667 {
7668 mflags |= MATCH_END;
7669 p++;
7670 }
7671 else
7672 mflags |= MATCH_ANY;
7673
7674 /* OK, we now want to substitute REP for PAT in VAL. If
7675 flags & MATCH_GLOBREP is non-zero, the substitution is done
7676 everywhere, otherwise only the first occurrence of PAT is
7677 replaced. The pattern matching code doesn't understand
7678 CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
7679 values passed in (VT_VARIABLE) so the pattern substitution
7680 code works right. We need to requote special chars after
7681 we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
7682 other cases if QUOTED == 0, since the posparams and arrays
7683 indexed by * or @ do special things when QUOTED != 0. */
7684
7685 switch (vtype)
7686 {
7687 case VT_VARIABLE:
7688 case VT_ARRAYMEMBER:
7689 temp = pat_subst (val, p, rep, mflags);
7690 if (vtype == VT_VARIABLE)
7691 FREE (val);
7692 if (temp)
7693 {
7694 tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
7695 free (temp);
7696 temp = tt;
7697 }
7698 break;
7699 case VT_POSPARMS:
7700 temp = pos_params_pat_subst (val, p, rep, mflags);
7701 if (temp && (mflags & MATCH_QUOTED) == 0)
7702 {
7703 tt = quote_escapes (temp);
7704 free (temp);
7705 temp = tt;
7706 }
7707 break;
7708 #if defined (ARRAY_VARS)
7709 case VT_ARRAYVAR:
7710 temp = assoc_p (v) ? assoc_patsub (assoc_cell (v), p, rep, mflags)
7711 : array_patsub (array_cell (v), p, rep, mflags);
7712 /* Don't call quote_escapes anymore; array_patsub calls
7713 array_quote_escapes as appropriate before adding the
7714 space separators; ditto for assoc_patsub. */
7715 break;
7716 #endif
7717 }
7718
7719 FREE (pat);
7720 FREE (rep);
7721 free (lpatsub);
7722
7723 return temp;
7724 }
7725
7726 /****************************************************************/
7727 /* */
7728 /* Functions to perform case modification on variable values */
7729 /* */
7730 /****************************************************************/
7731
7732 /* Do case modification on the positional parameters. */
7733
7734 static char *
7735 pos_params_modcase (string, pat, modop, mflags)
7736 char *string, *pat;
7737 int modop;
7738 int mflags;
7739 {
7740 WORD_LIST *save, *params;
7741 WORD_DESC *w;
7742 char *ret;
7743 int pchar, qflags;
7744
7745 save = params = list_rest_of_args ();
7746 if (save == 0)
7747 return ((char *)NULL);
7748
7749 for ( ; params; params = params->next)
7750 {
7751 ret = sh_modcase (params->word->word, pat, modop);
7752 w = alloc_word_desc ();
7753 w->word = ret ? ret : savestring ("");
7754 dispose_word (params->word);
7755 params->word = w;
7756 }
7757
7758 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
7759 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
7760
7761 ret = string_list_pos_params (pchar, save, qflags);
7762 dispose_words (save);
7763
7764 return (ret);
7765 }
7766
7767 /* Perform case modification on VALUE, which is the expansion of
7768 VARNAME. MODSPEC is an expression supplying the type of modification
7769 to perform. QUOTED is a flags word containing the type of quoting
7770 currently in effect. */
7771 static char *
7772 parameter_brace_casemod (varname, value, ind, modspec, patspec, quoted, flags)
7773 char *varname, *value;
7774 int ind, modspec;
7775 char *patspec;
7776 int quoted, flags;
7777 {
7778 int vtype, starsub, modop, mflags, x;
7779 char *val, *temp, *pat, *p, *lpat, *tt;
7780 SHELL_VAR *v;
7781
7782 if (value == 0)
7783 return ((char *)NULL);
7784
7785 this_command_name = varname;
7786
7787 vtype = get_var_and_type (varname, value, ind, quoted, flags, &v, &val);
7788 if (vtype == -1)
7789 return ((char *)NULL);
7790
7791 starsub = vtype & VT_STARSUB;
7792 vtype &= ~VT_STARSUB;
7793
7794 modop = 0;
7795 mflags = 0;
7796 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
7797 mflags |= MATCH_QUOTED;
7798 if (starsub)
7799 mflags |= MATCH_STARSUB;
7800
7801 p = patspec;
7802 if (modspec == '^')
7803 {
7804 x = p && p[0] == modspec;
7805 modop = x ? CASE_UPPER : CASE_UPFIRST;
7806 p += x;
7807 }
7808 else if (modspec == ',')
7809 {
7810 x = p && p[0] == modspec;
7811 modop = x ? CASE_LOWER : CASE_LOWFIRST;
7812 p += x;
7813 }
7814 else if (modspec == '~')
7815 {
7816 x = p && p[0] == modspec;
7817 modop = x ? CASE_TOGGLEALL : CASE_TOGGLE;
7818 p += x;
7819 }
7820
7821 lpat = p ? savestring (p) : 0;
7822 /* Perform the same expansions on the pattern as performed by the
7823 pattern removal expansions. FOR LATER */
7824 pat = lpat ? getpattern (lpat, quoted, 1) : 0;
7825
7826 /* OK, now we do the case modification. */
7827 switch (vtype)
7828 {
7829 case VT_VARIABLE:
7830 case VT_ARRAYMEMBER:
7831 temp = sh_modcase (val, pat, modop);
7832 if (vtype == VT_VARIABLE)
7833 FREE (val);
7834 if (temp)
7835 {
7836 tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
7837 free (temp);
7838 temp = tt;
7839 }
7840 break;
7841
7842 case VT_POSPARMS:
7843 temp = pos_params_modcase (val, pat, modop, mflags);
7844 if (temp && (mflags & MATCH_QUOTED) == 0)
7845 {
7846 tt = quote_escapes (temp);
7847 free (temp);
7848 temp = tt;
7849 }
7850 break;
7851
7852 #if defined (ARRAY_VARS)
7853 case VT_ARRAYVAR:
7854 temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags)
7855 : array_modcase (array_cell (v), pat, modop, mflags);
7856 /* Don't call quote_escapes; array_modcase calls array_quote_escapes
7857 as appropriate before adding the space separators; ditto for
7858 assoc_modcase. */
7859 break;
7860 #endif
7861 }
7862
7863 FREE (pat);
7864 free (lpat);
7865
7866 return temp;
7867 }
7868
7869 /* Check for unbalanced parens in S, which is the contents of $(( ... )). If
7870 any occur, this must be a nested command substitution, so return 0.
7871 Otherwise, return 1. A valid arithmetic expression must always have a
7872 ( before a matching ), so any cases where there are more right parens
7873 means that this must not be an arithmetic expression, though the parser
7874 will not accept it without a balanced total number of parens. */
7875 static int
7876 chk_arithsub (s, len)
7877 const char *s;
7878 int len;
7879 {
7880 int i, count;
7881 DECLARE_MBSTATE;
7882
7883 i = count = 0;
7884 while (i < len)
7885 {
7886 if (s[i] == LPAREN)
7887 count++;
7888 else if (s[i] == RPAREN)
7889 {
7890 count--;
7891 if (count < 0)
7892 return 0;
7893 }
7894
7895 switch (s[i])
7896 {
7897 default:
7898 ADVANCE_CHAR (s, len, i);
7899 break;
7900
7901 case '\\':
7902 i++;
7903 if (s[i])
7904 ADVANCE_CHAR (s, len, i);
7905 break;
7906
7907 case '\'':
7908 i = skip_single_quoted (s, len, ++i, 0);
7909 break;
7910
7911 case '"':
7912 i = skip_double_quoted ((char *)s, len, ++i, 0);
7913 break;
7914 }
7915 }
7916
7917 return (count == 0);
7918 }
7919
7920 /****************************************************************/
7921 /* */
7922 /* Functions to perform parameter expansion on a string */
7923 /* */
7924 /****************************************************************/
7925
7926 /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
7927 static WORD_DESC *
7928 parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at)
7929 char *string;
7930 int *indexp, quoted, pflags, *quoted_dollar_atp, *contains_dollar_at;
7931 {
7932 int check_nullness, var_is_set, var_is_null, var_is_special;
7933 int want_substring, want_indir, want_patsub, want_casemod;
7934 char *name, *value, *temp, *temp1;
7935 WORD_DESC *tdesc, *ret;
7936 int t_index, sindex, c, tflag, modspec, all_element_arrayref;
7937 intmax_t number;
7938 arrayind_t ind;
7939
7940 temp = temp1 = value = (char *)NULL;
7941 var_is_set = var_is_null = var_is_special = check_nullness = 0;
7942 want_substring = want_indir = want_patsub = want_casemod = 0;
7943
7944 all_element_arrayref = 0;
7945
7946 sindex = *indexp;
7947 t_index = ++sindex;
7948 /* ${#var} doesn't have any of the other parameter expansions on it. */
7949 if (string[t_index] == '#' && legal_variable_starter (string[t_index+1])) /* {{ */
7950 name = string_extract (string, &t_index, "}", SX_VARNAME);
7951 else
7952 #if defined (CASEMOD_EXPANSIONS)
7953 /* To enable case-toggling expansions using the `~' operator character
7954 change the 1 to 0. */
7955 # if defined (CASEMOD_CAPCASE)
7956 name = string_extract (string, &t_index, "#%^,~:-=?+/@}", SX_VARNAME);
7957 # else
7958 name = string_extract (string, &t_index, "#%^,:-=?+/@}", SX_VARNAME);
7959 # endif /* CASEMOD_CAPCASE */
7960 #else
7961 name = string_extract (string, &t_index, "#%:-=?+/@}", SX_VARNAME);
7962 #endif /* CASEMOD_EXPANSIONS */
7963
7964 /* Handle ${@[stuff]} now that @ is a word expansion operator. Not exactly
7965 the cleanest code ever. */
7966 if (*name == 0 && sindex == t_index && string[sindex] == '@')
7967 {
7968 name = (char *)xrealloc (name, 2);
7969 name[0] = '@';
7970 name[1] = '\0';
7971 t_index++;
7972 }
7973 else if (*name == '!' && t_index > sindex && string[t_index] == '@' && string[t_index+1] == '}')
7974 {
7975 name = (char *)xrealloc (name, t_index - sindex + 2);
7976 name[t_index - sindex] = '@';
7977 name[t_index - sindex + 1] = '\0';
7978 t_index++;
7979 }
7980
7981 ret = 0;
7982 tflag = 0;
7983
7984 ind = INTMAX_MIN;
7985
7986 /* If the name really consists of a special variable, then make sure
7987 that we have the entire name. We don't allow indirect references
7988 to special variables except `#', `?', `@' and `*'. This clause is
7989 designed to handle ${#SPECIAL} and ${!SPECIAL}, not anything more
7990 general. */
7991 if ((sindex == t_index && VALID_SPECIAL_LENGTH_PARAM (string[t_index])) ||
7992 (sindex == t_index && string[sindex] == '#' && VALID_SPECIAL_LENGTH_PARAM (string[sindex + 1])) ||
7993 (sindex == t_index - 1 && string[sindex] == '!' && VALID_INDIR_PARAM (string[t_index])))
7994 {
7995 t_index++;
7996 temp1 = string_extract (string, &t_index, "#%:-=?+/@}", 0);
7997 name = (char *)xrealloc (name, 3 + (strlen (temp1)));
7998 *name = string[sindex];
7999 if (string[sindex] == '!')
8000 {
8001 /* indirect reference of $#, $?, $@, or $* */
8002 name[1] = string[sindex + 1];
8003 strcpy (name + 2, temp1);
8004 }
8005 else
8006 strcpy (name + 1, temp1);
8007 free (temp1);
8008 }
8009 sindex = t_index;
8010
8011 /* Find out what character ended the variable name. Then
8012 do the appropriate thing. */
8013 if (c = string[sindex])
8014 sindex++;
8015
8016 /* If c is followed by one of the valid parameter expansion
8017 characters, move past it as normal. If not, assume that
8018 a substring specification is being given, and do not move
8019 past it. */
8020 if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
8021 {
8022 check_nullness++;
8023 if (c = string[sindex])
8024 sindex++;
8025 }
8026 else if (c == ':' && string[sindex] != RBRACE)
8027 want_substring = 1;
8028 else if (c == '/' /* && string[sindex] != RBRACE */) /* XXX */
8029 want_patsub = 1;
8030 #if defined (CASEMOD_EXPANSIONS)
8031 else if (c == '^' || c == ',' || c == '~')
8032 {
8033 modspec = c;
8034 want_casemod = 1;
8035 }
8036 #endif
8037
8038 /* Catch the valid and invalid brace expressions that made it through the
8039 tests above. */
8040 /* ${#-} is a valid expansion and means to take the length of $-.
8041 Similarly for ${#?} and ${##}... */
8042 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
8043 VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
8044 {
8045 name = (char *)xrealloc (name, 3);
8046 name[1] = c;
8047 name[2] = '\0';
8048 c = string[sindex++];
8049 }
8050
8051 /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
8052 if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
8053 member (c, "%:=+/") && string[sindex] == RBRACE)
8054 {
8055 temp = (char *)NULL;
8056 goto bad_substitution; /* XXX - substitution error */
8057 }
8058
8059 /* Indirect expansion begins with a `!'. A valid indirect expansion is
8060 either a variable name, one of the positional parameters or a special
8061 variable that expands to one of the positional parameters. */
8062 want_indir = *name == '!' &&
8063 (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
8064 || VALID_INDIR_PARAM (name[1]));
8065
8066 /* Determine the value of this variable. */
8067
8068 /* Check for special variables, directly referenced. */
8069 if (SPECIAL_VAR (name, want_indir))
8070 var_is_special++;
8071
8072 /* Check for special expansion things, like the length of a parameter */
8073 if (*name == '#' && name[1])
8074 {
8075 /* If we are not pointing at the character just after the
8076 closing brace, then we haven't gotten all of the name.
8077 Since it begins with a special character, this is a bad
8078 substitution. Also check NAME for validity before trying
8079 to go on. */
8080 if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
8081 {
8082 temp = (char *)NULL;
8083 goto bad_substitution; /* substitution error */
8084 }
8085
8086 number = parameter_brace_expand_length (name);
8087 if (number == INTMAX_MIN && unbound_vars_is_error)
8088 {
8089 last_command_exit_value = EXECUTION_FAILURE;
8090 err_unboundvar (name+1);
8091 free (name);
8092 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
8093 }
8094 free (name);
8095
8096 *indexp = sindex;
8097 if (number < 0)
8098 return (&expand_wdesc_error);
8099 else
8100 {
8101 ret = alloc_word_desc ();
8102 ret->word = itos (number);
8103 return ret;
8104 }
8105 }
8106
8107 /* ${@} is identical to $@. */
8108 if (name[0] == '@' && name[1] == '\0')
8109 {
8110 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
8111 *quoted_dollar_atp = 1;
8112
8113 if (contains_dollar_at)
8114 *contains_dollar_at = 1;
8115
8116 tflag |= W_DOLLARAT;
8117 }
8118
8119 /* Process ${!PREFIX*} expansion. */
8120 if (want_indir && string[sindex - 1] == RBRACE &&
8121 (string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
8122 legal_variable_starter ((unsigned char) name[1]))
8123 {
8124 char **x;
8125 WORD_LIST *xlist;
8126
8127 temp1 = savestring (name + 1);
8128 number = strlen (temp1);
8129 temp1[number - 1] = '\0';
8130 x = all_variables_matching_prefix (temp1);
8131 xlist = strvec_to_word_list (x, 0, 0);
8132 if (string[sindex - 2] == '*')
8133 temp = string_list_dollar_star (xlist);
8134 else
8135 {
8136 temp = string_list_dollar_at (xlist, quoted, 0);
8137 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
8138 *quoted_dollar_atp = 1;
8139 if (contains_dollar_at)
8140 *contains_dollar_at = 1;
8141
8142 tflag |= W_DOLLARAT;
8143 }
8144 free (x);
8145 dispose_words (xlist);
8146 free (temp1);
8147 *indexp = sindex;
8148
8149 free (name);
8150
8151 ret = alloc_word_desc ();
8152 ret->word = temp;
8153 ret->flags = tflag; /* XXX */
8154 return ret;
8155 }
8156
8157 #if defined (ARRAY_VARS)
8158 /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */
8159 if (want_indir && string[sindex - 1] == RBRACE &&
8160 string[sindex - 2] == ']' && valid_array_reference (name+1, 0))
8161 {
8162 char *x, *x1;
8163
8164 temp1 = savestring (name + 1);
8165 x = array_variable_name (temp1, &x1, (int *)0); /* [ */
8166 FREE (x);
8167 if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == ']')
8168 {
8169 temp = array_keys (temp1, quoted); /* handles assoc vars too */
8170 if (x1[0] == '@')
8171 {
8172 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
8173 *quoted_dollar_atp = 1;
8174 if (contains_dollar_at)
8175 *contains_dollar_at = 1;
8176
8177 tflag |= W_DOLLARAT;
8178 }
8179
8180 free (name);
8181 free (temp1);
8182 *indexp = sindex;
8183
8184 ret = alloc_word_desc ();
8185 ret->word = temp;
8186 ret->flags = tflag; /* XXX */
8187 return ret;
8188 }
8189
8190 free (temp1);
8191 }
8192 #endif /* ARRAY_VARS */
8193
8194 /* Make sure that NAME is valid before trying to go on. */
8195 if (valid_brace_expansion_word (want_indir ? name + 1 : name,
8196 var_is_special) == 0)
8197 {
8198 temp = (char *)NULL;
8199 goto bad_substitution; /* substitution error */
8200 }
8201
8202 if (want_indir)
8203 {
8204 tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at);
8205 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
8206 {
8207 temp = (char *)NULL;
8208 goto bad_substitution;
8209 }
8210 /* Turn off the W_ARRAYIND flag because there is no way for this function
8211 to return the index we're supposed to be using. */
8212 if (tdesc && tdesc->flags)
8213 tdesc->flags &= ~W_ARRAYIND;
8214 }
8215 else
8216 tdesc = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND|(pflags&(PF_NOSPLIT2|PF_ASSIGNRHS)), &ind);
8217
8218 if (tdesc)
8219 {
8220 temp = tdesc->word;
8221 tflag = tdesc->flags;
8222 dispose_word_desc (tdesc);
8223 }
8224 else
8225 temp = (char *)0;
8226
8227 if (temp == &expand_param_error || temp == &expand_param_fatal)
8228 {
8229 FREE (name);
8230 FREE (value);
8231 return (temp == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
8232 }
8233
8234 #if defined (ARRAY_VARS)
8235 if (valid_array_reference (name, 0))
8236 {
8237 int qflags;
8238 char *t;
8239
8240 qflags = quoted;
8241 /* If in a context where word splitting will not take place, treat as
8242 if double-quoted. Has effects with $* and ${array[*]} */
8243 if (pflags & PF_ASSIGNRHS)
8244 qflags |= Q_DOUBLE_QUOTES;
8245 chk_atstar (name, qflags, quoted_dollar_atp, contains_dollar_at);
8246 /* We duplicate a little code here */
8247 t = mbschr (name, '[');
8248 if (t && ALL_ELEMENT_SUB (t[1]) && t[2] == ']')
8249 all_element_arrayref = 1;
8250 }
8251 #endif
8252
8253 var_is_set = temp != (char *)0;
8254 var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
8255 /* XXX - this may not need to be restricted to special variables */
8256 if (check_nullness)
8257 var_is_null |= var_is_set && var_is_special && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && QUOTED_NULL (temp);
8258
8259 /* Get the rest of the stuff inside the braces. */
8260 if (c && c != RBRACE)
8261 {
8262 /* Extract the contents of the ${ ... } expansion
8263 according to the Posix.2 rules. */
8264 value = extract_dollar_brace_string (string, &sindex, quoted, (c == '%' || c == '#' || c =='/' || c == '^' || c == ',' || c ==':') ? SX_POSIXEXP|SX_WORD : SX_WORD);
8265 if (string[sindex] == RBRACE)
8266 sindex++;
8267 else
8268 goto bad_substitution; /* substitution error */
8269 }
8270 else
8271 value = (char *)NULL;
8272
8273 *indexp = sindex;
8274
8275 /* All the cases where an expansion can possibly generate an unbound
8276 variable error. */
8277 if (want_substring || want_patsub || want_casemod || c == '#' || c == '%' || c == RBRACE)
8278 {
8279 if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1]) && all_element_arrayref == 0)
8280 {
8281 last_command_exit_value = EXECUTION_FAILURE;
8282 err_unboundvar (name);
8283 FREE (value);
8284 FREE (temp);
8285 free (name);
8286 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
8287 }
8288 }
8289
8290 /* If this is a substring spec, process it and add the result. */
8291 if (want_substring)
8292 {
8293 temp1 = parameter_brace_substring (name, temp, ind, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
8294 FREE (name);
8295 FREE (value);
8296 FREE (temp);
8297
8298 if (temp1 == &expand_param_error)
8299 return (&expand_wdesc_error);
8300 else if (temp1 == &expand_param_fatal)
8301 return (&expand_wdesc_fatal);
8302
8303 ret = alloc_word_desc ();
8304 ret->word = temp1;
8305 /* We test quoted_dollar_atp because we want variants with double-quoted
8306 "$@" to take a different code path. In fact, we make sure at the end
8307 of expand_word_internal that we're only looking at these flags if
8308 quoted_dollar_at == 0. */
8309 if (temp1 &&
8310 (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
8311 QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
8312 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
8313 return ret;
8314 }
8315 else if (want_patsub)
8316 {
8317 temp1 = parameter_brace_patsub (name, temp, ind, value, quoted, pflags, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
8318 FREE (name);
8319 FREE (value);
8320 FREE (temp);
8321
8322 if (temp1 == &expand_param_error)
8323 return (&expand_wdesc_error);
8324 else if (temp1 == &expand_param_fatal)
8325 return (&expand_wdesc_fatal);
8326
8327 ret = alloc_word_desc ();
8328 ret->word = temp1;
8329 if (temp1 &&
8330 (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
8331 QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
8332 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
8333 return ret;
8334 }
8335 #if defined (CASEMOD_EXPANSIONS)
8336 else if (want_casemod)
8337 {
8338 temp1 = parameter_brace_casemod (name, temp, ind, modspec, value, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
8339 FREE (name);
8340 FREE (value);
8341 FREE (temp);
8342
8343 if (temp1 == &expand_param_error)
8344 return (&expand_wdesc_error);
8345 else if (temp1 == &expand_param_fatal)
8346 return (&expand_wdesc_fatal);
8347
8348 ret = alloc_word_desc ();
8349 ret->word = temp1;
8350 if (temp1 &&
8351 (quoted_dollar_atp == 0 || *quoted_dollar_atp == 0) &&
8352 QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
8353 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
8354 return ret;
8355 }
8356 #endif
8357
8358 /* Do the right thing based on which character ended the variable name. */
8359 switch (c)
8360 {
8361 default:
8362 case '\0':
8363 bad_substitution:
8364 last_command_exit_value = EXECUTION_FAILURE;
8365 report_error (_("%s: bad substitution"), string ? string : "??");
8366 FREE (value);
8367 FREE (temp);
8368 free (name);
8369 if (shell_compatibility_level <= 43)
8370 return &expand_wdesc_error;
8371 else
8372 return ((posixly_correct && interactive_shell == 0) ? &expand_wdesc_fatal : &expand_wdesc_error);
8373
8374 case RBRACE:
8375 break;
8376
8377 case '@':
8378 temp1 = parameter_brace_transform (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
8379 free (temp);
8380 free (value);
8381 free (name);
8382 if (temp1 == &expand_param_error || temp1 == &expand_param_fatal)
8383 {
8384 last_command_exit_value = EXECUTION_FAILURE;
8385 report_error (_("%s: bad substitution"), string ? string : "??");
8386 return (temp1 == &expand_param_error ? &expand_wdesc_error : &expand_wdesc_fatal);
8387 }
8388
8389 ret = alloc_word_desc ();
8390 ret->word = temp1;
8391 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
8392 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
8393 return ret;
8394
8395 case '#': /* ${param#[#]pattern} */
8396 case '%': /* ${param%[%]pattern} */
8397 if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
8398 {
8399 FREE (value);
8400 break;
8401 }
8402 temp1 = parameter_brace_remove_pattern (name, temp, ind, value, c, quoted, (tflag & W_ARRAYIND) ? AV_USEIND : 0);
8403 free (temp);
8404 free (value);
8405 free (name);
8406
8407 ret = alloc_word_desc ();
8408 ret->word = temp1;
8409 if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
8410 ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
8411 return ret;
8412
8413 case '-':
8414 case '=':
8415 case '?':
8416 case '+':
8417 if (var_is_set && var_is_null == 0)
8418 {
8419 /* If the operator is `+', we don't want the value of the named
8420 variable for anything, just the value of the right hand side. */
8421 if (c == '+')
8422 {
8423 /* XXX -- if we're double-quoted and the named variable is "$@",
8424 we want to turn off any special handling of "$@" --
8425 we're not using it, so whatever is on the rhs applies. */
8426 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
8427 *quoted_dollar_atp = 0;
8428 if (contains_dollar_at)
8429 *contains_dollar_at = 0;
8430
8431 FREE (temp);
8432 if (value)
8433 {
8434 /* From Posix discussion on austin-group list. Issue 221
8435 requires that backslashes escaping `}' inside
8436 double-quoted ${...} be removed. */
8437 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8438 quoted |= Q_DOLBRACE;
8439 ret = parameter_brace_expand_rhs (name, value, c,
8440 quoted,
8441 pflags,
8442 quoted_dollar_atp,
8443 contains_dollar_at);
8444 /* XXX - fix up later, esp. noting presence of
8445 W_HASQUOTEDNULL in ret->flags */
8446 free (value);
8447 }
8448 else
8449 temp = (char *)NULL;
8450 }
8451 else
8452 {
8453 FREE (value);
8454 }
8455 /* Otherwise do nothing; just use the value in TEMP. */
8456 }
8457 else /* VAR not set or VAR is NULL. */
8458 {
8459 FREE (temp);
8460 temp = (char *)NULL;
8461 if (c == '=' && var_is_special)
8462 {
8463 last_command_exit_value = EXECUTION_FAILURE;
8464 report_error (_("$%s: cannot assign in this way"), name);
8465 free (name);
8466 free (value);
8467 return &expand_wdesc_error;
8468 }
8469 else if (c == '?')
8470 {
8471 parameter_brace_expand_error (name, value);
8472 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
8473 }
8474 else if (c != '+')
8475 {
8476 /* XXX -- if we're double-quoted and the named variable is "$@",
8477 we want to turn off any special handling of "$@" --
8478 we're not using it, so whatever is on the rhs applies. */
8479 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
8480 *quoted_dollar_atp = 0;
8481 if (contains_dollar_at)
8482 *contains_dollar_at = 0;
8483
8484 /* From Posix discussion on austin-group list. Issue 221 requires
8485 that backslashes escaping `}' inside double-quoted ${...} be
8486 removed. */
8487 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
8488 quoted |= Q_DOLBRACE;
8489 ret = parameter_brace_expand_rhs (name, value, c, quoted, pflags,
8490 quoted_dollar_atp,
8491 contains_dollar_at);
8492 /* XXX - fix up later, esp. noting presence of
8493 W_HASQUOTEDNULL in tdesc->flags */
8494 }
8495 free (value);
8496 }
8497
8498 break;
8499 }
8500 free (name);
8501
8502 if (ret == 0)
8503 {
8504 ret = alloc_word_desc ();
8505 ret->flags = tflag;
8506 ret->word = temp;
8507 }
8508 return (ret);
8509 }
8510
8511 /* Expand a single ${xxx} expansion. The braces are optional. When
8512 the braces are used, parameter_brace_expand() does the work,
8513 possibly calling param_expand recursively. */
8514 static WORD_DESC *
8515 param_expand (string, sindex, quoted, expanded_something,
8516 contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
8517 pflags)
8518 char *string;
8519 int *sindex, quoted, *expanded_something, *contains_dollar_at;
8520 int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
8521 {
8522 char *temp, *temp1, uerror[3], *savecmd;
8523 int zindex, t_index, expok;
8524 unsigned char c;
8525 intmax_t number;
8526 SHELL_VAR *var;
8527 WORD_LIST *list;
8528 WORD_DESC *tdesc, *ret;
8529 int tflag;
8530
8531 /*itrace("param_expand: `%s' pflags = %d", string+*sindex, pflags);*/
8532 zindex = *sindex;
8533 c = string[++zindex];
8534
8535 temp = (char *)NULL;
8536 ret = tdesc = (WORD_DESC *)NULL;
8537 tflag = 0;
8538
8539 /* Do simple cases first. Switch on what follows '$'. */
8540 switch (c)
8541 {
8542 /* $0 .. $9? */
8543 case '0':
8544 case '1':
8545 case '2':
8546 case '3':
8547 case '4':
8548 case '5':
8549 case '6':
8550 case '7':
8551 case '8':
8552 case '9':
8553 temp1 = dollar_vars[TODIGIT (c)];
8554 if (unbound_vars_is_error && temp1 == (char *)NULL)
8555 {
8556 uerror[0] = '$';
8557 uerror[1] = c;
8558 uerror[2] = '\0';
8559 last_command_exit_value = EXECUTION_FAILURE;
8560 err_unboundvar (uerror);
8561 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
8562 }
8563 if (temp1)
8564 temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
8565 ? quote_string (temp1)
8566 : quote_escapes (temp1);
8567 else
8568 temp = (char *)NULL;
8569
8570 break;
8571
8572 /* $$ -- pid of the invoking shell. */
8573 case '$':
8574 temp = itos (dollar_dollar_pid);
8575 break;
8576
8577 /* $# -- number of positional parameters. */
8578 case '#':
8579 temp = itos (number_of_args ());
8580 break;
8581
8582 /* $? -- return value of the last synchronous command. */
8583 case '?':
8584 temp = itos (last_command_exit_value);
8585 break;
8586
8587 /* $- -- flags supplied to the shell on invocation or by `set'. */
8588 case '-':
8589 temp = which_set_flags ();
8590 break;
8591
8592 /* $! -- Pid of the last asynchronous command. */
8593 case '!':
8594 /* If no asynchronous pids have been created, expand to nothing.
8595 If `set -u' has been executed, and no async processes have
8596 been created, this is an expansion error. */
8597 if (last_asynchronous_pid == NO_PID)
8598 {
8599 if (expanded_something)
8600 *expanded_something = 0;
8601 temp = (char *)NULL;
8602 if (unbound_vars_is_error)
8603 {
8604 uerror[0] = '$';
8605 uerror[1] = c;
8606 uerror[2] = '\0';
8607 last_command_exit_value = EXECUTION_FAILURE;
8608 err_unboundvar (uerror);
8609 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
8610 }
8611 }
8612 else
8613 temp = itos (last_asynchronous_pid);
8614 break;
8615
8616 /* The only difference between this and $@ is when the arg is quoted. */
8617 case '*': /* `$*' */
8618 list = list_rest_of_args ();
8619
8620 #if 0
8621 /* According to austin-group posix proposal by Geoff Clare in
8622 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
8623
8624 "The shell shall write a message to standard error and
8625 immediately exit when it tries to expand an unset parameter
8626 other than the '@' and '*' special parameters."
8627 */
8628
8629 if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
8630 {
8631 uerror[0] = '$';
8632 uerror[1] = '*';
8633 uerror[2] = '\0';
8634 last_command_exit_value = EXECUTION_FAILURE;
8635 err_unboundvar (uerror);
8636 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
8637 }
8638 #endif
8639
8640 /* If there are no command-line arguments, this should just
8641 disappear if there are other characters in the expansion,
8642 even if it's quoted. */
8643 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
8644 temp = (char *)NULL;
8645 else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
8646 {
8647 /* If we have "$*" we want to make a string of the positional
8648 parameters, separated by the first character of $IFS, and
8649 quote the whole string, including the separators. If IFS
8650 is unset, the parameters are separated by ' '; if $IFS is
8651 null, the parameters are concatenated. */
8652 temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list) : string_list (list);
8653 if (temp)
8654 {
8655 temp1 = (quoted & Q_DOUBLE_QUOTES) ? quote_string (temp) : temp;
8656 if (*temp == 0)
8657 tflag |= W_HASQUOTEDNULL;
8658 if (temp != temp1)
8659 free (temp);
8660 temp = temp1;
8661 }
8662 }
8663 else
8664 {
8665 /* We check whether or not we're eventually going to split $* here,
8666 for example when IFS is empty and we are processing the rhs of
8667 an assignment statement. In that case, we don't separate the
8668 arguments at all. Otherwise, if the $* is not quoted it is
8669 identical to $@ */
8670 # if defined (HANDLE_MULTIBYTE)
8671 if (expand_no_split_dollar_star && ifs_firstc[0] == 0)
8672 # else
8673 if (expand_no_split_dollar_star && ifs_firstc == 0)
8674 # endif
8675 temp = string_list_dollar_star (list);
8676 else
8677 {
8678 temp = string_list_dollar_at (list, quoted, 0);
8679 if (quoted == 0 && (ifs_is_set == 0 || ifs_is_null))
8680 tflag |= W_SPLITSPACE;
8681 /* If we're not quoted but we still don't want word splitting, make
8682 we quote the IFS characters to protect them from splitting (e.g.,
8683 when $@ is in the string as well). */
8684 else if (temp && quoted == 0 && ifs_is_set && (pflags & PF_ASSIGNRHS))
8685 {
8686 temp1 = quote_string (temp);
8687 free (temp);
8688 temp = temp1;
8689 }
8690 }
8691
8692 if (expand_no_split_dollar_star == 0 && contains_dollar_at)
8693 *contains_dollar_at = 1;
8694 }
8695
8696 dispose_words (list);
8697 break;
8698
8699 /* When we have "$@" what we want is "$1" "$2" "$3" ... This
8700 means that we have to turn quoting off after we split into
8701 the individually quoted arguments so that the final split
8702 on the first character of $IFS is still done. */
8703 case '@': /* `$@' */
8704 list = list_rest_of_args ();
8705
8706 #if 0
8707 /* According to austin-group posix proposal by Geoff Clare in
8708 <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
8709
8710 "The shell shall write a message to standard error and
8711 immediately exit when it tries to expand an unset parameter
8712 other than the '@' and '*' special parameters."
8713 */
8714
8715 if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
8716 {
8717 uerror[0] = '$';
8718 uerror[1] = '@';
8719 uerror[2] = '\0';
8720 last_command_exit_value = EXECUTION_FAILURE;
8721 err_unboundvar (uerror);
8722 return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
8723 }
8724 #endif
8725
8726 /* We want to flag the fact that we saw this. We can't turn
8727 off quoting entirely, because other characters in the
8728 string might need it (consider "\"$@\""), but we need some
8729 way to signal that the final split on the first character
8730 of $IFS should be done, even though QUOTED is 1. */
8731 /* XXX - should this test include Q_PATQUOTE? */
8732 if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
8733 *quoted_dollar_at_p = 1;
8734 if (contains_dollar_at)
8735 *contains_dollar_at = 1;
8736
8737 /* We want to separate the positional parameters with the first
8738 character of $IFS in case $IFS is something other than a space.
8739 We also want to make sure that splitting is done no matter what --
8740 according to POSIX.2, this expands to a list of the positional
8741 parameters no matter what IFS is set to. */
8742 /* XXX - what to do when in a context where word splitting is not
8743 performed? Even when IFS is not the default, posix seems to imply
8744 that we behave like unquoted $* ? Maybe we should use PF_NOSPLIT2
8745 here. */
8746 /* XXX - bash-4.4/bash-5.0 passing PFLAGS */
8747 temp = string_list_dollar_at (list, (pflags & PF_ASSIGNRHS) ? (quoted|Q_DOUBLE_QUOTES) : quoted, pflags);
8748
8749 tflag |= W_DOLLARAT;
8750 dispose_words (list);
8751 break;
8752
8753 case LBRACE:
8754 tdesc = parameter_brace_expand (string, &zindex, quoted, pflags,
8755 quoted_dollar_at_p,
8756 contains_dollar_at);
8757
8758 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
8759 return (tdesc);
8760 temp = tdesc ? tdesc->word : (char *)0;
8761
8762 /* XXX */
8763 /* Quoted nulls should be removed if there is anything else
8764 in the string. */
8765 /* Note that we saw the quoted null so we can add one back at
8766 the end of this function if there are no other characters
8767 in the string, discard TEMP, and go on. The exception to
8768 this is when we have "${@}" and $1 is '', since $@ needs
8769 special handling. */
8770 if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp))
8771 {
8772 if (had_quoted_null_p)
8773 *had_quoted_null_p = 1;
8774 if (*quoted_dollar_at_p == 0)
8775 {
8776 free (temp);
8777 tdesc->word = temp = (char *)NULL;
8778 }
8779
8780 }
8781
8782 ret = tdesc;
8783 goto return0;
8784
8785 /* Do command or arithmetic substitution. */
8786 case LPAREN:
8787 /* We have to extract the contents of this paren substitution. */
8788 t_index = zindex + 1;
8789 /* XXX - might want to check for string[t_index+2] == LPAREN and parse
8790 as arithmetic substitution immediately. */
8791 temp = extract_command_subst (string, &t_index, (pflags&PF_COMPLETE) ? SX_COMPLETE : 0);
8792 zindex = t_index;
8793
8794 /* For Posix.2-style `$(( ))' arithmetic substitution,
8795 extract the expression and pass it to the evaluator. */
8796 if (temp && *temp == LPAREN)
8797 {
8798 char *temp2;
8799 temp1 = temp + 1;
8800 temp2 = savestring (temp1);
8801 t_index = strlen (temp2) - 1;
8802
8803 if (temp2[t_index] != RPAREN)
8804 {
8805 free (temp2);
8806 goto comsub;
8807 }
8808
8809 /* Cut off ending `)' */
8810 temp2[t_index] = '\0';
8811
8812 if (chk_arithsub (temp2, t_index) == 0)
8813 {
8814 free (temp2);
8815 #if 0
8816 internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
8817 #endif
8818 goto comsub;
8819 }
8820
8821 /* Expand variables found inside the expression. */
8822 temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES|Q_ARITH);
8823 free (temp2);
8824
8825 arithsub:
8826 /* No error messages. */
8827 savecmd = this_command_name;
8828 this_command_name = (char *)NULL;
8829 number = evalexp (temp1, &expok);
8830 this_command_name = savecmd;
8831 free (temp);
8832 free (temp1);
8833 if (expok == 0)
8834 {
8835 if (interactive_shell == 0 && posixly_correct)
8836 {
8837 last_command_exit_value = EXECUTION_FAILURE;
8838 return (&expand_wdesc_fatal);
8839 }
8840 else
8841 return (&expand_wdesc_error);
8842 }
8843 temp = itos (number);
8844 break;
8845 }
8846
8847 comsub:
8848 if (pflags & PF_NOCOMSUB)
8849 /* we need zindex+1 because string[zindex] == RPAREN */
8850 temp1 = substring (string, *sindex, zindex+1);
8851 else
8852 {
8853 tdesc = command_substitute (temp, quoted);
8854 temp1 = tdesc ? tdesc->word : (char *)NULL;
8855 if (tdesc)
8856 dispose_word_desc (tdesc);
8857 }
8858 FREE (temp);
8859 temp = temp1;
8860 break;
8861
8862 /* Do POSIX.2d9-style arithmetic substitution. This will probably go
8863 away in a future bash release. */
8864 case '[':
8865 /* Extract the contents of this arithmetic substitution. */
8866 t_index = zindex + 1;
8867 temp = extract_arithmetic_subst (string, &t_index);
8868 zindex = t_index;
8869 if (temp == 0)
8870 {
8871 temp = savestring (string);
8872 if (expanded_something)
8873 *expanded_something = 0;
8874 goto return0;
8875 }
8876
8877 /* Do initial variable expansion. */
8878 temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES|Q_ARITH);
8879
8880 goto arithsub;
8881
8882 default:
8883 /* Find the variable in VARIABLE_LIST. */
8884 temp = (char *)NULL;
8885
8886 for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
8887 ;
8888 temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
8889
8890 /* If this isn't a variable name, then just output the `$'. */
8891 if (temp1 == 0 || *temp1 == '\0')
8892 {
8893 FREE (temp1);
8894 temp = (char *)xmalloc (2);
8895 temp[0] = '$';
8896 temp[1] = '\0';
8897 if (expanded_something)
8898 *expanded_something = 0;
8899 goto return0;
8900 }
8901
8902 /* If the variable exists, return its value cell. */
8903 var = find_variable (temp1);
8904
8905 if (var && invisible_p (var) == 0 && var_isset (var))
8906 {
8907 #if defined (ARRAY_VARS)
8908 if (assoc_p (var) || array_p (var))
8909 {
8910 temp = array_p (var) ? array_reference (array_cell (var), 0)
8911 : assoc_reference (assoc_cell (var), "0");
8912 if (temp)
8913 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
8914 ? quote_string (temp)
8915 : quote_escapes (temp);
8916 else if (unbound_vars_is_error)
8917 goto unbound_variable;
8918 }
8919 else
8920 #endif
8921 {
8922 temp = value_cell (var);
8923
8924 temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
8925 ? quote_string (temp)
8926 : quote_escapes (temp);
8927 }
8928
8929 free (temp1);
8930
8931 goto return0;
8932 }
8933 else if (var && (invisible_p (var) || var_isset (var) == 0))
8934 temp = (char *)NULL;
8935 else if ((var = find_variable_last_nameref (temp1, 0)) && var_isset (var) && invisible_p (var) == 0)
8936 {
8937 temp = nameref_cell (var);
8938 #if defined (ARRAY_VARS)
8939 if (temp && *temp && valid_array_reference (temp, 0))
8940 {
8941 tdesc = parameter_brace_expand_word (temp, SPECIAL_VAR (temp, 0), quoted, pflags, (arrayind_t *)NULL);
8942 if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
8943 return (tdesc);
8944 ret = tdesc;
8945 goto return0;
8946 }
8947 else
8948 #endif
8949 /* y=2 ; typeset -n x=y; echo $x is not the same as echo $2 in ksh */
8950 if (temp && *temp && legal_identifier (temp) == 0)
8951 {
8952 last_command_exit_value = EXECUTION_FAILURE;
8953 report_error (_("%s: invalid variable name for name reference"), temp);
8954 return (&expand_wdesc_error); /* XXX */
8955 }
8956 else
8957 temp = (char *)NULL;
8958 }
8959
8960 temp = (char *)NULL;
8961
8962 unbound_variable:
8963 if (unbound_vars_is_error)
8964 {
8965 last_command_exit_value = EXECUTION_FAILURE;
8966 err_unboundvar (temp1);
8967 }
8968 else
8969 {
8970 free (temp1);
8971 goto return0;
8972 }
8973
8974 free (temp1);
8975 last_command_exit_value = EXECUTION_FAILURE;
8976 return ((unbound_vars_is_error && interactive_shell == 0)
8977 ? &expand_wdesc_fatal
8978 : &expand_wdesc_error);
8979 }
8980
8981 if (string[zindex])
8982 zindex++;
8983
8984 return0:
8985 *sindex = zindex;
8986
8987 if (ret == 0)
8988 {
8989 ret = alloc_word_desc ();
8990 ret->flags = tflag; /* XXX */
8991 ret->word = temp;
8992 }
8993 return ret;
8994 }
8995
8996 void
8997 invalidate_cached_quoted_dollar_at ()
8998 {
8999 dispose_words (cached_quoted_dollar_at);
9000 cached_quoted_dollar_at = 0;
9001 }
9002
9003 /* Make a word list which is the result of parameter and variable
9004 expansion, command substitution, arithmetic substitution, and
9005 quote removal of WORD. Return a pointer to a WORD_LIST which is
9006 the result of the expansion. If WORD contains a null word, the
9007 word list returned is also null.
9008
9009 QUOTED contains flag values defined in shell.h.
9010
9011 ISEXP is used to tell expand_word_internal that the word should be
9012 treated as the result of an expansion. This has implications for
9013 how IFS characters in the word are treated.
9014
9015 CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
9016 they point to an integer value which receives information about expansion.
9017 CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
9018 EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
9019 else zero.
9020
9021 This only does word splitting in the case of $@ expansion. In that
9022 case, we split on ' '. */
9023
9024 /* Values for the local variable quoted_state. */
9025 #define UNQUOTED 0
9026 #define PARTIALLY_QUOTED 1
9027 #define WHOLLY_QUOTED 2
9028
9029 static WORD_LIST *
9030 expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
9031 WORD_DESC *word;
9032 int quoted, isexp;
9033 int *contains_dollar_at;
9034 int *expanded_something;
9035 {
9036 WORD_LIST *list;
9037 WORD_DESC *tword;
9038
9039 /* The intermediate string that we build while expanding. */
9040 char *istring;
9041
9042 /* The current size of the above object. */
9043 size_t istring_size;
9044
9045 /* Index into ISTRING. */
9046 int istring_index;
9047
9048 /* Temporary string storage. */
9049 char *temp, *temp1;
9050
9051 /* The text of WORD. */
9052 register char *string;
9053
9054 /* The size of STRING. */
9055 size_t string_size;
9056
9057 /* The index into STRING. */
9058 int sindex;
9059
9060 /* This gets 1 if we see a $@ while quoted. */
9061 int quoted_dollar_at;
9062
9063 /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
9064 whether WORD contains no quoting characters, a partially quoted
9065 string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
9066 int quoted_state;
9067
9068 /* State flags */
9069 int had_quoted_null;
9070 int has_dollar_at, temp_has_dollar_at;
9071 int split_on_spaces;
9072 int tflag;
9073 int pflags; /* flags passed to param_expand */
9074 int mb_cur_max;
9075
9076 int assignoff; /* If assignment, offset of `=' */
9077
9078 register unsigned char c; /* Current character. */
9079 int t_index; /* For calls to string_extract_xxx. */
9080
9081 char twochars[2];
9082
9083 DECLARE_MBSTATE;
9084
9085 /* OK, let's see if we can optimize a common idiom: "$@" */
9086 if (STREQ (word->word, "\"$@\"") &&
9087 (word->flags == (W_HASDOLLAR|W_QUOTED)) &&
9088 dollar_vars[1]) /* XXX - check IFS here as well? */
9089 {
9090 if (contains_dollar_at)
9091 *contains_dollar_at = 1;
9092 if (expanded_something)
9093 *expanded_something = 1;
9094 if (cached_quoted_dollar_at)
9095 return (copy_word_list (cached_quoted_dollar_at));
9096 list = list_rest_of_args ();
9097 list = quote_list (list);
9098 cached_quoted_dollar_at = copy_word_list (list);
9099 return (list);
9100 }
9101
9102 istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
9103 istring[istring_index = 0] = '\0';
9104 quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
9105 split_on_spaces = 0;
9106 quoted_state = UNQUOTED;
9107
9108 string = word->word;
9109 if (string == 0)
9110 goto finished_with_string;
9111 mb_cur_max = MB_CUR_MAX;
9112
9113 /* Don't need the string length for the SADD... and COPY_ macros unless
9114 multibyte characters are possible. */
9115 string_size = (mb_cur_max > 1) ? strlen (string) : 1;
9116
9117 if (contains_dollar_at)
9118 *contains_dollar_at = 0;
9119
9120 assignoff = -1;
9121
9122 /* Begin the expansion. */
9123
9124 for (sindex = 0; ;)
9125 {
9126 c = string[sindex];
9127
9128 /* Case on top-level character. */
9129 switch (c)
9130 {
9131 case '\0':
9132 goto finished_with_string;
9133
9134 case CTLESC:
9135 sindex++;
9136 #if HANDLE_MULTIBYTE
9137 if (mb_cur_max > 1 && string[sindex])
9138 {
9139 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
9140 }
9141 else
9142 #endif
9143 {
9144 temp = (char *)xmalloc (3);
9145 temp[0] = CTLESC;
9146 temp[1] = c = string[sindex];
9147 temp[2] = '\0';
9148 }
9149
9150 dollar_add_string:
9151 if (string[sindex])
9152 sindex++;
9153
9154 add_string:
9155 if (temp)
9156 {
9157 istring = sub_append_string (temp, istring, &istring_index, &istring_size);
9158 temp = (char *)0;
9159 }
9160
9161 break;
9162
9163 #if defined (PROCESS_SUBSTITUTION)
9164 /* Process substitution. */
9165 case '<':
9166 case '>':
9167 {
9168 /* bash-4.4/bash-5.0
9169 XXX - technically this should only be expanded at the start
9170 of a word */
9171 if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)) || posixly_correct)
9172 {
9173 sindex--; /* add_character: label increments sindex */
9174 goto add_character;
9175 }
9176 else
9177 t_index = sindex + 1; /* skip past both '<' and LPAREN */
9178
9179 temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index, 0); /*))*/
9180 sindex = t_index;
9181
9182 /* If the process substitution specification is `<()', we want to
9183 open the pipe for writing in the child and produce output; if
9184 it is `>()', we want to open the pipe for reading in the child
9185 and consume input. */
9186 temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
9187
9188 FREE (temp1);
9189
9190 goto dollar_add_string;
9191 }
9192 #endif /* PROCESS_SUBSTITUTION */
9193
9194 case '=':
9195 /* Posix.2 section 3.6.1 says that tildes following `=' in words
9196 which are not assignment statements are not expanded. If the
9197 shell isn't in posix mode, though, we perform tilde expansion
9198 on `likely candidate' unquoted assignment statements (flags
9199 include W_ASSIGNMENT but not W_QUOTED). A likely candidate
9200 contains an unquoted :~ or =~. Something to think about: we
9201 now have a flag that says to perform tilde expansion on arguments
9202 to `assignment builtins' like declare and export that look like
9203 assignment statements. We now do tilde expansion on such words
9204 even in POSIX mode. */
9205 if (word->flags & (W_ASSIGNRHS|W_NOTILDE))
9206 {
9207 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
9208 goto add_ifs_character;
9209 else
9210 goto add_character;
9211 }
9212 /* If we're not in posix mode or forcing assignment-statement tilde
9213 expansion, note where the `=' appears in the word and prepare to
9214 do tilde expansion following the first `='. */
9215 if ((word->flags & W_ASSIGNMENT) &&
9216 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
9217 assignoff == -1 && sindex > 0)
9218 assignoff = sindex;
9219 if (sindex == assignoff && string[sindex+1] == '~') /* XXX */
9220 word->flags |= W_ITILDE;
9221 #if 0
9222 else if ((word->flags & W_ASSIGNMENT) &&
9223 (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
9224 string[sindex+1] == '~')
9225 word->flags |= W_ITILDE;
9226 #endif
9227
9228 /* XXX - bash-4.4/bash-5.0 */
9229 if (word->flags & W_ASSIGNARG)
9230 word->flags |= W_ASSIGNRHS; /* affects $@ */
9231
9232 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
9233 goto add_ifs_character;
9234 else
9235 goto add_character;
9236
9237 case ':':
9238 if (word->flags & W_NOTILDE)
9239 {
9240 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
9241 goto add_ifs_character;
9242 else
9243 goto add_character;
9244 }
9245
9246 if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) &&
9247 string[sindex+1] == '~')
9248 word->flags |= W_ITILDE;
9249
9250 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
9251 goto add_ifs_character;
9252 else
9253 goto add_character;
9254
9255 case '~':
9256 /* If the word isn't supposed to be tilde expanded, or we're not
9257 at the start of a word or after an unquoted : or = in an
9258 assignment statement, we don't do tilde expansion. If we don't want
9259 tilde expansion when expanding words to be passed to the arithmetic
9260 evaluator, remove the check for Q_ARITH. */
9261 if ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
9262 (sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
9263 ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && ((quoted & Q_ARITH) == 0)))
9264 {
9265 word->flags &= ~W_ITILDE;
9266 if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
9267 goto add_ifs_character;
9268 else
9269 goto add_character;
9270 }
9271
9272 if (word->flags & W_ASSIGNRHS)
9273 tflag = 2;
9274 else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
9275 tflag = 1;
9276 else
9277 tflag = 0;
9278
9279 temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
9280
9281 word->flags &= ~W_ITILDE;
9282
9283 if (temp && *temp && t_index > 0)
9284 {
9285 temp1 = bash_tilde_expand (temp, tflag);
9286 if (temp1 && *temp1 == '~' && STREQ (temp, temp1))
9287 {
9288 FREE (temp);
9289 FREE (temp1);
9290 goto add_character; /* tilde expansion failed */
9291 }
9292 free (temp);
9293 temp = temp1;
9294 sindex += t_index;
9295 goto add_quoted_string; /* XXX was add_string */
9296 }
9297 else
9298 {
9299 FREE (temp);
9300 goto add_character;
9301 }
9302
9303 case '$':
9304 if (expanded_something)
9305 *expanded_something = 1;
9306
9307 temp_has_dollar_at = 0;
9308 pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0;
9309 if (word->flags & W_NOSPLIT2)
9310 pflags |= PF_NOSPLIT2;
9311 if (word->flags & W_ASSIGNRHS)
9312 pflags |= PF_ASSIGNRHS;
9313 if (word->flags & W_COMPLETE)
9314 pflags |= PF_COMPLETE;
9315 tword = param_expand (string, &sindex, quoted, expanded_something,
9316 &temp_has_dollar_at, &quoted_dollar_at,
9317 &had_quoted_null, pflags);
9318 has_dollar_at += temp_has_dollar_at;
9319 split_on_spaces += (tword->flags & W_SPLITSPACE);
9320
9321 if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
9322 {
9323 free (string);
9324 free (istring);
9325 return ((tword == &expand_wdesc_error) ? &expand_word_error
9326 : &expand_word_fatal);
9327 }
9328 if (contains_dollar_at && has_dollar_at)
9329 *contains_dollar_at = 1;
9330
9331 if (tword && (tword->flags & W_HASQUOTEDNULL))
9332 had_quoted_null = 1;
9333
9334 temp = tword ? tword->word : (char *)NULL;
9335 dispose_word_desc (tword);
9336
9337 /* Kill quoted nulls; we will add them back at the end of
9338 expand_word_internal if nothing else in the string */
9339 if (had_quoted_null && temp && QUOTED_NULL (temp))
9340 {
9341 FREE (temp);
9342 temp = (char *)NULL;
9343 }
9344
9345 goto add_string;
9346 break;
9347
9348 case '`': /* Backquoted command substitution. */
9349 {
9350 t_index = sindex++;
9351
9352 temp = string_extract (string, &sindex, "`", SX_REQMATCH);
9353 /* The test of sindex against t_index is to allow bare instances of
9354 ` to pass through, for backwards compatibility. */
9355 if (temp == &extract_string_error || temp == &extract_string_fatal)
9356 {
9357 if (sindex - 1 == t_index)
9358 {
9359 sindex = t_index;
9360 goto add_character;
9361 }
9362 last_command_exit_value = EXECUTION_FAILURE;
9363 report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index);
9364 free (string);
9365 free (istring);
9366 return ((temp == &extract_string_error) ? &expand_word_error
9367 : &expand_word_fatal);
9368 }
9369
9370 if (expanded_something)
9371 *expanded_something = 1;
9372
9373 if (word->flags & W_NOCOMSUB)
9374 /* sindex + 1 because string[sindex] == '`' */
9375 temp1 = substring (string, t_index, sindex + 1);
9376 else
9377 {
9378 de_backslash (temp);
9379 tword = command_substitute (temp, quoted);
9380 temp1 = tword ? tword->word : (char *)NULL;
9381 if (tword)
9382 dispose_word_desc (tword);
9383 }
9384 FREE (temp);
9385 temp = temp1;
9386 goto dollar_add_string;
9387 }
9388
9389 case '\\':
9390 if (string[sindex + 1] == '\n')
9391 {
9392 sindex += 2;
9393 continue;
9394 }
9395
9396 c = string[++sindex];
9397
9398 if (quoted & Q_HERE_DOCUMENT)
9399 tflag = CBSHDOC;
9400 else if (quoted & Q_DOUBLE_QUOTES)
9401 tflag = CBSDQUOTE;
9402 else
9403 tflag = 0;
9404
9405 /* From Posix discussion on austin-group list: Backslash escaping
9406 a } in ${...} is removed. Issue 0000221 */
9407 if ((quoted & Q_DOLBRACE) && c == RBRACE)
9408 {
9409 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
9410 }
9411 /* This is the fix for " $@\ " */
9412 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0) && isexp == 0 && isifs (c))
9413 {
9414 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
9415 DEFAULT_ARRAY_SIZE);
9416 istring[istring_index++] = CTLESC;
9417 istring[istring_index++] = '\\';
9418 istring[istring_index] = '\0';
9419
9420 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
9421 }
9422 else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
9423 {
9424 SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
9425 }
9426 else if (c == 0)
9427 {
9428 c = CTLNUL;
9429 sindex--; /* add_character: label increments sindex */
9430 goto add_character;
9431 }
9432 else
9433 {
9434 SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
9435 }
9436
9437 sindex++;
9438 add_twochars:
9439 /* BEFORE jumping here, we need to increment sindex if appropriate */
9440 RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
9441 DEFAULT_ARRAY_SIZE);
9442 istring[istring_index++] = twochars[0];
9443 istring[istring_index++] = twochars[1];
9444 istring[istring_index] = '\0';
9445
9446 break;
9447
9448 case '"':
9449 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) && ((quoted & Q_ARITH) == 0))
9450 goto add_character;
9451
9452 t_index = ++sindex;
9453 temp = string_extract_double_quoted (string, &sindex, (word->flags & W_COMPLETE) ? SX_COMPLETE : 0);
9454
9455 /* If the quotes surrounded the entire string, then the
9456 whole word was quoted. */
9457 quoted_state = (t_index == 1 && string[sindex] == '\0')
9458 ? WHOLLY_QUOTED
9459 : PARTIALLY_QUOTED;
9460
9461 if (temp && *temp)
9462 {
9463 tword = alloc_word_desc ();
9464 tword->word = temp;
9465
9466 /* XXX - bash-4.4/bash-5.0 */
9467 if (word->flags & W_ASSIGNARG)
9468 tword->flags |= word->flags & (W_ASSIGNARG|W_ASSIGNRHS); /* affects $@ */
9469 if (word->flags & W_COMPLETE)
9470 tword->flags |= W_COMPLETE; /* for command substitutions */
9471 if (word->flags & W_NOCOMSUB)
9472 tword->flags |= W_NOCOMSUB;
9473 if (word->flags & W_NOPROCSUB)
9474 tword->flags |= W_NOPROCSUB;
9475
9476 temp = (char *)NULL;
9477
9478 temp_has_dollar_at = 0; /* XXX */
9479 /* Need to get W_HASQUOTEDNULL flag through this function. */
9480 list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &temp_has_dollar_at, (int *)NULL);
9481 has_dollar_at += temp_has_dollar_at;
9482
9483 if (list == &expand_word_error || list == &expand_word_fatal)
9484 {
9485 free (istring);
9486 free (string);
9487 /* expand_word_internal has already freed temp_word->word
9488 for us because of the way it prints error messages. */
9489 tword->word = (char *)NULL;
9490 dispose_word (tword);
9491 return list;
9492 }
9493
9494 dispose_word (tword);
9495
9496 /* "$@" (a double-quoted dollar-at) expands into nothing,
9497 not even a NULL word, when there are no positional
9498 parameters. */
9499 if (list == 0 && temp_has_dollar_at) /* XXX - was has_dollar_at */
9500 {
9501 quoted_dollar_at++;
9502 break;
9503 }
9504
9505 /* If we get "$@", we know we have expanded something, so we
9506 need to remember it for the final split on $IFS. This is
9507 a special case; it's the only case where a quoted string
9508 can expand into more than one word. It's going to come back
9509 from the above call to expand_word_internal as a list with
9510 a single word, in which all characters are quoted and
9511 separated by blanks. What we want to do is to turn it back
9512 into a list for the next piece of code. */
9513 if (list)
9514 dequote_list (list);
9515
9516 if (list && list->word && (list->word->flags & W_HASQUOTEDNULL))
9517 had_quoted_null = 1; /* XXX */
9518
9519 if (temp_has_dollar_at) /* XXX - was has_dollar_at */
9520 {
9521 quoted_dollar_at++;
9522 if (contains_dollar_at)
9523 *contains_dollar_at = 1;
9524 if (expanded_something)
9525 *expanded_something = 1;
9526 }
9527 }
9528 else
9529 {
9530 /* What we have is "". This is a minor optimization. */
9531 FREE (temp);
9532 list = (WORD_LIST *)NULL;
9533 }
9534
9535 /* The code above *might* return a list (consider the case of "$@",
9536 where it returns "$1", "$2", etc.). We can't throw away the
9537 rest of the list, and we have to make sure each word gets added
9538 as quoted. We test on tresult->next: if it is non-NULL, we
9539 quote the whole list, save it to a string with string_list, and
9540 add that string. We don't need to quote the results of this
9541 (and it would be wrong, since that would quote the separators
9542 as well), so we go directly to add_string. */
9543 if (list)
9544 {
9545 if (list->next)
9546 {
9547 /* Testing quoted_dollar_at makes sure that "$@" is
9548 split correctly when $IFS does not contain a space. */
9549 temp = quoted_dollar_at
9550 ? string_list_dollar_at (list, Q_DOUBLE_QUOTES, 0)
9551 : string_list (quote_list (list));
9552 dispose_words (list);
9553 goto add_string;
9554 }
9555 else
9556 {
9557 temp = savestring (list->word->word);
9558 tflag = list->word->flags;
9559 dispose_words (list);
9560
9561 /* If the string is not a quoted null string, we want
9562 to remove any embedded unquoted CTLNUL characters.
9563 We do not want to turn quoted null strings back into
9564 the empty string, though. We do this because we
9565 want to remove any quoted nulls from expansions that
9566 contain other characters. For example, if we have
9567 x"$*"y or "x$*y" and there are no positional parameters,
9568 the $* should expand into nothing. */
9569 /* We use the W_HASQUOTEDNULL flag to differentiate the
9570 cases: a quoted null character as above and when
9571 CTLNUL is contained in the (non-null) expansion
9572 of some variable. We use the had_quoted_null flag to
9573 pass the value through this function to its caller. */
9574 if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0)
9575 remove_quoted_nulls (temp); /* XXX */
9576 }
9577 }
9578 else
9579 temp = (char *)NULL;
9580
9581 /* We do not want to add quoted nulls to strings that are only
9582 partially quoted; we can throw them away. The exception to
9583 this is when we are going to be performing word splitting,
9584 since we have to preserve a null argument if the next character
9585 will cause word splitting. */
9586 if (temp == 0 && quoted_state == PARTIALLY_QUOTED && (word->flags & (W_NOSPLIT|W_NOSPLIT2)))
9587 continue;
9588
9589 add_quoted_string:
9590
9591 if (temp)
9592 {
9593 temp1 = temp;
9594 temp = quote_string (temp);
9595 free (temp1);
9596 goto add_string;
9597 }
9598 else
9599 {
9600 /* Add NULL arg. */
9601 c = CTLNUL;
9602 sindex--; /* add_character: label increments sindex */
9603 goto add_character;
9604 }
9605
9606 /* break; */
9607
9608 case '\'':
9609 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
9610 goto add_character;
9611
9612 t_index = ++sindex;
9613 temp = string_extract_single_quoted (string, &sindex);
9614
9615 /* If the entire STRING was surrounded by single quotes,
9616 then the string is wholly quoted. */
9617 quoted_state = (t_index == 1 && string[sindex] == '\0')
9618 ? WHOLLY_QUOTED
9619 : PARTIALLY_QUOTED;
9620
9621 /* If all we had was '', it is a null expansion. */
9622 if (*temp == '\0')
9623 {
9624 free (temp);
9625 temp = (char *)NULL;
9626 }
9627 else
9628 remove_quoted_escapes (temp); /* ??? */
9629
9630 /* We do not want to add quoted nulls to strings that are only
9631 partially quoted; such nulls are discarded. See above for the
9632 exception, which is when the string is going to be split. */
9633 if (temp == 0 && (quoted_state == PARTIALLY_QUOTED) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)))
9634 continue;
9635
9636 /* If we have a quoted null expansion, add a quoted NULL to istring. */
9637 if (temp == 0)
9638 {
9639 c = CTLNUL;
9640 sindex--; /* add_character: label increments sindex */
9641 goto add_character;
9642 }
9643 else
9644 goto add_quoted_string;
9645
9646 /* break; */
9647
9648 default:
9649 /* This is the fix for " $@ " */
9650 add_ifs_character:
9651 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c) && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0))
9652 {
9653 if (string[sindex]) /* from old goto dollar_add_string */
9654 sindex++;
9655 if (c == 0)
9656 {
9657 c = CTLNUL;
9658 goto add_character;
9659 }
9660 else
9661 {
9662 #if HANDLE_MULTIBYTE
9663 if (mb_cur_max > 1)
9664 sindex--;
9665
9666 if (mb_cur_max > 1)
9667 {
9668 SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
9669 }
9670 else
9671 #endif
9672 {
9673 twochars[0] = CTLESC;
9674 twochars[1] = c;
9675 goto add_twochars;
9676 }
9677 }
9678 }
9679
9680 SADD_MBCHAR (temp, string, sindex, string_size);
9681
9682 add_character:
9683 RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
9684 DEFAULT_ARRAY_SIZE);
9685 istring[istring_index++] = c;
9686 istring[istring_index] = '\0';
9687
9688 /* Next character. */
9689 sindex++;
9690 }
9691 }
9692
9693 finished_with_string:
9694 /* OK, we're ready to return. If we have a quoted string, and
9695 quoted_dollar_at is not set, we do no splitting at all; otherwise
9696 we split on ' '. The routines that call this will handle what to
9697 do if nothing has been expanded. */
9698
9699 /* Partially and wholly quoted strings which expand to the empty
9700 string are retained as an empty arguments. Unquoted strings
9701 which expand to the empty string are discarded. The single
9702 exception is the case of expanding "$@" when there are no
9703 positional parameters. In that case, we discard the expansion. */
9704
9705 /* Because of how the code that handles "" and '' in partially
9706 quoted strings works, we need to make ISTRING into a QUOTED_NULL
9707 if we saw quoting characters, but the expansion was empty.
9708 "" and '' are tossed away before we get to this point when
9709 processing partially quoted strings. This makes "" and $xxx""
9710 equivalent when xxx is unset. We also look to see whether we
9711 saw a quoted null from a ${} expansion and add one back if we
9712 need to. */
9713
9714 /* If we expand to nothing and there were no single or double quotes
9715 in the word, we throw it away. Otherwise, we return a NULL word.
9716 The single exception is for $@ surrounded by double quotes when
9717 there are no positional parameters. In that case, we also throw
9718 the word away. */
9719
9720 if (*istring == '\0')
9721 {
9722 if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
9723 {
9724 istring[0] = CTLNUL;
9725 istring[1] = '\0';
9726 tword = make_bare_word (istring);
9727 tword->flags |= W_HASQUOTEDNULL; /* XXX */
9728 list = make_word_list (tword, (WORD_LIST *)NULL);
9729 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
9730 tword->flags |= W_QUOTED;
9731 }
9732 /* According to sh, ksh, and Posix.2, if a word expands into nothing
9733 and a double-quoted "$@" appears anywhere in it, then the entire
9734 word is removed. */
9735 /* XXX - exception appears to be that quoted null strings result in
9736 null arguments */
9737 else if (quoted_state == UNQUOTED || quoted_dollar_at)
9738 list = (WORD_LIST *)NULL;
9739 #if 0
9740 else
9741 {
9742 tword = make_bare_word (istring);
9743 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
9744 tword->flags |= W_QUOTED;
9745 list = make_word_list (tword, (WORD_LIST *)NULL);
9746 }
9747 #else
9748 else
9749 list = (WORD_LIST *)NULL;
9750 #endif
9751 }
9752 else if (word->flags & W_NOSPLIT)
9753 {
9754 tword = make_bare_word (istring);
9755 if (word->flags & W_ASSIGNMENT)
9756 tword->flags |= W_ASSIGNMENT; /* XXX */
9757 if (word->flags & W_COMPASSIGN)
9758 tword->flags |= W_COMPASSIGN; /* XXX */
9759 if (word->flags & W_NOGLOB)
9760 tword->flags |= W_NOGLOB; /* XXX */
9761 if (word->flags & W_NOBRACE)
9762 tword->flags |= W_NOBRACE; /* XXX */
9763 if (word->flags & W_NOEXPAND)
9764 tword->flags |= W_NOEXPAND; /* XXX */
9765 if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
9766 tword->flags |= W_QUOTED;
9767 if (had_quoted_null && QUOTED_NULL (istring))
9768 tword->flags |= W_HASQUOTEDNULL;
9769 list = make_word_list (tword, (WORD_LIST *)NULL);
9770 }
9771 else
9772 {
9773 char *ifs_chars;
9774 char *tstring;
9775
9776 ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
9777
9778 /* If we have $@, we need to split the results no matter what. If
9779 IFS is unset or NULL, string_list_dollar_at has separated the
9780 positional parameters with a space, so we split on space (we have
9781 set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
9782 string_list_dollar_at has separated the positional parameters
9783 with the first character of $IFS, so we split on $IFS. If
9784 SPLIT_ON_SPACES is set, we expanded $* (unquoted) with IFS either
9785 unset or null, and we want to make sure that we split on spaces
9786 regardless of what else has happened to IFS since the expansion,
9787 or we expanded "$@" with IFS null and we need to split the positional
9788 parameters into separate words. */
9789 if (split_on_spaces)
9790 list = list_string (istring, " ", 1); /* XXX quoted == 1? */
9791 /* If we have $@ (has_dollar_at != 0) and we are in a context where we
9792 don't want to split the result (W_NOSPLIT2), and we are not quoted,
9793 we have already separated the arguments with the first character of
9794 $IFS. In this case, we want to return a list with a single word
9795 with the separator possibly replaced with a space (it's what other
9796 shells seem to do).
9797 quoted_dollar_at is internal to this function and is set if we are
9798 passed an argument that is unquoted (quoted == 0) but we encounter a
9799 double-quoted $@ while expanding it. */
9800 else if (has_dollar_at && quoted_dollar_at == 0 && ifs_chars && quoted == 0 && (word->flags & W_NOSPLIT2))
9801 {
9802 /* Only split and rejoin if we have to */
9803 if (*ifs_chars && *ifs_chars != ' ')
9804 {
9805 list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
9806 tstring = string_list (list);
9807 }
9808 else
9809 tstring = istring;
9810 tword = make_bare_word (tstring);
9811 if (tstring != istring)
9812 free (tstring);
9813 goto set_word_flags;
9814 }
9815 /* This is the attempt to make $* in an assignment context (a=$*) and
9816 array variables subscripted with * in an assignment context (a=${foo[*]})
9817 behave similarly. It has side effects that, though they increase
9818 compatibility with other shells, are not backwards compatible. */
9819 #if 0
9820 else if (has_dollar_at && quoted == 0 && ifs_chars && (word->flags & W_ASSIGNRHS))
9821 {
9822 tword = make_bare_word (istring);
9823 goto set_word_flags;
9824 }
9825 #endif
9826 else if (has_dollar_at && ifs_chars)
9827 list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
9828 else
9829 {
9830 tword = make_bare_word (istring);
9831 set_word_flags:
9832 if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
9833 tword->flags |= W_QUOTED;
9834 if (word->flags & W_ASSIGNMENT)
9835 tword->flags |= W_ASSIGNMENT;
9836 if (word->flags & W_COMPASSIGN)
9837 tword->flags |= W_COMPASSIGN;
9838 if (word->flags & W_NOGLOB)
9839 tword->flags |= W_NOGLOB;
9840 if (word->flags & W_NOBRACE)
9841 tword->flags |= W_NOBRACE;
9842 if (word->flags & W_NOEXPAND)
9843 tword->flags |= W_NOEXPAND;
9844 if (had_quoted_null && QUOTED_NULL (istring))
9845 tword->flags |= W_HASQUOTEDNULL; /* XXX */
9846 list = make_word_list (tword, (WORD_LIST *)NULL);
9847 }
9848 }
9849
9850 free (istring);
9851 return (list);
9852 }
9853
9854 /* **************************************************************** */
9855 /* */
9856 /* Functions for Quote Removal */
9857 /* */
9858 /* **************************************************************** */
9859
9860 /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
9861 backslash quoting rules for within double quotes or a here document. */
9862 char *
9863 string_quote_removal (string, quoted)
9864 char *string;
9865 int quoted;
9866 {
9867 size_t slen;
9868 char *r, *result_string, *temp, *send;
9869 int sindex, tindex, dquote;
9870 unsigned char c;
9871 DECLARE_MBSTATE;
9872
9873 /* The result can be no longer than the original string. */
9874 slen = strlen (string);
9875 send = string + slen;
9876
9877 r = result_string = (char *)xmalloc (slen + 1);
9878
9879 for (dquote = sindex = 0; c = string[sindex];)
9880 {
9881 switch (c)
9882 {
9883 case '\\':
9884 c = string[++sindex];
9885 if (c == 0)
9886 {
9887 *r++ = '\\';
9888 break;
9889 }
9890 if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
9891 *r++ = '\\';
9892 /* FALLTHROUGH */
9893
9894 default:
9895 SCOPY_CHAR_M (r, string, send, sindex);
9896 break;
9897
9898 case '\'':
9899 if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
9900 {
9901 *r++ = c;
9902 sindex++;
9903 break;
9904 }
9905 tindex = sindex + 1;
9906 temp = string_extract_single_quoted (string, &tindex);
9907 if (temp)
9908 {
9909 strcpy (r, temp);
9910 r += strlen (r);
9911 free (temp);
9912 }
9913 sindex = tindex;
9914 break;
9915
9916 case '"':
9917 dquote = 1 - dquote;
9918 sindex++;
9919 break;
9920 }
9921 }
9922 *r = '\0';
9923 return (result_string);
9924 }
9925
9926 #if 0
9927 /* UNUSED */
9928 /* Perform quote removal on word WORD. This allocates and returns a new
9929 WORD_DESC *. */
9930 WORD_DESC *
9931 word_quote_removal (word, quoted)
9932 WORD_DESC *word;
9933 int quoted;
9934 {
9935 WORD_DESC *w;
9936 char *t;
9937
9938 t = string_quote_removal (word->word, quoted);
9939 w = alloc_word_desc ();
9940 w->word = t ? t : savestring ("");
9941 return (w);
9942 }
9943
9944 /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
9945 the members of the list are treated as if they are surrounded by
9946 double quotes. Return a new list, or NULL if LIST is NULL. */
9947 WORD_LIST *
9948 word_list_quote_removal (list, quoted)
9949 WORD_LIST *list;
9950 int quoted;
9951 {
9952 WORD_LIST *result, *t, *tresult, *e;
9953
9954 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
9955 {
9956 tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
9957 #if 0
9958 result = (WORD_LIST *) list_append (result, tresult);
9959 #else
9960 if (result == 0)
9961 result = e = tresult;
9962 else
9963 {
9964 e->next = tresult;
9965 while (e->next)
9966 e = e->next;
9967 }
9968 #endif
9969 }
9970 return (result);
9971 }
9972 #endif
9973
9974 /*******************************************
9975 * *
9976 * Functions to perform word splitting *
9977 * *
9978 *******************************************/
9979
9980 void
9981 setifs (v)
9982 SHELL_VAR *v;
9983 {
9984 char *t;
9985 unsigned char uc;
9986
9987 ifs_var = v;
9988 ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n";
9989
9990 ifs_is_set = ifs_var != 0;
9991 ifs_is_null = ifs_is_set && (*ifs_value == 0);
9992
9993 /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
9994 handle multibyte chars in IFS */
9995 memset (ifs_cmap, '\0', sizeof (ifs_cmap));
9996 for (t = ifs_value ; t && *t; t++)
9997 {
9998 uc = *t;
9999 ifs_cmap[uc] = 1;
10000 }
10001
10002 #if defined (HANDLE_MULTIBYTE)
10003 if (ifs_value == 0)
10004 {
10005 ifs_firstc[0] = '\0';
10006 ifs_firstc_len = 1;
10007 }
10008 else
10009 {
10010 size_t ifs_len;
10011 ifs_len = strnlen (ifs_value, MB_CUR_MAX);
10012 ifs_firstc_len = MBLEN (ifs_value, ifs_len);
10013 if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len))
10014 {
10015 ifs_firstc[0] = ifs_value[0];
10016 ifs_firstc[1] = '\0';
10017 ifs_firstc_len = 1;
10018 }
10019 else
10020 memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
10021 }
10022 #else
10023 ifs_firstc = ifs_value ? *ifs_value : 0;
10024 #endif
10025 }
10026
10027 char *
10028 getifs ()
10029 {
10030 return ifs_value;
10031 }
10032
10033 /* This splits a single word into a WORD LIST on $IFS, but only if the word
10034 is not quoted. list_string () performs quote removal for us, even if we
10035 don't do any splitting. */
10036 WORD_LIST *
10037 word_split (w, ifs_chars)
10038 WORD_DESC *w;
10039 char *ifs_chars;
10040 {
10041 WORD_LIST *result;
10042
10043 if (w)
10044 {
10045 char *xifs;
10046
10047 xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
10048 result = list_string (w->word, xifs, w->flags & W_QUOTED);
10049 }
10050 else
10051 result = (WORD_LIST *)NULL;
10052
10053 return (result);
10054 }
10055
10056 /* Perform word splitting on LIST and return the RESULT. It is possible
10057 to return (WORD_LIST *)NULL. */
10058 static WORD_LIST *
10059 word_list_split (list)
10060 WORD_LIST *list;
10061 {
10062 WORD_LIST *result, *t, *tresult, *e;
10063
10064 for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
10065 {
10066 tresult = word_split (t->word, ifs_value);
10067 if (result == 0)
10068 result = e = tresult;
10069 else
10070 {
10071 e->next = tresult;
10072 while (e->next)
10073 e = e->next;
10074 }
10075 }
10076 return (result);
10077 }
10078
10079 /**************************************************
10080 * *
10081 * Functions to expand an entire WORD_LIST *
10082 * *
10083 **************************************************/
10084
10085 /* Do any word-expansion-specific cleanup and jump to top_level */
10086 static void
10087 exp_jump_to_top_level (v)
10088 int v;
10089 {
10090 set_pipestatus_from_exit (last_command_exit_value);
10091
10092 /* Cleanup code goes here. */
10093 expand_no_split_dollar_star = 0; /* XXX */
10094 expanding_redir = 0;
10095 assigning_in_environment = 0;
10096
10097 if (parse_and_execute_level == 0)
10098 top_level_cleanup (); /* from sig.c */
10099
10100 jump_to_top_level (v);
10101 }
10102
10103 /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
10104 ELIST, and set ELIST to the new list. */
10105 #define PREPEND_LIST(nlist, elist) \
10106 do { nlist->next = elist; elist = nlist; } while (0)
10107
10108 /* Separate out any initial variable assignments from TLIST. If set -k has
10109 been executed, remove all assignment statements from TLIST. Initial
10110 variable assignments and other environment assignments are placed
10111 on SUBST_ASSIGN_VARLIST. */
10112 static WORD_LIST *
10113 separate_out_assignments (tlist)
10114 WORD_LIST *tlist;
10115 {
10116 register WORD_LIST *vp, *lp;
10117
10118 if (tlist == 0)
10119 return ((WORD_LIST *)NULL);
10120
10121 if (subst_assign_varlist)
10122 dispose_words (subst_assign_varlist); /* Clean up after previous error */
10123
10124 subst_assign_varlist = (WORD_LIST *)NULL;
10125 vp = lp = tlist;
10126
10127 /* Separate out variable assignments at the start of the command.
10128 Loop invariant: vp->next == lp
10129 Loop postcondition:
10130 lp = list of words left after assignment statements skipped
10131 tlist = original list of words
10132 */
10133 while (lp && (lp->word->flags & W_ASSIGNMENT))
10134 {
10135 vp = lp;
10136 lp = lp->next;
10137 }
10138
10139 /* If lp != tlist, we have some initial assignment statements.
10140 We make SUBST_ASSIGN_VARLIST point to the list of assignment
10141 words and TLIST point to the remaining words. */
10142 if (lp != tlist)
10143 {
10144 subst_assign_varlist = tlist;
10145 /* ASSERT(vp->next == lp); */
10146 vp->next = (WORD_LIST *)NULL; /* terminate variable list */
10147 tlist = lp; /* remainder of word list */
10148 }
10149
10150 /* vp == end of variable list */
10151 /* tlist == remainder of original word list without variable assignments */
10152 if (!tlist)
10153 /* All the words in tlist were assignment statements */
10154 return ((WORD_LIST *)NULL);
10155
10156 /* ASSERT(tlist != NULL); */
10157 /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
10158
10159 /* If the -k option is in effect, we need to go through the remaining
10160 words, separate out the assignment words, and place them on
10161 SUBST_ASSIGN_VARLIST. */
10162 if (place_keywords_in_env)
10163 {
10164 WORD_LIST *tp; /* tp == running pointer into tlist */
10165
10166 tp = tlist;
10167 lp = tlist->next;
10168
10169 /* Loop Invariant: tp->next == lp */
10170 /* Loop postcondition: tlist == word list without assignment statements */
10171 while (lp)
10172 {
10173 if (lp->word->flags & W_ASSIGNMENT)
10174 {
10175 /* Found an assignment statement, add this word to end of
10176 subst_assign_varlist (vp). */
10177 if (!subst_assign_varlist)
10178 subst_assign_varlist = vp = lp;
10179 else
10180 {
10181 vp->next = lp;
10182 vp = lp;
10183 }
10184
10185 /* Remove the word pointed to by LP from TLIST. */
10186 tp->next = lp->next;
10187 /* ASSERT(vp == lp); */
10188 lp->next = (WORD_LIST *)NULL;
10189 lp = tp->next;
10190 }
10191 else
10192 {
10193 tp = lp;
10194 lp = lp->next;
10195 }
10196 }
10197 }
10198 return (tlist);
10199 }
10200
10201 #define WEXP_VARASSIGN 0x001
10202 #define WEXP_BRACEEXP 0x002
10203 #define WEXP_TILDEEXP 0x004
10204 #define WEXP_PARAMEXP 0x008
10205 #define WEXP_PATHEXP 0x010
10206
10207 /* All of the expansions, including variable assignments at the start of
10208 the list. */
10209 #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
10210
10211 /* All of the expansions except variable assignments at the start of
10212 the list. */
10213 #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
10214
10215 /* All of the `shell expansions': brace expansion, tilde expansion, parameter
10216 expansion, command substitution, arithmetic expansion, word splitting, and
10217 quote removal. */
10218 #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
10219
10220 /* Take the list of words in LIST and do the various substitutions. Return
10221 a new list of words which is the expanded list, and without things like
10222 variable assignments. */
10223
10224 WORD_LIST *
10225 expand_words (list)
10226 WORD_LIST *list;
10227 {
10228 return (expand_word_list_internal (list, WEXP_ALL));
10229 }
10230
10231 /* Same as expand_words (), but doesn't hack variable or environment
10232 variables. */
10233 WORD_LIST *
10234 expand_words_no_vars (list)
10235 WORD_LIST *list;
10236 {
10237 return (expand_word_list_internal (list, WEXP_NOVARS));
10238 }
10239
10240 WORD_LIST *
10241 expand_words_shellexp (list)
10242 WORD_LIST *list;
10243 {
10244 return (expand_word_list_internal (list, WEXP_SHELLEXP));
10245 }
10246
10247 static WORD_LIST *
10248 glob_expand_word_list (tlist, eflags)
10249 WORD_LIST *tlist;
10250 int eflags;
10251 {
10252 char **glob_array, *temp_string;
10253 register int glob_index;
10254 WORD_LIST *glob_list, *output_list, *disposables, *next;
10255 WORD_DESC *tword;
10256
10257 output_list = disposables = (WORD_LIST *)NULL;
10258 glob_array = (char **)NULL;
10259 while (tlist)
10260 {
10261 /* For each word, either globbing is attempted or the word is
10262 added to orig_list. If globbing succeeds, the results are
10263 added to orig_list and the word (tlist) is added to the list
10264 of disposable words. If globbing fails and failed glob
10265 expansions are left unchanged (the shell default), the
10266 original word is added to orig_list. If globbing fails and
10267 failed glob expansions are removed, the original word is
10268 added to the list of disposable words. orig_list ends up
10269 in reverse order and requires a call to REVERSE_LIST to
10270 be set right. After all words are examined, the disposable
10271 words are freed. */
10272 next = tlist->next;
10273
10274 /* If the word isn't an assignment and contains an unquoted
10275 pattern matching character, then glob it. */
10276 if ((tlist->word->flags & W_NOGLOB) == 0 &&
10277 unquoted_glob_pattern_p (tlist->word->word))
10278 {
10279 glob_array = shell_glob_filename (tlist->word->word);
10280
10281 /* Handle error cases.
10282 I don't think we should report errors like "No such file
10283 or directory". However, I would like to report errors
10284 like "Read failed". */
10285
10286 if (glob_array == 0 || GLOB_FAILED (glob_array))
10287 {
10288 glob_array = (char **)xmalloc (sizeof (char *));
10289 glob_array[0] = (char *)NULL;
10290 }
10291
10292 /* Dequote the current word in case we have to use it. */
10293 if (glob_array[0] == NULL)
10294 {
10295 temp_string = dequote_string (tlist->word->word);
10296 free (tlist->word->word);
10297 tlist->word->word = temp_string;
10298 }
10299
10300 /* Make the array into a word list. */
10301 glob_list = (WORD_LIST *)NULL;
10302 for (glob_index = 0; glob_array[glob_index]; glob_index++)
10303 {
10304 tword = make_bare_word (glob_array[glob_index]);
10305 glob_list = make_word_list (tword, glob_list);
10306 }
10307
10308 if (glob_list)
10309 {
10310 output_list = (WORD_LIST *)list_append (glob_list, output_list);
10311 PREPEND_LIST (tlist, disposables);
10312 }
10313 else if (fail_glob_expansion != 0)
10314 {
10315 last_command_exit_value = EXECUTION_FAILURE;
10316 report_error (_("no match: %s"), tlist->word->word);
10317 exp_jump_to_top_level (DISCARD);
10318 }
10319 else if (allow_null_glob_expansion == 0)
10320 {
10321 /* Failed glob expressions are left unchanged. */
10322 PREPEND_LIST (tlist, output_list);
10323 }
10324 else
10325 {
10326 /* Failed glob expressions are removed. */
10327 PREPEND_LIST (tlist, disposables);
10328 }
10329 }
10330 else
10331 {
10332 /* Dequote the string. */
10333 temp_string = dequote_string (tlist->word->word);
10334 free (tlist->word->word);
10335 tlist->word->word = temp_string;
10336 PREPEND_LIST (tlist, output_list);
10337 }
10338
10339 strvec_dispose (glob_array);
10340 glob_array = (char **)NULL;
10341
10342 tlist = next;
10343 }
10344
10345 if (disposables)
10346 dispose_words (disposables);
10347
10348 if (output_list)
10349 output_list = REVERSE_LIST (output_list, WORD_LIST *);
10350
10351 return (output_list);
10352 }
10353
10354 #if defined (BRACE_EXPANSION)
10355 static WORD_LIST *
10356 brace_expand_word_list (tlist, eflags)
10357 WORD_LIST *tlist;
10358 int eflags;
10359 {
10360 register char **expansions;
10361 char *temp_string;
10362 WORD_LIST *disposables, *output_list, *next;
10363 WORD_DESC *w;
10364 int eindex;
10365
10366 for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
10367 {
10368 next = tlist->next;
10369
10370 if (tlist->word->flags & W_NOBRACE)
10371 {
10372 /*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/
10373 PREPEND_LIST (tlist, output_list);
10374 continue;
10375 }
10376
10377 if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
10378 {
10379 /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
10380 PREPEND_LIST (tlist, output_list);
10381 continue;
10382 }
10383
10384 /* Only do brace expansion if the word has a brace character. If
10385 not, just add the word list element to BRACES and continue. In
10386 the common case, at least when running shell scripts, this will
10387 degenerate to a bunch of calls to `mbschr', and then what is
10388 basically a reversal of TLIST into BRACES, which is corrected
10389 by a call to REVERSE_LIST () on BRACES when the end of TLIST
10390 is reached. */
10391 if (mbschr (tlist->word->word, LBRACE))
10392 {
10393 expansions = brace_expand (tlist->word->word);
10394
10395 for (eindex = 0; temp_string = expansions[eindex]; eindex++)
10396 {
10397 w = alloc_word_desc ();
10398 w->word = temp_string;
10399
10400 /* If brace expansion didn't change the word, preserve
10401 the flags. We may want to preserve the flags
10402 unconditionally someday -- XXX */
10403 if (STREQ (temp_string, tlist->word->word))
10404 w->flags = tlist->word->flags;
10405 else
10406 w = make_word_flags (w, temp_string);
10407
10408 output_list = make_word_list (w, output_list);
10409 }
10410 free (expansions);
10411
10412 /* Add TLIST to the list of words to be freed after brace
10413 expansion has been performed. */
10414 PREPEND_LIST (tlist, disposables);
10415 }
10416 else
10417 PREPEND_LIST (tlist, output_list);
10418 }
10419
10420 if (disposables)
10421 dispose_words (disposables);
10422
10423 if (output_list)
10424 output_list = REVERSE_LIST (output_list, WORD_LIST *);
10425
10426 return (output_list);
10427 }
10428 #endif
10429
10430 #if defined (ARRAY_VARS)
10431 /* Take WORD, a compound associative array assignment, and internally run
10432 'declare -A w', where W is the variable name portion of WORD. */
10433 static int
10434 make_internal_declare (word, option, cmd)
10435 char *word;
10436 char *option;
10437 char *cmd;
10438 {
10439 int t, r;
10440 WORD_LIST *wl;
10441 WORD_DESC *w;
10442
10443 w = make_word (word);
10444
10445 t = assignment (w->word, 0);
10446 if (w->word[t] == '=')
10447 {
10448 w->word[t] = '\0';
10449 if (w->word[t - 1] == '+') /* cut off any append op */
10450 w->word[t - 1] = '\0';
10451 }
10452
10453 wl = make_word_list (w, (WORD_LIST *)NULL);
10454 wl = make_word_list (make_word (option), wl);
10455
10456 r = declare_builtin (wl);
10457
10458 dispose_words (wl);
10459 return r;
10460 }
10461 #endif
10462
10463 static WORD_LIST *
10464 shell_expand_word_list (tlist, eflags)
10465 WORD_LIST *tlist;
10466 int eflags;
10467 {
10468 WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list, *wcmd;
10469 int expanded_something, has_dollar_at;
10470 char *temp_string;
10471
10472 /* We do tilde expansion all the time. This is what 1003.2 says. */
10473 new_list = (WORD_LIST *)NULL;
10474 for (wcmd = tlist; wcmd; wcmd = wcmd->next)
10475 if (wcmd->word->flags & W_ASSNBLTIN)
10476 break;
10477
10478 for (orig_list = tlist; tlist; tlist = next)
10479 {
10480 temp_string = tlist->word->word;
10481
10482 next = tlist->next;
10483
10484 #if defined (ARRAY_VARS)
10485 /* If this is a compound array assignment to a builtin that accepts
10486 such assignments (e.g., `declare'), take the assignment and perform
10487 it separately, handling the semantics of declarations inside shell
10488 functions. This avoids the double-evaluation of such arguments,
10489 because `declare' does some evaluation of compound assignments on
10490 its own. */
10491 if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
10492 {
10493 int t;
10494 char opts[16], opti;
10495
10496 opti = 0;
10497 if (tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL|W_ASSIGNARRAY))
10498 opts[opti++] = '-';
10499
10500 if ((tlist->word->flags & (W_ASSIGNASSOC|W_ASSNGLOBAL)) == (W_ASSIGNASSOC|W_ASSNGLOBAL))
10501 {
10502 opts[opti++] = 'g';
10503 opts[opti++] = 'A';
10504 }
10505 else if (tlist->word->flags & W_ASSIGNASSOC)
10506 opts[opti++] = 'A';
10507 else if ((tlist->word->flags & (W_ASSIGNARRAY|W_ASSNGLOBAL)) == (W_ASSIGNARRAY|W_ASSNGLOBAL))
10508 {
10509 opts[opti++] = 'g';
10510 opts[opti++] = 'a';
10511 }
10512 else if (tlist->word->flags & W_ASSIGNARRAY)
10513 opts[opti++] = 'a';
10514 else if (tlist->word->flags & W_ASSNGLOBAL)
10515 opts[opti++] = 'g';
10516
10517 /* If we have special handling note the integer attribute and others
10518 that transform the value upon assignment. What we do is take all
10519 of the option arguments and scan through them looking for options
10520 that cause such transformations, and add them to the `opts' array. */
10521 /* if (opti > 0) */
10522 {
10523 char omap[128];
10524 int oind;
10525 WORD_LIST *l;
10526
10527 memset (omap, '\0', sizeof (omap));
10528 for (l = orig_list->next; l != tlist; l = l->next)
10529 {
10530 if (l->word->word[0] != '-')
10531 break; /* non-option argument */
10532 if (l->word->word[0] == '-' && l->word->word[1] == '-' && l->word->word[2] == 0)
10533 break; /* -- signals end of options */
10534 for (oind = 1; l->word->word[oind]; oind++)
10535 switch (l->word->word[oind])
10536 {
10537 case 'i':
10538 case 'l':
10539 case 'u':
10540 case 'c':
10541 omap[l->word->word[oind]] = 1;
10542 if (opti == 0)
10543 opts[opti++] = '-';
10544 break;
10545 default:
10546 break;
10547 }
10548 }
10549
10550 for (oind = 0; oind < sizeof (omap); oind++)
10551 if (omap[oind])
10552 opts[opti++] = oind;
10553 }
10554
10555 opts[opti] = '\0';
10556 if (opti > 0)
10557 {
10558 t = make_internal_declare (tlist->word->word, opts, wcmd ? wcmd->word->word : (char *)0);
10559 if (t != EXECUTION_SUCCESS)
10560 {
10561 last_command_exit_value = t;
10562 exp_jump_to_top_level (DISCARD);
10563 }
10564 }
10565
10566 t = do_word_assignment (tlist->word, 0);
10567 if (t == 0)
10568 {
10569 last_command_exit_value = EXECUTION_FAILURE;
10570 exp_jump_to_top_level (DISCARD);
10571 }
10572
10573 /* Now transform the word as ksh93 appears to do and go on */
10574 t = assignment (tlist->word->word, 0);
10575 tlist->word->word[t] = '\0';
10576 if (tlist->word->word[t - 1] == '+')
10577 tlist->word->word[t - 1] = '\0'; /* cut off append op */
10578 tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC|W_ASSIGNARRAY);
10579 }
10580 #endif
10581
10582 expanded_something = 0;
10583 expanded = expand_word_internal
10584 (tlist->word, 0, 0, &has_dollar_at, &expanded_something);
10585
10586 if (expanded == &expand_word_error || expanded == &expand_word_fatal)
10587 {
10588 /* By convention, each time this error is returned,
10589 tlist->word->word has already been freed. */
10590 tlist->word->word = (char *)NULL;
10591
10592 /* Dispose our copy of the original list. */
10593 dispose_words (orig_list);
10594 /* Dispose the new list we're building. */
10595 dispose_words (new_list);
10596
10597 last_command_exit_value = EXECUTION_FAILURE;
10598 if (expanded == &expand_word_error)
10599 exp_jump_to_top_level (DISCARD);
10600 else
10601 exp_jump_to_top_level (FORCE_EOF);
10602 }
10603
10604 /* Don't split words marked W_NOSPLIT. */
10605 if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
10606 {
10607 temp_list = word_list_split (expanded);
10608 dispose_words (expanded);
10609 }
10610 else
10611 {
10612 /* If no parameter expansion, command substitution, process
10613 substitution, or arithmetic substitution took place, then
10614 do not do word splitting. We still have to remove quoted
10615 null characters from the result. */
10616 word_list_remove_quoted_nulls (expanded);
10617 temp_list = expanded;
10618 }
10619
10620 expanded = REVERSE_LIST (temp_list, WORD_LIST *);
10621 new_list = (WORD_LIST *)list_append (expanded, new_list);
10622 }
10623
10624 if (orig_list)
10625 dispose_words (orig_list);
10626
10627 if (new_list)
10628 new_list = REVERSE_LIST (new_list, WORD_LIST *);
10629
10630 return (new_list);
10631 }
10632
10633 /* The workhorse for expand_words () and expand_words_no_vars ().
10634 First arg is LIST, a WORD_LIST of words.
10635 Second arg EFLAGS is a flags word controlling which expansions are
10636 performed.
10637
10638 This does all of the substitutions: brace expansion, tilde expansion,
10639 parameter expansion, command substitution, arithmetic expansion,
10640 process substitution, word splitting, and pathname expansion, according
10641 to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
10642 set, or for which no expansion is done, do not undergo word splitting.
10643 Words with the W_NOGLOB bit set do not undergo pathname expansion; words
10644 with W_NOBRACE set do not undergo brace expansion (see
10645 brace_expand_word_list above). */
10646 static WORD_LIST *
10647 expand_word_list_internal (list, eflags)
10648 WORD_LIST *list;
10649 int eflags;
10650 {
10651 WORD_LIST *new_list, *temp_list;
10652 int tint;
10653 char *savecmd;
10654
10655 tempenv_assign_error = 0;
10656 if (list == 0)
10657 return ((WORD_LIST *)NULL);
10658
10659 garglist = new_list = copy_word_list (list);
10660 if (eflags & WEXP_VARASSIGN)
10661 {
10662 garglist = new_list = separate_out_assignments (new_list);
10663 if (new_list == 0)
10664 {
10665 if (subst_assign_varlist)
10666 {
10667 /* All the words were variable assignments, so they are placed
10668 into the shell's environment. */
10669 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
10670 {
10671 savecmd = this_command_name;
10672 this_command_name = (char *)NULL; /* no arithmetic errors */
10673 tint = do_word_assignment (temp_list->word, 0);
10674 this_command_name = savecmd;
10675 /* Variable assignment errors in non-interactive shells
10676 running in Posix.2 mode cause the shell to exit. */
10677 if (tint == 0)
10678 {
10679 last_command_exit_value = EXECUTION_FAILURE;
10680 if (interactive_shell == 0 && posixly_correct)
10681 exp_jump_to_top_level (FORCE_EOF);
10682 else
10683 exp_jump_to_top_level (DISCARD);
10684 }
10685 }
10686 dispose_words (subst_assign_varlist);
10687 subst_assign_varlist = (WORD_LIST *)NULL;
10688 }
10689 return ((WORD_LIST *)NULL);
10690 }
10691 }
10692
10693 /* Begin expanding the words that remain. The expansions take place on
10694 things that aren't really variable assignments. */
10695
10696 #if defined (BRACE_EXPANSION)
10697 /* Do brace expansion on this word if there are any brace characters
10698 in the string. */
10699 if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
10700 new_list = brace_expand_word_list (new_list, eflags);
10701 #endif /* BRACE_EXPANSION */
10702
10703 /* Perform the `normal' shell expansions: tilde expansion, parameter and
10704 variable substitution, command substitution, arithmetic expansion,
10705 and word splitting. */
10706 new_list = shell_expand_word_list (new_list, eflags);
10707
10708 /* Okay, we're almost done. Now let's just do some filename
10709 globbing. */
10710 if (new_list)
10711 {
10712 if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
10713 /* Glob expand the word list unless globbing has been disabled. */
10714 new_list = glob_expand_word_list (new_list, eflags);
10715 else
10716 /* Dequote the words, because we're not performing globbing. */
10717 new_list = dequote_list (new_list);
10718 }
10719
10720 if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
10721 {
10722 sh_wassign_func_t *assign_func;
10723 int is_special_builtin, is_builtin_or_func;
10724
10725 /* If the remainder of the words expand to nothing, Posix.2 requires
10726 that the variable and environment assignments affect the shell's
10727 environment. */
10728 assign_func = new_list ? assign_in_env : do_word_assignment;
10729 tempenv_assign_error = 0;
10730
10731 is_builtin_or_func = (new_list && new_list->word && (find_shell_builtin (new_list->word->word) || find_function (new_list->word->word)));
10732 /* Posix says that special builtins exit if a variable assignment error
10733 occurs in an assignment preceding it. */
10734 is_special_builtin = (posixly_correct && new_list && new_list->word && find_special_builtin (new_list->word->word));
10735
10736 for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
10737 {
10738 savecmd = this_command_name;
10739 this_command_name = (char *)NULL;
10740 assigning_in_environment = (assign_func == assign_in_env);
10741 tint = (*assign_func) (temp_list->word, is_builtin_or_func);
10742 assigning_in_environment = 0;
10743 this_command_name = savecmd;
10744 /* Variable assignment errors in non-interactive shells running
10745 in Posix.2 mode cause the shell to exit. */
10746 if (tint == 0)
10747 {
10748 if (assign_func == do_word_assignment)
10749 {
10750 last_command_exit_value = EXECUTION_FAILURE;
10751 if (interactive_shell == 0 && posixly_correct && is_special_builtin)
10752 exp_jump_to_top_level (FORCE_EOF);
10753 else
10754 exp_jump_to_top_level (DISCARD);
10755 }
10756 else
10757 tempenv_assign_error++;
10758 }
10759 }
10760
10761 dispose_words (subst_assign_varlist);
10762 subst_assign_varlist = (WORD_LIST *)NULL;
10763 }
10764
10765 return (new_list);
10766 }