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