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