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